Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micro-optimizations for base_wire_entity #3192

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions lua/entities/base_wire_entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Astralcircle marked this conversation as resolved.
Show resolved Hide resolved
pos = pos:ToScreen()

pos.x = math.Round(pos.x)
Expand Down Expand Up @@ -117,19 +120,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
Astralcircle marked this conversation as resolved.
Show resolved Hide resolved
end

-- This is overridable by other wire entities which want to customize the overlay
Expand All @@ -142,7 +145,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
Expand Down Expand Up @@ -206,11 +209,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

Expand Down Expand Up @@ -258,11 +261,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
Astralcircle marked this conversation as resolved.
Show resolved Hide resolved
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
Astralcircle marked this conversation as resolved.
Show resolved Hide resolved
looked_at = cur_ent
else
looked_at = nil
Expand All @@ -282,12 +285,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
Comment on lines 285 to 296
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to investigate if this is even needed code anymore

Expand All @@ -301,9 +306,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
Astralcircle marked this conversation as resolved.
Show resolved Hide resolved
halo.Add(halos, color_halo, 3, 3, 1, true, true)
halos = {}
halos_inv = {}
end)
Expand All @@ -315,7 +322,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
Astralcircle marked this conversation as resolved.
Show resolved Hide resolved
local header = "- " .. name .. " -"

local data = self:GetOverlayData()
Expand Down Expand Up @@ -373,7 +380,8 @@ function ENT:SetOverlayData( data )
end

function ENT:GetOverlayData()
return self.OverlayData
local tab = self:GetTable()
return tab and tab.OverlayData or nil
Astralcircle marked this conversation as resolved.
Show resolved Hide resolved
end

if CLIENT then return end -- no more client
Expand Down Expand Up @@ -510,15 +518,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
Loading