2023-09-12 18:58:10 +00:00
|
|
|
#include "..\script_component.hpp"
|
2022-03-07 18:29:19 +00:00
|
|
|
/*
|
2023-07-13 10:01:28 +00:00
|
|
|
* Author: PabstMirror, LinkIsGrim
|
2023-12-18 16:47:01 +00:00
|
|
|
* Handles AI reloading.
|
2022-03-07 18:29:19 +00:00
|
|
|
*
|
|
|
|
* Arguments:
|
2023-07-13 10:20:21 +00:00
|
|
|
* 0: CSW <OBJECT>
|
2022-03-07 18:29:19 +00:00
|
|
|
* 1: Gunner <OBJECT>
|
2023-07-13 16:53:57 +00:00
|
|
|
* 2: Skip reload time <BOOL> (default: false)
|
2023-07-14 16:04:42 +00:00
|
|
|
* 3: Clear forced magazine after reloading (default: true)
|
2022-03-07 18:29:19 +00:00
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Public: No
|
|
|
|
*/
|
2023-12-18 16:47:01 +00:00
|
|
|
|
2023-07-14 16:04:42 +00:00
|
|
|
params ["_vehicle", "_gunner", ["_instantReload", false], ["_clearForcedMag", false]];
|
2023-07-13 16:53:57 +00:00
|
|
|
TRACE_3("AI reload",_vehicle,_gunner,_instantReload);
|
2022-03-07 18:29:19 +00:00
|
|
|
|
2023-07-14 16:04:42 +00:00
|
|
|
// API, used for ai_switchMagazine
|
2023-07-13 16:53:57 +00:00
|
|
|
private _forcedMag = _vehicle getVariable [QGVAR(forcedMag), ""];
|
2023-07-13 21:37:27 +00:00
|
|
|
private _turretIndex = [_gunner] call EFUNC(common,getTurretIndex);
|
|
|
|
if (_turretIndex isEqualTo []) then {
|
|
|
|
_turretIndex = [0];
|
|
|
|
};
|
2023-07-13 16:53:57 +00:00
|
|
|
|
|
|
|
// If this is called while CSW has ammo, unload mags in gunner's turret
|
2023-07-13 21:37:27 +00:00
|
|
|
if (someAmmo _vehicle) then {[_vehicle, _turretIndex] call FUNC(unloadMagazines)};
|
2023-07-13 16:53:57 +00:00
|
|
|
|
2023-07-13 21:48:14 +00:00
|
|
|
private _loadableMagazines = [_vehicle, _gunner, true] call FUNC(reload_getLoadableMagazines);
|
|
|
|
if (_loadableMagazines isEqualTo []) exitWith {TRACE_1("could not find reloadable mag",_vehicle)};
|
|
|
|
|
2023-07-13 07:42:30 +00:00
|
|
|
private _bestAmmo = 0;
|
|
|
|
private _magazineInfo = [];
|
2022-03-07 18:29:19 +00:00
|
|
|
{
|
2023-07-13 16:53:57 +00:00
|
|
|
_x params ["_xMag", "", "", "", "", "_ammo"];
|
|
|
|
if (_forcedMag isNotEqualTo "" && {_xMag != _forcedMag}) then {continue};
|
2023-07-13 07:42:30 +00:00
|
|
|
if (_ammo > _bestAmmo) then {
|
|
|
|
_bestAmmo = _ammo;
|
|
|
|
_magazineInfo = _x;
|
2022-03-07 18:29:19 +00:00
|
|
|
};
|
2023-07-13 07:42:30 +00:00
|
|
|
} forEach _loadableMagazines;
|
|
|
|
|
2023-07-14 16:04:42 +00:00
|
|
|
if (_clearForcedMag) then {
|
|
|
|
_vehicle setVariable [QGVAR(forcedMag), nil, true];
|
|
|
|
};
|
|
|
|
|
2023-07-13 16:53:57 +00:00
|
|
|
if (_magazineInfo isEqualTo []) exitWith {};
|
2023-07-13 07:42:30 +00:00
|
|
|
_magazineInfo params ["_carryMag", "_turretPath", "_loadInfo", "_magSource", "", "_ammo"];
|
2022-03-07 18:29:19 +00:00
|
|
|
|
|
|
|
// Remove the mag from the source
|
2023-07-13 07:42:30 +00:00
|
|
|
[_magSource, _carryMag, _ammo] call EFUNC(common,removeSpecificMagazine);
|
2022-03-07 18:29:19 +00:00
|
|
|
|
2023-07-01 15:39:42 +00:00
|
|
|
// AI never returns ammo and removes the magazine before reloading, so we can skip distance and weaponHolder checks
|
2023-07-13 10:20:21 +00:00
|
|
|
private _eventParams = [_vehicle, _turretPath, objNull, _carryMag, _ammo, _gunner];
|
2023-07-01 15:39:42 +00:00
|
|
|
|
2023-07-13 16:53:57 +00:00
|
|
|
if (_instantReload) exitWith {
|
|
|
|
TRACE_1("calling addTurretMag event: instant AI reload",_this);
|
|
|
|
[QGVAR(addTurretMag), _eventParams] call CBA_fnc_globalEvent;
|
|
|
|
};
|
|
|
|
|
2023-07-13 10:20:21 +00:00
|
|
|
private _timeToLoad = GET_NUMBER(configOf _vehicle >> QUOTE(ADDON) >> "ammoLoadTime", 1);
|
2022-03-07 18:29:19 +00:00
|
|
|
|
|
|
|
TRACE_1("Reloading in progress",_timeToLoad);
|
|
|
|
[{
|
2023-07-13 10:20:21 +00:00
|
|
|
params ["_vehicle", "", "", "", "", "_gunner"];
|
|
|
|
if !(alive _vehicle && {alive _gunner}) exitWith {TRACE_2("invalid state",alive _vehicle,alive _gunner);};
|
2022-03-07 18:29:19 +00:00
|
|
|
|
|
|
|
// Reload the static weapon
|
2023-07-13 07:42:30 +00:00
|
|
|
TRACE_1("calling addTurretMag event: AI reload",_this);
|
2022-03-07 18:29:19 +00:00
|
|
|
[QGVAR(addTurretMag), _this] call CBA_fnc_globalEvent;
|
2023-07-01 15:39:42 +00:00
|
|
|
}, _eventParams, _timeToLoad] call CBA_fnc_waitAndExecute;
|