Skip to content

Commit

Permalink
Use CFW for CalcMassRatio
Browse files Browse the repository at this point in the history
Notable performance improvement for both CalcMassRatio functions. Should hopefully also fix an occasional error thrown by the Armor Properties tool on reload
  • Loading branch information
thecraftianman committed Aug 27, 2024
1 parent f30735c commit 445fac4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
38 changes: 20 additions & 18 deletions lua/acf/contraption/contraption_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,23 @@ function Contraption.GetAllChildren(Ent, Tab)
end

function Contraption.GetEnts(Ent)
local Ancestor = Ent:GetAncestor()
local Phys = Contraption.GetAllPhysicalEntities(Ancestor)
local Pare = {}

for K in pairs(Phys) do
Contraption.GetAllChildren(K, Pare)
end
local Con = Ent:GetContraption()
local ConEnts = Con and Con.ents or {[Ent] = true}
local Children = Ent:GetFamilyChildren()
local Phys = {}
local Pare = {}

for K in pairs(ConEnts) do
if Children[K] then
Pare[K] = true
else
local CurFamily = K:GetFamily()

for K in pairs(Phys) do -- Go through the all physical ents (There's probably less of those than the parented ones)
if Pare[K] then -- Remove them from parented table
Pare[K] = nil
if CurFamily and CurFamily:GetRoot() ~= K then
Pare[K] = true
else
Phys[K] = true
end
end
end

Expand All @@ -117,7 +123,7 @@ function Contraption.HasConstraints(Ent)
end

function Contraption.CalcMassRatio(Ent, Tally)
local TotMass = 0
local Con = Ent:GetContraption()
local PhysMass = 0
local Time = CurTime()

Expand Down Expand Up @@ -161,10 +167,7 @@ function Contraption.CalcMassRatio(Ent, Tally)
PhysN = PhysN + 1
end


local Mass = Phys:GetMass()

TotMass = TotMass + Mass
PhysMass = PhysMass + Mass
end
end
Expand All @@ -188,12 +191,11 @@ function Contraption.CalcMassRatio(Ent, Tally)

ParN = ParN + 1
end

TotMass = TotMass + Phys:GetMass()

end
end

local TotMass = Con and Con.totalMass or PhysMass

for K in pairs(Physical) do
K.acfphystotal = PhysMass
K.acftotal = TotMass
Expand All @@ -209,7 +211,7 @@ function Contraption.CalcMassRatio(Ent, Tally)
if Tally then
local Owner = Ent:CPPIGetOwner()

return Power, Fuel, PhysN, ParN, ConN, IsValid(Owner) and Owner:Name() or "Unknown", OthN
return Power, Fuel, PhysN, ParN, ConN, IsValid(Owner) and Owner:Name() or "Unknown", OthN, TotMass, PhysMass
end
end

Expand Down
23 changes: 7 additions & 16 deletions lua/entities/acf_engine/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -653,31 +653,22 @@ end

-- specialized calcmassratio for engines
function ENT:CalcMassRatio(SelfTbl)
SelfTbl = SelfTbl or self:GetTable()
local PhysMass = 0
local TotalMass = 0
local Physical, Parented = Contraption.GetEnts(self)
SelfTbl = SelfTbl or self:GetTable()
local Con = self:GetContraption()
local PhysMass = 0

local Physical = Contraption.GetEnts(self)

for K in pairs(Physical) do
local Phys = K:GetPhysicsObject() -- Should always exist, but just in case

if IsValid(Phys) then
local Mass = Phys:GetMass()

TotalMass = TotalMass + Mass
PhysMass = PhysMass + Mass
PhysMass = PhysMass + Mass
end
end

for K in pairs(Parented) do
if not Physical[K] then
local Phys = K:GetPhysicsObject()

if IsValid(Phys) then
TotalMass = TotalMass + Phys:GetMass()
end
end
end
local TotalMass = Con and Con.totalMass or PhysMass

SelfTbl.MassRatio = PhysMass / TotalMass
TotalMass = Round(TotalMass, 2)
Expand Down

0 comments on commit 445fac4

Please sign in to comment.