Dayz Explorer 1.29.162510
Loading...
Searching...
No Matches
powergenerator.c
Go to the documentation of this file.
1class PowerGeneratorBase extends ItemBase
2{
3 float m_Fuel;
4 private static float m_FuelTankCapacity; // Capacity in ml.
5 private static float m_FuelToEnergyRatio; // Conversion ratio of 1 ml of fuel to X Energy
6 private int m_FuelPercentage;
7
8 protected const float LOW_ENERGY_FUEL_PERCENTAGE = 20; // how much % of fuel has to remain to trigger low fuel state
9 static const string START_SOUND = "powerGeneratorTurnOn_SoundSet";
10 static const string LOOP_SOUND = "powerGeneratorLoop_SoundSet";
11 protected const string LOOP_LOW_FUEL_SOUND = "powerGenerator_low_Fuel_Loop_SoundSet";
12 static const string STOP_SOUND = "powerGeneratorTurnOff_SoundSet";
13
14 protected bool m_IsLowEnergy;
19 ref protected Effect m_Smoke;
20
22
25 protected ref UniversalTemperatureSourceLambdaConstant m_UTSLEngine;
26
27 // Constructor
29 {
30 SetEventMask(EntityEvent.INIT); // Enable EOnInit event
31
32 m_FuelPercentage = 50;
33 RegisterNetSyncVariableInt("m_FuelPercentage");
34 }
35
40
41 override void EEInit()
42 {
43 super.EEInit();
44
45 if (g_Game.IsServer() || !g_Game.IsMultiplayer())
46 {
48 m_UTSSettings.m_ManualUpdate = true;
49 m_UTSSettings.m_TemperatureItemCap = GameConstants.ITEM_TEMPERATURE_NEUTRAL_ZONE_MIDDLE;
50 m_UTSSettings.m_TemperatureCap = 8;
51 m_UTSSettings.m_RangeFull = 1;
52 m_UTSSettings.m_RangeMax = 2.5;
53
54 m_UTSLEngine = new UniversalTemperatureSourceLambdaConstant();
56 }
57 }
58
59 override void EOnInit(IEntity other, int extra)
60 {
61 if (g_Game.IsServer())
62 {
63 m_FuelPercentage = GetCompEM().GetEnergy0To100();
64 SetSynchDirty();
65 }
66
68 }
69
70 override float GetLiquidThroughputCoef()
71 {
73 }
74
76 {
77 return "0.3 0.21 0.4";
78 }
79
81 {
82 return "270 0 0";
83 }
84
85 // Play the loop sound
87 {
88 if (g_Game.IsClient() || !g_Game.IsMultiplayer())
89 {
90 if (GetCompEM().IsWorking())
91 {
92 if (m_IsLowEnergy)
93 PlaySoundSetLoop(m_EngineLoop, LOOP_LOW_FUEL_SOUND, 0.3, 0.3);
94 else
95 PlaySoundSetLoop(m_EngineLoop, LOOP_SOUND, 0.3, 0.3);
96
97 // Particle
100 }
101 }
102 }
103
104 // Taking item into inventory
105 override bool CanPutInCargo( EntityAI parent )
106 {
107 if (!super.CanPutInCargo(parent))
108 {
109 return false;
110 }
111
112 return CanManipulate();
113 }
114
115 // Taking item into inventory
116 override bool CanPutIntoHands(EntityAI parent)
117 {
118 if(!super.CanPutIntoHands(parent))
119 {
120 return false;
121 }
122 return CanManipulate();
123 }
124
125 // Returns true/false if this item can be moved into inventory/hands
127 {
128 return GetCompEM().GetPluggedDevicesCount() == 0 && !GetCompEM().IsWorking();
129 }
130
131 /*===================================
132 EVENTS
133 ===================================*/
134
135 // Init
136 override void OnInitEnergy()
137 {
138 m_FuelTankCapacity = g_Game.ConfigGetFloat ("CfgVehicles " + GetType() + " fuelTankCapacity");
139 m_FuelToEnergyRatio = GetCompEM().GetEnergyMax() / m_FuelTankCapacity; // Conversion ratio of 1 ml of fuel to X Energy
140
142 }
143
144 // Generator is working
145 override void OnWorkStart()
146 {
147 if (g_Game.IsClient() || !g_Game.IsMultiplayer())
148 {
149 if (IsInitialized())
150 {
151 PlaySoundSet(m_EngineStart, START_SOUND, 0, 0);
152 }
153
155 {
157 }
158
159 if (!m_SoundLoopStartTimer.IsRunning()) // Makes sure the timer is NOT running already
160 {
161 m_SoundLoopStartTimer.Run(1.5, this, "StartLoopSound", NULL, false);
162 }
163 }
164
165 if (g_Game.IsServer() || !g_Game.IsMultiplayer())
166 {
167 m_UTSource.SetDefferedActive(true, 20.0);
168 }
169 }
170
171 // Do work
172 override void OnWork(float consumed_energy)
173 {
174 if (g_Game.IsServer() || !g_Game.IsMultiplayer())
175 {
177 }
178
179 if (g_Game.IsServer())
180 {
181 m_FuelPercentage = GetCompEM().GetEnergy0To100();
182 SetSynchDirty();
183 }
184
185 if (m_FuelPercentage < LOW_ENERGY_FUEL_PERCENTAGE && !m_IsLowEnergy)
186 SetLowEnergyState(true);
187 else if (m_FuelPercentage >= LOW_ENERGY_FUEL_PERCENTAGE && m_IsLowEnergy)
188 SetLowEnergyState(false);
189
191 }
192
193 // Turn off when this runs out of fuel
194 override void OnWorkStop()
195 {
196 if (g_Game.IsClient() || !g_Game.IsMultiplayer())
197 {
198 // Sound
199 PlaySoundSet(m_EngineStop, STOP_SOUND, 0, 0);
200 StopSoundSet(m_EngineLoop);
201
202 // particle
204
205 // Fuel meter
207 }
208
209 if (g_Game.IsServer() || !g_Game.IsMultiplayer())
210 {
211 m_UTSource.SetDefferedActive(false, 20.0);
212 }
213 }
214
215 // Called when this generator is picked up
216 override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
217 {
218 super.OnItemLocationChanged(old_owner, new_owner);
220 }
221
222 override void EEItemAttached(EntityAI item, string slot_name)
223 {
224 super.EEItemAttached(item, slot_name);
225 GetCompEM().InteractBranch(this);
226
227 ItemBase item_IB = ItemBase.Cast(item);
228 if (item_IB.IsKindOf("Sparkplug") && IsInitialized())
229 {
230 ShowSelection("sparkplug_installed");
231 }
232 }
233
234 override void EEItemDetached(EntityAI item, string slot_name)
235 {
236 super.EEItemDetached(item, slot_name);
237 GetCompEM().InteractBranch(this);
238
239 ItemBase item_IB = ItemBase.Cast(item);
240 if (item_IB.IsKindOf("Sparkplug"))
241 {
242 HideSelection("sparkplug_installed");
243 GetCompEM().SwitchOff();
244 }
245 }
246
247 /*================================
248 FUNCTIONS
249 ================================*/
250
251 protected void SetLowEnergyState(bool state)
252 {
253 m_IsLowEnergy = state;
254
255 if (g_Game.IsClient() || !g_Game.IsMultiplayer())
256 {
257 StopSoundSet(m_EngineLoop);
259 }
260 }
261
263 {
264 if (g_Game.IsClient() || !g_Game.IsMultiplayer())
265 {
266 SetAnimationPhase("dial_fuel", m_FuelPercentage * 0.01);
267 }
268 }
269
270 // Adds energy to the generator
271 void SetFuel(float fuel_amount)
272 {
273 // clamp
274 if (GetFuel() == 0.0 && fuel_amount <= 0.0)
275 return;
276
277 if (m_FuelTankCapacity > 0)
278 {
279 m_FuelToEnergyRatio = GetCompEM().GetEnergyMax() / m_FuelTankCapacity;
280 GetCompEM().SetEnergy(fuel_amount * m_FuelToEnergyRatio);
281 m_FuelPercentage = GetCompEM().GetEnergy0To100();
282 SetSynchDirty();
284 }
285 else
286 {
287 string error = string.Format("ERROR! Item %1 has fuel tank with 0 capacity! Add parameter 'fuelTankCapacity' to its config and set it to more than 0!", this.GetType());
288 DPrint(error);
289 }
290 }
291
292 // Adds fuel (energy) to the generator
293 // Returns how much fuel was accepted
294 float AddFuel(float available_fuel)
295 {
296 if (available_fuel == 0.0)
297 return 0.0;
298
299 GetCompEM().InteractBranch(this);
300 float needed_fuel = GetMaxFuel() - GetFuel();
301
302 if (needed_fuel > available_fuel)
303 {
304 SetFuel(GetFuel() + available_fuel);
305 return available_fuel; // Return used fuel amount
306 }
307 else
308 {
310 return needed_fuel;
311 }
312 }
313
314 // Check the bottle if it can be used to fill the tank
315 bool CanAddFuel(ItemBase container)
316 {
317 if (container)
318 {
319 // Get the liquid
320 int liquid_type = container.GetLiquidType();
321
322 // Do all checks
323 if ( container.GetQuantity() > 0 && GetCompEM().GetEnergy() < GetCompEM().GetEnergyMax() && (liquid_type & LIQUID_GASOLINE))
324 {
325 return true;
326 }
327 }
328
329 return false;
330 }
331
332 // Returns fuel amount
333 float GetFuel()
334 {
335 return Math.Clamp(GetCompEM().GetEnergy() / m_FuelToEnergyRatio, 0.0, GetMaxFuel());
336 }
337
338 // Returns max fuel amount
340 {
341 return m_FuelTankCapacity;
342 }
343
345 {
346 return m_FuelPercentage;
347 }
348
349 // Checks sparkplug
351 {
352 int slot = InventorySlots.GetSlotIdFromString("SparkPlug");
353 EntityAI ent = GetInventory().FindAttachment(slot);
354
355 return ent && !ent.IsRuined();
356 }
357
359 {
360 super.OnVariablesSynchronized();
361
363 }
364
365 //================================================================
366 // ADVANCED PLACEMENT
367 //================================================================
368
369 override string GetDeploySoundset()
370 {
371 return "placePowerGenerator_SoundSet";
372 }
373
384
385 //Debug menu Spawn Ground Special
386 override void OnDebugSpawn()
387 {
388 EntityAI entity;
389 if (Class.CastTo(entity, this))
390 {
391 entity.GetInventory().CreateInInventory("SparkPlug");
392 }
393
395 }
396
397 override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
398 {
399 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "PowerGenerator Fuel", FadeColors.RED));
400 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.GENERIC_FUEL_FULL, "Full", FadeColors.LIGHT_GREY));
401 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.GENERIC_FUEL_EMPTY, "Empty", FadeColors.LIGHT_GREY));
402 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.GENERIC_FUEL_INCREASE, "10% increase", FadeColors.LIGHT_GREY));
403 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.GENERIC_FUEL_DECREASE, "10% decrease", FadeColors.LIGHT_GREY));
404 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "___________________________", FadeColors.RED));
405
406 super.GetDebugActions(outputList);
407 }
408
409 override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
410 {
411 if (super.OnAction(action_id, player, ctx))
412 return true;
413
414 if (!g_Game.IsServer())
415 return false;
416
417 switch (action_id)
418 {
419 case EActions.GENERIC_FUEL_FULL:
421 return true;
422 case EActions.GENERIC_FUEL_EMPTY:
423 SetFuel(0);
424 return true;
425 case EActions.GENERIC_FUEL_INCREASE:
426 AddFuel(Math.Clamp(GetMaxFuel() * 0.1, 0.0, GetMaxFuel()));
427 return true;
428 case EActions.GENERIC_FUEL_DECREASE:
429 float value = GetMaxFuel() * 0.1;
430 if (value <= 0.0)
431 {
432 SetFuel(0.0);
433 return true;
434 }
435
436 SetFuel(GetFuel() - value);
437 return true;
438 }
439
440 return false;
441 }
442}
443
444
445class PowerGenerator extends PowerGeneratorBase {}
Param4< int, int, string, int > TSelectableActionInfoWithColor
Definition entityai.c:104
eBleedingSourceType GetType()
ActionPlaceObjectCB ActiondeployObjectCB ActionPlaceObject()
void AddAction(typename actionName)
#define LIQUID_GASOLINE
Super root of all classes in Enforce script.
Definition enscript.c:11
Wrapper class for managing sound through SEffectManager.
Definition effectsound.c:5
provides access to slot configuration
override void EEItemAttached(EntityAI item, string slot_name)
override void OnWork(float consumed_energy)
override void OnWorkStart()
ref UniversalTemperatureSource m_UTSource
DEPRECATED Attached spark plug item.
bool HasSparkplug()
static const string STOP_SOUND
void SetFuel(float fuel_amount)
void PowerGeneratorBase()
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
override bool CanPutIntoHands(EntityAI parent)
float GetMaxFuel()
override void SetActions()
override void OnInitEnergy()
EffectSound m_EngineStart
override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
Effect m_Smoke
ref UniversalTemperatureSourceSettings m_UTSSettings
static const string START_SOUND
ref UniversalTemperatureSourceLambdaConstant m_UTSLEngine
EffectSound m_EngineLoop
override string GetDeploySoundset()
override void OnWorkStop()
override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
float GetFuelPercentage()
override float GetLiquidThroughputCoef()
void StartLoopSound()
void UpdateFuelMeter()
override void EEItemDetached(EntityAI item, string slot_name)
override void OnVariablesSynchronized()
EffectSound m_EngineStop
float GetFuel()
override bool CanPutInCargo(EntityAI parent)
override void EEInit()
override void OnDebugSpawn()
vector GetSmokeParticleOrientation()
ItemBase m_SparkPlug
static const string LOOP_SOUND
ref Timer m_SoundLoopStartTimer
bool m_IsLowEnergy
float AddFuel(float available_fuel)
vector GetSmokeParticlePosition()
const string LOOP_LOW_FUEL_SOUND
void ~PowerGeneratorBase()
void SetLowEnergyState(bool state)
const float LOW_ENERGY_FUEL_PERCENTAGE
override void EOnInit(IEntity other, int extra)
bool CanManipulate()
bool CanAddFuel(ItemBase container)
Definition enmath.c:7
Manager class for managing Effect (EffectParticle, EffectSound).
static int PlayOnObject(notnull Effect eff, Object obj, vector local_pos="0 0 0", vector local_ori="0 0 0", bool force_rotation_relative_to_world=false)
Play an Effect.
static void DestroyEffect(Effect effect)
Unregisters, stops and frees the Effect.
original Timer deletes m_params which is unwanted
override bool IsInitialized()
DayZGame g_Game
Definition dayzgame.c:3942
EActions
Definition eactions.c:2
void Effect()
ctor
Definition effect.c:72
Serializer ParamsReadContext
Definition gameplay.c:15
proto void DPrint(string var)
Prints content of variable to console/log. Should be used for critical messages so it will appear in ...
EntityEvent
Entity events for event-mask, or throwing event from code.
Definition enentity.c:45
const float LIQUID_THROUGHPUT_GENERATOR
Definition constants.c:576
const int SAT_DEBUG_ACTION
Definition constants.c:457
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8
float GetEnergy()
Definition itembase.c:8525