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-11 22:53:06 +02:00
|
|
|
if ChunkPropertyUtilsG then
|
|
|
|
return ChunkPropertyUtilsG
|
2019-02-16 06:17:30 +02:00
|
|
|
end
|
2023-03-11 22:53:06 +02:00
|
|
|
local ChunkPropertyUtils = {}
|
2018-01-15 09:41:55 +02:00
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
--
|
2018-09-24 06:56:45 +02:00
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
local Universe
|
2023-03-12 03:21:22 +02:00
|
|
|
local Position
|
2019-03-08 05:40:55 +02:00
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
--
|
2023-01-19 07:49:15 +02:00
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
local Constants = require("Constants")
|
|
|
|
local MathUtils = require("MathUtils")
|
2021-12-09 08:01:54 +02:00
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
-- Constants
|
2019-12-16 03:16:56 +02:00
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local DURATION_ACTIVE_NEST = Constants.DURATION_ACTIVE_NEST
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
local PLAYER_GENERATOR_PERSISTANCE = Constants.PLAYER_GENERATOR_PERSISTANCE
|
|
|
|
local PLAYER_PHEROMONE_GENERATOR_AMOUNT = Constants.PLAYER_PHEROMONE_GENERATOR_AMOUNT
|
2018-01-15 09:41:55 +02:00
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
local COOLDOWN_DRAIN = Constants.COOLDOWN_DRAIN
|
2018-01-15 09:41:55 +02:00
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
local RAIDING_MINIMUM_BASE_THRESHOLD = Constants.RAIDING_MINIMUM_BASE_THRESHOLD
|
|
|
|
|
|
|
|
local PLAYER_PHEROMONE = Constants.PLAYER_PHEROMONE
|
|
|
|
local BASE_PHEROMONE = Constants.BASE_PHEROMONE
|
|
|
|
|
|
|
|
local MOVEMENT_GENERATOR_PERSISTANCE = Constants.MOVEMENT_GENERATOR_PERSISTANCE
|
|
|
|
local CHUNK_ALL_DIRECTIONS = Constants.CHUNK_ALL_DIRECTIONS
|
|
|
|
|
|
|
|
local MAGIC_MAXIMUM_NUMBER = Constants.MAGIC_MAXIMUM_NUMBER
|
2022-03-20 01:29:12 +02:00
|
|
|
|
2019-12-16 03:16:56 +02:00
|
|
|
-- imported functions
|
|
|
|
|
2023-03-12 23:21:04 +02:00
|
|
|
local activateMap
|
|
|
|
|
2023-03-21 09:02:49 +02:00
|
|
|
local euclideanDistancePoints = MathUtils.euclideanDistancePoints
|
2023-03-11 22:53:06 +02:00
|
|
|
local tableSize = table_size
|
2022-03-20 01:29:12 +02:00
|
|
|
|
2019-12-16 03:16:56 +02:00
|
|
|
local mMin = math.min
|
|
|
|
|
2018-01-15 09:41:55 +02:00
|
|
|
-- module code
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local function getActiveTick(chunk)
|
|
|
|
return chunk.activeTick or 0
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local function registerActiveSpawner(chunk, register, activeType, tick)
|
2023-03-12 22:02:31 +02:00
|
|
|
local chunkId = chunk.id
|
|
|
|
if Universe[register][chunkId] then
|
2023-03-12 03:21:22 +02:00
|
|
|
return
|
|
|
|
end
|
2019-11-30 02:49:22 +02:00
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.activeTick = tick + DURATION_ACTIVE_NEST
|
2019-11-30 02:49:22 +02:00
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local base = chunk.base
|
|
|
|
base[activeType] = base[activeType] + 1
|
2023-03-12 22:02:31 +02:00
|
|
|
Universe[register][chunkId] = chunk
|
2019-11-30 02:49:22 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 22:02:31 +02:00
|
|
|
local function unregisterActiveSpawner(chunk, register, iterator, activeType)
|
|
|
|
local chunkId = chunk.id
|
|
|
|
if not Universe[register][chunkId] then
|
2023-03-12 03:21:22 +02:00
|
|
|
return
|
2021-12-04 21:04:13 +02:00
|
|
|
end
|
2019-11-30 02:49:22 +02:00
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.activeTick = nil
|
2019-11-30 02:49:22 +02:00
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local base = chunk.base
|
|
|
|
base[activeType] = base[activeType] - 1
|
2023-03-12 22:02:31 +02:00
|
|
|
Universe[register][chunkId] = nil
|
|
|
|
if (Universe[iterator] == chunkId) then
|
|
|
|
Universe[iterator] = nil
|
2021-12-04 21:04:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local function registerActiveNest(chunk, tick)
|
|
|
|
registerActiveSpawner(chunk, "chunkToActiveNest", "activeNests", tick)
|
2019-11-30 02:49:22 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local function registerActiveRaidNest(chunk, tick)
|
|
|
|
registerActiveSpawner(chunk, "chunkToActiveRaidNest", "activeRaidNests", tick)
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local function unregisterActiveNest(chunk)
|
2023-03-12 22:02:31 +02:00
|
|
|
unregisterActiveSpawner(
|
|
|
|
chunk,
|
|
|
|
"chunkToActiveNest",
|
|
|
|
"processActiveSpawnerIterator",
|
|
|
|
"activeNests"
|
|
|
|
)
|
2021-12-04 21:04:13 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local function unregisterActiveRaidNest(chunk)
|
2023-03-12 22:02:31 +02:00
|
|
|
unregisterActiveSpawner(
|
|
|
|
chunk,
|
|
|
|
"chunkToActiveRaidNest",
|
|
|
|
"processActiveRaidSpawnerIterator",
|
|
|
|
"activeRaidNests"
|
|
|
|
)
|
2021-12-04 21:04:13 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local function addEnemyStructure(chunk, unitNumber, ids, counts, register)
|
2023-03-12 23:21:04 +02:00
|
|
|
activateMap(chunk.map)
|
2023-03-12 03:21:22 +02:00
|
|
|
local chunkId = chunk.id
|
|
|
|
local entityIds = chunk[ids]
|
|
|
|
if not entityIds then
|
|
|
|
entityIds = {}
|
|
|
|
chunk[ids] = entityIds
|
|
|
|
end
|
2023-03-12 22:02:31 +02:00
|
|
|
|
|
|
|
if entityIds[unitNumber] then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
entityIds[unitNumber] = true
|
|
|
|
chunk[counts] = (chunk[counts] or 0) + 1
|
|
|
|
if register then
|
|
|
|
Universe[register][chunkId] = chunk
|
2021-12-04 21:04:13 +02:00
|
|
|
end
|
2023-03-12 22:02:31 +02:00
|
|
|
return true
|
2021-12-04 21:04:13 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
local function removeEnemyStructure(chunk, unitNumber, ids, counts, register)
|
2021-12-08 04:38:02 +02:00
|
|
|
local chunkId = chunk.id
|
2023-03-12 03:21:22 +02:00
|
|
|
local entityIds = chunk[ids]
|
2023-03-12 22:02:31 +02:00
|
|
|
if not (entityIds and entityIds[unitNumber]) then
|
|
|
|
return false
|
|
|
|
end
|
2021-12-04 21:04:13 +02:00
|
|
|
|
2023-03-12 22:02:31 +02:00
|
|
|
entityIds[unitNumber] = nil
|
|
|
|
chunk[counts] = chunk[counts] - 1
|
|
|
|
if chunk[counts] > 0 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
chunk[ids] = nil
|
|
|
|
chunk[counts] = nil
|
|
|
|
if register then
|
|
|
|
Universe[register][chunkId] = nil
|
|
|
|
end
|
|
|
|
if (ids == "nestIds") then
|
|
|
|
unregisterActiveNest(chunk)
|
|
|
|
unregisterActiveRaidNest(chunk)
|
2023-03-12 03:21:22 +02:00
|
|
|
if (Universe.processMigrationIterator == chunkId) then
|
|
|
|
Universe.processMigrationIterator = nil
|
|
|
|
end
|
|
|
|
if (Universe.processNestIterator == chunkId) then
|
|
|
|
Universe.processNestIterator = nil
|
2020-05-22 08:57:03 +02:00
|
|
|
end
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
2023-03-12 22:02:31 +02:00
|
|
|
return true
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.addTurretCount(chunk, unitNumber)
|
|
|
|
return addEnemyStructure(chunk, unitNumber, "turretIds", "turretCount")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ChunkPropertyUtils.removeTurretCount(chunk, unitNumber)
|
|
|
|
return removeEnemyStructure(chunk, unitNumber, "turretIds", "turretCount")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ChunkPropertyUtils.addUtilitiesCount(chunk, unitNumber)
|
|
|
|
return addEnemyStructure(chunk, unitNumber, "utilityIds", "utilityCount", "chunkToUtilities")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ChunkPropertyUtils.removeUtilitiesCount(chunk, unitNumber)
|
|
|
|
return removeEnemyStructure(chunk, unitNumber, "utilityIds", "utilityCount", "chunkToUtilities")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ChunkPropertyUtils.addHiveCount(chunk, unitNumber)
|
|
|
|
return addEnemyStructure(chunk, unitNumber, "hiveIds", "hiveCount", "chunkToHives")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ChunkPropertyUtils.removeHiveCount(chunk, unitNumber)
|
|
|
|
return removeEnemyStructure(chunk, unitNumber, "hiveIds", "hiveCount", "chunkToHives")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ChunkPropertyUtils.addNestCount(chunk, unitNumber)
|
|
|
|
return addEnemyStructure(chunk, unitNumber, "nestIds", "nestCount", "chunkToNests")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ChunkPropertyUtils.removeNestCount(chunk, unitNumber)
|
|
|
|
return removeEnemyStructure(chunk, unitNumber, "nestIds", "nestCount", "chunkToNests")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ChunkPropertyUtils.isActiveNest(chunk)
|
|
|
|
return Universe.chunkToActiveNest[chunk.id] ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function ChunkPropertyUtils.isActiveRaidNest(chunk)
|
|
|
|
return Universe.chunkToActiveRaidNest[chunk.id] ~= nil
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function ChunkPropertyUtils.addBaseResourceChunk(base, chunk)
|
2023-03-12 03:21:22 +02:00
|
|
|
if not chunk.resourceGenerator then
|
|
|
|
return
|
2023-01-19 07:38:07 +02:00
|
|
|
end
|
2023-03-12 03:21:22 +02:00
|
|
|
|
|
|
|
base.resourceChunkCount = base.resourceChunkCount + 1
|
|
|
|
base.resourceChunks[chunk.id] = true
|
2023-01-19 07:38:07 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function ChunkPropertyUtils.removeBaseResourceChunk(base, chunk)
|
2023-03-12 03:21:22 +02:00
|
|
|
if not base.resourceChunks[chunk.id] then
|
|
|
|
return
|
2023-01-19 07:38:07 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
base.resourceChunkCount = base.resourceChunkCount - 1
|
|
|
|
base.resourceChunks[chunk.id] = nil
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.removeChunkBase(chunk, base)
|
|
|
|
if not chunk.base then
|
|
|
|
return
|
2021-12-04 21:04:13 +02:00
|
|
|
end
|
2023-03-12 03:21:22 +02:00
|
|
|
|
|
|
|
base.chunkCount = base.chunkCount - 1
|
2023-03-13 05:48:44 +02:00
|
|
|
base.totalX = base.totalX - chunk.x
|
|
|
|
base.totalY = base.totalY - chunk.y
|
|
|
|
base.x = base.totalX / base.chunkCount
|
|
|
|
base.y = base.totalY / base.chunkCount
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.base = nil
|
2021-12-04 21:04:13 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.setChunkBase(chunk, base)
|
|
|
|
if chunk.base then
|
|
|
|
return
|
2021-12-04 21:04:13 +02:00
|
|
|
end
|
2023-03-12 03:21:22 +02:00
|
|
|
|
|
|
|
base.chunkCount = base.chunkCount + 1
|
2023-03-13 05:48:44 +02:00
|
|
|
base.totalX = base.totalX + chunk.x
|
|
|
|
base.totalY = base.totalY + chunk.y
|
|
|
|
base.x = base.totalX / base.chunkCount
|
|
|
|
base.y = base.totalY / base.chunkCount
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.base = base
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getEnemyStructureCount(chunk)
|
|
|
|
return (chunk.nestCount or 0)
|
|
|
|
+ (chunk.turretCount or 0)
|
|
|
|
+ (chunk.utilityCount or 0)
|
|
|
|
+ (chunk.hiveCount or 0)
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function ChunkPropertyUtils.setDrainPylons(map, entity1, entity2)
|
2021-12-09 08:01:54 +02:00
|
|
|
local pair = {entity1, entity2}
|
|
|
|
map.drainPylons[entity1.unit_number] = pair
|
|
|
|
map.drainPylons[entity2.unit_number] = pair
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function ChunkPropertyUtils.removeDrainPylons(map, unitNumber)
|
2021-12-09 08:01:54 +02:00
|
|
|
local pair = map.drainPylons[unitNumber]
|
|
|
|
if pair then
|
|
|
|
local target = pair[1]
|
|
|
|
local pole = pair[2]
|
|
|
|
if target.unit_number == unitNumber then
|
|
|
|
map.drainPylons[unitNumber] = nil
|
|
|
|
if pole.valid then
|
|
|
|
map.drainPylons[pole.unit_number] = nil
|
2021-12-17 05:21:04 +02:00
|
|
|
pole.destroy()
|
2021-12-09 08:01:54 +02:00
|
|
|
end
|
|
|
|
elseif (pole.unit_number == unitNumber) then
|
|
|
|
map.drainPylons[unitNumber] = nil
|
|
|
|
if target.valid then
|
|
|
|
map.drainPylons[target.unit_number] = nil
|
2021-12-17 05:21:04 +02:00
|
|
|
target.die()
|
2021-12-09 08:01:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function ChunkPropertyUtils.getDrainPylonPair(map, unitNumber)
|
2021-12-09 08:01:54 +02:00
|
|
|
return map.drainPylons[unitNumber]
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function ChunkPropertyUtils.isDrained(chunk, tick)
|
2023-03-12 03:21:22 +02:00
|
|
|
local drainTick = Universe.chunkToDrained[chunk.id]
|
|
|
|
if not drainTick then
|
2021-12-09 08:01:54 +02:00
|
|
|
return false
|
|
|
|
end
|
2023-03-12 03:21:22 +02:00
|
|
|
return (tick - drainTick) < COOLDOWN_DRAIN
|
2021-12-09 08:01:54 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.setDrainedTick(chunk, tick)
|
|
|
|
Universe.chunkToDrained[chunk.id] = tick
|
2021-12-09 08:01:54 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function ChunkPropertyUtils.getRetreatTick(chunk)
|
2023-03-12 03:21:22 +02:00
|
|
|
return Universe.chunkToRetreats[chunk.id] or 0
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function ChunkPropertyUtils.getRallyTick(chunk)
|
2023-03-12 03:21:22 +02:00
|
|
|
return Universe.chunkToRallys[chunk.id] or 0
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.setRallyTick(chunk, tick)
|
|
|
|
Universe.chunkToRallys[chunk.id] = tick
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.setRetreatTick(chunk, tick)
|
|
|
|
Universe.chunkToRetreats[chunk.id] = tick
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.setResourceGenerator(chunk, resourceGenerator)
|
2019-05-16 07:11:43 +02:00
|
|
|
if (resourceGenerator <= 0) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.resourceGenerator = nil
|
2018-01-15 09:41:55 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.resourceGenerator = resourceGenerator
|
2021-12-08 07:36:56 +02:00
|
|
|
end
|
2019-02-20 08:16:43 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getResourceGenerator(chunk)
|
|
|
|
return chunk.resourceGenerator or 0
|
2019-02-20 08:16:43 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.addResourceGenerator(chunk, delta)
|
|
|
|
chunk.resourceGenerator = (chunk.resourceGenerator or 0) + delta
|
2019-02-20 08:16:43 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getPassable(chunk)
|
|
|
|
return chunk.passable or CHUNK_ALL_DIRECTIONS
|
2019-02-20 08:16:43 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.setPassable(chunk, value)
|
2018-09-24 06:56:45 +02:00
|
|
|
if (value == CHUNK_ALL_DIRECTIONS) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.passable = nil
|
2018-09-24 06:56:45 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.passable = value
|
2018-09-24 06:56:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getPathRating(chunk)
|
|
|
|
return chunk.pathRating or 1
|
2018-09-24 06:56:45 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.setPathRating(chunk, value)
|
2018-09-24 06:56:45 +02:00
|
|
|
if (value == 1) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.pathRating = nil
|
2018-09-24 06:56:45 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.pathRating = value
|
2018-09-24 06:56:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getDeathGeneratorRating(chunk)
|
|
|
|
return 1 + (chunk.deathGenerator or 0)
|
2023-01-08 03:59:21 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getCombinedDeathGeneratorRating(chunk)
|
|
|
|
local amount = 1 + ((chunk.deathGenerator or 0) + (chunk.permanentDeathGenerator or 0))
|
2023-01-08 03:59:21 +02:00
|
|
|
if (amount > 1) then
|
|
|
|
return 1
|
2023-04-05 07:48:39 +02:00
|
|
|
elseif (amount <= 0) then
|
|
|
|
return 0.001
|
2023-01-08 03:59:21 +02:00
|
|
|
else
|
|
|
|
return amount
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getDeathGenerator(chunk)
|
|
|
|
return chunk.deathGenerator or 0
|
2023-01-08 03:59:21 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getPermanentDeathGeneratorRating(chunk)
|
|
|
|
return 1 + (chunk.permanentDeathGenerator or 0)
|
2023-01-08 03:59:21 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getCombinedDeathGenerator(chunk)
|
|
|
|
local amount = (chunk.deathGenerator or 0) + (chunk.permanentDeathGenerator or 0)
|
2023-01-08 03:59:21 +02:00
|
|
|
if (amount > 1) then
|
|
|
|
return 1
|
|
|
|
elseif (amount < -1) then
|
|
|
|
return -1
|
|
|
|
else
|
|
|
|
return amount
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.addPermanentDeathGenerator(chunk, amount)
|
|
|
|
local adjustedAmount = (amount * 0.25) + (chunk.permanentDeathGenerator or 0)
|
2023-01-08 03:59:21 +02:00
|
|
|
if (adjustedAmount > 0.75) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.permanentDeathGenerator = 0.75
|
2023-01-08 03:59:21 +02:00
|
|
|
elseif (adjustedAmount < -0.75) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.permanentDeathGenerator = -0.75
|
2023-01-08 03:59:21 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.permanentDeathGenerator = adjustedAmount
|
2023-01-08 03:59:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.addDeathGenerator(chunk, value)
|
|
|
|
local currentAmount = (chunk.deathGenerator or 0) + value
|
2023-01-08 03:59:21 +02:00
|
|
|
if (currentAmount > 1) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.deathGenerator = 1
|
2023-01-08 03:59:21 +02:00
|
|
|
elseif (currentAmount < -1) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.deathGenerator = -1
|
2023-01-08 03:59:21 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.deathGenerator = currentAmount
|
2023-01-08 03:59:21 +02:00
|
|
|
end
|
2022-02-21 04:21:04 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.setDeathGenerator(chunk, value)
|
2023-01-08 03:59:21 +02:00
|
|
|
if (value > 1) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.deathGenerator = 1
|
2023-01-08 03:59:21 +02:00
|
|
|
elseif (value < -1) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.deathGenerator = -1
|
2022-02-21 04:21:04 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.deathGenerator = value
|
2022-02-21 04:21:04 +02:00
|
|
|
end
|
2018-09-24 06:56:45 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.addVictoryGenerator(chunk, value)
|
2023-03-11 22:53:06 +02:00
|
|
|
local cToV = Universe.chunkToVictory
|
2021-12-07 07:56:09 +02:00
|
|
|
local chunkId = chunk.id
|
|
|
|
if not cToV[chunkId] then
|
|
|
|
cToV[chunkId] = {
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk = chunk,
|
2021-12-07 07:56:09 +02:00
|
|
|
v = 0
|
|
|
|
}
|
|
|
|
end
|
|
|
|
cToV[chunkId].v = cToV[chunkId].v + value
|
2020-05-25 01:41:12 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.decayDeathGenerator(chunk)
|
|
|
|
local gen = chunk.deathGenerator
|
2020-05-22 20:45:05 +02:00
|
|
|
if gen then
|
2022-02-22 03:11:57 +02:00
|
|
|
local v = gen * MOVEMENT_GENERATOR_PERSISTANCE
|
2023-01-08 03:59:21 +02:00
|
|
|
if (v < 0.0001) and (v > -0.0001) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.deathGenerator = nil
|
2022-02-22 03:11:57 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.deathGenerator = v
|
2022-02-22 03:11:57 +02:00
|
|
|
end
|
2019-02-16 06:17:30 +02:00
|
|
|
end
|
2018-09-24 06:56:45 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.decayPlayerGenerator(chunk)
|
|
|
|
local gen = chunk.playerGenerator
|
2023-01-19 07:49:15 +02:00
|
|
|
if gen then
|
|
|
|
local v = gen * PLAYER_GENERATOR_PERSISTANCE
|
|
|
|
if (v < 0.0001) and (v > -0.0001) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.playerGenerator = nil
|
2020-05-22 08:57:03 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.playerGenerator = v
|
2020-05-22 08:57:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.addPlayerGenerator(chunk, playerMaxGenerator)
|
|
|
|
local value = chunk.playerGenerator
|
2023-01-19 07:49:15 +02:00
|
|
|
if value then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.playerGenerator = mMin(
|
2023-01-19 07:49:15 +02:00
|
|
|
value + PLAYER_PHEROMONE_GENERATOR_AMOUNT,
|
|
|
|
playerMaxGenerator
|
|
|
|
)
|
2020-05-22 08:57:03 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.playerGenerator = PLAYER_PHEROMONE_GENERATOR_AMOUNT
|
2020-05-22 08:57:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getPlayerGenerator(chunk)
|
|
|
|
return chunk.playerGenerator or 0
|
2020-05-22 08:57:03 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.getPlayerBaseGenerator(chunk)
|
|
|
|
return chunk.playerBaseGenerator or 0
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.addSquadToChunk(chunk, squad)
|
|
|
|
if (chunk ~= -1)
|
|
|
|
and (
|
|
|
|
(squad.chunk == -1)
|
|
|
|
or (squad.chunk.id ~= chunk.id)
|
|
|
|
)
|
|
|
|
then
|
|
|
|
ChunkPropertyUtils.removeSquadFromChunk(squad)
|
|
|
|
local squads = chunk.squads
|
2019-03-09 02:42:20 +02:00
|
|
|
if not squads then
|
|
|
|
squads = {}
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.squads = squads
|
2019-03-09 02:42:20 +02:00
|
|
|
end
|
2020-05-17 07:06:55 +02:00
|
|
|
squads[squad.groupNumber] = squad
|
2019-03-09 02:42:20 +02:00
|
|
|
squad.chunk = chunk
|
2019-12-09 05:31:51 +02:00
|
|
|
end
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.removeSquadFromChunk(squad)
|
2018-02-01 06:01:28 +02:00
|
|
|
local chunk = squad.chunk
|
2021-12-05 20:19:04 +02:00
|
|
|
if (chunk ~= -1) then
|
2023-03-12 03:21:22 +02:00
|
|
|
local squads = chunk.squads
|
2021-12-05 20:19:04 +02:00
|
|
|
if squads then
|
|
|
|
squads[squad.groupNumber] = nil
|
2023-03-11 22:53:06 +02:00
|
|
|
if (tableSize(squads) == 0) then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.squads = nil
|
2021-12-05 20:19:04 +02:00
|
|
|
end
|
2019-10-14 07:49:52 +02:00
|
|
|
end
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.setPlayerBaseGenerator(chunk, playerBaseGenerator)
|
|
|
|
if (playerBaseGenerator <= 0) then
|
|
|
|
chunk.playerBaseGenerator = nil
|
2023-01-08 08:17:46 +02:00
|
|
|
return 0
|
2018-01-15 09:41:55 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.playerBaseGenerator = playerBaseGenerator
|
|
|
|
return playerBaseGenerator
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.addPlayerBaseGenerator(chunk, playerBaseGenerator)
|
|
|
|
local amount = (chunk.playerBaseGenerator or 0) + playerBaseGenerator
|
2023-01-08 08:17:46 +02:00
|
|
|
if amount <= 0 then
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.playerBaseGenerator = nil
|
2023-01-08 08:17:46 +02:00
|
|
|
return 0
|
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
chunk.playerBaseGenerator = amount
|
2023-01-08 08:17:46 +02:00
|
|
|
return amount
|
|
|
|
end
|
2018-01-15 09:41:55 +02:00
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.findNearbyBase(chunk)
|
|
|
|
local foundBase = chunk.base
|
2022-03-20 01:29:12 +02:00
|
|
|
if foundBase then
|
|
|
|
return foundBase
|
|
|
|
end
|
|
|
|
|
2023-03-21 09:02:49 +02:00
|
|
|
return ChunkPropertyUtils.findNearbyBaseByPosition(
|
|
|
|
chunk.map,
|
|
|
|
chunk.x,
|
|
|
|
chunk.y
|
|
|
|
)
|
2022-03-20 01:29:12 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function ChunkPropertyUtils.findNearbyBaseByPosition(map, x, y)
|
2022-04-06 03:37:20 +02:00
|
|
|
local foundBase
|
|
|
|
|
|
|
|
local closest = MAGIC_MAXIMUM_NUMBER
|
|
|
|
for _, base in pairs(map.bases) do
|
2023-03-21 09:02:49 +02:00
|
|
|
local distance = euclideanDistancePoints(base.x, base.y, x, y)
|
2022-04-06 03:37:20 +02:00
|
|
|
if (distance <= base.distanceThreshold) and (distance < closest) then
|
|
|
|
closest = distance
|
|
|
|
foundBase = base
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return foundBase
|
|
|
|
end
|
|
|
|
|
2023-03-12 03:21:22 +02:00
|
|
|
function ChunkPropertyUtils.processNestActiveness(chunk, tick)
|
|
|
|
if getActiveTick(chunk) > tick then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if chunk.nestCount then
|
|
|
|
local surface = chunk.map.surface
|
2023-03-11 22:53:06 +02:00
|
|
|
if Universe.attackUsePlayer and (chunk[PLAYER_PHEROMONE] > Universe.attackPlayerThreshold) then
|
2023-03-12 03:21:22 +02:00
|
|
|
registerActiveNest(chunk, tick)
|
2019-12-16 03:16:56 +02:00
|
|
|
elseif (chunk[BASE_PHEROMONE] > 0) then
|
2023-03-12 03:21:22 +02:00
|
|
|
local getPollution = surface.get_pollution
|
|
|
|
if (getPollution(chunk) > 0) then
|
|
|
|
registerActiveNest(chunk, tick)
|
2019-12-16 03:16:56 +02:00
|
|
|
else
|
|
|
|
local x = chunk.x
|
|
|
|
local y = chunk.y
|
2023-03-11 22:53:06 +02:00
|
|
|
local pollutionThreshold = Universe.pollutionDiffuseMinimum
|
2023-03-12 03:21:22 +02:00
|
|
|
Position.x = x + 32
|
|
|
|
Position.y = y
|
|
|
|
if (getPollution(Position) > pollutionThreshold) then
|
|
|
|
registerActiveNest(chunk, tick)
|
2019-12-16 03:16:56 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
Position.x = x - 32
|
|
|
|
if (getPollution(Position) > pollutionThreshold) then
|
|
|
|
registerActiveNest(chunk, tick)
|
2019-12-16 03:16:56 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
Position.x = x
|
|
|
|
Position.y = y - 32
|
|
|
|
if (getPollution(Position) > pollutionThreshold) then
|
|
|
|
registerActiveNest(chunk, tick)
|
2019-12-16 03:16:56 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
Position.y = y + 32
|
|
|
|
if (getPollution(Position) > pollutionThreshold) then
|
|
|
|
registerActiveNest(chunk, tick)
|
2019-12-16 03:16:56 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
unregisterActiveNest(chunk)
|
2019-12-16 03:16:56 +02:00
|
|
|
if (chunk[BASE_PHEROMONE] > RAIDING_MINIMUM_BASE_THRESHOLD) then
|
2023-03-12 03:21:22 +02:00
|
|
|
registerActiveRaidNest(chunk, tick)
|
2019-12-16 03:16:56 +02:00
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
unregisterActiveRaidNest(chunk)
|
2019-12-16 03:16:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2023-03-12 03:21:22 +02:00
|
|
|
unregisterActiveNest(chunk)
|
|
|
|
unregisterActiveRaidNest(chunk)
|
2019-12-16 03:16:56 +02:00
|
|
|
end
|
2023-03-12 03:21:22 +02:00
|
|
|
else
|
|
|
|
unregisterActiveNest(chunk)
|
|
|
|
unregisterActiveRaidNest(chunk)
|
2019-12-16 03:16:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 23:21:04 +02:00
|
|
|
function ChunkPropertyUtils.init(universe, mapUtils)
|
2023-03-11 22:53:06 +02:00
|
|
|
Universe = universe
|
2023-03-12 23:21:04 +02:00
|
|
|
activateMap = mapUtils.activateMap
|
2023-03-26 23:05:49 +02:00
|
|
|
if universe.chunkPropertyUtilsQueries then
|
|
|
|
Position = universe.chunkPropertyUtilsQueries.position
|
|
|
|
end
|
2023-03-11 22:53:06 +02:00
|
|
|
end
|
2019-12-16 03:16:56 +02:00
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
ChunkPropertyUtilsG = ChunkPropertyUtils
|
|
|
|
return ChunkPropertyUtils
|