1
0
mirror of https://github.com/veden/Rampant.git synced 2025-02-01 13:07:53 +02:00
Rampant/libs/ChunkPropertyUtils.lua

385 lines
12 KiB
Lua
Raw Normal View History

2019-02-15 20:17:30 -08:00
if chunkPropertyUtilsG then
return chunkPropertyUtilsG
end
local chunkPropertyUtils = {}
2018-09-23 21:56:45 -07:00
local constants = require("Constants")
2019-12-15 17:16:56 -08:00
-- constants
2019-03-07 19:40:55 -08:00
2019-12-15 17:16:56 -08:00
local RAIDING_MINIMUM_BASE_THRESHOLD = constants.RAIDING_MINIMUM_BASE_THRESHOLD
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
local BASE_PHEROMONE = constants.BASE_PHEROMONE
2018-09-25 22:14:13 -07:00
local MOVEMENT_GENERATOR_PERSISTANCE = constants.MOVEMENT_GENERATOR_PERSISTANCE
2018-09-23 21:56:45 -07:00
local CHUNK_ALL_DIRECTIONS = constants.CHUNK_ALL_DIRECTIONS
2019-12-15 17:16:56 -08:00
-- imported functions
local mMin = math.min
-- module code
function chunkPropertyUtils.getNestCount(map, chunk)
return map.chunkToNests[chunk] or 0
end
2019-11-29 16:49:22 -08:00
function chunkPropertyUtils.getTurretCount(map, chunk)
return map.chunkToTurrets[chunk] or 0
end
2019-11-29 16:49:22 -08:00
function chunkPropertyUtils.getTrapCount(map, chunk)
return map.chunkToTraps[chunk] or 0
end
function chunkPropertyUtils.getUtilityCount(map, chunk)
return map.chunkToUtilities[chunk] or 0
end
function chunkPropertyUtils.getHiveCount(map, chunk)
return map.chunkToHives[chunk] or 0
end
function chunkPropertyUtils.setTurretCount(map, chunk, count)
2019-05-15 22:11:43 -07:00
if (count <= 0) then
2019-11-29 16:49:22 -08:00
map.chunkToTurrets[chunk] = nil
else
2019-11-29 16:49:22 -08:00
map.chunkToTurrets[chunk] = count
end
end
function chunkPropertyUtils.setHiveCount(map, chunk, count)
if (count <= 0) then
map.chunkToHives[chunk] = nil
else
map.chunkToHives[chunk] = count
end
end
function chunkPropertyUtils.setTrapCount(map, chunk, count)
if (count <= 0) then
map.chunkToTraps[chunk] = nil
else
map.chunkToTraps[chunk] = count
end
end
function chunkPropertyUtils.setUtilityCount(map, chunk, count)
if (count <= 0) then
map.chunkToUtilities[chunk] = nil
else
map.chunkToUtilities[chunk] = count
end
end
function chunkPropertyUtils.setNestCount(map, chunk, count)
2019-05-15 22:11:43 -07:00
if (count <= 0) then
map.chunkToNests[chunk] = nil
2020-05-19 19:37:16 -07:00
if (map.processMigrationIterator == chunk) then
map.processMigrationIterator = nil
end
if (map.processNestIterator == chunk) then
map.processNestIterator = nil
end
else
map.chunkToNests[chunk] = count
end
end
function chunkPropertyUtils.getNestCount(map, chunk)
return map.chunkToNests[chunk] or 0
end
function chunkPropertyUtils.getChunkBase(map, chunk)
return map.chunkToBase[chunk]
end
function chunkPropertyUtils.setChunkBase(map, chunk, base)
map.chunkToBase[chunk] = base
end
function chunkPropertyUtils.getEnemyStructureCount(map, chunk)
2019-11-29 16:49:22 -08:00
return (map.chunkToNests[chunk] or 0) + (map.chunkToTurrets[chunk] or 0) + (map.chunkToTraps[chunk] or 0) +
(map.chunkToUtilities[chunk] or 0) + (map.chunkToHives[chunk] or 0)
end
function chunkPropertyUtils.getRetreatTick(map, chunk)
return map.chunkToRetreats[chunk] or 0
end
function chunkPropertyUtils.getRallyTick(map, chunk)
return map.chunkToRallys[chunk] or 0
end
function chunkPropertyUtils.setRallyTick(map, chunk, tick)
2019-02-15 20:17:30 -08:00
map.chunkToRallys[chunk] = tick
end
function chunkPropertyUtils.setRetreatTick(map, chunk, tick)
map.chunkToRetreats[chunk] = tick
end
function chunkPropertyUtils.setResourceGenerator(map, chunk, resourceGenerator)
2019-05-15 22:11:43 -07:00
if (resourceGenerator <= 0) then
map.chunkToResource[chunk] = nil
else
map.chunkToResource[chunk] = resourceGenerator
end
end
function chunkPropertyUtils.getResourceGenerator(map, chunk)
return map.chunkToResource[chunk] or 0
end
function chunkPropertyUtils.addResourceGenerator(map, chunk, delta)
map.chunkToResource[chunk] = (map.chunkToResource[chunk] or 0) + delta
end
2018-09-23 21:56:45 -07:00
function chunkPropertyUtils.getDeathGenerator(map, chunk)
return map.chunkToDeathGenerator[chunk] or 0
end
function chunkPropertyUtils.getPassable(map, chunk)
return map.chunkToPassable[chunk] or CHUNK_ALL_DIRECTIONS
end
2019-02-19 22:16:43 -08:00
function chunkPropertyUtils.getRaidNestActiveness(map, chunk)
return map.chunkToActiveRaidNest[chunk] or 0
end
function chunkPropertyUtils.setRaidNestActiveness(map, chunk, value)
if (value <= 0) then
if (map.chunkToActiveRaidNest[chunk] ~= nil) then
2021-02-19 23:31:36 -08:00
map.activeRaidNests = map.activeRaidNests - 1
end
2020-05-19 19:37:16 -07:00
if (map.processActiveRaidSpawnerIterator == chunk) then
map.processActiveRaidSpawnerIterator = nil
end
2019-02-19 22:16:43 -08:00
map.chunkToActiveRaidNest[chunk] = nil
else
if (map.chunkToActiveRaidNest[chunk] == nil) then
2021-02-19 23:31:36 -08:00
map.activeRaidNests = map.activeRaidNests + 1
end
2019-02-19 22:16:43 -08:00
map.chunkToActiveRaidNest[chunk] = value
end
end
2020-04-27 20:41:18 -07:00
function chunkPropertyUtils.getNestActiveTick(map, chunk)
return map.tickActiveNest[chunk] or 0
end
function chunkPropertyUtils.setNestActiveTick(map, chunk, tick)
if (tick == 0) then
map.tickActiveNest[chunk] = nil
else
map.tickActiveNest[chunk] = tick
end
end
2019-02-19 22:16:43 -08:00
function chunkPropertyUtils.getNestActiveness(map, chunk)
return map.chunkToActiveNest[chunk] or 0
end
function chunkPropertyUtils.setNestActiveness(map, chunk, value)
if (value <= 0) then
if (map.chunkToActiveNest[chunk] ~= nil) then
2021-02-19 23:31:36 -08:00
map.activeNests = map.activeNests - 1
end
2020-05-19 19:37:16 -07:00
if (map.processActiveSpawnerIterator == chunk) then
map.processActiveSpawnerIterator = nil
end
2019-02-19 22:16:43 -08:00
map.chunkToActiveNest[chunk] = nil
else
if (map.chunkToActiveNest[chunk] == nil) then
2021-02-19 23:31:36 -08:00
map.activeNests = map.activeNests + 1
end
2019-02-19 22:16:43 -08:00
map.chunkToActiveNest[chunk] = value
end
end
2018-09-23 21:56:45 -07:00
function chunkPropertyUtils.setPassable(map, chunk, value)
if (value == CHUNK_ALL_DIRECTIONS) then
map.chunkToPassable[chunk] = nil
2018-09-23 21:56:45 -07:00
else
map.chunkToPassable[chunk] = value
2018-09-23 21:56:45 -07:00
end
end
function chunkPropertyUtils.getPathRating(map, chunk)
return map.chunkToPathRating[chunk] or 1
end
function chunkPropertyUtils.setPathRating(map, chunk, value)
if (value == 1) then
map.chunkToPathRating[chunk] = nil
2018-09-23 21:56:45 -07:00
else
map.chunkToPathRating[chunk] = value
2018-09-23 21:56:45 -07:00
end
end
2018-09-25 22:14:13 -07:00
function chunkPropertyUtils.addDeathGenerator(map, chunk, value)
map.chunkToDeathGenerator[chunk] = (map.chunkToDeathGenerator[chunk] or 0) + value
2018-09-23 21:56:45 -07:00
end
2020-05-24 16:41:12 -07:00
function chunkPropertyUtils.addVictoryGenerator(map, chunk, value)
map.chunkToVictory[chunk] = (map.chunkToVictory[chunk] or 0) + value
end
2018-09-23 21:56:45 -07:00
function chunkPropertyUtils.decayDeathGenerator(map, chunk)
local gen = map.chunkToDeathGenerator[chunk]
if gen then
gen = gen * MOVEMENT_GENERATOR_PERSISTANCE
2018-09-25 22:14:13 -07:00
if (gen >= -2) and (gen <= 2) then
map.chunkToDeathGenerator[chunk] = nil
else
map.chunkToDeathGenerator[chunk] = gen
end
2019-02-15 20:17:30 -08:00
end
2018-09-23 21:56:45 -07:00
end
function chunkPropertyUtils.addPlayerToChunk(map, chunk, name)
local playerChunks = map.playerToChunk
local playerCountChunks = map.chunkToPlayerCount
local playerChunk = playerChunks[name]
if not playerChunk then
playerChunks[name] = chunk
local playerCount = playerCountChunks[chunk]
if not playerCount then
playerCountChunks[chunk] = 1
else
playerCountChunks[chunk] = playerCount + 1
end
elseif (playerChunk ~= chunk) then
playerChunks[name] = chunk
local playerCount = playerCountChunks[playerChunk]
chunkPropertyUtils.setPlayersOnChunk(map, playerChunk, playerCount - 1)
playerCount = playerCountChunks[chunk]
if not playerCount then
playerCountChunks[chunk] = 1
else
playerCountChunks[chunk] = playerCount + 1
end
end
end
function chunkPropertyUtils.setPlayersOnChunk(map, chunk, value)
if (value <= 0) then
map.chunkToPlayerCount[chunk] = nil
else
map.chunkToPlayerCount[chunk] = value
end
end
function chunkPropertyUtils.getPlayersOnChunk(map, chunk)
return map.chunkToPlayerCount[chunk] or 0
end
function chunkPropertyUtils.getPlayerBaseGenerator(map, chunk)
return map.chunkToPlayerBase[chunk] or 0
end
function chunkPropertyUtils.addSquadToChunk(map, chunk, squad)
local chunkToSquad = map.chunkToSquad
2020-05-15 13:51:38 -07:00
if (chunk ~= -1) and (squad.chunk ~= chunk) then
chunkPropertyUtils.removeSquadFromChunk(map, squad)
2019-03-08 16:42:20 -08:00
local squads = chunkToSquad[chunk]
if not squads then
squads = {}
chunkToSquad[chunk] = squads
end
2020-05-16 22:06:55 -07:00
squads[squad.groupNumber] = squad
2019-03-08 16:42:20 -08:00
squad.chunk = chunk
end
end
function chunkPropertyUtils.removeSquadFromChunk(map, squad)
local chunkToSquad = map.chunkToSquad
local chunk = squad.chunk
2020-05-16 22:06:55 -07:00
local squads = chunkToSquad[chunk]
if squads then
squads[squad.groupNumber] = nil
if (table_size(squads) == 0) then
chunkToSquad[chunk] = nil
end
end
end
function chunkPropertyUtils.getSquadsOnChunk(map, chunk)
2019-10-20 13:45:43 -07:00
return map.chunkToSquad[chunk] or map.emptySquadsOnChunk
end
function chunkPropertyUtils.setPlayerBaseGenerator(map, chunk, playerGenerator)
2019-05-15 22:11:43 -07:00
if (playerGenerator <= 0) then
map.chunkToPlayerBase[chunk] = nil
else
map.chunkToPlayerBase[chunk] = playerGenerator
end
end
function chunkPropertyUtils.addPlayerBaseGenerator(map, chunk, playerGenerator)
map.chunkToPlayerBase[chunk] = (map.chunkToPlayerBase[chunk] or 0) + playerGenerator
end
2021-02-19 21:41:30 -08:00
function chunkPropertyUtils.processNestActiveness(map, chunk)
2019-12-15 17:16:56 -08:00
local nests = chunkPropertyUtils.getNestCount(map, chunk)
if (nests > 0) then
2021-02-19 23:31:36 -08:00
local surface = map.surface
2019-12-15 17:16:56 -08:00
local activeness = chunkPropertyUtils.getNestActiveness(map, chunk)
2021-02-19 23:31:36 -08:00
local universe = map.universe
2019-12-15 17:16:56 -08:00
local raidActiveness = chunkPropertyUtils.getRaidNestActiveness(map, chunk)
2021-02-19 23:31:36 -08:00
if universe.attackUsePlayer and (chunk[PLAYER_PHEROMONE] > universe.attackPlayerThreshold) then
2019-12-15 17:16:56 -08:00
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20))
elseif (chunk[BASE_PHEROMONE] > 0) then
2021-02-19 23:31:36 -08:00
local position = universe.position
2019-12-15 17:16:56 -08:00
if (surface.get_pollution(chunk) > 0) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20))
else
local x = chunk.x
local y = chunk.y
position.x = x + 32
position.y = y
if (surface.get_pollution(position) > 0) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20))
else
position.x = x - 32
if (surface.get_pollution(position) > 0) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20))
else
position.x = x
position.y = y - 32
if (surface.get_pollution(position) > 0) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20))
else
position.y = y + 32
if (surface.get_pollution(position) > 0) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20))
else
chunkPropertyUtils.setNestActiveness(map, chunk, activeness - 2)
if (chunk[BASE_PHEROMONE] > RAIDING_MINIMUM_BASE_THRESHOLD) then
chunkPropertyUtils.setRaidNestActiveness(map, chunk, mMin(raidActiveness + 3, 20))
else
chunkPropertyUtils.setRaidNestActiveness(map, chunk, raidActiveness - 1)
end
end
end
end
end
end
else
chunkPropertyUtils.setNestActiveness(map, chunk, activeness - 5)
chunkPropertyUtils.setRaidNestActiveness(map, chunk, raidActiveness - 5)
end
else
chunkPropertyUtils.setNestActiveness(map, chunk, 0)
chunkPropertyUtils.setRaidNestActiveness(map, chunk, 0)
end
end
2019-02-15 20:17:30 -08:00
chunkPropertyUtilsG = chunkPropertyUtils
return chunkPropertyUtils