Skip to content
Open
1 change: 1 addition & 0 deletions changelog/snippets/fix.7162.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- (#7162) Fix transfer of units with manually built enhancements.
8 changes: 8 additions & 0 deletions engine/Sim/CAiBrain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,12 @@ end
function CAiBrain:TakeResource(type, amount)
end

--- Called by the engine when a unit fails unit transfer due to issues other than unit cap.
---@type fun(self: moho.aibrain_methods)
CAiBrain.OnFailedUnitTransfer = nil

--- Called by the engine when a unit fails to create due to unit cap.
---@type fun(self: moho.aibrain_methods)
CAiBrain.OnUnitCapLimitReached = nil
Comment thread
coderabbitai[bot] marked this conversation as resolved.

return CAiBrain
26 changes: 19 additions & 7 deletions lua/SimUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
-- upvalues for performance
local ArmyBrains = ArmyBrains
local GetCurrentCommandSource = GetCurrentCommandSource
local TableEmpty = table.empty
local KillThread = KillThread

------------------------------------------------------------------------------------------------------------------------
--#region General Unit Transfer Scripts
Expand Down Expand Up @@ -326,11 +328,8 @@ function TransferUnitsOwnership(units, toArmy, captured, noRestrictions)
local unitEnh = SimUnitEnhancements[unit.EntityId]
if unitEnh then
activeEnhancements = {}
for i, enh in unitEnh do
activeEnhancements[i] = enh
end
if not activeEnhancements[1] then
activeEnhancements = nil
for slot, enh in unitEnh do
activeEnhancements[slot] = enh
end
end
end
Expand Down Expand Up @@ -420,8 +419,20 @@ function TransferUnitsOwnership(units, toArmy, captured, noRestrictions)
end

if activeEnhancements then
for _, enh in activeEnhancements do
newUnit:CreateEnhancement(enh)
local thread = newUnit.OnStopBeingBuiltEnhancementsThread
if thread then KillThread(thread) end
if TableEmpty(activeEnhancements) then
newUnit.OnStopBeingBuiltEnhancementsThread = nil
else
newUnit.OnStopBeingBuiltEnhancementsThread = newUnit:ForkThread(function()
WaitTicks(1)
if newUnit and not newUnit.Dead and not IsDestroyed(newUnit) then
for _, enh in activeEnhancements do
newUnit:CreateEnhancement(enh)
end
end
newUnit.OnStopBeingBuiltEnhancementsThread = nil
end)
end
end

Expand Down Expand Up @@ -738,6 +749,7 @@ function GiveUnitsToPlayer(data, units)
if manualShare == 'none' or table.empty(units) then
return
end
---@cast units -nil
local toArmy = data.To
local owner = units[1].Army
if OkayToMessWithArmy(owner) and IsAlly(owner, toArmy) then
Expand Down
3 changes: 3 additions & 0 deletions lua/defaultcomponents.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local Buff = import("/lua/sim/buff.lua")
local Entity = import("/lua/sim/entity.lua").Entity

local IsDestroyed = IsDestroyed

---@class ShieldEffectsComponent : Unit
---@field Trash TrashBag
---@field ShieldEffectsBag TrashBag
Expand Down Expand Up @@ -251,6 +253,7 @@ IntelComponent = ClassSimple {

--- display progress
for k = 1, ticks do
if self.Dead or IsDestroyed(self) then return end

-- prevent changing work progress when we are doing work (such as an enhancement)
if not self.WorkItem then
Expand Down
10 changes: 5 additions & 5 deletions lua/sim/Unit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ local cUnitGetBuildRate = cUnit.GetBuildRate
---@field ImmuneToStun? boolean
---@field Anims? Animator[] # Animators that get stopped when a unit is stunned. Not used in FAF.
---@field IsBeingTransferred? boolean
---@field OnStopBeingBuiltEnhancementsThread thread?
Unit = ClassUnit(moho.unit_methods, IntelComponent, VeterancyComponent, DebugUnitComponent, FastDecayComponent) {

IsUnit = true,
Expand Down Expand Up @@ -2490,7 +2491,7 @@ Unit = ClassUnit(moho.unit_methods, IntelComponent, VeterancyComponent, DebugUni
end

if bp.EnhancementPresetAssigned then
self:ForkThread(self.CreatePresetEnhancementsThread)
self.OnStopBeingBuiltEnhancementsThread = self:ForkThread(self.CreatePresetEnhancementsThread)
end

-- Don't try sending a Notify message from here if we're an ACU
Expand Down Expand Up @@ -2614,10 +2615,7 @@ Unit = ClassUnit(moho.unit_methods, IntelComponent, VeterancyComponent, DebugUni
local bp = self.Blueprint
if bp.Enhancements and bp.EnhancementPresetAssigned and bp.EnhancementPresetAssigned.Enhancements then
for k, v in bp.EnhancementPresetAssigned.Enhancements do
-- Enhancements may already have been created by SimUtils.TransferUnitsOwnership
if not self:HasEnhancement(v) then
self:CreateEnhancement(v)
end
self:CreateEnhancement(v)
end
end
end,
Expand All @@ -2630,6 +2628,7 @@ Unit = ClassUnit(moho.unit_methods, IntelComponent, VeterancyComponent, DebugUni
if self and not self.Dead then
self:CreatePresetEnhancements()
end
self.OnStopBeingBuiltEnhancementsThread = nil
end,

---@param self Unit
Expand Down Expand Up @@ -3324,6 +3323,7 @@ Unit = ClassUnit(moho.unit_methods, IntelComponent, VeterancyComponent, DebugUni
end

self:RequestRefreshUI()
return true
end,

---@param self Unit
Expand Down