1 ;;; chess-common.el --- Handler functions common to xboard based engine protocols
3 ;; Copyright (C) 2002, 2004, 2014 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: Mario Lang <mlang@delysid.org>
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.
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.
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/>.
24 ;; Define handler functions that are common to the (relatively)
25 ;; standard chess engine communication protocol:
27 ;; http://www.tim-mann.org/xboard/engine-intf.html
29 ;; See chess-uci.el for code shared by engines based on the
30 ;; Universal Chess Interface instead.
35 (require 'chess-engine)
36 (require 'chess-message)
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)
43 (defmacro chess-with-temp-file (&rest body)
44 `(let ((file (make-temp-file "chess")))
47 (push file chess-common-temp-files)
50 (put 'chess-with-temp-file 'lisp-indent-function 1)
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")))
60 (defun chess-common-handler (game event &rest args)
61 "Initialize the network chess engine."
63 ((eq event 'initialize)
64 (let* ((name (car args))
65 (path (intern (concat "chess-" name "-path")))
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)
76 (chess-game-set-data game 'active t)
77 (chess-game-run-hooks game 'check-autosave))
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")))
85 (dolist (file chess-common-temp-files)
86 (if (file-exists-p file)
88 (setq chess-common-temp-files nil))
91 (chess-engine-send nil "go\n"))
94 (chess-message 'draw-offer-declined))
97 (chess-engine-send nil "resign\n")
98 (chess-game-set-data game 'active nil))
101 (chess-engine-send nil "new\n")
102 (chess-engine-set-position nil))
105 (chess-error 'not-yet-implemented))
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"))
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))))
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)))
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))
127 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
129 (if (chess-game-over-p game)
130 (chess-game-set-data game 'active nil)))))
132 (provide 'chess-common)
134 ;;; chess-common.el ends here