2016-08-19 19:52:27 -07:00
|
|
|
-- imports
|
|
|
|
|
2016-08-26 23:44:17 -07:00
|
|
|
local entityUtils = require("libs/EntityUtils")
|
2017-01-19 21:58:36 -08:00
|
|
|
local mapUtils = require("libs/MapUtils")
|
2016-08-06 20:38:47 -07:00
|
|
|
local unitGroupUtils = require("libs/UnitGroupUtils")
|
2016-08-04 21:47:51 -07:00
|
|
|
local chunkProcessor = require("libs/ChunkProcessor")
|
2017-05-19 00:47:24 -07:00
|
|
|
local baseProcessor = require("libs/BaseProcessor")
|
2016-08-04 21:47:51 -07:00
|
|
|
local mapProcessor = require("libs/MapProcessor")
|
2016-08-03 15:22:20 -07:00
|
|
|
local constants = require("libs/Constants")
|
2016-08-04 21:47:51 -07:00
|
|
|
local pheromoneUtils = require("libs/PheromoneUtils")
|
2016-08-17 22:55:08 -07:00
|
|
|
local aiDefense = require("libs/AIDefense")
|
|
|
|
local aiAttack = require("libs/AIAttack")
|
2016-08-18 19:02:13 -07:00
|
|
|
local aiBuilding = require("libs/AIBuilding")
|
2016-10-14 17:00:18 -07:00
|
|
|
local aiPlanning = require("libs/AIPlanning")
|
2017-05-06 02:03:28 -07:00
|
|
|
local interop = require("libs/Interop")
|
2016-08-21 15:59:17 -07:00
|
|
|
local tests = require("tests")
|
2017-05-19 00:47:24 -07:00
|
|
|
local upgrade = require("Upgrade")
|
2017-05-27 21:50:37 -07:00
|
|
|
local baseUtils = require("libs/BaseUtils")
|
2016-08-03 15:22:20 -07:00
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
-- constants
|
|
|
|
|
|
|
|
local INTERVAL_LOGIC = constants.INTERVAL_LOGIC
|
|
|
|
local INTERVAL_PROCESS = constants.INTERVAL_PROCESS
|
|
|
|
|
2017-01-19 21:58:36 -08:00
|
|
|
local MOVEMENT_PHEROMONE = constants.MOVEMENT_PHEROMONE
|
|
|
|
|
|
|
|
local BASE_RALLY_CHANCE = constants.BASE_RALLY_CHANCE
|
|
|
|
local BONUS_RALLY_CHANCE = constants.BONUS_RALLY_CHANCE
|
|
|
|
|
|
|
|
local RETREAT_MOVEMENT_PHEROMONE_LEVEL = constants.RETREAT_MOVEMENT_PHEROMONE_LEVEL
|
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
-- imported functions
|
|
|
|
|
2017-01-19 21:58:36 -08:00
|
|
|
local getChunkByPosition = mapUtils.getChunkByPosition
|
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
local processPendingChunks = chunkProcessor.processPendingChunks
|
|
|
|
|
|
|
|
local processMap = mapProcessor.processMap
|
2016-10-14 17:00:18 -07:00
|
|
|
local processPlayers = mapProcessor.processPlayers
|
2016-08-28 17:05:28 -07:00
|
|
|
local scanMap = mapProcessor.scanMap
|
2016-08-19 19:52:27 -07:00
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
local planning = aiPlanning.planning
|
2016-08-19 19:52:27 -07:00
|
|
|
|
2017-01-19 21:58:36 -08:00
|
|
|
local rallyUnits = aiBuilding.rallyUnits
|
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
local deathScent = pheromoneUtils.deathScent
|
2017-04-21 16:14:04 -07:00
|
|
|
local victoryScent = pheromoneUtils.victoryScent
|
2016-08-19 19:52:27 -07:00
|
|
|
|
2017-05-23 23:46:23 -07:00
|
|
|
local cleanSquads = unitGroupUtils.cleanSquads
|
2016-08-19 19:52:27 -07:00
|
|
|
local regroupSquads = unitGroupUtils.regroupSquads
|
|
|
|
local convertUnitGroupToSquad = unitGroupUtils.convertUnitGroupToSquad
|
|
|
|
|
2016-08-28 13:48:19 -07:00
|
|
|
local squadAttack = aiAttack.squadAttack
|
2016-08-19 19:52:27 -07:00
|
|
|
local squadBeginAttack = aiAttack.squadBeginAttack
|
|
|
|
|
|
|
|
local retreatUnits = aiDefense.retreatUnits
|
|
|
|
|
2017-05-19 00:47:24 -07:00
|
|
|
local addRemovePlayerEntity = entityUtils.addRemovePlayerEntity
|
2017-05-27 21:50:37 -07:00
|
|
|
local unregisterEnemyBaseStructure = baseUtils.unregisterEnemyBaseStructure
|
|
|
|
local makeImmortalEntity = entityUtils.makeImmortalEntity
|
2016-08-19 19:52:27 -07:00
|
|
|
|
2017-05-19 00:47:24 -07:00
|
|
|
local processBases = baseProcessor.processBases
|
2016-10-14 17:00:18 -07:00
|
|
|
|
2016-08-24 17:05:20 -07:00
|
|
|
-- local references to global
|
|
|
|
|
|
|
|
local regionMap
|
|
|
|
local natives
|
|
|
|
local pendingChunks
|
|
|
|
|
2016-07-29 15:44:31 -07:00
|
|
|
-- hook functions
|
|
|
|
|
2016-09-12 15:33:00 -07:00
|
|
|
local function onLoad()
|
2016-08-24 17:05:20 -07:00
|
|
|
regionMap = global.regionMap
|
|
|
|
natives = global.natives
|
|
|
|
pendingChunks = global.pendingChunks
|
2016-07-29 15:44:31 -07:00
|
|
|
end
|
|
|
|
|
2016-09-12 15:33:00 -07: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.
|
|
|
|
if (event.surface.index == 1) then
|
|
|
|
pendingChunks[#pendingChunks+1] = event
|
|
|
|
end
|
2016-08-24 17:05:20 -07:00
|
|
|
end
|
|
|
|
|
2017-05-27 21:50:37 -07:00
|
|
|
local function reprocessChunks()
|
|
|
|
game.surfaces[1].print("Rampant - Reindexing chunks, please wait")
|
|
|
|
-- clear old regionMap processing Queue
|
|
|
|
-- prevents queue adding duplicate chunks
|
|
|
|
-- chunks are by key, so should overwrite old
|
|
|
|
regionMap.processQueue = {}
|
|
|
|
regionMap.processPointer = 1
|
|
|
|
regionMap.scanPointer = 1
|
|
|
|
-- clear pending chunks, will be added when loop runs below
|
|
|
|
pendingChunks = {}
|
|
|
|
|
|
|
|
-- queue all current chunks that wont be generated during play
|
|
|
|
local surface = game.surfaces[1]
|
|
|
|
for chunk in surface.get_chunks() do
|
|
|
|
onChunkGenerated({ tick = game.tick,
|
|
|
|
surface = surface,
|
|
|
|
area = { left_top = { x = chunk.x * 32,
|
|
|
|
y = chunk.y * 32 }}})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-06 02:03:28 -07:00
|
|
|
local function onModSettingsChange(event)
|
2017-05-26 17:58:33 -07:00
|
|
|
|
2017-05-06 02:03:28 -07:00
|
|
|
if event and (string.sub(event.setting, 1, 7) ~= "rampant") then
|
2017-05-27 21:50:37 -07:00
|
|
|
return false
|
2017-05-06 02:03:28 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
natives.safeBuildings = settings.global["rampant-safeBuildings"].value
|
|
|
|
|
|
|
|
natives.safeEntities["curved-rail"] = settings.global["rampant-safeBuildings-curvedRail"].value
|
|
|
|
natives.safeEntities["straight-rail"] = settings.global["rampant-safeBuildings-straightRail"].value
|
|
|
|
natives.safeEntities["rail-signal"] = settings.global["rampant-safeBuildings-railSignals"].value
|
|
|
|
natives.safeEntities["rail-chain-signal"] = settings.global["rampant-safeBuildings-railChainSignals"].value
|
|
|
|
natives.safeEntities["train-stop"] = settings.global["rampant-safeBuildings-trainStops"].value
|
2017-05-16 23:18:25 -07:00
|
|
|
|
|
|
|
local poles = settings.global["rampant-safeBuildings-bigElectricPole"].value
|
|
|
|
natives.safeEntityName["big-electric-pole"] = poles
|
|
|
|
natives.safeEntityName["big-electric-pole-2"] = poles
|
|
|
|
natives.safeEntityName["big-electric-pole-3"] = poles
|
|
|
|
natives.safeEntityName["big-electric-pole-4"] = poles
|
2017-05-23 23:46:23 -07:00
|
|
|
|
2017-05-06 02:03:28 -07:00
|
|
|
natives.attackUsePlayer = settings.global["rampant-attackWaveGenerationUsePlayerProximity"].value
|
|
|
|
natives.attackUsePollution = settings.global["rampant-attackWaveGenerationUsePollution"].value
|
|
|
|
|
|
|
|
natives.attackThresholdMin = settings.global["rampant-attackWaveGenerationThresholdMin"].value
|
|
|
|
natives.attackThresholdMax = settings.global["rampant-attackWaveGenerationThresholdMax"].value
|
|
|
|
natives.attackThresholdRange = natives.attackThresholdMax - natives.attackThresholdMin
|
|
|
|
natives.attackWaveMaxSize = settings.global["rampant-attackWaveMaxSize"].value
|
|
|
|
natives.attackPlayerThreshold = settings.global["rampant-attackPlayerThreshold"].value
|
2017-05-13 15:32:16 -07:00
|
|
|
natives.aiNocturnalMode = settings.global["rampant-permanentNocturnal"].value
|
2017-05-13 20:23:25 -07:00
|
|
|
natives.aiPointsScaler = settings.global["rampant-aiPointsScaler"].value
|
2017-05-06 02:03:28 -07:00
|
|
|
|
2017-05-27 21:50:37 -07:00
|
|
|
local useCustomAI = natives.useCustomAI
|
|
|
|
natives.useCustomAI = settings.startup["rampant-useCustomAI"].value
|
|
|
|
if not useCustomAI and natives.useCustomAI then
|
|
|
|
reprocessChunks()
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2017-05-26 17:58:33 -07:00
|
|
|
|
2017-05-14 17:09:43 -07:00
|
|
|
|
2017-04-15 23:04:22 -07:00
|
|
|
|
2017-05-27 21:50:37 -07:00
|
|
|
local function onConfigChanged()
|
|
|
|
if upgrade.attempt(natives, regionMap) and onModSettingsChange(nil) then
|
|
|
|
reprocessChunks()
|
2017-05-26 17:58:33 -07:00
|
|
|
end
|
2016-07-29 15:44:31 -07:00
|
|
|
end
|
|
|
|
|
2016-11-03 16:51:35 -07:00
|
|
|
local function onTick(event)
|
|
|
|
local tick = event.tick
|
2017-01-19 21:58:36 -08:00
|
|
|
if (tick == regionMap.processTick) then
|
|
|
|
regionMap.processTick = regionMap.processTick + INTERVAL_PROCESS
|
2016-09-12 15:33:00 -07:00
|
|
|
local surface = game.surfaces[1]
|
2017-04-24 21:08:41 -07:00
|
|
|
local evolutionFactor = game.forces.enemy.evolution_factor
|
2016-10-14 17:00:18 -07:00
|
|
|
local players = game.players
|
2017-05-26 17:58:33 -07:00
|
|
|
|
2017-05-26 22:56:45 -07:00
|
|
|
processPendingChunks(natives, regionMap, surface, pendingChunks, tick)
|
2017-04-21 16:33:17 -07:00
|
|
|
scanMap(regionMap, surface, natives, evolutionFactor)
|
2016-10-14 17:00:18 -07:00
|
|
|
|
2017-01-19 21:58:36 -08:00
|
|
|
if (tick == regionMap.logicTick) then
|
|
|
|
regionMap.logicTick = regionMap.logicTick + INTERVAL_LOGIC
|
|
|
|
|
2017-04-21 16:33:17 -07:00
|
|
|
planning(natives, evolutionFactor, tick, surface)
|
2017-05-26 17:58:33 -07:00
|
|
|
|
2017-05-23 23:46:23 -07:00
|
|
|
cleanSquads(natives, evolutionFactor)
|
2017-05-26 17:58:33 -07:00
|
|
|
regroupSquads(natives, evolutionFactor)
|
2016-10-14 17:00:18 -07:00
|
|
|
|
|
|
|
processPlayers(players, regionMap, surface, natives, evolutionFactor, tick)
|
2017-05-19 00:47:24 -07:00
|
|
|
|
2017-05-27 21:50:37 -07:00
|
|
|
if (natives.useCustomAI) then
|
|
|
|
processBases(regionMap, surface, natives, tick)
|
|
|
|
end
|
2016-10-14 17:00:18 -07:00
|
|
|
|
|
|
|
squadBeginAttack(natives, players, evolutionFactor)
|
|
|
|
squadAttack(regionMap, surface, natives)
|
|
|
|
end
|
2016-09-12 15:33:00 -07:00
|
|
|
|
2017-05-26 17:58:33 -07:00
|
|
|
processMap(regionMap, surface, natives, evolutionFactor)
|
2016-08-20 11:24:22 -07:00
|
|
|
end
|
2016-08-04 21:47:51 -07:00
|
|
|
end
|
|
|
|
|
2016-09-12 15:33:00 -07:00
|
|
|
local function onBuild(event)
|
2017-05-23 23:46:23 -07:00
|
|
|
local entity = event.created_entity
|
2017-05-27 21:50:37 -07:00
|
|
|
addRemovePlayerEntity(regionMap, entity, natives, true, false)
|
2017-05-23 23:46:23 -07:00
|
|
|
if natives.safeBuildings then
|
|
|
|
if natives.safeEntities[entity.type] or natives.safeEntityName[entity.name] then
|
|
|
|
entity.destructible = false
|
|
|
|
end
|
|
|
|
end
|
2016-08-17 22:55:08 -07:00
|
|
|
end
|
|
|
|
|
2016-09-12 15:33:00 -07:00
|
|
|
local function onPickUp(event)
|
2017-05-19 00:47:24 -07:00
|
|
|
addRemovePlayerEntity(regionMap, event.entity, natives, false, false)
|
2016-08-17 22:55:08 -07:00
|
|
|
end
|
|
|
|
|
2016-09-12 15:33:00 -07:00
|
|
|
local function onDeath(event)
|
2016-08-04 21:47:51 -07:00
|
|
|
local entity = event.entity
|
2016-08-26 23:44:17 -07:00
|
|
|
local surface = entity.surface
|
|
|
|
if (surface.index == 1) then
|
2016-08-24 16:30:45 -07:00
|
|
|
if (entity.force.name == "enemy") then
|
|
|
|
if (entity.type == "unit") then
|
2017-05-19 00:47:24 -07:00
|
|
|
local entityPosition = entity.position
|
2017-01-19 21:58:36 -08:00
|
|
|
local deathChunk = getChunkByPosition(regionMap, entityPosition.x, entityPosition.y)
|
2017-05-23 23:46:23 -07:00
|
|
|
|
2017-05-26 17:58:33 -07:00
|
|
|
if deathChunk then
|
2017-01-19 21:58:36 -08:00
|
|
|
-- drop death pheromone where unit died
|
|
|
|
deathScent(deathChunk)
|
|
|
|
|
2017-05-27 21:50:37 -07:00
|
|
|
if event.force and (event.force.name == "player") then
|
2017-05-26 17:58:33 -07:00
|
|
|
local evolutionFactor = entity.force.evolution_factor
|
2017-05-23 23:46:23 -07:00
|
|
|
local tick = event.tick
|
2017-01-19 21:58:36 -08:00
|
|
|
|
|
|
|
if (deathChunk[MOVEMENT_PHEROMONE] < -(evolutionFactor * RETREAT_MOVEMENT_PHEROMONE_LEVEL)) then
|
2017-04-21 16:14:04 -07:00
|
|
|
retreatUnits(deathChunk,
|
|
|
|
convertUnitGroupToSquad(natives,
|
|
|
|
entity.unit_group),
|
|
|
|
regionMap,
|
|
|
|
surface,
|
|
|
|
natives,
|
2017-05-23 23:46:23 -07:00
|
|
|
tick)
|
2017-01-19 21:58:36 -08:00
|
|
|
local rallyThreshold = BASE_RALLY_CHANCE + (evolutionFactor * BONUS_RALLY_CHANCE)
|
2017-05-23 23:46:23 -07:00
|
|
|
if (math.random() < rallyThreshold) then
|
2017-01-19 21:58:36 -08:00
|
|
|
rallyUnits(deathChunk,
|
|
|
|
regionMap,
|
|
|
|
surface,
|
|
|
|
natives,
|
2017-05-23 23:46:23 -07:00
|
|
|
evolutionFactor,
|
|
|
|
tick)
|
2017-01-19 21:58:36 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-24 16:30:45 -07:00
|
|
|
end
|
|
|
|
|
2017-04-15 23:04:22 -07:00
|
|
|
elseif (entity.type == "unit-spawner") or (entity.type == "turret") then
|
2017-05-27 21:50:37 -07:00
|
|
|
unregisterEnemyBaseStructure(regionMap, entity)
|
2016-08-17 22:55:08 -07:00
|
|
|
end
|
2016-08-24 16:30:45 -07:00
|
|
|
elseif (entity.force.name == "player") then
|
2016-11-03 16:51:35 -07:00
|
|
|
local creditNatives = false
|
2017-04-30 15:51:07 -07:00
|
|
|
local entityPosition = entity.position
|
2016-11-03 16:51:35 -07:00
|
|
|
if (event.force ~= nil) and (event.force.name == "enemy") then
|
|
|
|
creditNatives = true
|
2017-04-21 16:14:04 -07:00
|
|
|
local victoryChunk = getChunkByPosition(regionMap, entityPosition.x, entityPosition.y)
|
2017-05-26 17:58:33 -07:00
|
|
|
if victoryChunk then
|
|
|
|
victoryScent(victoryChunk, entity.type)
|
|
|
|
end
|
2016-11-03 16:51:35 -07:00
|
|
|
end
|
2017-05-06 02:03:28 -07:00
|
|
|
if creditNatives and natives.safeBuildings and (natives.safeEntities[entity.type] or natives.safeEntityName[entity.name]) then
|
2017-05-23 23:46:23 -07:00
|
|
|
makeImmortalEntity(surface, entity)
|
2017-04-16 19:46:36 -07:00
|
|
|
else
|
2017-05-19 00:47:24 -07:00
|
|
|
addRemovePlayerEntity(regionMap, entity, natives, false, creditNatives)
|
2017-04-16 19:46:36 -07:00
|
|
|
end
|
2016-08-08 19:54:41 -07:00
|
|
|
end
|
2016-08-03 08:54:21 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-12 16:23:26 -07:00
|
|
|
local function onSurfaceTileChange(event)
|
2017-05-07 23:56:11 -07:00
|
|
|
local player = game.players[event.player_index]
|
2017-05-27 21:50:37 -07:00
|
|
|
if (player.surface.index == 1) then
|
2017-05-07 23:56:11 -07:00
|
|
|
aiBuilding.fillTunnel(regionMap, player.surface, natives, event.positions)
|
|
|
|
end
|
2016-08-20 17:28:35 -07:00
|
|
|
end
|
|
|
|
|
2016-09-12 15:33:00 -07:00
|
|
|
local function onInit()
|
|
|
|
global.regionMap = {}
|
|
|
|
global.pendingChunks = {}
|
|
|
|
global.natives = {}
|
|
|
|
|
|
|
|
regionMap = global.regionMap
|
|
|
|
natives = global.natives
|
|
|
|
pendingChunks = global.pendingChunks
|
|
|
|
|
|
|
|
onConfigChanged()
|
|
|
|
end
|
|
|
|
|
2016-08-03 17:22:27 -07:00
|
|
|
-- hooks
|
|
|
|
|
2016-07-29 15:44:31 -07:00
|
|
|
script.on_init(onInit)
|
|
|
|
script.on_load(onLoad)
|
2017-05-06 02:03:28 -07: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)
|
2016-07-29 15:44:31 -07:00
|
|
|
|
2016-09-12 16:23:26 -07:00
|
|
|
script.on_event(defines.events.on_player_built_tile, onSurfaceTileChange)
|
2016-08-20 17:28:35 -07:00
|
|
|
|
2016-08-17 22:55:08 -07:00
|
|
|
script.on_event({defines.events.on_preplayer_mined_item,
|
|
|
|
defines.events.on_robot_pre_mined},
|
2016-11-04 00:26:19 -07:00
|
|
|
onPickUp)
|
2016-08-17 22:55:08 -07:00
|
|
|
script.on_event({defines.events.on_built_entity,
|
|
|
|
defines.events.on_robot_built_entity},
|
2016-11-04 00:26:19 -07:00
|
|
|
onBuild)
|
2016-09-09 03:02:50 -07:00
|
|
|
|
|
|
|
script.on_event(defines.events.on_entity_died, onDeath)
|
2016-08-19 19:52:27 -07:00
|
|
|
script.on_event(defines.events.on_tick, onTick)
|
2016-07-29 15:44:31 -07:00
|
|
|
script.on_event(defines.events.on_chunk_generated, onChunkGenerated)
|
|
|
|
|
2017-05-07 23:56:11 -07:00
|
|
|
remote.add_interface("rampantTests",
|
|
|
|
{
|
|
|
|
pheromoneLevels = tests.pheromoneLevels,
|
|
|
|
activeSquads = tests.activeSquads,
|
|
|
|
entitiesOnPlayerChunk = tests.entitiesOnPlayerChunk,
|
|
|
|
findNearestPlayerEnemy = tests.findNearestPlayerEnemy,
|
|
|
|
aiStats = tests.aiStats,
|
|
|
|
fillableDirtTest = tests.fillableDirtTest,
|
|
|
|
tunnelTest = tests.tunnelTest,
|
|
|
|
createEnemy = tests.createEnemy,
|
|
|
|
attackOrigin = tests.attackOrigin,
|
|
|
|
cheatMode = tests.cheatMode,
|
|
|
|
gaussianRandomTest = tests.gaussianRandomTest,
|
|
|
|
reveal = tests.reveal,
|
|
|
|
showMovementGrid = tests.showMovementGrid,
|
|
|
|
baseStats = tests.baseStats,
|
|
|
|
baseTiles = tests.baseTiles,
|
|
|
|
mergeBases = tests.mergeBases,
|
2017-05-27 21:50:37 -07:00
|
|
|
clearBases = tests.clearBases,
|
|
|
|
getOffsetChunk = tests.getOffsetChunk,
|
|
|
|
registeredNest = tests.registeredNest
|
2017-05-07 23:56:11 -07:00
|
|
|
}
|
|
|
|
)
|
2017-05-06 02:03:28 -07:00
|
|
|
|
|
|
|
remote.add_interface("rampant", interop)
|