DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
ChemLight.c
Go to the documentation of this file.
2{
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
13 {
14 return m_Efficiency0To10 / 10;
15 }
16
19 {
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
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
63 RegisterNetSyncVariableInt("m_Efficiency0To10");
64 }
65
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
eBleedingSourceType GetType()
void AddAction(typename actionName)
float GetEnergy()
Definition ItemBase.c:8092
ChemlightLight m_Light
Definition ChemLight.c:6
float GetEfficiencyDecayStart()
Returns efficiency. The value is synchronized from server to all clients and is accurate down to 0....
Definition ChemLight.c:18
override void OnInventoryExit(Man player)
Definition ChemLight.c:106
string m_GlowMaterial
Definition ChemLight.c:4
void Chemlight_ColorBase()
Definition ChemLight.c:46
override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
Definition ChemLight.c:39
override void OnWorkStop()
Definition ChemLight.c:122
override void OnWork(float consumed_energy)
Definition ChemLight.c:141
static private float m_EfficiencyDecayStart
Definition ChemLight.c:9
private int m_Efficiency0To10
Definition ChemLight.c:8
override void OnWorkStart()
Definition ChemLight.c:100
string m_DefaultMaterial
Definition ChemLight.c:3
string GetMaterialForDamageState(bool glowing, int healthLevel=-1)
Definition ChemLight.c:165
float GetEfficiency0To1()
Returns efficiency. The value is synchronized from server to all clients and is accurate down to 0....
Definition ChemLight.c:12
override void SetActions()
Definition ChemLight.c:158
override void OnEnergyConsumed()
Definition ChemLight.c:23
Definition EnMath.c:7
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
proto native CGame GetGame()
void Error(string err)
Messagebox with error message.
Definition EnDebug.c:90
const int STATE_RUINED
Definition constants.c:742
const int STATE_BADLY_DAMAGED
Definition constants.c:743
const int STATE_DAMAGED
Definition constants.c:744
static proto float Round(float f)
Returns mathematical round of value.
proto string Substring(int start, int len)
Substring of 'str' from 'start' position 'len' number of characters.
proto native int IndexOf(string sample)
Finds 'sample' in 'str'. Returns -1 when not found.