-
Notifications
You must be signed in to change notification settings - Fork 10
/
engine.lua
1560 lines (1448 loc) · 49.4 KB
/
engine.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
-- 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 = 52
local clone_pool = {}
Stack = class(function(s, which, mode, speed, difficulty)
s.character = uniformly(characters)
s.max_health = 1
s.mode = mode or "endless"
if mode ~= "puzzle" then
s.do_first_row = true
end
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_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 = Queue()
-- 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 = {}
s.pos_x = 4 -- Position of the play area on the screen
s.pos_y = 4
s.score_x = 315
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.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_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 = false -- boolean, panels in the top row (danger)
s.danger_music = 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.subpixels = 0x1000
s.subpixel_step = speed_to_subpixels[s.speed]
-- 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.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.game_over = false
s.card_q = Queue()
s.which = which or 1 -- Pk.which == k
s.shake_time = 0
s.prev_states = {}
s.raise_state = 0
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.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.displacement = self.displacement
other.subpixels = self.subpixels
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.raise_state = self.raise_state
other.manual_raise = self.manual_raise
other.manual_raise_yet = self.manual_raise_yet
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.card_q = deepcpy(self.card_q)
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.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
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
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
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.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
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
--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)
local times_to_run = min(string.len(self.input_buffer),
self.max_runs_per_frame)
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}
local raises = {
[0] = function(self)
self.raise_state = 1
end,
function(self)
if self.stop_time > 0 or self.pre_stop_time > 0 then
return
end
self.subpixels = self.subpixels - self.subpixel_step
while self.subpixels < -0x8000 do
self.subpixels = self.subpixels + 0x10000
end
if self.raise then
self.manual_raise = true
self.raise_state = 2
end
if self.subpixels < 0 then
self.raise_state = 2
end
end,
function(self)
if self.displacement == 0 then
self.raise_state = 3
self.subpixels = self.subpixels + 0x1000
elseif not self.manual_raise then
self.subpixels = self.subpixels + 0x1000
self.raise_state = 1
end
self.displacement = self.displacement - 1
end,
function(self)
self.manual_raise = false
self.top_cur_row = self.height
self:new_row()
self.raise_state = 0
while self.subpixels >= 0x8000 do
self.subpixels = self.subpixels - 0x10000
end
end,
}
-- 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
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
if self.raise then
self.stop_time = 0
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
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
self.danger_music = false
prow = panels[self.height-2]
for idx=1,width do
if prow[idx]:dangerous() then
self.danger_music = true
end
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
-- 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.subpixel_step = speed_to_subpixels[self.speed]
end
-- Phase 0 //////////////////////////////////////////////////////////////
-- Stack automatic rising
if self.speed ~= 0 and not self.rise_lock and self.mode ~= "puzzle" then
if self.panels_in_top_row then
if self.stop_time == 0 and self.pre_stop_time == 0 then
self.health = self.health - 1
if self.health == 0 and self.shake_time == 0 then
self.game_over = true
end
end
else
raises[self.raise_state](self)
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
-- CURSOR MOVEMENT
self.move_sound = false
local can_swap = true
if self.cur_dir and (self.cur_timer == 0 or
self.cur_timer == self.cur_wait_time) then
if self.cur_timer == 0 then
can_swap = false
end
self.cur_row = bound(1, self.cur_row + d_row[self.cur_dir],
self.top_cur_row)
self.cur_col = bound(1, self.cur_col + d_col[self.cur_dir],
width - 1)
else
self.cur_row = bound(1, self.cur_row, self.top_cur_row)
end
if self.cur_timer ~= self.cur_wait_time then
self.cur_timer = self.cur_timer + 1
--if(self.move_sound and self.cur_timer == 0) then SFX_P1Cursor_Play=1 end
--TODO:SFX
end
-- SWAPPING
can_swap = can_swap and self:can_swap()
if can_swap and (self.swap_1 or self.swap_2) then
self:swap()
end
self.swap_1 = false
self.swap_2 = false
-- 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 == 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,5,true,true)
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
shake_time = max(shake_time, panel.shake_time)
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;
--SFX LAWL
-- 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;
--SFX LEWL
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
end
elseif panels[row-1][col].state
== "hovering" then
-- swap may have landed on a hover
self:set_hoverers(row,col,
self.FRAMECOUNT_HOVER,false,true,
panels[row-1][col].match_anyway, "inherited")
end
end
else
-- an empty space finished swapping...
-- panels above it hover
self:set_hoverers(row+1,col,
self.FRAMECOUNT_HOVER+1,false,false,false,"empty")
end
elseif panel.state == "hovering" then
if panels[row-1][col].state == "hovering" then
panel.timer = panels[row-1][col].timer
-- This panel is no longer hovering.
-- it will now fall without sitting around
-- for any longer!
elseif panels[row-1][col].color ~= 0 then
panel.state = "landing"
panel.timer = 12
else
panel.state = "falling"
panels[row][col], panels[row-1][col] =
panels[row-1][col], panels[row][col]
panel.timer = 0
-- Not sure if needed:
panels[row][col]:clear_flags()
end
elseif panel.state == "landing" then
panel.state = "normal"
elseif panel.state == "matched" then
-- This panel's match just finished the whole
-- flashing and looking distressed thing.
-- It is given a pop time based on its place
-- in the match.
panel.state = "popping"
panel.timer = panel.combo_index*self.FRAMECOUNT_POP
elseif panel.state == "popping" then
self.score = self.score + 10;
-- self.score_render=1;
-- TODO: What is self.score_render?
-- this panel just popped
-- Now it's invisible, but sits and waits
-- for the last panel in the combo to pop
-- before actually being removed.
-- If it is the last panel to pop,
-- it should be removed immediately!
if panel.combo_size == panel.combo_index then
panel.color=0;
if(panel.chaining) then
self.n_chain_panels = self.n_chain_panels - 1
end
panel:clear_flags()
self:set_hoverers(row+1,col,
self.FRAMECOUNT_HOVER+1,true,false,true, "combo")
else
panel.state = "popped"
panel.timer = (panel.combo_size-panel.combo_index)
* self.FRAMECOUNT_POP
end
--something = panel.chain_index
--if(something == 0) then something=1 end
-- SFX_Pop_Play[0] = something;
-- SFX_Pop_Play[1] = whatever;
-- TODO: wtf are these
elseif panel.state == "popped" then
-- It's time for this panel
-- to be gone forever :'(
if self.panels_to_speedup then
self.panels_to_speedup = self.panels_to_speedup - 1
end
if panel.chaining then
self.n_chain_panels = self.n_chain_panels - 1
end
panel.color = 0
panel:clear_flags()
-- Any panels sitting on top of it
-- hover and are flagged as CHAINING
self:set_hoverers(row+1,col,self.FRAMECOUNT_HOVER+1,true,false,true, "popped")
else
-- what the heck.
-- if a timer runs out and the routine can't
-- figure out what flag it is, tell brandon.
-- No seriously, email him or something.
error("something terrible happened")
end
-- the timer-expiring action has completed
end
end
end
end
self.shake_time = self.shake_time - 1
self.shake_time = max(self.shake_time, shake_time)
-- Phase 3. /////////////////////////////////////////////////////////////
-- Actions performed according to player input
-- SWAPPING
-- MANUAL STACK RAISING
--[[if self.manual_raise and self.mode ~= "puzzle" then
if not self.rise_lock then
if self.panels_in_top_row then
self.game_over = true
end
self.has_risen = true
self.displacement = self.displacement - 1
if self.displacement == 1 then
self.manual_raise = false
self.rise_timer = 1
--if not self.prevent_manual_raise then
-- self.score = self.score + 1
--end
end
self.manual_raise_yet = true --ehhhh
self.stop_time = 0
elseif not self.manual_raise_yet then
self.manual_raise = false
end
-- if the stack is rise locked when you press the raise button,
-- the raising is cancelled
end--]]
-- if at the end of the routine there are no chain panels, the chain ends.
if self.chain_counter ~= 0 and self.n_chain_panels == 0 then
self:set_chain_garbage(self.chain_counter)
self.chain_counter=0
end
if(self.score>99999) then
self.score=99999
-- lol owned
end
self.prev_active_panels = self.n_active_panels
self.n_active_panels = 0
for row=1,self.height do
for col=1,self.width do
local panel = panels[row][col]
if (panel.garbage and panel.state ~= "normal") or
(panel.color ~= 0 and (panel:exclude_hover() or panel.state == "swapping") and not panel.garbage) or
panel.state == "swapping" then
self.n_active_panels = self.n_active_panels + 1
end
end
end
local to_send = self.garbage_to_send[self.CLOCK]
if to_send then
self.garbage_to_send[self.CLOCK] = nil
-- if there's no chain, we can send it
if self.chain_counter == 0 then
if #to_send > 0 then
table.sort(to_send, function(a,b)
if a[4] or b[4] then
return a[4] and not b[4]
elseif a[3] or b[3] then
return b[3] and not a[3]
else
return a[1] < b[1]
end
end)
self:really_send(to_send)
end
elseif self.garbage_to_send.chain then
local waiting_for_chain = self.garbage_to_send.chain
for i=1,#to_send do
waiting_for_chain[#waiting_for_chain+1] = to_send[i]
end
else
self.garbage_to_send.chain = to_send
end
end
self:remove_extra_rows()
local garbage = self.later_garbage[self.CLOCK]
if garbage then
for i=1,#garbage do
self.garbage_q:push(garbage[i])
end
end
self.later_garbage[self.CLOCK-409] = nil
local drop_it = self.n_active_panels == 0 and
self.prev_active_panels == 0 and
not self.panels_in_top_row
if drop_it and self.garbage_q:len() > 0 then
self:drop_garbage(unpack(self.garbage_q:pop()))
end
self.CLOCK = self.CLOCK + 1
end
function Stack.can_swap(self)
local panels = self.panels
local row = self.cur_row
local col = self.cur_col
-- in order for a swap to occur, one of the two panels in
-- the cursor must not be a non-panel.
local can_swap = (panels[row][col].color ~= 0 or
panels[row][col+1].color ~= 0) and
-- also, both spaces must be swappable.
(not panels[row][col]:exclude_swap()) and
(not panels[row][col+1]:exclude_swap()) and
-- also, neither space above us can be hovering.
(self.cur_row == #panels or (panels[row+1][col].state ~=
"hovering" and panels[row+1][col+1].state ~=
"hovering"))
-- If you have two pieces stacked vertically, you can't move
-- both of them to the right or left by swapping with empty space.
-- TODO: This might be wrong if something lands on a swapping panel?
if panels[row][col].color == 0 or panels[row][col+1].color == 0 then
can_swap = can_swap and not (self.cur_row ~= self.height and
(panels[row+1][col].state == "swapping" and
panels[row+1][col+1].state == "swapping") and
(panels[row+1][col].color == 0 or
panels[row+1][col+1].color == 0) and
(panels[row+1][col].color ~= 0 or
panels[row+1][col+1].color ~= 0))
can_swap = can_swap and not (self.cur_row ~= 1 and
(panels[row-1][col].state == "swapping" and
panels[row-1][col+1].state == "swapping") and
(panels[row-1][col].color == 0 or
panels[row-1][col+1].color == 0) and
(panels[row-1][col].color ~= 0 or
panels[row-1][col+1].color ~= 0))
end
can_swap = can_swap and (self.puzzle_moves == nil or self.puzzle_moves > 0)
return can_swap
end
function Stack.swap(self)
local panels = self.panels
local row = self.cur_row
local col = self.cur_col
if self.puzzle_moves then
self.puzzle_moves = self.puzzle_moves - 1
end
panels[row][col], panels[row][col+1] =
panels[row][col+1], panels[row][col]
local tmp_chaining = panels[row][col].chaining
panels[row][col]:clear_flags()
panels[row][col].state = "swapping"
panels[row][col].chaining = tmp_chaining
tmp_chaining = panels[row][col+1].chaining
panels[row][col+1]:clear_flags()
panels[row][col+1].state = "swapping"
panels[row][col+1].is_swapping_from_left = true
panels[row][col+1].chaining = tmp_chaining