1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-14 02:23:01 +02:00
Rampant/libs/Processor.lua

640 lines
21 KiB
Lua
Raw Normal View History

2022-01-15 00:08:58 +02:00
-- Copyright (C) 2022 veden
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-03-12 06:51:13 +02:00
if ProcessorG then
return ProcessorG
2019-02-16 06:17:30 +02:00
end
2023-03-12 06:51:13 +02:00
local Processor = {}
--
local Universe
-- imports
2023-03-12 06:51:13 +02:00
local Utils = require("Utils")
local MapUtils = require("MapUtils")
2023-03-12 06:51:13 +02:00
local Squad = require("Squad")
local Constants = require("Constants")
local ChunkUtils = require("ChunkUtils")
local ChunkPropertyUtils = require("ChunkPropertyUtils")
local BaseUtils = require("BaseUtils")
2023-03-12 06:51:13 +02:00
local MathUtils = require("MathUtils")
2016-09-14 13:16:33 +02:00
-- Constants
2020-04-28 05:41:18 +02:00
local PLAYER_PHEROMONE_GENERATOR_AMOUNT = Constants.PLAYER_PHEROMONE_GENERATOR_AMOUNT
2016-11-04 09:26:19 +02:00
local PROCESS_QUEUE_SIZE = Constants.PROCESS_QUEUE_SIZE
local RESOURCE_QUEUE_SIZE = Constants.RESOURCE_QUEUE_SIZE
local ENEMY_QUEUE_SIZE = Constants.ENEMY_QUEUE_SIZE
local PLAYER_QUEUE_SIZE = Constants.PLAYER_QUEUE_SIZE
2016-09-14 13:16:33 +02:00
local CLEANUP_QUEUE_SIZE = Constants.CLEANUP_QUEUE_SIZE
2016-10-15 02:00:18 +02:00
local PROCESS_PLAYER_BOUND = Constants.PROCESS_PLAYER_BOUND
local AI_VENGENCE_SQUAD_COST = Constants.AI_VENGENCE_SQUAD_COST
2016-11-04 09:26:19 +02:00
local BASE_AI_STATE_AGGRESSIVE = Constants.BASE_AI_STATE_AGGRESSIVE
local BASE_AI_STATE_SIEGE = Constants.BASE_AI_STATE_SIEGE
local BASE_AI_STATE_PEACEFUL = Constants.BASE_AI_STATE_PEACEFUL
local BASE_AI_STATE_MIGRATING = Constants.BASE_AI_STATE_MIGRATING
2016-11-04 09:26:19 +02:00
local COOLDOWN_DRAIN = Constants.COOLDOWN_DRAIN
local COOLDOWN_RALLY = Constants.COOLDOWN_RALLY
local COOLDOWN_RETREAT = Constants.COOLDOWN_RETREAT
2023-03-12 06:51:13 +02:00
local PROXY_ENTITY_LOOKUP = Constants.PROXY_ENTITY_LOOKUP
local BASE_DISTANCE_TO_EVO_INDEX = Constants.BASE_DISTANCE_TO_EVO_INDEX
local BUILDING_SPACE_LOOKUP = Constants.BUILDING_SPACE_LOOKUP
2018-02-12 05:21:28 +02:00
-- imported functions
2023-03-12 06:51:13 +02:00
local findInsertionPoint = MapUtils.findInsertionPoint
local createChunk = ChunkUtils.createChunk
local initialScan = ChunkUtils.initialScan
local euclideanDistancePoints = MathUtils.euclideanDistancePoints
local removeChunkFromMap = MapUtils.removeChunkFromMap
local chunkPassScan = ChunkUtils.chunkPassScan
local findEntityUpgrade = BaseUtils.findEntityUpgrade
local unregisterEnemyBaseStructure = ChunkUtils.unregisterEnemyBaseStructure
local registerEnemyBaseStructure = ChunkUtils.registerEnemyBaseStructure
local setPositionInQuery = Utils.setPositionInQuery
2023-01-08 08:53:47 +02:00
local addPlayerGenerator = ChunkPropertyUtils.addPlayerGenerator
local findNearbyBase = ChunkPropertyUtils.findNearbyBase
local removeChunkToNest = MapUtils.removeChunkToNest
2023-03-12 06:51:13 +02:00
local processPheromone = MapUtils.processPheromone
local getCombinedDeathGeneratorRating = ChunkPropertyUtils.getCombinedDeathGeneratorRating
local processBaseMutation = BaseUtils.processBaseMutation
2020-05-20 04:37:16 +02:00
local processNestActiveness = ChunkPropertyUtils.processNestActiveness
2020-05-20 04:37:16 +02:00
2023-03-12 06:51:13 +02:00
local formSquads = Squad.formSquads
local formVengenceSquad = Squad.formVengenceSquad
local formVengenceSettler = Squad.formVengenceSettler
local formSettlers = Squad.formSettlers
local getChunkByPosition = MapUtils.getChunkByPosition
local getChunkByXY = MapUtils.getChunkByXY
local getChunkById = MapUtils.getChunkById
2016-10-15 02:00:18 +02:00
2023-03-12 06:51:13 +02:00
local validPlayer = Utils.validPlayer
2017-11-21 09:27:03 +02:00
local mapScanEnemyChunk = ChunkUtils.mapScanEnemyChunk
local mapScanPlayerChunk = ChunkUtils.mapScanPlayerChunk
local mapScanResourceChunk = ChunkUtils.mapScanResourceChunk
local getEnemyStructureCount = ChunkPropertyUtils.getEnemyStructureCount
2023-03-12 06:51:13 +02:00
local canAttack = BaseUtils.canAttack
local canMigrate = BaseUtils.canMigrate
2017-05-14 00:32:16 +02:00
2021-02-14 06:49:54 +02:00
local tableSize = table_size
2017-05-28 06:50:37 +02:00
2016-08-29 02:05:28 +02:00
local mMin = math.min
local mMax = math.max
2023-03-12 06:51:13 +02:00
local tableInsert = table.insert
2016-08-29 02:05:28 +02:00
2020-05-20 04:37:16 +02:00
local next = next
-- module code
2016-10-15 02:00:18 +02:00
--[[
processing is not consistant as it depends on the number of chunks that have been generated
so if we process 400 chunks an iteration and 200 chunks have been generated than these are
processed 3 times a second and 1200 generated chunks would be processed once a second
2019-02-03 08:01:28 +02:00
In theory, this might be fine as smaller bases have less surface to attack and need to have
2016-10-15 02:00:18 +02:00
pheromone dissipate at a faster rate.
--]]
2023-03-12 06:51:13 +02:00
function Processor.processMap(map, tick)
2018-01-14 07:48:21 +02:00
local processQueue = map.processQueue
local processQueueLength = #processQueue
2018-10-20 07:17:37 +02:00
2020-12-05 06:49:51 +02:00
if (processQueueLength == 0) then
return
end
2021-02-14 06:49:54 +02:00
local startIndex = mMin(map.processIndex, processQueueLength)
local step
local endIndex
if map.outgoingScanWave then
step = 1
endIndex = mMin(startIndex + PROCESS_QUEUE_SIZE, processQueueLength)
if (endIndex == processQueueLength) then
map.outgoingScanWave = false
map.processIndex = processQueueLength
else
map.processIndex = endIndex + 1
end
else
step = -1
endIndex = mMax(startIndex - PROCESS_QUEUE_SIZE, 1)
if (endIndex == 1) then
map.outgoingScanWave = true
map.processIndex = 1
else
map.processIndex = endIndex - 1
end
end
Universe.processedChunks = Universe.processedChunks + ((startIndex - endIndex) * step)
for x=startIndex,endIndex,step do
processPheromone(map, processQueue[x], tick)
end
end
2016-10-15 02:00:18 +02:00
--[[
Localized player radius were processing takes place in realtime, doesn't store state
between calls.
2019-02-03 08:01:28 +02:00
vs
2016-10-15 02:00:18 +02:00
the slower passive version processing the entire map in multiple passes.
--]]
2023-03-12 06:51:13 +02:00
function Processor.processPlayers(players, tick)
2016-10-15 02:00:18 +02:00
-- put down player pheromone for player hunters
-- randomize player order to ensure a single player isn't singled out
2018-05-26 00:23:22 +02:00
-- not looping everyone because the cost is high enough already in multiplayer
local playerCount = #players
local playerMaxGenerator = playerCount * PLAYER_PHEROMONE_GENERATOR_AMOUNT
for i=1,playerCount do
local player = players[i]
if validPlayer(player) then
local char = player.character
local map = Universe.maps[char.surface.index]
if map then
local playerChunk = getChunkByPosition(map, char.position)
if (playerChunk ~= -1) then
addPlayerGenerator(playerChunk, playerMaxGenerator)
end
end
end
end
2019-12-07 07:57:20 +02:00
if (#players > 0) then
local player = players[Universe.random(#players)]
2021-02-20 09:31:36 +02:00
if validPlayer(player) then
2021-12-06 03:16:14 +02:00
local char = player.character
local map = Universe.maps[char.surface.index]
2021-12-06 03:16:14 +02:00
if map then
local playerChunk = getChunkByPosition(map, char.position)
if (playerChunk ~= -1) then
local base = findNearbyBase(playerChunk)
if not base then
return
end
local allowingAttacks = canAttack(map, base)
2021-12-06 03:16:14 +02:00
local vengence = allowingAttacks and
(base.unitPoints >= AI_VENGENCE_SQUAD_COST) and
((getEnemyStructureCount(playerChunk) > 0) or
(getCombinedDeathGeneratorRating(playerChunk) < Universe.retreatThreshold))
2021-12-06 03:16:14 +02:00
for x=playerChunk.x - PROCESS_PLAYER_BOUND, playerChunk.x + PROCESS_PLAYER_BOUND, 32 do
for y=playerChunk.y - PROCESS_PLAYER_BOUND, playerChunk.y + PROCESS_PLAYER_BOUND, 32 do
local chunk = getChunkByXY(map, x, y)
if (chunk ~= -1) then
processPheromone(map, chunk, tick, true)
2021-12-06 03:16:14 +02:00
if chunk.nestCount then
processNestActiveness(chunk, tick)
2021-12-06 03:16:14 +02:00
if vengence then
2023-03-12 04:14:42 +02:00
Universe.vengenceQueue[chunk.id] = chunk
2021-12-06 03:16:14 +02:00
end
2020-05-20 04:37:16 +02:00
end
2019-02-20 08:16:43 +02:00
end
2019-10-19 21:13:48 +02:00
end
end
end
end
end
2016-10-15 02:00:18 +02:00
end
end
2023-03-12 04:14:42 +02:00
local function processCleanUp(chunks, tick, duration)
local chunkId, eventTick = next(chunks, nil)
if not chunkId then
2023-03-12 04:14:42 +02:00
return
end
2023-03-12 04:14:42 +02:00
if (tick - eventTick) > duration then
chunks[chunkId] = nil
end
end
2023-03-12 06:51:13 +02:00
function Processor.cleanUpMapTables(tick)
local retreats = Universe.chunkToRetreats
local rallys = Universe.chunkToRallys
local drained = Universe.chunkToDrained
for _=1,CLEANUP_QUEUE_SIZE do
2023-03-12 04:14:42 +02:00
processCleanUp(retreats, tick, COOLDOWN_RETREAT)
2018-02-12 05:21:28 +02:00
2023-03-12 04:14:42 +02:00
processCleanUp(rallys, tick, COOLDOWN_RALLY)
2018-02-12 05:21:28 +02:00
2023-03-12 04:14:42 +02:00
processCleanUp(drained, tick, COOLDOWN_DRAIN)
end
end
--[[
Passive scan to find entities that have been generated outside the factorio event system
--]]
2023-03-12 06:51:13 +02:00
function Processor.scanPlayerMap(map, tick)
2021-02-14 06:49:54 +02:00
if (map.nextProcessMap == tick) or (map.nextPlayerScan == tick) or
(map.nextEnemyScan == tick) or (map.nextChunkProcess == tick)
then
return
end
local index = map.scanPlayerIndex
local processQueue = map.processQueue
2020-12-05 06:49:51 +02:00
local processQueueLength = #processQueue
2021-02-14 06:49:54 +02:00
2020-12-05 06:49:51 +02:00
local endIndex = mMin(index + PLAYER_QUEUE_SIZE, processQueueLength)
if (processQueueLength == 0) then
return
end
for x=index,endIndex do
mapScanPlayerChunk(processQueue[x], map)
end
2020-12-05 06:49:51 +02:00
if (endIndex == processQueueLength) then
map.scanPlayerIndex = 1
else
map.scanPlayerIndex = endIndex + 1
end
end
2023-03-12 06:51:13 +02:00
function Processor.scanEnemyMap(map, tick)
local index = map.scanEnemyIndex
local processQueue = map.processQueue
2020-12-05 06:49:51 +02:00
local processQueueLength = #processQueue
2021-02-14 06:49:54 +02:00
local endIndex = mMin(index + ENEMY_QUEUE_SIZE, #processQueue)
2019-02-19 02:43:01 +02:00
2020-12-05 06:49:51 +02:00
if (processQueueLength == 0) then
return
end
2021-02-14 06:49:54 +02:00
for x=index,endIndex do
mapScanEnemyChunk(processQueue[x], map, tick)
end
2017-04-22 01:14:04 +02:00
2020-12-05 06:49:51 +02:00
if (endIndex == processQueueLength) then
map.scanEnemyIndex = 1
2016-08-29 02:05:28 +02:00
else
map.scanEnemyIndex = endIndex + 1
end
end
2023-03-12 06:51:13 +02:00
function Processor.scanResourceMap(map, tick)
local index = map.scanResourceIndex
2021-02-14 06:49:54 +02:00
local processQueue = map.processQueue
2020-12-05 06:49:51 +02:00
local processQueueLength = #processQueue
2021-02-14 06:49:54 +02:00
2020-12-05 06:49:51 +02:00
local endIndex = mMin(index + RESOURCE_QUEUE_SIZE, processQueueLength)
2020-12-05 06:49:51 +02:00
if (processQueueLength == 0) then
return
end
2021-02-14 06:49:54 +02:00
for x=index,endIndex do
mapScanResourceChunk(processQueue[x], map)
end
2020-12-05 06:49:51 +02:00
if (endIndex == processQueueLength) then
map.scanResourceIndex = 1
else
map.scanResourceIndex = endIndex + 1
end
end
2023-03-12 06:51:13 +02:00
function Processor.processVengence()
local vengenceQueue = Universe.vengenceQueue
2023-03-12 04:14:42 +02:00
local chunkId, chunk = next(vengenceQueue, nil)
if not chunkId then
2021-12-06 05:40:39 +02:00
if (tableSize(vengenceQueue) == 0) then
Universe.vengenceQueue = {}
end
2023-03-12 04:14:42 +02:00
return
end
vengenceQueue[chunkId] = nil
local map = chunk.map
if not map.surface.valid then
return
end
local base = chunk.base
if canMigrate(map, base) and (Universe.random() < 0.075) then
formVengenceSettler(chunk, base)
2020-05-20 04:37:16 +02:00
else
2023-03-12 04:14:42 +02:00
formVengenceSquad(chunk, base)
2020-05-20 04:37:16 +02:00
end
end
2023-03-12 06:51:13 +02:00
function Processor.processNests(tick)
local chunkId = Universe.processNestIterator
local chunkPack
if not chunkId then
chunkId,chunkPack = next(Universe.chunkToNests, nil)
else
chunkPack = Universe.chunkToNests[chunkId]
end
if not chunkId then
Universe.processNestIterator = nil
else
Universe.processNestIterator = next(Universe.chunkToNests, chunkId)
local map = chunkPack.map
if not map.surface.valid then
removeChunkToNest(chunkId)
return
end
local chunk = getChunkById(chunkId)
processNestActiveness(chunk, tick)
2020-05-20 04:37:16 +02:00
if Universe.NEW_ENEMIES then
processBaseMutation(chunk,
map,
chunk.base)
end
end
end
local function processSpawnersBody(iterator, chunks)
local chunkId = Universe[iterator]
local chunkPack
if not chunkId then
chunkId,chunkPack = next(chunks, nil)
else
chunkPack = chunks[chunkId]
end
if not chunkId then
Universe[iterator] = nil
else
Universe[iterator] = next(chunks, chunkId)
2023-03-12 06:51:13 +02:00
local map = chunkPack.map -- error
if not map.surface.valid then
if (iterator == "processMigrationIterator") then
removeChunkToNest(chunkId)
else
chunks[chunkId] = nil
end
return
end
local chunk = getChunkById(chunkId)
local base = findNearbyBase(chunk)
if base.stateAI == BASE_AI_STATE_PEACEFUL then
return
end
if iterator == "processMigrationIterator" then
if (base.stateAI ~= BASE_AI_STATE_MIGRATING) and (base.stateAI ~= BASE_AI_STATE_SIEGE) then
return
end
elseif iterator == "processActiveRaidSpawnerIterator" then
if (base.stateAI == BASE_AI_STATE_AGGRESSIVE) or (base.stateAI == BASE_AI_STATE_MIGRATING) then
return
end
elseif iterator == "processActiveSpawnerIterator" then
if (base.stateAI == BASE_AI_STATE_MIGRATING) then
return
end
end
2023-03-12 06:51:13 +02:00
local migrate = canMigrate(base)
local attack = canAttack(base)
2021-02-20 07:41:30 +02:00
if migrate then
2023-03-12 04:14:42 +02:00
formSettlers(chunk, base)
2021-12-10 21:12:54 +02:00
end
if attack then
2023-03-12 04:14:42 +02:00
formSquads(chunk, base)
end
2020-05-20 04:37:16 +02:00
end
end
2020-04-28 05:41:18 +02:00
2023-03-12 06:51:13 +02:00
function Processor.processAttackWaves()
processSpawnersBody("processActiveSpawnerIterator",
Universe.chunkToActiveNest)
processSpawnersBody("processActiveRaidSpawnerIterator",
Universe.chunkToActiveRaidNest)
processSpawnersBody("processMigrationIterator",
Universe.chunkToNests)
2020-05-20 04:37:16 +02:00
end
2023-03-12 06:51:13 +02:00
function Processor.processClouds(tick)
2023-03-12 04:14:42 +02:00
local eventId, builderPack = next(Universe.settlePurpleCloud, nil)
2023-01-08 08:53:47 +02:00
if builderPack and (builderPack.tick <= tick) then
2023-03-12 04:14:42 +02:00
Universe.settlePurpleCloud[eventId] = nil
2023-01-08 08:53:47 +02:00
local map = builderPack.map
if builderPack.group.valid and map.surface.valid then
setPositionInQuery(
Universe.obaCreateBuildCloudQuery,
2023-03-12 04:14:42 +02:00
builderPack.group.position
)
map.surface.create_entity(Universe.obaCreateBuildCloudQuery)
2023-01-08 08:53:47 +02:00
end
end
end
2023-03-12 06:51:13 +02:00
function Processor.processPendingChunks(tick, flush)
local pendingChunks = Universe.pendingChunks
local eventId, event = next(pendingChunks, nil)
if not eventId then
if (tableSize(pendingChunks) == 0) then
-- this is needed as the next command remembers the max length a table has been
Universe.pendingChunks = {}
end
return
end
local endCount = 1
if flush then
endCount = tableSize(pendingChunks)
end
for _=1,endCount do
if not flush and (event.tick > tick) then
return
end
local newEventId, newEvent = next(pendingChunks, eventId)
pendingChunks[eventId] = nil
local map = event.map
if not map.surface.valid then
return
end
local topLeft = event.area.left_top
local x = topLeft.x
local y = topLeft.y
if not map[x] then
map[x] = {}
end
if map[x][y] then
local oldChunk = map[x][y]
local chunk = initialScan(oldChunk, map, tick)
if (chunk == -1) then
removeChunkFromMap(map, oldChunk)
end
else
local initialChunk = createChunk(map, x, y)
map[x][y] = initialChunk
Universe.chunkIdToChunk[initialChunk.id] = initialChunk
local chunk = initialScan(initialChunk, map, tick)
if (chunk ~= -1) then
tableInsert(
map.processQueue,
findInsertionPoint(map.processQueue, chunk),
chunk
)
else
map[x][y] = nil
Universe.chunkIdToChunk[initialChunk.id] = nil
end
end
eventId = newEventId
event = newEvent
if not eventId then
return
end
end
end
function Processor.processPendingUpgrades(tick)
local entityId, entityData = next(Universe.pendingUpgrades, nil)
if not entityId then
if tableSize(Universe.pendingUpgrades) == 0 then
Universe.pendingUpgrades = {}
end
return
end
local entity = entityData.entity
if not entity.valid then
Universe.pendingUpgrades[entityId] = nil
end
if entityData.delayTLL and tick < entityData.delayTLL then
return
end
Universe.pendingUpgrades[entityId] = nil
local base = entityData.base
local map = base.map
local baseAlignment = base.alignment
local position = entityData.position or entity.position
local pickedBaseAlignment
if baseAlignment[2] then
if Universe.random() < 0.75 then
pickedBaseAlignment = baseAlignment[2]
else
pickedBaseAlignment = baseAlignment[1]
end
else
pickedBaseAlignment = baseAlignment[1]
end
local currentEvo = entity.prototype.build_base_evolution_requirement or 0
local distance = mMin(1, euclideanDistancePoints(position.x, position.y, 0, 0) * BASE_DISTANCE_TO_EVO_INDEX)
local evoIndex = mMax(distance, Universe.evolutionLevel)
local name = findEntityUpgrade(pickedBaseAlignment,
currentEvo,
evoIndex,
entity,
map,
entityData.evolve)
local entityName = entity.name
if not name and PROXY_ENTITY_LOOKUP[entityName] then
entity.destroy()
return
elseif (name == entityName) or not name then
return
end
local surface = entity.surface
local query = Universe.ppuUpgradeEntityQuery
query.name = name
unregisterEnemyBaseStructure(map, entity, nil, true)
entity.destroy()
local foundPosition = surface.find_non_colliding_position(BUILDING_SPACE_LOOKUP[name],
position,
2,
1,
true)
setPositionInQuery(query, foundPosition or position)
local createdEntity = surface.create_entity({
name = query.name,
position = query.position
})
if createdEntity and createdEntity.valid then
if entityData.register then
registerEnemyBaseStructure(map, createdEntity, base, tick, true)
end
if not entityData.evolve and Universe.printBaseUpgrades then
surface.print("["..base.id.."]:"..surface.name.." Upgrading ".. entityName .. " to " .. name .. " [gps=".. position.x ..",".. position.y .."]")
end
if remote.interfaces["kr-creep"] then
remote.call("kr-creep", "spawn_creep_at_position", surface, foundPosition or position, false, createdEntity.name)
end
end
end
function Processor.processScanChunks()
local chunkId, chunk = next(Universe.chunkToPassScan, nil)
if not chunkId then
if (tableSize(Universe.chunkToPassScan) == 0) then
-- this is needed as the next command remembers the max length a table has been
Universe.chunkToPassScan = {}
end
return
end
Universe.chunkToPassScan[chunkId] = nil
local map = chunk.map
if not map.surface.valid then
return
end
if (chunkPassScan(chunk, map) == -1) then
removeChunkFromMap(map, chunk)
end
end
function Processor.init(universe)
Universe = universe
end
2023-03-12 06:51:13 +02:00
ProcessorG = Processor
return Processor