|
1 | 1 | #include "script_component.hpp"
|
2 | 2 | /*
|
3 | 3 | * Author: Kex
|
4 |
| - * Instantly loads the given magazine into the specified weapon. |
| 4 | + * Instantly loads the given magazine into the specified vehicle's turret weapon. |
5 | 5 | *
|
6 | 6 | * Arguments:
|
7 | 7 | * 0: Vehicle <OBJECT>
|
8 |
| - * 1: Turret path <ARRAY> |
9 |
| - * 2: Muzzle class <STRING> |
10 |
| - * 3: Magazine class <STRING> |
| 8 | + * 1: Turret Path <ARRAY> |
| 9 | + * 2: Weapon <STRING> |
| 10 | + * 3: Magazine <STRING> |
11 | 11 | *
|
12 | 12 | * Return Value:
|
13 | 13 | * None
|
14 | 14 | *
|
15 | 15 | * Example:
|
16 |
| - * [_vehicle, [0], "HE", "60Rnd_40mm_GPR_Tracer_Red_shells"] call zen_common_fnc_loadMagazineInstantly |
| 16 | + * [_vehicle, [0, 0], "HMG_127_MBT", "200Rnd_127x99_mag_Tracer_Red"] call zen_common_fnc_loadMagazineInstantly |
17 | 17 | *
|
18 | 18 | * Public: No
|
19 | 19 | */
|
20 | 20 |
|
21 |
| -params [["_vehicle", objNull, [objNull]], ["_turretPath", [], [[]]], ["_muzzle", "", [""]], ["_magazine", "", [""]]]; |
| 21 | +params [["_vehicle", objNull, [objNull]], ["_turretPath", [], [[]]], ["_weapon", "", [""]], ["_magazine", "", [""]]]; |
| 22 | + |
| 23 | +if !(_vehicle turretLocal _turretPath) exitWith { |
| 24 | + [QGVAR(loadMagazineInstantly), _this, _vehicle, _turretPath] call CBA_fnc_turretEvent; |
| 25 | +}; |
| 26 | + |
| 27 | +// Get all magazines compatible with the given weapon on the given turret |
| 28 | +private _magazines = createHashMap; |
| 29 | +private _compatibleMagazines = [_weapon, true] call CBA_fnc_compatibleMagazines; |
22 | 30 |
|
23 |
| -private _magazines = []; |
24 |
| -private _ammoCounts = []; |
25 | 31 | {
|
26 |
| - _x params ["_currentMagazine", "_currentTurretPath", "_currentAmmoCount"]; |
27 |
| - if (_turretPath isEqualTo _currentTurretPath) then { |
28 |
| - _magazines pushBack _currentMagazine; |
29 |
| - _ammoCounts pushBack _currentAmmoCount; |
| 32 | + _x params ["_xMagazine", "_xTurretPath", "_xAmmoCount"]; |
| 33 | + |
| 34 | + if (_turretPath isEqualTo _xTurretPath && {_xMagazine in _compatibleMagazines}) then { |
| 35 | + _magazines getOrDefault [_xMagazine, [], true] pushBack _xAmmoCount; |
30 | 36 | };
|
31 |
| -} forEach (magazinesAllTurrets _vehicle); |
| 37 | +} forEach magazinesAllTurrets _vehicle; |
| 38 | + |
| 39 | +// Ensure that the vehicle has the desired magazine to load |
| 40 | +if (_magazine in _magazines) then { |
| 41 | + // Remove given weapon and all compatible magazines |
| 42 | + _vehicle removeWeaponTurret [_weapon, _turretPath]; |
32 | 43 |
|
33 |
| -private _magIdx = _magazines findIf {_x == _magazine}; |
34 |
| -if (_magIdx != -1) then { |
35 |
| - // Remove weapon and magazines |
36 | 44 | {
|
37 |
| - _vehicle removeMagazineTurret [_x, _turretPath]; |
| 45 | + _vehicle removeMagazinesTurret [_x, _turretPath]; |
38 | 46 | } forEach _magazines;
|
39 |
| - _vehicle removeWeaponTurret [_muzzle, _turretPath]; |
40 | 47 |
|
41 |
| - // Add desired magazine first |
42 |
| - _vehicle addMagazineTurret [_magazine, _turretPath, _ammoCounts select _magIdx]; |
43 |
| - _magazines deleteAt _magIdx; |
44 |
| - _ammoCounts deleteAt _magIdx; |
| 48 | + // Add magazines of selected type back first (load magazine with most ammo) |
| 49 | + private _selectedMagazine = _magazines deleteAt _magazine; |
| 50 | + _selectedMagazine sort false; |
45 | 51 |
|
46 |
| - // Restore weapon and magazines |
47 |
| - _vehicle addWeaponTurret [_muzzle, _turretPath]; |
48 | 52 | {
|
49 |
| - _vehicle addMagazineTurret [_x, _turretPath, _ammoCounts select _forEachIndex]; |
| 53 | + _vehicle addMagazineTurret [_magazine, _turretPath, _x]; |
| 54 | + } forEach _selectedMagazine; |
| 55 | + |
| 56 | + // Restore weapon and other magazines (will load desired magazine since it was added first) |
| 57 | + _vehicle addWeaponTurret [_weapon, _turretPath]; |
| 58 | + |
| 59 | + { |
| 60 | + private _magazine = _x; |
| 61 | + |
| 62 | + { |
| 63 | + _vehicle addMagazineTurret [_magazine, _turretPath, _x]; |
| 64 | + } forEach _y; |
50 | 65 | } forEach _magazines;
|
| 66 | + |
| 67 | + // Select the weapon that the magazine was loaded into |
| 68 | + _vehicle selectWeaponTurret [_weapon, _turretPath]; |
51 | 69 | };
|
0 commit comments