Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
powergenerator.c
Go to the documentation of this file.
1 class 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  static const string START_SOUND = "powerGeneratorTurnOn_SoundSet";
9  static const string LOOP_SOUND = "powerGeneratorLoop_SoundSet";
10  static const string STOP_SOUND = "powerGeneratorTurnOff_SoundSet";
11  static const string SPARKPLUG_ATTACH_SOUND = "sparkplug_attach_SoundSet";
12  static const string SPARKPLUG_DETACH_SOUND = "sparkplug_detach_SoundSet";
13 
14  protected EffectSound m_EngineLoop;
15  protected EffectSound m_EngineStart;
16  protected EffectSound m_EngineStop;
17  ref Timer m_SoundLoopStartTimer;
18  ref protected Effect m_Smoke;
19 
20  ItemBase m_SparkPlug;
21 
25 
26  // Constructor
27  void PowerGeneratorBase()
28  {
29  SetEventMask(EntityEvent.INIT); // Enable EOnInit event
30 
31  m_FuelPercentage = 50;
32  RegisterNetSyncVariableInt("m_FuelPercentage");
33  RegisterNetSyncVariableBool("m_IsSoundSynchRemote");
34  RegisterNetSyncVariableBool("m_IsPlaceSound");
35  }
36 
37  void ~PowerGeneratorBase()
38  {
39  SEffectManager.DestroyEffect(m_Smoke);
40  }
41 
42  override void EEInit()
43  {
44  super.EEInit();
45 
46  if (GetGame().IsServer() || !GetGame().IsMultiplayer())
47  {
49  m_UTSSettings.m_ManualUpdate = true;
50  m_UTSSettings.m_TemperatureMin = 0;
51  m_UTSSettings.m_TemperatureMax = 80;
52  m_UTSSettings.m_RangeFull = 1;
53  m_UTSSettings.m_RangeMax = 2.5;
54  m_UTSSettings.m_TemperatureCap = 8;
55 
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 
77  // Play the loop sound
78  void StartLoopSound()
79  {
80  if (GetGame().IsClient() || !GetGame().IsMultiplayer())
81  {
82  if (GetCompEM().IsWorking())
83  {
84  PlaySoundSetLoop(m_EngineLoop, LOOP_SOUND, 0, 0);
85 
86  // Particle
87  vector local_pos = "0.3 0.21 0.4";
88  vector local_ori = "270 0 0";
89  m_Smoke = new EffGeneratorSmoke();
90  SEffectManager.PlayOnObject(m_Smoke, this, local_pos, local_ori);
91  }
92  }
93  }
94 
95  // Taking item into inventory
96  override bool CanPutInCargo( EntityAI parent )
97  {
98  if (!super.CanPutInCargo(parent))
99  {
100  return false;
101  }
102 
103  return CanManipulate();
104  }
105 
106  // Taking item into inventory
107  override bool CanPutIntoHands(EntityAI player)
108  {
109  if(!super.CanPutIntoHands(parent))
110  {
111  return false;
112  }
113  return CanManipulate();
114  }
115 
116  // Returns true/false if this item can be moved into inventory/hands
117  bool CanManipulate()
118  {
119  return GetCompEM().GetPluggedDevicesCount() == 0 && !GetCompEM().IsWorking();
120  }
121 
122  /*===================================
123  EVENTS
124  ===================================*/
125 
126  // Init
127  override void OnInitEnergy()
128  {
129  m_FuelTankCapacity = GetGame().ConfigGetFloat ("CfgVehicles " + GetType() + " fuelTankCapacity");
130  m_FuelToEnergyRatio = GetCompEM().GetEnergyMax() / m_FuelTankCapacity; // Conversion ratio of 1 ml of fuel to X Energy
131 
132  UpdateFuelMeter();
133  }
134 
135  // Generator is working
136  override void OnWorkStart()
137  {
138  if (GetGame().IsClient() || !GetGame().IsMultiplayer())
139  {
140  if (IsInitialized())
141  {
142  PlaySoundSet(m_EngineStart, START_SOUND, 0, 0);
143  }
144 
145  if (!m_SoundLoopStartTimer)
146  {
147  m_SoundLoopStartTimer = new Timer(CALL_CATEGORY_SYSTEM);
148  }
149 
150  if (!m_SoundLoopStartTimer.IsRunning()) // Makes sure the timer is NOT running already
151  {
152  m_SoundLoopStartTimer.Run(1.5, this, "StartLoopSound", NULL, false);
153  }
154  }
155 
156  if (GetGame().IsServer() || !GetGame().IsMultiplayer())
157  {
158  m_UTSource.SetDefferedActive(true, 20.0);
159  }
160  }
161 
162  // Do work
163  override void OnWork(float consumed_energy)
164  {
165  if (GetGame().IsServer() || !GetGame().IsMultiplayer())
166  {
168  }
169 
170  if (GetGame().IsServer())
171  {
172  m_FuelPercentage = GetCompEM().GetEnergy0To100();
173  SetSynchDirty();
174  }
175 
176  UpdateFuelMeter();
177  }
178 
179  // Turn off when this runs out of fuel
180  override void OnWorkStop()
181  {
182  if (GetGame().IsClient() || !GetGame().IsMultiplayer())
183  {
184  // Sound
185  PlaySoundSet(m_EngineStop, STOP_SOUND, 0, 0);
186  StopSoundSet(m_EngineLoop);
187 
188  // particle
189  SEffectManager.DestroyEffect(m_Smoke);
190 
191  // Fuel meter
192  UpdateFuelMeter();
193  }
194 
195  if (GetGame().IsServer() || !GetGame().IsMultiplayer())
196  {
197  m_UTSource.SetDefferedActive(false, 20.0);
198  }
199  }
200 
201  // Called when this generator is picked up
202  override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
203  {
204  super.OnItemLocationChanged(old_owner, new_owner);
205  UpdateFuelMeter();
206  }
207 
208  override void EEItemAttached(EntityAI item, string slot_name)
209  {
210  super.EEItemAttached(item, slot_name);
211  GetCompEM().InteractBranch(this);
212 
213  ItemBase item_IB = ItemBase.Cast(item);
214 
215  if (item_IB.IsKindOf("Sparkplug") && IsInitialized())
216  {
217  ShowSelection("sparkplug_installed");
218 
219  #ifndef SERVER
220  EffectSound sound = SEffectManager.PlaySound(SPARKPLUG_ATTACH_SOUND, GetPosition());
221  sound.SetAutodestroy( true );
222  #endif
223  }
224  }
225 
226  override void EEItemDetached(EntityAI item, string slot_name)
227  {
228  super.EEItemDetached(item, slot_name);
229 
230  GetCompEM().InteractBranch(this);
231 
232  ItemBase item_IB = ItemBase.Cast(item);
233 
234  if (item_IB.IsKindOf("Sparkplug"))
235  {
236  HideSelection("sparkplug_installed");
237  GetCompEM().SwitchOff();
238 
239  #ifndef SERVER
240  EffectSound sound = SEffectManager.PlaySound(SPARKPLUG_DETACH_SOUND, GetPosition());
241  sound.SetAutodestroy(true);
242  #endif
243  }
244  }
245 
246  /*================================
247  FUNCTIONS
248  ================================*/
249 
250  void UpdateFuelMeter()
251  {
252  if (GetGame().IsClient() || !GetGame().IsMultiplayer())
253  {
254  SetAnimationPhase("dial_fuel", m_FuelPercentage * 0.01);
255  }
256  }
257 
258  // Adds energy to the generator
259  void SetFuel(float fuel_amount)
260  {
261  if (m_FuelTankCapacity > 0)
262  {
263  m_FuelToEnergyRatio = GetCompEM().GetEnergyMax() / m_FuelTankCapacity;
264  GetCompEM().SetEnergy(fuel_amount * m_FuelToEnergyRatio);
265  m_FuelPercentage = GetCompEM().GetEnergy0To100();
266  SetSynchDirty();
267  UpdateFuelMeter();
268  }
269  else
270  {
271  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());
272  DPrint(error);
273  }
274  }
275 
276  // Adds fuel (energy) to the generator
277  // Returns how much fuel was accepted
278  float AddFuel(float available_fuel)
279  {
280  if (available_fuel == 0)
281  {
282  return 0;
283  }
284  GetCompEM().InteractBranch(this);
285  float needed_fuel = GetMaxFuel() - GetFuel();
286 
287  if (needed_fuel > available_fuel)
288  {
289  SetFuel(GetFuel() + available_fuel);
290  return available_fuel; // Return used fuel amount
291  }
292  else
293  {
294  SetFuel(GetMaxFuel());
295  return needed_fuel;
296  }
297  }
298 
299  // Check the bottle if it can be used to fill the tank
300  bool CanAddFuel(ItemBase container)
301  {
302  if (container)
303  {
304  // Get the liquid
305  int liquid_type = container.GetLiquidType();
306 
307  // Do all checks
308  if ( container.GetQuantity() > 0 && GetCompEM().GetEnergy() < GetCompEM().GetEnergyMax() && (liquid_type & LIQUID_GASOLINE))
309  {
310  return true;
311  }
312  }
313 
314  return false;
315  }
316 
317  // Returns fuel amount
318  float GetFuel()
319  {
320  return GetCompEM().GetEnergy() / m_FuelToEnergyRatio;
321  }
322 
323  // Returns max fuel amount
324  float GetMaxFuel()
325  {
326  return m_FuelTankCapacity;
327  }
328 
329  // Checks sparkplug
330  bool HasSparkplug()
331  {
332  int slot = InventorySlots.GetSlotIdFromString("SparkPlug");
333  EntityAI ent = GetInventory().FindAttachment(slot);
334 
335  return ent && !ent.IsRuined();
336  }
337 
338  override void OnVariablesSynchronized()
339  {
340  super.OnVariablesSynchronized();
341 
342  UpdateFuelMeter();
343 
344  if (IsPlaceSound())
345  {
346  PlayPlaceSound();
347  }
348  }
349 
350  //================================================================
351  // ADVANCED PLACEMENT
352  //================================================================
353 
354  override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
355  {
356  super.OnPlacementComplete(player, position, orientation);
357 
358  SetIsPlaceSound(true);
359  }
360 
361  override string GetPlaceSoundset()
362  {
363  return "placePowerGenerator_SoundSet";
364  }
365 
366  override void SetActions()
367  {
368  super.SetActions();
369 
375  }
376 
377  //Debug menu Spawn Ground Special
378  override void OnDebugSpawn()
379  {
380  EntityAI entity;
381  if (Class.CastTo(entity, this))
382  {
383  entity.GetInventory().CreateInInventory("SparkPlug");
384  }
385 
386  SetFuel(GetMaxFuel());
387  }
388 }
389 
390 
391 class PowerGenerator extends PowerGeneratorBase
392 {
393 
394 
395 }
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
CALL_CATEGORY_SYSTEM
const int CALL_CATEGORY_SYSTEM
Definition: tools.c:8
m_UTSource
protected ref UniversalTemperatureSource m_UTSource
Definition: fireplacebase.c:224
DPrint
proto void DPrint(string var)
Prints content of variable to console/log. Should be used for critical messages so it will appear in ...
UniversalTemperatureSourceLambdaEngine
Definition: universaltemperaturesourcelambdabaseimpl.c:62
ActionTurnOnPowerGenerator
Definition: actionturnonpowergenerator.c:1
InventorySlots
provides access to slot configuration
Definition: inventoryslots.c:5
ActionPlaceObject
Definition: actionplaceobject.c:9
LIQUID_THROUGHPUT_GENERATOR
const float LIQUID_THROUGHPUT_GENERATOR
Definition: constants.c:528
OnWorkStop
override void OnWorkStop()
Definition: m18smokegrenade_colorbase.c:2
IsInitialized
bool IsInitialized()
Definition: huddebug.c:299
OnVariablesSynchronized
override void OnVariablesSynchronized()
Definition: anniversarymusicsource.c:42
PowerGeneratorBase
PumpkinHelmet PowerGeneratorBase
UniversalTemperatureSource
original Timer deletes m_params which is unwanted
Definition: universaltemperaturesource.c:25
IsPlaceSound
bool IsPlaceSound()
Definition: itembase.c:4288
EOnInit
protected override void EOnInit(IEntity other, int extra)
Definition: testframework.c:235
LIQUID_GASOLINE
const int LIQUID_GASOLINE
Definition: constants.c:509
OnPlacementComplete
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
Definition: explosivesbase.c:133
GetLiquidThroughputCoef
override float GetLiquidThroughputCoef()
Definition: carscript.c:486
m_UTSSettings
protected ref UniversalTemperatureSourceSettings m_UTSSettings
Definition: fireplacebase.c:225
IEntity
Definition: enentity.c:164
ActionTogglePlaceObject
Definition: actiontoggleplaceobject.c:1
ActionPullOutPlug
Definition: actionpulloutplug.c:1
OnDebugSpawn
class Hatchback_02_Blue extends Hatchback_02 OnDebugSpawn
Definition: hatchback_02.c:404
GetPosition
class JsonUndergroundAreaTriggerData GetPosition
Definition: undergroundarealoader.c:9
EffectSound
Wrapper class for managing sound through SEffectManager.
Definition: effectsound.c:4
CanPutIntoHands
override bool CanPutIntoHands(EntityAI parent)
Definition: explosivesbase.c:257
vector
Definition: enconvert.c:105
Effect
void Effect()
ctor
Definition: effect.c:70
CanPutInCargo
override bool CanPutInCargo(EntityAI parent)
Definition: explosivesbase.c:247
UniversalTemperatureSourceSettings
Definition: universaltemperaturesource.c:1
PlayPlaceSound
void PlayPlaceSound()
Definition: itembase.c:4348
OnWork
override void OnWork(float consumed_energy)
Definition: smokegrenadebase.c:195
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
SetActions
void SetActions()
Definition: advancedcommunication.c:79
SetIsPlaceSound
void SetIsPlaceSound(bool is_place_sound)
Definition: itembase.c:4283
EffGeneratorSmoke
Definition: generatorsmoke.c:1
ActionTurnOffPowerGenerator
Definition: actionturnoffpowergenerator.c:1
m_UTSLEngine
protected ref UniversalTemperatureSourceLambdaEngine m_UTSLEngine
Definition: civiliansedan.c:4
EEInit
override void EEInit()
Definition: contaminatedarea.c:27
Timer
Definition: dayzplayerimplement.c:62
GetEnergy
float GetEnergy()
Definition: itembase.c:3461
EntityEvent
EntityEvent
Entity events for event-mask, or throwing event from code.
Definition: enentity.c:44
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
EEItemAttached
override void EEItemAttached(EntityAI item, string slot_name)
Definition: basebuildingbase.c:520
OnItemLocationChanged
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
Definition: combinationlock.c:75
SEffectManager
Manager class for managing Effect (EffectParticle, EffectSound)
Definition: effectmanager.c:5
EEItemDetached
override void EEItemDetached(EntityAI item, string slot_name)
Definition: basebuildingbase.c:529
EntityAI
Definition: building.c:5
GetPlaceSoundset
override string GetPlaceSoundset()
Definition: fireplacebase.c:2574
GetType
override int GetType()
Definition: huddebugwincharagents.c:49
OnWorkStart
override void OnWorkStart()
Definition: smokegrenadebase.c:175