1 ;;; gamegrid.el --- library for implementing grid-based games on Emacs
3 ;; Copyright (C) 1997-1998, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Glynn Clements <glynn@sensei.co.uk>
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/>.
32 ;; ;;;;;;;;;;;;; buffer-local variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (defvar gamegrid-use-glyphs t
35 "Non-nil means use glyphs when available.")
37 (defvar gamegrid-use-color t
38 "Non-nil means use color when available.")
40 (defvar gamegrid-font
"-*-courier-medium-r-*-*-*-140-100-75-*-*-iso8859-*"
41 "Name of the font used in X mode.")
43 (defvar gamegrid-face nil
44 "Indicates the face to use as a default.")
45 (make-variable-buffer-local 'gamegrid-face
)
47 (defvar gamegrid-display-options nil
)
49 (defvar gamegrid-buffer-width
0)
50 (defvar gamegrid-buffer-height
0)
51 (defvar gamegrid-blank
0)
53 (defvar gamegrid-timer nil
)
55 (defvar gamegrid-display-mode nil
)
57 (defvar gamegrid-display-table
)
59 (defvar gamegrid-face-table nil
)
61 (defvar gamegrid-buffer-start
1)
63 (defvar gamegrid-score-file-length
50
64 "Number of high scores to keep.")
66 (defvar gamegrid-user-score-file-directory
67 (locate-user-emacs-file "games/")
68 "A directory for game scores which can't be shared.
69 If Emacs was built without support for shared game scores, then this
70 directory will be used.")
72 (make-variable-buffer-local 'gamegrid-use-glyphs
)
73 (make-variable-buffer-local 'gamegrid-use-color
)
74 (make-variable-buffer-local 'gamegrid-font
)
75 (make-variable-buffer-local 'gamegrid-display-options
)
76 (make-variable-buffer-local 'gamegrid-buffer-width
)
77 (make-variable-buffer-local 'gamegrid-buffer-height
)
78 (make-variable-buffer-local 'gamegrid-blank
)
79 (make-variable-buffer-local 'gamegrid-timer
)
80 (make-variable-buffer-local 'gamegrid-display-mode
)
81 (make-variable-buffer-local 'gamegrid-display-table
)
82 (make-variable-buffer-local 'gamegrid-face-table
)
83 (make-variable-buffer-local 'gamegrid-buffer-start
)
84 (make-variable-buffer-local 'gamegrid-score-file-length
)
86 ;; ;;;;;;;;;;;;; global variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
88 (defvar gamegrid-grid-x-face nil
)
89 (defvar gamegrid-mono-x-face nil
)
90 (defvar gamegrid-mono-tty-face nil
)
92 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 (defconst gamegrid-glyph-height
16)
96 (defconst gamegrid-xpm
"\
98 static char *noname[] = {
99 /* width height ncolors chars_per_pixel */
106 \"---------------+\",
107 \"--------------++\",
108 \"--............++\",
109 \"--............++\",
110 \"--............++\",
111 \"--............++\",
112 \"--............++\",
113 \"--............++\",
114 \"--............++\",
115 \"--............++\",
116 \"--............++\",
117 \"--............++\",
118 \"--............++\",
119 \"--............++\",
120 \"-+++++++++++++++\",
124 "XPM format image used for each square")
126 (defvar gamegrid-xbm
"\
128 #define gamegrid_width 16
129 #define gamegrid_height 16
130 static unsigned char gamegrid_bits[] = {
131 0xff, 0xff, 0xff, 0x7f, 0xff, 0x3f, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a,
132 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a,
133 0x57, 0x15, 0x07, 0x00, 0x03, 0x00, 0x01, 0x00 };"
134 "XBM format image used for each square.")
136 ;; ;;;;;;;;;;;;;;;; miscellaneous functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 (defsubst gamegrid-characterp
(arg)
139 (if (fboundp 'characterp
)
143 (defsubst gamegrid-event-x
(event)
144 (if (fboundp 'event-x
)
146 (car (posn-col-row (event-end event
)))))
148 (defsubst gamegrid-event-y
(event)
149 (if (fboundp 'event-y
)
151 (cdr (posn-col-row (event-end event
)))))
153 ;; ;;;;;;;;;;;;; display functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 (defun gamegrid-color (color shade
)
156 (let* ((v (floor (* shade
255)))
157 (r (* v
(aref color
0)))
158 (g (* v
(aref color
1)))
159 (b (* v
(aref color
2))))
160 (format "#%02x%02x%02x" r g b
)))
162 (defun gamegrid-set-font (face)
165 (set-face-font face gamegrid-font
)
168 (defun gamegrid-setup-face (face color
)
169 (set-face-foreground face color
)
170 (set-face-background face color
)
171 (gamegrid-set-font face
)
173 (set-face-background-pixmap face
[nothing]);; XEmacs
176 (set-face-background-pixmap face nil);; Emacs
179 (defun gamegrid-make-mono-tty-face ()
180 (let ((face (make-face 'gamegrid-mono-tty-face)))
181 (set-face-inverse-video-p face t)
184 (defun gamegrid-make-color-tty-face (color)
185 (let* ((color-str (if (symbolp color) (symbol-value color) color))
186 (name (intern (format "gamegrid-color-tty-face-%s" color-str)))
187 (face (make-face name)))
188 (gamegrid-setup-face face color-str)
191 (defun gamegrid-make-grid-x-face ()
192 (let ((face (make-face 'gamegrid-x-border-face)))
193 (gamegrid-set-font face)
196 (defun gamegrid-make-mono-x-face ()
197 (let ((face (make-face 'gamegrid-mono-x-face))
198 (color (face-foreground 'default)))
201 (cdr-safe (assq 'foreground-color (frame-parameters)))))
202 (gamegrid-setup-face face color)
205 (defun gamegrid-make-color-x-face (color)
206 (let* ((hex (gamegrid-color color 1.0))
207 (name (intern (format "gamegrid-color-x-face-%s" hex)))
208 (face (make-face name)))
209 (gamegrid-setup-face face hex)
212 (defun gamegrid-make-face (data-spec-list color-spec-list)
213 (let ((data (gamegrid-match-spec-list data-spec-list))
214 (color (gamegrid-match-spec-list color-spec-list)))
217 (gamegrid-make-color-x-face color))
219 (unless gamegrid-grid-x-face
220 (setq gamegrid-grid-x-face (gamegrid-make-grid-x-face)))
221 gamegrid-grid-x-face)
223 (unless gamegrid-mono-x-face
224 (setq gamegrid-mono-x-face (gamegrid-make-mono-x-face)))
225 gamegrid-mono-x-face)
227 (gamegrid-make-color-tty-face color))
229 (unless gamegrid-mono-tty-face
230 (setq gamegrid-mono-tty-face (gamegrid-make-mono-tty-face)))
231 gamegrid-mono-tty-face))))
233 (defun gamegrid-colorize-glyph (color)
234 (find-image `((:type xpm :data ,gamegrid-xpm
237 (("col1" . ,(gamegrid-color color 0.6))
238 ("col2" . ,(gamegrid-color color 0.8))
239 ("col3" . ,(gamegrid-color color 1.0))))
240 (:type xbm :data ,gamegrid-xbm
242 :foreground ,(gamegrid-color color 1.0)
243 :background ,(gamegrid-color color 0.5)))))
245 (defun gamegrid-match-spec (spec)
246 (let ((locale (car spec))
248 (and (or (eq locale t)
250 (memq gamegrid-display-mode locale))
251 (and (symbolp locale)
252 (eq gamegrid-display-mode locale)))
255 (defun gamegrid-match-spec-list (spec-list)
257 (or (gamegrid-match-spec (car spec-list))
258 (gamegrid-match-spec-list (cdr spec-list)))))
260 (defun gamegrid-make-glyph (data-spec-list color-spec-list)
261 (let ((data (gamegrid-match-spec-list data-spec-list))
262 (color (gamegrid-match-spec-list color-spec-list)))
263 (cond ((gamegrid-characterp data)
266 (gamegrid-colorize-glyph color))
268 (find-image data)) ;untested!
270 (gamegrid-make-image-from-vector data)))))
272 (defun gamegrid-make-image-from-vector (vect)
273 "Convert an XEmacs style \"glyph\" to an image-spec."
274 (let ((l (list 'image :type)))
275 (dotimes (n (length vect))
276 (setf l (nconc l (list (aref vect n)))))
277 (nconc l (list :ascent 'center))))
279 (defun gamegrid-display-type ()
280 (cond ((and gamegrid-use-glyphs
283 ((and gamegrid-use-color
289 ((and gamegrid-use-color
292 ((display-multi-font-p) ;???
297 (defun gamegrid-set-display-table ()
298 (if (featurep 'xemacs)
299 (add-spec-to-specifier current-display-table
300 gamegrid-display-table
304 (setq buffer-display-table gamegrid-display-table)))
306 (declare-function image-size "image.c" (spec &optional pixels frame))
308 (defun gamegrid-setup-default-font ()
311 (intern (concat "gamegrid-face-" (buffer-name)))))
312 (when (eq gamegrid-display-mode 'glyph)
313 (let ((max-height nil))
314 (loop for c from 0 to 255 do
315 (let ((glyph (aref gamegrid-display-table c)))
316 (when (and (listp glyph) (eq (car glyph) 'image))
317 (let ((height (cdr (image-size glyph))))
318 (if (or (null max-height)
319 (< max-height height))
320 (setq max-height height))))))
321 (when (and max-height (< max-height 1))
322 (let ((default-font-height (face-attribute 'default :height))
323 (resy (/ (display-pixel-height) (/ (display-mm-height) 25.4)))
324 point-size pixel-size)
325 (setq point-size (/ (* (float default-font-height) max-height) 10)
326 pixel-size (floor (* resy (/ point-size 72.27)))
327 point-size (* (/ pixel-size resy) 72.27))
328 (face-spec-set gamegrid-face
329 `((t :height ,(floor (* point-size 10))))))))))
331 (defun gamegrid-initialize-display ()
332 (setq gamegrid-display-mode (gamegrid-display-type))
333 (setq gamegrid-display-table (make-display-table))
334 (setq gamegrid-face-table (make-vector 256 nil))
335 (loop for c from 0 to 255 do
336 (let* ((spec (aref gamegrid-display-options c))
337 (glyph (gamegrid-make-glyph (car spec) (caddr spec)))
338 (face (gamegrid-make-face (cadr spec) (caddr spec))))
339 (aset gamegrid-face-table c face)
340 (aset gamegrid-display-table c glyph)))
341 (gamegrid-setup-default-font)
342 (gamegrid-set-display-table)
343 (setq cursor-type nil))
346 (defun gamegrid-set-face (c)
347 (if (eq gamegrid-display-mode 'glyph)
348 (add-text-properties (1- (point)) (point)
349 (list 'display (list (aref gamegrid-display-table c))))
350 (put-text-property (1- (point))
353 (aref gamegrid-face-table c))))
355 (defun gamegrid-cell-offset (x y)
356 (+ gamegrid-buffer-start
357 (* (1+ gamegrid-buffer-width) y)
360 ;; ;;;;;;;;;;;;;;;; grid functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
362 (defun gamegrid-get-cell (x y)
363 (char-after (gamegrid-cell-offset x y)))
365 (defun gamegrid-set-cell (x y c)
367 (let ((buffer-read-only nil))
368 (goto-char (gamegrid-cell-offset x y))
371 (gamegrid-set-face c))))
373 (defun gamegrid-init-buffer (width height blank)
374 (setq gamegrid-buffer-width width
375 gamegrid-buffer-height height)
377 (make-string width blank)
379 (buffer-read-only nil))
381 (setq gamegrid-buffer-start (point))
384 ;; Adjust the height of the default face to the height of the
385 ;; images. Unlike XEmacs, Emacs doesn't allow to make the default
386 ;; face buffer-local; so we do this with an overlay.
387 (when (eq gamegrid-display-mode 'glyph)
388 (overlay-put (make-overlay (point-min) (point-max))
389 'face gamegrid-face))
390 (goto-char (point-min))))
392 (defun gamegrid-init (options)
393 (setq buffer-read-only t
396 gamegrid-display-options options)
397 (buffer-disable-undo (current-buffer))
398 (gamegrid-initialize-display))
400 ;; ;;;;;;;;;;;;;;;; timer functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
402 (defun gamegrid-start-timer (period func)
404 (if (featurep 'xemacs)
405 (start-itimer "Gamegrid"
412 (run-with-timer period
417 (defun gamegrid-set-timer (delay)
419 (if (fboundp 'set-itimer-restart)
420 (set-itimer-restart gamegrid-timer delay)
421 (timer-set-time gamegrid-timer
422 (list (aref gamegrid-timer 1)
423 (aref gamegrid-timer 2)
424 (aref gamegrid-timer 3))
427 (defun gamegrid-kill-timer ()
429 (if (featurep 'xemacs)
430 (delete-itimer gamegrid-timer)
431 (cancel-timer gamegrid-timer)))
432 (setq gamegrid-timer nil))
434 ;; ;;;;;;;;;;;;;;; high score functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
436 (defun gamegrid-add-score (file score)
437 "Add the current score to the high score file.
439 On POSIX systems there may be a shared game directory for all users in
440 which the scorefiles are kept. On such systems Emacs doesn't create
441 the score file FILE in this directory, if it doesn't already exist.
442 In this case Emacs searches for FILE in the directory specified by
443 `gamegrid-user-score-file-directory' and creates it there, if
446 To add the score file for a game to the system wide shared game
447 directory, create the file with the shell command \"touch\" in this
448 directory and make sure that it is owned by the correct user and
449 group. You probably need special user privileges to do this.
451 On non-POSIX systems Emacs searches for FILE in the directory
452 specified by the variable `temporary-file-directory'. If necessary,
453 FILE is created there."
456 (gamegrid-add-score-insecure file score))
458 (gamegrid-add-score-with-update-game-score file score))))
461 ;; On POSIX systems there are four cases to distinguish:
463 ;; 1. FILE is an absolute filename. Then it should be a file in
464 ;; temporary file directory. This is the way,
465 ;; `gamegrid-add-score' was supposed to be used in the past and
466 ;; is covered here for backward-compatibility.
468 ;; 2. The helper program "update-game-score" is setuid and the
469 ;; file FILE does already exist in a system wide shared game
470 ;; directory. This should be the normal case on POSIX systems,
471 ;; if the game was installed system wide. Use
472 ;; "update-game-score" to add the score to the file in the
473 ;; shared game directory.
475 ;; 3. "update-game-score" is setuid, but the file FILE does *not*
476 ;; exist in the system wide shared game directory. Use
477 ;; `gamegrid-add-score-insecure' to create--if necessary--and
478 ;; update FILE. This is for the case that a user has installed
479 ;; a game on her own.
481 ;; 4. "update-game-score" is not setuid. Use it to create/update
482 ;; FILE in the user's home directory. There is presumably no
483 ;; shared game directory.
485 (defvar gamegrid-shared-game-dir)
487 (defun gamegrid-add-score-with-update-game-score (file score)
488 (let* ((result nil) ;; What is this good for? -- os
489 (gamegrid-shared-game-dir
490 (not (zerop (logand (file-modes
491 (expand-file-name "update-game-score"
494 (cond ((file-name-absolute-p file)
495 (gamegrid-add-score-insecure file score))
496 ((and gamegrid-shared-game-dir
497 (file-exists-p (expand-file-name file shared-game-score-directory)))
498 ;; Use the setuid "update-game-score" program to update a
499 ;; system-wide score file.
500 (gamegrid-add-score-with-update-game-score-1 file
501 (expand-file-name file shared-game-score-directory) score))
502 ;; Else: Add the score to a score file in the user's home
504 (gamegrid-shared-game-dir
505 ;; If `gamegrid-shared-game-dir' is non-nil, then
506 ;; "update-gamescore" program is setuid, so don't use it.
507 (unless (file-exists-p
508 (directory-file-name gamegrid-user-score-file-directory))
509 (make-directory gamegrid-user-score-file-directory t))
510 (gamegrid-add-score-insecure file score
511 gamegrid-user-score-file-directory))
512 (t (let ((f (expand-file-name
513 gamegrid-user-score-file-directory)))
514 (when (file-writable-p f)
515 (unless (eq (car-safe (file-attributes f))
518 (setq f (expand-file-name file f))
519 (unless (file-exists-p f)
520 (write-region "" nil f nil 'silent nil 'excl)))
521 (gamegrid-add-score-with-update-game-score-1 file f score))))))
523 (defun gamegrid-add-score-with-update-game-score-1 (file target score)
524 (let ((default-directory "/")
525 (errbuf (generate-new-buffer " *update-game-score loss*"))
526 (marker-string (concat
529 (cond ((fboundp 'user-mail-address)
531 ((boundp 'user-mail-address)
535 (current-time-string))))
536 ;; This can be called from a timer, so enable local quits.
542 (expand-file-name "update-game-score" exec-directory)
544 "-m" (int-to-string gamegrid-score-file-length)
545 "-d" (if gamegrid-shared-game-dir
546 (expand-file-name shared-game-score-directory)
547 (file-name-directory target))
549 (int-to-string score)
551 (if (buffer-modified-p errbuf)
553 (display-buffer errbuf)
554 (error "Failed to update game score file"))
555 (kill-buffer errbuf))
556 (let ((buf (find-buffer-visiting target)))
560 (switch-to-buffer buf)
561 (revert-buffer nil t nil)
562 (display-buffer buf))
563 (find-file-read-only target))
564 (goto-char (point-min))
565 (search-forward (concat (int-to-string score)
566 " " (user-login-name) " "
568 (beginning-of-line)))))
570 (defun gamegrid-add-score-insecure (file score &optional directory)
572 (setq file (expand-file-name file (or directory
573 temporary-file-directory)))
574 (find-file-other-window file)
575 (setq buffer-read-only nil)
576 (goto-char (point-max))
577 (insert (format "%05d\t%s\t%s <%s>\n"
579 (current-time-string)
581 (cond ((fboundp 'user-mail-address)
583 ((boundp 'user-mail-address)
586 (sort-fields 1 (point-min) (point-max))
587 (reverse-region (point-min) (point-max))
588 (goto-char (point-min))
589 (forward-line gamegrid-score-file-length)
590 (delete-region (point) (point-max))
591 (setq buffer-read-only t)
595 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
599 ;;; gamegrid.el ends here