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

Interaction - Improve FUNC(switchWeaponAttachment) #10145

Merged
merged 7 commits into from
Aug 9, 2024
Merged
36 changes: 24 additions & 12 deletions addons/common/functions/fnc_switchAttachmentMode.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
/*
* Author: PabstMirror
* Switch attachment from one mode to another - based on CBA_accessory_fnc_switchAttachment
* ToDo: Port this to CBA?
*
* Arguments:
* 0: Unit <OBJECT>
* 1: From <STRING>
* 2: To <STRING>
* 1: Weapon (String or CBA-Weapon-Index (not ace's getWeaponIndex)) <STRING|NUMBER>
* 2: From <STRING>
* 3: To <STRING>
*
* Return Value:
* None
*
* Example:
* [player, "ACE_DBAL_A3_Green_VP", "ACE_DBAL_A3_Green"] call ace_common_fnc_switchAttachmentMode
* [player, 0, "ACE_DBAL_A3_Green_VP", "ACE_DBAL_A3_Green"] call ace_common_fnc_switchAttachmentMode
*
* Public: No
*/

params ["_unit", "_currItem", "_switchItem"];
TRACE_3("switchAttachmentMode",_unit,_currItem,_switchItem);

switch (currentWeapon _unit) do {
case (""): {};
params ["_unit", "_weapon", "_currItem", "_switchItem"];
TRACE_4("switchAttachmentMode",_unit,_weapon,_currItem,_switchItem);

if (_weapon isEqualTo "") exitWith {};

private _exit = _unit != ACE_player;
switch (_weapon) do {
case 0;
case (primaryWeapon _unit): {
private _currWeaponType = 0;
_unit removePrimaryWeaponItem _currItem;
Expand All @@ -31,6 +36,7 @@ switch (currentWeapon _unit) do {
["CBA_attachmentSwitched", _this] call CBA_fnc_localEvent;
}, [_unit, _currItem, _switchItem, _currWeaponType]] call CBA_fnc_execNextFrame;
};
case 1;
case (handgunWeapon _unit): {
private _currWeaponType = 1;
_unit removeHandgunItem _currItem;
Expand All @@ -40,6 +46,7 @@ switch (currentWeapon _unit) do {
["CBA_attachmentSwitched", _this] call CBA_fnc_localEvent;
}, [_unit, _currItem, _switchItem, _currWeaponType]] call CBA_fnc_execNextFrame;
};
case 2;
case (secondaryWeapon _unit): {
private _currWeaponType = 2;
_unit removeSecondaryWeaponItem _currItem;
Expand All @@ -49,13 +56,18 @@ switch (currentWeapon _unit) do {
["CBA_attachmentSwitched", _this] call CBA_fnc_localEvent;
}, [_unit, _currItem, _switchItem, _currWeaponType]] call CBA_fnc_execNextFrame;
};
default {
ERROR_1("bad weapon - %1",_this);
_exit = true;
};
};
if (_exit) exitWith {}; // Don't notify if the unit isn't the local player or if an invalid weapon was passed

