Skip to content

Commit

Permalink
fix and expand CBA_fnc_selectWeapon
Browse files Browse the repository at this point in the history
  • Loading branch information
commy2 committed Dec 14, 2015
1 parent e23bea7 commit 271426d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 18 deletions.
14 changes: 13 additions & 1 deletion addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class CfgFunctions
description = "Add weapon(s) to vehicle cargo. MP synchronized.";
file = "\x\cba\addons\common\fnc_addWeaponCargoGlobal.sqf";
};
// CBA_fnc_canUseWeapon
class canUseWeapon
{
description = "Checks if the unit can currently use a weapon.";
file = "\x\cba\addons\common\fnc_canUseWeapon.sqf";
};
// CBA_fnc_createCenter
class createCenter
{
Expand Down Expand Up @@ -279,6 +285,12 @@ class CfgFunctions
description = "A function used to find out if the group or object is alive.";
file = "\x\cba\addons\common\fnc_isAlive.sqf";
};
// CBA_fnc_isPerson
class isPerson
{
description = "Checks if an object is a person - soldier or civilian.";
file = "\x\cba\addons\common\fnc_isPerson.sqf";
};
// CBA_fnc_isTurnedOut
class isTurnedOut
{
Expand Down Expand Up @@ -450,7 +462,7 @@ class CfgFunctions
// CBA_fnc_selectWeapon
class selectWeapon
{
description = "Selects weapon, if the player has the weapon, including correctly selecting a muzzle, if any.";
description = "Selects a weapon including correctly selecting a weapon mode of specified.";

This comment has been minimized.

Copy link
@Killswitch00

Killswitch00 Dec 18, 2015

Contributor

"a weapon mode of specified." ?

file = "\x\cba\addons\common\fnc_selectWeapon.sqf";
};
// CBA_fnc_setHeight
Expand Down
30 changes: 30 additions & 0 deletions addons/common/fnc_canUseWeapon.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_canUseWeapon
Description:
Checks if the unit can currently use a weapon.
Parameters:
_unit - A unit <OBJECT>
Returns:
True if the unit is not in a vehicle or in a FFV position <BOOLEAN>
Examples:
(begin example)
_result = player call CBA_fnc_canUseWeapon;
(end)
Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(canUseWeapon);

params [["_unit", objNull, [objNull]]];

if (_unit == vehicle _unit) exitWith {true};

private _config = configFile >> "CfgMovesMaleSdr" >> "States" >> animationState _unit;

isClass _config && {getNumber (_config >> "canPullTrigger") == 1}
42 changes: 42 additions & 0 deletions addons/common/fnc_isPerson.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_isPerson
Description:
Checks if an object is a person - soldier or civilian.
Parameters:
_unit - A unit or classname <OBJECT, STRING>
Returns:
True if object is a person, otherwise false <BOOLEAN>
Examples:
(begin example)
player call CBA_fnc_isPerson;
-> true
"CAManBase" call CBA_fnc_isPerson;
-> true
(vehicle player) call CBA_fnc_isPerson;
-> false
_projectile call CBA_fnc_isPerson;
-> false
(end)
Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(isPerson);

params [["_unit", "", [objNull, ""]]];

if (_unit isEqualType objNull) then {
_unit = typeOf _unit;
};

private _config = configFile >> "CfgVehicles" >> _unit;

This comment has been minimized.

Copy link
@kymckay

kymckay Dec 18, 2015

Contributor

Wouldn't this cause errors if line 36 was false

This comment has been minimized.

Copy link
@jokoho48

jokoho48 Dec 18, 2015

Member

no its get converted to a typeOf _unit in L36

This comment has been minimized.

Copy link
@kymckay

kymckay Dec 18, 2015

Contributor

Yes, but only when L36 is true 😄

This comment has been minimized.

Copy link
@kymckay

kymckay Dec 18, 2015

Contributor

Ah wait, params default value to the rescue! Ignore me, I'm hungover 😄


isClass _config && {getNumber (_config >> "isMan") == 1}
47 changes: 30 additions & 17 deletions addons/common/fnc_selectWeapon.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,50 @@
Function: CBA_fnc_selectWeapon
Description:
Selects weapon, if the player has the weapon, including correctly selecting a muzzle, if any.
Selects a weapon including correctly selecting a weapon mode of specified.
Has to be executed on the machine where the unit is local.
Parameters:
_unit - Unit object to perform function on [Object]
_weap - Weapon to select [String]
_unit - Unit object to perform function on. <OBJECT>
_weapon - Weapon or muzzle to select <STRING>
_mode - Weapon mode to switch to [optional] <STRING> (default: "")
Returns:
Success or Failed [Boolean]
Success or Failed <BOOLEAN>
Examples:
(begin example)
_result = [player, secondaryWeapon player] call CBA_fnc_selectWeapon
_result = [player, secondaryWeapon player] call CBA_fnc_selectWeapon;
_result = [player, currentWeapon player, "FullAuto"] call CBA_fnc_selectWeapon;
_result = [player, "LMG_M200_body"] call CBA_fnc_selectWeapon;
(end)
Author:
Sickboy
Sickboy, commy2
---------------------------------------------------------------------------- */

#include "script_component.hpp"
SCRIPT(selectWeapon);

private ["_cfg", "_muz", "_ar"];
params ["_unit","_weap"];
_cfg = (configFile >> "CfgWeapons" >> _weap >> "muzzles");
if (isArray _cfg) then {
_ar = getArray _cfg;
_muz = _ar select 0;
if (_muz == "this") then { _muz = _weap };
#define MAXIMUM_TRIES 100

params [["_unit", objNull, [objNull]], ["_weapon", "", [""]], ["_mode", "", [""]]];

if (!local _unit) exitWith {false};

private _vehicle = [vehicle _unit, _unit] select (_unit call CBA_fnc_canUseWeapon);

private _index = 0;

if (_mode isEqualTo "") then {
while {_index < MAXIMUM_TRIES && {currentMuzzle _unit != _weapon}} do {
_unit action ["SwitchWeapon", _vehicle, _unit, _index];
_index = _index + 1;
};
} else {
_muz = _weap;
while {_index < MAXIMUM_TRIES && {currentMuzzle _unit != _weapon || {currentWeaponMode _unit != _mode}}} do {
_unit action ["SwitchWeapon", _vehicle, _unit, _index];
_index = _index + 1;
};
};

if (vehicle player != player) exitWith { false };
if (player hasWeapon _weap) then { _unit selectWeapon _muz; true } else { false };
_index < MAXIMUM_TRIES // return

0 comments on commit 271426d

Please sign in to comment.