Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dropWeapon, dropMagazine, add dropItem, weaponComponents #296

Merged
merged 3 commits into from
Mar 19, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class CfgFunctions {
F_FILEPATH(removeWeapon);
F_FILEPATH(removeMagazine);
F_FILEPATH(removeItem);
F_FILEPATH(weaponComponents);
F_FILEPATH(dropWeapon);
F_FILEPATH(dropMagazine);
F_FILEPATH(dropItem);
};

class Cargo {
Expand Down Expand Up @@ -119,8 +123,6 @@ class CfgFunctions {

class Broken {
F_FILEPATH(actionArgument);
F_FILEPATH(dropMagazine);
F_FILEPATH(dropWeapon);
};
};
};
45 changes: 45 additions & 0 deletions addons/common/fnc_dropItem.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_dropItem

Description:
Drops an item.

Function which verifies existence of _item and _unit, returns false in case
of trouble, or when able to remove _item from _unit true in case of success

Parameters:
_unit - the unit that should drop the item <OBJECT>
_item - class name of the item to drop <STRING>

Returns:
true if successful, false otherwise <BOOLEAN>

Examples:
(begin example)
_result = [player, "FirstAidKit"] call CBA_fnc_dropItem
(end)

Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(dropItem);

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

private _return = [_unit, _item, _ammo] call CBA_fnc_removeItem;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_ammo?


if (_return) then {
_unit switchMove "ainvpknlmstpslaywrfldnon_1";

private _weaponHolder = nearestObject [_unit, "WeaponHolder"];

if (isNull _weaponHolder || {_unit distance _weaponHolder > 2}) then {
_weaponHolder = createVehicle ["GroundWeaponHolder", [0,0,0], [], 0, "NONE"];
_weaponHolder setPosASL getPosASL _unit;
};

_weaponHolder addItemCargoGlobal [_item, 1];
};

_return
80 changes: 30 additions & 50 deletions addons/common/fnc_dropMagazine.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,56 @@
Function: CBA_fnc_dropMagazine

Description:
Drop a magazine.
Drops a magazine.

Function which verifies existence of _item and _unit, returns false in case
of trouble, or when able to remove _item from _unit true in case of success.

Parameters:
_unit - the unit that should drop a magazine [Object]
_item - class name of the magazine to drop [String]
_unit - the unit that should drop a magazine <OBJECT>
_item - class name of the magazine to drop <STRING>
_ammo - ammo count <NUMBER>

Returns:
true if successful, false otherwise
true if successful, false otherwise <BOOLEAN>

Examples:
(begin example)
_result = [player, "SmokeShell"] call CBA_fnc_dropMagazine
_result = [player, "SmokeShell"] call CBA_fnc_dropMagazine
(end)

Author:

?, commy2
---------------------------------------------------------------------------- */

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

#define __scriptname fnc_dropMagazine

#define __cfg (configFile >> "CfgMagazines")
#define __action "DROPMAGAZINE"
#define __ar (magazines _unit)
params [["_unit", objNull, [objNull]], ["_item", "", [""]], ["_ammo", -1, [0]]];

