Skip to content

Commit

Permalink
Hearing - Code cleanup (#10041)
Browse files Browse the repository at this point in the history
* Hearing cleanup

* Update fnc_updateHearingProtection.sqf

* Fixes + tweaks

* Update initSettings.inc.sqf

* Update fnc_removeEarplugs.sqf
  • Loading branch information
johnb432 authored May 29, 2024
1 parent be61424 commit e535988
Show file tree
Hide file tree
Showing 19 changed files with 146 additions and 109 deletions.
1 change: 0 additions & 1 deletion addons/hearing/CfgEventHandlers.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class Extended_PreStart_EventHandlers {
class ADDON {
init = QUOTE(call COMPILE_SCRIPT(XEH_preStart));
Expand Down
4 changes: 2 additions & 2 deletions addons/hearing/CfgVehicles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class CfgVehicles {
class ACE_Equipment {
class ACE_PutInEarplugs {
displayName = CSTRING(EarPlugs_On);
condition = QUOTE(GVAR(EnableCombatDeafness) && {!([_player] call FUNC(hasEarPlugsIn)) && {'ACE_EarPlugs' in items _player}});
condition = QUOTE(GVAR(enableCombatDeafness) && {!(_player call FUNC(hasEarPlugsIn)) && {[ARR_2(_player,'ACE_EarPlugs')] call EFUNC(common,hasItem)}});
exceptions[] = {"isNotSwimming", "isNotInside", "isNotSitting"};
statement = QUOTE([ARR_2(_player,true)] call FUNC(putInEarPlugs));
showDisabled = 0;
icon = QPATHTOF(UI\ACE_earplugs_x_ca.paa);
};
class ACE_RemoveEarplugs {
displayName = CSTRING(EarPlugs_Off);
condition = QUOTE(GVAR(EnableCombatDeafness) && {[_player] call FUNC(hasEarPlugsIn)});
condition = QUOTE(GVAR(enableCombatDeafness) && {_player call FUNC(hasEarPlugsIn)});
exceptions[] = {"isNotSwimming", "isNotInside", "isNotSitting"};
statement = QUOTE([ARR_2(_player,true)] call FUNC(removeEarPlugs));
showDisabled = 0;
Expand Down
2 changes: 1 addition & 1 deletion addons/hearing/CfgWeapons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class CfgWeapons {

class H_HelmetO_ocamo: H_HelmetB {
HEARING_PROTECTION_PELTOR;
}; // Defender and Assasin Helmet inherit.
}; // Defender and Assassin Helmet inherit.

class H_HelmetO_ViperSP_hex_f: H_HelmetB {
HEARING_PROTECTION_PELTOR;
Expand Down
1 change: 0 additions & 1 deletion addons/hearing/XEH_PREP.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

PREP(addEarPlugs);
PREP(earRinging);
PREP(explosionNear);
Expand Down
24 changes: 17 additions & 7 deletions addons/hearing/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

if (isServer) then {
["CBA_settingsInitialized", {
TRACE_1("settingInit - server",GVAR(EnableCombatDeafness));
TRACE_1("settingInit - server",GVAR(enableCombatDeafness));

// Only install event handler if combat deafness is enabled
if (!GVAR(EnableCombatDeafness)) exitWith {};
if (!GVAR(enableCombatDeafness)) exitWith {};

["CAManBase", "Init", LINKFUNC(addEarPlugs), true, [], true] call CBA_fnc_addClassEventHandler;
}] call CBA_fnc_addEventHandler;
Expand All @@ -26,18 +27,20 @@ GVAR(volumeAttenuation) = 1;
GVAR(lastPlayerVehicle) = objNull;

["CBA_settingsInitialized", {
TRACE_1("settingInit",GVAR(EnableCombatDeafness));
TRACE_1("settingInit",GVAR(enableCombatDeafness));

// Only run PFEH and install event handlers if combat deafness is enabled
if (!GVAR(EnableCombatDeafness)) exitWith {};
if (!GVAR(enableCombatDeafness)) exitWith {};

// Spawn volume updating process
[LINKFUNC(updateVolume), 1, [false]] call CBA_fnc_addPerFrameHandler;
[LINKFUNC(updateVolume), 1, false] call CBA_fnc_addPerFrameHandler;

[QGVAR(updateVolume), LINKFUNC(updateVolume)] call CBA_fnc_addEventHandler;

// Update veh attunation when player veh changes
["vehicle", {
params ["_player", "_vehicle"];

TRACE_2("vehicle change",_player,_vehicle);
_this call FUNC(updatePlayerVehAttenuation);

Expand All @@ -48,15 +51,16 @@ GVAR(lastPlayerVehicle) = objNull;
GVAR(lastPlayerVehicle) = objNull;
TRACE_2("removed veh eh",_firedEH,GVAR(lastPlayerVehicle));
};

if ((!isNull _vehicle) && {_player != _vehicle}) then {
private _firedEH = _vehicle addEventHandler ["FiredNear", {call FUNC(firedNear)}];
_vehicle setVariable [QGVAR(firedEH), _firedEH];
GVAR(lastPlayerVehicle) = _vehicle;
TRACE_2("added veh eh",_firedEH,GVAR(lastPlayerVehicle));
};
}, true] call CBA_fnc_addPlayerEventHandler;
["turret", LINKFUNC(updatePlayerVehAttenuation), false] call CBA_fnc_addPlayerEventHandler;

["turret", LINKFUNC(updatePlayerVehAttenuation), false] call CBA_fnc_addPlayerEventHandler;

// Reset deafness on respawn (or remote control player switch)
["unit", {
Expand All @@ -67,27 +71,33 @@ 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);
};
// 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];

TRACE_3("added unit eh",_player,_firedEH,_explosionEH);
};

GVAR(deafnessDV) = 0;
GVAR(deafnessPrior) = 0;
GVAR(time3) = 0;
[] call FUNC(updateHearingProtection);

call FUNC(updateHearingProtection);
}, true] call CBA_fnc_addPlayerEventHandler;

// Update protection on possible helmet change
Expand Down
4 changes: 3 additions & 1 deletion addons/hearing/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ PREP_RECOMPILE_END;

["CBA_loadoutSet", {
params ["_unit", "_loadout", "_extendedInfo"];

if (_extendedInfo getOrDefault ["ace_earplugs", false]) then {
_unit setVariable ["ACE_hasEarPlugsIn", true, true];

// Only force update volume if unit is a player (including remote controlled)
if (_unit call EFUNC(common,isPlayer)) then {
[QGVAR(updateVolume), [[true]], _unit] call CBA_fnc_targetEvent;
[QGVAR(updateVolume), true, _unit] call CBA_fnc_targetEvent;
};
};
}] call CBA_fnc_addEventHandler;

["CBA_loadoutGet", {
params ["_unit", "_loadout", "_extendedInfo"];

if (_unit getVariable ["ACE_hasEarPlugsin", false]) then {
_extendedInfo set ["ace_earplugs", true]
};
Expand Down
9 changes: 5 additions & 4 deletions addons/hearing/functions/fnc_earRinging.sqf
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#include "..\script_component.hpp"
/*
* Author: KoffeinFlummi, commy2, Rocko, Rommel, Ruthberg
* Handle new sound souce near ace_player and apply hearing damage
* Handle new sound souce near ace_player and apply hearing damage.
*
* Arguments:
* 0: strength of ear ringing <NUMBER>
* 0: Strength of ear ringing <NUMBER>
*
* Return Value:
* None
*
* Example:
* [_strength] call ace_hearing_fnc_earRinging
* 10 call ace_hearing_fnc_earRinging
*
* Public: No
*/

params ["_strength"];

if (_strength < 0.05) exitWith {};
if (!isNull curatorCamera) exitWith {};
if ((!GVAR(enabledForZeusUnits)) && {player != ACE_player}) exitWith {};
if (!GVAR(enabledForZeusUnits) && {player != ACE_player}) exitWith {};

TRACE_2("adding",_strength * GVAR(damageCoefficent),GVAR(deafnessDV));

Expand Down
8 changes: 4 additions & 4 deletions addons/hearing/functions/fnc_explosionNear.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Handles deafness due to explosions going off near the player.
*
* Arguments:
* 0: vehicle - Object the event handler is assigned to (player) <OBJECT>
* 1: damage - Damage inflicted to the object <NUMBER>
* 0: Unit <OBJECT>
* 1: Damage inflicted to the unit <NUMBER>
*
* Return Value:
* None
Expand All @@ -22,5 +22,5 @@ TRACE_2("explosion near player",_unit,_damage);

private _strength = (0 max _damage) * 30;

// Call inmediately, as it will get pick up later anyway by the update thread
[_strength] call FUNC(earRinging);
// Call immediately, as it will get picked up later by the update thread anyway
_strength call FUNC(earRinging);
4 changes: 2 additions & 2 deletions addons/hearing/functions/fnc_firedNear.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ private _strength = _vehAttenuation * (_loudness - (_loudness / 50 * _distance))

TRACE_1("result",_strength);

// Call inmediately, as it will get pick up later anyway by the update thread
[_strength] call FUNC(earRinging);
// Call immediately, as it will get picked up later by the update thread anyway
_strength call FUNC(earRinging);
20 changes: 10 additions & 10 deletions addons/hearing/functions/fnc_handleRespawn.sqf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "..\script_component.hpp"
/*
* Author: PabstMirror
* Reset earplugs on respawn, and then re-add if appropriate
* Reset earplugs on respawn, and then re-add if appropriate.
*
* Arguments:
* 0: Unit <OBJECT>
Expand All @@ -10,29 +10,29 @@
* None
*
* Example:
* [player] call ACE_hearing_fnc_handleRespawn;
* player call ace_hearing_fnc_handleRespawn;
*
* Public: No
*/

// Do not add or remove earplugs if gear should be preserved
if (missionNamespace getVariable [QEGVAR(respawn,savePreDeathGear), false]) exitWith {};

params ["_unit"];
TRACE_2("params",_unit,typeOf _unit);

if (!local _unit) exitWith {}; //XEH should only be called on local units

//Do not add or remove earplugs if gear should be preserved
if (missionNamespace getVariable [QEGVAR(respawn,SavePreDeathGear), false]) exitWith {};
if (!local _unit) exitWith {}; // XEH should only be called on local units

private _respawn = [0] call BIS_fnc_missionRespawnType;

//if respawn is not Group or side:
// If respawn is not group or side:
if (_respawn <= 3) then {
//Remove earplugs if they have them:
// Remove earplugs if they have them:
if (_unit getVariable ["ACE_hasEarPlugsin", false]) then {
TRACE_1("had EarPlugs in - removing",_unit);
_unit setVariable ["ACE_hasEarPlugsin", false, true];
};
};

//Re-add if they need them:
[_unit] call FUNC(addEarPlugs);
// Re-add if they need them
_unit call FUNC(addEarPlugs);
7 changes: 4 additions & 3 deletions addons/hearing/functions/fnc_hasEarPlugsIn.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
* Check if the unit has earplugs put in.
*
* Arguments:
* 0: Unit (player) <OBJECT>
* 0: Unit <OBJECT>
*
* Return Value:
* Have Earplugs in <BOOL>
* Has Earplugs in <BOOL>
*
* Example:
* [ace_player] call ace_hearing_fnc_hasEarPlugsIn
* player call ace_hearing_fnc_hasEarPlugsIn
*
* Public: No
*/

params ["_unit"];

_unit getVariable ["ACE_hasEarPlugsin", false]
4 changes: 3 additions & 1 deletion addons/hearing/functions/fnc_moduleHearing.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* None
*
* Example:
* [player] call ACE_hearing_fnc_moduleHearing
* player call ace_hearing_fnc_moduleHearing
*
* Public: No
*/
Expand All @@ -23,6 +23,8 @@ params ["_logic"];
if ((_logic getVariable "DisableEarRinging") != -1) then {
[_logic, QGVAR(DisableEarRinging), "DisableEarRinging"] call EFUNC(common,readSettingFromModule);
};

[_logic, QGVAR(enabledForZeusUnits), "enabledForZeusUnits"] call EFUNC(common,readSettingFromModule);
[_logic, QGVAR(autoAddEarplugsToUnits), "autoAddEarplugsToUnits"] call EFUNC(common,readSettingFromModule);

INFO("Hearing Module Initialized.");
25 changes: 11 additions & 14 deletions addons/hearing/functions/fnc_putInEarplugs.sqf
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
#include "..\script_component.hpp"
/*
* Author: Hope Johnson and commy2
* Author: Hope Johnson, commy2
* Puts in earplugs.
*
* Arguments:
* 0: Unit (player) <OBJECT>
* 0: Unit <OBJECT>
* 1: Display hint <BOOL> (default: false)
*
* Return Value:
* None
*
* Example:
* [ace_player, false] call ace_hearing_fnc_putInEarplugs
* [player, false] call ace_hearing_fnc_putInEarplugs
*
* Public: No
*/

params ["_player", ["_displayHint", false, [false]]];
if (!GVAR(enableCombatDeafness)) exitWith {};

if (!GVAR(EnableCombatDeafness)) exitWith {};
params ["_unit", ["_displayHint", false]];

// Plugs in inventory, putting them in
_player removeItem "ACE_EarPlugs";
_unit removeItem "ACE_EarPlugs";

_player setVariable ["ACE_hasEarPlugsIn", true, true];
_unit setVariable ["ACE_hasEarPlugsIn", true, true];

if (_displayHint) then {
[localize LSTRING(EarPlugs_Are_On)] call EFUNC(common,displayTextStructured);
[LLSTRING(EarPlugs_Are_On)] call EFUNC(common,displayTextStructured);
};

//Force an immediate fast volume update:
[[true]] call FUNC(updateVolume);
// Force an immediate volume update
true call FUNC(updateVolume);

// No Earplugs in inventory, telling user
//[localize LSTRING(NoPlugs)] call EFUNC(common,displayTextStructured);

[] call FUNC(updateHearingProtection);
call FUNC(updateHearingProtection);
27 changes: 14 additions & 13 deletions addons/hearing/functions/fnc_removeEarplugs.sqf
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
#include "..\script_component.hpp"
/*
* Author: Hope Johnson and commy2
* Author: Hope Johnson, commy2
* Takes out earplugs.
*
* Arguments:
* 0: Unit (player) <OBJECT>
* 1: Display hint <BOOL> (default false)
* 0: Unit <OBJECT>
* 1: Display hint <BOOL> (default: false)
*
* Return Value:
* None
*
* Example:
* [ace_player, false] call ace_hearing_fnc_removeEarplugs
* [player, false] call ace_hearing_fnc_removeEarplugs
*
* Public: No
*/

params ["_player", ["_displayHint", false, [false]]];
if (!GVAR(enableCombatDeafness)) exitWith {};

if (!GVAR(EnableCombatDeafness)) exitWith {};
params ["_unit", ["_displayHint", false]];

if !([_player, "ACE_EarPlugs"] call CBA_fnc_canAddItem) exitWith { // inventory full
// Inventory full
if !([_unit, "ACE_EarPlugs"] call CBA_fnc_canAddItem) exitWith {
[LELSTRING(common,Inventory_Full)] call EFUNC(common,displayTextStructured);
};

// Plugs already in and removing them.
_player addItem "ACE_EarPlugs";
_unit addItem "ACE_EarPlugs";

_player setVariable ["ACE_hasEarPlugsIn", false, true];
_unit setVariable ["ACE_hasEarPlugsIn", false, true];

if (_displayHint) then {
[localize LSTRING(EarPlugs_Are_Off)] call EFUNC(common,displayTextStructured);
[LLSTRING(EarPlugs_Are_Off)] call EFUNC(common,displayTextStructured);
};

//Force an immediate fast volume update:
[[true]] call FUNC(updateVolume);
// Force an immediate volume update
true call FUNC(updateVolume);

[] call FUNC(updateHearingProtection);
call FUNC(updateHearingProtection);
Loading

0 comments on commit e535988

Please sign in to comment.