-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
1410 lines (1122 loc) · 51.8 KB
/
Copy pathcontrol.lua
File metadata and controls
1410 lines (1122 loc) · 51.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
log("Entered control.lua")
local WT = require("common")()
local swap_turrets = require("swap_turrets")
local math2d = require("math2d")
local ignore_or_acid = require("ignore_fires")
-- Make remote interface for "Lua API global Variable Viewer (gvv)" by x2605.
-- (https://mods.factorio.com/mod/gvv)
-- If that mod is active, one can inspect the global table of this mod at runtime.
WT.show("script.active_mods['gvv']", script.active_mods["gvv"])
WT.show("WT.debug_gvv", WT.debug_gvv)
if script.active_mods["gvv"] and WT.debug_gvv then
require("__gvv__.gvv")()
WT.dprint({"WT-misc.WT-gvv_enabled", script.mod_name, {"mod_name.gvv"}})
end
local on_tick_profiler, turret_profiler
--~ ------------------------------------------------------------------------------------
--~ -- Check if target belongs to us (own force, friends, allies) or is an enemy
--~ -- (returns true or false)
--~ ------------------------------------------------------------------------------------
--~ local function is_enemy(turret, target)
--~ WT.dprint("Entered function is_enemy(%s, %s).", {
--~ WT.print_name(turret), WT.print_name(target)
--~ })
--~ local ret
--~ -- Check arguments
--~ if not (turret and turret.valid) then
--~ error(string.format("%s is not a valid turret!", WT.print_name_id(turret)))
--~ end
--~ if target and target.valid and target.force then
--~ ret = WT.is_enemy_force(turret.force, target.force)
--~ end
--~ WT.dprint("End of function is_enemy(%s, %s). Return: %s", {
--~ WT.print_name(turret), WT.print_name(target), ret
--~ })
--~ return ret
--~ end
------------------------------------------------------------------------------------
-- Check whether fire dummy actually marks fires. (Returns boolean value)
------------------------------------------------------------------------------------
local function dummy_marks_fire(dummy)
WT.dprint("Entered function dummy_marks_fire(%s) on tick %g.", {
WT.print_name_id(dummy), game.tick
})
local ret = false
-- Check argument
if dummy and dummy.valid then
WT.dprint("Dummy is valid!")
local dummy_id = dummy.unit_number
local fire_id = storage.fire_dummies[dummy_id] and storage.fire_dummies[dummy_id].fire_id
local fire = fire_id and storage.fires[fire_id] and
storage.fires[fire_id].fire_entity and
storage.fires[fire_id].fire_entity.valid
if storage.fire_dummies[dummy_id] and fire then
ret = true
end
end
WT.dprint("End of function dummy_marks_fire(%s) on tick %g. (Return: %s)", {
WT.print_name_id(dummy), game.tick, ret
})
return ret
end
------------------------------------------------------------------------------------
-- Search for fire
------------------------------------------------------------------------------------
local function find_fire(turret)
WT.dprint("Entered function find_fire(%s) on tick %g.",
{ WT.print_name_id(turret), game.tick })
-- Check argument
if not (turret and turret.valid and storage.WT_turrets[turret.unit_number]) then
error("Wrong arguments for function find_fire(turret):\nTurret is not valid!\n")
end
local ret
-- Determine search area in the direction the turret is facing
local area = storage.WT_turrets[turret.unit_number].area or
WT.get_turret_area(turret)
WT.show("Turret area", area)
local x_min = area.left_top.x or area.left_top[1]
local y_min = area.left_top.y or area.left_top[2]
local x_max = area.right_bottom.x or area.right_bottom[1]
local y_max = area.right_bottom.y or area.right_bottom[2]
WT.show("storage.dummy_positions", storage.dummy_positions)
for x, y in pairs(storage.dummy_positions) do
WT.dprint("x: %s\tx_min: %s\tx_max: %s", {x, x_min, x_max})
if x >= x_min and x <= x_max then
WT.dprint("x is valid -- checking y!")
for y, dummy_id in pairs(y) do
WT.dprint("y: %s\ty_min: %s\ty_max: %s", {y, y_min, y_max})
if y >= y_min and y <= y_max and WT.can_shoot(turret, {x, y}) then
ret = dummy_id
break
end
end
end
if ret then
break
end
end
WT.dprint("End of function find_fire(%s) on tick %g. (Return: %s)", {
WT.print_name_id(turret),
game.tick, ret or "nil"
})
return ret
end
------------------------------------------------------------------------------------
-- Search for enemies within the turret's range (returns nil or table of entities).
-- Expects: turret (entity)
------------------------------------------------------------------------------------
local function find_enemies(turret)
WT.dprint("Entered function find_enemies(%s) on tick %g.", {
WT.print_name_id(turret), game.tick
})
-- Check arguments
if not (turret and turret.valid) then
error(serpent.line(turret) .. " is not a valid argument!")
end
if not storage.WT_turrets[turret.unit_number] then
WT.dprint("Turret is not in list!")
return
end
local ret
-- Determine search area in the direction the turret is facing
local area = storage.WT_turrets[turret.unit_number].area or WT.get_turret_area(turret)
-- Find entities that are potential enemies
WT.show("storage.enemy_types", storage.enemy_types)
WT.show("area", area)
WT.dprint("WT.force_relations[%s]: %s", {turret.force.name, WT.force_relations[turret.force.name]})
local enemies = turret.surface.find_entities_filtered({
type = storage.enemy_types,
--~ position = turret.position,
--~ radius = storage.WT_turrets[turret.unit_number].range
area = area,
force = WT.force_relations[turret.force.name],
})
WT.show("Enemies in area", enemies)
-- Remove entities that are not enemies of the turret
local cnt = 0
for e, enemy in pairs(enemies) do
--~ WT.dprint("index: %g\tenemy: %s", { e, enemy })
--~ if not (
--~ is_enemy(turret, enemy) and
--~ math2d.bounding_box.contains_point(area, enemy.position) and
--~ WT.is_in_range(turret, enemy)
if not (WT.is_enemy(turret, enemy) and WT.can_shoot(turret, enemy.position)) then
enemies[e] = nil
cnt = cnt + 1
end
end
WT.dprint("Enemy list after removing %g enemies: %s", {cnt, enemies})
-- We want to find enemies in the order of enemy types stored in the storage table
for p, prototype in ipairs(storage.enemy_types) do
for e, enemy in pairs(enemies or {}) do
WT.dprint("p: %s\tprototype: %s\te: %s\tenemy: %s", {p, prototype, e, WT.print_name_id(enemy)})
if enemy.type == prototype then
ret = enemy
break
end
end
if ret then
break
end
end
WT.dprint("End of function find_enemies(%s) on tick %g (Found %s)", {
WT.print_name_id(turret), game.tick, (ret and WT.print_name_id(ret) or "no")
})
return ret
end
------------------------------------------------------------------------------------
-- Remove entry from lists
------------------------------------------------------------------------------------
local function remove_fire_and_dummy(entity_type, id)
WT.dprint("Entered function remove_fire_and_dummy(%s, %g).", { entity_type, id })
if not (entity_type and type(entity_type) == "string" ) then
error("Wrong entity (string expected): " .. serpent.line(entity_type))
elseif not (id and type(id) == "number") then
error("Wrong ID (number expected): " .. serpent.line(id))
end
if entity_type ~= "fire" and entity_type ~= "dummy" then
WT.dprint("Nothing to do for entity %s!", { entity_type })
return
end
------------------------------------------------------------------------------------
local fire, dummy, f_index, d_index
-- Get entities
if entity_type == "fire" and storage.fires[id] then
WT.dprint("Looking for fire %s", { id })
f_index = id
fire = storage.fires[f_index].fire_entity
d_index = f_index and storage.fires[f_index].dummy_id
dummy = d_index and storage.fire_dummies and
storage.fire_dummies[d_index] and
storage.fire_dummies[d_index].dummy_entity
elseif entity_type == "dummy" and storage.fire_dummies[id] then
WT.dprint("Looking for dummy %s", { id })
d_index = id
dummy = storage.fire_dummies[d_index].dummy_entity
f_index = d_index and storage.fire_dummies[d_index].fire_id
fire = f_index and storage.fires and
storage.fires[f_index] and
storage.fires[f_index].fire_entity
end
WT.dprint("fire[%s]:\t%s", {
f_index, (fire and fire.valid and fire.name or "NIL")
})
-- Remove from storage tables
local function remove(x, y)
if storage.dummy_positions[x] then
storage.dummy_positions[x][y] = nil
end
if storage.dummy_positions[x] and not next(storage.dummy_positions[x]) then
storage.dummy_positions[x] = nil
end
end
local x, y
if f_index then
if fire and fire.valid then
x = fire.position.x or fire.position[1]
y = fire.position.y or fire.position[2]
remove(x, y)
end
storage.fires[f_index] = nil
WT.dprint("Removed fire " .. f_index .. " from storage.fires!")
end
if d_index then
if dummy and dummy.valid then
x = dummy.position.x or dummy.position[1]
y = dummy.position.y or dummy.position[2]
remove(x, y)
end
storage.fire_dummies[d_index] = nil
WT.dprint("Removed dummy " .. d_index .. " from storage.fire_dummies!")
end
-- Destroy entities
if fire and fire.valid then
fire.surface.create_entity{position = fire.position, name = WT.burnt_patch }
fire.destroy()
WT.show("DESTROYED FIRE", f_index)
end
if dummy and dummy.valid then
dummy.surface.create_entity{position = dummy.position, name = WT.burnt_patch }
dummy.destroy()
WT.show("DESTROYED DUMMY", d_index)
end
--~ WT.dprint("storage: " .. serpent.block(storage))
WT.dprint("f_index: " .. tostring(f_index) .. "\td_index: " .. tostring(d_index))
WT.dprint("End of function remove_fire_and_dummy(%s, %g).", { entity_type, id })
return
end
------------------------------------------------------------------------------------
-- Target enemies or fire --
------------------------------------------------------------------------------------
local function target_enemy_or_fire(turret)
WT.dprint("Entered function target_enemy_or_fire(%s) on tick %s.", { turret, game.tick })
--~ WT.show("Number of turrets", table_size(storage.WT_turrets))
-- Check argument
--~ if type(turret) == "number" then
--~ turret = storage.WT_turrets[turret] and storage.WT_turrets[turret].entity
turret = (type(turret) == "number" and storage.WT_turrets[turret]
and storage.WT_turrets[turret].entity) or
(turret and turret.valid and
storage.WT_turrets[turret.unit_number] and
storage.WT_turrets[turret.unit_number].entity)
--~ if type(turret) == "number" then
--~ turret = storage.WT_turrets[turret] and storage.WT_turrets[turret].entity
--~ elseif (not WT.is_WT_turret(turret)) or turret.name == WT.steam_turret_name then
--~ elseif not WT.is_WT_turret_name(turret, WT.water_turret_name) then
--~ elseif not turret and WT.is_WT_turret_name(turret, WT.water_turret_name) then
--~ WT.show("Not a valid turret", print_name_id(turret))
--~ return
--~ end
--~ WT.show("turret", WT.print_name_id(turret))
--~ WT.show("Number of turrets", table_size(storage.WT_turrets))
------------------------------------------------------------------------------------
-- Leave early?
------------------------------------------------------------------------------------
-- Return if there is no turret
if not turret then
return
end
-- No need to do anything if turret has no ammo!
local ammo = turret.get_fluid_contents()
if table_size(ammo) == 0 then
WT.dprint("Leaving early: No ammo!")
return
end
------------------------------------------------------------------------------------
-- Check if turret is busy already
------------------------------------------------------------------------------------
WT.dprint("Check if %s is attacking something.", { WT.print_name_id(turret) })
local target = turret.shooting_target
WT.show("Current target", WT.print_name_id(target))
-- Turret attacks something.
if target and target.valid then
WT.dprint("%s: shooting at %s.", { WT.print_name_id(turret), WT.print_name_id(target) })
-- Fire dummy was attacked -- remove dummy if it marks no fire!
if WT.is_WT_dummy(target) and not dummy_marks_fire(target) then
WT.dprint("No fires at position of %s.", { WT.print_name_id(target) })
remove_fire_and_dummy("dummy", target.unit_number)
target.destroy()
end
end
-- Prioritize targets according to startup setting
WT.dprint("Prioritizing attack!")
-- Attack fire!
WT.show("Priority setting", WT.waterturret_priority)
WT.show("not target", (not target))
WT.show("WT.is_WT_dummy(target)", WT.is_WT_dummy(target))
if WT.waterturret_priority == "fire" then
WT.show("Looking for fire", storage.WT_turrets[turret.unit_number])
if not (target and WT.is_WT_dummy(target)) then
WT.dprint("Need to find fire")
--~ local fire = WT.find_fire(turret)
local fire = find_fire(turret)
WT.show("Found fire", fire)
fire = fire and storage.fire_dummies[fire] and storage.fire_dummies[fire].dummy_entity
if fire and fire.valid then
turret.shooting_target = fire
end
end
-- Attack enemies!
elseif WT.waterturret_priority == "enemy" then
WT.show("Looking for enemies", { WT.print_name_id(turret) })
if (not target) or WT.is_WT_dummy(target) then
local target = find_enemies(turret)
if target and target.valid then
WT.show("Attacking enemy", WT.print_name_id(target))
turret.shooting_target = target
return
end
end
-- Player doesn't care about priority, the game decides which entity to attack!
else
WT.dprint("Nothing to do -- automatic targetting is on!")
end
WT.dprint("End of function target_enemy_or_fire(%s) on tick %s.", {
WT.print_name_id(turret), game.tick
})
end
------------------------------------------------------------------------------------
-- Act on turret tick --
------------------------------------------------------------------------------------
local function check_turret(id)
local turret = id and storage.WT_turrets[id]
WT.show("Turret data", turret and WT.print_name_id(turret.entity or "nil"))
WT.show("turret.tick", turret and turret.tick)
WT.show("action_delay", WT.action_delay)
local turret_id = id
-- Remove invalid turrets from list
if not (turret and WT.is_WT_turret(turret.entity)) then
storage.WT_turrets[id] = nil
--~ remove_turret(id)
WT.dprint("Removed turret %s from list because it was not valid.", { id })
-- Check the turret!
else
WT.dprint("May act on turret")
local next_tick = turret.tick + WT.action_delay
--~ WT.dprint("%s is not an extinguisher turret: %s", {turret.entity.name, turret.entity.name ~= WT.extinguisher_turret_name})
-- Swap turrets if necessary
--~ local turret_id = (turret.entity.name ~= WT.extinguisher_turret_name) and
--~ (turret.entity.name ~= WT.extinguisher_turret_water_name) and swap_turrets.swap_turrets(id) or id
--~ turret_id = swap_turrets.swap_turrets(id) or id
turret_id = swap_turrets.swap_turrets(id)
WT.dprint("Returned from swap_turrets. Old ID: %s\tNew ID: %s turret_id", {id, turret_id})
--~ if turret_id and storage.WT_turrets[turret_id].entity.valid then
--~ if WT.is_WT_turret(turret_id) then
-- Remove old turret from list
if turret_id ~= id then
storage.WT_turrets[id] = nil
end
-- Set next action tick
--~ local next_tick = game.tick + WT.action_delay
--~ storage.WT_turrets[turret_id].tick = game.tick + WT.action_delay
storage.WT_turrets[turret_id].tick = next_tick
storage.turret_ticks[next_tick] = storage.turret_ticks[next_tick] or {}
storage.turret_ticks[next_tick][turret_id] = true
-- Check if we need to find fires or retarget
if WT.waterturret_priority ~= "default" and
WT.is_WT_turret_name(storage.WT_turrets[turret_id].entity,
WT.water_turret_name) then
WT.dprint("Water turret -- going to target_enemy_or_fire!")
target_enemy_or_fire(turret_id)
end
--~ -- This should never be reached!
--~ else
--~ error("Something went wrong! Turret with id ".. tostring(turret_id) ..
--~ "doesn't exist!\n" .. serpent.block(storage.WT_turrets))
--~ end
end
return turret_id
end
------------------------------------------------------------------------------------
-- Event handlers --
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-- Do this on every tick
local function on_tick(event)
WT.dprint("Entered function on_tick on tick %s", {event.tick})
--~ storage.enemy_types = storage.enemy_types or WT.enemies("get_list")
--~ storage.acids = storage.acids or WT.make_name_list(ignore_or_acid.get_acid())
--~ storage.ignore_fires = storage.ignore_fires or ignore_or_acid.get_ignorelist()
storage.turret_ticks = storage.turret_ticks or {}
local tick = event.tick
local next_tick = tick + WT.action_delay
WT.show("event tick", tick)
WT.show("next_tick", next_tick)
local turrets = storage.turret_ticks[tick]
WT.show("turrets", turrets)
if turrets and next(turrets) then
--~ log("Active turrets on tick " .. tick .. ": " .. table_size(turrets))
local new_id
storage.turret_ticks[next_tick] = storage.turret_ticks[next_tick] or {}
--~ WT.dprint("%s turrets are active this tick: %s", {table_size(turrets), turrets})
WT.dprint("%s turrets are active this tick.", {table_size(turrets)})
for turret, _ in pairs(turrets) do
WT.dprint("Checking turret %s", {turret})
new_id = check_turret(turret)
storage.turret_ticks[next_tick][new_id] = true
end
--~ storage.turret_ticks[tick] = nil
--~ WT.dprint("Removed tick %s from list: %s", {tick, storage.turret_ticks})
else
WT.dprint("Nothing to do on tick %s", {tick})
end
storage.turret_ticks[tick] = nil
--~ WT.dprint("Removed tick %s from list: %s", {tick, storage.turret_ticks})
WT.dprint("Removed tick %s from list.", {tick})
WT.dprint("End of function on_tick on tick %s", {event.tick})
end
------------------------------------------------------------------------------------
-- Extinguish_fire
local function extinguish_fire(entity)
WT.dprint("Entered function extinguish_fire(%s).", { WT.print_name_id(entity) })
local id = (entity and type(entity) == "number" and entity) or entity.unit_number
local dummy = id and storage.fire_dummies[id] and storage.fire_dummies[id].dummy_entity
if dummy then
local fires = dummy.surface.find_entities_filtered{
type = "fire",
position = dummy.position,
radius = WT.fire_dummy_radius,
}
if fires then
for f, fire in pairs(fires) do
if (not storage.ignore_fires[fire.name]) then
-- Don't need to raise the event because register_on_entity_destroyed is used!
fire.destroy()
end
end
end
WT.dprint("Extinguished %g fires around position %s.", { table_size(fires), entity.position })
end
WT.dprint("End of function extinguish_fire(%s).", { WT.print_name_id(entity) })
end
------------------------------------------------------------------------------------
-- Remove fire dummies without fire from surfaces
local function remove_fire_dummies_from_surface(event)
WT.dprint("Entered function remove_fire_dummies_from_surface (tick %g).", { event and event.tick })
local dummies = {}
for s, surface in pairs(game.surfaces) do
dummies = surface.find_entities_filtered({
name = WT.fire_dummy_name,
type = WT.dummy_type
})
WT.dprint("Found %g dummies on surface \"%s\".", {#dummies, s})
-- Destroy all dummies that are not in our storage lists!
for d, dummy in pairs(dummies) do
if not storage.fire_dummies[dummy.unit_number] then
WT.dprint("%s is not in storage dummy list!", { WT.print_name_id(dummy) })
dummy.destroy()
WT.dprint("Removed %s!", { WT.print_name_id(dummy) })
end
end
WT.dprint("Done!")
end
WT.dprint("End of function remove_fire_dummies_from_surface (tick %g).", { event and event.tick })
return
end
------------------------------------------------------------------------------------
-- Remove fire dummies without fire from tables
local function remove_fire_dummies_from_table(event)
WT.dprint("Entered function remove_fire_dummies_from_table (tick %g).",
{ event and event.tick })
local dummies = {}
local cnt
WT.dprint("Found %g dummies in storage.fire_dummies.", { table_size(storage.fire_dummies) })
-- Destroy all dummies in our storage list that don't mark fires!
for d, dummy in pairs(storage.fire_dummies) do
if not dummy_marks_fire(dummy) then
WT.dprint("%s is in our table but doesn't mark fire.", { WT.print_name_id(dummy) })
remove_fire_and_dummy("dummy", dummy.dummy_id)
WT.dprint("Removed %s!", { WT.print_name_id(dummy) })
end
end
WT.dprint("End of function remove_fire_dummies_from_table (tick %g).",
{ event and event.tick })
return
end
------------------------------------------------------------------------------------
-- on_built
local function on_built(event)
WT.dprint("Entered function on_built(%s).", { WT.print_name_id(event.created_entity) })
local entity = event.created_entity or event.entity
--~ local turret_data
if entity and entity.valid then
-- Turret was built
if entity.type == WT.turret_type and WT.turret_names[entity.name] then
storage.WT_turrets[entity.unit_number] = {}
local area = (entity.name ~= WT.extinguisher_turret_name) and
WT.get_turret_area(entity) or nil
-- range may already be set in WT.get_turret_area()!
local range = storage.WT_turrets[entity.unit_number].range
WT.force_relations[entity.force.name] = WT.force_relations[entity.force] or
WT.get_enemy_forces(entity.force)
--~ WT.show("entity.force", entity.force.name)
WT.dprint("WT.force_relations[%s]: %s", {entity.force.name, WT.force_relations[entity.force.name]})
local next_tick = event.tick
local id = entity.unit_number
storage.WT_turrets[id] = {
entity = entity,
-- We want to check the new turret as soon as possible!
tick = next_tick,
-- Set the rectangular area (2*range x range) in the direction the
-- turret is facing. It will be used when finding enemies or fires.
-- (Recalculate when turret is rotated or moved.)
area = area,
id = id,
min_range = entity.prototype.attack_parameters.min_range,
range = range or entity.prototype.attack_parameters.range,
enemy_forces = WT.force_relations[entity.force.name],
}
storage.turret_ticks[next_tick] = storage.turret_ticks[next_tick] or {}
storage.turret_ticks[next_tick][id] = true
WT.dprint("storage.WT_turrets[%s]: %s", {entity.unit_number, storage.WT_turrets[entity.unit_number]})
end
end
WT.dprint("End of function on_built(%s).", { WT.print_name_id(event.created_entity) })
end
------------------------------------------------------------------------------------
-- on_remove
local function remove_turret(turret_id)
WT.dprint("Entered function remove_turret(%s)", {turret_id})
local turret = turret_id and storage.WT_turrets[turret_id]
--~ storage.WT_turrets[entity.unit_number] = nil
if turret then
WT.show("Removing", WT.print_name_id(turret.entity))
storage.turret_ticks[turret.tick][turret_id] = nil
if not next(storage.turret_ticks[turret.tick]) then
storage.turret_ticks[turret.tick] = nil
end
storage.WT_turrets[turret_id] = nil
end
WT.dprint("End of function remove_turret(%s)", { WT.print_name_id(turret) })
end
------------------------------------------------------------------------------------
-- on_player_rotated_entity
local function on_player_rotated_entity(event)
WT.dprint("Entered function on_player_rotated_entity(%s).", { WT.print_name_id(event.entity) })
local entity = event.entity
if WT.is_WT_turret(entity) and not WT.is_WT_turret_name(WT.extinguisher_turret_name) then
WT.dprint("%s has been moved: recalculating area!", { WT.print_name(entity) })
storage.WT_turrets[entity.unit_number].area = WT.get_turret_area(entity)
end
WT.dprint("End of function on_player_rotated_entity(%s).", { WT.print_name_id(event.entity) })
end
------------------------------------------------------------------------------------
-- Picker Dollies: on_moved
local function on_moved(event)
WT.dprint("Entered function on_moved(%s).", { WT.print_name_id(event.moved_entity) })
local entity = event.moved_entity
if WT.is_WT_turret(entity) then
WT.dprint("%s has been moved: recalculating area!", { WT.print_name(entity) })
storage.WT_turrets[entity.unit_number].area = WT.get_turret_area(entity)
end
WT.dprint("End of function on_moved(%s).", { WT.print_name_id(event.moved_entity) })
end
------------------------------------------------------------------------------------
-- Remove slow-down effect for friendly entities
local function remove_slowdown_sticker(event)
WT.dprint("Entered event script for events.on_trigger_created_entity(%s).", { event }, "line")
local sticker = event.entity
local turret = event.source
--~ WT.show("sticker", WT.print_name_id(sticker))
--~ WT.show("turret", WT.print_name_id(turret))
if sticker and sticker.name == WT.slowdown_sticker_name and
turret and WT.is_WT_turret(turret) then
local affected = sticker.sticked_to
local turret_force = turret.force and turret.force.name
--~ WT.show("Sticker is attached to", affected and affected.name)
--~ WT.show("Stickers that affect the entity", affected and affected.stickers)
--~ WT.show("Force of affected entity", affected and affected.force and affected.force.name)
--~ WT.show("Force of turret", turret and turret.force and turret.force.name)
if affected and affected.force and affected.force.name == turret_force then
WT.dprint("%s affected %s with %s -- removing sticker!", {
WT.print_name_id(turret), WT.print_name_id(affected), sticker.name
})
sticker.destroy()
end
end
WT.dprint("End of event script for events.on_trigger_created_entity(%s).", { event }, "line")
end
------------------------------------------------------------------------------------
-- Init
local function init()
WT.dprint("Entered function init().")
if WT.debug_in_log then
helpers.check_prototype_translations()
end
--~ local dummy = game.entity_prototypes[WT.fire_dummy_name]
--~ WT.dprint("Fire dummy name: %s\tresistances: %s\thealth: %s\ttrigger_target_mask: %s", {dummy.name, dummy.resistances, dummy.max_health, dummy.trigger_target_mask})
------------------------------------------------------------------------------------
-- Enable debugging if necessary
WT.debug_in_log = settings.global["WT-debug_to_log"].value
------------------------------------------------------------------------------------
-- Initialize storage tables
storage = storage or {}
storage.WT_turrets = storage.WT_turrets or {}
storage.turret_ticks = storage.turret_ticks or {}
--~ WT.show("storage.WT_turrets", storage.WT_turrets)
storage.fires = storage.fires or {}
--~ WT.show("storage.fires", storage.fires)
storage.fire_dummies = storage.fire_dummies or {}
--~ WT.show("storage.fire_dummies", storage.fire_dummies)
storage.dummy_positions = storage.dummy_positions or {}
storage.acids = WT.make_name_list(ignore_or_acid.get_acid())
WT.acid_types = ignore_or_acid.get_acid()
storage.enemy_types = WT.enemies("get_list")
storage.enemy_healing = WT.enemy_healing(storage.enemy_types)
storage.ignore_fires = ignore_or_acid.get_ignorelist()
WT.show("storage.acids", {storage.acids})
WT.show("WT.acid_types", {WT.acid_types})
WT.show("storage.ignore_fires", {storage.ignore_fires})
WT.show("storage.enemy_types", {storage.enemy_types})
WT.show("storage.enemy_healing", {storage.enemy_healing})
WT.show("Number of turrets", table_size(storage.WT_turrets))
------------------------------------------------------------------------------------
-- Make sure our recipe is enabled if it should be
for f, force in pairs(game.forces) do
if force.technologies["gun-turret"].researched then
force.technologies["gun-turret"].researched = false
force.technologies["gun-turret"].researched = true
WT.dprint("Reset technology \"turrets\" for force %s.", { f })
end
end
WT.show("Number of turrets", table_size(storage.WT_turrets))
------------------------------------------------------------------------------------
-- Forces
-- Create force for fire dummy if it doesn't exist yet.
if not game.forces[WT.dummy_force] then
game.create_force(WT.dummy_force)
end
WT.show("Number of turrets", table_size(storage.WT_turrets))
WT.force_relations = {}
-- Set relations of dummy force towards other forces
for name, force in pairs(game.forces) do
WT.show(tostring(name), table_size(force.players))
-- Ignore dummy force
if force.name ~= WT.dummy_force then
-- If force has players, make it an enemy of fire dummies
if table_size(force.players) > 0 then
force.set_friend(WT.dummy_force, false)
force.set_cease_fire(WT.dummy_force, false)
-- Store force relations of this force
WT.force_relations[force.name] = WT.get_enemy_forces(force)
-- Forces without players are neutral to fire dummies
else
force.set_friend(WT.dummy_force, false)
force.set_cease_fire(WT.dummy_force, true)
end
end
WT.show(tostring(name .. " (friend)"), force.get_friend(WT.dummy_force))
WT.show(tostring(name .. " (cease_fire)"), force.get_cease_fire(WT.dummy_force))
end
-- Set enemy forces water turrets should target. This will allow for easier
-- filtering when searching the surface for enemies.
for t, turret in pairs(storage.WT_turrets) do
WT.show("t", t)
WT.show("turret.entity", turret.entity and turret.entity.valid)
if turret.entity.valid then
WT.get_turret_enemies(turret.entity)
else
storage.WT_turrets[t] = nil
WT.dprint("Removed invalid turret %s!", {t})
end
end
WT.show("List of forces with enemy forces", WT.force_relations)
WT.show("Turrets", storage.WT_turrets)
WT.show("Number of turrets", table_size(storage.WT_turrets))
-- Add event handler for "Picker Dollies"
if remote.interfaces["PickerDollies"] and
remote.interfaces["PickerDollies"]["dolly_moved_entity_id"] then
script.on_event(remote.call("PickerDollies", "dolly_moved_entity_id"), function(event)
WT.dprint("Entered event script for dolly_moved_entity_id(%s).", { event }, "line")
on_moved(event)
WT.dprint("End of event script for dolly_moved_entity_id(%s).", { event }, "line")
end)
log("Registered handler for \"dolly_moved_entity_id\" from \"PickerDollies\".")
end
WT.dprint("End of function init().")
end
------------------------------------------------------------------------------------
-- on_load
local function on_load()
log("Entered function on_load().")
-- Turn debugging on or off
WT.debug_in_log = settings.storage["WT-debug_to_log"].value
-- Compatibility with "Picker Dollies" -- add event handler
if remote.interfaces["PickerDollies"] and
remote.interfaces["PickerDollies"]["dolly_moved_entity_id"] then
script.on_event(remote.call("PickerDollies", "dolly_moved_entity_id"), on_moved)
WT.dprint("Registered handler for \"dolly_moved_entity_id\" from \"PickerDollies\".")
end
log("End of function on_load().")
end
------------------------------------------------------------------------------------
-- ENTITY DAMAGED
local function on_entity_damaged(event)
WT.dprint("Entered function on_entity_damaged(%s).", { WT.print_name_id(event.entity) })
--~ WT.show("event.cause.name", event.cause and event.cause.name)
--~ WT.show("event.entity.name", event.entity and event.entity.name)
--~ WT.show("event.entity.unit_number", event.entity and event.entity.unit_number)
WT.show("event.cause", WT.print_name_id(event.cause))
WT.show("event.entity", WT.print_name_id(event.entity))
WT.show("event.entity.health", event.entity and event.entity.health)
--WT.show("event.entity.prototype.max_health", event.entity and event.entity.prototype.max_health)
WT.show("event.damage_type.name", event.damage_type and event.damage_type.name)
WT.show("event.final_damage_amount", event.final_damage_amount)
WT.show("event.final_health", event.final_health)
WT.show("event.force", event.force.name)
WT.show("event", event)
------------------------------------------------------------------------------------
-- Fire/acid dummy was attacked -- remove it immediately if there is no fires/acid
-- in its location!!
local entity = event.entity
local turret = event.cause
if WT.is_WT_dummy(entity) then
if not dummy_marks_fire(entity) then
WT.dprint("Removing %s because it marks no fire.", { WT.print_name_id(entity) })
remove_fire_and_dummy("dummy", entity.unit_number)
WT.dprint("Done.")
-- This may be useful if we have another turret that will always and only attack
-- fires. (Scripted retargetting is needed only for water turrets!)
if turret and (turret.name == WT.water_turret_name) and
WT.waterturret_priority ~= "default" then
target_enemy_or_fire(turret)
end
end
return
end
------------------------------------------------------------------------------------
-- Return if we didn't do the damage -- shouldn't be necessary because of event
-- filtering by damage type, but let's just play it safe!
if not WT.is_WT_turret(turret) then
WT.dprint("Leaving function on_entity_damaged(%s) early -- damage was caused by %s!", {
WT.print_name(event.entity), (turret and WT.print_name_id(turret) or "something")
})
return
end
local ammo = turret.fluidbox[1]
local damage = event.final_damage_amount
local damage_type = event.damage_type
------------------------------------------------------------------------------------
-- We damaged something that doesn't belong to us!
--~ WT.show("turret.force", turret.force.name)
--~ WT.show("entity.force", entity.force.name)
if turret.force ~= entity.force then
-- Our turrets do additional damage to dummies
if WT.is_WT_dummy(entity) and
-- Fire dummies are only vulnerable to water and fire extinguisher fluid
(
entity.name == WT.fire_dummy_name and
(damage_type == WT.water_damage_name or damage_type == WT.fire_ex_damage_name)
) or
-- Acid dummies are also vulnerable against steam
--~ (
--~ entity.name == WT.acid_dummy_name and (
--~ damage_type == WT.water_damage_name or
--~ damage_type == WT.fire_ex_damage_name or
--~ damage_type == WT.steam_damage_name
--~ )
entity.name == WT.acid_dummy_name then
entity.health = entity.health - 0.5
end
-- Modify steam damage according to its temperature (base temperature is 165 °C)
-- Applying increased damage only makes sense if hot steam was used and
-- if the damaged entity survived
--~ if ammo and ammo.name == "steam" and ammo.temperature ~= 165 and entity.health > 0 then
--~ if ammo and ammo.name == "steam" and ammo.temperature ~= 165 then
--~ WT.dprint("Adjusting damage and entity health for steam temperature %g (factor %s).", {
--~ ammo.temperature, ammo.temperature / 165
--~ })
--~ -- Temporarily restore health because base damage has already been applied
--~ entity.health = entity.health + damage
--~ -- Calculate increased damage
--~ damage = damage * ammo.temperature / 165
--~ -- Subtract increased damage from health
--~ entity.health = entity.health - damage
--~ WT.show("entity.health after adjusting steam damage", entity.health)
--~ -- Damage from water and steam is so low that it may be instantly restored if
--~ -- the damaged entity has healing_per_tick. Not only would this cancel out all
--~ -- damage, it will also prevent turrets to switching to a new target. Therefore,
--~ -- we cancel out the healing effect.
--~ entity.health = entity.health - storage.enemy_healing[entity.type][entity.name]
--~ end