ACE3/addons/rearm/functions/fnc_getNeedRearmMagazines.sqf
Tuupertunut 0644ad9ca2 Rearm bugfixing (#5411)
* Added workaround functions for turret magazine ammo.

* Switched to retrieving turrets dynamically instead of looking them up from an array.

* Refactoring only, no functional changes

* Repurposed getNeedRearmMagazines as a general purpose rearm info function + more.

* The function retrieves all non-full magazines. This code was previously repeated in many different functions, but is now centralized in one.
* Empty pylon filling is no longer supported in favor of the upcoming "pylons" ACE-module.

Fixed bugs:

* Rearm was using broken commands "magazineTurretAmmo" and "setMagazineTurretAmmo". They are now replaced with their corresponding workaround functions.
* Pylon rearm was trying to set ammo count on empty pylons.

Note: rearmSuccessLocal is not yet fixed!

* Rearming no longer switches shell types in cannons/mortars.

* Added Tuupertunut to authors.

* Rearming no longer switches shell types. 2nd try.

Last time it was fixed only if the turret is manned. Now it is fixed even when unmanned.

* Updated fnc_rearm to support the earlier repurposing of fnc_getNeedRearmMagazines.

* Small fixes and comments.

* Renamed fnc_getVehicleMagazines to fnc_getTurretConfigMagazines.

The latter describes much better what the function actually does.

* Removed redundant checks.

* Refactoring.

* Fixed spaces in macros.

* Renamed fnc_getAllTurrets to fnc_getAllRearmTurrets
2017-09-29 14:53:25 -05:00

91 lines
3.3 KiB
Plaintext

/*
* Author: Tuupertunut
* Returns information about every magazine that can be rearmed in the vehicle. Multiple mags of
* same class in the same turret are grouped together for practical reasons.
*
* Arguments:
* 0: Vehicle <OBJECT>
*
* Return Value:
* Magazine info <ARRAY of ARRAYs>
* Child arrays:
* 0: Magazine class <STRING>
* 1: Turret path <ARRAY>
* 2: Is pylon magazine <BOOLEAN>
* 3: Pylon index (-1 if not pylon) <NUMBER>
* 4: Max magazines <NUMBER>
* 5: Current magazines <NUMBER>
* 6: Max rounds per magazine <NUMBER>
* 7: Current rounds in magazines <ARRAY of NUMBERs>
*
* Example:
* [tank] call ace_rearm_fnc_getNeedRearmMagazines
*
* Public: No
*/
#include "script_component.hpp"
params ["_vehicle"];
private _magazineInfo = [];
// 1.70 pylons
private _pylonConfigs = configProperties [configFile >> "CfgVehicles" >> (typeOf _vehicle) >> "Components" >> "TransportPylonsComponent" >> "Pylons", "isClass _x"];
{
private _pylonConfig = _x;
// Strangely, a 1-based index.
private _pylonIndex = _forEachIndex + 1;
// Retrieving pylon magazine by index. If the pylon is empty, it is marked with "".
private _pylonMagazine = (getPylonMagazines _vehicle) select (_pylonIndex - 1);
// Only care about pylons that have a magazine.
if (!(_pylonMagazine isEqualTo "")) then {
private _maxRounds = getNumber (configFile >> "CfgMagazines" >> _pylonMagazine >> "count");
private _currentRounds = _vehicle ammoOnPylon _pylonIndex;
if (_currentRounds < _maxRounds) then {
private _pylonTurret = getArray (_pylonConfig >> "turret");
// Converting to expected array for driver.
if (_pylonTurret isEqualTo []) then {_pylonTurret = [-1];};
_magazineInfo pushBack [_pylonMagazine, _pylonTurret, true, _pylonIndex, 1, 1, _maxRounds, [_currentRounds]];
};
};
} forEach _pylonConfigs;
private _turrets = [_vehicle] call FUNC(getAllRearmTurrets);
{
private _turretPath = _x;
private _magazines = [_vehicle, _turretPath] call FUNC(getTurretConfigMagazines);
// _magazines without duplicates
private _magazineClasses = _magazines arrayIntersect _magazines;
{
private _magazineClass = _x;
private _maxMagazines = [_vehicle, _turretPath, _magazineClass] call FUNC(getMaxMagazines);
private _maxRoundsPerMag = getNumber (configFile >> "CfgMagazines" >> _magazineClass >> "count");
/* Array of ammo counts in every magazine. Example: [200, 200, 152] means 2 mags with 200
* rounds and 1 mag with 152 rounds. */
private _currentRounds = [_vehicle, _turretPath, _magazineClass] call FUNC(getTurretMagazineAmmo);
private _currentMagazines = count _currentRounds;
/* If there is space for new magazines or if some magazines are not full, add the magazine
* type to _magazineInfo. */
if ((_currentMagazines < _maxMagazines) || {({_x < _maxRoundsPerMag} count _currentRounds) > 0}) then {
_magazineInfo pushBack [_magazineClass, _turretPath, false, -1, _maxMagazines, _currentMagazines, _maxRoundsPerMag, _currentRounds];
};
} forEach _magazineClasses;
} forEach _turrets;
TRACE_2("getNeedRearmMagazines",_vehicle,_magazineInfo);
_magazineInfo