diff --git a/addons/hearing/XEH_postInit.sqf b/addons/hearing/XEH_postInit.sqf index 0f27321b2b..adb7827ce7 100644 --- a/addons/hearing/XEH_postInit.sqf +++ b/addons/hearing/XEH_postInit.sqf @@ -1,56 +1,7 @@ -// by commy2 and CAA-Picard #include "script_component.hpp" -#define STRENGHTODEAFNESS 3 -#define MAXDEAFNESS 1.1 - GVAR(currentDeafness) = 0; GVAR(newStrength) = 0; // Spawn volume updating process -0 spawn { - while {true} do { - - // Check if new noises increase deafness - if (GVAR(newStrength) * STRENGHTODEAFNESS > GVAR(currentDeafness)) then { - GVAR(currentDeafness) = GVAR(newStrength) * STRENGHTODEAFNESS min MAXDEAFNESS; - }; - GVAR(newStrength) = 0; - - // Recover rate is slower if deafness is severe - _recoverRate = 0.01; - if (GVAR(currentDeafness) > 0.7) then { - _recoverRate = 0.005; - if (GVAR(currentDeafness) > 0.9) then { - _recoverRate = 0.002; - }; - }; - - // Deafness recovers with time - GVAR(currentDeafness) = GVAR(currentDeafness) - _recoverRate max 0; - - // needed until Bohemia fixes playSound to actually use the second argument - _volume = (1 - GVAR(currentDeafness) max 0)^2 max 0.04; - - // Earplugs reduce hearing 50% - if ([ACE_player] call FUNC(hasEarPlugsIn)) then { - _volume = _volume min 0.5; - }; - - // Reduce volume if player is unconscious - if (ACE_player getVariable ["ACE_isUnconscious", false]) then { - _volume = _volume min 0.4; - }; - - if (!(missionNameSpace getVariable [QGVAR(disableVolumeUpdate), false])) then { - 0.1 fadeSound _volume; - 0.1 fadeSpeech _volume; - ACE_player setVariable ["tf_globalVolume", _volume]; - ACE_player setVariable ["acre_sys_core_globalVolume", _volume]; - }; - - //hintSilent format ["GVAR(currentDeafness), _Volume = %1, %2", GVAR(currentDeafness), _volume]; - - sleep 0.1; - }; -}; +[FUNC(updateVolume), 0.1, [] ] call CBA_fnc_addPerFrameHandler; diff --git a/addons/hearing/XEH_preInit.sqf b/addons/hearing/XEH_preInit.sqf index 14e037595e..d160232c05 100644 --- a/addons/hearing/XEH_preInit.sqf +++ b/addons/hearing/XEH_preInit.sqf @@ -6,4 +6,5 @@ PREP(explosionNear); PREP(firedNear); PREP(hasEarPlugsIn); PREP(putInEarPlugs); -PREP(removeEarPlugs); \ No newline at end of file +PREP(removeEarPlugs); +PREP(updateVolume); diff --git a/addons/hearing/functions/fnc_updateVolume.sqf b/addons/hearing/functions/fnc_updateVolume.sqf new file mode 100644 index 0000000000..187d4ce116 --- /dev/null +++ b/addons/hearing/functions/fnc_updateVolume.sqf @@ -0,0 +1,45 @@ +// by commy2 and CAA-Picard +#include "script_component.hpp" + +#define STRENGHTODEAFNESS 3 +#define MAXDEAFNESS 1.1 + +// Check if new noises increase deafness +if (GVAR(newStrength) * STRENGHTODEAFNESS > GVAR(currentDeafness)) then { + GVAR(currentDeafness) = GVAR(newStrength) * STRENGHTODEAFNESS min MAXDEAFNESS; +}; +GVAR(newStrength) = 0; + +// Recover rate is slower if deafness is severe +_recoverRate = 0.01; +if (GVAR(currentDeafness) > 0.7) then { + _recoverRate = 0.005; + if (GVAR(currentDeafness) > 0.9) then { + _recoverRate = 0.002; + }; +}; + +// Deafness recovers with time +GVAR(currentDeafness) = GVAR(currentDeafness) - _recoverRate max 0; + +// needed until Bohemia fixes playSound to actually use the second argument +_volume = (1 - GVAR(currentDeafness) max 0)^2 max 0.04; + +// Earplugs reduce hearing 50% +if ([ACE_player] call FUNC(hasEarPlugsIn)) then { + _volume = _volume min 0.5; +}; + +// Reduce volume if player is unconscious +if (ACE_player getVariable ["ACE_isUnconscious", false]) then { + _volume = _volume min 0.4; +}; + +if (!(missionNameSpace getVariable [QGVAR(disableVolumeUpdate), false])) then { + 0.1 fadeSound _volume; + 0.1 fadeSpeech _volume; + ACE_player setVariable ["tf_globalVolume", _volume]; + ACE_player setVariable ["acre_sys_core_globalVolume", _volume]; +}; + +//hintSilent format ["GVAR(currentDeafness), _Volume = %1, %2", GVAR(currentDeafness), _volume];