1 ;;; gamegrid.el -- Library for implementing grid-based games on Emacs
3 ;; Copyright (C) 1997, 1998 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 2, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
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-display-options nil
)
45 (defvar gamegrid-buffer-width
0)
46 (defvar gamegrid-buffer-height
0)
47 (defvar gamegrid-blank
0)
49 (defvar gamegrid-timer nil
)
51 (defvar gamegrid-display-mode nil
)
53 (defvar gamegrid-display-table
)
55 (defvar gamegrid-face-table nil
)
57 (defvar gamegrid-buffer-start
1)
59 (defvar gamegrid-score-file-length
50
60 "Number of high scores to keep")
62 (make-variable-buffer-local 'gamegrid-use-glyphs
)
63 (make-variable-buffer-local 'gamegrid-use-color
)
64 (make-variable-buffer-local 'gamegrid-font
)
65 (make-variable-buffer-local 'gamegrid-display-options
)
66 (make-variable-buffer-local 'gamegrid-buffer-width
)
67 (make-variable-buffer-local 'gamegrid-buffer-height
)
68 (make-variable-buffer-local 'gamegrid-blank
)
69 (make-variable-buffer-local 'gamegrid-timer
)
70 (make-variable-buffer-local 'gamegrid-display-mode
)
71 (make-variable-buffer-local 'gamegrid-display-table
)
72 (make-variable-buffer-local 'gamegrid-face-table
)
73 (make-variable-buffer-local 'gamegrid-buffer-start
)
74 (make-variable-buffer-local 'gamegrid-score-file-length
)
76 ;; ;;;;;;;;;;;;; global variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78 (defvar gamegrid-grid-x-face nil
)
79 (defvar gamegrid-mono-x-face nil
)
80 (defvar gamegrid-mono-tty-face nil
)
82 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 (defconst gamegrid-glyph-height
16)
86 (defconst gamegrid-xpm
"\
88 static char *noname[] = {
89 /* width height ncolors chars_per_pixel */
100 \"--............++\",
101 \"--............++\",
102 \"--............++\",
103 \"--............++\",
104 \"--............++\",
105 \"--............++\",
106 \"--............++\",
107 \"--............++\",
108 \"--............++\",
109 \"--............++\",
110 \"-+++++++++++++++\",
114 "XPM format image used for each square")
116 ;; ;;;;;;;;;;;;;;;; miscellaneous functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 (defsubst gamegrid-characterp
(arg)
119 (if (fboundp 'characterp
)
123 (defsubst gamegrid-event-x
(event)
124 (if (fboundp 'event-x
)
126 (car (posn-col-row (event-end event
)))))
128 (defsubst gamegrid-event-y
(event)
129 (if (fboundp 'event-y
)
131 (cdr (posn-col-row (event-end event
)))))
133 ;; ;;;;;;;;;;;;; display functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 (defun gamegrid-color (color shade
)
136 (let* ((v (floor (* shade
255)))
137 (r (* v
(aref color
0)))
138 (g (* v
(aref color
1)))
139 (b (* v
(aref color
2))))
140 (format "#%02x%02x%02x" r g b
)))
142 (defun gamegrid-set-font (face)
145 (set-face-font face gamegrid-font
)
148 (defun gamegrid-setup-face (face color
)
149 (set-face-foreground face color
)
150 (set-face-background face color
)
151 (gamegrid-set-font face
)
153 (set-face-background-pixmap face
[nothing]);; XEmacs
156 (set-face-background-pixmap face nil);; Emacs
159 (defun gamegrid-make-mono-tty-face ()
160 (let ((face (make-face 'gamegrid-mono-tty-face)))
162 (set-face-property face 'reverse t)
166 (defun gamegrid-make-color-tty-face (color)
167 (let* ((hex (gamegrid-color color 1.0))
168 (name (intern (format "gamegrid-color-tty-face-%s" hex)))
169 (face (make-face name)))
170 (gamegrid-setup-face face color)
173 (defun gamegrid-make-grid-x-face ()
174 (let ((face (make-face 'gamegrid-x-border-face)))
175 (gamegrid-set-font face)
178 (defun gamegrid-make-mono-x-face ()
179 (let ((face (make-face 'gamegrid-mono-x-face))
180 (color (face-foreground 'default)))
183 (cdr-safe (assq 'foreground-color (frame-parameters)))))
184 (gamegrid-setup-face face color)
187 (defun gamegrid-make-color-x-face (color)
188 (let* ((hex (gamegrid-color color 1.0))
189 (name (intern (format "gamegrid-color-x-face-%s" hex)))
190 (face (make-face name)))
191 (gamegrid-setup-face face (gamegrid-color color 1.0))
194 (defun gamegrid-make-face (data-spec-list color-spec-list)
195 (let ((data (gamegrid-match-spec-list data-spec-list))
196 (color (gamegrid-match-spec-list color-spec-list)))
199 (gamegrid-make-color-x-face color))
201 (unless gamegrid-grid-x-face
202 (setq gamegrid-grid-x-face (gamegrid-make-grid-x-face)))
203 gamegrid-grid-x-face)
205 (unless gamegrid-mono-x-face
206 (setq gamegrid-mono-x-face (gamegrid-make-mono-x-face)))
207 gamegrid-mono-x-face)
209 (gamegrid-make-color-tty-face color))
211 (unless gamegrid-mono-tty-face
212 (setq gamegrid-mono-tty-face (gamegrid-make-mono-tty-face)))
213 gamegrid-mono-tty-face))))
215 (defun gamegrid-colorize-glyph (color)
220 :color-symbols (list (cons "col1" (gamegrid-color color 0.6))
221 (cons "col2" (gamegrid-color color 0.8))
222 (cons "col3" (gamegrid-color color 1.0))))))
224 (defun gamegrid-match-spec (spec)
225 (let ((locale (car spec))
227 (and (or (eq locale t)
229 (memq gamegrid-display-mode locale))
230 (and (symbolp locale)
231 (eq gamegrid-display-mode locale)))
234 (defun gamegrid-match-spec-list (spec-list)
236 (or (gamegrid-match-spec (car spec-list))
237 (gamegrid-match-spec-list (cdr spec-list)))))
239 (defun gamegrid-make-glyph (data-spec-list color-spec-list)
240 (let ((data (gamegrid-match-spec-list data-spec-list))
241 (color (gamegrid-match-spec-list color-spec-list)))
242 (cond ((gamegrid-characterp data)
245 (gamegrid-colorize-glyph color))
247 (make-glyph data)))))
249 (defun gamegrid-color-display-p ()
250 (if (fboundp 'device-class)
251 (eq (device-class (selected-device)) 'color)
252 (eq (cdr-safe (assq 'display-type (frame-parameters))) 'color)))
254 (defun gamegrid-display-type ()
255 (let ((window-system-p
256 (or (and (fboundp 'console-on-window-system-p)
257 (console-on-window-system-p))
259 (cond ((and gamegrid-use-glyphs
263 ((and gamegrid-use-color
265 (gamegrid-color-display-p))
269 ((and gamegrid-use-color
270 (gamegrid-color-display-p))
272 ((fboundp 'set-face-property)
277 (defun gamegrid-set-display-table ()
278 (if (fboundp 'specifierp)
279 (add-spec-to-specifier current-display-table
280 gamegrid-display-table
284 (setq buffer-display-table gamegrid-display-table)))
286 (defun gamegrid-hide-cursor ()
287 (if (fboundp 'specifierp)
288 (set-specifier text-cursor-visible-p nil (current-buffer))))
290 (defun gamegrid-setup-default-font ()
291 (cond ((eq gamegrid-display-mode 'glyph)
292 (let* ((font-spec (face-property 'default 'font))
293 (name (font-name font-spec))
295 (loop for c from 0 to 255 do
296 (let ((glyph (aref gamegrid-display-table c)))
297 (cond ((glyphp glyph)
298 (let ((height (glyph-height glyph)))
299 (if (or (null max-height)
300 (< max-height height))
301 (setq max-height height)))))))
303 (while (and (> (font-height font-spec) max-height)
304 (setq name (x-find-smaller-font name)))
305 (add-spec-to-specifier font-spec name (current-buffer))))))))
307 (defun gamegrid-initialize-display ()
308 (setq gamegrid-display-mode (gamegrid-display-type))
309 (setq gamegrid-display-table (make-display-table))
310 (setq gamegrid-face-table (make-vector 256 nil))
311 (loop for c from 0 to 255 do
312 (let* ((spec (aref gamegrid-display-options c))
313 (glyph (gamegrid-make-glyph (car spec) (caddr spec)))
314 (face (gamegrid-make-face (cadr spec) (caddr spec))))
315 (aset gamegrid-face-table c face)
316 (aset gamegrid-display-table c glyph)))
317 (gamegrid-setup-default-font)
318 (gamegrid-set-display-table)
319 (gamegrid-hide-cursor))
322 (defun gamegrid-set-face (c)
323 (unless (eq gamegrid-display-mode 'glyph)
324 (put-text-property (1- (point))
327 (aref gamegrid-face-table c))))
329 (defun gamegrid-cell-offset (x y)
330 (+ gamegrid-buffer-start
331 (* (1+ gamegrid-buffer-width) y)
334 ;; ;;;;;;;;;;;;;;;; grid functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
336 (defun gamegrid-get-cell (x y)
337 (char-after (gamegrid-cell-offset x y)))
339 (defun gamegrid-set-cell (x y c)
341 (let ((buffer-read-only nil))
342 (goto-char (gamegrid-cell-offset x y))
345 (gamegrid-set-face c))))
347 (defun gamegrid-init-buffer (width height blank)
348 (setq gamegrid-buffer-width width
349 gamegrid-buffer-height height)
351 (make-string width blank)
353 (buffer-read-only nil))
355 (setq gamegrid-buffer-start (point))
357 (insert-string line))
358 (goto-char (point-min))))
360 (defun gamegrid-init (options)
361 (setq buffer-read-only t
363 gamegrid-display-options options)
364 (buffer-disable-undo (current-buffer))
365 (gamegrid-initialize-display))
367 ;; ;;;;;;;;;;;;;;;; timer functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
369 (defun gamegrid-start-timer (period func)
371 (if (featurep 'itimer)
372 (start-itimer "Gamegrid"
379 (run-with-timer period
384 (defun gamegrid-set-timer (delay)
386 (if (featurep 'itimer)
387 (set-itimer-restart gamegrid-timer delay)
388 (timer-set-time gamegrid-timer
389 (list (aref gamegrid-timer 1)
390 (aref gamegrid-timer 2)
391 (aref gamegrid-timer 3))
394 (defun gamegrid-kill-timer ()
396 (if (featurep 'itimer)
397 (delete-itimer gamegrid-timer)
398 (timer-set-time gamegrid-timer '(0 0 0) nil)))
399 (setq gamegrid-timer nil))
401 ;; ;;;;;;;;;;;;;;; high score functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
403 (defun gamegrid-add-score (file score)
404 "Add the current score to the high score file."
406 (find-file-other-window file)
407 (setq buffer-read-only nil)
408 (goto-char (point-max))
409 (insert (format "%05d\t%s\t%s <%s>\n"
411 (current-time-string)
413 (cond ((fboundp 'user-mail-address)
415 ((boundp 'user-mail-address)
418 (sort-numeric-fields 1 (point-min) (point-max))
419 (reverse-region (point-min) (point-max))
420 (goto-line (1+ gamegrid-score-file-length))
421 (delete-region (point) (point-max))
422 (setq buffer-read-only t)
425 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;