private ["_item", "_holder"];
params ["_unit"];
if (typeName _unit != "OBJECT") exitWith {
TRACE_2("Unit not Object",_unit,_item);
false
};
_item = _this select 1;
if (typeName _item != "STRING") exitWith {
TRACE_2("Item not String",_unit,_item);
false
};
if (isNull _unit) exitWith {
TRACE_2("Unit isNull",_unit,_item);
false
};
if (_item == "") exitWith {
TRACE_2("Empty Item",_unit,_item);
false
};
if !(isClass (__cfg >> _item)) exitWith {
TRACE_2("Item not exist in Config",_unit,_item);
false
// random mag mode
if (_ammo < 0) then {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document this behaviour in the function header

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. also changed selectRandom to BIS_fnc_selectRandom for the Linux build.

_ammo = (selectRandom (magazinesAmmoFull _unit select {_x select 0 == _item && {toLower (_x select 4) in ["uniform","vest","backpack"]}})) param [1, "null"];
};
if !(_item in __ar) exitWith {

// no mag of this type in units inventory
if (_ammo isEqualTo "null") exitWith {
TRACE_2("Item not available on Unit",_unit,_item);
false
};
_holder = if (count _this > 2) then {
_this select 2
} else {
_unit
};
if (typeName _holder != "OBJECT") exitWith {
TRACE_3("Holder not object",_unit,_item,_holder);
false
};
if (isNull _holder) exitWith {
TRACE_3("Holder isNull",_unit,_item,_holder);
false

private _return = [_unit, _item, _ammo] call CBA_fnc_removeMagazine;

if (_return) then {
_unit switchMove "ainvpknlmstpslaywrfldnon_1";

private _weaponHolder = nearestObject [_unit, "WeaponHolder"];

if (isNull _weaponHolder || {_unit distance _weaponHolder > 2}) then {
_weaponHolder = createVehicle ["GroundWeaponHolder", [0,0,0], [], 0, "NONE"];
_weaponHolder setPosASL getPosASL _unit;
};

_weaponHolder addMagazineAmmoCargo [_item, 1, _ammo];
};
_unit action [__action, _holder, _item];
TRACE_3("Holder: %3 - Success",_unit,_item,_holder);
true

_return
97 changes: 45 additions & 52 deletions addons/common/fnc_dropWeapon.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,63 @@ Description:
of trouble, or when able to remove _item from _unit true in case of success

Parameters:
_unit - the unit that should drop a magazine [Object]
_item - class name of the weapon to drop [String]
_unit - the unit that should drop a weapon <OBJECT>
_item - class name of the weapon to drop <STRING>

Returns:
true if successful, false otherwise
true if successful, false otherwise <BOOLEAN>

Examples:
(begin example)
_result = [player, primaryWeapon player] call CBA_fnc_dropWeapon
_result = [player, primaryWeapon player] call CBA_fnc_dropWeapon
(end)

Author:

?, commy2
---------------------------------------------------------------------------- */

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

#define __scriptname fDropWeapon
params [["_unit", objNull, [objNull]], ["_item", "", [""]]];

#define __cfg (configFile >> "CfgWeapons")
#define __action "DROPWEAPON"
#define __ar (weapons _unit)
private _weaponInfo = (weaponsItems _unit select {_x select 0 == _item}) param [0, []];
private _return = [_unit, _item] call CBA_fnc_removeWeapon;

private ["_unit", "_item", "_holder"];
params ["_unit"];
if (typeName _unit != "OBJECT") exitWith {
TRACE_2("Unit not Object",_unit,_item);
false
};
_item = _this select 1;
if (typeName _item != "STRING") exitWith {
TRACE_2("Item not String",_unit,_item);
false
};
if (isNull _unit) exitWith {
TRACE_2("Unit isNull",_unit,_item);
false
};
if (_item == "") exitWith {
TRACE_2("Empty Item",_unit,_item);
false
};
if !(isClass (__cfg >> _item)) exitWith {
TRACE_2("Item not exist in Config",_unit,_item);
false
};
if !(_item in __ar) exitWith {
TRACE_2("Item not available on Unit",_unit,_item);
false
};
_holder = if (count _this > 2) then {
_this select 2
} else {
_unit
};
if (typeName _holder != "OBJECT") exitWith {
TRACE_3("Holder: %3 - Holder not object",_unit,_item,_holder);
false
};
if (isNull _holder) exitWith {
TRACE_3("Holder: %3 - Holder isNull",_unit,_item,_holder);
false
if (_return) then {
private _baseWeapon = _item call CBA_fnc_weaponComponents param [0, _item];

private _items = _weaponInfo;
_items deleteAt 0; // delete the weapon

private _magazines = [];

{
if (_x isEqualType []) then {
_magazines pushBack _x;
_items set [_forEachIndex, ""];
};
} forEach _items;

_items = _items - [""];

_unit switchMove "ainvpknlmstpslaywrfldnon_1";

private _weaponHolder = nearestObject [_unit, "WeaponHolder"];

if (isNull _weaponHolder || {_unit distance _weaponHolder > 2}) then {
_weaponHolder = createVehicle ["GroundWeaponHolder", [0,0,0], [], 0, "NONE"];
_weaponHolder setPosASL getPosASL _unit;
};

_weaponHolder addWeaponCargoGlobal [_baseWeapon, 1];

{
_weaponHolder addItemCargoGlobal [_x, 1];
} forEach _items;

{
_weaponHolder addMagazineAmmoCargo [_x select 0, 1, _x select 1];
} forEach _magazines;
};

_unit action [__action, _holder, _item];
TRACE_3("Holder: %3 - Success",_unit,_item,_holder);
true
_return
64 changes: 64 additions & 0 deletions addons/common/fnc_weaponComponents.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_weaponComponents

Description:
Reports class name of base weapon without attachments and all attachments belonging to a pre equipped weapon.
Base weapon and attachments are reported in lower case capitalization.
Fixed version of BIS_fnc_weaponComponents.

Parameters:
_weapon - a weapons class name with attachments build in <STRING>

Returns:
_components - class names of base weapon + attachments. <ARRAY>
attachments are in random order, but weapon is always at first position
empty array if weapon does not exist in config

Examples:
(begin example)
_components = (primaryWeapon player) call CBA_fnc_weaponComponents;
(end)

Author:
commy2, based on BIS_fnc_weaponComponents by Jiri Wainar
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(weaponComponents);

params [["_weapon", "", [""]]];

if (isNil QGVAR(weaponComponentsCache)) then {
GVAR(weaponComponentsCache) = [] call CBA_fnc_createNamespace;
};

private _components = GVAR(weaponComponentsCache) getVariable _weapon;

if (isNil "_components") then {
private _config = configfile >> "CfgWeapons" >> _weapon;

// return empty array if the weapon doesn't exist
if (!isClass _config) exitWith {[]};

// get attachments
private _attachments = [];

{
_attachments pushBack toLower getText (_x >> "item");
} forEach ("true" configClasses (_config >> "LinkedItems")); // inheritance is apparently disabled for these

// get first parent without attachments
while {isClass _config && {getNumber (_config >> "scope") == 2}} do {
if (count (_config >> "LinkedItems") == 0) exitWith {
_weapon = configName _config;
};

_config = inheritsFrom _config;
};

_components = [toLower _weapon];
_components append _attachments;

GVAR(weaponComponentsCache) setVariable [_weapon, _components];
};

+ _components
2 changes: 1 addition & 1 deletion addons/common/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define DEBUG_MODE_FULL
#include "script_component.hpp"

#define TESTS ["config", "inventory"]
#define TESTS ["config", "inventory", "weaponComponents"]

SCRIPT(test-common);

Expand Down
Loading