Hearing - Improve and cleanup code (#9933)

Co-authored-by: Jouni Järvinen <rautamiekka@users.noreply.github.com>
Co-authored-by: PabstMirror <pabstmirror@gmail.com>
Co-authored-by: Grim <69561145+LinkIsGrim@users.noreply.github.com>
This commit is contained in:
johnb432 2024-05-25 01:05:19 +02:00 committed by GitHub
parent 22c6621878
commit dc3753893f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 112 additions and 99 deletions

View File

@ -3,6 +3,7 @@ PREP(addEarPlugs);
PREP(earRinging); PREP(earRinging);
PREP(explosionNear); PREP(explosionNear);
PREP(firedNear); PREP(firedNear);
PREP(getAmmoLoudness);
PREP(handleRespawn); PREP(handleRespawn);
PREP(hasEarPlugsIn); PREP(hasEarPlugsIn);
PREP(moduleHearing); PREP(moduleHearing);

View File

@ -14,7 +14,7 @@ if (!hasInterface) exitWith {};
#include "initKeybinds.inc.sqf" #include "initKeybinds.inc.sqf"
GVAR(cacheAmmoLoudness) = call CBA_fnc_createNamespace; GVAR(cacheAmmoLoudness) = createHashMap;
GVAR(deafnessDV) = 0; GVAR(deafnessDV) = 0;
GVAR(deafnessPrior) = 0; GVAR(deafnessPrior) = 0;

View File

@ -4,7 +4,7 @@
* Called on unit initialization. Adds earplugs if the unit is equipped with either a really loud primary weapon or a rocket launcher. * Called on unit initialization. Adds earplugs if the unit is equipped with either a really loud primary weapon or a rocket launcher.
* *
* Arguments: * Arguments:
* 0: A Soldier <Object> * 0: Unit <Object>
* *
* Return Value: * Return Value:
* None * None
@ -15,9 +15,9 @@
* Public: No * Public: No
*/ */
// only run this after the settings are initialized // Only run this after the settings are initialized
if !(EGVAR(common,settingsInitFinished)) exitWith { if (!EGVAR(common,settingsInitFinished)) exitWith {
EGVAR(common,runAtSettingsInitialized) pushBack [FUNC(addEarPlugs), _this]; EGVAR(common,runAtSettingsInitialized) pushBack [LINKFUNC(addEarPlugs), _this];
}; };
// Exit if hearing is disabled or if autoAdd is disabled // Exit if hearing is disabled or if autoAdd is disabled
@ -29,42 +29,48 @@ TRACE_2("params",_unit,typeOf _unit);
// Exit if the unit already has earplugs (in ears (persistence scenarios) or inventory) // Exit if the unit already has earplugs (in ears (persistence scenarios) or inventory)
if (_unit call FUNC(hasEarPlugsIn) || {[_unit, "ACE_EarPlugs"] call EFUNC(common,hasItem)}) exitWith {}; if (_unit call FUNC(hasEarPlugsIn) || {[_unit, "ACE_EarPlugs"] call EFUNC(common,hasItem)}) exitWith {};
// Add earplugs if enabled for everyone or if the soldier has a rocket launcher // Add earplugs if enabled for everyone or if the unit has a rocket launcher
if (GVAR(autoAddEarplugsToUnits) == 2 || {(secondaryWeapon _unit) != ""}) exitWith { if (GVAR(autoAddEarplugsToUnits) == 2 || {(secondaryWeapon _unit) != ""}) exitWith {
TRACE_1("has launcher - adding",_unit); TRACE_1("has launcher - adding",_unit);
_unit addItem "ACE_EarPlugs"; _unit addItem "ACE_EarPlugs";
}; };
// otherwise add earplugs if the soldier has a big rifle // Otherwise add earplugs if the unit has a big rifle
if ((primaryWeapon _unit) == "") exitWith {}; private _weapon = primaryWeapon _unit;
(primaryWeaponMagazine _unit) params [["_magazine", ""]]; if (_weapon == "") exitWith {};
if (_magazine == "") exitWith {};
private _cfgMagazine = configFile >> "CfgMagazines" >> _magazine; if (isNil QGVAR(cacheMaxAmmoLoudness)) then {
GVAR(cacheMaxAmmoLoudness) = createHashMap;
private _initSpeed = getNumber (_cfgMagazine >> "initSpeed"); };
private _ammo = getText (_cfgMagazine >> "ammo");
private _count = getNumber (_cfgMagazine >> "count"); // Cache maximum loudness for future calls
private _maxLoudness = GVAR(cacheMaxAmmoLoudness) getOrDefaultCall [_weapon, {
private _cfgAmmo = configFile >> "CfgAmmo"; // Get the weapon's compatible magazines, so that all magazines are cached
// From all the loudness factors, take the max
private _caliber = getNumber (_cfgAmmo >> _ammo >> "ACE_caliber"); private _maxLoudness = selectMax ((compatibleMagazines _weapon) apply {_x call FUNC(getAmmoLoudness)});
_caliber = call {
if (_ammo isKindOf ["ShellBase", _cfgAmmo]) exitWith { 80 }; // ace_gunbag_fnc_isMachineGun
if (_ammo isKindOf ["RocketBase", _cfgAmmo]) exitWith { 200 }; private _config = _weapon call CBA_fnc_getItemConfig;
if (_ammo isKindOf ["MissileBase", _cfgAmmo]) exitWith { 600 };
if (_ammo isKindOf ["SubmunitionBase", _cfgAmmo]) exitWith { 80 }; // Definition of a machine gun by BIS_fnc_itemType
[_caliber, 6.5] select (_caliber <= 0); private _cursor = getText (_config >> "cursor");
if (toLowerANSI _cursor in ["", "emptycursor"]) then {
_cursor = getText (_config >> "cursorAim");
}; };
private _loudness = (_caliber ^ 1.25 / 10) * (_initspeed / 1000) / 5;
// If unit has a machine gun boost effective loudness 50% // If unit has a machine gun boost effective loudness 50%
if (_count >= 50) then {_loudness = _loudness * 1.5}; if (_cursor == "MG") then {
_maxLoudness = _maxLoudness * 1.5;
};
TRACE_2("primaryWeapon",_unit,_loudness); _maxLoudness
}, true];
if (_loudness > 0.2) then { TRACE_3("primaryWeapon",_unit,_weapon,_maxLoudness);
if (_maxLoudness > 0.2) then {
TRACE_1("loud gun - adding",_unit); TRACE_1("loud gun - adding",_unit);
_unit addItem "ACE_EarPlugs"; _unit addItem "ACE_EarPlugs";
}; };

View File

@ -21,7 +21,6 @@ params ["_unit", "_damage"];
TRACE_2("explosion near player",_unit,_damage); TRACE_2("explosion near player",_unit,_damage);
private _strength = (0 max _damage) * 30; private _strength = (0 max _damage) * 30;
if (_strength < 0.01) exitWith {};
// Call inmediately, as it will get pick up later anyway by the update thread // Call inmediately, as it will get pick up later anyway by the update thread
[_strength] call FUNC(earRinging); [_strength] call FUNC(earRinging);

View File

@ -4,97 +4,60 @@
* Handles deafness due to large-caliber weapons going off near the player. * Handles deafness due to large-caliber weapons going off near the player.
* *
* Arguments: * Arguments:
* 0: Unit - Object the event handler is assigned to <OBJECT> * 0: Object the event handler is assigned to <OBJECT> (unused)
* 1: Firer: Object - Object which fires a weapon near the unit <OBJECT> * 1: Object which fires a weapon near the unit <OBJECT>
* 2: Distance - Distance in meters between the unit and firer <NUMBER> * 2: Distance in meters between the unit and firer <NUMBER>
* 3: weapon - Fired weapon <STRING> * 3: Weapon <STRING>
* 4: muzzle - Muzzle that was used (not used) <STRING> * 4: Muzzle <STRING>
* 5: mode - Current mode of the fired weapon (not used) <STRING> * 5: Current mode of the fired weapon <STRING>
* 6: ammo - Ammo used <STRING> * 6: Ammo <STRING>
* 7: Unit that fired the weapon <STRING>
* *
* Return Value: * Return Value:
* None * None
* *
* Example: * Example:
* [clientFiredNearEvent] call ace_hearing_fnc_firedNear * [player, player, 10, "arifle_MX_ACO_pointer_F", "arifle_MX_ACO_pointer_F", "single", "B_65x39_Caseless", player] call ace_hearing_fnc_firedNear
* [player, player, 10, "arifle_MX_ACO_pointer_F", "arifle_MX_ACO_pointer_F", "single", "B_65x39_Caseless"] call ace_hearing_fnc_firedNear
* *
* Public: No * Public: No
*/ */
params ["_object", "_firer", "_distance", "_weapon", "", "", "_ammo"]; params ["", "_firer", "_distance", "_weapon", "_muzzle", "_mode", "_ammo", "_gunner"];
if (_weapon in ["Throw", "Put"]) exitWith {}; if (_weapon in ["Throw", "Put"]) exitWith {};
if (_distance > 50) exitWith {}; if (_distance > 50) exitWith {};
private _vehAttenuation = [GVAR(playerVehAttenuation), 1] select ( _distance = 1 max _distance;
(ACE_player == (vehicle ACE_player)) || {isTurnedOut ACE_player}
);
private _distance = 1 max _distance;
private _silencer = switch (_weapon) do {
case (primaryWeapon _firer) : {(primaryWeaponItems _firer) select 0};
case (secondaryWeapon _firer) : {(secondaryWeaponItems _firer) select 0};
case (handgunWeapon _firer) : {(handgunItems _firer) select 0};
default {""};
};
private _audibleFireCoef = 1; private _audibleFireCoef = 1;
if (_silencer != "") then {
_audibleFireCoef = getNumber (configFile >> "CfgWeapons" >> _silencer >> "ItemInfo" >> "AmmoCoef" >> "audibleFire"); // Unit that fired is on foot
private _magazine = if (_gunner == _firer) then {
// Check if the unit has a suppressor
private _suppressor = (_firer weaponAccessories _weapon) select 0;
if (_suppressor != "") then {
_audibleFireCoef = getNumber (configFile >> "CfgWeapons" >> _suppressor >> "ItemInfo" >> "AmmoCoef" >> "audibleFire");
}; };
private _loudness = GVAR(cacheAmmoLoudness) getVariable (format ["%1%2",_weapon,_ammo]); (_firer weaponState _muzzle) select 3
if (isNil "_loudness") then {
private _muzzles = getArray (configFile >> "CfgWeapons" >> _weapon >> "muzzles");
private _weaponMagazines = getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines");
{
if (_x != "this") then {
private _muzzleMagazines = getArray (configFile >> "CfgWeapons" >> _weapon >> _x >> "magazines");
_weaponMagazines append _muzzleMagazines;
};
} forEach _muzzles;
{
private _ammoType = getText(configFile >> "CfgMagazines" >> _x >> "ammo");
_weaponMagazines set [_forEachIndex, [_x, _ammoType]];
} forEach _weaponMagazines;
private _magazine = "";
{
_x params ["_magazineType", "_ammoType"];
if (_ammoType == _ammo) exitWith {
_magazine = _magazineType;
};
} forEach _weaponMagazines;
if (_magazine == "") then {
_loudness = 0;
TRACE_2("No mag for Weapon/Ammo??",_weapon,_ammo);
} else { } else {
private _initSpeed = getNumber(configFile >> "CfgMagazines" >> _magazine >> "initSpeed"); // Unit that fired is in a vehicle
private _caliber = getNumber (configFile >> "CfgAmmo" >> _ammo >> "ACE_caliber"); (weaponState [_firer, _firer unitTurret _gunner, _weapon, _muzzle, _mode]) select 3
_caliber = call {
// If explicilty defined, use ACE_caliber
if ((count configProperties [(configFile >> "CfgAmmo" >> _ammo), "configName _x == 'ACE_caliber'", false]) == 1) exitWith {_caliber};
if (_ammo isKindOf ["ShellBase", (configFile >> "CfgAmmo")]) exitWith { 80 };
if (_ammo isKindOf ["RocketBase", (configFile >> "CfgAmmo")]) exitWith { 200 };
if (_ammo isKindOf ["MissileBase", (configFile >> "CfgAmmo")]) exitWith { 600 };
if (_ammo isKindOf ["SubmunitionBase", (configFile >> "CfgAmmo")]) exitWith { 80 };
[_caliber, 6.5] select (_caliber <= 0)
}; };
_loudness = (_caliber ^ 1.25 / 10) * (_initspeed / 1000) / 5; if (_magazine == "") exitWith {
TRACE_6("building cache",_weapon,_ammo,_magazine,_initSpeed,_caliber,_loudness); TRACE_5("No mag for weapon/ammo??",_weapon,_muzzle,_ammo,_firer,_gunner);
};
GVAR(cacheAmmoLoudness) setVariable [(format ["%1%2",_weapon,_ammo]), _loudness];
}; };
TRACE_6("mag",_magazine,_weapon,_muzzle,_ammo,_firer,_gunner);
private _vehAttenuation = [GVAR(playerVehAttenuation), 1] select (isNull objectParent ACE_player || {isTurnedOut ACE_player});
private _loudness = _magazine call FUNC(getAmmoLoudness);
_loudness = _loudness * _audibleFireCoef; _loudness = _loudness * _audibleFireCoef;
private _strength = _vehAttenuation * (_loudness - (_loudness / 50 * _distance)); // linear drop off private _strength = _vehAttenuation * (_loudness - (_loudness / 50 * _distance)); // linear drop off
TRACE_1("result",_strength); TRACE_1("result",_strength);
if (_strength < 0.01) exitWith {};
// Call inmediately, as it will get pick up later anyway by the update thread // Call inmediately, as it will get pick up later anyway by the update thread
[_strength] call FUNC(earRinging); [_strength] call FUNC(earRinging);

View File

@ -0,0 +1,44 @@
#include "..\script_component.hpp"
/*
* Author: KoffeinFlummi, commy2, johnb43
* Get the loudness of ammo.
* However, because `initSpeed` is a magazine attribute, the magazine name needs to be used instead of the ammo.
*
* Arguments:
* 0: Magazine <STRING>
*
* Return Value:
* None
*
* Example:
* "30Rnd_65x39_caseless_mag" call ace_hearing_fnc_getAmmoLoudness
*
* Public: No
*/
params ["_magazine"];
GVAR(cacheAmmoLoudness) getOrDefaultCall [_magazine, {
private _magazineConfig = configFile >> "CfgMagazines" >> _magazine;
private _ammo = getText (_magazineConfig >> "ammo");
private _initSpeed = getNumber (_magazineConfig >> "initSpeed");
private _cfgAmmo = configFile >> "CfgAmmo";
private _ammoConfig = _cfgAmmo >> _ammo;
private _caliber = getNumber (_ammoConfig >> "ACE_caliber");
_caliber = switch (true) do {
// If explicilty defined, use ACE_caliber
case (inheritsFrom (_ammoConfig >> "ACE_caliber") isEqualTo _ammoConfig): {_caliber};
case (_ammo isKindOf ["ShellBase", _cfgAmmo]): {80};
case (_ammo isKindOf ["RocketBase", _cfgAmmo]): {200};
case (_ammo isKindOf ["MissileBase", _cfgAmmo]): {600};
case (_ammo isKindOf ["SubmunitionBase", _cfgAmmo]): {80};
default {[_caliber, 6.5] select (_caliber <= 0)};
};
private _loudness = (_caliber ^ 1.25 / 10) * (_initspeed / 1000) / 5;
TRACE_5("building cache",_ammo,_magazine,_initSpeed,_caliber,_loudness);
_loudness
}, true]