1 ;;; gamegrid.el --- library for implementing grid-based games on Emacs
3 ;; Copyright (C) 1997, 1998, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Glynn Clements <glynn@sensei.co.uk>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
35 ;; ;;;;;;;;;;;;; buffer-local variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 (defvar gamegrid-use-glyphs t
38 "Non-nil means use glyphs when available.")
40 (defvar gamegrid-use-color t
41 "Non-nil means use color when available.")
43 (defvar gamegrid-font
"-*-courier-medium-r-*-*-*-140-100-75-*-*-iso8859-*"
44 "Name of the font used in X mode.")
46 (defvar gamegrid-face nil
47 "Indicates the face to use as a default.")
48 (make-variable-buffer-local 'gamegrid-face
)
50 (defvar gamegrid-display-options nil
)
52 (defvar gamegrid-buffer-width
0)
53 (defvar gamegrid-buffer-height
0)
54 (defvar gamegrid-blank
0)
56 (defvar gamegrid-timer nil
)
58 (defvar gamegrid-display-mode nil
)
60 (defvar gamegrid-display-table
)
62 (defvar gamegrid-face-table nil
)
64 (defvar gamegrid-buffer-start
1)
66 (defvar gamegrid-score-file-length
50
67 "Number of high scores to keep")
69 (defvar gamegrid-user-score-file-directory
70 (concat user-emacs-directory
"games")
71 "A directory for game scores which can't be shared.
72 If Emacs was built without support for shared game scores, then this
73 directory will be used.")
75 (make-variable-buffer-local 'gamegrid-use-glyphs
)
76 (make-variable-buffer-local 'gamegrid-use-color
)
77 (make-variable-buffer-local 'gamegrid-font
)
78 (make-variable-buffer-local 'gamegrid-display-options
)
79 (make-variable-buffer-local 'gamegrid-buffer-width
)
80 (make-variable-buffer-local 'gamegrid-buffer-height
)
81 (make-variable-buffer-local 'gamegrid-blank
)
82 (make-variable-buffer-local 'gamegrid-timer
)
83 (make-variable-buffer-local 'gamegrid-display-mode
)
84 (make-variable-buffer-local 'gamegrid-display-table
)
85 (make-variable-buffer-local 'gamegrid-face-table
)
86 (make-variable-buffer-local 'gamegrid-buffer-start
)
87 (make-variable-buffer-local 'gamegrid-score-file-length
)
89 ;; ;;;;;;;;;;;;; global variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 (defvar gamegrid-grid-x-face nil
)
92 (defvar gamegrid-mono-x-face nil
)
93 (defvar gamegrid-mono-tty-face nil
)
95 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 (defconst gamegrid-glyph-height
16)
99 (defconst gamegrid-xpm
"\
101 static char *noname[] = {
102 /* width height ncolors chars_per_pixel */
109 \"---------------+\",
110 \"--------------++\",
111 \"--............++\",
112 \"--............++\",
113 \"--............++\",
114 \"--............++\",
115 \"--............++\",
116 \"--............++\",
117 \"--............++\",
118 \"--............++\",
119 \"--............++\",
120 \"--............++\",
121 \"--............++\",
122 \"--............++\",
123 \"-+++++++++++++++\",
127 "XPM format image used for each square")
129 (defvar gamegrid-xbm
"\
131 #define gamegrid_width 16
132 #define gamegrid_height 16
133 static unsigned char gamegrid_bits[] = {
134 0xff, 0xff, 0xff, 0x7f, 0xff, 0x3f, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a,
135 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a,
136 0x57, 0x15, 0x07, 0x00, 0x03, 0x00, 0x01, 0x00 };"
137 "XBM format image used for each square.")
139 ;; ;;;;;;;;;;;;;;;; miscellaneous functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 (defsubst gamegrid-characterp
(arg)
142 (if (fboundp 'characterp
)
146 (defsubst gamegrid-event-x
(event)
147 (if (fboundp 'event-x
)
149 (car (posn-col-row (event-end event
)))))
151 (defsubst gamegrid-event-y
(event)
152 (if (fboundp 'event-y
)
154 (cdr (posn-col-row (event-end event
)))))
156 ;; ;;;;;;;;;;;;; display functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 (defun gamegrid-color (color shade
)
159 (let* ((v (floor (* shade
255)))
160 (r (* v
(aref color
0)))
161 (g (* v
(aref color
1)))
162 (b (* v
(aref color
2))))
163 (format "#%02x%02x%02x" r g b
)))
165 (defun gamegrid-set-font (face)
168 (set-face-font face gamegrid-font
)
171 (defun gamegrid-setup-face (face color
)
172 (set-face-foreground face color
)
173 (set-face-background face color
)
174 (gamegrid-set-font face
)
176 (set-face-background-pixmap face
[nothing]);; XEmacs
179 (set-face-background-pixmap face nil);; Emacs
182 (defun gamegrid-make-mono-tty-face ()
183 (let ((face (make-face 'gamegrid-mono-tty-face)))
184 (set-face-inverse-video-p face t)
187 (defun gamegrid-make-color-tty-face (color)
188 (let* ((color-str (if (symbolp color) (symbol-value color) color))
189 (name (intern (format "gamegrid-color-tty-face-%s" color-str)))
190 (face (make-face name)))
191 (gamegrid-setup-face face color-str)
194 (defun gamegrid-make-grid-x-face ()
195 (let ((face (make-face 'gamegrid-x-border-face)))
196 (gamegrid-set-font face)
199 (defun gamegrid-make-mono-x-face ()
200 (let ((face (make-face 'gamegrid-mono-x-face))
201 (color (face-foreground 'default)))
204 (cdr-safe (assq 'foreground-color (frame-parameters)))))
205 (gamegrid-setup-face face color)
208 (defun gamegrid-make-color-x-face (color)
209 (let* ((hex (gamegrid-color color 1.0))
210 (name (intern (format "gamegrid-color-x-face-%s" hex)))
211 (face (make-face name)))
212 (gamegrid-setup-face face hex)
215 (defun gamegrid-make-face (data-spec-list color-spec-list)
216 (let ((data (gamegrid-match-spec-list data-spec-list))
217 (color (gamegrid-match-spec-list color-spec-list)))
220 (gamegrid-make-color-x-face color))
222 (unless gamegrid-grid-x-face
223 (setq gamegrid-grid-x-face (gamegrid-make-grid-x-face)))
224 gamegrid-grid-x-face)
226 (unless gamegrid-mono-x-face
227 (setq gamegrid-mono-x-face (gamegrid-make-mono-x-face)))
228 gamegrid-mono-x-face)
230 (gamegrid-make-color-tty-face color))
232 (unless gamegrid-mono-tty-face
233 (setq gamegrid-mono-tty-face (gamegrid-make-mono-tty-face)))
234 gamegrid-mono-tty-face))))
236 (defun gamegrid-colorize-glyph (color)
237 (find-image `((:type xpm :data ,gamegrid-xpm
240 (("col1" . ,(gamegrid-color color 0.6))
241 ("col2" . ,(gamegrid-color color 0.8))
242 ("col3" . ,(gamegrid-color color 1.0))))
243 (:type xbm :data ,gamegrid-xbm
245 :foreground ,(gamegrid-color color 1.0)
246 :background ,(gamegrid-color color 0.5)))))
248 (defun gamegrid-match-spec (spec)
249 (let ((locale (car spec))
251 (and (or (eq locale t)
253 (memq gamegrid-display-mode locale))
254 (and (symbolp locale)
255 (eq gamegrid-display-mode locale)))
258 (defun gamegrid-match-spec-list (spec-list)
260 (or (gamegrid-match-spec (car spec-list))
261 (gamegrid-match-spec-list (cdr spec-list)))))
263 (defun gamegrid-make-glyph (data-spec-list color-spec-list)
264 (let ((data (gamegrid-match-spec-list data-spec-list))
265 (color (gamegrid-match-spec-list color-spec-list)))
266 (cond ((gamegrid-characterp data)
269 (gamegrid-colorize-glyph color))
271 (find-image data)) ;untested!
273 (gamegrid-make-image-from-vector data)))))
275 (defun gamegrid-make-image-from-vector (vect)
276 "Convert an XEmacs style \"glyph\" to an image-spec."
277 (let ((l (list 'image :type)))
278 (dotimes (n (length vect))
279 (setf l (nconc l (list (aref vect n)))))
280 (nconc l (list :ascent 'center))))
282 (defun gamegrid-display-type ()
283 (cond ((and gamegrid-use-glyphs
286 ((and gamegrid-use-color
292 ((and gamegrid-use-color
295 ((display-multi-font-p) ;???
300 (defun gamegrid-set-display-table ()
301 (if (featurep 'xemacs)
302 (add-spec-to-specifier current-display-table
303 gamegrid-display-table
307 (setq buffer-display-table gamegrid-display-table)))
309 (defun gamegrid-setup-default-font ()
312 (intern (concat "gamegrid-face-" (buffer-name)))))
313 (when (eq gamegrid-display-mode 'glyph)
314 (let ((max-height nil))
315 (loop for c from 0 to 255 do
316 (let ((glyph (aref gamegrid-display-table c)))
317 (when (and (listp glyph) (eq (car glyph) 'image))
318 (let ((height (cdr (image-size glyph))))
319 (if (or (null max-height)
320 (< max-height height))
321 (setq max-height height))))))
322 (when (and max-height (< max-height 1))
323 (set-face-attribute gamegrid-face nil :height max-height)))))
325 (defun gamegrid-initialize-display ()
326 (setq gamegrid-display-mode (gamegrid-display-type))
327 (setq gamegrid-display-table (make-display-table))
328 (setq gamegrid-face-table (make-vector 256 nil))
329 (loop for c from 0 to 255 do
330 (let* ((spec (aref gamegrid-display-options c))
331 (glyph (gamegrid-make-glyph (car spec) (caddr spec)))
332 (face (gamegrid-make-face (cadr spec) (caddr spec))))
333 (aset gamegrid-face-table c face)
334 (aset gamegrid-display-table c glyph)))
335 (gamegrid-setup-default-font)
336 (gamegrid-set-display-table)
337 (setq cursor-type nil))
340 (defun gamegrid-set-face (c)
341 (if (eq gamegrid-display-mode 'glyph)
342 (add-text-properties (1- (point)) (point)
343 (list 'display (list (aref gamegrid-display-table c))))
344 (put-text-property (1- (point))
347 (aref gamegrid-face-table c))))
349 (defun gamegrid-cell-offset (x y)
350 (+ gamegrid-buffer-start
351 (* (1+ gamegrid-buffer-width) y)
354 ;; ;;;;;;;;;;;;;;;; grid functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
356 (defun gamegrid-get-cell (x y)
357 (char-after (gamegrid-cell-offset x y)))
359 (defun gamegrid-set-cell (x y c)
361 (let ((buffer-read-only nil))
362 (goto-char (gamegrid-cell-offset x y))
365 (gamegrid-set-face c))))
367 (defun gamegrid-init-buffer (width height blank)
368 (setq gamegrid-buffer-width width
369 gamegrid-buffer-height height)
371 (make-string width blank)
373 (buffer-read-only nil))
375 (setq gamegrid-buffer-start (point))
378 ;; Adjust the height of the default face to the height of the
379 ;; images. Unlike XEmacs, Emacs doesn't allow to make the default
380 ;; face buffer-local; so we do this with an overlay.
381 (when (eq gamegrid-display-mode 'glyph)
382 (overlay-put (make-overlay (point-min) (point-max))
383 'face gamegrid-face))
384 (goto-char (point-min))))
386 (defun gamegrid-init (options)
387 (setq buffer-read-only t
389 gamegrid-display-options options)
390 (buffer-disable-undo (current-buffer))
391 (gamegrid-initialize-display))
393 ;; ;;;;;;;;;;;;;;;; timer functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
395 (defun gamegrid-start-timer (period func)
397 (if (featurep 'itimer)
398 (start-itimer "Gamegrid"
405 (run-with-timer period
410 (defun gamegrid-set-timer (delay)
412 (if (fboundp 'set-itimer-restart)
413 (set-itimer-restart gamegrid-timer delay)
414 (timer-set-time gamegrid-timer
415 (list (aref gamegrid-timer 1)
416 (aref gamegrid-timer 2)
417 (aref gamegrid-timer 3))
420 (defun gamegrid-kill-timer ()
422 (if (featurep 'itimer)
423 (delete-itimer gamegrid-timer)
424 (cancel-timer gamegrid-timer)))
425 (setq gamegrid-timer nil))
427 ;; ;;;;;;;;;;;;;;; high score functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 (defun gamegrid-add-score (file score)
430 "Add the current score to the high score file.
432 On POSIX systems there may be a shared game directory for all users in
433 which the scorefiles are kept. On such systems Emacs doesn't create
434 the score file FILE in this directory, if it doesn't already exist. In
435 this case Emacs searches for FILE in the directory specified by
436 `gamegrid-user-score-file-directory' and creates it there, if
439 To add the score file for a game to the system wide shared game
440 directory, create the file with the shell command \"touch\" in this
441 directory and make sure that it is owned by the correct user and
442 group. You probably need special user privileges to do this.
444 On non-POSIX systems Emacs searches for FILE in the directory
445 specified by the variable `temporary-file-directory'. If necessary,
446 FILE is created there."
449 (gamegrid-add-score-insecure file score))
451 (gamegrid-add-score-with-update-game-score file score))))
454 ;; On POSIX systems there are four cases to distinguish:
456 ;; 1. FILE is an absolute filename. Then it should be a file in
457 ;; temporary file directory. This is the way,
458 ;; `gamegrid-add-score' was supposed to be used in the past and
459 ;; is covered here for backward-compatibility.
461 ;; 2. The helper program "update-game-score" is setuid and the
462 ;; file FILE does already exist in a system wide shared game
463 ;; directory. This should be the normal case on POSIX systems,
464 ;; if the game was installed system wide. Use
465 ;; "update-game-score" to add the score to the file in the
466 ;; shared game directory.
468 ;; 3. "update-game-score" is setuid, but the file FILE does *not*
469 ;; exist in the system wide shared game directory. Use
470 ;; `gamegrid-add-score-insecure' to create--if necessary--and
471 ;; update FILE. This is for the case that a user has installed
472 ;; a game on her own.
474 ;; 4. "update-game-score" is not setuid. Use it to create/update
475 ;; FILE in the user's home directory. There is presumably no
476 ;; shared game directory.
478 (defvar gamegrid-shared-game-dir)
480 (defun gamegrid-add-score-with-update-game-score (file score)
481 (let* ((result nil) ;; What is this good for? -- os
482 (gamegrid-shared-game-dir
483 (not (zerop (logand (file-modes
484 (expand-file-name "update-game-score"
487 (cond ((file-name-absolute-p file)
488 (gamegrid-add-score-insecure file score))
489 ((and gamegrid-shared-game-dir
490 (file-exists-p (expand-file-name file shared-game-score-directory)))
491 ;; Use the setuid "update-game-score" program to update a
492 ;; system-wide score file.
493 (gamegrid-add-score-with-update-game-score-1 file
494 (expand-file-name file shared-game-score-directory) score))
495 ;; Else: Add the score to a score file in the user's home
497 (gamegrid-shared-game-dir
498 ;; If `gamegrid-shared-game-dir' is non-nil, then
499 ;; "update-gamescore" program is setuid, so don't use it.
500 (unless (file-exists-p
501 (directory-file-name gamegrid-user-score-file-directory))
502 (make-directory gamegrid-user-score-file-directory t))
503 (gamegrid-add-score-insecure file score
504 gamegrid-user-score-file-directory))
505 (t (let ((f (expand-file-name
506 gamegrid-user-score-file-directory)))
507 (when (file-writable-p f)
508 (unless (eq (car-safe (file-attributes f))
511 (setq f (expand-file-name file f))
512 (unless (file-exists-p f)
513 (write-region "" nil f nil 'silent nil 'excl)))
514 (gamegrid-add-score-with-update-game-score-1 file f score))))))
516 (defun gamegrid-add-score-with-update-game-score-1 (file target score)
517 (let ((default-directory "/")
518 (errbuf (generate-new-buffer " *update-game-score loss*"))
519 (marker-string (concat
522 (cond ((fboundp 'user-mail-address)
524 ((boundp 'user-mail-address)
528 (current-time-string))))
529 ;; This can be called from a timer, so enable local quits.
535 (expand-file-name "update-game-score" exec-directory)
537 "-m" (int-to-string gamegrid-score-file-length)
538 "-d" (if gamegrid-shared-game-dir
539 (expand-file-name shared-game-score-directory)
540 (file-name-directory target))
542 (int-to-string score)
544 (if (buffer-modified-p errbuf)
546 (display-buffer errbuf)
547 (error "Failed to update game score file"))
548 (kill-buffer errbuf))
549 (let ((buf (find-buffer-visiting target)))
553 (switch-to-buffer buf)
554 (revert-buffer nil t nil)
555 (display-buffer buf))
556 (find-file-read-only target))
557 (goto-char (point-min))
558 (search-forward (concat (int-to-string score)
559 " " (user-login-name) " "
561 (beginning-of-line)))))
563 (defun gamegrid-add-score-insecure (file score &optional directory)
565 (setq file (expand-file-name file (or directory
566 temporary-file-directory)))
567 (find-file-other-window file)
568 (setq buffer-read-only nil)
569 (goto-char (point-max))
570 (insert (format "%05d\t%s\t%s <%s>\n"
572 (current-time-string)
574 (cond ((fboundp 'user-mail-address)
576 ((boundp 'user-mail-address)
579 (sort-fields 1 (point-min) (point-max))
580 (reverse-region (point-min) (point-max))
581 (goto-line (1+ gamegrid-score-file-length))
582 (delete-region (point) (point-max))
583 (setq buffer-read-only t)
587 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
591 ;;; arch-tag: a96c2ff4-1c12-427e-bd3d-faeaf174cd46
592 ;;; gamegrid.el ends here