-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
control.lua
1287 lines (1120 loc) · 46.1 KB
/
control.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
-- Copyright (C) 2022 veden
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
-- imports
local ChunkPropertyUtils = require("libs/ChunkPropertyUtils")
local UnitUtils = require("libs/UnitUtils")
local BaseUtils = require("libs/BaseUtils")
local Processor = require("libs/Processor")
local Constants = require("libs/Constants")
local MapUtils = require("libs/MapUtils")
local Squad = require("libs/Squad")
local tests = require("tests")
local ChunkUtils = require("libs/ChunkUtils")
local Upgrade = require("libs/Upgrade")
local Utils = require("libs/Utils")
-- Constants
local FACTION_SET = Constants.FACTION_SET
local ENEMY_ALIGNMENT_LOOKUP = Constants.ENEMY_ALIGNMENT_LOOKUP
local VANILLA_ENTITY_TYPE_LOOKUP = Constants.VANILLA_ENTITY_TYPE_LOOKUP
local ENTITY_SKIP_COUNT_LOOKUP = Constants.ENTITY_SKIP_COUNT_LOOKUP
local BUILDING_HIVE_TYPE_LOOKUP = Constants.BUILDING_HIVE_TYPE_LOOKUP
local TICKS_A_MINUTE = Constants.TICKS_A_MINUTE
local COMMAND_TIMEOUT = Constants.COMMAND_TIMEOUT
local RECOVER_NEST_COST = Constants.RECOVER_NEST_COST
local RECOVER_WORM_COST = Constants.RECOVER_WORM_COST
local RETREAT_GRAB_RADIUS = Constants.RETREAT_GRAB_RADIUS
local RETREAT_SPAWNER_GRAB_RADIUS = Constants.RETREAT_SPAWNER_GRAB_RADIUS
local BASE_PHEROMONE = Constants.BASE_PHEROMONE
local PLAYER_PHEROMONE = Constants.PLAYER_PHEROMONE
local UNIT_DEATH_POINT_COST = Constants.UNIT_DEATH_POINT_COST
local SETTLE_CLOUD_WARMUP = Constants.SETTLE_CLOUD_WARMUP
-- imported functions
local isMember = Utils.isMember
local split = Utils.split
local planning = BaseUtils.planning
local addExcludeSurface = MapUtils.addExcludedSurface
local removeExcludeSurface = MapUtils.removeExcludedSurface
local setPointAreaInQuery = Utils.setPointAreaInQuery
local nextMap = MapUtils.nextMap
local processClouds = Processor.processClouds
local prepMap = MapUtils.prepMap
local activateMap = MapUtils.activateMap
local findBaseInitialAlignment = BaseUtils.findBaseInitialAlignment
local processBaseAIs = BaseUtils.processBaseAIs
local registerEnemyBaseStructure = ChunkUtils.registerEnemyBaseStructure
local queueGeneratedChunk = MapUtils.queueGeneratedChunk
local isRampantSetting = Utils.isRampantSetting
local processPendingUpgrades = Processor.processPendingUpgrades
local canMigrate = BaseUtils.canMigrate
local squadDispatch = Squad.squadDispatch
local cleanUpMapTables = Processor.cleanUpMapTables
local positionToChunkXY = MapUtils.positionToChunkXY
local processVengence = Processor.processVengence
local processAttackWaves = Processor.processAttackWaves
local disperseVictoryScent = MapUtils.disperseVictoryScent
local getChunkByPosition = MapUtils.getChunkByPosition
local removeChunkFromMap = MapUtils.removeChunkFromMap
local getChunkByXY = MapUtils.getChunkByXY
local entityForPassScan = ChunkUtils.entityForPassScan
local processPendingChunks = Processor.processPendingChunks
local processScanChunks = Processor.processScanChunks
local processMap = Processor.processMap
local processPlayers = Processor.processPlayers
local scanEnemyMap = Processor.scanEnemyMap
local scanPlayerMap = Processor.scanPlayerMap
local scanResourceMap = Processor.scanResourceMap
local processNests = Processor.processNests
local processHives = Processor.processHives
local cleanHivesData = Processor.cleanHivesData
local rallyUnits = Squad.rallyUnits
local recycleBases = BaseUtils.recycleBases
local deathScent = MapUtils.deathScent
local victoryScent = MapUtils.victoryScent
local createSquad = Squad.createSquad
local createBase = BaseUtils.createBase
local findNearbyBaseByPosition = ChunkPropertyUtils.findNearbyBaseByPosition
local findNearbyBase = ChunkPropertyUtils.findNearbyBase
local removeDrainPylons = ChunkPropertyUtils.removeDrainPylons
local getDrainPylonPair = ChunkPropertyUtils.getDrainPylonPair
local createDrainPylon = UnitUtils.createDrainPylon
local decompressSquad = Squad.decompressSquad
local isDrained = ChunkPropertyUtils.isDrained
local setDrainedTick = ChunkPropertyUtils.setDrainedTick
local getCombinedDeathGeneratorRating = ChunkPropertyUtils.getCombinedDeathGeneratorRating
local retreatUnits = Squad.retreatUnits
local accountPlayerEntity = ChunkUtils.accountPlayerEntity
local unregisterEnemyBaseStructure = ChunkUtils.unregisterEnemyBaseStructure
local makeImmortalEntity = ChunkUtils.makeImmortalEntity
local registerResource = ChunkUtils.registerResource
local unregisterResource = ChunkUtils.unregisterResource
local cleanSquads = Squad.cleanSquads
local queueUpgrade = BaseUtils.queueUpgrade
local modifyBaseUnitPoints = BaseUtils.modifyBaseUnitPoints
local tRemove = table.remove
local sFind = string.find
local mRandom = math.random
-- local references to global
local Universe -- manages the chunks that make up the game Universe
-- hook functions
local function onIonCannonFired(event)
--[[
event.force, event.surface, event.player_index, event.position, event.radius
--]]
local map = Universe.maps[event.surface.index]
if not map then
return
end
local chunk = getChunkByPosition(map, event.position)
if (chunk ~= -1) then
local base = findNearbyBase(chunk)
if base then
base.ionCannonBlasts = base.ionCannonBlasts + 1
rallyUnits(chunk, event.tick)
modifyBaseUnitPoints(base, 4000, "Ion Cannon")
end
end
end
local function onAbandonedRuins(event)
local entity = event.entity
if entity.valid and (entity.force.name ~= "enemy") then
local map = Universe.maps[entity.surface.index]
if not map then
return
end
accountPlayerEntity(entity, map, true)
end
end
local function hookEvents()
if settings.startup["ion-cannon-radius"] ~= nil then
script.on_event(remote.call("orbital_ion_cannon", "on_ion_cannon_fired"),
onIonCannonFired)
end
if settings.global["AbandonedRuins-set"] ~= nil then
script.on_event(remote.call("AbandonedRuins", "get_on_entity_force_changed_event"),
onAbandonedRuins)
end
end
local function initializeLibraries(addProperties)
Upgrade.init(Universe)
if addProperties then
Upgrade.addUniverseProperties()
end
BaseUtils.init(Universe)
Squad.init(Universe)
BaseUtils.init(Universe)
Processor.init(Universe)
MapUtils.init(Universe)
ChunkPropertyUtils.init(Universe, MapUtils)
ChunkUtils.init(Universe)
end
local function onLoad()
Universe = global.universe
initializeLibraries()
hookEvents()
end
local function onChunkGenerated(event)
-- queue generated chunk for delayed processing, queuing is required because
-- some mods (RSO) mess with chunk as they are generated, which messes up the
-- scoring.
queueGeneratedChunk(event)
end
local function onModSettingsChange(event)
if not isRampantSetting(event.setting) then
return
end
Universe.safeEntities["curved-rail"] = settings.global["rampant--safeBuildings-curvedRail"].value
Universe.safeEntities["straight-rail"] = settings.global["rampant--safeBuildings-straightRail"].value
Universe.safeEntities["rail-signal"] = settings.global["rampant--safeBuildings-railSignals"].value
Universe.safeEntities["rail-chain-signal"] = settings.global["rampant--safeBuildings-railChainSignals"].value
Universe.safeEntities["train-stop"] = settings.global["rampant--safeBuildings-trainStops"].value
Universe.safeEntities["lamp"] = settings.global["rampant--safeBuildings-lamps"].value
Universe.safeEntities["big-electric-pole"] = settings.global["rampant--safeBuildings-bigElectricPole"].value
Universe.safeEntities["big-electric-pole"] = Universe.safeEntities["big-electric-pole"]
Universe.safeEntities["big-electric-pole-2"] = Universe.safeEntities["big-electric-pole"]
Universe.safeEntities["big-electric-pole-3"] = Universe.safeEntities["big-electric-pole"]
Universe.safeEntities["big-electric-pole-4"] = Universe.safeEntities["big-electric-pole"]
Universe.safeEntities["lighted-big-electric-pole-4"] = Universe.safeEntities["big-electric-pole"]
Universe.safeEntities["lighted-big-electric-pole-3"] = Universe.safeEntities["big-electric-pole"]
Universe.safeEntities["lighted-big-electric-pole-2"] = Universe.safeEntities["big-electric-pole"]
Universe.safeEntities["lighted-big-electric-pole"] = Universe.safeEntities["big-electric-pole"]
Universe["temperamentRateModifier"] = settings.global["rampant--temperamentRateModifier"].value
Universe["baseDistanceModifier"] = settings.global["rampant--baseDistanceModifier"].value
Universe["printBaseAdaptation"] = settings.global["rampant--printBaseAdaptation"].value
Universe["adaptationModifier"] = settings.global["rampant--adaptationModifier"].value
Universe["raidAIToggle"] = settings.global["rampant--raidAIToggle"].value
Universe["siegeAIToggle"] = settings.global["rampant--siegeAIToggle"].value
Universe["attackPlayerThreshold"] = settings.global["rampant--attackPlayerThreshold"].value
Universe["attackUsePlayer"] = settings.global["rampant--attackWaveGenerationUsePlayerProximity"].value
Universe["attackWaveMaxSize"] = settings.global["rampant--attackWaveMaxSize"].value
Universe["aiNocturnalMode"] = settings.global["rampant--permanentNocturnal"].value
Universe["aiPointsScaler"] = settings.global["rampant--aiPointsScaler"].value
Universe["aiPointsPrintGainsToChat"] = settings.global["rampant--aiPointsPrintGainsToChat"].value
Universe["aiPointsPrintSpendingToChat"] = settings.global["rampant--aiPointsPrintSpendingToChat"].value
Universe["printBaseUpgrades"] = settings.global["rampant--printBaseUpgrades"].value
Universe["PRINT_BASE_SETTLING"] = settings.global["rampant--printBaseSettling"].value
Universe["enabledMigration"] = Universe.expansion and settings.global["rampant--enableMigration"].value
Universe["peacefulAIToggle"] = settings.global["rampant--peacefulAIToggle"].value
Universe["printAIStateChanges"] = settings.global["rampant--printAIStateChanges"].value
Universe["debugTemperament"] = settings.global["rampant--debugTemperament"].value
Universe["legacyChunkScanning"] = settings.global["rampant--legacyChunkScanning"].value
Universe["enabledPurpleSettlerCloud"] = settings.global["rampant--enabledPurpleSettlerCloud"].value
Universe["squadCompressionThreshold"] = settings.global["rampant--squadCompressionThreshold"].value
Universe["AI_MAX_SQUAD_COUNT"] = settings.global["rampant--maxNumberOfSquads"].value
Universe["AI_MAX_BUILDER_COUNT"] = settings.global["rampant--maxNumberOfBuilders"].value
Universe["AI_MAX_VANILLA_SQUAD_COUNT"] = Universe["AI_MAX_SQUAD_COUNT"] * 0.65
Universe["AI_MAX_VANILLA_BUILDER_COUNT"] = Universe["AI_MAX_BUILDER_COUNT"] * 0.65
Universe["MAX_BASE_MUTATIONS"] = settings.global["rampant--max-base-mutations"].value
Universe["MAX_BASE_ALIGNMENT_HISTORY"] = settings.global["rampant--maxBaseAlignmentHistory"].value
Universe["initialPeaceTime"] =
(settings.global["rampant--initialPeaceTime"].value * TICKS_A_MINUTE) + Universe.modAddedTick
Universe["printAwakenMessage"] = settings.global["rampant--printAwakenMessage"].value
Universe["minimumAdaptationEvolution"] = settings.global["rampant--minimumAdaptationEvolution"].value
return true
end
local function onConfigChanged()
game.print("Rampant - Version 3.3.3")
initializeLibraries(true)
Upgrade.attempt()
onModSettingsChange({setting="rampant--"})
Universe["NEW_ENEMIES"] = settings.startup["rampant--newEnemies"].value
-- not a completed implementation needs if checks to use all forces
-- both in the data stage, commands, and if then logic
local enemyForces = {
["enemy"]=true
}
local npcForces = {
["neutral"]=true
}
if game.active_mods["AbandonedRuins"] then
npcForces["AbandonedRuins:enemy"] = true
end
Upgrade.setCommandForces(npcForces, enemyForces)
for _,surface in pairs(game.surfaces) do
if not Universe.maps[surface.index] then
prepMap(surface)
end
end
if not Universe.ranIncompatibleMessage and Universe.newEnemies and
(game.active_mods["bobenemies"] or game.active_mods["Natural_Evolution_Enemies"]) then
Universe.ranIncompatibleMessage = true
game.print({"description.rampant-bobs-nee-newEnemies"})
end
end
local function onEnemyBaseBuild(entity, tick)
local map = Universe.maps[entity.surface.index]
if not map then
return
end
local chunk = getChunkByPosition(map, entity.position)
if (chunk ~= -1) then
local base = findNearbyBase(chunk)
if not base then
base = createBase(map,
chunk,
tick)
end
if Universe.NEW_ENEMIES then
if VANILLA_ENTITY_TYPE_LOOKUP[entity.name] then
queueUpgrade(entity,
base,
nil,
true)
else
registerEnemyBaseStructure(entity, base, tick, true)
end
else
registerEnemyBaseStructure(entity, base, tick)
end
else
local x,y = positionToChunkXY(entity.position)
onChunkGenerated({
surface = entity.surface,
tick = tick,
area = {
left_top = {
x = x,
y = y
}
}
})
end
end
local function onBuild(event)
local entity = event.created_entity or event.entity
if entity.valid then
local entityForceName = entity.force.name
if entityForceName == "enemy" then
onEnemyBaseBuild(entity, event.tick)
else
local map = Universe.maps[entity.surface.index]
if not map then
return
end
if entity.type == "resource" then
registerResource(entity, map)
else
accountPlayerEntity(entity, map, true)
if Universe.safeEntities[entity.type] or Universe.safeEntities[entity.name] then
entity.destructible = false
end
end
end
end
end
local function onMine(event)
local entity = event.entity
if entity.valid then
local map = Universe.maps[entity.surface.index]
if not map then
return
end
accountPlayerEntity(entity, map, false)
end
end
local function onDeath(event)
local entity = event.entity
if not entity.valid then
return
end
local surface = entity.surface
local map = Universe.maps[surface.index]
if not map then
return
end
local entityForceName = entity.force.name
if (entityForceName == "neutral") then
if (entity.name == "cliff") then
entityForPassScan(map, entity)
end
return
end
local cause = event.cause
local causedByEnemyForce = event.force and (event.force.name == "enemy")
local tick = event.tick
local entityType = entity.type
local entityPosition = entity.position
local damageTypeName = event.damage_type and event.damage_type.name
local chunk = getChunkByPosition(map, entityPosition)
local base
local squad
if entityForceName == "enemy" then
if entityType ~= "unit" then
if getDrainPylonPair(map, entity.unit_number) then
removeDrainPylons(map, entity.unit_number)
else
unregisterEnemyBaseStructure(map, entity, damageTypeName)
end
else
local group = entity.unit_group
if group and group.valid then
squad = Universe.groupNumberToSquad[group.group_number]
if squad then
decompressSquad(squad, tick)
if damageTypeName then
base = squad.base
end
end
end
end
if (chunk ~= -1) then
if not base then
base = findNearbyBase(chunk)
end
local artilleryBlast = (cause and ((cause.type == "artillery-wagon") or (cause.type == "artillery-turret")))
if (entityType == "unit") and not ENTITY_SKIP_COUNT_LOOKUP[entity.name] then
deathScent(chunk)
if base then
base.lostEnemyUnits = base.lostEnemyUnits + 1
if damageTypeName then
base.damagedBy[damageTypeName] = (base.damagedBy[damageTypeName] or 0) + 0.01
base.deathEvents = base.deathEvents + 1
end
if base.lostEnemyUnits % 20 == 0 then
modifyBaseUnitPoints(base, -(20*UNIT_DEATH_POINT_COST), "20 Units Lost")
end
if (Universe.random() < Universe.rallyThreshold) and not surface.peaceful_mode then
rallyUnits(chunk, tick)
end
if artilleryBlast then
base.artilleryBlasts = base.artilleryBlasts + 1
end
end
if (getCombinedDeathGeneratorRating(chunk) < Universe.retreatThreshold) and cause and cause.valid and cause.get_health_ratio() ~= nil then
retreatUnits(chunk,
cause,
tick,
squad,
(artilleryBlast and RETREAT_SPAWNER_GRAB_RADIUS) or RETREAT_GRAB_RADIUS)
end
elseif BUILDING_HIVE_TYPE_LOOKUP[entity.name] or
(entityType == "unit-spawner") or
(entityType == "turret")
then
deathScent(chunk, true)
if base then
if (entityType == "unit-spawner") then
modifyBaseUnitPoints(base, RECOVER_NEST_COST, "Nest Lost")
elseif (entityType == "turret") then
modifyBaseUnitPoints(base, RECOVER_WORM_COST, "Worm Lost")
end
rallyUnits(chunk, tick)
if artilleryBlast then
base.artilleryBlasts = base.artilleryBlasts + 1
end
end
if cause and cause.valid and cause.get_health_ratio() ~= nil then
retreatUnits(chunk,
cause,
tick,
nil,
RETREAT_SPAWNER_GRAB_RADIUS)
end
end
end
else
local creditNatives = false
if causedByEnemyForce then
creditNatives = true
local drained = false
if chunk ~= -1 then
victoryScent(chunk, entityType)
drained = (entityType == "electric-turret") and isDrained(chunk, tick)
if cause and cause.type == "unit" then
local group = cause.unit_group
if group and group.valid then
squad = Universe.groupNumberToSquad[group.group_number]
if squad then
base = squad.base
end
end
end
if not base then
base = findNearbyBase(chunk)
end
end
if cause or drained then
createDrainPylon(map, cause, entity, entityType)
end
end
if creditNatives and (Universe.safeEntities[entityType] or Universe.safeEntities[entity.name])
then
makeImmortalEntity(surface, entity)
else
accountPlayerEntity(entity, map, false, base)
end
end
end
local function processSurfaceTile(map, position, chunks, tick)
local chunk = getChunkByPosition(map, position)
if (chunk ~= -1) then
Universe.chunkToPassScan[chunk.id] = chunk
else
local x,y = positionToChunkXY(position)
local addMe = true
if chunks then
for ci=1,#chunks do
local c = chunks[ci]
if (c.x == x) and (c.y == y) then
addMe = false
break
end
end
end
if addMe then
local chunkXY = {x=x,y=y}
if chunks then
chunks[#chunks+1] = chunkXY
end
onChunkGenerated({area = { left_top = chunkXY },
tick = tick,
surface = map.surface})
end
end
end
local function onSurfaceTileChange(event)
local surfaceIndex = event.surface_index or (event.robot and event.robot.surface and event.robot.surface.index)
local map = Universe.maps[surfaceIndex]
if not map then
return
end
local chunks = {}
local tiles = event.tiles
if event.tile then
if ((event.tile.name == "landfill") or sFind(event.tile.name, "water")) then
for i=1,#tiles do
processSurfaceTile(map, tiles[i].position, chunks, event.tick)
end
end
else
for i=1,#tiles do
local tile = tiles[i]
if (tile.name == "landfill") or sFind(tile.name, "water") then
processSurfaceTile(map, tiles[i].position, chunks, event.tick)
end
end
end
end
local function onResourceDepleted(event)
local entity = event.entity
if entity.valid then
local map = Universe.maps[entity.surface.index]
if not map then
return
end
unregisterResource(entity, map)
end
end
local function onRobotCliff(event)
local entity = event.robot
if entity.valid then
local map = Universe.maps[entity.surface.index]
if not map then
return
end
if (event.item.name == "cliff-explosives") then
entityForPassScan(map, event.cliff)
end
end
end
local function onUsedCapsule(event)
local surface = game.players[event.player_index].surface
local map = Universe.maps[surface.index]
if not map then
return
end
if (event.item.name == "cliff-explosives") then
setPointAreaInQuery(Universe.oucCliffQuery, event.position, 0.75)
local cliffs = surface.find_entities_filtered(Universe.oucCliffQuery)
for i=1,#cliffs do
entityForPassScan(map, cliffs[i])
end
end
end
local function onRocketLaunch(event)
local entity = event.rocket_silo or event.rocket
if entity.valid then
local map = Universe.maps[entity.surface.index]
if not map then
return
end
local chunk = getChunkByPosition(map, entity.position)
if (chunk ~= -1) then
local base = findNearbyBase(chunk)
if base then
base.rocketLaunched = base.rocketLaunched + 1
modifyBaseUnitPoints(base, 5000, "Rocket Launch")
end
end
end
end
local function onTriggerEntityCreated(event)
if (event.effect_id == "rampant-drain-trigger") then
local entity = event.target_entity
if (entity and entity.valid) then
local map = Universe.maps[event.surface_index]
if not map then
return
end
local chunk = getChunkByPosition(map, entity.position)
if (chunk ~= -1) then
setDrainedTick(chunk, event.tick)
end
elseif (event.target_position) then
local map = Universe.maps[event.surface_index]
if not map then
return
end
local chunk = getChunkByPosition(map, event.target_position)
if (chunk ~= -1) then
setDrainedTick(chunk, event.tick)
end
end
elseif (event.effect_id == "deathLandfillParticle--rampant") then
local map = Universe.maps[event.surface_index]
if not map then
return
end
processSurfaceTile(
map,
event.target_position,
nil,
event.tick
)
end
end
local function onInit()
global.universe = {}
Universe = global.universe
hookEvents()
onConfigChanged()
end
local function onUnitGroupCreated(event)
local group = event.group
if (group.force.name ~= "enemy") then
return
end
local surface = group.surface
local squad
if group.is_script_driven then
return
end
if not Universe.awake then
group.destroy()
return
end
local map = Universe.maps[surface.index]
if not map then
return
end
activateMap(map)
local position = group.position
local chunk = getChunkByPosition(map, position)
local base
if (chunk == -1) then
base = findNearbyBaseByPosition(map, position.x, position.y)
else
base = findNearbyBase(chunk)
end
if not base then
group.destroy()
return
end
if not Universe.aiNocturnalMode then
local settler = canMigrate(base) and
(Universe.builderCount < Universe.AI_MAX_VANILLA_BUILDER_COUNT) and
(Universe.random() < 0.25)
if not settler then
if (Universe.squadCount >= Universe.AI_MAX_VANILLA_SQUAD_COUNT)
or (chunk == -1)
or ((chunk[BASE_PHEROMONE] < 0.0001) and (chunk[PLAYER_PHEROMONE] < 0.0001))
then
group.destroy()
return
end
end
squad = createSquad(nil, map, group, settler, base)
Universe.groupNumberToSquad[group.group_number] = squad
if settler then
Universe.builderCount = Universe.builderCount + 1
else
Universe.squadCount = Universe.squadCount + 1
end
else
if not (surface.darkness > 0.65) then
group.destroy()
return
end
local settler = canMigrate(base) and
(Universe.builderCount < Universe.AI_MAX_VANILLA_BUILDER_COUNT) and
(Universe.random() < 0.25)
if not settler then
if (Universe.squadCount >= Universe.AI_MAX_VANILLA_SQUAD_COUNT)
or (chunk == -1)
or ((chunk[BASE_PHEROMONE] < 0.0001) and (chunk[PLAYER_PHEROMONE] < 0.0001))
then
group.destroy()
return
end
end
squad = createSquad(nil, map, group, settler, base)
Universe.groupNumberToSquad[group.group_number] = squad
if settler then
Universe.builderCount = Universe.builderCount + 1
else
Universe.squadCount = Universe.squadCount + 1
end
end
end
local function onGroupFinishedGathering(event)
local group = event.group
if not group.valid or (group.force.name ~= "enemy") then
return
end
if not Universe.awake then
group.destroy()
return
end
local map = Universe.maps[group.surface.index]
if not map then
return
end
activateMap(map)
local squad = Universe.groupNumberToSquad[group.group_number]
if squad then
if squad.settler then
if (Universe.builderCount <= Universe.AI_MAX_BUILDER_COUNT) then
squadDispatch(squad, event.tick)
else
group.destroy()
end
else
local chunk = getChunkByPosition(map, squad.group.position)
if (chunk ~= -1) and (chunk[BASE_PHEROMONE] < 0.0001) and (chunk[PLAYER_PHEROMONE] < 0.0001) then
group.destroy()
return
end
if (Universe.squadCount <= Universe.AI_MAX_SQUAD_COUNT) then
squadDispatch(squad, event.tick)
else
group.destroy()
end
end
else
local position = group.position
local chunk = getChunkByPosition(map, position)
local base
if chunk == -1 then
base = findNearbyBaseByPosition(map, position.x, position.y)
else
base = findNearbyBase(chunk)
end
if not base then
group.destroy()
return
end
local settler = canMigrate(base) and
(Universe.builderCount < Universe.AI_MAX_VANILLA_BUILDER_COUNT) and
(Universe.random() < 0.25)
if not settler then
if (Universe.squadCount >= Universe.AI_MAX_VANILLA_SQUAD_COUNT)
or (chunk == -1)
or ((chunk[BASE_PHEROMONE] < 0.0001) and (chunk[PLAYER_PHEROMONE] < 0.0001))
then
group.destroy()
return
end
end
squad = createSquad(nil, map, group, settler, base)
Universe.groupNumberToSquad[group.group_number] = squad
if settler then
Universe.builderCount = Universe.builderCount + 1
else
Universe.squadCount = Universe.squadCount + 1
end
squadDispatch(squad, event.tick)
end
end
local function onForceCreated(event)
if Universe.playerForces then
Universe.playerForces[#Universe.playerForces+1] = event.force.name
end
end
local function onForceMerged(event)
for i=#Universe.playerForces,1,-1 do
if (Universe.playerForces[i] == event.source_name) then
tRemove(Universe.playerForces, i)
break
end
end
end
local function onSurfaceCreated(event)
prepMap(game.surfaces[event.surface_index])
end
local function onSurfaceDeleted(event)
local surfaceIndex = event.surface_index
if (Universe.mapIterator == surfaceIndex) then
Universe.mapIterator, Universe.currentMap = next(
Universe.activeMaps,
Universe.mapIterator
)
end
if (Universe.processMapAIIterator == surfaceIndex) then
Universe.processMapAIIterator = nil
end
Universe.maps[surfaceIndex] = nil
end
local function onSurfaceCleared(event)
onSurfaceDeleted(event)
onSurfaceCreated(event)
end
local function onChunkDeleted(event)
local surfaceIndex = event.surface_index
local map = Universe.maps[surfaceIndex]
if map then
local positions = event.positions
for i=1,#positions do
local position = positions[i]
local x = position.x * 32
local y = position.y * 32
local chunk = getChunkByXY(map, x, y)
if chunk ~= -1 then
removeChunkFromMap(map, chunk)
end
end
end
end
local function onBuilderArrived(event)
local builder = event.group
local usingUnit = false
if not (builder and builder.valid) then
builder = event.unit
if not (builder and builder.valid and builder.force.name == "enemy") then
return
end
usingUnit = true
elseif (builder.force.name ~= "enemy") then
return
end
local map = Universe.maps[builder.surface.index]
if not map then
return
end
activateMap(map)
if not usingUnit then
local squad = Universe.groupNumberToSquad[builder.group_number]
squad.commandTick = event.tick + COMMAND_TIMEOUT * 10
end
if Universe.PRINT_BASE_SETTLING then
game.print(map.surface.name.." Settled: [gps=" .. builder.position.x .. "," .. builder.position.y .."]")
end
if Universe.enabledPurpleSettlerCloud then
Universe.eventId = Universe.eventId + 1
Universe.settlePurpleCloud[Universe.eventId] = {
map = map,
group = builder,
tick = event.tick + SETTLE_CLOUD_WARMUP
}
end
end
-- hooks
script.on_event(defines.events.on_tick,
function ()
local gameRef = game
local tick = gameRef.tick
local range = (Universe.legacyChunkScanning and 4) or 3
local pick = tick % range
-- local profiler = game.create_profiler()
local map = nextMap()
if (pick == 0) then
processPendingChunks(tick)
if map then
recycleBases()
end
cleanUpMapTables(tick)
planning(gameRef.forces.enemy.evolution_factor)
processHives(tick)
cleanHivesData()
elseif (pick == 1) then
processPlayers(gameRef.connected_players, tick)
elseif (pick == 2) then
processPendingUpgrades(tick)
processVengence()
disperseVictoryScent()
processAttackWaves()
processClouds(tick)
processScanChunks()
elseif (pick == 3) then
if map then
scanPlayerMap(map)
scanResourceMap(map)
scanEnemyMap(map, tick)