diff --git a/addons/common/fnc_removeMagazine.sqf b/addons/common/fnc_removeMagazine.sqf index 854456761..7bb0db6e2 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 e8562f5d1..4406f62df 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 +};