Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hearing - Fix explosions not affecting hearing #10002

Merged
merged 10 commits into from
Aug 12, 2024
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;
}];
}];

Comment on lines +95 to +105
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be moved to common, if the frag rewrite needs it.

// 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;
johnb432 marked this conversation as resolved.
Show resolved Hide resolved

// 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.

Loading