forked from btco/so12l
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.lua
4193 lines (4158 loc) · 488 KB
/
cart.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
-- title: Shadow Over the Twelve Lands
-- author: Bruno Oliveira
-- desc: An RPG game
-- script: lua
-- saveid: SO12L
sin,cos,sqrt,floor,ceil,abs,min,max,random=math.sin,math.cos,math.sqrt,math.floor,math.ceil,math.abs,math.min,math.max,math.random
insert,remove,PI,prn=table.insert,table.remove,math.pi,print
ssub,supper,schr,ast=string.sub,string.upper,string.char,assert
function f6(dir,x,z)if not dir then return x,z end
z=z+(dir==0 and 1 or dir==2 and -1 or 0)x=x+(dir==1 and 1 or dir==3 and -1 or 0)return x,z
end
function y9(dir)return (dir+2)%4 end
function f7(x1,y1,x2,y2,x)if x2<x1 then return f7(x2,y2,x1,y1,x)end
if x<x1 then return y1 end
if x>x2 then return y2 end
return y1+(y2-y1)*(x-x1)/(x2-x1)end
function d9(x1,z1,x2,z2)return (x1-x2)*(x1-x2)+(z1-z2)*(z1-z2)end
function z5(x,z)local m=sqrt(x*x+z*z)return x/m,z/m
end
function a4(t)if type(t)~="table" then return t end
local r={}for k,v in pairs(t)do
if type(v)=="table" then
r[k]=a4(v)else
r[k]=v
end
end
return r
end
function i4(a,b)local result=a4(a)for k,v in pairs(b)do
if result[k]and type(result[k])=="table" and
type(v)=="table" then
result[k]=i4(result[k],v)else
result[k]=a4(v)end
end
return result
end
function e0(list,obj)local idx=b1(list,obj)if not idx then return nil end
remove(list,idx)return 1
end
function z6(list,idx)if idx<0 or idx>#list then return nil end
local r=list[idx]list[idx]=list[#list]remove(list,#list)return r
end
function b1(list,obj)for i=1,#list do
if list[i]==obj then return i end
end
return nil
end
function n3(l1,l2)local r={}for i=1,#l1 do insert(r,l1[i])end
for i=1,#l2 do insert(r,l2[i])end
return r
end
function i5(x,y,w,h,frac,fg,bg)if h>3 then i7(x,y,w,h,bg,fg)else rect(x,y,w,h,bg)end
rect(x,y,frac>0 and max(1,a8(w*frac,0,w))or 0,h,fg)end
function a8(x,lo,hi)return min(max(x,lo),hi)end
local n4={126,48,109,121,51,91,95,112,127,123}function f8(n,x,y,c)repeat
local f=n4[(n%10)+1]n=n//10
i6(f&64,x+1,y,c)f9(f&32,x+2,y+1,c)f9(f&16,x+2,y+3,c)i6(f&8,x+1,y+4,c)f9(f&4,x,y+3,c)f9(f&2,x,y+1,c)i6(f&1,x+1,y+2,c)x=x-4
until n<=0
end
function i6(f,x,y,clr)if f>0 then line(x-1,y,x+1,y,clr)end end
function f9(f,x,y,clr)if f>0 then line(x,y-1,x,y+1,clr)end end
function a2(text,cx,cy,clr,sc,ty)local w,h=prn(text,0,-10,clr,nil,sc or 1,ty),6
prn(text,cx-w//2,cy-h//2,clr or 15,nil,sc or 1,ty)end
function i7(x,y,w,h,clr,bclr)rect(x,y,w,h,clr)rectb(x,y,w,h,bclr)end
function d0(x,y,w,h,clr,title)rect(x+3,y+3,w,h,3)c3(x,y,w,h,clr)if title then
local tw=prn(title,0,-10)local tx=x+w//2-tw//2
rect(tx-5,y-4,tw+10,8,15)prn(title,tx,y-2,clr)end
end
function i8(title,items,x,y,fg,bg,idx,cen)local w,h=e1(items)cen=cen or g1(1,#items)w,h=w+16,h+16
x=x-w//2
if bg then d0(x,y,w,h,bg,title)end
for i=1,#items do
local iy,c=y+10+(i-1)*8,fg
c=idx==i and 14 or c
c=cen[i]and c or 3
prn(items[i],x+8,iy,c)_=idx==i and G.clk&16>0 and spr(248,x+2,iy-1,0)end
idx=F(1)and c4(idx,#items)or
F(0)and g0(idx,#items)or idx
for i=1,#items do
if cen[idx]then break end
idx=F(0)and g0(idx,#items)or c4(idx,#items)end
return idx
end
function c4(i,count)return i==count and 1 or i+1 end
function g0(i,count)return i==1 and count or i-1 end
function z7(d)return max(1,random(d//2,d))end
function U(msg,fg,bg,sfx,cb)msg=i9(msg)fg=fg or 15
bg=bg or 2
local w,h=e1(msg)w,h=w+16,h+12
local x,y=120-w//2,68-h//2
J(z8,{msg=msg,fg=fg,bg=bg,x=x,y=y,w=w,h=h,sfx=sfx or 4,cb=cb},1)end
function z8(st)d0(st.x,st.y,st.w,st.h,st.bg)if st.sfx then a3(st.sfx)st.sfx=nil end
for i=1,#st.msg do
prn(st.msg[i],st.x+8,st.y+(i-1)*8+8,st.fg)end
if F(4)or F(5)then
_=st.cb and st.cb()return 1
end
end
function e1(lines,mode)if not lines then return 0,0 end
lines=i9(lines)local w=0
for i=1,#lines do w=max(w,prn(lines[i],300,200,1,mode or false))end
return w,#lines*8
end
function b6(text,choices,cb,fg,bg,udata,fl,cen)local tw,th,cw,ch,s
fg,bg,fl=fg or 15,bg or 2,fl or 0
tw,th=e1(text)cw,ch=e1(choices)cw,ch=cw+16,ch+8
s={sel=1,choices=choices,text=text,cb=cb,udata=udata,w=max(tw,cw)+16,h=th+ch+16,fg=fg,bg=bg,fl=fl,cen=cen}if fl&2<1 then
s.x,s.y=120-s.w//2,68-s.h//2
else
s.x,s.y=240-s.w,136-s.h
end
s.mx,s.my=s.x+s.w//2,s.y+th+4
J(z9,s,1)return 1
end
function z9(s,isFg)if s.ended then
return 1
end
d0(s.x,s.y,s.w,s.h,s.bg,s.title)d1(s.text,s.x+8,s.y+8,s.fg)local nsel=i8(nil,s.choices,s.mx,s.my,s.fg,nil,s.sel,s.cen)s.sel=isFg and nsel or s.sel
if F(4)then
if s.cen and not s.cen[s.sel]then return nil end
s.ended=1
s.cb(s.sel,s.udata)return 1
end
if s.fl&1>0 and F(5)then
s.ended=1
s.cb(nil,s.udata)return 1
end
return nil
end
function i9(s)return type(s)=="string" and {s}or s
end
function d1(l,x,y,c,lim,mode)if not l then return end
lim=lim or 9999
l=i9(l)for i=1,#l do
if lim<1 then break end
local li=ssub(l[i],1,min(lim,#l[i]))prn(li,x,y+8*(i-1),c or 15,mode or false)lim=lim-#l[i]end
return lim>0
end
function n5(len)return g1(0,len)end
function g1(v,len)local r={}for i=1,len do insert(r,v)end
return r
end
function n6(x0,y0,w,h,clr)for y=y0,y0+h-1 do
for x=x0+y%2,x0+w-1,2 do
pix(x,y,clr)end
end
end
function g2(map)for i=0,15 do
poke4(0x3FF0*2+i,map and map[i]or i)end
end
function A0(c)for i=1,15 do poke4(0x3FF0*2+i,c)end
end
function A1()return F(3)and 1 or F(2)and -1 or 0,F(1)and 1 or F(0)and -1 or 0
end
function c2(v)return v~=0 and v or nil
end
function A2(from,to)local r={}for i=from,to do insert(r,i)end
return r
end
function b2(b)return b and 1 or 0 end
function A3(x1,z1,x2,z2)return x1*x2+z1*z2 end
function F(b)return G.bp[b]end
function A4(x,y,ic,w,fg,bg,f,warn)spr(ic,x,y)if warn and G.clk&64<1 and f<.2 then
prn(warn,x+10,y+2,fg)else
rect(x+10,y+2,w,4,0)rectb(x+10,y+2,w,4,fg)rect(x+10,y+2,w*f,4,fg)_=f<.2 and warn and rectb(x+10,y+2,w,4,6)end
end
function j0(x,y,tx,ty,d)d=d or 1
return x<tx and min(tx,x+d)or max(tx,x-d),y<ty and min(ty,y+d)or max(ty,y-d)end
function c3(x,y,w,h,bg)_=bg and rect(x,y,w,h,bg)rectb(x,y,w,h,15)rectb(x+1,y+1,w-2,h-2,4)spr(234,x,y,0,1,0)spr(234,x+w-8,y,0,1,1)spr(234,x,y+h-8,0,1,2)spr(234,x+w-8,y+h-8,0,1,3)end
A={
queue={},}function A5()A.queue={}end
function J(proc,state,afl)A7(proc,state,afl)end
function A6()if #A.queue<1 then return nil end
local lma=nil
for i=#A.queue,1,-1 do
if A.queue[i].afl&1>0 then lma=i break end
end
if lma then
local s=min(lma+1,#A.queue)for i=s,2,-1 do
(A.queue[i].proc)(A.queue[i],nil)end
end
local a=A.queue[1]if (a.proc)(a,1)then
remove(A.queue,b1(A.queue,a))else
a.clk=a.clk+1
end
return 1
end
function A7(proc,state,afl)ast(proc)state=state or {}state.proc=proc
state.clk=0
state.afl=afl or 0
if state.afl&1>0 then
insert(A.queue,1,state)else
insert(A.queue,state)end
end
B=nil
n7={
c={},ord={},t=nil,tac=0,nx=nil,q=nil,am=nil,rbad=nil,bgc=0,dsp=0,xp=0,gp=0,ltchi=0,sur=nil
}function n8(foes,vst)B=a4(n7)for i=1,6 do insert(B.c,{})end
for i=1,#PA do C7(i)end
ast(#foes<7)for i,e in ipairs(foes)do
C8(e,4+(i-1)//2)B.xp,B.gp=B.xp+e.xp,B.gp+e.gp
end
a1(6728,{B.gp,B.xp})B.gp,B.xp=Q()if (H(171)&1>0)then
l4(B.xp,1)h4(B.gp,1)B=nil
return
end
B.sur=(foes[1].eid==34)c5()for i,t in ipairs(B.ord)do
t.rx,t.ry=t.ch and t.cx-40 or t.cx+180//2,t.cy
end
table.sort(B.ord,A8)B.nx=#B.ord
B.tac=0
a5()J(B3)J(function()B=nil return 1 end)J(j2,{},1)B.bgc=f2(26388)[vst]or 0
B.dsp=vst
d4(5)a1(6727,{})end
function A8(a,b)return (a.ent and a.ent.speed or l3(a.ch)or 0)>
(b.ent and b.ent.speed or l3(b.ch)or 0)end
function A9()B=nil end
function B0()cls(B.bgc)for x=0,184+8,8 do spr(B.dsp,x,0)spr(B.dsp,x,108-8)end
for i,t in ipairs(B.ord)do
local sid,ox=0,0
_=t.ent and g2(t.ent.palm)if t.po==4 then
elseif t.po==0 then
sid=t.sid+(G.clk&16>0 and t.ssz or 0)if t.ch then
if d2(t.chi,16)then sid=379+t.chi end
if d2(t.chi,2)then sid=327+t.chi end
if d2(t.chi,4)then sid=343+t.chi end
end
elseif t.po==3 then
sid,ox=t.sid,t.ch and -2 or 2
A0(14)elseif t.po<=2 then
sid=t.sid+(t.po-1+2)*t.ssz
elseif t.po==5 then
sid=t.sid+(min((G.clk-t.pocs)//10,3))*t.ssz+(t.ch and 64 or 0)elseif t.po==6 then
sid=t.sid+(min((G.clk-t.pocs)//10,3))*t.ssz-(t.ch and 4 or 0)elseif t.po==7 then sid=359+t.chi
end
if t.co&1>0 then
local os,f={0,1,2,1},1+(G.clk//8)%4
ox=ox+os[f]end
spr(sid,ox+t.rx-t.ssz*4,t.ry-t.ssz*4,0,1,0,0,t.ssz,t.ssz)g2()_=B.am and B.t==t and t.ch and G.clk&16>0 and
rectb(t.rx-7,t.ry-7,14,14,14)local o,fr
o=t.ch or t.ent
fr=o.hp/o.maxHp
_=fr<1 and t.po~=4 and
i5(ox+t.rx-3,t.ry-t.ssz*4-2,6,1,fr,fr>.8 and 11 or fr>.2 and 14 or 6,fr>.8 and 5 or fr>.2 and 9 or 1)end
end
function B1(chi)return B and B.t and B.t.chi==chi end
function B2(bad)bad.narr=B.t.ch.name.." "..bad.narr
B.rbad=bad
end
function B3(st,isFg)if s0()then return 1 end
if not isFg then return end
if B.q then
d4(L.lvl.mus)a1(6742,{B.q=="SUR" and 1 or 0})return 1
end
if D6()then
B.q=1
e4()J(D5,{},1)else
if B.t.ch then B4(B.t)else C4(B.t)end
end
end
function a5()D4()if B.tac>0 then
B.tac=B.tac-1
return
end
if B.t then B.t.co=B.t.co&~1 end
B.nx=c4(B.nx,#B.ord)B.t=B.ord[B.nx]B.tac=B.t.ch and 0 or B.t.ent.nat-1
ast(B.tac,"BTA")end
function B4(t)if not d5(t.chi)then
a5()return
end
B.am=1
local as=c9(26218)if B.sur then as[7]="SURRENDER" end
b6(t.ch.name.."?",as,B5,15,2,t,2)end
function B5(m,t)B.am=nil
local f=o0[m]ast(f)f(m,t)end
function n9(m,t)if j1(t,m<6 and 1 or -1)then
a5()return
end
U(c9(
m<1 and 26314 or 26257),15,2,2)return
end
function B6(m,t)local bad=r7(t.ch,2)if not bad then
U(c9(25772))return
end
g3(bad)end
function B7(m,t)local bad=r7(t.ch,1)if not bad then
U(c9(25840))return
end
g3(bad)end
function B8(m,t)m9(t.chi,B9)end
function B9(r)if not r then return end
if B.rbad then
local b
b,B.rbad=B.rbad,nil
g3(b)else
a5()end
end
function C0(m,t)l5(nil,nil,1,c9(26410),nil,C1)end
function C1(it)if it then
ast(B.t.chi)p2(it,B.t.chi,C2)end
end
function C2(r)if r<1 then return end
if B.rbad then
local b
b,B.rbad=B.rbad,nil
g3(b)else
a5()end
end
function C3()if B.sur then
B.q="SUR"
else
a5()end
end
o0={
B7,B6,B8,C0,n9,n9,C3,}function C4(t)local i,j,att
if t.ent.bad.ak==1 then
C5(t)else
C6(t)end
end
function C5(t)if j1(t,-1)then
a5()return
end
i,j=e2(t)if i<2 or #B.c[i]<1 then
a5()return
end
i=i-1
j=random(1,#B.c[i])local v=B.c[i][j]if v.chi and (B.ltchi and v.chi==B.ltchi or d2(v.chi,16))then
j=random(1,#B.c[i])v=B.c[i][j]end
if v.ent then
a5()return
end
B.ltchi=v.chi
j4(v,t.ent.bad)end
function C6(t)local i,j,off,cht
j=random(1,#B.ord)for i=1,#B.ord do
j=c4(j,#B.ord)cht=cht or (B.ord[j].ch and B.ord[j])end
if not cht then return end
j4(cht,t.ent.bad)end
function e2(t)ast(t)for i,c in ipairs(B.c)do
local j=b1(B.c[i],t)if j then return i,j end
end
error("!TOK")end
function j1(t,dir)local i,j,tci
i,j=e2(t)
tci=i+dir
if tci<1 or tci>6 then return nil end
if #B.c[tci]>3 then return nil end
if #B.c[tci]>0 and not D0(B.c[tci][1],t)then
return nil end
remove(B.c[i],j)insert(B.c[tci],t)c5()J(j2,{},1)return 1
end
function j2(st)local r,d=1,1
d=((H(171)&2>0)and btn(7))and 5 or 1
for i,t in ipairs(B.ord)do
t.rx,t.ry=j0(t.rx,t.ry,t.cx,t.cy,d)r=r and d9(t.rx,t.ry,t.cx,t.cy)<1
end
return r
end
function C7(chi)local ch,tok=PA[chi]if ch.hp<1 then return end
tok={chi=chi,ch=ch,sid=252+chi*16,ssz=1,po=0,pocs=G.clk,co=0}insert(B.c[(ch.class==4 or ch.class==3)and
2 or 3],tok)insert(B.ord,tok)return tok
end
function C8(e,ci)ast(e and ci)local tok={ent=e,sid=e.sid,ssz=e.fl&2 >0 and 2 or 1,po=0,pocs=G.clk,co=0}insert(B.c[ci],tok)insert(B.ord,tok)end
function C9(t)ast(t~=B.t,"BREM")local i,j=e2(t)e0(B.c[i],t)e0(B.ord,t)B.nx=b1(B.ord,B.t)end
function c5()for i,c in ipairs(B.c)do
local h=0
for j,tk in ipairs(c)do
h=h+(j>1 and 12 or 0)tk.cx=(i-1)*30+30//2
tk.cy,h=h,h+tk.ssz*8
end
for j,tk in ipairs(c)do
tk.cy=tk.cy+(110-h)//2
end
end
end
function D0(t1,t2)return (t1.ch and t2.ch)or (t1.ent and t2.ent)end
function g3(bad)local ri,rf=1,1
if bad.ak==2 or bad.ak==3 then ri,rf=1,9 end
local i,j=e2(B.t)if not D1(i+ri,i+rf,bad)then
U(c9(26337))return
end
end
function D1(ci,cf,bad)local cs
ci,cf=a8(ci,1,6),a8(cf,1,6)for i=ci,cf do if j3(i)then cs=i break end end
if not cs then return nil end
J(D2,{ci=ci,cf=cf,cs=cs,i=1,bad=bad},1)return 1
end
function D2(st)local c=B.c[st.cs]local t=c[st.i]if st.bad.ak==3 then
_=G.clk%16>4 and rectb(t.cx-30//2,5,30,110-10,14)else
_=G.clk%16>4 and spr(248,t.rx-t.ssz*4-9,t.ry-4,0)end
st.i=F(0)and g0(st.i,#c)or st.i
st.i=F(1)and c4(st.i,#c)or st.i
if F(2)and st.cs>st.ci then
for i=st.cs-1,st.ci,-1 do if j3(i)then st.cs=i break end end
elseif F(3)and st.cs<st.cf then
for i=st.cs+1,st.cf do if j3(i)then st.cs=i break end end
end
st.i=min(st.i,#B.c[st.cs])if F(5)then return 1 end
if F(4)then
j4(t,st.bad)return 1
end
end
function j3(ci)if ci<1 or ci>#B.c then return nil end
for i,t in ipairs(B.c[ci])do
if t.ent then return 1 end
end
return nil
end
function j4(t,bad)local hit,d,def,dm,i,mad
bad=a4(bad)if bad.ak==3 then
i,_=e2(t)J(D8,{at=B.t,tc=i,bad=bad},1)return
end
def=t.ent and t.ent.def or r6(t.ch)ast(bad and def,"BDAd")a1(B.t.ch and 6771 or 6860,{
B.t.chi or B.t.ent.eid,t.chi or t.ent.eid,bad.att,def,bad.fl,bad.ak})bad.att,def,bad.fl=Q()mad=max(3,bad.att-def)d=z7(mad)if t.ch then a1(6935,{t.chi,d})d=Q()end
d=t.ch and ceil((f7(t.ch.maxHp//2,1,0,0.4,t.ch.hp))*d)or d
hit=bad.fl&32>0 or (bad.fl&16<1 and (
d>0 and
(d>1 or random(1,5)>2)and
(B.t.co&1<1 or random(1,5)<2)))if hit and bad.fl&1>0 and t.ent then
hit=hit and random(0,100)>t.ent.mr
end
J(
bad.ak==1 and D3 or D7,{at=B.t,tt=t,dmg=hit and d,bad=bad},1)end
function D3(st)local a,t,ax,ay,d,sgn,bad
bad=st.bad
d=((H(171)&2>0)and btn(7))and 5 or 2
st.p=st.p or 0
a,t=st.at,st.tt
sgn=a.cx>t.cx and 1 or -1
ax,ay=t.cx+sgn*4*(t.ssz+1),t.cy
if st.p==0 then
a.rx,a.ry=j0(a.rx,a.ry,ax,ay,d)if d9(a.rx,a.ry,ax,ay)<1 then
st.p=1
b7(a,bad.fl&1>0 and 6 or 1)end
elseif st.p==1 then
st.p1c=(st.p1c or 0)+(((H(171)&2>0)and btn(7))and 5 or 1)_=st.p1c>20 and st.p1c<40 and j8(t,st.dmg,st.p1c//4,bad)if st.p1c==20 then
a3(st.dmg and 7 or 8)j6(t,st.dmg,bad)_=bad.fl&1<1 and b7(a,2)b7(t,st.dmg and 3 or t.po)end
st.p=st.p1c>50 and 2 or 1
_=bad.narr and a2(bad.narr,92,96)else
j5(a,t)a.rx,a.ry=j0(a.rx,a.ry,a.cx,a.cy,d)if d9(a.rx,a.ry,a.cx,a.cy)<1 then
a5()return 1
end
end
end
function j5(a,t)b7(a,0)b7(t,o1(t)>0 and 0 or 4)end
function j6(t,dmg,bad)if not dmg then a3(8)return end
a3(7)local o=t.ent or t.ch
o.hp=max(0,o.hp-dmg)if bad.fl&4>0 then
if not t.ch or d5(t.chi)then
t.co=t.co|1
end
end
if bad.fl&64>0 then
j1(t,t.ent and 1 or -1)end
if t.chi then
j7(bad.fl,128,t.chi,1)j7(bad.fl,256,t.chi,2)j7(bad.fl,512,t.chi,4)end
end
function j7(v,m,chi,cm)_=v&m>0 and l7(chi,cm)end
function D4()local d=nil
for i=#B.ord,1,-1 do
local t=B.ord[i]if o1(t)<1 then
a3(10)C9(t)d=1
end
end
if d then
c5()J(j2,{},1)end
end
function D5(st)B.q=1
a2("Victory!",184//2,18,15)_=st.clk==1 and a3(14)if st.clk==100 then l4(B.xp)h4(B.gp)end
return st.clk>110
end
function D6()for i,t in ipairs(B.ord)do
if t.ent then return nil end
end
return 1
end
function o1(t)return t.ch and t.ch.hp or t.ent.hp
end
function D7(st)local a,t,sgn,x0,y0,x1,y1,px,py,bad,d
bad=st.bad
st.pfc=((H(171)&2>0)and btn(7))and 5 or (st.pfc or 20)st.p=st.p or 0
st.pc=(st.pc or 0)+1
a,t=st.at,st.tt
sgn=a.cx>t.cx and 1 or -1
x0,y0=a.cx-sgn*a.ssz,a.cy-3
x1,y1=t.cx,t.cy
if st.p==0 then
b7(a,bad.fl&2>0 and 7 or
bad.fl&1>0 and 6 or 5)if st.pc>25 then st.p,st.pc=1,0 end
elseif st.p==1 then
px,py=f7(0,x0,st.pfc,x1,st.pc),f7(0,y0,st.pfc,y1,st.pc)spr(bad.psp,px,py,0)if st.pc>st.pfc then st.p,st.pc=2,0 end
_=bad.fl&2>0 and b7(a,0)else
if st.pc==1 then
a3(st.dmg and 7 or 8)_=st.dmg and b7(t,3)j6(t,st.dmg,bad)end
_=st.pc<20 and j8(t,st.dmg,st.pc//4,bad)if st.pc>30 then
j5(a,t)a5()return 1
end
end
_=st.p>0 and bad.narr and a2(bad.narr,92,100)end
function D8(st)local a,t,ts,bad,i,def,d
bad=st.bad
st.p=st.p or 0
st.pc=(st.pc or 0)+1
a=st.at
if st.p==0 then
b7(a,bad.fl&1>0 and 6 or 5)if st.pc>35 then st.p,st.pc=1,0 end
elseif st.p==1 then
for i=8,110-16,8 do spr(bad.psp,(st.tc-1)*30+st.pc//2,i,0)end
if st.pc>45 then st.p,st.pc=2,0 end
else
ts=B.c[st.tc]if st.pc==1 then
a3(7)for i=1,#ts do
def=ts[i].ent and ts[i].ent.def or r6(ts[i].ch)assert(def,"BAAd")d=max(1,bad.att-def)b7(ts[i],3)j6(ts[i],d,bad)_=st.pc<20 and j8(ts[i],d,st.pc//4,bad)end
end
if st.pc>30 then
for i=1,#ts do j5(a,ts[i])end
a5()return 1
end
end
_=st.p>0 and bad.narr and a2(bad.narr,92,100)end
function b7(t,po)if t.po~=po then t.po,t.pocs=po,G.clk end end
function j8(t,dmg,oy,bad)if dmg and dmg<1 then return end
a2(
dmg and ("-"..dmg)or (bad.fl&1>0 and "NO EFFECT" or "MISS"),t.cx-(t.ch and -8 or 8),t.cy-t.ssz*4-oy,dmg and 6 or 7)end
function j9(chi)ast(chi>0 and chi<5,"!CEFc"..chi)return w4(244+chi-1)end
function d2(chi,mask)return 0<(j9(chi)&mask)end
function D9(chi,mask)ast(chi>0 and chi<5,"!CEFs"..chi)return h9(244+chi-1,j9(chi)|mask)end
function E0(chi,mask)ast(chi>0 and chi<5,"!CEFc"..chi)return h9(244+chi-1,j9(chi)&~mask)end
function E1(c)if not c then return end
if c==1 and B then B.q=1 end
if c==2 then for k,v in pairs(ZD)do r9(k)end end
if c==3 then E4()end
if c==4 then E5()end
end
function E2()J(E3,{c=""},1)end
function E3(st)cls(3)prn("Patch:",8,40)prn(st.c,8,50,14,true,1,true)for k=27,36 do
if keyp(k)and #st.c<64 then st.c=st.c..schr(k-27+48)end
end
for k=1,7 do
if keyp(k)and #st.c<64 then st.c=st.c..schr(k+64)end
end
if F(2)and #st.c>0 then st.c=ssub(st.c,1,#st.c-1)end
if F(4)and #st.c>0 and #st.c%2==0 then
local p=53388
for i=1,#st.c,2 do
XP[p]=tonumber(ssub(st.c,i,i+1),16)p=p+1
end
XP[p]=125
a1(53388)return 1
end
return F(5)end
function E4()end
function E5()for _,it in ipairs(P.inv)do
it.st=it.st&~1
end
for _,ch in ipairs(PA)do
for k,v in pairs(ch.eq)do
if v then v.st=v.st&~1 end
end
end
end
function E6()E8()end
function E7(eid)local entd=ENTD[eid]ast(entd,"Bad EID " .. eid)return a4(entd)end
function E8()b4(51725)ENTD={}local c=0
while 1 do
local sig,d=C(),{}if sig==0 then break end
ast(sig==151,"Bad ent db")local eid=C()ast(not ENTD[eid],"DupE"..eid)d.eid=eid
d.name=c8()d.fl=C()d.sid=a6()d.palm=d.fl&1>0 and Q2(15)d.speed=C()d.maxHp=a6()d.hp=d.maxHp
d.def=0
d.xp=a6()d.gp=a6()d.mr=C()d.nat=C()d.bad=E9()ENTD[eid]=d
c=c+1
end
trace("ENT load "..c)end
function E9()local r={}r.ak=C()r.att=C()r.psp=C()r.narr=c8()r.fl=a6()return r.ak>0 and r or nil
end
G={
m=1,csn=0,xin=0,booted=nil,clk=0,bsr={},tbank=0,sbank=0,bsw=nil,b={},bp={},}function o2(lvlN,epnum)trace("ch lvl "..lvlN.." e"..epnum)p7(lvlN,epnum)end
function k0()A5()A9()L9()r5()q7()w3()end
function F0()a9(6)end
function F1(csn)e4()G.csn=csn a9(8)end
function F2(endn)G.endn=endn
a9(7)end
function F3()G.pok=pmem(254)==123
pmem(254,123)end
function TIC()
G.bsw=nil
G.frst=time()if G.xin==0 then
n1()G.xin=1
return
elseif G.xin==1 then
w2()G.xin=2
return
elseif G.xin==2 then
b8(4,0)G.xin=3
L8()H7()H6()E6()O5()I6()return
end
G.clk=G.clk+1
F4()G.booted=G.booted or F3()or 1
_=G.m==3 or G.m==4 or cls(3)GMTIC[G.m]()if (H(171)&4>0)then
local t=ceil(time()-G.frst)t=t<10 and "0"..t or t
prn(t,4,4,15)end
end
function F4()for b=0,7 do
G.bp[b]=btn(b)and not G.b[b]G.b[b]=btn(b)end
end
function b8(mask,bank)ast(not G.bsw,"BSW")G.bsw=1
sync(mask,bank)if mask&1>0 then G.tbank=bank end
if mask&2>0 then G.sbank=bank end
if mask&4>0 then G.mbank=bank end
end
function F5()cls(0)if G.clk==1 then
b8(1|2|4|32,4)d4(1)return
end
local off=G.clk&16>0 and 60 or 0
map(off,0,30,17,0,0)_=G.clk>40 and map(30+off,0,30,min(17,(G.clk-40)//4),0,0)e3()if G.clk<140 and not btn(7)then return end
map(0,17,30,17,0,0,0)local new=pmem(0)<1
_=G.clk&32>0 and
a2(i0(new and 25154 or 25172),120,110,14)if F(4)then
if new then a9(9)else e4()o3()end
end
if btn(1)and btn(2)and btn(6)and btnp(7)then
a9(5)end
prn(i0(27285),5,128,7,true,1,true)_=G.pok and prn("Save OK",0,0,1,true,1,true)end
function F6()map(30,0,30,17,0,0)d1(c9(26935),5,max(136-G.clk//2,5))if G.clk>260 or btn(7)then
_=G.clk&32>0 and
a2(i0(25154),120,110,14)_=btnp(4)and a9(2)end
end
function F7()assert(G.mbank==4,"INSmb")_=G.clk==1 and e4()cls(1)map(30,17,30,17,0,0,0)if G.clk>100 and G.clk&32>0 then
a2(i0(25202),120,120,15)end
_=(G.clk>100 or btn(7))and F(4)and o3()e3()end
function F8()cls(1)d1(c9(25226),20,20)G.etcks=(G.etcks or 0)+b2(F(4))if G.etcks>15 then
G.etcks=0
a3(9)pmem(0,0)a9(1)end
if F(5)then a9(1)end
end
function F9()Q1()_=F(5)and a9(3)e3()end
function o3()b8(1|2|4|32,0)k0()if pmem(0)<1 then
local sl={DBG_GAME_START_LOC}o2(sl[1]or 1,sl[2]or 1)a9(3)else
O4()a9(3)end
end
function G0()o4()g4()_=G.clk>10 and not B and H8()if s0()then
e4()a9(4)end
if btnp(6)and btn(3)and btn(0)then E2()end
e3()end
function G1()o4()clip()n6(0+1,0+1,184-2,108-2,2)a2(i0(25289),0+184//2,0+108//2,15)if G.clk>200 or (G.clk>30 and F(4))then
k0()a9(1)end
end
function G2()local off=G.clk&16>0 and 30 or 0
if G.clk==1 then
b8(63,4)d4(1)end
map((G.endn==1 and 120 or 60)+off,G.endn==1 and 0 or 17,30,17)local x,y,a=20,max(136-G.clk//2,5),G.endn==1 and c9(27343)or c9(27704)d1(a,x+1,y+1,0)d1(a,x,y)e3()end
function G3()cls(0)if G.clk<2 then
b8(2|1|4,5)return
end
local n=G.csn-1
map((n%8)*30,17*(n//8))a2(c9(26726)[G.csn],120,max(110,136-G.clk),15)if G.clk>200 then
b8(2|1|4,0)a9(3)end
end
function a9(m)G.clk,G.m=0,m
clip()end
function g4()if A6()then return end
p9()J6()end
function o4()if G.clk&1>0 then
b8(2|1,B and 6 or L.lvl.texb)clip(0,0,0,0)elseif G.clk>2 then
clip()cls(0)clip(0-1,0-1,184+1,108+2)if B then
B0()elseif G.tbank==L.lvl.texb then
M1(L.lvl.skyc)M0()end
clip()b8(2|1|4,0)c3(0,0,184,108)J8()H9(188,0,5,5)c3(188,0,40,40)prn(L.lvl.lvlt,188,45,7,true,1,true)if R.li==1 and not B then
a2(i0(25298),0+184//2,15,3)end
a1(7161,{b2(B)})_=#PA==2 and prn("Keys\nZ: action\nX: close/cancel",100,115,3,true,1,true)end
end
function e3()local s=G.clk/30
if s>=1 then return end
rect(0,0,240,68-s*68,0)rect(0,68+s*68,240,68,0)rect(0,0,120-s*120,136,0)rect(120+s*120,0,120,136,0)end
GMTIC={
[1]=F5,[2]=F7,[3]=G0,[4]=G1,[5]=F8,[6]=F9,[7]=G2,[8]=G3,[9]=F6
}local I=nil
function o5()k1=c9(25308)I=a4(k2)g5()J(G4,{})end
k2={
grid={},cr=1,cc=11,quitf=nil,}function G4(st,isFg)if not I then return 1 end
d0(8,8,224,120,0,i0(25336))if isFg and (F(5)or I.quitf)then
I=nil
return 1
end
H3(isFg)if not isFg then return end
if F(0)then g7(0,-1)end
if F(1)then g7(0,1)end
if F(2)then g7(-1,0)end
if F(3)then g7(1,0)end
if F(4)then
G5()return nil
end
return nil
end
function g5()I.grid={}for chi=1,#PA do
local col=1
local ch=PA[chi]for k,it in pairs(ch.eq)do
if it then
I.grid[col..","..chi]={chi=chi,it=it}col=col+1
end
end
end
local col,row=11,1
for i,it in ipairs(P.inv)do
I.grid[col..","..row]={idx=i,it=it}if col==20 then
col,row=11,row+1
else
col,row=col+1,row
end
end
end
function G5()local m={sel=1}m.acts=H5()if not m.acts then return end
m.names={}for _,ac in ipairs(m.acts)do
local n
if ac>=1 and ac<=4 then
n="Equip ("..PA[ac-1+1].name..")"
else
n=k1[ac]end
insert(m.names,n)end
J(G6,m,1)end
function G6(m,isFg)m.sel=i8("Item",m.names,120,40,15,4,m.sel)if not isFg then return nil end
if F(4)then
local cell=k3()local ac=m.acts[m.sel]local fn=o9[ac]ast(fn,"No ac hnd " .. m.sel)local itd=V(cell.it.itid)return fn(ac,cell)end
if F(5)then
return 1
end
return nil
end
function k3()return I.grid[I.cc..","..I.cr]end
function g6(ac,cell)local chi=ac-1+1
local ch=PA[chi]local itd=V(cell.it.itid)if (1<<ch.class)&itd.ac<1 then
U(ch.name..i0(25345),nil,nil,2)return nil
end
if ch.eq[itd.k]then
U({ch.name..i0(25361),"current "..k4[itd.k].."."},nil,nil,2)return nil
end
if (ch.eq[5]and itd.f&2>0)or
(itd.k==5 and G7(ch))then
U(c9(25379),nil,nil,2)return nil
end
ch.eq[itd.k]=cell.it
ast(cell.idx)remove(P.inv,cell.idx)g5()o6(cell.it)a3(4)return 1
end
function G7(ch)local w=ch.eq[3]return w and (V(w.itid).f&2>0)end
function o6(it)for r=1,8 do
for c=1,20 do
local cell=I.grid[c..","..r]if cell and cell.it==it then
I.cc,I.cr=c,r
return
end
end
end
end
function G8(ac,cell)if #P.inv>=80 then
U(i0(25427),nil,nil,2)return nil
end
local itd=V(cell.it.itid)if k5(cell.it)then
o7()return nil
end
PA[I.cr].eq[itd.k]=nil
insert(P.inv,cell.it)g5()o6(cell.it)a3(4)return 1
end
function o7()U(i0(25456),nil,nil,2)end
function G9(ac,cell)J(function()p2(cell.it)return 1 end)I.quitf=1
return 1
end
function H0(ac,cell)U(p6(cell.it))return 1
end
function H1(ac,cell)local itd=V(cell.it.itid)if not itd then return 1 end
if cell.chi and k5(cell.it)then
o7()return nil
end
if itd.k==15 or (itd.v or 0)<1 then
U(i0(25485),nil,nil,2)return 1
end
b6(i0(25524).." "..itd.n.."?",c9(25538),H2)return 1
end
function H2(ans)if ans~=2 then return end
local cell=k3()if not cell then return end
local itd=V(cell.it.itid)local chi,idx=cell.chi,cell.idx
ast(chi or idx)if chi then
PA[chi].eq[itd.k]=nil
else
e6(cell.it)end
g5()a3(4)end
function g7(dc,dr)I.cc=a8(I.cc+dc,1,20)I.cr=a8(I.cr+dr,1,8)if I.grid[I.cc..","..I.cr]or (I.cc>=11 and
I.cc<=20)then
return
end
if dc>0 then
I.cc=11
elseif dc<0 and I.cr<=#PA then
o8(-1,0)elseif dc>0 and I.cr>#PA then
I.cc=11
elseif dr~=0 and I.cr<=#PA then
o8(-1,0)else
I.cc,I.cr=I.cc-dc,I.cr-dr
end
end
function o8(dc,dr)while not I.grid[I.cc..","..I.cr]do
local nc=a8(I.cc+dc,1,20)local nr=a8(I.cr+dr,1,8)if nc==I.cc and nr==I.cr then break end
I.cc,I.cr=nc,nr
end
end
function H3(isFg)for r=1,8 do
for c=1,20 do
local cell=I.grid[c..","..r]H4(cell,24+10*(c-1),26+10*(r-1),I.cc==c and I.cr==r,isFg)end
end
local icx=24-12
local icy=26-1
for i=1,#PA do
spr(PA[i].face,icx,icy+(i-1)*10)end
prn("Equipped",icx,icy-10,3)prn("Inventory",24+10*(11-1),icy-10,3)prn("Gold",icx,26+50,3)prn(P.gold .. " gp",icx,26+60,15)end
function H4(cell,x,y,sel,isFg)local ic,n
if cell then
ic,n=d3(cell.it)spr(ic,x,y)end
if sel and (not isFg or G.clk&16>0)then
rectb(x-2,y-2,11,11,14)end
_=n and sel and a2(n,8+224//2,120,14)end
function H5()local ac={}local cell=k3()if not cell then return nil end
local itd=V(cell.it.itid)if cell.chi then
insert(ac,5)else
if itd.k<=9 then
for i=1,#PA do
insert(ac,1+i-1)end
else
insert(ac,6)end
end
insert(ac,7)insert(ac,8)return ac
end
o9={
[1]=g6,[2]=g6,[3]=g6,[4]=g6,[5]=G8,[6]=G9,[7]=H0,[8]=H1,}p0={1,2,3,4,5,6}p1={
[97]=6
}ITDB={}function H6()k4=c9(25563)b4(1983)while 1 do
local h=C()if h==0 then break end
ast(h==123,"ITDB err "..h)local itid=C()ast(not ITDB[itid],"Dup ITID "..itid)local r,aod={}r.n=c8()r.sp=itid+256
r.k=C()r.f=C()r.v=c2(C()*(r.f&8>0 and 100 or 1))r.ac=C()r.def=r.att
if r.k==3 or r.k==4 then
r.att,r.def=C(),0
else
r.att,r.def=0,C()end
r.desc=Q4()ITDB[itid]=r
end
end
function V(itid)ast(itid,"null ITID")local itdef=ITDB[itid]ast(itdef,"bad ITID "..itid)return itdef
end
function p2(it,chi,cb)local idx=b1(P.inv,it)ast(idx,"ITnf")a1(7074,{it.itid,idx,chi},function(r)_=cb and cb(r)end)end
function p3(it)if it then
a0({it.itid,it.st})else
a0({0,0})end
end
function p4(f)f=f or b0
local it={}it.itid=f()it.st=f()return it.itid>0 and it or nil
end
function k5(it)ast(it.st,"IT_IC/st")return it.st&2>0
end
function k6(it)ast(it.st,"IT_GE/st")return (k5(it)and -1 or 1)*(
(it.st&28)>>2)end
function p5(itid)return b1(p0,V(itid).k)end
function d3(it)local itd,ic,s,en,br=V(it.itid)ic,s=itd.sp,itd.n
br=" [?]"
if it.st&1>0 then
if k7[itd.k]then
ic=k7[itd.k]s=c9(25563)[itd.k]..br
s=supper(ssub(s,1,1))..ssub(s,2)else
s=s..br
end
else
en=itd.k~=10 and k6(it)or 0
s=s..(en>0 and " +"..en or en<0 and " "..en or "")s=s..(it.st&2>0 and " CURSED" or "")end
return ic,s
end
function g8(itid,st)st=st or 0
local itd=V(itid)if itd.f&4>0 then st=st|2 end
return {itid=itid,st=st}end
function p6(it)ast(it and it.itid)local itd,_,n,d=V(it.itid),d3(it)d=itd.desc
if not d or #d<1 then d=c9(26917)end
if it.st&1>0 then return c9(26634)end
return n3({n,""},d)end
k7={
[12]=491,[11]=492,[8]=493,[10]=494,[9]=495,}L=nil
L_INIT={
lvlN=nil,lvl=nil,lvlt=nil,tiles={},rtiles={},}local LVLS=nil
TD=nil
OTD=nil
function H7()b4(8231)LVLS={}while 1 do
local sig=C()local l={}if sig==0 then break end
ast(sig==250,"!LVLs"..#LVLS)ast(C()==1+#LVLS,"!LVLn"..#LVLS)l.lvlt=c8()l.mc0=C()l.mr0=C()l.mcols=C()l.mrows=C()l.flags=C()l.skyc=l.flags&(1|16)>0 and 0 or 8
l.mus=C()l.texb=l.flags&8>0 and 2 or 1
l.kfL=C()l.bsn=C()l.iproc=a6()l.uproc=a6()l.wproc=a6()l.bproc=a6()l.epts={}local entptc=C()for i=1,entptc do
local ept={}local rx=C()local rz=C()ept.x,ept.z=I5(l,rx,rz)ept.dir=C()insert(l.epts,ept)end
l.tsub={}local subc=C()for i=1,subc do
local f=C()l.tsub[f]=C()end
insert(LVLS,l)end
trace("LVL init "..#LVLS)b4(38220)TD,OTD={},{}local tc,ss=0,0
while 1 do
local sig=C()local d={}if sig==255 then break end
ast(sig==249 or 248,"!Tsig"..sig..'@'..tc)local id=C()d.flags=a6()d.ftid=c2(C())d.ctid=c2(C())d.wtid=c2(C())d.stid=c2(C())d.kstid=c2(C())ss=C()d.ssize=ss>0 and ss*.01 or 1
d.wons=d.flags&2048>0 and .5 or 0
d.woew=d.flags&4096>0 and .5 or 0
ast(not d.stid or d.ssize>0,"!ZS"..id)ast(not TD[id]and not OTD[id],"!DupTI"..id)if sig==249 then
TD[id]=d
else
OTD[id]=d
end
tc=tc+1
end
trace("TILE init "..tc)end
function p7(lvlN,epnum)if L then I4()end
epnum=epnum or 1
local lvl=LVLS[lvlN]ast(lvl)L=a4(L_INIT)L.lvlN=lvlN
L.lvl=lvl
d4(lvl.mus)local nextKfi=0
for lr=0,lvl.mrows-1 do
for lc=0,lvl.mcols-1 do
local ti=q6(lc,lr)if ti.td and ti.td.flags&32>0 then
ast((nextKfi//8)<lvl.kfL,"KF over cap")ti.kfi=nextKfi
nextKfi=nextKfi+1
end
end
end
if epnum>=0 then
local ep=lvl.epts[epnum]ast(ep,"No lvl entry "..epnum.." @L"..lvlN)J7(ep.x,ep.z,ep.dir)end
p9()a1(7147)t9(lvl.flags&1>0 and 1 or 3)end
function H8()_=I9()or d4(L.lvl.mus,1)end
function b3(lc,lr)return L.tiles[lr*1000+lc]end
function g9(mc,mr)return mc-L.lvl.mc0,L.lvl.mr0-mr+L.lvl.mrows-1
end
function p8(lc,lr,lvl)lvl=lvl or L.lvl
return lc+lvl.mc0,lvl.mr0+lvl.mrows-1-lr
end
function H9(x0,y0,cols,rows)if G.tbank>0 then return end
y0=y0+(rows-1)*8
local c0,r0=P.x-cols//2,P.z-rows//2
for c=c0,c0+cols-1 do
for r=r0,r0+rows-1 do
local t,x,y=b3(c,r),x0+(c-c0)*8,y0-(r-r0)*8
spr(t and t.msp or 0,x,y)spr(t and t.mspo or 0,x,y,0)if c==P.x and r==P.z then spr(247,x,y,0,1,0,P.dir)end
end
end
end
function p9()I3()end
function q0(tc,tr)local ti=b3(tc,tr)if not ti then return end
local kf=ti.kfi and h2(L.lvlN,ti.kfi)kf=b2(kf)a1(6980,{ti.mc+256*ti.mr,tc+tr*256,ti.sid,kf})end
function q1(c,r)local ti=b3(c,r)local f=ti and ti.td.flags or 16
local k=ti.kfi and h2(L.lvlN,ti.kfi)if not k and f&64>0 then f=f|16 end
return f&16>0
end
function I0(c,r)local ti=b3(c,r)ast(ti.kfi,"Can't kill "..c..","..r)J2(L.lvlN,ti.kfi)q5(ti)end
function q2(c,r,sid)ast(TD[sid]or OTD[sid],"!CSID.."..sid)q5(q6(c,r,sid))end
function I1(c,r)local ti=b3(c,r)if not ti or not ti.kfi then return nil end
return h2(L.lvlN,ti.kfi)end
function I2(lc,lr,tile)L.tiles[lr*1000+lc]=tile
end
function q3(ti)if ti.rends or not q4(ti.x,ti.z)then return end
insert(L.rtiles,ti)local x,z,td,ons,oew=ti.x,ti.z,ti.td
local ons,oew=td.wons,td.woew
ti.rends={}if td then
_=td.ftid and insert(ti.rends,M2(x,z,h0(td.ftid)))_=td.ctid and insert(ti.rends,M3(x,z,h0(td.ctid)))if td.wtid then
local wtid=h0(td.wtid)local wf=td.flags&512>0 and 2 or 0
_=td.flags&1>0 and
insert(ti.rends,h7(x,z+1-ons,1,wtid,wf))_=td.flags&2>0 and
insert(ti.rends,h7(x+1-oew,z+1,2,wtid,wf))_=td.flags&4>0 and
insert(ti.rends,h7(x+1,z+ons,3,wtid,wf))_=td.flags&8>0 and
insert(ti.rends,h7(x+oew,z,0,wtid,wf))end
local stid=td.stid
if ti.kfi and h2(L.lvlN,ti.kfi)then
stid=td.kstid or stid
end
if stid then
stid=h0(stid)local sz,r=td.ssize,b2(td.flags&128>0)insert(ti.rends,M4(
x+0.5+r*random(-9,9)*.01,sz/2,z+0.5+r*random(-9,9)*.01,sz,stid))end
end
end
function k8(ti)if not ti.rends then return end
for i=1,#ti.rends do M5(ti.rends[i])end
ti.rends=nil
end
function I3()for i=#L.rtiles,1,-1 do
local ti=L.rtiles[i]if not q4(ti.x,ti.z)then
k8(ti)z6(L.rtiles,i)end
end
for x=P.x-5,P.x+5 do
for z=P.z-5,P.z+5 do
local ti=b3(x,z)_=ti and q3(ti)end
end
end
function q4(x,z)return max(abs(x-P.x),abs(z-P.z))<=5
end
function I4()for _,ti in ipairs(L.rtiles)do
k8(ti)end
L.rtiles={}end
function I5(l,mc,mr)return mc-l.mc0,l.mr0+l.mrows-1-mr
end
function h0(tid)return L.lvl.tsub[tid]or tid end
function q5(ti)k8(ti)q3(ti)end
function q6(lc,lr,nsid)local lvl=L.lvl
local mc,mr=p8(lc,lr,lvl)local ti=b3(lc,lr)or {}ti.x,ti.z,ti.mc,ti.mr,ti.sid=lc,lr,mc,mr,nsid or mget(mc,mr)local otd=OTD[ti.sid]if otd then
local bsid=mget(mc+1,mr)if not TD[bsid]or not TD[bsid].ftid then bsid=mget(mc-1,mr)end
if not TD[bsid]or not TD[bsid].ftid then bsid=mget(mc,mr-1)end