]> code.delx.au - gnu-emacs-elpa/blob - packages/chess/chess-common.el
Initial import of chess.el.
[gnu-emacs-elpa] / packages / chess / chess-common.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Define handler functions that are common to the (relatively)
4 ;; standard chess engine communication protocol:
5 ;;
6 ;; http://www.tim-mann.org/xboard/engine-intf.html
7 ;;
8
9 (require 'chess)
10 (require 'chess-engine)
11 (require 'chess-message)
12
13 (defvar chess-common-engine-name nil)
14 (defvar chess-common-temp-files nil)
15 (make-variable-buffer-local 'chess-common-engine-name)
16 (make-variable-buffer-local 'chess-common-temp-files)
17
18 (defmacro chess-with-temp-file (&rest body)
19 `(let ((file (make-temp-file "chess")))
20 (with-temp-file file
21 ,@body)
22 (push file chess-common-temp-files)
23 file))
24
25 (put 'chess-with-temp-file 'lisp-indent-function 1)
26
27 (chess-message-catalog 'english
28 '((starting-engine . "Starting chess program '%s'...")
29 (starting-engine-done . "Starting chess program '%s'...done")
30 (could-not-find-engine . "Cannot find %s executable; check `%s'")
31 (draw-offer-declined . "Your draw offer was declined")
32 (illegal-move . "Illegal move")
33 (not-yet-implemented . "This feature is not yet implemented")))
34
35 (defun chess-common-handler (game event &rest args)
36 "Initialize the network chess engine."
37 (cond
38 ((eq event 'initialize)
39 (let* ((name (car args))
40 (path (intern (concat "chess-" name "-path")))
41 proc)
42 (chess-message 'starting-engine name)
43 (unless (and (boundp path) (symbol-value path))
44 (chess-error 'could-not-find-engine name path))
45 (setq proc (start-process (concat "chess-" name)
46 (current-buffer) (symbol-value path)))
47 (chess-message 'starting-engine-done name)
48 proc))
49
50 ((eq event 'ready)
51 (chess-game-set-data game 'active t)
52 (chess-game-run-hooks game 'check-autosave))
53
54 ((eq event 'destroy)
55 (let ((proc (get-buffer-process (current-buffer))))
56 (if (and (processp proc)
57 (memq (process-status proc) '(run open)))
58 (chess-engine-send nil "quit\n")))
59
60 (dolist (file chess-common-temp-files)
61 (if (file-exists-p file)
62 (delete-file file)))
63 (setq chess-common-temp-files nil))
64
65 ((eq event 'pass)
66 (chess-engine-send nil "go\n"))
67
68 ((eq event 'draw)
69 (chess-message 'draw-offer-declined))
70
71 ((eq event 'resign)
72 (chess-engine-send nil "resign\n")
73 (chess-game-set-data game 'active nil))
74
75 ((eq event 'new)
76 (chess-engine-send nil "new\n")
77 (chess-engine-set-position nil))
78
79 ((eq event 'force)
80 (chess-error 'not-yet-implemented))
81
82 ((eq event 'undo)
83 (dotimes (i (car args))
84 (chess-engine-send nil "undo\n"))
85 (if (= 1 (mod (car args) 2))
86 (chess-engine-send nil "go\n"))
87
88 ;; prevent us from handling the `undo' event which this triggers
89 (let ((chess-engine-handling-event t))
90 (chess-game-undo game (car args))))
91
92 ((eq event 'flag-fell)
93 (chess-game-set-data game 'active nil)
94 (let ((chess-game-inhibit-events t))
95 (chess-game-end game :flag-fell)))
96
97 ((eq event 'move)
98 (when (= 1 (chess-game-index game))
99 (chess-game-set-tag game "White" chess-full-name)
100 (chess-game-set-tag game "Black" chess-engine-opponent-name))
101
102 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
103 "\n"))
104 (if (chess-game-over-p game)
105 (chess-game-set-data game 'active nil)))))
106
107 (provide 'chess-common)
108
109 ;;; chess-common.el ends here