summaryrefslogtreecommitdiff
path: root/chess-common.el
blob: 24f0c14c8c88a0643e4f0b7b59320d5707580025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
;;; chess-common.el --- Handler functions common to xboard based engine protocols  -*- lexical-binding: t; -*-

;; Copyright (C) 2002, 2004, 2014  Free Software Foundation, Inc.

;; Author: John Wiegley <johnw@gnu.org>
;; Maintainer: Mario Lang <mlang@delysid.org>
;; Keywords: games

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Define handler functions that are common to the (relatively)
;; standard chess engine communication protocol:
;;
;;   http://www.tim-mann.org/xboard/engine-intf.html
;;
;; See chess-uci.el for code shared by engines based on the
;; Universal Chess Interface instead.

;;; Code:

(require 'chess)
(require 'chess-engine)
(require 'chess-message)

(defvar-local chess-common-engine-name nil)
(defvar-local chess-common-temp-files nil)

(defmacro chess-with-temp-file (&rest body)
  `(let ((file (make-temp-file "chess")))
     (with-temp-file file
       ,@body)
     (push file chess-common-temp-files)
     file))

(put 'chess-with-temp-file 'lisp-indent-function 1)

(chess-message-catalog 'english
  '((starting-engine	   . "Starting chess program '%s'...")
    (starting-engine-done  . "Starting chess program '%s'...done")
    (could-not-find-engine . "Cannot find %s executable; check `%s'")
    (draw-offer-declined   . "Your draw offer was declined")
    (illegal-move          . "Illegal move")
    (not-yet-implemented   . "This feature is not yet implemented")))

(defun chess-common-handler (game event &rest args)
  "Initialize the network chess engine."
  (cond
   ((eq event 'initialize)
    (let* ((name (car args))
	   (path (intern (concat "chess-" name "-path")))
	   (args (intern (concat "chess-" name "-args")))
	   proc)
      (chess-message 'starting-engine name)
      (unless (and (boundp path) (symbol-value path))
	(chess-error 'could-not-find-engine name path))
      (setq proc (start-process (concat "chess-" name)
				(current-buffer) (symbol-value path) 
                                (if (and (boundp args) (symbol-value args))
                                    (substitute-in-file-name (symbol-value args)) "")))
      (chess-message 'starting-engine-done name)
      proc))

   ((eq event 'ready)
    (chess-game-set-data game 'active t)
    (chess-game-run-hooks game 'check-autosave))

   ((eq event 'destroy)
    (let ((proc (get-buffer-process (current-buffer))))
      (if (and (processp proc)
	       (memq (process-status proc) '(run open)))
	  (chess-engine-send nil "quit\n")))

    (dolist (file chess-common-temp-files)
      (if (file-exists-p file)
	  (delete-file file)))
    (setq chess-common-temp-files nil))

   ((eq event 'pass)
    (chess-engine-send nil "go\n"))

   ((eq event 'draw)
    (chess-message 'draw-offer-declined))

   ((eq event 'resign)
    (chess-engine-send nil "resign\n")
    (chess-game-set-data game 'active nil))

   ((eq event 'new)
    (chess-engine-send nil "new\n")
    (chess-engine-set-position nil))

   ((eq event 'force)
    (chess-error 'not-yet-implemented))

   ((eq event 'undo)
    (dotimes (i (car args))
      (chess-engine-send nil "undo\n"))
    (if (= 1 (mod (car args) 2))
	(chess-engine-send nil "go\n"))

    ;; prevent us from handling the `undo' event which this triggers
    (let ((chess-engine-handling-event t))
      (chess-game-undo game (car args))))

   ((eq event 'flag-fell)
    (chess-game-set-data game 'active nil)
    (let ((chess-game-inhibit-events t))
      (chess-game-end game :flag-fell)))

   ((eq event 'move)
    (when (= 1 (chess-game-index game))
      (chess-game-set-tag game "White" chess-full-name)
      (chess-game-set-tag game "Black" chess-engine-opponent-name))

    (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
				   "\n"))
    (if (chess-game-over-p game)
	(chess-game-set-data game 'active nil)))))

(provide 'chess-common)

;;; chess-common.el ends here