From d44f116961db24bb4de7d10430c61c24df0db354 Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Mon, 12 Aug 2019 13:24:26 -0500 Subject: [PATCH 1/3] Medical Statemachine - Fix locality EH breaking current state --- .../medical_statemachine/CfgEventHandlers.hpp | 6 --- addons/medical_statemachine/XEH_PREP.hpp | 1 - addons/medical_statemachine/XEH_postInit.sqf | 3 -- .../functions/fnc_localityChangedEH.sqf | 53 +++++++++++++++---- .../functions/fnc_localityTransfer.sqf | 26 --------- 5 files changed, 43 insertions(+), 46 deletions(-) delete mode 100644 addons/medical_statemachine/XEH_postInit.sqf delete mode 100644 addons/medical_statemachine/functions/fnc_localityTransfer.sqf diff --git a/addons/medical_statemachine/CfgEventHandlers.hpp b/addons/medical_statemachine/CfgEventHandlers.hpp index ab659e1cda6..7196bf8aaf5 100644 --- a/addons/medical_statemachine/CfgEventHandlers.hpp +++ b/addons/medical_statemachine/CfgEventHandlers.hpp @@ -10,12 +10,6 @@ class Extended_PreInit_EventHandlers { }; }; -class Extended_PostInit_EventHandlers { - class ADDON { - init = QUOTE(call COMPILE_FILE(XEH_postInit)); - }; -}; - class Extended_Respawn_EventHandlers { class CAManBase { class ADDON { diff --git a/addons/medical_statemachine/XEH_PREP.hpp b/addons/medical_statemachine/XEH_PREP.hpp index 8e2725d3da7..228f6616dcc 100644 --- a/addons/medical_statemachine/XEH_PREP.hpp +++ b/addons/medical_statemachine/XEH_PREP.hpp @@ -9,6 +9,5 @@ PREP(handleStateInjured); PREP(handleStateUnconscious); PREP(leftStateCardiacArrest); PREP(localityChangedEH); -PREP(localityTransfer); PREP(resetStateDefault); PREP(transitionSecondChance); diff --git a/addons/medical_statemachine/XEH_postInit.sqf b/addons/medical_statemachine/XEH_postInit.sqf deleted file mode 100644 index 89d8891fdb5..00000000000 --- a/addons/medical_statemachine/XEH_postInit.sqf +++ /dev/null @@ -1,3 +0,0 @@ -#include "script_component.hpp" - -[QGVAR(localityTransfer), LINKFUNC(localityTransfer)] call CBA_fnc_addEventHandler; diff --git a/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf b/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf index 1bcc9a9bde1..9297f82774b 100644 --- a/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf +++ b/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf @@ -1,7 +1,8 @@ #include "script_component.hpp" /* * Author: PabstMirror - * Handles locality switch. + * Handles locality switch. Will also be called at unit init. + * Because state machine state is local only, when a unit transfers locality we need to manually transition to it's current state * * Arguments: * 0: Unit @@ -19,15 +20,47 @@ params ["_unit", "_isLocal"]; TRACE_2("localityChangedEH",_unit,_isLocal); -if (!alive _unit) exitWith {TRACE_1("dead", _this)}; - -if (!_isLocal) then { - // If locality changed, broadcast the last medical state - _unit setVariable [VAR_HEART_RATE, GET_HEART_RATE(_unit), true]; - _unit setVariable [VAR_BLOOD_PRESS, _unit getVariable [VAR_BLOOD_PRESS, [80, 120]], true]; - _unit setVariable [VAR_BLOOD_VOL, GET_BLOOD_VOLUME(_unit), true]; +if (!alive _unit) exitWith {}; +if (_isLocal) then { private _currentState = [_unit, EGVAR(medical,STATE_MACHINE)] call CBA_statemachine_fnc_getCurrentState; - TRACE_2("sending current state",_unit,_currentState); - [QGVAR(localityTransfer), [_unit, _currentState], _unit] call CBA_fnc_targetEvent; + TRACE_1("local",_currentState); + + switch (true) do { + case (IN_CRDC_ARRST(_unit)): { + if (_currentState == "CardiacArrest") exitWith {}; + _unit setVariable [VAR_CRDC_ARRST, false]; // force reset vars so setCardiacArrest can run (enteredStateCardiacArrest will also be called) + _unit setVariable [VAR_UNCON, false]; + TRACE_1("manually changing state to CardiacArrest",_currentState); + [_unit, EGVAR(medical,STATE_MACHINE), _currentState, "CardiacArrest", {}, "LocalityChange"] call CBA_statemachine_fnc_manualTransition; + }; + case (IS_UNCONSCIOUS(_unit)): { + if (_currentState == "Unconscious") exitWith {}; + _unit setVariable [VAR_UNCON, false]; // force reset var so ace_medical_status_fnc_setUnconscious can run + TRACE_1("manually changing state to Unconscious",_currentState); + [_unit, EGVAR(medical,STATE_MACHINE), _currentState, "Unconscious", {}, "LocalityChange"] call CBA_statemachine_fnc_manualTransition; + }; + // case (Injured?) - probably not needed, "Default" and "Injured" are the same + default { + // If locality transfers back and forth, we could be in an old state and should transfer back to default + if (_currentState == "Default") exitWith {}; + if (_currentState == "Injured") exitWith {}; + TRACE_1("manually changing state to Default",_currentState); + [_unit, EGVAR(medical,STATE_MACHINE), _currentState, "Default", {}, "LocalityChange"] call CBA_statemachine_fnc_manualTransition; + }; + }; +} else { + /* + // Not sure if this is even needed, idea is that on locality transfer we broadcast more up to date info + + private _lastTimeUpdated = _unit getVariable [QEGVAR(medical_vitals,lastTimeUpdated), 1e99]; + private _deltaT = CBA_missionTime - _lastTimeUpdated; + TRACE_1("not local",_deltaT); + if (_deltaT < 5) then { + // If locality changed and we have recently updated vitals, broadcast globally now + _unit setVariable [VAR_HEART_RATE, GET_HEART_RATE(_unit), true]; + _unit setVariable [VAR_BLOOD_PRESS, _unit getVariable [VAR_BLOOD_PRESS, [80, 120]], true]; + _unit setVariable [VAR_BLOOD_VOL, GET_BLOOD_VOLUME(_unit), true]; + }; + */ }; diff --git a/addons/medical_statemachine/functions/fnc_localityTransfer.sqf b/addons/medical_statemachine/functions/fnc_localityTransfer.sqf deleted file mode 100644 index abf4ddd5a34..00000000000 --- a/addons/medical_statemachine/functions/fnc_localityTransfer.sqf +++ /dev/null @@ -1,26 +0,0 @@ -#include "script_component.hpp" -/* - * Author: PabstMirror - * Handles locality switch. - * - * Arguments: - * 0: Unit - * 1: State - * - * Return Value: - * None - * - * Example: - * [player, "Injured"] call ace_medical_statemachine_fnc_localityTransfer - * - * Public: No - */ - -params ["_unit", "_currentState"]; -TRACE_2("localityTransfer",_unit,_currentState); - -private _oldState = [_unit, EGVAR(medical,STATE_MACHINE)] call CBA_statemachine_fnc_getCurrentState; -if (_oldState != _currentState) then { - TRACE_2("changing state",_oldState,_currentState); - [_unit, EGVAR(medical,STATE_MACHINE), _oldState, _currentState, {}, "LocalityChange"] call CBA_statemachine_fnc_manualTransition; -}; From e97545e1a86fff2628ac82700b4180067fe254f1 Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Tue, 3 Sep 2019 10:21:04 -0500 Subject: [PATCH 2/3] Handle Injured state --- .../functions/fnc_localityChangedEH.sqf | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf b/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf index 9297f82774b..1938b958419 100644 --- a/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf +++ b/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf @@ -40,11 +40,14 @@ if (_isLocal) then { TRACE_1("manually changing state to Unconscious",_currentState); [_unit, EGVAR(medical,STATE_MACHINE), _currentState, "Unconscious", {}, "LocalityChange"] call CBA_statemachine_fnc_manualTransition; }; - // case (Injured?) - probably not needed, "Default" and "Injured" are the same + case (IS_BLEEDING(_unit) || {IS_IN_PAIN(_unit)}): { + if (_currentState == "Injured") exitWith {}; + TRACE_1("manually changing state to Injured",_currentState); + [_unit, EGVAR(medical,STATE_MACHINE), _currentState, "Injured", {}, "LocalityChange"] call CBA_statemachine_fnc_manualTransition; + }; default { // If locality transfers back and forth, we could be in an old state and should transfer back to default - if (_currentState == "Default") exitWith {}; - if (_currentState == "Injured") exitWith {}; + if (_currentState == "Default")) exitWith {}; TRACE_1("manually changing state to Default",_currentState); [_unit, EGVAR(medical,STATE_MACHINE), _currentState, "Default", {}, "LocalityChange"] call CBA_statemachine_fnc_manualTransition; }; From 5e86e2c6b281f11b83689ff10dce75eafc0d4e5f Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Tue, 3 Sep 2019 10:23:40 -0500 Subject: [PATCH 3/3] Update fnc_localityChangedEH.sqf --- addons/medical_statemachine/functions/fnc_localityChangedEH.sqf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf b/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf index 1938b958419..e76784a939f 100644 --- a/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf +++ b/addons/medical_statemachine/functions/fnc_localityChangedEH.sqf @@ -47,7 +47,7 @@ if (_isLocal) then { }; default { // If locality transfers back and forth, we could be in an old state and should transfer back to default - if (_currentState == "Default")) exitWith {}; + if (_currentState == "Default") exitWith {}; TRACE_1("manually changing state to Default",_currentState); [_unit, EGVAR(medical,STATE_MACHINE), _currentState, "Default", {}, "LocalityChange"] call CBA_statemachine_fnc_manualTransition; };