]> code.delx.au - gnu-emacs-elpa/blob - chess-ucb.el
Correctly indent `chess-with-current-buffer' in lisp-mode.
[gnu-emacs-elpa] / chess-ucb.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Engine interface to the Novag Universal Chess Board
4 ;;
5 ;; jww (2002-04-25): This code has not been tested yet, since I don't
6 ;; have access to a UCB. If anybody wants to donate one, or the money
7 ;; for one ($300), I would be happy to correct this module. :)
8 ;;
9
10 (require 'chess-common)
11
12 (defgroup chess-ucb nil
13 "Interface to the Novag Universal Chess Board."
14 :group 'chess-engine)
15
16 (defcustom chess-ucb-device "/dev/ttyS0"
17 "The serial device used to talk to the Novag UCB."
18 :type 'file
19 :group 'chess-ucb)
20
21 (defvar chess-ucb-handling-event nil)
22
23 (defvar chess-ucb-regexp-alist
24 (list
25 (cons "^M\\(..\\)\\(..\\)\\(/\\([QRNB]\\)\\)?\r\n"
26 (function
27 (lambda ()
28 (let ((move (concat (match-string 1)
29 "-"
30 (match-string 2)))
31 (promote (match-string 4)))
32 (if promote
33 (setq move (concat move "=" promote)))
34 (setq move (chess-engine-convert-algebraic move))
35 ;; I don't use the usual engine logic for this, since
36 ;; technically the UCB is just an input interface, not a
37 ;; true engine.
38 (let ((chess-ucb-handling-event t))
39 (chess-game-move (chess-engine-game nil) move))))))))
40
41 (defun chess-ucb-handler (game event &rest args)
42 (unless chess-ucb-handling-event
43 (cond
44 ((eq event 'initialize)
45 (when (file-exists-p chess-ucb-device)
46 ;; jww (2002-04-25): cat is not bidirectional, so I need
47 ;; something like "nc" that can talk with characters devices
48 ;; at 9600 8N1.
49 (setq chess-engine-process
50 (start-process "*chess-ucb*" (current-buffer)
51 (executable-find "cat") chess-ucb-device))
52 t))
53
54 ((memq event 'orient)
55 (chess-engine-send nil "N\r\n")
56 (chess-engine-set-position nil)
57
58 ;; jww (2002-04-25): What happens if we're orienting to a
59 ;; non-standard starting position? How do we inform the UCB of
60 ;; the new position? If it doesn't test move legality, I
61 ;; suppose we could just move all the pieces around one by
62 ;; one...
63 (unless (eq chess-starting-position (chess-engine-position nil))
64 nil))
65
66 ((eq event 'undo)
67 (dotimes (i (car args))
68 (chess-engine-send nil "T\r\n"))
69 ;; prevent us from handling the `undo' event which this triggers
70 (let ((chess-engine-handling-event t))
71 (chess-game-undo game (car args))))
72
73 ((eq event 'move)
74 (let ((move (chess-ply-to-algebraic (car args) t)))
75 (cond
76 ((chess-ply-keyword (car args) :en-passant)
77 (setq move (concat move "ep")))
78 ((chess-ply-keyword (car args) :castle)
79 (if (chess-pos-side-to-move (chess-ply-pos (car args)))
80 (setq move "e1-g1")
81 (setq move "e8-g8")))
82 ((chess-ply-keyword (car args) :long-castle)
83 (if (chess-pos-side-to-move (chess-ply-pos (car args)))
84 (setq move "e1-c1")
85 (setq move "e8-c8"))))
86 (chess-engine-send nil (format "M%s\r\n" move)))))))
87
88 (provide 'chess-ucb)
89
90 ;;; chess-ucb.el ends here