diff --git a/addons/csw/functions/fnc_reload_handleAddTurretMag.sqf b/addons/csw/functions/fnc_reload_handleAddTurretMag.sqf index d7af22d319..c752f76f59 100644 --- a/addons/csw/functions/fnc_reload_handleAddTurretMag.sqf +++ b/addons/csw/functions/fnc_reload_handleAddTurretMag.sqf @@ -10,7 +10,7 @@ * 2: Source of magazine * 3: Vehicle Magazine * 4: Ammo in magazine - * 5: Unit or object to return ammo to + * 5: Unit or object to return ammo to (default: Source of magazine) * * Return Value: * None @@ -21,7 +21,8 @@ * Public: No */ -params ["_vehicle", "_turret", "_magSource", "_carryMag", "_ammoReceived", ["_returnTo", _magSource]]; +params ["_vehicle", "_turret", "_magSource", "_carryMag", "_ammoReceived"]; +private _returnTo = param [5, _magSource]; TRACE_6("reload_handleAddTurretMag",_vehicle,_turret,_magSource,_carryMag,_ammoReceived,_returnTo); TRACE_2("",local _vehicle,_vehicle turretLocal _turret); diff --git a/addons/csw/functions/fnc_reload_handleReturnAmmo.sqf b/addons/csw/functions/fnc_reload_handleReturnAmmo.sqf index ca445400b0..864bb2ab6f 100644 --- a/addons/csw/functions/fnc_reload_handleReturnAmmo.sqf +++ b/addons/csw/functions/fnc_reload_handleReturnAmmo.sqf @@ -18,13 +18,15 @@ */ params ["_unloadTo", "_carryMag", "_ammo"]; -TRACE_3("reload_handleReturnAmmo",_unloadTo,_carryMag,_ammo); +TRACE_4("reload_handleReturnAmmo",_unloadTo,typeOf _unloadTo,_carryMag,_ammo); private _carryMaxAmmo = getNumber (configFile >> "CfgMagazines" >> _carryMag >> "count"); private _fullMagazines = floor (_ammo / _carryMaxAmmo); private _bulletsRemaining = _ammo % _carryMaxAmmo; -if (_unloadTo isKindOf "CaManBase") then { +private _unloadToUnit = _unloadTo isKindOf "CAManBase"; + +if (_unloadToUnit) then { while {(_fullMagazines > 0) && {[_unloadTo, _carryMag] call CBA_fnc_canAddItem}} do { _unloadTo addMagazine [_carryMag, _carryMaxAmmo]; _fullMagazines = _fullMagazines - 1; @@ -37,19 +39,21 @@ if (_unloadTo isKindOf "CaManBase") then { if ((_fullMagazines == 0) && {_bulletsRemaining == 0}) exitWith {}; -// Try to use existing container -private _container = _unloadTo getVariable [QGVAR(container), objNull]; -if ((_container distance _unloadTo) > 10) then { _container = objNull; }; -if (isNull _container) then { - _container = (nearestObjects [_unloadTo, [["GroundWeaponHolder"], [QGVAR(ammo_holder)]] select GVAR(handleExtraMagazinesType), 10]) param [0, objNull]; +// Try to use object inventory or existing container +private _container = [_unloadTo, objNull] select _unloadToUnit; +if ((maxLoad _container) isEqualTo 0) then { + _container = _unloadTo getVariable [QGVAR(container), objNull]; + if ((_container distance _unloadTo) > 10) then { _container = objNull; }; + if (isNull _container) then { + _container = (nearestObjects [_unloadTo, [["GroundWeaponHolder"], [QGVAR(ammo_holder)]] select GVAR(handleExtraMagazinesType), 10]) param [0, objNull]; + }; }; - if (isNull _container) then { // Create ammo storage container private _weaponRelPos = _unloadTo getRelPos RELATIVE_DIRECTION(270); _weaponRelPos set [2, ((getPosATL _unloadTo) select 2) + 0.05]; - _container = createVehicle [["GroundWeaponHolder", QGVAR(ammo_holder)] select GVAR(handleExtraMagazinesType), [0, 0, 0], [], 0, "NONE"]; + _container = createVehicle [["GroundWeaponHolder", QGVAR(ammo_holder)] select GVAR(handleExtraMagazinesType), [0, 0, 0], [], 0, "CAN_COLLIDE"]; _unloadTo setVariable [QGVAR(container), _container, true]; _container setDir random [0, 180, 360]; _container setPosATL _weaponRelPos; @@ -59,7 +63,7 @@ if (isNull _container) then { TRACE_2("Creating NEW Container",_container,_weaponRelPos); }; -TRACE_3("adding to container",_container,_fullMagazines,_bulletsRemaining); +TRACE_4("adding to container",_container,typeOf _container,_fullMagazines,_bulletsRemaining); if (_fullMagazines > 0) then { _container addMagazineAmmoCargo [_carryMag, _fullMagazines, _carryMaxAmmo]; diff --git a/addons/csw/functions/fnc_reload_loadMagazine.sqf b/addons/csw/functions/fnc_reload_loadMagazine.sqf index 50081a87a5..07db0fb146 100644 --- a/addons/csw/functions/fnc_reload_loadMagazine.sqf +++ b/addons/csw/functions/fnc_reload_loadMagazine.sqf @@ -52,8 +52,19 @@ private _onFinish = { [_magSource, _carryMag, _bestAmmoToSend] call EFUNC(common,removeSpecificMagazine); if (_bestAmmoToSend == 0) exitWith {}; - TRACE_6("calling addTurretMag event",_vehicle,_turret,_magSource,_carryMag,_bestAmmoToSend,_unit); - [QGVAR(addTurretMag), [_vehicle, _turret, _magSource, _carryMag, _bestAmmoToSend, _unit]] call CBA_fnc_globalEvent; + // Workaround for removeSpecificMagazine and WeaponHolders being deleted when empty, give back to the unit if the weapon holder was deleted + // TODO: Pass type and position of deleted object to create a new one + // TODO: Use '_magSource getEntityInfo 14' in 2.18 and the isSetForDeletion flag to execute in same frame + [{ + params ["_magSource", "_unit", "_args"]; + + if (isNull _magSource) then { + _args pushBack _unit; + }; + + TRACE_1("calling addTurretMag event",_args); + [QGVAR(addTurretMag), _args] call CBA_fnc_globalEvent; + }, [_magSource, _unit, [_vehicle, _turret, _magSource, _carryMag, _bestAmmoToSend]]] call CBA_fnc_execNextFrame; };