From 86f08af250899d24ba2dda6ddaef7364880936ab Mon Sep 17 00:00:00 2001 From: Drofseh Date: Wed, 18 May 2022 11:07:47 -0700 Subject: [PATCH] Common - Add keybind and function to unload unit's weapon/muzzle (#8735) * add keybind and function to unload unit's weapon/muzzle - add keybind and function to unload unit's weapon/muzzle - requires https://github.com/CBATeam/CBA_A3/pull/1527 * change function name * rename the file too * Create common-framework.md * Set REQUIRED_CBA_VERSION to 3.15.7 * Update required CBA version Co-authored-by: PabstMirror * Apply suggestions from code review Co-authored-by: PabstMirror --- addons/common/XEH_PREP.hpp | 1 + addons/common/XEH_postInit.sqf | 17 ++++ .../common/functions/fnc_unloadUnitWeapon.sqf | 87 +++++++++++++++++++ addons/common/stringtable.xml | 3 + addons/main/script_mod.hpp | 2 +- docs/wiki/framework/common-framework.md | 26 ++++++ 6 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 addons/common/functions/fnc_unloadUnitWeapon.sqf create mode 100644 docs/wiki/framework/common-framework.md diff --git a/addons/common/XEH_PREP.hpp b/addons/common/XEH_PREP.hpp index f09b9353003..ab9756c95e8 100644 --- a/addons/common/XEH_PREP.hpp +++ b/addons/common/XEH_PREP.hpp @@ -185,6 +185,7 @@ PREP(uniqueElements); PREP(uniqueItems); PREP(unloadPerson); PREP(unloadPersonLocal); +PREP(unloadUnitWeapon); PREP(unmuteUnit); PREP(useItem); PREP(useMagazine); diff --git a/addons/common/XEH_postInit.sqf b/addons/common/XEH_postInit.sqf index 92459a93f1b..c21c9a0e4ff 100644 --- a/addons/common/XEH_postInit.sqf +++ b/addons/common/XEH_postInit.sqf @@ -539,4 +539,21 @@ GVAR(deviceKeyCurrentIndex) = -1; {false}, [0xC7, [true, false, false]], false] call CBA_fnc_addKeybind; //SHIFT + Home Key + +["ACE3 Weapons", QGVAR(unloadWeapon), localize LSTRING(unloadWeapon), { + // Conditions: + if !([ACE_player, objNull, ["isNotInside"]] call FUNC(canInteractWith)) exitWith {false}; + + private _currentWeapon = currentWeapon ACE_player; + if !(_currentWeapon != primaryWeapon _unit && {_currentWeapon != handgunWeapon _unit} && {_currentWeapon != secondaryWeapon _unit}) exitWith {false}; + + private _currentMuzzle = currentMuzzle ACE_player; + private _currentAmmoCount = ACE_player ammo _currentMuzzle; + if (_currentAmmoCount < 1) exitWith {false}; + + // Statement: + [ACE_player, _currentWeapon, _currentMuzzle, _currentAmmoCount, false] call FUNC(unloadUnitWeapon); + true +}, {false}, [19, [false, false, true]], false] call CBA_fnc_addKeybind; //ALT + R Key + GVAR(commonPostInited) = true; diff --git a/addons/common/functions/fnc_unloadUnitWeapon.sqf b/addons/common/functions/fnc_unloadUnitWeapon.sqf new file mode 100644 index 00000000000..6998370e363 --- /dev/null +++ b/addons/common/functions/fnc_unloadUnitWeapon.sqf @@ -0,0 +1,87 @@ +#include "script_component.hpp" +/* + * Author: drofseh & Commy2 + * Unload the magazine from the unit's weapon and attempt to put it in a sensible place. + * + * Arguments: + * 0: Player + * 1: Weapon + * 2: Muzzle (optional, default: Weapon) + * 3: Ammo count (optional, default: ammo currentMuzzle Player) + * 4: Skip animation? (optional, default: false) + * + * Return Value: + * None + * + * Example: +* [ACE_player, currentWeapon ACE_player, currentMuzzle ACE_player, 23, false] call ace_common_fnc_unloadUnitWeapon + * + * Public: No + */ + +params ["_unit", "_weapon", ["_muzzle", _weapon], ["_ammoCount", _unit ammo _muzzle ], ["_skipAnim", false]]; +TRACE_5("params",_unit,_weapon,_muzzle,_ammoCount,_skipAnim); + +// audiovisual effects +private _delay = 0; +if !(_skipAnim) then { + _delay = 1.5; + private _config = configFile >> "CfgWeapons" >> _weapon; + if (_weapon != _muzzle) then { + _config = _config >> _muzzle; + }; + + // get and play animation + private _unloadAction = getText (_config >> "ACE_unloadAction"); + + if (_unloadAction == "") then { + _unloadAction = getText (_config >> "reloadAction"); + }; + + [_unit, _unloadAction, 1] call FUNC(doGesture); + + // get and play sound + private _unloadSound = getText (_config >> "ACE_unloadSound"); + + if (_unloadSound == "") then { + _unloadSound = "A3\Sounds_F\arsenal\weapons\Rifles\Katiba\reload_Katiba.wss"; + private _unloadSoundArray = getArray (_config >> "reloadMagazineSound"); + + // file extention is required for playSound3D + if (_unloadSoundArray isNotEqualTo []) then { + private _wssTest = format ["%1.wss", _unloadSoundArray select 0]; + if (fileExists _wssTest) then { + _unloadSound = _wssTest; + } else { + private _wavTest = format ["%1.wav", _unloadSoundArray select 0]; + if (fileExists _wavTest) then { + _unloadSound = _wavTest; + }; + }; + }; + }; + + playSound3D [_unloadSound, _unit]; +}; + +// remove magazine from weapon and add it to inventory +[{ + params ["_unit", "_weapon", "_ammoCount"]; + + // remove weapon item + private _magazineClass = currentMagazine _unit; + + switch true do { + case (_weapon == primaryWeapon _unit): { + _unit removePrimaryWeaponItem _magazineClass; + }; + case (_weapon == handgunWeapon _unit): { + _unit removeHandgunItem _magazineClass; + }; + case (_weapon == secondaryWeapon _unit): { + _unit removeSecondaryWeaponItem _magazineClass; + }; + }; + + [_unit, _magazineClass, _ammoCount, true] call CBA_fnc_addMagazine; +}, [_unit, _weapon, _ammoCount], _delay] call CBA_fnc_waitAndExecute; diff --git a/addons/common/stringtable.xml b/addons/common/stringtable.xml index c400587d1a6..26c2d694061 100644 --- a/addons/common/stringtable.xml +++ b/addons/common/stringtable.xml @@ -1635,5 +1635,8 @@ 受所在位置影響提升醫療能力 Konumlar Tedaviyi Hızlandırır + + Unload Weapon + diff --git a/addons/main/script_mod.hpp b/addons/main/script_mod.hpp index 8e598ffeef4..8f1fcd1993c 100644 --- a/addons/main/script_mod.hpp +++ b/addons/main/script_mod.hpp @@ -11,7 +11,7 @@ // MINIMAL required version for the Mod. Components can specify others.. #define REQUIRED_VERSION 2.06 -#define REQUIRED_CBA_VERSION {3,15,6} +#define REQUIRED_CBA_VERSION {3,15,7} #ifdef COMPONENT_BEAUTIFIED #define COMPONENT_NAME QUOTE(ACE3 - COMPONENT_BEAUTIFIED) diff --git a/docs/wiki/framework/common-framework.md b/docs/wiki/framework/common-framework.md new file mode 100644 index 00000000000..464593eb05d --- /dev/null +++ b/docs/wiki/framework/common-framework.md @@ -0,0 +1,26 @@ +--- +layout: wiki +title: Common Framework +description: Notes on ACE3 Common. +group: framework +order: 5 +parent: wiki +mod: ace +version: + major: 3 + minor: 14 + patch: 2 +--- + +## 1. Config Values + +### 1.1 `CfgWeapons` + +```cpp +class CfgWeapons { + class yourWeaponClass { + ACE_unloadAction = "GestureUnloadMyWeaponClass"; // Animation to play when weapon is unloaded with ace_common_fnc_unloadUnitWeapon + ACE_unloadSound = "A3\Sounds_F\arsenal\weapons\Rifles\Katiba\reload_Katiba.wss"; // Sound to play when weapon is unloaded with ace_common_fnc_unloadUnitWeapon + }; +}; +```