]> code.delx.au - gnu-emacs-elpa/blob - chess-ucb.el
Work on the manual.
[gnu-emacs-elpa] / chess-ucb.el
1 ;;; chess-ucb.el --- Engine interface to the Novag Universal Chess Board
2
3 ;; Copyright (C) 2002, 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 is free software; you can redistribute it and/or modify it under
10 ;; the terms of the GNU General Public License as published by the Free
11 ;; Software Foundation; either version 3, or (at your option) any later
12 ;; version.
13 ;;
14 ;; This is distributed in the hope that it will be useful, but WITHOUT
15 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 ;; for more details.
18 ;;
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; jww (2002-04-25): This code has not been tested yet, since I don't
25 ;; have access to a UCB. If anybody wants to donate one, or the money
26 ;; for one ($300), I would be happy to correct this module. :)
27
28 ;;; Code:
29
30 (require 'chess-common)
31
32 (defgroup chess-ucb nil
33 "Interface to the Novag Universal Chess Board."
34 :group 'chess-engine)
35
36 (defcustom chess-ucb-device "/dev/ttyS0"
37 "The serial device used to talk to the Novag UCB."
38 :type 'file
39 :group 'chess-ucb)
40
41 (defvar chess-ucb-handling-event nil)
42
43 (defvar chess-ucb-regexp-alist
44 (list
45 (cons "^M\\(..\\)\\(..\\)\\(/\\([QRNB]\\)\\)?\r\n"
46 (function
47 (lambda ()
48 (let ((move (concat (match-string 1)
49 "-"
50 (match-string 2)))
51 (promote (match-string 4)))
52 (if promote
53 (setq move (concat move "=" promote)))
54 (setq move (chess-engine-convert-algebraic move))
55 ;; I don't use the usual engine logic for this, since
56 ;; technically the UCB is just an input interface, not a
57 ;; true engine.
58 (let ((chess-ucb-handling-event t))
59 (chess-game-move (chess-engine-game nil) move))))))))
60
61 (defun chess-ucb-handler (game event &rest args)
62 (unless chess-ucb-handling-event
63 (cond
64 ((eq event 'initialize)
65 (when (file-exists-p chess-ucb-device)
66 ;; jww (2002-04-25): cat is not bidirectional, so I need
67 ;; something like "nc" that can talk with characters devices
68 ;; at 9600 8N1.
69 (setq chess-engine-process
70 (start-process "*chess-ucb*" (current-buffer)
71 (executable-find "cat") chess-ucb-device))
72 t))
73
74 ((memq event 'orient)
75 (chess-engine-send nil "N\r\n")
76 (chess-engine-set-position nil)
77
78 ;; jww (2002-04-25): What happens if we're orienting to a
79 ;; non-standard starting position? How do we inform the UCB of
80 ;; the new position? If it doesn't test move legality, I
81 ;; suppose we could just move all the pieces around one by
82 ;; one...
83 (unless (eq chess-starting-position (chess-engine-position nil))
84 nil))
85
86 ((eq event 'undo)
87 (dotimes (i (car args))
88 (chess-engine-send nil "T\r\n"))
89 ;; prevent us from handling the `undo' event which this triggers
90 (let ((chess-engine-handling-event t))
91 (chess-game-undo game (car args))))
92
93 ((eq event 'move)
94 (let ((move (chess-ply-to-algebraic (car args) t)))
95 (cond
96 ((chess-ply-keyword (car args) :en-passant)
97 (setq move (concat move "ep")))
98 ((chess-ply-keyword (car args) :castle)
99 (if (chess-pos-side-to-move (chess-ply-pos (car args)))
100 (setq move "e1-g1")
101 (setq move "e8-g8")))
102 ((chess-ply-keyword (car args) :long-castle)
103 (if (chess-pos-side-to-move (chess-ply-pos (car args)))
104 (setq move "e1-c1")
105 (setq move "e8-c8"))))
106 (chess-engine-send nil (format "M%s\r\n" move)))))))
107
108 (provide 'chess-ucb)
109
110 ;;; chess-ucb.el ends here