forked from lilybeevee/bab-be-u
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua
3517 lines (3298 loc) · 112 KB
/
utils.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
function clear()
puffs_this_world = 0
levels_this_world = 0
--groups_exist = false
letters_exist = false
if not doing_past_turns then
replay_playback = false
replay_playback_turns = nil
replay_playback_string = nil
replay_playback_turn = 1
replay_playback_time = love.timer.getTime()
replay_playback_interval = 0.3
old_replay_playback_interval = 0.3
replay_pause = false
replay_string = ""
end
rhythm_time = love.timer.getTime()
rhythm_interval = 1
rhythm_queued_movement = {0, 0, "wait"}
new_units_cache = {}
undoing = false
successful_brite_cache = nil
next_level_name = ""
win_sprite_override = {}
level_destroyed = false
last_input_time = nil
most_recent_key = nil
just_moved = true
should_parse_rules_at_turn_boundary = false
should_parse_rules = true
graphical_property_cache = {}
initializeGraphicalPropertyCache()
debug_values = {}
rng_cache = {}
reset_count = 0
last_move = nil
particles = {}
units = {}
units_by_id = {}
units_by_name = {}
units_by_tile = {}
units_by_layer = {}
backers_cache = {}
empties_by_tile = {}
outerlvl = nil
still_converting = {}
portaling = {}
rules_effecting_names = {}
referenced_objects = {}
referenced_text = {}
undo_buffer = {}
update_undo = true
max_layer = 1
max_unit_id = 0
max_temp_id = 0
max_mouse_id = 0
first_turn = true
cursor_convert = nil
cursor_converted = false
mouse_X = love.mouse.getX()
mouse_Y = love.mouse.getY()
last_click_x = nil
last_click_y = nil
mouse_oldX = mouse_X
mouse_oldY = mouse_Y
drag_units = {}
cursors = {}
cursors_by_id = {}
shake_dur = 0
shake_intensity = 0.5
current_turn = 0
current_move = 0
--za warudo needs a lot
timeless = false
time_destroy = {}
time_delfx = {}
time_sfx = {}
timeless_split = {}
timeless_win = {}
timeless_unwin = {}
timeless_reset = false
timeless_crash = false
timeless_yote = {}
firsttimestop = true
--if scene == game then
if load_mode == "play" then
createMouse_direct(love.mouse.getX(), love.mouse.getY())
end
--createMouse_direct(20, 20)
currently_winning = false
music_fading = false
won_this_session = false
level_ending = false
win_size = 0
tile_grid = {}
for i,page in ipairs(selector_grid_contents) do
tile_grid[i] = {}
for j,tile_name in ipairs(page) do
if j and tiles_by_name[tile_name] then
tile_grid[i][j-1] = tiles_by_name[tile_name]
else
tile_grid[i][j-1] = nil
end
end
end
if not doing_past_turns then
change_past = false
past_playback = false
all_moves = {}
past_rules = {}
past_ends = {}
end
card_for_id = {}
love.mouse.setCursor()
end
function pastClear()
if stopwatch ~= nil then
stopwatch.visible = false
end
should_parse_rules = true
doing_past_turns = false
past_playback = false
past_rules = {}
cutscene_tick = tick.group()
end
function metaClear()
rules_with = nil
rules_with_unit = nil
level_tree = {}
playing_world = false
parent_filename = nil
stay_ther = nil
surrounds = nil
pastClear()
end
function initializeGraphicalPropertyCache()
local properties_to_init = -- list of properties that require the graphical cache
{
"flye", "slep", "tranz", "gay", "stelth", "colrful", "xwx", "rave", "enby", -- miscelleaneous graphical effects
}
for i = 1, #properties_to_init do
local prop = properties_to_init[i]
if (graphical_property_cache[prop] == nil) then graphical_property_cache[prop] = {} end
end
end
function loadMap()
--no longer necessary, we now lazy initialize these
--[[for x=0,mapwidth-1 do
for y=0,mapheight-1 do
units_by_tile[x + y * mapwidth] = {}
end
end]]
local has_missing_levels = false
local rects = {}
local extra_units = {}
for _,mapdata in ipairs(maps) do
local version = mapdata.info.version
local map = mapdata.data
local offset = {x = 0, y = 0}
if mapdata.info.width < mapwidth then
offset.x = math.floor((mapwidth / 2) - (mapdata.info.width / 2))
end
if mapdata.info.height < mapheight then
offset.y = math.floor((mapheight / 2) - (mapdata.info.height / 2))
end
table.insert(rects, {x = offset.x, y = offset.y, w = mapdata.info.width, h = mapdata.info.height})
if version == 0 or version == nil then
if map == nil then
map = {}
for x=1,mapwidth do
for y=1,mapheight do
table.insert(map, {})
end
end
end
for i,v in ipairs(map) do
local tileid = i-1
local x = tileid % mapwidth
local y = math.floor(tileid / mapwidth)
for _,id in ipairs(v) do
local new_unit = createUnit(id, x, y, 1)
end
end
elseif version >= 1 and version <= 3 then
local pos = 1
while pos <= #map do
if version == 1 then
local tile, x, y, dir
tile, x, y, dir, pos = love.data.unpack(PACK_UNIT_V1, map, pos)
if inBounds(x, y) then
createUnit(tile, x + offset.x, y + offset.y, dir)
end
elseif version == 2 or version == 3 then
local id, tile, x, y, dir, specials
id, tile, x, y, dir, specials, pos = love.data.unpack(version == 2 and PACK_UNIT_V2 or PACK_UNIT_V3, map, pos)
if inBounds(x + offset.y, y + offset.y) then
local unit = createUnit(tile, x + offset.x, y + offset.y, dir, false, id)
local spos = 1
while spos <= #specials do
local k, v
k, v, spos = love.data.unpack(PACK_SPECIAL_V2, specials, spos)
unit.special[k] = v
end
end
end
end
else
local ok = nil
ok, map = serpent.load(map)
if (ok ~= true) then
print("Serpent error while loading:", ok, fullDump(map))
end
local floodfill = {}
local objects = {}
local lvls = {}
local locked_lvls = {}
local dofloodfill = scene ~= editor
for _,unit in ipairs(map) do
id, tile, x, y, dir, specials, color = unit.id, unit.tile, unit.x, unit.y, unit.dir, unit.special, unit.color
x = x + offset.x
y = y + offset.y
--track how many puffs and levels exist in this world (have to do this separately so we count hidden levels etc)
if specials.level then
levels_this_world = levels_this_world + 1
if readSaveFile{"levels", specials.level, "won"} then
puffs_this_world = puffs_this_world + 1
end
end
if scene == editor and specials.level then
if not love.filesystem.getInfo(getWorldDir() .. "/" .. specials.level .. ".bab") then
has_missing_levels = true
print("missing level: " .. specials.level)
local search = searchForLevels(getWorldDir(), specials.name, true)
if #search > 0 then
print(" - located: " .. search[1].file)
specials.level = search[1].file
specials.name = search[1].data.name
else
print(" - could not locate!")
end
end
end
if not dofloodfill then
local unit = createUnit(tile, x, y, dir, false, id, nil, color)
unit.special = specials
elseif tile == tiles_by_name["lvl"] then
if readSaveFile{"levels", specials.level, "seen"} then
specials.visibility = "open"
local tfs = readSaveFile{"levels", specials.level, "transform"}
for i,t in ipairs(tfs or {tiles_listPossiblyMeta(tile).name}) do
if i == 1 then
local unit = createUnit(tiles_by_namePossiblyMeta(t), x, y, dir, false, id, nil, color)
unit.special = deepCopy(specials)
if readSaveFile{"levels", specials.level, "won"} or readSaveFile{"levels", specials.level, "clear"} then
table.insert(floodfill, {unit, 1})
end
else
table.insert(extra_units, {tiles_by_namePossiblyMeta(t), x, y, dir, color, deepCopy(specials)})
end
end
elseif specials.visibility == "open" then
local unit = createUnit(tile, x, y, dir, false, id, nil, color)
unit.special = specials
elseif specials.visibility == "locked" then
table.insert(locked_lvls, {id, tile, x, y, dir, specials, color})
table.insert(objects, {id, tile, x, y, dir, specials, color})
else
table.insert(objects, {id, tile, x, y, dir, specials, color})
end
elseif tile == tiles_by_name["lin"] then
if specials.visibility == "hidden" then
table.insert(objects, {id, tile, x, y, dir, specials, color})
else
local unit = createUnit(tile, x, y, dir, false, id, nil, color)
unit.special = specials
end
else
if specials.level then
if readSaveFile{"levels", specials.level, "seen"} then
specials.visibility = "open"
end
local tfs = readSaveFile{"levels", specials.level, "transform"}
for i,t in ipairs(tfs or {tiles_listPossiblyMeta(tile).name}) do
if i == 1 then
local unit = createUnit(tiles_by_namePossiblyMeta(t), x, y, dir, false, id, nil, color)
unit.special = specials
else
table.insert(extra_units, {tiles_by_namePossiblyMeta(t), x, y, dir, color, deepCopy(specials)})
end
end
else
local unit = createUnit(tile, x, y, dir, false, id, nil, color)
unit.special = specials
end
end
end
--now check if we should grant clear/complete
if (level_puffs_to_clear > 0 and puffs_this_world >= level_puffs_to_clear) then
writeSaveFile(true, {"levels", level_filename, "clear"})
end
if (levels_this_world > 0 and puffs_this_world >= levels_this_world) then
writeSaveFile(true, {"levels", level_filename, "complete"})
end
if dofloodfill then
local created = {}
while #floodfill > 0 do
local u, ptype = unpack(table.remove(floodfill, 1))
local orthos = {[-1] = {}, [0] = {}, [1] = {}}
for a = 0,1 do -- 0 = ortho, 1 = diag
for i = #objects,1,-1 do
local v = objects[i] -- {id, tile, x, y, dir, specials, color}
local dx = u.x-v[3]
local dy = u.y-v[4]
if (((dx == -1 or dx == 1) and (dy == -a or dy == a)) or ((dx == -a or dx == a) and (dy == -1 or dy == 1)))
and (a == 0 or (not orthos[dx][0] and not orthos[0][dy])) then
orthos[dx][dy] = true
if not created[v[1]] then
if v[2] == tiles_by_name["lvl"] then
if ptype ~= 2 then
local unit = createUnit(v[2], v[3], v[4], v[5], false, v[1], nil, v[7])
created[v[1]] = true
unit.special = v[6]
if ptype == 1 then
unit.special.visibility = "open"
table.insert(floodfill, {unit, 2})
elseif ptype == 3 then
unit.special.visibility = "open"
end
elseif ptype == 2 and not table.has_value(locked_lvls, v) then
table.insert(locked_lvls, v)
table.insert(floodfill, {{x = v[3], y = v[4]}, 2})
end
elseif (ptype == 1 or ptype == 3) and v[2] == tiles_by_name["lin"] and (not v[6].pathlock or v[6].pathlock == "none") then
local unit = createUnit(v[2], v[3], v[4], v[5], false, v[1], nil, v[7])
created[v[1]] = true
unit.special = v[6]
table.insert(floodfill, {unit, 3})
end
end
end
end
end
end
for _,v in ipairs(locked_lvls) do
if not created[v[1]] then
local unit = createUnit(v[2], v[3], v[4], v[5], false, v[1], nil, v[7])
created[v[1]] = true
unit.special = v[6]
end
end
end
end
end
for x=0,mapwidth-1 do
for y=0,mapheight-1 do
local in_bounds = false
for _,rect in ipairs(rects) do
if x >= rect.x and x < rect.x + rect.w and y >= rect.y and y < rect.y + rect.h then
in_bounds = true
break
end
end
if not in_bounds then
createUnit(tiles_by_name["bordr"], x, y, 1)
end
end
end
for _,t in ipairs(extra_units) do
local unit = createUnit(t[1], t[2], t[3], t[4], false, nil, nil, t[5])
unit.specials = t[6]
end
if (load_mode == "play") then
initializeOuterLvl()
initializeEmpties()
loadStayTher()
if (not unit_tests) then
writeSaveFile(true, {"levels", level_filename, "seen"})
end
end
if has_missing_levels then
print(colr.red("\nLEVELS MISSING - PLEASE CHECK & SAVE!"))
end
--I don't know why, but this is slower by a measurable amount (70-84 seconds for example).
--[[groups_exist = letters_exist
if not groups_exist then
for _,group_name in ipairs(group_names) do
if units_by_name["text_"..group_name] then
groups_exist = true
break
end
end
end]]
unsetNewUnits()
end
function loadStayTher()
if stay_ther ~= nil then
for _,unit in ipairs(stay_ther) do
local newunit = createUnit(unit.tile, unit.x, unit.y, unit.dir)
newunit.special = unit.special
end
end
end
function initializeOuterLvl()
outerlvl = createUnit(tiles_by_name["lvl"], -999, -999, 1, nil, nil, true)
end
function initializeEmpties()
--TODO: other ways to make a text_no1 could be to have a text_text_no1 but that seems contrived that you'd have text_text_no1 but not text_no1?
--text_her counts because it looks for no1, I think. similarly we could have text_text_her but again, contrived
if ((not letters_exist) and (not units_by_name["text_no1"]) and (not units_by_name["text_every3"]) and (not units_by_name["text_her"])) then return end
for x=0,mapwidth-1 do
for y=0,mapheight-1 do
local tileid = x + y * mapwidth
empties_by_tile[tileid] = createUnit(tiles_by_name["no1"], x, y, (((tileid - 1) % 8) + 1), nil, nil, true)
end
end
end
function compactIds()
units_by_id = {}
for i,unit in ipairs(units) do
unit.id = i
units_by_id[i] = unit
end
max_unit_id = #units + 1
end
--[[
First and third arguments can be:
unit, string, nil
Second argument can be:
string
Unit argument will check conditions for that unit, and match rules using its name
Both nil and "?" act as a wildcard, however a nil wildcard will only check units & return the argument as a unit
Return value changes depending on how many arguments are nil
Example:
Rules:
BAB BE U - FLOG BE =) - ROC BE KEEK - KEEK GOT MEEM
Units:
[BAB] [FLOG] [KEEK] [MEEM]
matchesRule(bab unit,"be","u") => {BAB BE U}
- Returns the matching "BAB BE U" rule, as it checks the unit's name
matchesRule("bab","be","?") => {BAB BE U}
- Same result, as the U property matches the wildcard
matchesRule(nil,"be","?") => {{BAB BE U, bab unit}, {FLOG BE =), flog unit}}
- The rule for ROC is not returned because no ROC exists, however the others do
matchesRule("?","be",nil) => {{ROC BE KEEK, keek unit}}
- The first two rules are not returned because properties have no matching units
matchesRule(nil,"?",nil) => {{KEEK GOT MEEM, keek unit, meem unit}}
- Both KEEK and MEEM units exist and GOT matches the wildcard, so it returns both units in order
Note that the rules returned are full rules, formatted like: {{subject,verb,object,{preconds,postconds}}, {ids}}
]]
function matchesRule(rule1,rule2,rule3,stopafterone,debugging)
if (debugging) then
print("matchesRule arguments:"..tostring(rule1)..","..tostring(rule2)..","..tostring(rule3))
end
local nrules = {} -- name
local fnrules = {} -- fullname
local rule_units = {}
local function getnrule(o,i)
if type(o) == "table" then
local name
local fullname
if o.class == "unit" then
name = o.name
if o.fullname ~= o.name then
fullname = o.fullname
end
elseif o.class == "cursor" then
name = "mous"
end
nrules[i] = name
if fullname then
fnrules[i] = fullname
end
rule_units[i] = o
else
if o ~= "?" then
nrules[i] = o
end
end
end
getnrule(rule1,1)
nrules[2] = rule2
getnrule(rule3,3)
if (debugging) then
for x,y in ipairs(nrules) do
print("in nrules:"..tostring(x)..","..tostring(y))
end
end
local ret = {}
local find = 0
local find_arg = 0
if (rule1 == nil and rule3 ~= nil) or (rule1 ~= nil and rule3 == nil) then
find = 1
if rule1 == nil then
find_arg = 1
elseif rule3 == nil then
find_arg = 3
end
elseif rule1 == nil and rule3 == nil then
find = 2
end
local rules_list
--there are more properties than there are nouns, so we're more likely to miss based on a property not existing than based on a noun not existing
rules_list = rules_with[(nrules[2] ~= "be" and nrules[2]) or nrules[3] or nrules[1] or nrules[2]] or {}
mergeTable(rules_list, rules_with[fnrules[3] or fnrules[1]] or {})
if (debugging) then
print ("found this many rules:"..tostring(#rules_list))
end
if #rules_list > 0 then
for _,rules in ipairs(rules_list) do
local rule = rules.rule
if (debugging) then
for i=1,3 do
print("checking this rule,"..tostring(i)..":"..tostring(rule[ruleparts[i] ].name))
end
end
local result = true
for i=1,3 do
local name = rule[ruleparts[i]].name
--special case for stuff like 'group be x' - if we are in that group, we do match that rule
--we also need to handle groupn't
--seems to not impact performance much?
local group_match = false
if rule_units[i] ~= nil then
if group_sets[name] and group_sets[name][rule_units[i] ] then
group_match = true
else
if rule_units[i].type == "object" and group_names_set_nt[name] then
local nament = name:sub(1, -4)
if not group_sets[nament][rule_units[i] ] then
group_match = true
end
end
end
end
if not (group_match) then
if nrules[i] ~= nil and nrules[i] ~= name and (fnrules[i] == nil or (fnrules[i] ~= nil and fnrules[i] ~= name)) then
if (debugging) then
print("false due to nrules/fnrules mismatch")
end
result = false
end
end
end
--don't test conditions until the rule fully matches
if result then
for i=1,3,2 do
if rule_units[i] ~= nil then
if not testConds(rule_units[i], rule[ruleparts[i]].conds, rule_units[1]) then
if (debugging) then
print("false due to cond", i)
end
result = false
else
--check that there isn't a verbn't rule - edge cases where this might happen: text vs specific text, group vs unit. This is slow (15% longer unit tests, 0.1 second per unit test) but it fixes old and new bugs so I think we just have to suck it up.
if rules_with[rule.verb.name.."n't"] ~= nil and #matchesRule(rule_units[i], rule.verb.name.."n't", rule.object.name, true) > 0 then
result = false
end
end
end
end
end
if result then
if (debugging) then
print("matched: " .. dump(rule) .. " | find: " .. find, nrules[1], fnrules[1], rule.subject.name, rule.subject.fullname)
end
if find == 0 then
table.insert(ret, rules)
if stopafterone then return ret end
elseif find == 1 then
for _,unit in ipairs(findUnitsByName(rule[ruleparts[find_arg]].name)) do
local cond
if testConds(unit, rule[ruleparts[find_arg]].conds) then
--check that there isn't a verbn't rule - edge cases where this might happen: text vs specific text, group vs unit. This is slow (15% longer unit tests, 0.1 second per unit test) but it fixes old and new bugs so I think we just have to suck it up.
if rules_with[rule.verb.name.."n't"] ~= nil and #matchesRule(unit, rule.verb.name.."n't", rule.object.name, true) > 0 then
else
table.insert(ret, {rules, unit})
if stopafterone then return ret end
end
end
end
elseif find == 2 then
local found1, found2
for _,unit1 in ipairs(findUnitsByName(rule.subject)) do
for _,unit2 in ipairs(findUnitsByName(rule.object)) do
if testConds(unit1, rule.subject.conds) and testConds(unit2, rule.object.conds, unit1) then
table.insert(ret, {rules, unit1, unit2})
if stopafterone then return ret end
end
end
end
end
end
end
end
return ret
end
function getUnitsWithEffect(effect)
local result = {}
local gotten = {}
local rules = matchesRule(nil, "be", effect)
--print ("h:"..tostring(#rules))
for _,dat in ipairs(rules) do
local unit = dat[2]
if not unit.removed and not hasRule(unit, "ben't", effect) then
table.insert(result, unit)
gotten[unit] = true
end
end
local rules = matchesRule(nil, "giv", effect)
for _,rule in ipairs(rules) do
local unit = rule[2]
if not unit.removed then
for _,other in ipairs(getUnitsOnTile(unit.x, unit.y, nil, false, unit, nil, hasProperty(unit,"big"))) do
if not gotten[other] and sameFloat(unit, other) and not hasRule(other, "ben't", effect) and ignoreCheck(other, unit) then
table.insert(result, other)
gotten[other] = true
end
end
end
end
if hasRule(outerlvl, "giv", effect) then
for _,unit in ipairs(units) do
if not gotten[unit] and inBounds(unit.x, unit.y) and not hasRule(unit, "ben't", effect) and ignoreCheck(unit, outerlvl) then
table.insert(result, unit)
end
end
end
if rules_with["rp"] then
for _,unit in ipairs(result) do
local isrp = matchesRule(nil,"rp",unit)
for _,ruleparent in ipairs(isrp) do
local mimic = ruleparent[2]
if not gotten[mimic] and not hasRule(mimic,"ben't",effect) then
gotten[mimic] = true
table.insert(result,mimic)
end
end
end
local therp = matchesRule(nil,"rp","the")
for _,ruleparent in ipairs(therp) do
local the = ruleparent[1].rule.object.unit
local tx = the.x+dirs8[the.dir][1]
local ty = the.y+dirs8[the.dir][2]
local mimic = ruleparent[2]
local stuff = getUnitsOnTile(tx,ty)
for _,unit in ipairs(stuff) do
if hasProperty(unit,effect) and not hasRule(mimic,"ben't",effect) then
table.insert(result,mimic)
break
end
end
end
end
return result
end
function getUnitsWithEffectAndCount(effect)
local result = {}
local rules = matchesRule(nil, "be", effect)
--print ("h:"..tostring(#rules))
for _,dat in ipairs(rules) do
local unit = dat[2]
if not unit.removed and not hasRule(unit, "ben't", effect) then
if result[unit] == nil then
result[unit] = 0
end
result[unit] = result[unit] + 1
end
end
local rules = matchesRule(nil, "giv", effect)
for _,rule in ipairs(rules) do
local unit = rule[2]
if not unit.removed then
for _,other in ipairs(getUnitsOnTile(unit.x, unit.y, nil, false, unit, nil, hasProperty(unit,"big"))) do
if sameFloat(unit, other) and not hasRule(other, "ben't", effect) and ignoreCheck(other, unit) then
if result[other] == nil then
result[other] = 0
end
result[other] = result[other] + 1
end
end
end
end
if hasRule(outerlvl, "giv", effect) then
for _,unit in ipairs(units) do
if inBounds(unit.x, unit.y) and not hasRule(unit, "ben't", effect) and ignoreCheck(unit, outerlvl) then
if result[unit] == nil then
result[unit] = 0
end
result[unit] = result[unit] + 1
end
end
end
if rules_with["rp"] then
for unit,count in pairs(result) do
local isrp = matchesRule(nil,"rp",unit)
for _,ruleparent in ipairs(isrp) do
local mimic = ruleparent[2]
if not mimic.removed and not hasRule(mimic,"ben't",effect) then
result[mimic] = count
end
end
end
local therp = matchesRule(nil,"rp","the")
for _,ruleparent in ipairs(therp) do
local the = ruleparent[1].rule.object.unit
local tx = the.x+dirs8[the.dir][1]
local ty = the.y+dirs8[the.dir][2]
local mimic = ruleparent[2]
local stuff = getUnitsOnTile(tx,ty)
for _,unit in ipairs(stuff) do
if hasProperty(unit,effect) and not hasRule(mimic,"ben't",effect) then
result[mimic] = countProperty(unit,effect)
end
end
end
end
return result
end
function getUnitsWithRuleAndCount(rule1, rule2, rule3)
local result = {}
local rules = matchesRule(rule1, rule2, rule3)
--print ("h:"..tostring(#rules))
for _,dat in ipairs(rules) do
local unit = dat[2]
if not unit.removed then
if result[unit] == nil then
result[unit] = 0
end
result[unit] = result[unit] + 1
end
end
if rules_with["rp"] then
for unit,count in pairs(result) do
local isrp = matchesRule(nil,"rp",unit)
for _,ruleparent in ipairs(isrp) do
local mimic = ruleparent[2]
if not mimic.removed and not hasRule(mimic,rule2.."n't",rule3) then
result[mimic] = count
end
end
end
local therp = matchesRule(nil,"rp","the")
for _,ruleparent in ipairs(therp) do
local the = ruleparent[1].rule.object.unit
local tx = the.x+dirs8[the.dir][1]
local ty = the.y+dirs8[the.dir][2]
local mimic = ruleparent[2]
local stuff = getUnitsOnTile(tx,ty)
for _,unit in ipairs(stuff) do
if hasRule(unit,rule2,rule3) and not hasRule(mimic,rule2.."n't",rule3) then
result[mimic] = countProperty(unit,effect)
end
end
end
end
return result
end
function hasRule(rule1,rule2,rule3)
if #matchesRule(rule1,rule2,rule3, true) > 0 then return true end
if not rules_with["rp"] then return false end
if #matchesRule(rule1,rule2.."n't",rule3, true) > 0 then return false end
local isrp = matchesRule(rule1,"rp",nil)
for _,ruleparent in ipairs(isrp) do
local mimic = ruleparent[2]
if #matchesRule(mimic,rule2,rule3, true) > 0 then return true end
end
return false
end
function validEmpty(unit)
return #unitsByTile(unit.x, unit.y) == 0
end
function findUnitsByName(name)
if group_names_set_nt[name] then
local everything_else_list = findUnitsByName(name:sub(1, -4))
local everything_else_set = {}
for _,unit in ipairs(everything_else_list) do
everything_else_set[unit] = true
end
local result = {}
for _,unit in ipairs(units) do
if unit.type == "object" and not everything_else_set[unit] then
table.insert(result, unit)
end
end
return result
elseif name == "mous" then
return cursors
elseif group_lists[name] ~= nil then
return group_lists[name]
elseif name == "no1" then
local result = {}
for _,unit in ipairs(units_by_name["no1"]) do
if validEmpty(unit) then
table.insert(result, unit)
end
end
return result
else
return units_by_name[name] or {}
end
end
function hasProperty(unit,prop)
if not rules_with[prop] and prop ~= "?" then return false end
if hasRule(unit, "be", prop) then return true end
if type(unit) ~= "table" then return false end
if not rules_with["giv"] then return false end
if hasRule(unit, "ben't", prop) then return false end
if unit == outerlvl then return false end
if unit and unit.class == "mous" then return false end
if unit then
if hasRule(outerlvl, "giv", prop) then return inBounds(unit.x, unit.y) end
for _,other in ipairs(getUnitsOnTile(unit.x, unit.y, nil, false, unit, true, hasRule(unit,"be","big"))) do
if #matchesRule(other, "giv", prop) > 0 and sameFloat(unit, other) and ignoreCheck(unit, other) then
return true
end
end
else
if hasRule(outerlvl, "giv", prop) then return true end
for _,ruleparent in ipairs(matchesRule(nil, "giv", prop)) do
for _,other in ipairs(ruleparent.units) do
if #getUnitsOnTile(other.x, other.y, nil, false, other, true, hasRule(unit,"be","big")) > 0 and sameFloat(unit, other) then
return true
end
end
end
end
return false
end
function countProperty(unit, prop, ignore_flye)
if not rules_with[prop] and prop ~= "?" then return 0 end
local result = #matchesRule(unit,"be",prop)
if hasRule(unit, "ben't", prop) then return 0 end
if not rules_with["giv"] then return result end
if unit == outerlvl then return result end
if unit and unit.class == "mous" then return result end
result = result + #matchesRule(outerlvl, "giv", prop)
if unit then
for _,other in ipairs(getUnitsOnTile(unit.x, unit.y, nil, false, unit, true, hasProperty(unit,"big"))) do
if ignoreCheck(unit, other) and (ignore_flye or sameFloat(unit, other)) then
result = result + #matchesRule(other, "giv", prop)
end
end
else -- I don't think anything uses this? it doesn't seem very useful at least, but I guess it's functional?
for _,ruleparent in ipairs(matchesRule(nil, "giv", prop)) do
for _,other in ipairs(ruleparent.units) do
if ignoreCheck(unit, other) and (ignore_flye or sameFloat(unit, other)) then
result = result + #getUnitsOnTile(other.x, other.y, nil, false, other, true, hasProperty(other,"big"))
end
end
end
end
return result
end
function hasU(unit)
return hasProperty(unit,"u") or hasProperty(unit,"u too") or hasProperty(unit,"u tres") or hasProperty(unit,"y'all")
end
function getUs()
local yous = getUnitsWithEffect("u")
mergeTable(yous,getUnitsWithEffect("u too"))
mergeTable(yous,getUnitsWithEffect("u tres"))
mergeTable(yous,getUnitsWithEffect("y'all"))
return yous
end
--to prevent infinite loops where a set of rules/conditions is self referencing
withrecursion = {}
function testConds(unit, conds, compare_with) --cond should be a {condtype,{object types},{cond_units}}
local endresult = true
for _,cond in ipairs(conds or {}) do
local condtype = cond.name
local lists = {} -- for iterating
local sets = {} -- for checking
if condtype:starts("that") then
lists = cond.others or {} -- using "lists" to store the names, since THAT doesn't allow nesting, and we need the name for hasRule
elseif cond.others then
for _,other in ipairs(cond.others) do
local list = {}
local set = {}
if other.name == "lvl" then -- probably have to account for group/every1 here too, maybe more
table.insert(list, outerlvl)
set[outerlvl] = true
elseif group_lists[other.name] then
list = group_lists[other.name]
set = group_sets[other.name]
else
for _,otherunit in ipairs(findUnitsByName(other.name)) do -- findUnitsByName handles mous and no1 already
if testConds(otherunit, other.conds, unit) then
table.insert(list, otherunit)
set[otherunit] = true
end
end
end
table.insert(lists, list)
table.insert(sets, set)
end
end
local result = true
local cond_not = false
if condtype:ends("n't") then
condtype = condtype:sub(1, -4)
cond_not = true
end
local x, y = unit.x, unit.y
local old_withrecursioncond = withrecursion[cond]
withrecursion[cond] = true
if (old_withrecursioncond) then
result = false
elseif condtype:starts("that") then
result = true
local verb = condtype:sub(6)
for _,param in ipairs(lists) do -- using "lists" to store the names, since THAT doesn't allow nesting, and we need the name for hasRule
local word = param.unit
local wx = word.x
local wy = word.y
local wdir = word.dir
local wdx = dirs8[wdir][1]
local wdy = dirs8[wdir][2]
if param.name == "her" then
if unit.x ~= wx+wdx or unit.y ~= wy+wdy then
result = false
end
elseif param.name == "thr" then
local wtx,wty = wx+wdx,wy+wdy
local stopped = false
while not stopped do
if canMove(unit,wdx,wdy,wdir,false,false,nil,nil,nil,wtx,wty) then
wdx,wdy,wdir,wtx,wty = getNextTile(word, wdx, wdy, wdir, nil, wtx, wty)
else
stopped = true
end
end
if unit.x ~= wtx or unit.y ~= wty then