diff --git a/addons/accessory/CfgFunctions.hpp b/addons/accessory/CfgFunctions.hpp index 0554973a9..15795c644 100644 --- a/addons/accessory/CfgFunctions.hpp +++ b/addons/accessory/CfgFunctions.hpp @@ -1,6 +1,7 @@ class CfgFunctions { class CBA { class Inventory { + PATHTO_FNC(addAttachmentCondition); PATHTO_FNC(switchableAttachments); }; }; diff --git a/addons/accessory/XEH_preInit.sqf b/addons/accessory/XEH_preInit.sqf index 9f6bf1f75..07b57c31f 100644 --- a/addons/accessory/XEH_preInit.sqf +++ b/addons/accessory/XEH_preInit.sqf @@ -4,6 +4,8 @@ if (!hasInterface) exitWith {}; #include "XEH_PREP.sqf" +GVAR(usageHash) = createHashMap; + [ELSTRING(common,WeaponsCategory), "MRT_SwitchItemNextClass_R", [LSTRING(railNext), LSTRING(railNext_tooltip)], { [1, "next"] call FUNC(switchAttachment) // return }, {}, [DIK_L, [false, true, false]]] call CBA_fnc_addKeybind; diff --git a/addons/accessory/fnc_addAttachmentCondition.sqf b/addons/accessory/fnc_addAttachmentCondition.sqf new file mode 100644 index 000000000..fa0169aba --- /dev/null +++ b/addons/accessory/fnc_addAttachmentCondition.sqf @@ -0,0 +1,29 @@ +#include "script_component.hpp" +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_addAttachmentCondition + +Description: + Adds condition to be able to switch to an attachment. + +Parameters: + 0: _item - Attachment classname + 1: _condition - Code (return false if not allowed) + +Returns: + None + +Examples: + (begin example) + ["ACE_acc_pointer_red", { false }] call CBA_fnc_addAttachmentCondition + (end) + +Author: + PabstMirror +---------------------------------------------------------------------------- */ + +params [["_item", "", [""]], ["_condition", {true}, [{}]]]; + +if (!isClass (configfile >> "CfgWeapons" >> _item)) exitWith { ERROR_1("Item not found [%1]", _item); }; + +private _usageArray = GVAR(usageHash) getOrDefault [_item, [], true]; +_usageArray pushBack _condition; diff --git a/addons/accessory/fnc_switchAttachment.sqf b/addons/accessory/fnc_switchAttachment.sqf index bb82e15dc..f08b9ee2a 100644 --- a/addons/accessory/fnc_switchAttachment.sqf +++ b/addons/accessory/fnc_switchAttachment.sqf @@ -41,17 +41,28 @@ private _currWeaponType = call { }; if (_currWeaponType < 0) exitWith {false}; -#define __cfgWeapons configfile >> "CfgWeapons" -#define __currItem __cfgWeapons >> _currItem +private _cfgWeapons = configfile >> "CfgWeapons"; -// Get the next/previous item from the attachement's config, but ignore inherited values -private _configs = if (_switchTo == "next") then { - configProperties [__currItem, "configName _x == 'MRT_SwitchItemNextClass'", false]; -} else { - configProperties [__currItem, "configName _x == 'MRT_SwitchItemPrevClass'", false]; -}; -if (_configs isNotEqualTo []) then { - _switchItem = getText (_configs select 0); +private _testItem = _currItem; +while {_testItem != ""} do { + // Get the next/previous item from the attachment's config, but ignore inherited values + private _configs = if (_switchTo == "next") then { + configProperties [_cfgWeapons >> _testItem, "configName _x == 'MRT_SwitchItemNextClass'", false]; + } else { + configProperties [_cfgWeapons >> _testItem, "configName _x == 'MRT_SwitchItemPrevClass'", false]; + }; + + if (_configs isEqualTo []) then { + _testItem = ""; + } else { + _testItem = getText (_configs select 0); + if (_testItem == "") exitWith {}; + if (_testItem == _currItem) exitWith { _testItem = ""; }; // same as start (full loop) + private _usageArray = GVAR(usageHash) getOrDefault [_testItem, []]; + if ((_usageArray findIf {([_testItem] call _x) isEqualTo false}) == -1) then { // none returned false + _switchItem = _testItem; + }; + }; }; TRACE_3("",_currItem,_switchTo,_switchItem); @@ -82,8 +93,9 @@ if (!isNil "_switchItem") then { }, [_unit, _currItem, _switchItem, _currWeaponType]] call CBA_fnc_execNextFrame; }; }; - private _switchItemHintText = getText (__cfgWeapons >> _switchItem >> "MRT_SwitchItemHintText"); - private _switchItemHintImage = getText (__cfgWeapons >> _switchItem >> "picture"); + private _configSwitchItem = _cfgWeapons >> _switchItem; + private _switchItemHintText = getText (_configSwitchItem >> "MRT_SwitchItemHintText"); + private _switchItemHintImage = getText (_configSwitchItem >> "picture"); if (_switchItemHintText isNotEqualTo "") then { [[_switchItemHintImage, 2.0], [_switchItemHintText], true] call CBA_fnc_notify; }; diff --git a/addons/accessory/fnc_switchableAttachments.sqf b/addons/accessory/fnc_switchableAttachments.sqf index 1f118ad87..9078721e6 100644 --- a/addons/accessory/fnc_switchableAttachments.sqf +++ b/addons/accessory/fnc_switchableAttachments.sqf @@ -39,4 +39,10 @@ while { } do {}; _forward = _forward + _backward; -_forward arrayIntersect _forward // return +_forward = _forward arrayIntersect _forward; +_forward select { + private _item = _x; + private _usageArray = GVAR(usageHash) getOrDefault [_x, []]; + (_usageArray findIf {([_item] call _x) isEqualTo false}) == -1 // none returned false +} // return +