From 1048d043ab0475c0fc0186da18f148d6bfeccaf7 Mon Sep 17 00:00:00 2001 From: Zaurzo <101999193+Zaurzo@users.noreply.github.com> Date: Mon, 11 Nov 2024 03:03:34 -0500 Subject: [PATCH 1/6] Add ENT:DrawPhysgunHalo This allows entities to determine how the physgun halo is drawn on them. It can also be used to add the halo to other entities, e.g. the other entities that are parented to it --- .../gamemodes/sandbox/gamemode/cl_init.lua | 90 +++++++++++++++++-- 1 file changed, 82 insertions(+), 8 deletions(-) diff --git a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua index 6c6466ee28..c328e4f368 100644 --- a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua +++ b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua @@ -123,28 +123,103 @@ local PhysgunHalos = {} -----------------------------------------------------------]] function GM:DrawPhysgunBeam( ply, weapon, bOn, target, boneid, pos ) - if ( physgun_halo:GetInt() == 0 ) then return true end + if ( !physgun_halo:GetBool() ) then + + PhysgunHalos = nil + + return true + end + + PhysgunHalos = PhysgunHalos || {} + + local tab = PhysgunHalos[ ply ] || {} + if ( IsValid( target ) ) then - PhysgunHalos[ ply ] = target + + local DrawPhysgunHalo = target.DrawPhysgunHalo + + -- + -- Allow entities to suppress the halo or to add the halo to other entities + -- e.g. adding the halo to entities parented to the entity + -- + if ( DrawPhysgunHalo ) then + + local val, color, size = DrawPhysgunHalo( target, ply, weapon, boneid, pos ) + + if ( val != nil ) then + + tab.color = color + tab.size = size + + if ( val == true ) then + + tab[ 1 ] = target + + elseif ( isentity( val ) ) then + + tab[ 1 ] = val + + elseif ( istable( val ) ) then + + for k, ent in ipairs( val ) do + + tab[ k ] = ent + + end + + end + + end + + else + + tab[ 1 ] = target + + end + end + PhysgunHalos[ ply ] = tab + return true end hook.Add( "PreDrawHalos", "AddPhysgunHalos", function() - if ( !PhysgunHalos || table.IsEmpty( PhysgunHalos ) ) then return end + if ( !PhysgunHalos ) then return end for k, v in pairs( PhysgunHalos ) do - if ( !IsValid( k ) ) then continue end + if ( !IsValid( k ) or !v[ 1 ] ) then continue end + + local size = v.size + local color = v.color + + if ( size ) then + + v.size = nil + + else + + size = math.random( 1, 2 ) - local size = math.random( 1, 2 ) - local colr = k:GetWeaponColor() + VectorRand() * 0.3 + end + + if ( color ) then + + v.color = nil + + halo.Add( v, color, size, size, 1, true, false ) + + else - halo.Add( PhysgunHalos, Color( colr.x * 255, colr.y * 255, colr.z * 255 ), size, size, 1, true, false ) + color = k:GetWeaponColor() + VectorRand() * 0.3 + + halo.Add( v, Color( color.x * 255, color.y * 255, color.z * 255 ), size, size, 1, true, false ) + + end end @@ -152,7 +227,6 @@ hook.Add( "PreDrawHalos", "AddPhysgunHalos", function() end ) - --[[--------------------------------------------------------- Name: gamemode:NetworkEntityCreated() Desc: Entity is created over the network From c4291cb5d0bb86765d9cb17d34a5023c8dced0a4 Mon Sep 17 00:00:00 2001 From: Zaurzo <101999193+Zaurzo@users.noreply.github.com> Date: Mon, 11 Nov 2024 03:14:31 -0500 Subject: [PATCH 2/6] This check isn't needed Just return `self` --- garrysmod/gamemodes/sandbox/gamemode/cl_init.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua index c328e4f368..c72ff7c14e 100644 --- a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua +++ b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua @@ -152,11 +152,7 @@ function GM:DrawPhysgunBeam( ply, weapon, bOn, target, boneid, pos ) tab.color = color tab.size = size - if ( val == true ) then - - tab[ 1 ] = target - - elseif ( isentity( val ) ) then + if ( isentity( val ) ) then tab[ 1 ] = val From 9c01e2de62a09f6f2e43fc8ba9029f4dd3ca1a48 Mon Sep 17 00:00:00 2001 From: Zaurzo <101999193+Zaurzo@users.noreply.github.com> Date: Mon, 11 Nov 2024 03:16:19 -0500 Subject: [PATCH 3/6] Be more clear --- garrysmod/gamemodes/sandbox/gamemode/cl_init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua index c72ff7c14e..3a9ba8568e 100644 --- a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua +++ b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua @@ -140,7 +140,7 @@ function GM:DrawPhysgunBeam( ply, weapon, bOn, target, boneid, pos ) local DrawPhysgunHalo = target.DrawPhysgunHalo -- - -- Allow entities to suppress the halo or to add the halo to other entities + -- Allow entities to modify how the halo is drawn, or to add the halo to other entities -- e.g. adding the halo to entities parented to the entity -- if ( DrawPhysgunHalo ) then From 71388202513ca25a987292ed0bc448dd3326c78f Mon Sep 17 00:00:00 2001 From: Zaurzo <101999193+Zaurzo@users.noreply.github.com> Date: Mon, 11 Nov 2024 22:57:10 -0500 Subject: [PATCH 4/6] Rename to DeterminePhysgunHalo + Improvements --- .../gamemodes/sandbox/gamemode/cl_init.lua | 101 ++++++++---------- 1 file changed, 45 insertions(+), 56 deletions(-) diff --git a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua index 3a9ba8568e..650fe49862 100644 --- a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua +++ b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua @@ -115,7 +115,7 @@ function GM:PostRenderVGUI() end -local PhysgunHalos = {} +local physgunHalos = {} --[[--------------------------------------------------------- Name: gamemode:DrawPhysgunBeam() @@ -125,58 +125,65 @@ function GM:DrawPhysgunBeam( ply, weapon, bOn, target, boneid, pos ) if ( !physgun_halo:GetBool() ) then - PhysgunHalos = nil + physgunHalos = nil return true end - PhysgunHalos = PhysgunHalos || {} - - local tab = PhysgunHalos[ ply ] || {} + physgunHalos = physgunHalos || {} - if ( IsValid( target ) ) then - - local DrawPhysgunHalo = target.DrawPhysgunHalo + if ( !IsValid( target ) ) then + + return true - -- - -- Allow entities to modify how the halo is drawn, or to add the halo to other entities - -- e.g. adding the halo to entities parented to the entity - -- - if ( DrawPhysgunHalo ) then + end + + local entsToHalo = {} - local val, color, size = DrawPhysgunHalo( target, ply, weapon, boneid, pos ) + local haloColor = ply:GetWeaponColor() + VectorRand() * 0.3 + local haloSize = math.random( 1, 2 ) - if ( val != nil ) then + haloColor = Color( haloColor.r * 255, haloColor.g * 255, haloColor.b * 255 ) - tab.color = color - tab.size = size + -- + -- Allow entities to suppress the halo or to add the halo to other entities + -- e.g. adding the halo to entities parented to the entity + -- + local DeterminePhysgunHalo = target.DeterminePhysgunHalo + + if ( DeterminePhysgunHalo ) then - if ( isentity( val ) ) then + local val, color, size = DeterminePhysgunHalo( target, ply, haloColor, haloSize, weapon, boneid, pos ) - tab[ 1 ] = val + if ( val != nil ) then - elseif ( istable( val ) ) then + haloColor = color or haloColor + haloSize = size or haloSize - for k, ent in ipairs( val ) do + if ( isentity( val ) ) then - tab[ k ] = ent + entsToHalo[ 1 ] = val - end + elseif ( istable( val ) ) then - end + entsToHalo = val end - else + end - tab[ 1 ] = target + else - end + entsToHalo[ 1 ] = target end - PhysgunHalos[ ply ] = tab + -- We put these in the table for convenience + entsToHalo.color = haloColor + entsToHalo.size = haloSize + + physgunHalos[ ply ] = entsToHalo return true @@ -184,42 +191,24 @@ end hook.Add( "PreDrawHalos", "AddPhysgunHalos", function() - if ( !PhysgunHalos ) then return end - - for k, v in pairs( PhysgunHalos ) do + if ( !physgunHalos ) then return end - if ( !IsValid( k ) or !v[ 1 ] ) then continue end + for ply, entsToHalo in pairs( physgunHalos ) do - local size = v.size - local color = v.color + if ( !IsValid( ply ) or !entsToHalo[ 1 ] ) then continue end - if ( size ) then + local size = entsToHalo.size + local color = entsToHalo.color - v.size = nil + -- Remove these from the table, we got what we needed - halo.Add only accepts entities + entsToHalo.size = nil + entsToHalo.color = nil - else - - size = math.random( 1, 2 ) - - end - - if ( color ) then - - v.color = nil - - halo.Add( v, color, size, size, 1, true, false ) - - else - - color = k:GetWeaponColor() + VectorRand() * 0.3 - - halo.Add( v, Color( color.x * 255, color.y * 255, color.z * 255 ), size, size, 1, true, false ) - - end + halo.Add( entsToHalo, color, size, size, 1, true, false ) end - PhysgunHalos = {} + physgunHalos = {} end ) From be4635071da923159383b04b8a82377fc0cda813 Mon Sep 17 00:00:00 2001 From: Zaurzo <101999193+Zaurzo@users.noreply.github.com> Date: Mon, 11 Nov 2024 23:06:57 -0500 Subject: [PATCH 5/6] micro optimization --- garrysmod/gamemodes/sandbox/gamemode/cl_init.lua | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua index 650fe49862..aa03d9004e 100644 --- a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua +++ b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua @@ -131,13 +131,9 @@ function GM:DrawPhysgunBeam( ply, weapon, bOn, target, boneid, pos ) end - physgunHalos = physgunHalos || {} - - if ( !IsValid( target ) ) then - - return true + if ( !IsValid( target ) ) then return true end - end + physgunHalos = physgunHalos || {} local entsToHalo = {} From 3f71739e4351f58d359309cbd36b85e1729724f2 Mon Sep 17 00:00:00 2001 From: Zaurzo <101999193+Zaurzo@users.noreply.github.com> Date: Wed, 13 Nov 2024 00:13:50 -0500 Subject: [PATCH 6/6] micro optimization --- garrysmod/gamemodes/sandbox/gamemode/cl_init.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua index aa03d9004e..deeac146d6 100644 --- a/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua +++ b/garrysmod/gamemodes/sandbox/gamemode/cl_init.lua @@ -140,12 +140,8 @@ function GM:DrawPhysgunBeam( ply, weapon, bOn, target, boneid, pos ) local haloColor = ply:GetWeaponColor() + VectorRand() * 0.3 local haloSize = math.random( 1, 2 ) - haloColor = Color( haloColor.r * 255, haloColor.g * 255, haloColor.b * 255 ) + haloColor = Color( haloColor.r * 255, haloColor.g * 255, haloColor.b * 255, 255 ) - -- - -- Allow entities to suppress the halo or to add the halo to other entities - -- e.g. adding the halo to entities parented to the entity - -- local DeterminePhysgunHalo = target.DeterminePhysgunHalo if ( DeterminePhysgunHalo ) then