diff --git a/addons/csw/XEH_PREP.hpp b/addons/csw/XEH_PREP.hpp index 36e898913a..cc3545042b 100644 --- a/addons/csw/XEH_PREP.hpp +++ b/addons/csw/XEH_PREP.hpp @@ -20,6 +20,7 @@ PREP(canGetIn); PREP(getIn); PREP(compatibleMagazines); +PREP(getAvailableAmmo); PREP(getCarryMagazine); PREP(getNearbySources); PREP(getSourceCompatibleMagazines); diff --git a/addons/csw/functions/fnc_getAvailableAmmo.sqf b/addons/csw/functions/fnc_getAvailableAmmo.sqf new file mode 100644 index 0000000000..cd296eebcb --- /dev/null +++ b/addons/csw/functions/fnc_getAvailableAmmo.sqf @@ -0,0 +1,59 @@ +#include "script_component.hpp" +/* + * Author: LinkIsGrim + * Gets available ammo for a CSW + * + * Arguments: + * 0: CSW + * 1: Only loaded magazines (default: false) + * 2: Skip ammo from vehicles (default: true) + * 3: Include CSW crew (default: true) + * + * Return Value: + * Available Ammo + * Magazine classname + * Total Ammo + * + * Example: + * [cursorObject] call ace_csw_fnc_getAvailableAmmo + * + * Public: Yes + */ +params [["_vehicle", objNull, [objNull]], ["_onlyLoaded", false, [false]], ["_skipVehicles", true, [true]], ["_includeCrew", true, [true]]]; + +if (isNull _vehicle) exitWith {createHashMap}; + +private _availableMagazines = createHashMap; + +private _fnc_addAmmo = { + params ["_magazine", "_ammo"]; + if !(_magazine in _availableMagazines) then { + _availableMagazines set [_magazine, _ammo]; + } else { + _availableMagazines set [_magazine, (_availableMagazines get _magazine) + _ammo]; + }; +}; + +{ + _x params ["_xMag", "", "_xAmmo"]; + + private _carryMag = _xMag call FUNC(getCarryMagazine); + if (_carryMag isEqualTo "") then {continue}; + + [_carryMag, _xAmmo] call _fnc_addAmmo +} forEach (magazinesAllTurrets _vehicle); + +if (_onlyLoaded) exitWith {_availableMagazines}; + +[QGVAR(clearNearbySourcesCache), []] call CBA_fnc_localEvent; +private _sources = [_vehicle, _skipVehicles, _includeCrew] call FUNC(getNearbySources); +if (_sources isEqualTo []) exitWith {_availableMagazines}; + +{ + private _source = _x; + { + _x call _fnc_addAmmo + } forEach ([_source, _vehicle] call FUNC(getSourceCompatibleMagazines)); +} forEach _sources; + +_availableMagazines // return diff --git a/addons/csw/functions/fnc_getNearbySources.sqf b/addons/csw/functions/fnc_getNearbySources.sqf index 22fd19f045..4fb98d1117 100644 --- a/addons/csw/functions/fnc_getNearbySources.sqf +++ b/addons/csw/functions/fnc_getNearbySources.sqf @@ -4,7 +4,9 @@ * Gets available ammo sources for loading a CSW * * Arguments: - * 0: Unit attempting to load + * 0: Unit or vehicle attempting to load + * 1: Skip vehicle sources (default: false) + * 2: Include CSW crew (default: false) * * Return Value: * Ammo sources @@ -14,16 +16,25 @@ * * Public: No */ -params ["_unit"]; - -[_unit, { - params ["_unit"]; +[_this, { + params ["_unit", ["_skipVehicles", false], ["_includeCrew", false]]; private _nearSupplies = (_unit nearSupplies 5) select { isNull (group _x) || {!([_x] call EFUNC(common,isPlayer)) && {[side group _unit, side group _x] call BIS_fnc_sideIsFriendly}} }; - _nearSupplies pushBack _unit; + if (_includeCrew) then { + _nearSupplies append (crew _unit); + }; + + if (_skipVehicles) then { + _nearSupplies = _nearSupplies select { + private _source = _x; + (["Ship", "Car", "Air", "Tank"] findIf {_source isKindOf _x}) == -1 + }; + }; + + _nearSupplies pushBackUnique _unit; { if (_x isKindOf "CAManBase") then { _nearSupplies append [uniformContainer _x, vestContainer _x, backpackContainer _x]; @@ -37,4 +48,4 @@ params ["_unit"]; } forEach _nearSupplies; _nearSupplies select {!(_x isKindOf "CAManBase")} // return -}, _unit, QGVAR(nearbySourcesCache), NEARBY_SOURCES_CACHE_EXPIRY, QGVAR(clearNearbySourcesCache)] call EFUNC(common,cachedCall); +}, _this select 0, QGVAR(nearbySourcesCache), NEARBY_SOURCES_CACHE_EXPIRY, QGVAR(clearNearbySourcesCache)] call EFUNC(common,cachedCall);