DayZ 1.24
Loading...
Searching...
No Matches
BatteryCharger.c
Go to the documentation of this file.
1class BatteryCharger extends ItemBase
2{
3 // Model selections
4 static protected const string SEL_CLIPS_CAR = "clips_car_battery";
5 static protected const string SEL_CLIPS_TRUCK = "clips_truck_battery";
6 static protected const string SEL_CLIPS_DETACHED = "clips_detached";
7 static protected const string SEL_CLIPS_FOLDED = "clips_folded";
8 static protected const string SEL_SWITCH_ON = "switch_on";
9 static protected const string SEL_SWITCH_OFF = "switch_off";
10 static protected const string SEL_CORD_PLUGGED = "cord_plugged";
11 static protected const string SEL_CORD_FOLDED = "cord_folded";
12 static protected const string SEL_LIGHT_STATE_1 = "light_stand_by";
13 static protected const string SEL_LIGHT_STATE_2 = "light_charging";
14 static protected const string SEL_LIGHT_STATE_3 = "light_charged";
15
16 // glow materials
17 static protected const string RED_LIGHT_GLOW = "dz\\gear\\camping\\data\\battery_charger_light_r.rvmat";
18 static protected const string GREEN_LIGHT_GLOW = "dz\\gear\\camping\\data\\battery_charger_light_g.rvmat";
19 static protected const string YELLOW_LIGHT_GLOW = "dz\\gear\\camping\\data\\battery_charger_light_y.rvmat";
20 static protected const string SWITCH_LIGHT_GLOW = "dz\\gear\\camping\\data\\battery_charger_light_switch_on.rvmat";
21 static protected const string DEFAULT_MATERIAL = "dz\\gear\\camping\\data\\battery_charger.rvmat";
22
23 protected const string ATTACHED_CLIPS_STATES[] = {SEL_CLIPS_CAR, SEL_CLIPS_TRUCK}; // TO DO: If it's required by design, add helicopter battery here and register its selection names.
24 protected const int ATTACHED_CLIPS_STATES_COUNT = 2; // Reffers to this ^ array
25
26
28 protected float m_ChargeEnergyPerSecond;
29
30 static protected float m_BlinkingStatusLightInterval = 0.4; // How often the lights blink
32 protected bool m_BlinkingStatusLightIsOn = false; // Status of one blinking light
33
35 {
36 m_ChargeEnergyPerSecond = GetGame().ConfigGetFloat("CfgVehicles " + GetType() + " ChargeEnergyPerSecond");
37 m_UpdateStatusLightsTimer = new Timer(CALL_CATEGORY_SYSTEM);
39 RegisterNetSyncVariableInt("m_BatteryEnergy0To100");
40 RegisterNetSyncVariableBool("m_IsSoundSynchRemote");
41 RegisterNetSyncVariableBool("m_IsPlaceSound");
42 }
43
44 override bool IsElectricAppliance()
45 {
46 return true;
47 }
48
49 override void OnWork(float consumed_energy)
50 {
51 // Charging functionality
52 ItemBase battery = ItemBase.Cast(GetCompEM().GetPluggedDevice());
53
54 if (battery)
55 {
56 if (GetGame().IsServer())
57 {
58 float battery_capacity = battery.GetCompEM().GetEnergyMax();
59
60 if (battery.GetCompEM().GetEnergy() < battery_capacity)
61 {
62 // Heat up the items so players know they are working.
63 this.SetTemperature(60);
64 battery.SetTemperature(60);
65
66 float charger_health = GetHealth("", "");
67 float energy_add = m_ChargeEnergyPerSecond * (consumed_energy / GetCompEM().GetEnergyUsage());
68
69#ifdef DIAG_DEVELOPER
70 if (FeatureTimeAccel.GetFeatureTimeAccelEnabled(ETimeAccelCategories.ENERGY_RECHARGE))
71 {
72 float timeAccel = FeatureTimeAccel.GetFeatureTimeAccelValue();
74 }
75#endif
76
77 if (GetCompEM().ConsumeEnergy(energy_add)) // consumes energy from the power source
78 {
79 // There is enough of energy to use
80 energy_add = energy_add * (0.5 + charger_health * 0.005); // Damaged charger works less efficiently - 50% damage causes 75% efficiency
81 }
82 else
83 {
84 // There is NOT enough of energy to use
85 energy_add = 0;
86 }
87
88 battery.GetCompEM().AddEnergy(energy_add);
89 }
90 else
91 battery.GetCompEM().SetEnergy(battery_capacity);
92 m_BatteryEnergy0To100 = battery.GetCompEM().GetEnergy0To100();
93 SetSynchDirty();
94 }
95 }
96 }
97
98 override void OnWorkStart()
99 {
100 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
101 {
103 m_UpdateStatusLightsTimer.Run(m_BlinkingStatusLightInterval / 2, this, "UpdateStatusLights", NULL, true);
104 }
105 }
106
107 override void OnWorkStop()
108 {
109 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
110 {
112 m_UpdateStatusLightsTimer.Stop();
113 }
114 }
115
117 {
118 if (GetGame().IsClient() || !GetGame().IsMultiplayer())
119 {
120 if (GetCompEM().IsWorking())
121 {
123 ItemBase battery = ItemBase.Cast(GetCompEM().GetPluggedDevice());
124
125 if (battery)
126 {
127 RedLightOff();
128
129 if (m_BatteryEnergy0To100 <= 33)
130 {
131 // Less than 1/3 charged, yellow status light must repeatedly blink
132
133 if (m_BlinkingStatusLightIsOn)
135 else
137
138 m_BlinkingStatusLightIsOn = !m_BlinkingStatusLightIsOn;
139 }
140 else if (m_BatteryEnergy0To100 > 33 && m_BatteryEnergy0To100 <= 66)
141 {
142 // Less than 2/3 charged, yellow status light must glow
143
145 }
146 else if (m_BatteryEnergy0To100 > 66 && m_BatteryEnergy0To100 < 100)
147 {
148 // Less than 3/3 charged, yellow status light must glow, green light must blink
149
151
152 if (m_BlinkingStatusLightIsOn)
153 GreenLightOn();
154 else
156
157 m_BlinkingStatusLightIsOn = !m_BlinkingStatusLightIsOn;
158 }
159 else if (m_BatteryEnergy0To100 >= 100)
160 {
161 // Fully charged, green light must glow
163 GreenLightOn();
164 }
165 }
166 else
167 {
168 if (m_BlinkingStatusLightIsOn)
169 RedLightOn();
170 else
171 RedLightOff();
172
173 m_BlinkingStatusLightIsOn = !m_BlinkingStatusLightIsOn;
174
177 }
178 }
179 else
180 {
183 RedLightOff();
185 }
186 }
187 }
188
189 override bool CanPutInCargo(EntityAI parent)
190 {
191 if (!super.CanPutInCargo(parent)) return false;
192 // No "Take" action if the item is connected
193 if (!GetCompEM().IsPlugged() && !GetCompEM().GetPluggedDevice())
194 return true;
195
196 return false;
197 }
198
200 {
201 if (!super.CanPutIntoHands(parent))
202 return false;
203 // No "Take into hands" action if the item is connected
204 if (!GetCompEM().IsPlugged() && !GetCompEM().GetPluggedDevice())
205 return true;
206
207 return false;
208 }
209
211 {
212 string att_type = device.GetType();
213
214 if (att_type == "CarBattery")
215 {
217 ShowSelection(SEL_CLIPS_CAR);
218 }
219
220 if (att_type == "TruckBattery")
221 {
223 ShowSelection(SEL_CLIPS_TRUCK);
224 }
225
226 HideSelection(SEL_CLIPS_DETACHED);
227 HideSelection(SEL_CLIPS_FOLDED);
228 }
229
231 {
233 ShowSelection(SEL_CLIPS_DETACHED);
234 }
235
237 {
238 if (!super.CanReceiveAttachment(attachment, slotId))
239 return false;
240
243
244 // No attaching if the charger is in inventory!
245 PlayerBase charger_owner = PlayerBase.Cast(GetHierarchyRootPlayer());
246 if (charger_owner)
247 return false;
248
249 // Only one attachment allowed
250 if (GetCompEM().GetPluggedDevice())
251 return false;
252
253 if (ibase.HasEnergyManager() && ibase.GetCompEM().GetPluggedDevicesCount() >= 1) // Make sure nothing is plugged into the battery
254 return false;
255
256 return true;
257 }
258
260 {
261 if (!super.CanLoadAttachment(attachment))
262 return false;
263
266
267 // Only one attachment allowed
268 if (GetCompEM().GetPluggedDevice())
269 return false;
270
271 if (ibase.HasEnergyManager() && ibase.GetCompEM().GetPluggedDevicesCount() >= 1) // Make sure nothing is plugged into the battery
272 return false;
273
274 return true;
275 }
276
278 {
279 for (int i = 0; i < ATTACHED_CLIPS_STATES_COUNT; i++)
280 {
281 string selection = ATTACHED_CLIPS_STATES[i];
282 HideSelection(selection);
283 }
284 }
285
286
287 // Control of status lights
288 // ON
290 {
291 SetObjectMaterial(0, RED_LIGHT_GLOW);
292 }
294 {
295 SetObjectMaterial(2, GREEN_LIGHT_GLOW);
296 }
298 {
299 SetObjectMaterial(1, YELLOW_LIGHT_GLOW);
300 }
302 {
303 SetObjectMaterial(3, SWITCH_LIGHT_GLOW);
304 }
305 // OFF
307 {
308 SetObjectMaterial(0, DEFAULT_MATERIAL);
309 }
311 {
312 SetObjectMaterial(2, DEFAULT_MATERIAL);
313 }
315 {
316 SetObjectMaterial(1, DEFAULT_MATERIAL);
317 }
319 {
320 SetObjectMaterial(3, DEFAULT_MATERIAL);
321 }
322
323
324 override void OnSwitchOn()
325 {
326 HideSelection(SEL_SWITCH_OFF);
327 ShowSelection(SEL_SWITCH_ON);
328 }
329
330 override void OnSwitchOff()
331 {
332 HideSelection(SEL_SWITCH_ON);
333 ShowSelection(SEL_SWITCH_OFF);
334 }
335
336 // Inventory manipulation
337 override void OnInventoryExit(Man player)
338 {
339 super.OnInventoryExit(player);
340
342 HideSelection(SEL_CLIPS_FOLDED);
343 ShowSelection(SEL_CLIPS_DETACHED);
344 }
345
346 override void OnInventoryEnter(Man player)
347 {
348 super.OnInventoryEnter(player);
349
351 HideSelection(SEL_CLIPS_DETACHED);
352 ShowSelection(SEL_CLIPS_FOLDED);
353 }
354
356 {
357 super.OnVariablesSynchronized();
358
359 if (IsPlaceSound())
361 }
362
363 override void RefreshPhysics()
364 {
365 super.RefreshPhysics();
366
367 if (GetAttachmentByType(CarBattery))
368 {
369 RemoveProxyPhysics("battery");
370 AddProxyPhysics("battery");
371 }
372 else
373 RemoveProxyPhysics("battery");
374 }
375
376 //================================================================
377 // ADVANCED PLACEMENT
378 //================================================================
379
380 override void OnPlacementStarted(Man player)
381 {
382 super.OnPlacementStarted(player);
383
384 SetAnimationPhase(SEL_CLIPS_DETACHED, 0);
385 SetAnimationPhase(SEL_CLIPS_FOLDED, 1);
386 SetAnimationPhase(SEL_SWITCH_ON, 1);
387 SetAnimationPhase(SEL_SWITCH_OFF, 1);
388 SetAnimationPhase(SEL_LIGHT_STATE_1, 1);
389 SetAnimationPhase(SEL_LIGHT_STATE_2, 1);
390 SetAnimationPhase(SEL_LIGHT_STATE_3, 1);
391
396 SEL_CLIPS_FOLDED
397 };
398
400 foreach (string selection : selections)
401 {
402 if (GetGame().IsMultiplayer() && GetGame().IsServer())
403 playerPB.GetHologramServer().SetSelectionToRefresh(selection);
404 else
405 playerPB.GetHologramLocal().SetSelectionToRefresh(selection);
406 }
407 }
408
409 override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
410 {
411 super.OnPlacementComplete(player, position, orientation);
412
413 SetIsPlaceSound(true);
414 }
415
416 override bool IsDeployable()
417 {
418 return true;
419 }
420
421 override string GetPlaceSoundset()
422 {
423 return "placeBatteryCharger_SoundSet";
424 }
425
437}
eBleedingSourceType GetType()
void AddAction(typename actionName)
override void SetTemperature(float value, bool allow_client=false)
Definition ItemBase.c:8178
void PlayPlaceSound()
Definition ItemBase.c:9027
void SetIsPlaceSound(bool is_place_sound)
Definition ItemBase.c:8962
bool IsPlaceSound()
Definition ItemBase.c:8967
Super root of all classes in Enforce script.
Definition EnScript.c:11
override bool CanPutIntoHands(EntityAI player)
override void OnSwitchOn()
override void OnInventoryEnter(Man player)
void GreenLightOn()
override void OnWork(float consumed_energy)
override void OnWorkStart()
override void OnOwnSocketReleased(EntityAI device)
override void OnOwnSocketTaken(EntityAI device)
void GreenLightOff()
override void OnPlacementStarted(Man player)
override bool IsElectricAppliance()
override void SetActions()
void SwitchLightOff()
override bool CanLoadAttachment(EntityAI attachment)
const string SEL_CORD_PLUGGED
void UpdateStatusLights()
override void OnWorkStop()
int m_BatteryEnergy0To100
override string GetPlaceSoundset()
void HideAttachedClipsStates()
ref Timer m_UpdateStatusLightsTimer
float m_ChargeEnergyPerSecond
override void OnVariablesSynchronized()
override void RefreshPhysics()
bool m_BlinkingStatusLightIsOn
override void OnSwitchOff()
const int ATTACHED_CLIPS_STATES_COUNT
const string SEL_CORD_FOLDED
override bool CanReceiveAttachment(EntityAI attachment, int slotId)
override bool CanPutInCargo(EntityAI parent)
const string SEL_CLIPS_DETACHED
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
override void OnInventoryExit(Man player)
void RedLightOff()
void YellowLightOn()
void SwitchLightOn()
const string SEL_CLIPS_CAR
void BatteryCharger()
void RedLightOn()
override bool IsDeployable()
void YellowLightOff()
proto native CGame GetGame()
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8