-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoko.p8
573 lines (534 loc) · 18.6 KB
/
soko.p8
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
pico-8 cartridge // http://www.pico-8.com
version 38
__lua__
-- soko
-- by ymao
-- sprite flags:
-- usr: 0-usr
-- env: (1) 1-wall
-- (2) 2-target
-- (3) 4-btn
-- (4) 5-door
-- obj: (1) 3-box
-- sfx:
-- 0: move
-- 1: level clear
-- 2: error
-- 3: undo move
-- global variables:
-- mn: menu
-- clv: current level
function _init()
extcmd("set_title","soko")
_initmenu()
_update=_updatemenu
_draw=_drawmenu
end
-- open level mn.lv
function _openlevel()
local lv=readlevel(mn.lv)
_initlevel(lv)
_update=_updatelevel
_draw=_drawlevel
end
function _openlevelnext()
mn.lv+=1
_openlevel()
end
function _openmenu()
_initmenu()
_update=_updatemenu
_draw=_drawmenu
end
-- end main
-->8
-- levels
-- read level from map editor
function readlevel(n)
local lv={
-- will not changes
x0=0, y0=0, -- pos in map
siz={x=16,y=16},
env={},
-- can changes
envs={}, -- env states (for btn/door)
obj={}, -- box positions
cnt=0, -- count filled targets
usr={x=1,y=1},
-- in level only
usrface=3, -- face orientation
updated=2, -- 0-no 1-yes 2-forceredrawall
win=0,
moves={},
}
lv.x0=((n-1)%8)*16
lv.y0=((n-1)\8)*16 -- int div
for y=1,lv.siz.y do
local lv_env_y={}
local lv_obj_y={}
local lv_envs_y={}
for x=1,lv.siz.x do
local sp=mget(lv.x0+x-1,lv.y0+y-1)
local env=0
local obj=0
local envs=0
-- env
if (fget(sp,1)) env=1
if (fget(sp,2)) then
env=2
lv.cnt+=1
end
if (fget(sp,4)) then
env=3
envs=(sp-48)\3+1
end
if (fget(sp,5)) then
env=4
envs=(sp-48)\3+1
end
-- obj
if (fget(sp,3)) obj=1
-- usr
if (fget(sp,0)) lv.usr={x=x,y=y}
add(lv_env_y,env,x)
add(lv_envs_y,envs,x)
add(lv_obj_y,obj,x)
end
add(lv.env,lv_env_y,y)
add(lv.envs,lv_envs_y,y)
add(lv.obj,lv_obj_y,y)
end
return lv
end
-- end preset levels
-->8
-- inlevel
-- init, update, draw
function _initlevel(lv)
clv=lv
end
function _updatelevel()
-- allow undo even if win
if (btnp(4)) then
_updatelevelundo()
clv.updated=1
clv.win=0
end
if (clv.win != 1) then
if (btnp(0) or btnp(1)
or btnp(2) or btnp(3)) then
_updatelevelmove()
clv.updated=1
end
end
if ((clv.win == 1) and
btnp(5)) then
if (mn.lv < mn.lvmax) then
_openlevelnext()
else
_openmenu()
end
end
end
function _drawlevel()
if (clv.updated >= 1) then
_drawlevelall()
clv.updated=0
end
end
-- custom functions
function iswall(x,y)
return ((clv.env[y][x] == 1)
or (isdoor(x,y)
and not isdooropen(x,y)))
end
function isoutsidelv(x,y)
return ((x < 1)
or (x > clv.siz.x)
or (y < 1)
or (y > clv.siz.y))
end
function isbox(x,y)
return (clv.obj[y][x] > 0)
end
function isusr(x,y)
return ((clv.usr.x == x)
and (clv.usr.y == y))
end
function isbtn(x,y)
return (clv.env[y][x] == 3)
end
function getbtnn(x,y)
return abs(clv.envs[y][x])
end
function isdoor(x,y)
return (clv.env[y][x] == 4)
end
function isdooropen(x,y)
return (clv.envs[y][x] < 0)
end
-- envs > 0 door close, < 0 open
function getdoorn(x,y)
return abs(clv.envs[y][x])
end
-- n > 0
function setdoorn(x,y,n,open)
if (open) then
clv.envs[y][x]=-n
else
clv.envs[y][x]=n
end
end
function ispressed(x,y)
return (isusr(x,y) or isbox(x,y))
end
-- open/close doors with color n
function triggerdoors(n,open)
for y=1,clv.siz.y do
for x=1,clv.siz.x do
if (isdoor(x,y)
and (getdoorn(x,y) == n)) then
setdoorn(x,y,n,open)
end
end
end
end
function triggerbtn(x,y)
if (isbtn(x,y)) then
triggerdoors(getbtnn(x,y),
ispressed(x,y))
end
end
-- update only if any dpad button is pressed
function _updatelevelmove()
local x0=clv.usr.x
local y0=clv.usr.y
local nx=x0
local ny=y0
local nx2=x0
local ny2=y0
local cant_move=0
local is_push=0
if btnp(0) then -- left
clv.usrface=0
nx-=1
nx2-=2
elseif btnp(1) then -- right
clv.usrface=1
nx+=1
nx2+=2
elseif btnp(2) then -- up
clv.usrface=2
ny-=1
ny2-=2
elseif btnp(3) then -- down
clv.usrface=3
ny+=1
ny2+=2
end
-- detect outside map
if (isoutsidelv(nx,ny)) cant_move=1
-- detect wall collision
if (cant_move == 0
and iswall(nx,ny)) then
cant_move=1
end
-- next position is obj
if (cant_move == 0
and isbox(nx,ny)) then
-- detect out of map
if (isoutsidelv(nx2,ny2)) cant_move=1
-- detect wall collision
if (cant_move == 0
and iswall(nx2,ny2)) then
cant_move=1
end
-- detect obj collision
if (cant_move == 0
and isbox(nx2,ny2)) then
cant_move=1
end
-- detect if reach target
if (cant_move == 0) then
is_push=1
-- compute remain targets
if (clv.env[ny][nx] != 2
and clv.env[ny2][nx2] == 2) then
clv.cnt-=1
end
if (clv.env[ny][nx] == 2
and clv.env[ny2][nx2] != 2) then
clv.cnt+=1
end
end
end
-- move usr and obj
if (cant_move == 0) then
sfx(0)
add(clv.moves,
{face=clv.usrface,push=is_push})
clv.usr.x=nx
clv.usr.y=ny
if (is_push == 1) then
clv.obj[ny2][nx2]=clv.obj[ny][nx]
clv.obj[ny][nx]=0
-- trigger btn after move
triggerbtn(nx2,ny2)
end
triggerbtn(nx,ny)
triggerbtn(x0,y0)
clv.updated=1
end
if (cant_move == 1) then
sfx(2)
end
end
-- update level undo
function _updatelevelundo()
-- last move
local lm=deli(clv.moves)
local llm=clv.moves[#clv.moves]
local x0=clv.usr.x
local y0=clv.usr.y
local nx=x0
local ny=y0
local nx2=x0
local ny2=y0
if (lm != nil) then
-- undo if has last move
if (lm.face == 0) then
nx+=1
nx2-=1
end
if (lm.face == 1) then
nx-=1
nx2+=1
end
if (lm.face == 2) then
ny+=1
ny2-=1
end
if (lm.face == 3) then
ny-=1
ny2+=1
end
-- compute last face orientation
if (llm == nil) then
clv.usrface=lm.face
else
clv.usrface=llm.face
end
-- no outside map no collision
-- detect if pushed
if (lm.push == 1) then
is_push=1
-- compute remain targets
if (clv.env[ny2][nx2] != 2
and clv.env[y0][x0] == 2) then
clv.cnt-=1
end
if (clv.env[ny2][nx2] == 2
and clv.env[y0][x0] != 2) then
clv.cnt+=1
end
end
-- move usr and obj
sfx(3)
clv.usr.x=nx
clv.usr.y=ny
if (lm.push == 1) then
clv.obj[y0][x0]=clv.obj[ny2][nx2]
clv.obj[ny2][nx2]=0
-- trigger btn after move
triggerbtn(nx2,ny2)
end
triggerbtn(nx,ny)
triggerbtn(x0,y0)
clv.updated=1
else
-- no action to undo
sfx(2)
end
end
-- clean and redraw all elements
function _drawlevelall()
cls()
-- draw env layer (2|3|4)
map(clv.x0,clv.y0,0,0,clv.siz.x,clv.siz.y,22)
for y=1,clv.siz.y do
for x=1,clv.siz.x do
local x0=(x-1)*8
local y0=(y-1)*8
-- draw door
if isdoor(x,y) then
local n=getdoorn(x,y)
if isdooropen(x,y) then
spr(48+n*3-1,x0,y0)
else
spr(48+n*3-2,x0,y0)
end
end
-- draw obj
if isbox(x,y) then
spr(clv.obj[y][x]+15,x0,y0)
end
end
end
-- draw usr
spr(32+clv.usrface,(clv.usr.x-1)*8,(clv.usr.y-1)*8)
-- draw help msg
print("\#2🅾️ undo",99,2)
-- draw win msg
if (clv.cnt == 0) then
clv.win=1
sfx(1)
print("\#2\^w\^tlevel clear!",5,5)
if (mn.lv < mn.lvmax) then
print("\#2press ❎ to next level",5,20)
else
print("\#2thanks for playing!",5,20)
print("\#2press ❎ to menu",5,26)
end
end
end
-- end inlevel
-->8
-- menu
-- init, update, draw
function _initmenu()
mn={
itm={"start", "level 1"},
pos={x=5,y=8},
sel=1,
lvmax=3,
lv=1,
updated=2, -- 0-no 1-yes 2-forceredrawall
}
end
function _updatemenu()
-- navigate with up/down
if btnp(2) then
mn.sel-=1
if (mn.sel < 1) mn.sel=1
mn.updated=1
end
if btnp(3) then
mn.sel+=1
if (mn.sel > #mn.itm) mn.sel=#mn.itm
mn.updated=1
end
-- conditional action
if (mn.sel == 1) then
-- enter level
if btnp(5) then
_openlevel()
end
elseif (mn.sel == 2) then
-- next level
if (btnp(1)) then
if (mn.lv < mn.lvmax) then
mn.lv+=1
else
mn.lv=1
end
mn.itm[2]="level "..mn.lv
mn.updated=1
end
-- previous level
if (btnp(0)) then
if (mn.lv > 1) then
mn.lv-=1
else
mn.lv=mn.lvmax
end
mn.itm[2]="level "..mn.lv
mn.updated=1
end
-- enter level
if (btnp(5)) then
_openlevel()
end
end
end
function _drawmenu()
if (mn.updated >= 1) then
_drawmenuall()
mn.updated=0
end
end
-- custom functions
function _drawmenuall()
cls()
-- draw title
sspr(88,0,32,16,mn.pos.x*8-10,mn.pos.y*8-40,64,32)
sspr(104,24,16,8,mn.pos.x*8+36,mn.pos.y*8-16)
-- draw menu item
for i=1,#mn.itm do
if (mn.sel == i) then
print("> "..mn.itm[i],
mn.pos.x*8, (mn.pos.y+i)*8)
else
print(" "..mn.itm[i],
mn.pos.x*8, (mn.pos.y+i)*8)
end
end
end
-- end menu
__gfx__
00000000666666660000000000000000111111115555555555555555555555555555555555555555000000000000000000000000000000000000000000000000
00000000666666660999999000000000111111115666666556666666666666666666666556666665000000000000000000000000000000000000000000000000
00700700666666660999999000000000111111115666666556666666666666666666666556666665000000000008888880000000008800000000000000000000
00077000666666660990099000000000111111115666666556666666666666666666666556666665000000000008888880000000008800000000000000000000
00077000666666660990099000000000111111115666666556666666666666666666666556666665000000000008000880000000008800000000000000000000
00700700666666660999999000000000111111115666666556666666666666666666666556666665000000000008000000000000008800000000000000000000
00000000666666660999999000000000111111115666666556666666666666666666666556666665000000000008888888088888808808800555500000000000
00000000666666660000000000000000111111115555555555555555555555555555555556666665000000000008888888088888808808805533550000000000
00000000000000000000000000000000000000005555555555555555566666665555555556666665000000000000000088088888808888005333350000000000
06666660000000000000000000000000000000005666666666666665566666666666666656666665000000000088000088081881808888005333350000000000
06500660000000000000000000000000000000005666666666666665566666666666666656666665000000000088888888081881808888805533550000000000
06056060000000000000000000000000000000005666666666666665566666666666666656666665000000000088888888088888808808800555500000000000
06065060000000000000000000000000000000005666666666666665566666666666666656666665000000000000000000000000000000000000000000000000
06600560000000000000000000000000000000005666666666666665566666666666666656666665000000000000000000000000000000000000000000000000
06666660000000000000000000000000000000005666666666666665566666666666666656666665000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000005666666666666665566666666666666656666665000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000005666666666666665666666666666666556666665000000000000000000000000000000000000000000000000
08888880088888800888888008888880000000005666666666666665666666666666666556666665000000000000000000000000000000000000000000000000
08888880088888800818818008888880000000005666666666666665666666666666666556666665000000000000000000000000000000000000000000000000
08181880088181800818818008888880000000005666666666666665666666666666666556666665000000000000000000000000000000000000000000000000
08181880088181800888888008188180000000005666666666666665666666666666666556666665000000000000000000000000000000000000000000000000
08888880088888800888888008188180000000005666666666666665666666666666666556666665000000000000000000000000000000000000000000000000
08888880088888800888888008888880000000005666666666666665666666666666666556666665000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000005555555555555555555555556666666555555555000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00555500066666600033330000555500066666600044440000555500066666600022220000000000000000000000000000000000000009900909000900000000
05533550063663600300003005544550064664600400004005522550062662600200002000000000000000000000000000000000000000900909909900000000
05333350066336600300003005444450066446600400004005222250066226600200002000000000000000000000000000000000000000999909090900000000
05333350066336600300003005444450066446600400004005222250066226600200002000000000000000000000000000000000009900000909000900000000
05533550063663600300003005544550064664600400004005522550062662600200002000000000000000000000000000000000000000000909000900000000
00555500066666600033330000555500066666600044440000555500066666600022220000000000000000000000000000000000000000999909000900000000
__gff__
0002040002020202020202000000000008000000000202020202020000000000010101010002020202020200000000001020201020201020200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__map__
0101010101010101010101010101010104040404040404040404040404040404010101010101010101010101010101010404040404040404040404040404040401010101010101010101010101010101040404040404040404040404040404040101010101010101010101010101010104040404040404040404040404040404
0100000000000000000000000000000104000000000000000000000000000004010000000000000000000000010000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000000000000000000000000104000000000000000000000000000004010000000000000000000000010000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000000000000000000000000104000000150707070707070716000004010000000000020000000000010000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000000000000000000000000104000000193300003400000019000004010000000000000000003000310000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000001507070716000000000104000000190010000900001019000004010000100000000000000000010000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000001902000019000000000104000000190023002507080219000004010000100000000000000000010101010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000152600100019000000000104000000190000003000310019000004010000100023000000000000010000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000192310000019000000000104000000170831061807070726000004010000100000000000003300340000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000251602150726000000000104000000190010001900000000000004010000100000000000000000010000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000002507260000000000000104000000190000001900000000000004010000000000000000000000010101010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000000000000000000000000104000000190000001900000000000004010000000000000000003600370000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000000000000000000000000104000000250707072600000000000004010000000000020000000000010000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000000000000000000000000104000000000000000000000000000004010000000000000000000000010000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0100000000000000000000000000000104000000000000000000000000000004010000000000000000000000010000010400000000000000000000000000000401000000000000000000000000000001040000000000000000000000000000040100000000000000000000000000000104000000000000000000000000000004
0101010101010101010101010101010104040404040404040404040404040404010101010101010101010101010101010404040404040404040404040404040401010101010101010101010101010101040404040404040404040404040404040101010101010101010101010101010104040404040404040404040404040404
0404040404040404040404040404040401010101010101010101010101010101040404040404040404040404040404040101010101010101010101010101010104040404040404040404040404040404010101010101010101010101010101010404040404040404040404040404040401010101010101010101010101010101
__sfx__
00030000130501b05023030240101f0001f0001f000000000000032000180001300011000110000f0000f0000f0000f0000f0000f000160001600016000160001600016000160001b0001f0003a0000100000000
000800000a2500f25013250162501b250222502b23012200002000020000200002000020000200002000020000200002000020000200002000020000200002000020000200002000020000200002000020000200
000800000327000200002000020000200002000020000200002000020000200002000020000200002000020000200002000020000200002000020000200002000020000200002000020000200002000020000200
0003000013050100500e0000d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000