forked from paines/cl-puyopuyo
/
cl-puyopuyo.lisp
439 lines (377 loc) · 14 KB
/
cl-puyopuyo.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
(defpackage :cl-puyopuyo
(:use :common-lisp :quicklisp )
(:export #:cl-puyopuyo))
(in-package :cl-puyopuyo)
;;width/height of bitmaos is 32x32
(defconstant +puyo-width+ 32)
(defconstant +puyo-height+ 32)
(defconstant +field-width+ 6)
(defconstant +field-height+ 12)
(defvar *max-cols* 4)
(defvar *points* 0)
(defvar *level* 1)
(defparameter *field* (make-array (* +field-width+ +field-height+)))
(defparameter *solve-field* (make-array (* +field-width+ +field-height+)))
(defvar *state* nil)
(defvar *run* nil)
(defparameter *puyos* (make-list 0))
;;a puyo consists of colors 0-3 (0=blue, 1=red, 2=green and 3=yellow), a state (dropping or landed) and a position
(defstruct puyo
x
y
col
state) ;;dropping / landed
;;we need two stones, 1st and 2nd
;;these need to be global, so that the dropPuyo-thread can access the stones
(defvar *first* nil)
(defvar *second* nil)
;;sdl surface pictures for blitting
(defparameter *blue* nil)
(defparameter *red* nil)
(defparameter *green* nil)
(defparameter *yellow* nil)
(defparameter *gameover* nil)
(defun clear-field ()
(dotimes (y +field-height+)
(dotimes (x +field-width+)
(setf (aref *field* (getOffset x y)) -1))))
(defun print-field ()
(dotimes (y +field-height+)
(dotimes (x +field-width+)
(format t "~D " (aref *field* (getOffset x y))))
(terpri)))
(defun count-elements (f elem)
(count elem f :test #'=))
(defun copy-field (src dst)
(dotimes(i (length src))
(setf (aref dst i) (aref src i))))
(defun number-to-color (number)
(case number
(0 *blue*)
(1 *red*)
(2 *green*)
(3 *yellow*)))
(defun draw-field (f)
;;i guess there is a sneaky lispy way to cut this down somehow
(dotimes (y +field-height+)
(dotimes (x +field-width+)
(let ((col (aref f (getOffset x y))))
(lispbuilder-sdl:draw-surface-at-*
(number-to-color col)
(* x +puyo-width+)
(* y +puyo-height+)
:surface lispbuilder-sdl:*default-display*)))))
(defun moveToLeft (f s)
(setf *state* 'pause)
(let (
(fx (slot-value f 'x))
(fy (slot-value f 'y))
(sy (slot-value s 'y))
(sx (slot-value s 'x))
(fstate (slot-value f 'state))
(sstate (slot-value s 'state)))
(if (and (eq sstate 'dropping) (eq fstate 'dropping))
(cond
;;stones lay on each other
((= fx sx)
(if (and (= (aref *field* (getoffset (- fx 1) fy)) -1) (>= (- fx 1) 0)
(= (aref *field* (getoffset (- sx 1) sy)) -1) (>= (- sx 1) 0))
(progn
(setf (slot-value f 'x) (- fx 1))
(setf (slot-value s 'x) (- sx 1)))))
;;s is left from f
((< sx fx)
(if (and (= (aref *field* (getoffset (- sx 1) sy)) -1) (>= (- sx 1) 0))
(progn
(setf (slot-value s 'x) (- sx 1))
(setf (slot-value f 'x) (- fx 1)))))
;;f is left from s
((< fx sx)
(if (and (= (aref *field* (getoffset (- fx 1) fy)) -1) (>= (- fx 1) 0))
(progn
(setf (slot-value f 'x) (- fx 1))
(setf (slot-value s 'x) (- sx 1))))))))
(setf *state* 'unpause))
(defun moveToRight (f s)
(setf *state* 'pause)
(let ((fx (slot-value f 'x))
(fy (slot-value f 'y))
(sy (slot-value s 'y))
(sx (slot-value s 'x))
(fstate (slot-value f 'state))
(sstate (slot-value s 'state)))
(if (and (eq sstate 'dropping) (eq fstate 'dropping))
(cond
;;stones lay on each other
((= fx sx)
(progn
(if (and
(= (aref *field* (getoffset (+ fx 1) fy)) -1) (< (+ fx 1 ) +field-width+)
(= (aref *field* (getoffset (+ sx 1) sy)) -1) (< (+ sx 1 ) +field-width+))
(progn
(setf (slot-value f 'x) (+ fx 1))
(setf (slot-value s 'x) (+ sx 1))))))
;;fx is left from sx
((< fx sx)
(progn
(if (and (= (aref *field* (getoffset (+ sx 1) sy)) -1) (< (+ sx 1) +field-width+))
(progn
(setf (slot-value s 'x) (+ sx 1))
(setf (slot-value f 'x) (+ fx 1))))))
;;sx is left from sx
((< sx fx)
(progn
(if (and (= (aref *field* (getoffset (+ fx 1) fy)) -1) (< (+ fx 1) +field-width+))
(progn
(setf (slot-value f 'x) (+ fx 1))
(setf (slot-value s 'x) (+ sx 1)))))))))
(setf *state* 'unpause))
(defun rotateStones (f s)
(setf *state* 'pause)
(let ((fx (slot-value f 'x))
(fy (slot-value f 'y))
(sy (slot-value s 'y))
(sx (slot-value s 'x)))
(cond
((< fx sx)
(if (/= sy 0)
(progn
(setf (slot-value f 'y) (- sy 1))
(setf (slot-value f 'x) sx))
(progn
(setf (slot-value f 'y) (+ sy 1))
(setf (slot-value f 'x) sx))))
((< fy sy)(= fx sx)
(if (= fx 0)
(progn
(setf (slot-value f 'x) (+ fx 1))
(setf (slot-value f 'y) (+ fy 1)))
(progn
(setf (slot-value s 'y) (- sy 1))
(setf (slot-value s 'x) (- sx 1)))))
((> fx sx) (= fy sy)
(if (/= sy 0)
(progn
(setf (slot-value s 'x) (+ sx 1))
(setf (slot-value s 'y) (- sy 1)))
(progn
(setf (slot-value s 'x) (+ sx 1))
(setf (slot-value f 'y) (+ fy 1)))))
((> fy sy)(= fx sx)
(if (= fx 0)
(progn
(setf (slot-value s 'x) (+ sx 1))
(setf (slot-value s 'y) (+ sy 1)))
(progn
(setf (slot-value s 'y) (+ sy 1))
(setf (slot-value f 'x) (- sx 1)))))))
(setf *state* 'unpause))
(defun getOffset (x y)
(+ (* y +field-width+) x))
(defun dropPuyos ()
(setq *puyos* (sort *puyos* #'> :key #'(lambda (x) (slot-value x 'y))))
(loop for p in *puyos* do
(if (eq (slot-value p 'state) 'dropping)
(if (and (< (+ (slot-value p 'y) 1) +field-height+)
(= (aref *field* (getOffset (slot-value p 'x) (+ (slot-value p 'y) 1))) -1))
(setf (slot-value p 'y) (+ (slot-value p 'y) 1))
(setf (slot-value p 'state) 'landed)))
(puyosToField *puyos* *field*))
(let ((landed 0))
(loop for p in *puyos* do
(if (eq (slot-value p 'state) 'landed)
(incf landed)))
(if (eq landed (length *puyos*))
0
1)))
(defun backtrack (f x y col)
(if (and (>= x 0) (>= y 0) (< x +field-width+) (< y +field-height+))
(if (= (aref f (getOffset x y)) col)
(progn
(setf (aref f (getOffset x y)) 255)
(if (or
(backtrack f (+ x 1) y col)
(backtrack f x (+ y 1) col)
(backtrack f (- x 1) y col)
(backtrack f x (- y 1) col)
nil)
nil))
nil)))
(defun sweeping ()
(dotimes (i (length *solve-field*))
(when (= (aref *solve-field* i) 255)
(setf *points* (+ *points* 2))
(when (< *level* 10)
(when (zerop (mod *points* 50))
(incf *level*)))
(setf (aref *field* i) -1)))
(dolist (p *puyos*)
(pop *puyos*))
(fieldToPuyos)
(loop do
(backtrackPuyos)
;; (puyosToField *puyos* *field*)
;; (lispbuilder-sdl:clear-display lispbuilder-sdl:*white*)
;; (draw-field *field*)
;; (lispbuilder-sdl:update-display)
;; (sleep 1)
while (= (dropPuyos) 1)))
(defun dropPuyo-thread ()
(loop do
(unless (or (eq *state* 'computing) (eq *state* 'pause))
(sleep (- 1 (/ *level* 10)))
(if (eq (dropPuyos) 0)
(setf *state* 'newPuyos)))
while (= 1 *run*)))
(defun fieldToPuyos ()
"this helper function will take a field->f and fill the lisp of puyos->p. this is only for testing purpose"
(loop for y from 0 to (- +field-height+ 1) do
(loop for x from 0 to (- +field-width+ 1) do
(if (/= (aref *field* (getOffset x y)) -1)
(progn
(push (make-puyo :x x :y y :col (aref *field* (getOffset x y)) :state 'dropping) *puyos*))))))
(defun puyosToField (puyos f)
"this helper function will take a list of puyos->p and place them into a field ->f"
(loop for y from 0 to (- +field-height+ 1) do
(loop for x from 0 to (- +field-width+ 1) do
(setf (aref f (getOffset x y)) -1)))
(loop for p in puyos do
(setf (aref f (getOffset (slot-value p 'x) (slot-value p 'y))) (slot-value p 'col))))
(defun fileToField (f)
"this helper function will read in a file called test.txt and construct a playing field->f. this is for testing purposes..."
(let ((in (open "test.txt"))
(l 0)
(data 0))
(dotimes (i (length f))
(setf data (digit-char-p (read-char in)))
(unless data
(setf data -1))
(setf (aref f i) data)
(incf l)
(when (= l 6)
(read-char in)
(setf l 0)))
(close in)))
(defun backtrackPuyos ()
(dolist (p *puyos*)
(let ((col (slot-value p 'col))
(x (slot-value p 'x))
(y (slot-value p 'y)))
(when (member col '(0 1 2 3))
(copy-field *field* *solve-field*)
(backtrack *solve-field* x y col)
(if (>= (count-elements *solve-field* 255) 4)
(sweeping))))))
;;start / main
(defun main ()
;;is this sufficient to generate random colors/numbers ?
(setf *random-state* (make-random-state t))
;;test stuff
;;(setf *first* (make-puyo :x 3 :y 1 :col 1 :state 'dropping))
;;(setf *second* (make-puyo :x 3 :y 2 :col 2 :state 'dropping))
(setf *first* (make-puyo :x 2
:y 0
:col (random *max-cols*)
:state 'dropping))
(setf *second* (make-puyo :x 3
:y 0
:col (random *max-cols*)
:state 'dropping))
(setf *level* 0)
(setf *points* 0)
(setf *run* 1)
(setf *state* 'unpause)
(push *first* *puyos*)
(push *second* *puyos*)
;;sdl
(lispbuilder-sdl:init-video)
(lispbuilder-sdl:initialise-default-font sdl:*ttf-font-vera*)
;;the puyo sprites are 32x32 pixels, so the playfield is 6*32 wide and 12*32 heigh
;;we need double with, for displaying points and next puyos right from the playfield
;(SETF (SDL:FRAME-RATE) 60)
(lispbuilder-sdl:window (* 2 +puyo-width+ +field-width+)
(* +puyo-height+ +field-height+))
(lispbuilder-sdl:init-subsystems lispbuilder-sdl:sdl-init-timer)
(lispbuilder-sdl:clear-display lispbuilder-sdl:*white*)
(clear-field)
(bordeaux-threads:make-thread #'dropPuyo-thread :name "dropPuyo-thread")
;;test stuff
;;(fileToField *field*)
;;(fieldToPuyos)
;;sdl surface pictures for blitting
(setf *blue* (lispbuilder-sdl:load-image "puyo_blue.png")
*red* (lispbuilder-sdl:load-image "puyo_red.png")
*green* (lispbuilder-sdl:load-image "puyo_green.png")
*yellow* (lispbuilder-sdl:load-image "puyo_yellow.png")
*gameover* (lispbuilder-sdl:load-image "gameover.png"))
;;game-loop
(lispbuilder-sdl:with-events (:poll)
(:quit-event () T)
(:key-down-event (:key key)
(when (lispbuilder-sdl:key= key :sdl-key-escape)
(lispbuilder-sdl:push-quit-event))
(when (lispbuilder-sdl:key= key :sdl-key-p)
;;pause handling
(cond ((eq *state* 'pause)
(setf *state* 'unpause))
((eq *state* 'unpause)
(setf *state* 'pause))))
(when (lispbuilder-sdl:key= key :sdl-key-up)
(rotateStones *first* *second*)
(puyosToField *puyos* *field*))
(when (lispbuilder-sdl:key= key :sdl-key-down)
(dropPuyos))
(when (lispbuilder-sdl:key= key :sdl-key-left)
(movetoLeft *first* *second*)
(puyosToField *puyos* *field*))
(when (lispbuilder-sdl:key= key :sdl-key-right)
(moveToRight *first* *second*)
(puyosToField *puyos* *field*)))
(:idle ()
(lispbuilder-sdl:clear-display lispbuilder-sdl:*white*)
(lispbuilder-sdl:draw-string-solid-*
"Puyopuyo"
(+ 20 (* +field-width+ +puyo-width+))
10
:color (sdl:color :r 0 :g 0 :b 0))
(lispbuilder-sdl-gfx:draw-line-*
(* +field-width+ +puyo-width+) 0
(* +field-width+ +puyo-width+) (* +field-height+ +puyo-height+)
:surface sdl:*default-display*
:color (sdl:color :r 0 :g 0 :b 0))
(puyosToField *puyos* *field*)
(if (eq *run* 0)
(lispbuilder-sdl:draw-surface-at-* *gameover* 0 0 :surface lispbuilder-sdl:*default-display*)
(draw-field *field*))
(if (eq *state* 'pause)
(lispbuilder-sdl:draw-string-solid-* (format nil "Paused") (+ (* +field-width+ +puyo-width+) 50) (/ (* +field-height+ +puyo-height+) 2) :color (sdl:color :r 0 :g 0 :b 0))
(progn
(lispbuilder-sdl:draw-string-solid-* (format nil "~D" *points*) (+ (* +field-width+ +puyo-width+) 80) (/ (* +field-height+ +puyo-height+) 2) :color (sdl:color :r 0 :g 0 :b 0))
(lispbuilder-sdl:draw-string-solid-* (format nil "Level ~D" *level*) (+ (* +field-width+ +puyo-width+) 25) (+ (/ (* +field-height+ +puyo-height+) 2) 50) :color (sdl:color :r 0 :g 0 :b 0))))
(lispbuilder-sdl:update-display)
(when (eq *state* 'newPuyos)
(if (or (/= (aref *field* (getOffset 2 0)) -1)
(/= (aref *field* (getOffset 3 0)) -1))
(setf *run* 0)
(progn
(backtrackPuyos)
;; we need to create new objects. what is the
;; impact on memory / gc
(setf *first* (make-puyo :x 2
:y 0
:col (random *max-cols*)
:state 'dropping))
(setf *second* (make-puyo :x 3
:y 0
:col (random *max-cols*)
:state 'dropping))
(push *first* *puyos*)
(push *second* *puyos*)
(setf *state* 'unpause)))))))
(main)
;;when esp ist pressed we drop out here. set run to zero so that the therad stops
;;we are done. bye bye
(setf *run* 0)
(sleep 1)
(format t "~%we are done...")
(lispbuilder-sdl:quit-sdl)