Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
chemlight.c
Go to the documentation of this file.
2 {
3  string m_DefaultMaterial;
4  string m_GlowMaterial;
5 
6  ChemlightLight m_Light;
7 
8  private int m_Efficiency0To10; // Synchronized variable
9  static private float m_EfficiencyDecayStart = 0.05; // At this % of maximum energy the output of the light starts to weaken.
10 
12  float GetEfficiency0To1()
13  {
14  return m_Efficiency0To10 / 10;
15  }
16 
18  float GetEfficiencyDecayStart()
19  {
20  return m_EfficiencyDecayStart;
21  }
22 
23  override void OnEnergyConsumed()
24  {
25  super.OnEnergyConsumed();
26 
27  if ( GetGame().IsServer() )
28  {
29  float energy_coef = GetCompEM().GetEnergy0To1();
30 
31  if ( energy_coef < m_EfficiencyDecayStart && m_EfficiencyDecayStart > 0 )
32  {
33  m_Efficiency0To10 = Math.Round( (energy_coef / m_EfficiencyDecayStart) * 10 );
34  SetSynchDirty();
35  }
36  }
37  }
38 
39  override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
40  {
41  super.EEHealthLevelChanged(oldLevel,newLevel,zone);
42 
43  SetObjectMaterial( 0, GetMaterialForDamageState(GetCompEM().IsWorking(),newLevel) );
44  }
45 
46  void Chemlight_ColorBase()
47  {
48  //materials
49  array<string> config_materials = GetHiddenSelectionsMaterials();
50 
51  if (config_materials.Count() == 2)
52  {
53  m_DefaultMaterial = config_materials[0];
54  m_GlowMaterial = config_materials[1];
55  }
56  else
57  {
58  string error = "Error! Item " + GetType() + " must have 2 entries in config array hiddenSelectionsMaterials[]. One for the default state, the other one for the glowing state. Currently it has " + config_materials.Count() + ".";
59  Error(error);
60  }
61 
62  m_Efficiency0To10 = 10;
63  RegisterNetSyncVariableInt("m_Efficiency0To10");
64  }
65 
66  void CreateLight()
67  {
68  SetObjectMaterial( 0, GetMaterialForDamageState(true) ); // must be server side!
69 
70  if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // client side
71  {
72  m_Light = ChemlightLight.Cast( ScriptedLightBase.CreateLight( ChemlightLight, "0 0 0") );
73  m_Light.AttachOnMemoryPoint(this, "light");
74 
75  string type = GetType();
76 
77  switch( type )
78  {
79  case "Chemlight_White":
80  m_Light.SetColorToWhite();
81  break;
82  case "Chemlight_Red":
83  m_Light.SetColorToRed();
84  break;
85  case "Chemlight_Green":
86  m_Light.SetColorToGreen();
87  break;
88  case "Chemlight_Blue":
89  m_Light.SetColorToBlue();
90  break;
91  case "Chemlight_Yellow":
92  m_Light.SetColorToYellow();
93  break;
94 
95  default: { m_Light.SetColorToWhite(); };
96  }
97  }
98  }
99 
100  override void OnWorkStart()
101  {
102 
103  }
104 
105  // Inventory manipulation
106  override void OnInventoryExit(Man player)
107  {
108  super.OnInventoryExit(player);
109 
110  StandUp();
111  }
112 
113  void StandUp()
114  {
115  if ( GetGame().IsServer() && GetCompEM().IsWorking() )
116  {
117  vector ori_rotate = "0 0 0";
118  SetOrientation(ori_rotate);
119  }
120  }
121 
122  override void OnWorkStop()
123  {
124  SetObjectMaterial( 0, GetMaterialForDamageState(false) );
125 
126  if (m_Light)
127  {
128  m_Light.FadeOut();
129  }
130 
131  if ( GetGame().IsServer() )
132  {
133  //Safeguard if item is turned off by another event than running out of energy
134  if (GetCompEM().GetEnergy() > 0)
135  return;
136 
137  SetHealth(0);
138  }
139  }
140 
141  override void OnWork (float consumed_energy)
142  {
143  if (!m_Light)
144  CreateLight();
145 
146  // Handle fade out of chemlight
147  if (m_Light)
148  {
149  float efficiency = GetEfficiency0To1();
150 
151  if ( efficiency < 1 )
152  {
153  m_Light.SetIntensity( efficiency, GetCompEM().GetUpdateInterval() );
154  }
155  }
156  }
157 
158  override void SetActions()
159  {
160  super.SetActions();
161 
163  }
164 
165  string GetMaterialForDamageState(bool glowing,int healthLevel = -1)
166  {
167  int currentHealthLevel;
168  int suffixIndex;
169  string base;
170 
171  if (healthLevel == -1)
172  currentHealthLevel = GetHealthLevel();
173  else
174  currentHealthLevel = healthLevel;
175 
176  if (glowing)
177  base = m_GlowMaterial;
178  else
179  base = m_DefaultMaterial;
180 
181  suffixIndex = base.IndexOf(".rvmat");
182  if (suffixIndex == -1)
183  {
184  Error("Error - no valid rvmat found for chemlight");
185  return "";
186  }
187  base = base.Substring(0,suffixIndex);
188 
189  if (currentHealthLevel == GameConstants.STATE_BADLY_DAMAGED || currentHealthLevel == GameConstants.STATE_DAMAGED)
190  {
191  base = base + "_damage";
192  }
193  else if (currentHealthLevel == GameConstants.STATE_RUINED)
194  {
195  base = base + "_destruct";
196  }
197 
198  return base + ".rvmat";
199  }
200 };
201 
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
Error
void Error(string err)
Messagebox with error message.
Definition: endebug.c:90
ActionTurnOnWhileInHands
Definition: actionturnonwhileinhands.c:1
Chemlight_Red
Definition: chemlight.c:203
Chemlight_White
Definition: chemlight.c:202
m_Light
protected ExplosiveLight m_Light
light
Definition: explosivesbase.c:31
vector
Definition: enconvert.c:105
Chemlight_ColorBase
Definition: chemlight.c:1
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
ScriptedLightBase
Definition: pointlightbase.c:1
array< string >
Chemlight_Yellow
Definition: chemlight.c:206
GameConstants
Definition: constants.c:612
GetEnergy
float GetEnergy()
Definition: itembase.c:3461
Math
Definition: enmath.c:6
Chemlight_Blue
Definition: chemlight.c:205
Chemlight_Green
Definition: chemlight.c:204
GetType
override int GetType()
Definition: huddebugwincharagents.c:49