From e99e8ee8ebc664f65c1878cffa3a313d2d7e7811 Mon Sep 17 00:00:00 2001 From: johnb432 <58661205+johnb432@users.noreply.github.com> Date: Tue, 28 May 2024 21:52:47 +0200 Subject: [PATCH 1/4] Use `SlotItemChanged` --- addons/hearing/XEH_postInit.sqf | 21 +++++++++++++------ .../hearing/functions/fnc_putInEarplugs.sqf | 2 +- .../hearing/functions/fnc_removeEarplugs.sqf | 2 +- .../functions/fnc_updateHearingProtection.sqf | 11 ++++++---- addons/hearing/script_component.hpp | 2 ++ 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/addons/hearing/XEH_postInit.sqf b/addons/hearing/XEH_postInit.sqf index 4a2ca909926..2658c4de6e6 100644 --- a/addons/hearing/XEH_postInit.sqf +++ b/addons/hearing/XEH_postInit.sqf @@ -67,10 +67,16 @@ GVAR(lastPlayerVehicle) = objNull; private _firedEH = _oldPlayer getVariable [QGVAR(firedEH), -1]; _oldPlayer removeEventHandler ["FiredNear", _firedEH]; _oldPlayer setVariable [QGVAR(firedEH), nil]; + private _explosionEH = _oldPlayer getVariable [QGVAR(explosionEH), -1]; _oldPlayer removeEventHandler ["Explosion", _explosionEH]; _oldPlayer setVariable [QGVAR(explosionEH), nil]; - TRACE_3("removed unit eh",_oldPlayer,_firedEH,_explosionEH); + + private _slotItemChangedEH = _oldPlayer getVariable [QGVAR(slotItemChangedEH), -1]; + _oldPlayer removeEventHandler ["SlotItemChanged", _slotItemChangedEH]; + _oldPlayer setVariable [QGVAR(slotItemChangedEH), nil]; + + TRACE_4("removed unit eh",_oldPlayer,_firedEH,_explosionEH,_slotItemChangedEH); }; // Don't add a new EH if the unit respawned if ((_player getVariable [QGVAR(firedEH), -1]) == -1) then { @@ -79,17 +85,20 @@ GVAR(lastPlayerVehicle) = objNull; }; private _firedEH = _player addEventHandler ["FiredNear", {call FUNC(firedNear)}]; _player setVariable [QGVAR(firedEH), _firedEH]; + private _explosionEH = _player addEventHandler ["Explosion", {call FUNC(explosionNear)}]; _player setVariable [QGVAR(explosionEH), _explosionEH]; - TRACE_3("added unit eh",_player,_firedEH,_explosionEH); + + // Update protection on possible helmet change + private _slotItemChangedEH = _player addEventHandler ["SlotItemChanged", {(_this select 2) call FUNC(updateHearingProtection)}]; + _player setVariable [QGVAR(slotItemChangedEH), _slotItemChangedEH]; + + TRACE_4("added unit eh",_player,_firedEH,_explosionEH,_slotItemChangedEH); }; GVAR(deafnessDV) = 0; GVAR(deafnessPrior) = 0; GVAR(time3) = 0; - [] call FUNC(updateHearingProtection); + UPDATE_HEARING_EARPLUGS call FUNC(updateHearingProtection); }, true] call CBA_fnc_addPlayerEventHandler; - - // Update protection on possible helmet change - ["loadout", LINKFUNC(updateHearingProtection), false] call CBA_fnc_addPlayerEventHandler; }] call CBA_fnc_addEventHandler; diff --git a/addons/hearing/functions/fnc_putInEarplugs.sqf b/addons/hearing/functions/fnc_putInEarplugs.sqf index 2af4df8e86c..bd2e137743a 100644 --- a/addons/hearing/functions/fnc_putInEarplugs.sqf +++ b/addons/hearing/functions/fnc_putInEarplugs.sqf @@ -35,4 +35,4 @@ if (_displayHint) then { // No Earplugs in inventory, telling user //[localize LSTRING(NoPlugs)] call EFUNC(common,displayTextStructured); -[] call FUNC(updateHearingProtection); +UPDATE_HEARING_EARPLUGS call FUNC(updateHearingProtection); diff --git a/addons/hearing/functions/fnc_removeEarplugs.sqf b/addons/hearing/functions/fnc_removeEarplugs.sqf index 20a49bb5300..8c6d4fb9404 100644 --- a/addons/hearing/functions/fnc_removeEarplugs.sqf +++ b/addons/hearing/functions/fnc_removeEarplugs.sqf @@ -36,4 +36,4 @@ if (_displayHint) then { //Force an immediate fast volume update: [[true]] call FUNC(updateVolume); -[] call FUNC(updateHearingProtection); +UPDATE_HEARING_EARPLUGS call FUNC(updateHearingProtection); diff --git a/addons/hearing/functions/fnc_updateHearingProtection.sqf b/addons/hearing/functions/fnc_updateHearingProtection.sqf index 15973b73a92..4db2fe449fd 100644 --- a/addons/hearing/functions/fnc_updateHearingProtection.sqf +++ b/addons/hearing/functions/fnc_updateHearingProtection.sqf @@ -4,24 +4,27 @@ * Updates the hearing protection and volume attenuation for player on earbuds/helmet change * * Arguments: - * None + * 0: Slot * * Return Value: * None * * Example: - * [] call ace_hearing_fnc_updateHearingProtection + * UPDATE_HEARING_EARPLUGS call ace_hearing_fnc_updateHearingProtection * * Public: No */ -TRACE_1("params",_this); - if (isNull ACE_player) exitWith { GVAR(damageCoefficent) = 0; GVAR(volumeAttenuation) = 1; }; +params ["_slot"]; +TRACE_1("",_slot); + +if !(_slot in [UPDATE_HEARING_EARPLUGS, TYPE_GOGGLE, TYPE_HEADGEAR]) exitWith {}; + // Handle Earplugs private _hasEarPlugsIn = [ACE_player] call FUNC(hasEarPlugsIn); GVAR(damageCoefficent) = [1, 0.25] select _hasEarPlugsIn; diff --git a/addons/hearing/script_component.hpp b/addons/hearing/script_component.hpp index 178310cd209..ed9052a0a1d 100644 --- a/addons/hearing/script_component.hpp +++ b/addons/hearing/script_component.hpp @@ -16,3 +16,5 @@ #include "\z\ace\addons\main\script_macros.hpp" #include "\z\ace\addons\hearing\script_macros_hearingProtection.hpp" + +#defined UPDATE_HEARING_EARPLUGS -1 From baf2c66570593f932f1eeebe617dd7ae2f5a1025 Mon Sep 17 00:00:00 2001 From: johnb432 <58661205+johnb432@users.noreply.github.com> Date: Tue, 28 May 2024 21:55:55 +0200 Subject: [PATCH 2/4] Update script_component.hpp --- addons/hearing/script_component.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/hearing/script_component.hpp b/addons/hearing/script_component.hpp index ed9052a0a1d..a211922087d 100644 --- a/addons/hearing/script_component.hpp +++ b/addons/hearing/script_component.hpp @@ -17,4 +17,4 @@ #include "\z\ace\addons\main\script_macros.hpp" #include "\z\ace\addons\hearing\script_macros_hearingProtection.hpp" -#defined UPDATE_HEARING_EARPLUGS -1 +#define UPDATE_HEARING_EARPLUGS -1 From a69b5b40362feff4e8e894633e983d9473e2f328 Mon Sep 17 00:00:00 2001 From: johnb432 <58661205+johnb432@users.noreply.github.com> Date: Mon, 10 Jun 2024 10:42:01 +0200 Subject: [PATCH 3/4] Use `addPlayerEH` --- addons/hearing/XEH_postInit.sqf | 41 ++++----------------------------- 1 file changed, 4 insertions(+), 37 deletions(-) diff --git a/addons/hearing/XEH_postInit.sqf b/addons/hearing/XEH_postInit.sqf index a8d0df89512..19f28a244cd 100644 --- a/addons/hearing/XEH_postInit.sqf +++ b/addons/hearing/XEH_postInit.sqf @@ -62,45 +62,12 @@ GVAR(lastPlayerVehicle) = objNull; ["turret", LINKFUNC(updatePlayerVehAttenuation), false] call CBA_fnc_addPlayerEventHandler; + [QGVAR(firedNear), "FiredNear", LINKFUNC(firedNear), true] call EFUNC(common,addPlayerEH); + [QGVAR(explosion), "Explosion", LINKFUNC(explosionNear), true] call EFUNC(common,addPlayerEH); + [QGVAR(slotItemChanged), "SlotItemChanged", {(_this select 2) call FUNC(updateHearingProtection)}, true] call EFUNC(common,addPlayerEH); + // Reset deafness on respawn (or remote control player switch) ["unit", { - params ["_player", "_oldPlayer"]; - TRACE_2("unit change",_player,_oldPlayer); - - if (!isNull _oldPlayer) then { - private _firedEH = _oldPlayer getVariable [QGVAR(firedEH), -1]; - _oldPlayer removeEventHandler ["FiredNear", _firedEH]; - _oldPlayer setVariable [QGVAR(firedEH), nil]; - - private _explosionEH = _oldPlayer getVariable [QGVAR(explosionEH), -1]; - _oldPlayer removeEventHandler ["Explosion", _explosionEH]; - _oldPlayer setVariable [QGVAR(explosionEH), nil]; - - private _slotItemChangedEH = _oldPlayer getVariable [QGVAR(slotItemChangedEH), -1]; - _oldPlayer removeEventHandler ["SlotItemChanged", _slotItemChangedEH]; - _oldPlayer setVariable [QGVAR(slotItemChangedEH), nil]; - - TRACE_4("removed unit eh",_oldPlayer,_firedEH,_explosionEH,_slotItemChangedEH); - }; - // Don't add a new EH if the unit respawned - if ((_player getVariable [QGVAR(firedEH), -1]) == -1) then { - if ((getNumber (configOf _player >> "isPlayableLogic")) == 1) exitWith { - TRACE_1("skipping playable logic",typeOf _player); // VirtualMan_F (placeable logic zeus / spectator) - }; - - private _firedEH = _player addEventHandler ["FiredNear", {call FUNC(firedNear)}]; - _player setVariable [QGVAR(firedEH), _firedEH]; - - private _explosionEH = _player addEventHandler ["Explosion", {call FUNC(explosionNear)}]; - _player setVariable [QGVAR(explosionEH), _explosionEH]; - - // Update protection on possible helmet change - private _slotItemChangedEH = _player addEventHandler ["SlotItemChanged", {(_this select 2) call FUNC(updateHearingProtection)}]; - _player setVariable [QGVAR(slotItemChangedEH), _slotItemChangedEH]; - - TRACE_4("added unit eh",_player,_firedEH,_explosionEH,_slotItemChangedEH); - }; - GVAR(deafnessDV) = 0; GVAR(deafnessPrior) = 0; GVAR(time3) = 0; From 942879a91e3eea3a0b4e92aa8ac6999e32bbad2c Mon Sep 17 00:00:00 2001 From: johnb432 <58661205+johnb432@users.noreply.github.com> Date: Mon, 10 Jun 2024 10:43:30 +0200 Subject: [PATCH 4/4] Use `LINKFUNC` --- addons/hearing/XEH_postInit.sqf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/hearing/XEH_postInit.sqf b/addons/hearing/XEH_postInit.sqf index 19f28a244cd..1fdd8008bc0 100644 --- a/addons/hearing/XEH_postInit.sqf +++ b/addons/hearing/XEH_postInit.sqf @@ -53,7 +53,7 @@ GVAR(lastPlayerVehicle) = objNull; }; if ((!isNull _vehicle) && {_player != _vehicle}) then { - private _firedEH = _vehicle addEventHandler ["FiredNear", {call FUNC(firedNear)}]; + private _firedEH = _vehicle addEventHandler ["FiredNear", LINKFUNC(firedNear)]; _vehicle setVariable [QGVAR(firedEH), _firedEH]; GVAR(lastPlayerVehicle) = _vehicle; TRACE_2("added veh eh",_firedEH,GVAR(lastPlayerVehicle));