CSW - Fix belt linking (#10148)

* fix unloading to units with full inventory

* fix belt linking issues

* Enhance workaround

* Use unit instead if magSource is null

---------

Co-authored-by: Salluci <salluci.lovi@gmail.com>
This commit is contained in:
johnb432 2024-07-27 19:15:50 +02:00 committed by GitHub
parent ba22c407e3
commit b7cd72e936
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 14 deletions

View File

@ -10,7 +10,7 @@
* 2: Source of magazine <OBJECT>
* 3: Vehicle Magazine <STRING>
* 4: Ammo in magazine <NUMBER>
* 5: Unit or object to return ammo to <OBJECT>
* 5: Unit or object to return ammo to <OBJECT> (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);

View File

@ -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];
// 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];

View File

@ -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;
};