From 4355df118cc4407358bd2b6bbe6affcb8756c107 Mon Sep 17 00:00:00 2001 From: ulteq Date: Mon, 13 Apr 2015 11:53:19 +0200 Subject: [PATCH] Refactored the Kestrel4500 Code. --- addons/kestrel4500/XEH_postInit.sqf | 5 +- addons/kestrel4500/XEH_preInit.sqf | 2 + .../kestrel4500/functions/fnc_collectData.sqf | 13 ++--- .../functions/fnc_displayKestrel.sqf | 47 +++++++++---------- .../functions/fnc_generateOutputData.sqf | 43 +++++------------ .../functions/fnc_measureWindSpeed.sqf | 42 +++++++++++++++++ .../functions/fnc_updateImpellerState.sqf | 22 +++++++++ 7 files changed, 107 insertions(+), 67 deletions(-) create mode 100644 addons/kestrel4500/functions/fnc_measureWindSpeed.sqf create mode 100644 addons/kestrel4500/functions/fnc_updateImpellerState.sqf diff --git a/addons/kestrel4500/XEH_postInit.sqf b/addons/kestrel4500/XEH_postInit.sqf index 0cd506c95c..e77eea4afb 100644 --- a/addons/kestrel4500/XEH_postInit.sqf +++ b/addons/kestrel4500/XEH_postInit.sqf @@ -19,7 +19,10 @@ GVAR(RefHeading) = 0; GVAR(updateTimer) = 0; GVAR(outputData) = ["", "", "", "", "", "", "", "", "", ""]; GVAR(MeasuredWindSpeed) = 0; -GVAR(WheelState) = 0; +GVAR(ImpellerState) = 0; GVAR(Kestrel4500) = false; GVAR(Overlay) = false; + +// TODO: Remove this, after merging the ab branch +if (isNil QUOTE(EGVAR(weather,Altitude))) then {EGVAR(weather,Altitude) = 0}; diff --git a/addons/kestrel4500/XEH_preInit.sqf b/addons/kestrel4500/XEH_preInit.sqf index 293eb79f99..d081f485ec 100644 --- a/addons/kestrel4500/XEH_preInit.sqf +++ b/addons/kestrel4500/XEH_preInit.sqf @@ -9,6 +9,8 @@ PREP(collectData); PREP(createKestrelDialog); PREP(displayKestrel); PREP(generateOutputData); +PREP(measureWindSpeed); PREP(updateDisplay); +PREP(updateImpellerState); ADDON = true; diff --git a/addons/kestrel4500/functions/fnc_collectData.sqf b/addons/kestrel4500/functions/fnc_collectData.sqf index 5e6a6572b7..ef7be60cd8 100644 --- a/addons/kestrel4500/functions/fnc_collectData.sqf +++ b/addons/kestrel4500/functions/fnc_collectData.sqf @@ -42,21 +42,13 @@ if (GVAR(MinAvgMaxMode) == 1) then { } forEach [1, 2, 3]; // Wind SPD - _windSpeed = vectorMagnitude ACE_wind; - _windDir = (ACE_wind select 0) atan2 (ACE_wind select 1); - - if (missionNamespace getVariable [QEGVAR(advanced_ballistics,enabled), false]) then { - _windSpeed = (eyePos ACE_player) call EFUNC(advanced_ballistics,calculateWindSpeed); - _windSpeed = abs(cos(_playerDir - _windDir)) * _windSpeed; - } else { - _windSpeed = (eyePos ACE_player) call FUNC(calculateWindSpeed); - }; - + _windSpeed = call FUNC(measureWindSpeed); GVAR(MIN) set [1, (GVAR(MIN) select 1) min abs(_windSpeed)]; GVAR(MAX) set [1, abs(_windSpeed) max (GVAR(MAX) select 1)]; GVAR(TOTAL) set [1, (GVAR(TOTAL) select 1) + abs(_windSpeed)]; // CROSSWIND + _crosswind = 0; if (missionNamespace getVariable [QEGVAR(advanced_ballistics,enabled), false]) then { _crosswind = abs(sin(GVAR(RefHeading) - _playerDir) * _windSpeed); } else { @@ -67,6 +59,7 @@ if (GVAR(MinAvgMaxMode) == 1) then { GVAR(TOTAL) set [2, (GVAR(TOTAL) select 2) + _crosswind]; // HEADWIND + _headwind = 0; if (missionNamespace getVariable [QEGVAR(advanced_ballistics,enabled), false]) then { _headwind = abs(cos(GVAR(RefHeading) - _playerDir) * _windSpeed); } else { diff --git a/addons/kestrel4500/functions/fnc_displayKestrel.sqf b/addons/kestrel4500/functions/fnc_displayKestrel.sqf index b9245f5bee..2c1241bb6c 100644 --- a/addons/kestrel4500/functions/fnc_displayKestrel.sqf +++ b/addons/kestrel4500/functions/fnc_displayKestrel.sqf @@ -52,35 +52,34 @@ GVAR(Overlay) = true; [_this select 1] call CBA_fnc_removePerFrameHandler; }; - _outputData = [] call FUNC(generateOutputData); - if (diag_tickTime > GVAR(updateTimer)) then { GVAR(updateTimer) = diag_tickTime + 1; - GVAR(outputData) = _outputData; + + _outputData = [] call FUNC(generateOutputData); + + 3 cutRsc ["RscKestrel4500", "PLAIN", 1, false]; + + __ctrlTop ctrlSetText (_outputData select 0); + __ctrlCenterBig ctrlSetText (_outputData select 1); + + __ctrlTop ctrlSetText (_outputData select 0); + __ctrlCenterBig ctrlSetText (_outputData select 1); + + __ctrlCenterLine1Left ctrlSetText (_outputData select 2); + __ctrlCenterLine2Left ctrlSetText (_outputData select 3); + __ctrlCenterLine3Left ctrlSetText (_outputData select 4); + + __ctrlCenterLine1Right ctrlSetText (_outputData select 5); + __ctrlCenterLine2Right ctrlSetText (_outputData select 6); + __ctrlCenterLine3Right ctrlSetText (_outputData select 7); + + __ctrlInfoLine1 ctrlSetText (_outputData select 8); + __ctrlInfoLine2 ctrlSetText (_outputData select 9); }; - _outputData = GVAR(outputData); + call FUNC(updateImpellerState); + __ctrlKestrel4500 ctrlSetText format [QUOTE(PATHTOF(UI\Kestrel4500_%1.paa)), floor(GVAR(ImpellerState) % 7)]; - 3 cutRsc ["RscKestrel4500", "PLAIN", 1, false]; - - __ctrlKestrel4500 ctrlSetText format [QUOTE(PATHTOF(UI\Kestrel4500_%1.paa)), floor(GVAR(WheelState) % 7)]; - - __ctrlTop ctrlSetText (_outputData select 0); - __ctrlCenterBig ctrlSetText (_outputData select 1); - - __ctrlTop ctrlSetText (_outputData select 0); - __ctrlCenterBig ctrlSetText (_outputData select 1); - - __ctrlCenterLine1Left ctrlSetText (_outputData select 2); - __ctrlCenterLine2Left ctrlSetText (_outputData select 3); - __ctrlCenterLine3Left ctrlSetText (_outputData select 4); - - __ctrlCenterLine1Right ctrlSetText (_outputData select 5); - __ctrlCenterLine2Right ctrlSetText (_outputData select 6); - __ctrlCenterLine3Right ctrlSetText (_outputData select 7); - - __ctrlInfoLine1 ctrlSetText (_outputData select 8); - __ctrlInfoLine2 ctrlSetText (_outputData select 9); }, 0.01, []] call CBA_fnc_addPerFrameHandler; true diff --git a/addons/kestrel4500/functions/fnc_generateOutputData.sqf b/addons/kestrel4500/functions/fnc_generateOutputData.sqf index 3d874edade..954b886eff 100644 --- a/addons/kestrel4500/functions/fnc_generateOutputData.sqf +++ b/addons/kestrel4500/functions/fnc_generateOutputData.sqf @@ -17,8 +17,6 @@ private ["_playerDir", "_textTop", "_textCenterBig", "_textCenterLine1Left", "_textCenterLine2Left", "_textCenterLine3Left", "_textCenterLine1Right", "_textCenterLine2Right", "_textCenterLine3Right", "_textInfoLine1", "_textInfoLine2", "_temperature", "_humidity", "_windSpeed", "_windDir", "_newWindSpeed", "_windSource", "_height"]; -if (isNil QUOTE(EGVAR(weather,Altitude))) then {EGVAR(weather,Altitude) = 0}; - [] call FUNC(collectData); _textTop = GVAR(Menus) select GVAR(Menu); @@ -34,7 +32,7 @@ _textCenterLine3Right = ""; _textInfoLine1 = ""; _textInfoLine2 = ""; -_windSpeed = vectorMagnitude ACE_wind; +_windSpeed = call FUNC(measureWindSpeed); _windDir = (ACE_wind select 0) atan2 (ACE_wind select 1); _temperature = GET_TEMPERATURE_AT_HEIGHT((getPosASL ACE_player) select 2); @@ -42,25 +40,6 @@ _humidity = EGVAR(weather,currentHumidity); _playerDir = getDir ACE_player; -if (missionNamespace getVariable [QEGVAR(advanced_ballistics,enabled), false]) then { - _windSpeed = (eyePos ACE_player) call EFUNC(advanced_ballistics,calculateWindSpeed); - _windSpeed = abs(cos(_playerDir - _windDir)) * _windSpeed; -} else { - _windSpeed = (eyePos ACE_player) call FUNC(calculateWindSpeed); -}; - -if (_windSpeed > 0.3) then { - GVAR(MeasuredWindSpeed) = _windSpeed; -} else { - GVAR(MeasuredWindSpeed) = GVAR(MeasuredWindSpeed) * 0.99; - if (GVAR(MeasuredWindSpeed) < 0.05) then { - GVAR(MeasuredWindSpeed) = 0; - }; -}; - -GVAR(WheelState) = GVAR(WheelState) + (ceil(GVAR(MeasuredWindSpeed)) min 1) max GVAR(MeasuredWindSpeed); -if (GVAR(WheelState) > 1000) then { GVAR(WheelState) = 0 }; - GVAR(Direction) = 4 * floor(_playerDir / 90); if (_playerDir % 90 > 10) then { GVAR(Direction) = GVAR(Direction) + 1}; if (_playerDir % 90 > 35) then { GVAR(Direction) = GVAR(Direction) + 1}; @@ -83,7 +62,7 @@ switch (GVAR(Menu)) do { }; case 1: { // Wind SPD if (!GVAR(MinAvgMax)) then { - _textCenterBig = Str(round(abs(GVAR(MeasuredWindSpeed)) * 10) / 10); + _textCenterBig = Str(round(abs(_windSpeed) * 10) / 10); } else { _textCenterLine1Left = "Max"; _textCenterLine2Left = "Avg"; @@ -109,11 +88,11 @@ switch (GVAR(Menu)) do { case 2: { // CROSSWIND if (!GVAR(MinAvgMax)) then { if (missionNamespace getVariable [QEGVAR(advanced_ballistics,enabled), false]) then { - _textCenterBig = Str(round(abs(sin(GVAR(RefHeading) - _playerDir) * GVAR(MeasuredWindSpeed)) * 10) / 10); - _textInfoLine1 = format["%1 m/s @ %2", round((cos(_playerDir - _windDir) * GVAR(MeasuredWindSpeed)) * 10) / 10, round(_playerDir)]; + _textCenterBig = Str(round(abs(sin(GVAR(RefHeading) - _playerDir) * _windSpeed) * 10) / 10); + _textInfoLine1 = format["%1 m/s @ %2", round((cos(_playerDir - _windDir) * _windSpeed) * 10) / 10, round(_playerDir)]; } else { - _textCenterBig = Str(round(abs(sin(GVAR(RefHeading)) * GVAR(MeasuredWindSpeed)) * 10) / 10); - _textInfoLine1 = format["%1 m/s @ %2", round(GVAR(MeasuredWindSpeed) * 10) / 10, round(_windDir)]; + _textCenterBig = Str(round(abs(sin(GVAR(RefHeading)) * _windSpeed) * 10) / 10); + _textInfoLine1 = format["%1 m/s @ %2", round(_windSpeed * 10) / 10, round(_windDir)]; }; _textInfoLine2 = "- set heading"; } else { @@ -141,11 +120,11 @@ switch (GVAR(Menu)) do { case 3: { // HEADWIND if (!GVAR(MinAvgMax)) then { if (missionNamespace getVariable [QEGVAR(advanced_ballistics,enabled), false]) then { - _textCenterBig = Str(round(abs(cos(GVAR(RefHeading) - _playerDir) * GVAR(MeasuredWindSpeed)) * 10) / 10); - _textInfoLine1 = format["%1 m/s @ %2", round((cos(_playerDir - _windDir) * GVAR(MeasuredWindSpeed)) * 10) / 10, round(_playerDir)]; + _textCenterBig = Str(round(abs(cos(GVAR(RefHeading) - _playerDir) * _windSpeed) * 10) / 10); + _textInfoLine1 = format["%1 m/s @ %2", round((cos(_playerDir - _windDir) * _windSpeed) * 10) / 10, round(_playerDir)]; } else { - _textCenterBig = Str(round(abs(cos(GVAR(RefHeading)) * GVAR(MeasuredWindSpeed)) * 10) / 10); - _textInfoLine1 = format["%1 m/s @ %2", round(GVAR(MeasuredWindSpeed) * 10) / 10, round(_windDir)]; + _textCenterBig = Str(round(abs(cos(GVAR(RefHeading)) * _windSpeed) * 10) / 10); + _textInfoLine1 = format["%1 m/s @ %2", round(_windSpeed * 10) / 10, round(_windDir)]; }; _textInfoLine2 = "- set heading"; } else { @@ -221,7 +200,7 @@ switch (GVAR(Menu)) do { case 8: { // User Screen 1 _textCenterLine1Left = Str(round(_playerDir)); _textCenterLine2Left = Str(round(EGVAR(weather,Altitude) + ((getPosASL ACE_player) select 2))); - _textCenterLine3Left = Str(round(abs(GVAR(MeasuredWindSpeed)) * 10) / 10); + _textCenterLine3Left = Str(round(abs(_windSpeed) * 10) / 10); _textCenterLine1Right = GVAR(Directions) select GVAR(Direction); _textCenterLine2Right = "m"; _textCenterLine3Right = "m/s"; diff --git a/addons/kestrel4500/functions/fnc_measureWindSpeed.sqf b/addons/kestrel4500/functions/fnc_measureWindSpeed.sqf new file mode 100644 index 0000000000..7c18499e7d --- /dev/null +++ b/addons/kestrel4500/functions/fnc_measureWindSpeed.sqf @@ -0,0 +1,42 @@ +/* + * Author: Ruthberg + * Measures the wind speed, stores the information in GVAR(MeasuredWindSpeed) and updates GVAR(ImpellerState) + * + * Arguments: + * Nothing + * + * Return Value: + * wind speed + * + * Example: + * + * Public: No + */ +#include "script_component.hpp" +#include "defines.h" + +private ["_playerDir", "_windSpeed", "_windDir"]; + +_playerDir = getDir ACE_player; +_windSpeed = vectorMagnitude ACE_wind; +_windDir = (ACE_wind select 0) atan2 (ACE_wind select 1); + +if (missionNamespace getVariable [QEGVAR(advanced_ballistics,enabled), false]) then { + _windSpeed = (eyePos ACE_player) call EFUNC(advanced_ballistics,calculateWindSpeed); + _windSpeed = abs(cos(_playerDir - _windDir)) * _windSpeed; +} else { + _windSpeed = (eyePos ACE_player) call FUNC(calculateWindSpeed); +}; + +hintSilent format["%1, %2", GVAR(MeasuredWindSpeed), _windSpeed]; + +if (_windSpeed > 0.3 || {GVAR(MeasuredWindSpeed) > 0.1 && _windSpeed > 0.125}) then { + GVAR(MeasuredWindSpeed) = _windSpeed; +} else { + GVAR(MeasuredWindSpeed) = GVAR(MeasuredWindSpeed) * 0.99; + if (GVAR(MeasuredWindSpeed) < 0.05) then { + GVAR(MeasuredWindSpeed) = 0; + }; +}; + +GVAR(MeasuredWindSpeed) diff --git a/addons/kestrel4500/functions/fnc_updateImpellerState.sqf b/addons/kestrel4500/functions/fnc_updateImpellerState.sqf new file mode 100644 index 0000000000..075ed80080 --- /dev/null +++ b/addons/kestrel4500/functions/fnc_updateImpellerState.sqf @@ -0,0 +1,22 @@ +/* + * Author: Ruthberg + * Updates the Kestrel 4500 Impeller state + * + * Arguments: + * Nothing + * + * Return Value: + * Nothing + * + * Example: + * + * Public: No + */ +#include "script_component.hpp" + +private ["_windSpeed"]; + +_windSpeed = call FUNC(measureWindSpeed); + +GVAR(ImpellerState) = GVAR(ImpellerState) + (ceil(_windSpeed) min 1) max _windSpeed; +if (GVAR(ImpellerState) > 1000) then { GVAR(ImpellerState) = 0 };