From 130348d3a59397c89bf31c540d83be2ce651fd9d Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Sun, 9 Jun 2024 18:20:17 -0500 Subject: [PATCH] Hit Reactions - Prevent AI's hitpart EH running on all clients (#10057) also dynamically add ehs --- addons/hitreactions/XEH_postInit.sqf | 42 ++++++++++++++---------- addons/hitreactions/XEH_preInit.sqf | 4 +-- addons/hitreactions/initSettings.inc.sqf | 6 ++-- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/addons/hitreactions/XEH_postInit.sqf b/addons/hitreactions/XEH_postInit.sqf index d45d4f3df2..2750ae1159 100644 --- a/addons/hitreactions/XEH_postInit.sqf +++ b/addons/hitreactions/XEH_postInit.sqf @@ -1,27 +1,35 @@ #include "script_component.hpp" -["ace_firedNonPlayer", { - if (GVAR(weaponDropChanceArmHitPlayer) + GVAR(weaponDropChanceArmHitAI) == 0) exitWith {}; - (_this select 6) addEventHandler ["HitPart", { - params ["", "_entity", "", "", "", "", "_selections"]; - - [_entity, _selections] call FUNC(checkWeaponDrop); - }]; -}] call CBA_fnc_addEventHandler; - -["ace_firedPlayer", { - if (GVAR(weaponDropChanceArmHitPlayer) + GVAR(weaponDropChanceArmHitAI) == 0) exitWith {}; - - (_this select 6) addEventHandler ["HitPart", { - params ["", "_entity", "", "", "", "", "_selections"]; - - [_entity, _selections] call FUNC(checkWeaponDrop); - }]; +[QGVAR(updateFiredEHs), { + TRACE_2("updateFiredEH",GVAR(weaponDropChanceArmHitPlayer),GVAR(weaponDropChanceArmHitAI)); + if (GVAR(weaponDropChanceArmHitPlayer) + GVAR(weaponDropChanceArmHitAI) == 0) then { + if (isNil QGVAR(firedEHs)) exitWith {}; + { + _x call CBA_fnc_removeEventHandler; + } forEach GVAR(firedEHs); + GVAR(firedEHs) = nil; + TRACE_1("removed EHs",GVAR(firedEHs)); + } else { + if (!isNil QGVAR(firedEHs)) exitWith {}; + private _firedEH = { + if (!local (_this select 0)) exitWith {}; + (_this select 6) addEventHandler ["HitPart", { + params ["", "_entity", "", "", "", "", "_selections"]; + [_entity, _selections] call FUNC(checkWeaponDrop); + }]; + }; + GVAR(firedEHs) = []; + { + GVAR(firedEHs) pushBack [_x, [_x, _firedEH] call CBA_fnc_addEventHandler]; + } forEach ["ace_firedNonPlayer", "ace_firedPlayer"]; + TRACE_1("added EHs",GVAR(firedEHs)); + }; }] call CBA_fnc_addEventHandler; [QGVAR(dropWeapon), { params ["_unit"]; + TRACE_1("dropWeaponEH",_unit); if !(_unit getVariable [QGVAR(canDropWeapon), true]) exitWith {}; diff --git a/addons/hitreactions/XEH_preInit.sqf b/addons/hitreactions/XEH_preInit.sqf index 9aa0acea0e..296279c7eb 100644 --- a/addons/hitreactions/XEH_preInit.sqf +++ b/addons/hitreactions/XEH_preInit.sqf @@ -8,8 +8,6 @@ PREP_RECOMPILE_END; #include "initSettings.inc.sqf" -ADDON = true; - GVAR(armSelections) = [ "leftshoulder", "rightshoulder", @@ -21,3 +19,5 @@ GVAR(armSelections) = [ "righthand", "rightarm" ]; + +ADDON = true; diff --git a/addons/hitreactions/initSettings.inc.sqf b/addons/hitreactions/initSettings.inc.sqf index 2f51a25647..820f664767 100644 --- a/addons/hitreactions/initSettings.inc.sqf +++ b/addons/hitreactions/initSettings.inc.sqf @@ -15,7 +15,8 @@ private _category = [LELSTRING(common,categoryUncategorized), QUOTE(COMPONENT_BE LSTRING(weaponDropChanceArmHitPlayer_displayName), _category, [0, 1, 0, 2, true], - 1 + 1, + {[QGVAR(updateFiredEHs)] call CBA_fnc_localEvent} ] call CBA_fnc_addSetting; [ @@ -24,5 +25,6 @@ private _category = [LELSTRING(common,categoryUncategorized), QUOTE(COMPONENT_BE LSTRING(weaponDropChanceArmHitAI_displayName), _category, [0, 1, 0, 2, true], - 1 + 1, + {[QGVAR(updateFiredEHs)] call CBA_fnc_localEvent} ] call CBA_fnc_addSetting;