Skip to content

Commit

Permalink
Hearing - Fix explosions not affecting hearing (acemod#10002)
Browse files Browse the repository at this point in the history
* Have explosions affect hearing

* Update fnc_explosion.sqf

* Update XEH_postInit.sqf

* Update addons/hearing/functions/fnc_explosion.sqf

Co-authored-by: PabstMirror <pabstmirror@gmail.com>

* Update fnc_explosion.sqf

* Make EH local

* Use sound entry instead

---------

Co-authored-by: PabstMirror <pabstmirror@gmail.com>
  • Loading branch information
2 people authored and blake8090 committed Aug 18, 2024
1 parent e0b0fbc commit a364c0d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 38 deletions.
2 changes: 1 addition & 1 deletion addons/hearing/XEH_PREP.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PREP(addEarPlugs);
PREP(earRinging);
PREP(explosionNear);
PREP(explosion);
PREP(firedNear);
PREP(getAmmoLoudness);
PREP(handleRespawn);
Expand Down
25 changes: 14 additions & 11 deletions addons/hearing/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ GVAR(lastPlayerVehicle) = objNull;
// Spawn volume updating process
[LINKFUNC(updateVolume), 1, false] call CBA_fnc_addPerFrameHandler;

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

// Update veh attunation when player veh changes
Expand Down Expand Up @@ -71,12 +72,7 @@ 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);
TRACE_2("removed unit eh",_oldPlayer,_firedEH);
};
// Don't add a new EH if the unit respawned
if ((_player getVariable [QGVAR(firedEH), -1]) == -1) then {
Expand All @@ -86,11 +82,7 @@ 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);
TRACE_2("added unit eh",_player,_firedEH);
};

GVAR(deafnessDV) = 0;
Expand All @@ -100,6 +92,17 @@ GVAR(lastPlayerVehicle) = objNull;
call FUNC(updateHearingProtection);
}, true] call CBA_fnc_addPlayerEventHandler;

addMissionEventHandler ["ProjectileCreated", {
params ["_projectile"];

if (!local _projectile) exitWith {};

// Rockets only explode on local clients
_projectile addEventHandler ["Explode", {
[QGVAR(explosion), _this] call CBA_fnc_globalEvent;
}];
}];

// Update protection on possible helmet change
["loadout", LINKFUNC(updateHearingProtection), false] call CBA_fnc_addPlayerEventHandler;
}] call CBA_fnc_addEventHandler;
55 changes: 55 additions & 0 deletions addons/hearing/functions/fnc_explosion.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "..\script_component.hpp"
/*
* Author: johnb43
* Handles deafness due to explosions going off near the player.
*
* Arguments:
* 0: Projectile <OBJECT>
* 1: Explosion position ASL <ARRAY>
* 2: Velocity <ARRAY> (unused)
*
* Return Value:
* None
*
* Example:
* [_projectile, [0, 0, 0], [0, 0, 0]] call ace_hearing_fnc_explosion
*
* Public: No
*/

// Ignore spectators, curators and alike
if ((getNumber (configOf ACE_player >> "isPlayableLogic")) == 1) exitWith {};

params ["_projectile", "_pos"];

// Don't allow for distances under 1
private _distance = ((eyePos ACE_player) vectorDistance _pos) max 1;

// Fast exit if explosion far away
if (_distance > 100) exitWith {
TRACE_1("too far away",_distance);
};

private _ammoConfig = configOf _projectile;
private _explosive = getNumber (_ammoConfig >> "explosive");

private _vehAttenuation = [GVAR(playerVehAttenuation), 1] select (isNull objectParent ACE_player || {isTurnedOut ACE_player});

TRACE_5("",typeOf _projectile,_distance,_explosive,_audibleFire,_vehAttenuation);

(if (isArray (_ammoConfig >> "soundHit1")) then {
getArray (_ammoConfig >> "soundHit1")
} else {
getArray (_ammoConfig >> "soundHit")
}) params ["", ["_volume", 1], "", ["_maxDistance", 1500]];

if (_distance > _maxDistance) exitWith {
TRACE_2("too far away",_distance,_maxDistance);
};

private _strength = _vehAttenuation * _explosive * _volume * _maxDistance / _distance^2;

TRACE_2("strength",_volume,_strength);

// Call immediately, as it will get picked up later by the update thread anyway
_strength call FUNC(earRinging);
26 changes: 0 additions & 26 deletions addons/hearing/functions/fnc_explosionNear.sqf

This file was deleted.

0 comments on commit a364c0d

Please sign in to comment.