Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compatibleMagazines - Add optional bool to get all muzzles #1123

Merged
merged 3 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions addons/common/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ activateAddons GVAR(addons);
}];
}] call CBA_fnc_addClassEventHandler;

GVAR(magNamespace) = call CBA_fnc_createNamespace; // namespace used for CBA_fnc_compatibleMagazines
PabstMirror marked this conversation as resolved.
Show resolved Hide resolved

PabstMirror marked this conversation as resolved.
Show resolved Hide resolved
ADDON = true;
65 changes: 26 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,42 @@ 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 (_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