Skip to content

Commit 940bdb2

Browse files
gunslingermodXottab-DUTY
authored andcommitted
Improve weapon weight calculation code.
Get rid of code duplication
1 parent e270edf commit 940bdb2

File tree

5 files changed

+47
-32
lines changed

5 files changed

+47
-32
lines changed

src/xrGame/Weapon.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,24 @@ bool CWeapon::unlimited_ammo()
17301730
return ((GameID() == eGameIDDeathmatch) && m_DefaultCartridge.m_flags.test(CCartridge::cfCanBeUnlimited));
17311731
};
17321732

1733+
float CWeapon::GetMagazineWeight(const decltype(CWeapon::m_magazine)& mag) const
1734+
{
1735+
float res = 0;
1736+
const char* last_type = nullptr;
1737+
float last_ammo_weight = 0;
1738+
for (auto& c : mag)
1739+
{
1740+
// Usually ammos in mag have same type, use this fact to improve performance
1741+
if (last_type != c.m_ammoSect.c_str())
1742+
{
1743+
last_type = c.m_ammoSect.c_str();
1744+
last_ammo_weight = c.Weight();
1745+
}
1746+
res += last_ammo_weight;
1747+
}
1748+
return res;
1749+
}
1750+
17331751
float CWeapon::Weight() const
17341752
{
17351753
float res = CInventoryItemObject::Weight();
@@ -1745,20 +1763,7 @@ float CWeapon::Weight() const
17451763
{
17461764
res += pSettings->r_float(GetSilencerName(), "inv_weight");
17471765
}
1748-
1749-
const char* last_type = nullptr;
1750-
float w = 0, bs = 0;
1751-
for (auto& c : m_magazine)
1752-
{
1753-
// Usually ammos in mag have same type, use it to improve performance
1754-
if (last_type != c.m_ammoSect.c_str())
1755-
{
1756-
last_type = c.m_ammoSect.c_str();
1757-
w = pSettings->r_float(last_type, "inv_weight");
1758-
bs = pSettings->r_float(last_type, "box_size");
1759-
}
1760-
res += w / bs;
1761-
}
1766+
res += GetMagazineWeight(m_magazine);
17621767

17631768
return res;
17641769
}

src/xrGame/Weapon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ class CWeapon : public CHudItemObject, public CShootingObject
465465

466466
bool unlimited_ammo();
467467
IC bool can_be_strapped() const { return m_can_be_strapped; };
468+
469+
float GetMagazineWeight(const decltype(m_magazine)& mag) const;
470+
468471
protected:
469472
u32 m_ef_main_weapon_type;
470473
u32 m_ef_weapon_type;

src/xrGame/WeaponAmmo.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ void CCartridge::Load(LPCSTR section, u8 LocalAmmoType)
7070
m_InvShortName = CStringTable().translate(pSettings->r_string(section, "inv_name_short"));
7171
}
7272

73+
float CCartridge::Weight() const
74+
{
75+
auto s = m_ammoSect.c_str();
76+
float res = 0;
77+
if (s)
78+
{
79+
float box = pSettings->r_float(s, "box_size");
80+
if (box > 0)
81+
{
82+
float w = pSettings->r_float(s, "inv_weight");
83+
res = w / box;
84+
}
85+
}
86+
return res;
87+
}
88+
7389
CWeaponAmmo::CWeaponAmmo(void) {}
7490
CWeaponAmmo::~CWeaponAmmo(void) {}
7591
void CWeaponAmmo::Load(LPCSTR section)
@@ -215,11 +231,13 @@ CInventoryItem* CWeaponAmmo::can_make_killing(const CInventory* inventory) const
215231

216232
float CWeaponAmmo::Weight() const
217233
{
218-
float res = inherited::Weight();
219-
220-
res *= (float)m_boxCurr / (float)m_boxSize;
221-
222-
return res;
234+
if (m_boxSize > 0)
235+
{
236+
float res = inherited::Weight();
237+
res *= (float)m_boxCurr / (float)m_boxSize;
238+
return res;
239+
}
240+
return 0.f;
223241
}
224242

225243
u32 CWeaponAmmo::Cost() const

src/xrGame/WeaponAmmo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CCartridge : public IAnticheatDumpable
2828
public:
2929
CCartridge();
3030
void Load(LPCSTR section, u8 LocalAmmoType);
31+
float Weight() const;
3132

3233
shared_str m_ammoSect;
3334
enum

src/xrGame/WeaponMagazinedWGrenade.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -748,20 +748,8 @@ void CWeaponMagazinedWGrenade::net_Import(NET_Packet& P)
748748
float CWeaponMagazinedWGrenade::Weight() const
749749
{
750750
float res = inherited::Weight();
751+
res += GetMagazineWeight(m_magazine2);
751752

752-
const char* last_type = nullptr;
753-
float w = 0, bs = 0;
754-
for (auto& c : m_magazine2)
755-
{
756-
// Usually ammos in mag have same type, use it to improve performance
757-
if (last_type != c.m_ammoSect.c_str())
758-
{
759-
last_type = c.m_ammoSect.c_str();
760-
w = pSettings->r_float(last_type, "inv_weight");
761-
bs = pSettings->r_float(last_type, "box_size");
762-
}
763-
res += w / bs;
764-
}
765753
return res;
766754
}
767755

0 commit comments

Comments
 (0)