From cb3693f6648df798c53c481b3e561ecfdbc2838e Mon Sep 17 00:00:00 2001 From: ulteq Date: Wed, 16 Nov 2016 14:20:44 +0100 Subject: [PATCH] ATragMX - Fixes possible divison through zero --- .../fnc_recalculate_c1_ballistic_coefficient.sqf | 12 ++++++++---- .../functions/fnc_recalculate_muzzle_velocity.sqf | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/addons/atragmx/functions/fnc_recalculate_c1_ballistic_coefficient.sqf b/addons/atragmx/functions/fnc_recalculate_c1_ballistic_coefficient.sqf index 3ad4f26845..028a06fa4a 100644 --- a/addons/atragmx/functions/fnc_recalculate_c1_ballistic_coefficient.sqf +++ b/addons/atragmx/functions/fnc_recalculate_c1_ballistic_coefficient.sqf @@ -37,16 +37,20 @@ private ["_lowerIndex", "_upperIndex"]; for "_index" from 1 to (_lookupTableSize - 1) do { _upperIndex = _index; _lowerIndex = _upperIndex - 1; - if (((_lookupTable select _index) select 0) >= (GVAR(targetRange) select GVAR(currentTarget))) exitWith {} + if (((_lookupTable select _index) select 0) >= (GVAR(targetRange) select GVAR(currentTarget))) exitWith {}; }; -private ["_lowerDistance", "_upperDistance", "_lowerC1", "_upperC1", "_slope", "_c1"]; +private ["_lowerDistance", "_upperDistance", "_lowerC1", "_upperC1", "_c1"]; _lowerDistance = (_lookupTable select _lowerIndex) select 0; _upperDistance = (_lookupTable select _upperIndex) select 0; _lowerC1 = (_lookupTable select _lowerIndex) select 1; _upperC1 = (_lookupTable select _upperIndex) select 1; -_slope = (_upperC1 - _lowerC1) / (_upperDistance - _lowerDistance); -_c1 = 0.1 max (_lowerC1 + ((GVAR(targetRange) select GVAR(currentTarget)) - _lowerDistance) * _slope) min 2.0; +_c1 = _lowerC1; +if (_lowerDistance != _upperDistance) then { + private _slope = (_upperC1 - _lowerC1) / (_upperDistance - _lowerDistance); + _c1 = _lowerC1 + ((GVAR(targetRange) select GVAR(currentTarget)) - _lowerDistance) * _slope; +}; +_c1 = 0.1 max _c1 min 2.0; if (_c1 != GVAR(workingMemory) select 15) then { GVAR(workingMemory) set [15, _c1]; diff --git a/addons/atragmx/functions/fnc_recalculate_muzzle_velocity.sqf b/addons/atragmx/functions/fnc_recalculate_muzzle_velocity.sqf index 430e8ecc68..5830ba7683 100644 --- a/addons/atragmx/functions/fnc_recalculate_muzzle_velocity.sqf +++ b/addons/atragmx/functions/fnc_recalculate_muzzle_velocity.sqf @@ -37,16 +37,20 @@ private ["_lowerIndex", "_upperIndex"]; for "_index" from 1 to (_lookupTableSize - 1) do { _upperIndex = _index; _lowerIndex = _upperIndex - 1; - if (((_lookupTable select _index) select 0) >= GVAR(temperature)) exitWith {} + if (((_lookupTable select _index) select 0) >= GVAR(temperature)) exitWith {}; }; -private ["_lowerTemperature", "_upperTemperature", "_lowerMuzzleVelocity", "_upperMuzzleVelocity", "_slope", "_muzzleVelocity"]; +private ["_lowerTemperature", "_upperTemperature", "_lowerMuzzleVelocity", "_upperMuzzleVelocity", "_muzzleVelocity"]; _lowerTemperature = (_lookupTable select _lowerIndex) select 0; _upperTemperature = (_lookupTable select _upperIndex) select 0; _lowerMuzzleVelocity = (_lookupTable select _lowerIndex) select 1; _upperMuzzleVelocity = (_lookupTable select _upperIndex) select 1; -_slope = (_upperMuzzleVelocity - _lowerMuzzleVelocity) / (_upperTemperature - _lowerTemperature); -_muzzleVelocity = _lowerMuzzleVelocity + (GVAR(temperature) - _lowerTemperature) * _slope; +_muzzleVelocity = _lowerMuzzleVelocity; +if (_lowerTemperature != _upperTemperature) then { + private _slope = (_upperMuzzleVelocity - _lowerMuzzleVelocity) / (_upperTemperature - _lowerTemperature); + _muzzleVelocity = _lowerMuzzleVelocity + (GVAR(temperature) - _lowerTemperature) * _slope; +}; +_muzzleVelocity = 100 max _muzzleVelocity min 1400; if (_muzzleVelocity != GVAR(workingMemory) select 1) then { GVAR(workingMemory) set [1, _muzzleVelocity];