Skip to content

Commit

Permalink
Deduplicate UTIL_StringToVector between server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave committed Sep 25, 2024
1 parent 36d1573 commit 5258690
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 68 deletions.
32 changes: 1 addition & 31 deletions cl_dll/hud_spectator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "screenfade.h"

#include "string_utils.h"
#include "util_shared.h"

#pragma warning(disable: 4244)

Expand Down Expand Up @@ -212,37 +213,6 @@ int CHudSpectator::Init()
return 1;
}

//-----------------------------------------------------------------------------
// UTIL_StringToVector originally from ..\dlls\util.cpp, slightly changed
//-----------------------------------------------------------------------------
void UTIL_StringToVector( float * pVector, const char *pString )
{
char *pstr, *pfront, tempString[128];
int j;

strncpyEnsureTermination( tempString, pString, sizeof( tempString ) );

pstr = pfront = tempString;

for( j = 0; j < 3; j++ )
{
pVector[j] = atof( pfront );

while( *pstr && *pstr != ' ' )
pstr++;
if( !( *pstr ) )
break;
pstr++;
pfront = pstr;
}

if( j < 2 )
{
for( j = j + 1;j < 3; j++ )
pVector[j] = 0;
}
}

int UTIL_FindEntityInMap( const char *name, float *origin, float *angle )
{
int n, found = 0;
Expand Down
35 changes: 0 additions & 35 deletions dlls/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1430,41 +1430,6 @@ BOOL UTIL_TeamsMatch( const char *pTeamName1, const char *pTeamName2 )
return FALSE;
}

void UTIL_StringToVector( float *pVector, const char *pString, int* componentsRead )
{
char *pstr, *pfront, tempString[128];
int j;

strncpyEnsureTermination( tempString, pString, sizeof( tempString ));
pstr = pfront = tempString;

int componentsParsed = 0;

for( j = 0; j < 3; j++ ) // lifted from pr_edict.c
{
pVector[j] = atof( pfront );
componentsParsed++;

while( *pstr && *pstr != ' ' )
pstr++;
if( !( *pstr ) )
break;
pstr++;
pfront = pstr;
}
if (componentsRead)
*componentsRead = componentsParsed;
if( j < 2 )
{
/*
ALERT( at_error, "Bad field in entity!! %s:%s == \"%s\"\n",
pkvd->szClassName, pkvd->szKeyName, pkvd->szValue );
*/
for( j = j + 1;j < 3; j++ )
pVector[j] = 0;
}
}

Vector UTIL_StringToVector(const char* str)
{
float x, y, z;
Expand Down
1 change: 0 additions & 1 deletion dlls/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ extern void UTIL_GunshotDecalTrace( TraceResult *pTrace, int decalNumber );
extern void UTIL_Sparks( const Vector &position );
extern void UTIL_SparkShower( const Vector &position, const SparkEffectParams& params );
extern void UTIL_Ricochet( const Vector &position, float scale );
extern void UTIL_StringToVector( float *pVector, const char *pString, int* componentsRead = NULL );
extern Vector UTIL_StringToVector( const char *str );
extern void UTIL_StringToRandomVector( float *pVector, const char *pString );
extern void UTIL_StringToIntArray( int *pVector, int count, const char *pString );
Expand Down
38 changes: 37 additions & 1 deletion game_shared/util_shared.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "util_shared.h"

#include "string_utils.h"
#include <cmath>
#include <cstdlib>

static unsigned int glSeed = 0;

Expand Down Expand Up @@ -185,3 +186,38 @@ float UTIL_AngleDistance( float next, float cur )

return delta;
}

void UTIL_StringToVector( float *pVector, const char *pString, int* componentsRead )
{
char *pstr, *pfront, tempString[128];
int j;

strncpyEnsureTermination( tempString, pString, sizeof( tempString ));
pstr = pfront = tempString;

int componentsParsed = 0;

for( j = 0; j < 3; j++ ) // lifted from pr_edict.c
{
pVector[j] = atof( pfront );
componentsParsed++;

while( *pstr && *pstr != ' ' )
pstr++;
if( !( *pstr ) )
break;
pstr++;
pfront = pstr;
}
if (componentsRead)
*componentsRead = componentsParsed;
if( j < 2 )
{
/*
ALERT( at_error, "Bad field in entity!! %s:%s == \"%s\"\n",
pkvd->szClassName, pkvd->szKeyName, pkvd->szValue );
*/
for( j = j + 1;j < 3; j++ )
pVector[j] = 0;
}
}
2 changes: 2 additions & 0 deletions game_shared/util_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ extern float UTIL_Approach( float target, float value, float speed );
extern float UTIL_ApproachAngle( float target, float value, float speed );
extern float UTIL_AngleDistance( float next, float cur );

extern void UTIL_StringToVector( float *pVector, const char *pString, int* componentsRead = nullptr );

#endif

0 comments on commit 5258690

Please sign in to comment.