DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
PowerGenerator.c
Go to the documentation of this file.
1class PowerGenerator 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 {
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();
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
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
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
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
190
191 // Fuel meter
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);
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
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();
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 {
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
325 {
326 return m_FuelTankCapacity;
327 }
328
329 // Checks sparkplug
331 {
332 int slot = InventorySlots.GetSlotIdFromString("SparkPlug");
333 EntityAI ent = GetInventory().FindAttachment(slot);
334
335 return ent && !ent.IsRuined();
336 }
337
339 {
340 super.OnVariablesSynchronized();
341
343
344 if (IsPlaceSound())
345 {
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
387 }
388}
eBleedingSourceType GetType()
void AddAction(typename actionName)
protected ref UniversalTemperatureSourceLambdaEngine m_UTSLEngine
override bool IsInitialized()
void Effect()
ctor
Definition Effect.c:70
protected ref UniversalTemperatureSourceSettings m_UTSSettings
protected ref UniversalTemperatureSource m_UTSource
float GetEnergy()
Definition ItemBase.c:8092
void SetIsPlaceSound(bool is_place_sound)
Definition ItemBase.c:8900
void PlayPlaceSound()
Definition ItemBase.c:8938
bool IsPlaceSound()
Definition ItemBase.c:8905
class JsonUndergroundAreaTriggerData GetPosition
proto native float ConfigGetFloat(string path)
Get float value from config on path.
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
static proto native int GetSlotIdFromString(string slot_name)
converts string to slot_id
protected ref UniversalTemperatureSourceSettings m_UTSSettings
float GetMaxFuel()
bool HasSparkplug()
override void OnDebugSpawn()
void StartLoopSound()
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
protected EffectSound m_EngineStart
override void OnInitEnergy()
void SetFuel(float fuel_amount)
float GetFuel()
static private float m_FuelToEnergyRatio
void PowerGenerator()
override void EEInit()
static private float m_FuelTankCapacity
override bool CanPutIntoHands(EntityAI player)
override bool CanPutInCargo(EntityAI parent)
override void OnWorkStop()
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
override void EEItemDetached(EntityAI item, string slot_name)
override string GetPlaceSoundset()
float AddFuel(float available_fuel)
override void OnWork(float consumed_energy)
protected ref UniversalTemperatureSource m_UTSource
DEPRECATED Attached spark plug item.
void UpdateFuelMeter()
ref Timer m_SoundLoopStartTimer
bool CanAddFuel(ItemBase container)
private int m_FuelPercentage
void ~PowerGenerator()
bool CanManipulate()
ItemBase m_SparkPlug
override void EOnInit(IEntity other, int extra)
override void OnWorkStart()
override void EEItemAttached(EntityAI item, string slot_name)
protected EffectSound m_EngineStop
override void OnVariablesSynchronized()
protected EffectSound m_EngineLoop
float m_Fuel
override float GetLiquidThroughputCoef()
protected ref UniversalTemperatureSourceLambdaEngine m_UTSLEngine
ref protected Effect m_Smoke
override void SetActions()
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.
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.
original Timer deletes m_params which is unwanted
float m_TemperatureMax
min temperature you can get from the TemperatureSource
float m_RangeFull
temperature cap that will limit the return value from GetTemperature method
bool m_ManualUpdate
if the Update is running periodically
float m_TemperatureMin
how often the Update is ticking
float m_RangeMax
range where the full temperature is given to receiver
float m_TemperatureCap
max temperature you can get from the TemperatureSource
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:45
const float LIQUID_THROUGHPUT_GENERATOR
Definition constants.c:512
const int LIQUID_GASOLINE
Definition constants.c:493
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8