fnc_startRefuel.sqf 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. params["_vehicle"];
  2. private _rate = 10;
  3. private _attached = _vehicle getVariable["RD501_mc_attached_large", objNull];
  4. _vehicle setVariable["RD501_mc_stop_refuel", false, true];
  5. _maxFuelSource = getNumber (configFile >> "CfgVehicles" >> typeOf _vehicle >> "fuelCapacity");
  6. _maxFuelTarget = getNumber (configFile >> "CfgVehicles" >> typeOf _attached >> "fuelCapacity");
  7. private _firstTick = 0;
  8. if (isMultiplayer) then
  9. {
  10. _firstTick = serverTime;
  11. } else
  12. {
  13. _firstTick = time;
  14. };
  15. hint "Refueling started";
  16. _vehicle setVariable["RD501_mc_lastRefuelTick", _firstTick, true];
  17. _vehicle setVariable["RD501_mc_nextRefuelUpdate", 0.1, true];
  18. [{
  19. params ["_args", "_pfID"];
  20. _args params ["_target", "_source", "_rate", "_maxFuelSource", "_maxFuelTarget"];
  21. if (!alive _source || {!alive _sink}) then
  22. {
  23. _source setVariable["RD501_mc_lastRefuelTick", nil, true];
  24. _source setVariable["RD501_mc_nextRefuelUpdate", nil, true];
  25. _source setVariable["RD501_mc_lastTargetFuelAmount", nil, true];
  26. [_pfID] call CBA_fnc_removePerFrameHandler;
  27. };
  28. if (_source getVariable["RD501_mc_stop_refuel", false]) then
  29. {
  30. hint "Refueling stopped";
  31. _source setVariable["RD501_mc_lastRefuelTick", nil, true];
  32. _source setVariable["RD501_mc_nextRefuelUpdate", nil, true];
  33. _source setVariable["RD501_mc_lastTargetFuelAmount", nil, true];
  34. [_pfID] call CBA_fnc_removePerFrameHandler;
  35. };
  36. private _currentTime = 0;
  37. if (isMultiplayer) then
  38. {
  39. _currentTime = serverTime;
  40. } else
  41. {
  42. _currentTime = time;
  43. };
  44. private _target_fuel = (fuel _target) * _maxFuelTarget;
  45. private _source_fuel = (fuel _source) * _maxFuelSource;
  46. // if last ticks have not been applied yet, act as if they have
  47. private _last_target_fuel = (_source getVariable["RD501_mc_lastTargetFuelAmount", 0]) * _maxFuelTarget;
  48. if (_last_target_fuel > _target_fuel) then
  49. {
  50. _target_fuel = _last_target_fuel;
  51. };
  52. private _deltaT = _currentTime - (_source getVariable["RD501_mc_lastRefuelTick", 0]);
  53. private _transfer = _rate * _deltaT;
  54. private _target_new_fuel = ((_target_fuel + _transfer) / _maxFuelTarget);
  55. private _source_new_fuel = ((_source_fuel - _transfer) / _maxFuelSource);
  56. private _last_update = _source getVariable["RD501_mc_nextRefuelUpdate", 0];
  57. if (_target_new_fuel >= 1.0) then
  58. {
  59. _target_new_fuel = 1.0;
  60. hint "Refueling complete";
  61. _source setVariable["RD501_mc_lastRefuelTick", nil, true];
  62. _source setVariable["RD501_mc_nextRefuelUpdate", nil, true];
  63. _source setVariable["RD501_mc_lastTargetFuelAmount", nil, true];
  64. [_pfID] call CBA_fnc_removePerFrameHandler;
  65. } else
  66. {
  67. if (_target_new_fuel >= _last_update) then
  68. {
  69. _last_update = round(_target_new_fuel * 10) / 10;
  70. hint format ["Refueling at %1 percent", str (_last_update*100)];
  71. _source setVariable["RD501_mc_nextRefuelUpdate", _last_update+0.1, true];
  72. };
  73. };
  74. ["RD501_mc_set_fuel", _target_new_fuel, _target] call CBA_fnc_targetEvent;
  75. _source setVariable["RD501_mc_lastTargetFuelAmount", _target_new_fuel, true];
  76. _source setFuel _source_new_fuel;
  77. _source setVariable["RD501_mc_lastRefuelTick", _currentTime, true];
  78. }, 1, [_attached, _vehicle, _rate, _maxFuelSource, _maxFuelTarget]] call CBA_fnc_addPerFrameHandler;