Skip to content

Commit

Permalink
compatibleMagazines - Add optional bool to get all muzzles (#1123)
Browse files Browse the repository at this point in the history
* compatibleMagazines - Add optional bool to get all muzzles

* Apply suggestions from code review

Co-Authored-By: PabstMirror <pabstmirror@gmail.com>

* fix github code review messing up
  • Loading branch information
PabstMirror authored and commy2 committed Apr 24, 2019
1 parent a583752 commit 45564d0
Showing 1 changed file with 27 additions and 39 deletions.
66 changes: 27 additions & 39 deletions addons/common/fnc_compatibleMagazines.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Description:
Parameters:
_weapon - Weapon configName or config
_allMuzzles - Get magazines for all muzzles on this weapon (default: false)
Example:
(begin example)
Expand All @@ -22,56 +23,43 @@ Author:
---------------------------------------------------------------------------- */
SCRIPT(compatibleMagazines);

params [["_weapon", "", ["", configNull]]];
params [["_weapon", "", ["", configNull]], ["_allMuzzles", false, [false]]];

if (_weapon isEqualType "") then {
_weapon = configFile >> "CfgWeapons" >> _weapon;
};

private _cacheKey = str _weapon;
private _cacheKey = format ["%1#%2",_weapon,_allMuzzles];
if (isNil QGVAR(magNamespace)) then { GVAR(magNamespace) = call CBA_fnc_createNamespace; };

if (_cacheKey == "") exitWith {
ERROR_1("Weapon Does Not Exist %1",_this);
[]
};

if (isNil QGVAR(magNamespace)) then {
GVAR(magNamespace) = call CBA_fnc_createNamespace;
};

private _compatibleMagazines = GVAR(magNamespace) getVariable _cacheKey;

if (isNil "_compatibleMagazines") then {
_compatibleMagazines = [];

private _fnc_appendMagazines = {
params ["_muzzle"];

_compatibleMagazines append getArray (_muzzle >> "magazines");
private _returnMags = GVAR(magNamespace) getVariable _cacheKey;

if (isNil "_returnMags") then {
if (_allMuzzles) then {
_returnMags = []; // get all mags from all muzzles
{
if (_x == "this") then {
_returnMags append (_weapon call CBA_fnc_compatibleMagazines);
} else {
_returnMags append ((_weapon >> _x) call CBA_fnc_compatibleMagazines);
};
} forEach getArray (_weapon >> "muzzles");
_returnMags = _returnMags arrayIntersect _returnMags;
} else {
_returnMags = getArray (_weapon >> "magazines"); // get mags just for a specific muzzle
{
private _wellConfig = configFile >> "CfgMagazineWells" >> _x;

{
_compatibleMagazines append getArray _x;
_returnMags append getArray _x;
} forEach configProperties [_wellConfig, "isArray _x", false];
} forEach getArray (_muzzle >> "magazineWell");
};
} forEach (getArray (_weapon >> "magazineWell"));

{
if (_x == "this") then {
_weapon call _fnc_appendMagazines;
} else {
(_weapon >> _x) call _fnc_appendMagazines
};
} forEach getArray (_weapon >> "muzzles");

private _cfgMagazines = configFile >> "CfgMagazines";
_compatibleMagazines = _compatibleMagazines select {isClass (_cfgMagazines >> _x)};
_compatibleMagazines = _compatibleMagazines apply {configName (_cfgMagazines >> _x)};
_compatibleMagazines = _compatibleMagazines arrayIntersect _compatibleMagazines;

GVAR(magNamespace) setVariable [_cacheKey, _compatibleMagazines];
private _cfgMagazines = configFile >> "CfgMagazines";
_returnMags = _returnMags select {isClass (_cfgMagazines >> _x)};
_returnMags = _returnMags apply {configName (_cfgMagazines >> _x)};
_returnMags = _returnMags arrayIntersect _returnMags;
};
GVAR(magNamespace) setVariable [_cacheKey, _returnMags];
};

+_compatibleMagazines
+_returnMags

0 comments on commit 45564d0

Please sign in to comment.