File size: 4,781 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class Chemlight_ColorBase : ItemBase
{
	string 					m_DefaultMaterial;
	string 					m_GlowMaterial;
	
	ChemlightLight 			m_Light;
	
	private int				m_Efficiency0To10; // Synchronized variable
	static private float 	m_EfficiencyDecayStart = 0.05; // At this % of maximum energy the output of the light starts to weaken.
	
	//! Returns efficiency. The value is synchronized from server to all clients and is accurate down to 0.1 units.
	float GetEfficiency0To1()
	{
		return m_Efficiency0To10 / 10;
	}
	
	//! Returns efficiency. The value is synchronized from server to all clients and is accurate down to 0.1 units.
	float GetEfficiencyDecayStart()
	{
		return m_EfficiencyDecayStart;
	}
	
	override void OnEnergyConsumed()
	{
		super.OnEnergyConsumed();
		
		if ( GetGame().IsServer() )
		{
			float energy_coef = GetCompEM().GetEnergy0To1();
			
			if ( energy_coef < m_EfficiencyDecayStart  &&  m_EfficiencyDecayStart > 0 )
			{
				m_Efficiency0To10 = Math.Round(  (energy_coef / m_EfficiencyDecayStart) * 10  );
				SetSynchDirty();
			}
		}
	}
	
	override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
	{
		super.EEHealthLevelChanged(oldLevel,newLevel,zone);
		
		SetObjectMaterial( 0, GetMaterialForDamageState(GetCompEM().IsWorking(),newLevel) );
	}
	
	void Chemlight_ColorBase()
	{
		//materials
		array<string> config_materials	= GetHiddenSelectionsMaterials();

		if (config_materials.Count() == 2)
		{
			m_DefaultMaterial = config_materials[0];
			m_GlowMaterial = config_materials[1];
		}
		else
		{
			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() + ".";
			Error(error);
		}
		
		m_Efficiency0To10 = 10;
		RegisterNetSyncVariableInt("m_Efficiency0To10");
	}
	
	void CreateLight()
	{
		SetObjectMaterial( 0, GetMaterialForDamageState(true) ); // must be server side!
		
		if ( !GetGame().IsServer()  ||  !GetGame().IsMultiplayer() ) // client side
		{
			m_Light = ChemlightLight.Cast( ScriptedLightBase.CreateLight( ChemlightLight, "0 0 0") );
			m_Light.AttachOnMemoryPoint(this, "light");
			
			string type = GetType();
			
			switch( type )
			{
				case "Chemlight_White": 
					m_Light.SetColorToWhite();
					break;
				case "Chemlight_Red": 
					m_Light.SetColorToRed();
					break;
				case "Chemlight_Green": 
					m_Light.SetColorToGreen();
					break;
				case "Chemlight_Blue": 
					m_Light.SetColorToBlue();
					break;
				case "Chemlight_Yellow": 
					m_Light.SetColorToYellow();
					break;
					
				default: { m_Light.SetColorToWhite(); };
			}
		}
	}
	
	override void OnWorkStart()
	{
		
	}
	
	// Inventory manipulation
	override void OnInventoryExit(Man player)
	{
		super.OnInventoryExit(player);
		
		StandUp();
	}
	
	void StandUp()
	{
		if ( GetGame().IsServer()  &&  GetCompEM().IsWorking() )
		{
			vector ori_rotate = "0 0 0";
			SetOrientation(ori_rotate);
		}
	}
	
	override void OnWorkStop()
	{
		SetObjectMaterial( 0, GetMaterialForDamageState(false) );
		
		if (m_Light)
		{
			m_Light.FadeOut();
		}
		
		if ( GetGame().IsServer() )
		{
			//Safeguard if item is turned off by another event than running out of energy
			if (GetCompEM().GetEnergy() > 0)
				return;
			
			SetHealth(0);
		}
	}
	
	override void OnWork (float consumed_energy)
	{
		if (!m_Light)
			CreateLight();
		
		// Handle fade out of chemlight
		if (m_Light)
		{
			float efficiency = GetEfficiency0To1();
			
			if ( efficiency < 1 )
			{
				m_Light.SetIntensity( efficiency, GetCompEM().GetUpdateInterval() );
			}
		}
	}
	
	override void SetActions()
	{
		super.SetActions();
		
		AddAction(ActionTurnOnWhileInHands);
	}
	
	string GetMaterialForDamageState(bool glowing,int healthLevel = -1)
	{
		int currentHealthLevel;
		int suffixIndex;
		string base;
		
		if (healthLevel == -1)
			currentHealthLevel = GetHealthLevel();
		else
			currentHealthLevel = healthLevel;
		
		if (glowing)
			base = m_GlowMaterial;
		else
			base = m_DefaultMaterial;
		
		suffixIndex = base.IndexOf(".rvmat");
		if (suffixIndex == -1)
		{
			Error("Error - no valid rvmat found for chemlight");
			return "";
		}
		base = base.Substring(0,suffixIndex);
		
		if (currentHealthLevel == GameConstants.STATE_BADLY_DAMAGED || currentHealthLevel == GameConstants.STATE_DAMAGED)
		{
			base = base + "_damage";
		}
		else if (currentHealthLevel == GameConstants.STATE_RUINED)
		{
			base = base + "_destruct";
		}
		
		return base + ".rvmat";
	}
};

class Chemlight_White: Chemlight_ColorBase {};
class Chemlight_Red: Chemlight_ColorBase {};
class Chemlight_Green: Chemlight_ColorBase {};
class Chemlight_Blue: Chemlight_ColorBase {};
class Chemlight_Yellow: Chemlight_ColorBase {};