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

404 lines
14 KiB
Lua
Raw Normal View History

-- imports
local entityUtils = require("libs/EntityUtils")
2017-01-20 07:58:36 +02:00
local mapUtils = require("libs/MapUtils")
2016-08-07 05:38:47 +02:00
local unitGroupUtils = require("libs/UnitGroupUtils")
local chunkProcessor = require("libs/ChunkProcessor")
2017-05-19 09:47:24 +02:00
local baseProcessor = require("libs/BaseProcessor")
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-05-19 09:47:24 +02:00
local upgrade = require("Upgrade")
2017-06-08 02:57:24 +02:00
local baseRegisterUtils = require("libs/BaseRegisterUtils")
2017-06-01 03:46:53 +02:00
local mathUtils = require("libs/MathUtils")
2017-06-01 09:03:07 +02:00
local config = require("config")
2016-10-15 02:00:18 +02:00
-- constants
local INTERVAL_LOGIC = constants.INTERVAL_LOGIC
local INTERVAL_PROCESS = constants.INTERVAL_PROCESS
2017-01-20 07:58:36 +02:00
local MOVEMENT_PHEROMONE = constants.MOVEMENT_PHEROMONE
2017-06-01 09:03:07 +02:00
local AI_MAX_OVERFLOW_POINTS = constants.AI_MAX_OVERFLOW_POINTS
2017-06-01 03:46:53 +02:00
-- imported functions
2017-01-20 07:58:36 +02:00
2017-06-01 03:46:53 +02:00
local roundToNearest = mathUtils.roundToNearest
2017-01-20 07:58:36 +02:00
-- imported functions
2017-01-20 07:58:36 +02:00
local getChunkByPosition = mapUtils.getChunkByPosition
local processPendingChunks = chunkProcessor.processPendingChunks
local processMap = mapProcessor.processMap
2016-10-15 02:00:18 +02:00
local processPlayers = mapProcessor.processPlayers
2016-08-29 02:05:28 +02:00
local scanMap = mapProcessor.scanMap
2016-10-15 02:00:18 +02:00
local planning = aiPlanning.planning
local rallyUnits = aiAttackWave.rallyUnits
2017-01-20 07:58:36 +02:00
local deathScent = pheromoneUtils.deathScent
2017-04-22 01:14:04 +02:00
local victoryScent = pheromoneUtils.victoryScent
local cleanSquads = unitGroupUtils.cleanSquads
local regroupSquads = unitGroupUtils.regroupSquads
local convertUnitGroupToSquad = unitGroupUtils.convertUnitGroupToSquad
local squadsAttack = squadAttack.squadsAttack
local squadsBeginAttack = squadAttack.squadsBeginAttack
local retreatUnits = squadDefense.retreatUnits
2017-05-19 09:47:24 +02:00
local addRemovePlayerEntity = entityUtils.addRemovePlayerEntity
2017-06-08 02:57:24 +02:00
local unregisterEnemyBaseStructure = baseRegisterUtils.unregisterEnemyBaseStructure
local registerEnemyBaseStructure = baseRegisterUtils.registerEnemyBaseStructure
2017-05-28 06:50:37 +02:00
local makeImmortalEntity = entityUtils.makeImmortalEntity
2017-05-19 09:47:24 +02:00
local processBases = baseProcessor.processBases
2016-10-15 02:00:18 +02:00
local mRandom = math.random
2016-08-25 02:05:20 +02:00
-- local references to global
local regionMap
local natives
local pendingChunks
-- hook functions
2017-07-05 00:52:20 +02:00
local function onIonCannonFired(event)
--[[
event.force, event.surface, event.player_index, event.position, event.radius
--]]
local surface = event.surface
if (surface.index == 1) then
natives.points = natives.points + 3000
if (natives.points > AI_MAX_OVERFLOW_POINTS) then
natives.points = AI_MAX_OVERFLOW_POINTS
end
local chunk = getChunkByPosition(regionMap, event.position.x, event.position.y)
if chunk then
rallyUnits(chunk,
regionMap,
surface,
natives,
event.tick)
end
end
end
local function hookEvents()
if config.ionCannonPresent then
script.on_event(remote.call("orbital_ion_cannon", "on_ion_cannon_fired"),
onIonCannonFired)
-- script.on_event(remote.call("orbital_ion_cannon", "on_ion_cannon_targeted"),
-- onIonCannonTargeted)
end
end
2016-09-13 00:33:00 +02:00
local function onLoad()
2016-08-25 02:05:20 +02:00
regionMap = global.regionMap
natives = global.natives
pendingChunks = global.pendingChunks
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.
if (event.surface.index == 1) then
pendingChunks[#pendingChunks+1] = event
end
2016-08-25 02:05:20 +02:00
end
2017-06-01 03:46:53 +02:00
local function rebuildRegionMap()
game.surfaces[1].print("Rampant - Reindexing chunks, please wait.")
2017-05-28 06:50:37 +02:00
-- clear old regionMap processing Queue
-- prevents queue adding duplicate chunks
-- chunks are by key, so should overwrite old
2017-06-01 03:46:53 +02:00
global.regionMap = {}
regionMap = global.regionMap
2017-05-28 06:50:37 +02:00
regionMap.processQueue = {}
regionMap.processPointer = 1
regionMap.scanPointer = 1
-- preallocating memory to be used in code, making it fast by reducing garbage generated.
2017-06-10 10:38:20 +02:00
regionMap.neighbors = { nil, nil, nil, nil, nil, nil, nil, nil }
regionMap.cardinalNeighbors = { nil, nil, nil, nil }
2017-06-11 02:59:06 +02:00
regionMap.chunkTiles = {}
regionMap.position = {x=0,
y=0}
2017-06-11 02:59:06 +02:00
for i=1,1024 do
regionMap.chunkTiles[i] = nil
end
2017-06-01 03:46:53 +02:00
-- switched over to tick event
regionMap.logicTick = roundToNearest(game.tick + INTERVAL_LOGIC, INTERVAL_LOGIC)
regionMap.processTick = roundToNearest(game.tick + INTERVAL_PROCESS, INTERVAL_PROCESS)
2017-05-28 06:50:37 +02:00
-- clear pending chunks, will be added when loop runs below
2017-06-11 02:59:06 +02:00
global.pendingChunks = {}
pendingChunks = global.pendingChunks
2017-05-28 06:50:37 +02:00
-- queue all current chunks that wont be generated during play
local surface = game.surfaces[1]
2017-06-01 03:46:53 +02:00
local tick = game.tick
2017-05-28 06:50:37 +02:00
for chunk in surface.get_chunks() do
2017-06-01 03:46:53 +02:00
onChunkGenerated({ tick = tick,
2017-05-28 06:50:37 +02:00
surface = surface,
area = { left_top = { x = chunk.x * 32,
y = chunk.y * 32 }}})
2017-06-01 03:46:53 +02:00
end
2017-06-24 20:41:57 +02:00
processPendingChunks(natives, regionMap, surface, pendingChunks, tick)
2017-05-28 06:50:37 +02:00
end
2017-05-06 11:03:28 +02:00
local function onModSettingsChange(event)
2017-05-27 02:58:33 +02:00
2017-05-06 11:03:28 +02:00
if event and (string.sub(event.setting, 1, 7) ~= "rampant") then
2017-05-28 06:50:37 +02:00
return false
2017-05-06 11:03:28 +02:00
end
2017-06-01 03:46:53 +02:00
upgrade.compareTable(natives, "safeBuildings", settings.global["rampant-safeBuildings"].value)
2017-05-06 11:03:28 +02:00
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
local changed, newValue = upgrade.compareTable(natives.safeEntityName,
"big-electric-pole",
settings.global["rampant-safeBuildings-bigElectricPole"].value)
if changed then
natives.safeEntityName["big-electric-pole"] = newValue
natives.safeEntityName["big-electric-pole-2"] = newValue
natives.safeEntityName["big-electric-pole-3"] = newValue
natives.safeEntityName["big-electric-pole-4"] = newValue
end
2017-06-01 03:46:53 +02:00
upgrade.compareTable(natives, "attackUsePlayer", settings.global["rampant-attackWaveGenerationUsePlayerProximity"].value)
upgrade.compareTable(natives, "attackUsePollution", settings.global["rampant-attackWaveGenerationUsePollution"].value)
2017-05-06 11:03:28 +02:00
2017-06-01 03:46:53 +02:00
upgrade.compareTable(natives, "attackThresholdMin", settings.global["rampant-attackWaveGenerationThresholdMin"].value)
upgrade.compareTable(natives, "attackThresholdMax", settings.global["rampant-attackWaveGenerationThresholdMax"].value)
upgrade.compareTable(natives, "attackThresholdRange", natives.attackThresholdMax - natives.attackThresholdMin)
upgrade.compareTable(natives, "attackWaveMaxSize", settings.global["rampant-attackWaveMaxSize"].value)
upgrade.compareTable(natives, "attackPlayerThreshold", settings.global["rampant-attackPlayerThreshold"].value)
upgrade.compareTable(natives, "aiNocturnalMode", settings.global["rampant-permanentNocturnal"].value)
upgrade.compareTable(natives, "aiPointsScaler", settings.global["rampant-aiPointsScaler"].value)
2017-06-11 02:59:06 +02:00
-- RE-ENABLE WHEN COMPLETE
natives.useCustomAI = constants.DEV_CUSTOM_AI
-- changed, newValue = upgrade.compareTable(natives, "useCustomAI", settings.startup["rampant-useCustomAI"].value)
-- if natives.useCustomAI then
-- game.forces.enemy.ai_controllable = false
-- else
-- game.forces.enemy.ai_controllable = true
-- end
-- if changed and newValue then
-- rebuildRegionMap()
-- return false
-- end
2017-05-28 06:50:37 +02:00
return true
end
2017-05-27 02:58:33 +02:00
2017-05-28 06:50:37 +02:00
local function onConfigChanged()
if upgrade.attempt(natives, regionMap) and onModSettingsChange(nil) then
2017-06-01 03:46:53 +02:00
rebuildRegionMap()
2017-05-27 02:58:33 +02:00
end
end
2016-11-04 01:51:35 +02:00
local function onTick(event)
local tick = event.tick
2017-01-20 07:58:36 +02:00
if (tick == regionMap.processTick) then
regionMap.processTick = regionMap.processTick + INTERVAL_PROCESS
2016-09-13 00:33:00 +02:00
local surface = game.surfaces[1]
2017-05-27 02:58:33 +02:00
2017-05-27 07:56:45 +02:00
processPendingChunks(natives, regionMap, surface, pendingChunks, tick)
2017-06-01 03:46:53 +02:00
scanMap(regionMap, surface, natives)
2016-10-15 02:00:18 +02:00
2017-01-20 07:58:36 +02:00
if (tick == regionMap.logicTick) then
regionMap.logicTick = regionMap.logicTick + INTERVAL_LOGIC
2017-06-01 03:46:53 +02:00
local players = game.players
planning(natives,
2017-06-08 02:57:24 +02:00
game.forces.enemy.evolution_factor,
tick,
surface)
2017-05-27 02:58:33 +02:00
2017-06-01 03:46:53 +02:00
cleanSquads(natives)
2017-06-01 04:48:59 +02:00
regroupSquads(natives)
2016-10-15 02:00:18 +02:00
2017-06-01 03:46:53 +02:00
processPlayers(players, regionMap, surface, natives, tick)
2017-05-19 09:47:24 +02:00
2017-06-01 09:03:07 +02:00
if natives.useCustomAI then
processBases(regionMap, surface, natives, tick)
end
2016-10-15 02:00:18 +02:00
squadsBeginAttack(natives, players)
squadsAttack(regionMap, surface, natives)
2016-10-15 02:00:18 +02:00
end
2016-09-13 00:33:00 +02:00
2017-06-01 03:46:53 +02:00
processMap(regionMap, surface, natives, tick)
2016-08-20 20:24:22 +02:00
end
end
2016-09-13 00:33:00 +02:00
local function onBuild(event)
local entity = event.created_entity
2017-05-28 06:50:37 +02:00
addRemovePlayerEntity(regionMap, entity, natives, true, false)
if natives.safeBuildings then
if natives.safeEntities[entity.type] or natives.safeEntityName[entity.name] then
entity.destructible = false
end
end
end
2016-09-13 00:33:00 +02:00
local function onPickUp(event)
2017-05-19 09:47:24 +02:00
addRemovePlayerEntity(regionMap, event.entity, natives, false, false)
end
2016-09-13 00:33:00 +02:00
local function onDeath(event)
local entity = event.entity
local surface = entity.surface
if (surface.index == 1) then
if (entity.force.name == "enemy") then
if (entity.type == "unit") then
2017-05-19 09:47:24 +02:00
local entityPosition = entity.position
2017-01-20 07:58:36 +02:00
local deathChunk = getChunkByPosition(regionMap, entityPosition.x, entityPosition.y)
2017-05-27 02:58:33 +02:00
if deathChunk then
2017-01-20 07:58:36 +02:00
-- drop death pheromone where unit died
deathScent(deathChunk)
2017-06-01 03:46:53 +02:00
if event.force and (event.force.name == "player") and (deathChunk[MOVEMENT_PHEROMONE] < natives.retreatThreshold) then
local tick = event.tick
2017-06-01 03:46:53 +02:00
2017-06-09 07:18:59 +02:00
retreatUnits(deathChunk,
entityPosition,
2017-06-01 03:46:53 +02:00
convertUnitGroupToSquad(natives,
entity.unit_group),
regionMap,
surface,
natives,
tick)
if (mRandom() < natives.rallyThreshold) and not surface.peaceful_mode then
2017-06-01 03:46:53 +02:00
rallyUnits(deathChunk,
regionMap,
surface,
natives,
2017-06-10 10:38:20 +02:00
tick)
2017-01-20 07:58:36 +02:00
end
end
end
2017-04-16 08:04:22 +02:00
elseif (entity.type == "unit-spawner") or (entity.type == "turret") then
2017-05-28 06:50:37 +02:00
unregisterEnemyBaseStructure(regionMap, entity)
end
elseif (entity.force.name == "player") then
2016-11-04 01:51:35 +02:00
local creditNatives = false
2017-05-01 00:51:07 +02:00
local entityPosition = entity.position
2016-11-04 01:51:35 +02:00
if (event.force ~= nil) and (event.force.name == "enemy") then
creditNatives = true
2017-04-22 01:14:04 +02:00
local victoryChunk = getChunkByPosition(regionMap, entityPosition.x, entityPosition.y)
2017-05-27 02:58:33 +02:00
if victoryChunk then
victoryScent(victoryChunk, entity.type)
end
2016-11-04 01:51:35 +02:00
end
2017-05-06 11:03:28 +02:00
if creditNatives and natives.safeBuildings and (natives.safeEntities[entity.type] or natives.safeEntityName[entity.name]) then
makeImmortalEntity(surface, entity)
else
2017-05-19 09:47:24 +02:00
addRemovePlayerEntity(regionMap, entity, natives, false, creditNatives)
end
end
end
end
2017-06-01 03:46:53 +02:00
local function onEnemyBaseBuild(event)
local entity = event.entity
registerEnemyBaseStructure(regionMap, entity, nil)
end
local function onSurfaceTileChange(event)
2017-06-02 07:15:21 +02:00
-- local player = game.players[event.player_index]
-- if (player.surface.index == 1) then
-- aiAttackWave.fillTunnel(regionMap, player.surface, natives, event.positions)
2017-06-02 07:15:21 +02:00
-- end
end
2016-09-13 00:33:00 +02:00
local function onInit()
global.regionMap = {}
global.pendingChunks = {}
global.natives = {}
regionMap = global.regionMap
natives = global.natives
pendingChunks = global.pendingChunks
onConfigChanged()
2017-07-05 00:52:20 +02:00
hookEvents()
2016-09-13 00:33:00 +02:00
end
-- hooks
script.on_init(onInit)
script.on_load(onLoad)
2017-05-06 11:03:28 +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_player_built_tile, onSurfaceTileChange)
2017-06-01 03:46:53 +02:00
script.on_event(defines.events.on_biter_base_built,
onEnemyBaseBuild)
script.on_event({defines.events.on_preplayer_mined_item,
defines.events.on_robot_pre_mined},
2016-11-04 09:26:19 +02:00
onPickUp)
script.on_event({defines.events.on_built_entity,
defines.events.on_robot_built_entity},
2016-11-04 09:26:19 +02:00
onBuild)
2016-09-09 12:02:50 +02:00
script.on_event(defines.events.on_entity_died, onDeath)
script.on_event(defines.events.on_tick, onTick)
script.on_event(defines.events.on_chunk_generated, onChunkGenerated)
2017-05-08 08:56:11 +02: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-28 06:50:37 +02:00
clearBases = tests.clearBases,
getOffsetChunk = tests.getOffsetChunk,
2017-06-08 02:57:24 +02:00
registeredNest = tests.registeredNest,
2017-06-09 07:18:59 +02:00
colorResourcePoints = tests.colorResourcePoints,
stepAdvanceTendrils = tests.stepAdvanceTendrils
2017-05-08 08:56:11 +02:00
}
)
2017-05-06 11:03:28 +02:00
remote.add_interface("rampant", interop)