]> code.delx.au - gnu-emacs-elpa/blob - chess-common.el
(chess-display-mode-map): Bind DEL to `chess-input-shortcut-delete
[gnu-emacs-elpa] / chess-common.el
1 ;;; chess-common.el --- Handler functions common to xboard based engine protocols
2
3 ;; Copyright (C) 2002, 2004, 2014 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: Mario Lang <mlang@delysid.org>
7 ;; Keywords: games
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; Define handler functions that are common to the (relatively)
25 ;; standard chess engine communication protocol:
26 ;;
27 ;; http://www.tim-mann.org/xboard/engine-intf.html
28 ;;
29 ;; See chess-uci.el for code shared by engines based on the
30 ;; Universal Chess Interface instead.
31
32 ;;; Code:
33
34 (require 'chess)
35 (require 'chess-engine)
36 (require 'chess-message)
37
38 (defvar chess-common-engine-name nil)
39 (defvar chess-common-temp-files nil)
40 (make-variable-buffer-local 'chess-common-engine-name)
41 (make-variable-buffer-local 'chess-common-temp-files)
42
43 (defmacro chess-with-temp-file (&rest body)
44 `(let ((file (make-temp-file "chess")))
45 (with-temp-file file
46 ,@body)
47 (push file chess-common-temp-files)
48 file))
49
50 (put 'chess-with-temp-file 'lisp-indent-function 1)
51
52 (chess-message-catalog 'english
53 '((starting-engine . "Starting chess program '%s'...")
54 (starting-engine-done . "Starting chess program '%s'...done")
55 (could-not-find-engine . "Cannot find %s executable; check `%s'")
56 (draw-offer-declined . "Your draw offer was declined")
57 (illegal-move . "Illegal move")
58 (not-yet-implemented . "This feature is not yet implemented")))
59
60 (defun chess-common-handler (game event &rest args)
61 "Initialize the network chess engine."
62 (cond
63 ((eq event 'initialize)
64 (let* ((name (car args))
65 (path (intern (concat "chess-" name "-path")))
66 proc)
67 (chess-message 'starting-engine name)
68 (unless (and (boundp path) (symbol-value path))
69 (chess-error 'could-not-find-engine name path))
70 (setq proc (start-process (concat "chess-" name)
71 (current-buffer) (symbol-value path)))
72 (chess-message 'starting-engine-done name)
73 proc))
74
75 ((eq event 'ready)
76 (chess-game-set-data game 'active t)
77 (chess-game-run-hooks game 'check-autosave))
78
79 ((eq event 'destroy)
80 (let ((proc (get-buffer-process (current-buffer))))
81 (if (and (processp proc)
82 (memq (process-status proc) '(run open)))
83 (chess-engine-send nil "quit\n")))
84
85 (dolist (file chess-common-temp-files)
86 (if (file-exists-p file)
87 (delete-file file)))
88 (setq chess-common-temp-files nil))
89
90 ((eq event 'pass)
91 (chess-engine-send nil "go\n"))
92
93 ((eq event 'draw)
94 (chess-message 'draw-offer-declined))
95
96 ((eq event 'resign)
97 (chess-engine-send nil "resign\n")
98 (chess-game-set-data game 'active nil))
99
100 ((eq event 'new)
101 (chess-engine-send nil "new\n")
102 (chess-engine-set-position nil))
103
104 ((eq event 'force)
105 (chess-error 'not-yet-implemented))
106
107 ((eq event 'undo)
108 (dotimes (i (car args))
109 (chess-engine-send nil "undo\n"))
110 (if (= 1 (mod (car args) 2))
111 (chess-engine-send nil "go\n"))
112
113 ;; prevent us from handling the `undo' event which this triggers
114 (let ((chess-engine-handling-event t))
115 (chess-game-undo game (car args))))
116
117 ((eq event 'flag-fell)
118 (chess-game-set-data game 'active nil)
119 (let ((chess-game-inhibit-events t))
120 (chess-game-end game :flag-fell)))
121
122 ((eq event 'move)
123 (when (= 1 (chess-game-index game))
124 (chess-game-set-tag game "White" chess-full-name)
125 (chess-game-set-tag game "Black" chess-engine-opponent-name))
126
127 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
128 "\n"))
129 (if (chess-game-over-p game)
130 (chess-game-set-data game 'active nil)))))
131
132 (provide 'chess-common)
133
134 ;;; chess-common.el ends here