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

Add keep contents/attachments support and fix non-existing non-preset weapon parents in removeXCargo functions #726

Merged
merged 5 commits into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
119 changes: 79 additions & 40 deletions addons/common/fnc_removeBackpackCargo.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ Description:
Removes specific backpack(s) from cargo space.

Warning: All weapon attachments/magazines in all backpacks in container will become detached.
Warning: Preset weapons without non-preset parents will get their attachments readded (engine limitation).

Parameters:
_container - Object with cargo <OBJECT>
_item - Classname of backpack(s) to remove <STRING>
_count - Number of backpack(s) to remove <NUMBER> (Default: 1)
_container - Object with cargo <OBJECT>
_item - Classname of backpack(s) to remove <STRING>
_count - Number of backpack(s) to remove <NUMBER> (Default: 1)
_keepContents - Keep contents of the removed backpack <BOOLEAN> (Default: false)

Returns:
true on success, false otherwise <BOOLEAN>
Expand All @@ -21,6 +23,9 @@ Examples:

// Remove 2 Carryall Desert Camo backpacks from a box
_success = [myCoolBackpackBox, "B_Carryall_ocamo", 2] call CBA_fnc_removeBackpackCargo;

// Remove 1 Backpack from a box and keep conents
Copy link
Contributor

Choose a reason for hiding this comment

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

"contents"

_success = [myCoolWeaponBox, "B_AssaultPack_khk", 1, true] call CBA_fnc_removeBackpackCargo;
(end)

Author:
Expand All @@ -29,7 +34,7 @@ Author:
#include "script_component.hpp"
SCRIPT(removeBackpackCargo);

params [["_container", objNull, [objNull]], ["_item", "", [""]], ["_count", 1, [0]]];
params [["_container", objNull, [objNull]], ["_item", "", [""]], ["_count", 1, [0]], ["_keepContents", false, [true]]];

