From 649b0c28e2baf72229076fbd4698304d8255967f Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Tue, 16 Feb 2016 18:17:42 -0600 Subject: [PATCH] Calculate hearing protection coefficient on events --- addons/hearing/XEH_postInit.sqf | 32 +++++++++++-------- addons/hearing/XEH_preInit.sqf | 1 + addons/hearing/functions/fnc_earRinging.sqf | 22 +++---------- .../hearing/functions/fnc_putInEarplugs.sqf | 2 ++ .../hearing/functions/fnc_removeEarplugs.sqf | 2 ++ .../functions/fnc_updateHearingProtection.sqf | 28 ++++++++++++++++ 6 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 addons/hearing/functions/fnc_updateHearingProtection.sqf diff --git a/addons/hearing/XEH_postInit.sqf b/addons/hearing/XEH_postInit.sqf index 8153a55dbd..6448c3cbcf 100644 --- a/addons/hearing/XEH_postInit.sqf +++ b/addons/hearing/XEH_postInit.sqf @@ -10,24 +10,30 @@ GVAR(deafnessPrior) = 0; GVAR(volume) = 1; GVAR(playerVehAttenuation) = 1; GVAR(time3) = 0; +GVAR(damageCoefficent) = 1; ["SettingsInitialized", { TRACE_1("settingInit",GVAR(EnableCombatDeafness)); - // Only run if combat deafness is enabled + // Only run PFEH and install event handlers if combat deafness is enabled if (!GVAR(EnableCombatDeafness)) exitWith {}; // Spawn volume updating process [FUNC(updateVolume), 1, [false]] call CBA_fnc_addPerFrameHandler; + + //Update veh attunation when player veh changes + ["playerVehicleChanged", {_this call FUNC(updatePlayerVehAttenuation);}] call EFUNC(common,addEventHandler); + ["playerTurretChanged", {_this call FUNC(updatePlayerVehAttenuation);}] call EFUNC(common,addEventHandler); + + //Reset deafness on respawn (or remote control player switch) + ["playerChanged", { + GVAR(deafnessDV) = 0; + GVAR(deafnessPrior) = 0; + ACE_player setVariable [QGVAR(deaf), false]; + GVAR(time3) = 0; + [] call FUNC(updateHearingProtection); + }] call EFUNC(common,addEventhandler); + + //Update protection on possible helmet change + ["playerInventoryChanged", {[] call FUNC(updateHearingProtection);}] call EFUNC(common,addEventhandler); + }] call EFUNC(common,addEventHandler); - -//Update veh attunation when player veh changes -["playerVehicleChanged", {_this call FUNC(updatePlayerVehAttenuation);}] call EFUNC(common,addEventHandler); -["playerTurretChanged", {_this call FUNC(updatePlayerVehAttenuation);}] call EFUNC(common,addEventHandler); - -//Reset deafness on respawn (or remote control player switch) -["playerChanged", { - GVAR(deafnessDV) = 0; - GVAR(deafnessPrior) = 0; - ACE_player setVariable [QGVAR(deaf), false]; - GVAR(time3) = 0; -}] call EFUNC(common,addEventhandler); diff --git a/addons/hearing/XEH_preInit.sqf b/addons/hearing/XEH_preInit.sqf index 3355d0cff4..b451f0eab4 100644 --- a/addons/hearing/XEH_preInit.sqf +++ b/addons/hearing/XEH_preInit.sqf @@ -11,6 +11,7 @@ PREP(hasEarPlugsIn); PREP(moduleHearing); PREP(putInEarPlugs); PREP(removeEarPlugs); +PREP(updateHearingProtection); PREP(updatePlayerVehAttenuation); PREP(updateVolume); diff --git a/addons/hearing/functions/fnc_earRinging.sqf b/addons/hearing/functions/fnc_earRinging.sqf index 0bd94943a7..273fad8107 100644 --- a/addons/hearing/functions/fnc_earRinging.sqf +++ b/addons/hearing/functions/fnc_earRinging.sqf @@ -1,15 +1,15 @@ /* * Author: KoffeinFlummi, commy2, Rocko, Rommel, Ruthberg - * Handle new sound souce near player and apply hearing damage + * Handle new sound souce near ace_player and apply hearing damage * * Arguments: - * 0: strength of ear ringing (Number between 0 and 1) + * 0: strength of ear ringing * * Return Value: * None * * Example: - * [_unit, _strength] call ace_hearing_fnc_earRinging + * [_strength] call ace_hearing_fnc_earRinging * * Public: No */ @@ -20,18 +20,6 @@ if (_strength < 0.05) exitWith {}; if (!isNull curatorCamera) exitWith {}; if ((!GVAR(enabledForZeusUnits)) && {player != ACE_player}) exitWith {}; -if (_unit getVariable ["ACE_hasEarPlugsin", false]) then { - _strength = _strength / 4; -}; +TRACE_2("adding",_strength * GVAR(damageCoefficent),GVAR(deafnessDV)); -//headgear hearing protection -if (headgear _unit != "") then { - private _protection = (getNumber (configFile >> "CfgWeapons" >> (headgear _unit) >> QGVAR(protection))) min 1; - if (_protection > 0) then { - _strength = _strength * (1 - _protection); - }; -}; - -TRACE_2("adding",_strength,GVAR(deafnessDV)); - -GVAR(deafnessDV) = GVAR(deafnessDV) + _strength; +GVAR(deafnessDV) = GVAR(deafnessDV) + (_strength * GVAR(damageCoefficent)); diff --git a/addons/hearing/functions/fnc_putInEarplugs.sqf b/addons/hearing/functions/fnc_putInEarplugs.sqf index 8d5479c17b..90fe8d62ac 100644 --- a/addons/hearing/functions/fnc_putInEarplugs.sqf +++ b/addons/hearing/functions/fnc_putInEarplugs.sqf @@ -29,3 +29,5 @@ _player setVariable ["ACE_hasEarPlugsIn", true, true]; /*// No Earplugs in inventory, telling user [localize LSTRING(NoPlugs)] call EFUNC(common,displayTextStructured);*/ + +[] call FUNC(updateHearingProtection); diff --git a/addons/hearing/functions/fnc_removeEarplugs.sqf b/addons/hearing/functions/fnc_removeEarplugs.sqf index f5b5ca2442..3154230ecf 100644 --- a/addons/hearing/functions/fnc_removeEarplugs.sqf +++ b/addons/hearing/functions/fnc_removeEarplugs.sqf @@ -30,3 +30,5 @@ _player setVariable ["ACE_hasEarPlugsIn", false, true]; //Force an immediate fast volume update: [[true]] call FUNC(updateVolume); + +[] call FUNC(updateHearingProtection); diff --git a/addons/hearing/functions/fnc_updateHearingProtection.sqf b/addons/hearing/functions/fnc_updateHearingProtection.sqf new file mode 100644 index 0000000000..d7726fd681 --- /dev/null +++ b/addons/hearing/functions/fnc_updateHearingProtection.sqf @@ -0,0 +1,28 @@ +/* + * Author: PabstMirror + * Updates the hearing protection for player on earbuds/helmet change + * + * Arguments: + * None + * + * Return Value: + * None + * + * Example: + * [] call ace_hearing_fnc_updateHearingProtection + * + * Public: No + */ +#include "script_component.hpp" + +TRACE_1("params",_this); + +if (isNull ACE_player) exitWith {GVAR(damageCoefficent) = 0;}; + +GVAR(damageCoefficent) = if (ACE_player getVariable ["ACE_hasEarPlugsin", false]) then {0.25} else {1}; +if (headgear ACE_player != "") then { //headgear hearing protection + private _protection = (getNumber (configFile >> "CfgWeapons" >> (headgear ACE_player) >> QGVAR(protection))) min 1; + if (_protection > 0) then { + GVAR(damageCoefficent) = GVAR(damageCoefficent) * (1 - _protection); + }; +};