DayZ 1.24
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 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
18 ref protected Effect m_Smoke;
19
21
25
26 // Constructor
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
38 {
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
70 }
71
72 override float GetLiquidThroughputCoef()
73 {
75 }
76
77 // Play the loop sound
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();
91 }
92 }
93 }
94
95 // Taking item into inventory
96 override bool CanPutInCargo(EntityAI parent)
97 {
98 if (!super.CanPutInCargo(parent))
99 return false;
100
101 return CanManipulate();
102 }
103
104 // Taking item into inventory
106 {
107 if (!super.CanPutIntoHands(parent))
108 return false;
109 return CanManipulate();
110 }
111
112 // Returns true/false if this item can be moved into inventory/hands
114 {
115 return GetCompEM().GetPluggedDevicesCount() == 0 && !GetCompEM().IsWorking();
116 }
117
118 /*===================================
119 EVENTS
120 ===================================*/
121
122 // Init
123 override void OnInitEnergy()
124 {
125 m_FuelTankCapacity = GetGame().ConfigGetFloat("CfgVehicles " + GetType() + " fuelTankCapacity");
126 m_FuelToEnergyRatio = GetCompEM().GetEnergyMax() / m_FuelTankCapacity; // Conversion ratio of 1 ml of fuel to X Energy
127
129 }
130
131 // Generator is working
132 override void OnWorkStart()
133 {
134 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
135 {
136 if (IsInitialized())
137 PlaySoundSet(m_EngineStart, START_SOUND, 0, 0);
138
139 if (!m_SoundLoopStartTimer)
140 m_SoundLoopStartTimer = new Timer(CALL_CATEGORY_SYSTEM);
141
142 if (!m_SoundLoopStartTimer.IsRunning()) // Makes sure the timer is NOT running already
143 m_SoundLoopStartTimer.Run(1.5, this, "StartLoopSound", NULL, false);
144 }
145
146 if (GetGame().IsServer() || !GetGame().IsMultiplayer())
147 m_UTSource.SetDefferedActive(true, 20.0);
148 }
149
150 // Do work
151 override void OnWork(float consumed_energy)
152 {
153 if (GetGame().IsServer() || !GetGame().IsMultiplayer())
155
156 if (GetGame().IsServer())
157 {
158 m_FuelPercentage = GetCompEM().GetEnergy0To100();
159 SetSynchDirty();
160 }
161
163 }
164
165 // Turn off when this runs out of fuel
166 override void OnWorkStop()
167 {
168 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
169 {
170 // Sound
171 PlaySoundSet(m_EngineStop, STOP_SOUND, 0, 0);
172 StopSoundSet(m_EngineLoop);
173
174 // particle
176
177 // Fuel meter
179 }
180
181 if (GetGame().IsServer() || !GetGame().IsMultiplayer())
182 m_UTSource.SetDefferedActive(false, 20.0);
183 }
184
185 // Called when this generator is picked up
187 {
188 super.OnItemLocationChanged(old_owner, new_owner);
190 }
191
192 override void EEItemAttached(EntityAI item, string slot_name)
193 {
194 super.EEItemAttached(item, slot_name);
195 GetCompEM().InteractBranch(this);
196
198
199 if (item_IB.IsKindOf("Sparkplug") && IsInitialized())
200 {
201 ShowSelection("sparkplug_installed");
202
203#ifndef SERVER
204 EffectSound sound = SEffectManager.PlaySound(SPARKPLUG_ATTACH_SOUND, GetPosition());
205 sound.SetAutodestroy(true);
206#endif
207 }
208 }
209
210 override void EEItemDetached(EntityAI item, string slot_name)
211 {
212 super.EEItemDetached(item, slot_name);
213
214 GetCompEM().InteractBranch(this);
215
217
218 if (item_IB.IsKindOf("Sparkplug"))
219 {
220 HideSelection("sparkplug_installed");
221 GetCompEM().SwitchOff();
222
223#ifndef SERVER
224 EffectSound sound = SEffectManager.PlaySound(SPARKPLUG_DETACH_SOUND, GetPosition());
225 sound.SetAutodestroy(true);
226#endif
227 }
228 }
229
230 /*================================
231 FUNCTIONS
232 ================================*/
233
235 {
236 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
237 SetAnimationPhase("dial_fuel", m_FuelPercentage * 0.01);
238 }
239
240 // Adds energy to the generator
242 {
243 if (m_FuelTankCapacity > 0)
244 {
245 m_FuelToEnergyRatio = GetCompEM().GetEnergyMax() / m_FuelTankCapacity;
246 GetCompEM().SetEnergy(fuel_amount * m_FuelToEnergyRatio);
247 m_FuelPercentage = GetCompEM().GetEnergy0To100();
248 SetSynchDirty();
250 }
251 else
252 {
253 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());
254 DPrint(error);
255 }
256 }
257
258 // Adds fuel (energy) to the generator
259 // Returns how much fuel was accepted
261 {
262 if (available_fuel == 0)
263 return 0;
264 GetCompEM().InteractBranch(this);
265 float needed_fuel = GetMaxFuel() - GetFuel();
266
268 {
270 return available_fuel; // Return used fuel amount
271 }
272 else
273 {
275 return needed_fuel;
276 }
277 }
278
279 // Check the bottle if it can be used to fill the tank
281 {
282 if (container)
283 {
284 // Get the liquid
285 int liquid_type = container.GetLiquidType();
286
287 // Do all checks
288 if (container.GetQuantity() > 0 && GetCompEM().GetEnergy() < GetCompEM().GetEnergyMax() && (liquid_type & LIQUID_GASOLINE))
289 return true;
290 }
291
292 return false;
293 }
294
295 // Returns fuel amount
296 float GetFuel()
297 {
298 return GetCompEM().GetEnergy() / m_FuelToEnergyRatio;
299 }
300
301 // Returns max fuel amount
303 {
304 return m_FuelTankCapacity;
305 }
306
307 // Checks sparkplug
309 {
310 int slot = InventorySlots.GetSlotIdFromString("SparkPlug");
311 EntityAI ent = GetInventory().FindAttachment(slot);
312
313 return ent && !ent.IsRuined();
314 }
315
317 {
318 super.OnVariablesSynchronized();
319
321
322 if (IsPlaceSound())
324 }
325
326 //================================================================
327 // ADVANCED PLACEMENT
328 //================================================================
329
330 override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
331 {
332 super.OnPlacementComplete(player, position, orientation);
333
334 SetIsPlaceSound(true);
335 }
336
337 override string GetPlaceSoundset()
338 {
339 return "placePowerGenerator_SoundSet";
340 }
341
352
353 //Debug menu Spawn Ground Special
354 override void OnDebugSpawn()
355 {
357 if (Class.CastTo(entity, this))
358 entity.GetInventory().CreateInInventory("SparkPlug");
359
361 }
362}
363
364
365class PowerGenerator extends PowerGeneratorBase
366{
367
368
369}
eBleedingSourceType GetType()
void AddAction(typename actionName)
ref UniversalTemperatureSourceLambdaEngine m_UTSLEngine
override bool IsInitialized()
ref UniversalTemperatureSourceSettings m_UTSSettings
ref UniversalTemperatureSource m_UTSource
void PlayPlaceSound()
Definition ItemBase.c:9027
void SetIsPlaceSound(bool is_place_sound)
Definition ItemBase.c:8962
float GetEnergy()
Definition ItemBase.c:8140
bool IsPlaceSound()
Definition ItemBase.c:8967
class JsonUndergroundAreaTriggerData GetPosition
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
static proto native int GetSlotIdFromString(string slot_name)
converts string to slot_id
override void EEItemAttached(EntityAI item, string slot_name)
override bool CanPutIntoHands(EntityAI player)
override void OnWork(float consumed_energy)
override void OnWorkStart()
ref UniversalTemperatureSource m_UTSource
DEPRECATED Attached spark plug item.
bool HasSparkplug()
void SetFuel(float fuel_amount)
void PowerGeneratorBase()
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
static float m_FuelTankCapacity
float GetMaxFuel()
override void SetActions()
override void OnInitEnergy()
EffectSound m_EngineStart
Effect m_Smoke
ref UniversalTemperatureSourceSettings m_UTSSettings
EffectSound m_EngineLoop
override void OnWorkStop()
override string GetPlaceSoundset()
override float GetLiquidThroughputCoef()
static float m_FuelToEnergyRatio
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()
ItemBase m_SparkPlug
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
ref Timer m_SoundLoopStartTimer
ref UniversalTemperatureSourceLambdaEngine m_UTSLEngine
float AddFuel(float available_fuel)
void ~PowerGeneratorBase()
float m_Fuel
int m_FuelPercentage
override void EOnInit(IEntity other, int extra)
bool CanManipulate()
bool CanAddFuel(ItemBase container)
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.
original Timer deletes m_params which is unwanted
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 ...
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
EntityEvent
Entity events for event-mask, or throwing event from code.
Definition EnEntity.c:44
const float LIQUID_THROUGHPUT_GENERATOR
Definition constants.c:527
const int LIQUID_GASOLINE
Definition constants.c:508
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8