Skip to content

Commit

Permalink
Accessory - Add conditional blocking of switchable attachments (#1595)
Browse files Browse the repository at this point in the history
Co-authored-by: Jouni Järvinen <[email protected]>
Co-authored-by: jonpas <[email protected]>
  • Loading branch information
3 people authored Sep 6, 2023
1 parent c760f60 commit 33f6c0d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
1 change: 1 addition & 0 deletions addons/accessory/CfgFunctions.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class CfgFunctions {
class CBA {
class Inventory {
PATHTO_FNC(addAttachmentCondition);
PATHTO_FNC(switchableAttachments);
};
};
Expand Down
2 changes: 2 additions & 0 deletions addons/accessory/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions addons/accessory/fnc_addAttachmentCondition.sqf
Original file line number Diff line number Diff line change
@@ -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 <STRING>
1: _condition - Code (return false if not allowed) <CODE>
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;
36 changes: 24 additions & 12 deletions addons/accessory/fnc_switchAttachment.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
};
Expand Down
8 changes: 7 additions & 1 deletion addons/accessory/fnc_switchableAttachments.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 33f6c0d

Please sign in to comment.