Fixed "divided by zero" error when returning 0 in AMMOCOST hook

* with CheckWeaponAmmoCost=1

Added a value check to the return value of AMMOCOST hook.
Updated hookscripts.md to reflect this change.
This commit is contained in:
NovaRain
2023-05-18 10:01:49 +08:00
parent 5b9e90100d
commit befea69909
5 changed files with 10 additions and 5 deletions
+1 -1
View File
@@ -619,7 +619,7 @@ ExplosionsEmitLight=0
;MovieTimer_artimer4=360 ;MovieTimer_artimer4=360
;Set to 1 to add proper check for ammo before attacking and proper calculation of the number of burst rounds ;Set to 1 to add proper check for ammo before attacking and proper calculation of the number of burst rounds
;By default, weapons can make attacks when at least one ammo is left, regardless of ammo cost calculations ;By default, a weapon can perform an attack with at least one ammo, regardless of ammo cost calculation
;Note that enabling this option will prevent super cattle prods and mega power fists from attacking with only one ammo left ;Note that enabling this option will prevent super cattle prods and mega power fists from attacking with only one ammo left
CheckWeaponAmmoCost=0 CheckWeaponAmmoCost=0
+3 -1
View File
@@ -296,9 +296,11 @@
id: HOOK_AMMOCOST id: HOOK_AMMOCOST
doc: | doc: |
Runs when calculating ammo cost for a weapon. Doesn't affect damage, only how much ammo is spent.<br> Runs when calculating ammo cost for a weapon. Doesn't affect damage, only how much ammo is spent.<br>
By default, weapons can make attacks when at least 1 ammo is left, regardless of ammo cost calculations.<br> By default, a weapon can perform an attack with at least one ammo, regardless of ammo cost calculation.<br>
To add proper check for ammo before attacking and proper calculation of the number of burst rounds (hook type 1 and 2 in `arg3`), set **CheckWeaponAmmoCost=1** in **Misc** section of ddraw.ini. To add proper check for ammo before attacking and proper calculation of the number of burst rounds (hook type 1 and 2 in `arg3`), set **CheckWeaponAmmoCost=1** in **Misc** section of ddraw.ini.
__NOTE:__ The return value must be greater than or equal to 0 to be valid.
``` ```
Item arg0 - The weapon Item arg0 - The weapon
int arg1 - Number of bullets in burst or 1 for single shots int arg1 - Number of bullets in burst or 1 for single shots
+3 -1
View File
@@ -394,9 +394,11 @@ int ret1 - The new maximum damage
#### `HOOK_AMMOCOST (hs_ammocost.int)` #### `HOOK_AMMOCOST (hs_ammocost.int)`
Runs when calculating ammo cost for a weapon. Doesn't affect damage, only how much ammo is spent.\ Runs when calculating ammo cost for a weapon. Doesn't affect damage, only how much ammo is spent.\
By default, weapons can make attacks when at least 1 ammo is left, regardless of ammo cost calculations.\ By default, a weapon can perform an attack with at least one ammo, regardless of ammo cost calculation.\
To add proper check for ammo before attacking and proper calculation of the number of burst rounds (hook type 1 and 2 in `arg3`), set **CheckWeaponAmmoCost=1** in **Misc** section of ddraw.ini. To add proper check for ammo before attacking and proper calculation of the number of burst rounds (hook type 1 and 2 in `arg3`), set **CheckWeaponAmmoCost=1** in **Misc** section of ddraw.ini.
__NOTE:__ The return value must be greater than or equal to 0 to be valid.
``` ```
Item arg0 - The weapon Item arg0 - The weapon
int arg1 - Number of bullets in burst or 1 for single shots int arg1 - Number of bullets in burst or 1 for single shots
+2 -1
View File
@@ -110,7 +110,7 @@ long __fastcall Combat::check_item_ammo_cost(fo::GameObject* weapon, fo::AttackT
} }
// calculate the cost // calculate the cost
long cost = (newRounds != rounds) ? newRounds / rounds : 1; // 1 - default cost long cost = (newRounds != rounds) ? (newRounds / rounds) : 1; // 1 - default cost
return (cost > currAmmo) ? 0 : currAmmo; // 0 - this will force "Not Enough Ammo" return (cost > currAmmo) ? 0 : currAmmo; // 0 - this will force "Not Enough Ammo"
} }
@@ -171,6 +171,7 @@ static long __fastcall divide_burst_rounds_by_ammo_cost(long currAmmo, fo::GameO
roundsCost = burstRounds; // rounds in burst (the number of rounds fired in the burst) roundsCost = burstRounds; // rounds in burst (the number of rounds fired in the burst)
AmmoCostHook_Script(2, weapon, roundsCost); // roundsCost returns the new cost AmmoCostHook_Script(2, weapon, roundsCost); // roundsCost returns the new cost
} }
if (roundsCost == 0) return burstRounds; // cost is free, skip the rest of calculation
long cost = burstRounds * roundsCost; // amount of ammo required for this burst (multiplied by 1 or by the value returned from HOOK_AMMOCOST) long cost = burstRounds * roundsCost; // amount of ammo required for this burst (multiplied by 1 or by the value returned from HOOK_AMMOCOST)
if (cost > currAmmo) cost = currAmmo; // if cost ammo more than current ammo, set it to current if (cost > currAmmo) cost = currAmmo; // if cost ammo more than current ammo, set it to current
+1 -1
View File
@@ -350,7 +350,7 @@ int __fastcall AmmoCostHook_Script(DWORD hookType, fo::GameObject* weapon, DWORD
RunHookScript(HOOK_AMMOCOST); RunHookScript(HOOK_AMMOCOST);
if (cRet > 0) rounds = rets[0]; // override rounds if (cRet > 0 && (long)rets[0] >= 0) rounds = rets[0]; // override rounds
failed: failed:
EndHook(); EndHook();