Skip to content

Commit

Permalink
Improve weapon weight calculation code.
Browse files Browse the repository at this point in the history
Get rid of code duplication
  • Loading branch information
gunslingermod authored and Xottab-DUTY committed Aug 14, 2017
1 parent e270edf commit 940bdb2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 32 deletions.
33 changes: 19 additions & 14 deletions src/xrGame/Weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,24 @@ bool CWeapon::unlimited_ammo()
return ((GameID() == eGameIDDeathmatch) && m_DefaultCartridge.m_flags.test(CCartridge::cfCanBeUnlimited));
};

float CWeapon::GetMagazineWeight(const decltype(CWeapon::m_magazine)& mag) const
{
float res = 0;
const char* last_type = nullptr;
float last_ammo_weight = 0;
for (auto& c : mag)
{
// Usually ammos in mag have same type, use this fact to improve performance
if (last_type != c.m_ammoSect.c_str())
{
last_type = c.m_ammoSect.c_str();
last_ammo_weight = c.Weight();
}
res += last_ammo_weight;
}
return res;
}

float CWeapon::Weight() const
{
float res = CInventoryItemObject::Weight();
Expand All @@ -1745,20 +1763,7 @@ float CWeapon::Weight() const
{
res += pSettings->r_float(GetSilencerName(), "inv_weight");
}

const char* last_type = nullptr;
float w = 0, bs = 0;
for (auto& c : m_magazine)
{
// Usually ammos in mag have same type, use it to improve performance
if (last_type != c.m_ammoSect.c_str())
{
last_type = c.m_ammoSect.c_str();
w = pSettings->r_float(last_type, "inv_weight");
bs = pSettings->r_float(last_type, "box_size");
}
res += w / bs;
}
res += GetMagazineWeight(m_magazine);

return res;
}
Expand Down
3 changes: 3 additions & 0 deletions src/xrGame/Weapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ class CWeapon : public CHudItemObject, public CShootingObject

bool unlimited_ammo();
IC bool can_be_strapped() const { return m_can_be_strapped; };

float GetMagazineWeight(const decltype(m_magazine)& mag) const;

protected:
u32 m_ef_main_weapon_type;
u32 m_ef_weapon_type;
Expand Down
28 changes: 23 additions & 5 deletions src/xrGame/WeaponAmmo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ void CCartridge::Load(LPCSTR section, u8 LocalAmmoType)
m_InvShortName = CStringTable().translate(pSettings->r_string(section, "inv_name_short"));
}

float CCartridge::Weight() const
{
auto s = m_ammoSect.c_str();
float res = 0;
if (s)
{
float box = pSettings->r_float(s, "box_size");
if (box > 0)
{
float w = pSettings->r_float(s, "inv_weight");
res = w / box;
}
}
return res;
}

CWeaponAmmo::CWeaponAmmo(void) {}
CWeaponAmmo::~CWeaponAmmo(void) {}
void CWeaponAmmo::Load(LPCSTR section)
Expand Down Expand Up @@ -215,11 +231,13 @@ CInventoryItem* CWeaponAmmo::can_make_killing(const CInventory* inventory) const

float CWeaponAmmo::Weight() const
{
float res = inherited::Weight();

res *= (float)m_boxCurr / (float)m_boxSize;

return res;
if (m_boxSize > 0)
{
float res = inherited::Weight();
res *= (float)m_boxCurr / (float)m_boxSize;
return res;
}
return 0.f;
}

u32 CWeaponAmmo::Cost() const
Expand Down
1 change: 1 addition & 0 deletions src/xrGame/WeaponAmmo.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CCartridge : public IAnticheatDumpable
public:
CCartridge();
void Load(LPCSTR section, u8 LocalAmmoType);
float Weight() const;

shared_str m_ammoSect;
enum
Expand Down
14 changes: 1 addition & 13 deletions src/xrGame/WeaponMagazinedWGrenade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,20 +748,8 @@ void CWeaponMagazinedWGrenade::net_Import(NET_Packet& P)
float CWeaponMagazinedWGrenade::Weight() const
{
float res = inherited::Weight();
res += GetMagazineWeight(m_magazine2);

const char* last_type = nullptr;
float w = 0, bs = 0;
for (auto& c : m_magazine2)
{
// Usually ammos in mag have same type, use it to improve performance
if (last_type != c.m_ammoSect.c_str())
{
last_type = c.m_ammoSect.c_str();
w = pSettings->r_float(last_type, "inv_weight");
bs = pSettings->r_float(last_type, "box_size");
}
res += w / bs;
}
return res;
}

Expand Down

0 comments on commit 940bdb2

Please sign in to comment.