forked from sharpobject/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.lua
More file actions
2125 lines (1986 loc) · 71.4 KB
/
engine.lua
File metadata and controls
2125 lines (1986 loc) · 71.4 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
-- Stuff defined in this file:
-- . the data structures that store the configuration of
-- the stack of panels
-- . the main game routine
-- (rising, timers, falling, cursor movement, swapping, landing)
-- . the matches-checking routine
local min, pairs, deepcpy = math.min, pairs, deepcpy
local max = math.max
local garbage_bounce_time = #garbage_bounce_table
local GARBAGE_DELAY = 60
local GARBAGE_TRANSIT_TIME = 90
local clone_pool = {}
local current_music_is_casual = false -- must be false so that casual music start playing
Stack = class(function(s, which, mode, panels_dir, speed, difficulty, player_number)
s.character = config.character
s.max_health = 1
s.panels_dir = panels_dir or config.panels_dir
if IMG_panels[panels_dir] == nil then
s.panels_dir = config.panels_dir
end
s.mode = mode or "endless"
if mode ~= "puzzle" then
s.do_first_row = true
end
if s.mode == "endless" then
s.NCOLORS = difficulty_to_ncolors_endless[difficulty]
end
if s.mode == "time" then
s.NCOLORS = difficulty_to_ncolors_1Ptime[difficulty]
end
-- frame.png dimensions
s.canvas = love.graphics.newCanvas(104*GFX_SCALE,204*GFX_SCALE)
s.canvas:setFilter("nearest","nearest")
if s.mode == "2ptime" or s.mode == "vs" then
local level = speed or 5
s.character = (type(difficulty) == "string") and difficulty or s.character
s.level = level
speed = level_to_starting_speed[level]
--difficulty = level_to_difficulty[level]
s.speed_times = {15*60, idx=1, delta=15*60}
s.max_health = level_to_hang_time[level]
s.FRAMECOUNT_HOVER = level_to_hover[s.level]
s.FRAMECOUNT_GPHOVER = level_to_garbage_panel_hover[s.level]
s.FRAMECOUNT_FLASH = level_to_flash[s.level]
s.FRAMECOUNT_FACE = level_to_face[s.level]
s.FRAMECOUNT_POP = level_to_pop[s.level]
s.combo_constant = level_to_combo_constant[s.level]
s.combo_coefficient = level_to_combo_coefficient[s.level]
s.chain_constant = level_to_chain_constant[s.level]
s.chain_coefficient = level_to_chain_coefficient[s.level]
if s.mode == "2ptime" then
s.NCOLORS = level_to_ncolors_time[level]
else
s.NCOLORS = level_to_ncolors_vs[level]
end
end
s.health = s.max_health
s.garbage_cols = {{1,2,3,4,5,6,idx=1},
{1,3,5,idx=1},
{1,4,idx=1},
{1,2,3,idx=1},
{1,2,idx=1},
{1,idx=1}}
s.later_garbage = {}
s.garbage_q = GarbageQueue()
-- garbage_to_send[frame] is an array of garbage to send at frame.
-- garbage_to_send.chain is an array of garbage to send when the chain ends.
s.garbage_to_send = {}
move_stack(s,1)
s.panel_buffer = ""
s.gpanel_buffer = ""
s.input_buffer = ""
s.panels = {}
s.width = 6
s.height = 12
for i=0,s.height do
s.panels[i] = {}
for j=1,s.width do
s.panels[i][j] = Panel()
end
end
s.CLOCK = 0
s.game_stopwatch = 0
s.do_countdown = true
s.max_runs_per_frame = 3
s.displacement = 16
-- This variable indicates how far below the top of the play
-- area the top row of panels actually is.
-- This variable being decremented causes the stack to rise.
-- During the automatic rising routine, if this variable is 0,
-- it's reset to 15, all the panels are moved up one row,
-- and a new row is generated at the bottom.
-- Only when the displacement is 0 are all 12 rows "in play."
s.danger_col = {false,false,false,false,false,false}
-- set true if this column is near the top
s.danger_timer = 0 -- decides bounce frame when in danger
s.difficulty = difficulty or 2
s.speed = speed or 1 -- The player's speed level decides the amount of time
-- the stack takes to rise automatically
if s.speed_times == nil then
s.panels_to_speedup = panels_to_next_speed[s.speed]
end
s.rise_timer = 1 -- When this value reaches 0, the stack will rise a pixel
s.rise_lock = false -- If the stack is rise locked, it won't rise until it is
-- unlocked.
s.has_risen = false -- set once the stack rises once during the game
s.stop_time = 0
s.pre_stop_time = 0
s.NCOLORS = s.NCOLORS or 5
s.score = 0 -- der skore
s.chain_counter = 0 -- how high is the current chain
s.panels_in_top_row = false -- boolean, for losing the game
s.danger = s.danger or false -- boolean, panels in the top row (danger)
s.danger_music = s.danger_music or false -- changes music state
s.n_active_panels = 0
s.prev_active_panels = 0
s.n_chain_panels= 0
-- These change depending on the difficulty and speed levels:
s.FRAMECOUNT_HOVER = s.FRAMECOUNT_HOVER or FC_HOVER[s.difficulty]
s.FRAMECOUNT_FLASH = s.FRAMECOUNT_FLASH or FC_FLASH[s.difficulty]
s.FRAMECOUNT_FACE = s.FRAMECOUNT_FACE or FC_FACE[s.difficulty]
s.FRAMECOUNT_POP = s.FRAMECOUNT_POP or FC_POP[s.difficulty]
s.FRAMECOUNT_MATCH = s.FRAMECOUNT_FACE + s.FRAMECOUNT_FLASH
s.FRAMECOUNT_RISE = speed_to_rise_time[s.speed]
s.rise_timer = s.FRAMECOUNT_RISE
-- Player input stuff:
s.manual_raise = false -- set until raising is completed
s.manual_raise_yet = false -- if not set, no actual raising's been done yet
-- since manual raise button was pressed
s.prevent_manual_raise = false
s.swap_1 = false -- attempt to initiate a swap on this frame
s.swap_2 = false
s.cur_wait_time = 25 -- number of ticks to wait before the cursor begins
-- to move quickly... it's based on P1CurSensitivity
s.cur_timer = 0 -- number of ticks for which a new direction's been pressed
s.cur_dir = nil -- the direction pressed
s.cur_row = 7 -- the row the cursor's on
s.cur_col = 3 -- the column the left half of the cursor's on
s.top_cur_row = s.height + (s.mode == "puzzle" and 0 or -1)
s.move_sound = false -- this is set if the cursor movement sound should be played
s.poppedPanelIndex = s.poppedPanelIndex or 1
s.panels_cleared = s.panels_cleared or 0
s.metal_panels_queued = s.metal_panels_queued or 0
s.lastPopLevelPlayed = s.lastPopLevelPlayed or 1
s.lastPopIndexPlayed = s.lastPopIndexPlayed or 1
s.combo_chain_play = nil
s.game_over = false
s.card_q = Queue()
s.which = which or 1 -- Pk.which == k
s.player_number = player_number or s.which --player number according to the multiplayer server, for game outcome reporting
s.shake_time = 0
s.prev_states = {}
s.enable_analytics = false
end)
function Stack.mkcpy(self, other)
if other == nil then
if #clone_pool == 0 then
other = {}
else
other = clone_pool[#clone_pool]
clone_pool[#clone_pool] = nil
end
end
other.do_swap = self.do_swap
other.speed = self.speed
other.health = self.health
other.garbage_cols = deepcpy(self.garbage_cols)
--[[if self.garbage_cols then
other.garbage_idxs = other.garbage_idxs or {}
local n_g_cols = #(self.garbage_cols or other.garbage_cols)
for i=1,n_g_cols do
other.garbage_idxs[i]=self.garbage_cols[i].idx
end
else
end--]]
other.garbage_q = deepcpy(self.garbage_q)
other.garbage_to_send = deepcpy(self.garbage_to_send)
other.input_state = self.input_state
local height = self.height or other.height
local width = self.width or other.width
local height_to_cpy = #self.panels
other.panels = other.panels or {}
for i=1,height_to_cpy do
if other.panels[i] == nil then
other.panels[i] = {}
for j=1,width do
other.panels[i][j] = Panel()
end
end
for j=1,width do
local opanel = other.panels[i][j]
local spanel = self.panels[i][j]
opanel:clear()
for k,v in pairs(spanel) do
opanel[k] = v
end
end
end
for i=height_to_cpy+1,#other.panels do
for j=1,width do
other.panels[i][j]:clear()
end
end
other.CLOCK = self.CLOCK
other.game_stopwatch = self.game_stopwatch
other.game_stopwatch_running = self.game_stopwatch_running
other.cursor_lock = self.cursor_lock
other.displacement = self.displacement
other.speed_times = deepcpy(self.speed_times)
other.panels_to_speedup = self.panels_to_speedup
other.stop_time = self.stop_time
other.pre_stop_time = self.pre_stop_time
other.score = self.score
other.chain_counter = self.chain_counter
other.n_active_panels = self.n_active_panels
other.prev_active_panels = self.prev_active_panels
other.n_chain_panels = self.n_chain_panels
other.FRAMECOUNT_RISE = self.FRAMECOUNT_RISE
other.rise_timer = self.rise_timer
other.manual_raise_yet = self.manual_raise_yet
other.prevent_manual_raise = self.prevent_manual_raise
other.cur_timer = self.cur_timer
other.cur_dir = self.cur_dir
other.cur_row = self.cur_row
other.cur_col = self.cur_col
other.shake_time = self.shake_time
other.peak_shake_time = self.peak_shake_time
other.card_q = deepcpy(self.card_q)
other.do_countdown = self.do_countdown
other.ready_y = self.ready_y
return other
end
function Stack.fromcpy(self, other)
Stack.mkcpy(other,self)
self:remove_extra_rows()
end
Panel = class(function(p)
p:clear()
end)
function Panel.clear(self)
-- color 0 is an empty panel.
-- colors 1-7 are normal colors, 8 is [!].
self.color = 0
-- A panel's timer indicates for how many more frames it will:
-- . be swapping
-- . sit in the MATCHED state before being set POPPING
-- . sit in the POPPING state before actually being POPPED
-- . sit and be POPPED before disappearing for good
-- . hover before FALLING
-- depending on which one of these states the panel is in.
self.timer = 0
-- is_swapping is set if the panel is swapping.
-- The panel's timer then counts down from 3 to 0,
-- causing the swap to end 3 frames later.
-- The timer is also used to offset the panel's
-- position on the screen.
self.initial_time = nil
self.pop_time = nil
self.pop_index = nil
self.x_offset = nil
self.y_offset = nil
self.width = nil
self.height = nil
self.garbage = nil
self.metal = nil
-- Also flags
self:clear_flags()
end
-- states:
-- swapping, matched, popping, popped, hovering,
-- falling, dimmed, landing, normal
-- flags:
-- from_left
-- dont_swap
-- chaining
GarbageQueue = class(function(s)
s.chain_garbage = Queue()
s.combo_garbage = {0,0,0,0,0,0} --index here represents width, and value represents how many of that width queued
s.metal = 0
end)
function GarbageQueue.push(self, garbage)
local width, height, metal, from_chain = unpack(garbage)
if metal then
self.metal = self.metal + 1
elseif from_chain or height > 1 then
if not from_chain then
print("ERROR: garbage with height > 1 was not marked as 'from_chain'")
print("adding it to the chain garbage queue anyway")
end
self.chain_garbage:push(garbage)
else
self.combo_garbage[width] = self.combo_garbage[width] + 1
end
end
function GarbageQueue.pop(self, just_peeking)
--check for any chain garbage, and return the first one (chronologically), if any
if self.chain_garbage:peek() then
if just_peeking then
return self.chain_garbage:peek()
else
return self.chain_garbage:pop()
end
end
--check for any combo garbage, and return the smallest one, if any
for k,v in ipairs(self.combo_garbage) do
if v > 0 then
if not just_peeking then
self.combo_garbage[k] = v - 1
end
--returning {width, height, is_metal, is_from_chain}
return {k, 1, false, false}
end
end
--check for any metal garbage, and return one if any
if self.metal > 0 then
if not just_peeking then
self.metal = self.metal - 1
end
return {6, 1, true, false}
end
return nil
end
function GarbageQueue.peek(self)
return self:pop(true) --(just peeking)
end
function GarbageQueue.len(self)
local ret = 0
ret = ret + self.chain_garbage:len()
for k,v in ipairs(self.combo_garbage) do
ret = ret + v
end
ret = ret + self.metal
return ret
end
function GarbageQueue.grow_chain(self)
-- TODO: this should increase the size of the first chain garbage by 1.
-- This is used by the telegraph to increase the size of the chain garbage being built
-- or add a 6-wide if there is not chain garbage yet in the queue
end
Telegraph = class(function(self, sender, recipient)
self.garbage_queue = new GarbageQueue()
self.stopper = { garbage_type, size, frame_to_release}
self.sender = sender
self.recipient = recipient
end)
function Telegraph.push(self, attack_type, attack_size)
self.stopper = {garbage_type=attack_type, attack_size, frame_to_release=self.stack.CLOCK+GARBAGE_TRANSIT_TIME+GARBAGE_DELAY}
if attack_type == "chain" then
self.garbage_queue:grow_chain()
elseif attack_type == "combo" then
local garbage = {--[[TODO: pull this code from sharpobject's existing code for changing combos to garbage]]}
self.garbage_queue:push(garbage)
end
end
function Telegraph.pop_all_ready_garbage()
local ready_garbage = {}
if self.stopper and self.stopper.frame_to_release <= self.recipient.CLOCK then
self.stopper = nil
end
if not self.stopper then
local next_block = {}
local number_of_blocks = self.garbage_queue:len()
for i=1, number_of_blocks do
ready_garbage[i] = self.garbage_queue:pop()
end
return ready_garbage
elseif self.stopper and self.stopper.garbage_type == "chain" then
return {} --waiting on sender chain to end
elseif self.stopper and self.stopper.garbage_type == "combo" and stopper.garbage then
local next_block_type = "combo"
local next_in_queue = self.garbage_queue:peek()
while not next_in_queue[4]--[[is_from_chain]] and next_in_queue[1]--[[width]] < self.stopper.size do
ready_garbage[#ready_garbage+1] = self.garbage_queue:pop()
next_in_queue = self.garbage_queue:peek()
end
return ready_garbage
end
end
function Telegraph.sender_chain_ended()
self.stopper = nil
end
do
local exclude_hover_set = {matched=true, popping=true, popped=true,
hovering=true, falling=true}
function Panel.exclude_hover(self)
return exclude_hover_set[self.state] or self.garbage
end
local exclude_match_set = {swapping=true, matched=true, popping=true,
popped=true, dimmed=true, falling=true}
function Panel.exclude_match(self)
return exclude_match_set[self.state] or self.color == 0 or self.color == 9
or (self.state == "hovering" and not self.match_anyway)
end
local exclude_swap_set = {matched=true, popping=true, popped=true,
hovering=true, dimmed=true}
function Panel.exclude_swap(self)
return exclude_swap_set[self.state] or self.dont_swap or self.garbage
end
function Panel.support_garbage(self)
return self.color ~= 0 or self.hovering
end
-- "Block garbage fall" means
-- "falling-ness should not propogate up through this panel"
-- We need this because garbage doesn't hover, it just falls
-- opportunistically.
local block_garbage_fall_set = {matched=true, popping=true,
popped=true, hovering=true, swapping=true}
function Panel.block_garbage_fall(self)
return block_garbage_fall_set[self.state] or self.color == 0
end
function Panel.dangerous(self)
return self.color ~= 0 and (self.state ~= "falling" or not self.garbage)
end
end
function Panel.has_flags(self)
return (self.state ~= "normal") or self.is_swapping_from_left
or self.dont_swap or self.chaining
end
function Panel.clear_flags(self)
self.combo_index = nil
self.combo_size = nil
self.chain_index = nil
self.is_swapping_from_left = nil
self.dont_swap = nil
self.chaining = nil
-- Animation timer for "bounce" after falling from garbage.
self.fell_from_garbage = nil
self.state = "normal"
end
function Stack.set_puzzle_state(self, pstr, n_turns)
-- Copy the puzzle into our state
local sz = self.width * self.height
while string.len(pstr) < sz do
pstr = "0" .. pstr
end
local idx = 1
local panels = self.panels
for row=self.height,1,-1 do
for col=1, self.width do
panels[row][col]:clear()
panels[row][col].color = string.sub(pstr, idx, idx) + 0
idx = idx + 1
end
end
self.puzzle_moves = n_turns
characters[self.character]:stop_sounds()
end
function Stack.puzzle_done(self)
local panels = self.panels
for row=1, self.height do
for col=1, self.width do
local color = panels[row][col].color
if color ~= 0 and color ~= 9 then
return false
end
end
end
return true
end
function Stack.has_falling_garbage(self)
for i=1,self.height+3 do --we shouldn't have to check quite 3 rows above height, but just to make sure...
local prow = self.panels[i]
for j=1,self.width do
if prow and prow[j].garbage and prow[j].state == "falling" then
return true
end
end
end
return false
end
function Stack.prep_rollback(self)
-- Do stuff for rollback.
local prev_states = self.prev_states
-- prev_states will not exist if we're doing a rollback right now
if prev_states then
local garbage_target = self.garbage_target
self.garbage_target = nil
self.prev_states = nil
prev_states[self.CLOCK] = self:mkcpy()
clone_pool[#clone_pool+1] = prev_states[self.CLOCK-400]
prev_states[self.CLOCK-400] = nil
self.prev_states = prev_states
self.garbage_target = garbage_target
end
end
function Stack.starting_state(self, n)
if self.do_first_row then
self.do_first_row = nil
for i=1,(n or 8) do
self:new_row()
self.cur_row = self.cur_row-1
end
end
characters[self.character]:stop_sounds()
end
function Stack.prep_first_row(self)
if self.do_first_row then
self.do_first_row = nil
self:new_row()
self.cur_row = self.cur_row-1
end
end
function Stack.controls(self)
local new_dir = nil
local sdata = self.input_state
local raise, swap, up, down, left, right = unpack(base64decode[sdata])
if (raise) and (not self.prevent_manual_raise) then
self.manual_raise = true
self.manual_raise_yet = false
end
self.swap_1 = swap
self.swap_2 = swap
if up then
new_dir = "up"
elseif down then
new_dir = "down"
elseif left then
new_dir = "left"
elseif right then
new_dir = "right"
end
if new_dir == self.cur_dir then
if self.cur_timer ~= self.cur_wait_time then
self.cur_timer = self.cur_timer + 1
end
else
self.cur_dir = new_dir
self.cur_timer = 0
end
end
--local_run is for the stack that belongs to this client.
function Stack.local_run(self)
self:update_cards()
self.input_state = self:send_controls()
self:prep_rollback()
self:controls()
self:prep_first_row()
self:PdP()
end
--foreign_run is for a stack that belongs to another client.
function Stack.foreign_run(self)
-- Decide how many frames of input we should run.
local times_to_run = 0
local buffer_len = string.len(self.input_buffer)
-- If we're way behind, run at max speed.
if buffer_len >= 15 then
times_to_run = self.max_runs_per_frame
-- When we're closer, run fewer per frame, so things are less choppy.
-- This might have a side effect of being a little farther behind on average,
-- since we don't always run at top speed until the buffer is empty.
elseif buffer_len >= 10 then
times_to_run = 2
elseif buffer_len >= 1 then
times_to_run = 1
end
if self.play_to_end then
if string.len(self.input_buffer) < 4 then
self.play_to_end = nil
stop_sounds = true
end
end
for i=1,times_to_run do
self:update_cards()
self.input_state = string.sub(self.input_buffer,1,1)
self:prep_rollback()
self:controls()
self:prep_first_row()
self:PdP()
self.input_buffer = string.sub(self.input_buffer,2)
end
end
function Stack.enqueue_card(self, chain, x, y, n)
self.card_q:push({frame=1, chain=chain, x=x, y=y, n=n})
end
local d_col = {up=0, down=0, left=-1, right=1}
local d_row = {up=1, down=-1, left=0, right=0}
-- The engine routine.
function Stack.PdP(self)
local panels = self.panels
local width = self.width
local height = self.height
local prow = nil
local panel = nil
local swapped_this_frame = nil
self.game_stopwatch_running = true
if self.do_countdown then
self.game_stopwatch_running = nil
self.rise_lock = true
if not self.countdown_cursor_state then
self.countdown_CLOCK = self.CLOCK
self.starting_cur_row = self.cur_row
self.starting_cur_col = self.cur_col
self.cur_row = self.height
self.cur_col = self.width-1
self.countdown_cursor_state = "ready_falling"
self.countdown_cur_speed = 4 --one move every this many frames
self.cursor_lock = true
end
if self.countdown_CLOCK == 8 then
self.countdown_cursor_state = "moving_down"
self.countdown_timer = 180 --3 seconds at 60 fps
elseif self.countdown_cursor_state == "moving_down" then
--move down
if self.cur_row == self.starting_cur_row then
self.countdown_cursor_state = "moving_left"
elseif self.CLOCK % self.countdown_cur_speed == 0 then
self.cur_row = self.cur_row - 1
end
elseif self.countdown_cursor_state == "moving_left" then
--move left
if self.cur_col == self.starting_cur_col then
self.countdown_cursor_state = "ready"
self.cursor_lock = nil
elseif self.CLOCK % self.countdown_cur_speed == 0 then
self.cur_col = self.cur_col - 1
end
end
if self.countdown_timer then
if self.countdown_timer == 0 then
--we are done counting down
self.do_countdown = nil
self.countdown_timer = nil
self.starting_cur_row = nil
self.starting_cur_col = nil
self.countdown_CLOCK = nil
if self.which == 1 then
SFX_Go_Play=1
end
elseif self.countdown_timer and self.countdown_timer % 60 == 0 and self.which == 1 then
--play beep for timer dropping to next second in 3-2-1 countdown
if self.which == 1 then
SFX_Countdown_Play=1
end
end
if self.countdown_timer then
self.countdown_timer = self.countdown_timer - 1
end
end
if self.countdown_CLOCK then
self.countdown_CLOCK = self.countdown_CLOCK + 1
end
end
if self.pre_stop_time ~= 0 then
self.pre_stop_time = self.pre_stop_time - 1
elseif self.stop_time ~= 0 then
self.stop_time = self.stop_time - 1
end
self.panels_in_top_row = false
local top_row = self.height--self.displacement%16==0 and self.height or self.height-1
prow = panels[top_row]
for idx=1,width do
if prow[idx]:dangerous() then
self.panels_in_top_row = true
end
end
-- calculate which columns should bounce
local prev_danger = self.danger
self.danger = false
prow = panels[self.height-1]
for idx=1,width do
if prow[idx]:dangerous() then
self.danger = true
self.danger_col[idx] = true
else
self.danger_col[idx] = false
end
end
if self.danger and self.stop_time == 0 then
self.danger_timer = self.danger_timer - 1
if self.danger_timer<0 then
self.danger_timer=17
end
end
-- determine whether to play danger music
-- Changed this to play danger when something in top 3 rows
-- and to play casual when nothing in top 3 or 4 rows
if not self.danger_music then
-- currently playing casual
for _, prow in pairs({panels[self.height], panels[self.height-1], panels[self.height-2]}) do
for idx=1, width do
if prow[idx].color ~= 0 and prow[idx].state ~= "falling" or prow[idx]:dangerous() then
self.danger_music = true
break
end
end
end
if self.shake_time > 0 then self.danger_music = false end
else
--currently playing danger
local toggle_back = true
-- Normally, change back if nothing is in the top 3 rows
local changeback_rows = {panels[self.height], panels[self.height-1], panels[self.height-2]}
-- But optionally, wait until nothing is in the fourth row
if (config.danger_music_changeback_delay) then
table.insert(changeback_rows, panels[self.height-3])
end
for _, prow in pairs(changeback_rows) do
for idx=1, width do
if prow[idx].color ~= 0 then
toggle_back = false
break
end
end
end
self.danger_music = not toggle_back
end
if self.displacement == 0 and self.has_risen then
self.top_cur_row = self.height
self:new_row()
end
self.rise_lock = self.n_active_panels ~= 0 or
self.prev_active_panels ~= 0 or
self.shake_time ~= 0 or
self.do_countdown or
self.do_swap
-- Increase the speed if applicable
if self.speed_times then
local time = self.speed_times[self.speed_times.idx]
if self.CLOCK == time then
self.speed = min(self.speed + 1, 99)
if self.speed_times.idx ~= #self.speed_times then
self.speed_times.idx = self.speed_times.idx + 1
else
self.speed_times[self.speed_times.idx] = time + self.speed_times.delta
end
end
elseif self.panels_to_speedup <= 0 then
self.speed = self.speed + 1
self.panels_to_speedup = self.panels_to_speedup +
panels_to_next_speed[self.speed]
self.FRAMECOUNT_RISE = speed_to_rise_time[self.speed]
end
-- Phase 0 //////////////////////////////////////////////////////////////
-- Stack automatic rising
if self.speed ~= 0 and not self.manual_raise and self.stop_time == 0
and not self.rise_lock and self.mode ~= "puzzle" then
if self.panels_in_top_row then
self.health = self.health - 1
if self.health < 1 and self.shake_time < 1 then
self.game_over = true
end
else
self.rise_timer = self.rise_timer - 1
if self.rise_timer <= 0 then -- try to rise
self.displacement = self.displacement - 1
if self.displacement == 0 then
self.prevent_manual_raise = false
self.top_cur_row = self.height
self:new_row()
end
self.rise_timer = self.rise_timer + self.FRAMECOUNT_RISE
end
end
end
if not self.panels_in_top_row then
self.health = self.max_health
end
if self.displacement % 16 ~= 0 then
self.top_cur_row = self.height - 1
end
-- Begin the swap we input last frame.
if self.do_swap then
self:swap()
swapped_this_frame = true
self.do_swap = nil
end
-- Look for matches.
self:check_matches()
-- Clean up the value we're using to match newly hovering panels
-- This is pretty dirty :(
for row=1,#panels do
for col=1,width do
panels[row][col].match_anyway = nil
end
end
-- Phase 2. /////////////////////////////////////////////////////////////
-- Timer-expiring actions + falling
local propogate_fall = {false,false,false,false,false,false}
local skip_col = 0
local fallen_garbage = 0
local shake_time = 0
for row=1,#panels do
for col=1,width do
local cntinue = false
if skip_col > 0 then
skip_col = skip_col - 1
cntinue=true
end
panel = panels[row][col]
if cntinue then
elseif panel.garbage then
if panel.state == "matched" then
panel.timer = panel.timer - 1
if panel.timer == panel.pop_time then
SFX_Garbage_Pop_Play = panel.pop_index
end
if panel.timer == 0 then
if panel.y_offset == -1 then
local color, chaining = panel.color, panel.chaining
panel:clear()
panel.color, panel.chaining = color, chaining
self:set_hoverers(row,col,self.FRAMECOUNT_GPHOVER,true,true)
panel.fell_from_garbage = 12
else
panel.state = "normal"
end
end
-- try to fall
elseif (panel.state=="normal" or panel.state=="falling") then
if panel.x_offset==0 then
local prow = panels[row-1]
local supported = false
if panel.y_offset == 0 then
for i=col,col+panel.width-1 do
supported = supported or prow[i]:support_garbage()
end
else
supported = not propogate_fall[col]
end
if supported then
for x=col,col-1+panel.width do
panels[row][x].state = "normal"
propogate_fall[x] = false
end
else
skip_col = panel.width-1
for x=col,col-1+panel.width do
panels[row-1][x]:clear()
propogate_fall[x] = true
panels[row][x].state = "falling"
panels[row-1][x], panels[row][x] =
panels[row][x], panels[row-1][x]
end
end
end
if panel.shake_time and panel.state == "normal" then
if row <= self.height then
if panel.height > 3 then
SFX_GarbageThud_Play = 3
else SFX_GarbageThud_Play = panel.height
end
shake_time = max(shake_time, panel.shake_time, self.peak_shake_time or 0)
--a smaller garbage block landing should renew the largest of the previous blocks' shake times since our shake time was last zero.
self.peak_shake_time = max(shake_time, self.peak_shake_time or 0)
panel.shake_time = nil
end
end
end
cntinue = true
end
if propogate_fall[col] and not cntinue then
if panel:block_garbage_fall() then
propogate_fall[col] = false
else
panel.state = "falling"
panel.timer = 0
end
end
if cntinue then
elseif panel.state == "falling" then
-- if it's on the bottom row, it should surely land
if row == 1 then
panel.state = "landing"
panel.timer = 12
SFX_Land_Play=1;
-- if there's a panel below, this panel's gonna land
-- unless the panel below is falling.
elseif panels[row-1][col].color ~= 0 and
panels[row-1][col].state ~= "falling" then
-- if it lands on a hovering panel, it inherits
-- that panel's hover time.
if panels[row-1][col].state == "hovering" then
panel.state = "normal"
self:set_hoverers(row,col,panels[row-1][col].timer,false,false)
else
panel.state = "landing"
panel.timer = 12
end
SFX_Land_Play=1;
else
panels[row-1][col], panels[row][col] =
panels[row][col], panels[row-1][col]
panels[row][col]:clear()
end
elseif panel:has_flags() and panel.timer~=0 then
panel.timer = panel.timer - 1
if panel.timer == 0 then
if panel.state=="swapping" then
-- a swap has completed here.
panel.state = "normal"
panel.dont_swap = nil
local from_left = panel.is_swapping_from_left
panel.is_swapping_from_left = nil
-- Now there are a few cases where some hovering must
-- be done.
if panel.color ~= 0 then
if row~=1 then
if panels[row-1][col].color == 0 then
self:set_hoverers(row,col,
self.FRAMECOUNT_HOVER,false,true,false)
-- if there is no panel beneath this panel
-- it will begin to hover.
-- CRAZY BUG EMULATION:
-- the space it was swapping from hovers too
if from_left then
if panels[row][col-1].state == "falling" then
self:set_hoverers(row,col-1,
self.FRAMECOUNT_HOVER,false,true)
end
else
if panels[row][col+1].state == "falling" then
self:set_hoverers(row,col+1,
self.FRAMECOUNT_HOVER+1,false,false)
end