-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrmario.fs
402 lines (336 loc) · 9 KB
/
drmario.fs
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
\ drmario-like game
17 ficl-vocabulary drmario-voc
also drmario-voc definitions
0 DEFINE-SFX connect.wav
1 DEFINE-SFX zap.wav
: maxrow ( lvl -- row )
DUP 15 < IF DROP 10 EXIT THEN
DUP 17 < IF DROP 11 EXIT THEN
19 < IF 12 EXIT THEN
13
;
CREATE extra-rng 0 C, 1 C, 2 C, 2 C, 1 C, 0 C, 0 C, 1 C, 2 C, 2 C, 1 C, 0 C, 0 C, 1 C, 2 C, 1 C,
: v>color ( spr# -- c ) DUP 33 48 WITHIN IF 32 - 1- 5 / 1+ ELSE DROP 0 THEN ;
: c>color ( spr# -- c ) DUP 17 32 WITHIN IF 16 - 1- 5 / 1+ ELSE DROP 0 THEN ;
: >color ( spr# -- c ) DUP 32 > IF v>color ELSE c>color THEN ;
: c>virus ( c -- spr# ) 1- 5 * 32 + 1+ ;
0 CONSTANT faces-nowhere
1 CONSTANT faces-right
2 CONSTANT faces-left
3 CONSTANT faces-down
4 CONSTANT faces-up
: >facing ( spr# -- dir ) DUP 17 33 WITHIN IF 16 - 1- 5 MOD 1+ ELSE DROP faces-nowhere THEN ;
: split ( x' y -- )
{ x y }
x y M@ >facing CASE
faces-right OF x 1+ y M@ 3 + x 1+ y M! ENDOF
faces-left OF x 1- y M@ 4 + x 1- y M! ENDOF
faces-down OF x y 1+ M@ 1 + x y 1+ M! ENDOF
faces-up OF x y 1- M@ 2 + x y 1- M! ENDOF
ENDCASE
;
\ virus generation
\ based on https://tetris.wiki/Dr._Mario#Virus_Generation
: fetch-far-neighbours ( x y -- c1 c2 c3 c4 )
{ x y }
x 11 + 2- 20 y - M@ v>color
x 11 + 2+ 20 y - M@ v>color
x 11 + 20 y 2- - M@ v>color
x 11 + 20 y 2+ - M@ v>color
;
: check-three-colors ( x y -- c1 c2 c3 )
{ x y | C1 C2 C3 }
x y fetch-far-neighbours
4 0 DO
CASE
1 OF 1 TO C1 ENDOF
2 OF 2 TO C2 ENDOF
3 OF 3 TO C3 ENDOF
ENDCASE
LOOP
C1 C2 C3
;
: cycle-color ( c -- c' ) 3 MOD 1+ ;
\ this code uses 1-based coords?
: gen-virus ( lvl remaining -- remaining' )
{ lvl rem | row col vtype c1 c2 c3 done }
BEGIN 16 RND 1+ DUP lvl maxrow <= UNTIL TO row
8 RND 1+ TO col
rem 4 MOD
DUP 3 = IF DROP extra-rng 16 RND + C@ THEN TO vtype
BEGIN ( P )
row 1- 11 + 20 col 1- - M@ 0<>
WHILE
row 1+ TO row
row 9 = IF 1 TO row col 1+ TO col THEN
col 17 = IF rem EXIT THEN
REPEAT
BEGIN
row 1- col 1- check-three-colors TO c3 TO c2 TO c1
\ all three colors -> look at next position
c1 c2 c3 + + 6 = IF
row 1+ TO row
row 9 = IF 1 TO row col 1+ TO col THEN
col 17 = IF rem EXIT THEN
THEN
\ otherwise, lacks current color?
vtype 1 = c1 NOT AND
vtype 2 = c2 NOT AND
vtype 3 = c3 NOT AND
OR OR IF TRUE TO done ( make virus ) THEN
\ cycle color and check neighbours again
vtype cycle-color TO vtype
done
UNTIL
\ create virus ( offset the type by -1 ) ( why? )
vtype c>virus row 1- 11 + 20 col 1- - M!
\ return the new remainder
rem 1-
;
: gen-bottle ( lvl n -- ) BEGIN OVER SWAP gen-virus DUP WHILE REPEAT 2DROP ;
\ capsules
CREATE capsules
\ single color
HEX
1 C, 2 C, 0 C, 0 C, 0 C, 3 C, 0 C, 4 C, 0 C, 0 C, 1 C, 2 C, 3 C, 0 C, 4 C, 0 C,
6 C, 7 C, 0 C, 0 C, 0 C, 8 C, 0 C, 9 C, 0 C, 0 C, 6 C, 7 C, 8 C, 0 C, 9 C, 0 C,
B C, C C, 0 C, 0 C, 0 C, D C, 0 C, E C, 0 C, 0 C, B C, C C, D C, 0 C, E C, 0 C,
\ mixed color, with duplicated pairs as the original
1 C, 7 C, 0 C, 0 C, 0 C, 3 C, 0 C, 9 C, 0 C, 0 C, 6 C, 2 C, 8 C, 0 C, 4 C, 0 C,
1 C, C C, 0 C, 0 C, 0 C, 3 C, 0 C, E C, 0 C, 0 C, B C, 2 C, D C, 0 C, 4 C, 0 C,
6 C, C C, 0 C, 0 C, 0 C, 8 C, 0 C, E C, 0 C, 0 C, B C, 7 C, D C, 0 C, 9 C, 0 C,
6 C, 2 C, 0 C, 0 C, 0 C, 8 C, 0 C, 4 C, 0 C, 0 C, 1 C, 7 C, 3 C, 0 C, 9 C, 0 C,
B C, 2 C, 0 C, 0 C, 0 C, D C, 0 C, 4 C, 0 C, 0 C, 1 C, C C, 3 C, 0 C, E C, 0 C,
B C, 7 C, 0 C, 0 C, 0 C, D C, 0 C, 9 C, 0 C, 0 C, 6 C, C C, 8 C, 0 C, E C, 0 C,
DECIMAL
: capsule@ ( x y rot# piece# -- ) >R >R 2 * + R> 4 * + R> 16 * + capsules + C@ ;
: fits? ( rot# piece# x y -- f )
{ r p x y }
2 0 ?DO
2 0 ?DO
11 x + I + y J + m@ 0<> I J r p capsule@ 0<> AND IF FALSE EXIT THEN
LOOP
LOOP
TRUE
;
: single? ( spr# -- f ) 16 - DUP 1 16 WITHIN SWAP 5 MOD 0= AND ;
: vertical? ( spr# -- f ) 16 - DUP 1 16 WITHIN SWAP 5 MOD 2 > AND ;
: horizontal? ( spr# -- f ) 16 - DUP 1 16 WITHIN SWAP 5 MOD 1 3 WITHIN AND ;
\ play code
VARIABLE capsule-x
VARIABLE capsule-y
VARIABLE capsule-rot
VARIABLE capsule-type
VARIABLE next-capsule
: move-left ( -- )
capsule-x @ 1- \ 0 MAX
DUP capsule-y @ capsule-rot @ capsule-type @ 2SWAP fits? IF capsule-x ! ELSE DROP THEN
;
: move-right ( -- )
capsule-x @ 1+ 9 MIN
DUP capsule-y @ capsule-rot @ capsule-type @ 2SWAP fits? IF capsule-x ! ELSE DROP THEN
;
: rotate-left ( -- )
capsule-rot @ 4 + 1- 4 MOD DUP
capsule-type @ capsule-x @ capsule-y @ fits? IF capsule-rot ! ELSE DROP THEN
;
: rotate-right ( -- )
capsule-rot @ 4 + 1+ 4 MOD DUP
capsule-type @ capsule-x @ capsule-y @ fits? IF capsule-rot ! ELSE DROP THEN
;
: advance ( -- ) 1 capsule-y +! ;
: new-capsule ( -- )
next-capsule @ capsule-type !
9 RND next-capsule !
-4 capsule-y !
5 capsule-x !
0 capsule-rot !
;
: clear-field ( -- ) 20 0 DO MRAM W I * + 11 + 10 ERASE LOOP ;
VARIABLE tick
: +tick ( -- ) tick @ 1+ 20 MOD tick ! ;
: ?move ( -- )
SCANCODE_W just-pressed? IF rotate-left THEN
SCANCODE_E just-pressed? IF rotate-right THEN
SCANCODE_A just-pressed? IF move-left THEN
SCANCODE_D just-pressed? IF move-right THEN
SCANCODE_S pressed? IF 0 tick ! THEN
;
\ gravity code
: pull-single ( x y -- f )
{ x y | pulled? }
11 x + y M@
BEGIN
11 x + y 1+ M@ 0=
WHILE
TRUE TO pulled?
DUP 11 x + y 1+ M!
0 11 x + y M!
y 1+ TO y
REPEAT DROP
pulled?
;
: pull-right ( -- )
{ x y | pulled? }
11 x + y M@
11 x + 1+ y M@
BEGIN
11 x + y 1+ M@ 0=
11 x + 1+ y 1+ M@ 0=
AND
WHILE
TRUE TO pulled?
2DUP 11 x + 1+ y 1+ M!
11 x + y 1+ M!
0 11 x + y M!
0 11 x + 1+ y M!
REPEAT 2DROP
pulled?
;
: pull-left ( -- )
{ x y | pulled? }
11 x + y M@
11 x + 1- y M@
BEGIN
11 x + y 1+ M@ 0=
11 x + 1- y 1+ M@ 0=
AND
WHILE
TRUE TO pulled?
2DUP 11 x + 1- y 1+ M!
11 x + y 1+ M!
0 11 x + y M!
0 11 x + 1- y M!
REPEAT 2DROP
pulled?
;
: pull-capsule ( x y -- f )
{ x y }
11 x + y M@ >facing CASE
1 OF x y pull-right ENDOF
2 OF x y pull-left ENDOF
ENDCASE
;
: pull-column ( x -- f )
{ x | color pulled? }
0 19 ?DO
x 11 + I M@ TO color
color single? IF x I pull-single pulled? OR TO pulled? THEN
color vertical? IF x I pull-single pulled? OR TO pulled? THEN
color horizontal? IF x I pull-capsule pulled? OR TO pulled? THEN
-1 +LOOP
pulled?
;
: gravity ( -- f ) FALSE 10 0 DO I pull-column OR LOOP ;
\ clearing code
: remove-cells-h ( x y u -- )
>R SWAP 11 + SWAP R>
0 ?DO
2DUP I UNDER+
2DUP split
0 -ROT M!
LOOP 2DROP
1 sfx
;
: clear-row ( u -- )
{ y | count last-color color }
11 0 DO
11 I + y M@ >color TO color
count 1+ TO count
color last-color <> IF
count 4 > last-color 0<> AND IF I count - y count remove-cells-h THEN
color TO last-color
0 TO count
THEN
LOOP
;
: clear-rows ( -- ) 20 0 DO I clear-row LOOP ;
: remove-cells-v ( x y u -- )
>R SWAP 11 + SWAP R>
0 ?DO
( x y )
2DUP ( x y x y ) I + ( x y x' y' )
2DUP ( x y x' y' x' y' ) split ( x y x' y' )
0 -ROT M! ( x y )
LOOP 2DROP
1 sfx
;
: clear-column ( u -- )
{ x | count last-color color }
21 0 DO
11 x + I M@ >color TO color
count 1+ TO count
color last-color <> IF
count 4 > last-color 0<> AND IF x I count - count remove-cells-v THEN
color TO last-color
0 TO count
THEN
LOOP
;
: clear-columns ( -- ) 10 0 DO I clear-column LOOP ;
: persist-capsule ( -- )
2 0 ?DO
2 0 ?DO
I J capsule-rot @ capsule-type @ capsule@ IF
I J capsule-rot @ capsule-type @ capsule@ 16 + 11 capsule-x @ + I + capsule-y @ J + m!
THEN
LOOP
LOOP
0 sfx
;
: ?advance ( -- )
tick @ 0= IF
capsule-rot @ capsule-type @ capsule-x @ capsule-y @ 1+ fits? IF
advance
ELSE
persist-capsule
clear-rows
clear-columns
BEGIN gravity WHILE clear-rows clear-columns REPEAT
new-capsule
THEN
THEN
;
: ?exit ( -- ) SCANCODE_Q pressed? IF retro-40 THEN ;
: <update> ( -- )
+tick
?move
?advance
?exit
;
: draw-next-capsule ( -- )
2 0 DO
2 0 DO
I J 0 next-capsule @ capsule@ IF
6 I + 8 * 10 J + 8 * I J 0 capsule-type @ capsule@ 16 + spr
THEN
LOOP
LOOP
;
: draw-capsule ( -- )
2 0 DO
2 0 DO
I J capsule-rot @ capsule-type @ capsule@ IF
11 capsule-x @ + I + 8 * capsule-y @ J + 8 * I J capsule-rot @ capsule-type @ capsule@ 16 + spr
THEN
LOOP
LOOP
;
: <draw> ( -- )
0 cls
0 0 map
draw-next-capsule
draw-capsule
;
: <init> ( -- )
0 tick !
s" drmario.spr" load-sprites
s" drmario.map" load-map
10 10 gen-bottle
new-capsule
;
\ install the software
PREVIOUS DEFINITIONS
ALSO drmario-voc
INSTALL drmario
PREVIOUS