diff --git a/AUTHORS.txt b/AUTHORS.txt index abb66683b8..4fe978dd40 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -128,6 +128,7 @@ Tessa Elieff Toaster Tonic Tourorist +Tuupertunut Valentin Torikian voiper VyMajoris(W-Cephei) diff --git a/addons/rearm/XEH_PREP.hpp b/addons/rearm/XEH_PREP.hpp index 59a43b4b29..05377af37a 100644 --- a/addons/rearm/XEH_PREP.hpp +++ b/addons/rearm/XEH_PREP.hpp @@ -9,12 +9,14 @@ PREP(canTakeAmmo); PREP(createDummy); PREP(disable); PREP(dropAmmo); +PREP(getAllRearmTurrets); PREP(getCaliber); PREP(getHardpointMagazines); PREP(getMaxMagazines); PREP(getNeedRearmMagazines); PREP(getSupplyCount); -PREP(getVehicleMagazines); +PREP(getTurretConfigMagazines); +PREP(getTurretMagazineAmmo); PREP(grabAmmo); PREP(handleKilled); PREP(handleUnconscious); @@ -32,6 +34,7 @@ PREP(rearmSuccess); PREP(rearmSuccessLocal); PREP(removeMagazineFromSupply); PREP(setSupplyCount); +PREP(setTurretMagazineAmmo); PREP(storeAmmo); PREP(takeAmmo); PREP(takeSuccess); diff --git a/addons/rearm/functions/fnc_addRearmActions.sqf b/addons/rearm/functions/fnc_addRearmActions.sqf index c017c93509..b164449575 100644 --- a/addons/rearm/functions/fnc_addRearmActions.sqf +++ b/addons/rearm/functions/fnc_addRearmActions.sqf @@ -3,7 +3,7 @@ * Show the resupplyable ammunition of all surrounding vehicles. * * Arguments: - * 0: Target + * 0: Ammo Truck * * Return Value: * ChildActions @@ -15,7 +15,7 @@ */ #include "script_component.hpp" -params [["_truck", objNull, [objNull]]]; +params ["_truck"]; private _vehicles = nearestObjects [_truck, ["AllVehicles"], 20]; _vehicles = _vehicles select {(_x != _truck) && {!(_x isKindOf "CAManBase")} && {!(_x getVariable [QGVAR(disabled), false])}}; @@ -23,49 +23,12 @@ _vehicles = _vehicles select {(_x != _truck) && {!(_x isKindOf "CAManBase")} && private _vehicleActions = []; { private _vehicle = _x; - private _magazineHelper = []; - { - private _turretPath = _x; - private _magazines = [_vehicle, _turretPath] call FUNC(getVehicleMagazines); - { - private _magazine = _x; - if (!(_magazine in _magazineHelper)) then { - private _currentMagazines = { _x == _magazine } count (_vehicle magazinesTurret _turretPath); - private _maxMagazines = [_vehicle, _turretPath, _magazine] call FUNC(getMaxMagazines); - - if ((_currentMagazines < _maxMagazines) || {(_vehicle magazineTurretAmmo [_magazine, _turretPath]) < getNumber (configFile >> "CfgMagazines" >> _magazine >> "count")}) then { - _magazineHelper pushBack _magazine; - }; - }; - false - } count _magazines; - false - } count REARM_TURRET_PATHS; - - // 1.70 pylons - private _pylonConfigs = configProperties [configFile >> "CfgVehicles" >> (typeOf _vehicle) >> "Components" >> "TransportPylonsComponent" >> "Pylons", "isClass _x"]; - { - private _pylonName = configName _x; - private _pylonAmmo = _vehicle ammoOnPylon _pylonName; - private _pylonMagazine = (getPylonMagazines _vehicle) select _forEachIndex; - TRACE_3("",_pylonName,_pylonAmmo,_pylonMagazine); - - if (_pylonAmmo > 0) then { - // Try to refill current pylon: - private _magAmmo = getNumber (configFile >> "CfgMagazines" >> _pylonMagazine >> "count"); - if ((!(_pylonMagazine in _magazineHelper)) && {_pylonAmmo < _magAmmo}) then { - _magazineHelper pushBack _pylonMagazine; - }; - } else { - // See what we magazines can add to the empty pylon: - private _hardpointMags = [_x] call FUNC(getHardpointMagazines); - { - if (!(_x in _magazineHelper)) then { - _magazineHelper pushBack _x; - }; - } forEach _hardpointMags; - }; - } forEach _pylonConfigs; + + // Array of magazines that can be rearmed in the vehicle + private _needRearmMags = ([_vehicle] call FUNC(getNeedRearmMagazines)) apply {_x select 0}; + + // _needRearmMags without duplicates + private _magazineHelper = _needRearmMags arrayIntersect _needRearmMags; _magazineHelper = _magazineHelper select {[_truck, _x] call FUNC(hasEnoughSupply)}; TRACE_2("can add",_x,_magazineHelper); @@ -77,40 +40,46 @@ private _vehicleActions = []; }; if (GVAR(level) == 0) then { // [Level 0] adds a single action to rearm the entire vic - private _action = [_vehicle, - getText(configFile >> "CfgVehicles" >> (typeOf _vehicle) >> "displayName"), - _icon, - {_this call FUNC(rearmEntireVehicle)}, - {true}, - {}, - _vehicle] call EFUNC(interact_menu,createAction); + private _action = [ + _vehicle, + getText(configFile >> "CfgVehicles" >> (typeOf _vehicle) >> "displayName"), + _icon, + {_this call FUNC(rearmEntireVehicle)}, + {true}, + {}, + _vehicle + ] call EFUNC(interact_menu,createAction); _vehicleActions pushBack [_action, [], _truck]; } else { // [Level 1,2] - Add actions for each magazine private _actions = []; { - private _action = [_x, - getText(configFile >> "CfgMagazines" >> _x >> "displayName"), - getText(configFile >> "CfgMagazines" >> _x >> "picture"), - {_this call FUNC(takeAmmo)}, - {true}, - {}, - [_x, _vehicle]] call EFUNC(interact_menu,createAction); + private _action = [ + _x, + getText(configFile >> "CfgMagazines" >> _x >> "displayName"), + getText(configFile >> "CfgMagazines" >> _x >> "picture"), + {_this call FUNC(takeAmmo)}, + {true}, + {}, + [_x, _vehicle] + ] call EFUNC(interact_menu,createAction); _actions pushBack [_action, [], _truck]; } forEach _magazineHelper; - private _action = [_vehicle, + private _action = [ + _vehicle, getText(configFile >> "CfgVehicles" >> (typeOf _vehicle) >> "displayName"), _icon, {}, {true}, {}, - []] call EFUNC(interact_menu,createAction); + [] + ] call EFUNC(interact_menu,createAction); + _vehicleActions pushBack [_action, _actions, _truck]; }; }; - false -} count _vehicles; +} forEach _vehicles; _vehicleActions diff --git a/addons/rearm/functions/fnc_addVehicleMagazinesToSupply.sqf b/addons/rearm/functions/fnc_addVehicleMagazinesToSupply.sqf index 087ea7fddd..2a8f9fed14 100644 --- a/addons/rearm/functions/fnc_addVehicleMagazinesToSupply.sqf +++ b/addons/rearm/functions/fnc_addVehicleMagazinesToSupply.sqf @@ -29,16 +29,17 @@ if (_vehicle isEqualType objNull) then {_vehicle = typeOf _vehicle}; if (_vehicle == "") exitWith { ERROR_1("VehicleType [%1] is empty in ace_rearm_fnc_addVehicleMagazinesToSupply",_string); }; +private _turrets = [_vehicle] call FUNC(getAllRearmTurrets); { private _turretPath = _x; - private _magazines = [_vehicle, _turretPath] call FUNC(getVehicleMagazines); + private _magazines = [_vehicle, _turretPath] call FUNC(getTurretConfigMagazines); TRACE_2("",_turretPath,_magazines); { [_truck, _x] call FUNC(addMagazineToSupply); false } count _magazines; false -} count REARM_TURRET_PATHS; +} count _turrets; // 1.70 pylons private _pylonConfigs = configProperties [configFile >> "CfgVehicles" >> _vehicle >> "Components" >> "TransportPylonsComponent" >> "Pylons", "isClass _x"]; diff --git a/addons/rearm/functions/fnc_canReadSupplyCounter.sqf b/addons/rearm/functions/fnc_canReadSupplyCounter.sqf index 80b5d35cd7..60ca21b3f5 100644 --- a/addons/rearm/functions/fnc_canReadSupplyCounter.sqf +++ b/addons/rearm/functions/fnc_canReadSupplyCounter.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_truck", objNull, [objNull]], - ["_unit", objNull, [objNull]] -]; +params ["_truck", "_unit"]; (alive _unit) && {_unit isKindOf "CAManBase"} diff --git a/addons/rearm/functions/fnc_canRearm.sqf b/addons/rearm/functions/fnc_canRearm.sqf index ea41cae5a9..236f23e233 100644 --- a/addons/rearm/functions/fnc_canRearm.sqf +++ b/addons/rearm/functions/fnc_canRearm.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_vehicle", objNull, [objNull]], - ["_unit", objNull, [objNull]] -]; +params ["_vehicle", "_unit"]; if (!alive _vehicle) exitWith {false}; if (GVAR(level) == 0 || {isNull _unit} || {!(_unit isKindOf "CAManBase")} || {!local _unit} || {_vehicle distance _unit > REARM_ACTION_DISTANCE} || {_vehicle getVariable [QGVAR(disabled), false]}) exitWith {false}; @@ -29,4 +26,9 @@ if (isNull _dummy) exitwith {false}; private _magazineClass = _dummy getVariable QGVAR(magazineClass); if (isNil "_magazineClass") exitWith {false}; -([_vehicle, _magazineClass] call FUNC(getNeedRearmMagazines)) select 0 +private _needRearmMags = [_vehicle] call FUNC(getNeedRearmMagazines); + +// Testing if vehicle needs rearm on any magazines of class _magazineClass +private _needsRearm = ({(_x select 0) isEqualTo _magazineClass} count _needRearmMags) > 0; + +_needsRearm diff --git a/addons/rearm/functions/fnc_canStoreAmmo.sqf b/addons/rearm/functions/fnc_canStoreAmmo.sqf index b303af372c..4c4989b1d7 100644 --- a/addons/rearm/functions/fnc_canStoreAmmo.sqf +++ b/addons/rearm/functions/fnc_canStoreAmmo.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_truck", objNull, [objNull]], - ["_unit", objNull, [objNull]] -]; +params ["_truck", "_unit"]; (alive _unit) && {_unit isKindOf "CAManBase"} diff --git a/addons/rearm/functions/fnc_canTakeAmmo.sqf b/addons/rearm/functions/fnc_canTakeAmmo.sqf index 6f15f46eb0..c508df378d 100644 --- a/addons/rearm/functions/fnc_canTakeAmmo.sqf +++ b/addons/rearm/functions/fnc_canTakeAmmo.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_truck", objNull, [objNull]], - ["_unit", objNull, [objNull]] -]; +params ["_truck", "_unit"]; (alive _unit) && {_unit isKindOf "CAManBase"} diff --git a/addons/rearm/functions/fnc_createDummy.sqf b/addons/rearm/functions/fnc_createDummy.sqf index ecdaf9ff55..afca11935f 100644 --- a/addons/rearm/functions/fnc_createDummy.sqf +++ b/addons/rearm/functions/fnc_createDummy.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_unit", objNull, [objNull]], - ["_magazineClass", "", [""]] -]; +params ["_unit", "_magazineClass"]; private _ammo = getText (configFile >> "CfgMagazines" >> _magazineClass >> "ammo"); private _dummyName = getText (configFile >> "CfgAmmo" >> _ammo >> QGVAR(dummy)); diff --git a/addons/rearm/functions/fnc_dropAmmo.sqf b/addons/rearm/functions/fnc_dropAmmo.sqf index 8c1336476d..61e744fcf5 100644 --- a/addons/rearm/functions/fnc_dropAmmo.sqf +++ b/addons/rearm/functions/fnc_dropAmmo.sqf @@ -18,9 +18,9 @@ #include "script_component.hpp" params [ - ["_unit", objNull, [objNull]], - ["_delete", false, [false]], - ["_unholster", true, [true]] + "_unit", + ["_delete", false], + ["_unholster", true] ]; private _dummy = _unit getVariable [QGVAR(dummy), objNull]; diff --git a/addons/rearm/functions/fnc_getAllRearmTurrets.sqf b/addons/rearm/functions/fnc_getAllRearmTurrets.sqf new file mode 100644 index 0000000000..890fe5cc6f --- /dev/null +++ b/addons/rearm/functions/fnc_getAllRearmTurrets.sqf @@ -0,0 +1,28 @@ +/* + * Author: Tuupertunut + * Returns all turrets in a vehicle. + * + * BIS command "allTurrets" does not return the driver turret at the time of writing (2017-07-16). + * This function just adds driver turret to the array returned by "allTurrets". + * + * Arguments: + * 0: Vehicle + * + * Return Value: + * Turret paths + * + * Example: + * [vehicle] call ace_rearm_fnc_getAllRearmTurrets + * + * Public: No + */ +#include "script_component.hpp" + +params ["_vehicle"]; + +private _turrets = allTurrets _vehicle; + +// Adding the driver turret "[-1]". +_turrets pushBack [-1]; + +_turrets diff --git a/addons/rearm/functions/fnc_getCaliber.sqf b/addons/rearm/functions/fnc_getCaliber.sqf index 7a07b9a1db..87f7b6cd88 100644 --- a/addons/rearm/functions/fnc_getCaliber.sqf +++ b/addons/rearm/functions/fnc_getCaliber.sqf @@ -18,7 +18,7 @@ #include "script_component.hpp" params [ - ["_magazineClass", "", [""]] + ["_magazineClass", ""] ]; if (_magazineClass isEqualTo "") exitWith {[8, 2]}; diff --git a/addons/rearm/functions/fnc_getMaxMagazines.sqf b/addons/rearm/functions/fnc_getMaxMagazines.sqf index c747714d86..6eba4e0ecc 100644 --- a/addons/rearm/functions/fnc_getMaxMagazines.sqf +++ b/addons/rearm/functions/fnc_getMaxMagazines.sqf @@ -17,13 +17,7 @@ */ #include "script_component.hpp" -params [ - ["_vehicle", objNull, [objNull]], - ["_turretPath", [], [[]]], - ["_magazineClass", "", [""]] -]; +params ["_vehicle", "_turretPath", "_magazineClass"]; -if (isNull _vehicle) exitWith {0}; - -private _count = {_x == _magazineClass} count ([_vehicle, _turretPath] call FUNC(getVehicleMagazines)); +private _count = {_x == _magazineClass} count ([_vehicle, _turretPath] call FUNC(getTurretConfigMagazines)); _count diff --git a/addons/rearm/functions/fnc_getNeedRearmMagazines.sqf b/addons/rearm/functions/fnc_getNeedRearmMagazines.sqf index 6b2620a99a..c3ac8abfed 100644 --- a/addons/rearm/functions/fnc_getNeedRearmMagazines.sqf +++ b/addons/rearm/functions/fnc_getNeedRearmMagazines.sqf @@ -1,78 +1,90 @@ /* - * Author: GitHawk, Jonpas - * Get rearm return value. + * 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 - * 1: Magazine Classname * * Return Value: - * Return Value - * 0: Can Rearm - * 1: TurretPath - * 2: Number of current magazines in turret path - * 3: Pylon Index (-1 if not a pylon) + * Magazine info + * Child arrays: + * 0: Magazine class + * 1: Turret path + * 2: Is pylon magazine + * 3: Pylon index (-1 if not pylon) + * 4: Max magazines + * 5: Current magazines + * 6: Max rounds per magazine + * 7: Current rounds in magazines * * Example: - * [tank, "500Rnd_127x99_mag_Tracer_Red"] call ace_rearm_fnc_getNeedRearmMagazines + * [tank] call ace_rearm_fnc_getNeedRearmMagazines * * Public: No */ #include "script_component.hpp" -params [["_vehicle", objNull, [objNull]], ["_magazineClass", "", [""]]]; +params ["_vehicle"]; -private _return = [false, [], 0, -1]; +private _magazineInfo = []; + +// 1.70 pylons +private _pylonConfigs = configProperties [configFile >> "CfgVehicles" >> (typeOf _vehicle) >> "Components" >> "TransportPylonsComponent" >> "Pylons", "isClass _x"]; { - private _magazines = [_vehicle, _x] call FUNC(getVehicleMagazines); - - if (_magazineClass in _magazines) then { - private _currentMagazines = {_x == _magazineClass} count (_vehicle magazinesTurret _x); - - if ((_vehicle magazineTurretAmmo [_magazineClass, _x]) < getNumber (configFile >> "CfgMagazines" >> _magazineClass >> "count")) exitWith { - _return = [true, _x, _currentMagazines, -1]; - }; - - if (_currentMagazines < ([_vehicle, _x, _magazineClass] call FUNC(getMaxMagazines))) exitWith { - _return = [true, _x, _currentMagazines, -1]; + 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; - if (_return select 0) exitWith {}; - false -} count REARM_TURRET_PATHS; +private _turrets = [_vehicle] call FUNC(getAllRearmTurrets); +{ + private _turretPath = _x; + private _magazines = [_vehicle, _turretPath] call FUNC(getTurretConfigMagazines); -if (!(_return select 0)) then { - // 1.70 pylons - private _pylonConfigs = configProperties [configFile >> "CfgVehicles" >> (typeOf _vehicle) >> "Components" >> "TransportPylonsComponent" >> "Pylons", "isClass _x"]; + // _magazines without duplicates + private _magazineClasses = _magazines arrayIntersect _magazines; + { - private _pylonName = configName _x; - private _pylonIndex = _forEachIndex + 1; // WTF BIS - private _pylonAmmo = _vehicle ammoOnPylon _pylonName; - private _pylonMagazine = (getPylonMagazines _vehicle) select _forEachIndex; - private _pylonTurret = getArray (_x >> "turret"); - if (_pylonTurret isEqualTo []) then {_pylonTurret = [-1];}; // convert to expected array for driver - TRACE_4("",_pylonName,_pylonAmmo,_pylonMagazine,_pylonTurret); + 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 (_pylonAmmo > 0) then { - if (_magazineClass == _pylonMagazine) then { // Try to refill current pylon: - private _magAmmo = getNumber (configFile >> "CfgMagazines" >> _pylonMagazine >> "count"); - if (_pylonAmmo < _magAmmo) then { - _return = [true, _pylonTurret, 0, _pylonIndex]; - }; - }; - } else { - // See what we magazines can add to the empty pylon: - private _hardpointMags = [_x] call FUNC(getHardpointMagazines); - { - if (_x == _magazineClass) then { - _return = [true, _pylonTurret, 0, _pylonIndex]; - }; - } forEach _hardpointMags; + /* 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]; }; - if (_return select 0) exitWith {}; - } forEach _pylonConfigs; -}; + + } forEach _magazineClasses; +} forEach _turrets; -TRACE_3("getNeedRearmMagazines",_vehicle,_magazineClass,_return); -_return +TRACE_2("getNeedRearmMagazines",_vehicle,_magazineInfo); +_magazineInfo diff --git a/addons/rearm/functions/fnc_getVehicleMagazines.sqf b/addons/rearm/functions/fnc_getTurretConfigMagazines.sqf similarity index 86% rename from addons/rearm/functions/fnc_getVehicleMagazines.sqf rename to addons/rearm/functions/fnc_getTurretConfigMagazines.sqf index 6a0932d908..0421e1ed1a 100644 --- a/addons/rearm/functions/fnc_getVehicleMagazines.sqf +++ b/addons/rearm/functions/fnc_getTurretConfigMagazines.sqf @@ -10,7 +10,7 @@ * Magazine classes in TurretPath * * Example: - * [vehicle, [0]] call ace_rearm_fnc_getVehicleMagazines + * [vehicle, [0]] call ace_rearm_fnc_getTurretConfigMagazines * * Public: No */ diff --git a/addons/rearm/functions/fnc_getTurretMagazineAmmo.sqf b/addons/rearm/functions/fnc_getTurretMagazineAmmo.sqf new file mode 100644 index 0000000000..53107dd7b0 --- /dev/null +++ b/addons/rearm/functions/fnc_getTurretMagazineAmmo.sqf @@ -0,0 +1,27 @@ +/* + * Author: Tuupertunut + * Returns the current ammo counts in all magazines of given class in turret. + * + * BIS command "magazineTurretAmmo" is broken at the time of writing (2017-06-24) + * (https://feedback.bistudio.com/T79689). This function is intended as a workaround for it, + * extracting the data from the array returned by "magazinesAllTurrets". + * + * Arguments: + * 0: Vehicle + * 1: Turret Path + * 2: Magazine Classname + * + * Return Value: + * Current ammo counts in magazines. + * + * Example: + * [vehicle, [0], "200Rnd_127x99_mag_Tracer_Red"] call ace_rearm_fnc_getTurretMagazineAmmo + * + * Public: No + */ +#include "script_component.hpp" + +params ["_vehicle", "_turretPath", "_magazineClass"]; + +private _ammo = magazinesAllTurrets _vehicle select {(_x select 0) isEqualTo _magazineClass && {(_x select 1) isEqualTo _turretPath}} apply {_x select 2}; +_ammo diff --git a/addons/rearm/functions/fnc_grabAmmo.sqf b/addons/rearm/functions/fnc_grabAmmo.sqf index a77a17284b..7dc22ebfdb 100644 --- a/addons/rearm/functions/fnc_grabAmmo.sqf +++ b/addons/rearm/functions/fnc_grabAmmo.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_dummy", objNull, [objNull]], - ["_unit", objNull, [objNull]] -]; +params ["_dummy", "_unit"]; REARM_HOLSTER_WEAPON; [_unit, "forceWalk", QGVAR(vehRearm), true] call EFUNC(common,statusEffect_set); diff --git a/addons/rearm/functions/fnc_handleKilled.sqf b/addons/rearm/functions/fnc_handleKilled.sqf index 3e39dc727e..b0cdabaed5 100644 --- a/addons/rearm/functions/fnc_handleKilled.sqf +++ b/addons/rearm/functions/fnc_handleKilled.sqf @@ -15,9 +15,7 @@ */ #include "script_component.hpp" -params [ - ["_unit", objNull, [objNull]] -]; +params ["_unit"]; if (!local _unit) exitWith {}; diff --git a/addons/rearm/functions/fnc_handleUnconscious.sqf b/addons/rearm/functions/fnc_handleUnconscious.sqf index 0fbf1b8f06..f9d703f4ec 100644 --- a/addons/rearm/functions/fnc_handleUnconscious.sqf +++ b/addons/rearm/functions/fnc_handleUnconscious.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_unit", objNull, [objNull]], - ["_isUnconscious", false, [false]] -]; +params ["_unit", "_isUnconscious"]; if (!local _unit || {!_isUnconscious}) exitWith {}; diff --git a/addons/rearm/functions/fnc_hasEnoughSupply.sqf b/addons/rearm/functions/fnc_hasEnoughSupply.sqf index a2944db014..93f1f9ea3a 100644 --- a/addons/rearm/functions/fnc_hasEnoughSupply.sqf +++ b/addons/rearm/functions/fnc_hasEnoughSupply.sqf @@ -16,9 +16,7 @@ */ #include "script_component.hpp" -params [["_truck", objNull, [objNull]], ["_magazineClass", "", [""]]]; - -if (isNull _truck || {_magazineClass isEqualTo ""}) exitWith {false}; +params ["_truck", "_magazineClass"]; // With infinite supply, there is always enough if (GVAR(supply) == 0) exitWith {true}; diff --git a/addons/rearm/functions/fnc_makeDummy.sqf b/addons/rearm/functions/fnc_makeDummy.sqf index fbdd336a0d..574944a974 100644 --- a/addons/rearm/functions/fnc_makeDummy.sqf +++ b/addons/rearm/functions/fnc_makeDummy.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_obj", objNull, [objNull]], - ["_dirAndUp", [[1,0,0],[0,0,1]], [[]]] -]; +params ["_obj", "_dirAndUp"]; _obj setVectorDirAndUp _dirAndUp; _obj allowDamage false; diff --git a/addons/rearm/functions/fnc_moduleRearmSettings.sqf b/addons/rearm/functions/fnc_moduleRearmSettings.sqf index 2d498c8b57..61b6016c9d 100644 --- a/addons/rearm/functions/fnc_moduleRearmSettings.sqf +++ b/addons/rearm/functions/fnc_moduleRearmSettings.sqf @@ -1,6 +1,6 @@ /* * Author: GitHawk - * Module for adjusting the refuel settings. + * Module for adjusting the rearm settings. * * Arguments: * 0: The module logic @@ -28,4 +28,4 @@ if (!_activated) exitWith {}; [_logic, QGVAR(level), "level"] call EFUNC(common,readSettingFromModule); [_logic, QGVAR(supply), "supply"] call EFUNC(common,readSettingFromModule); -INFO_2("Module Initialized [level: %1][supply: %2]", GVAR(level), GVAR(supply)); +INFO_2("Module Initialized [level: %1][supply: %2]",GVAR(level),GVAR(supply)); diff --git a/addons/rearm/functions/fnc_pickUpAmmo.sqf b/addons/rearm/functions/fnc_pickUpAmmo.sqf index ee2845336b..0f47325a13 100644 --- a/addons/rearm/functions/fnc_pickUpAmmo.sqf +++ b/addons/rearm/functions/fnc_pickUpAmmo.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_dummy", objNull, [objNull]], - ["_unit", objNull, [objNull]] -]; +params ["_dummy", "_unit"]; private _attachedDummy = _unit getVariable [QGVAR(dummy), objNull]; if !(isNull _attachedDummy) exitWith {}; diff --git a/addons/rearm/functions/fnc_readSupplyCounter.sqf b/addons/rearm/functions/fnc_readSupplyCounter.sqf index cfcdb1b865..5f1e37b466 100644 --- a/addons/rearm/functions/fnc_readSupplyCounter.sqf +++ b/addons/rearm/functions/fnc_readSupplyCounter.sqf @@ -16,7 +16,7 @@ */ #include "script_component.hpp" -params [["_truck", objNull, [objNull]],["_unit", objNull, [objNull]]]; +params ["_truck", "_unit"]; TRACE_2("readSupplyCounter",_truck,_unit); if (GVAR(supply) == 0) exitWith {WARNING("Supply is unlimited");}; diff --git a/addons/rearm/functions/fnc_rearm.sqf b/addons/rearm/functions/fnc_rearm.sqf index 363a4905c7..bca193f621 100644 --- a/addons/rearm/functions/fnc_rearm.sqf +++ b/addons/rearm/functions/fnc_rearm.sqf @@ -16,7 +16,7 @@ */ #include "script_component.hpp" -params [["_target", objNull, [objNull]],["_unit", objNull, [objNull]]]; +params ["_target", "_unit"]; TRACE_2("rearm",_target,_unit); private _attachedDummy = _unit getVariable [QGVAR(dummy), objNull]; @@ -27,21 +27,24 @@ if (isNil "_magazineClass") exitWith {ERROR_1("magazineClass nil",_attachedDummy ([_magazineClass] call FUNC(getCaliber)) params ["_cal", "_idx"]; // Get magazines that can be rearmed -private _needRearmMags = [_target, _magazineClass] call FUNC(getNeedRearmMagazines); -_needRearmMags params ["_needRearm", "_turretPath", "_cnt", "_pylon"]; +private _needRearmMags = [_target] call FUNC(getNeedRearmMagazines); +private _needRearmMagsOfClass = _needRearmMags select {(_x select 0) isEqualTo _magazineClass}; // Exit if no magazines need rearming -if (!_needRearm) exitWith {ERROR_2("Could not find turret for %1 in %2", _magazineClass, typeOf _target);}; +if ((count _needRearmMagsOfClass) == 0) exitWith {ERROR_2("Could not find turret for %1 in %2",_magazineClass,typeOf _target);}; + +private _currentRearmableMag = _needRearmMagsOfClass select 0; +_currentRearmableMag params ["", "_turretPath", "", "_pylon", "", "_magazineCount"]; private _magazineDisplayName = getText(configFile >> "CfgMagazines" >> _magazineClass >> "displayName"); if (_magazineDisplayName == "") then { _magazineDisplayName = _magazineClass; - ERROR_1("Magazine is missing display name [%1]", _magazineClass); + ERROR_1("Magazine is missing display name [%1]",_magazineClass); }; [ TIME_PROGRESSBAR(REARM_DURATION_REARM select _idx), - [_target, _unit, _turretPath, _cnt, _magazineClass, (REARM_COUNT select _idx), _pylon], + [_target, _unit, _turretPath, _magazineCount, _magazineClass, (REARM_COUNT select _idx), _pylon], {(_this select 0) call FUNC(rearmSuccess)}, "", format [localize LSTRING(RearmAction), getText(configFile >> "CfgVehicles" >> (typeOf _target) >> "displayName"), _magazineDisplayName], diff --git a/addons/rearm/functions/fnc_rearmEntireVehicle.sqf b/addons/rearm/functions/fnc_rearmEntireVehicle.sqf index 8fddb4ab85..b0fe9f7ccf 100644 --- a/addons/rearm/functions/fnc_rearmEntireVehicle.sqf +++ b/addons/rearm/functions/fnc_rearmEntireVehicle.sqf @@ -17,11 +17,7 @@ */ #include "script_component.hpp" -params [ - ["_truck", objNull, [objNull]], - "", - ["_vehicle", objNull, [objNull]] -]; +params ["_truck", "", "_vehicle"]; [ TIME_PROGRESSBAR(10), diff --git a/addons/rearm/functions/fnc_rearmEntireVehicleSuccess.sqf b/addons/rearm/functions/fnc_rearmEntireVehicleSuccess.sqf index dda3ff9ca7..2ad70f9626 100644 --- a/addons/rearm/functions/fnc_rearmEntireVehicleSuccess.sqf +++ b/addons/rearm/functions/fnc_rearmEntireVehicleSuccess.sqf @@ -5,7 +5,7 @@ * Arguments: * 0: Rearm information * 0: Ammo Truck - * 1: Vehicle * * Return Value: * None @@ -18,10 +18,11 @@ #include "script_component.hpp" params ["_args"]; -_args params [["_truck", objNull, [objNull]], ["_vehicle", objNull, [objNull]]]; +_args params ["_truck", "_vehicle"]; TRACE_2("rearmEntireVehicleSuccess",_truck,_vehicle); if (isServer) then { + private _turrets = [_vehicle] call FUNC(getAllRearmTurrets); { private _turretOwnerID = _vehicle turretOwner _x; if (_turretOwnerID == 0) then { @@ -30,7 +31,7 @@ if (isServer) then { [QGVAR(rearmEntireVehicleSuccessLocalEH), [_truck, _vehicle, _x], _turretOwnerID] call CBA_fnc_ownerEvent; }; false - } count REARM_TURRET_PATHS; + } count _turrets; } else { [QGVAR(rearmEntireVehicleSuccessEH), _this] call CBA_fnc_serverEvent; }; diff --git a/addons/rearm/functions/fnc_rearmEntireVehicleSuccessLocal.sqf b/addons/rearm/functions/fnc_rearmEntireVehicleSuccessLocal.sqf index 01901fdf87..031c1d1f4d 100644 --- a/addons/rearm/functions/fnc_rearmEntireVehicleSuccessLocal.sqf +++ b/addons/rearm/functions/fnc_rearmEntireVehicleSuccessLocal.sqf @@ -1,5 +1,5 @@ /* - * Author: GitHawk + * Author: Tuupertunut * Rearm an entire turret locally. * * Arguments: @@ -17,60 +17,42 @@ */ #include "script_component.hpp" -params [["_truck", objNull, [objNull]], ["_vehicle", objNull, [objNull]], ["_turretPath", [], [[]]]]; +params ["_truck", "_vehicle", "_turretPath"]; TRACE_3("rearmEntireVehicleSuccessLocal",_truck,_vehicle,_turretPath); -// 1.70 pylons -private _pylonConfigs = configProperties [configFile >> "CfgVehicles" >> (typeOf _vehicle) >> "Components" >> "TransportPylonsComponent" >> "Pylons", "isClass _x"]; +// Fetching all rearmable magazines in this turret +private _magazines = ([_vehicle] call FUNC(getNeedRearmMagazines)) select {(_x select 1) isEqualTo _turretPath}; { - private _pylonTurret = getArray (_x >> "turret"); - if (_pylonTurret isEqualTo []) then {_pylonTurret = [-1];}; // convert to expected array for driver - if (_pylonTurret isEqualTo _turretPath) then { - private _pylonIndex = _forEachIndex + 1; // GJ BIS - private _pylonAmmo = _vehicle ammoOnPylon _pylonIndex; - private _pylonMagazine = (getPylonMagazines _vehicle) select _forEachIndex; - private _maxRounds = getNumber (configFile >> "CfgMagazines" >> _pylonMagazine >> "count"); - TRACE_4("",_pylonIndex,_pylonAmmo,_maxRounds,_pylonMagazine); - if (_pylonAmmo < _maxRounds) then { - if ((GVAR(supply) == 0) || {[_truck, _pylonMagazine, (_maxRounds - _pylonAmmo)] call FUNC(removeMagazineFromSupply)}) then { - TRACE_3("Adding Rounds",_vehicle,_pylonIndex,_maxRounds); - _vehicle setAmmoOnPylon [_pylonIndex, _maxRounds]; + _x params ["_magazineClass", "_magTurretPath", "_isPylonMag", "_pylonIndex", "_maxMagazines", "_currentMagazines", "_maxRoundsPerMag", "_currentRounds"]; + + // Array of planned ammo counts in every magazine after the rearm is complete + private _plannedRounds = +_currentRounds; + + // Trying to fill all existing magazines. + { + if (_x < _maxRoundsPerMag) then { + if ((GVAR(supply) == 0) || {[_truck, _magazineClass, (_maxRoundsPerMag - _x)] call FUNC(removeMagazineFromSupply)}) then { + _plannedRounds set [_forEachIndex, _maxRoundsPerMag]; }; }; - }; -} forEach _pylonConfigs; - -private _magazines = [_vehicle, _turretPath] call FUNC(getVehicleMagazines); -if (isNil "_magazines") exitWith {}; -{ - private _magazine = _x; - private _currentMagazines = { _x == _magazine } count (_vehicle magazinesTurret _turretPath); - private _maxMagazines = [_vehicle, _turretPath, _magazine] call FUNC(getMaxMagazines); - private _maxRounds = getNumber (configFile >> "CfgMagazines" >> _magazine >> "count"); - private _currentRounds = _vehicle magazineTurretAmmo [_magazine, _turretPath]; - - TRACE_7("Rearmed Turret",_vehicle,_turretPath,_currentMagazines,_maxMagazines,_currentRounds,_maxRounds,_magazine); - - if (_turretPath isEqualTo [-1] && _currentMagazines == 0) then { - // On driver, the empty magazine is still there, but is not returned by magazinesTurret - _currentMagazines = _currentMagazines + 1; - }; + } forEach _currentRounds; + + // Trying to add new full magazines, if there is space left. if (_currentMagazines < _maxMagazines) then { - private _success = true; - if ((GVAR(supply) == 0) || {[_truck, _magazine, (_maxRounds - _currentRounds)] call FUNC(removeMagazineFromSupply)}) then { - _vehicle setMagazineTurretAmmo [_magazine, _maxRounds, _turretPath]; - }; - for "_idx" from 1 to (_maxMagazines - _currentMagazines) do { - if ((GVAR(supply) == 0) || {[_truck, _magazine, _maxRounds] call FUNC(removeMagazineFromSupply)}) then { - _vehicle addMagazineTurret [_magazine, _turretPath]; + if ((GVAR(supply) == 0) || {[_truck, _magazineClass, _maxRoundsPerMag] call FUNC(removeMagazineFromSupply)}) then { + _plannedRounds pushBack _maxRoundsPerMag; }; }; - } else { - if ((GVAR(supply) == 0) || {[_truck, _magazine, (_maxRounds - _currentRounds)] call FUNC(removeMagazineFromSupply)}) then { - _vehicle setMagazineTurretAmmo [_magazine, _maxRounds, _turretPath]; - }; }; - false -} count _magazines; + + TRACE_2("rearming",_x,_plannedRounds); + + // Updating new ammo counts to vehicle. + if (_isPylonMag) then { + _vehicle setAmmoOnPylon [_pylonIndex, (_plannedRounds select 0)]; + } else { + [_vehicle, _magTurretPath, _magazineClass, _plannedRounds] call FUNC(setTurretMagazineAmmo); + }; +} forEach _magazines; diff --git a/addons/rearm/functions/fnc_rearmSuccess.sqf b/addons/rearm/functions/fnc_rearmSuccess.sqf index c609117002..0fc8219180 100644 --- a/addons/rearm/functions/fnc_rearmSuccess.sqf +++ b/addons/rearm/functions/fnc_rearmSuccess.sqf @@ -21,7 +21,7 @@ */ #include "script_component.hpp" -params [["_vehicle", objNull, [objNull]], ["_unit", objNull, [objNull]], "_turretPath", "_numMagazines", "_magazineClass", "_numRounds", "_pylon"]; +params ["_vehicle", "_unit", "_turretPath", "_numMagazines", "_magazineClass", "_numRounds", "_pylon"]; TRACE_7("rearmSuccess",_vehicle,_unit,_turretPath,_numMagazines,_magazineClass,_numRounds,_pylon); if (local _unit) then { diff --git a/addons/rearm/functions/fnc_setSupplyCount.sqf b/addons/rearm/functions/fnc_setSupplyCount.sqf index d9d5194550..ef99a78416 100644 --- a/addons/rearm/functions/fnc_setSupplyCount.sqf +++ b/addons/rearm/functions/fnc_setSupplyCount.sqf @@ -23,6 +23,6 @@ if !(EGVAR(common,settingsInitFinished)) exitWith { // only run this after the s params [["_truck", objNull, [objNull]], ["_supply", 0, [0]]]; if (GVAR(supply) != 1) exitWith {WARNING("supply setting is not set to limited");}; -if (isNull _truck) exitWith {WARNING_1("Truck is null [%1]", _truck);}; +if (isNull _truck) exitWith {WARNING_1("Truck is null [%1]",_truck);}; _truck setVariable [QGVAR(currentSupply), (_supply max 0), true]; diff --git a/addons/rearm/functions/fnc_setTurretMagazineAmmo.sqf b/addons/rearm/functions/fnc_setTurretMagazineAmmo.sqf new file mode 100644 index 0000000000..2cc5e156d5 --- /dev/null +++ b/addons/rearm/functions/fnc_setTurretMagazineAmmo.sqf @@ -0,0 +1,89 @@ +/* + * Author: Tuupertunut + * Sets the ammo counts of all magazines of given class in turret. + * + * BIS command "setMagazineTurretAmmo" is broken at the time of writing (2017-06-24) + * (https://feedback.bistudio.com/T79689). This function is intended as a workaround for it. All + * magazines are removed and then added again with updated ammo counts. + * Note: As an unintended side effect, the turret reloads after running this function. + * + * Arguments: + * 0: Vehicle + * 1: Turret Path + * 2: Magazine Classname + * 3: Ammo Counts in Magazines + * + * Return Value: + * None + * + * Example: + * [vehicle, [0], "200Rnd_127x99_mag_Tracer_Red", [200, 152]] call ace_rearm_fnc_setTurretMagazineAmmo + * + * Public: No + */ +#include "script_component.hpp" + +params ["_vehicle", "_turretPath", "_magazineClass", "_ammoCounts"]; + +// Checking if a magazine of given class is currently loaded in any weapon. +private _magLoadedInWeapon = false; +private _loadedWeapon = ""; +{ + private _currentlyLoadedMag = (weaponState [_vehicle, _turretPath, _x]) select 3; + + if (_currentlyLoadedMag isEqualTo _magazineClass) exitWith { + _magLoadedInWeapon = true; + _loadedWeapon = _x; + }; +} forEach (_vehicle weaponsTurret _turretPath); + + +if (!_magLoadedInWeapon) then { + /* The easy case: + * The magazine class was not loaded, so we can just remove those magazines and + * add them back with updated ammo counts. */ + + _vehicle removeMagazinesTurret [_magazineClass, _turretPath]; + { + _vehicle addMagazineTurret [_magazineClass, _turretPath, _x]; + } forEach _ammoCounts; + +} else { + /* Special hack case: + * The magazine class was loaded into a weapon. If the weapon has more than one type of + * magazine (e.g. AP and HEAT in a cannon), then removing all magazines would trigger the + * weapon to load a different magazine type. For example, removing the HEAT shells while HEAT + * is loaded makes the cannon switch to AP. + * + * To prevent that, we must remove all magazines that would fit into the weapon and then add + * them back with the magazine-to-be-loaded being the first. */ + + private _allowedMagClassesInWeapon = getArray (configFile >> "CfgWeapons" >> _loadedWeapon >> "magazines"); + + /* Current ammo counts of all allowed magazine classes in weapon. + * Example: [["8Rnd_82mm_Mo_shells", [8, 8, 2]], ["8Rnd_82mm_Mo_Flare_white", [7]]] */ + private _ammoCountsByMagClass = _allowedMagClassesInWeapon apply {[_x, ([_vehicle, _turretPath, _x] call FUNC(getTurretMagazineAmmo))]}; + + // Removing all magazines that fit into the weapon. + { + _vehicle removeMagazinesTurret [_x, _turretPath]; + } forEach _allowedMagClassesInWeapon; + + // Adding the mags of the given class first with updated ammo counts. + { + _vehicle addMagazineTurret [_magazineClass, _turretPath, _x]; + } forEach _ammoCounts; + + // Adding back all other magazines with their original ammo counts. + { + _x params ["_loopMagClass", "_loopAmmoCounts"]; + + if (!(_loopMagClass isEqualTo _magazineClass)) then { + { + _vehicle addMagazineTurret [_loopMagClass, _turretPath, _x]; + } forEach _loopAmmoCounts; + }; + } forEach _ammoCountsByMagClass; +}; + +TRACE_5("setTurretMagazineAmmo",_vehicle,_turretPath,_magazineClass,_ammoCounts,_loadedWeapon); diff --git a/addons/rearm/functions/fnc_storeAmmo.sqf b/addons/rearm/functions/fnc_storeAmmo.sqf index c056421bba..0da99c081a 100644 --- a/addons/rearm/functions/fnc_storeAmmo.sqf +++ b/addons/rearm/functions/fnc_storeAmmo.sqf @@ -16,10 +16,7 @@ */ #include "script_component.hpp" -params [ - ["_truck", objNull, [objNull]], - ["_unit", objNull, [objNull]] -]; +params ["_truck", "_unit"]; private _attachedDummy = _unit getVariable [QGVAR(dummy), objNull]; if (isNull _attachedDummy) exitwith {}; diff --git a/addons/rearm/functions/fnc_takeAmmo.sqf b/addons/rearm/functions/fnc_takeAmmo.sqf index 23f19fedf4..69db94729d 100644 --- a/addons/rearm/functions/fnc_takeAmmo.sqf +++ b/addons/rearm/functions/fnc_takeAmmo.sqf @@ -19,11 +19,7 @@ */ #include "script_component.hpp" -params [ - ["_truck", objNull, [objNull]], - ["_unit", objNull, [objNull]], - ["_args", ["", objNull], [[]]] -]; +params ["_truck", "_unit", "_args"]; _args params ["_magazineClass", "_vehicle"]; TRACE_5("takeAmmo",_truck,_unit,_args,_magazineClass,_vehicle); diff --git a/addons/rearm/functions/fnc_takeSuccess.sqf b/addons/rearm/functions/fnc_takeSuccess.sqf index e458036176..e9d8b1daaa 100644 --- a/addons/rearm/functions/fnc_takeSuccess.sqf +++ b/addons/rearm/functions/fnc_takeSuccess.sqf @@ -18,7 +18,7 @@ */ #include "script_component.hpp" -params [["_args", [objNull, "", objNull], [[]], 3]]; +params ["_args"]; _args params ["_unit", "_magazineClass", "_truck"]; TRACE_3("takeSuccess",_unit,_magazineClass,_truck); diff --git a/addons/rearm/script_component.hpp b/addons/rearm/script_component.hpp index 3edfeda3f1..92e279950c 100644 --- a/addons/rearm/script_component.hpp +++ b/addons/rearm/script_component.hpp @@ -19,7 +19,6 @@ #define REARM_ACTION_DISTANCE 7 -#define REARM_TURRET_PATHS [[-1], [0], [0,0], [0,1], [1], [2], [0,2]] #define REARM_CALIBERS [ 6, 7, 8, 13, 19, 20, 25, 30, 35, 39, 40, 60, 70, 80, 82, 100, 105, 120, 122, 125, 155, 230, 250] #define REARM_DURATION_TAKE [ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 13, 10]