forked from stevelavietes/pico8carts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
maze.p8
3193 lines (2531 loc) · 76.7 KB
/
maze.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pico-8 cartridge // http://www.pico-8.com
version 8
__lua__
function _init()
--[[
g_dbgnohit = false
g_dbgforcecell = nil
g_dbgmove = false
--]]
g_rotbtnfnc = btnn
g_maxcells = 8
g_rotsignalobjs = {}
g_score = 0
g_hiscore = 0
stdinit()
--supports bg color
g_mazecols = {
{8,14},
{12,13},
{3,11},
{4,9},
}
g_mazecol = g_mazecols[1]
-- init map cells
for i = 0,g_maxcells-1 do
local y = (i%4) * 8
for j = 0,2 do
local x = flr(i/4)*32
rotmapcw(x+j*8,y,x+j*8+8,y,8)
end
end
add(g_objs, make_main())
end
--[[
function _update()
stdupdate()
end
function _draw()
stddraw()
end
--]]
------------------------------
function stdinit()
g_tick=0 --time
g_ct=0 --controllers
g_ctl=0 --last controllers
g_cs = {} --camera stack
g_objs = {} --objects
g_camx = 0
g_camy = 0
end
function _update()
g_tick = max(0,g_tick+1)
-- current/last controller
g_ctl = g_ct
g_ct = btn()
updateobjs(g_objs)
end
function updateobjs(objs)
foreach(objs, function(t)
if t.update then
t:update(objs)
end
end)
end
function _draw()
cls()
--[[
-- to opt: not animating
local n = 8
local s = 72
local l = 1
sprcpy(32,s+
flr((g_tick%(n*l))/n)
,1,1)
--]]
--pal(1, g_mazecol[3] or 1)
map(112,47,0,-(g_tick%48)/6,
16,17)
--pal()
pushc(g_camx, g_camy)
drawobjs(g_objs)
popc()
end
function drawobjs(objs)
foreach(objs, function(t)
if t.draw then
pushc(-t.x,-t.y)
t:draw(objs)
popc()
end
end)
end
--returns state,changed
function btns(i,p)
i=shl(1,i)
if p==1 then
i=shl(i,8)
end
local c,cng =
band(i,g_ct),
band(i,g_ctl)
return c>0,c~=cng
end
--returns new press only
function btnn(i,p)
if p==-1 then --either
return btnn(i,0) or btnn(i,1)
end
local pr,chg=btns(i,p)
return pr and chg
end
function getspraddr(n)
return flr(n/16)*512+(n%16)*4
end
function sprcpy(dst,src,w,h)
w = w or 1
h = h or 1
for i=0,h*8-1 do
memcpy(getspraddr(dst)+64*i,
getspraddr(src)+64*i,4*w)
end
end
function pushc(x, y)
local l=g_cs[#g_cs] or {0,0}
local n={l[1]+x,l[2]+y}
add(g_cs, n)
camera(n[1], n[2])
end
function popc()
local len = #g_cs
g_cs[len] = nil
len -= 1
if len > 0 then
local xy=g_cs[len]
camera(xy[1],xy[2])
else
camera()
end
end
function vecadd(a, b)
return {x=a.x+b.x, y=a.y+b.y}
end
function vecsub(a, b)
return {x=a.x-b.x, y=a.y-b.y}
end
function vecscale(v, m)
return {x=v.x*m, y=v.y*m}
end
function vecrot(v, a)
local s = sin(a/360)
local c = cos(a/360)
return {
x=v.x * c - v.y * s,
y=v.x * s + v.y * c,
}
end
function vecang(a, b)
local d = vecsub(b, a)
return atan2(d.x, d.y) * 360
end
function vecdistsq(a, b, sf)
if sf then
a = vecscale(a, sf)
b = vecscale(b, sf)
end
local distsq =
(b.x-a.x)^2 + (b.y-a.y)^2
if sf then
distsq = distsq/sf
end
return distsq
end
function rectsect(
x1,y1,x2,y2,x3,y3,x4,y4)
return (
x1 <= x4
and x2 >= x3
and y1 <= y4
and y2 >= y3)
end
function linesect(
x1,y1,x2,y2,x3,y3,x4,y4)
local sx1 = x2 - x1
local sy1 = y2 - y1
local sx2 = x4 - x3
local sy2 = y4 - y3
local s = (-sy1 * (x1 - x3)
+ sx1 * (y1 - y3)) /
(-sx2 * sy1 + sx1 * sy2)
local t = ( sx2 * (y1 - y3)
- sy2 * (x1 - x3)) /
(-sx2 * sy1 + sx1 * sy2)
return {
x = x1 + (t * sx1),
y = y1 + (t * sy1)
}
end
--todo, sget simpler?
function sprstochcpy(
dst, src, prob, w, h)
local s = getspraddr(src)
local d = getspraddr(dst)
local m1 = 15
local m2 = shl(m1, 4)
w = w or 1
h = h or 1
for y=0,h*8-1 do
for x=0,w*4-1 do
local p = peek(s+64*y+x)
if p ~= 0 then
local v1 = band(p, m1)
local v2 = band(p, m2)
local dp = peek(d+64*y+x)
if v1 > 0 and
rnd(100) < prob then
dp = bor(
band(dp, m2), v1)
end
if v2 > 0 and
rnd(100) < prob then
dp = bor(
band(dp, m1), v2)
end
poke(d+64*y+x, dp)
end
end
end
end
--not currently using
--[[
function sprblend(dst, s1, s2,
ptop,pbot,w,h)
w = w or 1
h = h or 1
pbot = pbot or ptop
local sa1 = getspraddr(s1)
local sa2 = getspraddr(s2)
local d = getspraddr(dst)
local m1 = 15
local m2 = shl(m1, 4)
local ym = h*8-1
for y=0,ym do
local p = y/ym*(pbot-ptop)
+ ptop
for x=0,w*4-1 do
local v1 = peek(sa1+64*y+x)
local v2 = peek(sa2+64*y+x)
local v3 = 0
local v = v1
if (rnd(100) <= p) v = v2
v3 = band(v, m2)
v = v1
if (rnd(100) <= p) v = v2
v3 = bor(band(v, m1), v3)
poke(d+64*y+x, v3)
end
end
end
--]]
--not currently using
--[[
function elapsed(t)
if g_tick>=t then
return g_tick - t
end
return 32767-t+g_tick
end
function make_trans(f,d,i)
--if (not i) sfx(0)
return {
d=d,
e=g_tick,
f=f,
i=i,
x=0,
y=0,
update=function(t,s)
if elapsed(t.e)>10 then
if (t.f) t:f(s)
del(s,t)
if not t.i then
add(s,
make_trans(nil,nil,1))
end
end
end,
draw=function(t)
local x=flr(elapsed(t.e)/2)
if t.i then
x=5-x
end
draw_wave(x*2, 64, g_tick/10)
end
}
end
------------------------------
function getcell(t,mzx,mzy)
if (not mzx) mzx = t.mzx
if (not mzy) mzy = t.mzy
local cx = flr(mzx/8)
local cy = flr(mzy/8)
local row = t.maze.b[cy+1]
if (not row) return nil
return row[cx+1]
end
------------------------------
function cango(t, dr)
local v = g_drdirs[dr]
local s, ix, iy, cx, cy =
getmazespr(
t.maze.b,t.mzx+v[3],
t.mzy+v[4])
if ix == 7 and dr == 1 then
if not cellhasdr(
getcell(t),1) or
not cellhasdr(
getcell(t, t.mzx+8,
t.mzy) ,3)
then
return false
end
end
if ix == 0 and dr == 3 then
if not cellhasdr(
getcell(t),3) or
not cellhasdr(
getcell(t, t.mzx-8,
t.mzy) ,1)
then
return false
end
end
if iy == 0 and dr == 0 then
if not cellhasdr(
getcell(t),0) or
not cellhasdr(
getcell(t, t.mzx,
t.mzy-8) ,2)
then
return false
end
end
if iy == 7 and dr == 2 then
if not cellhasdr(
getcell(t),2) or
not cellhasdr(
getcell(t, t.mzx,
t.mzy+8) ,0)
then
return false
end
end
return s and s > 0 and
not fget(s,v[5])
end
function candirs(t,omit)
result = {}
for i = 0,3 do
if omit ~= i
and cango(t, i) then
add(result, i)
end
end
return result
end
function char_rotdone(t,maze)
--[[
local iscur =
maze.cx == flr(t.mzx/8) and
maze.cy == flr(t.mzy/8)
--]]
local iscur = shouldrot(t)
if not iscur then
revdrcheck(t)
return
end
local ox = t.maze.cx * 8
local oy = t.maze.cy * 8
local ix = t.mzx - ox
local iy = t.mzy - oy
if maze.state == 0 then
-- todo, move in from edges
elseif maze.state == 2 then
if t.dr then
t.dr = (t.dr + 1) % 4
end
-- huh? 6?
t.mzx = (ox+6)-iy
t.mzy = oy + ix
else
if t.dr then
t.dr = (t.dr - 1) % 4
end
t.mzx = ox + iy
t.mzy = (oy+6)-ix
end
--reverse direction
--if now cannot go
revdrcheck(t)
end
function revdrcheck(t)
if t.dr then
if not cango(t, t.dr) then
t.dr = (t.dr + 2) % 4
if t.step and t.step ~= 0
then
--taco
t.step = t.rate - t.step
v = g_drdirs[t.dr]
t.mzx -= v[1]
t.mzy -= v[2]
end
end
end
end
function shouldrot(t)
local cx = flr(t.mzx/8)
local cy = flr(t.mzy/8)
local ix = flr(t.mzx)%8
local iy = flr(t.mzy)%8
local incell = cx == t.maze.cx
and cy == t.maze.cy
if not incell then
--todo, allow from left
if cx + 1 == t.maze.cx
and cy == t.maze.cy
and t.dr == 1
and ix == 7
then
--xxx need to update rotdone
return true
-- and from top
elseif cy + 1 == t.maze.cy
and cx == t.maze.cx
and t.dr == 2
and iy == 7
then
--xxx need to update rotdone
return true
end
return false
--else
-- xxx, extends the dir
--return true
end
if t.step then
if
--(t.dr == 0 and iy == 0)
--or
(t.dr == 1 and ix == 7)
or
(t.dr == 2 and iy == 7)
--or
--(t.dr == 3 and ix == 0)
then
if t.step > 0 then
return false
end
end
end
return true
end
function char_getpos(t)
local ox, oy =
t.mzx*8, t.mzy*8
if t.step then
local v = g_drdirs[t.dr]
--[[if not v then
cls()
print('bad dr ' .. t.dr,
0,0,7)
stop()
end
--]]
local r = t.step/t.rate
ox += v[1]*r*8
oy += v[2]*r*8
end
if t.maze.state > 0
and shouldrot(t)
--[[
and flr(t.mzx/8) ==
t.maze.cx
and flr(t.mzy/8) ==
t.maze.cy
--]]
then
local px = t.maze.cx*64+24
local py = t.maze.cy*64+24
local x = ox - px
local y = oy - py
local r =
vecrot({x=x,y=y},
t.maze.ang)
ox = -(r.x + px)
oy = -(r.y + py)
else
ox = -ox
oy = -oy
end
return ox, oy, {x=-ox,y=-oy}
end
-- [1] x movement delta
-- [2] y movement delta
-- [3] x map test
-- [4] y map test
-- [5] direction flag
g_drdirs = {
[0]={0,-1, 0,0, 1},
[1]={1,0, 1, 0, 2},
[2]={0,1, 0,1, 1},
[3]={-1,0, 0,0,2},
}
function make_mark(maze)
local t = {
x=0,
y=0,
mzx=0,
mzy=0,
step=0,
rate=8,
dr=1,
dst=100,
dstthr=16,
maze=maze,
ro=rnd(360),
rotdone=char_rotdone,
update=function(t,s)
-- debug
--if(true)return
--[[
if g_dbgmove
and not btnn(0,1)
and t.maze.state == 0
then
return
end
--]]
if (t.maze.state~=0) return
local flagdst =
vecdistsq(
{x=t.mzx,y=t.mzy},
{x=g_flag.mzx,
y=g_flag.mzy})
local dst =
vecdistsq(
{x=t.mzx,y=t.mzy},
{x=g_pmx,y=g_pmy})
t.dst = dst
if dst < t.dstthr then
--moveherdable(t)
--move away from player
movetarget(t,
{mzx=g_pmx,mzy=g_pmy})
elseif flagdst < 10 then
--move toward flag
movetarget(t, g_flag, true)
else
moverandom2(t)
end
end,
draw=function(t)
local ox, oy =
char_getpos(t)
if rectsect(
-ox+4,-oy+4,-ox+12,-oy+12,
g_camx, g_camy,
g_camx + 127, g_camy + 127)
then
--[[
if t.dst < t.dstthr then
pal(10,8)
pal(9,2)
end
--]]
local r =
vecrot({x=-1.5,y=0},
g_tick*10+t.ro)
local upset =
t.dst < t.dstthr
if not upset then
r.x = 0
r.y = 0
end
pushc(ox+r.x, oy+r.y)
spr(80+t.dr,4,4,1,1)
if upset
and g_tick % 2 == 0 then
local f= flr(
(g_tick%8)/2)
spr(84+f,1,4)
spr(84+f,7,4,1,1,true)
end
if g_dbgmove then
pushc(0,-8)
print(t.step, 10,0,7)
print(flr(t.mzx)%8, 10,7,7)
print(flr(t.mzy)%8, 10,14,7)
print(t.dr, 10,21,7)
print(shouldrot(t),
10,28,7)
popc()
end
popc()
--pal()
else
draw_radararrow(
-ox+8,-oy+8,10)
end
end
}
return t
end
function moverandom2(t)
if t.step == 0 then
--random movement
if not cango(t, t.dr) then
local drs =
candirs(t, t.dr)
local idx = flr(rnd(
#drs)) + 1
if (idx > #drs) idx = #drs
--xxx
--[[
if #drs == 0 then
return
end
--]]
if #drs > 0 then
t.dr = drs[idx]
end
else
local drs =
candirs(t, (t.dr+2)%4)
local idx = flr(rnd(
#drs)) + 1
if (idx > #drs) idx = #drs
--xxx
--[[
if #drs == 0 then
return
end
--]]
if #drs > 0 then
t.dr = drs[idx]
end
end
end
t.step += 1
if t.step >= t.rate then
local cal v = g_drdirs[t.dr]
t.step = 0
t.mzx += v[1]
t.mzy += v[2]
end
end
function movetarget(t,t2,
towards)
local checkdr = false
if t.step then
checkdr = t.step == 0
else
checkdr = t.mzx % 1 == 0
and t.mzy % 1 == 0
end
if checkdr then
local lngdrs = {}
local lngdst = 0
local mult = 1
if towards then
mult = -1
lngdst = -20000
end
for dr in all(
candirs(t)) do
local v = g_drdirs[dr]
local x = t.mzx
+ v[1]
local y = t.mzy
+ v[2]
local dst =
vecdistsq({x=x,y=y},
{x=t2.mzx,y=t2.mzy})
*mult
if dst > lngdst then
lngdrs = {dr}
lngdst = dst
elseif dst == lngdst
then
add(lngdrs, dr)
end
end
--xxx
if #lngdrs == 0 then
return
end
local idx = flr(rnd(
#lngdrs)) + 1
--xxx, why?
if idx > #lngdrs then
idx = #lngdrs
end
if idx <= #lngdrs then
t.dr = lngdrs[idx]
end
end
local v = g_drdirs[t.dr]
if t.step then
t.step += 1
if t.step >= t.rate then
t.step = 0
t.mzx += v[1]
t.mzy += v[2]
end
else
t.mzx = t.mzx + v[1]*0.125
t.mzy = t.mzy + v[2]*0.125
end
end
function make_enemy2(maze)
return {
x=0,
y=0,
mzx=0,
mzy=0,
dr=0,
step=0,
rate=10,
mvmode=flr(rnd(2.99)),
maze=maze,
rotdone=char_rotdone,
update=function(t,s)
if t.maze.state == 0 then
if t.mvmode == 0 then
moverandom2(t)
elseif t.mvmode == 1 then
movetarget(t, g_player,true)
else
movetarget(t, g_player,false)
end
end
end,
draw=function(t)
local ox, oy =
char_getpos(t)
if rectsect(
-ox+4,-oy+4,-ox+12,-oy+12,
g_camx, g_camy,
g_camx + 127, g_camy + 127)
then
pushc(ox,oy)
local s = 64 -- + t.dr
--s += t.mvmode
spr(s,4,4,1,1)
popc()
else
draw_radararrow(
-ox+8,-oy+8,8)
end
end
}
end
function make_flag(maze)
local t = {
x=0,
y=0,
mzx=0,
mzy=0,
maze=maze,
ups={},
rotdone=char_rotdone,
update=function(t,s)
g_fx = t.mzx