From b6532a05cc33dca159d0de4c633ffc37d2c121d1 Mon Sep 17 00:00:00 2001 From: commy2 Date: Sun, 27 Sep 2015 16:47:02 +0200 Subject: [PATCH] new function to select a grenade --- addons/weaponselect/XEH_preInit.sqf | 11 +++ .../functions/fnc_selectNextGrenade.sqf | 76 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 addons/weaponselect/functions/fnc_selectNextGrenade.sqf diff --git a/addons/weaponselect/XEH_preInit.sqf b/addons/weaponselect/XEH_preInit.sqf index 4c5216c2f6..4319a507dd 100644 --- a/addons/weaponselect/XEH_preInit.sqf +++ b/addons/weaponselect/XEH_preInit.sqf @@ -13,6 +13,7 @@ PREP(putWeaponAway); PREP(selectGrenadeAll); PREP(selectGrenadeFrag); PREP(selectGrenadeOther); +PREP(selectNextGrenade); PREP(selectWeaponMode); PREP(selectWeaponMuzzle); PREP(selectWeaponVehicle); @@ -36,6 +37,10 @@ with uiNamespace do { GVAR(NonFragMagazines) = []; GVAR(AllMagazines) = []; + GVAR(GrenadesAll) = []; + GVAR(GrenadesFrag) = [];// + GVAR(GrenadesNonFrag) = [];// + { _magazines = getArray (configFile >> "CfgWeapons" >> "Throw" >> _x >> "magazines"); _magazine = _magazines select 0; @@ -46,13 +51,16 @@ with uiNamespace do { if (_explosive == 0) then { GVAR(NonFragMuzzles) pushBack _x; GVAR(NonFragMagazines) pushBack _magazines; + GVAR(GrenadesNonFrag) append _magazines;// } else { GVAR(FragMuzzles) pushBack _x; GVAR(FragMagazines) pushBack _magazines; + GVAR(GrenadesFrag) append _magazines;// }; GVAR(AllMuzzles) pushBack _x; GVAR(AllMagazines) pushBack _magazines; + GVAR(GrenadesAll) append _magazines;// } forEach getArray (configfile >> "CfgWeapons" >> "Throw" >> "muzzles"); }; @@ -64,5 +72,8 @@ GVAR(AllMuzzles) = uiNamespace getVariable QGVAR(AllMuzzles); GVAR(FragMagazines) = uiNamespace getVariable QGVAR(FragMagazines); GVAR(NonFragMagazines) = uiNamespace getVariable QGVAR(NonFragMagazines); GVAR(AllMagazines) = uiNamespace getVariable QGVAR(AllMagazines); +GVAR(GrenadesAll) = uiNamespace getVariable QGVAR(GrenadesAll);// +GVAR(GrenadesFrag) = uiNamespace getVariable QGVAR(GrenadesFrag);// +GVAR(GrenadesNonFrag) = uiNamespace getVariable QGVAR(GrenadesNonFrag);// ADDON = true; diff --git a/addons/weaponselect/functions/fnc_selectNextGrenade.sqf b/addons/weaponselect/functions/fnc_selectNextGrenade.sqf new file mode 100644 index 0000000000..c0d9e8ccf4 --- /dev/null +++ b/addons/weaponselect/functions/fnc_selectNextGrenade.sqf @@ -0,0 +1,76 @@ +/* + * Author: commy2 + * Select the next grenade. + * + * Arguments: + * 0: Unit + * 1: Grenade type [0: all, 1: frags, 2: non-frags] (default: 0) + * + * Return Value: + * Selecting successful? + * + * Example: + * [player] call ace_weaponselect_fnc_selectNextGrenade + * + * Public: Yes + */ +#include "script_component.hpp" + +params ["_unit", ["_type", 0]]; + +private ["_currentGrenade", "_magazines", "_grenades", "_nextGrenadeIndex", "_nextGrenade", "_uniformGrenades", "_vestGrenades", "_backpackGrenades"]; + +// get currently selected grenade +_currentGrenade = currentThrowable _unit; + +// get correct array format if no grenade is selected +if (_currentGrenade isEqualTo []) then { + _currentGrenade = ["", ""]; +}; + +_currentGrenade = _currentGrenade select 0; + +// get available magazines for that unit +_magazines = magazines _unit; + +_grenades = []; + +{ + if (_x in _magazines) then { + _grenades pushBack _x; + }; + false +} count ([GVAR(GrenadesAll), GVAR(GrenadesFrag), GVAR(GrenadesNonFrag)] select _type); + +// abort if no grenades are available +if (_grenades isEqualTo []) exitWith {false}; + +// get next grenade muzzle +_nextGrenadeIndex = (_grenades find _currentGrenade) + 1; + +// roll over if the last grenade was selected +if (_nextGrenadeIndex >= count _grenades) then { + _nextGrenadeIndex = 0; +}; + +_nextGrenade = _grenades select _nextGrenadeIndex; + +// abort if the same grenade would be selected +if (_currentGrenade == _nextGrenade) exitWith {false}; + +// current best method to select a grenade: remove all grenades, add the one you want to select first and then add the rest +_uniformGrenades = [uniformItems _unit, {_x in GVAR(GrenadesAll) && {_x != _nextGrenade}}] call EFUNC(common,filter); +_vestGrenades = [vestItems _unit, {_x in GVAR(GrenadesAll) && {_x != _nextGrenade}}] call EFUNC(common,filter); +_backpackGrenades = [backpackItems _unit, {_x in GVAR(GrenadesAll) && {_x != _nextGrenade}}] call EFUNC(common,filter); + +// remove all grenades except those we are switching to --> this breaks the selector +{_unit removeItemFromUniform _x; false} count _uniformGrenades; +{_unit removeItemFromVest _x; false} count _vestGrenades; +{_unit removeItemFromBackpack _x; false} count _backpackGrenades; + +// readd grenades +{_unit addItemToUniform _x; false} count _uniformGrenades; +{_unit addItemToVest _x; false} count _vestGrenades; +{_unit addItemToBackpack _x; false} count _backpackGrenades; + +true