Skip to content

Commit

Permalink
Move a bunch of functions to the ACF namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
thecraftianman committed Jan 20, 2024
1 parent a8a7fa3 commit 92a2958
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 55 deletions.
39 changes: 16 additions & 23 deletions lua/acf/contraption/contraption_sv.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
-- Contraption-aware functionality

-- Local Funcs ----------------------------------
-- These functions are used within this file and made global at the end
local ACF = ACF

local function GetAncestor(Ent)
function ACF.GetAncestor(Ent)
if not IsValid(Ent) then return nil end

local Parent = Ent
Expand All @@ -17,7 +16,7 @@ local function GetAncestor(Ent)
return Parent, Last
end

function ACF.getAncestors(ent)
function ACF.GetAncestors(ent)
local ancestors = {}
local parent = ent:GetParent()
local count = 0
Expand All @@ -32,7 +31,7 @@ function ACF.getAncestors(ent)
return ancestors
end

function ACF.hasAncestor(ent, ancestor)
function ACF.HasAncestor(ent, ancestor)
if not IsValid(ent) then return false end
if not IsValid(ancestor) then return false end

Expand All @@ -49,7 +48,7 @@ function ACF.hasAncestor(ent, ancestor)
return false
end

local function GetAllPhysicalEntities(Ent, Tab)
function ACF.GetAllPhysicalEntities(Ent, Tab)
local Res = Tab or {}

if IsValid(Ent) and not Res[Ent] then
Expand All @@ -63,8 +62,8 @@ local function GetAllPhysicalEntities(Ent, Tab)
end

if V.Type ~= "NoCollide" then -- NoCollides aren't a real constraint
GetAllPhysicalEntities(V.Ent1, Res)
GetAllPhysicalEntities(V.Ent2, Res)
ACF.GetAllPhysicalEntities(V.Ent1, Res)
ACF.GetAllPhysicalEntities(V.Ent2, Res)
end
end
end
Expand All @@ -73,26 +72,26 @@ local function GetAllPhysicalEntities(Ent, Tab)
return Res
end

local function GetAllChildren(Ent, Tab)
function ACF.GetAllChildren(Ent, Tab)
local Res = Tab or {}

for _, V in pairs(Ent:GetChildren()) do
if not IsValid(V) or Res[V] then continue end

Res[V] = true
GetAllChildren(V, Res)
ACF.GetAllChildren(V, Res)
end

return Res
end

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

for K in pairs(Phys) do
GetAllChildren(K, Pare)
ACF.GetAllChildren(K, Pare)
end

