added doors and keys.
[uxul-world.git] / leveleditor.lisp
1 ;;; Copyright 2009 Christoph Senjak
2
3 (in-package :uxul-world-leveleditor)
4
5 (defparameter *leveleditor-images* nil)
6
7 (defun stretched-base64-image (img)
8   "Call ImageMagick to resize that file to 32x32."
9   (lisp-magick:with-magick-wand (mywand)
10     (lisp-magick::magick-read-image-blob mywand img)
11     (lisp-magick::magick-resize-image mywand 32 32 #x00000000 1d0)
12     (lisp-magick::magick-set-format mywand "gif")
13     (base64-encode-byteseq (lisp-magick::magick-get-image-blob mywand))))
14
15 (defun prepare-base64-images (&optional (care-about-initialization *leveleditor-images*))
16   (when (not care-about-initialization)
17     (setf *leveleditor-images* (make-hash-table))
18     (setf (gethash 'uxul-world::uxul *leveleditor-images*) (stretched-base64-image uxul-world::|uxul_small1|))
19     (setf (gethash 'uxul-world::leaf *leveleditor-images*) (stretched-base64-image uxul-world::|leaf|))
20     (setf (gethash 'uxul-world::nasobem *leveleditor-images*) (stretched-base64-image uxul-world::|nasobem|))
21     (setf (gethash 'uxul-world::blue-nasobem *leveleditor-images*) (stretched-base64-image uxul-world::|blue_nasobem|))
22     (setf (gethash 'uxul-world::burning-marshmallow *leveleditor-images*) (stretched-base64-image uxul-world::|burning_marshmallow_ld1|))
23     (setf (gethash 'uxul-world::gray-stone *leveleditor-images*) (stretched-base64-image uxul-world::|gray_stone|))
24     (setf (gethash 'uxul-world::brown-stone *leveleditor-images*) (stretched-base64-image uxul-world::|brown_stone|))
25     (setf (gethash 'uxul-world::empty *leveleditor-images*) (stretched-base64-image uxul-world::|empty|))
26     (setf (gethash 'uxul-world::tulip *leveleditor-images*) (stretched-base64-image uxul-world::|tulip|))))
27
28 (defun load-image-into-tk (png-base64)
29   "return a tkobject with this image"
30   (let ((name (ltk::create-name)))
31     (ltk:format-wish "set ~A [ image create photo -data \"~A\" ]"
32                      name png-base64)
33     (make-instance 'ltk:tkobject :name name)))
34
35 (defun config-button-image (button tkobject)
36   (ltk:format-wish "~A configure -image $~A" (ltk::widget-path button) (ltk::name tkobject)))
37
38 (defun item-table-to-list (item-table)
39   "Special Function vor level-editor. Returns a list of lists of the
40 form (x y object)."
41   (let ((ret nil))
42     (maphash #'(lambda (key val)
43                  (when val
44                    (push (list (car key) (cdr key) val) ret)))
45              item-table)
46     ret))
47
48 (defun level-editor (&optional (level nil) (width 16) (height 16))
49   (prepare-base64-images)
50   (let ((item-table (make-hash-table :test 'equal)))
51     ;;initialize given level
52     (dolist (item level)
53       (setf (gethash (cons (car item) (cadr item)) item-table) (caddr item)))
54     
55     (ltk:with-ltk ()
56       (let*
57           ((uxul (load-image-into-tk (gethash 'uxul-world::uxul *leveleditor-images*)))
58            (leaf (load-image-into-tk (gethash 'uxul-world::leaf *leveleditor-images*)))
59            (nasobem (load-image-into-tk (gethash 'uxul-world::nasobem *leveleditor-images*)))
60            (blue-nasobem (load-image-into-tk (gethash 'uxul-world::blue-nasobem *leveleditor-images*)))
61            (burning-marshmallow (load-image-into-tk (gethash 'uxul-world::burning-marshmallow *leveleditor-images*)))
62            (gray-stone (load-image-into-tk (gethash 'uxul-world::gray-stone *leveleditor-images*)))
63            (brown-stone (load-image-into-tk (gethash 'uxul-world::brown-stone *leveleditor-images*)))
64            (empty (load-image-into-tk (gethash 'uxul-world::empty *leveleditor-images*)))
65            (tulip (load-image-into-tk (gethash 'uxul-world::tulip *leveleditor-images*)))
66            (current-upper-left (cons 0 0))
67            (current-chosen-object 'uxul)
68            (objects-and-arrows (make-instance 'ltk:frame))
69            (object-frame (make-instance 'ltk:frame :master objects-and-arrows))
70            (arrow-frame (make-instance 'ltk:frame :master objects-and-arrows))
71            (grid-frame (make-instance 'ltk:frame))
72            (right-button (make-instance 'ltk:button :text ">"
73                                         :master arrow-frame))
74            (left-button (make-instance 'ltk:button :text "<"
75                                        :master arrow-frame))
76            (up-button (make-instance 'ltk:button :text "/\\"
77                                      :master arrow-frame))
78            (down-button (make-instance 'ltk:button :text "\\/"
79                                        :master arrow-frame))
80            (rright-button (make-instance 'ltk:button :text ">>"
81                                         :master arrow-frame))
82            (lleft-button (make-instance 'ltk:button :text "<<"
83                                        :master arrow-frame))
84            (uup-button (make-instance 'ltk:button :text "//\\\\"
85                                      :master arrow-frame))
86            (ddown-button (make-instance 'ltk:button :text "\\\\//"
87                                        :master arrow-frame))
88            (uxul-button (make-instance 'ltk:button :text ""
89                                        :master object-frame))
90            (nasobem-button (make-instance 'ltk:button :text ""
91                                           :master object-frame))
92            (blue-nasobem-button (make-instance 'ltk:button :text ""
93                                                :master object-frame))
94            (burning-marshmallow-button (make-instance 'ltk:button :text ""
95                                                       :master object-frame))
96            (gray-stone-button (make-instance 'ltk:button :text ""
97                                              :master object-frame))
98            (brown-stone-button (make-instance 'ltk:button :text ""
99                                        :master object-frame))
100            (empty-button (make-instance 'ltk:button :text ""
101                                         :master object-frame))
102            (tulip-button (make-instance 'ltk:button :text ""
103                                         :master object-frame))
104            (leaf-button (make-instance 'ltk:button :text ""
105                                        :master object-frame))
106            (btns (make-array (list width height) :adjustable nil :element-type 'ltk:button)))
107         (labels ((redraw-button (i j)
108                    "Redraw Button (i, j)"
109                    (let* ((current-upper-x (car current-upper-left))
110                           (current-upper-y (cdr current-upper-left))
111                           (cval (gethash (cons (+ i current-upper-x)
112                                                (+ j current-upper-y))
113                                         item-table nil))
114                          (cbtn (aref btns i j)))
115                      (if (listp cval)
116                          (setf cval (car cval)))
117                      (cond
118                        ((eq cval 'uxul-world::leaf)
119                         (config-button-image cbtn leaf))
120                        ((eq cval 'uxul-world::nasobem)
121                         (config-button-image cbtn nasobem))
122                        ((eq cval 'uxul-world::blue-nasobem)
123                         (config-button-image cbtn blue-nasobem))
124                        ((eq cval 'uxul-world::burning-marshmallow)
125                         (config-button-image cbtn burning-marshmallow))
126                        ((eq cval 'uxul-world::gray-stone)
127                         (config-button-image cbtn gray-stone))
128                        ((eq cval 'uxul-world::brown-stone)
129                         (config-button-image cbtn brown-stone))
130                        ((eq cval nil)
131                         (config-button-image cbtn empty))
132                        ((eq cval 'uxul-world::tulip)
133                         (config-button-image cbtn tulip))
134                        ((eq cval 'uxul-world::uxul)
135                         (config-button-image cbtn uxul)))))
136                  (redraw-buttons ()
137                    "Redraw all Buttons"
138                      (dotimes (i width)
139                        (dotimes (j height)
140                          (redraw-button i j))))
141                  (react (i j)
142                    (let ((current-upper-x (car current-upper-left))
143                          (current-upper-y (cdr current-upper-left)))
144                      (cond
145                        ((eq current-chosen-object 'burning-marshmallow)
146                         (setf (gethash (cons (+ i current-upper-x)
147                                              (+ j current-upper-y))
148                                        item-table) 'burning-marshmallow))
149                        (t
150                         (setf (gethash (cons (+ i current-upper-x)
151                                              (+ j current-upper-y))
152                                        item-table) current-chosen-object)))
153                      (redraw-button i j)))
154                  (move-field-about (i j)
155                    (let ((current-upper-y (car current-upper-left))
156                          (current-upper-x (cdr current-upper-left)))
157                      (setf current-upper-left (cons (+ i current-upper-y) (+ j current-upper-x))))
158                    (redraw-buttons)))
159           (ltk:pack grid-frame)
160           (ltk:grid arrow-frame 0 1)
161           (ltk:grid left-button 1 0)
162           (setf (ltk:command left-button) #'(lambda () (move-field-about 0 1)))
163           (ltk:grid lleft-button 2 0)
164           (setf (ltk:command lleft-button) #'(lambda () (move-field-about 0 15)))
165           (ltk:grid right-button 1 2)
166           (setf (ltk:command right-button) #'(lambda () (move-field-about 0 -1)))
167           (ltk:grid rright-button 0 2)
168           (setf (ltk:command rright-button) #'(lambda () (move-field-about 0 -15)))
169           (ltk:grid up-button 0 1)
170           (setf (ltk:command up-button) #'(lambda () (move-field-about 1 0)))
171           (ltk:grid uup-button 0 0)
172           (setf (ltk:command uup-button) #'(lambda () (move-field-about 15 0)))
173           (ltk:grid down-button 2 1)
174           (setf (ltk:command down-button) #'(lambda () (move-field-about -1 0)))
175           (ltk:grid ddown-button 2 2)
176           (setf (ltk:command ddown-button) #'(lambda () (move-field-about -15 0)))
177
178           (ltk:grid empty-button 0 0)
179           (config-button-image empty-button empty)
180           (setf (ltk:command empty-button)
181                              #'(lambda ()
182                                  (setf current-chosen-object nil)))
183           (ltk:grid uxul-button 0 1)
184           (config-button-image uxul-button uxul)
185           (setf (ltk:command uxul-button)
186                              #'(lambda ()
187                                  (setf current-chosen-object 'uxul-world::uxul)))
188           (ltk:grid nasobem-button 0 2)
189           (config-button-image nasobem-button nasobem)
190           (setf (ltk:command nasobem-button)
191                              #'(lambda ()
192                                  (setf current-chosen-object 'uxul-world::nasobem)))
193           (ltk:grid blue-nasobem-button 0 3)
194           (config-button-image blue-nasobem-button blue-nasobem)
195           (setf (ltk:command blue-nasobem-button)
196                              #'(lambda ()
197                                  (setf current-chosen-object 'uxul-world::blue-nasobem)))
198           (ltk:grid burning-marshmallow-button 0 4)
199           (config-button-image burning-marshmallow-button burning-marshmallow)
200           (setf (ltk:command burning-marshmallow-button)
201                              #'(lambda ()
202                                  (setf current-chosen-object 'uxul-world::burning-marshmallow)))
203           (ltk:grid gray-stone-button 0 5)
204           (config-button-image gray-stone-button gray-stone)
205           (setf (ltk:command gray-stone-button)
206                              #'(lambda ()
207                                  (setf current-chosen-object 'uxul-world::gray-stone)))
208           (ltk:grid brown-stone-button 0 6)
209           (config-button-image brown-stone-button brown-stone)
210           (setf (ltk:command brown-stone-button)
211                              #'(lambda ()
212                                  (setf current-chosen-object 'uxul-world::brown-stone)))
213           (ltk:grid leaf-button 0 7)
214           (config-button-image leaf-button leaf)
215           (setf (ltk:command leaf-button)
216                              #'(lambda ()
217                                  (setf current-chosen-object 'uxul-world::leaf)))
218
219           (ltk:grid tulip-button 0 8)
220           (config-button-image tulip-button tulip)
221           (setf (ltk:command tulip-button)
222                              #'(lambda ()
223                                  (setf current-chosen-object 'uxul-world::tulip)))
224
225           (ltk:grid object-frame 0 0)
226           (ltk:pack objects-and-arrows)
227
228           (dotimes (i width)
229             (dotimes (j height)
230               (let ((cbtn
231                      (make-instance 'ltk:button
232                                     :master grid-frame
233                                     :text "")))
234                 (setf (ltk:command cbtn) (let ((i i) (j j)) #'(lambda () (react i j))))
235                 (config-button-image cbtn empty)
236                 (setf (aref btns i j) cbtn)
237                 (ltk:grid cbtn i j))))
238           (redraw-buttons))))
239     (item-table-to-list item-table)))
240
241
242 (defun get-base64-char-for-number (i)
243   (declare (type (integer 0 63) i))
244   (elt "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" i))
245
246 (defun base64-encode-threebytes (byte1 byte2 byte3)
247   (declare (type (unsigned-byte 8) byte1 byte2 byte3))
248   (coerce
249    (list
250     (get-base64-char-for-number (logand #b111111 (ash byte1 -2)))
251     (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte1 6) -2) (ash byte2 -4))))
252     (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte2 4) -2) (ash byte3 -6))))
253     (get-base64-char-for-number (logand #b111111 (ash (ash byte3 2) -2)))) 'string))  
254
255
256 (defun base64-encode-bytelist (bytelist &optional (ret ""))
257   (if bytelist
258       (if (cdr bytelist)
259           (if (cddr bytelist)
260               (base64-encode-bytelist
261                (cdddr bytelist)
262                (concatenate 'string
263                             ret
264                             (base64-encode-threebytes
265                              (car bytelist)
266                              (cadr bytelist)
267                              (caddr bytelist))))
268               ;;else (genau zwei elemente)
269               (concatenate 'string ret                     
270                            (base64-encode-threebytes
271                             (car bytelist)
272                             (cadr bytelist)
273                             0)
274                            "="))
275           ;;else (genau ein element)
276           (concatenate 'string ret                         
277                        (base64-encode-threebytes
278                         (car bytelist) 0 0)
279                        "=="))
280       ;;else (kein element)
281       ret))
282
283
284 (defun base64-encode-byteseq (byteseq &optional (ret ""))
285   (case (length byteseq)
286     (0 ret)
287     (1 (concatenate 'string ret                    
288                     (base64-encode-threebytes
289                      (elt byteseq 0) 0 0) "=="))
290     (2 (concatenate 'string ret                    
291                     (base64-encode-threebytes
292                      (elt byteseq 0)
293                      (elt byteseq 1)
294                      0)
295                     "="))
296     (t (base64-encode-byteseq
297         (subseq byteseq 3)
298         (concatenate 'string
299                      ret
300                      (base64-encode-threebytes
301                       (elt byteseq 0)
302                       (elt byteseq 1)
303                       (elt byteseq 2)))))))