Repository navigation
Expand file tree
/
Copy pathbrikPlayer.lua
More file actions
2060 lines (1871 loc) · 66.7 KB
/
Copy pathbrikPlayer.lua
File metadata and controls
2060 lines (1871 loc) · 66.7 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
local require=simpRequire('ZTC.')
local gc=love.graphics
local gc_push,gc_pop=gc.push,gc.pop
local gc_translate,gc_scale,gc_rotate=gc.translate,gc.scale,gc.rotate
local gc_setColor=gc.setColor
local gc_draw=gc.draw
local max,min,rnd=math.max,math.min,math.random
local floor,ceil=math.floor,math.ceil
local ins,rem=table.insert,table.remove
local clamp,expApproach=MATH.clamp,MATH.expApproach
---@class Techmino.Player.Brik: Techmino.Player
---@field stat Techmino.PlayerStatTable.Brik
---@field lastMovement Techmino.BrikMovement
---@field dropHistory Techmino.BrikDropHis[]
---@field clearHistory Techmino.BrikClearHis[]
local BP=setmetatable({},{__index=require'basePlayer',__metatable=true})
--------------------------------------------------------------
-- Function tables
---@type Map<fun(P:Techmino.Player.Brik): any>
BP.scriptCmd={
clearHold=function(P) P:clearHold() end,
clearNext=function(P) P:clearNext() end,
pushNext=function(P,arg) P:pushNext(arg) end,
setField=function(P,arg) P:setField(arg) end,
switchAction=function(P,arg) P:switchAction(arg) end,
}
--------------------------------------------------------------
-- Actions
BP._actions={}
for k,v in next,GAME.mechLib.brik.actions do BP._actions[k]=BP:_getActionObj(v) end
--------------------------------------------------------------
-- Utils
---@return Mat<boolean>?
function BP:getBoolHandMatrix()
if not self.hand then return end
local m=self.hand.matrix
local w=#m[1]
local shape={}
for y=1,#m do
shape[y]={}
for x=1,w do
shape[y][x]=m[y][x] and true
end
end
return shape
end
--------------------------------------------------------------
-- Effects
function BP:createMoveEffect(x1,y1,x2,y2)
local p=self.particles.rectShade
local dx,dy=self:getSmoothPos()
for x=x1,x2,x2>x1 and 1 or -1 do for y=y1,y2,y2>y1 and 1 or -1 do
p:setPosition((x-.5)*40+dx,-(y-.5)*40+dy)
p:emit(1)
end end
end
function BP:createRotateEffect(dir,ifInit)
local brikData=brikRotSys[self.settings.rotSys][self.hand.shape]
local state=brikData[self.hand.direction]
local centerPos=state and state.center or type(brikData.center)=='function' and brikData.center(self)
local cx,cy
if centerPos then
cx,cy=self.handX+centerPos[1]-.5,self.handY+centerPos[2]-.5
else
cx,cy=self.handX+#self.hand.matrix[1]/2-.5,self.handY+#self.hand.matrix/2-.5
end
local p=self.particles.line
p:setParticleLifetime(.26,.42)
if ifInit then
p:setSpeed(260,620)
else
p:setSpeed(80,120)
end
p:setTangentialAcceleration(dir=='L' and -2600 or 0,dir=='R' and 2600 or 0)
p:setPosition((cx-.5)*40,-(cy-.5)*40+self.fieldDived)
p:emit(12)
end
function BP:createRotateCornerEffect(cx,cy)
local p=self.particles.cornerCheck
for x=-1,1,2 do
for y=-1,1,2 do
p:setPosition(
(cx+x-.5)*40,
-(cy+y-.5)*40
)
p:emit(1)
end
end
end
function BP:createHandErrorEffect(r,g,b,a)
local CB,bx,by=self.hand.matrix,self.handX,self.handY
local dx,dy=self:getSmoothPos()
local p=self.particles.rectTilt
p:setColors(r,g,b,a or 1,r,g,b,0)
for y=1,#CB do for x=1,#CB[1] do
local c=CB[y][x]
if c then
p:setPosition(
(bx+x-1.5)*40+dx,
-(by+y-1.5)*40+dy
)
p:emit(1)
end
end end
end
local _oneCell={{true}}
function BP:createTouchEffect()
local p=self.particles.hitSparkle
local mat=self.hand.matrix
local cx,cy=self.handX,self.handY
for x=1,#mat[1] do for y=1,#mat do
if mat[y][x] and self:ifoverlap(_oneCell,cx+x-1,cy+y-2) then
for _=1,rnd(3,4) do
p:setPosition(
(cx+x-2+rnd())*40,
-(cy+y-2)*40
)
p:emit(1)
end
end
end end
end
function BP:createTuckEffect()
end
function BP:createHoldEffect(ifInit)
if not self.handX then return end
local cx,cy=self.handX+#self.hand.matrix[1]/2-.5,self.handY+#self.hand.matrix/2-.5
local p=self.particles.spinArrow
p:new((cx-.5)*40,-(cy-.5)*40,ifInit)
end
function BP:createPieceDropEffect()
local p=self.particles.trail
p:setPosition(
(self.handX+#self.hand.matrix[1]/2-1)*40,
-(self.handY+#self.hand.matrix/2-1)*40
)
p:emit(1)
end
function BP:createHandLockEffect() -- Basically the same as createHandErrorEffect
local CB,bx,by=self.hand.matrix,self.handX,self.handY
local dx,dy=self:getSmoothPos()
local p=self.particles.rectScale
for y=1,#CB do for x=1,#CB[1] do
local c=CB[y][x]
if c then
p:setPosition(
(bx+x-1.5)*40+dx,
-(by+y-1.5)*40+dy
)
p:emit(1)
end
end end
end
function BP:createFrenzyEffect(amount)
local p=self.particles.star
p:setEmissionArea('uniform',200,400,0,true)
p:setParticleLifetime(.626,1.6)
p:setPosition(200,-400)
p:emit(amount)
end
function BP:createSuffocateEffect()
end
function BP:createDesuffocateEffect()
end
function BP:setCellBias(x,y,bias)
local c=self.field:getCell(x,y)
if c then
bias.x=40*(bias.x or 0)
bias.y=-40*(bias.y or 0)
if c.bias then
bias.x=bias.x+c.bias.x
bias.y=bias.y+c.bias.y
end
c.bias=bias
end
end
function BP:showInvis(visStep,visMax)
for y=1,self.field:getHeight() do
for x=1,self.settings.fieldW do
local c=self.field:getCell(x,y)
if c then
c.visTimer=c.visTimer or 0
c.visStep=visStep or 1
c.visMax=visMax
end
end
end
end
function BP:getSmoothPos()
if not self.hand or self.deathTimer then
return 0,0
else
return
-- self.moveDir and self.moveCharge<self.settings.asd and 15*self.moveDir*(max(self.moveCharge,0)/self.settings.asd-.5) or 0,
0,
self.ghostY and self.handY>self.ghostY and 40*(max(1-self.dropTimer/self.settings.dropDelay*2.6,0))^2.6 or 0
end
end
--------------------------------------------------------------
-- Game methods
---@param action 'moveX' | 'moveY' | 'drop' | 'rotate' | 'reset'
function BP:moveHand(action,A,B,C,D)
--[[
moveX: dx,noShade,byAutoShift
moveY: dy,noShade
drop: dy,noShade
rotate: x,y,dir,ifInit
reset: x,y
]]
self:triggerEvent('beforeMove',action,A,B,C,D)
ins(self.hand.path,{action,A,B,C,D}) -- Track movement
local SET=self.settings
if action=='moveX' then
self.handX=self.handX+A
self:checkLanding()
if not B and SET.effect_move then
local hx,hy=self.handX,self.handY
local mat=self.hand.matrix
local w,h=#mat[1],#mat
if A<0 then
for y=1,h do for x=w,1,-1 do
if mat[y][x] then
self:createMoveEffect(hx+x-1+1,hy+y-1,hx+x-1+1-A-1,hy+y-1)
break
end end
end
elseif A>0 then
for y=1,h do for x=1,w do
if mat[y][x] then
self:createMoveEffect(hx+x-1-1,hy+y-1,hx+x-1-1-A+1,hy+y-1)
break
end end
end
end
end
elseif action=='moveY' or action=='drop' then
self.handY=self.handY+A
self.dropTimer=SET.dropDelay
self:checkLanding(true,true)
if SET.effect_move then
local hx,hy=self.handX,self.handY
local mat=self.hand.matrix
local w,h=#mat[1],#mat
if not B then
if A<0 then
for x=1,w do for y=h,1,-1 do
if mat[y][x] then
self:createMoveEffect(hx+x-1,hy+y-1+1,hx+x-1,hy+y-1+1-A-1)
break
end end
end
elseif A>0 then
for x=1,w do for y=1,h do
if mat[y][x] then
self:createMoveEffect(hx+x-1,hy+y-1-1,hx+x-1,hy+y-1-1-A+1)
break
end end
end
end
end
end
elseif action=='rotate' or action=='reset' then
self.handX,self.handY=A,B
self:checkLanding()
else
error("WTF why action is "..tostring(action))
end
if self.handX%1~=0 or self.handY%1~=0 then error("EUREKA! Decimal position") end
---@type Techmino.BrikMovement
local movement={
action=action,
brik=self.hand,
x=self.handX,y=self.handY,
}
if action=='moveX' then
if
C and
not self.ghostState and
(self.lastMovement.action=='moveY' or self.lastMovement.action=='drop') and
self:ifoverlap(self.hand.matrix,self.handX,self.handY-1) and
self:ifoverlap(self.hand.matrix,self.handX,self.handY+1)
then
self:playSound('tuck')
if SET.effect_tuck then
self:createTuckEffect()
end
end
elseif action=='moveY' then
elseif action=='rotate' then
if not self.ghostState then
if
SET.spin_immobile and
self:ifoverlap(self.hand.matrix,self.handX,self.handY-1) and
self:ifoverlap(self.hand.matrix,self.handX,self.handY+1) and
self:ifoverlap(self.hand.matrix,self.handX-1,self.handY) and
self:ifoverlap(self.hand.matrix,self.handX+1,self.handY)
then
movement.immobile=true
self:shakeBoard(C=='L' and '-ccw' or C=='R' and '-cw' or '-180')
self:playSound('rotate_locked')
if SET.effect_spin_immobile then
self:createHandErrorEffect(.942,1,1)
end
end
if SET.spin_corners then
local brikData=brikRotSys[SET.rotSys][self.hand.shape]
local state=brikData[self.hand.direction]
local centerPos=state and state.center or type(brikData.center)=='function' and brikData.center(self)
if centerPos then
local cx,cy=self.handX+centerPos[1]-.5,self.handY+centerPos[2]-.5
if cx%1+cy%1==0 then
local corners=0
if self.field:getCell(cx-1,cy-1) then corners=corners+1 end
if self.field:getCell(cx+1,cy-1) then corners=corners+1 end
if self.field:getCell(cx-1,cy+1) then corners=corners+1 end
if self.field:getCell(cx+1,cy+1) then corners=corners+1 end
if corners>=SET.spin_corners then
movement.corners=true
self:playSound('rotate_corners')
if SET.effect_spin_corners then
self:createRotateCornerEffect(cx,cy)
end
end
end
end
end
end
self:playSound(D and 'rotate_init' or 'rotate')
if SET.effect_rotate then
self:createRotateEffect(C,D)
end
end
self.lastMovement=movement
self:tryCancelSuffocate()
self:triggerEvent('afterMove',action,A,B,C,D)
end
function BP:restoreBrikState(brik) -- Restore a brik object's state (only inside, like shape, name, direction)
if not brik then brik=self.hand end
if brik._origin then
for k,v in next,brik._origin do
brik[k]=v
end
end
end
function BP:resetPos() -- Move hand piece to the normal spawn position
if self.hand.path then
TABLE.clear(self.hand.path)
else
self.hand.path={}
end
self:moveHand('reset',floor(self.settings.fieldW/2-#self.hand.matrix[1]/2+1),self.settings.spawnH+1+ceil(self.fieldDived/40))
self:triggerEvent('beforeResetPos')
self.deathTimer=false
self.ghostState=false
while self:isSuffocate() and self.handY<self.settings.spawnH+self.settings.extraSpawnH+1 do self.handY=self.handY+1 end
self.minY=self.handY
self.ghostY=false
self:resetPosCheck()
self.stat.spawn=self.stat.spawn+1
self:triggerEvent('afterResetPos')
end
function BP:resetPosCheck()
local SET=self.settings
local suffocated
if self.deathTimer then
self.ghostState=false
suffocated=self:isSuffocate()
self.ghostState=true
else
suffocated=self:isSuffocate()
end
if suffocated then
if SET.deathDelay>0 then
self.deathTimer=SET.deathDelay
self.ghostState=true
-- Suffocate IMS, always trigger when held
if self.keyState.softDrop then self:moveDown() end
if self.keyState.moveRight~=self.keyState.moveLeft then
if self.keyState.moveRight then self:moveRight() else self:moveLeft() end
end
-- Suffocate IRS
if SET.bufferRotate then
if SET.bufferRotate=='hold' then
if self.keyState.rotate180 then
self:rotate('F',true)
elseif self.keyState.rotateCW~=self.keyState.rotateCCW then
self:rotate(self.keyState.rotateCW and 'R' or 'L',true)
end
elseif SET.bufferRotate=='buffer' then
if self.keyBuffer.rotate then
self:rotate(self.keyBuffer.rotate,true)
if not self.keyBuffer.hold then
self.keyBuffer.rotate=false
end
end
end
end
self:tryCancelSuffocate()
self:freshGhost()
if self.deathTimer then
self:playSound('suffocate')
if SET.effect_suffocate then
self:createSuffocateEffect()
end
end
else
self:triggerEvent('whenSuffocate')
self:freshGhost()
if self:isSuffocate() then
self:lock()
self:finish('suffocate')
end
return
end
else
-- IMS
if SET.bufferMove then
if SET.bufferMove=='hold' then
if self.keyState.softDrop then self:moveDown() end
if self.keyState.moveRight~=self.keyState.moveLeft then
if self.keyState.moveRight then self:moveRight(true) else self:moveLeft(true) end
end
elseif SET.bufferMove=='buffer' then
if self.keyBuffer.move then
if self.keyBuffer.move=='L' then
self:moveLeft(true)
elseif self.keyBuffer.move=='R' then
self:moveRight(true)
end
self.keyBuffer.move=false
end
end
end
-- IRS
if SET.bufferRotate then
if SET.bufferRotate=='hold' then
if self.keyState.rotate180 then
self:rotate('F',true)
elseif self.keyState.rotateCW~=self.keyState.rotateCCW then
self:rotate(self.keyState.rotateCW and 'R' or 'L',true)
end
elseif SET.bufferRotate=='buffer' then
if self.keyBuffer.rotate then
self:rotate(self.keyBuffer.rotate,true)
if not self.keyBuffer.hold then
self.keyBuffer.rotate=false
end
end
end
end
self:freshGhost()
self:freshDelay('spawn')
end
if SET.stopMoveWhenSpawn then
self.moveDir=false
self.moveCharge=0
elseif SET.ash>0 then
self.moveCharge=min(self.moveCharge,SET.asd-SET.ash)
end
end
function BP:freshGhost()
if self.hand then
if self.ghostState then
self.ghostY=false
else
self.ghostY=min(self.field:getHeight()+1,self.handY)
-- Move ghost to bottom
while not self:ifoverlap(self.hand.matrix,self.handX,self.ghostY-1) do
self.ghostY=self.ghostY-1
end
-- infG check
if (self.settings.dropDelay<=0 or self.downCharge and self.settings.adp==0) and self.ghostY<self.handY then
local dY=self.ghostY-self.handY
self:moveHand('drop',dY)
self:freshDelay('drop')
self:shakeBoard('-drop',-dY/self.settings.spawnH)
else
self:freshDelay('move')
end
end
end
end
function BP:tryCancelSuffocate()
if self.deathTimer then
self.ghostState=false
if self:isSuffocate() then
self.ghostState=true
else
self.deathTimer=false
self:playSound('desuffocate')
if self.settings.effect_suffocate then
self:createDesuffocateEffect()
end
end
end
end
local freshRuleMap={-- Normal: cost 1 chance, F: free, S: step (fall only) R: reset all, _: none
move= {any='N',fall='S',never='_'},
rotate={any='N',fall='S',never='_'},
moving={any='N',fall='S',never='_'},
drop= {any='F',fall='S',never='_'},
spawn= {any='R',fall='R',never='R'},
}
---@param reason 'move' | 'rotate' | 'moving' | 'drop' | 'spawn'
function BP:freshDelay(reason)
local fell=self.handY<self.minY
if fell then self.minY=self.handY end
local mode=freshRuleMap[reason][self.settings.freshCondition]
if mode=='S' then mode=fell and 'N' or '_' end
if mode=='N' or mode=='F' then
local add=min(self.settings.lockDelay-self.lockTimer,self.freshTime)
if self.freshChance>0 and self.lockTimer<self.settings.lockDelay and add>0 then
self.lockTimer=self.lockTimer+add
self.freshTime=self.freshTime-add
self.freshChance=self.freshChance-(mode=='N' and 1 or 0)
return true
end
elseif mode=='R' then
self.dropTimer=self.settings.dropDelay
self.lockTimer=self.settings.lockDelay
self.freshChance=self.settings.maxFreshChance
self.freshTime=self.settings.maxFreshTime
return true
elseif not mode then
error("WTF why settings.freshCondition is "..tostring(self.settings.freshCondition))
end
end
---@param seqData string | Techmino.Mech.Brik.SequenceName | fun(P:Techmino.Player.Brik, d:table, init:boolean): Techmino.Brik.ID?
---@param args? string Can include '-clearData' and '-clearNext'
function BP:setSequenceGen(seqData,args)
if type(args)~='string' then args='' end
if args:sArg('-clearData') then self.seqData={} end
if args:sArg('-clearNext') then TABLE.clear(self.nextQueue) end
self.seqGen=GAME.mechLib.brik.sequence[seqData] or seqData
assert(self:seqGen(self.seqData,true)==nil,"First call of sequence generator must return nil")
self:freshNextQueue()
end
function BP:freshNextQueue()
while #self.nextQueue<max(self.settings.nextSlot,1) do
local shape=self:seqGen(self.seqData)
if type(shape)=='number' then
self:pushNext(shape)
elseif type(shape)=='string' then
self:pushNext(shape)
elseif type(shape)=='table' then
if shape[1] then
self:pushNext(shape)
else
ins(self.nextQueue,self:getBrik(shape))
end
else
break
end
end
end
function BP:clearHold()
TABLE.clear(self.holdQueue)
end
function BP:clearNext()
TABLE.clear(self.nextQueue)
end
---@param piece string | number | table
function BP:pushNext(piece)
if type(piece)=='number' then
ins(self.nextQueue,self:getBrik(Brik.get(piece)))
elseif type(piece)=='string' then
for i=1,#piece do
ins(self.nextQueue,self:getBrik(Brik.get(piece:sub(i,i))))
end
elseif type(piece)=='table' then
for i=1,#piece do
assert(type(piece[i])=='number' or type(piece[i])=='string',"Must be simple table")
self:pushNext(piece[i])
end
else
error("arg must be string or table")
end
end
function BP:popNext(ifHold)
-- TODO: IHS can make hold slot empty
if self.nextQueue[1] then -- Normally there area pieces in next queue
self.hand=rem(self.nextQueue,1)
self:freshNextQueue()
if not ifHold then
self.holdTime=0
end
else -- Next queue is empty
if self.holdQueue[1] then -- Force using hold
ins(self.nextQueue,rem(self.holdQueue,1))
self:popNext()
else -- No piece to use, game over
self:finish('exhaust')
end
return
end
-- IHS
local IHStriggered
if not ifHold and self.settings.bufferHold then
if self.settings.bufferHold=='hold' then
if self.keyState.holdPiece then
IHStriggered=self:hold(true)
end
elseif self.settings.bufferHold=='buffer' then
if self.keyBuffer.hold then
self.keyBuffer.hold=false
IHStriggered=self:hold(true)
end
end
end
if not IHStriggered then
self:resetPos()
end
self:triggerEvent('afterSpawn')
if self.settings.bufferDrop then -- IHdS
if self.settings.bufferDrop=='hold' then
if self.keyState.hardDrop then
self:brikDropped()
end
elseif self.settings.bufferDrop=='buffer' then
if self.keyBuffer.hardDrop then
self.keyBuffer.hardDrop=false
self:brikDropped()
end
end
end
end
---@return Techmino.Brik.Cell
function BP:newCell(color,id)
self.totalCellCount=self.totalCellCount+1
return {
cid='_'..STRING.toHex(self.totalCellCount),
id=id,
color=color or 0,
conn={},
}
end
---@param shapeData Techmino.Brik | Techmino.Brik.Name | Techmino.Brik.ID
function BP:getBrik(shapeData)
local shapeID,shapeName,shapeMat,shapeColor
if type(shapeData)=='table' then
---@cast shapeData table
shapeID=shapeData.id
shapeName=shapeData.name or "?"
shapeMat=TABLE.copy(shapeData.shape)
shapeColor=shapeData.color or self.settings.palette[shapeID] or self:random('rndPieceColor',0,999)
else
---@cast shapeData Techmino.Brik.Name | Techmino.Brik.ID
local brik=Brik.get(shapeData)
if not brik then errorf("invalid shapeData %s",tostring(shapeData)) end
shapeID=brik.id
shapeName=brik.name
shapeMat=TABLE.copy(brik.shape)
shapeColor=self.settings.palette[shapeID] or self:random('rndPieceColor',0,999)
end
self.pieceCount=self.pieceCount+1
-- Generate cell matrix from bool matrix
---@cast shapeMat Mat<Techmino.Brik.Cell>
for y=1,#shapeMat do for x=1,#shapeMat[1] do
shapeMat[y][x]=shapeMat[y][x] and self:newCell(shapeColor,self.pieceCount)
end end
-- Connect cells
for y=1,#shapeMat do for x=1,#shapeMat[1] do
if shapeMat[y][x] then
local L=shapeMat[y][x].conn
local b
b=shapeMat[y] if b and b[x-1] then L[b[x-1].cid]=0 end
b=shapeMat[y] if b and b[x+1] then L[b[x+1].cid]=0 end
b=shapeMat[y-1] if b and b[x] then L[b[x] .cid]=0 end
b=shapeMat[y+1] if b and b[x] then L[b[x] .cid]=0 end
b=shapeMat[y-1] if b and b[x-1] then L[b[x-1].cid]=0 end
b=shapeMat[y-1] if b and b[x+1] then L[b[x+1].cid]=0 end
b=shapeMat[y+1] if b and b[x-1] then L[b[x-1].cid]=0 end
b=shapeMat[y+1] if b and b[x+1] then L[b[x+1].cid]=0 end
end
end end
local brik={
id=self.pieceCount,
shape=shapeID,
direction=0,
name=shapeName,
matrix=shapeMat,
}
brik._origin=TABLE.copyAll(brik,0)
return brik
end
function BP:getConnectedCells(x0,y0)
local F=self.field
local cell=F:getCell(x0,y0)
if cell then
local list={{x0,y0,cell},[cell]=true}
local ptr=1
while list[ptr] do
local x,y,c0=list[ptr][1],list[ptr][2],list[ptr][3]
if c0.conn then
local c1
c1=F:getCell(x+1,y) if c1 and not list[c1] and c0.conn[c1.cid] then ins(list,{x+1,y,c1}) list[c1]=true end
c1=F:getCell(x-1,y) if c1 and not list[c1] and c0.conn[c1.cid] then ins(list,{x-1,y,c1}) list[c1]=true end
c1=F:getCell(x,y+1) if c1 and not list[c1] and c0.conn[c1.cid] then ins(list,{x,y+1,c1}) list[c1]=true end
c1=F:getCell(x,y-1) if c1 and not list[c1] and c0.conn[c1.cid] then ins(list,{x,y-1,c1}) list[c1]=true end
end
ptr=ptr+1
end
return list
else
return {}
end
end
function BP:compareMatrix(S1,S2)
if not S2 then
if not self.hand then return false end
S2=self.hand.matrix
end
if #S1~=#S2 then return false end
for y=1,#S1 do
if #S1[y]~=#S2[y] then return false end
for x=1,#S1[1] do
if not not S1[y][x]~=not not S2[y][x] then
return false
end
end
end
return true
end
function BP:cutConnection(y) -- y is cutting line height to ground
local F=self.field
if y<1 or y>F:getHeight() then return end
for x=1,self.settings.fieldW do
local cell=F:getCell(x,y)
if cell then
local listLo=self:getConnectedCells(x,y)
local listHi={}
for i=#listLo,1,-1 do
if listLo[i][2]>y then
ins(listHi,rem(listLo,i))
end
end
for i=1,#listLo do
for j=1,#listHi do
listLo[i][3].conn[listHi[j][3].cid]=nil
listHi[j][3].conn[listLo[i][3].cid]=nil
end
end
end
end
end
function BP:ifoverlap(CB,cx,cy)
local F=self.field
-- Must in wall
if cx<=0 or cx+#CB[1]-1>self.settings.fieldW or cy<=0 then return true end
-- Special check
for i=1,#self.event.extraSolidCheck do
local res=self.event.extraSolidCheck[i][2](self,CB,cx,cy)
if res~=nil then return res end
end
-- Must in air
if cy>F:getHeight() then return false end
if not self.ghostState then
-- Check field
for y=1,#CB do for x=1,#CB[1] do
if CB[y][x] and self.field:getCell(cx+x-1,cy+y-1) then
return true
end
end end
end
-- No collision
return false
end
function BP:checkLanding(sparkles,drop)
if self.handY==self.ghostY then
if drop then
self:playSound('touch')
end
if sparkles and self.settings.effect_touch then
self:createTouchEffect()
end
end
end
function BP:isSuffocate()
return self.hand and self:ifoverlap(self.hand.matrix,self.handX,self.handY)
end
function BP:moveLeft(ifInit)
if not self:ifoverlap(self.hand.matrix,self.handX-1,self.handY) then
self:moveHand('moveX',-1,true,false,ifInit)
if not ifInit then self:freshGhost() end
return true
end
end
function BP:moveRight(ifInit)
if not self:ifoverlap(self.hand.matrix,self.handX+1,self.handY) then
self:moveHand('moveX',1,true,false,ifInit)
if not ifInit then self:freshGhost() end
return true
end
end
function BP:moveDown(ifInstant)
if not self:ifoverlap(self.hand.matrix,self.handX,self.handY-1) then
self:moveHand('moveY',-1,not ifInstant)
self:freshDelay('drop')
return true
end
end
function BP:moveUp() -- ?
if not self:ifoverlap(self.hand.matrix,self.handX,self.handY+1) then
self:moveHand('moveY',1,true)
return true
end
end
---@param dir 'R' | 'L' | 'F'
function BP:rotate(dir,ifInit)
if self.settings.stopMoveWhenRotate then
self.moveDir=false
self.moveCharge=0
end
if not self.hand then return end
if dir~='R' and dir~='L' and dir~='F' then error("WTF why dir isn't R/L/F ("..tostring(dir)..")") end
local brikData=brikRotSys[self.settings.rotSys][self.hand.shape]
if not brikData then return end
local origY=self.handY -- For IRS pushing up
if brikData.rotate then -- Custom rotate function
brikData.rotate(self,dir,ifInit)
if self.ghostState and self.settings.IRSpushUp then self:moveHand('moveY',origY-self.handY) end
self:freshGhost()
else -- Normal rotate procedure
local preState=brikData[self.hand.direction]
if preState then
-- Rotate matrix
local kick=preState[dir]
if not kick then return end -- This RS doesn't define this rotation
local cb=self.hand.matrix
local icb=TABLE.rotate(cb,dir)
local baseX,baseY
local afterState=brikData[kick.target]
if kick.base then
baseX=kick.base[1]
baseY=kick.base[2]
elseif preState.center and afterState.center then
baseX=preState.center[1]-afterState.center[1]
baseY=preState.center[2]-afterState.center[2]
else
error("Cannot get baseX/Y")
end
for n=1,#kick.test do
local ix,iy=self.handX+baseX+kick.test[n][1],self.handY+baseY+kick.test[n][2]
if
not self:ifoverlap(icb,ix,iy) and
(self.freshTime>0 and self.freshChance>0 or kick.test[n][2]<=0)
then
self.hand.matrix=icb
self.hand.direction=kick.target
self:moveHand('rotate',ix,iy,dir,ifInit)
if self.ghostState and self.settings.IRSpushUp then self:moveHand('moveY',origY-self.handY) end
self:freshGhost()
return
end
end
if not ifInit then self:freshDelay('rotate') end
self:playSound('rotate_failed')
if self.settings.effect_move_fail then
self:createHandErrorEffect(1,.26,.26)
end
self:triggerEvent('onMoveFail','rotate',dir)
else
error("WTF why no state in brikData")
end
end
end
---@return boolean success
function BP:hold(ifInit)
if self.holdTime>=self.settings.holdSlot and not self.settings.infHold or self.settings.holdSlot==0 then return end
if self.settings.effect_hold then
self:createHoldEffect(ifInit)
end
if self.settings.stopMoveWhenHold then
self.moveDir=false
self.moveCharge=0
end
local mode=self.settings.holdMode
if mode=='hold' then self:hold_hold()
elseif mode=='swap' then self:hold_swap()
elseif mode=='float' then self:hold_float()
else error("WTF why hold mode is "..tostring(mode))
end
self.holdTime=self.holdTime+1
self:playSound(ifInit and 'hold_init' or 'hold')
return true
end
function BP:hold_hold()
if not self.settings.holdKeepState then
self:restoreBrikState()
end
-- Swap hand and hold
local swapN=self.holdTime%self.settings.holdSlot+1
self.hand,self.holdQueue[swapN]=self.holdQueue[swapN],self.hand
-- Reset position or pop next out
if self.hand then
self:resetPos()
else
self:popNext(true)
end
end
function BP:hold_swap()
local swapN=self.holdTime%self.settings.holdSlot+1
if self.nextQueue[swapN] then
if not self.settings.holdKeepState then
self:restoreBrikState()
end
self.hand,self.nextQueue[swapN]=self.nextQueue[swapN],self.hand
self:resetPos()
end
end
function BP:hold_float()
local swapN=self.holdTime%self.settings.holdSlot+1
if self.floatHolds[swapN] then
local h=self.floatHolds[swapN]
h.hand,self.hand=self.hand,h.hand
h.handX,self.handX=self.handX,h.handX
h.handY,self.handY=self.handY,h.handY
h.lastMovement,self.lastMovement=self.lastMovement,h.lastMovement
h.minY,self.minY=self.minY,h.minY
while self:isSuffocate() and self.handY<self.settings.spawnH+self.settings.extraSpawnH+1 do self.handY=self.handY+1 end
self:resetPosCheck()
else
self.floatHolds[swapN]={
hand=self.hand,
handX=self.handX,
handY=self.handY,
lastMovement=self.lastMovement,
minY=self.minY,
}
self.hand=false
self.handX,self.handY=false,false
self:popNext(true)
end
end