Skip to content

Commit 7ae2d84

Browse files
committed
fix TraceLineTable function degrading performance over time
issue is in the built-in g_pScriptVM->GetValue when it tries to get a value of a vector from a table. Here we make a wrapper to the TraceLineTable function to extract the vectors in squirrel instead
1 parent 0d91bb7 commit 7ae2d84

3 files changed

Lines changed: 526 additions & 485 deletions

File tree

src/game/server/vscript_server.cpp

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,23 +1143,7 @@ static float ScriptTraceLine( const Vector &vecStart, const Vector &vecEnd, HSCR
11431143
}
11441144
}
11451145

1146-
static float ScriptTraceLineParams( const Vector &vecStart, const Vector &vecEnd, HSCRIPT entIgnore, int nMask, int nCollisiongroup )
1147-
{
1148-
// UTIL_TraceLine( vecAbsStart, vecAbsEnd, MASK_BLOCKLOS, pLooker, COLLISION_GROUP_NONE, ptr );
1149-
trace_t tr;
1150-
CBaseEntity *pLooker = ToEnt(entIgnore);
1151-
UTIL_TraceLine( vecStart, vecEnd, ( unsigned int )nMask, pLooker, nCollisiongroup, &tr );
1152-
if (tr.fractionleftsolid && tr.startsolid)
1153-
{
1154-
return 1.0 - tr.fractionleftsolid;
1155-
}
1156-
else
1157-
{
1158-
return tr.fraction;
1159-
}
1160-
}
1161-
1162-
static void ScriptTraceLineTable( HSCRIPT hTable )
1146+
static void ScriptTraceLineTable( HSCRIPT hTable, const Vector &vecStart, const Vector &vecEnd, const Vector &vecMins, const Vector &vecMaxs )
11631147
{
11641148
if ( !hTable )
11651149
return;
@@ -1168,26 +1152,22 @@ static void ScriptTraceLineTable( HSCRIPT hTable )
11681152
return;
11691153

11701154
trace_t tr;
1171-
ScriptVariant_t start, end, mask, ignore, collisiongroup, mins, maxs;
1172-
mask = MASK_VISIBLE_AND_NPCS;
1173-
collisiongroup = COLLISION_GROUP_NONE;
1174-
mins = vec3_origin;
1175-
maxs = vec3_origin;
1176-
g_pScriptVM->GetValue( hTable, "start", &start );
1177-
g_pScriptVM->GetValue( hTable, "end", &end );
1178-
g_pScriptVM->GetValue( hTable, "mask", &mask );
1179-
g_pScriptVM->GetValue( hTable, "ignore", &ignore );
1180-
g_pScriptVM->GetValue( hTable, "collisiongroup", &collisiongroup );
1181-
g_pScriptVM->GetValue( hTable, "mins", &mins );
1182-
g_pScriptVM->GetValue( hTable, "maxs", &maxs );
1183-
const Vector vecStart = start;
1184-
const Vector vecEnd = end;
1185-
const Vector vecMins = mins;
1186-
const Vector vecMaxs = maxs;
1155+
ScriptVariant_t rval;
1156+
int nMask = MASK_VISIBLE_AND_NPCS;
1157+
int nCollisionGroup = COLLISION_GROUP_NONE;
1158+
CBaseEntity *pIgnore = NULL;
1159+
1160+
if ( g_pScriptVM->GetValue( hTable, "mask", &rval ) )
1161+
nMask = ( int )rval;
1162+
if ( g_pScriptVM->GetValue( hTable, "ignore", &rval ) )
1163+
pIgnore = ToEnt( ( HSCRIPT )rval );
1164+
if ( g_pScriptVM->GetValue( hTable, "collisiongroup", &rval ) )
1165+
nCollisionGroup = ( int )rval;
1166+
11871167
if ( vecMins == vecMaxs )
1188-
UTIL_TraceLine( vecStart, vecEnd, ( int )mask, ToEnt( ignore ), ( int )collisiongroup, &tr );
1168+
UTIL_TraceLine( vecStart, vecEnd, ( unsigned int )nMask, pIgnore, nCollisionGroup, &tr );
11891169
else
1190-
UTIL_TraceHull( vecStart, vecEnd, vecMins, vecMaxs, ( int )mask, ToEnt( ignore ), ( int )collisiongroup, &tr );
1170+
UTIL_TraceHull( vecStart, vecEnd, vecMins, vecMaxs, ( unsigned int )nMask, pIgnore, nCollisionGroup, &tr );
11911171

11921172
g_pScriptVM->SetValue( hTable, "pos", tr.endpos );
11931173
g_pScriptVM->SetValue( hTable, "fraction", tr.fraction );
@@ -1736,8 +1716,7 @@ bool VScriptServerInit()
17361716
ScriptRegisterFunction( g_pScriptVM, SendToServerConsole, "Send a string to the server console as a command" );
17371717
ScriptRegisterFunction( g_pScriptVM, GetMapName, "Get the name of the map.");
17381718
ScriptRegisterFunctionNamed( g_pScriptVM, ScriptTraceLine, "TraceLine", "given 2 points & ent to ignore, return fraction along line that hits world or models" );
1739-
ScriptRegisterFunctionNamed( g_pScriptVM, ScriptTraceLineParams, "TraceLineParams", "given 2 points, ent to ignore, mask and collision group, return fraction along line that hits world or models" );
1740-
ScriptRegisterFunctionNamed( g_pScriptVM, ScriptTraceLineTable, "TraceLineTable", "Uses a configuration table to do a raytrace, puts return information into the table for return usage." );
1719+
ScriptRegisterFunction( g_pScriptVM, ScriptTraceLineTable, "Uses a configuration table to do a raytrace, puts return information into the table for return usage." );
17411720

17421721
ScriptRegisterFunction( g_pScriptVM, Time, "Get the current server time" );
17431722
ScriptRegisterFunction( g_pScriptVM, FrameTime, "Get the time spent on the server in the last frame" );

src/game/server/vscript_server.nut

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,22 @@ function ClientPrint( player, target, message, param1 = "", param2 = "", param3
272272
{
273273
DoClientPrint( player, target, message, param1.tostring(), param2.tostring(), param3.tostring(), param4.tostring() );
274274
}
275+
276+
function TraceLineTable( table )
277+
{
278+
local vecStart = Vector( 0.0, 0.0, 0.0 );
279+
local vecEnd = Vector( 0.0, 0.0, 0.0 );
280+
local vecMins = Vector( 0.0, 0.0, 0.0 );
281+
local vecMaxs = Vector( 0.0, 0.0, 0.0 );
282+
283+
if ( "start" in table )
284+
vecStart = table[ "start" ];
285+
if ( "end" in table )
286+
vecEnd = table[ "end" ];
287+
if ( "mins" in table )
288+
vecMins = table[ "mins" ];
289+
if ( "maxs" in table )
290+
vecMaxs = table[ "maxs" ];
291+
292+
ScriptTraceLineTable( table, vecStart, vecEnd, vecMins, vecMaxs );
293+
}

0 commit comments

Comments
 (0)