-
Notifications
You must be signed in to change notification settings - Fork 148
/
fnc_compatibleItems.sqf
96 lines (74 loc) · 3.13 KB
/
fnc_compatibleItems.sqf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_compatibleItems
Description:
Return all compatible weapon attachments.
Parameters:
_weapon - A weapons class name <STRING>
_typefilter - Optional filter. Can be "muzzle", "optic", "pointer" or "bipod". <STRING, NUMBER>
Returns:
Class names of attachments compatible with weapon <ARRAY>
Examples:
(begin example)
_acclist = ["LMG_Mk200_F"] call CBA_fnc_compatibleItems;
_muzzleacclist = ["LMG_Mk200_F", "muzzle"] call CBA_fnc_compatibleItems;
(end)
Author:
Original by Karel Moricky, Enhanced by Robalo, jokoho, commy2, johnb43
---------------------------------------------------------------------------- */
SCRIPT(compatibleItems);
params [["_weapon", "", [""]], ["_typefilter", "", ["", 0]]];
if (_weapon == "") exitWith {[]};
private _cfgWeapons = configFile >> "CfgWeapons";
private _weaponConfig = _cfgWeapons >> _weapon;
// Check if weapon exists
if !(isClass _weaponConfig) exitWith {
["'%1' not found in CfgWeapons", _weapon] call BIS_fnc_error;
[]
};
private _typeFilterExists = _typefilter isNotEqualTo "";
// Convert filter into number (if string)
if (_typeFilterExists && {_typefilter isEqualType ""}) then {
_typefilter = [101, 201, 301, 302] param [["muzzle", "optic", "pointer", "bipod"] find _typefilter, -1];
};
// Check if valid type filter
if (_typeFilterExists && {!(_typefilter in [101, 201, 301, 302])}) exitWith {[]};
if (isNil QGVAR(namespace)) then {
GVAR(namespace) = createHashMap;
};
// Get cached result, if it exists
private _cachekey = format ["%1#%2", _weapon, ["all", _typefilter] select _typeFilterExists];
private _compatibleItems = GVAR(namespace) get _cachekey;
if (!isNil "_compatibleItems") exitWith {
+_compatibleItems
};
if (_typeFilterExists) then {
// Get all compatible weapon attachments, then filter
_compatibleItems = _weapon call CBA_fnc_compatibleItems;
_compatibleItems = _compatibleItems select {_typefilter == getNumber (_cfgWeapons >> _x >> "itemInfo" >> "type")};
} else {
_compatibleItems = [];
{
private _cfgCompatibleItems = _x >> "compatibleItems";
if (isArray _cfgCompatibleItems) then {
{
// Ensure item class name is in config case
_compatibleItems pushBackUnique configName (_cfgWeapons >> _x);
} forEach getArray _cfgCompatibleItems;
} else {
if (isClass _cfgCompatibleItems) then {
{
if (getNumber _x > 0) then {
// Ensure item class name is in config case
_compatibleItems pushBackUnique configName (_cfgWeapons >> configName _x);
};
} forEach configProperties [_cfgCompatibleItems, "isNumber _x"];
};
};
} forEach configProperties [_weaponConfig >> "WeaponSlotsInfo", "isClass _x"];
// Remove non-existent item(s)
_compatibleItems deleteAt (_compatibleItems find "");
};
// Cache result
GVAR(namespace) set [_cachekey, _compatibleItems];
+_compatibleItems