1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-05 22:53:24 +02:00
Rampant/control.lua

1208 lines
47 KiB
Lua
Raw Normal View History

-- imports
2019-10-19 21:13:48 +02:00
local chunkPropertyUtils = require("libs/ChunkPropertyUtils")
2019-02-13 07:50:25 +02:00
local unitUtils = require("libs/UnitUtils")
local baseUtils = require("libs/BaseUtils")
2017-01-20 07:58:36 +02:00
local mapUtils = require("libs/MapUtils")
2019-10-21 06:38:36 +02:00
local mathUtils = require("libs/MathUtils")
2016-08-07 05:38:47 +02:00
local unitGroupUtils = require("libs/UnitGroupUtils")
local chunkProcessor = require("libs/ChunkProcessor")
local mapProcessor = require("libs/MapProcessor")
local constants = require("libs/Constants")
local pheromoneUtils = require("libs/PheromoneUtils")
local squadDefense = require("libs/SquadDefense")
local squadAttack = require("libs/SquadAttack")
local aiAttackWave = require("libs/AIAttackWave")
2016-10-15 02:00:18 +02:00
local aiPlanning = require("libs/AIPlanning")
2016-08-22 00:59:17 +02:00
local tests = require("tests")
2017-11-21 09:27:03 +02:00
local chunkUtils = require("libs/ChunkUtils")
2017-05-19 09:47:24 +02:00
local upgrade = require("Upgrade")
2020-05-17 07:06:55 +02:00
local aiPredicates = require("libs/AIPredicates")
local stringUtils = require("libs/StringUtils")
2020-05-17 07:06:55 +02:00
2016-10-15 02:00:18 +02:00
-- constants
local COMMAND_TIMEOUT = constants.COMMAND_TIMEOUT
2020-05-24 07:13:57 +02:00
local AI_SQUAD_COST = constants.AI_SQUAD_COST
local AI_SETTLER_COST = constants.AI_SETTLER_COST
2020-05-22 21:43:44 +02:00
2019-04-06 05:01:46 +02:00
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
2021-02-20 09:31:36 +02:00
local PROCESS_QUEUE_SIZE = constants.PROCESS_QUEUE_SIZE
2019-02-13 07:50:25 +02:00
local DEFINES_WIRE_TYPE_RED = defines.wire_type.red
local DEFINES_WIRE_TYPE_GREEN = defines.wire_type.green
local ENERGY_THIEF_CONVERSION_TABLE = constants.ENERGY_THIEF_CONVERSION_TABLE
local ENERGY_THIEF_LOOKUP = constants.ENERGY_THIEF_LOOKUP
2017-06-01 03:46:53 +02:00
-- imported functions
2017-01-20 07:58:36 +02:00
local registerEnemyBaseStructure = chunkUtils.registerEnemyBaseStructure
2021-11-26 08:49:28 +02:00
local queueGeneratedChunk = mapUtils.queueGeneratedChunk
local isRampantSetting = stringUtils.isRampantSetting
local processPendingUpgrades = chunkProcessor.processPendingUpgrades
2020-05-17 07:06:55 +02:00
local canMigrate = aiPredicates.canMigrate
2019-02-13 07:50:25 +02:00
local convertTypeToDrainCrystal = unitUtils.convertTypeToDrainCrystal
2020-05-17 07:06:55 +02:00
local squadDispatch = squadAttack.squadDispatch
2018-02-13 09:10:17 +02:00
local cleanUpMapTables = mapProcessor.cleanUpMapTables
2018-01-28 00:46:45 +02:00
local positionToChunkXY = mapUtils.positionToChunkXY
local temperamentPlanner = aiPlanning.temperamentPlanner
2020-05-20 04:37:16 +02:00
local processVengence = mapProcessor.processVengence
local processSpawners = mapProcessor.processSpawners
local processStaticMap = mapProcessor.processStaticMap
2020-05-25 01:41:12 +02:00
local disperseVictoryScent = pheromoneUtils.disperseVictoryScent
2017-01-20 07:58:36 +02:00
local getChunkByPosition = mapUtils.getChunkByPosition
2018-01-02 20:36:23 +02:00
local entityForPassScan = chunkUtils.entityForPassScan
local processPendingChunks = chunkProcessor.processPendingChunks
local processScanChunks = chunkProcessor.processScanChunks
local processMap = mapProcessor.processMap
2016-10-15 02:00:18 +02:00
local processPlayers = mapProcessor.processPlayers
local scanEnemyMap = mapProcessor.scanEnemyMap
local scanPlayerMap = mapProcessor.scanPlayerMap
local scanResourceMap = mapProcessor.scanResourceMap
local processNests = mapProcessor.processNests
2016-10-15 02:00:18 +02:00
local planning = aiPlanning.planning
local rallyUnits = aiAttackWave.rallyUnits
2017-01-20 07:58:36 +02:00
2018-01-26 07:48:12 +02:00
local recycleBases = baseUtils.recycleBases
local deathScent = pheromoneUtils.deathScent
2017-04-22 01:14:04 +02:00
local victoryScent = pheromoneUtils.victoryScent
2020-05-17 07:06:55 +02:00
local createSquad = unitGroupUtils.createSquad
local createBase = baseUtils.createBase
local findNearbyBase = baseUtils.findNearbyBase
2020-04-28 05:41:18 +02:00
local processActiveNests = mapProcessor.processActiveNests
2020-05-20 04:37:16 +02:00
local getDeathGenerator = chunkPropertyUtils.getDeathGenerator
local retreatUnits = squadDefense.retreatUnits
local accountPlayerEntity = chunkUtils.accountPlayerEntity
2017-11-21 09:27:03 +02:00
local unregisterEnemyBaseStructure = chunkUtils.unregisterEnemyBaseStructure
local makeImmortalEntity = chunkUtils.makeImmortalEntity
2019-05-16 07:11:43 +02:00
local registerResource = chunkUtils.registerResource
local unregisterResource = chunkUtils.unregisterResource
2020-10-19 05:33:01 +02:00
local cleanSquads = squadAttack.cleanSquads
2020-05-17 07:06:55 +02:00
2018-01-15 01:10:56 +02:00
local upgradeEntity = baseUtils.upgradeEntity
local rebuildNativeTables = baseUtils.rebuildNativeTables
2016-10-15 02:00:18 +02:00
local mRandom = math.random
2019-05-10 02:46:57 +02:00
local tRemove = table.remove
2020-05-20 04:37:16 +02:00
local sFind = string.find
2016-08-25 02:05:20 +02:00
-- local references to global
2021-02-20 09:31:36 +02:00
local universe -- manages the chunks that make up the game universe
2016-08-25 02:05:20 +02:00
-- hook functions
2017-07-05 00:52:20 +02:00
local function onIonCannonFired(event)
--[[
2019-10-13 22:53:36 +02:00
event.force, event.surface, event.player_index, event.position, event.radius
2017-07-05 00:52:20 +02:00
--]]
2021-02-20 09:31:36 +02:00
local map = universe.maps[event.surface.index]
map.ionCannonBlasts = map.ionCannonBlasts + 1
2021-03-29 07:00:49 +02:00
map.points = map.points + 4000
if universe.aiPointsPrintGainsToChat then
2021-05-01 19:26:05 +02:00
game.print(map.surface.name .. ": Points: +" .. 4000 .. ". [Ion Cannon] Total: " .. string.format("%.2f", map.points))
end
2021-05-01 19:26:05 +02:00
2021-02-20 07:41:30 +02:00
local chunk = getChunkByPosition(map, event.position)
if (chunk ~= -1) then
rallyUnits(chunk, map, event.tick)
2019-02-03 08:01:28 +02:00
end
2017-07-05 00:52:20 +02:00
end
local function hookEvents()
if settings.startup["ion-cannon-radius"] ~= nil then
2019-10-13 22:53:36 +02:00
script.on_event(remote.call("orbital_ion_cannon", "on_ion_cannon_fired"),
onIonCannonFired)
2019-02-03 08:01:28 +02:00
end
2017-07-05 00:52:20 +02:00
end
2016-09-13 00:33:00 +02:00
local function onLoad()
2021-02-20 09:31:36 +02:00
universe = global.universe
2017-07-05 00:52:20 +02:00
hookEvents()
end
2016-09-13 00:33:00 +02:00
local function onChunkGenerated(event)
2021-02-20 07:41:30 +02:00
-- 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.
2021-11-26 08:49:28 +02:00
queueGeneratedChunk(universe, event)
2021-02-20 07:41:30 +02:00
end
local function onModSettingsChange(event)
if not isRampantSetting(event.setting) then
return
2016-09-13 00:33:00 +02:00
end
2021-02-20 07:41:30 +02:00
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"safeBuildings",
settings.global["rampant--safeBuildings"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe.safeEntities,
2021-02-20 07:41:30 +02:00
"curved-rail",
settings.global["rampant--safeBuildings-curvedRail"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe.safeEntities,
2021-02-20 07:41:30 +02:00
"straight-rail",
settings.global["rampant--safeBuildings-straightRail"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe.safeEntities,
2021-02-20 07:41:30 +02:00
"rail-signal",
settings.global["rampant--safeBuildings-railSignals"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe.safeEntities,
2021-02-20 07:41:30 +02:00
"rail-chain-signal",
settings.global["rampant--safeBuildings-railChainSignals"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe.safeEntities,
2021-02-20 07:41:30 +02:00
"train-stop",
settings.global["rampant--safeBuildings-trainStops"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe.safeEntities,
2021-02-20 07:41:30 +02:00
"lamp",
settings.global["rampant--safeBuildings-lamps"].value)
2021-11-25 19:04:52 +02:00
upgrade.compareTable(universe,
"MAX_BASE_MUTATIONS",
settings.global["rampant--max-base-mutations"].value)
2021-02-20 07:41:30 +02:00
2021-02-20 09:31:36 +02:00
local changed, newValue = upgrade.compareTable(universe.safeEntities,
2021-02-20 07:41:30 +02:00
"big-electric-pole",
settings.global["rampant--safeBuildings-bigElectricPole"].value)
2021-02-20 07:41:30 +02:00
if changed then
2021-02-20 09:31:36 +02:00
universe.safeEntities["big-electric-pole"] = newValue
universe.safeEntities["big-electric-pole-2"] = newValue
universe.safeEntities["big-electric-pole-3"] = newValue
universe.safeEntities["big-electric-pole-4"] = newValue
universe.safeEntities["lighted-big-electric-pole-4"] = newValue
universe.safeEntities["lighted-big-electric-pole-3"] = newValue
universe.safeEntities["lighted-big-electric-pole-2"] = newValue
universe.safeEntities["lighted-big-electric-pole"] = newValue
2021-02-20 07:41:30 +02:00
end
2021-02-28 00:50:26 +02:00
upgrade.compareTable(universe,
"temperamentRateModifier",
settings.global["rampant--temperamentRateModifier"].value)
2021-07-26 02:25:14 +02:00
upgrade.compareTable(universe,
"baseDistanceModifier",
settings.global["rampant--baseDistanceModifier"].value)
upgrade.compareTable(universe,
"printBaseAdaptation",
settings.global["rampant--printBaseAdaptation"].value)
2021-07-25 05:23:38 +02:00
upgrade.compareTable(universe,
"adaptationModifier",
settings.global["rampant--adaptationModifier"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"raidAIToggle",
settings.global["rampant--raidAIToggle"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"siegeAIToggle",
settings.global["rampant--siegeAIToggle"].value)
2021-02-20 07:41:30 +02:00
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"attackPlayerThreshold",
settings.global["rampant--attackPlayerThreshold"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"attackUsePlayer",
settings.global["rampant--attackWaveGenerationUsePlayerProximity"].value)
2021-02-20 07:41:30 +02:00
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"attackWaveMaxSize",
settings.global["rampant--attackWaveMaxSize"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"aiNocturnalMode",
settings.global["rampant--permanentNocturnal"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"aiPointsScaler",
settings.global["rampant--aiPointsScaler"].value)
2021-02-20 07:41:30 +02:00
universe.aiPointsPrintGainsToChat = settings.global["rampant--aiPointsPrintGainsToChat"].value
universe.aiPointsPrintSpendingToChat = settings.global["rampant--aiPointsPrintSpendingToChat"].value
universe.printBaseUpgrades = settings.global["rampant--printBaseUpgrades"].value
2021-05-01 19:26:05 +02:00
universe.enabledMigration = universe.expansion and settings.global["rampant--enableMigration"].value
2021-04-16 22:47:43 +02:00
universe.peacefulAIToggle = settings.global["rampant--peacefulAIToggle"].value
universe.printAIStateChanges = settings.global["rampant--printAIStateChanges"].value
2021-04-16 22:47:43 +02:00
universe.debugTemperament = settings.global["rampant--debugTemperament"].value
2021-02-20 07:41:30 +02:00
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"AI_MAX_SQUAD_COUNT",
settings.global["rampant--maxNumberOfSquads"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"AI_MAX_BUILDER_COUNT",
settings.global["rampant--maxNumberOfBuilders"].value)
2021-02-20 07:41:30 +02:00
return true
2016-08-25 02:05:20 +02:00
end
local function prepMap(surface)
surface.print("Rampant - Indexing surface:" .. tostring(surface.index) .. ", please wait.")
2017-06-01 03:46:53 +02:00
2021-02-20 07:41:30 +02:00
local surfaceIndex = surface.index
2021-02-20 09:31:36 +02:00
if not universe.maps then
universe.maps = {}
2021-02-20 07:41:30 +02:00
end
2021-02-20 09:31:36 +02:00
local map = universe.maps[surfaceIndex]
2021-02-20 07:41:30 +02:00
if not map then
map = {}
2021-02-20 09:31:36 +02:00
universe.maps[surfaceIndex] = map
2021-02-20 07:41:30 +02:00
end
map.maxAggressiveGroups = 1
map.sentAggressiveGroups = 0
2021-02-20 07:41:30 +02:00
map.processedChunks = 0
2018-01-14 07:48:21 +02:00
map.processQueue = {}
map.processIndex = 1
map.cleanupIndex = 1
map.scanPlayerIndex = 1
map.scanResourceIndex = 1
map.scanEnemyIndex = 1
map.processStaticIndex = 1
map.outgoingScanWave = true
map.outgoingStaticScanWave = true
2018-01-14 07:48:21 +02:00
2021-11-26 08:49:28 +02:00
map.pendingUpgrades = {}
2020-05-20 04:37:16 +02:00
map.pendingChunks = {}
map.chunkToBase = {}
2018-01-14 07:48:21 +02:00
map.chunkToNests = {}
2019-11-30 02:49:22 +02:00
map.chunkToTurrets = {}
map.chunkToTraps = {}
map.chunkToUtilities = {}
map.chunkToHives = {}
map.chunkToNestIds = {}
map.chunkToHiveIds = {}
map.chunkToTrapIds = {}
map.chunkToTurretIds = {}
map.chunkToUtilityIds = {}
2018-01-14 07:48:21 +02:00
map.chunkToPlayerBase = {}
map.chunkToResource = {}
map.chunkToPlayerCount = {}
map.playerToChunk = {}
map.pendingChunks = {}
2018-02-12 05:21:28 +02:00
2018-01-14 07:48:21 +02:00
map.chunkToPassScan = {}
map.chunkToSquad = {}
2018-02-12 05:21:28 +02:00
map.chunkToRetreats = {}
map.chunkToRallys = {}
2018-09-24 06:56:45 +02:00
map.chunkToPassable = {}
map.chunkToPathRating = {}
map.chunkToDeathGenerator = {}
2019-02-19 02:43:01 +02:00
map.chunkToDrained = {}
2020-05-25 01:41:12 +02:00
map.chunkToVictory = {}
2019-02-20 08:16:43 +02:00
map.chunkToActiveNest = {}
map.chunkToActiveRaidNest = {}
2018-09-24 06:56:45 +02:00
2021-11-26 08:49:28 +02:00
map.pendingUpgradeIterator = nil
2020-05-17 07:06:55 +02:00
map.squadIterator = nil
map.regroupIterator = nil
2020-05-20 04:37:16 +02:00
map.deployVengenceIterator = nil
map.recycleBaseIterator = nil
map.processActiveSpawnerIterator = nil
map.processActiveRaidSpawnerIterator = nil
map.processMigrationIterator = nil
map.processNestIterator = nil
2020-05-25 01:41:12 +02:00
map.victoryScentIterator = nil
2020-05-17 07:06:55 +02:00
2019-12-07 07:57:20 +02:00
map.chunkScanCounts = {}
2019-10-20 22:45:43 +02:00
map.chunkRemovals = {}
2020-04-28 05:41:18 +02:00
map.processActiveNest = {}
map.tickActiveNest = {}
2019-10-20 22:45:43 +02:00
map.emptySquadsOnChunk = {}
2019-05-10 02:46:57 +02:00
2021-02-20 07:41:30 +02:00
map.surface = surface
2021-02-20 09:31:36 +02:00
map.universe = universe
map.vengenceQueue = {}
map.bases = {}
map.baseIndex = 1
map.baseIncrement = 0
map.points = 0
map.state = constants.AI_STATE_AGGRESSIVE
map.baseId = 0
map.squads = nil
map.pendingAttack = nil
map.building = nil
map.evolutionLevel = game.forces.enemy.evolution_factor
map.canAttackTick = 0
map.drainPylons = {}
map.groupNumberToSquad = {}
map.activeRaidNests = 0
map.activeNests = 0
map.destroyPlayerBuildings = 0
map.lostEnemyUnits = 0
map.lostEnemyBuilding = 0
map.rocketLaunched = 0
map.builtEnemyBuilding = 0
map.ionCannonBlasts = 0
map.artilleryBlasts = 0
2021-04-12 05:46:21 +02:00
map.temperament = 0.5
2021-02-20 09:31:36 +02:00
map.temperamentScore = 0
map.stateTick = 0
2021-02-20 07:41:30 +02:00
-- queue all current chunks that wont be generated during play
local tick = game.tick
local position = {0,0}
for chunk in surface.get_chunks() do
local x = chunk.x
local y = chunk.y
position[1] = x
position[2] = y
if surface.is_chunk_generated(position) then
onChunkGenerated({ surface = surface,
tick = tick,
2021-02-20 07:41:30 +02:00
area = { left_top = { x = x * 32,
y = y * 32}}})
2019-05-10 02:46:57 +02:00
end
end
processPendingChunks(map, tick, true)
2019-02-03 08:01:28 +02:00
end
2017-06-24 20:41:57 +02:00
2021-02-20 07:41:30 +02:00
local function onConfigChanged()
local version = upgrade.attempt(universe)
if version then
if not universe then
universe = global.universe
end
2017-06-01 03:46:53 +02:00
end
2019-02-03 08:01:28 +02:00
2021-02-28 00:50:26 +02:00
onModSettingsChange({setting="rampant--"})
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"ENEMY_SEED",
settings.startup["rampant--enemySeed"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"ENEMY_VARIATIONS",
settings.startup["rampant--newEnemyVariations"].value)
2021-02-20 09:31:36 +02:00
upgrade.compareTable(universe,
2021-02-20 07:41:30 +02:00
"NEW_ENEMIES",
settings.startup["rampant--newEnemies"].value)
2018-05-24 02:40:05 +02:00
2021-02-20 09:31:36 +02:00
if universe.NEW_ENEMIES then
rebuildNativeTables(universe, game.create_random_generator(universe.ENEMY_SEED))
2019-12-07 07:57:20 +02:00
else
2021-02-20 09:31:36 +02:00
universe.buildingHiveTypeLookup = {}
universe.buildingHiveTypeLookup["biter-spawner"] = "biter-spawner"
universe.buildingHiveTypeLookup["spitter-spawner"] = "spitter-spawner"
universe.buildingHiveTypeLookup["small-worm-turret"] = "turret"
universe.buildingHiveTypeLookup["medium-worm-turret"] = "turret"
universe.buildingHiveTypeLookup["big-worm-turret"] = "turret"
universe.buildingHiveTypeLookup["behemoth-worm-turret"] = "turret"
end
2021-04-30 07:24:14 +02:00
for _,surface in pairs(game.surfaces) do
if not universe.maps then
universe.maps = {}
end
if not universe.maps[surface.index] then
prepMap(surface)
end
end
end
2016-09-13 00:33:00 +02:00
local function onBuild(event)
local entity = event.created_entity or event.entity
2021-02-20 07:41:30 +02:00
if entity.valid then
2021-02-20 09:31:36 +02:00
local map = universe.maps[entity.surface.index]
2019-05-16 07:11:43 +02:00
if (entity.type == "resource") and (entity.force.name == "neutral") then
registerResource(entity, map)
else
2021-02-20 09:31:36 +02:00
accountPlayerEntity(entity, map, true, false)
if universe.safeBuildings then
if universe.safeEntities[entity.type] or universe.safeEntities[entity.name] then
2019-05-16 07:11:43 +02:00
entity.destructible = false
end
end
2019-10-19 21:13:48 +02:00
end
end
end
2017-08-08 10:19:51 +02:00
local function onMine(event)
local entity = event.entity
2021-02-20 07:41:30 +02:00
if entity.valid then
2021-02-20 09:31:36 +02:00
local map = universe.maps[entity.surface.index]
2019-05-16 07:11:43 +02:00
if (entity.type == "resource") and (entity.force.name == "neutral") then
if (entity.amount == 0) then
unregisterResource(entity, map)
end
2019-10-19 21:13:48 +02:00
else
2021-02-20 09:31:36 +02:00
accountPlayerEntity(entity, map, false, false)
2019-05-16 07:11:43 +02:00
end
end
end
2016-09-13 00:33:00 +02:00
local function onDeath(event)
local entity = event.entity
2020-05-15 22:51:38 +02:00
if entity.valid then
local surface = entity.surface
2021-02-20 09:31:36 +02:00
local map = universe.maps[surface.index]
2021-02-20 07:41:30 +02:00
local entityPosition = entity.position
local chunk = getChunkByPosition(map, entityPosition)
local cause = event.cause
local tick = event.tick
local entityType = entity.type
if (entity.force.name == "enemy") then
local artilleryBlast = (cause and
((cause.type == "artillery-wagon") or (cause.type == "artillery-turret")))
if artilleryBlast then
2021-02-20 09:31:36 +02:00
map.artilleryBlasts = map.artilleryBlasts + 1
2021-02-20 07:41:30 +02:00
end
2019-10-13 22:53:36 +02:00
2021-02-20 07:41:30 +02:00
if (entityType == "unit") then
if (chunk ~= -1) and event.force and (event.force.name ~= "enemy") then
2021-04-30 07:24:14 +02:00
local group = entity.unit_group
if group then
local damageType = event.damage_type
local squad = map.groupNumberToSquad[group.group_number]
2021-07-25 03:20:13 +02:00
if damageType and squad then
local base = squad.base
if base then
2021-07-25 03:20:13 +02:00
local damageTypeName = damageType.name
base.damagedBy[damageTypeName] = (base.damagedBy[damageTypeName] or 0) + 0.01
base.deathEvents = base.deathEvents + 1
end
end
end
2021-05-01 06:25:55 +02:00
2021-02-20 07:41:30 +02:00
-- drop death pheromone where unit died
deathScent(map, chunk)
if (-getDeathGenerator(map, chunk) < -universe.retreatThreshold) and cause and cause.valid then
2021-02-20 07:41:30 +02:00
retreatUnits(chunk,
cause,
map,
tick,
(artilleryBlast and RETREAT_SPAWNER_GRAB_RADIUS) or RETREAT_GRAB_RADIUS)
2019-10-13 22:53:36 +02:00
end
2019-05-01 04:08:21 +02:00
2021-02-20 09:31:36 +02:00
map.lostEnemyUnits = map.lostEnemyUnits + 1
2019-10-13 22:53:36 +02:00
if (mRandom() < universe.rallyThreshold) and not surface.peaceful_mode then
2021-02-14 06:49:54 +02:00
rallyUnits(chunk, map, tick)
2020-05-24 05:47:14 +02:00
end
2021-02-20 07:41:30 +02:00
end
elseif map.drainPylons[entity.unit_number] then
2021-02-20 07:41:30 +02:00
local entityUnitNumber = entity.unit_number
2021-02-20 09:31:36 +02:00
local pair = map.drainPylons[entityUnitNumber]
2021-02-20 07:41:30 +02:00
if pair then
local target = pair[1]
local pole = pair[2]
if target == entity then
2021-02-20 09:31:36 +02:00
map.drainPylons[entityUnitNumber] = nil
2021-02-20 07:41:30 +02:00
if pole.valid then
2021-02-20 09:31:36 +02:00
map.drainPylons[pole.unit_number] = nil
2021-02-20 07:41:30 +02:00
pole.die()
end
elseif (pole == entity) then
2021-02-20 09:31:36 +02:00
map.drainPylons[entityUnitNumber] = nil
2021-02-20 07:41:30 +02:00
if target.valid then
2021-02-20 09:31:36 +02:00
map.drainPylons[target.unit_number] = nil
2021-02-20 07:41:30 +02:00
target.destroy()
2020-05-15 22:51:38 +02:00
end
2019-02-19 02:43:01 +02:00
end
end
else
if event.force and (event.force.name ~= "enemy") then
local rally = false
if (entityType == "unit-spawner") then
map.points = map.points + RECOVER_NEST_COST
if universe.aiPointsPrintGainsToChat then
game.print(map.surface.name .. ": Points: +" .. RECOVER_NEST_COST .. ". [Nest Lost] Total: " .. string.format("%.2f", map.points))
end
rally = true
elseif (entityType == "turret") then
map.points = map.points + RECOVER_WORM_COST
if universe.aiPointsPrintGainsToChat then
game.print(map.surface.name .. ": Points: +" .. RECOVER_WORM_COST .. ". [Worm Lost] Total: " .. string.format("%.2f", map.points))
end
rally = true
end
if (chunk ~= -1) and rally then
rallyUnits(chunk, map, tick)
if cause and cause.valid then
retreatUnits(chunk,
cause,
map,
tick,
RETREAT_SPAWNER_GRAB_RADIUS)
end
end
end
if universe.buildingHiveTypeLookup[entity.name] or
(entityType == "unit-spawner") or
(entityType == "turret")
then
unregisterEnemyBaseStructure(map, entity, event.damage_type)
end
2021-02-20 07:41:30 +02:00
end
2020-05-24 05:47:14 +02:00
2021-02-20 07:41:30 +02:00
elseif (entity.force.name ~= "enemy") then
local creditNatives = false
if (event.force ~= nil) and (event.force.name == "enemy") then
creditNatives = true
if (chunk ~= -1) then
victoryScent(map, chunk, entityType)
end
2019-02-13 07:50:25 +02:00
2021-02-20 07:41:30 +02:00
local drained = (entityType == "electric-turret") and map.chunkToDrained[chunk]
if cause or (drained and (drained - tick) > 0) then
if ((cause and ENERGY_THIEF_LOOKUP[cause.name]) or (not cause)) then
local conversion = ENERGY_THIEF_CONVERSION_TABLE[entityType]
if conversion then
local newEntity = surface.create_entity({
position=entity.position,
name=convertTypeToDrainCrystal(entity.force.evolution_factor, conversion),
direction=entity.direction
})
if (conversion == "pole") then
local targetEntity = surface.create_entity({
2021-02-14 06:49:54 +02:00
position=entity.position,
2021-02-20 07:41:30 +02:00
name="pylon-target-rampant",
2021-02-14 06:49:54 +02:00
direction=entity.direction
})
2021-02-20 07:41:30 +02:00
targetEntity.backer_name = ""
local pair = {targetEntity, newEntity}
2021-02-20 09:31:36 +02:00
map.drainPylons[targetEntity.unit_number] = pair
map.drainPylons[newEntity.unit_number] = pair
2021-02-20 07:41:30 +02:00
local wires = entity.neighbours
if wires then
for _,v in pairs(wires.copper) do
if (v.valid) then
newEntity.connect_neighbour(v);
2019-02-19 02:43:01 +02:00
end
2021-02-20 07:41:30 +02:00
end
for _,v in pairs(wires.red) do
if (v.valid) then
newEntity.connect_neighbour({
wire = DEFINES_WIRE_TYPE_RED,
target_entity = v
});
2019-02-19 02:43:01 +02:00
end
2021-02-20 07:41:30 +02:00
end
for _,v in pairs(wires.green) do
if (v.valid) then
newEntity.connect_neighbour({
wire = DEFINES_WIRE_TYPE_GREEN,
target_entity = v
});
2019-02-19 02:43:01 +02:00
end
2019-02-13 07:50:25 +02:00
end
2019-10-13 22:53:36 +02:00
end
2021-02-20 07:41:30 +02:00
elseif newEntity.backer_name then
newEntity.backer_name = ""
2019-10-13 22:53:36 +02:00
end
end
end
end
2021-02-20 07:41:30 +02:00
elseif (entity.type == "resource") and (entity.force.name == "neutral") then
if (entity.amount == 0) then
unregisterResource(entity, map)
2020-05-13 23:45:27 +02:00
end
2019-10-13 22:53:36 +02:00
end
2021-02-20 09:31:36 +02:00
if creditNatives and universe.safeBuildings and
(universe.safeEntities[entityType] or universe.safeEntities[entity.name])
2021-02-20 07:41:30 +02:00
then
makeImmortalEntity(surface, entity)
else
2021-02-20 09:31:36 +02:00
accountPlayerEntity(entity, map, false, creditNatives)
2021-02-20 07:41:30 +02:00
end
end
end
end
2017-06-01 03:46:53 +02:00
local function onEnemyBaseBuild(event)
local entity = event.entity
2020-05-17 07:06:55 +02:00
if entity.valid then
2021-02-20 09:31:36 +02:00
local map = universe.maps[entity.surface.index]
2021-02-20 07:41:30 +02:00
local chunk = getChunkByPosition(map, entity.position)
if (chunk ~= -1) then
local base
2021-02-20 09:31:36 +02:00
if universe.NEW_ENEMIES then
2021-02-20 07:41:30 +02:00
base = findNearbyBase(map, chunk)
if not base then
2021-02-20 09:31:36 +02:00
base = createBase(map,
2021-02-20 07:41:30 +02:00
chunk,
event.tick)
2019-05-01 04:08:21 +02:00
end
registerEnemyBaseStructure(map, entity, event.tick, base)
2021-11-26 08:49:28 +02:00
upgradeEntity(entity,
base.alignment,
map,
nil,
true,
true)
end
2021-05-01 22:50:40 +02:00
else
local x,y = positionToChunkXY(entity.position)
onChunkGenerated({
surface = entity.surface,
area = {
left_top = {
x = x,
y = y
}
}
})
end
end
2017-06-01 03:46:53 +02:00
end
local function onSurfaceTileChange(event)
2020-05-15 22:51:38 +02:00
local surfaceIndex = event.surface_index or (event.robot and event.robot.surface and event.robot.surface.index)
2021-02-20 09:31:36 +02:00
local map = universe.maps[surfaceIndex]
2021-02-20 07:41:30 +02:00
local surface = map.surface
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
local position = tiles[i].position
local chunk = getChunkByPosition(map, position)
if (chunk ~= -1) then
map.chunkToPassScan[chunk] = true
else
local x,y = positionToChunkXY(position)
local addMe = true
for ci=1,#chunks do
local c = chunks[ci]
if (c.x == x) and (c.y == y) then
addMe = false
break
2020-05-20 04:37:16 +02:00
end
2019-10-13 22:53:36 +02:00
end
2021-02-20 07:41:30 +02:00
if addMe then
local chunkXY = {x=x,y=y}
chunks[#chunks+1] = chunkXY
onChunkGenerated({area = { left_top = chunkXY },
surface = surface})
end
2019-10-13 22:53:36 +02:00
end
2020-05-26 02:05:01 +02:00
end
2021-02-20 07:41:30 +02:00
end
else
for i=1,#tiles do
local tile = tiles[i]
if (tile.name == "landfill") or sFind(tile.name, "water") then
local position = tile.position
local chunk = getChunkByPosition(map, position)
if (chunk ~= -1) then
map.chunkToPassScan[chunk] = true
else
local x,y = positionToChunkXY(position)
local addMe = true
for ci=1,#chunks do
local c = chunks[ci]
if (c.x == x) and (c.y == y) then
addMe = false
break
2020-05-25 01:41:12 +02:00
end
end
2021-02-20 07:41:30 +02:00
if addMe then
local chunkXY = {x=x,y=y}
chunks[#chunks+1] = chunkXY
onChunkGenerated({area = { left_top = chunkXY },
surface = surface})
end
2020-05-25 01:41:12 +02:00
end
end
2019-10-13 22:53:36 +02:00
end
end
end
local function onResourceDepleted(event)
local entity = event.entity
2021-02-20 07:41:30 +02:00
if entity.valid then
2021-02-20 09:31:36 +02:00
unregisterResource(entity, universe.maps[entity.surface.index])
end
end
2019-03-10 21:28:43 +02:00
local function onRobotCliff(event)
2021-02-20 07:41:30 +02:00
local entity = event.robot
if entity.valid then
2021-02-20 09:31:36 +02:00
local map = universe.maps[entity.surface.index]
2021-02-20 07:41:30 +02:00
if (event.item.name == "cliff-explosives") then
entityForPassScan(map, event.cliff)
end
2019-03-10 21:28:43 +02:00
end
end
local function onUsedCapsule(event)
local surface = game.players[event.player_index].surface
2021-02-20 09:31:36 +02:00
local map = universe.maps[surface.index]
2021-02-20 07:41:30 +02:00
if (event.item.name == "cliff-explosives") then
2021-02-28 00:50:26 +02:00
local position2Top = universe.position2Top
local position2Bottom = universe.position2Bottom
position2Top.x = event.position.x-0.75
position2Top.y = event.position.y-0.75
position2Bottom.x = event.position.x+0.75
position2Bottom.y = event.position.y+0.75
local cliffs = surface.find_entities_filtered(universe.cliffQuery)
2019-10-13 22:53:36 +02:00
for i=1,#cliffs do
entityForPassScan(map, cliffs[i])
end
end
end
local function onRocketLaunch(event)
local entity = event.rocket_silo or event.rocket
2021-02-20 07:41:30 +02:00
if entity.valid then
2021-02-20 09:31:36 +02:00
local map = universe.maps[entity.surface.index]
map.rocketLaunched = map.rocketLaunched + 1
map.points = map.points + 5000
if universe.aiPointsPrintGainsToChat then
2021-05-01 19:26:05 +02:00
game.print(map.surface.name .. ": Points: +" .. 5000 .. ". [Rocket Launch] Total: " .. string.format("%.2f", map.points))
end
2019-02-03 08:01:28 +02:00
end
end
2019-02-19 02:43:01 +02:00
local function onTriggerEntityCreated(event)
2021-11-29 07:39:05 +02:00
if (event.effect_id == "rampant-drain-trigger") then
local entity = event.target_entity
if (entity.valid) then
local map = universe.maps[event.surface_index]
local chunk = getChunkByPosition(map, entity.position)
if (chunk ~= -1) then
map.chunkToDrained[chunk] = event.tick + 60
end
2019-02-19 02:43:01 +02:00
end
end
end
2016-09-13 00:33:00 +02:00
local function onInit()
2021-02-20 09:31:36 +02:00
global.universe = {}
2019-02-03 08:01:28 +02:00
2021-02-20 09:31:36 +02:00
universe = global.universe
2019-02-03 08:01:28 +02:00
2017-07-05 00:52:20 +02:00
hookEvents()
2021-02-20 07:41:30 +02:00
onConfigChanged()
2016-09-13 00:33:00 +02:00
end
2019-10-20 22:45:43 +02:00
local function onEntitySpawned(event)
2020-05-22 23:40:51 +02:00
local entity = event.mine
2021-02-20 09:31:36 +02:00
if universe.NEW_ENEMIES and entity.valid then
local map = universe.maps[entity.surface.index]
if universe.buildingHiveTypeLookup[entity.name] then
2020-05-22 23:40:51 +02:00
local disPos = mathUtils.distortPosition(entity.position, 8)
2020-05-17 07:06:55 +02:00
2020-05-22 23:40:51 +02:00
local chunk = getChunkByPosition(map, disPos)
if (chunk ~= -1) then
local base = findNearbyBase(map, chunk)
if not base then
2021-02-20 09:31:36 +02:00
base = createBase(map,
2020-05-22 23:40:51 +02:00
chunk,
event.tick)
end
2020-04-28 05:41:18 +02:00
registerEnemyBaseStructure(map, entity, event.tick, base)
2021-11-26 08:49:28 +02:00
upgradeEntity(entity,
base.alignment,
map,
disPos,
true,
true)
2019-11-30 02:49:22 +02:00
else
2021-05-01 22:50:40 +02:00
local x,y = positionToChunkXY(entity.position)
onChunkGenerated({
2021-07-26 02:25:14 +02:00
surface = entity.surface,
area = {
left_top = {
x = x,
y = y
}
2021-05-01 22:50:40 +02:00
}
})
2019-11-30 02:49:22 +02:00
entity.destroy()
2019-11-04 08:19:22 +02:00
end
2019-11-30 02:49:22 +02:00
end
2019-10-20 22:45:43 +02:00
end
end
local function onUnitGroupCreated(event)
local group = event.group
2021-02-20 07:41:30 +02:00
if (group.force.name == "enemy") then
local surface = group.surface
local squad
2020-02-02 21:30:50 +02:00
if not group.is_script_driven then
2021-02-20 09:31:36 +02:00
local map = universe.maps[surface.index]
if not universe.aiNocturnalMode then
2020-05-22 21:43:44 +02:00
local settler = mRandom() < 0.25 and
2021-02-20 09:31:36 +02:00
canMigrate(map) and
(universe.builderCount < universe.AI_MAX_BUILDER_COUNT)
2020-05-25 01:41:12 +02:00
2021-02-20 09:31:36 +02:00
if not settler and universe.squadCount > universe.AI_MAX_SQUAD_COUNT then
2020-05-25 01:41:12 +02:00
group.destroy()
2021-02-20 09:31:36 +02:00
map.points = map.points + AI_SQUAD_COST
if universe.aiPointsPrintGainsToChat then
2021-05-01 19:26:05 +02:00
game.print(map.surface.name .. ": Points: +" .. AI_SQUAD_COST .. ". [Squad Refund] Total: " .. string.format("%.2f", map.points))
end
2020-05-25 01:41:12 +02:00
return
end
2020-05-22 21:43:44 +02:00
squad = createSquad(nil, nil, group, settler)
2021-02-20 09:31:36 +02:00
map.groupNumberToSquad[group.group_number] = squad
2020-05-24 05:47:14 +02:00
if universe.NEW_ENEMIES then
local chunk = getChunkByPosition(map, group.position)
if (chunk ~= -1) then
squad.base = findNearbyBase(map, chunk)
end
end
2021-04-30 07:24:14 +02:00
2020-05-22 21:43:44 +02:00
if settler then
2021-02-20 09:31:36 +02:00
universe.builderCount = universe.builderCount + 1
2020-05-23 23:29:56 +02:00
else
2021-02-20 09:31:36 +02:00
universe.squadCount = universe.squadCount + 1
2020-05-22 21:43:44 +02:00
end
2020-05-20 04:37:16 +02:00
else
2020-05-25 01:41:12 +02:00
if not (surface.darkness > 0.65) then
group.destroy()
return
end
2020-05-26 02:05:01 +02:00
2020-05-22 21:43:44 +02:00
local settler = mRandom() < 0.25 and
2021-02-20 09:31:36 +02:00
canMigrate(map) and
(universe.builderCount < universe.AI_MAX_BUILDER_COUNT)
2020-05-25 01:41:12 +02:00
2021-02-20 09:31:36 +02:00
if not settler and universe.squadCount > universe.AI_MAX_SQUAD_COUNT then
2020-05-25 01:41:12 +02:00
group.destroy()
2021-02-20 09:31:36 +02:00
map.points = map.points + AI_SQUAD_COST
if universe.aiPointsPrintGainsToChat then
2021-05-01 19:26:05 +02:00
game.print(map.surface.name .. ": Points: +" .. AI_SQUAD_COST .. ". [Squad Refund] Total: " .. string.format("%.2f", map.points))
end
2020-05-25 01:41:12 +02:00
return
end
2020-05-22 21:43:44 +02:00
squad = createSquad(nil, nil, group, settler)
2021-02-28 21:28:48 +02:00
map.groupNumberToSquad[group.group_number] = squad
2021-04-30 07:24:14 +02:00
if universe.NEW_ENEMIES then
local chunk = getChunkByPosition(group.position)
if (chunk ~= -1) then
squad.base = findNearbyBase(map, chunk)
end
end
2021-04-30 07:24:14 +02:00
2020-05-22 21:43:44 +02:00
if settler then
2021-02-20 09:31:36 +02:00
universe.builderCount = universe.builderCount + 1
2020-05-23 23:29:56 +02:00
else
2021-02-20 09:31:36 +02:00
universe.squadCount = universe.squadCount + 1
2020-05-22 21:43:44 +02:00
end
2020-05-20 04:37:16 +02:00
end
2020-02-02 21:30:50 +02:00
end
end
end
2020-05-17 07:06:55 +02:00
local function onGroupFinishedGathering(event)
local group = event.group
2020-05-26 02:05:01 +02:00
if group.valid and (group.force.name == "enemy") then
2021-02-20 09:31:36 +02:00
local map = universe.maps[group.surface.index]
2021-02-28 00:50:26 +02:00
local squad = map.groupNumberToSquad[group.group_number]
2021-02-20 07:41:30 +02:00
if squad then
if squad.settler then
2021-02-20 09:31:36 +02:00
if (universe.builderCount < universe.AI_MAX_BUILDER_COUNT) then
squadDispatch(map, squad, event.tick)
2020-05-24 07:13:57 +02:00
else
2021-02-20 07:41:30 +02:00
group.destroy()
2021-02-20 09:31:36 +02:00
map.points = map.points + AI_SETTLER_COST
if universe.aiPointsPrintGainsToChat then
game.print(map.surface.name .. ": Points: +" .. AI_SETTLER_COST .. ". [Settler Refund] Total: " .. string.format("%.2f", map.points))
end
2020-05-23 23:29:56 +02:00
end
else
2021-02-20 09:31:36 +02:00
if (universe.squadCount < universe.AI_MAX_SQUAD_COUNT) then
squadDispatch(map, squad, event.tick)
2021-02-20 07:41:30 +02:00
else
2020-05-24 07:13:57 +02:00
group.destroy()
2021-02-20 09:31:36 +02:00
map.points = map.points + AI_SQUAD_COST
if universe.aiPointsPrintGainsToChat then
2021-05-01 19:26:05 +02:00
game.print(map.surface.name .. ": Points: +" .. AI_SQUAD_COST .. ". [Squad Refund] Total: " .. string.format("%.2f", map.points))
end
2020-05-23 23:29:56 +02:00
end
2021-02-20 07:41:30 +02:00
end
else
local settler = mRandom() < 0.25 and
2021-02-20 09:31:36 +02:00
canMigrate(map) and
(universe.builderCount < universe.AI_MAX_BUILDER_COUNT)
2021-02-20 07:41:30 +02:00
2021-02-20 09:31:36 +02:00
if not settler and universe.squadCount > universe.AI_MAX_SQUAD_COUNT then
2021-02-20 07:41:30 +02:00
group.destroy()
2021-02-20 09:31:36 +02:00
map.points = map.points + AI_SQUAD_COST
if universe.aiPointsPrintGainsToChat then
2021-05-01 19:26:05 +02:00
game.print(map.surface.name .. ": Points: +" .. AI_SQUAD_COST .. ". [Squad Refund] Total: " .. string.format("%.2f", map.points))
end
2021-02-20 07:41:30 +02:00
return
end
2020-05-26 02:05:01 +02:00
2021-02-20 07:41:30 +02:00
squad = createSquad(nil, nil, group, settler)
2021-02-20 09:31:36 +02:00
map.groupNumberToSquad[group.group_number] = squad
2021-02-20 07:41:30 +02:00
if settler then
2021-02-20 09:31:36 +02:00
universe.builderCount = universe.builderCount + 1
2021-02-20 07:41:30 +02:00
else
2021-02-20 09:31:36 +02:00
universe.squadCount = universe.squadCount + 1
2020-05-23 23:29:56 +02:00
end
squadDispatch(map, squad, event.tick)
2020-05-17 07:06:55 +02:00
end
end
end
2019-05-10 02:46:57 +02:00
local function onForceCreated(event)
2021-02-20 09:31:36 +02:00
universe.activePlayerForces[#universe.activePlayerForces+1] = event.force.name
2019-05-10 02:46:57 +02:00
end
local function onForceMerged(event)
2021-02-20 09:31:36 +02:00
for i=#universe.activePlayerForces,1,-1 do
if (universe.activePlayerForces[i] == event.source_name) then
tRemove(universe.activePlayerForces, i)
2019-05-10 02:46:57 +02:00
break
end
2019-10-19 21:13:48 +02:00
end
2019-05-10 02:46:57 +02:00
end
2021-02-20 07:41:30 +02:00
local function onSurfaceCreated(event)
prepMap(game.surfaces[event.surface_index])
2020-04-28 05:41:18 +02:00
end
local function onSurfaceDeleted(event)
2021-02-20 07:41:30 +02:00
local surfaceIndex = event.surface_index
if (universe.mapIterator == surfaceIndex) then
universe.mapIterator, universe.activeMap = next(universe.maps, universe.mapIterator)
end
2021-02-20 09:31:36 +02:00
universe.maps[surfaceIndex] = nil
2020-04-28 05:41:18 +02:00
end
2020-08-08 22:38:15 +02:00
local function onBuilderArrived(event)
local builder = event.group
if not (builder and builder.valid) then
builder = event.unit
if not (builder and builder.valid and builder.force.name == "enemy") then
return
end
elseif (builder.force.name ~= "enemy") then
return
end
2021-02-20 09:31:36 +02:00
local targetPosition = universe.position
2020-08-08 22:38:15 +02:00
targetPosition.x = builder.position.x
targetPosition.y = builder.position.y
2021-05-01 19:26:05 +02:00
local squad = universe.maps[builder.surface.index].groupNumberToSquad[builder.group_number]
squad.commandTick = event.tick + COMMAND_TIMEOUT * 10
2021-05-01 19:26:05 +02:00
if universe.aiPointsPrintSpendingToChat then
game.print("Settled: [gps=" .. targetPosition.x .. "," .. targetPosition.y .."]")
end
2021-02-20 09:31:36 +02:00
builder.surface.create_entity(universe.createBuildCloudQuery)
2020-08-08 22:38:15 +02:00
end
-- hooks
2020-04-28 05:41:18 +02:00
script.on_event(defines.events.on_tick,
2021-02-20 07:41:30 +02:00
function ()
2020-05-22 23:40:51 +02:00
local gameRef = game
2021-02-20 07:41:30 +02:00
local tick = gameRef.tick
2020-05-22 23:40:51 +02:00
local pick = tick % 7
2020-10-19 05:33:01 +02:00
-- local profiler = game.create_profiler()
2021-02-20 09:31:36 +02:00
local map = universe.activeMap
if (not map) or (universe.processedChunks > #map.processQueue) then
2021-02-20 09:31:36 +02:00
universe.mapIterator, map = next(universe.maps, universe.mapIterator)
if not map then
2021-02-20 09:31:36 +02:00
universe.mapIterator, map = next(universe.maps, universe.mapIterator)
2021-02-20 07:41:30 +02:00
end
universe.processedChunks = 0
universe.activeMap = map
2021-02-20 07:41:30 +02:00
end
2020-10-19 05:33:01 +02:00
2020-05-22 23:40:51 +02:00
if (pick == 0) then
processPendingChunks(map, tick)
2021-02-20 07:41:30 +02:00
processScanChunks(map)
universe.processedChunks = universe.processedChunks + PROCESS_QUEUE_SIZE
2021-02-20 09:31:36 +02:00
planning(map, gameRef.forces.enemy.evolution_factor, tick)
if universe.NEW_ENEMIES then
recycleBases(map)
2021-02-20 07:41:30 +02:00
end
cleanUpMapTables(map, tick)
elseif (pick == 1) then
processPlayers(gameRef.connected_players, map, tick)
elseif (pick == 2) then
processMap(map, tick)
2020-05-22 23:40:51 +02:00
elseif (pick == 3) then
2021-02-14 06:49:54 +02:00
processStaticMap(map)
2021-02-20 07:41:30 +02:00
disperseVictoryScent(map)
processVengence(map)
2020-05-22 23:40:51 +02:00
elseif (pick == 4) then
2021-02-20 07:41:30 +02:00
scanResourceMap(map, tick)
2020-05-22 23:40:51 +02:00
elseif (pick == 5) then
2021-02-20 07:41:30 +02:00
scanEnemyMap(map, tick)
processSpawners(map)
2020-05-22 23:40:51 +02:00
elseif (pick == 6) then
2021-02-20 07:41:30 +02:00
scanPlayerMap(map, tick)
processNests(map, tick)
temperamentPlanner(map)
2020-05-22 23:40:51 +02:00
end
2021-02-20 07:41:30 +02:00
processActiveNests(map, tick)
2021-11-26 08:49:28 +02:00
processPendingUpgrades(map, tick)
processPendingUpgrades(map, tick)
cleanSquads(map, tick)
-- game.print({"", "--dispatch4 ", profiler, ", ", pick, ", ", game.tick, " ", mRandom()})
2020-04-28 05:41:18 +02:00
end)
2021-02-20 07:41:30 +02:00
script.on_event(defines.events.on_surface_deleted, onSurfaceDeleted)
script.on_event(defines.events.on_surface_cleared, onSurfaceCreated)
script.on_event(defines.events.on_surface_created, onSurfaceCreated)
2020-04-28 05:41:18 +02:00
script.on_init(onInit)
script.on_load(onLoad)
2017-11-21 09:27:03 +02:00
script.on_event(defines.events.on_runtime_mod_setting_changed, onModSettingsChange)
2016-08-25 02:05:20 +02:00
script.on_configuration_changed(onConfigChanged)
script.on_event(defines.events.on_resource_depleted, onResourceDepleted)
script.on_event({defines.events.on_player_built_tile,
2020-05-25 01:41:12 +02:00
defines.events.on_robot_built_tile,
defines.events.script_raised_set_tiles}, onSurfaceTileChange)
script.on_event(defines.events.on_player_used_capsule, onUsedCapsule)
2021-11-29 07:39:05 +02:00
script.on_event(defines.events.on_script_trigger_effect, onTriggerEntityCreated)
2019-02-19 02:43:01 +02:00
2019-03-10 21:28:43 +02:00
script.on_event(defines.events.on_pre_robot_exploded_cliff, onRobotCliff)
2017-11-21 09:27:03 +02:00
script.on_event(defines.events.on_biter_base_built, onEnemyBaseBuild)
2017-08-14 02:35:44 +02:00
script.on_event({defines.events.on_player_mined_entity,
2020-05-13 23:45:27 +02:00
defines.events.on_robot_mined_entity}, onMine)
2020-05-15 22:51:38 +02:00
script.on_event({defines.events.on_built_entity,
defines.events.on_robot_built_entity,
2019-12-25 03:06:11 +02:00
defines.events.script_raised_built,
defines.events.script_raised_revive}, onBuild)
2016-09-09 12:02:50 +02:00
2020-05-22 23:40:51 +02:00
script.on_event(defines.events.on_land_mine_armed, onEntitySpawned)
script.on_event(defines.events.on_rocket_launched, onRocketLaunch)
2020-05-13 23:45:27 +02:00
script.on_event({defines.events.on_entity_died,
defines.events.script_raised_destroy}, onDeath)
script.on_event(defines.events.on_chunk_generated, onChunkGenerated)
2020-02-02 21:30:50 +02:00
script.on_event(defines.events.on_unit_group_created, onUnitGroupCreated)
2019-05-10 02:46:57 +02:00
script.on_event(defines.events.on_force_created, onForceCreated)
script.on_event(defines.events.on_forces_merged, onForceMerged)
2020-05-17 07:06:55 +02:00
script.on_event(defines.events.on_unit_group_finished_gathering, onGroupFinishedGathering)
2020-08-08 22:38:15 +02:00
script.on_event(defines.events.on_build_base_arrived, onBuilderArrived)
2021-03-29 07:00:49 +02:00
-- testing
2017-05-08 08:56:11 +02:00
remote.add_interface("rampantTests",
2019-10-13 22:53:36 +02:00
{
pheromoneLevels = tests.pheromoneLevels,
activeSquads = tests.activeSquads,
entitiesOnPlayerChunk = tests.entitiesOnPlayerChunk,
findNearestPlayerEnemy = tests.findNearestPlayerEnemy,
morePoints = tests.morePoints,
aiStats = tests.aiStats,
dumpEnvironment = tests.dumpEnvironment,
fillableDirtTest = tests.fillableDirtTest,
tunnelTest = tests.tunnelTest,
dumpNatives = tests.dumpatives,
createEnemy = tests.createEnemy,
attackOrigin = tests.attackOrigin,
cheatMode = tests.cheatMode,
gaussianRandomTest = tests.gaussianRandomTest,
reveal = tests.reveal,
showMovementGrid = tests.showMovementGrid,
showBaseGrid = tests.showBaseGrid,
baseStats = tests.baseStats,
mergeBases = tests.mergeBases,
clearBases = tests.clearBases,
getOffsetChunk = tests.getOffsetChunk,
registeredNest = tests.registeredNest,
colorResourcePoints = tests.colorResourcePoints,
entityStats = tests.entityStats,
stepAdvanceTendrils = tests.stepAdvanceTendrils,
unitGroupBuild = tests.unitGroupBuild,
exportAiState = tests.exportAiState(nil),
createEnergyTest = tests.createEnergyTest,
killActiveSquads = tests.killActiveSquads,
scanChunkPaths = tests.scanChunkPaths,
scanEnemy = tests.scanEnemy,
getEnemyStructureCount = tests.getEnemyStructureCount
2019-10-13 22:53:36 +02:00
}
2017-05-08 08:56:11 +02:00
)
2021-05-01 22:50:40 +02:00
local function rampantSetAIState(event)
local surfaceIndex = game.players[event.player_index].surface.index
local map = universe.maps[surfaceIndex]
2021-05-01 19:26:05 +02:00
game.print(map.surface.name .. " is in " .. constants.stateEnglish[map.state])
2021-05-01 19:26:05 +02:00
if event.parameter then
local target = tonumber(event.parameter)
2021-05-01 19:26:05 +02:00
if (target == nil) then
game.print("invalid param")
return
end
2021-05-01 19:26:05 +02:00
if (target ~= constants.AI_STATE_PEACEFUL and target ~= constants.AI_STATE_AGGRESSIVE and target ~= constants.AI_STATE_RAIDING and target ~= constants.AI_STATE_MIGRATING and target ~= constants.AI_STATE_SIEGE and target ~= constants.AI_STATE_ONSLAUGHT) then
game.print(target .. " is not a valid state")
return
else
map.state = target
game.print(map.surface.name .. " is now in " .. constants.stateEnglish[map.state])
end
end
end
2021-05-01 19:26:05 +02:00
commands.add_command('rampantSetAIState', "", rampantSetAIState)