Skip to content

Commit

Permalink
Fix problems with polys
Browse files Browse the repository at this point in the history
Massively simplify global pos getting
Split getting global vertices into different function
Fixed bad egpo indexget
Harden EGPObjects against bad inputs
Change some small syntax things
  • Loading branch information
Denneisk committed Oct 27, 2023
1 parent 47f8b9e commit fd3900b
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 126 deletions.
9 changes: 8 additions & 1 deletion lua/entities/gmod_wire_egp/lib/egplib/objectcontrol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ function baseObj:Contains(x, y)
end
function baseObj:EditObject(args)
local ret = false
if args.x or args.y or args.angle then
self:SetPos(args.x or self.x, args.y or self.y, args.angle or self.angle)
args.x, args.y, args.angle = nil, nil, nil
end
for k, v in pairs(args) do
if self[k] ~= nil and self[k] ~= v then
self[k] = v
Expand All @@ -63,7 +67,10 @@ function baseObj:SetPos(x, y, angle)
local ret = false
if self.x ~= x then self.x, ret = x, true end
if self.y ~= y then self.y, ret = y, true end
if angle and self.angle ~= angle then self.angle, ret = angle, true end
if angle then
angle = angle % 360
if self.angle ~= angle then self.angle, ret = angle, true end
end
return ret
end
function baseObj:Set(member, value)
Expand Down
131 changes: 53 additions & 78 deletions lua/entities/gmod_wire_egp/lib/egplib/parenting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ EGP.ParentingFuncs.addUV = addUV
local function makeArray( v, fakepos )
local ret = {}
if isstring(v.verticesindex) then
if fakepos then
if not fakepos then
if (not v["_"..v.verticesindex]) then EGP:AddParentIndexes( v ) end
for k,v in ipairs( v["_"..v.verticesindex] ) do
ret[#ret+1] = v.x
Expand Down Expand Up @@ -90,7 +90,8 @@ end
EGP.getCenterFrom = getCenterFrom

-- (returns true if obj has vertices, false if not, followed by the new position data)
function EGP:GetGlobalPos( Ent, index )
local function GetGlobalPos(self, Ent, index)
if self ~= EGP then Ent, index = self, Ent end
local bool, obj
if istable(index) then
obj = index
Expand All @@ -99,84 +100,58 @@ function EGP:GetGlobalPos( Ent, index )
bool, _, obj = self:HasObject(Ent, index)
end
if bool then
if obj.verticesindex then -- Object has vertices
if obj.parent and obj.parent ~= 0 then -- Object is parented
local data, ret
if obj.parent == -1 then -- object is parented to the cursor
local xy = { 0, 0 }
if CLIENT then
xy = self:EGPCursor(Ent, LocalPlayer())
end
local x, y = xy[1], xy[2]
local r = makeArray(obj)
for i = 1, #r, 2 do
local x_ = r[i]
local y_ = r[i + 1]
local vec = LocalToWorld(Vector(x_, y_, 0), Angle(0, obj._angle or 0, 0), Vector(x, y, 0), angle_zero)
r[i] = vec.x
r[i + 1] = vec.y
end

if isstring(obj.verticesindex) then
local temp = makeTable( obj, r )
addUV( obj, temp )
ret = { [obj.verticesindex] = temp }
else ret = makeTable(obj, r) end
local vec, ang = LocalToWorld(Vector(obj._x, obj._y, 0), Angle(0, obj._angle or 0, 0), Vector(x, y, 0), Angle(0, -data.angle or 0, 0))
ret.x, ret.y, ret.angle = vec.x, vec.y, -ang.y
return true, ret
else
local _, _, prnt = self:HasObject(Ent, obj.parent)
local _, data = self:GetGlobalPos(Ent, prnt)
local x, y, ang = data.x, data.y, data.angle or 0
local objang = obj._angle or 0
local temp = makeArray( obj )
for i = 1, #temp, 2 do
local x_ = temp[i]
local y_ = temp[i + 1]
local vec = LocalToWorld(Vector(x_, y_, 0), Angle(0, objang, 0), Vector(x, y, 0), Angle(0, -ang, 0))
temp[i] = vec.x
temp[i + 1] = vec.y
end

if isstring(obj.verticesindex) then ret = { [obj.verticesindex] = makeTable(obj, temp) } else ret = makeTable(obj, temp) end
local vec, ang = LocalToWorld(Vector(obj._x, obj._y, 0), Angle(0, objang, 0), Vector(x, y, 0), Angle(0, -ang, 0))
ret.x, ret.y, ret.angle = vec.x, vec.y, -ang.y
return true, ret
if obj.parent and obj.parent ~= 0 then -- Object is parented
if obj.parent == -1 then -- Object is parented to the cursor
local x, y = 0, 0
if CLIENT then
xy = EGP:EGPCursor( Ent, LocalPlayer() )
x, y = xy[1], xy[2]
end

if isstring(obj.verticesindex) then ret = { [obj.verticesindex] = makeTable(obj, makeArray(obj)) } else ret = makeTable(obj, makeArray(obj)) end
local vec, ang = LocalToWorld(Vector(obj._x, obj._y, 0), Angle(0, obj._angle or 0, 0), Vector(data.x, data.y, 0), Angle(0, -data.angle, 0))
ret.x, ret.y, ret.angle = vec.x, vec.y, -ang.y
return true, ret
end
local ret = {}
if isstring(obj.verticesindex) then ret = { [obj.verticesindex] = makeTable(obj, makeArray(obj, true)) } else ret = makeTable(obj, makeArray(obj, true)) end
ret.x, ret.y, ret.angle = obj.x, obj.y, obj.angle
return true, ret
else -- Object does not have vertices
if obj.parent and obj.parent ~= 0 then -- Object is parented
if obj.parent == -1 then -- Object is parented to the cursor
local xy = { 0, 0 }
if (CLIENT) then
xy = self:EGPCursor( Ent, LocalPlayer() )
end
local x, y = xy[1], xy[2]
local vec, ang = LocalToWorld( Vector( obj._x, obj._y, 0 ), Angle( 0, obj._angle or 0, 0 ), Vector( x, y, 0 ), angle_zero )
return false, { x = vec.x, y = vec.y, angle = -ang.y }
else
local _, _, prnt = self:HasObject(Ent, obj.parent)
local _, data = self:GetGlobalPos(Ent, prnt)
local vec, ang = LocalToWorld( Vector( obj._x, obj._y, 0 ), Angle( 0, obj._angle or 0, 0 ), Vector( data.x, data.y, 0 ), Angle( 0, -(data.angle or 0), 0 ) )
return false, { x = vec.x, y = vec.y, angle = -ang.y }
end
local vec, ang = LocalToWorld(Vector(obj._x, obj._y, 0), Angle(0, obj._angle or 0, 0), Vector(x, y, 0), angle_zero)
return obj.verticesindex ~= nil, { x = vec.x, y = vec.y, angle = -ang.y }
else
local _, data = GetGlobalPos(Ent, select(3, EGP:HasObject(Ent, obj.parent)))
local vec, ang = LocalToWorld(Vector(obj._x, obj._y, 0), Angle(0, -obj._angle or 0, 0), Vector(data.x, data.y, 0), Angle(0, -data.angle or 0, 0))
return obj.verticesindex ~= nil, { x = vec.x, y = vec.y, angle = -ang.y }
end
return false, { x = obj.x, y = obj.y, angle = obj.angle or 0 }
end
return obj.verticesindex ~= nil, { x = obj.x, y = obj.y, angle = obj.angle or 0 }
end

return false, {x=0,y=0,angle=0}
return false, { x = 0, y = 0, angle = 0 }
end
EGP.GetGlobalPos = GetGlobalPos


local function getGlobalVertices(ent, obj)
if obj.verticesindex then
local _, globalpos = GetGlobalPos(ent, obj)
local gx, gy, gang = globalpos.x, globalpos.y, globalpos.angle

local r = makeArray(obj, obj.parent ~= NULL_EGPOBJECT)
local globalvec, globalang = Vector(gx, gy, 0), Angle(0, -gang, 0)
local objang = obj._angle or obj.angle or 0
for i = 1, #r, 2 do
local x_ = r[i]
local y_ = r[i + 1]
local vec = LocalToWorld(Vector(x_, y_, 0), Angle(0, objang, 0), globalvec, globalang)
r[i] = vec.x
r[i + 1] = vec.y
end

local ret
if isstring(obj.verticesindex) then
local temp = makeTable(obj, r)
addUV(obj, temp)
ret = { [obj.verticesindex] = temp }
else
ret = makeTable(obj, r)
end
return ret
end
end
EGP.GetGlobalVertices = getGlobalVertices

--------------------------------------------------------
-- Parenting functions
Expand Down Expand Up @@ -245,7 +220,7 @@ function EGP:SetParent( Ent, index, parentindex )
bool, parentindex = parentindex ~= nil, parentindex.index
end
if (bool) then
self:AddParentIndexes( v )
EGP:AddParentIndexes( v )

if (SERVER) then parentindex = math.Clamp(parentindex,1,self.ConVars.MaxObjects:GetInt()) end

Expand All @@ -256,7 +231,7 @@ function EGP:SetParent( Ent, index, parentindex )
-- If the user is trying to create a circle of parents, causing an infinite loop
if (not CheckParents( Ent, v, parentindex, {} )) then return false end

if (self:EditObject( v, { parent = parentindex } )) then return true, v end
if v:Set("parent", parentindex) then return true, v end
end
end
end
Expand Down Expand Up @@ -290,13 +265,13 @@ function EGP:UnParent( Ent, index )
index = v.index
end
if (bool) then
local hasVertices, data = self:GetGlobalPos( Ent, index )
self:RemoveParentIndexes( v, hasVertices )
local hasVertices, data = EGP:GetGlobalPos( Ent, index )
EGP:RemoveParentIndexes( v, hasVertices )

if (not v.parent or v.parent == 0) then return false end

data.parent = 0

if (self:EditObject( v, data, Ent:GetPlayer() )) then return true, v end
if v:EditObject(data) then return true, v end
end
end
4 changes: 2 additions & 2 deletions lua/entities/gmod_wire_egp/lib/egplib/queuesystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ function EGP:AddQueueObject( Ent, ply, Function, Object )
found = true
if (v.OnRemove) then v:OnRemove() end
local Obj = self:GetObjectByID( Object.ID )
self:EditObject( Obj, Object:DataStreamInfo() )
Obj:EditObject(Object:DataStreamInfo())
Obj.index = v.index
if (Obj.OnCreate) then Obj:OnCreate() end
LastItem.Args[1][k] = Obj
else -- Edit
found = true
self:EditObject( v, Object:DataStreamInfo() )
v:EditObject(Object:DataStreamInfo())
end

break
Expand Down
8 changes: 6 additions & 2 deletions lua/entities/gmod_wire_egp/lib/egplib/transmitreceive.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,12 @@ if (SERVER) then
else
net.WriteUInt(v.ID, 8) -- Else send the ID of the object

local original = v

if (Ent.Scaling or Ent.TopLeft) then
v = table.Copy(v) -- Make a copy of the table so it doesn't overwrite the serverside object
-- Todo: Make transmit only used for server join/leave/"newframes"/etc, not every time it updates
if original.VerticesUpdate then original.VerticesUpdate = false end
end

-- Scale the positions and size
Expand All @@ -273,7 +277,7 @@ if (SERVER) then

-- Move the object to draw from the top left
if (Ent.TopLeft) then
EGP:MoveTopLeft( Ent, v )
EGP.MoveTopLeft( Ent, v )
end

if v.ChangeOrder then -- We want to change the order of this object, send the index to where we wish to move it
Expand Down Expand Up @@ -620,7 +624,7 @@ if (SERVER) then

-- Move the object to draw from the top left
if (v.TopLeft) then
EGP:MoveTopLeft( v, obj )
EGP.MoveTopLeft( v, obj )
end

DataToSend[#DataToSend+1] = { ID = obj.ID, index = obj.index, Settings = obj:DataStreamInfo() }
Expand Down
19 changes: 9 additions & 10 deletions lua/entities/gmod_wire_egp/lib/egplib/usefulfunctions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ function EGP:ScaleObject( ent, v )
end
local settings = makeTable(v, r)
addUV(v, settings)
if isstring(v.verticesindex) then settings = { [v.verticesindex] = settings } end
self:EditObject( v, settings )
if isstring(v.verticesindex) then v.vertices = settings else v:EditObject(settings) end
else
if (v.x) then
v.x = (v.x - xMin) * xMul
Expand All @@ -81,8 +80,8 @@ end
-- Draw from top left
--------------------------------------------------------

function EGP:MoveTopLeft(ent, obj)
if not self:ValidEGP(ent) then return end
function EGP.MoveTopLeft(ent, obj)
if not EGP:ValidEGP(ent) then return end

local t = nil
if obj.CanTopLeft and obj.x and obj.y and obj.w and obj.h then
Expand All @@ -91,14 +90,14 @@ function EGP:MoveTopLeft(ent, obj)
if obj.angle then t.angle = -ang.yaw end
end
if obj.IsParented then
local bool, _, parent = self:HasObject(ent, obj.parent)
local bool, _, parent = EGP:HasObject(ent, obj.parent)
if bool and parent.CanTopLeft and parent.w and parent.h then
if not t then t = { x = obj.x, y = obj.y, angle = obj.angle } end
t.x = t.x - parent.w / 2
t.y = t.y - parent.h / 2

if t.angle then t.angle = t.angle end
end
if not t then t = { angle = obj.angle } end
if t.angle then t.angle = -t.angle end
end

if t then
Expand Down Expand Up @@ -488,9 +487,9 @@ function EGP.Draw(ent)
for _, obj in ipairs(rt) do
if obj.parent == -1 or obj.NeedsConstantUpdate then ent.NeedsUpdate = true end
if obj.parent ~= 0 then
if not obj.IsParented then EGP:SetParent(ent, obj.index, obj.parent) end
local _, data = EGP:GetGlobalPos(ent, obj.index)
EGP:EditObject(obj, data)
if not obj.IsParented then EGP:SetParent(ent, obj, obj.parent) end
local _, data = EGP.GetGlobalPos(ent, obj)
obj:SetPos(data.x, data.y, data.angle)
elseif obj.IsParented then
EGP:UnParent(ent, obj)
end
Expand Down
Loading

0 comments on commit fd3900b

Please sign in to comment.