Skip to content

Commit

Permalink
Transfer all unbuilt units on death (FAForever#6363)
Browse files Browse the repository at this point in the history
## Description of the proposed changes
<!-- A clear and concise description (or visuals) of what the changes
imply. -->
<!-- If it closes an issue, make sure to link the issue by using
"(Closes/Fixes/Resolves) #(Issue Number)" in your pull request. -->
Resolves FAForever#6361.
- Changes the categories for structure transfer from T4s and T3 Arty to
all units.
- Adjusts the stats of the wreckage created when a unit rebuild fails so
that the wreckage has the full mass of the failed unit.

## Testing done on the proposed changes
<!-- List all relevant testing that you've done to confirm the changes
work. -->
Spawn a paragon and some t3 engineers (they dont shoot) then build any
unfinished structure or unit from a factory and run the console command
to transfer the units to the opposite of the current focused army:
```lua
SimLua 
local units = ArmyBrains[GetFocusArmy()]:GetListOfUnits(categories.ALLUNITS)
local SimUtils = import('/lua/SimUtils.lua')
local target = GetFocusArmy() == 2 and {1} or {2}
ForkThread(SimUtils.TransferUnfinishedUnitsAfterDeath, units, target, false)
```
All structures get shared properly, and unbuilt units in factories turn
into wrecks of the same partial cost as the unbuilt unit.

Didn't test performance, I'm not sure how to.

## Checklist
- [x] Changes are annotated, including comments where useful
- [x] Changes are documented in the changelog for the next game version
  • Loading branch information
lL1l1 authored Jul 22, 2024
1 parent 409fedc commit 4cac141
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelog/snippets/features.6363.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- (#6363) When an army is defeated, transfer all unbuilt units instead of only Experimentals and T3 Arty.
- When a unit fails to rebuild because its build site was blocked (for example a unit in factory), the mass invested is returned as a wreck.
12 changes: 8 additions & 4 deletions lua/SimUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

local CreateWreckage = import("/lua/wreckage.lua").CreateWreckage

local transferUnbuiltCategory = categories.EXPERIMENTAL + categories.TECH3 * categories.STRUCTURE * categories.ARTILLERY
local transferUnbuiltCategory = categories.ALLUNITS
local transferUnitsCategory = categories.ALLUNITS - categories.INSIGNIFICANTUNIT
local buildersCategory = categories.ALLUNITS - categories.CONSTRUCTION - categories.ENGINEER

Expand Down Expand Up @@ -66,7 +66,7 @@ end
--- replaces the units with new ones)
---@param units Unit[]
---@param toArmy number
---@param captured boolean
---@param captured boolean?
---@return Unit[]?
function TransferUnitsOwnership(units, toArmy, captured)
local toBrain = GetArmyBrain(toArmy)
Expand Down Expand Up @@ -569,12 +569,16 @@ end
---@param blockingEntities RevertibleCollisionShapeEntity[]
function FinalizeRebuiltUnits(trackers, blockingEntities)
for _, tracker in trackers do
if not tracker.Success and tracker.CanCreateWreck then -- create 50% wreck. Copied from Unit:CreateWreckageProp()
if not tracker.Success and tracker.CanCreateWreck then
local bp = tracker.UnitBlueprint
local pos = tracker.UnitPos
local orientation = tracker.UnitOrientation
local mass = bp.Economy.BuildCostMass * 0.57 --0.57 to compensate some multipliers in CreateWreckage()
-- Refund exactly how much mass was put into the unit
local completionFactor = tracker.TargetBuildTime / bp.Economy.BuildTime
local mass = bp.Economy.BuildCostMass * completionFactor
-- Don't refund energy because it would be counterintuitive for wreckage
local energy = 0
-- global 2x time multiplier for unit wrecks, see `Unit:CreateWreckageProp`
local time = (bp.Wreckage.ReclaimTimeMultiplier or 1) * 2
CreateWreckage(bp, pos, orientation, mass, energy, time)
end
Expand Down

0 comments on commit 4cac141

Please sign in to comment.