2015-04-07 19:46:43 +00:00
|
|
|
/*
|
2015-08-05 02:13:36 +00:00
|
|
|
* Author: Ruthberg, MikeMatrix, joko // Jonas
|
2015-04-07 19:46:43 +00:00
|
|
|
*
|
|
|
|
* Calculates the muzzle velocity shift caused by different barrel lengths
|
|
|
|
*
|
|
|
|
* Arguments:
|
2017-06-08 13:31:51 +00:00
|
|
|
* 0: barrel length - mm <NUMBER>
|
2015-05-08 15:20:56 +00:00
|
|
|
* 1: muzzle velocity lookup table - m/s <ARRAY>
|
|
|
|
* 2: barrel length lookup table - mm <ARRAY>
|
|
|
|
* 3: muzzle velocity - m/s <NUMBER>
|
2015-04-07 19:46:43 +00:00
|
|
|
*
|
|
|
|
* Return Value:
|
2015-08-05 03:01:30 +00:00
|
|
|
* muzzle velocity shift - m/s <NUMBER>
|
2015-08-05 02:38:04 +00:00
|
|
|
*
|
2017-06-08 13:31:51 +00:00
|
|
|
* Example:
|
|
|
|
* [5, [0,5], [0,5], 5] call ace_advanced_ballistics_fnc_calculateBarrelLengthVelocityShift
|
|
|
|
*
|
2015-08-05 02:38:04 +00:00
|
|
|
* Public: No
|
2015-04-07 19:46:43 +00:00
|
|
|
*/
|
2015-04-05 19:08:55 +00:00
|
|
|
#include "script_component.hpp"
|
|
|
|
|
2015-08-05 02:13:36 +00:00
|
|
|
scopeName "main";
|
|
|
|
|
2015-08-04 22:32:48 +00:00
|
|
|
params ["_barrelLength", "_muzzleVelocityTable", "_barrelLengthTable", "_muzzleVelocity"];
|
2016-01-07 20:54:28 +00:00
|
|
|
TRACE_4("params",_barrelLength,_muzzleVelocityTable,_barrelLengthTable,_muzzleVelocity);
|
2015-04-05 19:08:55 +00:00
|
|
|
|
2015-08-05 02:13:36 +00:00
|
|
|
// If barrel length is not defined, then there is no point in calculating muzzle velocity
|
2015-04-05 19:08:55 +00:00
|
|
|
if (_barrelLength == 0) exitWith { 0 };
|
|
|
|
|
2017-10-10 14:39:59 +00:00
|
|
|
private _muzzleVelocityTableCount = count _muzzleVelocityTable;
|
|
|
|
private _barrelLengthTableCount = count _barrelLengthTable;
|
2015-04-05 19:08:55 +00:00
|
|
|
|
2015-08-05 02:13:36 +00:00
|
|
|
// Exit if tables are different sizes, have no elements or have only one element
|
2015-08-23 15:39:48 +00:00
|
|
|
if (_muzzleVelocityTableCount != _barrelLengthTableCount || _muzzleVelocityTableCount == 0 || _barrelLengthTableCount == 0) exitWith { 0 };
|
2015-08-05 02:13:36 +00:00
|
|
|
if (_muzzleVelocityTableCount == 1) exitWith { (_muzzleVelocityTable select 0) - _muzzleVelocity };
|
2015-04-05 19:08:55 +00:00
|
|
|
|
2015-08-05 02:13:36 +00:00
|
|
|
// If we have the precise barrel length value, return result immediately
|
|
|
|
if (_barrelLength in _barrelLengthTable) exitWith {
|
2015-08-23 15:53:33 +00:00
|
|
|
(_muzzleVelocityTable select (_barrelLengthTable find _barrelLength)) - _muzzleVelocity
|
2015-04-05 19:08:55 +00:00
|
|
|
};
|
2015-08-05 02:13:36 +00:00
|
|
|
|
|
|
|
// 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 };
|
|
|
|
|
2017-10-10 14:39:59 +00:00
|
|
|
private _upperDataIndex = -1;
|
|
|
|
private _lowerDataIndex = -1;
|
|
|
|
|
2015-08-05 02:13:36 +00:00
|
|
|
// Find closest bordering values for barrel length
|
|
|
|
{
|
2015-08-23 20:26:31 +00:00
|
|
|
if (_barrelLength <= _x) then {
|
|
|
|
_upperDataIndex = _forEachIndex;
|
|
|
|
_lowerDataIndex = _upperDataIndex - 1;
|
2015-08-05 02:13:36 +00:00
|
|
|
breakTo "main";
|
2015-04-07 19:27:04 +00:00
|
|
|
};
|
2015-08-05 02:13:36 +00:00
|
|
|
} forEach _barrelLengthTable;
|
2015-04-05 19:08:55 +00:00
|
|
|
|
2015-08-05 02:13:36 +00:00
|
|
|
// Worst case scenario
|
2017-10-10 14:39:59 +00:00
|
|
|
if (_upperDataIndex == -1 || _lowerDataIndex == -1) exitWith {0};
|
2015-04-05 19:08:55 +00:00
|
|
|
|
2017-10-10 14:39:59 +00:00
|
|
|
private _lowerBarrelLength = _barrelLengthTable select _lowerDataIndex;
|
|
|
|
private _upperBarrelLength = _barrelLengthTable select _upperDataIndex;
|
|
|
|
private _lowerMuzzleVelocity = _muzzleVelocityTable select _lowerDataIndex;
|
|
|
|
private _upperMuzzleVelocity = _muzzleVelocityTable select _upperDataIndex;
|
2015-08-05 02:13:36 +00:00
|
|
|
|
|
|
|
// Calculate interpolation ratio
|
2017-10-10 14:39:59 +00:00
|
|
|
private _interpolationRatio = [0, (_upperBarrelLength - _barrelLength) / (_upperBarrelLength - _lowerBarrelLength)] select (abs (_lowerBarrelLength - _upperBarrelLength) > 0);
|
2015-04-05 19:08:55 +00:00
|
|
|
|
2015-08-23 15:39:48 +00:00
|
|
|
// Calculate interpolated muzzle velocity shift
|
2015-08-05 02:13:36 +00:00
|
|
|
(_lowerMuzzleVelocity + ((_upperMuzzleVelocity - _lowerMuzzleVelocity) * (1 - _interpolationRatio))) - _muzzleVelocity // Return
|