Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
batterycharger.c
Go to the documentation of this file.
1 class 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 
27 
28  int m_BatteryEnergy0To100;
29  protected float m_ChargeEnergyPerSecond;
30 
31  static protected float m_BlinkingStatusLightInterval = 0.4; // How often the lights blink
32  ref Timer m_UpdateStatusLightsTimer;
33  protected bool m_BlinkingStatusLightIsOn = false; // Status of one blinking light
34 
35  void BatteryCharger()
36  {
37  m_ChargeEnergyPerSecond = GetGame().ConfigGetFloat ("CfgVehicles " + GetType() + " ChargeEnergyPerSecond");
38  m_UpdateStatusLightsTimer = new Timer( CALL_CATEGORY_SYSTEM );
39  SwitchLightOff();
40  RegisterNetSyncVariableInt("m_BatteryEnergy0To100");
41  RegisterNetSyncVariableBool("m_IsSoundSynchRemote");
42  RegisterNetSyncVariableBool("m_IsPlaceSound");
43  }
44 
45  override bool IsElectricAppliance()
46  {
47  return true;
48  }
49 
50  override void OnWork( float consumed_energy )
51  {
52  // Charging functionality
53  ItemBase battery = ItemBase.Cast( GetCompEM().GetPluggedDevice() );
54 
55  if ( battery )
56  {
57  if ( GetGame().IsServer() )
58  {
59  float battery_capacity = battery.GetCompEM().GetEnergyMax();
60 
61  if ( battery.GetCompEM().GetEnergy() < battery_capacity )
62  {
63  // Heat up the items so players know they are working.
64  this.SetTemperature(60);
65  battery.SetTemperature(60);
66 
67  float charger_health = GetHealth("", "");
68  float energy_add = m_ChargeEnergyPerSecond * ( consumed_energy / GetCompEM().GetEnergyUsage() );
69 
70  #ifdef DIAG_DEVELOPER
71  if (FeatureTimeAccel.GetFeatureTimeAccelEnabled(ETimeAccelCategories.ENERGY_RECHARGE))
72  {
73  float timeAccel = FeatureTimeAccel.GetFeatureTimeAccelValue();
74  energy_add *= timeAccel;
75  }
76  #endif
77 
78  if ( GetCompEM().ConsumeEnergy(energy_add) ) // consumes energy from the power source
79  {
80  // There is enough of energy to use
81  energy_add = energy_add * ( 0.5 + charger_health*0.005 ); // Damaged charger works less efficiently - 50% damage causes 75% efficiency
82  }
83  else
84  {
85  // There is NOT enough of energy to use
86  energy_add = 0;
87  }
88 
89  battery.GetCompEM().AddEnergy( energy_add );
90  }
91  else
92  {
93  battery.GetCompEM().SetEnergy( battery_capacity );
94  }
95  m_BatteryEnergy0To100 = battery.GetCompEM().GetEnergy0To100();
96  SetSynchDirty();
97  }
98  }
99  }
100 
101  override void OnWorkStart()
102  {
103  if ( GetGame().IsClient() || !GetGame().IsMultiplayer() )
104  {
105  UpdateStatusLights();
106  m_UpdateStatusLightsTimer.Run( m_BlinkingStatusLightInterval/2 , this, "UpdateStatusLights", NULL, true);
107  }
108  }
109 
110  override void OnWorkStop()
111  {
112  if ( GetGame().IsClient() || !GetGame().IsMultiplayer() )
113  {
114  UpdateStatusLights();
115  m_UpdateStatusLightsTimer.Stop();
116  }
117  }
118 
119  void UpdateStatusLights()
120  {
121  if ( GetGame().IsClient() || !GetGame().IsMultiplayer() )
122  {
123  if (GetCompEM().IsWorking())
124  {
125  SwitchLightOn();
126  ItemBase battery = ItemBase.Cast( GetCompEM().GetPluggedDevice() );
127 
128  if (battery)
129  {
130  RedLightOff();
131 
132  if (m_BatteryEnergy0To100 <= 33)
133  {
134  // Less than 1/3 charged, yellow status light must repeatedly blink
135 
136  if (m_BlinkingStatusLightIsOn)
137  YellowLightOn();
138  else
139  YellowLightOff();
140 
141  m_BlinkingStatusLightIsOn = !m_BlinkingStatusLightIsOn;
142  }
143  else if (m_BatteryEnergy0To100 > 33 && m_BatteryEnergy0To100 <= 66)
144  {
145  // Less than 2/3 charged, yellow status light must glow
146 
147  YellowLightOn();
148  }
149  else if (m_BatteryEnergy0To100 > 66 && m_BatteryEnergy0To100 < 100)
150  {
151  // Less than 3/3 charged, yellow status light must glow, green light must blink
152 
153  YellowLightOn();
154 
155  if (m_BlinkingStatusLightIsOn)
156  GreenLightOn();
157  else
158  GreenLightOff();
159 
160  m_BlinkingStatusLightIsOn = !m_BlinkingStatusLightIsOn;
161  }
162  else if (m_BatteryEnergy0To100 >= 100)
163  {
164  // Fully charged, green light must glow
165  YellowLightOff();
166  GreenLightOn();
167  }
168  }
169  else
170  {
171  if (m_BlinkingStatusLightIsOn)
172  RedLightOn();
173  else
174  RedLightOff();
175 
176  m_BlinkingStatusLightIsOn = !m_BlinkingStatusLightIsOn;
177 
178  GreenLightOff();
179  YellowLightOff();
180  }
181  }
182  else
183  {
184  SwitchLightOff();
185  GreenLightOff();
186  RedLightOff();
187  YellowLightOff();
188  }
189  }
190  }
191 
192  override bool CanPutInCargo( EntityAI parent )
193  {
194  if( !super.CanPutInCargo(parent) ) {return false;}
195  // No "Take" action if the item is connected
196  if ( !GetCompEM().IsPlugged() && !GetCompEM().GetPluggedDevice() )
197  {
198  return true;
199  }
200 
201  return false;
202  }
203 
204  override bool CanPutIntoHands( EntityAI player )
205  {
206  if( !super.CanPutIntoHands( parent ) )
207  {
208  return false;
209  }
210  // No "Take into hands" action if the item is connected
211  if ( !GetCompEM().IsPlugged() && !GetCompEM().GetPluggedDevice() )
212  {
213  return true;
214  }
215 
216  return false;
217  }
218 
219  override void OnOwnSocketTaken( EntityAI device )
220  {
221  string att_type = device.GetType();
222 
223  if ( att_type == "CarBattery" )
224  {
225  HideAttachedClipsStates();
226  ShowSelection(SEL_CLIPS_CAR);
227  }
228 
229  if ( att_type == "TruckBattery" )
230  {
231  HideAttachedClipsStates();
232  ShowSelection(SEL_CLIPS_TRUCK);
233  }
234 
235  HideSelection(SEL_CLIPS_DETACHED);
236  HideSelection(SEL_CLIPS_FOLDED);
237  }
238 
239  override void OnOwnSocketReleased( EntityAI device )
240  {
241  HideAttachedClipsStates();
242  ShowSelection(SEL_CLIPS_DETACHED);
243  }
244 
245  override bool CanReceiveAttachment( EntityAI attachment, int slotId )
246  {
247  if ( !super.CanReceiveAttachment(attachment, slotId) )
248  return false;
249 
250  ItemBase ibase;
251  Class.CastTo(ibase, attachment);
252 
253  // No attaching if the charger is in inventory!
254  PlayerBase charger_owner = PlayerBase.Cast( GetHierarchyRootPlayer() );
255  if ( charger_owner )
256  return false;
257 
258  // Only one attachment allowed
259  if ( GetCompEM().GetPluggedDevice() )
260  return false;
261 
262  if ( ibase.HasEnergyManager() && ibase.GetCompEM().GetPluggedDevicesCount() >= 1 ) // Make sure nothing is plugged into the battery
263  return false;
264 
265  return true;
266  }
267 
268  override bool CanLoadAttachment( EntityAI attachment)
269  {
270  if ( !super.CanLoadAttachment(attachment) )
271  return false;
272 
273  ItemBase ibase;
274  Class.CastTo(ibase, attachment);
275 
276  // Only one attachment allowed
277  if ( GetCompEM().GetPluggedDevice() )
278  return false;
279 
280  if ( ibase.HasEnergyManager() && ibase.GetCompEM().GetPluggedDevicesCount() >= 1 ) // Make sure nothing is plugged into the battery
281  return false;
282 
283  return true;
284  }
285 
286  void HideAttachedClipsStates()
287  {
288  for ( int i = 0; i < ATTACHED_CLIPS_STATES_COUNT; i++ )
289  {
290  string selection = ATTACHED_CLIPS_STATES[i];
291  HideSelection(selection);
292  }
293  }
294 
295 
296 
297  // Control of status lights
298  // ON
299  void RedLightOn()
300  {
301  SetObjectMaterial( 0, RED_LIGHT_GLOW );
302  }
303  void GreenLightOn()
304  {
305  SetObjectMaterial( 2, GREEN_LIGHT_GLOW );
306  }
307  void YellowLightOn()
308  {
309  SetObjectMaterial( 1, YELLOW_LIGHT_GLOW );
310  }
311  void SwitchLightOn()
312  {
313  SetObjectMaterial( 3, SWITCH_LIGHT_GLOW );
314  }
315  // OFF
316  void RedLightOff()
317  {
318  SetObjectMaterial( 0, DEFAULT_MATERIAL );
319  }
320  void GreenLightOff()
321  {
322  SetObjectMaterial( 2, DEFAULT_MATERIAL );
323  }
324  void YellowLightOff()
325  {
326  SetObjectMaterial( 1, DEFAULT_MATERIAL );
327  }
328  void SwitchLightOff()
329  {
330  SetObjectMaterial( 3, DEFAULT_MATERIAL );
331  }
332 
333 
334  override void OnSwitchOn()
335  {
336  HideSelection(SEL_SWITCH_OFF);
337  ShowSelection(SEL_SWITCH_ON);
338  }
339 
340  override void OnSwitchOff()
341  {
342  HideSelection(SEL_SWITCH_ON);
343  ShowSelection(SEL_SWITCH_OFF);
344  }
345 
346  // Inventory manipulation
347  override void OnInventoryExit(Man player)
348  {
349  super.OnInventoryExit(player);
350 
351  HideAttachedClipsStates();
352  HideSelection(SEL_CLIPS_FOLDED);
353  ShowSelection(SEL_CLIPS_DETACHED);
354  }
355 
356  override void OnInventoryEnter(Man player)
357  {
358  super.OnInventoryEnter(player);
359 
360  HideAttachedClipsStates();
361  HideSelection(SEL_CLIPS_DETACHED);
362  ShowSelection(SEL_CLIPS_FOLDED);
363  }
364 
365  override void OnVariablesSynchronized()
366  {
367  super.OnVariablesSynchronized();
368 
369  if ( IsPlaceSound() )
370  {
371  PlayPlaceSound();
372  }
373  }
374 
375  override void RefreshPhysics()
376  {
377  super.RefreshPhysics();
378 
379  if ( GetAttachmentByType(CarBattery) )
380  {
381  RemoveProxyPhysics( "battery" );
382  AddProxyPhysics( "battery" );
383  }
384  else
385  RemoveProxyPhysics( "battery" );
386  }
387 
388  //================================================================
389  // ADVANCED PLACEMENT
390  //================================================================
391 
392  override void OnPlacementStarted(Man player)
393  {
394  super.OnPlacementStarted(player);
395 
396  SetAnimationPhase(SEL_CLIPS_DETACHED, 0);
397  SetAnimationPhase(SEL_CLIPS_FOLDED, 1);
398  SetAnimationPhase(SEL_SWITCH_ON, 1);
399  SetAnimationPhase(SEL_SWITCH_OFF, 1);
400  SetAnimationPhase(SEL_LIGHT_STATE_1, 1);
401  SetAnimationPhase(SEL_LIGHT_STATE_2, 1);
402  SetAnimationPhase(SEL_LIGHT_STATE_3, 1);
403 
404  array<string> selections = {
405  SEL_CORD_PLUGGED,
406  SEL_CORD_FOLDED,
407  SEL_CLIPS_DETACHED,
408  SEL_CLIPS_FOLDED
409  };
410 
411  PlayerBase playerPB = PlayerBase.Cast(player);
412  foreach (string selection : selections)
413  {
414  if (GetGame().IsMultiplayer() && GetGame().IsServer())
415  playerPB.GetHologramServer().SetSelectionToRefresh(selection);
416  else
417  playerPB.GetHologramLocal().SetSelectionToRefresh(selection);
418  }
419  }
420 
421  override void OnPlacementComplete( Man player, vector position = "0 0 0", vector orientation = "0 0 0" )
422  {
423  super.OnPlacementComplete( player, position, orientation );
424 
425  SetIsPlaceSound( true );
426  }
427 
428  override bool IsDeployable()
429  {
430  return true;
431  }
432 
433  override string GetPlaceSoundset()
434  {
435  return "placeBatteryCharger_SoundSet";
436  }
437 
438  override void SetActions()
439  {
440  super.SetActions();
441 
448  }
449 }
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
CALL_CATEGORY_SYSTEM
const int CALL_CATEGORY_SYSTEM
Definition: tools.c:8
OnPlacementStarted
override void OnPlacementStarted(Man player)
Definition: itembase.c:3804
ActionPlugIn
Definition: actionplugin.c:1
ActionPlaceObject
Definition: actionplaceobject.c:9
OnWorkStop
override void OnWorkStop()
Definition: m18smokegrenade_colorbase.c:2
OnVariablesSynchronized
override void OnVariablesSynchronized()
Definition: anniversarymusicsource.c:42
IsPlaceSound
bool IsPlaceSound()
Definition: itembase.c:4288
OnInventoryEnter
override protected void OnInventoryEnter(Man player)
Definition: fireworksbase.c:71
OnInventoryExit
override protected void OnInventoryExit(Man player)
Definition: fireworksbase.c:79
OnPlacementComplete
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
Definition: explosivesbase.c:133
RefreshPhysics
void RefreshPhysics()
ActionTogglePlaceObject
Definition: actiontoggleplaceobject.c:1
PlayerBase
Definition: playerbaseclient.c:1
CanPutIntoHands
override bool CanPutIntoHands(EntityAI parent)
Definition: explosivesbase.c:257
vector
Definition: enconvert.c:105
CanPutInCargo
override bool CanPutInCargo(EntityAI parent)
Definition: explosivesbase.c:247
CarBattery
Definition: carbattery.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
ActionUnplugThisByCord
Definition: actionunplugthisbycord.c:1
array< string >
IsDeployable
override bool IsDeployable()
Definition: basebuildingbase.c:339
SetTemperature
override void SetTemperature(float value, bool allow_client=false)
Definition: itembase.c:3499
Timer
Definition: dayzplayerimplement.c:62
CanLoadAttachment
override bool CanLoadAttachment(EntityAI attachment)
Definition: container_base.c:64
ActionTurnOffWhileOnGround
Definition: actionturnoffwhileonground.c:1
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
CanReceiveAttachment
override bool CanReceiveAttachment(EntityAI attachment, int slotId)
Definition: basebuildingbase.c:895
EntityAI
Definition: building.c:5
ActionTurnOnWhileOnGround
Definition: actionturnonwhileonground.c:1
GetPlaceSoundset
override string GetPlaceSoundset()
Definition: fireplacebase.c:2574
GetType
override int GetType()
Definition: huddebugwincharagents.c:49
OnWorkStart
override void OnWorkStart()
Definition: smokegrenadebase.c:175