1
0
mirror of https://github.com/veden/Rampant.git synced 2024-12-26 20:54:12 +02:00

FACTO-286: Cleanup compression code, added cooldown, check group state

This commit is contained in:
Aaron Veden 2023-04-05 17:26:14 -07:00
parent 5cb00d5bce
commit 3f7d47814c
No known key found for this signature in database
GPG Key ID: FF5990B1C6DD3F84
4 changed files with 66 additions and 26 deletions

View File

@ -131,7 +131,6 @@ local getDrainPylonPair = ChunkPropertyUtils.getDrainPylonPair
local createDrainPylon = UnitUtils.createDrainPylon local createDrainPylon = UnitUtils.createDrainPylon
local compressSquad = Squad.compressSquad
local decompressSquad = Squad.decompressSquad local decompressSquad = Squad.decompressSquad
local isDrained = ChunkPropertyUtils.isDrained local isDrained = ChunkPropertyUtils.isDrained
@ -459,7 +458,7 @@ local function onDeath(event)
if group then if group then
local squad = Universe.groupNumberToSquad[group.group_number] local squad = Universe.groupNumberToSquad[group.group_number]
if squad then if squad then
decompressSquad(squad) decompressSquad(squad, tick)
if damageTypeName then if damageTypeName then
base = squad.base base = squad.base
end end

View File

@ -209,6 +209,8 @@ constants.SQUAD_BUILDING = 6
-- Squad Related -- Squad Related
constants.COMPRESSION_COOLDOWN = 600
constants.MINIMUM_EXPANSION_DISTANCE = 10 constants.MINIMUM_EXPANSION_DISTANCE = 10
constants.RETREAT_GRAB_RADIUS = 24 constants.RETREAT_GRAB_RADIUS = 24

View File

