Skip to content

Commit

Permalink
Allow game_player_settings set armor to 0, add or substruct armor. Al…
Browse files Browse the repository at this point in the history
…low TakeArmor accept negative values
  • Loading branch information
FreeSlave committed Jun 11, 2024
1 parent 033b435 commit 3c5fb47
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cl_dll/hl/hl_baseentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ bool CBaseMonster::HandleDoorBlockage(CBaseEntity* pDoor) { return false; }

void CBasePlayer::DeathSound( void ) { }
int CBasePlayer::TakeHealth( CBaseEntity* pHealer, float flHealth, int bitsDamageType ) { return 0; }
int CBasePlayer::TakeArmor(CBaseEntity *pCharger, float flArmor) { return 0; }
int CBasePlayer::TakeArmor(CBaseEntity *pCharger, float flArmor, int flags) { return 0; }
void CBasePlayer::TraceAttack( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) { }
int CBasePlayer::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) { return 0; }
void CBasePlayer::SetAnimation( PLAYER_ANIM playerAnim ) { }
Expand Down
10 changes: 7 additions & 3 deletions dlls/cbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,20 @@ void DispatchKeyValue( edict_t *pentKeyvalue, KeyValueData *pkvd )
if( !pkvd || !pentKeyvalue )
return;

// Get the actualy entity object
CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE( pentKeyvalue );
if (pEntity && pkvd->szClassName)
pEntity->PreEntvarsKeyvalue(pkvd);
if (pkvd->fHandled)
return;

EntvarsKeyvalue( VARS( pentKeyvalue ), pkvd );

// If the key was an entity variable, or there's no class set yet, don't look for the object, it may
// not exist yet.
if ( pkvd->fHandled || pkvd->szClassName == NULL )
return;

// Get the actualy entity object
CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE( pentKeyvalue );

if( !pEntity )
return;

Expand Down
3 changes: 2 additions & 1 deletion dlls/cbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class CBaseEntity
virtual void Spawn( void ) { return; }
virtual void Precache( void ) { return; }
virtual bool IsEnabledInMod() { return true; }
virtual void PreEntvarsKeyvalue( KeyValueData* pkvd ) { pkvd->fHandled = FALSE; }
virtual void KeyValue( KeyValueData* pkvd );
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
Expand All @@ -232,7 +233,7 @@ class CBaseEntity
void ApplyTraceAttack( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType );
virtual int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType );
virtual int TakeHealth( CBaseEntity* pHealer, float flHealth, int bitsDamageType );
virtual int TakeArmor( CBaseEntity* pCharger, float flArmor ) { return 0; }
virtual int TakeArmor( CBaseEntity* pCharger, float flArmor, int flags = 0 ) { return 0; }
virtual void Killed( entvars_t *pevInflictor, entvars_t *pevAttacker, int iGib );
virtual int BloodColor( void ) { return DONT_BLEED; }
virtual void TraceBleed( float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType );
Expand Down
59 changes: 57 additions & 2 deletions dlls/maprules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,14 @@ void CGamePlayerTeam::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TY
#define SF_PLAYER_SETTINGS_SPORELAUNCHER (1 << 20)
#define SF_PLAYER_SETTINGS_LONGJUMP (1 << 23)

enum
{
ARMOR_SETTING_DEFAULT = 0,
ARMOR_SETTING_SET,
ARMOR_SETTING_ADD,
ARMOR_SETTING_SUBSTRUCT,
};

class CGamePlayerSettings : public CRulePointEntity
{
public:
Expand All @@ -940,6 +948,7 @@ class CGamePlayerSettings : public CRulePointEntity
SUIT_LIGHT_NVG = 2,
};

void PreEntvarsKeyvalue( KeyValueData* pkvd );
void KeyValue( KeyValueData *pkvd );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
Expand Down Expand Up @@ -968,6 +977,7 @@ class CGamePlayerSettings : public CRulePointEntity
BOOL m_allowOverheal;
BOOL m_allowOvercharge;
float m_armorStrength;
short m_armorSetting;
};

LINK_ENTITY_TO_CLASS( game_player_settings, CGamePlayerSettings )
Expand All @@ -980,10 +990,38 @@ TYPEDESCRIPTION CGamePlayerSettings::m_SaveData[] =
DEFINE_FIELD( CGamePlayerSettings, m_allowOverheal, FIELD_BOOLEAN ),
DEFINE_FIELD( CGamePlayerSettings, m_allowOvercharge, FIELD_BOOLEAN ),
DEFINE_FIELD( CGamePlayerSettings, m_armorStrength, FIELD_FLOAT ),
DEFINE_FIELD( CGamePlayerSettings, m_armorSetting, FIELD_SHORT ),
};

IMPLEMENT_SAVERESTORE( CGamePlayerSettings, CRulePointEntity )

void CGamePlayerSettings::PreEntvarsKeyvalue( KeyValueData* pkvd )
{
if (FStrEq(pkvd->szKeyName, "armorvalue"))
{
const char* value = pkvd->szValue;
if (*value == '=')
{
m_armorSetting = ARMOR_SETTING_SET;
++value;
}
else if (*value == '+')
{
m_armorSetting = ARMOR_SETTING_ADD;
++value;
}
else if (*value == '-')
{
m_armorSetting = ARMOR_SETTING_SUBSTRUCT;
++value;
}
pev->armorvalue = atof(value);
pkvd->fHandled = TRUE;
}
else
CRulePointEntity::PreEntvarsKeyvalue(pkvd);
}

