From 4b89862452e865d1bde7ec726f288e966a47f291 Mon Sep 17 00:00:00 2001 From: jenz Date: Mon, 8 Jun 2026 23:07:13 +0200 Subject: [PATCH] probably a bit cheaper calculationwise --- .../scripting/RenderDistance.sp | 85 ++++++++++--------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/RenderDistance_webclient/scripting/RenderDistance.sp b/RenderDistance_webclient/scripting/RenderDistance.sp index 7bc687e..e2861be 100644 --- a/RenderDistance_webclient/scripting/RenderDistance.sp +++ b/RenderDistance_webclient/scripting/RenderDistance.sp @@ -15,15 +15,32 @@ static const char entityList[][] = { "env_entity_igniter", "env_explosion", "env "info_player_counterterrorist", "info_player_terrorist", "infodecal", \ "item_assaultsuit", "item_defuser", "item_kevlar", \ "light", "light_environment", "light_spot", "move_rope", "path_track", "planted_c4", \ - "point_viewcontrol", "shadow_control", "vgui_screen", "worldspawn"}; + "point_viewcontrol", "shadow_control", "vgui_screen", "func_tracktrain", "worldspawn", \ + "func_tanktrain"}; -bool bEnabled[MAXPLAYERS + 1], - bBind[MAXPLAYERS + 1], - bHolding[MAXPLAYERS + 1], - bDontRenderFire[MAXPLAYERS + 1]; +bool bEnabled[MAXPLAYERS + 1]; -int iDistance[MAXPLAYERS + 1], - iFlameEntity = -1; +int iDistance[MAXPLAYERS + 1]; + +float g_fClientOrigin[MAXPLAYERS + 1][3]; + +public void OnPluginStart() +{ + // Run the cache timer every 0.5 seconds globally + CreateTimer(0.5, Timer_CacheOrigins, _, TIMER_REPEAT); +} + +public Action Timer_CacheOrigins(Handle timer) +{ + for (int i = 1; i <= MaxClients; i++) + { + if (IsClientConnected(i) && IsClientInGame(i) && !IsFakeClient(i)) + { + GetClientAbsOrigin(i, g_fClientOrigin[i]); + } + } + return Plugin_Continue; +} public Plugin myinfo = { @@ -37,9 +54,6 @@ public Plugin myinfo = public void OnClientAuthorized(int client, const char[] auth) { bEnabled[client] = false; - bBind[client] = false; - bHolding[client] = false; - bDontRenderFire[client] = false; iDistance[client] = 800; char sIP[32]; @@ -59,12 +73,7 @@ public void OnEntityCreated(int entity, const char[] classname) { bool hook = false; - if (!strcmp(classname, "entityflame")) - { - iFlameEntity = EntIndexToEntRef(entity); - hook = true; - } - else if (!strncmp(classname, "prop_", 5)) + if (!strncmp(classname, "prop_", 5)) { hook = true; } @@ -86,37 +95,35 @@ public void OnEntityCreated(int entity, const char[] classname) } } -bool bFoundEntity[MAXPLAYERS + 1]; -int iSearchEntity[MAXPLAYERS + 1]; - -public bool EnumerateSphere(int entity, any client) -{ - if (entity == iSearchEntity[client]) - { - bFoundEntity[client] = true; - return false; // Stop enumerating early - } - return true; // Keep enumerating -} - public Action DoTransmit(int entity, int client) { - if (bDontRenderFire[client] && entity == EntRefToEntIndex(iFlameEntity)) - return Plugin_Handled; - - if (IsFakeClient(client) || !bEnabled[client] || (bBind[client] && !bHolding[client])) + if (IsFakeClient(client) || !bEnabled[client]) return Plugin_Continue; if (GetEdictFlags(entity) & FL_EDICT_ALWAYS) SetEdictFlags(entity, GetEdictFlags(entity) ^ FL_EDICT_ALWAYS); - static float vec[3]; - GetClientAbsOrigin(client, vec); + // Get Entity Origin + static float entVec[3]; + if (HasEntProp(entity, Prop_Send, "m_vecOrigin")) + { + GetEntPropVector(entity, Prop_Send, "m_vecOrigin", entVec); + } + else + { + return Plugin_Continue; // If it doesnt have an origin, let it render safely + } - iSearchEntity[client] = entity; - bFoundEntity[client] = false; + // Use GetVectorDistance with UseSquared=true (avoids an expensive Square Root calculation) + float maxDist = float(iDistance[client]); + float distanceSq = ((g_fClientOrigin[client][0] - entVec[0]) * (g_fClientOrigin[client][0] - entVec[0])) + + ((g_fClientOrigin[client][1] - entVec[1]) * (g_fClientOrigin[client][1] - entVec[1])) + + ((g_fClientOrigin[client][2] - entVec[2]) * (g_fClientOrigin[client][2] - entVec[2])); - TR_EnumerateEntitiesSphere(vec, float(iDistance[client]), PARTITION_SOLID_EDICTS | PARTITION_TRIGGER_EDICTS | PARTITION_NON_STATIC_EDICTS, EnumerateSphere, client); + if (distanceSq > (maxDist * maxDist)) + { + return Plugin_Handled; // Out of range, hide it from the software renderer! + } - return bFoundEntity[client] ? Plugin_Continue : Plugin_Handled; + return Plugin_Continue; }