diff --git a/addons/advanced_ballistics/functions/fnc_calculateAmmoTemperatureVelocityShift.sqf b/addons/advanced_ballistics/functions/fnc_calculateAmmoTemperatureVelocityShift.sqf index 16b094b462..8013bbd80a 100644 --- a/addons/advanced_ballistics/functions/fnc_calculateAmmoTemperatureVelocityShift.sqf +++ b/addons/advanced_ballistics/functions/fnc_calculateAmmoTemperatureVelocityShift.sqf @@ -15,21 +15,23 @@ */ #include "script_component.hpp" -private ["_muzzleVelocityShift", "_temperatureIndexA", "_temperatureIndexB", "_temperatureRatio"]; +private ["_muzzleVelocityShiftTableUpperLimit", "_temperatureIndexFunction", + "_temperatureIndexA", "_temperatureIndexB", "_interpolationRatio"]; params["_muzzleVelocityShiftTable", "_temperature"]; -if (count _muzzleVelocityShiftTable != 11) exitWith { 0 }; +// Check if muzzleVelocityShiftTable is Larger Than 11 Entrys +_muzzleVelocityShiftTableUpperLimit = _muzzleVelocityShiftTable select 10; +if (isNil "_muzzleVelocityShiftTableUpperLimit") exitWith { 0 }; -_temperatureIndexA = floor((_temperature + 15) / 5); -_temperatureIndexA = 0 max _temperatureIndexA; -_temperatureIndexA = _temperatureIndexA min 10; +// Find exact data index required for given temperature +_temperatureIndexFunction = (_temperature + 15) / 5; -_temperatureIndexB = ceil((_temperature + 15) / 5); -_temperatureIndexB = 0 max _temperatureIndexB; -_temperatureIndexB = _temperatureIndexB min 10; +// lower and upper data index used for interpolation +_temperatureIndexA = (0 max (floor(_temperatureIndexFunction)) min 10; +_temperatureIndexB = (0 max (ceil(_temperatureIndexFunction))) min 10; -_temperatureRatio = ((_temperature + 15) / 5) - floor((_temperature + 15) / 5); +// Interpolation ratio +_interpolationRatio = _temperatureIndexFunction - floor(_temperatureIndexFunction); -_muzzleVelocityShift = (_muzzleVelocityShiftTable select _temperatureIndexA) * (1 - _temperatureRatio) + (_muzzleVelocityShiftTable select _temperatureIndexB) * _temperatureRatio; - -_muzzleVelocityShift +// Interpolation +(_muzzleVelocityShiftTable select _temperatureIndexA) * (1 - _interpolationRatio) + (_muzzleVelocityShiftTable select _temperatureIndexB) * _interpolationRatio // Return diff --git a/addons/advanced_ballistics/functions/fnc_calculateBarrelLengthVelocityShift.sqf b/addons/advanced_ballistics/functions/fnc_calculateBarrelLengthVelocityShift.sqf index 1896ef816b..513752c538 100644 --- a/addons/advanced_ballistics/functions/fnc_calculateBarrelLengthVelocityShift.sqf +++ b/addons/advanced_ballistics/functions/fnc_calculateBarrelLengthVelocityShift.sqf @@ -1,5 +1,5 @@ /* - * Author: Ruthberg + * Author: Ruthberg, MikeMatrix, joko // Jonas * * Calculates the muzzle velocity shift caused by different barrel lengths * @@ -17,36 +17,56 @@ */ #include "script_component.hpp" -private ["_lowerIndex", "_upperIndex", "_barrelLengthRatio", "_muzzleVelocityNew"]; +scopeName "main"; + +private ["_muzzleVelocityTableCount", "_barrelLengthTableCount", "_lowerDataIndex", + "_upperDataIndex", "_lowerBarrelLength", "_upperBarrelLength", "_lowerMuzzleVelocity", + "_upperMuzzleVelocity", "_interpolationRatio"]; params ["_barrelLength", "_muzzleVelocityTable", "_barrelLengthTable", "_muzzleVelocity"]; +// If barrel length is not defined, then there is no point in calculating muzzle velocity if (_barrelLength == 0) exitWith { 0 }; -if (count _muzzleVelocityTable != count _barrelLengthTable) exitWith { 0 }; -if (count _muzzleVelocityTable == 0 || count _barrelLengthTable == 0) exitWith { 0 }; -if (count _muzzleVelocityTable == 1) exitWith { (_muzzleVelocityTable select 0) - _muzzleVelocity }; -_lowerIndex = 0; -_upperIndex = (count _barrelLengthTable) - 1; +_muzzleVelocityTableCount = count _muzzleVelocityTable; +_barrelLengthTableCount = count _barrelLengthTable; -if (_barrelLength <= (_barrelLengthTable select _lowerIndex)) exitWith { (_muzzleVelocityTable select _lowerIndex) - _muzzleVelocity }; -if (_barrelLength >= (_barrelLengthTable select _upperIndex)) exitWith { (_muzzleVelocityTable select _upperIndex) - _muzzleVelocity }; +// Exit if tables are different sizes, have no elements or have only one element +if (_muzzleVelocityTableCount != _barrelLengthTableCount) exitWith { 0 }; +if (_muzzleVelocityTableCount == 0 || _barrelLengthTableCount == 0) exitWith { 0 }; +if (_muzzleVelocityTableCount == 1) exitWith { (_muzzleVelocityTable select 0) - _muzzleVelocity }; -for "_i" from 0 to (count _barrelLengthTable) - 1 do { - if (_barrelLength >= _barrelLengthTable select _i) then { - _lowerIndex = _i; - }; -}; -for "_i" from (count _barrelLengthTable) - 1 to 0 step -1 do { - if (_barrelLength <= _barrelLengthTable select _i) then { - _upperIndex = _i; +// If we have the precise barrel length value, return result immediately +if (_barrelLength in _barrelLengthTable) exitWith { + _muzzleVelocityTable select (_barrelLengthTable find _barrelLength) - _muzzleVelocity +}; + +// Limit values to lower and upper bound of data we have available +if (_barrelLength <= (_barrelLengthTable select 0)) exitWith { (_muzzleVelocityTable select 0) - _muzzleVelocity }; +if (_barrelLength >= (_barrelLengthTable select _barrelLengthTableCount - 1)) exitWith { (_muzzleVelocityTable select _barrelLengthTableCount - 1) - _muzzleVelocity }; + +// Find closest bordering values for barrel length +{ + if (_barrelLength >= _x) then { + _lowerDataIndex = _forEachIndex; + _upperDataIndex = _lowerDataIndex + 1; + breakTo "main"; }; +} forEach _barrelLengthTable; + +// Worst case scenario +if (isNil "_lowerDataIndex" || isNil "_upperDataIndex") exitWith {0}; + +_lowerBarrelLength = _barrelLengthTable select _lowerDataIndex; +_upperBarrelLength = _barrelLengthTable select _upperDataIndex; +_lowerMuzzleVelocity = _muzzleVelocityTable select _lowerDataIndex; +_upperMuzzleVelocity = _muzzleVelocityTable select _upperDataIndex; + +// Calculate interpolation ratio +_interpolationRatio = if (abs (_lowerBarrelLength - _upperBarrelLength) > 0) then { + (_upperBarrelLength - _barrelLength) / (_upperBarrelLength - _lowerBarrelLength) +} else { + 0 }; -_barrelLengthRatio = 0; -if ((_barrelLengthTable select _upperIndex) - (_barrelLengthTable select _lowerIndex) > 0) then { - _barrelLengthRatio = ((_barrelLengthTable select _upperIndex) - _barrelLength) / ((_barrelLengthTable select _upperIndex) - (_barrelLengthTable select _lowerIndex)); -}; - -_muzzleVelocityNew = (_muzzleVelocityTable select _lowerIndex) + ((_muzzleVelocityTable select _upperIndex) - (_muzzleVelocityTable select _lowerIndex)) * (1 - _barrelLengthRatio); - -_muzzleVelocityNew - _muzzleVelocity +// Calculate interpolated muzzle velocity +(_lowerMuzzleVelocity + ((_upperMuzzleVelocity - _lowerMuzzleVelocity) * (1 - _interpolationRatio))) - _muzzleVelocity // Return