Skip to content

Commit

Permalink
Corrected functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnb432 committed Mar 31, 2024
1 parent 64c04b9 commit 06ff2b0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
19 changes: 12 additions & 7 deletions addons/common/fnc_removeMagazine.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
4 changes: 3 additions & 1 deletion addons/common/fnc_removeMagazineCargo.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
14 changes: 9 additions & 5 deletions addons/common/test_inventory.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand Down

0 comments on commit 06ff2b0

Please sign in to comment.