Skip to content

Commit

Permalink
some __index optimization and add Get/SetColor4Part usage (#1952)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrb93 authored Dec 31, 2024
1 parent 7ecd2b4 commit d28f63c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 64 deletions.
63 changes: 34 additions & 29 deletions lua/entities/starfall_processor/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ include("shared.lua")

DEFINE_BASECLASS("base_gmodentity")

local IsValid = FindMetaTable("Entity").IsValid
local IsWorld = FindMetaTable("Entity").IsWorld
local Ent_GetTable = FindMetaTable("Entity").GetTable
local Ent_IsValid = FindMetaTable("Entity").IsValid
local Ent_IsWorld = FindMetaTable("Entity").IsWorld

function ENT:Initialize()
self.name = "Generic ( No-Name )"
Expand All @@ -25,12 +26,14 @@ function ENT:Initialize()
end

function ENT:GetOverlayText()
local ent_tbl = Ent_GetTable(self)
local state = self:GetNWInt("State", 1)

local clientstr, serverstr
if self.instance then
local bufferAvg = self.instance.cpu_average
clientstr = tostring(math.Round(bufferAvg * 1000000)) .. "us. (" .. tostring(math.floor(bufferAvg / self.instance.cpuQuota * 100)) .. "%)"
elseif self.error then
if ent_tbl.instance then
local bufferAvg = ent_tbl.instance.cpu_average
clientstr = tostring(math.Round(bufferAvg * 1000000)) .. "us. (" .. tostring(math.floor(bufferAvg / ent_tbl.instance.cpuQuota * 100)) .. "%)"
elseif ent_tbl.error then
clientstr = "Errored / Terminated"
else
clientstr = "None"
Expand All @@ -43,19 +46,20 @@ function ENT:GetOverlayText()
serverstr = "None"
end

local authorstr = self.author and self.author:Trim() ~= "" and "\nAuthor: " .. self.author or ""
local authorstr = ent_tbl.author and string.Trim(ent_tbl.author) ~= "" and "\nAuthor: " .. ent_tbl.author or ""

return "- Starfall Processor -\n[ " .. self.name .. " ]"..authorstr.."\nServer CPU: " .. serverstr .. "\nClient CPU: " .. clientstr
return "- Starfall Processor -\n[ " .. ent_tbl.name .. " ]"..authorstr.."\nServer CPU: " .. serverstr .. "\nClient CPU: " .. clientstr
end

function ENT:Think()
local lookedAt = self:BeingLookedAtByLocalPlayer()
self.lookedAt = lookedAt
local ent_tbl = Ent_GetTable(self)
local lookedAt = ent_tbl.BeingLookedAtByLocalPlayer(self)
ent_tbl.lookedAt = lookedAt

if lookedAt then
if self.CustomOverlay then
if ent_tbl.CustomOverlay then
halo.Add( { self }, color_white, 1, 1, 1, true, true )
elseif not self:GetNoDraw() and self:GetColor().a > 0 then
elseif not self:GetNoDraw() and select(4, self:GetColor4Part()) > 0 then
AddWorldTip( self:EntIndex(), self:GetOverlayText(), 0.5, self:GetPos(), self )
halo.Add( { self }, color_white, 1, 1, 1, true, true )
end
Expand All @@ -73,17 +77,18 @@ function ENT:SetCustomOverlay(rt)
end

function ENT:DrawCustomOverlay()
if self.lookedAt then
self.OverlayFade = math.min(self.OverlayFade + FrameTime()*2, 1)
local ent_tbl = Ent_GetTable(self)
if ent_tbl.lookedAt then
ent_tbl.OverlayFade = math.min(ent_tbl.OverlayFade + FrameTime()*2, 1)
else
self.OverlayFade = math.max(self.OverlayFade - FrameTime()*2, 0)
ent_tbl.OverlayFade = math.max(ent_tbl.OverlayFade - FrameTime()*2, 0)
end
if self.OverlayFade > 0 then
if ent_tbl.OverlayFade > 0 then
local pos = self:GetPos():ToScreen()

SF.RT_Material:SetTexture("$basetexture", self.CustomOverlay)
SF.RT_Material:SetTexture("$basetexture", ent_tbl.CustomOverlay)
render.SetMaterial( SF.RT_Material )
render.DrawQuad( Vector(pos.x-128,pos.y-300,0), Vector(pos.x+128,pos.y-300,0), Vector(pos.x+128,pos.y-44,0), Vector(pos.x-128,pos.y-44,0), Color(255,255,255,self.OverlayFade*255) )
render.DrawQuad( Vector(pos.x-128,pos.y-300,0), Vector(pos.x+128,pos.y-300,0), Vector(pos.x+128,pos.y-44,0), Vector(pos.x-128,pos.y-44,0), Color(255,255,255,ent_tbl.OverlayFade*255) )
end
end

Expand Down Expand Up @@ -114,21 +119,21 @@ function ENT:SetReuploadOnReload(enabled)
end

if WireLib then
function ENT:DrawTranslucent()
self:DrawModel()
function ENT:DrawTranslucent(flags)
self:DrawModel(flags)
Wire_Render(self)
end
else
function ENT:DrawTranslucent()
self:DrawModel()
function ENT:DrawTranslucent(flags)
self:DrawModel(flags)
end
end

hook.Add("StarfallError", "StarfallErrorReport", function(_, owner, client, main_file, message, traceback, should_notify)
if not IsValid(owner) then return end
if not Ent_IsValid(owner) then return end
local local_player = LocalPlayer()
if owner == local_player then
if client:IsWorld() or client == owner then
if Ent_IsWorld(client) or client == owner then
SF.AddNotify(owner, message, "ERROR", 7, "ERROR1")
elseif client then
if should_notify then
Expand All @@ -149,9 +154,9 @@ end)

net.Receive("starfall_processor_download", function(len)
net.ReadStarfall(nil, function(ok, sfdata, err)
if ok and IsValid(sfdata.proc) and (IsValid(sfdata.owner) or IsWorld(sfdata.owner)) then
if ok and Ent_IsValid(sfdata.proc) and (Ent_IsValid(sfdata.owner) or Ent_IsWorld(sfdata.owner)) then
sfdata.proc:Compile(sfdata)
elseif IsValid(sfdata.proc) and IsValid(sfdata.owner) then
elseif Ent_IsValid(sfdata.proc) and Ent_IsValid(sfdata.owner) then
sfdata.proc.owner = sfdata.owner
sfdata.proc:Error({message = "Failed to download and initialize client: " .. tostring(err), traceback = "" })
end
Expand All @@ -171,7 +176,7 @@ end)

net.Receive("starfall_processor_kill", function()
local target = net.ReadEntity()
if IsValid(target) and target:GetClass()=="starfall_processor" then
if Ent_IsValid(target) and target:GetClass()=="starfall_processor" then
target:Error({message = "Killed", traceback = ""})
end
end)
Expand All @@ -180,8 +185,8 @@ net.Receive("starfall_processor_used", function(len)
local chip = net.ReadEntity()
local used = net.ReadEntity()
local activator = net.ReadEntity()
if not IsValid(chip) then return end
if not IsValid(used) then return end
if not Ent_IsValid(chip) then return end
if not Ent_IsValid(used) then return end
local instance = chip.instance
if not instance then return end

Expand Down
2 changes: 1 addition & 1 deletion lua/entities/starfall_processor/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function ENT:Initialize()
self:AddEFlags( EFL_FORCE_CHECK_TRANSMIT )

self:SetNWInt("State", self.States.None)
self:SetColor(Color(255, 0, 0, self:GetColor().a))
self:SetColor4Part(255, 0, 0, select(4, self:GetColor4Part()))
self.ErroredPlayers = {}
self.ActiveHuds = {}
end
Expand Down
5 changes: 2 additions & 3 deletions lua/entities/starfall_processor/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ function ENT:Compile(sfdata)

if SERVER then
self.ErroredPlayers = {}
local clr = self:GetColor()
self:SetColor(Color(255, 255, 255, clr.a))
self:SetColor4Part(255, 255, 255, select(4, self:GetColor4Part()))
self:SetNWInt("State", self.States.Normal)

if self.Inputs then
Expand Down Expand Up @@ -118,7 +117,7 @@ function ENT:Error(err)

if SERVER then
self:SetNWInt("State", self.States.Error)
self:SetColor(Color(255, 0, 0, 255))
self:SetColor4Part(255, 0, 0, 255)
end

hook.Run("StarfallError", self, self.owner, CLIENT and LocalPlayer() or Entity(0), self.sfdata and self.sfdata.mainfile or "", msg, traceback)
Expand Down
49 changes: 23 additions & 26 deletions lua/entities/starfall_prop/cl_init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
include("shared.lua")
ENT.RenderGroup = RENDERGROUP_OPAQUE

ENT.DefaultMaterial = Material( "models/wireframe" )
ENT.Material = ENT.DefaultMaterial

local IsValid = FindMetaTable("Entity").IsValid
local IsValidPhys = FindMetaTable("PhysObj").IsValid
local Ent_IsValid = FindMetaTable("Entity").IsValid
local Phys_IsValid = FindMetaTable("PhysObj").IsValid
local Ent_GetTable = FindMetaTable("Entity").GetTable

function ENT:Initialize()
self.rendermesh = Mesh(self.Material)
Expand All @@ -28,41 +28,37 @@ end

function ENT:Think()
local physobj = self:GetPhysicsObject()
if IsValidPhys(physobj) then
if Phys_IsValid(physobj) then
physobj:SetPos( self:GetPos() )
physobj:SetAngles( self:GetAngles() )
physobj:EnableMotion(false)
physobj:Sleep()
end
end

function ENT:Draw()
if self:GetColor().a ~= 255 then
self.RenderGroup = RENDERGROUP_BOTH
else
self.RenderGroup = RENDERGROUP_OPAQUE
end

self:DrawModel()
function ENT:Draw(flags)
self:DrawModel(flags)
end

function ENT:GetRenderMesh()
if self.custom_mesh then
if self.custom_mesh_data[self.custom_mesh] then
return { Mesh = self.custom_mesh, Material = self.Material--[[, Matrix = self.render_matrix]] }
local ent_tbl = Ent_GetTable(self)
if ent_tbl.custom_mesh then
if ent_tbl.custom_mesh_data[ent_tbl.custom_mesh] then
return { Mesh = ent_tbl.custom_mesh, Material = ent_tbl.Material--[[, Matrix = ent_tbl.render_matrix]] }
else
self.custom_mesh = nil
ent_tbl.custom_mesh = nil
end
else
return { Mesh = self.rendermesh, Material = self.Material--[[, Matrix = self.render_matrix]] }
return { Mesh = ent_tbl.rendermesh, Material = ent_tbl.Material--[[, Matrix = ent_tbl.render_matrix]] }
end
end

net.Receive("starfall_custom_prop", function()
local self, data

local function applyData()
if not (IsValid(self) and self.rendermesh:IsValid() and data and not self.rendermeshloaded) then return end
local ent_tbl = Ent_GetTable(self)
if not (ent_tbl and ent_tbl.rendermesh:IsValid() and data and not ent_tbl.rendermeshloaded) then return end
local stream = SF.StringStream(data)
local physmesh = {}
local mins, maxs = Vector(math.huge, math.huge, math.huge), Vector(-math.huge, -math.huge, -math.huge)
Expand All @@ -80,11 +76,11 @@ net.Receive("starfall_custom_prop", function()
end
physmesh[i] = convex
end
self.sf_physmesh = physmesh
self:BuildPhysics(physmesh)
ent_tbl.sf_physmesh = physmesh
ent_tbl.BuildPhysics(self, physmesh)

local phys = self:GetPhysicsObject()
if IsValidPhys(phys) then
if Phys_IsValid(phys) then
local convexes = phys:GetMeshConvexes()
local rendermesh = convexes[1]
for i=2, #convexes do
Expand All @@ -95,12 +91,12 @@ net.Receive("starfall_custom_prop", function()

-- less than 3 can crash
if #rendermesh >= 3 then
self.rendermesh:BuildFromTriangles(rendermesh)
ent_tbl.rendermesh:BuildFromTriangles(rendermesh)
end
self:SetRenderBounds(mins, maxs)
self:SetCollisionBounds(mins, maxs)
end
self.rendermeshloaded = true
ent_tbl.rendermeshloaded = true
end

net.ReadReliableEntity(function(e)
Expand All @@ -119,8 +115,9 @@ net.Receive("starfall_custom_prop", function()
end)

hook.Add("NetworkEntityCreated", "starfall_prop_physics", function(ent)
local mesh = ent.sf_physmesh
if mesh and not IsValidPhys(ent:GetPhysicsObject()) then
ent:BuildPhysics(mesh)
local ent_tbl = Ent_GetTable(ent)
local mesh = ent_tbl.sf_physmesh
if mesh and not Phys_IsValid(ent:GetPhysicsObject()) then
ent_tbl.BuildPhysics(ent, mesh)
end
end)
Loading

0 comments on commit d28f63c

Please sign in to comment.