Skip to content

Commit

Permalink
Improved Proper Clipping compatibility
Browse files Browse the repository at this point in the history
- Fixed the physclipped entity recalculation bit, now it'll use the correct hooks.
- Replaced some hijacked Proper Clipping functions with their intended hooks.
- Reverted use of ACF.Check instead of IsValid on the contraption library.
  • Loading branch information
TwistedTail committed Aug 27, 2024
1 parent e2bb9d8 commit f30735c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
55 changes: 42 additions & 13 deletions lua/acf/compatibility/proper_clipping_sv.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
-- Resolves issues with properClipping
-- Triggers an ACF.Activate update whenever a new clip is created or removed
local hook = hook
local ACF = ACF

local timerSimple = timer.Simple
do -- Triggers an ACF.Activate update whenever a new physical clip is created or removed
local timer = timer

local function activate(ent)
if ent._ACF_ProperClipping then return end
local function Update(Entity)
if Entity._ACF_Physclip_Update then return end

ent._ACF_ProperClipping = true
Entity._ACF_Physclip_Update = true

timerSimple(engine.TickInterval() * 2, function()
if not IsValid(ent) then return end
timer.Simple(0, function()
if not IsValid(Entity) then return end

ent._ACF_ProperClipping = nil
Entity._ACF_Physclip_Update = nil

ACF.Activate(ent, true)
end)
ACF.Activate(Entity, true)
end)
end

hook.Add("ProperClippingPhysicsClipped", "ACF", Update)
hook.Add("ProperClippingPhysicsReset", "ACF", Update)
end

hook.Add("ProperClippingClipAdded", "ACF", activate)
hook.Add("ProperClippingClipRemoved", "ACF", activate)
do -- Forces an ACF armored entity to get rid of their mass entity modifier and use the ACF_Armor one instead
local duplicator = duplicator

local function UpdateMass(Entity)
local EntMods = Entity.EntityMods

if not EntMods then return end

local Armor = EntMods.ACF_Armor

if Armor and Armor.Thickness then
local MassMod = EntMods and EntMods.mass

if MassMod then
duplicator.ClearEntityModifier(Entity, "ACF_Armor")
duplicator.StoreEntityModifier(Entity, "ACF_Armor", { Ductility = Armor.Ductility })
else
duplicator.ClearEntityModifier(Entity, "mass")
end
end
end

hook.Add("ProperClippingClipAdded", "ACF", UpdateMass)
hook.Add("ProperClippingClipRemoved", "ACF", UpdateMass)
hook.Add("ProperClippingClipsRemoved", "ACF", UpdateMass)
end
4 changes: 2 additions & 2 deletions lua/acf/contraption/contraption_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end
function Contraption.GetAllPhysicalEntities(Ent, Tab)
local Res = Tab or {}

if ACF.Check(Ent) and not Res[Ent] then
if IsValid(Ent) and not Res[Ent] then
Res[Ent] = true

if Ent.Constraints then
Expand All @@ -77,7 +77,7 @@ function Contraption.GetAllChildren(Ent, Tab)
local Res = Tab or {}

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

Res[V] = true
Contraption.GetAllChildren(V, Res)
Expand Down
35 changes: 0 additions & 35 deletions lua/weapons/gmod_tool/stools/acfarmorprop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -360,41 +360,6 @@ else -- Serverside-only stuff

UpdateArmor(_, Entity, { Thickness = Thickness, Ductility = Ductility * 100 })
end)

-- ProperClipping compatibility

if ProperClipping then
local Override = {
AddClip = true,
RemoveClip = true,
RemoveClips = true,
}

for Name in pairs(Override) do
local Old = ProperClipping[Name]

ProperClipping[Name] = function(Entity, ...)
local EntMods = Entity.EntityMods
local MassMod = EntMods and EntMods.mass
local Result = Old(Entity, ...)

if not EntMods then return Result end

local Armor = EntMods.ACF_Armor

if Armor and Armor.Thickness then
if MassMod then
duplicator.ClearEntityModifier(Entity, "ACF_Armor")
duplicator.StoreEntityModifier(Entity, "ACF_Armor", { Ductility = Armor.Ductility })
else
duplicator.ClearEntityModifier(Entity, "mass")
end
end

return Result
end
end
end
end

do -- Allowing everyone to read contraptions
Expand Down

0 comments on commit f30735c

Please sign in to comment.