Skip to content

Commit f5bcee1

Browse files
Misc. ballistic components - Clean up funtions (#11272)
* Optimize misc. ballistics funtions * Update addons/xm157/functions/fnc_ballistics_getData.sqf Co-authored-by: PabstMirror <pabstmirror@gmail.com> --------- Co-authored-by: PabstMirror <pabstmirror@gmail.com>
1 parent 8702204 commit f5bcee1

File tree

6 files changed

+97
-60
lines changed

6 files changed

+97
-60
lines changed

addons/ballistics/functions/fnc_statTextStatement_weaponMuzzleVelocity.sqf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ if (_useAB) then {
4141
};
4242
} else {
4343
private _initSpeedCoef = getNumber (_configWeapon >> "initSpeed");
44-
if (_initSpeedCoef < 0) then {
45-
_muzzleVelocity = _muzzleVelocity * -_initSpeedCoef;
46-
};
44+
4745
if (_initSpeedCoef > 0) then {
4846
_muzzleVelocity = _initSpeedCoef;
47+
} else {
48+
if (_initSpeedCoef < 0) then {
49+
_muzzleVelocity = _muzzleVelocity * -_initSpeedCoef;
50+
};
4951
};
5052
};
5153

addons/rangecard/functions/fnc_calculateRangeCard.sqf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ if (_useABConfig) then {
7676

7777
private _airFrictionCoef = if (!_useABConfig && _isABenabled) then {
7878
private _airDensity = [_temperature, _barometricPressure, _relativeHumidity] call EFUNC(weather,calculateAirDensity);
79-
_airDensity / 1.22498
79+
_airDensity / STD_AIR_DENSITY_ICAO
8080
} else {
8181
1
8282
};

addons/rangecard/script_component.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#define DEBUG_SETTINGS DEBUG_SETTINGS_RANGECARD
1515
#endif
1616

17+
#define STD_AIR_DENSITY_ICAO 1.22498
18+
1719
#define RANGE_CARD_INCREMENT 50
1820
#define RANGE_CARD_START_RANGE 100
1921
#define RANGE_CARD_END_RANGE (RANGE_CARD_START_RANGE + 49 * RANGE_CARD_INCREMENT)

addons/scopes/functions/fnc_calculateZeroAngleCorrection.sqf

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
#include "..\script_component.hpp"
22
/*
33
* Author: Ruthberg
4-
* Calculates the zero angle correction for the new zero range based on current zero range and bore height (distance between bore- and sight axis)
4+
* Calculates the zero angle correction for the new zero range based on current zero range and bore height (distance between bore- and sight axis).
55
*
66
* Arguments:
77
* 0: Old Zero range <NUMBER>
88
* 1: New Zero range <NUMBER>
9-
* 2: Bore height <NUMBER>
9+
* 2: Bore height - cm <NUMBER>
1010
* 3: Weapon <STRING>
1111
* 4: Muzzle <STRING>
1212
* 5: Ammo <STRING>
1313
* 6: Magazine <STRING>
1414
* 7: Advanced Ballistics enabled? <BOOL>
1515
*
1616
* Return Value:
17-
* zeroAngleCorrection <NUMBER>
17+
* Zero angle correction <NUMBER>
1818
*
1919
* Example:
20-
* [5, 6, 7, gun, ammo, magazine, true] call ace_scopes_fnc_calculateZeroAngleCorrection
20+
* [5, 6, 3.81, "arifle_MXM_F", "B_65x39_Caseless", "30Rnd_65x39_caseless_mag", true] call ace_scopes_fnc_calculateZeroAngleCorrection
2121
*
2222
* Public: No
2323
*/
2424

25-
params ["_oldZeroRange", "_newZeroRange", "_boreHeight"/*in cm*/, "_weapon", "_muzzle", "_ammo", "_magazine", "_advancedBallistics"];
25+
params ["_oldZeroRange", "_newZeroRange", "_boreHeight", "_weapon", "_muzzle", "_ammo", "_magazine", "_advancedBallistics"];
2626

2727
// When FFV from vehicles currentZeroing will report 0 so just bail
2828
if (_oldZeroRange <= 0) exitWith { 0 };
2929

3030
GVAR(zeroAngleCorrectionData) getOrDefaultCall [_this, {
3131
private _airFriction = getNumber (configFile >> "CfgAmmo" >> _ammo >> "airFriction");
32-
private _initSpeed = getNumber(configFile >> "CfgMagazines" >> _magazine >> "initSpeed");
33-
private _initSpeedCoef = getNumber(configFile >> "CfgWeapons" >> _weapon >> "initSpeed");
32+
private _initSpeed = 0;
33+
private _initSpeedCoef = getNumber (configFile >> "CfgWeapons" >> _weapon >> "initSpeed");
34+
3435
if (_initSpeedCoef > 0) then {
3536
_initSpeed = _initSpeedCoef;
36-
};
37-
if (_initSpeedCoef < 0) then {
38-
_initSpeed = _initSpeed * (-1 * _initSpeedCoef);
37+
} else {
38+
_initSpeed = getNumber (configFile >> "CfgMagazines" >> _magazine >> "initSpeed");
39+
40+
if (_initSpeedCoef < 0) then {
41+
_initSpeed = _initSpeed * -_initSpeedCoef;
42+
};
3943
};
4044

4145
private _vanillaZero = parseNumber (("ace" callExtension ["ballistics:replicate_vanilla_zero", [_oldZeroRange, _initSpeed, _airFriction]]) select 0);

addons/xm157/functions/fnc_ballistics_calculator.sqf

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,67 @@
2121
params ["_targetRange", "_directionOfFire", "_inclinationAngle", "_bank"];
2222

2323
private _weaponInfo = [] call FUNC(ballistics_getData);
24-
if (_weaponInfo isEqualTo []) exitWith { [0,0,0] };
25-
_weaponInfo params ["_scopeBaseAngle","_boreHeight","_airFriction","_muzzleVelocity","_bc",
26-
"_dragModel","_atmosphereModel","_barrelTwist","_twistDirection","_caliber","_bulletLength","_bulletMass"];
24+
25+
if (_weaponInfo isEqualTo []) exitWith {
26+
[0, 0, 0] // return
27+
};
28+
29+
_weaponInfo params [
30+
"_scopeBaseAngle",
31+
"_boreHeight",
32+
"_airFriction",
33+
"_muzzleVelocity",
34+
"_bc",
35+
"_dragModel",
36+
"_atmosphereModel",
37+
"_barrelTwist",
38+
"_twistDirection",
39+
"_caliber",
40+
"_bulletLength",
41+
"_bulletMass"
42+
];
2743

2844
private _latitude = GVAR(data) getOrDefault ["latitude", 0];
2945

3046
// Get Wind
3147
private _windSpeed = GVAR(data) getOrDefault ["wind_speed", 0];
3248
private _windDirection = 22.5 * (GVAR(data) getOrDefault ["wind_dir", 0]);
33-
private _wind = [sin (_directionOfFire-_windDirection) * _windSpeed, -cos (_directionOfFire-_windDirection) * _windSpeed, 0];
49+
private _wind = [sin (_directionOfFire - _windDirection), -cos (_directionOfFire - _windDirection), 0] vectorMultiply _windSpeed;
3450

3551
// Get atmosphere
36-
private _altitude = (getPosASL ace_player) select 2;
52+
private _altitude = (getPosASL ACE_player) select 2;
3753
private _relativeHumidity = EGVAR(weather,currentHumidity);
3854
private _temperature = _altitude call EFUNC(weather,calculateTemperatureAtHeight);
3955
private _barometricPressure = _altitude call EFUNC(weather,calculateBarometricPressure);
4056

4157

42-
private _bulletPos = [0,0,-(_boreHeight / 100)];
58+
private _bulletPos = [0, 0, -_boreHeight / 100];
4359
private _lastBulletPos = +_bulletPos;
44-
private _bulletVelocity = [0,cos(_scopeBaseAngle) * _muzzleVelocity,sin(_scopeBaseAngle) * _muzzleVelocity];
45-
private _gravity = [-sin (_bank) * cos(_scopeBaseAngle + _inclinationAngle) * -GRAVITY,
46-
sin(_scopeBaseAngle + _inclinationAngle) * -GRAVITY,
47-
cos (_bank) * cos(_scopeBaseAngle + _inclinationAngle) * -GRAVITY];
60+
private _bulletVelocity = [0, cos _scopeBaseAngle, sin _scopeBaseAngle] vectorMultiply _muzzleVelocity;
61+
private _gravity = [(-sin _bank) * cos (_scopeBaseAngle + _inclinationAngle),
62+
sin (_scopeBaseAngle + _inclinationAngle),
63+
(cos _bank) * cos (_scopeBaseAngle + _inclinationAngle)] vectorMultiply -GRAVITY;
4864

4965
private _useAB = missionNamespace getVariable [QEGVAR(advanced_ballistics,enabled), false];
66+
5067
if (_useAB) then {
5168
_bc = parseNumber (("ace" callExtension ["ballistics:atmospheric_correction", [_bc, _temperature, _barometricPressure, _relativeHumidity, _atmosphereModel]]) select 0);
5269
};
5370

5471
private _deltaT = 1 / 60;
5572
private _TOF = 0; // Limit TOF to 5 seconds!
73+
5674
while {(_TOF < 5) && {(_bulletPos # 1) < _targetRange}} do {
5775
private _trueVelocity = _bulletVelocity vectorDiff _wind;
5876
private _trueSpeed = vectorMagnitude _trueVelocity;
5977

6078
private _bulletAccel = if (_useAB) then {
6179
private _drag = parseNumber (("ace" callExtension ["ballistics:retard", [_dragModel, _bc, _trueSpeed, _temperature]]) select 0);
62-
(vectorNormalized _trueVelocity) vectorMultiply (-1 * _drag);
80+
(vectorNormalized _trueVelocity) vectorMultiply -_drag;
6381
} else {
6482
_trueVelocity vectorMultiply (_trueSpeed * _airFriction);
6583
};
84+
6685
_bulletAccel = _bulletAccel vectorAdd _gravity;
6786
_lastBulletPos = _bulletPos;
6887
_bulletPos = _bulletPos vectorAdd (_bulletVelocity vectorMultiply (_deltaT * 0.5));
@@ -74,27 +93,30 @@ while {(_TOF < 5) && {(_bulletPos # 1) < _targetRange}} do {
7493

7594
private _tx = linearConversion [_lastBulletPos select 1, _bulletPos select 1, _targetRange, _lastBulletPos select 0, _bulletPos select 0];
7695
private _tz = linearConversion [_lastBulletPos select 1, _bulletPos select 1, _targetRange, _lastBulletPos select 2, _bulletPos select 2];
77-
private _elevation = - atan(_tz / _targetRange);
78-
private _windage = - atan(_tx / _targetRange);
79-
96+
private _elevation = -atan (_tz / _targetRange);
97+
private _windage = -atan (_tx / _targetRange);
8098

8199
if (_useAB && {(_bulletPos select 1) > 0}) then {
82100
// Coriolis
83-
private _horizontalDeflection = 0.0000729 * (_bulletPos select 1) * _TOF * sin(_latitude);
84-
private _horizontalCoriolis = - atan(_horizontalDeflection / (_bulletPos select 1));
101+
private _horizontalDeflection = 0.0000729 * (_bulletPos select 1) * _TOF * (sin _latitude);
102+
private _horizontalCoriolis = -atan (_horizontalDeflection / (_bulletPos select 1));
85103
_windage = _windage + _horizontalCoriolis;
104+
86105
// Eoetvoes
87-
private _eoetvoesMultiplier = 2 * (0.0000729 * _muzzleVelocity / -GRAVITY) * cos(_latitude) * sin(_directionOfFire);
106+
private _eoetvoesMultiplier = 2 * (0.0000729 * _muzzleVelocity / -GRAVITY) * (cos _latitude) * (sin _directionOfFire);
88107
private _verticalDeflection = (_bulletPos select 2) * _eoetvoesMultiplier;
89-
private _verticalCoriolis = - atan(_verticalDeflection / (_bulletPos select 1));
108+
private _verticalCoriolis = -atan (_verticalDeflection / (_bulletPos select 1));
90109
_elevation = _elevation + _verticalCoriolis;
110+
91111
// Spin drift
92-
private _stabilityFactor = 1.5;
93-
if (_caliber * _bulletLength * _bulletMass * _barrelTwist > 0) then {
94-
_stabilityFactor = [_caliber, _bulletLength, _bulletMass, _barrelTwist, _muzzleVelocity, _temperature, _barometricPressure] call EFUNC(advanced_ballistics,calculateStabilityFactor);
112+
private _stabilityFactor = if (_caliber * _bulletLength * _bulletMass * _barrelTwist > 0) then {
113+
[_caliber, _bulletLength, _bulletMass, _barrelTwist, _muzzleVelocity, _temperature, _barometricPressure] call EFUNC(advanced_ballistics,calculateStabilityFactor)
114+
} else {
115+
1.5
95116
};
117+
96118
private _spinDeflection = _twistDirection * 0.0254 * 1.25 * (_stabilityFactor + 1.2) * _TOF ^ 1.83;
97-
private _spinDrift = - atan(_spinDeflection / (_bulletPos select 1));
119+
private _spinDrift = -atan (_spinDeflection / (_bulletPos select 1));
98120
_windage = _windage + _spinDrift;
99121
};
100122

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "..\script_component.hpp"
22
/*
33
* Author: PabstMirror, Ruthberg (Based on ace_atragmx)
4-
* Gets ballistic info for a weapon, mag and ammo
4+
* Gets ballistic info for a weapon, mag and ammo.
55
*
66
* Arguments:
77
* None
@@ -15,24 +15,25 @@
1515
* Public: No
1616
*/
1717

18-
private _unit = ace_player;
19-
private _weaponClass = primaryWeapon _unit;
18+
private _unit = ACE_player;
2019
private _magazineClass = (primaryWeaponMagazine _unit) param [0, ""];
21-
private _ammoClass = getText (configFile >> "CfgMagazines" >> _magazineClass >> "ammo");
2220

23-
private _key = format ["weaponInfoCache-%1-%2-%3",_weaponClass,_magazineClass,_ammoClass];
24-
private _weaponInfo = GVAR(data) getOrDefault [_key, []];
25-
if ((_weaponInfo isEqualTo []) && {_magazineClass != ""}) then {
21+
if (_magazineClass == "") exitWith {
22+
[] // return
23+
};
24+
25+
private _weaponClass = primaryWeapon _unit;
26+
27+
GVAR(data) getOrDefaultCall [[_weaponClass, _magazineClass], {
28+
private _ammoClass = getText (configFile >> "CfgMagazines" >> _magazineClass >> "ammo");
2629
TRACE_3("new weapon/mag",_weaponClass,_magazineClass,_ammoClass);
2730

2831
private _zeroRange = 100;
2932
private _boreHeight = [_unit, 0] call EFUNC(scopes,getBoreHeight);
3033

31-
private _ammoConfig = _ammoClass call EFUNC(advanced_ballistics,readAmmoDataFromConfig);
32-
_ammoConfig params ["_airFriction","_caliber","_bulletLength","_bulletMass","","_dragModel","_ballisticCoefficients","","_atmosphereModel","","_muzzleVelocityTable","_barrelLengthTable"];
33-
private _weaponConfig = _weaponClass call EFUNC(advanced_ballistics,readWeaponDataFromConfig);
34-
_weaponConfig params ["_barrelTwist", "_twistDirection", "_barrelLength"];
35-
private _bc = if (_ballisticCoefficients isEqualTo []) then { 0 } else { _ballisticCoefficients # 0 };
34+
(_ammoClass call EFUNC(advanced_ballistics,readAmmoDataFromConfig)) params ["_airFriction", "_caliber", "_bulletLength", "_bulletMass", "", "_dragModel", "_ballisticCoefficients", "", "_atmosphereModel", "", "_muzzleVelocityTable", "_barrelLengthTable"];
35+
(_weaponClass call EFUNC(advanced_ballistics,readWeaponDataFromConfig)) params ["_barrelTwist", "_twistDirection", "_barrelLength"];
36+
private _bc = _ballisticCoefficients select 0;
3637

3738
private _useAB = (
3839
missionNamespace getVariable [QEGVAR(advanced_ballistics,enabled), false] &&
@@ -42,31 +43,37 @@ if ((_weaponInfo isEqualTo []) && {_magazineClass != ""}) then {
4243

4344
// Get Muzzle Velocity
4445
private _muzzleVelocity = 0;
45-
if (_barrelLength > 0 && _useAB) then {
46+
47+
if (_useAB && _barrelLength > 0) then {
4648
_muzzleVelocity = [_barrelLength, _muzzleVelocityTable, _barrelLengthTable, 0] call EFUNC(advanced_ballistics,calculateBarrelLengthVelocityShift)
4749
};
50+
4851
if (_muzzleVelocity == 0) then {
49-
private _initSpeed = getNumber (configFile >> "CfgMagazines" >> _magazineClass >> "initSpeed");
5052
private _initSpeedCoef = getNumber (configFile >> "CfgWeapons" >> _weaponClass >> "initSpeed");
51-
if (_initSpeedCoef < 0) then {
52-
_initSpeed = _initSpeed * -_initSpeedCoef;
53-
};
53+
private _initSpeed = 0;
54+
5455
if (_initSpeedCoef > 0) then {
5556
_initSpeed = _initSpeedCoef;
57+
} else {
58+
_initSpeed = getNumber (configFile >> "CfgMagazines" >> _magazineClass >> "initSpeed");
59+
60+
if (_initSpeedCoef < 0) then {
61+
_initSpeed = _initSpeed * -_initSpeedCoef;
62+
};
5663
};
57-
_muzzleVelocity = _initSpeed
64+
65+
_muzzleVelocity = _initSpeed;
5866
};
5967

6068
// Scope Base Angle
61-
private _scopeBaseAngle = if ((getText (configFile >> "CfgAmmo" >> _ammoClass >> "simulation")) != "shotshell") then {
69+
private _scopeBaseAngle = if (getText (configFile >> "CfgAmmo" >> _ammoClass >> "simulation") != "shotshell") then {
6270
parseNumber (("ace" callExtension ["ballistics:zero_vanilla", [_zeroRange, _muzzleVelocity, _airFriction, _boreHeight]]) select 0)
6371
} else {
6472
0 // shotshell will not have any vanilla zeroing applied, 0 is a reasonable default for now
6573
};
6674

67-
_weaponInfo = [_scopeBaseAngle,_boreHeight,_airFriction,_muzzleVelocity,_bc,_dragModel,_atmosphereModel,_barrelTwist,_twistDirection,_caliber,_bulletLength,_bulletMass];
68-
GVAR(data) set [_key, _weaponInfo];
69-
TRACE_1("setting cache",_weaponInfo);
70-
};
75+
TRACE_8("setting cache",_airFriction,_muzzleVelocity,_bc,_dragModel,_atmosphereModel,_caliber,_bulletLength,_bulletMass);
76+
TRACE_4("setting cache",_scopeBaseAngle,_boreHeight,_barrelTwist,_twistDirection);
7177

72-
_weaponInfo
78+
[_scopeBaseAngle, _boreHeight, _airFriction, _muzzleVelocity, _bc, _dragModel, _atmosphereModel, _barrelTwist, _twistDirection, _caliber, _bulletLength, _bulletMass]
79+
}, true] // return

0 commit comments

Comments
 (0)