1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-03 22:52:20 +02:00
Rampant/control.lua

1397 lines
56 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 movementUtils = require("libs/MovementUtils")
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")
2017-05-06 11:03:28 +02:00
local interop = require("libs/Interop")
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")
2017-06-01 09:03:07 +02:00
local config = require("config")
2020-05-17 07:06:55 +02:00
local aiPredicates = require("libs/AIPredicates")
2016-10-15 02:00:18 +02:00
-- constants
2020-05-20 04:37:16 +02:00
local DIVISOR_DEATH_TRAIL_TABLE = constants.DIVISOR_DEATH_TRAIL_TABLE
local TRIPLE_CHUNK_SIZE = constants.TRIPLE_CHUNK_SIZE
2016-10-15 02:00:18 +02:00
local INTERVAL_LOGIC = constants.INTERVAL_LOGIC
2019-03-06 08:18:03 +02:00
local INTERVAL_PLAYER_PROCESS = constants.INTERVAL_PLAYER_PROCESS
local INTERVAL_MAP_PROCESS = constants.INTERVAL_MAP_PROCESS
local INTERVAL_ENEMY_SCAN = constants.INTERVAL_ENEMY_SCAN
local INTERVAL_SCAN = constants.INTERVAL_SCAN
local INTERVAL_PLAYER_SCAN = constants.INTERVAL_PLAYER_SCAN
local INTERVAL_SQUAD = constants.INTERVAL_SQUAD
2019-10-20 22:45:43 +02:00
local INTERVAL_RESQUAD = constants.INTERVAL_RESQUAD
local INTERVAL_TEMPERAMENT = constants.INTERVAL_TEMPERAMENT
2020-05-20 04:37:16 +02:00
local INTERVAL_SPAWNER = constants.INTERVAL_SPAWNER
2020-05-21 03:03:32 +02:00
local INTERVAL_CHUNK_PROCESS = constants.INTERVAL_CHUNK_PROCESS
local INTERVAL_PASS_SCAN = constants.INTERVAL_PASS_SCAN
local INTERVAL_NEST = constants.INTERVAL_NEST
local INTERVAL_CLEANUP = constants.INTERVAL_CLEANUP
local INTERVAL_MAP_STATIC_PROCESS = constants.INTERVAL_MAP_STATIC_PROCESS
2016-10-15 02:00:18 +02:00
2019-11-04 08:19:22 +02:00
local HIVE_BUILDINGS = constants.HIVE_BUILDINGS
2020-05-22 21:43:44 +02:00
local AI_MAX_BUILDER_COUNT = constants.AI_MAX_BUILDER_COUNT
2020-05-23 23:29:56 +02:00
local AI_MAX_SQUAD_COUNT = constants.AI_MAX_SQUAD_COUNT
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
2019-04-08 07:22:02 +02:00
local DOUBLE_CHUNK_SIZE = constants.DOUBLE_CHUNK_SIZE
2019-04-06 05:01:46 +02:00
local AI_MAX_BITER_GROUP_SIZE = constants.AI_MAX_BITER_GROUP_SIZE
2018-10-20 07:17:37 +02:00
local PROCESS_QUEUE_SIZE = constants.PROCESS_QUEUE_SIZE
2018-05-19 22:16:16 +02:00
local WATER_TILE_NAMES = constants.WATER_TILE_NAMES
2017-01-20 07:58:36 +02:00
local MOVEMENT_PHEROMONE = constants.MOVEMENT_PHEROMONE
local RETREAT_GRAB_RADIUS = constants.RETREAT_GRAB_RADIUS
local RETREAT_SPAWNER_GRAB_RADIUS = constants.RETREAT_SPAWNER_GRAB_RADIUS
2020-05-20 04:37:16 +02:00
local DEFINES_BEHAVIOR_RESULT_FAIL = defines.behavior_result.fail
2018-01-14 07:48:21 +02:00
local DEFINES_COMMAND_GROUP = defines.command.group
2019-04-08 07:22:02 +02:00
local DEFINES_COMMAND_WANDER = defines.command.wander
2018-02-13 09:10:17 +02:00
local DEFINES_COMMAND_BUILD_BASE = defines.command.build_base
2018-01-14 07:48:21 +02:00
local DEFINES_COMMAND_ATTACK_AREA = defines.command.attack_area
2019-03-08 05:40:55 +02:00
local DEFINES_COMMAND_GO_TO_LOCATION = defines.command.go_to_location
2019-03-10 05:57:58 +02:00
local DEFINES_COMMMAD_COMPOUND = defines.command.compound
local DEFINES_COMMAND_FLEE = defines.command.flee
2019-10-06 20:57:33 +02:00
local DEFINES_COMMAND_STOP = defines.command.stop
2019-03-10 05:57:58 +02:00
local DEFINES_COMPOUND_COMMAND_RETURN_LAST = defines.compound_command.return_last
local DEFINES_COMPOUND_COMMAND_AND = defines.compound_command.logical_and
local DEFINES_COMPOUND_COMMAND_OR = defines.compound_command.logical_or
2018-01-14 07:48:21 +02:00
local CHUNK_SIZE = constants.CHUNK_SIZE
local DEFINES_DISTRACTION_NONE = defines.distraction.none
local DEFINES_DISTRACTION_BY_ENEMY = defines.distraction.by_enemy
2019-03-08 05:40:55 +02:00
local DEFINES_DISTRACTION_BY_ANYTHING = defines.distraction.by_anything
2018-01-14 07:48:21 +02:00
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
2019-11-13 07:21:55 +02:00
-- local POISON_LOOKUP = constants.POISON_LOOKUP
2019-02-14 07:53:31 +02:00
2017-06-01 03:46:53 +02:00
-- imported functions
2017-01-20 07:58:36 +02:00
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
2019-10-19 21:13:48 +02:00
local getPlayerBaseGenerator = chunkPropertyUtils.getPlayerBaseGenerator
2017-01-20 07:58:36 +02:00
local getChunkByPosition = mapUtils.getChunkByPosition
2018-01-02 20:36:23 +02:00
local entityForPassScan = chunkUtils.entityForPassScan
2020-05-23 23:29:56 +02:00
local addMovementPenalty = movementUtils.addMovementPenalty
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
local regroupSquads = unitGroupUtils.regroupSquads
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 removeSquadFromChunk = chunkPropertyUtils.removeSquadFromChunk
local addDeathGenerator = chunkPropertyUtils.addDeathGenerator
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 registerEnemyBaseStructure = chunkUtils.registerEnemyBaseStructure
local makeImmortalEntity = chunkUtils.makeImmortalEntity
2019-05-16 07:11:43 +02:00
local registerResource = chunkUtils.registerResource
local unregisterResource = chunkUtils.unregisterResource
2020-05-17 07:06:55 +02:00
local cleanSquads = unitGroupUtils.cleanSquads
2018-01-15 01:10:56 +02:00
local upgradeEntity = baseUtils.upgradeEntity
local rebuildNativeTables = baseUtils.rebuildNativeTables
2016-10-15 02:00:18 +02:00
2020-05-20 04:37:16 +02:00
local mMin = math.min
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
2020-04-28 05:41:18 +02:00
local gameSurfaces -- used for manage which surfaces have been visited
2018-01-14 07:48:21 +02:00
local map -- manages the chunks that make up the game world
local natives -- manages the enemy units, structures, and ai
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
--]]
local surface = event.surface
2020-04-28 05:41:18 +02:00
if (surface.name == natives.activeSurface) then
natives.ionCannonBlasts = natives.ionCannonBlasts + 1
2019-10-13 22:53:36 +02:00
natives.points = natives.points + 3000
local chunk = getChunkByPosition(map, event.position)
2020-05-15 22:51:38 +02:00
if (chunk ~= -1) then
2019-11-30 02:49:22 +02:00
rallyUnits(chunk, map, surface, event.tick)
2019-10-13 22:53:36 +02:00
end
2019-02-03 08:01:28 +02:00
end
2017-07-05 00:52:20 +02:00
end
local function hookEvents()
if config.ionCannonPresent 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()
2018-01-14 07:48:21 +02:00
map = global.map
2016-08-25 02:05:20 +02:00
natives = global.natives
2020-04-28 05:41:18 +02:00
gameSurfaces = global.gameSurfaces
2017-07-05 00:52:20 +02:00
hookEvents()
end
2016-09-13 00:33:00 +02:00
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.
2020-04-28 05:41:18 +02:00
if (event.surface.name == natives.activeSurface) then
2020-05-20 04:37:16 +02:00
map.pendingChunks[event] = true
2016-09-13 00:33:00 +02:00
end
2016-08-25 02:05:20 +02:00
end
2018-01-15 01:10:56 +02:00
local function rebuildMap()
2020-04-28 05:41:18 +02:00
game.get_surface(natives.activeSurface).print("Rampant - Reindexing chunks, please wait.")
2018-01-14 07:48:21 +02:00
-- clear old map processing Queue
2017-05-28 06:50:37 +02:00
-- prevents queue adding duplicate chunks
-- chunks are by key, so should overwrite old
2017-06-01 03:46:53 +02:00
2018-01-14 07:48:21 +02:00
global.map = {}
map = global.map
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
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 = {}
2018-01-14 07:48:21 +02:00
map.chunkToPlayerBase = {}
map.chunkToResource = {}
map.chunkToPlayerCount = {}
map.playerToChunk = {}
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 = {}
2019-02-20 08:16:43 +02:00
map.chunkToActiveNest = {}
map.chunkToActiveRaidNest = {}
2018-09-24 06:56:45 +02:00
2019-11-30 02:49:22 +02:00
map.nextChunkSort = 0
map.nextChunkSortTick = 0
2019-02-03 08:01:28 +02:00
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-17 07:06:55 +02:00
-- preallocating memory to be used in code, making it fast by reducing garbage generated.
map.neighbors = {
2020-05-15 22:51:38 +02:00
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1
}
map.cardinalNeighbors = {
2020-05-15 22:51:38 +02:00
-1,
-1,
-1,
-1
}
map.position = {
x=0,
y=0
}
map.position2 = {
x=0,
y=0
}
2020-04-28 05:41:18 +02:00
map.position3 = {
x=0,
y=0
}
2017-12-21 05:50:36 +02:00
2018-10-20 07:17:37 +02:00
map.scentStaging = {}
for x=1,PROCESS_QUEUE_SIZE+1 do
2019-10-13 22:53:36 +02:00
map.scentStaging[x] = {0,0,0,0}
2018-10-20 07:17:37 +02:00
end
2019-02-03 08:01:28 +02:00
2019-12-07 07:57:20 +02:00
map.chunkScanCounts = {}
map.chunkOverlapArray = {
2020-05-15 22:51:38 +02:00
-1,
-1,
-1,
-1
}
2019-11-30 02:49:22 +02:00
2019-10-20 22:45:43 +02:00
map.enemiesToSquad = {}
map.enemiesToSquad.len = 0
map.chunkRemovals = {}
2020-04-28 05:41:18 +02:00
map.processActiveNest = {}
map.tickActiveNest = {}
2019-10-20 22:45:43 +02:00
map.emptySquadsOnChunk = {}
map.position2Top = {0, 0}
map.position2Bottom = {0, 0}
2017-12-21 05:50:36 +02:00
--this is shared between two different queries
2018-01-14 07:48:21 +02:00
map.area = {{0, 0}, {0, 0}}
2019-02-21 08:31:47 +02:00
map.testArea = {{0, 0}, {0, 0}}
map.area2 = {map.position2Top, map.position2Bottom}
2019-04-08 07:22:02 +02:00
map.buildPositionTop = {0, 0}
2019-05-01 04:08:21 +02:00
map.buildPositionBottom = {0, 0}
2019-04-09 01:30:09 +02:00
map.buildArea = {map.buildPositionTop, map.buildPositionBottom}
2018-01-14 07:48:21 +02:00
map.countResourcesQuery = { area=map.area, type="resource" }
2019-04-07 06:41:00 +02:00
map.filteredEntitiesUnitQuery = { area=map.area, force="enemy",type="unit" }
2019-04-09 01:30:09 +02:00
map.filteredEntitiesClearBuildingQuery = { area=map.buildArea, force="neutral",collision_mask="player-layer" }
2018-01-14 07:48:21 +02:00
map.filteredEntitiesEnemyUnitQuery = { area=map.area, force="enemy", type="unit", limit=301 }
map.hasPlayerStructuresQuery = { area=map.area, force={"enemy","neutral"}, invert=true, limit=1 }
2020-03-16 00:16:00 +02:00
map.filteredEntitiesEnemyStructureQuery = { area=map.area, force="enemy", type={"turret","unit-spawner"} }
2019-11-30 02:49:22 +02:00
map.filteredEntitiesPointQueryLimited = {
position = map.position,
radius = 10,
limit = 1,
force = "enemy",
type = {
"unit-spawner",
"turret"
}
}
2020-05-20 04:37:16 +02:00
map.createBuildCloudQuery = {
name = "build-clear-cloud-rampant",
position = map.position
}
2019-11-30 02:49:22 +02:00
2019-05-10 02:46:57 +02:00
map.activePlayerForces = {"player"}
for _,force in pairs(game.forces) do
local add = true
if (force.name ~= "neutral") and (force.name ~= "enemy") then
for i=1,#map.activePlayerForces do
if (map.activePlayerForces[i] == force.name) then
add = false
break
end
end
if add then
map.activePlayerForces[#map.activePlayerForces+1] = force.name
end
end
end
map.filteredEntitiesPlayerQueryLowest = { area=map.area,
force=map.activePlayerForces,
collision_mask = "player-layer",
type={"wall",
"transport-belt"}}
map.filteredEntitiesPlayerQueryLow = { area=map.area,
force=map.activePlayerForces,
collision_mask = "player-layer",
type={"splitter",
"pump",
"offshore-pump",
"lamp",
"solar-panel",
"programmable-speaker",
"accumulator",
"assembling-machine",
"turret",
"ammo-turret"}}
map.filteredEntitiesPlayerQueryHigh = { area=map.area,
force=map.activePlayerForces,
collision_mask = "player-layer",
type={"furnace",
"lab",
"roboport",
"beacon",
"radar",
"electric-turret",
"boiler",
"generator",
"fluid-turret",
"mining-drill"}}
map.filteredEntitiesPlayerQueryHighest = { area=map.area,
force=map.activePlayerForces,
collision_mask = "player-layer",
type={"artillery-turret",
"reactor",
"rocket-silo"}
}
2019-05-10 02:46:57 +02:00
2019-02-21 08:31:47 +02:00
local sharedArea = {{0,0},{0,0}}
map.filteredEntitiesCliffQuery = { area=sharedArea, type="cliff", limit = 1 }
2019-05-03 21:32:59 +02:00
map.filteredTilesPathQuery = { area=sharedArea, collision_mask="water-tile", limit = 1 }
2019-11-30 02:49:22 +02:00
map.cliffQuery = {
area=map.area2,
type="cliff"
}
2018-01-14 07:48:21 +02:00
map.canPlaceQuery = { name="", position={0,0} }
2019-05-03 21:32:59 +02:00
map.filteredTilesQuery = { collision_mask="water-tile", area=map.area }
2019-02-03 08:01:28 +02:00
2019-11-30 02:49:22 +02:00
map.upgradeEntityQuery = {
name = "",
position = nil
}
2019-03-08 05:40:55 +02:00
map.attackCommand = {
2019-10-13 22:53:36 +02:00
type = DEFINES_COMMAND_ATTACK_AREA,
destination = map.position,
radius = CHUNK_SIZE * 1.5,
distraction = DEFINES_DISTRACTION_BY_ANYTHING
}
2019-03-08 05:40:55 +02:00
map.moveCommand = {
2019-10-13 22:53:36 +02:00
type = DEFINES_COMMAND_GO_TO_LOCATION,
destination = map.position,
2019-10-19 21:13:48 +02:00
radius = 2,
pathfind_flags = { prefer_straight_paths = true, cache = true },
2019-10-13 22:53:36 +02:00
distraction = DEFINES_DISTRACTION_BY_ENEMY
2018-01-14 07:48:21 +02:00
}
2018-02-13 09:10:17 +02:00
map.settleCommand = {
2019-10-13 22:53:36 +02:00
type = DEFINES_COMMAND_BUILD_BASE,
destination = map.position,
distraction = DEFINES_DISTRACTION_BY_ENEMY,
ignore_planner = true
2019-05-01 04:08:21 +02:00
}
2019-02-03 08:01:28 +02:00
2019-04-08 07:22:02 +02:00
map.wonderCommand = {
type = DEFINES_COMMAND_WANDER,
wander_in_group = false,
2019-10-19 21:13:48 +02:00
radius = TRIPLE_CHUNK_SIZE,
ticks_to_wait = 3600
2019-04-08 07:22:02 +02:00
}
2019-05-01 04:08:21 +02:00
2019-10-19 23:04:38 +02:00
map.wonder2Command = {
type = DEFINES_COMMAND_WANDER,
wander_in_group = false,
radius = TRIPLE_CHUNK_SIZE,
ticks_to_wait = 360
}
2020-05-23 23:29:56 +02:00
map.wonder3Command = {
type = DEFINES_COMMAND_WANDER,
wander_in_group = true,
radius = TRIPLE_CHUNK_SIZE,
distraction = DEFINES_DISTRACTION_BY_ANYTHING,
ticks_to_wait = 60 * 30
}
2019-10-06 20:57:33 +02:00
map.stopCommand = {
type = DEFINES_COMMAND_STOP
}
2019-04-08 07:22:02 +02:00
map.compoundSettleCommand = {
type = DEFINES_COMMMAD_COMPOUND,
structure_type = DEFINES_COMPOUND_COMMAND_RETURN_LAST,
commands = {
2019-10-19 23:04:38 +02:00
map.wonder2Command,
2019-04-08 07:22:02 +02:00
map.settleCommand
}
}
2019-05-01 04:08:21 +02:00
2019-03-07 08:12:39 +02:00
map.retreatCommand = {
type = DEFINES_COMMAND_GROUP,
group = nil,
distraction = DEFINES_DISTRACTION_NONE,
use_group_distraction = false
}
2019-02-03 08:01:28 +02:00
2020-05-20 04:37:16 +02:00
map.mergeGroupCommand = {
type = DEFINES_COMMAND_GROUP,
group = nil,
distraction = DEFINES_DISTRACTION_NONE,
use_group_distraction = false
}
2019-03-10 05:57:58 +02:00
map.fleeCommand = {
type = DEFINES_COMMAND_FLEE,
from = nil,
distraction = DEFINES_DISTRACTION_NONE
}
2019-05-01 04:08:21 +02:00
2019-03-10 05:57:58 +02:00
map.compoundRetreatCommand = {
type = DEFINES_COMMMAD_COMPOUND,
structure_type = DEFINES_COMPOUND_COMMAND_RETURN_LAST,
commands = {
map.fleeCommand,
map.retreatCommand
}
}
map.formGroupCommand = { type = DEFINES_COMMAND_GROUP,
group = nil,
2020-05-23 23:29:56 +02:00
distraction = DEFINES_DISTRACTION_BY_ANYTHING,
2019-10-20 22:45:43 +02:00
use_group_distraction = false
}
2019-05-01 04:08:21 +02:00
map.formLocalGroupCommand = { type = DEFINES_COMMAND_GROUP,
group = nil,
2020-05-23 23:29:56 +02:00
distraction = DEFINES_DISTRACTION_BY_ANYTHING,
use_group_distraction = false
}
map.formCommand = { command = map.formGroupCommand,
unit_count = 0,
unit_search_distance = TRIPLE_CHUNK_SIZE }
2020-05-21 03:03:32 +02:00
map.formLocalCommand = { command = map.formLocalGroupCommand,
unit_count = AI_MAX_BITER_GROUP_SIZE,
2020-05-21 03:03:32 +02:00
unit_search_distance = CHUNK_SIZE }
2019-02-03 08:01:28 +02:00
end
2017-06-24 20:41:57 +02:00
2017-05-28 06:50:37 +02:00
2017-05-06 11:03:28 +02:00
local function onModSettingsChange(event)
2019-02-03 08:01:28 +02:00
2020-03-16 00:42:13 +02:00
if event and ((string.sub(event.setting, 1, 7) ~= "rampant") or
(string.sub(event.setting, 1, 15) == "rampant-arsenal") or
2020-04-28 05:41:18 +02:00
(string.sub(event.setting, 1, 17) == "rampant-resources") or
(string.sub(event.setting, 1, 17) == "rampant-evolution"))
2020-03-16 00:42:13 +02:00
then
2019-10-13 22:53:36 +02:00
return false
2017-05-06 11:03:28 +02:00
end
2019-02-03 08:01:28 +02:00
upgrade.compareTable(natives, "safeBuildings", settings.global["rampant-safeBuildings"].value)
2017-06-01 03:46:53 +02:00
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)
2017-06-03 03:24:56 +02:00
upgrade.compareTable(natives.safeEntities, "lamp", settings.global["rampant-safeBuildings-lamps"].value)
2017-06-01 03:46:53 +02:00
2020-04-28 05:41:18 +02:00
local changed, newValue = upgrade.compareTable(natives.safeEntities,
2019-10-13 22:53:36 +02:00
"big-electric-pole",
settings.global["rampant-safeBuildings-bigElectricPole"].value)
2017-06-01 03:46:53 +02:00
if changed then
2020-04-28 05:41:18 +02:00
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
2017-06-01 03:46:53 +02:00
end
2019-02-03 08:01:28 +02:00
upgrade.compareTable(natives, "deadZoneFrequency", settings.global["rampant-deadZoneFrequency"].value)
upgrade.compareTable(natives, "raidAIToggle", settings.global["rampant-raidAIToggle"].value)
2019-02-03 08:01:28 +02:00
2020-02-02 05:47:51 +02:00
upgrade.compareTable(natives, "attackPlayerThreshold", settings.global["rampant-attackPlayerThreshold"].value)
upgrade.compareTable(natives, "attackUsePlayer", settings.global["rampant-attackWaveGenerationUsePlayerProximity"].value)
2020-04-28 05:41:18 +02:00
2017-06-01 03:46:53 +02:00
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)
2019-02-03 08:01:28 +02:00
upgrade.compareTable(natives, "newEnemies", settings.startup["rampant-newEnemies"].value)
upgrade.compareTable(natives, "enemySeed", settings.startup["rampant-enemySeed"].value)
2019-05-01 04:08:21 +02:00
natives.enabledMigration = natives.expansion and settings.global["rampant-enableMigration"].value
2019-02-03 08:01:28 +02:00
upgrade.compareTable(natives, "ENEMY_VARIATIONS", settings.startup["rampant-newEnemyVariations"].value)
2019-11-30 02:49:22 +02:00
2017-05-28 06:50:37 +02:00
return true
end
2017-05-27 02:58:33 +02:00
2020-04-28 05:41:18 +02:00
local function prepWorld(rebuild, surfaceName)
local upgraded
2020-04-28 05:41:18 +02:00
if surfaceName then
natives.activeSurface = surfaceName
if rebuild then
global.version = nil
end
2018-05-24 02:25:08 +02:00
end
2018-05-24 02:40:05 +02:00
2020-04-28 05:41:18 +02:00
upgraded, natives = upgrade.attempt(natives, surfaceName, gameSurfaces)
2020-05-05 04:19:17 +02:00
gameSurfaces = global.gameSurfaces
2018-05-24 02:40:05 +02:00
onModSettingsChange(nil)
if natives.newEnemies then
2020-04-28 05:41:18 +02:00
rebuildNativeTables(natives, game.get_surface(natives.activeSurface), game.create_random_generator(natives.enemySeed))
2019-12-07 07:57:20 +02: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
2018-05-19 22:16:16 +02:00
if upgraded then
2019-10-13 22:53:36 +02:00
rebuildMap()
2019-11-30 02:49:22 +02:00
map.natives = natives
natives.map = map
2019-10-13 22:53:36 +02:00
-- queue all current chunks that wont be generated during play
2020-04-28 05:41:18 +02:00
local surface = game.get_surface(natives.activeSurface)
2019-10-13 22:53:36 +02:00
local tick = game.tick
2019-11-30 02:49:22 +02:00
local position = {0,0}
2019-02-20 08:16:43 +02:00
natives.nextChunkSort = 0
2019-10-13 22:53:36 +02:00
for chunk in surface.get_chunks() do
2019-11-30 02:49:22 +02:00
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}}})
end
2019-10-13 22:53:36 +02:00
end
2020-05-20 04:37:16 +02:00
processPendingChunks(map, surface, tick, rebuild, true)
end
end
local function onConfigChanged()
2020-04-28 05:41:18 +02:00
prepWorld(true, natives.activeSurface)
end
2016-09-13 00:33:00 +02:00
local function onBuild(event)
2020-05-22 23:40:51 +02:00
local profiler = game.create_profiler()
local entity = event.created_entity or event.entity
2020-04-28 05:41:18 +02:00
if (entity.surface.name == natives.activeSurface) then
2019-05-16 07:11:43 +02:00
if (entity.type == "resource") and (entity.force.name == "neutral") then
registerResource(entity, map)
else
2019-11-30 02:49:22 +02:00
accountPlayerEntity(entity, natives, true, false)
2019-05-16 07:11:43 +02:00
if natives.safeBuildings then
2020-04-28 05:41:18 +02:00
if natives.safeEntities[entity.type] or natives.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
2020-05-22 23:40:51 +02:00
-- game.print({"", "onBuild", profiler, event.tick})
end
2017-08-08 10:19:51 +02:00
local function onMine(event)
2020-05-22 23:40:51 +02:00
local profiler = game.create_profiler()
local entity = event.entity
local surface = entity.surface
2020-04-28 05:41:18 +02:00
if (surface.name == natives.activeSurface) then
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
2019-11-30 02:49:22 +02:00
accountPlayerEntity(entity, natives, false, false)
2019-05-16 07:11:43 +02:00
end
end
2020-05-22 23:40:51 +02:00
-- game.print({"", "onMine", profiler, event.tick})
end
2016-09-13 00:33:00 +02:00
local function onDeath(event)
2020-05-22 23:40:51 +02:00
local profiler = game.create_profiler()
local entity = event.entity
2020-05-15 22:51:38 +02:00
if entity.valid then
local surface = entity.surface
if (surface.name == natives.activeSurface) then
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
2019-02-03 08:01:28 +02:00
2020-05-15 22:51:38 +02:00
local artilleryBlast = (cause and ((cause.type == "artillery-wagon") or (cause.type == "artillery-turret")))
2019-05-01 04:08:21 +02:00
2020-05-15 22:51:38 +02:00
if artilleryBlast then
natives.artilleryBlasts = natives.artilleryBlasts + 1
end
2020-05-15 22:51:38 +02:00
if (entityType == "unit") then
if (chunk ~= -1) then
-- drop death pheromone where unit died
deathScent(map, chunk)
2019-10-13 22:53:36 +02:00
2020-05-15 22:51:38 +02:00
if event.force and (event.force.name ~= "enemy") and (chunk[MOVEMENT_PHEROMONE] < -natives.retreatThreshold) then
2019-10-13 22:53:36 +02:00
2020-05-15 22:51:38 +02:00
natives.lostEnemyUnits = natives.lostEnemyUnits + 1
2020-05-20 04:37:16 +02:00
-- retreatUnits(chunk,
-- entityPosition,
-- entity.unit_group,
-- map,
-- surface,
-- tick,
-- (artilleryBlast and RETREAT_SPAWNER_GRAB_RADIUS) or RETREAT_GRAB_RADIUS-- ,
-- -- artilleryBlast
-- )
2019-10-13 22:53:36 +02:00
2020-05-15 22:51:38 +02:00
if (mRandom() < natives.rallyThreshold) and not surface.peaceful_mode then
rallyUnits(chunk, map, surface, tick)
end
2019-10-13 22:53:36 +02:00
end
end
2019-02-03 08:01:28 +02:00
2020-05-15 22:51:38 +02:00
elseif event.force and (event.force.name ~= "enemy") and ((entityType == "unit-spawner") or (entityType == "turret")) then
2020-05-15 22:51:38 +02:00
natives.points = natives.points + (((entityType == "unit-spawner") and RECOVER_NEST_COST) or RECOVER_WORM_COST)
2019-05-01 04:08:21 +02:00
2020-05-20 04:37:16 +02:00
unregisterEnemyBaseStructure(map, entity)
2019-10-13 22:53:36 +02:00
2020-05-20 04:37:16 +02:00
if (chunk ~= -1) then
2020-05-15 22:51:38 +02:00
rallyUnits(chunk, map, surface, tick)
2019-10-13 22:53:36 +02:00
2020-05-20 04:37:16 +02:00
-- retreatUnits(chunk,
-- entityPosition,
-- nil,
-- map,
-- surface,
-- tick,
-- RETREAT_SPAWNER_GRAB_RADIUS-- ,
-- -- (cause and ((cause.type == "artillery-wagon") or (cause.type == "artillery-turret")))
-- )
2020-05-15 22:51:38 +02:00
end
2019-10-13 22:53:36 +02:00
end
2019-02-19 02:43:01 +02:00
2020-05-15 22:51:38 +02:00
local pair = natives.drainPylons[entity.unit_number]
if pair then
local target = pair[1]
local pole = pair[2]
if target == entity then
natives.drainPylons[entity.unit_number] = nil
if pole.valid then
natives.drainPylons[pole.unit_number] = nil
pole.die()
end
elseif (pole == entity) then
natives.drainPylons[entity.unit_number] = nil
if target.valid then
natives.drainPylons[target.unit_number] = nil
target.destroy()
end
2019-02-19 02:43:01 +02:00
end
end
2020-05-15 22:51:38 +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
2020-05-15 22:51:38 +02:00
local drained = (entityType == "electric-turret") and map.chunkToDrained[chunk]
if (cause ~= nil) 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({position=entity.position,
name="pylon-target-rampant",
direction=entity.direction})
targetEntity.backer_name = ""
local pair = {targetEntity, newEntity}
natives.drainPylons[targetEntity.unit_number] = pair
natives.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);
end
2019-02-19 02:43:01 +02:00
end
2020-05-15 22:51:38 +02:00
for _,v in pairs(wires.red) do
if (v.valid) then
newEntity.connect_neighbour({wire = DEFINES_WIRE_TYPE_RED, target_entity = v});
end
2019-02-19 02:43:01 +02:00
end
2020-05-15 22:51:38 +02:00
for _,v in pairs(wires.green) do
if (v.valid) then
newEntity.connect_neighbour({wire = DEFINES_WIRE_TYPE_GREEN, target_entity = v});
end
2019-02-19 02:43:01 +02:00
end
2019-02-13 07:50:25 +02:00
end
2020-05-15 22:51:38 +02:00
elseif newEntity.backer_name then
newEntity.backer_name = ""
2019-10-13 22:53:36 +02:00
end
end
end
end
2020-05-15 22:51:38 +02:00
elseif (entity.type == "resource") and (entity.force.name == "neutral") then
if (entity.amount == 0) then
unregisterResource(entity, map)
end
2019-10-13 22:53:36 +02:00
end
2020-05-15 22:51:38 +02:00
if creditNatives and natives.safeBuildings and (natives.safeEntities[entityType] or natives.safeEntities[entity.name]) then
makeImmortalEntity(surface, entity)
else
accountPlayerEntity(entity, natives, false, creditNatives)
2020-05-13 23:45:27 +02:00
end
2019-10-13 22:53:36 +02:00
end
end
end
2020-05-22 23:40:51 +02:00
-- game.print({"", "onDeath", profiler, event.tick})
end
2017-06-01 03:46:53 +02:00
local function onEnemyBaseBuild(event)
2020-05-22 23:40:51 +02:00
local profiler = game.create_profiler()
local entity = event.entity
2020-05-17 07:06:55 +02:00
if entity.valid then
2020-05-15 22:51:38 +02:00
local surface = entity.surface
2020-05-15 22:51:38 +02:00
if (surface.name == natives.activeSurface) then
local chunk = getChunkByPosition(map, entity.position)
if (chunk ~= -1) then
local base
if natives.newEnemies then
base = findNearbyBase(map, chunk)
if not base then
base = createBase(natives,
chunk,
event.tick)
end
entity = upgradeEntity(entity,
surface,
base.alignment,
natives,
nil,
true)
end
if entity and entity.valid then
event.entity = registerEnemyBaseStructure(map, entity, base, surface)
2019-05-01 04:08:21 +02:00
end
end
end
end
2020-05-22 23:40:51 +02:00
-- game.print({"", "baseBuild", profiler, event.tick})
2017-06-01 03:46:53 +02:00
end
local function onSurfaceTileChange(event)
2020-05-22 23:40:51 +02:00
local profiler = game.create_profiler()
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)
2020-05-20 04:37:16 +02:00
local surface = game.get_surface(natives.activeSurface)
if (surface.index == surfaceIndex) then
2019-10-13 22:53:36 +02:00
local chunks = {}
local tiles = event.tiles
2020-05-20 04:37:16 +02:00
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)
2019-10-13 22:53:36 +02:00
2020-05-20 04:37:16 +02:00
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
end
end
if addMe then
local chunkXY = {x=x,y=y}
chunks[#chunks+1] = chunkXY
onChunkGenerated({area = { left_top = chunkXY },
surface = surface})
2019-10-13 22:53:36 +02:00
end
end
end
end
end
2020-05-22 23:40:51 +02:00
-- game.print({"", "tileChange", profiler, event.tick})
end
local function onResourceDepleted(event)
2020-05-22 23:40:51 +02:00
local profiler = game.create_profiler()
local entity = event.entity
2020-04-28 05:41:18 +02:00
if (entity.surface.name == natives.activeSurface) then
2019-10-13 22:53:36 +02:00
unregisterResource(entity, map)
end
2020-05-22 23:40:51 +02:00
-- game.print({"", "resourceDepleted", profiler, event.tick})
end
2019-03-10 21:28:43 +02:00
local function onRobotCliff(event)
2020-05-22 23:40:51 +02:00
local profiler = game.create_profiler()
2019-03-10 21:28:43 +02:00
local surface = event.robot.surface
2020-05-15 22:51:38 +02:00
if (surface.name == natives.activeSurface) and (event.item.name == "cliff-explosives") then
2019-03-10 21:28:43 +02:00
entityForPassScan(map, event.cliff)
end
2020-05-22 23:40:51 +02:00
-- game.print({"", "cliff", profiler, event.tick})
2019-03-10 21:28:43 +02:00
end
local function onUsedCapsule(event)
2020-05-22 23:40:51 +02:00
local profiler = game.create_profiler()
local surface = game.players[event.player_index].surface
2020-05-15 22:51:38 +02:00
if (surface.name == natives.activeSurface) and (event.item.name == "cliff-explosives") then
2019-11-30 02:49:22 +02: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 22:53:36 +02:00
for i=1,#cliffs do
entityForPassScan(map, cliffs[i])
end
end
2020-05-22 23:40:51 +02:00
-- game.print({"", "capsule", profiler, event.tick})
end
local function onRocketLaunch(event)
local entity = event.rocket_silo or event.rocket
2020-04-28 05:41:18 +02:00
if entity and entity.valid and (entity.surface.name == natives.activeSurface) then
natives.rocketLaunched = natives.rocketLaunched + 1
2020-05-17 00:34:54 +02:00
natives.points = natives.points + 5000
2019-02-03 08:01:28 +02:00
end
end
2019-02-19 02:43:01 +02:00
local function onTriggerEntityCreated(event)
2020-05-22 23:40:51 +02:00
local profiler = game.create_profiler()
2019-02-19 02:43:01 +02:00
local entity = event.entity
2020-05-15 22:51:38 +02:00
if entity.valid and (entity.surface.name == natives.activeSurface) and (entity.name == "drain-trigger-rampant") then
2019-02-19 02:43:01 +02:00
local chunk = getChunkByPosition(map, entity.position)
2020-05-15 22:51:38 +02:00
if (chunk ~= -1) then
2019-02-19 02:43:01 +02:00
map.chunkToDrained[chunk] = event.tick + 60
end
entity.destroy()
end
2020-05-22 23:40:51 +02:00
game.print({"", "trigger", profiler, event.tick})
2019-02-19 02:43:01 +02:00
end
2016-09-13 00:33:00 +02:00
local function onInit()
2018-01-14 07:48:21 +02:00
global.map = {}
2016-09-13 00:33:00 +02:00
global.natives = {}
2020-04-28 05:41:18 +02:00
global.gameSurfaces = {}
2019-02-03 08:01:28 +02:00
2018-01-14 07:48:21 +02:00
map = global.map
2016-09-13 00:33:00 +02:00
natives = global.natives
2020-04-28 05:41:18 +02:00
gameSurfaces = global.gameSurfaces
2019-02-03 08:01:28 +02:00
2020-04-28 05:41:18 +02:00
prepWorld(false, "nauvis")
2017-07-05 00:52:20 +02:00
hookEvents()
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 profiler = game.create_profiler()
local entity = event.mine
local unitNumber = entity.unit_number
2020-05-15 22:51:38 +02:00
if natives.newEnemies and entity.valid then
local surface = entity.surface
2020-05-22 23:40:51 +02:00
if (surface.name == natives.activeSurface) and natives.buildingHiveTypeLookup[entity.name] then
local disPos = mathUtils.distortPosition(entity.position, 8)
local canPlaceQuery = map.canPlaceQuery
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
base = createBase(natives,
chunk,
event.tick)
end
2020-04-28 05:41:18 +02:00
2020-05-22 23:40:51 +02:00
entity = upgradeEntity(entity,
surface,
base.alignment,
natives,
disPos)
2019-12-18 03:09:08 +02:00
2020-05-22 23:40:51 +02:00
if entity and entity.valid then
event.entity = registerEnemyBaseStructure(map, entity, base, surface)
2019-11-04 08:19:22 +02:00
end
2019-11-30 02:49:22 +02:00
else
entity.destroy()
2019-11-04 08:19:22 +02:00
end
2020-05-22 23:40:51 +02:00
else
entity.destroy()
2019-11-30 02:49:22 +02:00
end
2019-10-20 22:45:43 +02:00
end
2020-05-22 23:40:51 +02:00
-- game.print({"", "spawned", profiler, event.tick, " ", unitNumber})
2019-10-20 22:45:43 +02:00
end
local function onUnitGroupCreated(event)
local profiler = game.create_profiler()
local group = event.group
2020-05-20 04:37:16 +02:00
local surface = group.surface
if (surface.name == natives.activeSurface) and (group.force.name == "enemy") then
2020-02-02 21:30:50 +02:00
if not group.is_script_driven then
2020-05-20 04:37:16 +02:00
if not natives.aiNocturnalMode then
2020-05-22 21:43:44 +02:00
local settler = mRandom() < 0.25 and
canMigrate(natives, group.surface) and
(natives.builderCount < AI_MAX_BUILDER_COUNT)
squad = createSquad(nil, nil, group, settler)
2020-05-20 04:37:16 +02:00
natives.groupNumberToSquad[group.group_number] = squad
2020-05-22 21:43:44 +02:00
if settler then
natives.builderCount = natives.builderCount + 1
2020-05-23 23:29:56 +02:00
else
natives.squadCount = natives.squadCount + 1
2020-05-22 21:43:44 +02:00
end
2020-05-20 04:37:16 +02:00
elseif not (surface.darkness > 0.65) then
group.destroy()
else
2020-05-22 21:43:44 +02:00
local settler = mRandom() < 0.25 and
canMigrate(natives, group.surface) and
(natives.builderCount < AI_MAX_BUILDER_COUNT)
squad = createSquad(nil, nil, group, settler)
2020-05-20 04:37:16 +02:00
natives.groupNumberToSquad[group.group_number] = squad
2020-05-22 21:43:44 +02:00
if settler then
natives.builderCount = natives.builderCount + 1
2020-05-23 23:29:56 +02:00
else
natives.squadCount = natives.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
2020-05-22 21:43:44 +02:00
-- game.print({"", "squadCreated", profiler, event.tick})
end
2020-05-17 07:06:55 +02:00
local function onCommandComplete(event)
2020-05-17 07:06:55 +02:00
local unitNumber = event.unit_number
local squad = natives.groupNumberToSquad[unitNumber]
if squad then
local profiler = game.create_profiler()
2020-05-17 07:06:55 +02:00
local group = squad.group
if group and group.valid and (group.surface.name == natives.activeSurface) then
2020-05-20 04:37:16 +02:00
if (event.result == DEFINES_BEHAVIOR_RESULT_FAIL) then
if (#group.members == 0) then
group.destroy()
else
squadDispatch(map, group.surface, squad, unitNumber)
end
else
squadDispatch(map, group.surface, squad, unitNumber)
end
2020-05-17 07:06:55 +02:00
end
2020-05-22 21:43:44 +02:00
-- game.print({"", "aiCommand", profiler, event.tick})
2020-05-17 07:06:55 +02:00
end
end
local function onGroupFinishedGathering(event)
local profiler = game.create_profiler()
2020-05-17 07:06:55 +02:00
local group = event.group
if group.valid then
local unitNumber = group.group_number
2020-05-23 23:29:56 +02:00
if (group.surface.name == natives.activeSurface) then
local squad = natives.groupNumberToSquad[unitNumber]
if squad.settler then
if (natives.builderCount < AI_MAX_BUILDER_COUNT) then
squadDispatch(map, group.surface, squad, unitNumber)
elseif not (group.command and group.command.type == DEFINES_COMMAND_WANDER) then
group.set_command(map.wonder3Command)
end
else
if (natives.squadCount < AI_MAX_SQUAD_COUNT) then
squadDispatch(map, group.surface, squad, unitNumber)
elseif not (group.command and group.command.type == DEFINES_COMMAND_WANDER) then
group.set_command(map.wonder3Command)
end
end
2020-05-17 07:06:55 +02:00
end
end
2020-05-22 21:43:44 +02:00
-- game.print({"", "finishedGather", profiler, event.tick})
end
2019-05-10 02:46:57 +02:00
local function onForceCreated(event)
map.activePlayerForces[#map.activePlayerForces+1] = event.force.name
end
local function onForceMerged(event)
for i=#map.activePlayerForces,1,-1 do
if (map.activePlayerForces[i] == event.source_name) then
tRemove(map.activePlayerForces, i)
break
end
2019-10-19 21:13:48 +02:00
end
2019-05-10 02:46:57 +02:00
end
2020-04-28 05:41:18 +02:00
local function onSurfaceRenamed(event)
if event.old_name == natives.activeSurface then
natives.activeSurface = event.new_name
end
if (gameSurfaces[event.old_name]) then
gameSurfaces[event.new_name] = gameSurfaces[event.old_name]
gameSurfaces[event.old_name] = nil
end
end
local function onSurfaceCleared(event)
local surface = game.get_surface(event.surface_index)
if surface and surface.valid and (surface.name == natives.activeSurface) then
prepWorld(true, natives.activeSurface)
end
end
local function onPlayerChangedSurface(event)
local player = game.players[event.player_index]
local surface
if player and player.valid and not settings.get_player_settings(player)["rampant-suppress-surface-change-warnings"].value
then
surface = player.surface
if (natives.activeSurface ~= surface.name) then
local playerName = player.name
local surfaceName = surface.name
local playerSurfaces = gameSurfaces[surfaceName]
if not playerSurfaces then
playerSurfaces = {}
gameSurfaces[surfaceName] = playerSurfaces
player.print({"description.rampant-change-surface", surfaceName, natives.activeSurface})
playerSurfaces[playerName] = true
elseif not playerSurfaces[playerName] then
player.print({"description.rampant-change-surface", surfaceName, natives.activeSurface})
playerSurfaces[playerName] = true
end
end
end
end
local function onSurfaceDeleted(event)
local surface = game.get_surface(event.surface_index)
if surface and surface.valid then
if (surface.name == natives.activeSurface) then
prepWorld(true, "nauvis")
end
if (gameSurfaces[surface.name]) then
gameSurfaces[surface.name] = nil
end
end
end
-- hooks
2020-05-21 03:03:32 +02:00
script.on_nth_tick(INTERVAL_PASS_SCAN,
function (event)
local profiler = game.create_profiler()
processScanChunks(map,
game.get_surface(natives.activeSurface))
2020-05-22 21:43:44 +02:00
-- game.print({"", "passscan", profiler, event.tick})
2020-05-21 03:03:32 +02:00
end)
2020-04-13 00:41:45 +02:00
script.on_nth_tick(INTERVAL_LOGIC,
function (event)
2020-05-20 04:37:16 +02:00
local profiler = game.create_profiler()
2020-04-13 00:41:45 +02:00
local tick = event.tick
planning(natives,
game.forces.enemy.evolution_factor,
tick)
2020-05-20 04:37:16 +02:00
cleanSquads(natives, map.squadIterator)
2020-04-13 00:41:45 +02:00
if natives.newEnemies then
recycleBases(natives, tick)
end
2020-05-22 21:43:44 +02:00
-- game.print({"", "logic", profiler, event.tick})
end)
script.on_nth_tick(INTERVAL_NEST,
function (event)
local profiler = game.create_profiler()
processNests(map,
game.get_surface(natives.activeSurface),
event.tick)
2020-05-22 21:43:44 +02:00
-- game.print({"", "nest", profiler, event.tick})
end)
script.on_nth_tick(INTERVAL_CLEANUP,
function (event)
local profiler = game.create_profiler()
cleanUpMapTables(map,
event.tick)
2020-05-22 21:43:44 +02:00
-- game.print({"", "cleanup", profiler, event.tick})
2020-05-20 04:37:16 +02:00
end)
script.on_nth_tick(INTERVAL_SPAWNER,
function (event)
local profiler = game.create_profiler()
processSpawners(map,
game.get_surface(natives.activeSurface),
event.tick)
2020-05-22 21:43:44 +02:00
-- game.print({"", "spawners", profiler, event.tick})
2020-05-20 04:37:16 +02:00
end)
script.on_nth_tick(INTERVAL_SQUAD,
function (event)
local profiler = game.create_profiler()
processVengence(map,
game.get_surface(natives.activeSurface),
event.tick)
2020-05-22 21:43:44 +02:00
-- game.print({"", "vengence", profiler, event.tick})
2020-04-13 00:41:45 +02:00
end)
script.on_nth_tick(INTERVAL_TEMPERAMENT,
function (event)
2020-05-20 04:37:16 +02:00
local profiler = game.create_profiler()
2020-04-13 00:41:45 +02:00
temperamentPlanner(natives)
2020-05-22 21:43:44 +02:00
-- game.print({"", "temperament", profiler, event.tick})
2020-04-13 00:41:45 +02:00
end)
2020-05-23 23:29:56 +02:00
-- script.on_nth_tick(INTERVAL_RESQUAD,
-- function (event)
-- local profiler = game.create_profiler()
-- regroupSquads(natives,
-- game.get_surface(natives.activeSurface),
-- map.regroupIterator)
-- -- game.print({"", "regroup", profiler, event.tick})
-- end)
2020-04-13 00:41:45 +02:00
2020-04-28 05:41:18 +02:00
script.on_event(defines.events.on_tick,
function (event)
-- if (event.tick > 36678750) then
-- local allGroups = {}
-- for _, unit in pairs(game.surfaces["nauvis"].find_entities_filtered{type = "unit", force = "enemy"}) do
-- if unit and unit.unit_group then
-- allGroups[unit.unit_group.group_number] = unit.unit_group
-- end
-- end
-- for group_number, group in pairs(allGroups) do
-- if group.position.x < -1000000 or group.position.x > 1000000 or group.position.y < -1000000 or group.position.y > 1000000 then
-- game.print(tostring(group_number..": "..serpent.dump(group.position)))
-- end
-- end
-- end
2020-05-22 23:40:51 +02:00
local gameRef = game
local profiler = gameRef.create_profiler()
local tick = event.tick
local pick = tick % 7
local surface = gameRef.get_surface(natives.activeSurface)
if (pick == 0) then
processPendingChunks(map,
surface,
event.tick)
elseif (pick == 1) then
processPlayers(gameRef.connected_players, map, surface, tick)
elseif (pick == 2) then
processMap(map, surface, tick)
elseif (pick == 3) then
processStaticMap(map, surface, tick)
elseif (pick == 4) then
scanResourceMap(map, surface, tick)
elseif (pick == 5) then
scanEnemyMap(map, surface, tick)
elseif (pick == 6) then
scanPlayerMap(map, surface, tick)
end
2020-05-22 23:40:51 +02:00
processActiveNests(map, surface, tick)
-- game.print({"", "tick", profiler, event.tick})
2020-04-28 05:41:18 +02:00
end)
script.on_event(defines.events.on_surface_cleared, onSurfaceCleared)
script.on_event(defines.events.on_surface_renamed, onSurfaceRenamed)
script.on_event(defines.events.on_player_changed_surface, onPlayerChangedSurface)
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,
2019-10-13 22:53:36 +02:00
defines.events.on_robot_built_tile}, onSurfaceTileChange)
script.on_event(defines.events.on_player_used_capsule, onUsedCapsule)
2019-02-19 02:43:01 +02:00
script.on_event(defines.events.on_trigger_created_entity, onTriggerEntityCreated)
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-17 07:06:55 +02:00
script.on_event(defines.events.on_ai_command_completed, onCommandComplete)
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)
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,
2019-02-21 08:31:47 +02:00
scanChunkPaths = tests.scanChunkPaths
2019-10-13 22:53:36 +02:00
}
2017-05-08 08:56:11 +02:00
)
2017-05-06 11:03:28 +02:00
2020-04-28 05:41:18 +02:00
interop.setActiveSurface = function (surfaceName)
prepWorld(true, surfaceName)
2019-11-04 08:19:22 +02:00
end
2020-04-28 05:41:18 +02:00
commands.add_command("GetRampantAISurface",
{"description.rampant-get-surface"},
function (event)
for _,player in pairs(game.connected_players) do
if (player.valid) then
player.print({"description.rampant-get-surface",
player.surface.name,
natives.activeSurface})
end
end
end)
commands.add_command("SetRampantAISurface",
{"description.rampant-set-surface"},
function (event)
local surfaceName = event.parameter
if surfaceName then
if (surfaceName ~= natives.activeSurface) then
if not game.get_surface(surfaceName) then
game.print({"description.rampant-invalid-set-surface", surfaceName})
else
local surface = game.get_surface(natives.activeSurface)
if surface and surface.valid then
for _,entity in pairs(natives.drainPylons) do
if entity and entity.valid then
entity.destroy()
end
end
end
prepWorld(true, surfaceName)
game.print({"description.rampant-set-surface", surfaceName})
end
else
game.print({"description.rampant-already-set-surface", surfaceName})
end
else
game.print({"description.rampant-error-set-surface"})
end
end)
2017-05-06 11:03:28 +02:00
remote.add_interface("rampant", interop)