Skip to content

Commit

Permalink
assister cache in place, pods (almost) respect new build orders
Browse files Browse the repository at this point in the history
  • Loading branch information
clyfordv committed May 16, 2024
1 parent de0a3bc commit 6ff6534
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
6 changes: 4 additions & 2 deletions lua/sim/commands/abort-navigation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
58 changes: 45 additions & 13 deletions lua/sim/units/MobileUnit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -158,48 +162,48 @@ 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,

--- 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
Expand All @@ -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)
Expand Down Expand Up @@ -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,
}

0 comments on commit 6ff6534

Please sign in to comment.