From 9c6103ab444bd6ebffc95018facaf3e8ecf4fd12 Mon Sep 17 00:00:00 2001 From: drjaska Date: Thu, 5 Sep 2024 21:38:52 +0300 Subject: [PATCH] Avoid div by zeros --- qcsrc/common/weapons/weapon/machinegun.qc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/qcsrc/common/weapons/weapon/machinegun.qc b/qcsrc/common/weapons/weapon/machinegun.qc index a96152466..900d54db1 100644 --- a/qcsrc/common/weapons/weapon/machinegun.qc +++ b/qcsrc/common/weapons/weapon/machinegun.qc @@ -214,7 +214,9 @@ METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponen { // We don't want to shoot 3 rounds if there's 2 left in the mag, so we'll use a fraction. // Also keep the fraction <= 1 otherwise we'd mag dump in one burst. - float burst_fraction = min(1, ammo_available / WEP_CVAR(WEP_MACHINEGUN, burst_ammo)); + float burst_fraction = 1; + if(WEP_CVAR(WEP_MACHINEGUN, burst_ammo)) // avoid div by 0 + burst_fraction = min(1, ammo_available / WEP_CVAR(WEP_MACHINEGUN, burst_ammo)); to_shoot = floor(to_shoot * burst_fraction); // We also don't want to use 3 rounds if there's only 2 left. @@ -269,9 +271,11 @@ METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity wea METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity)) { - float ammo_amount; - float burst_ammo_per_shot = WEP_CVAR(WEP_MACHINEGUN, burst_ammo) / WEP_CVAR(WEP_MACHINEGUN, burst); + float burst_ammo_per_shot = 1; + if (WEP_CVAR(WEP_MACHINEGUN, burst)) // avoid div by 0 + burst_ammo_per_shot = WEP_CVAR(WEP_MACHINEGUN, burst_ammo) / WEP_CVAR(WEP_MACHINEGUN, burst); + float ammo_amount; if(WEP_CVAR(WEP_MACHINEGUN, mode) == 1) ammo_amount = GetResource(actor, thiswep.ammo_type) >= burst_ammo_per_shot; else -- 2.39.2