]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Avoid div by zeros drjaska/mgreload 1349/head
authordrjaska <drjaska83@gmail.com>
Thu, 5 Sep 2024 18:38:52 +0000 (21:38 +0300)
committerdrjaska <drjaska83@gmail.com>
Fri, 6 Sep 2024 07:44:29 +0000 (10:44 +0300)
qcsrc/common/weapons/weapon/machinegun.qc

index 2178a5198d615972eea694311738c8be8646a412..85948bcba1078b343123a0cd71056d2cc6505272 100644 (file)
@@ -211,7 +211,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));
                        int to_shoot = floor(WEP_CVAR(WEP_MACHINEGUN, burst) * burst_fraction);
 
                        // We also don't want to use 3 rounds if there's only 2 left.
@@ -265,9 +267,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