diff --git a/addons/reload/CfgEventHandlers.hpp b/addons/reload/CfgEventHandlers.hpp index 8de6a01f06..754ff0db87 100644 --- a/addons/reload/CfgEventHandlers.hpp +++ b/addons/reload/CfgEventHandlers.hpp @@ -14,7 +14,7 @@ class Extended_PostInit_EventHandlers { class Extended_Take_EventHandlers { class CAManBase { class ACE_AmmoIndicatorReload { - clientTake = QUOTE(if (_this select 0 == ACE_player && {(_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_3(_this select 0, vehicle (_this select 0), true)] call FUNC(checkAmmo)};); + clientTake = QUOTE(if (_this select 0 == ACE_player && {(_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)};); }; }; }; diff --git a/addons/reload/XEH_postInit.sqf b/addons/reload/XEH_postInit.sqf index 887b673503..b39b357ad0 100644 --- a/addons/reload/XEH_postInit.sqf +++ b/addons/reload/XEH_postInit.sqf @@ -15,7 +15,7 @@ if !(hasInterface) exitWith {}; {(vehicle ACE_player) isKindOf 'StaticWeapon'}) exitWith {false}; // Statement - [ACE_player, vehicle ACE_player, false] call FUNC(checkAmmo); + [ACE_player] call FUNC(checkAmmo); true }, [19, [false, true, false]], diff --git a/addons/reload/XEH_preInit.sqf b/addons/reload/XEH_preInit.sqf index f0e19f9379..7f22161a77 100644 --- a/addons/reload/XEH_preInit.sqf +++ b/addons/reload/XEH_preInit.sqf @@ -4,6 +4,7 @@ ADDON = false; PREP(canLinkBelt); PREP(checkAmmo); +PREP(displayAmmo); PREP(startLinkingBelt); ADDON = true; diff --git a/addons/reload/functions/fnc_checkAmmo.sqf b/addons/reload/functions/fnc_checkAmmo.sqf index fab1ebf333..7c24ff1ff3 100644 --- a/addons/reload/functions/fnc_checkAmmo.sqf +++ b/addons/reload/functions/fnc_checkAmmo.sqf @@ -1,13 +1,11 @@ /* - * Author: commy2 - * + * Author: commy2 and CAA-Picard * Count the ammo of the currently loaded magazine or count rifle grenades. Play animation and display message. - * + * * Argument: - * 0: The player (Object) - * 1: The vehicle (Object) - * 2: Skip the animation? Used after reloading (Bool) - * + * 0: Player + * 1: Target. Optional, if not suplied the player counts his personal or static weapon ammo + * * Return value: * Nothing */ @@ -15,87 +13,24 @@ #define COUNT_BARS 12 -private ["_unit", "_vehicle"]; +EXPLODE_1_PVT(_this,_unit); -_unit = _this select 0; -_vehicle = _this select 1; +private ["_target"]; +_target = vehicle _unit; -if (_unit != _vehicle && !(_vehicle isKindOf "StaticWeapon")) then { - _vehicle = _unit; +if (count _this > 1) then { + _target = _this select 1; +} else { + + // If the unit is on foot, count it's own ammo + if (_unit == _target) exitWith {}; + + // If it's mounted on a movile weapon, count it's own ammo + if !(_target isKindOf "StaticWeapon") then { + _target = _unit; + }; }; -[_vehicle, currentWeapon _vehicle, currentMuzzle _vehicle, currentMagazine _vehicle, _this select 2] spawn { - _vehicle = _this select 0; - _weapon = _this select 1; - _muzzle = _this select 2; - _magazine = _this select 3; - _skipDelay = _this select 4; +_unit playActionNow "Gear"; - if (currentWeapon _vehicle == "") exitWith {}; - if (typeName _muzzle != "STRING") then {_muzzle = _weapon}; - - _showNumber = false; - _ammo = 0; - _maxRounds = 1; - _count = 0; - - // not grenade launcher - if (_muzzle == _weapon) then { - _maxRounds = getNumber (configFile >> "CfgMagazines" >> _magazine >> "count") max 1; - - _ammo = _vehicle ammo _weapon; - if (_maxRounds >= COUNT_BARS) then { - _count = round (COUNT_BARS * _ammo / _maxRounds); - - if (_ammo > 0) then {_count = _count max 1}; - if (_ammo < _maxRounds) then {_count = _count min (COUNT_BARS - 1)}; - } else { - _count = _ammo; - }; - - // grenade launcher - } else { - _showNumber = true; - - _count = if (_magazine != "") then { - {_x == _magazine} count (magazines _vehicle + [_magazine]) - } else { - {_x in getArray (configFile >> "CfgWeapons" >> _weapon >> _muzzle >> "Magazines")} count magazines _vehicle - }; - }; - - if !(_skipDelay) then { - _vehicle playActionNow "Gear"; - sleep 1 - }; - - _text = if (_showNumber) then { - parseText format ["%1x", _count] - } else { - _color = [ - 2 * (1 - _ammo / _maxRounds) min 1, - 2 * _ammo / _maxRounds min 1, - 00 - ]; - - _string = ""; - for "_a" from 1 to _count do { - _string = _string + "|"; - }; - _text = [_string, _color] call EFUNC(common,stringToColoredText); - - _string = ""; - for "_a" from (_count + 1) to (_maxRounds min COUNT_BARS) do { - _string = _string + "|"; - }; - - composeText [ - _text, - [_string, [0.5, 0.5, 0.5]] call EFUNC(common,stringToColoredText) - ] - }; - - _picture = getText (configFile >> "CfgMagazines" >> _magazine >> "picture"); - - [_text, _picture] call EFUNC(common,displayTextPicture); -}; +[FUNC(displayAmmo), [_target], 1, 0.1] call EFUNC(common,waitAndExecute); diff --git a/addons/reload/functions/fnc_displayAmmo.sqf b/addons/reload/functions/fnc_displayAmmo.sqf new file mode 100644 index 0000000000..07e1a4593a --- /dev/null +++ b/addons/reload/functions/fnc_displayAmmo.sqf @@ -0,0 +1,94 @@ +/* + * Author: commy2 and CAA-Picard + * Display the ammo of the currently loaded magazine of the target or count rifle grenades. + * + * Argument: + * 0: Target + * + * Return value: + * Nothing + */ +#include "script_component.hpp" + +#define COUNT_BARS 12 + +EXPLODE_1_PVT(_this,_target); + +private ["_weapon","_muzzle","_magazine","_showNumber","_ammo","_maxRounds","_count","_text","_color","_picture"]; + +_weapon = currentWeapon _target; +_muzzle = currentMuzzle _target; +_magazine = currentMagazine _target; + +// currentWeapon returns "" for static weapons before they are shot once +if (_target isKindOf "StaticWeapon") then { + if (_weapon == "") then { + if (count (weapons _target) == 1) then { + _weapon = (weapons _target) select 0; + _muzzle = _weapon; + }; + }; +}; + +if (_weapon == "") exitWith {}; +if (typeName _muzzle != "STRING") then {_muzzle = _weapon}; + +_showNumber = false; +_ammo = 0; +_maxRounds = 1; +_count = 0; + +// not grenade launcher +if (_muzzle == _weapon) then { + _maxRounds = getNumber (configFile >> "CfgMagazines" >> _magazine >> "count") max 1; + + _ammo = _target ammo _weapon; + if (_maxRounds >= COUNT_BARS) then { + _count = round (COUNT_BARS * _ammo / _maxRounds); + + if (_ammo > 0) then {_count = _count max 1}; + if (_ammo < _maxRounds) then {_count = _count min (COUNT_BARS - 1)}; + } else { + _count = _ammo; + }; + +// grenade launcher +} else { + _showNumber = true; + + _count = if (_magazine != "") then { + {_x == _magazine} count (magazines _target + [_magazine]) + } else { + {_x in getArray (configFile >> "CfgWeapons" >> _weapon >> _muzzle >> "Magazines")} count magazines _target + }; +}; + +_text = if (_showNumber) then { + parseText format ["%1x", _count] +} else { + _color = [ + 2 * (1 - _ammo / _maxRounds) min 1, + 2 * _ammo / _maxRounds min 1, + 00 + ]; + + _string = ""; + for "_a" from 1 to _count do { + _string = _string + "|"; + }; + _text = [_string, _color] call EFUNC(common,stringToColoredText); + + _string = ""; + for "_a" from (_count + 1) to (_maxRounds min COUNT_BARS) do { + _string = _string + "|"; + }; + + composeText [ + _text, + [_string, [0.5, 0.5, 0.5]] call EFUNC(common,stringToColoredText) + ] +}; + +_picture = getText (configFile >> "CfgMagazines" >> _magazine >> "picture"); + +[_text, _picture] call EFUNC(common,displayTextPicture);