@@ -19,16 +19,41 @@ local buildersCategory = categories.ALLUNITS - categories.CONSTRUCTION - categor
1919
2020--- @alias FactoryRebuildDataTable table<UnitId , (FactoryUnit | FactoryRebuildData )[]>
2121
22+ --- Clear data for a factory so transferring it doesn't try to rebuild units again
23+ --- @param factory FactoryUnit | FactoryRebuildData
24+ local function clearFactoryRebuildData (factory )
25+ factory .FacRebuild_UnitId = nil
26+ factory .FacRebuild_Progress = nil
27+ factory .FacRebuild_BuildTime = nil
28+ factory .FacRebuild_Health = nil
29+ factory .FacRebuild_OldBuildRate = nil
30+ end
31+
2232--- @param factoryRebuildDataTable FactoryRebuildDataTable
2333function FactoryRebuildUnits (factoryRebuildDataTable )
2434 for buildUnitId , factories in factoryRebuildDataTable do
35+ -- Remove support factories that can't build their unit due to lacking an HQ
36+ local noFactories = false
37+ for i , factory in factories do
38+ if not factory :CanBuild (buildUnitId ) then
39+ clearFactoryRebuildData (factory )
40+ factories [i ] = nil
41+ if table .empty (factories ) then
42+ factoryRebuildDataTable [buildUnitId ] = nil
43+ noFactories = true
44+ end
45+ continue
46+ end
47+ end
48+ if noFactories then continue end
49+
2550 IssueClearCommands (factories )
2651 IssueBuildFactory (factories , buildUnitId , 1 )
2752 end
2853 -- wait for build order to start and then rebuild the units for free
2954 WaitTicks (1 )
30- for k , factories in pairs ( factoryRebuildDataTable ) do
31- for i , factory in pairs ( factories ) do
55+ for k , factories in factoryRebuildDataTable do
56+ for i , factory in factories do
3257 if factory .Dead then
3358 factories [i ] = nil
3459 if table .empty (factories ) then
@@ -45,8 +70,8 @@ function FactoryRebuildUnits(factoryRebuildDataTable)
4570 end
4671 -- wait for buildpower to apply then return the factories to normal and pause them
4772 WaitTicks (1 )
48- for k , factories in pairs ( factoryRebuildDataTable ) do
49- for i , factory in pairs ( factories ) do
73+ for k , factories in factoryRebuildDataTable do
74+ for i , factory in factories do
5075 if factory .Dead then
5176 factories [i ] = nil
5277 if table .empty (factories ) then
@@ -97,11 +122,7 @@ Health: %f
97122 rebuiltUnit :SetHealth (nil , factory .FacRebuild_Health )
98123 end
99124
100- -- clean up after the rebuilding
101- factory .FacRebuild_Progress = nil
102- factory .FacRebuild_BuildTime = nil
103- factory .FacRebuild_Health = nil
104- factory .FacRebuild_OldBuildRate = nil
125+ clearFactoryRebuildData (factory )
105126 end
106127 end
107128end
@@ -185,10 +206,10 @@ function TransferUnitsOwnership(units, toArmy, captured, noRestrictions)
185206 local defaultBuildRate
186207 local upgradeBuildTimeComplete
187208 local exclude
188- local FacRebuild_UnitId
189- local FacRebuild_Progress
190- local FacRebuild_BuildTime
191- local FacRebuild_Health
209+ local FacRebuild_UnitId = unit . FacRebuild_UnitId
210+ local FacRebuild_Progress = unit . FacRebuild_Progress
211+ local FacRebuild_BuildTime = unit . FacRebuild_BuildTime
212+ local FacRebuild_Health = unit . FacRebuild_Health
192213
193214 local shield = unit .MyShield
194215 if shield then
@@ -380,6 +401,9 @@ function TransferUnitsOwnership(units, toArmy, captured, noRestrictions)
380401 else
381402 table.insert (data , newFactoryUnit )
382403 end
404+ -- store data for rebuilding
405+ -- unit id is not needed during rebuild but is needed if transferred again in the middle of rebuild
406+ newFactoryUnit .FacRebuild_UnitId = FacRebuild_UnitId
383407 newFactoryUnit .FacRebuild_Progress = FacRebuild_Progress
384408 newFactoryUnit .FacRebuild_BuildTime = FacRebuild_BuildTime
385409 newFactoryUnit .FacRebuild_Health = FacRebuild_Health
0 commit comments