forked from icegoat9/picohaven
-
Notifications
You must be signed in to change notification settings - Fork 1
/
picohaven100e_minify.lua
1684 lines (1684 loc) · 41.3 KB
/
picohaven100e_minify.lua
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
function _init()
dlvl=2
initmspr()
initglobals()
initdbs()
initpersist()
initlevel()
palt(0b0000000000000010)
music(1,1000)
changestate("splash")
end
function _draw()
local s=128-2*wipe
clip(wipe,wipe,s,s)
_drwstate()
clip()
end
function _update60()
if animt<1 then
_updonlyanim()
else
shake=0 --turn off screenshake if it was on (e.g. due to "*2" mod card drawn)
for a in all(actor) do
if (a.hp<=0 and a!=p) a.x=-99
end
if btnp(❎) and not msgreview and msgreviewenabled then
msgreview,_updprev,_updstate=true,_updstate,_updscrollmsg
else
_updstate()
end
end
_updtimers()
end
function _updtimers()
fram+=1
afram=flr(fram/act_td)%4
wipe=max(0,wipe-5)
if fram % msg_td==0 and #msgq>4 and msg_yd<(#msgq-4)*6 and not msgreview then
msg_yd+=1
end
end
function _updonlyanim()
animt=min(animt+animtd,1)
for a in all(actor) do
a.ox,a.oy=a.sox*(1-animt),a.soy*(1-animt)
if animt==1 then
a.sox,a.soy=0,0
if (a.ephem) del(actor,a)
end
end
end
function changestate(_state,_wipe)
prevstate=state
state=_state
selvalid,showmapsel=false,false
selx,sely,seln=1,1,1
msgreviewenabled=false
wipe = _wipe or 63
msg_x0,msg_w=0,map_w
if (initfn[_state]) initfn[state]()
end
function _upd🅾️()
if (btnp(🅾️)) changestate(nextstate)
end
function initnewlevel()
initlevel()
if (prevstate!="splash") music(0)
mapmsg=pretxt[dlvl]
addmsg("\fc🅾️\f6:begin")
nextstate,_updstate,_drwstate="newturn",_upd🅾️,_drawlvltxt
end
function _drawlvltxt()
clsrect(0)
drawstatus()
drawmapframe()
printwrap(mapmsg,21,4,10,6)
drawheadsup()
drawmsgbox()
end
function initnewturn()
clrmsg()
addmsg("\f7----- new round -----\narrows:inspect enemies\n\fc🅾️\f6:choose action cards")
selx,sely,showmapsel=p.x,p.y,true
_updstate,_drwstate=_updnewturn,_drawmain
end
function _updnewturn()
selxy_update_clamped(10,10,0,0)
if (btnp(🅾️)) changestate("choosecards")
end
function selxy_update_clamped(xmax,ymax,xmin,ymin)
xmin,ymin = xmin or 1, ymin or 1
for i=1,4 do
if btnp(i-1) then
selx+=dirx[i]
sely+=diry[i]
break --only allow one button to be enabled at once, no "diagonal" moves
end
end
selx,sely=min(max(xmin,selx),xmax),min(max(ymin,sely),ymax)
seln=(selx-1)*ymax+sely
end
function initchoosecards()
tpdeck={}
for crd in all(pdeck) do
add(tpdeck,crd)
end
refresh(longrestcrd)
add(tpdeck,longrestcrd)
add(tpdeck,splt(";confirm;1;\nconfirm\n\n\f6confirm\nthe two\nselected\ncards"))
addmsg("select 2 cards to play\n(or rest+card to burn)\n\fc🅾️\f6:select\n\fc❎\f6:review map")
p.crds={}
_updstate,_drwstate=_updhand,_drawhand
end
function _updhand()
selxy_update_clamped(2,(#tpdeck+1)\2)
if (seln>#tpdeck) sely-=1
if btnp(🅾️) then
local selc=tpdeck[seln]
if selc[3]==0 then
if indextable(p.crds,selc) then
del(p.crds,selc)
else
if selc[2]=="rest" then
p.crds={}
end
if seln==#tpdeck then
for c in all(p.crds) do
c[3]=1
end
pdeckbld(p.crds)
changestate("precombat")
elseif #p.crds<2 then
add(p.crds,selc)
if tutorialmode then
if (#p.crds==1) addmsg("\f7initiative\f6 will be \f7"..selc[1].."\f6.\n (low init: act first)\nnow select 2nd card.")
if (#p.crds==2) addmsg("select \f7confirm\f6 if done.")
end
end
end
tpdeck[#tpdeck][3] = #p.crds==2 and 0 or 1
end
elseif btnp(❎) then
changestate("newturn")
end
end
function _drawhand()
clsrect(5)
print("\f6your deck:\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\*f \*7 \+fdlegend:",8,14)
drawcard("discard",94,108,1)
drawcard("burned",94,118,2)
local tp1,tp2=splitarr(tpdeck)
drawcardsellists({tp1,tp2},0,19,p.crds,9)
if (#p.crds<1) printmspr("\f61st card chosen\nsets \f7initiative\f6\n for turn◆",67,4)
drawmsgbox()
end
function pdeckbld(clist)
if clist[1][2]=="rest" then
restburnmsg="\f8burned\f6 ["..clist[2][2].."]"
clist[2][3]=2
deli(clist,2)
else
add(clist,{0,hasitem("swift") and "😐3" or "😐2"})
add(clist,{0,hasitem("barbs") and "█2∧" or "█2"})
end
end
function initprecombat()
selectenemyactions()
ilist,initi=initiativelist(),1
addmsg("enemy actions drawn\n\fc🅾️\f6:continue to combat")
nextstate,_updstate,_drwstate="actloop",_upd🅾️,_drawmain
end
function selectenemyactions()
local etypes=activeenemytypes()
for et in all(etypes) do
et.crds = rnd(enemydecks[et.id])
et.init = et.crds[1][1]
end
for a in all(actor) do
if (a.type) a.crds=a.type.crds
a.init=a.crds[1][1]
a.crdi=1
end
end
function activeenemytypes()
local etypes={}
for a in all(actor) do
if (a!=p and not(indextable(etypes,a.type))) add(etypes,a.type)
end
return etypes
end
function initactloop()
_updstate=_updactloop
_drwstate=_drawmain
end
function _updactloop()
if p.hp<=0 then
loselevel()
return
end
if initi>#ilist then
changestate("cleanup",0)
return
end
actorn=ilist[initi][2]
local a=actor[actorn]
initi+=1
if (a.hp<1) return
if a==p and p.crds[1]==longrestcrd then
longrest()
p.stun=nil
elseif a.stun then
addmsg(a.name..": ▥, turn skipped")
a.stun=nil
else
if (a==p) then
changestate("actplayerpre",0)
else
changestate("actenemy",0)
end
end
end
function initactenemy()
_updstate=_updactenemy
_drwstate=_drawmain
end
function _updactenemy()
local e=actor[actorn]
if e.crdi>#e.crds then
changestate("actloop",0)
return
end
e.crd=parsecard(e.crds[e.crdi][2])
e.crdi+=1
if e.crd.act==sh("m") then
if e.crdi<=#e.crds then
local nextcrd=parsecard(e.crds[e.crdi][2])
if (nextcrd.act==sh("a")) e.crd.rng=nextcrd.rng
end
end
runcard(e)
end
function summon(a)
local smn=a.type.summon
local neighb=valid_emove_neighbors(a,true)
if #neighb>0 then
local smnxy=rnd(neighb)
initenemy(smn,smnxy.x,smnxy.y)
addmsg(a.name.." \f8calls\f6 "..enemytype[smn].name.." (\f8-2♥\f6)")
dmgactor(a,2)
end
end
function enemymoveastar(e)
mvq = trimmv(pathfind(e,p),e.crd.val,e.crd.rng)
if not mvq or #mvq<=1 then
mvq = trimmv(pathfind(e,p,false,true),e.crd.val,e.crd.rng)
end
changestate("animmovestep",0)
end
function trimmv(_mvq,mvval,rng)
if (not _mvq) return _mvq
local trimto
for i,xy in ipairs(_mvq) do
local v=validmove(xy.x,xy.y,true)
if i==1 or v and i<=(mvval+1) then
trimto=i
if (dst(xy,p)<=rng and pseudolos(xy,p)) break
end
end
return {unpack(_mvq,1,trimto)}
end
function validmove(x,y,endat,jmp,actorn,allyblocks)
if (fget(mget(x,y),1) or isfogoroffboard(x,y)) return false
if (fget(mget(x,y),2) and (endat or not jmp)) return false
local ai=actorat(x,y)
if (endat and ai>0 and actorn!=ai) return false
if ((allyblocks or actorn==1) and ai>1 and not jmp) return false
return true
end
function valid_emove_neighbors(node,endat,jmp,allyblocks)
local neighbors = {}
for i=1,4 do
local tx,ty=node.x+dirx[i], node.y+diry[i]
if validmove(tx,ty,endat,jmp,nil,allyblocks) then
add(neighbors, xylst(tx,ty))
end
end
return neighbors
end
function runattack(a,d)
local crd=a.crd
local basestun,basewound=crd.stun,crd.wound
local dmg=crd.val
if a==p then
local mod=modcard()
if mod=="*2" then
dmg*=2
shufflemod()
shake=3
elseif mod=="/2" then
dmg\=2
shufflemod()
else
if sub(mod,-1)=="▥" then
crd.stun=true
mod=sub(mod,1,#mod-1)
elseif sub(mod,-1)=="∧" then
crd.wound=true
mod=sub(mod,1,#mod-1)
end
dmg+=tonum(mod)
end
addmsg("you draw modifier \f7"..mod)
end
sfx(12)
local msg=a.name.." █ "..d.name..":"
if d==p and hasitem("shld",true) then
p.shld+=2
addmsg("\f7great shield used\f6: +★2")
end
if d.shld>0 then
msg..=dmg.."-"..d.shld.."★:"
dmg=max(0,dmg-d.shld)
end
msg..="\f8-"..dmg.."♥\f6"
if a.crd.stun then
msg..="▥"
d.stun=true
end
if a.crd.wound then
msg..="∧"
d.wound=true
end
crd.stun,crd.wound=basestun,basewound
addmsg(msg)
local aspr=144+dmg
if (dmg>9) aspr=154
queueanim(nil,d.x,d.y,a.x,a.y,aspr)
dmgactor(d,dmg)
end
function modcard()
if #pmoddeck==0 then
shufflemod()
end
local c = rnd(pmoddeck)
add(pmoddiscard,c)
del(pmoddeck,c)
return c
end
function enemyattack(e)
if dst(e,p) <= e.crd.rng and pseudolos(e,p) then
runattack(e,p)
end
end
function healactor(a,val)
local heal=min(val,a.maxhp-a.hp)
a.hp+=heal
addmsg(a.name.." healed \f8+"..heal.."♥")
a.wound=nil
end
function dmgactor(a,val)
a.hp-=val
if a.hp<=0 then
if a==p then
if hasitem("life",true) then
a.hp,a.wound=1,false
addmsg("\f7your life charm glows\n and you survive @ 1hp")
else
local crd=rnd(cardsleft())
if crd then
crd[3]=2
a.hp+=val
addmsg("you \f8burn\f6 a random card\n\f8[\f6"..crd[2].."\f8]\f6 to avoid death")
end
end
else
addmsg("\f7"..a.name.." is defeated!")
mset(a.x,a.y,36)
p.xp+=1
end
end
end
function initactplayerpre()
p.actionsleft=2
changestate("actplayer",0)
end
function initactplayer()
checktriggers(p)
if (p.actionsleft == 0) then
p.crds,p.init=nil
changestate("actloop",0)
return
end
addmsg("\fc⬆️⬇️,🅾️\f6:choose card "..3-p.actionsleft)
if (tutorialmode) addmsg(" or dflt act █2 / 😐2 ◆")
_updstate=_updactplayer
_drwstate=_drawmain
end
function _updactplayer()
selxy_update_clamped(1,#p.crds)
if btnp(🅾️) then
local crd=p.crds[sely]
crdplayed,crdplayedpos=crd,indextable(p.crds,crd)
p.crd=parsecard(crd[2])
if (hasitem("goggl") and p.crd.rng and p.crd.rng>1) p.crd.rng+=2
p.actionsleft -= 1
runcard(p)
if (p.crd.burn) crd[3]=2
del(p.crds,crd)
end
end
function runcard(a)
local crd=a.crd
if crd.act==sh("m") then
if a==p then
changestate("actplayermove",0)
else
enemymoveastar(a)
end
elseif crd.aoe==8 then
local aoepat=splt3d("x;-1;y;-1|x;0;y;-1|x;1;y;-1|x;-1;y;0|x;1;y;0|x;-1;y;1|x;0;y;1|x;1;y;1",true)
foreach(aoepat,pdeltatoabs)
foreach(aoepat,pattackxy)
changestate("actplayer",0)
elseif crd.act==sh("a") then
if a==p then
changestate("actplayerattack",0)
else
enemyattack(a)
end
else
if (crd.act==sh("h")) healactor(a,crd.val)
if crd.act==sh("s") then
a.shld+=crd.val
addmsg(a.name.." ★+"..crd.val)
elseif crd.act==sh("l") and a==p then
addmsg("looting treasure @➡️"..crd.val)
rangeloot(crd.val)
elseif crd.act=="hail▒" then
foreach(inrngxy(p,crd.rng),pattackxy)
elseif crd.act=="howl" then
addmsg(a.name.." howls.. \f8-1♥,▥")
dmgactor(p,1)
p.stun=true
elseif crd.act=="call" then
summon(a)
end
if (a==p) changestate("actplayer",0)
end
if (crd.burn) p.xp+=2
end
function pdeltatoabs(xy)
xy.x+=p.x
xy.y+=p.y
end
function pattackxy(xy)
local ai=actorat(xy.x,xy.y)
if ai>1 then
runattack(p,actor[ai])
else
queueanim(nil,xy.x,xy.y,p.x,p.y,2)
end
end
function inrngxy(a,r)
local inrng={}
for i=-r,r do
for j=-r,r do
local tx,ty=a.x+i,a.y+j
local txy=xylst(tx,ty)
if (not isfogoroffboard(tx,ty) and dst(a,txy)<=r and pseudolos(a,txy)) add(inrng,txy)
end
end
return inrng
end
function longrest()
p.actionsleft=0
addmsg("you take a \f7long rest\f6:")
foreach(pdeck,refresh)
foreach(pitems,refresh)
healactor(p,3)
addmsg(restburnmsg)
end
function loot(x,y)
if fget(mget(x,y),5) then
if mget(x,y)==36 then
p.gold+=gppercoin
addmsg("picked up "..gppercoin.."● (gold)")
elseif mget(x,y)==37 then
if dlvl==15 then
lootedchests+=1
addmsg("you find a map piece!")
else
local tr=rnd(rndtreasures[difficulty])
local tt,tv=tr[1],tr[2]
if tt=="g" then
p.gold+=tv
addmsg("you find "..tv.."●!")
elseif tt=="d" then
addmsg("chest is trapped! \f8-"..tv.."♥")
dmgactor(p,tv)
end
end
end
mset(x,y,33)
end
end
function rangeloot(r)
for xy in all(inrngxy(p,r)) do
loot(xy.x,xy.y)
end
end
function initactplayermove()
showmapsel=true
selx,sely=p.x,p.y
mvq={xylst(selx,sely)}
if (hasitem("belt")) p.crd.jmp=true
addmsg("move up to "..p.crd.val)
if (tutorialmode) addmsg(" (\fc🅾️\f6:confirm, \fc❎\f6:undo)")
_updstate=_updactplayermove
_drwstate=_drawmain
end
function _updactplayermove()
local selx0,sely0=selx,sely
selxy_update_clamped(10,10,0,0)
if selx!=selx0 or sely!=sely0 then
if #mvq>=2 and mvq[#mvq-1].x==selx and mvq[#mvq-1].y==sely then
deli(mvq,#mvq)
elseif #mvq>p.crd.val or not validmove(selx,sely,false,p.crd.jmp,1) then
selx,sely=selx0,sely0
else
add(mvq,xylst(selx,sely))
end
end
selvalid = (#mvq-1) <= p.crd.val and validmove(selx,sely,true,false,1)
if btnp(🅾️) then
if selvalid then
if (#mvq>1) sfx(11)
changestate("animmovestep",0)
else
addmsg("invalid move")
end
elseif btnp(❎) then
undoactplayer()
end
end
function undoactplayer()
p.actionsleft+=1
mvq={}
crdplayed[3]=1
add(p.crds,crdplayed,crdplayedpos)
changestate("actplayer")
end
function initanimmovestep()
a=actor[actorn]
if not mvq or #mvq<=1 then
mvq={}
if actorn==1 then
changestate("actplayer",0)
else
changestate("actenemy",0)
end
else
local x0,y0=mvq[1].x,mvq[1].y
local xf,yf=mvq[2].x,mvq[2].y
queueanim(a,xf,yf,x0,y0)
_updstate=_updanimmovestep
checktriggers(a,a.crd.jmp)
end
end
function checktriggers(a,jmp)
local ax,ay=a.x,a.y
if fget(mget(ax,ay),4) then
if mget(ax,ay)==43 and not jmp then
addmsg(a.name.." @ trap! \f8-"..trapdmg.."♥")
dmgactor(a,trapdmg)
mset(ax,ay,33)
end
if fget(mget(ax,ay),7) then
for i=1,4 do
unfogroom(ax+dirx[i],ay+diry[i])
end
initactorxys()
doorsleft-=1
mset(ax,ay,33)
end
end
end
function _updanimmovestep()
deli(mvq,1)
changestate("animmovestep",0)
end
function initactplayerattack()
showmapsel=true
selx,sely=p.x,p.y
addmsg("select attack target")
if (tutorialmode) addmsg(" (\fc🅾️\f6:confirm, \fc❎\f6:undo)")
_updstate=_updactplayerattack
_drwstate=_drawmain
end
function _updactplayerattack()
selxy_update_clamped(10,10,0,0)
local xy=xylst(selx,sely)
local d=dst(p,xy)
local crd=p.crd
selvalid = d <= crd.rng and d>0 and not isfogoroffboard(selx,sely) and pseudolos(p,xy)
if btnp(🅾️) then
if selvalid then
pattackxy(xy)
changestate("actplayer",0)
else
addmsg(" invalid target")
end
elseif btnp(❎) then
undoactplayer()
end
end
function initcleanup()
for a in all(actor) do
clearcards(a)
if (a.type) clearcards(a.type)
if a.wound and a.hp>0 then
addmsg(a.name.." ∧:\f8-1♥")
dmgactor(a,1)
end
a.shld=a.pshld
checktriggers(a)
if (a.hp<=0 and a!=p) del(actor,a)
end
loot(p.x,p.y)
local bossn=indextable(actor,"noah","name")
if dlvl==18 and eventtrigger() and not bossphase2 then
local bossa=actor[bossn]
bossphase2=true
bossa.type.id+=1
addmsg("\fcthe blue barriers\n \fcdissipate and noah\n \fchowls with rage.")
removebarriers()
summon(bossa)
end
if (#actor==1 and doorsleft==0) or (eventtrigger() and (dlvl==12 or dlvl==15)) or (dlvl==18 and not bossn) then
removebarriers()
winlevel()
elseif checkexhaust() then
loselevel()
else
if #cardsleft()<2 then
local burned=shortrestdeck()
addmsg("\fchand empty\f6: you short\nrest, redraw, and \f8burn\nrandom card: \f8[\f6"..burned.."\f8]")
end
addmsg("\fc❎\f6:'review msg' mode\n\fc🅾️\f6:next round")
msgreviewenabled=true
nextstate,_updstate="newturn",_upd🅾️
_drwstate=_drawmain
end
end
function removebarriers()
for i=0,10 do
for j=0,10 do
if (mget(i,j)==48) mset(i,j,33)
end
end
end
function eventtrigger()
if dlvl==12 or dlvl==18 then
return not indextable(actor,"rune","name")
elseif dlvl==15 then
return lootedchests==4
end
end
function shortrestdeck()
foreach(pdeck,refresh)
local crd=rnd(cardsleft())
crd[3]=2
return crd[2]
end
function cardsleft(incldiscrds)
local crds={}
for crd in all(pdeck) do
if (crd[3]==0 or (incldiscrds and crd[3]==1)) add(crds,crd)
end
return crds
end
function _updscrollmsg()
if btnp(❎) or btnp(🅾️) then
_updstate,msgreview=_updprev
elseif btn(⬆️) then
msg_yd=max(msg_yd-1,0)
elseif btn(⬇️) and #msgq>4 then
msg_yd=min(msg_yd+1,(#msgq-4)*6)
end
end
function initendlevel()
decksreset()
addmsg("\fc🅾️\f6:end of scenario")
nextstate,_updstate="pretown",_upd🅾️
end
function initpretown()
addmsg("\fc🅾️\f6:return to town")
nextstate,_drwstate="town",_drawlvltxt
_updstate=_upd🅾️
end
function winlevel()
local l=lvls[dlvl]
p.xp+=l.xp
p.gold+=l.gp
addmsg("\f7 victory! \f6(+"..l.xp.."xp)")
if (l.gp>0) addmsg(" you are paid ●"..l.gp)
mapmsg=fintxt[dlvl]
l.unlocked=false
for u in all(split(l.unlocks)) do
if (u!="") lvls[tonum(u)].unlocked=true
end
if (dlvl==18) wongame=true
if (dlvl==14) add(pitems,slvrstl)
changestate("endlevel",0)
end
function checkexhaust()
if (p.hp<=0) return true
if (#cardsleft(true)<2) return true
if (#cardsleft(true)==2 and #cardsleft()<2) return true
end
function loselevel()
addmsg("\f8you are exhausted")
mapmsg="defeated, you hobble back to town to nurse your wounds and plan a return."
changestate("endlevel",0)
end
function clsrect(c)
rectfill(0,0,127,127,c)
end
function _drawmain()
clsrect(0)
drawstatus()
drawmapframe()
drawmap()
drawheadsup()
drawmsgbox()
end
function drawstatus()
print(sub(lvls[dlvl].name,1,15),0,0,7)
printmspr("♥"..p.hp.."/"..p.maxhp,66,0,8)
end
function drawmsgbox()
local c=msgreview and 12 or 13
rectborder(msg_x0,99,msg_x0+msg_w-1,127,5,c)
clip(max(msg_x0,wipe)+1,
max(101,wipe),
min(msg_x0+msg_w,127-2*wipe)-max(msg_x0,wipe)-2,
126-2*wipe-max(101,wipe))
textboxm(msgq,msg_x0,99-msg_yd,msg_w,29,2,5)
clip()
if (msgreview) printmspr("\fcU\n\+04X\n\+06D",msg_x0+msg_w-4,100)
end
function drawmapframe()
rectborder(0,6,91,97,0,5)
end
function drawmap()
camera(rnd(shake),rnd(shake))
for i=0,10 do
for j=0,10 do
sprn=mget(i,j)
if (fget(mget(i,j),3)) sprn+=afram
if (isfogoroffboard(i,j)) sprn=39
spr(sprn,2+8*i,8+8*j)
end
end
camera(0,0)
foreach(actor,drawactor)
if #mvq>1 then
local x0,y0=mvq[1].x,mvq[1].y
for mv in all(mvq) do
line(6+8*x0,12+8*y0,6+8*mv.x,12+8*mv.y,12)
x0,y0=mv.x,mv.y
end
circfill(6+8*x0,12+8*y0,1,12)
end
if (showmapsel) drawmapsel()
end
function drawactor(a)
local animfram = a.noanim and 0 or afram
if a.stun then
animfram=0
pal(splt("1;12;3;12;4;12;5;12;6;12;7;12;13;12",false,true))
end
spr(a.spr+animfram,2+8*a.x+a.ox,8+8*a.y+a.oy)
pal()
palt(0b0000000000000010)
if (a!=p and not a.ephem) drawactor(p)
end
function drawmapsel()
local mx,my=1+selx*8,7+sely*8
if (not selvalid) fillp(0x5a5a)
rect(mx,my,mx+9,my+9,12)
fillp()
end
function drawecards(x,y,n,sel)
local a=actor[n]
if (not sel) drawcardbase(x,y,a,sel)
local acrds=a.type.crds
if acrds and #acrds>=1 then
local strs={}
for crd in all(acrds) do
add(strs,crd[2])
end
textboxm(strs,x+10,y+10,25,15,nil,nil,1)
printmspr(a.type.init..":",x+2,y+15,7)
end
if (sel) drawcardbase(x,y,a,sel)
end
function drawcardbase(x,y,a,sel)
local str={"\+90"..a.name}
local c,h = 13,22
local hpstr = "?/"..a.maxhp
local st="\+91"
if (a.wound) st="\+01∧\+30"
if (a.shld>0) st..="★"..a.shld
if (a.stun) st..=" ▥"
if a==p then
h = 37
if (p.init) add(str,"\+13\f7"..p.init..":")
add(str,"\+03"..st)
if #pitems>0 then
add(str,"\+04items:")
st="\+04"
for it in all(pitems) do
st..=it[5]
end
add(str,st)
end
else
if sel then
hpstr=a.hp
a.type.hpdrawn=true
c=12
end
add(str,"\+90♥"..hpstr)
if (sel) add(str,st)
end
textboxm(str,x,y,33,h,nil,c)
spr(a.spr,x+2,y+2)
end
function drawpcards()
local hx,hy=hud_x0+2,hud_py+7
drawcardbase(hud_x0,hud_py,p)
if state=="precombat" or state=="actenemy" or state=="actloop" or state=="animmovestep" and actorn!=1 then
if p.crds then
for i=1,min(#p.crds,2) do
drawcard(p.crds[i],hx,hy+10*i)
end
end
elseif sub(state,1,9)=="actplayer" or state=="animmovestep" and actorn==1 then
for i,crd in ipairs(p.crds) do
local style=0
if (crd[1]==0) style=5
local cardsel=(i==sely and state=="actplayer")
drawcard(crd,hx,hy-10+10*i,style,cardsel)
fillp()
end
end
end
function drawcard(card,x,y,style,sel,lg,rawtext)
local strs=card
if not rawtext then
if lg then
strs=desccard(card)
else
if (type(card)=="table") strs=card[2]
end
end
local c1,c2,c3,cf,c4=13,1,6
local w,h,b=32,9,1
if (style==9) style=card[3]
if lg then
w,h,b=39,67,3
else
if (style==1) c1,c3,cf=0,5,true
if (style==2) c1,c2,c3,cf=0,0x82,0,true
if (style==3) c1=5
if (style==4) c1=12
if (style==5) c2=0
if (sel) c4=12
end
textboxm(strs,x,y,w,h,b,c1,c2,c3,cf,c4)
if (lg and not rawtext) then
line(x,y+18,x+w-1,y+18,c1)
circfill(x+w-2,y-1,7,c2)
circ(x+w-2,y-1,7,c1)
print(card[1],x+w-5,y-3,c3)
end
end
function desccard(card)
local crd=parsecard(card[2])
local strs={"\f7"..card[4],"",""}
if not crd.special then
addflat(strs,descact[crd.act]..crd.val)
if (crd.jmp) add(strs," jump")
if (crd.rng>1) add(strs," @ rng "..crd.rng)
if (crd.wound) add(strs," \f8wound")
if (crd.stun) add(strs," \fcstun")
if (crd.aoe) addflat(strs,"multiple\n targets")
if (crd.burn) add(strs,"\n\f8burn\f6 crd\n on use")
end
return strs
end
function drawheadsup()
local ehudn={}
for i,a in ipairs(actor) do
if a!=p and not a.ephem then
local enam=a.name
if (not indextable(ehudn,enam)) add(ehudn,enam)
local hy = hud_ey*indextable(ehudn,enam)-hud_ey
local selon = actorat(selx,sely)==i and showmapsel
if not a.type.hpdrawn then
drawecards(hud_x0,hy,i,selon)
end
end
end
for e in all(enemytype) do
e.hpdrawn=false
end
drawpcards()
end
function sh(ch)
return chr(ord(ch)+31)
end
function printmspr(str,x,y,c)
local xt,i=x,1
while i<=#str do
local ch=sub(str,i,i)
if ord(ch)==5 then
i+=1
xt+=tonum("0x"..sub(str,i,i))
i+=1
y+=tonum("0x"..sub(str,i,i))
elseif ord(ch)==12 then
i+=1
c=tonum("0x"..sub(str,i,i))
elseif ord(ch)==10 then
xt=x
y+=6
elseif minispr[ch] then
spr(minispr[ch],xt,y)
xt += 6
elseif ch==":" or ch==" " or ch=="." then
print(ch,xt-1,y,c)
xt += 3
if (ch==".") xt-=1
else
print(ch,xt,y,c)
xt += 4
if (ord(ch)>=128) xt += 4
end
i+=1
end
end
function printwrap(txt,w,x,y,c)
local txts=split(txt,"\n")
for txt in all(txts) do
while #txt>w do
local i=w+1
while sub(txt,i,i)!=" " do
i-=1
end
print(sub(txt,1,i),x,y,c)
txt=sub(txt,i+1)
y+=6
end
print(sub(txt,1,w),x,y,c)
y+=9
end
end
function textboxm(strs,x,y,w,h,b,c1,c2,c3,cf,c4)
b=b or 1
c1=c1 or 13
c2=c2 or 5
c3=c3 or 6
if (type(strs)!="table") strs={strs}
h=h or #strs*6+3
if (cf) fillp(0x5a5a)
rectborder(x,y,x+w-1,y+h-1,c2,c1)
fillp()
for i,str in ipairs(strs) do
printmspr(str,x+b+1,y+b-5+6*i,c3)
end
if (c4) rect(x-1,y-1,x+w,y+h,c4)
end
function drawcardsellists(clsts,x0,y0,sellst,style,spacing,modmode)
sellst = sellst or {}
spacing = spacing or 36
x0 = x0 or 0
lg=false
yd = style==3 and 8 or 10
for i=1,#clsts do
for j,crd in ipairs(clsts[i]) do
x=x0+8+(i-1)*spacing
y=y0+5+(j-1)*yd
local tstyle=style
if (indextable(sellst,crd)) tstyle=4
local selon = (i==selx and j==sely)
drawcard(crd,x,y,tstyle,selon)
end
end
if selx>0 and sely>0 then