@ -49,6 +49,8 @@ local ENEMY_PHEROMONE = Constants.ENEMY_PHEROMONE
local KAMIKAZE_PHEROMONE = Constants.KAMIKAZE_PHEROMONE local KAMIKAZE_PHEROMONE = Constants.KAMIKAZE_PHEROMONE
local RESOURCE_PHEROMONE = Constants.RESOURCE_PHEROMONE local RESOURCE_PHEROMONE = Constants.RESOURCE_PHEROMONE
local COMPRESSION_COOLDOWN = Constants.COMPRESSION_COOLDOWN
local HALF_CHUNK_SIZE = Constants.HALF_CHUNK_SIZE local HALF_CHUNK_SIZE = Constants.HALF_CHUNK_SIZE
local FIVE_DEATH_PHEROMONE_GENERATOR_AMOUNT = Constants.FIVE_DEATH_PHEROMONE_GENERATOR_AMOUNT local FIVE_DEATH_PHEROMONE_GENERATOR_AMOUNT = Constants.FIVE_DEATH_PHEROMONE_GENERATOR_AMOUNT
@ -80,6 +82,10 @@ local AI_VENGENCE_SQUAD_COST = Constants.AI_VENGENCE_SQUAD_COST
local AI_VENGENCE_SETTLER_COST = Constants.AI_VENGENCE_SETTLER_COST local AI_VENGENCE_SETTLER_COST = Constants.AI_VENGENCE_SETTLER_COST
local CHUNK_ALL_DIRECTIONS = Constants.CHUNK_ALL_DIRECTIONS local CHUNK_ALL_DIRECTIONS = Constants.CHUNK_ALL_DIRECTIONS
local ATTACKING_DISTRACTION = defines.group_state.attacking_distraction
local ATTACKING_TARGET = defines.group_state.attacking_target
local GATHERING = defines.group_state.gathering
-- imported functions -- imported functions
local setPositionInQuery = Utils.setPositionInQuery local setPositionInQuery = Utils.setPositionInQuery
@ -297,20 +303,29 @@ local function addMovementPenalty(squad, chunk)
c = chunk }) c = chunk })
end end
function Squad.compressSquad(squad) local function compressSquad(squad, tick)
if not squad.canBeCompressed then if squad.compressionSet
or (Universe.squadCompressionThreshold == -1)
or (squad.canBeCompressed >= tick)
then
return return
end end
local compressionSet = {}
local group = squad.group local group = squad.group
local members = group.members local groupState = group.state
if (Universe.squadCompressionThreshold == -1) if (groupState == ATTACKING_DISTRACTION)
or (#members <= Universe.squadCompressionThreshold) or (groupState == ATTACKING_TARGET)
or (groupState == GATHERING)
or (squad.status == SQUAD_BUILDING)
then then
squad.canBeCompressed = false
return return
end end
local members = group.members
if (#members <= Universe.squadCompressionThreshold) then
return
end
local cmd = group.command
local compressionSet = {}
local compressedTotal = 0 local compressedTotal = 0
local totalTypes = 0 local totalTypes = 0
local entityToTag local entityToTag
@ -339,10 +354,14 @@ function Squad.compressSquad(squad)
query.target = entityToTag query.target = entityToTag
squad.compressionText = rendering.draw_text(query) squad.compressionText = rendering.draw_text(query)
squad.compressionSet = compressionSet squad.compressionSet = compressionSet
squad.compressedTotal = compressedTotal
squad.canBeCompressed = false squad.canBeCompressed = false
if cmd then
group.set_command(cmd)
end
end end
function Squad.decompressSquad(squad) function Squad.decompressSquad(squad, tick)
if not squad.compressionSet then if not squad.compressionSet then
return return
end end
@ -350,6 +369,7 @@ function Squad.decompressSquad(squad)
local group = squad.group local group = squad.group
local query = Queries.createEntityQuery local query = Queries.createEntityQuery
local add_member = group.add_member local add_member = group.add_member
local cmd = group.command
local create_entity = squad.map.surface.create_entity local create_entity = squad.map.surface.create_entity
setPositionInQuery( setPositionInQuery(
query, query,
@ -362,7 +382,11 @@ function Squad.decompressSquad(squad)
end end
end end
rendering.destroy(squad.compressionText) rendering.destroy(squad.compressionText)
if cmd then
group.set_command(cmd)
end
squad.compressionSet = nil squad.compressionSet = nil
squad.canBeCompressed = tick + COMPRESSION_COOLDOWN
end end
--[[ --[[
@ -438,7 +462,7 @@ local function scoreNeighbors(map, chunk, neighborDirectionChunks, scoreFunction
return SearchPath return SearchPath
end end
local function settleMove(squad) local function settleMove(squad, tick)
local group = squad.group local group = squad.group
local map = squad.map local map = squad.map
@ -480,7 +504,7 @@ local function settleMove(squad)
) )
) )
then then
Squad.decompressSquad(squad) Squad.decompressSquad(squad, tick)
position = findMovementPosition(surface, groupPosition) or groupPosition position = findMovementPosition(surface, groupPosition) or groupPosition
@ -557,6 +581,7 @@ local function settleMove(squad)
cmd = Queries.settleCommand cmd = Queries.settleCommand
squad.status = SQUAD_BUILDING squad.status = SQUAD_BUILDING
squad.commandTick = tick + BUILD_COMMAND_TIMEOUT squad.commandTick = tick + BUILD_COMMAND_TIMEOUT
Squad.decompressSquad(squad, tick)
if squad.kamikaze then if squad.kamikaze then
cmd.distraction = DEFINES_DISTRACTION_NONE cmd.distraction = DEFINES_DISTRACTION_NONE
else else
@ -576,7 +601,7 @@ local function settleMove(squad)
group.set_command(cmd) group.set_command(cmd)
end end
local function attackMove(squad) local function attackMove(squad, tick)
local group = squad.group local group = squad.group
local groupPosition = group.position local groupPosition = group.position
@ -643,7 +668,7 @@ local function attackMove(squad)
end end
if attack then if attack then
Squad.decompressSquad(squad) Squad.decompressSquad(squad, tick)
cmd = Queries.attackCommand cmd = Queries.attackCommand
if not squad.rabid then if not squad.rabid then
@ -674,7 +699,7 @@ local function buildMove(squad, tick)
setPositionInCommand(Queries.settleCommand, position) setPositionInCommand(Queries.settleCommand, position)
Squad.decompressSquad(squad) Squad.decompressSquad(squad, tick)
group.set_command(Queries.settleCommand) group.set_command(Queries.settleCommand)
end end
@ -750,7 +775,7 @@ function Squad.squadDispatch(squad, tick)
return return
end end
compressSquad(squad) compressSquad(squad, tick)
local status = squad.status local status = squad.status
if (status == SQUAD_RAIDING) then if (status == SQUAD_RAIDING) then
squad.commandTick = tick + COMMAND_TIMEOUT squad.commandTick = tick + COMMAND_TIMEOUT
@ -954,11 +979,16 @@ function Squad.retreatUnits(chunk, cause, map, tick, radius)
end end
function Squad.createSquad(position, map, group, settlers, base) function Squad.createSquad(position, map, group, settlers, base)
local unitGroup = group or map.surface.create_unit_group({position=position}) local unitGroup = group
if not unitGroup then
local query = Queries.createUnitGroup
setPositionInQuery(query, position)
unitGroup = map.surface.create_unit_group(query)
end
local squad = { local squad = {
group = unitGroup, group = unitGroup,
canBeCompressed = true, canBeCompressed = 0,
compressionSet = nil, compressionSet = nil,
compressionText = nil, compressionText = nil,
status = SQUAD_GUARDING, status = SQUAD_GUARDING,

View File

@ -391,12 +391,15 @@ local function addCommandSet()
{chunk=-1, direction=-1} {chunk=-1, direction=-1}
} }
Universe.squadQueries.renderText = { Universe.squadQueries.renderText = {
target = nil, target = nil,
text = "", text = "",
target_offset = {0, -10}, target_offset = {0, -10},
color = {r=1,g=1,b=1,a=0.5}, color = {r=1,g=1,b=1,a=0.5},
surface = nil, surface = nil,
scale = 5 scale = 5
}
Universe.squadQueries.createUnitGroup = {
position = {0,0}
} }
end end
@ -518,8 +521,6 @@ function Upgrade.addUniverseProperties()
Universe.maxPoints = 0 Universe.maxPoints = 0
Universe.maxOverflowPoints = 0 Universe.maxOverflowPoints = 0
addCommandSet()
Universe.bases = {} Universe.bases = {}
Universe.processBaseAIIterator = nil Universe.processBaseAIIterator = nil
@ -530,6 +531,10 @@ function Upgrade.addUniverseProperties()
Universe.pendingUpgradesLength = 0 Universe.pendingUpgradesLength = 0
Universe.settlePurpleCloud = {} Universe.settlePurpleCloud = {}
end end
if global.universePropertyVersion < 2 then
global.universePropertyVersion = 2
addCommandSet()
end
end end
function Upgrade.attempt() function Upgrade.attempt()
@ -561,6 +566,10 @@ function Upgrade.attempt()
end end
if global.gameVersion < 2 then if global.gameVersion < 2 then
global.gameVersion = 2 global.gameVersion = 2
game.map_settings.max_failed_behavior_count = 999
for _, squad in pairs(Universe.groupNumberToSquad) do
squad.canBeCompressed = 0
end
for _, map in pairs(Universe.maps) do for _, map in pairs(Universe.maps) do
for _, chunk in pairs(map.processQueue) do for _, chunk in pairs(map.processQueue) do