Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_FuelConsumptionComponent.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/ScriptWizard", description: "SCR_FuelConsumptionComponent", color: "0 0 255 255")]
3 {
4  [Attribute( defvalue: "1", uiwidget: UIWidgets.CheckBox, desc: "Automatic fuel tank switching. Automatically select next fuel tank upon depletion of current fuel tank." )]
5  bool m_bAutomaticFuelTankSwitching;
6 
7  [Attribute( defvalue: "20", uiwidget: UIWidgets.Auto, desc: "Fuel consumption at max power RPM\n[liters/hour]" )]
8  float m_fFuelConsumption;
9 
10  [Attribute( defvalue: "0.5", uiwidget: UIWidgets.Auto, desc: "Fuel consumption idle\n[liters/hour]" )]
11  float m_fFuelConsumptionIdle;
12 }
13 
14 class SCR_FuelConsumptionComponent : ScriptGameComponent
15 {
16  protected const float SECOND_TO_HOUR = 1/3600;
17  protected const float MIN_FUEL = 0;
18  protected const float MIN_THRUST = 0.04;
19  protected const float MIN_CLUTCH = 0.04;
20  protected const float NEUTRAL_GEAR = 1;
21  protected const float TIME_STEP = 1;
22 
23  protected bool m_bConnectedToSystem = false;
24 
25  protected float m_fTimeDelta;
27  protected VehicleBaseSimulation m_Simulation;
29 
31  protected static float s_fGlobalFuelConsumptionScale = 8;
32 
33  //------------------------------------------------------------------------------------------------
37  static void SetGlobalFuelConsumptionScale(float globalFuelConsumptionScale, int playerThatChangedValue = -1)
38  {
39  if (s_fGlobalFuelConsumptionScale == globalFuelConsumptionScale || globalFuelConsumptionScale < 0)
40  return;
41 
43  if (!gameMode || gameMode.IsMaster())
44  {
45  //~ Send notification when fuel consumption scale is changed and player Id is given
46  if (playerThatChangedValue > 0)
47  SCR_NotificationsComponent.SendToUnlimitedEditorPlayers(ENotification.EDITOR_CHANGED_FUEL_CONSUMPTION_SCALE, playerThatChangedValue, globalFuelConsumptionScale * 100);
48 
49  s_fGlobalFuelConsumptionScale = globalFuelConsumptionScale;
50  }
51  }
52 
53  //------------------------------------------------------------------------------------------------
55  static float GetGlobalFuelConsumptionScale()
56  {
57  return s_fGlobalFuelConsumptionScale;
58  }
59 
60  //------------------------------------------------------------------------------------------------
63  void SetCurrentFuelTank(int iFuelTankID)
64  {
65  m_CurrentFuelTank = FindFuelTankByID(iFuelTankID);
66  }
67 
68  //------------------------------------------------------------------------------------------------
71  {
72  return m_CurrentFuelTank;
73  }
74 
75  //------------------------------------------------------------------------------------------------
79  protected BaseFuelNode FindFuelTankByID(int iFuelTankID)
80  {
81  FuelManagerComponent fuelManager = FuelManagerComponent.Cast(GetOwner().FindComponent(FuelManagerComponent));
82  if (!fuelManager)
83  return null;
84 
85  array<BaseFuelNode> nodes = {};
86  fuelManager.GetFuelNodesList(nodes);
87  SCR_FuelNode scrNode;
88  foreach (BaseFuelNode node: nodes)
89  {
90  // Currently ID is limited to SCR_FuelNode
91  scrNode = SCR_FuelNode.Cast(node);
92  if (scrNode && scrNode.GetFuelTankID() == iFuelTankID)
93  return scrNode;
94  }
95 
96  return null;
97  }
98 
99  //------------------------------------------------------------------------------------------------
102  {
103  FuelManagerComponent fuelManager = FuelManagerComponent.Cast(GetOwner().FindComponent(FuelManagerComponent));
104  if (!fuelManager)
105  return null;
106 
107  array<BaseFuelNode> nodes = {};
108  fuelManager.GetFuelNodesList(nodes);
109  foreach (BaseFuelNode node: nodes)
110  {
111  if (node && node.GetFuel() > MIN_FUEL)
112  return node;
113  }
114 
115  return null;
116  }
117 
118  //------------------------------------------------------------------------------------------------
120  void SetEnabled(bool enabled)
121  {
122  if (!m_ComponentData)
123  return;
124 
125  // Irrelevant on proxies
126  RplComponent rpl = RplComponent.Cast(GetOwner().FindComponent(RplComponent));
127  if (rpl && rpl.IsProxy())
128  return;
129 
130  if (enabled && m_bConnectedToSystem)
131  m_fTimeDelta = 0;
132 
133  if (m_Simulation)
134  Update();
135  else
136  enabled = false;
137 
138  if (enabled)
140  else
142  }
143 
144  //------------------------------------------------------------------------------------------------
146  void Update()
147  {
148  // if there is fuel in the actual tank
149  if (!m_CurrentFuelTank || m_CurrentFuelTank.GetFuel() <= MIN_FUEL)
150  {
151  // automatic switching not disabled
152  if (m_ComponentData.m_bAutomaticFuelTankSwitching)
154  else
155  m_CurrentFuelTank = null;
156  }
157 
158  // vehicle has at least one fuel tank
159  if (m_CurrentFuelTank)
160  {
161  float currentConsumption;
162 
164  {
165  VehicleWheeledSimulation wheeledSimulation = VehicleWheeledSimulation.Cast(m_Simulation);
166  if (wheeledSimulation)
167  {
168  float throttle = wheeledSimulation.GetThrottle();
169  if (throttle >= MIN_THRUST && wheeledSimulation.GetGear() != NEUTRAL_GEAR && wheeledSimulation.GetClutch() >= MIN_CLUTCH)
170  currentConsumption = m_ComponentData.m_fFuelConsumption * throttle * wheeledSimulation.EngineGetRPM() / wheeledSimulation.EngineGetRPMPeakTorque();
171  else if (throttle >= MIN_THRUST)
172  currentConsumption = m_ComponentData.m_fFuelConsumptionIdle * wheeledSimulation.EngineGetRPM() / wheeledSimulation.EngineGetRPMIdle();
173  else
174  currentConsumption = m_ComponentData.m_fFuelConsumptionIdle;
175  }
176 
177  VehicleHelicopterSimulation helicopterSimulation = VehicleHelicopterSimulation.Cast(m_Simulation);
178  if (helicopterSimulation)
179  {
180  currentConsumption = m_ComponentData.m_fFuelConsumption;
181  }
182 
183  // Scale and convert consumption rate per hour to per second
184  float consumptionScale = m_fTimeDelta * s_fGlobalFuelConsumptionScale * SECOND_TO_HOUR;
185 
186  float newFuel = m_CurrentFuelTank.GetFuel() - currentConsumption * consumptionScale;
187 
188  #ifdef ENABLE_DIAG
189  if (wheeledSimulation && currentConsumption > 0 && DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_VEHICLES_FUEL_CONSUMPTION))
190  {
191  float throttle = wheeledSimulation.GetThrottle();
192  float speed = wheeledSimulation.GetSpeedKmh();
193  float range;
194 
195  if (s_fGlobalFuelConsumptionScale != 0 && currentConsumption != 0)
196  range = speed * newFuel / (currentConsumption * s_fGlobalFuelConsumptionScale);
197  else
198  range = -1;
199 
200  float litersPer100KM;
201  if (speed > 0)
202  litersPer100KM = currentConsumption * 100 / speed;
203 
204  Print(throttle);
205  Print(speed);
206  Print(currentConsumption);
207  Print(newFuel);
208  Print(range);
209  Print(litersPer100KM);
210  }
211  #endif // ENABLE_DIAG
212  m_CurrentFuelTank.SetFuel(newFuel);
213  }
214  else
215  {
216  VehicleWheeledSimulation_SA wheeledSimulation = VehicleWheeledSimulation_SA.Cast(m_Simulation);
217  if (wheeledSimulation)
218  {
219  float throttle = wheeledSimulation.GetThrottle();
220  if (throttle >= MIN_THRUST && wheeledSimulation.GetGear() != NEUTRAL_GEAR && wheeledSimulation.GetClutch() >= MIN_CLUTCH)
221  currentConsumption = m_ComponentData.m_fFuelConsumption * throttle * wheeledSimulation.EngineGetRPM() / wheeledSimulation.EngineGetRPMPeakTorque();
222  else if (throttle >= MIN_THRUST)
223  currentConsumption = m_ComponentData.m_fFuelConsumptionIdle * wheeledSimulation.EngineGetRPM() / wheeledSimulation.EngineGetRPMIdle();
224  else
225  currentConsumption = m_ComponentData.m_fFuelConsumptionIdle;
226  }
227 
228  VehicleHelicopterSimulation helicopterSimulation = VehicleHelicopterSimulation.Cast(m_Simulation);
229  if (helicopterSimulation)
230  {
231  currentConsumption = m_ComponentData.m_fFuelConsumption;
232  }
233 
234  // Scale and convert consumption rate per hour to per second
235  float consumptionScale = m_fTimeDelta * s_fGlobalFuelConsumptionScale * SECOND_TO_HOUR;
236 
237  float newFuel = m_CurrentFuelTank.GetFuel() - currentConsumption * consumptionScale;
238 
239  #ifdef ENABLE_DIAG
240  if (wheeledSimulation && currentConsumption > 0 && DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_VEHICLES_FUEL_CONSUMPTION))
241  {
242  float throttle = wheeledSimulation.GetThrottle();
243  float speed = wheeledSimulation.GetSpeedKmh();
244  float range;
245 
246  if (s_fGlobalFuelConsumptionScale != 0 && currentConsumption != 0)
247  range = speed * newFuel / (currentConsumption * s_fGlobalFuelConsumptionScale);
248  else
249  range = -1;
250 
251  float litersPer100KM;
252  if (speed > 0)
253  litersPer100KM = currentConsumption * 100 / speed;
254 
255  Print(throttle);
256  Print(speed);
257  Print(currentConsumption);
258  Print(newFuel);
259  Print(range);
260  Print(litersPer100KM);
261  }
262  #endif // ENABLE_DIAG
263  m_CurrentFuelTank.SetFuel(newFuel);
264  }
265  }
266  else
267  {
269  {
270  VehicleControllerComponent controller = VehicleControllerComponent.Cast(GetOwner().FindComponent(VehicleControllerComponent));
271  if (controller)
272  {
273  controller.StopEngine(false);
274  }
275  }
276  else
277  {
278  VehicleControllerComponent_SA controller = VehicleControllerComponent_SA.Cast(GetOwner().FindComponent(VehicleControllerComponent_SA));
279  if (controller)
280  {
281  controller.StopEngine(false);
282  }
283  }
284  }
285 
286  m_fTimeDelta = 0;
287  }
288 
289  //------------------------------------------------------------------------------------------------
291  {
292  World world = GetOwner().GetWorld();
293  FuelConsumptionSystem updateSystem = FuelConsumptionSystem.Cast(world.FindSystem(FuelConsumptionSystem));
294  if (!updateSystem)
295  return;
296 
297  updateSystem.Register(this);
298  m_bConnectedToSystem = true;
299  }
300 
301  //------------------------------------------------------------------------------------------------
303  {
304  World world = GetOwner().GetWorld();
305  FuelConsumptionSystem updateSystem = FuelConsumptionSystem.Cast(world.FindSystem(FuelConsumptionSystem));
306  if (!updateSystem)
307  return;
308 
309  updateSystem.Unregister(this);
310  m_bConnectedToSystem = false;
311  }
312 
313  //------------------------------------------------------------------------ COMMON METHODS ------------------------------------------------------------------------//
314 
315  //------------------------------------------------------------------------------------------------
318  void Update(float timeSlice)
319  {
320  m_fTimeDelta += timeSlice;
321  if (m_fTimeDelta > TIME_STEP)
322  Update();
323  }
324 
325  //------------------------------------------------------------------------------------------------
326  override void OnPostInit(IEntity owner)
327  {
328  m_ComponentData = SCR_FuelConsumptionComponentClass.Cast(GetComponentData(owner));
329  if (!m_ComponentData)
330  return;
331 
332  // Irrelevant on proxies
333  RplComponent rpl = RplComponent.Cast(owner.FindComponent(RplComponent));
334  if (rpl && !rpl.IsProxy())
335  SetEventMask(owner, EntityEvent.INIT);
336  }
337 
338  //------------------------------------------------------------------------------------------------
339  override void EOnInit(IEntity owner)
340  {
341  FuelManagerComponent fuelManager = FuelManagerComponent.Cast(owner.FindComponent(FuelManagerComponent));
342  if (!fuelManager)
343  return;
344 
346 
347  m_Simulation = VehicleBaseSimulation.Cast(owner.FindComponent(VehicleBaseSimulation));
348 
349  if (m_Simulation && !m_Simulation.IsValid())
350  m_Simulation = null;
351 
352  if (!m_Simulation)
353  return;
354 
356  {
357  VehicleWheeledSimulation wheeledSimulation = VehicleWheeledSimulation.Cast(m_Simulation);
358  if (wheeledSimulation && wheeledSimulation.EngineIsOn())
359  SetEnabled(true);
360  }
361  else
362  {
363  VehicleWheeledSimulation_SA wheeledSimulation = VehicleWheeledSimulation_SA.Cast(m_Simulation);
364  if (wheeledSimulation && wheeledSimulation.EngineIsOn())
365  SetEnabled(true);
366  }
367 
368 
369  VehicleHelicopterSimulation helicopterSimulation = VehicleHelicopterSimulation.Cast(m_Simulation);
370  if (helicopterSimulation && helicopterSimulation.EngineIsOn())
371  SetEnabled(true);
372 
373 #ifdef ENABLE_DIAG
374  DiagMenu.RegisterBool(SCR_DebugMenuID.DEBUGUI_VEHICLES_FUEL_CONSUMPTION, "", "Fuel consumption", "Vehicles");
375 #endif // ENABLE_DIAG
376  }
377 
378  //------------------------------------------------------------------------------------------------
379  override void OnDelete(IEntity owner)
380  {
382 
383  super.OnDelete(owner);
384  }
385 }
ConnectToFuelConsumptionSystem
protected void ConnectToFuelConsumptionSystem()
Definition: SCR_FuelConsumptionComponent.c:290
SCR_BaseGameMode
Definition: SCR_BaseGameMode.c:137
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
SetEnabled
void SetEnabled(bool enabled)
Definition: SCR_FuelConsumptionComponent.c:120
m_fTimeDelta
protected float m_fTimeDelta
Definition: SCR_FuelConsumptionComponent.c:25
BaseFuelNode
Definition: BaseFuelNode.c:12
MIN_CLUTCH
const protected float MIN_CLUTCH
Definition: SCR_FuelConsumptionComponent.c:19
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
ScriptGameComponentClass
Definition: ScriptGameComponent.c:12
OnDelete
override void OnDelete(IEntity owner)
Definition: SCR_FuelConsumptionComponent.c:379
m_bConnectedToSystem
protected bool m_bConnectedToSystem
Definition: SCR_FuelConsumptionComponent.c:23
m_CurrentFuelTank
protected BaseFuelNode m_CurrentFuelTank
Definition: SCR_FuelConsumptionComponent.c:26
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
FindNonEmptyFuelTank
private BaseFuelNode FindNonEmptyFuelTank()
Definition: SCR_FuelConsumptionComponent.c:101
GetGameMode
SCR_BaseGameMode GetGameMode()
Definition: SCR_BaseGameModeComponent.c:15
TIME_STEP
const protected float TIME_STEP
Definition: SCR_FuelConsumptionComponent.c:21
ENotification
ENotification
Definition: ENotification.c:4
GetIsClientAuthority
override bool GetIsClientAuthority()
Definition: game.c:268
SetCurrentFuelTank
void SetCurrentFuelTank(int iFuelTankID)
Definition: SCR_FuelConsumptionComponent.c:63
MIN_FUEL
const protected float MIN_FUEL
Definition: SCR_FuelConsumptionComponent.c:17
FuelConsumptionSystem
Definition: FuelConsumptionSystem.c:1
Attribute
typedef Attribute
Post-process effect of scripted camera.
GetCurrentFuelTank
BaseFuelNode GetCurrentFuelTank()
Definition: SCR_FuelConsumptionComponent.c:70
OnPostInit
override void OnPostInit(IEntity owner)
Called on PostInit when all components are added.
Definition: SCR_FuelConsumptionComponent.c:326
SCR_FuelConsumptionComponentClass
Definition: SCR_FuelConsumptionComponent.c:2
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
Update
void Update()
updates currently active fueltank to see if it can still be used. turns off engine if no usable fuelt...
Definition: SCR_FuelConsumptionComponent.c:146
EOnInit
override void EOnInit(IEntity owner)
Definition: SCR_FuelConsumptionComponent.c:339
FindFuelTankByID
protected BaseFuelNode FindFuelTankByID(int iFuelTankID)
Definition: SCR_FuelConsumptionComponent.c:79
SECOND_TO_HOUR
SCR_FuelConsumptionComponentClass SECOND_TO_HOUR
NEUTRAL_GEAR
const protected float NEUTRAL_GEAR
Definition: SCR_FuelConsumptionComponent.c:20
DisconnectFromFuelConsumptionSystem
protected void DisconnectFromFuelConsumptionSystem()
Definition: SCR_FuelConsumptionComponent.c:302
MIN_THRUST
const protected float MIN_THRUST
Definition: SCR_FuelConsumptionComponent.c:18
SCR_DebugMenuID
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition: DebugMenuID.c:3
m_ComponentData
protected SCR_FuelConsumptionComponentClass m_ComponentData
Definition: SCR_FuelConsumptionComponent.c:28
m_Simulation
protected VehicleBaseSimulation m_Simulation
Definition: SCR_FuelConsumptionComponent.c:27
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180