Skip to content

Commit 583e675

Browse files
authored
Fixes #3982: armor limit and synchronization between client and server (#3983)
Fix issue #3982
1 parent a02e7a9 commit 583e675

File tree

17 files changed

+100
-145
lines changed

17 files changed

+100
-145
lines changed

Client/game_sa/CPedSA.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,14 @@ void CPedSA::SetHealth(float fHealth)
259259
GetPedInterface()->fHealth = fHealth;
260260
}
261261

262-
float CPedSA::GetArmor()
262+
float CPedSA::GetArmor() noexcept
263263
{
264264
return GetPedInterface()->fArmor;
265265
}
266266

267-
void CPedSA::SetArmor(float fArmor)
267+
void CPedSA::SetArmor(float armor) noexcept
268268
{
269-
GetPedInterface()->fArmor = fArmor;
269+
GetPedInterface()->fArmor = armor;
270270
}
271271

272272
float CPedSA::GetOxygenLevel()

Client/game_sa/CPedSA.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ class CPedSA : public virtual CPed, public virtual CPhysicalSA
313313
float GetHealth();
314314
void SetHealth(float fHealth);
315315

316-
float GetArmor();
317-
void SetArmor(float fArmor);
316+
float GetArmor() noexcept;
317+
void SetArmor(float armor) noexcept;
318318

319319
float GetOxygenLevel();
320320
void SetOxygenLevel(float fOxygen);

