Merge branch 'emacs-24' of git.sv.gnu.org:/srv/git/emacs into emacs-24
[emacs.git] / lisp / play / gamegrid.el
blob4f2be48746357cbb7ebe35a4fe90f2f10e1f09ca
1 ;;; gamegrid.el --- library for implementing grid-based games on Emacs
3 ;; Copyright (C) 1997-1998, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Glynn Clements <glynn@sensei.co.uk>
6 ;; Version: 1.02
7 ;; Created: 1997-08-13
8 ;; Keywords: games
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 ;; ;;;;;;;;;;;;; buffer-local variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 (defvar gamegrid-use-glyphs t
32 "Non-nil means use glyphs when available.")
34 (defvar gamegrid-use-color t
35 "Non-nil means use color when available.")
37 (defvar gamegrid-font "-*-courier-medium-r-*-*-*-140-100-75-*-*-iso8859-*"
38 "Name of the font used in X mode.")
40 (defvar gamegrid-face nil
41 "Indicates the face to use as a default.")
42 (make-variable-buffer-local 'gamegrid-face)
44 (defvar gamegrid-display-options nil)
46 (defvar gamegrid-buffer-width 0)
47 (defvar gamegrid-buffer-height 0)
48 (defvar gamegrid-blank 0)
50 (defvar gamegrid-timer nil)
52 (defvar gamegrid-display-mode nil)
54 (defvar gamegrid-display-table)
56 (defvar gamegrid-face-table nil)
58 (defvar gamegrid-buffer-start 1)
60 (defvar gamegrid-score-file-length 50
61 "Number of high scores to keep.")
63 (defvar gamegrid-user-score-file-directory
64 (locate-user-emacs-file "games/")
65 "A directory for game scores which can't be shared.
66 If Emacs was built without support for shared game scores, then this
67 directory will be used.")
69 (make-variable-buffer-local 'gamegrid-use-glyphs)
70 (make-variable-buffer-local 'gamegrid-use-color)
71 (make-variable-buffer-local 'gamegrid-font)
72 (make-variable-buffer-local 'gamegrid-display-options)
73 (make-variable-buffer-local 'gamegrid-buffer-width)
74 (make-variable-buffer-local 'gamegrid-buffer-height)
75 (make-variable-buffer-local 'gamegrid-blank)
76 (make-variable-buffer-local 'gamegrid-timer)
77 (make-variable-buffer-local 'gamegrid-display-mode)
78 (make-variable-buffer-local 'gamegrid-display-table)
79 (make-variable-buffer-local 'gamegrid-face-table)
80 (make-variable-buffer-local 'gamegrid-buffer-start)
81 (make-variable-buffer-local 'gamegrid-score-file-length)
83 ;; ;;;;;;;;;;;;; global variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85 (defvar gamegrid-grid-x-face nil)
86 (defvar gamegrid-mono-x-face nil)
87 (defvar gamegrid-mono-tty-face nil)
89 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 (defconst gamegrid-glyph-height 16)
93 (defconst gamegrid-xpm "\
94 /* XPM */
95 static char *noname[] = {
96 /* width height ncolors chars_per_pixel */
97 \"16 16 3 1\",
98 /* colors */
99 \"+ s col1\",
100 \". s col2\",
101 \"- s col3\",
102 /* pixels */
103 \"---------------+\",
104 \"--------------++\",
105 \"--............++\",
106 \"--............++\",
107 \"--............++\",
108 \"--............++\",
109 \"--............++\",
110 \"--............++\",
111 \"--............++\",
112 \"--............++\",
113 \"--............++\",
114 \"--............++\",
115 \"--............++\",
116 \"--............++\",
117 \"-+++++++++++++++\",
118 \"++++++++++++++++\"
121 "XPM format image used for each square")
123 (defvar gamegrid-xbm "\
124 /* gamegrid XBM */
125 #define gamegrid_width 16
126 #define gamegrid_height 16
127 static unsigned char gamegrid_bits[] = {
128 0xff, 0xff, 0xff, 0x7f, 0xff, 0x3f, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a,
129 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a,
130 0x57, 0x15, 0x07, 0x00, 0x03, 0x00, 0x01, 0x00 };"
131 "XBM format image used for each square.")
133 ;; ;;;;;;;;;;;;;;;; miscellaneous functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 (defsubst gamegrid-characterp (arg)
136 (if (fboundp 'characterp)
137 (characterp arg)
138 (integerp arg)))
140 (defsubst gamegrid-event-x (event)
141 (if (fboundp 'event-x)
142 (event-x event)
143 (car (posn-col-row (event-end event)))))
145 (defsubst gamegrid-event-y (event)
146 (if (fboundp 'event-y)
147 (event-y event)
148 (cdr (posn-col-row (event-end event)))))
150 ;; ;;;;;;;;;;;;; display functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 (defun gamegrid-color (color shade)
153 (let* ((v (floor (* shade 255)))
154 (r (* v (aref color 0)))
155 (g (* v (aref color 1)))
156 (b (* v (aref color 2))))
157 (format "#%02x%02x%02x" r g b)))
159 (defun gamegrid-set-font (face)
160 (if gamegrid-font
161 (condition-case nil
162 (set-face-font face gamegrid-font)
163 (error nil))))
165 (defun gamegrid-setup-face (face color)
166 (set-face-foreground face color)
167 (set-face-background face color)
168 (gamegrid-set-font face)
169 (condition-case nil
170 (set-face-background-pixmap face [nothing]);; XEmacs
171 (error nil))
172 (condition-case nil
173 (set-face-background-pixmap face nil);; Emacs
174 (error nil)))
176 (defun gamegrid-make-mono-tty-face ()
177 (let ((face (make-face 'gamegrid-mono-tty-face)))
178 (set-face-inverse-video face t)
179 face))
181 (defun gamegrid-make-color-tty-face (color)
182 (let* ((color-str (if (symbolp color) (symbol-value color) color))
183 (name (intern (format "gamegrid-color-tty-face-%s" color-str)))
184 (face (make-face name)))
185 (gamegrid-setup-face face color-str)
186 face))
188 (defun gamegrid-make-grid-x-face ()
189 (let ((face (make-face 'gamegrid-x-border-face)))
190 (gamegrid-set-font face)
191 face))
193 (defun gamegrid-make-mono-x-face ()
194 (let ((face (make-face 'gamegrid-mono-x-face))
195 (color (face-foreground 'default)))
196 (if (null color)
197 (setq color
198 (cdr-safe (assq 'foreground-color (frame-parameters)))))
199 (gamegrid-setup-face face color)
200 face))
202 (defun gamegrid-make-color-x-face (color)
203 (let* ((hex (gamegrid-color color 1.0))
204 (name (intern (format "gamegrid-color-x-face-%s" hex)))
205 (face (make-face name)))
206 (gamegrid-setup-face face hex)
207 face))
209 (defun gamegrid-make-face (data-spec-list color-spec-list)
210 (let ((data (gamegrid-match-spec-list data-spec-list))
211 (color (gamegrid-match-spec-list color-spec-list)))
212 (pcase data
213 (`color-x
214 (gamegrid-make-color-x-face color))
215 (`grid-x
216 (unless gamegrid-grid-x-face
217 (setq gamegrid-grid-x-face (gamegrid-make-grid-x-face)))
218 gamegrid-grid-x-face)
219 (`mono-x
220 (unless gamegrid-mono-x-face
221 (setq gamegrid-mono-x-face (gamegrid-make-mono-x-face)))
222 gamegrid-mono-x-face)
223 (`color-tty
224 (gamegrid-make-color-tty-face color))
225 (`mono-tty
226 (unless gamegrid-mono-tty-face
227 (setq gamegrid-mono-tty-face (gamegrid-make-mono-tty-face)))
228 gamegrid-mono-tty-face))))
230 (defun gamegrid-colorize-glyph (color)
231 (find-image `((:type xpm :data ,gamegrid-xpm
232 :ascent center
233 :color-symbols
234 (("col1" . ,(gamegrid-color color 0.6))
235 ("col2" . ,(gamegrid-color color 0.8))
236 ("col3" . ,(gamegrid-color color 1.0))))
237 (:type xbm :data ,gamegrid-xbm
238 :ascent center
239 :foreground ,(gamegrid-color color 1.0)
240 :background ,(gamegrid-color color 0.5)))))
242 (defun gamegrid-match-spec (spec)
243 (let ((locale (car spec))
244 (value (cadr spec)))
245 (and (or (eq locale t)
246 (and (listp locale)
247 (memq gamegrid-display-mode locale))
248 (and (symbolp locale)
249 (eq gamegrid-display-mode locale)))
250 value)))
252 (defun gamegrid-match-spec-list (spec-list)
253 (and spec-list
254 (or (gamegrid-match-spec (car spec-list))
255 (gamegrid-match-spec-list (cdr spec-list)))))
257 (defun gamegrid-make-glyph (data-spec-list color-spec-list)
258 (let ((data (gamegrid-match-spec-list data-spec-list))
259 (color (gamegrid-match-spec-list color-spec-list)))
260 (cond ((gamegrid-characterp data)
261 (vector data))
262 ((eq data 'colorize)
263 (gamegrid-colorize-glyph color))
264 ((listp data)
265 (find-image data)) ;untested!
266 ((vectorp data)
267 (gamegrid-make-image-from-vector data)))))
269 (defun gamegrid-make-image-from-vector (vect)
270 "Convert an XEmacs style \"glyph\" to an image-spec."
271 (let ((l (list 'image :type)))
272 (dotimes (n (length vect))
273 (setf l (nconc l (list (aref vect n)))))
274 (nconc l (list :ascent 'center))))
276 (defun gamegrid-display-type ()
277 (cond ((and gamegrid-use-glyphs
278 (display-images-p))
279 'glyph)
280 ((and gamegrid-use-color
281 (display-graphic-p)
282 (display-color-p))
283 'color-x)
284 ((display-graphic-p)
285 'mono-x)
286 ((and gamegrid-use-color
287 (display-color-p))
288 'color-tty)
289 ((display-multi-font-p) ;???
290 'mono-tty)
292 'emacs-tty)))
294 (defun gamegrid-set-display-table ()
295 (if (featurep 'xemacs)
296 (add-spec-to-specifier current-display-table
297 gamegrid-display-table
298 (current-buffer)
300 'remove-locale)
301 (setq buffer-display-table gamegrid-display-table)))
303 (declare-function image-size "image.c" (spec &optional pixels frame))
305 (defun gamegrid-setup-default-font ()
306 (setq gamegrid-face
307 (copy-face 'default
308 (intern (concat "gamegrid-face-" (buffer-name)))))
309 (when (eq gamegrid-display-mode 'glyph)
310 (let ((max-height nil))
311 (dotimes (c 256)
312 (let ((glyph (aref gamegrid-display-table c)))
313 (when (and (listp glyph) (eq (car glyph) 'image))
314 (let ((height (cdr (image-size glyph))))
315 (if (or (null max-height)
316 (< max-height height))
317 (setq max-height height))))))
318 (when (and max-height (< max-height 1))
319 (let ((default-font-height (face-attribute 'default :height))
320 (resy (/ (display-pixel-height) (/ (display-mm-height) 25.4)))
321 point-size pixel-size)
322 (setq point-size (/ (* (float default-font-height) max-height) 10)
323 pixel-size (floor (* resy (/ point-size 72.27)))
324 point-size (* (/ pixel-size resy) 72.27))
325 (face-spec-set gamegrid-face
326 `((t :height ,(floor (* point-size 10))))))))))
328 (defun gamegrid-initialize-display ()
329 (setq gamegrid-display-mode (gamegrid-display-type))
330 (setq gamegrid-display-table (make-display-table))
331 (setq gamegrid-face-table (make-vector 256 nil))
332 (dotimes (c 256)
333 (let* ((spec (aref gamegrid-display-options c))
334 (glyph (gamegrid-make-glyph (car spec) (nth 2 spec)))
335 (face (gamegrid-make-face (cadr spec) (nth 2 spec))))
336 (aset gamegrid-face-table c face)
337 (aset gamegrid-display-table c glyph)))
338 (gamegrid-setup-default-font)
339 (gamegrid-set-display-table)
340 (setq cursor-type nil))
343 (defun gamegrid-set-face (c)
344 (if (eq gamegrid-display-mode 'glyph)
345 (add-text-properties (1- (point)) (point)
346 (list 'display (list (aref gamegrid-display-table c))))
347 (put-text-property (1- (point))
348 (point)
349 'face
350 (aref gamegrid-face-table c))))
352 (defun gamegrid-cell-offset (x y)
353 (+ gamegrid-buffer-start
354 (* (1+ gamegrid-buffer-width) y)
357 ;; ;;;;;;;;;;;;;;;; grid functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
359 (defun gamegrid-get-cell (x y)
360 (char-after (gamegrid-cell-offset x y)))
362 (defun gamegrid-set-cell (x y c)
363 (save-excursion
364 (let ((buffer-read-only nil))
365 (goto-char (gamegrid-cell-offset x y))
366 (delete-char 1)
367 (insert-char c 1)
368 (gamegrid-set-face c))))
370 (defun gamegrid-init-buffer (width height blank)
371 (setq gamegrid-buffer-width width
372 gamegrid-buffer-height height)
373 (let ((line (concat
374 (make-string width blank)
375 "\n"))
376 (buffer-read-only nil))
377 (erase-buffer)
378 (setq gamegrid-buffer-start (point))
379 (dotimes (i height)
380 (insert line))
381 ;; Adjust the height of the default face to the height of the
382 ;; images. Unlike XEmacs, Emacs doesn't allow to make the default
383 ;; face buffer-local; so we do this with an overlay.
384 (when (eq gamegrid-display-mode 'glyph)
385 (overlay-put (make-overlay (point-min) (point-max))
386 'face gamegrid-face))
387 (goto-char (point-min))))
389 (defun gamegrid-init (options)
390 (setq buffer-read-only t
391 truncate-lines t
392 line-spacing 0
393 gamegrid-display-options options)
394 (buffer-disable-undo (current-buffer))
395 (gamegrid-initialize-display))
397 ;; ;;;;;;;;;;;;;;;; timer functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
399 (defun gamegrid-start-timer (period func)
400 (setq gamegrid-timer
401 (if (featurep 'xemacs)
402 (start-itimer "Gamegrid"
403 func
404 period
405 period
408 (current-buffer))
409 (run-with-timer period
410 period
411 func
412 (current-buffer)))))
414 (defun gamegrid-set-timer (delay)
415 (if gamegrid-timer
416 (if (fboundp 'set-itimer-restart)
417 (set-itimer-restart gamegrid-timer delay)
418 (timer-set-time gamegrid-timer
419 (list (aref gamegrid-timer 1)
420 (aref gamegrid-timer 2)
421 (aref gamegrid-timer 3))
422 delay))))
424 (defun gamegrid-kill-timer ()
425 (if gamegrid-timer
426 (if (featurep 'xemacs)
427 (delete-itimer gamegrid-timer)
428 (cancel-timer gamegrid-timer)))
429 (setq gamegrid-timer nil))
431 ;; ;;;;;;;;;;;;;;; high score functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
433 (defun gamegrid-add-score (file score)
434 "Add the current score to the high score file.
436 On POSIX systems there may be a shared game directory for all users in
437 which the scorefiles are kept. On such systems Emacs doesn't create
438 the score file FILE in this directory, if it doesn't already exist.
439 In this case Emacs searches for FILE in the directory specified by
440 `gamegrid-user-score-file-directory' and creates it there, if
441 necessary.
443 To add the score file for a game to the system wide shared game
444 directory, create the file with the shell command \"touch\" in this
445 directory and make sure that it is owned by the correct user and
446 group. You probably need special user privileges to do this.
448 On non-POSIX systems Emacs searches for FILE in the directory
449 specified by the variable `temporary-file-directory'. If necessary,
450 FILE is created there."
451 (pcase system-type
452 ((or `ms-dos `windows-nt)
453 (gamegrid-add-score-insecure file score))
455 (gamegrid-add-score-with-update-game-score file score))))
458 ;; On POSIX systems there are four cases to distinguish:
460 ;; 1. FILE is an absolute filename. Then it should be a file in
461 ;; temporary file directory. This is the way,
462 ;; `gamegrid-add-score' was supposed to be used in the past and
463 ;; is covered here for backward-compatibility.
465 ;; 2. The helper program "update-game-score" is setuid and the
466 ;; file FILE does already exist in a system wide shared game
467 ;; directory. This should be the normal case on POSIX systems,
468 ;; if the game was installed system wide. Use
469 ;; "update-game-score" to add the score to the file in the
470 ;; shared game directory.
472 ;; 3. "update-game-score" is setuid, but the file FILE does *not*
473 ;; exist in the system wide shared game directory. Use
474 ;; `gamegrid-add-score-insecure' to create--if necessary--and
475 ;; update FILE. This is for the case that a user has installed
476 ;; a game on her own.
478 ;; 4. "update-game-score" is not setuid. Use it to create/update
479 ;; FILE in the user's home directory. There is presumably no
480 ;; shared game directory.
482 (defvar gamegrid-shared-game-dir)
484 (defun gamegrid-add-score-with-update-game-score (file score)
485 (let ((gamegrid-shared-game-dir
486 (not (zerop (logand (file-modes
487 (expand-file-name "update-game-score"
488 exec-directory))
489 #o4000)))))
490 (cond ((file-name-absolute-p file)
491 (gamegrid-add-score-insecure file score))
492 ((and gamegrid-shared-game-dir
493 (file-exists-p (expand-file-name file shared-game-score-directory)))
494 ;; Use the setuid "update-game-score" program to update a
495 ;; system-wide score file.
496 (gamegrid-add-score-with-update-game-score-1 file
497 (expand-file-name file shared-game-score-directory) score))
498 ;; Else: Add the score to a score file in the user's home
499 ;; directory.
500 (gamegrid-shared-game-dir
501 ;; If `gamegrid-shared-game-dir' is non-nil, then
502 ;; "update-gamescore" program is setuid, so don't use it.
503 (unless (file-exists-p
504 (directory-file-name gamegrid-user-score-file-directory))
505 (make-directory gamegrid-user-score-file-directory t))
506 (gamegrid-add-score-insecure file score
507 gamegrid-user-score-file-directory))
509 (unless (file-exists-p
510 (directory-file-name gamegrid-user-score-file-directory))
511 (make-directory gamegrid-user-score-file-directory t))
512 (let ((f (expand-file-name file
513 gamegrid-user-score-file-directory)))
514 (unless (file-exists-p f)
515 (write-region "" nil f nil 'silent nil 'excl))
516 (gamegrid-add-score-with-update-game-score-1 file f score))))))
518 (defun gamegrid-add-score-with-update-game-score-1 (file target score)
519 (let ((default-directory "/")
520 (errbuf (generate-new-buffer " *update-game-score loss*"))
521 (marker-string (concat
522 (user-full-name)
523 " <"
524 (cond ((fboundp 'user-mail-address)
525 (user-mail-address))
526 ((boundp 'user-mail-address)
527 user-mail-address)
528 (t ""))
529 "> "
530 (current-time-string))))
531 ;; This can be called from a timer, so enable local quits.
532 (with-local-quit
533 (apply
534 'call-process
535 (append
536 (list
537 (expand-file-name "update-game-score" exec-directory)
538 nil errbuf nil
539 "-m" (int-to-string gamegrid-score-file-length)
540 "-d" (if gamegrid-shared-game-dir
541 (expand-file-name shared-game-score-directory)
542 (file-name-directory target))
543 file
544 (int-to-string score)
545 marker-string))))
546 (if (buffer-modified-p errbuf)
547 (progn
548 (display-buffer errbuf)
549 (error "Failed to update game score file"))
550 (kill-buffer errbuf))
551 (let ((buf (find-buffer-visiting target)))
552 (save-excursion
553 (if buf
554 (progn
555 (switch-to-buffer buf)
556 (revert-buffer nil t nil)
557 (display-buffer buf))
558 (find-file-read-only target))
559 (goto-char (point-min))
560 (search-forward (concat (int-to-string score)
561 " " (user-login-name) " "
562 marker-string) nil t)
563 (beginning-of-line)))))
565 (defun gamegrid-add-score-insecure (file score &optional directory)
566 (save-excursion
567 (setq file (expand-file-name file (or directory
568 temporary-file-directory)))
569 (find-file-other-window file)
570 (setq buffer-read-only nil)
571 (goto-char (point-max))
572 (insert (format "%05d\t%s\t%s <%s>\n"
573 score
574 (current-time-string)
575 (user-full-name)
576 (cond ((fboundp 'user-mail-address)
577 (user-mail-address))
578 ((boundp 'user-mail-address)
579 user-mail-address)
580 (t ""))))
581 (sort-fields 1 (point-min) (point-max))
582 (reverse-region (point-min) (point-max))
583 (goto-char (point-min))
584 (forward-line gamegrid-score-file-length)
585 (delete-region (point) (point-max))
586 (setq buffer-read-only t)
587 (save-buffer)))
590 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
592 (provide 'gamegrid)
594 ;;; gamegrid.el ends here