-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintoruins_main.p8
More file actions
3098 lines (2849 loc) · 106 KB
/
intoruins_main.p8
File metadata and controls
3098 lines (2849 loc) · 106 KB
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 42
__lua__
--keep:iNTO rUINS cART 2
--keep:
--keep:unminned:github.com/woflox/intoruins
function modeis(m)
return mode==m
end
function getbtn(b)
b&=btns
btns&=~b
return b!=0
end
function _update()
btns|=btnp()
if modeis"play" and not waitforanim then
updateturn()
elseif modeis"aim" then
updateaim(unpack(aimparams))
elseif modeis"reset" and
statet>0.3
then
load"intoruins"
end
statet+=0.03333
if mode!="ui" then
waitforanim=#rangedatks>0
for i,_ENV in inext,ents do
update()
end
end
smoothb=lerp(smoothb,screenpos(
lerp(player.pos,
vec2s"10,9.5",
(modeis"gameover" or
modeis"victory") and
max(0.36-statet*2) or
0.36)),
0.5)
smooth=lerp(smooth,smoothb,0.25)
function getcampos(val)
return flr(rnd(player.shake*2)-
player.shake+val-63.5)
end
campos=vec2(getcampos(smooth.x),
getcampos(smooth.y))
player.shake*=shakedamp
end
function _draw()
cls()
camera(campos.x,campos.y)
lfillp,anyfire=localfillp(0xbfd6.4,
-campos)
for i,drawcall in
inext,drawcalls do
drawcall[1](
unpack(drawcall[2]))
end
if anyfire != fireplaying then
fireplaying=anyfire
call(anyfire and "music(32,500,3" or "music(-1,500")
end
foreach(rangedatks, function(atk)
atk[2][1]+=1 --counter
if atk[1](unpack(atk[2])) then
del(rangedatks,atk)
end
end)
call"pal()palt(1)pal(15,129,1)pal(11,131,1)fillp("
if modeis"play" or modeis"victory"then
for i,ent in inext,ents do
local _ENV=ent.textanim
if _ENV and t<=0.5 then
t+=speed
pos.y-=0.5-t
?text,mid(campos.x,4+pos.x-#text*2,campos.x+128-#text*4)-wavy*cos(t*2),pos.y+offset,t>0.433 and 5 or col
else
ent.textanim=nil
end
end
elseif modeis"aim" then
?"\f7\+fd⁙",aimscrposx,aimscrposy
end
camera()
if modeis"play" or
modeis"ui" then
barx=-2
drawbar("HP",
player.hp,player.maxhp,2,8)
for k,v in
pairs(player.statuses)
do
drawbar(unpack(v))
end
elseif modeis"aim" then
call"print(\f6⬅️\+fd⬆️\+8m⬇️\+fd➡️:aIM ❎:fIRE,18,117"
end
if modeis"ui" then
if (getbtn"16") popdiag()
uitrans*=0.33
for i,d in inext,diags do
focus,curindex=i==#diags,0
d()
end
end
call("textcrawl(\-fgAME oVER \
\
\
\
\
\
\
\
\
\+jcdEPTH:"..depth..
" \n\n\-a❎:tRY AGAIN,47,28,1.3,13,gameover,16)textcrawl( \^x5◆ victory ◆\^x4 \
\
\
yOU ESCAPED WITH THE\
\-owINGS OF yENDOR! \
\
\-h\|isTEPS TAKEN:\-x"..stepstaken.." \
\-hiTEMS F\-fOUND:\-y"..itemsfound.." \
\-hcREATURES SLAIN: "..creaturesslain.." \
\
\
\
\-i❎:cONTINUE,24,21,6,7,victory,8")
end
function popdiag()
deli(diags)
uitrans=1
if #diags==0 then
uimode=--nil
setmode"play"
end
end
function drawbar(label,val,maxval,col1,col2)
camera(barx,0)
local w=max(print("\#0"..label,0,117,0),20)
local barw=ceil(val*w/maxval)-1
rect(-1,122,w,126,15)
rect(0,123,barw,125,col2)
if barw>0 then
rectfill(0,124,barw-1,
125,col1)
end
?label,0,116,col1
barx-=w+4
camera()
end
function textcrawl(str,x,y,fadet,col,m,mus)
if mode==m and statet>fadet then
if not musicplayed then
music(mus)
musicplayed=true
end
?sub(str,0,statet*30+tonum(statet>7.25)*40),x,y,statet>fadet+0.1and col or 1
if btnp"5" then
fadetoblack=true
call"music(-1,300)setmode(reset"
end
end
end
function log(text)
player.animtext(text..",col:7,speed:0.01666")
end
function frame(x,y,x2,y2,func)
clip()
func(x-1,y-1,x2+1,y2+1,0)
rect(x,y,x2,y2,1)
cursor(x+5,y+3)
clip(0,0,x2-0.4,y2)
end
function listitem(str,sel,dis)
if sel==nil then
curindex+=1
sel=curindex==menuindex
end
?(sel and "\#0\-8❎ \-g"or"")..str, dis and 5 or sel and 7 or 13
return sel and focus and
getbtn"32" and not dis
end
function getindex(maxind,cur)
return focus and
(cur+tonum(getbtn"8")-
tonum(getbtn"4")+
maxind-1)%maxind+1
or cur
end
function gettrans(str)
local a,b=usplit(str)
return lerp(b,a,focus and uitrans or 0.56*(1-uitrans))
end
function listitems(nop,eqpd)
foreach(inventory,function(item)
if item.equipped==eqpd then
invi+=1
if listitem(item.getname(),
invi==invindex,
uimode=="iDENTIFY"and
item.isid() or
uimode=="eMPOWER"and
item.orb)
then
dialog(info)
selitem=item
end
end
end)
end
function inv()
frame(gettrans"126,40",6,126,111,rect)
?uimode and"\fc"..uimode.." AN iTEM\n\f1\-c……………… EQUIPPED"or"\fdiNVENTORY\n\f1\-c……………… EQUIPPED"
invi,invindex=
0,getindex(#inventory,invindex)
call"listitems(t,t)print(\n\-c………………★ \-dSTOWED,1)listitems("
end
function info()
menuindex=getindex(uimode and 1 or 2,menuindex)
local _ENV,x=selitem,gettrans"42,4"
frame(x,6,gettrans"42,93.5",111,rectfill)
spr(typ+profilepic,x+3,8)
?"\fd \-h"..getname()
local statstr="\f1\-c……………………………\fd"
if isid() then
foreach(split(
"\
nAME: ,name|\
wHEN DESCENDING\
STAFFS CHARGE +,recharge|\
cASTS 」GHT\
tHROW TO START FIRE⁵gk\
sTOWING DOUSES FLAME,lit|\
,desc1|\
,desc2|\
,desc3|\
,desc4|\
aTTACK SHAPE: \|h●\+2i●\-k●\+2h♥⁵0k,arc|\
dARKSIGHT:\-s+,darksight|\
hEAL⁵fgTH: \-z,hp|/,maxhp|\
fREEZE TURNS:\-l,freezeturns|\
kNOCKBACK:\-v1,knockback|\
sTUN: \-x,stun|\
aCCURACY:\-v+,atk|\
dAMAGE: \-z,dmg|\
rANGE: \-y,range|\
aRMOR: \-z+,armor|\
tHROW RANGE: ,throw|\
tHROW ACC:\-q+,throwatk|\
tHROW DAMAGE:,throwdmg|\
⁵gkcHARGES: ,charges|/,maxcharges|\
⁵gk\fecURSED: cANNOT BE\
REMOVED; dESTROYED\
BY EMPOWERMENT,cursed",
"|"),function(str)
k,v=usplit(str)
local val,enchval=
selitem[v],
eMPOWER(v,true)
if val then
statstr..=k..val
if uimode=="eMPOWER" and
enchval
then
statstr..="\fc▶"..enchval.."\fd"
end
end
end)
else
statstr..="\n ????"
end
?statstr
--menu
?"\f1\-c……………………………",x+5,86
foreach (uimode and{uimode}or
{slot and
(equipped and
(lit and"eXTINGUISH" or "sTOW")
or"eQUIP")or "uSE",
"tHROW"}, function(action)
if listitem(action,nil,
cursed and equipped and not _g.uimode or
action=="uSE" and charges==0 or
action=="eQUIP" and player[slot]
and player[slot].cursed)
then
call"popdiag()popdiag()sfx(25"
player.skipturn=true
selitem[action]()
end
end)
end
function confirmjump()
frame(33,gettrans"32,37.5",95,gettrans"33,82.5",rect)
menuindex=getindex(2,menuindex)
?"\fd\|itHE HOLE OPENS\nUP BELOW YOU...\n\-d\|h\f1……………………"
if listitem" jUMP DOWN" then
popdiag()
player.move(playerdst)
elseif listitem" dON'T JUMP" then
popdiag()
end
end
function dialog(func,nosnd)
uitrans,menuindex=
mode=="ui" and 0.33 or 1,1
setmode"ui"
add(diags,func)
return nosnd or sfx"39"
end
function setmode(m)
at("statet:0,btns:0,mode:"..m,_ENV)
end
-->8
--[[tiles, rendering
flags:
0:navigable
1:passlight
2:xtraheight
3:infrontwall
4:genable
5:drawnormal
6:flippable
7:aggro on player step
8:navflying
9:flammable
10:flammable2
11:wall
12:bridge
13:freezable
14:ywall
15:hole
]]
function tile(_typ,_pos)
local _ENV=objtable"fow:1,fire:0,spores:0,newspores:0,hilight:0,hifade:0,light:-10,lflash:-10,lflashl:-10,death:41,adjtl:{}"
typ,pos,tlscrpos=
_typ,_pos,screenpos(_pos)
--tile member functions)
set=function(ntyp)
typ,bg,genned=ntyp
end
draw=function(_typ,postl,scrpos,offset,size,flp,_bg,_hilight)
dtyp=_typ or _bg and bg or typ
if frozen then
if fget(dtyp+1,5) then
dtyp,flp=56
else
pal(frozepal)
end
end
local xtraheight,litsprite=
fget(dtyp,2)and 5 or 0,dtyp+192
--lighting
for i=0,6 do
if not _bg and fow==4 and
fget(litsprite,i) then
local adjtile=postl.adjtl[i]
if adjtile and adjtile.lightsrc then
dtyp=litsprite
pal(8,adjtile.lcool and 13 or 4)
pal(9,adjtile.lcool and 12 or 9)
end
end
end
sspr(dtyp%16*8+offset.x,
dtyp\16*8+
offset.y-xtraheight,
size.x,size.y+xtraheight,
scrpos.x,scrpos.y-xtraheight,
size.x,size.y+xtraheight,flp)
if _hilight then
local _ENV=postl
hifade+=mid(-1,hilight-hifade,1)
if hifade>0 then
call"pal(2,34,2"
spr(hifade*16-8,scrpos.x,scrpos.y,2,1)
end
hilight=0
end
end
drawents = function()
checkeffects()
for i,var in inext,tlentvars do
if (_ENV[var]) _ENV[var].draw()
end
end
initpal=function(fadefow)
pal()
palt(1)
local nfow=1
if fadefow then
if not fadetoblack then
if modeis"gameover" then
nfow=_ENV==player.tl and 3 or 1
elseif vistoplayer and
mode != "ui" and
(mode != "victory" or statet<6)then
nfow=light>=2 and 4 or 3
elseif explored then
nfow=2
end
end
fow+=mid(-1,nfow-fow,1)
end
if fow<4 then
fillp(lfillp)
pal(fowpals[fow],2)
else
fillp()
end
end
checkeffects=function()
function checkeffect(_typ,has)
if effect then
local _ENV = effect
if typ==_typ then
if alive and
not has then
setanim(deathanim)
alive,light=nil
end
elseif has then
destroy(_ENV)
end
end
if not effect and has then
create(_typ,pos)
end
end
local hasfire=fire>0 or
ent and
ent.statuses.BURN
_g.anyfire=_g.anyfire or hasfire and mode!="ui" and mode!="victory"
checkeffect(138,hasfire)
checkeffect(139,spores>0 and not hasfire)
end
tileflag=function(i)
return fget(typ+i\8,i%8)
end
navigable=function(flying)
return tileflag(flying and 8 or 0)
end
flatten=function()
if typ==tlonggrass then
typ=tflatgrass
end
end
setfire=function(clearspores)
fire,frozen=
max(fire,1)
if clearspores then
spores,newspores=0,0
end
entfire()
end
sporeburst=function(val)
spores+=val
sfx"17"
end
entfire=function()
if ent and
not ent.nofire then
ent.burn()
end
end
freeze=function(turns)
frozen,fire=true,0
flatten()
local _ENV = ent
if _ENV then
if hitfire then
setanim"brazierdeath"
end
statuses.BURN=--nil
setstatus("FROZEN,"..turns..","..turns..",13,6")
animtext"○"
_g.aggro(tl)
end
end
visitadjrnd=function(func)
local neighbors={unpack(adjtl)}
while #neighbors>0 do
func(del(neighbors,rnd(neighbors)),0.5)--token saving hack for trysetfire
end
end
--end tile member functions
return _ENV
end
function setupdrawcalls()
alltiles(
function(_ENV)
local palready=nil
function tdraw(tltodraw,postl,i,_bg)
if not palready then
add(drawcalls,{initpal,{true}})
palready=true
end
local _typ,flp=
i and (bg and tltodraw.bg or
tltodraw.typ),
flip
if not i and tltodraw.tileflag"14" then
_typ=tdunjfloor
end
local baseoffset,offsets,sizes=unpack(specialtiles[i and _typ or "default"])
local offset=
offsets[i or 1]
--special tiles
if i then
if _typ==tywall and
not postl.altwall then
baseoffset+=vec2s"-6,-2"
elseif _typ==thole then
_typ+=192
if i>3 then
_typ += 2--brick hole
flp=false
baseoffset+=vec2s"0,1"
end
end
flp=flp and i%3==2
end
add(drawcalls,{draw,
{_typ,postl,
postl.tlscrpos+offset+baseoffset,
offset,sizes[i or 1],
flp and
tltodraw.tileflag"6", _bg,
not(i or _bg)}})
end
local infront,uprtl=fget(typ,3),adjtl[3]
if bg then
tdraw(_ENV,_ENV,nil,true)
end
if not infront and
tileflag"5" or
tileflag"14" and
altwall then
tdraw(_ENV,_ENV)
end
for k,i in inext,split"2,1,3,4,5,6" do
if infront and i==4 then
tdraw(_ENV,_ENV)
end
local _adjtl=adjtl[i]
if _adjtl then
local adjtyp = _adjtl.typ
if typ!=tcavewall and
typ!=tempty and
adjtyp==tcavewall
then
tdraw(_adjtl,_ENV,i)
elseif i<=2 and
_adjtl.tileflag"11"
then
walltl=adjtl[i]
if adjtyp==tywall and
i==1 and
not walltl.altwall
then
tdraw(_adjtl,walltl)
end
tdraw(_adjtl,walltl,i)
end
if (tileflag"15" or
bg==thole) and
i<=3 and
adjtyp!=thole and
_adjtl.bg!=thole
then
tdraw(_ENV,_ENV,i+
(_adjtl.manmade and 3 or 0),--brick hole
bg==thole)--bridges
end
end
end
if uprtl and
uprtl.tileflag"8" then
add(drawcalls,{uprtl.drawents,{}})
end
end)
end
-->8
--map stuff
function gettile(pos)
local y=world[pos.y]
return y and y[pos.x]
end
function inbounds(pos)
local y=tileinbounds[pos.y]
return y and y[pos.x]
end
function rndtl()
return rnd(inboundtls)
end
function alltiles(func)
foreach(validtiles,func)
end
function dijkstra(var,tovisit,flag)
while #tovisit>0 do
local tl=deli(tovisit,1)
local d=tl[var]-1
for k,_ENV in inext,tl.adjtl do
if _ENV[var]<d then
_ENV[var]=d
if fget(typ,flag) then
add(tovisit,_ENV)
end
end
end
end
end
function calcdist(var,tl,mindist)
tl=tl or player.tl
alltiles(
function(ntl)
ntl[var]=mindist or-1000
end)
tl[var]=0
dijkstra(var,{tl},0)
end
function viscone(pos,dir1,dir2,lim1,lim2,d)
pos+=dir1
local lastvis,notfirst=false
for i=ceil(lim1),flr(lim2) do
local _ENV=gettile(pos+i*dir2)
if _ENV then
local _vis,splitlim=
tileflag"1"
vis=tileflag"5" or
tileflag"14" and
player.pos.x<
pos.x
if _vis then
if notfirst and
not lastvis then
lim1=i-0.5
end
if i==flr(lim2) then
splitlim=lim2
end
elseif lastvis then
splitlim=i-0.5
end
if splitlim then
local expand=(d+1)/d
viscone(pos,dir1,dir2,
expand*lim1,
expand*splitlim,
d+1)
end
lastvis,notfirst=
_vis,true
end
end
end
function calcvis()
alltiles(
function(_ENV)
vis=ent==player
end)
for i,offs in inext,adj do
viscone(player.pos,offs,adj[(i+1)%6+1],usplit"0,1,1")
end
end
function calclight(nop,checkburn,clearflash,despawn)
local tovisit={}
alltiles(
function(_ENV)
light=lflash
if clearflash then
lflash,lflashl=-10,-10
else
light=max(light,lflashl)
end
lcool=light>2
for i,var in inext,tlentvars do
local nent =_ENV[var]
if nent then
local tlight=nent.stat"light"
if tlight and tlight>light then
light,lcool=
tlight,lcool or nent.lcool
end
end
end
lightsrc=light>2
if light>0 then
add(tovisit,_ENV)
end
end)
dijkstra("light",tovisit,1)
if checkburn then
repeat
local noburn=true
for _ENV in all(ents) do
if hp and
stat"burnlight" and
tl.light>=2 and
not statuses.BURN
then
if despawn and nolightspawn then
destroy(_ENV)
else
burn()
trysetfire(tl)
tl.light,noburn=4
dijkstra("light",{tl},1)
sfx"36"
end
end
end
until noburn
end
alltiles(
function(_ENV)
local darks=player.stat"darksight"
vistoplayer=vis and (light>-darks
or pdist>-2-darks)
explored=explored or vistoplayer
end)
end
function trysetfire(_ENV,p)
frozen=nil
if tileflag"10" or
spores>0 or
rndp(p or 1) and
(tileflag"9" or
ent and
ent.flammable)
then
setfire"true"
end
end
function updateenv()
alltiles(
function(_ENV)
if spores>0 then
spores=max(spores-rnd"0.25")
if spores>1 then
local adjtls={}
visitadjrnd(
function(_ENV)
if tileflag"8" and fire==0 then
add(adjtls,_ENV)
end
end)
local portion=spores/(#adjtls+1)
newspores-=spores-portion
for i,_ENV in inext,adjtls do
newspores+=portion
end
end
end
end)
alltiles(
function(_ENV)
if fire>=2 then
entfire()
visitadjrnd(trysetfire)
if tileflag"9" then
if rndp"0.2" then
fire,typ=0,34
end
elseif spores==0 then
fire=0
if tileflag"10" then
typ=thole
checkfall(ent)
checkfall(item)
end
end
end
end)
alltiles(
function(_ENV)
spores+=newspores
newspores=0
if fire>=1 then
fire+=1
setfire"true"
end
checkeffects()
end)
end
function findfree(tl,var,distlimit)
calcdist("free",tl,distlimit)
local bestd,bestpos=distlimit or-1000
alltiles(
function(_ENV)
local d=free-rnd()
if navigable() and
not _ENV[var] and
d>bestd then
bestd,bestpos=
d,pos
end
end)
return bestpos
end
-->8
--utility
function screenpos(pos)
return vec2(pos.x*12,
pos.y*8+pos.x*4)
end
function usplit(...)
return unpack(split(...))
end
function round(x)
local rnded=flr(x+0.5)
return rnded,x-rnded
end
--adapted from redblobgames.com/grids/hexagons
function hexdist(p1,p2)
local delta=p1+-p2
return (abs(delta.x)+abs(delta.y)+abs(p1.x+p1.y-p2.x-p2.y))/2
end
function hexline(p1,p2,range,linemode,cont)
local ln,dist={},hexdist(p1,p2)
for i=1,min(cont and 20 or dist,range) do
local tl=gettile(hexnearest(
lerp(p1,p2,i/dist)))
if not tl or
not tl.tileflag"8" or
tl.ent and linemode=="block" then
break
end
add(ln,tl)
if tl.ent and linemode!="pass" then
break
end
end
return ln,#ln>=dist
end
--adapted from observablehq.com/@jrus/hexround
function hexnearest(pos)
local roundx,remx=round(pos.x)
local roundy,remy=round(pos.y)
return abs(remx) > abs(remy) and
vec2(roundx+round(remx+remy/2),roundy) or
vec2(roundx,roundy+round(remy+remx/2))
end
function hexdir(p1,p2)
local dir=hexnearest(
lerp(p1,p2,1/hexdist(p1,p2)))+-p1
for i,d in inext,adj do
if (d==dir) return dir,i
end
end
function vec2s(str)
return vec2(usplit(str))
end
function vec2list(str)
local ret={}
for i,vec in inext,split(str,"|") do
add(ret,vec2s(vec))
end
return ret
end
function at(str,table,delim1,delim2)--assign table
table = table or {}
for i,var in
inext,split(str,delim1)
do
local k,v=usplit(var,delim2 or ":")
table[k]=v=="{}"and {} or v
end
return table
end
function objtable(str)
return setmetatable(at(str),{__index=_ENV})
end
function call(str)
for i,s in inext,split(str,")")do
local func,args=unpack(split(s,"(",false))
_ENV[func](usplit(args))
end
end
function rndint(maxval)
return flr(rnd(maxval))
end
function rndp(p)
return rnd()<tonum(p)
end
function lerp(a,b,t)
return a and (1-t)*a+t*b or b
end