From 52ed0fc6be5669521dd4acdd756e6afd18e4fe6d Mon Sep 17 00:00:00 2001 From: BaerMitUmlaut Date: Sat, 22 Jul 2023 08:41:20 +0200 Subject: [PATCH] Medical Engine - Prevent automatic unloading of dead or unconscious units (#7959) * Lock seats of unconscious or dead units * disable pullOutBody if medical is loaded * fix undefined var and switch to objectParent --------- Co-authored-by: Salluci Co-authored-by: Grim <69561145+LinkIsGrim@users.noreply.github.com> --- .../functions/fnc_canPullOutBody.sqf | 3 ++ addons/medical_engine/XEH_PREP.hpp | 2 + addons/medical_engine/XEH_postInit.sqf | 29 +++++++++++++-- .../functions/fnc_lockUnconsciousSeat.sqf | 37 +++++++++++++++++++ .../functions/fnc_unlockUnconsciousSeat.sqf | 35 ++++++++++++++++++ 5 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 addons/medical_engine/functions/fnc_lockUnconsciousSeat.sqf create mode 100644 addons/medical_engine/functions/fnc_unlockUnconsciousSeat.sqf diff --git a/addons/interaction/functions/fnc_canPullOutBody.sqf b/addons/interaction/functions/fnc_canPullOutBody.sqf index 7b7d93418c..732d373dd6 100644 --- a/addons/interaction/functions/fnc_canPullOutBody.sqf +++ b/addons/interaction/functions/fnc_canPullOutBody.sqf @@ -18,6 +18,9 @@ params ["_body", "_unit"]; +// Defer to ACE Medical's unload patient if present +if (["ace_medical"] call EFUNC(common,isModLoaded)) exitWith {false}; + private _vehicle = objectParent _body; if ( diff --git a/addons/medical_engine/XEH_PREP.hpp b/addons/medical_engine/XEH_PREP.hpp index 63faaf7bbe..ffc3543745 100644 --- a/addons/medical_engine/XEH_PREP.hpp +++ b/addons/medical_engine/XEH_PREP.hpp @@ -4,7 +4,9 @@ PREP(disableThirdParty); PREP(getHitpointArmor); PREP(getItemArmor); PREP(handleDamage); +PREP(lockUnconsciousSeat); PREP(setStructuralDamage); PREP(setUnconsciousAnim); +PREP(unlockUnconsciousSeat); PREP(updateBodyPartVisuals); PREP(updateDamageEffects); diff --git a/addons/medical_engine/XEH_postInit.sqf b/addons/medical_engine/XEH_postInit.sqf index 2ded996bbf..f1f1780fa8 100644 --- a/addons/medical_engine/XEH_postInit.sqf +++ b/addons/medical_engine/XEH_postInit.sqf @@ -42,12 +42,23 @@ // this handles moving units into vehicles via load functions or zeus // needed, because the vanilla INCAPACITATED state does not handle vehicles ["CAManBase", "GetInMan", { - params ["_unit"]; - if (!local _unit) exitWith {}; + params ["_unit", "", "_vehicle"]; - if (lifeState _unit == "INCAPACITATED") then { + if (local _unit && {lifeState _unit == "INCAPACITATED"}) then { [_unit, true] call FUNC(setUnconsciousAnim); }; + + if (local _vehicle) then { + [_unit] call FUNC(lockUnconsciousSeat); + }; +}] call CBA_fnc_addClassEventHandler; + +["CAManBase", "GetOutMan", { + params ["_unit", "", "_vehicle"]; + + if (local _vehicle) then { + [_unit] call FUNC(unlockUnconsciousSeat); + }; }] call CBA_fnc_addClassEventHandler; // Guarantee aircraft crashes are more lethal @@ -71,3 +82,15 @@ [_unit, false] call FUNC(setUnconsciousAnim); }; }] call CBA_fnc_addClassEventHandler; + +["ace_unconscious", { + params ["_unit", "_unconscious"]; + + if (vehicle _unit != _unit && {local vehicle _unit}) then { + if (_unconscious) then { + [_unit] call FUNC(lockUnconsciousSeat); + } else { + [_unit] call FUNC(unlockUnconsciousSeat); + }; + }; +}] call CBA_fnc_addEventHandler; diff --git a/addons/medical_engine/functions/fnc_lockUnconsciousSeat.sqf b/addons/medical_engine/functions/fnc_lockUnconsciousSeat.sqf new file mode 100644 index 0000000000..97d514278e --- /dev/null +++ b/addons/medical_engine/functions/fnc_lockUnconsciousSeat.sqf @@ -0,0 +1,37 @@ +#include "script_component.hpp" +/* + * Author: BaerMitUmlaut + * Locks the seat of an unconscious or dead unit to prevent automatic unloading. + * + * Arguments: + * 0: Unit + * + * Return Value: + * None + * + * Public: No + */ +params ["_unit"]; + +private _vehicle = objectParent _unit; + +if (alive _unit && {lifeState _unit != "INCAPACITATED"}) exitWith {}; + +switch (true) do { + case (_unit isEqualTo (driver _vehicle)): { + _vehicle lockDriver true; + _unit setVariable [QGVAR(lockedSeat), [_vehicle, "driver"], true]; + }; + + case (_vehicle getCargoIndex _unit != -1): { + private _cargoIndex = _vehicle getCargoIndex _unit; + _vehicle lockCargo [_cargoIndex, true]; + _unit setVariable [QGVAR(lockedSeat), [_vehicle, "cargo", _cargoIndex], true]; + }; + + case ((_vehicle unitTurret _unit) isNotEqualTo []): { + private _turretPath = _vehicle unitTurret _unit; + _vehicle lockTurret [_turretPath, true]; + _unit setVariable [QGVAR(lockedSeat), [_vehicle, "turret", _turretPath], true]; + }; +}; diff --git a/addons/medical_engine/functions/fnc_unlockUnconsciousSeat.sqf b/addons/medical_engine/functions/fnc_unlockUnconsciousSeat.sqf new file mode 100644 index 0000000000..6c772b5c24 --- /dev/null +++ b/addons/medical_engine/functions/fnc_unlockUnconsciousSeat.sqf @@ -0,0 +1,35 @@ +#include "script_component.hpp" +/* + * Author: BaerMitUmlaut + * Unlocks the seat of an unconscious or dead unit after getting moved out or waking up. + * + * Arguments: + * 0: Unit + * + * Return Value: + * None + * + * Public: No + */ +params ["_unit"]; + +private _seat = _unit getVariable [QGVAR(lockedSeat), []]; +_seat params ["_vehicle", "_type", "_position"]; + +if (_seat isEqualTo []) exitWith {}; + +switch (_type) do { + case "driver": { + _vehicle lockDriver false; + }; + + case "cargo": { + _vehicle lockCargo [_position, false]; + }; + + case "turret": { + _vehicle lockTurret [_position, false]; + }; +}; + +_unit setVariable [QGVAR(lockedSeat), nil, true];