From b7b1a783a627d7957ca5a89170259e27c5c4dbe2 Mon Sep 17 00:00:00 2001 From: Aaron Veden Date: Sun, 28 Nov 2021 17:59:51 -0800 Subject: [PATCH] moved processUpgrades into chunk processor --- control.lua | 2 +- libs/ChunkProcessor.lua | 65 +++++++++++++++++++++++++++++++++++++++++ libs/ChunkUtils.lua | 59 ------------------------------------- 3 files changed, 66 insertions(+), 60 deletions(-) diff --git a/control.lua b/control.lua index 9a164e0..2879d70 100644 --- a/control.lua +++ b/control.lua @@ -47,7 +47,7 @@ local ENERGY_THIEF_LOOKUP = constants.ENERGY_THIEF_LOOKUP local queueGeneratedChunk = mapUtils.queueGeneratedChunk local isRampantSetting = stringUtils.isRampantSetting -local processPendingUpgrades = chunkUtils.processPendingUpgrades +local processPendingUpgrades = chunkProcessor.processPendingUpgrades local canMigrate = aiPredicates.canMigrate local convertTypeToDrainCrystal = unitUtils.convertTypeToDrainCrystal diff --git a/libs/ChunkProcessor.lua b/libs/ChunkProcessor.lua index 9295959..9e10357 100644 --- a/libs/ChunkProcessor.lua +++ b/libs/ChunkProcessor.lua @@ -7,6 +7,8 @@ local chunkProcessor = {} local chunkUtils = require("ChunkUtils") local constants = require("Constants") +local baseUtils = require("BaseUtils") +local mapUtils = require("MapUtils") -- constants @@ -14,6 +16,12 @@ local CHUNK_SIZE = constants.CHUNK_SIZE -- imported functions +local queueGeneratedChunk = mapUtils.queueGeneratedChunk +local getChunkByPosition = mapUtils.getChunkByPosition + +local findNearbyBase = baseUtils.findNearbyBase +local createBase = baseUtils.createBase + local mapScanEnemyChunk = chunkUtils.mapScanEnemyChunk local mapScanPlayerChunk = chunkUtils.mapScanPlayerChunk local mapScanResourceChunk = chunkUtils.mapScanResourceChunk @@ -117,6 +125,63 @@ function chunkProcessor.processPendingChunks(map, tick, flush) map.chunkProcessorIterator = event end +function chunkProcessor.processPendingUpgrades(map, tick) + local pendingUpgrades = map.pendingUpgrades + local entity = map.pendingUpgradeIterator + local entityData + if not entity then + entity, entityData = next(pendingUpgrades, nil) + else + entityData = pendingUpgrades[entity] + end + if entity then + if entity.valid then + map.pendingUpgradeIterator = next(pendingUpgrades, entity) + pendingUpgrades[entity] = nil + local universe = map.universe + local query = universe.upgradeEntityQuery + query.position = entityData.position or entity.position + query.name = entityData.name + local surface = entity.surface + entity.destroy() + if remote.interfaces["kr-creep"] then + remote.call("kr-creep", "spawn_creep_at_position", surface, query.position) + end + local createdEntity = surface.create_entity(query) + if createdEntity and createdEntity.valid and entityData.register then + local chunk = getChunkByPosition(map, createdEntity.position) + if (chunk ~= -1) then + local base = findNearbyBase(map, chunk) + if not base then + base = createBase(map, + chunk, + tick) + end + if base then + chunkUtils.registerEnemyBaseStructure(map, createdEntity, base) + end + else + queueGeneratedChunk( + universe, + { + surface = createdEntity.surface, + area = { + left_top = { + x = createdEntity.position.x, + y = createdEntity.position.y + } + } + } + ) + end + end + else + map.pendingUpgradeIterator = next(pendingUpgrades, entity) + pendingUpgrades[entity] = nil + end + end +end + function chunkProcessor.processScanChunks(map) local area = map.universe.area diff --git a/libs/ChunkUtils.lua b/libs/ChunkUtils.lua index a64393b..4a6058f 100644 --- a/libs/ChunkUtils.lua +++ b/libs/ChunkUtils.lua @@ -81,9 +81,7 @@ local setChunkBase = chunkPropertyUtils.setChunkBase local setPassable = chunkPropertyUtils.setPassable local setPathRating = chunkPropertyUtils.setPathRating -local getChunkByPosition = mapUtils.getChunkByPosition local getChunkByXY = mapUtils.getChunkByXY -local queueGeneratedChunk = mapUtils.queueGeneratedChunk local mMin = math.min local mMax = math.max @@ -421,63 +419,6 @@ function chunkUtils.colorXY(x, y, surface, color) }) end -function chunkUtils.processPendingUpgrades(map, tick) - local pendingUpgrades = map.pendingUpgrades - local entity = map.pendingUpgradeIterator - local entityData - if not entity then - entity, entityData = next(pendingUpgrades, nil) - else - entityData = pendingUpgrades[entity] - end - if entity then - if entity.valid then - map.pendingUpgradeIterator = next(pendingUpgrades, entity) - pendingUpgrades[entity] = nil - local universe = map.universe - local query = universe.upgradeEntityQuery - query.position = entityData.position or entity.position - query.name = entityData.name - local surface = entity.surface - entity.destroy() - if remote.interfaces["kr-creep"] then - remote.call("kr-creep", "spawn_creep_at_position", surface, query.position) - end - local createdEntity = surface.create_entity(query) - if createdEntity and createdEntity.valid and entityData.register then - local chunk = getChunkByPosition(map, createdEntity.position) - if (chunk ~= -1) then - local base = findNearbyBase(map, chunk) - if not base then - base = createBase(map, - chunk, - tick) - end - if base then - chunkUtils.registerEnemyBaseStructure(map, createdEntity, base) - end - else - queueGeneratedChunk( - universe, - { - surface = createdEntity.surface, - area = { - left_top = { - x = createdEntity.position.x, - y = createdEntity.position.y - } - } - } - ) - end - end - else - map.pendingUpgradeIterator = next(pendingUpgrades, entity) - pendingUpgrades[entity] = nil - end - end -end - function chunkUtils.registerEnemyBaseStructure(map, entity, base) local entityType = entity.type if ((entityType == "unit-spawner") or (entityType == "turret")) and (entity.force.name == "enemy") then