diff --git a/addons/reload/CfgEventHandlers.hpp b/addons/reload/CfgEventHandlers.hpp index a3dd6de29b..0a01e72a4f 100644 --- a/addons/reload/CfgEventHandlers.hpp +++ b/addons/reload/CfgEventHandlers.hpp @@ -13,14 +13,12 @@ class Extended_PreInit_EventHandlers { class Extended_PostInit_EventHandlers { class ADDON { - init = QUOTE( call COMPILE_FILE(XEH_postInit) ); + init = QUOTE(call COMPILE_FILE(XEH_postInit)); }; }; -class Extended_Take_EventHandlers { +class Extended_Reloaded_EventHandlers { class CAManBase { - class ACE_AmmoIndicatorReload { - clientTake = QUOTE(if (_this select 0 == ACE_player && {GVAR(DisplayText)} && {(_this select 1) in [ARR_3(uniformContainer (_this select 0), vestContainer (_this select 0), backpackContainer (_this select 0))]} && {_this select 2 == currentMagazine (_this select 0)}) then {[ARR_2(_this select 0, vehicle (_this select 0))] call FUNC(displayAmmo)};); - }; + ADDON = QUOTE(call FUNC(onReload)); }; }; diff --git a/addons/reload/XEH_PREP.hpp b/addons/reload/XEH_PREP.hpp index bfd89b098e..b6fd6c0db7 100644 --- a/addons/reload/XEH_PREP.hpp +++ b/addons/reload/XEH_PREP.hpp @@ -1,6 +1,7 @@ - PREP(canCheckAmmo); PREP(canLinkBelt); PREP(checkAmmo); PREP(displayAmmo); +PREP(onInventoryChanged); +PREP(onReload); PREP(startLinkingBelt); diff --git a/addons/reload/XEH_postInit.sqf b/addons/reload/XEH_postInit.sqf index 7a5e17464c..8387664316 100644 --- a/addons/reload/XEH_postInit.sqf +++ b/addons/reload/XEH_postInit.sqf @@ -59,3 +59,5 @@ if (!hasInterface) exitWith {}; _receiver addMagazine _magazine; }] call CBA_fnc_addEventHandler; + +["loadout", {call FUNC(onInventoryChanged)}] call CBA_fnc_addPlayerEventHandler; diff --git a/addons/reload/functions/fnc_onInventoryChanged.sqf b/addons/reload/functions/fnc_onInventoryChanged.sqf new file mode 100644 index 0000000000..a54f1fe819 --- /dev/null +++ b/addons/reload/functions/fnc_onInventoryChanged.sqf @@ -0,0 +1,47 @@ +/* + * Author: BaerMitUmlaut + * Handles unit weight coefficient for empty magazines. + * + * Arguments: + * 0: Unit + * 1: Previous loadout + * + * Return Value: + * None + */ +#include "script_component.hpp" +params ["_unit", "_previousLoadout"]; + +private _loadout = getUnitLoadout _unit; +_loadout params ["", "", "", "_uniformInventory", "_vestInventory", "_backpackInventory"]; + +private _prevEmptyMagsWeight = _unit getVariable [QGVAR(emptyMagazinesWeight), 0]; +private _emptyMagsWeight = 0; + +if (isNil QGVAR(magWeightCache)) then { + GVAR(magWeightCache) = [] call CBA_fnc_createNamespace; +}; + +{ + if !(_x isEqualTo []) then { + private _mags = _x select 1; + { + _x params ["_classname", "_ammoCount", "_magCount"]; + + if (_ammoCount == 0) then { + private _weight = GVAR(magWeightCache) getVariable _classname; + if (isNil "_weight") then { + _weight = getNumber (configFile >> "CfgMagazines" >> _classname >> "mass"); + GVAR(magWeightCache) setVariable [_classname, _weight]; + systemChat "nocache"; + }; + _emptyMagsWeight = _emptyMagsWeight + _weight * _magCount; + }; + } forEach _mags; + }; +} forEach [_uniformInventory, _vestInventory, _backpackInventory]; + +if (_emptyMagsWeight != _prevEmptyMagsWeight) then { + _unit setVariable [QGVAR(emptyMagazinesWeight), _emptyMagsWeight]; + [_unit, _unit, -0.8 * (_emptyMagsWeight - _prevEmptyMagsWeight)] call EFUNC(movement,addLoadToUnitContainer); +}; diff --git a/addons/reload/functions/fnc_onReload.sqf b/addons/reload/functions/fnc_onReload.sqf new file mode 100644 index 0000000000..e3d9cf33ae --- /dev/null +++ b/addons/reload/functions/fnc_onReload.sqf @@ -0,0 +1,27 @@ +/* + * Author: BaerMitUmlaut + * Handles the ammo display on reload and and adds empty magazines. + * + * Arguments: + * 0: Unit + * 1: Weapon + * 2: Muzzle + * 3: New magazine info + * 4: Old magazine info + * + * Return Value: + * None + */ +#include "script_component.hpp" +params ["_unit", "_weapon", "_muzzle", "_newMagInfo", ["_oldMagInfo", ["", -1]]]; +_oldMagInfo params ["_oldMagClassname", "_oldMagAmmo"]; + +if (_unit == ACE_player) then { + if (GVAR(DisplayText)) then { + [_unit] call FUNC(displayAmmo); + }; + + if (_oldMagAmmo == 0 && {_unit canAdd _oldMagClassname} && {_weapon != secondaryWeapon _unit}) then { + _unit addMagazine [_oldMagClassname, 0]; + }; +};