From 02bdf14b06382a4acfc64e0de01c24b698e174a9 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Fri, 29 Nov 2024 03:24:17 +0300 Subject: [PATCH 1/4] base_wire_entity optimizations --- lua/entities/base_wire_entity.lua | 60 +++++++++++++++++-------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/lua/entities/base_wire_entity.lua b/lua/entities/base_wire_entity.lua index 97c027e485..a1095391ec 100644 --- a/lua/entities/base_wire_entity.lua +++ b/lua/entities/base_wire_entity.lua @@ -117,19 +117,19 @@ if CLIENT then } render.CullMode(MATERIAL_CULLMODE_CCW) - surface.DrawPoly( poly ) + surface.DrawPoly(poly) surface.SetDrawColor(0, 0, 0, 255) - for i=1,#poly-1 do + for i = 1, 5 do surface.DrawLine( poly[i].x, poly[i].y, poly[i+1].x, poly[i+1].y ) end - surface.DrawLine( poly[#poly].x, poly[#poly].y, poly[1].x, poly[1].y ) + surface.DrawLine( poly[6].x, poly[6].y, poly[1].x, poly[1].y ) end local function getWireName( ent ) local name = ent:GetNWString("WireName") - if not name or name == "" then return ent.PrintName else return name end + if not name or name == "" then return ent:GetTable().PrintName else return name end end -- This is overridable by other wire entities which want to customize the overlay @@ -142,7 +142,7 @@ if CLIENT then -- This is overridable by other wire entities which want to customize the overlay function ENT:DrawWorldTipBody( pos ) local data = self:GetOverlayData() - draw.DrawText( data.txt, "GModWorldtip", pos.center.x, pos.min.y + edgesize/2, Color(255,255,255,255), TEXT_ALIGN_CENTER ) + draw.DrawText( data.txt, "GModWorldtip", pos.center.x, pos.min.y + edgesize/2, color_white, TEXT_ALIGN_CENTER ) end -- This is overridable by other wire entities which want to customize the overlay @@ -206,11 +206,11 @@ if CLIENT then end if info_requires_multiline then - draw.DrawText( class, "GModWorldtip", pos.center.x, offset + 8, Color(255,255,255,255), TEXT_ALIGN_CENTER ) - draw.DrawText( name, "GModWorldtip", pos.center.x, offset + h_class + 16, Color(255,255,255,255), TEXT_ALIGN_CENTER ) + draw.DrawText( class, "GModWorldtip", pos.center.x, offset + 8, color_white, TEXT_ALIGN_CENTER ) + draw.DrawText( name, "GModWorldtip", pos.center.x, offset + h_class + 16, color_white, TEXT_ALIGN_CENTER ) else - draw.DrawText( class, "GModWorldtip", pos.min.x + edgesize, offset + 16, Color(255,255,255,255) ) - draw.DrawText( name, "GModWorldtip", pos.min.x + pos.size.w - w_name - edgesize, offset + 16, Color(255,255,255,255) ) + draw.DrawText( class, "GModWorldtip", pos.min.x + edgesize, offset + 16, color_white ) + draw.DrawText( name, "GModWorldtip", pos.min.x + pos.size.w - w_name - edgesize, offset + 16, color_white ) end end @@ -258,11 +258,11 @@ if CLIENT then local cur_ent = ply:GetEyeTrace().Entity - if cur_ent ~= looked_at and IsValid(looked_at) and looked_at.IsWire then + if cur_ent ~= looked_at and IsValid(looked_at) and looked_at:GetTable().IsWire then looked_at:BeingLookedAtByLocalPlayer() end - if IsValid(cur_ent) and cur_ent.IsWire and cur_ent:BeingLookedAtByLocalPlayer() then + if IsValid(cur_ent) and cur_ent:GetTable().IsWire and cur_ent:BeingLookedAtByLocalPlayer() then looked_at = cur_ent else looked_at = nil @@ -282,12 +282,14 @@ if CLIENT then end function ENT:Think() - if (CurTime() >= (self.NextRBUpdate or 0)) then + local tab = self:GetTable() + + if (CurTime() >= (tab.NextRBUpdate or 0)) then -- We periodically update the render bounds every 10 seconds - the -- reasons why are mostly anecdotal, but in some circumstances -- entities might 'forget' their renderbounds. Nobody really knows -- if this is still needed or not. - self.NextRBUpdate = CurTime() + 10 + tab.NextRBUpdate = CurTime() + 10 Wire_UpdateRenderBounds(self) end end @@ -301,9 +303,11 @@ if CLIENT then halos_inv[self] = true end + local color_halo = Color(100, 100, 255) + hook.Add("PreDrawHalos", "Wiremod_overlay_halos", function() - if #halos == 0 then return end - halo.Add(halos, Color(100,100,255), 3, 3, 1, true, true) + if table.IsEmpty(halos) then return end + halo.Add(halos, color_halo, 3, 3, 1, true, true) halos = {} halos_inv = {} end) @@ -315,7 +319,7 @@ if CLIENT then -- Basic legacy GetOverlayText, is no longer used here but we leave it here in case other addons rely on it. function ENT:GetOverlayText() local name = self:GetNWString("WireName") - if name == "" then name = self.PrintName end + if name == "" then name = self:GetTable().PrintName end local header = "- " .. name .. " -" local data = self:GetOverlayData() @@ -373,7 +377,8 @@ function ENT:SetOverlayData( data ) end function ENT:GetOverlayData() - return self.OverlayData + local tab = self:GetTable() + return tab and tab.OverlayData or nil end if CLIENT then return end -- no more client @@ -510,15 +515,18 @@ ENT.LINK_STATUS_DEACTIVATED = 2 -- alias ENT.LINK_STATUS_ACTIVE = 3 ENT.LINK_STATUS_ACTIVATED = 3 -- alias function ENT:ColorByLinkStatus(status) - local a = self:GetColor().a - - if status == self.LINK_STATUS_UNLINKED then - self:SetColor(Color(255,0,0,a)) - elseif status == self.LINK_STATUS_LINKED then - self:SetColor(Color(255,165,0,a)) - elseif status == self.LINK_STATUS_ACTIVE then - self:SetColor(Color(0,255,0,a)) + local tab = self:GetTable() + local color = self:GetColor() + + if status == tab.LINK_STATUS_UNLINKED then + color.r, color.g, color.b = 255, 0, 0 + elseif status == tab.LINK_STATUS_LINKED then + color.r, color.g, color.b = 255, 165, 0 + elseif status == tab.LINK_STATUS_ACTIVE then + color.r, color.g, color.b = 0, 255, 0 else - self:SetColor(Color(255,255,255,a)) + color.r, color.g, color.b = 255, 255, 255 end + + self:SetColor(color) end From a600451036ec82ef8258883e56c7805b8b199883 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:15:35 +0300 Subject: [PATCH 2/4] Do not create new vectors --- lua/entities/base_wire_entity.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/entities/base_wire_entity.lua b/lua/entities/base_wire_entity.lua index a1095391ec..e563fbd7cf 100644 --- a/lua/entities/base_wire_entity.lua +++ b/lua/entities/base_wire_entity.lua @@ -40,8 +40,11 @@ if CLIENT then local pos = localPly:GetEyeTrace().HitPos local spos = localPly:GetShootPos() if pos == spos then -- if the position is right in your face, get a better position - pos = spos + localPly:GetAimVector() * 5 + pos = localPly:GetAimVector() + pos:Mul(5) + pos:Add(spos) end + pos = pos:ToScreen() pos.x = math.Round(pos.x) From 7b1a707ea985fc773bc4ddc025b5dd6979c25f18 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Mon, 2 Dec 2024 02:31:37 +0300 Subject: [PATCH 3/4] table.IsEmpty -> halos[1] == nil Co-authored-by: thegrb93 --- lua/entities/base_wire_entity.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/entities/base_wire_entity.lua b/lua/entities/base_wire_entity.lua index e563fbd7cf..ef23719d08 100644 --- a/lua/entities/base_wire_entity.lua +++ b/lua/entities/base_wire_entity.lua @@ -309,7 +309,7 @@ if CLIENT then local color_halo = Color(100, 100, 255) hook.Add("PreDrawHalos", "Wiremod_overlay_halos", function() - if table.IsEmpty(halos) then return end + if halos[1]==nil then return end halo.Add(halos, color_halo, 3, 3, 1, true, true) halos = {} halos_inv = {} From 4df7bf43840d6e70dc992c13fa1798628a95f573 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Mon, 2 Dec 2024 07:38:44 +0300 Subject: [PATCH 4/4] Revert some changes --- lua/entities/base_wire_entity.lua | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lua/entities/base_wire_entity.lua b/lua/entities/base_wire_entity.lua index ef23719d08..09bffaea22 100644 --- a/lua/entities/base_wire_entity.lua +++ b/lua/entities/base_wire_entity.lua @@ -40,9 +40,7 @@ if CLIENT then local pos = localPly:GetEyeTrace().HitPos local spos = localPly:GetShootPos() if pos == spos then -- if the position is right in your face, get a better position - pos = localPly:GetAimVector() - pos:Mul(5) - pos:Add(spos) + pos = spos + localPly:GetAimVector() * 5 end pos = pos:ToScreen() @@ -132,7 +130,7 @@ if CLIENT then local function getWireName( ent ) local name = ent:GetNWString("WireName") - if not name or name == "" then return ent:GetTable().PrintName else return name end + if not name or name == "" then return ent.PrintName else return name end end -- This is overridable by other wire entities which want to customize the overlay @@ -261,11 +259,11 @@ if CLIENT then local cur_ent = ply:GetEyeTrace().Entity - if cur_ent ~= looked_at and IsValid(looked_at) and looked_at:GetTable().IsWire then + if cur_ent ~= looked_at and IsValid(looked_at) and looked_at.IsWire then looked_at:BeingLookedAtByLocalPlayer() end - if IsValid(cur_ent) and cur_ent:GetTable().IsWire and cur_ent:BeingLookedAtByLocalPlayer() then + if IsValid(cur_ent) and cur_ent.IsWire and cur_ent:BeingLookedAtByLocalPlayer() then looked_at = cur_ent else looked_at = nil @@ -322,7 +320,7 @@ if CLIENT then -- Basic legacy GetOverlayText, is no longer used here but we leave it here in case other addons rely on it. function ENT:GetOverlayText() local name = self:GetNWString("WireName") - if name == "" then name = self:GetTable().PrintName end + if name == "" then name = self.PrintName end local header = "- " .. name .. " -" local data = self:GetOverlayData() @@ -380,8 +378,7 @@ function ENT:SetOverlayData( data ) end function ENT:GetOverlayData() - local tab = self:GetTable() - return tab and tab.OverlayData or nil + return self.OverlayData end if CLIENT then return end -- no more client