private _configSwitchItem = configfile >> "CfgWeapons" >> _switchItem;
private _switchItemHintText = getText (_configSwitchItem >> "MRT_SwitchItemHintText");
private _switchItemHintImage = getText (_configSwitchItem >> "picture");
if (_switchItemHintText isNotEqualTo "") then {

playSound "click";
if (_switchItemHintText != "") then {
[[_switchItemHintImage, 2.0], [_switchItemHintText], true] call CBA_fnc_notify;
};
if (_unit == ACE_player) then {
playSound "click";
};
10 changes: 7 additions & 3 deletions addons/interaction/functions/fnc_getWeaponAttachmentsActions.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ params ["_unit"];
{},
{true},
{
params ["_unit", "", "_args"];
params ["", "_unit", "_args"];
_args params ["_attachment", "_name", "_picture", "_weaponItems", "_currentWeapon"];

private _cfgWeapons = configFile >> "CfgWeapons";
Expand Down Expand Up @@ -110,10 +110,14 @@ params ["_unit"];
QGVAR(switch_) + _x,
format ["%1: %2", localize "str_sensortype_switch", _modeName],
getText (_config >> "picture"),
LINKFUNC(switchWeaponAttachment),
{
params ["", "_unit", "_actionParams"];
_actionParams params ["_weapon", "_newAttachment", "_oldAttachment"];
[_unit, _weapon, _oldAttachment, _newAttachment] call EFUNC(common,switchAttachmentMode);
},
{true},
{},
[_currentWeapon, _x, ""]
[_currentWeapon, _x, _attachment]
] call EFUNC(interact_menu,createAction),
[],
_unit
Expand Down
41 changes: 29 additions & 12 deletions addons/interaction/functions/fnc_switchWeaponAttachment.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* Switches weapon attachment.
*
* Arguments:
* 0: Target <OBJECT>
* 1: Player (not used) <OBJECT>
* 0: Target (not used) <OBJECT>
* 1: Player <OBJECT>
* 2: Action params <ARRAY>
* - 0: Weapon <STRING>
* - 1: New Attachment <STRING>
* - 2: Old Attachment <STRING>
*
* Return Value:
* None
Expand All @@ -17,11 +20,13 @@
* Public: No
*/

params ["_unit", "", "_actionParams"];
params ["", "_unit", "_actionParams"];
_actionParams params ["_weapon", "_newAttachment", "_oldAttachment"];
TRACE_3("Switching attachment",_weapon,_newAttachment,_oldAttachment);

[_unit, "Gear"] call EFUNC(common,doGesture);
private _currWeaponType = [_unit, _weapon] call EFUNC(common,getWeaponIndex);

if (_currWeaponType == -1) exitWith {};

private _addNew = _newAttachment isNotEqualTo "";
private _removeOld = _oldAttachment isNotEqualTo "";
Expand All @@ -38,22 +43,34 @@ if (_removeOld && {!([_unit, _oldAttachment] call CBA_fnc_canAddItem)}) exitWith
};
};

[_unit, "Gear"] call EFUNC(common,doGesture);

if (_removeOld) then {
[{
params ["_unit", "_weapon", "_oldAttachment"];
switch (_weapon) do {
case (primaryWeapon _unit): {_unit removePrimaryWeaponItem _oldAttachment;};
case (handgunWeapon _unit): {_unit removeHandgunItem _oldAttachment;};
default {_unit removeSecondaryWeaponItem _oldAttachment;};
params ["_unit", "_oldAttachment", "_currWeaponType"];

switch (_currWeaponType) do {
case 0: {_unit removePrimaryWeaponItem _oldAttachment};
case 1: {_unit removeSecondaryWeaponItem _oldAttachment};
case 2: {_unit removeHandgunItem _oldAttachment};
default {};
};

_unit addItem _oldAttachment;
}, [_unit, _weapon, _oldAttachment], 0.3] call CBA_fnc_waitAndExecute;
}, [_unit, _oldAttachment, _currWeaponType], 0.3] call CBA_fnc_waitAndExecute;
};

if (!_addNew) exitWith {};

[{
params ["_unit", "_weapon", "_newAttachment"];
params ["_unit", "_newAttachment", "_weapon"];

// Delete weapon from array, to be able to pass _this to EH
PabstMirror marked this conversation as resolved.
Show resolved Hide resolved
_unit addWeaponItem [_weapon, _newAttachment];

[[getText (configFile >> "CfgWeapons" >> _newAttachment >> "picture"), 4], true] call CBA_fnc_notify;
}, [_unit, _weapon, _newAttachment], 1] call CBA_fnc_waitAndExecute;

if (_unit == ACE_player) then {
playSound "click";
johnb432 marked this conversation as resolved.
Show resolved Hide resolved
};
}, [_unit, _newAttachment, _weapon], 1] call CBA_fnc_waitAndExecute;