for K in pairs(Phys) do -- Go through the all physical ents (There's probably less of those than the parented ones)
Expand All @@ -116,7 +115,7 @@ function ACF.HasConstraints(Ent)
return false
end

function ACF_CalcMassRatio(Ent, Tally)
function ACF.CalcMassRatio(Ent, Tally)
local TotMass = 0
local PhysMass = 0
local Time = CurTime()
Expand All @@ -129,7 +128,7 @@ function ACF_CalcMassRatio(Ent, Tally)
local OthN = 0
local ConN = 0

local Physical, Parented = GetEnts(Ent)
local Physical, Parented = ACF.GetEnts(Ent)
local Constraints = {}

for K in pairs(Physical) do
Expand Down Expand Up @@ -289,10 +288,4 @@ do -- ASSUMING DIRECT CONTROL
SetNoDraw(self, Bool)
end
end
end

-- Globalize ------------------------------------
ACF_GetAllPhysicalEntities = GetAllPhysicalEntities
ACF_GetAllChildren = GetAllChildren
ACF_GetEnts = GetEnts
ACF_GetAncestor = GetAncestor
end
6 changes: 1 addition & 5 deletions lua/acf/core/round_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,10 @@ function ACF.Kinetic(Speed, Mass)
end

-- changes here will be automatically reflected in the armor properties tool
function ACF_CalcArmor(Area, Ductility, Mass)
function ACF.CalcArmor(Area, Ductility, Mass)
return (Mass * 1000 / Area / 0.78) / (1 + Ductility) ^ 0.5 * ACF.ArmorMod
end

-- Backwards compatibility
ACF_MuzzleVelocity = ACF.MuzzleVelocity
ACF_Kinetic = ACF.Kinetic

local Weaponry = {
Piledrivers = Classes.Piledrivers,
Missiles = Classes.Missiles,
Expand Down
2 changes: 0 additions & 2 deletions lua/acf/core/utilities/util_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ do -- Networked notifications
net.WriteString(Message or "")
net.Send(Player)
end

ACF_SendNotify = ACF.SendNotify -- Backwards compatibility
end

do -- HTTP Request
Expand Down
9 changes: 2 additions & 7 deletions lua/acf/core/validation_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function ACF.UpdateThickness(Entity, PhysObj, Area, Ductility)
end

local Mass = MassMod and MassMod.Mass or PhysObj:GetMass()
local Armor = ACF_CalcArmor(Area, Ductility, Mass)
local Armor = ACF.CalcArmor(Area, Ductility, Mass)

if Mass ~= Entity.ACF.Mass then
Entity.ACF.Mass = Mass
Expand Down Expand Up @@ -241,9 +241,4 @@ function ACF.Activate(Entity, Recalc)
Entity.ACF.Armour = Thickness * (0.5 + Percent * 0.5)
Entity.ACF.MaxArmour = Thickness
Entity.ACF.Ductility = Ductility
end

-- Globalize ------------------------------------
ACF_CheckLegal = ACF.CheckLegal
ACF_Check = ACF.Check
ACF_Activate = ACF.Activate
end
2 changes: 1 addition & 1 deletion lua/acf/damage/debris_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end

function ACF.KillChildProps(Entity, BlastPos, Energy)
local Explosives = {}
local Children = ACF_GetAllChildren(Entity)
local Children = ACF.GetAllChildren(Entity)
local Count = 0

-- do an initial processing pass on children, separating out explodey things to handle last
Expand Down
4 changes: 2 additions & 2 deletions lua/acf/damage/ke_shove_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ function ACF.KEShove(Target, Pos, Vec, KE)

if hook.Run("ACF_KEShove", Target, Pos, Vec, KE) == false then return end

local Ancestor = ACF_GetAncestor(Target)
local Ancestor = ACF.GetAncestor(Target)
local Phys = Ancestor:GetPhysicsObject()

if IsValid(Phys) then
if not Ancestor.acflastupdatemass or Ancestor.acflastupdatemass + 2 < Clock.CurTime then
ACF_CalcMassRatio(Ancestor)
ACF.CalcMassRatio(Ancestor)
end

local Ratio = Ancestor.acfphystotal / Ancestor.acftotal
Expand Down
2 changes: 1 addition & 1 deletion lua/entities/acf_ammo/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ do -- ACF Activation and Damage -----------------
Entity:EmitSound("ambient/explosions/explode_4.wav", 140, Pitch, ACF.Volume)

BulletData.Pos = Entity:LocalToWorld(Entity:OBBCenter() + VectorRand() * Entity:GetSize() * 0.5)
BulletData.Flight = VectorRand():GetNormalized() * Speed * 39.37 + ACF_GetAncestor(Entity):GetVelocity()
BulletData.Flight = VectorRand():GetNormalized() * Speed * 39.37 + ACF.GetAncestor(Entity):GetVelocity()
BulletData.Owner = Entity.Inflictor or Entity.Owner
BulletData.Gun = Entity
BulletData.Crate = Entity:EntIndex()
Expand Down
2 changes: 1 addition & 1 deletion lua/entities/acf_engine/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ end
function ENT:CalcMassRatio()
local PhysMass = 0
local TotalMass = 0
local Physical, Parented = ACF_GetEnts(self)
local Physical, Parented = ACF.GetEnts(self)

for K in pairs(Physical) do
local Phys = K:GetPhysicsObject() -- Should always exist, but just in case
Expand Down
6 changes: 3 additions & 3 deletions lua/entities/acf_gearbox/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ do -- Movement -----------------------------------------
self.InGear = true
end

local BoxPhys = ACF_GetAncestor(self):GetPhysicsObject()
local BoxPhys = ACF.GetAncestor(self):GetPhysicsObject()
local SelfWorld = BoxPhys:LocalToWorldVector(BoxPhys:GetAngleVelocity())

if self.CVT and self.Gear == 1 then
Expand Down Expand Up @@ -859,7 +859,7 @@ do -- Movement -----------------------------------------
end

if ReactTq ~= 0 then
local BoxPhys = ACF_GetAncestor(self):GetPhysicsObject()
local BoxPhys = ACF.GetAncestor(self):GetPhysicsObject()

if IsValid(BoxPhys) then
BoxPhys:ApplyTorqueCenter(self:GetRight() * Clamp(2 * math.deg(ReactTq * MassRatio) * DeltaTime, -500000, 500000))
Expand Down Expand Up @@ -897,7 +897,7 @@ do -- Braking ------------------------------------------
if not next(self.Wheels) then return end -- No brakes for the non-wheel users
if self.LastBrake == Clock.CurTime then return end -- Don't run this twice in a tick

local BoxPhys = ACF_GetAncestor(self):GetPhysicsObject()
local BoxPhys = ACF.GetAncestor(self):GetPhysicsObject()
local SelfWorld = BoxPhys:LocalToWorldVector(BoxPhys:GetAngleVelocity())
local DeltaTime = Clock.DeltaTime

Expand Down
2 changes: 1 addition & 1 deletion lua/entities/acf_gun/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ do -- Metamethods --------------------------------
local randUnitSquare = (self:GetUp() * (2 * math.random() - 1) + self:GetRight() * (2 * math.random() - 1))
local Spread = randUnitSquare:GetNormalized() * Cone * (math.random() ^ (1 / ACF.GunInaccuracyBias))
local Dir = (self:GetForward() + Spread):GetNormalized()
local Velocity = ACF_GetAncestor(self):GetVelocity()
local Velocity = ACF.GetAncestor(self):GetVelocity()
local AmmoType = AmmoTypes.Get(self.BulletData.Type)

if self.BulletData.CanFuze and self.SetFuze then
Expand Down
6 changes: 3 additions & 3 deletions lua/tests/acf/damage/damage_sv/acf_keshove.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ return {
}

-- For simplicity's sake, we'll pretend the ent's ancestor is itself
stub( _G, "ACF_GetAncestor" ).returns( State.Ent )
stub( ACF, "GetAncestor" ).returns( State.Ent )
end,

cases = {
Expand Down Expand Up @@ -71,7 +71,7 @@ return {
local Ent = State.Ent
Ent.acflastupdatemass = nil

local calcMass = stub( _G, "ACF_CalcMassRatio" )
local calcMass = stub( ACF, "CalcMassRatio" )
ACF.KEShove( Ent, Ones, Ones, 1 )

expect( calcMass ).was.called()
Expand All @@ -84,7 +84,7 @@ return {
local Ent = State.Ent
Ent.acflastupdatemass = -math.huge

local calcMass = stub( _G, "ACF_CalcMassRatio" )
local calcMass = stub( ACF, "CalcMassRatio" )
ACF.KEShove( Ent, Ones, Ones, 1 )

expect( calcMass ).was.called()
Expand Down
12 changes: 6 additions & 6 deletions lua/weapons/gmod_tool/stools/acfarmorprop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local MaximumArmor = ACF.MaximumArmor
-- Calculates mass, armor, and health given prop area and desired ductility and thickness.
local function CalcArmor(Area, Ductility, Thickness)
local mass = Area * (1 + Ductility) ^ 0.5 * Thickness * 0.00078
local armor = ACF_CalcArmor(Area, Ductility, mass)
local armor = ACF.CalcArmor(Area, Ductility, mass)
local health = (Area / ACF.Threshold) * (1 + Ductility)

return mass, armor, health
Expand Down Expand Up @@ -252,7 +252,7 @@ if CLIENT then
if mass > 50000 or mass < 0.1 then
mass = math.Clamp(mass, 0.1, 50000)

thickness = ACF_CalcArmor(area, ductility, mass)
thickness = ACF.CalcArmor(area, ductility, mass)
ArmorProp_Thickness:SetFloat(math.Clamp(thickness, MinimumArmor, MaximumArmor))
end
end)
Expand All @@ -275,7 +275,7 @@ if CLIENT then
ductility = -(39 * area * thickness - mass * 50000) / (39 * area * thickness)
ArmorProp_Ductility:SetFloat(math.Clamp(ductility * 100, -80, 80))

thickness = ACF_CalcArmor(area, ductility, mass)
thickness = ACF.CalcArmor(area, ductility, mass)
ArmorProp_Thickness:SetFloat(math.Clamp(thickness, MinimumArmor, MaximumArmor))
end
end)
Expand Down Expand Up @@ -347,7 +347,7 @@ else -- Serverside-only stuff
local Area = Entity.ACF.Area
local Mass = MassMod and MassMod.Mass or PhysObj:GetMass()
local Ductility = math.Clamp(Data.Ductility or 0, -80, 80) * 0.01
local Thickness = ACF_CalcArmor(Area, Ductility, Mass)
local Thickness = ACF.CalcArmor(Area, Ductility, Mass)

duplicator.ClearEntityModifier(Entity, "mass")
duplicator.ClearEntityModifier(Entity, "acfsettings")
Expand Down Expand Up @@ -450,7 +450,7 @@ do -- Armor readout
local Text3 = "Mobility: %s hp/ton @ %s hp | %s liters of fuel"
local Text4 = "Entities: %s (%s physical, %s parented, %s other entities) | %s constraints"

-- Emulates the stuff done by ACF_CalcMassRatio except with a given set of entities
-- Emulates the stuff done by ACF.CalcMassRatio except with a given set of entities
local function ProcessList(Entities)
local Constraints = {}

Expand Down Expand Up @@ -541,7 +541,7 @@ do -- Armor readout
end,
GetResult = function(_, Trace)
local Ent = Trace.Entity
local Power, Fuel, PhysNum, ParNum, ConNum, Name, OtherNum = ACF_CalcMassRatio(Ent, true)
local Power, Fuel, PhysNum, ParNum, ConNum, Name, OtherNum = ACF.CalcMassRatio(Ent, true)

return Power, Fuel, PhysNum, ParNum, ConNum, Name, OtherNum, Ent.acftotal, Ent.acfphystotal
end
Expand Down

0 comments on commit 92a2958

Please sign in to comment.