Skip to content

Commit

Permalink
Fix CBA_fnc_removeItemCargo removing all containers instead of specif…
Browse files Browse the repository at this point in the history
…ied quantity (#1168)

* Fix CBA_fnc_removeBackpackCargo unit test

* Fix CBA_fnc_removeItemCargo removing all containers instead of specified quantity - fix #1153
  • Loading branch information
jonpas authored Jun 29, 2019
1 parent f941810 commit 07a2e04
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
64 changes: 36 additions & 28 deletions addons/common/fnc_removeItemCargo.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,31 @@ if (_count <= 0) exitWith {
_count = round _count;

// Save containers and contents
private _containerData = []; // [object1, object2, ...]
private _containerNames = [];
private _containerData = [];
{
_x params ["_class", "_object"];
if !(_object in (everyBackpack _container)) then {
_containerData pushBack [getItemCargo _object, magazinesAmmoCargo _object, weaponsItemsCargo _object];
_containerNames pushBack _class;
_containerData pushBack [_class, getItemCargo _object, magazinesAmmoCargo _object, weaponsItemsCargo _object];
};
} forEach (everyContainer _container); // [["class1", object1], ["class2", object2]]

// [[type1, typeN, ...], [count1, countN, ...]]
(getItemCargo _container) params ["_allItemsType", "_allItemsCount"];
// Save non-container items
(getItemCargo _container) params ["_allItemsType", "_allItemsCount"]; // [[type1, typeN, ...], [count1, countN, ...]]
{
private _class = _x;
private _count = _allItemsCount select _forEachIndex;

private _sameData = _containerData select {_x select 0 == _class};
if (_sameData isEqualTo []) then {
_containerData pushBack [_class, _count];
};
} forEach _allItemsType;

// Clear cargo space and readd the items as long it's not the type in question
clearItemCargoGlobal _container;

TRACE_1("Old cargo",_containerData);


// Add contents to backpack or box helper function
private _fnc_addContents = {
Expand Down Expand Up @@ -146,47 +155,46 @@ private _fnc_addContents = {

// Process removal
{
private _itemCount = _allItemsCount select _forEachIndex;
private _containerIndex = _containerNames find _x;
_x params ["_itemClass", "_itemCargoOrCount", "_magazinesAmmoCargo", "_weaponsItemsCargo"];

if (_count != 0 && {_x == _item}) then {
if (_count != 0 && {_itemClass == _item}) then {
// Process removal
if (_containerIndex < 0) then {
_itemCount = _itemCount - _count;
if (_itemCount > 0) then {
// Add with new count
_container addItemCargoGlobal [_x, _itemCount];
};
if (count _x < 4) then {
// Non-container item
// Add with new count
_container addItemCargoGlobal [_itemClass, _itemCargoOrCount - _count]; // Silently fails on 'count < 1'
TRACE_2("Readding",_itemClass,_itemCargoOrCount - _count);

_count = 0;
} else {
// Container item
_count = _count - 1;

if (_keepContents) then {
(_containerData select _containerIndex) params ["_itemCargo", "_magazinesAmmoCargo", "_weaponsItemsCargo"];
[_container, _itemCargo, _magazinesAmmoCargo, _weaponsItemsCargo] call _fnc_addContents;
[_container, _itemCargoOrCount, _magazinesAmmoCargo, _weaponsItemsCargo] call _fnc_addContents;
};

_containerData deleteAt _containerIndex;
_containerNames deleteAt _containerIndex;
};
_count = 0;
} else {
// Readd only
if (_containerIndex < 0) then {
_container addItemCargoGlobal [_x, _itemCount];
if (count _x < 4) then {
// Non-container item
_container addItemCargoGlobal [_itemClass, _itemCargoOrCount];
TRACE_2("Readding",_itemClass,_itemCargoOrCount);
} else {
(_containerData select _containerIndex) params ["_itemCargo", "_magazinesAmmoCargo", "_weaponsItemsCargo"];

// Container item
// Save all containers for finding the one we readd after this
private _addedContainers = ((everyContainer _container) apply {_x select 1}) - everyBackpack _container;

// Readd
private _addedContainer = [_containerNames select _containerIndex] call CBA_fnc_getNonPresetClass;
private _addedContainer = [_itemClass] call CBA_fnc_getNonPresetClass;
_container addItemCargoGlobal [_addedContainer, 1];

// 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;

[_addedContainer, _itemCargo, _magazinesAmmoCargo, _weaponsItemsCargo] call _fnc_addContents;
[_addedContainer, _itemCargoOrCount, _magazinesAmmoCargo, _weaponsItemsCargo] call _fnc_addContents;
};
};
} forEach _allItemsType;
} forEach _containerData;

(_count == 0)
7 changes: 6 additions & 1 deletion addons/common/test_inventory.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ clearBackpackCargoGlobal _container;
_container addBackpackCargoGlobal ["B_AssaultPack_mcamo_Ammo", 1];
_result = [_container, "B_AssaultPack_mcamo_Ammo", 1, true] call CBA_fnc_removeBackpackCargo;
TEST_TRUE(_result,_funcName);
TEST_TRUE(count (backpackCargo _container) == 0 && count (itemCargo _container) == 4 && count (magazineCargo _container) == 19,_funcName);
TEST_TRUE(count (backpackCargo _container) == 0 && count (itemCargo _container) == 4 && count (magazineCargo _container) == 20,_funcName);
clearBackpackCargoGlobal _container;
clearItemCargoGlobal _container;
clearMagazineCargoGlobal _container;
Expand Down Expand Up @@ -141,6 +141,11 @@ TEST_TRUE(_result,_funcName);
TEST_TRUE(count (itemCargo _container) == 2,_funcName);
clearItemCargoGlobal _container;

_container addItemCargoGlobal ["V_PlateCarrier1_rgr", 5];
_result = [_container, "V_PlateCarrier1_rgr", 1] call CBA_fnc_removeItemCargo;
TEST_TRUE(count (itemCargo _container) == 4,_funcName);
clearItemCargoGlobal _container;

_container addItemCargoGlobal ["V_PlateCarrier1_rgr", 1];
(((everyContainer _container) select 0) select 1) addItemCargoGlobal ["FirstAidKit", 5];
_container addItemCargoGlobal ["FirstAidKit", 5];
Expand Down

0 comments on commit 07a2e04

Please sign in to comment.