void CGamePlayerSettings::KeyValue(KeyValueData *pkvd)
{
const char* ammoName = pkvd->szKeyName;
Expand Down Expand Up @@ -1051,10 +1089,27 @@ void CGamePlayerSettings::EquipPlayer(CBaseEntity *pPlayer)
{
player->SetHealth((int)pev->health, m_allowOverheal);
}
if (pev->armorvalue > 0)

if (m_armorSetting == ARMOR_SETTING_SET)
{
player->SetArmor(pev->armorvalue, m_allowOvercharge);
}
else if (m_armorSetting == ARMOR_SETTING_ADD)
{
player->TakeArmor(this, pev->armorvalue, m_allowOvercharge ? GIVEARMOR_ALLOW_OVERFLOW : GIVEARMOR_GENERIC);
}
else if (m_armorSetting == ARMOR_SETTING_SUBSTRUCT)
{
player->SetArmor((int)pev->armorvalue, m_allowOvercharge);
player->TakeArmor(this, -pev->armorvalue, m_allowOvercharge ? GIVEARMOR_ALLOW_OVERFLOW : GIVEARMOR_GENERIC);
}
else
{
if (pev->armorvalue > 0)
{
player->SetArmor(pev->armorvalue, m_allowOvercharge);
}
}

if (m_armorStrength > 0)
{
player->m_armorStrength = m_armorStrength;
Expand Down
16 changes: 13 additions & 3 deletions dlls/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,20 @@ void CBasePlayer::SetMaxHealth(int maxHealth, bool clampValue)
}
}

int CBasePlayer::TakeArmor(CBaseEntity *pCharger, float flArmor)
int CBasePlayer::TakeArmor(CBaseEntity *pCharger, float flArmor, int flags)
{
if (pev->armorvalue >= MaxArmor())
if (!flArmor)
return false;
const int maxArmor = MaxArmor();
const bool allowOvercharge = FBitSet(flags, GIVEARMOR_ALLOW_OVERFLOW);

if (flArmor > 0 && pev->armorvalue >= maxArmor && !allowOvercharge)
return false;
pev->armorvalue += flArmor;
pev->armorvalue = Q_min( pev->armorvalue, MaxArmor() );
if (flArmor > 0 && !allowOvercharge)
{
pev->armorvalue = Q_min( pev->armorvalue, maxArmor );
}
if (pev->armorvalue < 0)
pev->armorvalue = 0;
return true;
Expand Down Expand Up @@ -521,6 +529,8 @@ void CBasePlayer::SetArmor(int armor, bool allowOvercharge)
{
pev->armorvalue = maxArmor;
}
if (pev->armorvalue < 0)
pev->armorvalue = 0;
}

float CBasePlayer::ArmorStrength()
Expand Down
2 changes: 1 addition & 1 deletion dlls/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class CBasePlayer : public CBaseMonster
virtual int TakeHealth(CBaseEntity *pHealer, float flHealth, int bitsDamageType );
void SetHealth(int health, bool allowOverheal = false);
void SetMaxHealth(int maxHealth, bool clampValue = true);
virtual int TakeArmor(CBaseEntity *pCharger, float flArmor);
virtual int TakeArmor(CBaseEntity *pCharger, float flArmor, int flags = 0);
int MaxArmor();
void SetMaxArmor(int maxArmor, bool clampValue = true);
void SetArmor(int armor, bool allowOvercharge = false);
Expand Down
6 changes: 1 addition & 5 deletions dlls/triggers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3087,11 +3087,7 @@ void CTriggerChangeValue::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, US
mypkvd.szValue = STRING(m_iszNewValue);
mypkvd.fHandled = FALSE;

EntvarsKeyvalue( pTarget->pev, &mypkvd );
if ( mypkvd.fHandled )
return;

pTarget->KeyValue(&mypkvd);
DispatchKeyValue(pTarget->edict(), &mypkvd);
}

if (!FStringNull(pev->message))
Expand Down
2 changes: 1 addition & 1 deletion fgd/halflife.fgd
Original file line number Diff line number Diff line change
Expand Up @@ -2911,7 +2911,7 @@
1: "Yes"
]
max_health(integer): "Set max health"
armorvalue(integer) : "Set armor"
armorvalue(integer) : "Set armor" : : "Prefix the value with = to set the armor amount (this allows to reset the armor to 0). The '+' prefix means to add armor. The '-' prefix means to substruct armor. If no prefix is given, the specified amount if it's not 0."
allow_overcharge(choices) : "Allow armor OverCharge" : : "Allow armor overcharge if new armor is higher than the current max armor. Don't clamp armor value to the max armor if the new max armor is lower than the current armor." =
[
0: "No"
Expand Down
3 changes: 3 additions & 0 deletions game_shared/dmg_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@
#define HEAL_GENERIC 0
#define HEAL_CHARGE (1<<0) // Charge my portable medkit

#define GIVEARMOR_GENERIC 0
#define GIVEARMOR_ALLOW_OVERFLOW (1<<1)

#endif

0 comments on commit 3c5fb47

Please sign in to comment.