Client/mods/deathmatch/logic/CClientGame.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4371,7 +4371,7 @@ bool CClientGame::ApplyPedDamageFromGame(eWeaponType weaponUsed, float fDamage,
43714371
{
43724372
float fPreviousHealth = pDamagedPed->m_fHealth;
43734373
float fCurrentHealth = pDamagedPed->GetGamePlayer()->GetHealth();
4374-
float fPreviousArmor = pDamagedPed->m_fArmor;
4374+
float fPreviousArmor = pDamagedPed->m_armor;
43754375
float fCurrentArmor = pDamagedPed->GetGamePlayer()->GetArmor();
43764376

43774377
// Have we taken any damage here?
@@ -4407,7 +4407,7 @@ bool CClientGame::ApplyPedDamageFromGame(eWeaponType weaponUsed, float fDamage,
44074407
{
44084408
// Reget values in case they have been changed during onClientPlayerDamage event (Avoid AC#1 kick)
44094409
fPreviousHealth = pDamagedPed->m_fHealth;
4410-
fPreviousArmor = pDamagedPed->m_fArmor;
4410+
fPreviousArmor = pDamagedPed->m_armor;
44114411
}
44124412
pDamagedPed->GetGamePlayer()->SetHealth(fPreviousHealth);
44134413
pDamagedPed->GetGamePlayer()->SetArmor(fPreviousArmor);
@@ -4453,7 +4453,7 @@ bool CClientGame::ApplyPedDamageFromGame(eWeaponType weaponUsed, float fDamage,
44534453

44544454
// Update our stored health/armor
44554455
pDamagedPed->m_fHealth = fCurrentHealth;
4456-
pDamagedPed->m_fArmor = fCurrentArmor;
4456+
pDamagedPed->m_armor = fCurrentArmor;
44574457

44584458
ElementID damagerID = INVALID_ELEMENT_ID;
44594459
if (pInflictingEntity && !pInflictingEntity->IsLocalEntity())

Client/mods/deathmatch/logic/CClientPed.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void CClientPed::Init(CClientManager* pManager, unsigned long ulModelID, bool bI
129129
m_uiOccupiedVehicleSeat = 0xFF;
130130
m_bHealthLocked = false;
131131
m_bDontChangeRadio = false;
132-
m_bArmorLocked = false;
132+
m_armorLocked = false;
133133
m_ulLastOnScreenTime = 0;
134134
m_pLoadedModelInfo = NULL;
135135
m_pOutOfVehicleWeaponSlot = WEAPONSLOT_MAX; // WEAPONSLOT_MAX = invalid
@@ -157,7 +157,7 @@ void CClientPed::Init(CClientManager* pManager, unsigned long ulModelID, bool bI
157157
m_bVisible = true;
158158
m_bUsesCollision = true;
159159
m_fHealth = 100.0f;
160-
m_fArmor = 0.0f;
160+
m_armor = 0.0f;
161161
m_bDead = false;
162162
m_bWorldIgnored = false;
163163
m_fCurrentRotation = 0.0f;
@@ -1793,29 +1793,28 @@ void CClientPed::InternalSetHealth(float fHealth)
17931793
}
17941794
}
17951795

1796-
float CClientPed::GetArmor()
1796+
float CClientPed::GetArmor() const noexcept
17971797
{
1798-
if (m_bArmorLocked)
1799-
return m_fArmor;
1798+
if (m_armorLocked)
1799+
return m_armor;
18001800

18011801
if (m_pPlayerPed)
1802-
{
18031802
return m_pPlayerPed->GetArmor();
1804-
}
1805-
return m_fArmor;
1803+
1804+
return m_armor;
18061805
}
18071806

1808-
void CClientPed::SetArmor(float fArmor)
1807+
void CClientPed::SetArmor(float armor) noexcept
18091808
{
1810-
// If our armor is locked, dont allow any change
1811-
if (m_bArmorLocked)
1809+
if (m_armorLocked)
18121810
return;
18131811

1812+
armor = std::clamp(armor, 0.0f, 100.0f);
1813+
18141814
if (m_pPlayerPed)
1815-
{
1816-
m_pPlayerPed->SetArmor(fArmor);
1817-
}
1818-
m_fArmor = fArmor;
1815+
m_pPlayerPed->SetArmor(armor);
1816+
1817+
m_armor = armor;
18191818
}
18201819

18211820
void CClientPed::LockHealth(float fHealth)
@@ -1824,10 +1823,10 @@ void CClientPed::LockHealth(float fHealth)
18241823
m_fHealth = fHealth;
18251824
}
18261825

1827-
void CClientPed::LockArmor(float fArmor)
1826+
void CClientPed::LockArmor(float armor) noexcept
18281827
{
1829-
m_bArmorLocked = true;
1830-
m_fArmor = fArmor;
1828+
m_armorLocked = true;
1829+
m_armor = armor;
18311830
}
18321831

18331832
float CClientPed::GetOxygenLevel()
@@ -2772,9 +2771,9 @@ void CClientPed::StreamedInPulse(bool bDoStandardPulses)
27722771
}
27732772

27742773
// Is our armor locked?
2775-
if (m_bArmorLocked)
2774+
if (m_armorLocked)
27762775
{
2777-
m_pPlayerPed->SetArmor(m_fArmor);
2776+
m_pPlayerPed->SetArmor(m_armor);
27782777
}
27792778

27802779
// In a vehicle?
@@ -3623,7 +3622,7 @@ void CClientPed::_CreateModel()
36233622
m_pPlayerPed->SetVisible(m_bVisible);
36243623
m_pPlayerPed->SetUsesCollision(m_bUsesCollision);
36253624
m_pPlayerPed->SetHealth(m_fHealth);
3626-
m_pPlayerPed->SetArmor(m_fArmor);
3625+
m_pPlayerPed->SetArmor(m_armor);
36273626
m_pPlayerPed->SetLighting(m_fLighting);
36283627
WorldIgnore(m_bWorldIgnored);
36293628

@@ -4225,10 +4224,10 @@ bool CClientPed::PerformChecks()
42254224
// The player should not be able to gain any health/armor without us knowing..
42264225
// meaning all health/armor giving must go through SetHealth/SetArmor.
42274226
if ((m_fHealth > 0.0f && m_pPlayerPed->GetHealth() > m_fHealth + FLOAT_EPSILON) ||
4228-
(m_fArmor < 100.0f && m_pPlayerPed->GetArmor() > m_fArmor + FLOAT_EPSILON))
4227+
(m_armor < 100.0f && m_pPlayerPed->GetArmor() > m_armor + FLOAT_EPSILON))
42294228
{
42304229
g_pCore->GetConsole()->Printf("healthCheck: %f %f", m_pPlayerPed->GetHealth(), m_fHealth);
4231-
g_pCore->GetConsole()->Printf("armorCheck: %f %f", m_pPlayerPed->GetArmor(), m_fArmor);
4230+
g_pCore->GetConsole()->Printf("armorCheck: %f %f", m_pPlayerPed->GetArmor(), m_armor);
42324231
return false;
42334232
}
42344233
// Perform the checks in CGame

Client/mods/deathmatch/logic/CClientPed.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,17 @@ class CClientPed : public CClientStreamElement, public CAntiCheatModule
267267
float GetHealth();
268268
void SetHealth(float fHealth);
269269
void InternalSetHealth(float fHealth);
270-
float GetArmor();
271-
void SetArmor(float fArmor);
270+
float GetArmor() const noexcept;
271+
void SetArmor(float armor) noexcept;
272272
float GetOxygenLevel();
273273
void SetOxygenLevel(float fOxygen);
274274

275275
void LockHealth(float fHealth);
276-
void LockArmor(float fArmor);
276+
void LockArmor(float armor) noexcept;
277277
void UnlockHealth() noexcept { m_bHealthLocked = false; };
278-
void UnlockArmor() noexcept { m_bArmorLocked = false; };
278+
void UnlockArmor() noexcept { m_armorLocked = false; };
279279
bool IsHealthLocked() const noexcept { return m_bHealthLocked; };
280-
bool IsArmorLocked() const noexcept { return m_bArmorLocked; };
280+
bool IsArmorLocked() const noexcept { return m_armorLocked; };
281281

282282
bool IsDying();
283283
bool IsDead();
@@ -619,7 +619,7 @@ class CClientPed : public CClientStreamElement, public CAntiCheatModule
619619
bool m_bRadioOn;
620620
unsigned char m_ucRadioChannel;
621621
bool m_bHealthLocked;
622-
bool m_bArmorLocked;
622+
bool m_armorLocked;
623623
unsigned long m_ulLastOnScreenTime;
624624
CClientVehiclePtr m_pOccupiedVehicle;
625625
CClientVehiclePtr m_pOccupyingVehicle;
@@ -678,7 +678,7 @@ class CClientPed : public CClientStreamElement, public CAntiCheatModule
678678
bool m_bVisible;
679679
bool m_bUsesCollision;
680680
float m_fHealth;
681-
float m_fArmor;
681+
float m_armor;
682682
bool m_bDead;
683683
bool m_bWorldIgnored;
684684
float m_fCurrentRotation;

Client/mods/deathmatch/logic/CClientPlayer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,13 @@ void CClientPlayer::DischargeWeapon(eWeaponType weaponType, const CVector& vecSt
311311
// CPlayerPed has post damage health/armor
312312

313313
float fPreviousHealth = pBackupDamagedPlayer->m_fHealth;
314-
float fPreviousArmor = pBackupDamagedPlayer->m_fArmor;
314+
float fPreviousArmor = pBackupDamagedPlayer->m_armor;
315315

316316
// Calculate how much damage should be applied to health/armor
317-
float fArmorDamage = std::min(fBackupDamage, pBackupDamagedPlayer->m_fArmor);
317+
float fArmorDamage = std::min(fBackupDamage, pBackupDamagedPlayer->m_armor);
318318
float fHealthDamage = std::min(fBackupDamage - fArmorDamage, pBackupDamagedPlayer->m_fHealth);
319319

320-
float fNewArmor = pBackupDamagedPlayer->m_fArmor - fArmorDamage;
320+
float fNewArmor = pBackupDamagedPlayer->m_armor - fArmorDamage;
321321
float fNewHealth = pBackupDamagedPlayer->m_fHealth - fHealthDamage;
322322

323323
// Ensure CPlayerPed has post damage health/armor

Client/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void CLuaCompatibilityDefs::LoadFunctions()
3737
{"getPlayerTotalAmmo", CLuaPedDefs::GetPedTotalAmmo},
3838
{"getPedWeaponMuzzlePosition", CLuaPedDefs::GetPedWeaponMuzzlePosition},
3939
{"getPlayerOccupiedVehicle", CLuaPedDefs::GetPedOccupiedVehicle},
40-
{"getPlayerArmor", CLuaPedDefs::GetPedArmor},
40+
{"getPlayerArmor", ArgumentParserWarn<false, CLuaPedDefs::GetPedArmor>},
4141
{"getPlayerSkin", CLuaElementDefs::GetElementModel},
4242
{"isPlayerChoking", CLuaPedDefs::IsPedChoking},
4343
{"isPlayerDucked", CLuaPedDefs::IsPedDucked},

Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void CLuaPedDefs::LoadFunctions()
8787

8888
{"getPedStat", GetPedStat},
8989
{"getPedOxygenLevel", GetPedOxygenLevel},
90-
{"getPedArmor", GetPedArmor},
90+
{"getPedArmor", ArgumentParserWarn<false, GetPedArmor>},
9191
{"isPedBleeding", ArgumentParser<IsPedBleeding>},
9292

9393
{"getPedContactElement", GetPedContactElement},
@@ -784,26 +784,9 @@ int CLuaPedDefs::OOP_GetPedTargetCollision(lua_State* luaVM)
784784
return 1;
785785
}
786786

787-
int CLuaPedDefs::GetPedArmor(lua_State* luaVM)
787+
float CLuaPedDefs::GetPedArmor(CClientPed* const ped) noexcept
788788
{
789-
// Verify the argument
790-
CClientPed* pPed = NULL;
791-
CScriptArgReader argStream(luaVM);
792-
argStream.ReadUserData(pPed);
793-
794-
if (!argStream.HasErrors())
795-
{
796-
// Grab the armor and return it
797-
float fArmor = pPed->GetArmor();
798-
lua_pushnumber(luaVM, fArmor);
799-
return 1;
800-
}
801-
else
802-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
803-
804-
// Failed
805-
lua_pushboolean(luaVM, false);
806-
return 1;
789+
return ped->GetArmor();
807790
}
808791

809792
int CLuaPedDefs::GetPedStat(lua_State* luaVM)
@@ -2395,6 +2378,12 @@ int CLuaPedDefs::SetPedMoveAnim(lua_State* luaVM)
23952378

23962379
bool CLuaPedDefs::SetPedArmor(CClientPed* const ped, const float armor)
23972380
{
2381+
if (armor < 0.0f)
2382+
throw std::invalid_argument("Armor must be greater than or equal to 0");
2383+
2384+
if (armor > 100.0f)
2385+
throw std::invalid_argument("Armor must be less than or equal to 100");
2386+
23982387
ped->SetArmor(armor);
23992388
return true;
24002389
}

Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CLuaPedDefs : public CLuaDefs
3838
LUA_DECLARE(GetPedStat);
3939
LUA_DECLARE(GetPedOccupiedVehicle);
4040
LUA_DECLARE(GetPedOccupiedVehicleSeat);
41-
LUA_DECLARE(GetPedArmor);
41+
static float GetPedArmor(CClientPed* const ped) noexcept;
4242
LUA_DECLARE(IsPedChoking);
4343
LUA_DECLARE(IsPedDucked);
4444
LUA_DECLARE(IsPedInVehicle);

Client/sdk/game/CPed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class CPed : public virtual CPhysical
195195
virtual float GetHealth() = 0;
196196
virtual void SetHealth(float fHealth) = 0;
197197
virtual float GetArmor() = 0;
198-
virtual void SetArmor(float fArmor) = 0;
198+
virtual void SetArmor(float armor) = 0;
199199
virtual float GetOxygenLevel() = 0;
200200
virtual void SetOxygenLevel(float fOxygen) = 0;
201201
virtual bool AddProjectile(eWeaponType eWeapon, CVector vecOrigin, float fForce, CVector* target, CEntity* targetEntity) = 0;

0 commit comments

Comments
 (0)