Skip to content

Commit

Permalink
Merge pull request #1650 from johnb432/remove-magazines
Browse files Browse the repository at this point in the history
Common - Improve remove magazine functions
  • Loading branch information
PabstMirror committed Mar 31, 2024
2 parents 1db1838 + 06ff2b0 commit 332bfbd
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 88 deletions.
93 changes: 36 additions & 57 deletions addons/common/fnc_removeMagazine.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Examples:
(end)
Author:
esteldunedain, johnb43 (from ACE)
---------------------------------------------------------------------------- */
SCRIPT(removeMagazine);
Expand All @@ -47,76 +48,54 @@ if (!isClass _config || {getNumber (_config >> "scope") < 2}) exitWith {
_return
};

if !(configName _config in magazines _unit) exitWith {
TRACE_2("Item not available on Unit",_unit,_item);
_return
};
// Make sure magazine is in config case
_item = configName _config;

if (_ammo < 0) then {
_unit removeMagazineGlobal _item; // removeMagazine fails on remote units
_return = true;
} else {
private _uniformMagazines = [];
private _vestMagazines = [];
private _backpackMagazines = [];
// Ensure proper ammo
_ammo = round _ammo;

private _uniform = uniformContainer _unit;
private _vest = vestContainer _unit;
private _backpack = backpackContainer _unit;
if (_ammo < 0) then {
if (_unit isKindOf "CAManBase") then {
if !(_item in magazines _unit) exitWith {
TRACE_2("Item not available on Unit",_unit,_item);
};

// magazinesAmmoCargo bugged. returns nil for objNull.
if (!isNull _uniform) then {
_uniformMagazines = magazinesAmmoCargo _uniform select {_x select 0 == _item};
};
_unit removeMagazineGlobal _item; // removeMagazine fails on remote units

if (!isNull _vest) then {
_vestMagazines = magazinesAmmoCargo _vest select {_x select 0 == _item};
};
_return = true;
} else {
if !(_item in magazineCargo _unit) exitWith {
TRACE_2("Item not available on Unit",_unit,_item);
};

if (!isNull _backpack) then {
_backpackMagazines = magazinesAmmoCargo _backpack select {_x select 0 == _item};
};
_unit addMagazineCargoGlobal [_item, -1];

{
if (_x select 1 == _ammo) exitWith {
_uniformMagazines deleteAt _forEachIndex;
_return = true;
};
} forEach _uniformMagazines;

if !(_return) then {
{
if (_x select 1 == _ammo) exitWith {
_vestMagazines deleteAt _forEachIndex;
_return = true;
};
} forEach _vestMagazines;
_return = true;
};
} else {
private _magArray = [_item, _ammo];

if !(_return) then {
{
if (_x select 1 == _ammo) exitWith {
_backpackMagazines deleteAt _forEachIndex;
_return = true;
};
} forEach _backpackMagazines;
};
private _fnc_removeMagazine = {
params ["_container"];

if (_return) then {
_unit removeMagazines _item; // doc wrong. works on remote units
if (_magArray in (magazinesAmmoCargo _container)) exitWith {
_container addMagazineAmmoCargo [_item, -1, _ammo];

{
_uniform addMagazineAmmoCargo [_item, 1, _x select 1];
} forEach _uniformMagazines;
true
};

{
_vest addMagazineAmmoCargo [_item, 1, _x select 1];
} forEach _vestMagazines;
false
};

{
_backpack addMagazineAmmoCargo [_item, 1, _x select 1];
} forEach _backpackMagazines;
private _containerArray = if (_unit isKindOf "CAManBase") then {
[uniformContainer _unit, vestContainer _unit, backpackContainer _unit]
} else {
[_unit]
};

{
if (_x call _fnc_removeMagazine) exitWith {_return = true};
} forEach _containerArray;
};

_return
63 changes: 39 additions & 24 deletions addons/common/fnc_removeMagazineCargo.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Examples:
(end)
Author:
silencer.helling3r 2012-12-22, Jonpas
silencer.helling3r 2012-12-22, Jonpas, esteldunedain, johnb43 (from ACE)
---------------------------------------------------------------------------- */
SCRIPT(removeMagazineCargo);

Expand All @@ -38,6 +38,11 @@ if (isNull _container) exitWith {
false
};

if (_container isKindOf "CAManBase") exitWith {
TRACE_2("Container is unit",_container,_item);
false
};

if (_item isEqualTo "") exitWith {
TRACE_2("Item not String or empty",_container,_item);
false
Expand All @@ -55,35 +60,45 @@ if (_count <= 0) exitWith {
false
};

// Make sure magazine is in config case
_item = configName _config;

// Ensure proper count
_count = round _count;
_ammo = round _ammo;

// [[type1, ammo1], [type2, ammo2], ...]
private _magazinesAmmo = magazinesAmmoCargo _container;
if (_ammo < 0) then {
(getMagazineCargo _container) params ["_magazines", "_magazinesCount"];

// Clear cargo space and readd the items as long it's not the type in question
clearMagazineCargoGlobal _container;
private _index = _magazines find _item;

// Engine will agressively cleanup "empty" ground containers, even if magazines are re-added in same frame, so re-create a new container
private _containerType = typeOf _container;
if ((_containerType isKindOf "WeaponHolder")
&& {([configOf _container >> "forceSupply", "NUMBER", 0] call CBA_fnc_getConfigEntry) != 0}
&& {(weaponCargo _container) isEqualTo []}
&& {(itemCargo _container) isEqualTo []}
&& {(backpackCargo _container) isEqualTo []}) then {
_container = createVehicle [_containerType, getPosATL _container, [], 0, "CAN_COLLIDE"];
};
if (_index == -1) exitWith {
false // return
};

_container addMagazineCargoGlobal [_item, -_count];

// Check the amount of mags that were present before removal
(_magazinesCount select _index) >= _count // return
} else {
// [[type1, ammo1], [type2, ammo2], ...]
private _magazinesAmmo = magazinesAmmoCargo _container;
private _index = -1;
private _magArray = [_item, _ammo];

{
_x params ["_magazineClass", "_magazineAmmo"];
while {_count != 0} do {
_index = _magazinesAmmo find _magArray;

if (_count != 0 && {_magazineClass == _item} && {_ammo < 0 || {_magazineAmmo == _ammo}}) then {
// Process removal
_count = _count - 1;
} else {
// Readd
_container addMagazineAmmoCargo [_magazineClass, 1, _magazineAmmo];
if (_index != -1) then {
_container addMagazineAmmoCargo [_item, -1, _ammo];

// Process removal
_magazinesAmmo deleteAt _index;
_count = _count - 1;
} else {
break;
};
};
} forEach _magazinesAmmo;

(_count == 0)
_count == 0 // return
};
86 changes: 79 additions & 7 deletions addons/common/test_inventory.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,41 @@ LOG("Testing " + _funcName);

TEST_DEFINED("CBA_fnc_addMagazine","");

_result = [objNull, "SmokeShell"] call CBA_fnc_addMagazine;
_result = [objNull, "30Rnd_556x45_Stanag"] call CBA_fnc_addMagazine;
TEST_FALSE(_result,_funcName);

_result = [player, ""] call CBA_fnc_addMagazine;
TEST_FALSE(_result,_funcName);

_result = [player, "SmokeShell"] call CBA_fnc_addMagazine;
_result = [player, "30Rnd_556x45_Stanag"] call CBA_fnc_addMagazine;
TEST_TRUE(_result,_funcName);

_result = [player, "30Rnd_556x45_Stanag", 1, true] call CBA_fnc_addMagazine;
TEST_TRUE(_result,_funcName);

_result = [player, "30Rnd_556x45_Stanag", 1, true] call CBA_fnc_addMagazine;
TEST_TRUE(_result,_funcName);

////////////////////////////////////////////////////////////////////////////////////////////////////

_funcName = "CBA_fnc_removeMagazine";
LOG("Testing " + _funcName);

_result = [objNull, "SmokeShell"] call CBA_fnc_removeMagazine;
_result = [objNull, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazine;
TEST_FALSE(_result,_funcName);

_result = [player, ""] call CBA_fnc_removeMagazine;
TEST_FALSE(_result,_funcName);

_result = [player, "SmokeShell"] call CBA_fnc_removeMagazine;
_result = [player, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazine;
TEST_TRUE(_result,_funcName);

_result = [player, "30Rnd_556x45_Stanag", 1] call CBA_fnc_removeMagazine;
TEST_TRUE(_result,_funcName);

player removeMagazines "SmokeShell";
player removeMagazines "30Rnd_556x45_Stanag";

_result = [player, "SmokeShell"] call CBA_fnc_removeMagazine;
_result = [player, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazine;
TEST_FALSE(_result,_funcName);

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -160,12 +169,75 @@ TEST_TRUE(count (itemCargo _container) == 5,_funcName);
clearItemCargoGlobal _container;


_funcName = "CBA_fnc_removeMagazine";
LOG("Testing " + _funcName);

_result = [objNull, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazine;
TEST_FALSE(_result,_funcName);

_result = [_container, ""] call CBA_fnc_removeMagazine;
TEST_FALSE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazine;
TEST_FALSE(_result,_funcName);

_container addMagazineAmmoCargo ["30Rnd_556x45_Stanag", 2, 1];

_result = [_container, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazine;
TEST_TRUE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag", 2] call CBA_fnc_removeMagazine;
TEST_FALSE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag", 1] call CBA_fnc_removeMagazine;
TEST_TRUE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazine;
TEST_FALSE(_result,_funcName);


_funcName = "CBA_fnc_addMagazineCargo";
LOG("Testing " + _funcName);

_result = [objNull, "30Rnd_556x45_Stanag"] call CBA_fnc_addMagazineCargo;
TEST_FALSE(_result,_funcName);

_result = [_container, ""] call CBA_fnc_addMagazineCargo;
TEST_FALSE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag", 5] call CBA_fnc_addMagazineCargo;
TEST_TRUE(_result,_funcName);
TEST_TRUE(count (magazineCargo _container) == 5,_funcName);

_result = [_container, "30Rnd_556x45_Stanag", 10, false, 1] call CBA_fnc_addMagazineCargo;
TEST_TRUE(_result,_funcName);


_funcName = "CBA_fnc_removeMagazineCargo";
LOG("Testing " + _funcName);

_result = [objNull, "SmokeShell"] call CBA_fnc_removeMagazineCargo;
_result = [objNull, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazineCargo;
TEST_FALSE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazineCargo;
TEST_TRUE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag", 2] call CBA_fnc_removeMagazineCargo;
TEST_TRUE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag", 1, 2] call CBA_fnc_removeMagazineCargo;
TEST_FALSE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag", 2, 1] call CBA_fnc_removeMagazineCargo;
TEST_TRUE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag", 10] call CBA_fnc_removeMagazineCargo;
TEST_TRUE(_result,_funcName);

_result = [_container, "30Rnd_556x45_Stanag"] call CBA_fnc_removeMagazineCargo;
TEST_FALSE(_result,_funcName);

// No mags of "30Rnd_556x45_Stanag" left at this point
_container addMagazineCargoGlobal ["30Rnd_556x45_Stanag", 5];
_result = [_container, "30Rnd_556x45_Stanag", 3] call CBA_fnc_removeMagazineCargo;
TEST_TRUE(_result,_funcName);
Expand Down

0 comments on commit 332bfbd

Please sign in to comment.