ACE3/addons/weaponselect/functions/fnc_selectNextGrenade.sqf

76 lines
2.3 KiB
Plaintext
Raw Permalink Normal View History

#include "..\script_component.hpp"
2015-09-27 14:47:02 +00:00
/*
* Author: commy2
* Select the next grenade.
*
* Arguments:
* 0: Unit <OBJECT>
* 1: Grenade type [0: all, 1: frags, 2: non-frags] (default: 0) <NUMBER>
*
* Return Value:
* Selecting successful? <BOOL>
*
* Example:
* [player] call ace_weaponselect_fnc_selectNextGrenade
*
* Public: Yes
*/
params ["_unit", ["_type", 0]];
// get currently selected grenade
2016-01-06 21:42:02 +00:00
private _currentGrenade = currentThrowable _unit;
2015-09-27 14:47:02 +00:00
// get correct array format if no grenade is selected
if (_currentGrenade isEqualTo []) then {
_currentGrenade = ["", ""];
};
_currentGrenade = _currentGrenade select 0;
// get available magazines for that unit
2016-01-06 21:42:02 +00:00
private _magazines = magazines _unit;
2015-09-27 14:47:02 +00:00
2016-01-06 21:42:02 +00:00
private _grenades = [];
2015-09-27 14:47:02 +00:00
{
if (_x in _magazines) then {
_grenades pushBack _x;
};
} forEach ([GVAR(GrenadesAll), GVAR(GrenadesFrag), GVAR(GrenadesNonFrag)] select _type);
2015-09-27 14:47:02 +00:00
// abort if no grenades are available
if (_grenades isEqualTo []) exitWith {false};
// get next grenade muzzle
2016-01-06 21:42:02 +00:00
private _nextGrenadeIndex = (_grenades find _currentGrenade) + 1;
2015-09-27 14:47:02 +00:00
// roll over if the last grenade was selected
if (_nextGrenadeIndex >= count _grenades) then {
_nextGrenadeIndex = 0;
};
2016-01-06 21:42:02 +00:00
private _nextGrenade = _grenades select _nextGrenadeIndex;
2015-09-27 14:47:02 +00:00
// abort if the same grenade would be selected
if (_currentGrenade == _nextGrenade) exitWith {false};
2015-09-27 14:55:43 +00:00
// current best method to select a grenade: remove all grenades except the one you want to select, then add them back
2016-02-06 10:58:31 +00:00
private _uniformGrenades = uniformItems _unit select {_x in GVAR(GrenadesAll) && {_x != _nextGrenade}};
private _vestGrenades = vestItems _unit select {_x in GVAR(GrenadesAll) && {_x != _nextGrenade}};
private _backpackGrenades = backpackItems _unit select {_x in GVAR(GrenadesAll) && {_x != _nextGrenade}};
2015-09-27 14:47:02 +00:00
// remove all grenades except those we are switching to --> this breaks the selector
{_unit removeItemFromUniform _x} forEach _uniformGrenades;
{_unit removeItemFromVest _x} forEach _vestGrenades;
{_unit removeItemFromBackpack _x} forEach _backpackGrenades;
2015-09-27 14:47:02 +00:00
// readd grenades
{_unit addItemToUniform _x} forEach _uniformGrenades;
{_unit addItemToVest _x} forEach _vestGrenades;
{_unit addItemToBackpack _x} forEach _backpackGrenades;
2015-09-27 14:47:02 +00:00
2015-09-27 15:15:59 +00:00
[_nextGrenade, {_x == _nextGrenade} count _magazines] call FUNC(displayGrenadeTypeAndNumber);
2015-09-27 14:47:02 +00:00
true