Skip to content

Commit

Permalink
Refactor utility functions of the Unit class (FAForever#3678)
Browse files Browse the repository at this point in the history
Refactors various small utility functions to improve performance.
  • Loading branch information
Tagada14 authored Mar 11, 2022
1 parent f3a00dc commit ddda0ca
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 130 deletions.
2 changes: 1 addition & 1 deletion lua/AI/aiutilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ function GetTransports(platoon, units)
local location = platoon:GetPlatoonPosition()
if not location then
-- We can assume we have at least one unit here
location = units[1]:GetCachePosition()
location = units[1]:GetPosition()
end

if not location then
Expand Down
4 changes: 2 additions & 2 deletions lua/EffectUtilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ function SpawnBuildBots(builder, unitBeingBuilt, BuildEffectsBag)
local bot = CreateUnit('ura0001', builder.Army, x + xVec, y + yVec, z + zVec, qx, qy, qz, qw, 'Air')

-- Make build bots unkillable
bot:SetCanTakeDamage(false)
bot:SetCanBeKilled(false)
bot.CanTakeDamage = false
bot.CanBeKilled = false
bot.spawnedBy = builder

table.insert(builder.buildBots, bot)
Expand Down
2 changes: 1 addition & 1 deletion lua/RemoteViewing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function RemoteViewing(SuperClass)
-- visible entity already off
if self.RemoteViewingData.DisableCounter > 1 then return end
-- disable vis entity and monitor resources
if not self:IsDead() and self.RemoteViewingData.Satellite then
if not self.Dead and self.RemoteViewingData.Satellite then
self.RemoteViewingData.Satellite:DisableIntel('Omni')
self.RemoteViewingData.Satellite:DisableIntel('Radar')
self.RemoteViewingData.Satellite:DisableIntel('Vision')
Expand Down
34 changes: 17 additions & 17 deletions lua/ScenarioFramework.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ end
function PauseUnitDeath(unit)
if unit and not unit.Dead then
unit.OnKilled = OverrideKilled
unit:SetCanBeKilled(false)
unit.CanBeKilled = false
unit.DoTakeDamage = OverrideDoDamage
end
end
Expand Down Expand Up @@ -169,7 +169,7 @@ function UnlockAndKillUnitThread(self, instigator, damageType, excessDamageRatio
while PauseUnitDeathActive do
WaitSeconds(1)
end
self:SetCanBeKilled(true)
self.CanBeKilled = true
self:Kill(instigator, damageType, excessDamageRatio)
end

Expand Down Expand Up @@ -935,7 +935,7 @@ end
function FakeTeleportUnit(unit, killUnit)
IssueStop({unit})
IssueClearCommands({unit})
unit:SetCanBeKilled(false)
unit.CanBeKilled = false

unit:PlayTeleportChargeEffects(unit:GetPosition(), unit:GetOrientation())
unit:PlayUnitSound('GateCharge')
Expand Down Expand Up @@ -1649,14 +1649,14 @@ function EndOperationSafety(units)
end
end
for subk, subv in v:GetListOfUnits(categories.COMMAND, false) do
subv:SetCanTakeDamage(false)
subv:SetCanBeKilled(false)
subv.CanTakeDamage = false
subv.CanBeKilled = false
end
if units and not table.empty(units) then
for subk, subv in units do
if not subv.Dead then
subv:SetCanTakeDamage(false)
subv:SetCanBeKilled(false)
subv.CanTakeDamage = false
subv.CanBeKilled = false
end
end
end
Expand Down Expand Up @@ -1942,11 +1942,11 @@ function FlagUnkillableSelect(armyNumber, units)
if not v.CanTakeDamage then
v.UndamagableFlagSet = true
end
if not v:CheckCanBeKilled() then
if not v.CanBeKilled then
v.UnKillableFlagSet = true
end
v:SetCanTakeDamage(false)
v:SetCanBeKilled(false)
v.CanTakeDamage = false
v.CanBeKilled = false
end
end
end
Expand All @@ -1957,20 +1957,20 @@ function FlagUnkillable(armyNumber, exceptions)
if not v.CanTakeDamage then
v.UndamagableFlagSet = true
end
if not v:CheckCanBeKilled() then
if not v.CanBeKilled then
v.UnKillableFlagSet = true
end
v:SetCanTakeDamage(false)
v:SetCanBeKilled(false)
v.CanTakeDamage = false
v.CanBeKilled = false
end
if exceptions then
for _, v in exceptions do
-- Only process units that weren't already set
if not v.UnKillableFlagSet then
v:SetCanBeKilled(true)
v.CanBeKilled = true
end
if not v.UndamagableFlagSet then
v:SetCanTakeDamage(true)
v.CanTakeDamage = true
end
end
end
Expand All @@ -1981,10 +1981,10 @@ function UnflagUnkillable(armyNumber)
for _, v in units do
-- Only revert units that weren't already set
if not v.UnKillableFlagSet then
v:SetCanBeKilled(true)
v.CanBeKilled = true
end
if not v.UndamagableFlagSet then
v:SetCanTakeDamage(true)
v.CanTakeDamage = true
end
v.KilledFlagSet = nil
v.DamageFlagSet = nil
Expand Down
2 changes: 1 addition & 1 deletion lua/aeonweapons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ADFTractorClaw = Class(DefaultBeamWeapon) {

-- Create vacuum suck up from ground effects on the unit targetted.
for _, v in EffectTemplate.ACollossusTractorBeamVacuum01 do
CreateEmitterAtEntity(target, target.Army, v):ScaleEmitter(0.25 * target:GetFootPrintSize()/0.5)
CreateEmitterAtEntity(target, target.Army, v):ScaleEmitter(0.125 * target.FootPrintSize)
end

DefaultBeamWeapon.PlayFxBeamStart(self, muzzle)
Expand Down
5 changes: 3 additions & 2 deletions lua/defaultunits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ StructureUnit = Class(Unit) {
end,

FlattenSkirt = function(self)
local x, y, z = unpack(self:GetCachePosition())
local x, y, z = self:GetPositionXYZ()
local x0, z0, x1, z1 = self:GetSkirtRect()
x0, z0, x1, z1 = math.floor(x0), math.floor(z0), math.ceil(x1), math.ceil(z1)
FlattenMapRect(x0, z0, x1 - x0, z1 - z0, y)
Expand Down Expand Up @@ -1808,10 +1808,11 @@ AirUnit = Class(MobileUnit) {

CreateUnitAirDestructionEffects = function(self, scale)
local scale = explosion.GetAverageBoundingXZRadius(self)
local size = self.Size
explosion.CreateDefaultHitExplosion(self, scale)

if self.ShowUnitDestructionDebris then
explosion.CreateDebrisProjectiles(self, scale, {self:GetUnitSizes()})
explosion.CreateDebrisProjectiles(self, scale, {size.SizeX, size.SizeY, size.SizeZ})
end
end,

Expand Down
2 changes: 1 addition & 1 deletion lua/formations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ function GuardFormation(formationUnits)
remainingShields = remainingShields + 1
end

local fs = u:GetFootPrintSize()
local fs = u.FootPrintSize
footprintCounts[fs] = (footprintCounts[fs] or 0) + 1
end

Expand Down
2 changes: 1 addition & 1 deletion lua/platoon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ Platoon = Class(moho.platoon_methods) {
for k,v in ents do
if not IsProp(v) or eng.BadReclaimables[v] then continue end
if not needEnergy or v.MaxEnergyReclaim then
local rpos = v:GetCachePosition()
local rpos = v:GetPosition()
table.insert(reclaim, {entity=v, pos=rpos, distance=VDist2(pos[1], pos[3], rpos[1], rpos[3])})
end
end
Expand Down
6 changes: 3 additions & 3 deletions lua/seraphimunits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,8 @@ SEnergyBallUnit = Class(SHoverLandUnit) {

OnCreate = function(self)
SHoverLandUnit.OnCreate(self)
self:SetCanTakeDamage(false)
self:SetCanBeKilled(false)
self.CanTakeDamage = false
self.CanBeKilled = false
self:PlayUnitSound('Spawn')
ChangeState(self, self.KillingState)
end,
Expand Down Expand Up @@ -648,7 +648,7 @@ SEnergyBallUnit = Class(SHoverLandUnit) {

DeathState = State {
Main = function(self)
self:SetCanBeKilled(true)
self.CanBeKilled = true
if self.Layer == 'Water' then
self:PlayUnitSound('HoverKilledOnWater')
end
Expand Down
6 changes: 3 additions & 3 deletions lua/shield.lua
Original file line number Diff line number Diff line change
Expand Up @@ -934,13 +934,13 @@ TransportShield = Class(Shield) {
-- Yes it says contents, but this includes the generating transport too
SetContentsVulnerable = function(self, canTakeDamage)
for k, v in self.protectedUnits do
k:SetCanTakeDamage(canTakeDamage)
k.CanTakeDamage = canTakeDamage
end
end,

RemoveProtectedUnit = function(self, unit)
self.protectedUnits[unit] = nil
unit:SetCanTakeDamage(true)
unit.CanTakeDamage = true
end,

AddProtectedUnit = function(self, unit)
Expand All @@ -965,7 +965,7 @@ TransportShield = Class(Shield) {

AddProtectedUnit = function(self, unit)
self.protectedUnits[unit] = true
unit:SetCanTakeDamage(false)
unit.CanTakeDamage = false
end
},

Expand Down
Loading

0 comments on commit ddda0ca

Please sign in to comment.