From 4a82c747da92bf2e3fc0e5bfa3d57a1de994683e Mon Sep 17 00:00:00 2001 From: johnb432 <58661205+johnb432@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:14:00 +0100 Subject: [PATCH 1/3] Improve remove magazine functions --- addons/common/fnc_removeMagazine.sqf | 84 ++++++++--------------- addons/common/fnc_removeMagazineCargo.sqf | 61 +++++++++------- 2 files changed, 66 insertions(+), 79 deletions(-) diff --git a/addons/common/fnc_removeMagazine.sqf b/addons/common/fnc_removeMagazine.sqf index 8544567616..7bb0db6e25 100644 --- a/addons/common/fnc_removeMagazine.sqf +++ b/addons/common/fnc_removeMagazine.sqf @@ -22,6 +22,7 @@ Examples: (end) Author: + esteldunedain, johnb43 (from ACE) ---------------------------------------------------------------------------- */ SCRIPT(removeMagazine); @@ -47,76 +48,49 @@ if (!isClass _config || {getNumber (_config >> "scope") < 2}) exitWith { _return }; -if !(configName _config in magazines _unit) exitWith { +// Make sure magazine is in config case +_item = configName _config; + +if !(_item in magazines _unit) exitWith { TRACE_2("Item not available on Unit",_unit,_item); _return }; +// Ensure proper ammo +_ammo = round _ammo; + if (_ammo < 0) then { - _unit removeMagazineGlobal _item; // removeMagazine fails on remote units + if (_unit isKindOf "CAManBase") then { + _unit removeMagazineGlobal _item; // removeMagazine fails on remote units + } else { + _unit addMagazineCargoGlobal [_item, -1]; + }; + _return = true; } else { - private _uniformMagazines = []; - private _vestMagazines = []; - private _backpackMagazines = []; + private _magArray = [_item, _ammo]; - private _uniform = uniformContainer _unit; - private _vest = vestContainer _unit; - private _backpack = backpackContainer _unit; - - // magazinesAmmoCargo bugged. returns nil for objNull. - if (!isNull _uniform) then { - _uniformMagazines = magazinesAmmoCargo _uniform select {_x select 0 == _item}; - }; - - if (!isNull _vest) then { - _vestMagazines = magazinesAmmoCargo _vest select {_x select 0 == _item}; - }; + private _fnc_removeMagazine = { + params ["_container"]; - if (!isNull _backpack) then { - _backpackMagazines = magazinesAmmoCargo _backpack select {_x select 0 == _item}; - }; + if (_magArray in (magazinesAmmoCargo _container)) exitWith { + _container addMagazineAmmoCargo [_item, -1, _ammo]; - { - if (_x select 1 == _ammo) exitWith { - _uniformMagazines deleteAt _forEachIndex; - _return = true; + true }; - } forEach _uniformMagazines; - - if !(_return) then { - { - if (_x select 1 == _ammo) exitWith { - _vestMagazines deleteAt _forEachIndex; - _return = true; - }; - } forEach _vestMagazines; - }; - if !(_return) then { - { - if (_x select 1 == _ammo) exitWith { - _backpackMagazines deleteAt _forEachIndex; - _return = true; - }; - } forEach _backpackMagazines; + false }; - if (_return) then { - _unit removeMagazines _item; // doc wrong. works on remote units - - { - _uniform addMagazineAmmoCargo [_item, 1, _x select 1]; - } forEach _uniformMagazines; - - { - _vest addMagazineAmmoCargo [_item, 1, _x select 1]; - } forEach _vestMagazines; - - { - _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 diff --git a/addons/common/fnc_removeMagazineCargo.sqf b/addons/common/fnc_removeMagazineCargo.sqf index e8562f5d14..4406f62df8 100644 --- a/addons/common/fnc_removeMagazineCargo.sqf +++ b/addons/common/fnc_removeMagazineCargo.sqf @@ -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); @@ -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 @@ -55,35 +60,43 @@ 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 {}; -{ - _x params ["_magazineClass", "_magazineAmmo"]; + _container addMagazineCargoGlobal [_item, -_count]; - if (_count != 0 && {_magazineClass == _item} && {_ammo < 0 || {_magazineAmmo == _ammo}}) then { - // Process removal - _count = _count - 1; - } else { - // Readd - _container addMagazineAmmoCargo [_magazineClass, 1, _magazineAmmo]; + // 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]; + + while {_count != 0} do { + _index = _magazinesAmmo find _magArray; + + 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 +}; From 64c04b9877644cdb072cb230fe633bf9a53d62b1 Mon Sep 17 00:00:00 2001 From: johnb432 <58661205+johnb432@users.noreply.github.com> Date: Sun, 31 Mar 2024 19:20:32 +0200 Subject: [PATCH 2/3] Added missing tests for magazine functions --- addons/common/test_inventory.sqf | 82 +++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/addons/common/test_inventory.sqf b/addons/common/test_inventory.sqf index 4a0326a40a..e4dfa2405d 100644 --- a/addons/common/test_inventory.sqf +++ b/addons/common/test_inventory.sqf @@ -45,13 +45,19 @@ 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); //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -59,18 +65,21 @@ 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); //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -160,10 +169,69 @@ 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", 1000, false, 1] call CBA_fnc_addMagazineCargo; +TEST_FALSE(_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", 1, 1] call CBA_fnc_removeMagazineCargo; +TEST_FALSE(_result,_funcName); + +_result = [_container, "30Rnd_556x45_Stanag", 1000] call CBA_fnc_removeMagazineCargo; TEST_FALSE(_result,_funcName); _container addMagazineCargoGlobal ["30Rnd_556x45_Stanag", 5]; From 06ff2b01edd5e2072503d4e585e148a2ff1ff310 Mon Sep 17 00:00:00 2001 From: johnb432 <58661205+johnb432@users.noreply.github.com> Date: Sun, 31 Mar 2024 19:54:09 +0200 Subject: [PATCH 3/3] Corrected functions and tests --- addons/common/fnc_removeMagazine.sqf | 19 ++++++++++++------- addons/common/fnc_removeMagazineCargo.sqf | 4 +++- addons/common/test_inventory.sqf | 14 +++++++++----- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/addons/common/fnc_removeMagazine.sqf b/addons/common/fnc_removeMagazine.sqf index 7bb0db6e25..7c722977aa 100644 --- a/addons/common/fnc_removeMagazine.sqf +++ b/addons/common/fnc_removeMagazine.sqf @@ -51,22 +51,27 @@ if (!isClass _config || {getNumber (_config >> "scope") < 2}) exitWith { // Make sure magazine is in config case _item = configName _config; -if !(_item in magazines _unit) exitWith { - TRACE_2("Item not available on Unit",_unit,_item); - _return -}; - // Ensure proper ammo _ammo = round _ammo; if (_ammo < 0) then { if (_unit isKindOf "CAManBase") then { + if !(_item in magazines _unit) exitWith { + TRACE_2("Item not available on Unit",_unit,_item); + }; + _unit removeMagazineGlobal _item; // removeMagazine fails on remote units + + _return = true; } else { + if !(_item in magazineCargo _unit) exitWith { + TRACE_2("Item not available on Unit",_unit,_item); + }; + _unit addMagazineCargoGlobal [_item, -1]; - }; - _return = true; + _return = true; + }; } else { private _magArray = [_item, _ammo]; diff --git a/addons/common/fnc_removeMagazineCargo.sqf b/addons/common/fnc_removeMagazineCargo.sqf index 4406f62df8..3fbe2b0ade 100644 --- a/addons/common/fnc_removeMagazineCargo.sqf +++ b/addons/common/fnc_removeMagazineCargo.sqf @@ -72,7 +72,9 @@ if (_ammo < 0) then { private _index = _magazines find _item; - if (_index == -1) exitWith {}; + if (_index == -1) exitWith { + false // return + }; _container addMagazineCargoGlobal [_item, -_count]; diff --git a/addons/common/test_inventory.sqf b/addons/common/test_inventory.sqf index e4dfa2405d..b946896766 100644 --- a/addons/common/test_inventory.sqf +++ b/addons/common/test_inventory.sqf @@ -209,8 +209,8 @@ _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", 1000, false, 1] call CBA_fnc_addMagazineCargo; -TEST_FALSE(_result,_funcName); +_result = [_container, "30Rnd_556x45_Stanag", 10, false, 1] call CBA_fnc_addMagazineCargo; +TEST_TRUE(_result,_funcName); _funcName = "CBA_fnc_removeMagazineCargo"; @@ -228,12 +228,16 @@ TEST_TRUE(_result,_funcName); _result = [_container, "30Rnd_556x45_Stanag", 1, 2] call CBA_fnc_removeMagazineCargo; TEST_FALSE(_result,_funcName); -_result = [_container, "30Rnd_556x45_Stanag", 1, 1] 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", 1000] call CBA_fnc_removeMagazineCargo; +_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);