diff --git a/addons/hearing/XEH_PREP.hpp b/addons/hearing/XEH_PREP.hpp index a2bcbb708a..64161300d3 100644 --- a/addons/hearing/XEH_PREP.hpp +++ b/addons/hearing/XEH_PREP.hpp @@ -1,6 +1,6 @@ PREP(addEarPlugs); PREP(earRinging); -PREP(explosionNear); +PREP(explosion); PREP(firedNear); PREP(getAmmoLoudness); PREP(handleRespawn); diff --git a/addons/hearing/XEH_postInit.sqf b/addons/hearing/XEH_postInit.sqf index 4261933bd9..e0bee153f8 100644 --- a/addons/hearing/XEH_postInit.sqf +++ b/addons/hearing/XEH_postInit.sqf @@ -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 @@ -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 { @@ -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; @@ -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; diff --git a/addons/hearing/functions/fnc_explosion.sqf b/addons/hearing/functions/fnc_explosion.sqf new file mode 100644 index 0000000000..159f666011 --- /dev/null +++ b/addons/hearing/functions/fnc_explosion.sqf @@ -0,0 +1,55 @@ +#include "..\script_component.hpp" +/* + * Author: johnb43 + * Handles deafness due to explosions going off near the player. + * + * Arguments: + * 0: Projectile + * 1: Explosion position ASL + * 2: Velocity (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); diff --git a/addons/hearing/functions/fnc_explosionNear.sqf b/addons/hearing/functions/fnc_explosionNear.sqf deleted file mode 100644 index 583c55749e..0000000000 --- a/addons/hearing/functions/fnc_explosionNear.sqf +++ /dev/null @@ -1,26 +0,0 @@ -#include "..\script_component.hpp" -/* - * Author: KoffeinFlummi, commy2, Ruthberg - * Handles deafness due to explosions going off near the player. - * - * Arguments: - * 0: Unit - * 1: Damage inflicted to the unit - * - * Return Value: - * None - * - * Example: - * [clientExplosionEvent] call ace_hearing_fnc_explosionNear - * - * Public: No - */ - -params ["_unit", "_damage"]; - -TRACE_2("explosion near player",_unit,_damage); - -private _strength = (0 max _damage) * 30; - -// Call immediately, as it will get picked up later by the update thread anyway -_strength call FUNC(earRinging);