2023-09-12 18:58:10 +00:00
#include "..\script_component.hpp"
2015-08-13 17:33:55 +00:00
/*
2023-02-17 02:06:11 +00:00
* Author: GitHawk, QuantX
2015-08-20 20:10:26 +00:00
* Refuels the vehicle.
2015-08-13 17:33:55 +00:00
*
* Arguments:
2015-08-20 20:10:26 +00:00
* 0: Unit <OBJECT>
2017-09-10 19:43:03 +00:00
* 1: Vehicle <OBJECT>
2015-08-20 20:10:26 +00:00
* 2: Nozzle <OBJECT>
2016-02-02 14:03:06 +00:00
* 3: Connection Point <ARRAY>
2015-08-13 17:33:55 +00:00
*
* Return Value:
2015-08-14 01:18:54 +00:00
* None
2015-08-13 17:33:55 +00:00
*
2017-06-08 13:31:51 +00:00
* Example:
* [bob, kevin, nozzle, [2, 1, 5]] call ace_refuel_fnc_refuel
*
2015-08-13 17:33:55 +00:00
* Public: No
*/
2017-09-10 19:43:03 +00:00
params [["_unit", objNull, [objNull]], ["_sink", objNull, [objNull]], ["_nozzle", objNull, [objNull]], ["_connectToPoint", [0,0,0], [[]], 3]];
2015-08-13 17:33:55 +00:00
2021-02-18 18:58:08 +00:00
private _config = configOf _sink;
2023-02-17 02:06:11 +00:00
private _rate = if (isNumber (_config >> QGVAR(flowRate))) then {
getNumber (_config >> QGVAR(flowRate)) * GVAR(rate)
} else {
// Jerry cans for example have no flow rate defined, default to 1
GVAR(rate)
};
2016-10-18 18:21:28 +00:00
2023-02-17 02:06:11 +00:00
// How much fuel is in a vehicle's fuel tank
private _maxFuelTank = getNumber (_config >> QGVAR(fuelCapacity));
2016-10-18 18:21:28 +00:00
// Fall back to vanilla fuelCapacity value (only air and sea vehicles don't have this defined by default by us)
// Air and sea vehicles have that value properly defined in liters, unlike ground vehicles which is is formula of (range * tested factor) - different fuel consumption system than ground vehicles
2023-02-17 02:06:11 +00:00
if (_maxFuelTank == 0) then {
_maxFuelTank = getNumber (_config >> "fuelCapacity");
2016-10-18 18:21:28 +00:00
};
2015-08-15 14:49:57 +00:00
2015-08-16 23:57:05 +00:00
[{
2015-08-13 17:33:55 +00:00
params ["_args", "_pfID"];
2023-02-17 02:06:11 +00:00
_args params [["_source", objNull, [objNull]], ["_sink", objNull, [objNull]], ["_unit", objNull, [objNull]], ["_nozzle", objNull, [objNull]], ["_rate", 1, [0]], ["_maxFuelTank", 1, [0]], ["_connectFromPoint", [0,0,0], [[]], 3], ["_connectToPoint", [0,0,0], [[]], 3]];
2015-08-16 23:57:05 +00:00
2015-12-12 14:12:50 +00:00
if !(_nozzle getVariable [QGVAR(isConnected), false]) exitWith {
2016-01-01 02:51:44 +00:00
[_pfID] call CBA_fnc_removePerFrameHandler;
2015-12-11 21:45:28 +00:00
};
2023-02-17 02:06:11 +00:00
// Quit if target or fuel tank got destroyed
2015-08-20 20:10:26 +00:00
if (!alive _source || {!alive _sink}) exitWith {
2023-02-17 02:06:11 +00:00
[objNull, _nozzle] call FUNC(disconnect);
2016-07-24 18:11:35 +00:00
[_pfID] call CBA_fnc_removePerFrameHandler;
2015-08-13 17:33:55 +00:00
};
2023-02-17 02:06:11 +00:00
// Quit if hose distance was exceeded
2017-05-31 16:30:48 +00:00
private _hoseLength = _source getVariable [QGVAR(hoseLength), GVAR(hoseLength)];
private _tooFar = ((_sink modelToWorld _connectToPoint) distance (_source modelToWorld _connectFromPoint)) > (_hoseLength - 2);
2015-12-21 16:54:52 +00:00
if (_tooFar && {!(_nozzle getVariable [QGVAR(jerryCan), false])}) exitWith {
2015-08-18 21:18:32 +00:00
[LSTRING(Hint_TooFar), 2, _unit] call EFUNC(common,displayTextStructured);
2023-02-17 02:06:11 +00:00
[objNull, _nozzle] call FUNC(disconnect);
2016-07-24 18:11:35 +00:00
[_pfID] call CBA_fnc_removePerFrameHandler;
2015-08-13 17:33:55 +00:00
};
2015-08-18 21:18:32 +00:00
2023-02-17 02:06:11 +00:00
// Main fueling process
2016-02-02 14:03:06 +00:00
private _finished = false;
private _fueling = _nozzle getVariable [QGVAR(isRefueling), false];
2015-08-18 21:18:32 +00:00
if (_fueling) then {
2023-02-17 02:06:11 +00:00
private _refuelContainer = _nozzle getVariable [QGVAR(refuelContainer), false];
// Use special cargo refuel rate when refueling containers
// TODO: Add flow dedicated input/output flow rates for every container and use the lower of the two instead
if (_refuelContainer) then {_rate = GVAR(cargoRate)};
2016-07-24 18:11:35 +00:00
// Calculate rate using mission time to take time acceleration and pause into account
2023-02-17 02:06:11 +00:00
private _addedFuel = _rate * (CBA_missionTime - (_nozzle getVariable [QGVAR(lastTickMissionTime), CBA_missionTime]));
2016-07-24 18:11:35 +00:00
_nozzle setVariable [QGVAR(lastTickMissionTime), CBA_missionTime];
2023-02-17 02:06:11 +00:00
// Figure out exactly how much fuel to transfer while being sure not to take too much from source
private _fuelInSource = [_source] call FUNC(getFuel);
if (([_source] call FUNC(getCapacity)) != REFUEL_INFINITE_FUEL) then {
if (_addedFuel > _fuelInSource) then {
_addedFuel = _fuelInSource;
2021-10-11 05:47:54 +00:00
_fuelInSource = 0;
2023-02-17 02:06:11 +00:00
_finished = true;
[LSTRING(Hint_SourceEmpty), 2, _unit] call EFUNC(common,displayTextStructured);
2021-10-11 05:47:54 +00:00
} else {
2023-02-17 02:06:11 +00:00
_fuelInSource = _fuelInSource - _addedFuel;
2021-10-11 05:47:54 +00:00
};
2015-08-18 21:18:32 +00:00
};
2023-02-17 02:06:11 +00:00
private _fuelInSink = (if (_refuelContainer) then {
[_sink] call FUNC(getFuel)
} else {
// How full the gas tank is. We keep our own record, since `fuel _sink` doesn't update quick enough
(_nozzle getVariable [QGVAR(tempFuel), fuel _sink]) * _maxFuelTank
}) + _addedFuel;
// Add fuel to target while being sure not to put too much into sink
private _maxFuelContainer = [_sink] call FUNC(getCapacity);
private _maxFuel = [_maxFuelTank, _maxFuelContainer] select _refuelContainer;
if (_fuelInSink >= _maxFuel) then {
// Put any extra fuel back
_fuelInSource = _fuelInSource + (_fuelInSink - _maxFuel);
_addedFuel = _maxFuel - _fuelInSink;
// We're done
_fuelInSink = _maxFuel;
2015-08-18 21:18:32 +00:00
_finished = true;
[LSTRING(Hint_Completed), 2, _unit] call EFUNC(common,displayTextStructured);
};
2023-02-17 02:06:11 +00:00
if (_refuelContainer) then {
[_sink, _fuelInSink] call FUNC(setFuel);
} else {
private _fillRatio = _fuelInSink / _maxFuelTank;
[QEGVAR(common,setFuel), [_sink, _fillRatio], _sink] call CBA_fnc_targetEvent;
_nozzle setVariable [QGVAR(tempFuel), _fillRatio];
};
// Increment fuel counter
_source setVariable [QGVAR(fuelCounter), (_source getVariable [QGVAR(fuelCounter), 0]) + _addedFuel, true];
2015-08-22 11:34:24 +00:00
2023-02-17 02:06:11 +00:00
[QGVAR(tick), [_source, _sink, _addedFuel, _refuelContainer]] call CBA_fnc_localEvent;
2019-10-13 18:08:35 +00:00
2015-08-21 20:43:45 +00:00
[_source, _fuelInSource] call FUNC(setFuel);
2015-08-22 11:34:24 +00:00
} else {
2023-02-17 02:06:11 +00:00
_nozzle setVariable [QGVAR(tempFuel), fuel _sink];
2015-08-13 17:33:55 +00:00
};
2015-08-16 23:57:05 +00:00
2023-02-17 02:06:11 +00:00
// Reset variables when done
if (_finished) then {
2019-10-13 18:08:35 +00:00
[QGVAR(stopped), [_source, _sink]] call CBA_fnc_localEvent;
2016-07-24 18:11:35 +00:00
_nozzle setVariable [QGVAR(lastTickMissionTime), nil];
2015-08-17 22:42:35 +00:00
_nozzle setVariable [QGVAR(isRefueling), false, true];
2015-08-13 17:33:55 +00:00
};
2016-07-24 18:11:35 +00:00
}, 1, [
_nozzle getVariable QGVAR(source),
2017-09-10 19:43:03 +00:00
_sink,
2015-08-20 20:10:26 +00:00
_unit,
_nozzle,
_rate,
2023-02-17 02:06:11 +00:00
_maxFuelTank,
2015-08-20 20:10:26 +00:00
_nozzle getVariable [QGVAR(attachPos), [0,0,0]],
2016-07-24 18:11:35 +00:00
_connectToPoint
]] call CBA_fnc_addPerFrameHandler;