-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-tiles.lisp
164 lines (146 loc) · 4.69 KB
/
gen-tiles.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;; gen-tiles.lisp - Generate nethack-tiles.el and slashem-tiles.el
;; Author: Shawn Betts
;; Copyright (C) 2005 Shawn Betts
;;
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;
;; To generate the tiles eval: (txt2tiles)
;;
(defpackage :nh-tiles
(:use :cl)
(:export txt2tiles))
(in-package :nh-tiles)
(defconstant +nethack-files+ '(#p"../nethack/win/share/monsters.txt"
#p"../nethack/win/share/objects.txt"
#p"../nethack/win/share/other.txt"))
(defconstant +slashem-files+ '(#p"../slashem/win/share/monsters.txt"
#p"../slashem/win/share/objects.txt"
#p"../slashem/win/share/other.txt"))
;; If we've read from the file too far, we can push it back
(defvar *line* nil)
(defun pop-line (stream)
(when (null *line*)
(push (read-line stream nil nil) *line*))
(pop *line*))
(defun push-line (l)
(push l *line*))
(defun read-palette (stream)
(let (colors)
;; Read the colors. Stop when we've hit a line that starts with #
(push-line (do ((c (pop-line stream) (pop-line stream)))
((char-equal (char c 0) #\#) c)
(push c colors)))
(nreverse colors)))
(defun read-glyph (stream)
(let (glyph)
;; Read the # line
(pop-line stream)
;; read the { line
(pop-line stream)
;; read till we hit a } line
(do ((line (pop-line stream) (pop-line stream)))
((or (null line)
(char-equal (char line 0) #\})))
(push line glyph))
(nreverse glyph)))
;; ya, it's sorta hacky.
(defun gen-color (c stream)
(let* ((ch (char c 0))
(rgb (read-from-string (subseq (remove #\, c) 4))))
(format stream "\\\"~C c #~2,'0X~2,'0X~2,'0X\\\",~%" ch (first rgb) (second rgb) (third rgb))))
(defun gen-palette (colors stream)
(loop for c in colors
do (gen-color c stream)))
(defun gen-glyph (palette glyph stream)
"generate a glyph and write it to STREAM"
(format stream "~%(create-image \"/* XPM */~%")
(format stream "static char *xpm[] = {~%")
(format stream "/* width height ncolors chars_per_pixel */~%")
(format stream "\\\"16 16 16 1\\\",~%")
(format stream "/* colors */~%")
(gen-palette palette stream)
(format stream "/* pixels */~%")
(loop for g on glyph
do (format stream "\\\"~A\\\"" (subseq (car g) 2 18))
do (if (cdr g)
(format stream ",~%")
(format stream "~%")))
(format stream "};\" nil t)"))
(defun gen-blank-tile (stream)
(princ "(defconst nh-empty-tile
(create-image \"/* XPM */
static char *xpm[] = {
/* width height ncolors chars_per_pixel */
\\\"16 16 16 1\\\",
/* colors */
\\\"A c #000000\\\",
\\\"B c #00B6FF\\\",
\\\"C c #FF6C00\\\",
\\\"D c #FF0000\\\",
\\\"E c #0000FF\\\",
\\\"F c #009100\\\",
\\\"G c #6CFF00\\\",
\\\"H c #FFFF00\\\",
\\\"I c #FF00FF\\\",
\\\"J c #914700\\\",
\\\"K c #CC7900\\\",
\\\"L c #FFB691\\\",
\\\"M c #476C6C\\\",
\\\"N c #FFFFFF\\\",
\\\"O c #DADAB6\\\",
\\\"P c #6C91B6\\\",
/* pixels */
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\",
\\\"AAAAAAAAAAAAAAAA\\\"
};\" nil t))
" stream))
(defun gen-header (stream)
(format stream ";;; This file in auto-generated with gen-tiles.lisp~%")
(gen-blank-tile stream)
(format stream "(defconst nh-tile-vector (vector"))
(defun gen-footer (stream)
(format stream "))~%(provide 'nethack-tiles)"))
(defun gen-glyphs (src out)
(with-open-file (in src)
(let ((palette (read-palette in)))
(do ((g (read-glyph in) (read-glyph in)))
((null g))
(gen-glyph palette g out)))))
(defun do-conversion (output files)
(with-open-file (out output :direction :output :if-exists :supersede)
(gen-header out)
(loop for f in files
do (gen-glyphs f out))
(gen-footer out)))
(defun txt2tiles ()
;; Reset our pop buffer
(setf *line* nil)
(do-conversion "nethack-tiles.el" +nethack-files+)
(do-conversion "slashem-tiles.el" +slashem-files+))