From 1c7b67ef0c0b02324b8c5f7bf3eb117d9ad30bbd Mon Sep 17 00:00:00 2001 From: Garrett Brown Date: Thu, 10 Jan 2019 19:58:02 -0500 Subject: [PATCH] Revert "Cleanup old code and fix constraints with world not rotating correctly" This reverts commit c4b8020fb5ef1aabf2108df1f75ffeee03648121. --- lua/advdupe2/sv_clipboard.lua | 20 +++-- lua/advdupe2/sv_misc.lua | 159 ++++++++++++++++++++++++++-------- 2 files changed, 135 insertions(+), 44 deletions(-) diff --git a/lua/advdupe2/sv_clipboard.lua b/lua/advdupe2/sv_clipboard.lua index abf6786..7c9b3c8 100644 --- a/lua/advdupe2/sv_clipboard.lua +++ b/lua/advdupe2/sv_clipboard.lua @@ -320,6 +320,7 @@ local function Copy( Ent, EntTable, ConstraintTable, Offset ) if Constraint:IsValid() then index = Constraint:GetCreationID() if index and not ConstraintTable[index] then + Constraint.Identity = index local ConstTable, EntTab = CopyConstraintTable( table.Copy(Constraint:GetTable()), Offset ) ConstraintTable[index] = ConstTable for j,e in pairs(EntTab) do @@ -389,6 +390,7 @@ function AdvDupe2.duplicator.AreaCopy( Entities, Offset, CopyOutside ) index = v:GetCreationID() if(index and not Constraints[index])then + v.Identity = v:GetCreationID() Constraints[index] = v end end @@ -520,16 +522,14 @@ local function CreateConstraintFromTable(Constraint, EntityList, EntityTable, Pl local Bone2 local Bone2Index local ReEnableSecond - if Constraint.BuildDupeInfo and first ~= nil and second ~= nil and not first:IsWorld() and not second:IsWorld() then - if Constraint.BuildDupeInfo.Ent4Ang then Constraint.BuildDupeInfo.Ent2Ang = Constraint.BuildDupeInfo.Ent4Ang end -- Backwards compatibility... - if Constraint.BuildDupeInfo.Ent2Ang ~= nil then + if(Constraint.BuildDupeInfo)then + + if first ~= nil and second ~= nil and not second:IsWorld() and Constraint.BuildDupeInfo.EntityPos ~= nil then local SecondPhys = second:GetPhysicsObject() if IsValid(SecondPhys) then if not DontEnable then ReEnableSecond = SecondPhys:IsMoveable() end SecondPhys:EnableMotion(false) - second:SetPos(first:GetPos()-Constraint.BuildDupeInfo.EntityPos) - second:SetAngles(Constraint.BuildDupeInfo.Ent2Ang) if(Constraint.BuildDupeInfo.Bone2) then Bone2Index = Constraint.BuildDupeInfo.Bone2 Bone2 = second:GetPhysicsObjectNum(Bone2Index) @@ -542,7 +542,7 @@ local function CreateConstraintFromTable(Constraint, EntityList, EntityTable, Pl end end - if Constraint.BuildDupeInfo.Ent1Ang ~= nil then + if first ~= nil and Constraint.BuildDupeInfo.Ent1Ang ~= nil then local FirstPhys = first:GetPhysicsObject() if IsValid(FirstPhys) then if not DontEnable then ReEnableFirst = FirstPhys:IsMoveable() end @@ -559,6 +559,14 @@ local function CreateConstraintFromTable(Constraint, EntityList, EntityTable, Pl end end end + + if second ~= nil and Constraint.BuildDupeInfo.Ent2Ang ~= nil then + second:SetAngles(Constraint.BuildDupeInfo.Ent2Ang) + end + + if second ~= nil and Constraint.BuildDupeInfo.Ent4Ang ~= nil then + second:SetAngles(Constraint.BuildDupeInfo.Ent4Ang) + end end local status, Ent = pcall( Factory.Func, unpack(Args)) diff --git a/lua/advdupe2/sv_misc.lua b/lua/advdupe2/sv_misc.lua index 3138f07..c694b16 100644 --- a/lua/advdupe2/sv_misc.lua +++ b/lua/advdupe2/sv_misc.lua @@ -9,45 +9,128 @@ ]] --[[ - Name: AdvDupe2_BypassAddConstraintTable - Desc: Hook into AddConstraintTable to get entities involved when a constraint is created + Name: SavePositions + Desc: Save the position of the entities to prevent sagging on dupe. + Params: Constraint + Returns: nil ]] -hook.Add("Initialize","AdvDupe2_BypassAddConstraintTable",function() - local old = constraint.AddConstraintTable - function constraint.AddConstraintTable( Ent1, Constraint, Ent2, Ent3, Ent4 ) - if IsValid(Constraint) then - old( Ent1, Constraint, Ent2, Ent3, Ent4 ) - - timer.Simple(0, function() - if Constraint.BuildDupeInfo then return end - Constraint.BuildDupeInfo = {} - - if IsValid(Ent1) then - Constraint.BuildDupeInfo.Ent1Ang = Ent1:GetAngles() - if(Ent1:GetPhysicsObjectCount()>1)then - Constraint.BuildDupeInfo.Bone1 = Constraint["Bone1"] - Constraint.BuildDupeInfo.Bone1Pos = Ent1:GetPhysicsObjectNum(Constraint["Bone1"]):GetPos() - Ent1:GetPos() - Constraint.BuildDupeInfo.Bone1Angle = Ent1:GetPhysicsObjectNum(Constraint["Bone1"]):GetAngles() - end - if IsValid(Ent2) then - Constraint.BuildDupeInfo.EntityPos = Ent1:GetPos() - Ent2:GetPos() - Constraint.BuildDupeInfo.Ent2Ang = Ent2:GetAngles() - if(Ent2:GetPhysicsObjectCount()>1)then - Constraint.BuildDupeInfo.Bone2 = Constraint["Bone2"] - Constraint.BuildDupeInfo.Bone2Pos = Ent2:GetPhysicsObjectNum(Constraint["Bone2"]):GetPos() - Ent2:GetPos() - Constraint.BuildDupeInfo.Bone2Angle = Ent2:GetPhysicsObjectNum(Constraint["Bone2"]):GetAngles() - end - elseif IsValid(Ent4) then - Constraint.BuildDupeInfo.EntityPos = Ent1:GetPos() - Ent4:GetPos() - Constraint.BuildDupeInfo.Ent2Ang = Ent4:GetAngles() - if(Ent4:GetPhysicsObjectCount()>1)then - Constraint.BuildDupeInfo.Bone2 = Constraint["Bone4"] - Constraint.BuildDupeInfo.Bone2Pos = Ent4:GetPhysicsObjectNum(Constraint["Bone4"]):GetPos() - Ent4:GetPos() - Constraint.BuildDupeInfo.Bone2Angle = Ent4:GetPhysicsObjectNum(Constraint["Bone4"]):GetAngles() - end - end + +local function SavePositions( Constraint ) + + if IsValid(Constraint) then + + Constraint.Identity = Constraint:GetCreationID() + + if Constraint.BuildDupeInfo then return end + + if not Constraint.BuildDupeInfo then Constraint.BuildDupeInfo = {} end + + local Ent1 + local Ent2 + if IsValid(Constraint.Ent) then + Constraint.BuildDupeInfo.Ent1Ang = Constraint.Ent:GetAngles() + end + + if IsValid(Constraint.Ent1) then + Constraint.BuildDupeInfo.Ent1Ang = Constraint.Ent1:GetAngles() + if(Constraint.Ent1:GetPhysicsObjectCount()>1)then + Constraint.BuildDupeInfo.Bone1 = Constraint["Bone1"] + Constraint.BuildDupeInfo.Bone1Pos = Constraint.Ent1:GetPhysicsObjectNum(Constraint["Bone1"]):GetPos() - Constraint.Ent1:GetPos() + Constraint.BuildDupeInfo.Bone1Angle = Constraint.Ent1:GetPhysicsObjectNum(Constraint["Bone1"]):GetAngles() + end + if IsValid(Constraint.Ent2) then + Constraint.BuildDupeInfo.EntityPos = Constraint.Ent1:GetPos() - Constraint.Ent2:GetPos() + Constraint.BuildDupeInfo.Ent2Ang = Constraint.Ent2:GetAngles() + if(Constraint.Ent2:GetPhysicsObjectCount()>1)then + Constraint.BuildDupeInfo.Bone2 = Constraint["Bone2"] + Constraint.BuildDupeInfo.Bone2Pos = Constraint.Ent2:GetPhysicsObjectNum(Constraint["Bone2"]):GetPos() - Constraint.Ent2:GetPos() + Constraint.BuildDupeInfo.Bone2Angle = Constraint.Ent2:GetPhysicsObjectNum(Constraint["Bone2"]):GetAngles() + end + elseif IsValid(Constraint.Ent4) then + Constraint.BuildDupeInfo.EntityPos = Constraint.Ent1:GetPos() - Constraint.Ent4:GetPos() + Constraint.BuildDupeInfo.Ent4Ang = Constraint.Ent4:GetAngles() + if(Constraint.Ent4:GetPhysicsObjectCount()>1)then + Constraint.BuildDupeInfo.Bone2 = Constraint["Bone4"] + Constraint.BuildDupeInfo.Bone2Pos = Constraint.Ent4:GetPhysicsObjectNum(Constraint["Bone4"]):GetPos() - Constraint.Ent4:GetPos() + Constraint.BuildDupeInfo.Bone2Angle = Constraint.Ent4:GetPhysicsObjectNum(Constraint["Bone4"]):GetAngles() end - end) + end + end + + end + +end + + +local function FixMagnet(Magnet) + Magnet.Entity = Magnet +end + +//Find out when a Constraint is created +timer.Simple(0, function() + hook.Add( "OnEntityCreated", "AdvDupe2_SavePositions", function(entity) + + if not IsValid( entity ) then return end + + local a,b = entity:GetClass():match("^(.-)_(.+)") + + if b == "magnet" then + timer.Simple( 0, function() FixMagnet(entity) end) + end + + if a == "phys" then + if(b=="constraintsystem")then return end + timer.Simple( 0, function() SavePositions(entity) end) + end + + end ) + end) + +-- Register camera entity class +-- fixes key not being saved (Conna) +local function CamRegister(Player, Pos, Ang, Key, Locked, Toggle, Vel, aVel, Frozen, Nocollide) + if (!Key) then return end + + local Camera = ents.Create("gmod_cameraprop") + Camera:SetAngles(Ang) + Camera:SetPos(Pos) + Camera:Spawn() + Camera:SetKey(Key) + Camera:SetPlayer(Player) + Camera:SetLocked(Locked) + Camera.toggle = Toggle + Camera:SetTracking(NULL, Vector(0)) + + if (Toggle == 1) then + numpad.OnDown(Player, Key, "Camera_Toggle", Camera) + else + numpad.OnDown(Player, Key, "Camera_On", Camera) + numpad.OnUp(Player, Key, "Camera_Off", Camera) end -end) + + if (Nocollide) then Camera:GetPhysicsObject():EnableCollisions(false) end + + -- Merge table + local Table = { + key = Key, + toggle = Toggle, + locked = Locked, + pl = Player, + nocollide = nocollide + } + table.Merge(Camera:GetTable(), Table) + + -- remove any camera that has the same key defined for this player then add the new one + local ID = Player:UniqueID() + GAMEMODE.CameraList[ID] = GAMEMODE.CameraList[ID] or {} + local List = GAMEMODE.CameraList[ID] + if (List[Key] and List[Key] != NULL ) then + local Entity = List[Key] + Entity:Remove() + end + List[Key] = Camera + return Camera + +end +duplicator.RegisterEntityClass("gmod_cameraprop", CamRegister, "Pos", "Ang", "key", "locked", "toggle", "Vel", "aVel", "frozen", "nocollide") \ No newline at end of file