Skip to content

Commit

Permalink
Merge pull request #296 from commy2/fixdropfunctions
Browse files Browse the repository at this point in the history
fix dropWeapon, dropMagazine, add dropItem, weaponComponents
  • Loading branch information
Killswitch00 committed Mar 19, 2016
2 parents 0018474 + b845711 commit 21dd81e
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 105 deletions.
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] call CBA_fnc_removeItem;

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 (optional). If not specified a random magazine is chosen <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 {
_ammo = ((magazinesAmmoFull _unit select {_x select 0 == _item && {toLower (_x select 4) in ["uniform","vest","backpack"]}}) call BIS_fnc_selectRandom) 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

0 comments on commit 21dd81e

Please sign in to comment.