Skip to content

Commit 368796a

Browse files
Clientside vscript additions:
Added C_BaseCombatCharacter script desc Added boundary checks for script funcs
1 parent 3a97969 commit 368796a

File tree

5 files changed

+110
-38
lines changed

5 files changed

+110
-38
lines changed

sp/src/game/client/c_basecombatcharacter.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,39 @@ BEGIN_PREDICTION_DATA( C_BaseCombatCharacter )
178178
DEFINE_PRED_ARRAY( m_hMyWeapons, FIELD_EHANDLE, MAX_WEAPONS, FTYPEDESC_INSENDTABLE ),
179179

180180
END_PREDICTION_DATA()
181+
182+
#ifdef MAPBASE_VSCRIPT
183+
184+
BEGIN_ENT_SCRIPTDESC( C_BaseCombatCharacter, CBaseEntity, "" )
185+
DEFINE_SCRIPTFUNC_NAMED( ScriptGetAmmoCount, "GetAmmoCount", "" )
186+
DEFINE_SCRIPTFUNC_NAMED( ScriptGetActiveWeapon, "GetActiveWeapon", "" )
187+
DEFINE_SCRIPTFUNC_NAMED( ScriptGetWeapon, "GetWeapon", "" )
188+
END_SCRIPTDESC();
189+
190+
191+
int C_BaseCombatCharacter::ScriptGetAmmoCount( int i )
192+
{
193+
Assert( i == -1 || i < MAX_AMMO_SLOTS );
194+
195+
if ( i < 0 || i >= MAX_AMMO_SLOTS )
196+
return NULL;
197+
198+
return GetAmmoCount( i );
199+
}
200+
201+
HSCRIPT C_BaseCombatCharacter::ScriptGetActiveWeapon()
202+
{
203+
return ToHScript( GetActiveWeapon() );
204+
}
205+
206+
HSCRIPT C_BaseCombatCharacter::ScriptGetWeapon( int i )
207+
{
208+
Assert( i >= 0 && i < MAX_WEAPONS );
209+
210+
if ( i < 0 || i >= MAX_WEAPONS )
211+
return NULL;
212+
213+
return ToHScript( GetWeapon(i) );
214+
}
215+
216+
#endif

sp/src/game/client/c_basecombatcharacter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class C_BaseCombatCharacter : public C_BaseFlex
2929
public:
3030
DECLARE_CLIENTCLASS();
3131
DECLARE_PREDICTABLE();
32+
#ifdef MAPBASE_VSCRIPT
33+
DECLARE_ENT_SCRIPTDESC();
34+
#endif
3235

3336
C_BaseCombatCharacter( void );
3437
virtual ~C_BaseCombatCharacter( void );
@@ -99,6 +102,12 @@ class C_BaseCombatCharacter : public C_BaseFlex
99102
virtual void GetGlowEffectColor( float *r, float *g, float *b );
100103
#endif // GLOWS_ENABLE
101104

105+
#ifdef MAPBASE_VSCRIPT
106+
int ScriptGetAmmoCount( int i );
107+
HSCRIPT ScriptGetActiveWeapon();
108+
HSCRIPT ScriptGetWeapon( int i );
109+
#endif
110+
102111
public:
103112

104113
float m_flNextAttack;

sp/src/game/server/basecombatcharacter.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ ScriptHook_t CBaseCombatCharacter::g_Hook_RelationshipPriority;
151151

152152
BEGIN_ENT_SCRIPTDESC( CBaseCombatCharacter, CBaseFlex, "The base class shared by players and NPCs." )
153153

