-
Notifications
You must be signed in to change notification settings - Fork 3
/
emthree.lua
1213 lines (1061 loc) · 32 KB
/
emthree.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
local M = {}
local async = require "emthree.internal.async"
local utils = require "emthree.internal.utils"
M.REMOVE = hash("emthree_remove")
M.CHANGE = hash("emthree_change")
M.SELECT = hash("emthree_select")
M.RESET = hash("emthree_reset")
M.COLLAPSE_DOWN = hash("emthree_slide_down")
M.COLLAPSE_UP = hash("emthree_slide_up")
M.COLLAPSE_LEFT = hash("emthree_slide_left")
M.COLLAPSE_RIGHT = hash("emthree_slide_right")
local DIRECTIONS = {
[M.COLLAPSE_DOWN] = vmath.vector3(0, -1, 0),
[M.COLLAPSE_UP] = vmath.vector3(0, 1, 0),
[M.COLLAPSE_LEFT] = vmath.vector3(-1, 0, 0),
[M.COLLAPSE_RIGHT] = vmath.vector3(1, 0, 0),
}
local ORTHOGONAL = {
[M.COLLAPSE_DOWN] = vmath.vector3(1, 0, 0),
[M.COLLAPSE_UP] = vmath.vector3(1, 0, 0),
[M.COLLAPSE_LEFT] = vmath.vector3(0, 1, 0),
[M.COLLAPSE_RIGHT] = vmath.vector3(0, 1, 0),
}
local function spawner_filter(board, x, y)
local block = board.slots[x][y]
return block and block.spawner
end
local function create_color_filter(color)
return function(board, x, y)
local block = board.slots[x][y]
return (not color and block) or (color and block and block.color == color)
end
end
local function delay(seconds, fn, a)
timer.delay(seconds, false, function()
fn(a)
end)
end
--
-- Returns a list of neighbor slots of the same color as
-- the one on x, y. Horizontally.
--
local function horisontal_neighbors(board, x, y)
assert(board, "You must provide a board")
local neighbors = {}
if not board.slots[x][y] or not board.slots[x][y].color then
return neighbors
end
local color = board.slots[x][y].color
--
-- Search from slot left to the edge
--
for i = x - 1, 0, -1 do
local block = board.slots[i][y]
if block and block.color == color then
table.insert(neighbors, block)
else
--
-- Break the search as soon as we hit something of a different color
--
break
end
end
--
-- Search from slot right to the edge
--
for i = x + 1, board.width - 1 do
local block = board.slots[i][y]
if block and block.color == color then
table.insert(neighbors, block)
else
--
-- Break the search as soon as we hit something of a different color
--
break
end
end
return neighbors
end
--
-- Returns a list of neighbor slots of the same color as
-- the one on x, y. Vertically.
--
local function vertical_neighbors(board, x, y)
assert(board, "You must provide a board")
local neighbors = {}
if not board.slots[x][y] or not board.slots[x][y].color then
return neighbors
end
local color = board.slots[x][y].color
--
-- Search from slot down to the edge
--
for i = y - 1, 0, -1 do
local slot = board.slots[x][i]
if slot and slot.color == color then
table.insert(neighbors, slot)
else
--
-- Break the search as soon as we hit something of a different type
--
break
end
end
--
-- Search from slot up to the edge
--
for i = y + 1, board.height - 1 do
local slot = board.slots[x][i]
if slot and slot.color == color then
table.insert(neighbors, slot)
else
--
-- Break the search as soon as we hit something of a different color
--
break
end
end
return neighbors
end
--
-- Scans the board for any matching neighbors (row or column)
-- and count them.
--
local function find_matching_neighbors(board)
assert(board, "You must provide a board")
for block in M.iterate_blocks(board) do
--
-- Count the same type line of neighbors horisontally and
-- vertically. Note that any number of subsequent neighbors
-- are counted, so if a blue block has 3 blue block immediately
-- to the right and 3 to the left if has 6 horisontal neighbors.
--
local hn = horisontal_neighbors(board, block.x, block.y)
local vn = vertical_neighbors(board, block.x, block.y)
block.horisontal_neighbors = hn
block.vertical_neighbors = vn
end
end
--
-- Remove blocks that are part of matches, then call callback
--
local function remove_matching_neighbors(board, callback)
assert(board, "You must provide a board")
assert(callback, "You must provide a callback")
local duration = 0
local did_remove = false
-- handle t, l and cross shaped block formations in a first pass
for block in M.iterate_blocks(board) do
if #block.horisontal_neighbors >= 2 and #block.vertical_neighbors >= 2 then
did_remove = true
board.on_match(board, block, block.horisontal_neighbors, block.vertical_neighbors)
end
end
-- handle horisontal and vertical
for block in M.iterate_blocks(board) do
if #block.horisontal_neighbors >= 2 or #block.vertical_neighbors >= 2 then
did_remove = true
board.on_match(board, block, block.horisontal_neighbors, block.vertical_neighbors)
end
end
if did_remove then
delay(duration, callback)
else
callback()
end
end
--
-- Apply shift logic to all slots on the board, then
-- call the callback.
--
local function collapse(board, callback)
assert(board, "You must provide a board")
assert(callback, "You must provide a callback")
local duration = board.config.collapse_duration
local direction = DIRECTIONS[board.config.collapse_direction]
local collapsed = false
local blocks = board.slots
local empty_x = nil
local empty_y = nil
local block_size = board.block_size
local function check_collapse(x, y)
local block = blocks[x][y]
if block and block.blocker then
empty_x = nil
empty_y = nil
elseif block and block.spawner then
-- do nothing
elseif block then
-- collapse block if there's an empty block below
if empty_x and empty_y then
--
-- Move to empty slot
--
blocks[empty_x][empty_y] = block
blocks[x][y] = nil
block.x = empty_x
block.y = empty_y
--
-- Calc new position and animate
---
local to = vmath.vector3((block_size / 2) + (block_size * empty_x), (block_size / 2) + (block_size * empty_y), 0)
go.animate(block.id, "position", go.PLAYBACK_ONCE_FORWARD, to, board.config.collapse_easing, duration)
collapsed = true
empty_x = empty_x - direction.x
empty_y = empty_y - direction.y
end
-- first empty slot
elseif not empty_x then
empty_x = x
empty_y = y
end
end
if board.config.collapse_direction == M.COLLAPSE_DOWN then
for x = 0,board.width - 1 do
empty_x = nil
empty_y = nil
for y = 0,board.height - 1 do
check_collapse(x, y)
end
end
elseif board.config.collapse_direction == M.COLLAPSE_UP then
for x = 0,board.width - 1 do
empty_x = nil
empty_y = nil
for y = board.height-1,0,-1 do
check_collapse(x, y)
end
end
elseif board.config.collapse_direction == M.COLLAPSE_LEFT then
for y = 0,board.height - 1 do
empty_x = nil
empty_y = nil
for x = 0, board.width-1 do
check_collapse(x, y)
end
end
elseif board.config.collapse_direction == M.COLLAPSE_RIGHT then
for y = 0,board.height - 1 do
empty_x = nil
empty_y = nil
for x = board.width-1,0,-1 do
check_collapse(x, y)
end
end
end
if collapsed then
delay(duration, callback, collapsed)
else
callback(collapsed)
end
end
--
-- Apply slide logic to all slots on the board, then
-- call the callback.
--
local function slide(board, callback)
assert(board, "You must provide a board")
assert(callback, "You must provide a callback")
local duration = board.config.slide_duration
local direction = DIRECTIONS[board.config.collapse_direction]
local orthogonal = ORTHOGONAL[board.config.collapse_direction]
local did_slide = false
local blocks = board.slots
local block_size = board.block_size
local empty_right = 0
local empty_left = 0
local function check_slide(x, y)
local block = blocks[x][y]
if block and block.blocker then
empty_right = 0
empty_left = 0
elseif block and not block.spawner and not block.blocker then
-- slide block if there's empty slots left and/or right
if empty_right >= 1 or empty_left >= 1 then
-- left or right?
if empty_right >= 1 and empty_left >= 1 then
if (x % 2 == 0) then
empty_right = 0
else
empty_left = 0
end
end
-- where to slide?
local tox = x
local toy = y + direction.y
if empty_right > 0 then
tox = tox + orthogonal.x
empty_right = 0
else
tox = tox - orthogonal.x
empty_left = 0
end
blocks[tox][toy] = block
blocks[x][y] = nil
block.x = tox
block.y = toy
-- slide diagonal
local to = vmath.vector3((block_size / 2) + (block_size * tox), (block_size / 2) + (block_size * toy), 0)
go.animate(block.id, "position", go.PLAYBACK_ONCE_FORWARD, to, board.config.slide_easing, duration)
did_slide = true
else
local rx = x + orthogonal.x
local ry = y + orthogonal.y
if M.is_empty(board, rx, ry) then
empty_right = empty_right + 1
elseif M.is_blocker(board, rx, ry) then
empty_right = 0
end
local lx = x - orthogonal.x
local ly = y - orthogonal.y
if M.is_empty(board, lx, ly) then
empty_left = empty_left + 1
elseif M.is_blocker(board, lx, ly) then
empty_left = 0
end
end
end
end
if board.config.collapse_direction == M.COLLAPSE_DOWN then
for x = 0,board.width - 1 do
empty_right = 0
empty_left = 0
for y = 0,board.height - 1 do
check_slide(x, y)
if did_slide then
break
end
end
end
elseif board.config.collapse_direction == M.COLLAPSE_UP then
for x = 0,board.width - 1 do
empty_right = 0
empty_left = 0
for y = board.height-1,0,-1 do
check_slide(x, y)
if did_slide then
break
end
end
end
elseif board.config.collapse_direction == M.COLLAPSE_LEFT then
for y = 0,board.height - 1 do
empty_right = 0
empty_left = 0
for x = 0, board.width-1 do
check_slide(x, y)
if did_slide then
break
end
end
end
elseif board.config.collapse_direction == M.COLLAPSE_RIGHT then
for y = 0,board.height - 1 do
empty_right = 0
empty_left = 0
for x = board.width-1,0,-1 do
check_slide(x, y)
if did_slide then
break
end
end
end
end
if did_slide then
delay(duration, callback, did_slide)
else
callback(did_slide)
end
end
--
-- Construct a new random board. It's a 2D table with origo in the
-- bottom left corner:
--
-- ...
-- (0,2) (1,2) (2,2)
-- (0,1) (1,1) (2,1)
-- (0,0) (1,0) (2,0) ...
--
-- Each slot stores the id of the game object that sits there, the x and y
-- position and the type, for easy searching. Storing the x and y position is
-- redundant but useful if we use the slots out of context, which we do at times.
-- @param width
-- @param height
-- @param block_size Size of the blocks in pixels
-- @param config Additional (and optional) board configuration values
-- @return The created bord. Pass it when calling the other functions
function M.create_board(width, height, block_size, config)
assert(width, "You must provide a board width")
assert(height, "You must provide a board height")
assert(block_size, "You must provide a block size")
config = config or {}
config.swap_duration = config.swap_duration or 0.2
config.remove_duration = config.remove_duration or 0
config.slide_duration = config.slide_duration or 0.2
config.collapse_duration = config.collapse_duration or 0.2
config.collapse_direction = config.collapse_direction or M.COLLAPSE_DOWN
config.collapse_easing = config.collapse_easing or go.EASING_OUTBOUNCE
config.slide_easing = config.slide_easing or go.EASING_LINEAR
local board = {
width = width,
height = height,
block_size = block_size,
slots = {},
config = config,
}
for x = 0, width - 1 do
board.slots[x] = {}
end
M.on_match(board, function(board, block, horisontal_neighbors, vertical_neighbors)
if #horisontal_neighbors >= 2 then
M.remove_block(board, block)
M.remove_blocks(board, horisontal_neighbors)
end
if #vertical_neighbors >= 2 then
M.remove_block(board, block)
M.remove_blocks(board, vertical_neighbors)
end
end)
M.on_block_removed(board, function(board, block)
-- do nothing
end)
M.on_stabilized(board, function(board)
-- do nothing, board is stable
end)
M.on_swap(board, function(board, slot1, slot2)
return false
end)
M.on_create_block(board, function(board, x, y, type, color)
error("You must call emthree.on_create_block() and provide a function to create blocks on the board")
end)
M.on_create_blocker(board, function(board, x, y, type)
return nil, type
end)
M.on_create_spawner(board, function(board, x, y, type)
return nil, type
end)
M.on_no_possible_switches(board, function(board)
-- do nothing
end)
return board
end
--- Fill the board with blocks
-- @param board The board to fill
function M.fill_board(board)
for x = 0, board.width - 1 do
for y = 0, board.height - 1 do
if not board.slots[x][y] then
M.create_block(board, x, y)
end
end
end
end
local function swap(board, slot1, slot2, callback)
local duration = board.config.swap_duration
local pos1 = go.get_position(slot1.id)
local pos2 = go.get_position(slot2.id)
go.animate(slot1.id, "position", go.PLAYBACK_ONCE_FORWARD, pos2, go.EASING_INOUTSINE, duration)
go.animate(slot2.id, "position", go.PLAYBACK_ONCE_FORWARD, pos1, go.EASING_INOUTSINE, duration)
--
-- Switch the board structure data content of the two slots
-- In Lua we can write a, b = b, a to swap two values
--
local block1 = board.slots[slot1.x][slot1.y]
local block2 = board.slots[slot2.x][slot2.y]
board.slots[block1.x][block1.y], board.slots[block2.x][block2.y] = board.slots[block2.x][block2.y], board.slots[block1.x][block1.y]
block1.x, block2.x = block2.x, block1.x
block1.y, block2.y = block2.y, block1.y
delay(duration, callback)
end
---
-- Swap the contents of two board slots
-- @param board
-- @param slot1
-- @param slot2
local function swap_slots(board, slot1, slot2)
assert(board, "You must provide a board")
assert(slot1, "You must provide a first slot")
assert(slot2, "You must provide a second slot")
assert(coroutine.running())
async(function(done) swap(board, slot1, slot2, done) end)
if not board.on_swap(board, slot1, slot2) then
local hn1 = horisontal_neighbors(board, slot1.x, slot1.y)
local vn1 = vertical_neighbors(board, slot1.x, slot1.y)
local hn2 = horisontal_neighbors(board, slot2.x, slot2.y)
local vn2 = vertical_neighbors(board, slot2.x, slot2.y)
-- not a valid swap, did not generate a match - swap back again
if #hn1 < 2 and #hn2 < 2 and #vn1 < 2 and #vn2 < 2 then
async(function(done) swap(board, slot1, slot2, done) end)
end
end
M.stabilize(board)
end
--
-- Return an iterator function for use in generic for loops
-- to iterate all blocks on the board
-- @param board
-- @param filter_fn Optional filter function (args: board, x, y)
-- @return Function iterator
function M.iterate_blocks(board, filter_fn)
assert(board, "You must provide a board")
local x = 0
local y = -1
return function()
repeat
y = y + 1
if x == board.width - 1 and y == board.height then
return nil
end
if y == board.height then
y = 0
x = x + 1
end
local block = board.slots[x][y]
until (filter_fn and filter_fn(board, x, y) and block) or (not filter_fn and block)
return board.slots[x][y]
end
end
--- Check if a slot is empty
-- @param board
-- @param x
-- @param y
-- @return true if the slot is empty
function M.is_empty(board, x, y)
assert(board, "You must provide a board")
assert(x and y, "You must provide a position")
if M.on_board(board, x, y) then
return board.slots[x][y] == nil
end
return false
end
--- Check if the content of a slot is a block
-- @param board
-- @param x
-- @param y
-- @return true if there is a block in the slot
function M.is_block(board, x, y)
assert(board, "You must provide a board")
assert(x and y, "You must provide a position")
if M.on_board(board, x, y) then
return board.slots[x][y] and board.slots[x][y].block
end
return false
end
--- Check if the content of a slot is a blocker
-- @param board
-- @param x
-- @param y
-- @return true if there is a blocker in the slot
function M.is_blocker(board, x, y)
assert(board, "You must provide a board")
assert(x and y, "You must provide a position")
if M.on_board(board, x, y) then
return board.slots[x][y] and board.slots[x][y].blocker
end
return false
end
--- Check if the content of a slot is a spawner
-- @param board
-- @param x
-- @param y
-- @return true if there is a spawner in the slot
function M.is_spawner(board, x, y)
assert(board, "You must provide a board")
assert(x and y, "You must provide a position")
if M.on_board(board, x, y) then
return board.slots[x][y] and board.slots[x][y].spawner
end
return false
end
--- Get a block on the board
-- @param board
-- @param x
-- @param y
-- @return The block at the given position or nil if out of bounds
function M.get_block(board, x, y)
assert(board, "You must provide a board")
assert(x and y, "You must provide a position")
if M.on_board(board, x, y) then
return board.slots[x][y]
end
return nil
end
---
-- Get all blocks of a specific color
-- @param board
-- @param color Color to search for or nil to get all blocks
-- @return All blocks of the specified color
function M.get_blocks(board, color)
assert(board, "You must provide a board")
local blocks = {}
for block in M.iterate_blocks(board, create_color_filter(color)) do
table.insert(blocks, block)
end
return blocks
end
--
-- Remove a single block from the board
-- @param board
-- @param block The block to remove
-- @param no_trigger True if the on_block_removed function
-- should NOT be called (defaults to true)
function M.remove_block(board, block, no_trigger)
assert(board, "You must provide a board")
if not block then
return
end
if not board.slots[block.x][block.y] then
-- the block has already been removed
-- this can happen when we remove a match and a line blast or
-- other special block effect takes place at the same time
return
end
msg.post(block.id, M.REMOVE)
block.removed = true
--
-- Empty slots are set to nil so we can find them
--
board.slots[block.x][block.y] = nil
if not no_trigger then
board.on_block_removed(board, block)
end
end
--
-- Remove a list of blocks from the board
-- @param board
-- @param blocks
function M.remove_blocks(board, blocks)
assert(board, "You must provide a board")
assert(blocks, "You must provide a list of blocks")
for _,block in pairs(blocks) do
M.remove_block(board, block)
end
end
--
-- Change type and color of an existing block
-- Use this when converting blocks into other types due
-- to a match of some kind
-- Will clear list of neighbors
function M.change_block(block, type, color)
assert(block, "You must provide a block")
assert(type or color, "You must provide type and/or color")
block.color = color or block.color
block.type = type or block.type
block.vertical_neighbors = {}
block.horisontal_neighbors = {}
msg.post(block.id, M.CHANGE, { color = block.color, type = block.type, position = go.get_position(block.id) })
end
--- Create a new block on the board. This will call the function that was provided
-- by on_create_block()
-- @param board
-- @param x
-- @param y
-- @param type
-- @param color
-- @return The created block
function M.create_block(board, x, y, type, color)
assert(board, "You must provide a board")
assert(x and y, "You must provide a position")
assert(not board.slots[x][y], "The position is not empty")
local sx, sy = M.slot_to_screen(board, x, y)
local id, color, type = board.on_create_block(vmath.vector3(sx, sy, 0), type, color)
board.slots[x][y] = { id = id, x = x, y = y, color = color, type = type, block = true }
return board.slots[x][y]
end
--- Create a new blocker on the board. This will call the function that was provided
-- by on_create_blocker()
-- @param board
-- @param x
-- @param y
-- @param type
-- @return The created blocker
function M.create_blocker(board, x, y, type)
assert(board, "You must provide a board")
assert(x and y, "You must provide a position")
assert(not board.slots[x][y], "The position is not empty")
local sx, sy = M.slot_to_screen(board, x, y)
local id, type = board.on_create_blocker(vmath.vector3(sx, sy, 0), type)
board.slots[x][y] = { id = id, x = x, y = y, type = type, blocker = true }
return board.slots[x][y]
end
--- Create a new spawner on the board. This will call the function that was provided
-- by on_create_spawner()
-- @param board
-- @param x
-- @param y
-- @param type
-- @return The created spawner
function M.create_spawner(board, x, y, type)
assert(board, "You must provide a board")
assert(x and y, "You must provide a position")
assert(not board.slots[x][y], "The position is not empty")
local sx, sy = M.slot_to_screen(board, x, y)
local id, type = board.on_create_spawner(vmath.vector3(sx, sy, 0), type)
board.slots[x][y] = { id = id, x = x, y = y, type = type, spawner = true }
return board.slots[x][y]
end
--
-- Find and return any empty slots.
--
local function find_empty_slots(board)
assert(board, "You must provide a board")
local slots = {}
for x = 0, board.width - 1 do
for y = 0, board.height - 1 do
if not board.slots[x][y] then
--
-- The slot is nil/empty so we store this position in the
-- list of slots that we will return
--
table.insert(slots, { x = x, y = y })
end
end
end
return slots
end
--
-- Find and return any slots containing spawners
--
local function find_spawners(board)
assert(board, "You must provide a board")
local spawners = {}
for block in M.iterate_blocks(board, spawner_filter) do
table.insert(spawners, { x = block.x, y = block.y })
end
return spawners
end
local function trigger_spawners(board, callback)
assert(board, "You must provide a board")
local duration = board.config.collapse_duration
local triggered = false
for _,spawner in pairs(find_spawners(board)) do
local direction = DIRECTIONS[board.config.collapse_direction]
local x = spawner.x + direction.x
local y = spawner.y + direction.y
local distance = 0
while M.on_board(board, x, y) and not board.slots[x][y] do
distance = distance + 1
x = x + direction.x
y = y + direction.y
end
if distance > 0 then
triggered = true
local fromslot_x = spawner.x
local fromslot_y = spawner.y
local from_x, from_y = M.slot_to_screen(board, spawner.x, spawner.y)
local from = vmath.vector3(from_x, from_y, 0)
for i=1,distance do
local toslot_x = spawner.x + direction.x * i
local toslot_y = spawner.y + direction.y * i
local block = M.create_block(board, toslot_x, toslot_y)
local to_x, to_y = M.slot_to_screen(board, toslot_x, toslot_y)
local to = vmath.vector3(to_x, to_y, 0)
go.set_position(from, block.id)
go.animate(block.id, "position", go.PLAYBACK_ONCE_FORWARD, to, board.config.collapse_easing, duration)
end
end
end
if triggered then
delay(duration, callback, triggered)
else
callback(triggered)
end
end
-- simple block swap without animations or callbacks
local function quick_swap(board, block1, block2)
board.slots[block1.x][block1.y], board.slots[block2.x][block2.y] = board.slots[block2.x][block2.y], board.slots[block1.x][block1.y]
block1.x, block2.x = block2.x, block1.x
block1.y, block2.y = block2.y, block1.y
end
-- swap two blocks amd check if there is a match
local function swap_and_check_match(board, block1, block2)
if not block1 or not block2 then return false end
if block1.blocker or block2.blocker then return false end
if block1.spawner or block2.spawner then return false end
quick_swap(board, block1, block2)
local hn1 = horisontal_neighbors(board, block1.x, block1.y)
local vn1 = vertical_neighbors(board, block1.x, block1.y)
local hn2 = horisontal_neighbors(board, block2.x, block2.y)
local vn2 = vertical_neighbors(board, block2.x, block2.y)
quick_swap(board, block1, block2)
return #hn1 >= 2 or #hn2 >= 2 or #vn1 >= 2 or #vn2 >= 2
end
-- check if the board has any possible switches which will
-- result in a match of three or more blocks in a row
-- this does not take into account special blocks
local function has_possible_switches(board)
assert(board, "You must provide a board")
for x = 1, board.width - 2 do
for y = 1, board.height - 2 do
local slot = board.slots[x][y]
local up = board.slots[x][y + 1]
local down = board.slots[x][y - 1]
local left = board.slots[x - 1][y]
local right = board.slots[x + 1][y]
if swap_and_check_match(board, slot, up) then return true end
if swap_and_check_match(board, slot, down) then return true end
if swap_and_check_match(board, slot, left) then return true end
if swap_and_check_match(board, slot, right) then return true end
end
end
return false
end
--- Stabilize the board
-- This will find and remove matching blocks and spawn new blocks in their
-- place. This process will be repeated until the board is filled and no
-- more matches exists. The board is at that point considered stable
-- @param board The board to stabilize
-- @param callback Optional callback to invoke when board is stable
function M.stabilize(board, callback)
assert(board, "You must provide a board")
local fn = function()
while true do
async(function(done)
find_matching_neighbors(board)
remove_matching_neighbors(board, done)
end)
local did_collapse = async(function(done) collapse(board, done) end)
local did_spawn = async(function(done) trigger_spawners(board, done) end)
local did_slide = async(function(done) slide(board, done) end)
if not did_spawn and not did_collapse and not did_slide then
board.on_stabilized(board)
if not has_possible_switches(board) then
board.on_no_possible_switches(board)
end
if callback then callback() end
break
end
end
end
if not coroutine.running() then
utils.corun(fn)
else
fn()
end
end
function M.shuffle(board, callback)
assert(board, "You must provide a board")
assert(coroutine.running())
-- get list of all blocks that can be shuffled
local blocks = {}
for x = 0, board.width - 1 do
for y = 0, board.height - 1 do
local slot = board.slots[x][y]
if slot and not slot.blocker and not slot.spawner then
blocks[#blocks + 1] = slot
end
end
end
-- make sure there is an even number of blovks
if #blocks % 2 == 1 then
blocks[#blocks] = nil
end
-- shuffle blocks
for i = #blocks, 2, -1 do
local j = math.random(i)
blocks[i], blocks[j] = blocks[j], blocks[i]
end
-- swap places on blocks
while #blocks > 0 do
local block1 = table.remove(blocks)
local block2 = table.remove(blocks)
async(function(done) swap(board, block1, block2, done) end)
end
M.stabilize(board, callback)
end
--- Check if a position is on the board or not
-- @param board
-- @param x
-- @param y
-- @return true of the position is on the board
function M.on_board(board, x, y)
return x >= 0 and x < board.width and y >= 0 and y < board.height
end
--- Handle user input
-- @param board The board to apply the input on
-- @param action The user action table (must be a pressed or released action)
-- @return true if the action was handled