From 6ff6534bd4da3c09e4ca52ab2132eedfeeb21a90 Mon Sep 17 00:00:00 2001 From: clyf Date: Thu, 16 May 2024 15:07:41 -0400 Subject: [PATCH] assister cache in place, pods (almost) respect new build orders --- lua/sim/commands/abort-navigation.lua | 6 ++- lua/sim/units/MobileUnit.lua | 58 +++++++++++++++++++++------ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/lua/sim/commands/abort-navigation.lua b/lua/sim/commands/abort-navigation.lua index 46f0de5b67..f195f113ff 100644 --- a/lua/sim/commands/abort-navigation.lua +++ b/lua/sim/commands/abort-navigation.lua @@ -43,7 +43,8 @@ function AbortNavigation(units, doPrint) if not IsDestroyed(unit) then local navigator = unit:GetNavigator() navigator:AbortMove() - if not unit:GetGuardedUnit() and unit.assistFocusCache then + if not unit:GetGuardedUnit() and unit.assistedCommandCache then + LOG(reprs(unit.assistedCommandCache)) local engineerGuards = EntityCategoryFilterDown(categories.ENGINEER, unit:GetGuards()) local cmd if engineerGuards[1]:IsUnitState('Reclaiming') then @@ -55,8 +56,9 @@ function AbortNavigation(units, doPrint) end if cmd then IssueClearCommands(engineerGuards) - cmd(engineerGuards, GetEntityById(unit.assistFocusCache)) + cmd(engineerGuards, unit.assistedCommandCache.target) IssueGuard(engineerGuards, unit) + unit.assisterCache = nil end end end diff --git a/lua/sim/units/MobileUnit.lua b/lua/sim/units/MobileUnit.lua index 681a5ef030..5483e7498f 100644 --- a/lua/sim/units/MobileUnit.lua +++ b/lua/sim/units/MobileUnit.lua @@ -42,13 +42,17 @@ local CreateUEFUnitBeingBuiltEffects = import("/lua/effectutilities.lua").Create -- upvalue scope for performance local TrashBag = TrashBag +-- for override +local oldGetGuards = Unit.GetGuards + ---@class MobileUnit : Unit, TreadComponent ---@field MovementEffectsBag TrashBag ---@field TopSpeedEffectsBag TrashBag ---@field BeamExhaustEffectsBag TrashBag ---@field TransportBeamEffectsBag? TrashBag ---@field OnBeingBuiltEffectsBag? TrashBag ----@field assistFocusCache string +---@field assistedCommandCache CommandQueueElement +---@field assisterCache Unit[] MobileUnit = ClassUnit(Unit, TreadComponent) { ---@param self MobileUnit @@ -158,38 +162,39 @@ MobileUnit = ClassUnit(Unit, TreadComponent) { ---@param order string OnStartBuild = function(self, unitBeingBuilt, order) Unit.OnStartBuild(self, unitBeingBuilt, order) - self:RefreshAssistersFocus('Building', unitBeingBuilt) + self:RefreshAssistersFocus(unitBeingBuilt) end, OnStopBuild = function(self, built, order) Unit.OnStopBuild(self, built, order) -- Check if we left an unfinished building - if self.assistFocusCache then + if self.assistedCommandCache then if built:GetFractionComplete() < 1.0 then LOG('We left an unfinished building behind!') - self:RefreshAssistersFocus('Building', nil) + self:RefreshAssistersFocus() else LOG('Focus cache cleared!') - self.assistFocusCache = nil + self.assistedCommandCache = nil end end end, OnStartReclaim = function(self, target) Unit.OnStartReclaim(self, target) - self:RefreshAssistersFocus('Building', target) + self:RefreshAssistersFocus(target) end, OnStopReclaim = function(self, target) Unit.OnStopReclaim(self, target) -- Check if we left an unreclaimed prop behind - if self.assistFocusCache then + -- We'll only have a cached assisted command after starting a build/reclaim order with assisters + if self.assistedCommandCache then if not table.empty(target) then LOG('We left an unreclaimed prop behind') - self:RefreshAssistersFocus('Reclaiming', nil) + self:RefreshAssistersFocus() else LOG('Focus cache cleared!') - self.assistFocusCache = nil + self.assistedCommandCache = nil end end end, @@ -197,9 +202,8 @@ MobileUnit = ClassUnit(Unit, TreadComponent) { --- This function is called when a unit with assisting engineers finishes. It forces the assisting engineers to assist with the new task. --- (much more efficient to do this in a group as well, instead of called for each assister) ---@param self Unit - ---@param stateKey string ---@param target? Prop|Unit - RefreshAssistersFocus = function(self, stateKey, target) + RefreshAssistersFocus = function(self, target) if self.Dead then return @@ -214,8 +218,12 @@ MobileUnit = ClassUnit(Unit, TreadComponent) { -- We'll use this to track our focus in cause we want to give a sticky order later if target then - LOG('Focus cache set!') - self.assistFocusCache = target:GetEntityId() + LOG('Assisted command cache set!') + self.assistedCommandCache = self:GetCommandQueue()[1] + if not self.assistedCommandCache.target then + self.assistedCommandCache.target = target + end + LOG(reprs(self.assistedCommandCache)) end -- Pods without orders have a silent 'AssistCommander' order (commandType == 29) @@ -269,7 +277,31 @@ MobileUnit = ClassUnit(Unit, TreadComponent) { IssueClearCommands(engineerGuards) -- Clear whatever commands exist to refocus guards if cmd then -- If we have an explicit command, issue it cmd(engineerGuards, focus) + self.assisterCache = engineerGuards + self.assisterCache.focus = focus end IssueGuard(engineerGuards, self) -- Replace the guard command at the end of the queue end, + + ---Override get guards to deal with our special pod stuff + GetGuards = function(self) + local guards = oldGetGuards(self) + local count = 0 + if self.assisterCache then + local firstCommand, secondCommand + local focus = self.assisterCache.focus + for _, assister in ipairs(self.assisterCache) do + firstCommand, secondCommand = unpack(assister:GetCommandQueue()) + if firstCommand.target == focus + and secondCommand.target == self then + table.insert(guards, assister) + count = count + 1 + end + end + end + if count > 0 then + print(string.format('Found %d cached guards', count)) + end + return guards + end, }