Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
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
14class 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;
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 {
66 }
67
68 //------------------------------------------------------------------------------------------------
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 //------------------------------------------------------------------------------------------------
101 private BaseFuelNode FindNonEmptyFuelTank()
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)
153 m_CurrentFuelTank = FindNonEmptyFuelTank();
154 else
155 m_CurrentFuelTank = null;
156 }
157
158 // vehicle has at least one fuel tank
160 {
161 float currentConsumption;
162
164 if (wheeledSimulation)
165 {
166 float throttle = wheeledSimulation.GetThrottle();
167 if (throttle >= MIN_THRUST && wheeledSimulation.GetGear() != NEUTRAL_GEAR && wheeledSimulation.GetClutch() >= MIN_CLUTCH)
168 currentConsumption = m_ComponentData.m_fFuelConsumption * throttle * wheeledSimulation.EngineGetRPM() / wheeledSimulation.EngineGetRPMPeakPower();
169 else if (throttle >= MIN_THRUST)
170 currentConsumption = m_ComponentData.m_fFuelConsumptionIdle * wheeledSimulation.EngineGetRPM() / wheeledSimulation.EngineGetRPMIdle();
171 else
172 currentConsumption = m_ComponentData.m_fFuelConsumptionIdle;
173 }
174
176 if (helicopterSimulation)
177 {
178 currentConsumption = m_ComponentData.m_fFuelConsumption;
179 }
180
181 // Scale and convert consumption rate per hour to per second
182 float consumptionScale = m_fTimeDelta * s_fGlobalFuelConsumptionScale * SECOND_TO_HOUR;
183
184 float newFuel = m_CurrentFuelTank.GetFuel() - currentConsumption * consumptionScale;
185
186 #ifdef ENABLE_DIAG
187 if (wheeledSimulation && currentConsumption > 0 && DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_VEHICLES_FUEL_CONSUMPTION))
188 {
189 float throttle = wheeledSimulation.GetThrottle();
190 float speed = wheeledSimulation.GetSpeedKmh();
191 float range;
192
193 if (s_fGlobalFuelConsumptionScale != 0 && currentConsumption != 0)
194 range = speed * newFuel / (currentConsumption * s_fGlobalFuelConsumptionScale);
195 else
196 range = -1;
197
198 float litersPer100KM;
199 if (speed > 0)
200 litersPer100KM = currentConsumption * 100 / speed;
201
202 Print(throttle);
203 Print(speed);
204 Print(currentConsumption);
205 Print(newFuel);
206 Print(range);
207 Print(litersPer100KM);
208 }
209 #endif // ENABLE_DIAG
210 m_CurrentFuelTank.SetFuel(newFuel);
211 }
212 else
213 {
214 VehicleControllerComponent controller = VehicleControllerComponent.Cast(GetOwner().FindComponent(VehicleControllerComponent));
215 if (controller)
216 {
217 controller.StopEngine(false);
218 }
219 }
220
221 m_fTimeDelta = 0;
222 }
223
224 //------------------------------------------------------------------------------------------------
226 {
227 World world = GetOwner().GetWorld();
228 FuelConsumptionSystem updateSystem = FuelConsumptionSystem.Cast(world.FindSystem(FuelConsumptionSystem));
229 if (!updateSystem)
230 return;
231
232 updateSystem.Register(this);
234 }
235
236 //------------------------------------------------------------------------------------------------
238 {
239 World world = GetOwner().GetWorld();
240 FuelConsumptionSystem updateSystem = FuelConsumptionSystem.Cast(world.FindSystem(FuelConsumptionSystem));
241 if (!updateSystem)
242 return;
243
244 updateSystem.Unregister(this);
245 m_bConnectedToSystem = false;
246 }
247
248 //------------------------------------------------------------------------ COMMON METHODS ------------------------------------------------------------------------//
249
250 //------------------------------------------------------------------------------------------------
253 void Update(float timeSlice)
254 {
255 m_fTimeDelta += timeSlice;
257 Update();
258 }
259
260 //------------------------------------------------------------------------------------------------
261 override void OnPostInit(IEntity owner)
262 {
264 if (!m_ComponentData)
265 return;
266
267 // Irrelevant on proxies
268 RplComponent rpl = RplComponent.Cast(owner.FindComponent(RplComponent));
269 if (rpl && !rpl.IsProxy())
270 SetEventMask(owner, EntityEvent.INIT);
271 }
272
273 //------------------------------------------------------------------------------------------------
274 override void EOnInit(IEntity owner)
275 {
276 FuelManagerComponent fuelManager = FuelManagerComponent.Cast(owner.FindComponent(FuelManagerComponent));
277 if (!fuelManager)
278 return;
279
280 m_CurrentFuelTank = FindNonEmptyFuelTank();
281
283
284 if (m_Simulation && !m_Simulation.IsValid())
285 m_Simulation = null;
286
287 if (!m_Simulation)
288 return;
289
291 if (wheeledSimulation && wheeledSimulation.EngineIsOn())
292 SetEnabled(true);
293
295 if (helicopterSimulation && helicopterSimulation.EngineIsOn())
296 SetEnabled(true);
297
298#ifdef ENABLE_DIAG
299 DiagMenu.RegisterBool(SCR_DebugMenuID.DEBUGUI_VEHICLES_FUEL_CONSUMPTION, "", "Fuel consumption", "Vehicles");
300#endif // ENABLE_DIAG
301 }
302
303 //------------------------------------------------------------------------------------------------
304 override void OnDelete(IEntity owner)
305 {
307
308 super.OnDelete(owner);
309 }
310}
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition DebugMenuID.c:4
ENotification
ArmaReforgerScripted GetGame()
Definition game.c:1398
SCR_BaseGameMode GetGameMode()
SCR_CharacterSoundComponentClass GetComponentData()
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
bool m_bConnectedToSystem
SCR_FuelConsumptionComponentClass SECOND_TO_HOUR
const float MIN_THRUST
void SetCurrentFuelTank(int iFuelTankID)
const float NEUTRAL_GEAR
const float MIN_CLUTCH
void DisconnectFromFuelConsumptionSystem()
BaseFuelNode m_CurrentFuelTank
BaseFuelNode GetCurrentFuelTank()
BaseFuelNode FindFuelTankByID(int iFuelTankID)
SCR_FuelConsumptionComponentClass m_ComponentData
const float TIME_STEP
void ConnectToFuelConsumptionSystem()
const float MIN_FUEL
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
VehicleBaseSimulation m_Simulation
Diagnostic and developer menu system.
Definition DiagMenu.c:18
void Unregister(SCR_FuelConsumptionComponent component)
void Register(SCR_FuelConsumptionComponent component)
proto external Managed FindComponent(typename typeName)
proto external BaseWorld GetWorld()
sealed bool IsMaster()
Definition World.c:16
IEntity GetOwner()
Owner entity of the fuel tank.
override void EOnInit(IEntity owner)
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
SCR_FieldOfViewSettings Attribute
void SetEnabled(bool enabled)
EntityEvent
Various entity events.
Definition EntityEvent.c:14