From 16e63df1077dd992a8fcd47f5c45894f8a5d0959 Mon Sep 17 00:00:00 2001 From: LinkIsGrim <> Date: Thu, 23 May 2024 17:44:10 -0300 Subject: [PATCH] switch to 2.18 commands --- addons/common/XEH_PREP.hpp | 1 + addons/common/XEH_postInit.sqf | 79 ++++--------------- .../common/functions/fnc_addReloadMutex.sqf | 71 +++++++++++++++++ .../functions/fnc_removeReloadMutex.sqf | 34 ++++++++ 4 files changed, 120 insertions(+), 65 deletions(-) create mode 100644 addons/common/functions/fnc_addReloadMutex.sqf create mode 100644 addons/common/functions/fnc_removeReloadMutex.sqf diff --git a/addons/common/XEH_PREP.hpp b/addons/common/XEH_PREP.hpp index 0f3ae9625d..79973704d7 100644 --- a/addons/common/XEH_PREP.hpp +++ b/addons/common/XEH_PREP.hpp @@ -10,6 +10,7 @@ PREP(readSettingsFromParamsArray); PREP(actionKeysNamesConverted); PREP(addCanInteractWithCondition); PREP(addLineToDebugDraw); +PREP(addReloadMutex); PREP(addSwayFactor); PREP(addToInventory); PREP(addWeapon); diff --git a/addons/common/XEH_postInit.sqf b/addons/common/XEH_postInit.sqf index d153322c76..e4be24556f 100644 --- a/addons/common/XEH_postInit.sqf +++ b/addons/common/XEH_postInit.sqf @@ -441,74 +441,23 @@ addMissionEventHandler ["PlayerViewChanged", { ////////////////////////////////////////////////// GVAR(isReloading) = false; -GVAR(reloadMutex_lastAction) = ""; -["CAManBase", "GestureChanged", { - params ["_unit", "_gesture"]; - if !( - _unit isEqualTo ACE_Player && - {!GVAR(isReloading)} && - {_gesture isNotEqualTo ""} - ) exitWith {}; +["unit", { + params ["_newPlayer","_oldPlayer"]; - (weaponState ACE_player) params ["_weapon", "_muzzle"]; - if (_weapon isEqualTo "") exitWith {}; + GVAR(isReloading) = ((weaponState _newPlayer) select 6) == 0; // Catch the new unit reloading + if (GVAR(isReloading)) then { + private _unitMovesInfo = getUnitMovesInfo _newPlayer; // 6 - gesture elapsed time, 7 - gesture duration + [{ + if (_this isEqualTo ACE_player) exitWith { // player might switch units again before reload finished + GVAR(isReloading) = false + }; + }, _newPlayer, (_unitMovesInfo select 7) - (_unitMovesInfo select 6)] call CBA_fnc_waitAndExecute; + }; - private _reloadAction = [_weapon, _muzzle] call FUNC(getReloadAction); - - // gesture is always lowercase, skip empty reload actions (binoculars) - if (_gesture isNotEqualTo _reloadAction || {_reloadAction isEqualTo ""}) exitWith {}; - - TRACE_2("Reloading, blocking gestures",weaponState ACE_Player,_gesture); - GVAR(isReloading) = true; - GVAR(reloadMutex_lastAction) = _gesture; -}] call CBA_fnc_addClassEventHandler; - -["CAManBase", "GestureDone", { - params ["_unit", "_gesture"]; - if !( - GVAR(isReloading) && - {_unit isEqualTo ACE_Player} && - {_gesture isEqualTo GVAR(reloadMutex_lastAction)} - ) exitWith {}; - - GVAR(isReloading) = false; - GVAR(reloadMutex_lastAction) = ""; -}] call CBA_fnc_addClassEventHandler; - -// Some secondary weapons (mainly heavy launchers) have reloads as anims instead of gestures -["CAManBase", "AnimChanged", { - params ["_unit", "_anim"]; - if !( - _unit isEqualTo ACE_Player && - {!GVAR(isReloading)} && - {currentWeapon ACE_Player isEqualTo secondaryWeapon ACE_Player} - ) exitWith {}; - - (weaponState ACE_Player) params ["_weapon", "_muzzle"]; - if (_weapon isEqualTo "") exitWith {}; - - private _reloadAction = [_weapon, _muzzle] call FUNC(getReloadAction); - - // anim is always lowercase, skip empty reload actions (binoculars) - if (_anim isNotEqualTo _reloadAction || {_reloadAction isEqualTo ""}) exitWith {}; - - TRACE_2("Reloading with animation, blocking gestures",weaponState ACE_Player,_anim); - GVAR(isReloading) = true; - GVAR(reloadMutex_lastAction) = _anim; -}] call CBA_fnc_addClassEventHandler; - -["CAManBase", "AnimDone", { - params ["_unit", "_anim"]; - if !( - GVAR(isReloading) && - {_unit isEqualTo ACE_Player} && - {_anim isEqualTo GVAR(reloadMutex_lastAction)} - ) exitWith {}; - - GVAR(isReloading) = false; - GVAR(reloadMutex_lastAction) = ""; -}] call CBA_fnc_addClassEventHandler; + _oldPlayer call FUNC(removeReloadMutex); + _newPlayer call FUNC(addReloadMutex); +}, true] call CBA_fnc_addPlayerEventHandler; ////////////////////////////////////////////////// // Start the sway loop diff --git a/addons/common/functions/fnc_addReloadMutex.sqf b/addons/common/functions/fnc_addReloadMutex.sqf new file mode 100644 index 0000000000..7ca0fe49c1 --- /dev/null +++ b/addons/common/functions/fnc_addReloadMutex.sqf @@ -0,0 +1,71 @@ +#include "..\script_component.hpp" +/* + * Author: LinkIsGrim + * Adds reload mutex to a player unit, sets variable to prevent gestures from being played during a reload animation + * + * Arguments: + * 0: Player unit + * + * Return Value: + * None. + * + * Example: + * ACE_player call ace_common_fnc_addReloadMutex + * + * Public: No + */ + +params ["_unit"]; + +_unit setVariable [ + QGVAR(reloadMutex_gestureEH), + _unit addEventHandler ["GestureChanged", { + params ["_unit", "_gesture"]; + if (GVAR(isReloading) || _gesture == "") exitWith {}; // skip empty gesture (include empty reload actions for binoculars) + + (weaponState ACE_player) params ["_weapon", "_muzzle"]; + if (_weapon isEqualTo "") exitWith {}; + + private _reloadAction = [_weapon, _muzzle] call FUNC(getReloadAction); + + // gesture is always lowercase + if (_gesture isNotEqualTo _reloadAction) exitWith {}; + + TRACE_2("Reloading, blocking gestures",weaponState ACE_Player,_gesture); + GVAR(isReloading) = true; + + private _unitMovesInfo = getUnitMovesInfo _unit; // 6 - gesture elapsed time, 7 - gesture duration + [{ + if (_this isEqualTo ACE_player) exitWith { // player might switch units again before reload finished + GVAR(isReloading) = false + }; + }, _unit, (_unitMovesInfo select 7) - (_unitMovesInfo select 6)] call CBA_fnc_waitAndExecute; + }] +]; + +// Some secondary weapons (mainly heavy launchers) have reloads as anims instead of gestures +_unit setVariable [ + QGVAR(reloadMutex_animEH), + _unit addEventHandler ["AnimChanged", { + params ["_unit", "_anim"]; + if ((currentWeapon _unit isNotEqualTo secondaryWeapon _unit) || {GVAR(isReloading) || _anim == ""}) exitWith {}; + + (weaponState ACE_Player) params ["_weapon", "_muzzle"]; + if (_weapon isEqualTo "") exitWith {}; + + private _reloadAction = [_weapon, _muzzle] call FUNC(getReloadAction); + + // anim is always lowercase + if (_anim isNotEqualTo _reloadAction) exitWith {}; + + TRACE_2("Reloading with animation, blocking gestures",weaponState ACE_Player,_anim); + GVAR(isReloading) = true; + + private _unitMovesInfo = getUnitMovesInfo _unit; // 1 - anim elapsed time, 2 - anim duration + [{ + if (_this isEqualTo ACE_player) exitWith { // player might switch units again before reload finished + GVAR(isReloading) = false + }; + }, _unit, (_unitMovesInfo select 2) - (_unitMovesInfo select 1)] call CBA_fnc_waitAndExecute; + }] +]; diff --git a/addons/common/functions/fnc_removeReloadMutex.sqf b/addons/common/functions/fnc_removeReloadMutex.sqf new file mode 100644 index 0000000000..25f397fbae --- /dev/null +++ b/addons/common/functions/fnc_removeReloadMutex.sqf @@ -0,0 +1,34 @@ +#include "..\script_component.hpp" +/* + * Author: LinkIsGrim + * Removes reload mutex from a player unit + * + * Arguments: + * 0: Player unit + * + * Return Value: + * None. + * + * Example: + * ACE_player call ace_common_fnc_removeReloadMutex + * + * Public: No + */ + +params ["_unit"]; + +if (_unit isNil QGVAR(reloadMutex_gestureEH)) exitWith { + WARNING_1("removing reload mutex from unit %1 without eventhandlers - race condition?",_unit); +}; + +_unit removeEventHandler [ + "GestureChanged", + _unit getVariable QGVAR(reloadMutex_gestureEH) +] +_unit setVariable [QGVAR(reloadMutex_gestureEH), nil]. + +_unit removeEventHandler [ + "AnimChanged", + _unit getVariable QGVAR(reloadMutex_animEH) +] +_unit setVariable [QGVAR(reloadMutex_animEH), nil].