if (isNull _container) exitWith {
TRACE_2("Container not Object or null",_container,_item);
Expand Down Expand Up @@ -65,12 +70,81 @@ private _backpackData = [];
// Clear cargo space and readd the items as long it's not the type in question
clearBackpackCargoGlobal _container;


// Add contents to backpack or box helper function
private _fnc_addContents = {
params ["_container", "_itemCargo", "_magazinesAmmoCargo", "_weaponsItemsCargo"];

// Items
{
private _itemCount = (_itemCargo select 1) select _forEachIndex;
_container addItemCargoGlobal [_x, _itemCount];
} forEach (_itemCargo select 0);

// Magazines (and their ammo count)
{
_container addMagazineAmmoCargo [_x select 0, 1, _x select 1];
} forEach _magazinesAmmoCargo;

// Weapons (and their attachments)
// Put attachments next to weapon, no command to put it directly onto a weapon when weapon is in a container
{
_x params ["_weapon", "_muzzle", "_pointer", "_optic", "_magazine", "_magazineGL", "_bipod"];

// weaponsItems magazineGL does not exist if not loaded (not even as empty array)
if (count _x < 7) then {
_bipod = _magazineGL;
_magazineGL = "";
};

// Some weapons don't have non-preset parents
private _weaponNonPreset = [_weapon] call CBA_fnc_getNonPresetClass;
if (_weaponNonPreset == "") then {
_weaponNonPreset = _weapon;
};

_container addWeaponCargoGlobal [_weaponNonPreset, 1];

// If weapon does not have a non-preset parent, only add attachments that were custom added
// Removed attachments cannot be handled (engine limitation) and will be readded due to having to readd preset weapon

private _presetAttachments = [];
if (_weaponNonPreset == _weapon) then {
_presetAttachments = _weapon call CBA_fnc_weaponComponents;
} else {
_magazine params [["_magazineClass", ""], ["_magazineAmmoCount", ""]];
_container addMagazineAmmoCargo [_magazineClass, 1, _magazineAmmoCount];

_magazineGL params [["_magazineGLClass", ""], ["_magazineGLAmmoCount", ""]];
_container addMagazineAmmoCargo [_magazineGLClass, 1, _magazineGLAmmoCount];
};

if !(toLower _muzzle in _presetAttachments) then {
_container addItemCargoGlobal [_muzzle, 1];
};
if !(toLower _pointer in _presetAttachments) then {
_container addItemCargoGlobal [_pointer, 1];
};
if !(toLower _optic in _presetAttachments) then {
_container addItemCargoGlobal [_optic, 1];
};
if !(toLower _bipod in _presetAttachments) then {
_container addItemCargoGlobal [_bipod, 1];
};
} forEach _weaponsItemsCargo;
};

// Process all backpacks
{
_x params ["_backpackClass", "_itemCargo", "_magazinesAmmoCargo", "_weaponsItemsCargo"];

if (_count != 0 && {_backpackClass == _item}) then {
// Process removal
_count = _count - 1;

if (_keepContents) then {
[_container, _itemCargo, _magazinesAmmoCargo, _weaponsItemsCargo] call _fnc_addContents;
};
} else {
// Save all backpacks for finding the one we readd after this
private _addedBackpacks = everyBackpack _container;
Expand All @@ -82,42 +156,7 @@ clearBackpackCargoGlobal _container;
// Find just added backpack and add contents (no command returns reference when adding)
private _backpack = (everyBackpack _container - _addedBackpacks) select 0;

// Items
{
private _itemCount = (_itemCargo select 1) select _forEachIndex;
_backpack addItemCargoGlobal [_x, _itemCount];
} forEach (_itemCargo select 0);

// Magazines (and their ammo count)
{
_backpack addMagazineAmmoCargo [_x select 0, 1, _x select 1];
} forEach _magazinesAmmoCargo;

// Weapons (and their attachments)
// Put attachments next to weapon, no command to put it directly onto a weapon when weapon is in a container
{
_x params ["_weapon", "_muzzle", "_pointer", "_optic", "_magazine", "_magazineGL", "_bipod"];

// weaponsItems magazineGL does not exist if not loaded (not even as empty array)
if (count _x < 7) then {
_bipod = _magazineGL;
_magazineGL = "";
};

private _weapon = [_weapon] call CBA_fnc_getNonPresetClass;
_backpack addWeaponCargoGlobal [_weapon, 1];

_backpack addItemCargoGlobal [_muzzle, 1];
_backpack addItemCargoGlobal [_pointer, 1];
_backpack addItemCargoGlobal [_optic, 1];
_backpack addItemCargoGlobal [_bipod, 1];

_magazine params [["_magazineClass", ""], ["_magazineAmmoCount", ""]];
_backpack addMagazineAmmoCargo [_magazineClass, 1, _magazineAmmoCount];

_magazineGL params [["_magazineGLClass", ""], ["_magazineGLAmmoCount", ""]];
_backpack addMagazineAmmoCargo [_magazineGLClass, 1, _magazineGLAmmoCount];
} forEach _weaponsItemsCargo;
[_backpack, _itemCargo, _magazinesAmmoCargo, _weaponsItemsCargo] call _fnc_addContents;
};
} forEach _backpackData;

Expand Down
120 changes: 80 additions & 40 deletions addons/common/fnc_removeItemCargo.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ Description:
Removes specific item(s) from cargo space.

Warning: All weapon attachments/magazines in containers in container will become detached.
Warning: Preset weapons without non-preset parents will get their attachments readded (engine limitation).

Parameters:
_container - Object with cargo <OBJECT>
_item - Classname of item(s) to remove <STRING>
_count - Number of item(s) to remove <NUMBER> (Default: 1)
_container - Object with cargo <OBJECT>
_item - Classname of item(s) to remove <STRING>
_count - Number of item(s) to remove <NUMBER> (Default: 1)
_keepContents - Keep contents of the removed item (if uniform/vest) <BOOLEAN> (Default: false)

Returns:
true on success, false otherwise <BOOLEAN>
Expand All @@ -21,6 +23,9 @@ Examples:

// Remove 2 Compasses from a box
_success = [myCoolItemBox, "ItemCompass", 2] call CBA_fnc_removeItemCargo;

// Remove 1 Vest from a box and keep conents
_success = [myCoolWeaponBox, "V_PlateCarrier1_rgr", 1, true] call CBA_fnc_removeItemCargo;
(end)

Author:
Expand All @@ -29,7 +34,7 @@ Author:
#include "script_component.hpp"
SCRIPT(removeItemCargo);

params [["_container", objNull, [objNull]], ["_item", "", [""]], ["_count", 1, [0]]];
params [["_container", objNull, [objNull]], ["_item", "", [""]], ["_count", 1, [0]], ["_keepContents", false, [true]]];

if (isNull _container) exitWith {
TRACE_2("Container not Object or null",_container,_item);
Expand Down Expand Up @@ -73,6 +78,71 @@ private _containerNames = [];
// Clear cargo space and readd the items as long it's not the type in question
clearItemCargoGlobal _container;


// Add contents to backpack or box helper function
private _fnc_addContents = {
params ["_container", "_itemCargo", "_magazinesAmmoCargo", "_weaponsItemsCargo"];

// Items
{
private _itemCount = (_itemCargo select 1) select _forEachIndex;
_container addItemCargoGlobal [_x, _itemCount];
} forEach (_itemCargo select 0);

// Magazines (and their ammo count)
{
_container addMagazineAmmoCargo [_x select 0, 1, _x select 1];
} forEach _magazinesAmmoCargo;

// Weapons (and their attachments)
// Put attachments next to weapon, no command to put it directly onto a weapon when weapon is in a container
{
_x params ["_weapon", "_muzzle", "_pointer", "_optic", "_magazine", "_magazineGL", "_bipod"];

// weaponsItems magazineGL does not exist if not loaded (not even as empty array)
if (count _x < 7) then {
_bipod = _magazineGL;
_magazineGL = "";
};

// Some weapons don't have non-preset parents
private _weaponNonPreset = [_weapon] call CBA_fnc_getNonPresetClass;
if (_weaponNonPreset == "") then {
_weaponNonPreset = _weapon;
};

_container addWeaponCargoGlobal [_weaponNonPreset, 1];

// If weapon does not have a non-preset parent, only add attachments that were custom added
// Removed attachments cannot be handled (engine limitation) and will be readded due to having to readd preset weapon

private _presetAttachments = [];
if (_weaponNonPreset == _weapon) then {
_presetAttachments = _weapon call CBA_fnc_weaponComponents;
} else {
_magazine params [["_magazineClass", ""], ["_magazineAmmoCount", ""]];
_container addMagazineAmmoCargo [_magazineClass, 1, _magazineAmmoCount];

_magazineGL params [["_magazineGLClass", ""], ["_magazineGLAmmoCount", ""]];
_container addMagazineAmmoCargo [_magazineGLClass, 1, _magazineGLAmmoCount];
};

if !(toLower _muzzle in _presetAttachments) then {
_container addItemCargoGlobal [_muzzle, 1];
};
if !(toLower _pointer in _presetAttachments) then {
_container addItemCargoGlobal [_pointer, 1];
};
if !(toLower _optic in _presetAttachments) then {
_container addItemCargoGlobal [_optic, 1];
};
if !(toLower _bipod in _presetAttachments) then {
_container addItemCargoGlobal [_bipod, 1];
};
} forEach _weaponsItemsCargo;
};

// Process removal
{
private _itemCount = _allItemsCount select _forEachIndex;
private _containerIndex = _containerNames find _x;
Expand All @@ -86,6 +156,11 @@ clearItemCargoGlobal _container;
_container addItemCargoGlobal [_x, _itemCount];
};
} else {
if (_keepContents) then {
(_containerData select _containerIndex) params ["_itemCargo", "_magazinesAmmoCargo", "_weaponsItemsCargo"];
[_container, _itemCargo, _magazinesAmmoCargo, _weaponsItemsCargo] call _fnc_addContents;
};

_containerData deleteAt _containerIndex;
_containerNames deleteAt _containerIndex;
};
Expand All @@ -107,42 +182,7 @@ clearItemCargoGlobal _container;
// Find just added container and add contents (no command returns reference when adding)
private _addedContainer = ((((everyContainer _container) apply {_x select 1}) - everyBackpack _container) - _addedContainers) select 0;

// Items
{
private _itemCount = (_itemCargo select 1) select _forEachIndex;
_addedContainer addItemCargoGlobal [_x, _itemCount];
} forEach (_itemCargo select 0);

// Magazines (and their ammo count)
{
_addedContainer addMagazineAmmoCargo [_x select 0, 1, _x select 1];
} forEach _magazinesAmmoCargo;

// Weapons (and their attachments)
// Put attachments next to weapon, no command to put it directly onto a weapon when weapon is in a container
{
_x params ["_weapon", "_muzzle", "_pointer", "_optic", "_magazine", "_magazineGL", "_bipod"];

// weaponsItems magazineGL does not exist if not loaded (not even as empty array)
if (count _x < 7) then {
_bipod = _magazineGL;
_magazineGL = "";
};

private _weapon = [_weapon] call CBA_fnc_getNonPresetClass;
_addedContainer addWeaponCargoGlobal [_weapon, 1];

_addedContainer addItemCargoGlobal [_muzzle, 1];
_addedContainer addItemCargoGlobal [_pointer, 1];
_addedContainer addItemCargoGlobal [_optic, 1];
_addedContainer addItemCargoGlobal [_bipod, 1];

_magazine params [["_magazineClass", ""], ["_magazineAmmoCount", ""]];
_addedContainer addMagazineAmmoCargo [_magazineClass, 1, _magazineAmmoCount];

_magazineGL params [["_magazineGLClass", ""], ["_magazineGLAmmoCount", ""]];
_addedContainer addMagazineAmmoCargo [_magazineGLClass, 1, _magazineGLAmmoCount];
} forEach _weaponsItemsCargo;
[_addedContainer, _itemCargo, _magazinesAmmoCargo, _weaponsItemsCargo] call _fnc_addContents;
};
};
} forEach _allItemsType;
Expand Down
Loading