Dayz Explorer 1.28.160049
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 static const string SPARKPLUG_ATTACH_SOUND = "sparkplug_attach_SoundSet";
14 static const string SPARKPLUG_DETACH_SOUND = "sparkplug_detach_SoundSet";
15
16 protected bool m_IsLowEnergy;
21 ref protected Effect m_Smoke;
22
24
27 protected ref UniversalTemperatureSourceLambdaConstant m_UTSLEngine;
28
29 // Constructor
31 {
32 SetEventMask(EntityEvent.INIT); // Enable EOnInit event
33
34 m_FuelPercentage = 50;
35 RegisterNetSyncVariableInt("m_FuelPercentage");
36 }
37
39 {
41 }
42
43 override void EEInit()
44 {
45 super.EEInit();
46
47 if (GetGame().IsServer() || !GetGame().IsMultiplayer())
48 {
50 m_UTSSettings.m_ManualUpdate = true;
51 m_UTSSettings.m_TemperatureItemCap = GameConstants.ITEM_TEMPERATURE_NEUTRAL_ZONE_MIDDLE;
52 m_UTSSettings.m_TemperatureCap = 8;
53 m_UTSSettings.m_RangeFull = 1;
54 m_UTSSettings.m_RangeMax = 2.5;
55
56 m_UTSLEngine = new UniversalTemperatureSourceLambdaConstant();
58 }
59 }
60
61 override void EOnInit(IEntity other, int extra)
62 {
63 if (GetGame().IsServer())
64 {
65 m_FuelPercentage = GetCompEM().GetEnergy0To100();
66 SetSynchDirty();
67 }
68
69 UpdateFuelMeter();
70 }
71
72 override float GetLiquidThroughputCoef()
73 {
75 }
76
78 {
79 return "0.3 0.21 0.4";
80 }
81
83 {
84 return "270 0 0";
85 }
86
87 // Play the loop sound
89 {
90 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
91 {
92 if (GetCompEM().IsWorking())
93 {
94 if (m_IsLowEnergy)
95 PlaySoundSetLoop(m_EngineLoop, LOOP_LOW_FUEL_SOUND, 0.3, 0.3);
96 else
97 PlaySoundSetLoop(m_EngineLoop, LOOP_SOUND, 0.3, 0.3);
98
99 // Particle
100 m_Smoke = new EffGeneratorSmoke();
101 SEffectManager.PlayOnObject(m_Smoke, this, GetSmokeParticlePosition(), GetSmokeParticleOrientation());
102 }
103 }
104 }
105
106 // Taking item into inventory
107 override bool CanPutInCargo( EntityAI parent )
108 {
109 if (!super.CanPutInCargo(parent))
110 {
111 return false;
112 }
113
114 return CanManipulate();
115 }
116
117 // Taking item into inventory
118 override bool CanPutIntoHands(EntityAI parent)
119 {
120 if(!super.CanPutIntoHands(parent))
121 {
122 return false;
123 }
124 return CanManipulate();
125 }
126
127 // Returns true/false if this item can be moved into inventory/hands
129 {
130 return GetCompEM().GetPluggedDevicesCount() == 0 && !GetCompEM().IsWorking();
131 }
132
133 /*===================================
134 EVENTS
135 ===================================*/
136
137 // Init
138 override void OnInitEnergy()
139 {
140 m_FuelTankCapacity = GetGame().ConfigGetFloat ("CfgVehicles " + GetType() + " fuelTankCapacity");
141 m_FuelToEnergyRatio = GetCompEM().GetEnergyMax() / m_FuelTankCapacity; // Conversion ratio of 1 ml of fuel to X Energy
142
143 UpdateFuelMeter();
144 }
145
146 // Generator is working
147 override void OnWorkStart()
148 {
149 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
150 {
151 if (IsInitialized())
152 {
153 PlaySoundSet(m_EngineStart, START_SOUND, 0, 0);
154 }
155
156 if (!m_SoundLoopStartTimer)
157 {
158 m_SoundLoopStartTimer = new Timer(CALL_CATEGORY_SYSTEM);
159 }
160
161 if (!m_SoundLoopStartTimer.IsRunning()) // Makes sure the timer is NOT running already
162 {
163 m_SoundLoopStartTimer.Run(1.5, this, "StartLoopSound", NULL, false);
164 }
165 }
166
167 if (GetGame().IsServer() || !GetGame().IsMultiplayer())
168 {
169 m_UTSource.SetDefferedActive(true, 20.0);
170 }
171 }
172
173 // Do work
174 override void OnWork(float consumed_energy)
175 {
176 if (GetGame().IsServer() || !GetGame().IsMultiplayer())
177 {
179 }
180
181 if (GetGame().IsServer())
182 {
183 m_FuelPercentage = GetCompEM().GetEnergy0To100();
184 SetSynchDirty();
185 }
186
187 if (m_FuelPercentage < LOW_ENERGY_FUEL_PERCENTAGE && !m_IsLowEnergy)
188 SetLowEnergyState(true);
189 else if (m_FuelPercentage >= LOW_ENERGY_FUEL_PERCENTAGE && m_IsLowEnergy)
190 SetLowEnergyState(false);
191
192 UpdateFuelMeter();
193 }
194
195 // Turn off when this runs out of fuel
196 override void OnWorkStop()
197 {
198 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
199 {
200 // Sound
201 PlaySoundSet(m_EngineStop, STOP_SOUND, 0, 0);
202 StopSoundSet(m_EngineLoop);
203
204 // particle
206
207 // Fuel meter
208 UpdateFuelMeter();
209 }
210
211 if (GetGame().IsServer() || !GetGame().IsMultiplayer())
212 {
213 m_UTSource.SetDefferedActive(false, 20.0);
214 }
215 }
216
217 // Called when this generator is picked up
218 override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
219 {
220 super.OnItemLocationChanged(old_owner, new_owner);
221 UpdateFuelMeter();
222 }
223
224 override void EEItemAttached(EntityAI item, string slot_name)
225 {
226 super.EEItemAttached(item, slot_name);
227 GetCompEM().InteractBranch(this);
228
229 ItemBase item_IB = ItemBase.Cast(item);
230
231 if (item_IB.IsKindOf("Sparkplug") && IsInitialized())
232 {
233 ShowSelection("sparkplug_installed");
234
235 #ifndef SERVER
236 EffectSound sound = SEffectManager.PlaySound(SPARKPLUG_ATTACH_SOUND, GetPosition());
237 sound.SetAutodestroy( true );
238 #endif
239 }
240 }
241
242 override void EEItemDetached(EntityAI item, string slot_name)
243 {
244 super.EEItemDetached(item, slot_name);
245
246 GetCompEM().InteractBranch(this);
247
248 ItemBase item_IB = ItemBase.Cast(item);
249
250 if (item_IB.IsKindOf("Sparkplug"))
251 {
252 HideSelection("sparkplug_installed");
253 GetCompEM().SwitchOff();
254
255 #ifndef SERVER
256 EffectSound sound = SEffectManager.PlaySound(SPARKPLUG_DETACH_SOUND, GetPosition());
257 sound.SetAutodestroy(true);
258 #endif
259 }
260 }
261
262 /*================================
263 FUNCTIONS
264 ================================*/
265
266 protected void SetLowEnergyState(bool state)
267 {
268 m_IsLowEnergy = state;
269
270 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
271 {
272 StopSoundSet(m_EngineLoop);
273 StartLoopSound();
274 }
275 }
276
278 {
279 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
280 {
281 SetAnimationPhase("dial_fuel", m_FuelPercentage * 0.01);
282 }
283 }
284
285 // Adds energy to the generator
286 void SetFuel(float fuel_amount)
287 {
288 // clamp
289 if (GetFuel() == 0.0 && fuel_amount <= 0.0)
290 return;
291
292 if (m_FuelTankCapacity > 0)
293 {
294 m_FuelToEnergyRatio = GetCompEM().GetEnergyMax() / m_FuelTankCapacity;
295 GetCompEM().SetEnergy(fuel_amount * m_FuelToEnergyRatio);
296 m_FuelPercentage = GetCompEM().GetEnergy0To100();
297 SetSynchDirty();
298 UpdateFuelMeter();
299 }
300 else
301 {
302 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());
303 DPrint(error);
304 }
305 }
306
307 // Adds fuel (energy) to the generator
308 // Returns how much fuel was accepted
309 float AddFuel(float available_fuel)
310 {
311 if (available_fuel == 0.0)
312 return 0.0;
313
314 GetCompEM().InteractBranch(this);
315 float needed_fuel = GetMaxFuel() - GetFuel();
316
317 if (needed_fuel > available_fuel)
318 {
319 SetFuel(GetFuel() + available_fuel);
320 return available_fuel; // Return used fuel amount
321 }
322 else
323 {
324 SetFuel(GetMaxFuel());
325 return needed_fuel;
326 }
327 }
328
329 // Check the bottle if it can be used to fill the tank
330 bool CanAddFuel(ItemBase container)
331 {
332 if (container)
333 {
334 // Get the liquid
335 int liquid_type = container.GetLiquidType();
336
337 // Do all checks
338 if ( container.GetQuantity() > 0 && GetCompEM().GetEnergy() < GetCompEM().GetEnergyMax() && (liquid_type & LIQUID_GASOLINE))
339 {
340 return true;
341 }
342 }
343
344 return false;
345 }
346
347 // Returns fuel amount
348 float GetFuel()
349 {
350 return Math.Clamp(GetCompEM().GetEnergy() / m_FuelToEnergyRatio, 0.0, GetMaxFuel());
351 }
352
353 // Returns max fuel amount
355 {
356 return m_FuelTankCapacity;
357 }
358
360 {
361 return m_FuelPercentage;
362 }
363
364 // Checks sparkplug
366 {
367 int slot = InventorySlots.GetSlotIdFromString("SparkPlug");
368 EntityAI ent = GetInventory().FindAttachment(slot);
369
370 return ent && !ent.IsRuined();
371 }
372
374 {
375 super.OnVariablesSynchronized();
376
377 UpdateFuelMeter();
378 }
379
380 //================================================================
381 // ADVANCED PLACEMENT
382 //================================================================
383
384 override string GetDeploySoundset()
385 {
386 return "placePowerGenerator_SoundSet";
387 }
388
399
400 //Debug menu Spawn Ground Special
401 override void OnDebugSpawn()
402 {
403 EntityAI entity;
404 if (Class.CastTo(entity, this))
405 {
406 entity.GetInventory().CreateInInventory("SparkPlug");
407 }
408
409 SetFuel(GetMaxFuel());
410 }
411
412 override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
413 {
414 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "PowerGenerator Fuel", FadeColors.RED));
415 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.GENERIC_FUEL_FULL, "Full", FadeColors.LIGHT_GREY));
416 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.GENERIC_FUEL_EMPTY, "Empty", FadeColors.LIGHT_GREY));
417 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.GENERIC_FUEL_INCREASE, "10% increase", FadeColors.LIGHT_GREY));
418 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.GENERIC_FUEL_DECREASE, "10% decrease", FadeColors.LIGHT_GREY));
419 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "___________________________", FadeColors.RED));
420
421 super.GetDebugActions(outputList);
422 }
423
424 override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
425 {
426 if (super.OnAction(action_id, player, ctx))
427 return true;
428
429 if (!GetGame().IsServer())
430 return false;
431
432 switch (action_id)
433 {
434 case EActions.GENERIC_FUEL_FULL:
435 SetFuel(GetMaxFuel());
436 return true;
437 case EActions.GENERIC_FUEL_EMPTY:
438 SetFuel(0);
439 return true;
440 case EActions.GENERIC_FUEL_INCREASE:
441 AddFuel(Math.Clamp(GetMaxFuel() * 0.1, 0.0, GetMaxFuel()));
442 return true;
443 case EActions.GENERIC_FUEL_DECREASE:
444 float value = GetMaxFuel() * 0.1;
445 if (value <= 0.0)
446 {
447 SetFuel(0.0);
448 return true;
449 }
450
451 SetFuel(GetFuel() - value);
452 return true;
453 }
454
455 return false;
456 }
457}
458
459
460class PowerGenerator extends PowerGeneratorBase {}
Param4< int, int, string, int > TSelectableActionInfoWithColor
Definition entityai.c:97
eBleedingSourceType GetType()
ActionPlaceObjectCB ActiondeployObjectCB ActionPlaceObject()
void AddAction(typename actionName)
#define LIQUID_GASOLINE
ref UniversalTemperatureSourceLambdaEngine m_UTSLEngine
Super root of all classes in Enforce script.
Definition enscript.c:11
Wrapper class for managing sound through SEffectManager.
Definition effectsound.c:5
override void SetAutodestroy(bool auto_destroy)
Sets whether Effect automatically cleans up when it stops.
provides access to slot configuration
override void EEItemAttached(EntityAI item, string slot_name)
override void OnWork(float consumed_energy)
override void OnWorkStart()
bool HasSparkplug()
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 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
ref Timer m_SoundLoopStartTimer
bool m_IsLowEnergy
float AddFuel(float available_fuel)
vector GetSmokeParticlePosition()
void ~PowerGeneratorBase()
void SetLowEnergyState(bool state)
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 EffectSound PlaySound(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound.
static void DestroyEffect(Effect effect)
Unregisters, stops and frees the Effect.
Serialization general interface. Serializer API works with:
Definition serializer.c:56
original Timer deletes m_params which is unwanted
override bool IsInitialized()
EActions
Definition eactions.c:2
ref UniversalTemperatureSourceSettings m_UTSSettings
ref UniversalTemperatureSource m_UTSource
proto native CGame GetGame()
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:573
const int SAT_DEBUG_ACTION
Definition constants.c:454
class JsonUndergroundAreaTriggerData GetPosition
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8
float GetEnergy()
Definition itembase.c:8420