From 7d4a2b07bbfb63eb20ff5934d6478b2b431d57e3 Mon Sep 17 00:00:00 2001 From: mjc4wilton Date: Fri, 19 Jun 2020 11:35:19 -0400 Subject: [PATCH] Gunbag - Add weapon swapping (#7713) * Gunbag Update adds capability to swap a currently held primary weapon and the weapon current stored in the gunbag. Has a 1.5x time to complete compared to just adding or removing a weapon from the gunbag. * Update stringtable.xml * Update addons/gunbag/functions/fnc_swapWeapon.sqf Update authors field to add credit to the original author of much of the changed code Co-authored-by: Joko * Update addons/gunbag/functions/fnc_swapWeaponCallback.sqf Update the virtual load in a more efficient way. Co-authored-by: Joko * Update addons/gunbag/functions/fnc_swapWeaponCallback.sqf Properly attribute author of majority of original code Co-authored-by: Joko * Update stringtable.xml * Update French translation Co-authored-by: Elgin675 * Remove non-English translations Leave translations open to translators * Add CBA setting to enable weapon switching (Default false) * Fixed variables and updated names for consistancy * Convert from ACE Settings to CBA Settings * Fix stringtable.xml indentation * Update addons/gunbag/initSettings.sqf Co-authored-by: Filip Maciejewski * Update addons/gunbag/initSettings.sqf Co-authored-by: Filip Maciejewski * Update addons/gunbag/functions/fnc_swapGunbagCallback.sqf Co-authored-by: Filip Maciejewski * Update addons/gunbag/functions/fnc_swapGunbag.sqf Co-authored-by: Filip Maciejewski * Update addons/gunbag/functions/fnc_swapGunbagCallback.sqf Co-authored-by: Filip Maciejewski * Update addons/gunbag/initSettings.sqf Co-authored-by: Filip Maciejewski * Update addons/gunbag/initSettings.sqf Co-authored-by: PabstMirror * Update initSettings.sqf Change default value to true * Update CfgVehicles.hpp Co-authored-by: Joko Co-authored-by: Elgin675 Co-authored-by: Filip Maciejewski Co-authored-by: PabstMirror --- addons/gunbag/CfgVehicles.hpp | 14 ++++ addons/gunbag/XEH_PREP.hpp | 2 + addons/gunbag/XEH_preInit.sqf | 2 + addons/gunbag/functions/fnc_canInteract.sqf | 6 +- addons/gunbag/functions/fnc_swapGunbag.sqf | 34 +++++++++ .../functions/fnc_swapGunbagCallback.sqf | 76 +++++++++++++++++++ addons/gunbag/initSettings.sqf | 9 +++ addons/gunbag/stringtable.xml | 25 ++++++ 8 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 addons/gunbag/functions/fnc_swapGunbag.sqf create mode 100644 addons/gunbag/functions/fnc_swapGunbagCallback.sqf create mode 100644 addons/gunbag/initSettings.sqf diff --git a/addons/gunbag/CfgVehicles.hpp b/addons/gunbag/CfgVehicles.hpp index 0addf22710..a5cae40f77 100644 --- a/addons/gunbag/CfgVehicles.hpp +++ b/addons/gunbag/CfgVehicles.hpp @@ -10,6 +10,13 @@ class CfgVehicles { showDisabled = 0; icon = QPATHTOF(ui\gunbag_icon_ca.paa); }; + class GVAR(weaponSwap) { + displayName = CSTRING(SwapGunbag); + condition = QUOTE((GVAR(swapGunbagEnabled)) && ([_target] call FUNC(hasGunbag)) && {[ARR_2(_player,_target)] call FUNC(canInteract) == 2}); + statement = QUOTE([ARR_2(_player,_target)] call FUNC(swapGunbag)); + showDisabled = 0; + icon = QPATHTOF(ui\gunbag_icon_ca.paa); + }; class GVAR(weaponOff) { displayName = CSTRING(OffGunbag); condition = QUOTE(([_target] call FUNC(hasGunbag)) && {[ARR_2(_player,_target)] call FUNC(canInteract) == 1}); @@ -41,6 +48,13 @@ class CfgVehicles { showDisabled = 0; icon = QPATHTOF(ui\gunbag_icon_ca.paa); }; + class GVAR(weaponSwap) { + displayName = CSTRING(SwapGunbag); + condition = QUOTE((GVAR(swapGunbagEnabled)) && ([ARR_2(_player,_player)] call FUNC(canInteract) == 2)); + statement = QUOTE([ARR_2(_player,_player)] call FUNC(swapGunbag)); + showDisabled = 0; + icon = QPATHTOF(ui\gunbag_icon_ca.paa); + }; class GVAR(weaponOff) { displayName = CSTRING(OffGunbag); condition = QUOTE([ARR_2(_player,_player)] call FUNC(canInteract) == 1); diff --git a/addons/gunbag/XEH_PREP.hpp b/addons/gunbag/XEH_PREP.hpp index d76682f986..45d55207d6 100644 --- a/addons/gunbag/XEH_PREP.hpp +++ b/addons/gunbag/XEH_PREP.hpp @@ -1,6 +1,8 @@ PREP(toGunbag); PREP(toGunbagCallback); +PREP(swapGunbag); +PREP(swapGunbagCallback); PREP(offGunbag); PREP(offGunbagCallback); PREP(status); diff --git a/addons/gunbag/XEH_preInit.sqf b/addons/gunbag/XEH_preInit.sqf index c3a98792f6..582d64f7e5 100644 --- a/addons/gunbag/XEH_preInit.sqf +++ b/addons/gunbag/XEH_preInit.sqf @@ -6,6 +6,8 @@ PREP_RECOMPILE_START; #include "XEH_PREP.hpp" PREP_RECOMPILE_END; +#include "initSettings.sqf" + // restore gunbag info after respawn ["CAManBase", "respawn", { [{ diff --git a/addons/gunbag/functions/fnc_canInteract.sqf b/addons/gunbag/functions/fnc_canInteract.sqf index fbf17819f6..04313f6741 100644 --- a/addons/gunbag/functions/fnc_canInteract.sqf +++ b/addons/gunbag/functions/fnc_canInteract.sqf @@ -8,7 +8,7 @@ * 1: Target * * Return Value: - * -1: can't interact 0: empty gunbag 1: full gunbag + * -1: can't interact 0: empty gunbag 1: full gunbag 2: full gunbag & has primary * * Example: * _canInteract = [player, target] call ace_gunbag_fnc_canInteract @@ -29,5 +29,7 @@ if ((_gunbag getVariable [QGVAR(gunbagWeapon), []]) isEqualTo [] && {_weapon != if (!((_gunbag getVariable [QGVAR(gunbagWeapon), []]) isEqualTo []) && {_weapon == ""}) then { _result = 1; }; - +if (!((_gunbag getVariable [QGVAR(gunbagWeapon), []]) isEqualTo []) && {_weapon != ""}) then { + _result = 2; +}; _result diff --git a/addons/gunbag/functions/fnc_swapGunbag.sqf b/addons/gunbag/functions/fnc_swapGunbag.sqf new file mode 100644 index 0000000000..46eb926f38 --- /dev/null +++ b/addons/gunbag/functions/fnc_swapGunbag.sqf @@ -0,0 +1,34 @@ +#include "script_component.hpp" +/* + * Author: Ir0n1E and mjc4wilton + * Swap primary weapon and weapon in gunbag. + * + * Arguments: + * 0: Unit + * 1: Target + * + * Return Value: + * None + * + * Example: + * [player, target] call ace_gunbag_fnc_swapGunbag + * + * Public: No + */ + +params ["_unit", "_target"]; + +private _gunbag = backpackContainer _target; + +_unit call EFUNC(common,goKneeling); + +// play sound +if (["ace_backpacks"] call EFUNC(common,isModLoaded)) then { + [_target, _gunbag] call EFUNC(backpacks,backpackOpened); +}; + +[(PROGRESSBAR_TIME * 1.5), _this, { + (_this select 0) call FUNC(swapGunbagCallback) +}, {}, LLSTRING(swapGunbag), +{(_this select 0) call FUNC(canInteract) == 2} +] call EFUNC(common,progressBar); diff --git a/addons/gunbag/functions/fnc_swapGunbagCallback.sqf b/addons/gunbag/functions/fnc_swapGunbagCallback.sqf new file mode 100644 index 0000000000..8ed4f2cf59 --- /dev/null +++ b/addons/gunbag/functions/fnc_swapGunbagCallback.sqf @@ -0,0 +1,76 @@ +#include "script_component.hpp" +/* + * Author: Ir0n1E and mjc4wilton + * Swap primary weapon and weapon in gunbag. + * + * Arguments: + * 0: Unit + * 1: Target + * + * Return Value: + * None + * + * Example: + * [player, target] call ace_gunbag_fnc_swapGunbag + * + * Public: No + */ + +params ["_unit", "_target"]; +private _currentWeapon = primaryWeapon _unit; //Get Current Weapon +private _gunbag = backpackContainer _target; + + +//---Set up current weapon for storing +private _currentWeaponState = [_unit, _currentWeapon] call EFUNC(common,getWeaponState); //Gets weapon attachments + +/* + * example return value _state + * [["","","optic_Aco",""],["arifle_MX_GL_ACO_F","GL_3GL_F"],["30Rnd_65x39_caseless_mag","1Rnd_HE_Grenade_shell"],[30,1]] + */ + +_currentWeaponState params ["_currentWeaponItems", "", "_currentWeaponMagazines", "_currentWeaponAmmo"]; //Extract Weapon Attachments to separate arrays + +private _currentWeaponMass = [_currentWeapon, _currentWeaponItems, _currentWeaponMagazines] call FUNC(calculateMass); + +{ + _currentWeaponMagazines set [_forEachIndex, [_x, _currentWeaponAmmo select _forEachIndex]]; +} forEach _currentWeaponMagazines; + +//---Set up weapon in gunbag +private _newWeaponState = _gunbag getVariable [QGVAR(gunbagWeapon), []]; + +if (_newWeaponState isEqualTo []) exitWith { + [LLSTRING(empty)] call EFUNC(common,displayTextStructured); +}; + +_newWeaponState params ["_newWeapon", "_newWeaponItems", "_newWeaponMagazines"]; + +//---Swap Weapons +_unit removeWeapon _currentWeapon; +_unit addWeapon _newWeapon; + +// Game will auto add magazines from player's inventory, put these back in player inventory as they will be overwritten +([_unit, _newWeapon] call EFUNC(common,getWeaponState)) params ["", "", "_addedMags", "_addedAmmo"]; +{ + if (((_x select 0) != "") && {(_addedMags select _forEachIndex) != ""}) then { + TRACE_2("Re-adding mag",_x,_addedMags select _forEachIndex); + _unit addMagazine [_addedMags select _forEachIndex, _addedAmmo select _forEachIndex]; + }; +} forEach _newWeaponMagazines; + +removeAllPrimaryWeaponItems _unit; + +{ + _unit addWeaponItem [_newWeapon, _x]; +} forEach (_newWeaponItems + _newWeaponMagazines); + +_unit selectWeapon _newWeapon; + +_newWeaponMagazines = _newWeaponMagazines apply {_x select 0}; + +private _newWeaponMass = [_newWeapon, _newWeaponItems, _newWeaponMagazines] call FUNC(calculateMass); + +// update virtual load +[_target, _gunbag, _currentWeaponMass - _newWeaponMass] call EFUNC(movement,addLoadToUnitContainer); +_gunbag setVariable [QGVAR(gunbagWeapon), [_currentWeapon, _currentWeaponItems, _currentWeaponMagazines], true]; //Replace weapon in gunbag diff --git a/addons/gunbag/initSettings.sqf b/addons/gunbag/initSettings.sqf new file mode 100644 index 0000000000..f48981e2fd --- /dev/null +++ b/addons/gunbag/initSettings.sqf @@ -0,0 +1,9 @@ +// CBA Settings [ADDON: ace_gunbag]: + +[ + QGVAR(swapGunbagEnabled), "CHECKBOX", + [LSTRING(SwapGunbagEnabled_DisplayName), LSTRING(SwapGunbagEnabled_Description)], + ["ACE Uncategorized", LLSTRING(DisplayName_Settings)], + true, // default value + true // isGlobal +] call CBA_fnc_addSetting; diff --git a/addons/gunbag/stringtable.xml b/addons/gunbag/stringtable.xml index 73b21f45e7..232a8506e3 100644 --- a/addons/gunbag/stringtable.xml +++ b/addons/gunbag/stringtable.xml @@ -33,6 +33,22 @@ Silah Çantası (Tan) Funda de arma (Tan) + + ACE Gunbag + ACE Waffentasche + ACE Housse d'arme + ACE Чехол + ACE Pouzdro na zbraň + ACE ガンバッグ + ACE Torba na broń + ACE 총가방 + ACE Borsa per Armi + ACE 枪袋 + ACE 槍袋 + ACE Bolsa de Arma + ACE Silah Çantası + ACE Funda de arma + Put weapon into gunbag Lege Waffe in Waffentasche @@ -49,6 +65,15 @@ Silahını silah çantasına koy Poner el arma en la funda + + Exchange weapon in gunbag + + + Enable Weapon Swap + + + Allows interaction to directly swap the primary weapon and stored weapon. + Get weapon out of gunbag Hole Waffe aus Waffentasche