154-
DEFINE_SCRIPTFUNC_NAMED( GetScriptActiveWeapon, "GetActiveWeapon", "Get the character's active weapon entity." )
154+
DEFINE_SCRIPTFUNC_NAMED( ScriptGetActiveWeapon, "GetActiveWeapon", "Get the character's active weapon entity." )
155155
DEFINE_SCRIPTFUNC( WeaponCount, "Get the number of weapons a character possesses." )
156-
DEFINE_SCRIPTFUNC_NAMED( GetScriptWeaponIndex, "GetWeapon", "Get a specific weapon in the character's inventory." )
157-
DEFINE_SCRIPTFUNC_NAMED( GetScriptWeaponByType, "FindWeapon", "Find a specific weapon in the character's inventory by its classname." )
158-
DEFINE_SCRIPTFUNC_NAMED( GetScriptAllWeapons, "GetAllWeapons", "Get the character's weapon inventory." )
156+
DEFINE_SCRIPTFUNC_NAMED( ScriptGetWeapon, "GetWeapon", "Get a specific weapon in the character's inventory." )
157+
DEFINE_SCRIPTFUNC_NAMED( ScriptGetWeaponByType, "FindWeapon", "Find a specific weapon in the character's inventory by its classname." )
158+
DEFINE_SCRIPTFUNC_NAMED( ScriptGetAllWeapons, "GetAllWeapons", "Get the character's weapon inventory." )
159159
DEFINE_SCRIPTFUNC_NAMED( ScriptGetCurrentWeaponProficiency, "GetCurrentWeaponProficiency", "Get the character's current proficiency (accuracy) with their current weapon." )
160160

