From 92a2958fc778af7ec791d484087b9a2ffa38503a Mon Sep 17 00:00:00 2001 From: thecraftianman <64441307+thecraftianman@users.noreply.github.com> Date: Sat, 20 Jan 2024 16:21:24 -0500 Subject: [PATCH] Move a bunch of functions to the ACF namespace --- lua/acf/contraption/contraption_sv.lua | 39 ++++++++----------- lua/acf/core/round_functions.lua | 6 +-- lua/acf/core/utilities/util_sv.lua | 2 - lua/acf/core/validation_sv.lua | 9 +---- lua/acf/damage/debris_sv.lua | 2 +- lua/acf/damage/ke_shove_sv.lua | 4 +- lua/entities/acf_ammo/init.lua | 2 +- lua/entities/acf_engine/init.lua | 2 +- lua/entities/acf_gearbox/init.lua | 6 +-- lua/entities/acf_gun/init.lua | 2 +- .../acf/damage/damage_sv/acf_keshove.lua | 6 +-- lua/weapons/gmod_tool/stools/acfarmorprop.lua | 12 +++--- 12 files changed, 37 insertions(+), 55 deletions(-) diff --git a/lua/acf/contraption/contraption_sv.lua b/lua/acf/contraption/contraption_sv.lua index b8228cb8c..d25ccea91 100644 --- a/lua/acf/contraption/contraption_sv.lua +++ b/lua/acf/contraption/contraption_sv.lua @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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() @@ -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 @@ -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 \ No newline at end of file +end \ No newline at end of file diff --git a/lua/acf/core/round_functions.lua b/lua/acf/core/round_functions.lua index 45ee38d6b..8dfd2b319 100644 --- a/lua/acf/core/round_functions.lua +++ b/lua/acf/core/round_functions.lua @@ -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, diff --git a/lua/acf/core/utilities/util_sv.lua b/lua/acf/core/utilities/util_sv.lua index 29f05a469..26f741a7b 100644 --- a/lua/acf/core/utilities/util_sv.lua +++ b/lua/acf/core/utilities/util_sv.lua @@ -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 diff --git a/lua/acf/core/validation_sv.lua b/lua/acf/core/validation_sv.lua index dd9bfa6a1..4482a20d2 100644 --- a/lua/acf/core/validation_sv.lua +++ b/lua/acf/core/validation_sv.lua @@ -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 @@ -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 \ No newline at end of file +end \ No newline at end of file diff --git a/lua/acf/damage/debris_sv.lua b/lua/acf/damage/debris_sv.lua index e3a8186e1..fcd334c40 100644 --- a/lua/acf/damage/debris_sv.lua +++ b/lua/acf/damage/debris_sv.lua @@ -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 diff --git a/lua/acf/damage/ke_shove_sv.lua b/lua/acf/damage/ke_shove_sv.lua index a8a28574a..f1a6f7d2a 100644 --- a/lua/acf/damage/ke_shove_sv.lua +++ b/lua/acf/damage/ke_shove_sv.lua @@ -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 diff --git a/lua/entities/acf_ammo/init.lua b/lua/entities/acf_ammo/init.lua index 8f0f76145..1da858cd0 100644 --- a/lua/entities/acf_ammo/init.lua +++ b/lua/entities/acf_ammo/init.lua @@ -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() diff --git a/lua/entities/acf_engine/init.lua b/lua/entities/acf_engine/init.lua index 3871d0c81..5d93867bb 100644 --- a/lua/entities/acf_engine/init.lua +++ b/lua/entities/acf_engine/init.lua @@ -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 diff --git a/lua/entities/acf_gearbox/init.lua b/lua/entities/acf_gearbox/init.lua index 554b08381..4f078a4d3 100644 --- a/lua/entities/acf_gearbox/init.lua +++ b/lua/entities/acf_gearbox/init.lua @@ -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 @@ -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)) @@ -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 diff --git a/lua/entities/acf_gun/init.lua b/lua/entities/acf_gun/init.lua index fff8fa019..028a08bde 100644 --- a/lua/entities/acf_gun/init.lua +++ b/lua/entities/acf_gun/init.lua @@ -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 diff --git a/lua/tests/acf/damage/damage_sv/acf_keshove.lua b/lua/tests/acf/damage/damage_sv/acf_keshove.lua index 88460ea61..6d98ed44e 100644 --- a/lua/tests/acf/damage/damage_sv/acf_keshove.lua +++ b/lua/tests/acf/damage/damage_sv/acf_keshove.lua @@ -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 = { @@ -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() @@ -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() diff --git a/lua/weapons/gmod_tool/stools/acfarmorprop.lua b/lua/weapons/gmod_tool/stools/acfarmorprop.lua index bb7c69fea..95d4d0b07 100644 --- a/lua/weapons/gmod_tool/stools/acfarmorprop.lua +++ b/lua/weapons/gmod_tool/stools/acfarmorprop.lua @@ -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 @@ -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) @@ -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) @@ -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") @@ -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 = {} @@ -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