2018-09-17 19:19:29 +00:00
#include "script_component.hpp"
2015-08-13 17:33:55 +00:00
/*
* Author: GitHawk
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;
2016-10-18 18:21:28 +00:00
private _rate = getNumber (_config >> QGVAR(flowRate)) * GVAR(rate);
private _maxFuel = getNumber (_config >> QGVAR(fuelCapacity));
// 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
if (_maxFuel == 0) then {
_maxFuel = getNumber (_config >> "fuelCapacity");
};
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"];
2016-02-02 14:03:06 +00:00
_args params [["_source", objNull, [objNull]], ["_sink", objNull, [objNull]], ["_unit", objNull, [objNull]], ["_nozzle", objNull, [objNull]], ["_rate", 1, [0]], ["_startFuel", 0, [0]], ["_maxFuel", 0, [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
};
2015-08-20 20:10:26 +00:00
if (!alive _source || {!alive _sink}) exitWith {
2015-08-22 11:34:24 +00:00
[objNull, _nozzle] call FUNC(dropNozzle);
2015-08-18 14:09:59 +00:00
_nozzle setVariable [QGVAR(isConnected), false, true];
2017-09-16 09:36:42 +00:00
if (_nozzle isKindOf "Land_CanisterFuel_F") then { _nozzle setVariable [QEGVAR(cargo,canLoad), true, true]; };
2017-09-10 19:43:03 +00:00
_nozzle setVariable [QGVAR(sink), nil, true];
_sink setVariable [QGVAR(nozzle), nil, true];
2016-07-24 18:11:35 +00:00
[_pfID] call CBA_fnc_removePerFrameHandler;
2015-08-13 17:33:55 +00:00
};
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);
2015-08-22 11:34:24 +00:00
[objNull, _nozzle] call FUNC(dropNozzle);
2015-08-18 21:18:32 +00:00
_nozzle setVariable [QGVAR(isConnected), false, true];
2017-09-16 09:36:42 +00:00
if (_nozzle isKindOf "Land_CanisterFuel_F") then { _nozzle setVariable [QEGVAR(cargo,canLoad), true, true]; };
2017-09-10 19:43:03 +00:00
_nozzle setVariable [QGVAR(sink), nil, true];
_sink setVariable [QGVAR(nozzle), nil, true];
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
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 {
2016-02-02 14:03:06 +00:00
private _fuelInSource = [_source] call FUNC(getFuel);
2015-08-18 21:18:32 +00:00
if (_fuelInSource == 0) exitWith {
[LSTRING(Hint_SourceEmpty), 2, _unit] call EFUNC(common,displayTextStructured);
2016-07-24 18:11:35 +00:00
_nozzle setVariable [QGVAR(lastTickMissionTime), nil];
2015-08-18 21:18:32 +00:00
_nozzle setVariable [QGVAR(isRefueling), false, true];
};
2016-07-24 18:11:35 +00:00
// Calculate rate using mission time to take time acceleration and pause into account
private _rateTime = _rate * (CBA_missionTime - (_nozzle getVariable [QGVAR(lastTickMissionTime), CBA_missionTime]));
_nozzle setVariable [QGVAR(lastTickMissionTime), CBA_missionTime];
2021-10-11 05:47:54 +00:00
if (_fuelInSource != REFUEL_INFINITE_FUEL) then {
if (_rateTime > _fuelInSource) then {
_rateTime = _fuelInSource;
_fuelInSource = 0;
} else {
_fuelInSource = _fuelInSource - _rateTime;
};
2016-02-26 15:35:34 +00:00
} else {
2016-07-24 18:11:35 +00:00
_source setVariable [QGVAR(fuelCounter), (_source getVariable [QGVAR(fuelCounter), 0]) + _rateTime, true];
2015-08-18 21:18:32 +00:00
};
2021-10-11 05:47:54 +00:00
if (_fuelInSource <= 0 && {_fuelInSource != REFUEL_INFINITE_FUEL}) then {
2015-08-18 21:18:32 +00:00
_fuelInSource = 0;
2021-10-11 05:47:54 +00:00
[_source, _fuelInSource] call FUNC(setFuel);
2015-08-18 21:18:32 +00:00
_finished = true;
[LSTRING(Hint_SourceEmpty), 2, _unit] call EFUNC(common,displayTextStructured);
};
2015-08-16 23:57:05 +00:00
2016-07-24 18:11:35 +00:00
private _fuelInSink = (_unit getVariable [QGVAR(tempFuel), _startFuel]) + ( _rateTime / _maxFuel);
2015-08-18 21:18:32 +00:00
if (_fuelInSink > 1) then {
_fuelInSink = 1;
_finished = true;
[LSTRING(Hint_Completed), 2, _unit] call EFUNC(common,displayTextStructured);
};
2015-08-22 11:34:24 +00:00
_unit setVariable [QGVAR(tempFuel), _fuelInSink];
2019-10-13 18:08:35 +00:00
[QGVAR(tick), [_source, _sink, _rateTime]] call CBA_fnc_localEvent;
2016-06-04 10:12:56 +00:00
[QEGVAR(common,setFuel), [_sink, _fuelInSink], _sink] call CBA_fnc_targetEvent;
2015-08-21 20:43:45 +00:00
[_source, _fuelInSource] call FUNC(setFuel);
2015-08-22 11:34:24 +00:00
} else {
_unit setVariable [QGVAR(tempFuel), fuel _sink];
2015-08-13 17:33:55 +00:00
};
2015-08-16 23:57:05 +00:00
2015-08-20 20:10:26 +00:00
if (_finished) exitWith {
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,
2017-09-10 19:43:03 +00:00
fuel _sink,
2015-08-20 20:10:26 +00:00
_maxFuel,
_nozzle getVariable [QGVAR(attachPos), [0,0,0]],
2016-07-24 18:11:35 +00:00
_connectToPoint
]] call CBA_fnc_addPerFrameHandler;