161161
DEFINE_SCRIPTFUNC_NAMED( Weapon_ShootPosition, "ShootPosition", "Get the character's shoot position." )
@@ -174,7 +174,7 @@ BEGIN_ENT_SCRIPTDESC( CBaseCombatCharacter, CBaseFlex, "The base class shared by
174174
DEFINE_SCRIPTFUNC_NAMED( ScriptRelationPriority, "GetRelationPriority", "Get a character's relationship priority for a specific entity." )
175175
DEFINE_SCRIPTFUNC_NAMED( ScriptSetRelationship, "SetRelationship", "Set a character's relationship with a specific entity." )
176176

177-
DEFINE_SCRIPTFUNC_NAMED( GetScriptVehicleEntity, "GetVehicleEntity", "Get the entity for a character's current vehicle if they're in one." )
177+
DEFINE_SCRIPTFUNC_NAMED( ScriptGetVehicleEntity, "GetVehicleEntity", "Get the entity for a character's current vehicle if they're in one." )
178178

179179
DEFINE_SCRIPTFUNC_NAMED( ScriptInViewCone, "InViewCone", "Check if the specified position is in the character's viewcone." )
180180
DEFINE_SCRIPTFUNC_NAMED( ScriptEntInViewCone, "EntInViewCone", "Check if the specified entity is in the character's viewcone." )
@@ -4430,28 +4430,33 @@ void CBaseCombatCharacter::DoMuzzleFlash()
44304430
#ifdef MAPBASE_VSCRIPT
44314431
//-----------------------------------------------------------------------------
44324432
//-----------------------------------------------------------------------------
4433-
HSCRIPT CBaseCombatCharacter::GetScriptActiveWeapon()
4433+
HSCRIPT CBaseCombatCharacter::ScriptGetActiveWeapon()
44344434
{
44354435
return ToHScript( GetActiveWeapon() );
44364436
}
44374437

44384438
//-----------------------------------------------------------------------------
44394439
//-----------------------------------------------------------------------------
4440-
HSCRIPT CBaseCombatCharacter::GetScriptWeaponIndex( int i )
4440+
HSCRIPT CBaseCombatCharacter::ScriptGetWeapon( int i )
44414441
{
4442+
Assert( i >= 0 && i < MAX_WEAPONS );
4443+
4444+
if ( i < 0 || i >= MAX_WEAPONS )
4445+
return NULL;
4446+
44424447
return ToHScript( GetWeapon( i ) );
44434448
}
44444449

44454450
//-----------------------------------------------------------------------------
44464451
//-----------------------------------------------------------------------------
4447-
HSCRIPT CBaseCombatCharacter::GetScriptWeaponByType( const char *pszWeapon, int iSubType )
4452+
HSCRIPT CBaseCombatCharacter::ScriptGetWeaponByType( const char *pszWeapon, int iSubType )
44484453
{
44494454
return ToHScript( Weapon_OwnsThisType( pszWeapon, iSubType ) );
44504455
}
44514456

44524457
//-----------------------------------------------------------------------------
44534458
//-----------------------------------------------------------------------------
4454-
void CBaseCombatCharacter::GetScriptAllWeapons( HSCRIPT hTable )
4459+
void CBaseCombatCharacter::ScriptGetAllWeapons( HSCRIPT hTable )
44554460
{
44564461
for (int i=0;i<MAX_WEAPONS;i++)
44574462
{
@@ -4520,18 +4525,22 @@ void CBaseCombatCharacter::ScriptEquipWeapon( HSCRIPT hWeapon )
45204525
//-----------------------------------------------------------------------------
45214526
int CBaseCombatCharacter::ScriptGetAmmoCount( int iType ) const
45224527
{
4528+
Assert( iType == -1 || iType < MAX_AMMO_SLOTS );
4529+
4530+
if ( iType < 0 || iType >= MAX_AMMO_SLOTS )
4531+
return 0;
4532+
45234533
return GetAmmoCount( iType );
45244534
}
45254535

45264536
//-----------------------------------------------------------------------------
45274537
//-----------------------------------------------------------------------------
45284538
void CBaseCombatCharacter::ScriptSetAmmoCount( int iType, int iCount )
45294539
{
4530-
if (iType == -1)
4531-
{
4532-
Warning("%i is not a valid ammo type\n", iType);
4540+
Assert( iType == -1 || iType < MAX_AMMO_SLOTS );
4541+
4542+
if ( iType < 0 || iType >= MAX_AMMO_SLOTS )
45334543
return;
4534-
}
45354544

45364545
return SetAmmoCount( iCount, iType );
45374546
}
@@ -4590,7 +4599,7 @@ void CBaseCombatCharacter::ScriptSetRelationship( HSCRIPT pTarget, int dispositi
45904599

45914600
//-----------------------------------------------------------------------------
45924601
//-----------------------------------------------------------------------------
4593-
HSCRIPT CBaseCombatCharacter::GetScriptVehicleEntity()
4602+
HSCRIPT CBaseCombatCharacter::ScriptGetVehicleEntity()
45944603
{
45954604
return ToHScript( GetVehicleEntity() );
45964605
}

sp/src/game/server/basecombatcharacter.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,10 @@ class CBaseCombatCharacter : public CBaseFlex
420420
virtual void DoMuzzleFlash();
421421

422422
#ifdef MAPBASE_VSCRIPT
423-
HSCRIPT GetScriptActiveWeapon();
424-
HSCRIPT GetScriptWeaponIndex( int i );
425-
HSCRIPT GetScriptWeaponByType( const char *pszWeapon, int iSubType = 0 );
426-
void GetScriptAllWeapons( HSCRIPT hTable );
423+
HSCRIPT ScriptGetActiveWeapon();
424+
HSCRIPT ScriptGetWeapon( int i );
425+
HSCRIPT ScriptGetWeaponByType( const char *pszWeapon, int iSubType = 0 );
426+
void ScriptGetAllWeapons( HSCRIPT hTable );
427427
int ScriptGetCurrentWeaponProficiency() { return GetCurrentWeaponProficiency(); }
428428

429429
void ScriptDropWeapon( HSCRIPT hWeapon );
@@ -439,7 +439,7 @@ class CBaseCombatCharacter : public CBaseFlex
439439
int ScriptRelationPriority( HSCRIPT pTarget );
440440
void ScriptSetRelationship( HSCRIPT pTarget, int disposition, int priority );
441441

442-
HSCRIPT GetScriptVehicleEntity();
442+
HSCRIPT ScriptGetVehicleEntity();
443443

444444
bool ScriptInViewCone( const Vector &vecSpot ) { return FInViewCone( vecSpot ); }
445445
bool ScriptEntInViewCone( HSCRIPT pEntity ) { return FInViewCone( ToEnt( pEntity ) ); }

sp/src/game/shared/basecombatweapon_shared.cpp

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,15 +2949,33 @@ END_PREDICTION_DATA()
29492949
IMPLEMENT_NETWORKCLASS_ALIASED( BaseCombatWeapon, DT_BaseCombatWeapon )
29502950

29512951
#ifdef MAPBASE_VSCRIPT
2952+
2953+
// Don't allow client to use Set functions.
2954+
// They will only cause visual discrepancies,
2955+
// and will be reverted on the next update from the server.
2956+
#ifdef GAME_DLL
2957+
#define DEFINE_SCRIPTFUNC_SV( p1, p2 ) DEFINE_SCRIPTFUNC( p1, p2 )
2958+
#define DEFINE_SCRIPTFUNC_NAMED_SV( p1, p2, p3 ) DEFINE_SCRIPTFUNC_NAMED( p1, p2, p3 )
2959+
2960+
#define DEFINE_SCRIPTFUNC_CL( p1, p2 )
2961+
#define DEFINE_SCRIPTFUNC_NAMED_CL( p1, p2, p3 )
2962+
#else
2963+
#define DEFINE_SCRIPTFUNC_SV( p1, p2 )
2964+
#define DEFINE_SCRIPTFUNC_NAMED_SV( p1, p2, p3 )
2965+
2966+
#define DEFINE_SCRIPTFUNC_CL( p1, p2 ) DEFINE_SCRIPTFUNC( p1, p2 )
2967+
#define DEFINE_SCRIPTFUNC_NAMED_CL( p1, p2, p3 ) DEFINE_SCRIPTFUNC_NAMED( p1, p2, p3 )
2968+
#endif
2969+
29522970
BEGIN_ENT_SCRIPTDESC( CBaseCombatWeapon, CBaseAnimating, "The base class for all equippable weapons." )
29532971

29542972
DEFINE_SCRIPTFUNC_NAMED( ScriptGetOwner, "GetOwner", "Get the weapon's owner." )
2955-
DEFINE_SCRIPTFUNC_NAMED( ScriptSetOwner, "SetOwner", "Set the weapon's owner." )
2973+
DEFINE_SCRIPTFUNC_NAMED_SV( ScriptSetOwner, "SetOwner", "Set the weapon's owner." )
29562974

29572975
DEFINE_SCRIPTFUNC( Clip1, "Get the weapon's current primary ammo." )
29582976
DEFINE_SCRIPTFUNC( Clip2, "Get the weapon's current secondary ammo." )
2959-
DEFINE_SCRIPTFUNC_NAMED( ScriptSetClip1, "SetClip1", "Set the weapon's current primary ammo." )
2960-
DEFINE_SCRIPTFUNC_NAMED( ScriptSetClip2, "SetClip2", "Set the weapon's current secondary ammo." )
2977+
DEFINE_SCRIPTFUNC_NAMED_SV( ScriptSetClip1, "SetClip1", "Set the weapon's current primary ammo." )
2978+
DEFINE_SCRIPTFUNC_NAMED_SV( ScriptSetClip2, "SetClip2", "Set the weapon's current secondary ammo." )
29612979
DEFINE_SCRIPTFUNC( GetMaxClip1, "Get the weapon's maximum primary ammo." )
29622980
DEFINE_SCRIPTFUNC( GetMaxClip2, "Get the weapon's maximum secondary ammo." )
29632981
DEFINE_SCRIPTFUNC( GetDefaultClip1, "Get the weapon's default primary ammo." )
@@ -2968,18 +2986,16 @@ BEGIN_ENT_SCRIPTDESC( CBaseCombatWeapon, CBaseAnimating, "The base class for all
29682986
DEFINE_SCRIPTFUNC( HasSecondaryAmmo, "Check if the weapon currently has ammo or doesn't need secondary ammo." )
29692987
DEFINE_SCRIPTFUNC( UsesPrimaryAmmo, "Check if the weapon uses primary ammo." )
29702988
DEFINE_SCRIPTFUNC( UsesSecondaryAmmo, "Check if the weapon uses secondary ammo." )
2971-
DEFINE_SCRIPTFUNC( GiveDefaultAmmo, "Fill the weapon back up to default ammo." )
2989+
DEFINE_SCRIPTFUNC_SV( GiveDefaultAmmo, "Fill the weapon back up to default ammo." )
29722990

29732991
DEFINE_SCRIPTFUNC( UsesClipsForAmmo1, "Check if the weapon uses clips for primary ammo." )
29742992
DEFINE_SCRIPTFUNC( UsesClipsForAmmo2, "Check if the weapon uses clips for secondary ammo." )
29752993

2976-
#ifndef CLIENT_DLL
29772994
DEFINE_SCRIPTFUNC( GetPrimaryAmmoType, "Get the weapon's primary ammo type." )
29782995
DEFINE_SCRIPTFUNC( GetSecondaryAmmoType, "Get the weapon's secondary ammo type." )
2979-
#endif
29802996

29812997
DEFINE_SCRIPTFUNC( GetSubType, "Get the weapon's subtype." )
2982-
DEFINE_SCRIPTFUNC( SetSubType, "Set the weapon's subtype." )
2998+
DEFINE_SCRIPTFUNC_SV( SetSubType, "Set the weapon's subtype." )
29832999

29843000
DEFINE_SCRIPTFUNC( GetFireRate, "Get the weapon's firing rate." )
29853001
DEFINE_SCRIPTFUNC( AddViewKick, "Applies the weapon's view kick." )
@@ -2988,16 +3004,18 @@ BEGIN_ENT_SCRIPTDESC( CBaseCombatWeapon, CBaseAnimating, "The base class for all
29883004
DEFINE_SCRIPTFUNC( GetViewModel, "Get the weapon's view model." )
29893005

29903006
DEFINE_SCRIPTFUNC( GetWeight, "Get the weapon's weight." )
3007+
DEFINE_SCRIPTFUNC( GetPrintName, "" )
3008+
3009+
DEFINE_SCRIPTFUNC_CL( GetSlot, "" )
3010+
DEFINE_SCRIPTFUNC_CL( GetPosition, "" )
29913011

29923012
DEFINE_SCRIPTFUNC( CanBePickedUpByNPCs, "Check if the weapon can be picked up by NPCs." )
29933013

2994-
#ifndef CLIENT_DLL
2995-
DEFINE_SCRIPTFUNC( CapabilitiesGet, "Get the capabilities the weapon currently possesses." )
2996-
#endif
3014+
DEFINE_SCRIPTFUNC_SV( CapabilitiesGet, "Get the capabilities the weapon currently possesses." )
29973015

29983016
DEFINE_SCRIPTFUNC( HasWeaponIdleTimeElapsed, "Returns true if the idle time has elapsed." )
29993017
DEFINE_SCRIPTFUNC( GetWeaponIdleTime, "Returns the next time WeaponIdle() will run." )
3000-
DEFINE_SCRIPTFUNC( SetWeaponIdleTime, "Sets the next time WeaponIdle() will run." )
3018+
DEFINE_SCRIPTFUNC_SV( SetWeaponIdleTime, "Sets the next time WeaponIdle() will run." )
30013019

30023020
DEFINE_SCRIPTFUNC_NAMED( ScriptWeaponClassify, "WeaponClassify", "Returns the weapon's classify class from the WEPCLASS_ constant group" )
30033021
DEFINE_SCRIPTFUNC_NAMED( ScriptWeaponSound, "WeaponSound", "Plays one of the weapon's sounds." )
@@ -3014,22 +3032,22 @@ BEGIN_ENT_SCRIPTDESC( CBaseCombatWeapon, CBaseAnimating, "The base class for all
30143032
DEFINE_SCRIPTFUNC( IsViewModelSequenceFinished, "Returns true if the current view model animation is finished." )
30153033

30163034
DEFINE_SCRIPTFUNC( FiresUnderwater, "Returns true if this weapon can fire underwater." )
3017-
DEFINE_SCRIPTFUNC( SetFiresUnderwater, "Sets whether this weapon can fire underwater." )
3035+
DEFINE_SCRIPTFUNC_SV( SetFiresUnderwater, "Sets whether this weapon can fire underwater." )
30183036
DEFINE_SCRIPTFUNC( AltFiresUnderwater, "Returns true if this weapon can alt-fire underwater." )
3019-
DEFINE_SCRIPTFUNC( SetAltFiresUnderwater, "Sets whether this weapon can alt-fire underwater." )
3037+
DEFINE_SCRIPTFUNC_SV( SetAltFiresUnderwater, "Sets whether this weapon can alt-fire underwater." )
30203038
DEFINE_SCRIPTFUNC( MinRange1, "Returns the closest this weapon can be used." )
3021-
DEFINE_SCRIPTFUNC( SetMinRange1, "Sets the closest this weapon can be used." )
3039+
DEFINE_SCRIPTFUNC_SV( SetMinRange1, "Sets the closest this weapon can be used." )
30223040
DEFINE_SCRIPTFUNC( MinRange2, "Returns the closest this weapon can be used." )
3023-
DEFINE_SCRIPTFUNC( SetMinRange2, "Sets the closest this weapon can be used." )
3041+
DEFINE_SCRIPTFUNC_SV( SetMinRange2, "Sets the closest this weapon can be used." )
30243042
DEFINE_SCRIPTFUNC( ReloadsSingly, "Returns true if this weapon reloads 1 round at a time." )
3025-
DEFINE_SCRIPTFUNC( SetReloadsSingly, "Sets whether this weapon reloads 1 round at a time." )
3043+
DEFINE_SCRIPTFUNC_SV( SetReloadsSingly, "Sets whether this weapon reloads 1 round at a time." )
30263044
DEFINE_SCRIPTFUNC( FireDuration, "Returns the amount of time that the weapon has sustained firing." )
3027-
DEFINE_SCRIPTFUNC( SetFireDuration, "Sets the amount of time that the weapon has sustained firing." )
3045+
DEFINE_SCRIPTFUNC_SV( SetFireDuration, "Sets the amount of time that the weapon has sustained firing." )
30283046

30293047
DEFINE_SCRIPTFUNC( NextPrimaryAttack, "Returns the next time PrimaryAttack() will run when the player is pressing +ATTACK." )
3030-
DEFINE_SCRIPTFUNC( SetNextPrimaryAttack, "Sets the next time PrimaryAttack() will run when the player is pressing +ATTACK." )
3048+
DEFINE_SCRIPTFUNC_SV( SetNextPrimaryAttack, "Sets the next time PrimaryAttack() will run when the player is pressing +ATTACK." )
30313049
DEFINE_SCRIPTFUNC( NextSecondaryAttack, "Returns the next time SecondaryAttack() will run when the player is pressing +ATTACK2." )
3032-
DEFINE_SCRIPTFUNC( SetNextSecondaryAttack, "Sets the next time SecondaryAttack() will run when the player is pressing +ATTACK2." )
3050+
DEFINE_SCRIPTFUNC_SV( SetNextSecondaryAttack, "Sets the next time SecondaryAttack() will run when the player is pressing +ATTACK2." )
30333051

30343052
END_SCRIPTDESC();
30353053
#endif

0 commit comments

Comments
 (0)