diff --git a/addons/csw/XEH_PREP.hpp b/addons/csw/XEH_PREP.hpp index cc3545042b..7985e2aff9 100644 --- a/addons/csw/XEH_PREP.hpp +++ b/addons/csw/XEH_PREP.hpp @@ -5,6 +5,7 @@ PREP(aceRearmGetCarryMagazines); PREP(ai_handleFired); PREP(ai_handleGetIn); PREP(ai_reload); +PREP(ai_unloadMagazines); PREP(assemble_canDeployTripod); PREP(assemble_canDeployWeapon); diff --git a/addons/csw/functions/fnc_ai_reload.sqf b/addons/csw/functions/fnc_ai_reload.sqf index b3429c1167..3b53d3c71c 100644 --- a/addons/csw/functions/fnc_ai_reload.sqf +++ b/addons/csw/functions/fnc_ai_reload.sqf @@ -6,38 +6,50 @@ * Arguments: * 0: CSW * 1: Gunner - * 2: Weapon + * 2: Skip reload time (default: false) * * Return Value: * None * * Public: No */ -params ["_vehicle", "_gunner", "_weapon"]; -TRACE_3("AI reload",_vehicle,_gunner,_weapon); +params ["_vehicle", "_gunner", ["_instantReload", false]]; +TRACE_3("AI reload",_vehicle,_gunner,_instantReload); private _loadableMagazines = [_vehicle, _gunner, true] call FUNC(reload_getLoadableMagazines); if (_loadableMagazines isEqualTo []) exitWith {TRACE_1("could not find reloadable mag",_vehicle)}; +// API, to be used by artillerytables +private _forcedMag = _vehicle getVariable [QGVAR(forcedMag), ""]; + +// If this is called while CSW has ammo, unload mags in gunner's turret +if (someAmmo _vehicle) then {[_vehicle, _gunner, [_gunner] call EFUNC(common,getTurretIndex)] call FUNC(ai_unloadMagazine)}; + private _bestAmmo = 0; private _magazineInfo = []; { - _x params ["", "", "", "", "", "_ammo"]; + _x params ["_xMag", "", "", "", "", "_ammo"]; + if (_forcedMag isNotEqualTo "" && {_xMag != _forcedMag}) then {continue}; if (_ammo > _bestAmmo) then { _bestAmmo = _ammo; _magazineInfo = _x; }; } forEach _loadableMagazines; +if (_magazineInfo isEqualTo []) exitWith {}; _magazineInfo params ["_carryMag", "_turretPath", "_loadInfo", "_magSource", "", "_ammo"]; // Remove the mag from the source [_magSource, _carryMag, _ammo] call EFUNC(common,removeSpecificMagazine); -// see fnc_reload_loadMagazine #L54 // AI never returns ammo and removes the magazine before reloading, so we can skip distance and weaponHolder checks private _eventParams = [_vehicle, _turretPath, objNull, _carryMag, _ammo, _gunner]; +if (_instantReload) exitWith { + TRACE_1("calling addTurretMag event: instant AI reload",_this); + [QGVAR(addTurretMag), _eventParams] call CBA_fnc_globalEvent; +}; + private _timeToLoad = GET_NUMBER(configOf _vehicle >> QUOTE(ADDON) >> "ammoLoadTime", 1); TRACE_1("Reloading in progress",_timeToLoad); diff --git a/addons/csw/functions/fnc_ai_unloadMagazines.sqf b/addons/csw/functions/fnc_ai_unloadMagazines.sqf new file mode 100644 index 0000000000..544944dd59 --- /dev/null +++ b/addons/csw/functions/fnc_ai_unloadMagazines.sqf @@ -0,0 +1,43 @@ +#include "script_component.hpp" +/* + * Author: LinkIsGrim + * Switch loaded magazine on an AI CSW + * + * Arguments: + * 0: CSW + * 1: Gunner + * 2: Turret Path + * + * Return Value: + * None + * + * Public: No + */ +params ["_vehicle", "_gunner", "_turretPath"]; + +private _magsToRemove = []; +private _containerMagazineClassnames = []; +private _containerMagazineCount = []; + +{ + _x params ["_xMag", "_xTurret", "_xAmmo"]; + if (_xTurret isNotEqualTo _turretPath) then {continue}; + private _carryMag = _xMag call FUNC(getCarryMagazine); + if (_carryMag != "") then { + _magsToRemove pushBackUnique [_xMag, _xTurret]; + private _index = _containerMagazineClassnames find _carryMag; + if (_index < 0) then { + _index = _containerMagazineClassnames pushBack _carryMag; + _containerMagazineCount pushBack 0; + }; + _containerMagazineCount set [_index, (_containerMagazineCount select _index) + _xAmmo]; + }; +} forEach (_vehicle magazinesAllTurrets _vehicle); + +{ + _vehicle removeMagazinesTurret _x; +} forEach _magsToRemove; + +{ + [_vehicle, _x, _containerMagazineCount select _forEachIndex] call FUNC(reload_handleReturnAmmo); +} forEach _containerMagazineClassnames;