1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-18 02:58:32 +02:00
Rampant/control.lua

1059 lines
40 KiB
Lua
Raw Normal View History

-- imports
2019-10-19 12:13:48 -07:00
local chunkPropertyUtils = require("libs/ChunkPropertyUtils")
2019-02-12 21:50:25 -08:00
local unitUtils = require("libs/UnitUtils")
local baseUtils = require("libs/BaseUtils")
2017-01-19 21:58:36 -08:00
local mapUtils = require("libs/MapUtils")
2019-10-20 21:38:36 -07:00
local mathUtils = require("libs/MathUtils")
2016-08-06 20:38:47 -07: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-14 17:00:18 -07:00
local aiPlanning = require("libs/AIPlanning")
2021-02-19 21:41:30 -08:00
-- local interop = require("libs/Interop")
2016-08-21 15:59:17 -07:00
local tests = require("tests")
2017-11-20 23:27:03 -08:00
local chunkUtils = require("libs/ChunkUtils")
2017-05-19 00:47:24 -07:00
local upgrade = require("Upgrade")
2017-06-01 00:03:07 -07:00
local config = require("config")
2020-05-16 22:06:55 -07:00
local aiPredicates = require("libs/AIPredicates")
2016-10-14 17:00:18 -07:00
-- constants
2020-05-23 22:13:57 -07:00
local AI_SQUAD_COST = constants.AI_SQUAD_COST
local AI_SETTLER_COST = constants.AI_SETTLER_COST
2020-05-22 12:43:44 -07:00
2019-04-05 20:01:46 -07: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
2019-02-12 21:50:25 -08: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-05-31 18:46:53 -07:00
-- imported functions
2017-01-19 21:58:36 -08:00
2020-05-16 22:06:55 -07:00
local canMigrate = aiPredicates.canMigrate
2019-02-12 21:50:25 -08:00
local convertTypeToDrainCrystal = unitUtils.convertTypeToDrainCrystal
2020-05-16 22:06:55 -07:00
local squadDispatch = squadAttack.squadDispatch
2018-02-12 23:10:17 -08:00
local cleanUpMapTables = mapProcessor.cleanUpMapTables
2018-01-27 14:46:45 -08:00
local positionToChunkXY = mapUtils.positionToChunkXY
local temperamentPlanner = aiPlanning.temperamentPlanner
2020-05-19 19:37:16 -07:00
local processVengence = mapProcessor.processVengence
local processSpawners = mapProcessor.processSpawners
local processStaticMap = mapProcessor.processStaticMap
2020-05-24 16:41:12 -07:00
local disperseVictoryScent = pheromoneUtils.disperseVictoryScent
2017-01-19 21:58:36 -08:00
local getChunkByPosition = mapUtils.getChunkByPosition
2018-01-02 10:36:23 -08:00
local entityForPassScan = chunkUtils.entityForPassScan
local processPendingChunks = chunkProcessor.processPendingChunks
local processScanChunks = chunkProcessor.processScanChunks
local processMap = mapProcessor.processMap
2016-10-14 17:00:18 -07:00
local processPlayers = mapProcessor.processPlayers
local scanEnemyMap = mapProcessor.scanEnemyMap
local scanPlayerMap = mapProcessor.scanPlayerMap
local scanResourceMap = mapProcessor.scanResourceMap
local processNests = mapProcessor.processNests
2016-10-14 17:00:18 -07:00
local planning = aiPlanning.planning
local rallyUnits = aiAttackWave.rallyUnits
2017-01-19 21:58:36 -08:00
2018-01-25 21:48:12 -08:00
local recycleBases = baseUtils.recycleBases
local deathScent = pheromoneUtils.deathScent
2017-04-21 16:14:04 -07:00
local victoryScent = pheromoneUtils.victoryScent
2020-05-16 22:06:55 -07:00
local createSquad = unitGroupUtils.createSquad
local createBase = baseUtils.createBase
local findNearbyBase = baseUtils.findNearbyBase
2020-04-27 20:41:18 -07:00
local processActiveNests = mapProcessor.processActiveNests
2020-05-19 19:37:16 -07:00
local getDeathGenerator = chunkPropertyUtils.getDeathGenerator
local retreatUnits = squadDefense.retreatUnits
local accountPlayerEntity = chunkUtils.accountPlayerEntity
2017-11-20 23:27:03 -08:00
local unregisterEnemyBaseStructure = chunkUtils.unregisterEnemyBaseStructure
local registerEnemyBaseStructure = chunkUtils.registerEnemyBaseStructure
local makeImmortalEntity = chunkUtils.makeImmortalEntity
2019-05-15 22:11:43 -07:00
local registerResource = chunkUtils.registerResource
local unregisterResource = chunkUtils.unregisterResource
2020-10-18 20:33:01 -07:00
local cleanSquads = squadAttack.cleanSquads
2020-05-16 22:06:55 -07:00
2018-01-14 15:10:56 -08:00
local upgradeEntity = baseUtils.upgradeEntity
local rebuildNativeTables = baseUtils.rebuildNativeTables
2016-10-14 17:00:18 -07:00
local mRandom = math.random
2019-05-09 17:46:57 -07:00
local tRemove = table.remove
2020-05-19 19:37:16 -07:00
local sFind = string.find
2016-08-24 17:05:20 -07:00
-- local references to global
2021-02-19 21:41:30 -08:00
local maps -- manages the chunks that make up the game world
local queriesAndCommands -- manages all the queries and commands that will be used
local natives -- manages the enemy units, structures, and ai
2016-08-24 17:05:20 -07:00
-- hook functions
2017-07-04 15:52:20 -07:00
local function onIonCannonFired(event)
--[[
2019-10-13 13:53:36 -07:00
event.force, event.surface, event.player_index, event.position, event.radius
2017-07-04 15:52:20 -07:00
--]]
local surface = event.surface
2021-02-19 21:41:30 -08:00
local surfaceIndex = surface.index
local map = maps.map[surfaceIndex]
local native = natives[surfaceIndex]
native.ionCannonBlasts = native.ionCannonBlasts + 1
native.points = native.point + 4000
local chunk = getChunkByPosition(map, event.position)
if (chunk ~= -1) then
rallyUnits(chunk, map, event.tick)
2019-02-02 22:01:28 -08:00
end
2017-07-04 15:52:20 -07:00
end
local function hookEvents()
if config.ionCannonPresent then
2019-10-13 13:53:36 -07:00
script.on_event(remote.call("orbital_ion_cannon", "on_ion_cannon_fired"),
onIonCannonFired)
2019-02-02 22:01:28 -08:00
end
2017-07-04 15:52:20 -07:00
end
2016-09-12 15:33:00 -07:00
local function onLoad()
2021-02-19 21:41:30 -08:00
maps = global.maps
queriesAndCommands = global.queriesAndCommands
2016-08-24 17:05:20 -07:00
natives = global.natives
2017-07-04 15:52:20 -07:00
hookEvents()
end
2016-09-12 15:33:00 -07:00
local function onChunkGenerated(event)
2021-02-19 21:41:30 -08: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.
maps.map[event.surface.index].pendingChunks[event] = true
end
local function onModSettingsChange(event)
if event and ((string.sub(event.setting, 1, 7) ~= "rampant") or
(string.sub(event.setting, 1, 15) == "rampant-arsenal") or
(string.sub(event.setting, 1, 17) == "rampant-resources") or
(string.sub(event.setting, 1, 17) == "rampant-evolution") or
(string.sub(event.setting, 1, 19) == "rampant-maintenance") or
(string.sub(event.setting, 1, 16) == "rampant-industry"))
then
return false
2016-09-12 15:33:00 -07:00
end
2021-02-19 21:41:30 -08:00
upgrade.compareTable(natives,
"safeBuildings",
settings.global["rampant-safeBuildings"].value)
upgrade.compareTable(natives.safeEntities,
"curved-rail",
settings.global["rampant-safeBuildings-curvedRail"].value)
upgrade.compareTable(natives.safeEntities,
"straight-rail",
settings.global["rampant-safeBuildings-straightRail"].value)
upgrade.compareTable(natives.safeEntities,
"rail-signal",
settings.global["rampant-safeBuildings-railSignals"].value)
upgrade.compareTable(natives.safeEntities,
"rail-chain-signal",
settings.global["rampant-safeBuildings-railChainSignals"].value)
upgrade.compareTable(natives.safeEntities,
"train-stop",
settings.global["rampant-safeBuildings-trainStops"].value)
upgrade.compareTable(natives.safeEntities,
"lamp",
settings.global["rampant-safeBuildings-lamps"].value)
local changed, newValue = upgrade.compareTable(natives.safeEntities,
"big-electric-pole",
settings.global["rampant-safeBuildings-bigElectricPole"].value)
if changed then
natives.safeEntities["big-electric-pole"] = newValue
natives.safeEntities["big-electric-pole-2"] = newValue
natives.safeEntities["big-electric-pole-3"] = newValue
natives.safeEntities["big-electric-pole-4"] = newValue
natives.safeEntities["lighted-big-electric-pole-4"] = newValue
natives.safeEntities["lighted-big-electric-pole-3"] = newValue
natives.safeEntities["lighted-big-electric-pole-2"] = newValue
natives.safeEntities["lighted-big-electric-pole"] = newValue
end
upgrade.compareTable(natives,
"deadZoneFrequency",
settings.global["rampant-deadZoneFrequency"].value)
upgrade.compareTable(natives,
"raidAIToggle",
settings.global["rampant-raidAIToggle"].value)
upgrade.compareTable(natives,
"siegeAIToggle",
settings.global["rampant-siegeAIToggle"].value)
upgrade.compareTable(natives,
"attackPlayerThreshold",
settings.global["rampant-attackPlayerThreshold"].value)
upgrade.compareTable(natives,
"attackUsePlayer",
settings.global["rampant-attackWaveGenerationUsePlayerProximity"].value)
upgrade.compareTable(natives,
"attackWaveMaxSize",
settings.global["rampant-attackWaveMaxSize"].value)
upgrade.compareTable(natives,
"aiNocturnalMode",
settings.global["rampant-permanentNocturnal"].value)
upgrade.compareTable(natives,
"aiPointsScaler",
settings.global["rampant-aiPointsScaler"].value)
natives.enabledMigration = natives.expansion and settings.global["rampant-enableMigration"].value
upgrade.compareTable(natives,
"AI_MAX_SQUAD_COUNT",
settings.global["rampant-maxNumberOfSquads"].value)
upgrade.compareTable(natives,
"AI_MAX_BUILDER_COUNT",
settings.global["rampant-maxNumberOfBuilders"].value)
return true
2016-08-24 17:05:20 -07:00
end
2021-02-19 21:41:30 -08:00
local function prepWorld(surface)
surface.print("Rampant - Indexing chunks, please wait.")
2017-05-31 18:46:53 -07:00
2021-02-19 21:41:30 -08:00
local surfaceIndex = surface.index
if not maps.map then
maps.map = {}
end
local map = maps.map[surfaceIndex]
if not map then
map = {}
maps.map[surfaceIndex] = map
if not maps.registeredMaps then
maps.registeredMaps = {}
end
local add = true
for _, v in pairs(maps.registeredMaps) do
if surfaceIndex == v then
add = false
break
end
end
if add then
maps.registeredMaps[#maps.registeredMaps+1] = surfaceIndex
end
end
map.totalChunks = 0
map.processedChunks = 0
map.mapIterator = nil
2018-01-13 21:48:21 -08: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-13 21:48:21 -08:00
2020-05-19 19:37:16 -07:00
map.pendingChunks = {}
map.chunkToBase = {}
2018-01-13 21:48:21 -08:00
map.chunkToNests = {}
2019-11-29 16:49:22 -08:00
map.chunkToTurrets = {}
map.chunkToTraps = {}
map.chunkToUtilities = {}
map.chunkToHives = {}
2018-01-13 21:48:21 -08:00
map.chunkToPlayerBase = {}
map.chunkToResource = {}
map.chunkToPlayerCount = {}
map.playerToChunk = {}
2018-02-11 19:21:28 -08:00
2018-01-13 21:48:21 -08:00
map.chunkToPassScan = {}
map.chunkToSquad = {}
2018-02-11 19:21:28 -08:00
map.chunkToRetreats = {}
map.chunkToRallys = {}
2018-09-23 21:56:45 -07:00
map.chunkToPassable = {}
map.chunkToPathRating = {}
map.chunkToDeathGenerator = {}
2019-02-18 16:43:01 -08:00
map.chunkToDrained = {}
2020-05-24 16:41:12 -07:00
map.chunkToVictory = {}
2019-02-19 22:16:43 -08:00
map.chunkToActiveNest = {}
map.chunkToActiveRaidNest = {}
2018-09-23 21:56:45 -07:00
2019-11-29 16:49:22 -08:00
map.nextChunkSort = 0
map.nextChunkSortTick = 0
2019-02-02 22:01:28 -08:00
2020-05-16 22:06:55 -07:00
map.squadIterator = nil
map.regroupIterator = nil
2020-05-19 19:37:16 -07:00
map.deployVengenceIterator = nil
map.recycleBaseIterator = nil
map.processActiveSpawnerIterator = nil
map.processActiveRaidSpawnerIterator = nil
map.processMigrationIterator = nil
map.processNestIterator = nil
2020-05-24 16:41:12 -07:00
map.victoryScentIterator = nil
2020-05-16 22:06:55 -07:00
2019-12-06 21:57:20 -08:00
map.chunkScanCounts = {}
2019-10-20 13:45:43 -07:00
map.enemiesToSquad = {}
map.enemiesToSquad.len = 0
map.chunkRemovals = {}
2020-04-27 20:41:18 -07:00
map.processActiveNest = {}
map.tickActiveNest = {}
2019-10-20 13:45:43 -07:00
map.emptySquadsOnChunk = {}
2019-05-09 17:46:57 -07:00
2021-02-19 21:41:30 -08:00
local native = natives[surface.index]
if not native then
native = {}
natives[surface.index] = native
end
map.native = native
native.map = map
map.surface = surface
native.surface = surface
map.queriesAndCommands = queriesAndCommands
native.queriesAndCommands = queriesAndCommands
-- queue all current chunks that wont be generated during play
local tick = game.tick
local position = {0,0}
native.nextChunkSort = 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,
area = { left_top = { x = x * 32,
y = y * 32}}})
2019-05-09 17:46:57 -07:00
end
end
2021-02-19 21:41:30 -08:00
processPendingChunks(map, tick)
2019-02-02 22:01:28 -08:00
end
2017-06-24 11:41:57 -07:00
2021-02-19 21:41:30 -08:00
local function onConfigChanged()
if upgrade.attempt(natives, queriesAndCommands) then
onModSettingsChange(nil)
2017-05-31 18:46:53 -07:00
end
2019-02-02 22:01:28 -08:00
2021-02-13 20:49:54 -08:00
upgrade.compareTable(natives,
2021-02-19 21:41:30 -08:00
"ENEMY_SEED",
settings.startup["rampant-enemySeed"].value)
upgrade.compareTable(natives,
"ENEMY_VARIATIONS",
settings.startup["rampant-newEnemyVariations"].value)
upgrade.compareTable(natives,
"NEW_ENEMIES",
settings.startup["rampant-newEnemies"].value)
2018-05-23 17:40:05 -07:00
2021-02-19 21:41:30 -08:00
if natives.NEW_ENEMIES then
rebuildNativeTables(natives, game.create_random_generator(natives.ENEMY_SEED))
2019-12-06 21:57:20 -08:00
else
natives.buildingHiveTypeLookup = {}
natives.buildingHiveTypeLookup["biter-spawner"] = "biter-spawner"
natives.buildingHiveTypeLookup["spitter-spawner"] = "spitter-spawner"
natives.buildingHiveTypeLookup["small-worm-turret"] = "turret"
natives.buildingHiveTypeLookup["medium-worm-turret"] = "turret"
natives.buildingHiveTypeLookup["big-worm-turret"] = "turret"
natives.buildingHiveTypeLookup["behemoth-worm-turret"] = "turret"
end
end
2016-09-12 15:33:00 -07:00
local function onBuild(event)
local entity = event.created_entity or event.entity
2021-02-19 21:41:30 -08:00
if entity.valid then
local native = natives[entity.surface.index]
local map = native.map
2019-05-15 22:11:43 -07:00
if (entity.type == "resource") and (entity.force.name == "neutral") then
registerResource(entity, map)
else
2021-02-19 21:41:30 -08:00
accountPlayerEntity(entity, native, true, false)
2019-05-15 22:11:43 -07:00
if natives.safeBuildings then
2020-04-27 20:41:18 -07:00
if natives.safeEntities[entity.type] or natives.safeEntities[entity.name] then
2019-05-15 22:11:43 -07:00
entity.destructible = false
end
end
2019-10-19 12:13:48 -07:00
end
end
end
2017-08-08 01:19:51 -07:00
local function onMine(event)
local entity = event.entity
2021-02-19 21:41:30 -08:00
if entity.valid then
local native = natives[entity.surface.index]
local map = native.map
2019-05-15 22:11:43 -07:00
if (entity.type == "resource") and (entity.force.name == "neutral") then
if (entity.amount == 0) then
unregisterResource(entity, map)
end
2019-10-19 12:13:48 -07:00
else
2021-02-19 21:41:30 -08:00
accountPlayerEntity(entity, native, false, false)
2019-05-15 22:11:43 -07:00
end
end
end
2016-09-12 15:33:00 -07:00
local function onDeath(event)
local entity = event.entity
2020-05-15 13:51:38 -07:00
if entity.valid then
local surface = entity.surface
2021-02-19 21:41:30 -08:00
local native = natives[surface.index]
local map = native.map
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
native.artilleryBlasts = native.artilleryBlasts + 1
end
2019-10-13 13:53:36 -07:00
2021-02-19 21:41:30 -08:00
if (entityType == "unit") then
if (chunk ~= -1) and event.force and (event.force.name ~= "enemy") then
-- drop death pheromone where unit died
deathScent(map, chunk)
if (-getDeathGenerator(map, chunk) < -native.retreatThreshold) and cause and cause.valid then
retreatUnits(chunk,
cause,
map,
tick,
(artilleryBlast and RETREAT_SPAWNER_GRAB_RADIUS) or RETREAT_GRAB_RADIUS)
2019-10-13 13:53:36 -07:00
end
2019-04-30 19:08:21 -07:00
2021-02-19 21:41:30 -08:00
native.lostEnemyUnits = native.lostEnemyUnits + 1
2019-10-13 13:53:36 -07:00
2021-02-19 21:41:30 -08:00
if (mRandom() < native.rallyThreshold) and not surface.peaceful_mode then
2021-02-13 20:49:54 -08:00
rallyUnits(chunk, map, tick)
2020-05-23 20:47:14 -07:00
end
2021-02-19 21:41:30 -08:00
end
elseif event.force and (event.force.name ~= "enemy") and
((entityType == "unit-spawner") or (entityType == "turret"))
then
native.points = native.points +
(((entityType == "unit-spawner") and RECOVER_NEST_COST) or RECOVER_WORM_COST)
unregisterEnemyBaseStructure(map, entity)
if (chunk ~= -1) then
rallyUnits(chunk, map, tick)
if cause and cause.valid then
retreatUnits(chunk,
cause,
map,
tick,
RETREAT_SPAWNER_GRAB_RADIUS)
end
end
else
local entityUnitNumber = entity.unit_number
local pair = native.drainPylons[entityUnitNumber]
if pair then
local target = pair[1]
local pole = pair[2]
if target == entity then
native.drainPylons[entityUnitNumber] = nil
if pole.valid then
native.drainPylons[pole.unit_number] = nil
pole.die()
end
elseif (pole == entity) then
native.drainPylons[entityUnitNumber] = nil
if target.valid then
native.drainPylons[target.unit_number] = nil
target.destroy()
2020-05-15 13:51:38 -07:00
end
2019-02-18 16:43:01 -08:00
end
end
2021-02-19 21:41:30 -08:00
end
2020-05-23 20:47:14 -07:00
2021-02-19 21:41:30 -08: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-12 21:50:25 -08:00
2021-02-19 21:41:30 -08: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-13 20:49:54 -08:00
position=entity.position,
2021-02-19 21:41:30 -08:00
name="pylon-target-rampant",
2021-02-13 20:49:54 -08:00
direction=entity.direction
})
2021-02-19 21:41:30 -08:00
targetEntity.backer_name = ""
local pair = {targetEntity, newEntity}
native.drainPylons[targetEntity.unit_number] = pair
native.drainPylons[newEntity.unit_number] = pair
local wires = entity.neighbours
if wires then
for _,v in pairs(wires.copper) do
if (v.valid) then
newEntity.connect_neighbour(v);
2019-02-18 16:43:01 -08:00
end
2021-02-19 21:41:30 -08: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-18 16:43:01 -08:00
end
2021-02-19 21:41:30 -08: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-18 16:43:01 -08:00
end
2019-02-12 21:50:25 -08:00
end
2019-10-13 13:53:36 -07:00
end
2021-02-19 21:41:30 -08:00
elseif newEntity.backer_name then
newEntity.backer_name = ""
2019-10-13 13:53:36 -07:00
end
end
end
end
2021-02-19 21:41:30 -08:00
elseif (entity.type == "resource") and (entity.force.name == "neutral") then
if (entity.amount == 0) then
unregisterResource(entity, map)
2020-05-13 14:45:27 -07:00
end
2019-10-13 13:53:36 -07:00
end
2021-02-19 21:41:30 -08:00
if creditNatives and natives.safeBuildings and
(natives.safeEntities[entityType] or natives.safeEntities[entity.name])
then
makeImmortalEntity(surface, entity)
else
accountPlayerEntity(entity, native, false, creditNatives)
end
end
end
end
2017-05-31 18:46:53 -07:00
local function onEnemyBaseBuild(event)
local entity = event.entity
2020-05-16 22:06:55 -07:00
if entity.valid then
2021-02-19 21:41:30 -08:00
local native = natives[entity.surface.index]
local map = native.map
local chunk = getChunkByPosition(map, entity.position)
if (chunk ~= -1) then
local base
if natives.NEW_ENEMIES then
base = findNearbyBase(map, chunk)
if not base then
base = createBase(native,
chunk,
event.tick)
2019-04-30 19:08:21 -07:00
end
2021-02-19 21:41:30 -08:00
entity = upgradeEntity(entity,
base.alignment,
native,
nil,
true)
end
if entity and entity.valid then
event.entity = registerEnemyBaseStructure(map, entity, base)
end
end
end
2017-05-31 18:46:53 -07:00
end
local function onSurfaceTileChange(event)
2020-05-15 13:51:38 -07:00
local surfaceIndex = event.surface_index or (event.robot and event.robot.surface and event.robot.surface.index)
2021-02-19 21:41:30 -08:00
local map = maps.map[surfaceIndex]
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-19 19:37:16 -07:00
end
2019-10-13 13:53:36 -07:00
end
2021-02-19 21:41:30 -08: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 13:53:36 -07:00
end
2020-05-25 17:05:01 -07:00
end
2021-02-19 21:41:30 -08: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-24 16:41:12 -07:00
end
end
2021-02-19 21:41:30 -08:00
if addMe then
local chunkXY = {x=x,y=y}
chunks[#chunks+1] = chunkXY
onChunkGenerated({area = { left_top = chunkXY },
surface = surface})
end
2020-05-24 16:41:12 -07:00
end
end
2019-10-13 13:53:36 -07:00
end
end
end
local function onResourceDepleted(event)
local entity = event.entity
2021-02-19 21:41:30 -08:00
if entity.valid then
local map = maps.map[entity.surface.index]
2019-10-13 13:53:36 -07:00
unregisterResource(entity, map)
end
end
2019-03-10 12:28:43 -07:00
local function onRobotCliff(event)
2021-02-19 21:41:30 -08:00
local entity = event.robot
if entity.valid then
local map = maps.map[entity.surface.index]
if (event.item.name == "cliff-explosives") then
entityForPassScan(map, event.cliff)
end
2019-03-10 12:28:43 -07:00
end
end
local function onUsedCapsule(event)
local surface = game.players[event.player_index].surface
2021-02-19 21:41:30 -08:00
local map = maps.map[surface.index]
if (event.item.name == "cliff-explosives") then
2019-11-29 16:49:22 -08:00
map.position2Top.x = event.position.x-0.75
map.position2Top.y = event.position.y-0.75
map.position2Bottom.x = event.position.x+0.75
map.position2Bottom.y = event.position.y+0.75
local cliffs = surface.find_entities_filtered(map.cliffQuery)
2019-10-13 13:53:36 -07: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-19 21:41:30 -08:00
if entity.valid then
local native = natives[entity.surface.index]
native.rocketLaunched = native.rocketLaunched + 1
native.points = native.points + 5000
2019-02-02 22:01:28 -08:00
end
end
2019-02-18 16:43:01 -08:00
local function onTriggerEntityCreated(event)
local entity = event.entity
2021-02-19 21:41:30 -08:00
if entity.valid and (entity.name == "drain-trigger-rampant") then
local map = maps.map[entity.surface.index]
2019-02-18 16:43:01 -08:00
local chunk = getChunkByPosition(map, entity.position)
2020-05-15 13:51:38 -07:00
if (chunk ~= -1) then
2019-02-18 16:43:01 -08:00
map.chunkToDrained[chunk] = event.tick + 60
end
entity.destroy()
end
end
2016-09-12 15:33:00 -07:00
local function onInit()
2021-02-19 21:41:30 -08:00
global.maps = {}
2016-09-12 15:33:00 -07:00
global.natives = {}
2021-02-19 21:41:30 -08:00
global.queriesAndCommands = {}
2019-02-02 22:01:28 -08:00
2021-02-19 21:41:30 -08:00
maps = global.maps
queriesAndCommands = global.queriesAndCommands
2016-09-12 15:33:00 -07:00
natives = global.natives
2019-02-02 22:01:28 -08:00
2017-07-04 15:52:20 -07:00
hookEvents()
2021-02-19 21:41:30 -08:00
onConfigChanged()
prepWorld(game.surfaces["nauvis"])
2016-09-12 15:33:00 -07:00
end
2019-10-20 13:45:43 -07:00
local function onEntitySpawned(event)
2020-05-22 14:40:51 -07:00
local entity = event.mine
2021-02-19 21:41:30 -08:00
if natives.NEW_ENEMIES and entity.valid then
local native = natives[entity.surface.index]
local map = native.map
if natives.buildingHiveTypeLookup[entity.name] then
2020-05-22 14:40:51 -07:00
local disPos = mathUtils.distortPosition(entity.position, 8)
2020-05-16 22:06:55 -07:00
2020-05-22 14:40:51 -07:00
local chunk = getChunkByPosition(map, disPos)
if (chunk ~= -1) then
local base = findNearbyBase(map, chunk)
if not base then
2021-02-19 21:41:30 -08:00
base = createBase(native,
2020-05-22 14:40:51 -07:00
chunk,
event.tick)
end
2020-04-27 20:41:18 -07:00
2020-05-22 14:40:51 -07:00
entity = upgradeEntity(entity,
base.alignment,
2021-02-19 21:41:30 -08:00
native,
2020-05-22 14:40:51 -07:00
disPos)
2019-12-17 17:09:08 -08:00
2020-05-22 14:40:51 -07:00
if entity and entity.valid then
2021-02-19 21:41:30 -08:00
event.entity = registerEnemyBaseStructure(map, entity, base)
2019-11-03 22:19:22 -08:00
end
2019-11-29 16:49:22 -08:00
else
entity.destroy()
2019-11-03 22:19:22 -08:00
end
2019-11-29 16:49:22 -08:00
end
2019-10-20 13:45:43 -07:00
end
end
local function onUnitGroupCreated(event)
local group = event.group
2021-02-19 21:41:30 -08:00
if (group.force.name == "enemy") then
local surface = group.surface
local squad
2020-02-02 11:30:50 -08:00
if not group.is_script_driven then
2021-02-19 21:41:30 -08:00
local native = natives[surface.index]
2020-05-19 19:37:16 -07:00
if not natives.aiNocturnalMode then
2020-05-22 12:43:44 -07:00
local settler = mRandom() < 0.25 and
2021-02-19 21:41:30 -08:00
canMigrate(native) and
2020-05-24 16:41:12 -07:00
(natives.builderCount < natives.AI_MAX_BUILDER_COUNT)
if not settler and natives.squadCount > natives.AI_MAX_SQUAD_COUNT then
group.destroy()
2021-02-19 21:41:30 -08:00
native.points = native.points + AI_SQUAD_COST
2020-05-24 16:41:12 -07:00
return
end
2020-05-22 12:43:44 -07:00
squad = createSquad(nil, nil, group, settler)
2021-02-19 21:41:30 -08:00
native.groupNumberToSquad[group.group_number] = squad
2020-05-23 20:47:14 -07:00
2020-05-22 12:43:44 -07:00
if settler then
natives.builderCount = natives.builderCount + 1
2020-05-23 14:29:56 -07:00
else
natives.squadCount = natives.squadCount + 1
2020-05-22 12:43:44 -07:00
end
2020-05-19 19:37:16 -07:00
else
2020-05-24 16:41:12 -07:00
if not (surface.darkness > 0.65) then
2021-02-19 21:41:30 -08:00
native.points = native.points + AI_SQUAD_COST
2020-05-24 16:41:12 -07:00
group.destroy()
return
end
2020-05-25 17:05:01 -07:00
2020-05-22 12:43:44 -07:00
local settler = mRandom() < 0.25 and
2021-02-19 21:41:30 -08:00
canMigrate(native) and
2020-05-24 16:41:12 -07:00
(natives.builderCount < natives.AI_MAX_BUILDER_COUNT)
if not settler and natives.squadCount > natives.AI_MAX_SQUAD_COUNT then
group.destroy()
2021-02-19 21:41:30 -08:00
native.points = native.points + AI_SQUAD_COST
2020-05-24 16:41:12 -07:00
return
end
2020-05-22 12:43:44 -07:00
squad = createSquad(nil, nil, group, settler)
2020-05-19 19:37:16 -07:00
natives.groupNumberToSquad[group.group_number] = squad
2020-05-22 12:43:44 -07:00
if settler then
natives.builderCount = natives.builderCount + 1
2020-05-23 14:29:56 -07:00
else
natives.squadCount = natives.squadCount + 1
2020-05-22 12:43:44 -07:00
end
2020-05-19 19:37:16 -07:00
end
2020-02-02 11:30:50 -08:00
end
end
end
2020-05-16 22:06:55 -07:00
local function onGroupFinishedGathering(event)
local group = event.group
2020-05-25 17:05:01 -07:00
if group.valid and (group.force.name == "enemy") then
2020-05-16 22:06:55 -07:00
local unitNumber = group.group_number
2021-02-19 21:41:30 -08:00
local native = natives[group.surface.index]
local map = native.map
local squad = native.groupNumberToSquad[unitNumber]
if squad then
if squad.settler then
if (natives.builderCount < natives.AI_MAX_BUILDER_COUNT) then
squadDispatch(map, squad, unitNumber)
2020-05-23 22:13:57 -07:00
else
2021-02-19 21:41:30 -08:00
group.destroy()
native.points = native.points + AI_SETTLER_COST
2020-05-23 14:29:56 -07:00
end
else
2021-02-19 21:41:30 -08:00
if (natives.squadCount < natives.AI_MAX_SQUAD_COUNT) then
squadDispatch(map, squad, unitNumber)
else
2020-05-23 22:13:57 -07:00
group.destroy()
2021-02-19 21:41:30 -08:00
native.points = native.points + AI_SQUAD_COST
2020-05-23 14:29:56 -07:00
end
2021-02-19 21:41:30 -08:00
end
else
local settler = mRandom() < 0.25 and
canMigrate(native) and
(natives.builderCount < natives.AI_MAX_BUILDER_COUNT)
if not settler and natives.squadCount > natives.AI_MAX_SQUAD_COUNT then
group.destroy()
native.points = native.points + AI_SQUAD_COST
return
end
2020-05-25 17:05:01 -07:00
2021-02-19 21:41:30 -08:00
squad = createSquad(nil, nil, group, settler)
native.groupNumberToSquad[group.group_number] = squad
if settler then
natives.builderCount = natives.builderCount + 1
else
natives.squadCount = natives.squadCount + 1
2020-05-23 14:29:56 -07:00
end
2021-02-19 21:41:30 -08:00
squadDispatch(map, squad, unitNumber)
2020-05-16 22:06:55 -07:00
end
end
end
2019-05-09 17:46:57 -07:00
local function onForceCreated(event)
2021-02-19 21:41:30 -08:00
queriesAndCommands.activePlayerForces[#queriesAndCommands.activePlayerForces+1] = event.force.name
2019-05-09 17:46:57 -07:00
end
local function onForceMerged(event)
2021-02-19 21:41:30 -08:00
for i=#queriesAndCommands.activePlayerForces,1,-1 do
if (queriesAndCommands.activePlayerForces[i] == event.source_name) then
tRemove(queriesAndCommands.activePlayerForces, i)
2019-05-09 17:46:57 -07:00
break
end
2019-10-19 12:13:48 -07:00
end
2019-05-09 17:46:57 -07:00
end
2021-02-19 21:41:30 -08:00
local function onSurfaceCreated(event)
2020-04-27 20:41:18 -07:00
end
2021-02-19 21:41:30 -08:00
local function onSurfaceCleared(event)
local surfaceIndex = event.surface_index
-- maps.map[surfaceIndex] = nil
-- natives[surfaceIndex] = nil
2020-04-27 20:41:18 -07:00
end
local function onSurfaceDeleted(event)
2021-02-19 21:41:30 -08:00
local surfaceIndex = event.surface_index
maps.map[surfaceIndex] = nil
natives[surfaceIndex] = nil
2020-04-27 20:41:18 -07:00
end
2020-08-08 13:38:15 -07: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-19 21:41:30 -08:00
local targetPosition = queriesAndCommands.position
2020-08-08 13:38:15 -07:00
targetPosition.x = builder.position.x
targetPosition.y = builder.position.y
2021-02-19 21:41:30 -08:00
builder.surface.create_entity(queriesAndCommands.createBuildCloudQuery)
2020-08-08 13:38:15 -07:00
end
-- hooks
2020-04-27 20:41:18 -07:00
script.on_event(defines.events.on_tick,
2021-02-19 21:41:30 -08:00
function ()
2020-05-22 14:40:51 -07:00
local gameRef = game
2021-02-19 21:41:30 -08:00
local tick = gameRef.tick
2020-05-22 14:40:51 -07:00
local pick = tick % 7
2020-10-18 20:33:01 -07:00
-- local profiler = game.create_profiler()
2021-02-19 21:41:30 -08:00
local map = maps.activeMap
if (not map) or (map.processedChunks > map.totalChunks) then
maps.mapIterator, map = next(maps.map, maps.mapIterator)
maps.activeMap = map
print("initial", map, maps.mapIterator)
end
if not map then
print("after", map, maps.mapIterator)
maps.mapIterator, map = next(maps.map, maps.mapIterator)
if not map then
return
end
end
local native = map.native
2020-10-18 20:33:01 -07:00
2020-05-22 14:40:51 -07:00
if (pick == 0) then
2021-02-19 21:41:30 -08:00
processPendingChunks(map, tick)
processScanChunks(map)
2020-05-22 14:40:51 -07:00
elseif (pick == 1) then
2021-02-19 21:41:30 -08:00
processPlayers(gameRef.connected_players, map, tick)
cleanUpMapTables(map, tick)
2020-05-22 14:40:51 -07:00
elseif (pick == 2) then
2021-02-13 20:49:54 -08:00
processMap(map, tick)
2021-02-19 21:41:30 -08:00
planning(natives, native, gameRef.forces.enemy.evolution_factor, tick)
if natives.NEW_ENEMIES then
recycleBases(native, tick)
end
2020-05-22 14:40:51 -07:00
elseif (pick == 3) then
2021-02-13 20:49:54 -08:00
processStaticMap(map)
2021-02-19 21:41:30 -08:00
disperseVictoryScent(map)
cleanSquads(natives, native)
2020-05-22 14:40:51 -07:00
elseif (pick == 4) then
2021-02-19 21:41:30 -08:00
scanResourceMap(map, tick)
processVengence(map)
2020-05-22 14:40:51 -07:00
elseif (pick == 5) then
2021-02-19 21:41:30 -08:00
scanEnemyMap(map, tick)
processSpawners(map, tick)
temperamentPlanner(native)
2020-05-22 14:40:51 -07:00
elseif (pick == 6) then
2021-02-19 21:41:30 -08:00
scanPlayerMap(map, tick)
processNests(map, tick)
2020-05-22 14:40:51 -07:00
end
2021-02-19 21:41:30 -08:00
processActiveNests(map, tick)
2020-10-18 20:33:01 -07:00
-- game.print({"", "--dispatch4 ", profiler, ", ", game.tick, " ", mRandom()})
2020-04-27 20:41:18 -07:00
end)
script.on_event(defines.events.on_surface_cleared, onSurfaceCleared)
2021-02-19 21:41:30 -08:00
script.on_event(defines.events.on_surface_deleted, onSurfaceDeleted)
2020-04-27 20:41:18 -07:00
script.on_init(onInit)
script.on_load(onLoad)
2017-11-20 23:27:03 -08:00
script.on_event(defines.events.on_runtime_mod_setting_changed, onModSettingsChange)
2016-08-24 17:05:20 -07: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-24 16:41:12 -07:00
defines.events.on_robot_built_tile,
defines.events.script_raised_set_tiles}, onSurfaceTileChange)
script.on_event(defines.events.on_player_used_capsule, onUsedCapsule)
2019-02-18 16:43:01 -08:00
script.on_event(defines.events.on_trigger_created_entity, onTriggerEntityCreated)
2019-03-10 12:28:43 -07:00
script.on_event(defines.events.on_pre_robot_exploded_cliff, onRobotCliff)
2017-11-20 23:27:03 -08:00
script.on_event(defines.events.on_biter_base_built, onEnemyBaseBuild)
2017-08-13 17:35:44 -07:00
script.on_event({defines.events.on_player_mined_entity,
2020-05-13 14:45:27 -07:00
defines.events.on_robot_mined_entity}, onMine)
2020-05-15 13:51:38 -07:00
script.on_event({defines.events.on_built_entity,
defines.events.on_robot_built_entity,
2019-12-24 17:06:11 -08:00
defines.events.script_raised_built,
defines.events.script_raised_revive}, onBuild)
2016-09-09 03:02:50 -07:00
2020-05-22 14:40:51 -07:00
script.on_event(defines.events.on_land_mine_armed, onEntitySpawned)
script.on_event(defines.events.on_rocket_launched, onRocketLaunch)
2020-05-13 14:45:27 -07: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 11:30:50 -08:00
script.on_event(defines.events.on_unit_group_created, onUnitGroupCreated)
2019-05-09 17:46:57 -07:00
script.on_event(defines.events.on_force_created, onForceCreated)
script.on_event(defines.events.on_forces_merged, onForceMerged)
2020-05-16 22:06:55 -07:00
script.on_event(defines.events.on_unit_group_finished_gathering, onGroupFinishedGathering)
2020-08-08 13:38:15 -07:00
script.on_event(defines.events.on_build_base_arrived, onBuilderArrived)
2017-05-07 23:56:11 -07:00
remote.add_interface("rampantTests",
2019-10-13 13:53:36 -07: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,
2019-02-20 22:31:47 -08:00
scanChunkPaths = tests.scanChunkPaths
2019-10-13 13:53:36 -07:00
}
2017-05-07 23:56:11 -07:00
)
2017-05-06 02:03:28 -07:00
2021-02-19 21:41:30 -08:00
-- remote.add_interface("rampant", interop)