Skip to content

Commit 6279b7f

Browse files
authored
Update drawing for box selection area-copy (#534)
* Update drawing for box selection area-copy Remove the unnecessary vectors created in real time and code repetition * Reduce vector creation even more. Use top points for trace instead. * Change the comment relative to the code * Changed: Size related variable instead of an index one * Put direction first and origin second * Added: Some optimization for creating the box * Initialization is not needed
1 parent 8c430cc commit 6279b7f

1 file changed

Lines changed: 48 additions & 44 deletions

File tree

lua/weapons/gmod_tool/stools/advdupe2.lua

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ if(SERVER) then
156156

157157
--Find all the entities in a box, given the adjacent corners and the player
158158
local function FindInBox(min, max, ply)
159-
local PPCheck = (tobool(ply:GetInfo("advdupe2_copy_only_mine")) and ply.CPPIGetOwner~=nil) and PlayerCanDupeCPPI or PlayerCanDupeTool
159+
local PPFlags = (tobool(ply:GetInfo("advdupe2_copy_only_mine")) and ply.CPPIGetOwner ~= nil)
160+
local PPCheck = (PPFlags and PlayerCanDupeCPPI or PlayerCanDupeTool)
160161
local EntTable = {}
161162

162163
for _, ent in ipairs(ents.FindInBox(min, max)) do
@@ -1741,27 +1742,44 @@ if(CLIENT) then
17411742
return EntTable
17421743
end
17431744

1745+
local function GetCubeToScreen(pos, tracedata)
1746+
tracedata.start:Set(pos)
1747+
tracedata.endpos:Set(tracedata.offset)
1748+
tracedata.endpos:Mul(-2)
1749+
tracedata.endpos:Add(pos)
1750+
util.TraceLine( tracedata )
1751+
local top = tracedata.start
1752+
local bot = tracedata.output.HitPos
1753+
return top:ToScreen(), bot:ToScreen()
1754+
end
17441755

1745-
local GreenSelected = Color(0, 255, 0, 255)
1746-
function AdvDupe2.DrawSelectionBox()
1747-
1748-
local TraceRes = util.TraceLine(util.GetPlayerTrace(LocalPlayer()))
1749-
local i = math.Clamp(tonumber(LocalPlayer():GetInfo("advdupe2_area_copy_size")) or 50, 0, 30720)
1756+
-- Trace points
1757+
local TraceBoxEdge = {
1758+
output = {},
1759+
start = Vector(),
1760+
endpos = Vector(),
1761+
corner = Vector(),
1762+
offset = Vector(),
1763+
mask = MASK_NPCWORLDSTATIC,
1764+
baseco = Color(0, 255, 0, 255)
1765+
}
17501766

1751-
--Bottom Points
1752-
local B1 = (Vector(-i,-i,-i) + TraceRes.HitPos)
1753-
local B2 = (Vector(-i, i,-i) + TraceRes.HitPos)
1754-
local B3 = (Vector( i, i,-i) + TraceRes.HitPos)
1755-
local B4 = (Vector( i,-i,-i) + TraceRes.HitPos)
1767+
function AdvDupe2.DrawSelectionBox()
1768+
local User = LocalPlayer()
1769+
local TraceRes = User:GetEyeTrace()
1770+
local s = math.Clamp(User:GetInfoNum("advdupe2_area_copy_size", 50), 0, 30720)
1771+
TraceBoxEdge.corner:SetUnpacked(s, s, s)
1772+
TraceBoxEdge.offset:SetUnpacked(0, 0, s)
17561773

17571774
--Top Points
1758-
local T1 = (Vector(-i,-i, i) + TraceRes.HitPos):ToScreen()
1759-
local T2 = (Vector(-i, i, i) + TraceRes.HitPos):ToScreen()
1760-
local T3 = (Vector( i, i, i) + TraceRes.HitPos):ToScreen()
1761-
local T4 = (Vector( i,-i, i) + TraceRes.HitPos):ToScreen()
1762-
1763-
if(not AdvDupe2.LastUpdate or CurTime()>=AdvDupe2.LastUpdate) then
1764-
1775+
local OO = Vector(TraceRes.HitPos)
1776+
local T1 = Vector(-s,-s, s); T1:Add(OO)
1777+
local T2 = Vector(-s, s, s); T2:Add(OO)
1778+
local T3 = Vector( s, s, s); T3:Add(OO)
1779+
local T4 = Vector( s,-s, s); T4:Add(OO)
1780+
1781+
if(not AdvDupe2.LastUpdate or CurTime() >= AdvDupe2.LastUpdate) then
1782+
-- Revert the marked entities original color
17651783
if AdvDupe2.ColorEntities then
17661784
for k,v in pairs(AdvDupe2.EntityColors)do
17671785
local ent = AdvDupe2.ColorEntities[k]
@@ -1770,38 +1788,24 @@ if(CLIENT) then
17701788
end
17711789
end
17721790
end
1773-
1774-
local Entities = FindInBox(B1, (Vector(i,i,i)+TraceRes.HitPos), LocalPlayer())
1791+
-- Paint all entities with the base color
1792+
local B3 = Vector(TraceBoxEdge.corner)
1793+
B3:Mul(-2); B3:Add(T3)
1794+
local Entities = FindInBox(B3, T3, User)
17751795
AdvDupe2.ColorEntities = Entities
17761796
AdvDupe2.EntityColors = {}
1777-
for k,v in pairs(Entities)do
1797+
for k, v in pairs(Entities)do
17781798
AdvDupe2.EntityColors[k] = v:GetColor()
1779-
v:SetColor(GreenSelected)
1799+
v:SetColor(TraceBoxEdge.baseco)
17801800
end
1781-
AdvDupe2.LastUpdate = CurTime()+0.25
1782-
1801+
AdvDupe2.LastUpdate = CurTime() + 0.25
17831802
end
17841803

1785-
local tracedata = {}
1786-
tracedata.mask = MASK_NPCWORLDSTATIC
1787-
local WorldTrace
1788-
1789-
tracedata.start = B1+Vector(0,0,i*2)
1790-
tracedata.endpos = B1
1791-
WorldTrace = util.TraceLine( tracedata )
1792-
B1 = WorldTrace.HitPos:ToScreen()
1793-
tracedata.start = B2+Vector(0,0,i*2)
1794-
tracedata.endpos = B2
1795-
WorldTrace = util.TraceLine( tracedata )
1796-
B2 = WorldTrace.HitPos:ToScreen()
1797-
tracedata.start = B3+Vector(0,0,i*2)
1798-
tracedata.endpos = B3
1799-
WorldTrace = util.TraceLine( tracedata )
1800-
B3 = WorldTrace.HitPos:ToScreen()
1801-
tracedata.start = B4+Vector(0,0,i*2)
1802-
tracedata.endpos = B4
1803-
WorldTrace = util.TraceLine( tracedata )
1804-
B4 = WorldTrace.HitPos:ToScreen()
1804+
-- Calculate Cube to screen points
1805+
local T1, B1 = GetCubeToScreen(T1, TraceBoxEdge)
1806+
local T2, B2 = GetCubeToScreen(T2, TraceBoxEdge)
1807+
local T3, B3 = GetCubeToScreen(T3, TraceBoxEdge)
1808+
local T4, B4 = GetCubeToScreen(T4, TraceBoxEdge)
18051809

18061810
surface.SetDrawColor( 0, 255, 0, 255 )
18071811

0 commit comments

Comments
 (0)