From d6c39e661f82015b281c098394f73c7c24b76799 Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Thu, 5 Sep 2019 15:56:14 -0500 Subject: [PATCH] Medical StateMachine - Tweak dead state handling (#7061) * Medical StateMachine - Tweak dead state handling - Don't call setDammage inside HandleDamage EH - Add death reason on entering death state * add a getVar disable in case this causes problems * Cleanup * cleanup * Update addons/medical_statemachine/functions/fnc_enteredStateDeath.sqf Co-Authored-By: Ferran Obon --- .../functions/fnc_enteredStateDeath.sqf | 10 +++-- addons/medical_status/XEH_PREP.hpp | 1 + addons/medical_status/XEH_preInit.sqf | 15 +++++++ .../functions/fnc_handleKilled.sqf | 41 +++++++++++++++++++ .../medical_status/functions/fnc_setDead.sqf | 13 +++--- 5 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 addons/medical_status/functions/fnc_handleKilled.sqf diff --git a/addons/medical_statemachine/functions/fnc_enteredStateDeath.sqf b/addons/medical_statemachine/functions/fnc_enteredStateDeath.sqf index d99d13faca..cb6f63c39a 100644 --- a/addons/medical_statemachine/functions/fnc_enteredStateDeath.sqf +++ b/addons/medical_statemachine/functions/fnc_enteredStateDeath.sqf @@ -16,10 +16,12 @@ */ params ["_unit"]; +if (isNull _unit) exitWith {}; -// TODO: Probably also needs additional logic to deal with edge cases +//IGNORE_PRIVATE_WARNING ["_thisOrigin", "_thisTransition"]; // vars provided by CBA_statemachine +TRACE_3("enteredStateDeath",_this,_thisOrigin,_thisTransition); -// Send a local event before death -[QEGVAR(medical,death), [_unit]] call CBA_fnc_localEvent; +private _causeOfDeath = format ["%1:%2", _thisOrigin, _thisTransition]; -[_unit] call EFUNC(medical_status,setDead); +// could delay a frame here to fix the double killed EH, but we lose it being a "native" kill (scoreboard / rating) +[_unit, _causeOfDeath] call EFUNC(medical_status,setDead); diff --git a/addons/medical_status/XEH_PREP.hpp b/addons/medical_status/XEH_PREP.hpp index 32e02d18c8..18f5ee699a 100644 --- a/addons/medical_status/XEH_PREP.hpp +++ b/addons/medical_status/XEH_PREP.hpp @@ -4,6 +4,7 @@ PREP(getBloodLoss); PREP(getBloodPressure); PREP(getBloodVolumeChange); PREP(getCardiacOutput); +PREP(handleKilled); PREP(hasStableVitals); PREP(initUnit); PREP(isBeingCarried); diff --git a/addons/medical_status/XEH_preInit.sqf b/addons/medical_status/XEH_preInit.sqf index 9361d05015..9ef36381eb 100644 --- a/addons/medical_status/XEH_preInit.sqf +++ b/addons/medical_status/XEH_preInit.sqf @@ -8,4 +8,19 @@ PREP_RECOMPILE_END; #include "initSettings.sqf" +// Add vanilla killed EH to unit to set correct killer +["CAManBase", "init", { + params ["_unit"]; + + private _config = [_unit] call CBA_fnc_getObjectConfig; + if (getText (_config >> "simulation") == "UAVPilot") exitWith {TRACE_1("ignore UAV AI",typeOf _unit);}; + if (getNumber (_config >> "isPlayableLogic") == 1) exitWith {TRACE_1("ignore logic unit",typeOf _unit)}; + + // Hopefully this EH gets added first as it can only effect other EH called after it + private _ehIndex = _unit addEventHandler ["Killed", {_this call FUNC(handleKilled)}]; + #ifdef DEBUG_MODE_FULL + if (_ehIndex != 0) then { WARNING_1("killed EH not first [%1]",_ehIndex); }; + #endif +}, nil, [IGNORE_BASE_UAVPILOTS], true] call CBA_fnc_addClassEventHandler; + ADDON = true; diff --git a/addons/medical_status/functions/fnc_handleKilled.sqf b/addons/medical_status/functions/fnc_handleKilled.sqf new file mode 100644 index 0000000000..25099a731d --- /dev/null +++ b/addons/medical_status/functions/fnc_handleKilled.sqf @@ -0,0 +1,41 @@ +#include "script_component.hpp" +/* + * Author: PabstMirror + * Vanilla Killed EH, attempts to set correct source/killer for other killed event handlers (vanilla and XEH) + * + * Arguments: + * 0: Unit + * 1: Killer + * 2: Instigator + * 3: Use Effects + * + * Return Value: + * None + * + * Example: + * [cursorObject, player, player, true] call ace_medical_status_fnc_handleKilled + * + * Public: No + */ + +params ["_unit", "_killer", "_instigator", "_useEffects"]; +TRACE_4("handleKilled",_unit,_killer,_instigator,_useEffects); + +private _causeOfDeath = _unit getVariable [QEGVAR(medical,causeOfDeath), "#scripted"]; + +// if undefined then it's a death not caused by ace's setDead (mission setDamage, disconnect) +if (_causeOfDeath != "#scripted") then { + _killer = _unit getVariable [QEGVAR(medical,lastDamageSource), _killer]; // vehicle + _instigator = _unit getVariable [QEGVAR(medical,lastInstigator), _instigator]; // unit in the turret + + // All Killed EHs uses the same array, so we can modify it now to pass the correct killer/instigator + if (missionNamespace getVariable [QEGVAR(medical,modifyKilledArray), true]) then { // getVar so this can be disabled + _this set [1, _killer]; + _this set [2, _instigator]; + }; +}; +TRACE_3("killer info",_killer,_instigator,_causeOfDeath); + +if (_unit isEqualTo (_unit getVariable [QGVAR(killed), objNull])) exitWith {}; // ensure event is only called once +_unit setVariable [QGVAR(killed), _unit]; +["ace_killed", [_unit, _causeOfDeath, _killer, _instigator]] call CBA_fnc_globalEvent; diff --git a/addons/medical_status/functions/fnc_setDead.sqf b/addons/medical_status/functions/fnc_setDead.sqf index b76ced3ddc..7f41ebf3da 100644 --- a/addons/medical_status/functions/fnc_setDead.sqf +++ b/addons/medical_status/functions/fnc_setDead.sqf @@ -13,16 +13,17 @@ * Public: No */ -params ["_unit", ["_reason", "unknown"]]; +params ["_unit", ["_reason", "#setDead"]]; +TRACE_2("setDead",_unit,_reason); // No heart rate or blood pressure to measure when dead _unit setVariable [VAR_HEART_RATE, 0, true]; _unit setVariable [VAR_BLOOD_PRESS, [0, 0], true]; +_unit setVariable [QEGVAR(medical,causeOfDeath), _reason, true]; + +// Send a local event before death +[QEGVAR(medical,death), [_unit]] call CBA_fnc_localEvent; + // Kill the unit without changing visual apperance [_unit, 1] call EFUNC(medical_engine,setStructuralDamage); - -private _lastShooter = _unit getVariable [QEGVAR(medical,lastDamageSource), objNull]; -private _lastInstigator = _unit getVariable [QEGVAR(medical,lastInstigator), objNull]; - -["ace_killed", [_unit, _reason, _lastShooter, _lastInstigator]] call CBA_fnc_globalEvent;