File size: 6,693 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
class UniversalTemperatureSourceSettings
{
	float m_UpdateInterval		= 1.0;			//! how often the Update is ticking
	float m_TemperatureMin		= 0;			//! min temperature you can get from the TemperatureSource
	float m_TemperatureMax		= 100;			//! max temperature you can get from the TemperatureSource
	float m_TemperatureCap		= float.MAX;	//! temperature cap that will limit the return value from GetTemperature method
	float m_RangeFull			= 1;			//! range where the full temperature is given to receiver
	float m_RangeMax			= 2;			//! maximum range where the receiver can get some temperature
	
	bool m_Updateable 			= false;		//! if the Update is running periodically
	bool m_ManualUpdate  		= false;		//! update is called manually (ex. own tick of parent entity)
	bool m_AffectStat			= false;		//! if the temperature generated is also set as Temperature Stat on Item itself

	vector m_Position			= vector.Zero;
	EntityAI m_Parent			= null;			//! parent Entity of the UTS
}

class UniversalTemperatureSourceResult
{
	float m_Temperature = 0;
	// ?? what else
}

//! original Timer deletes m_params which is unwanted
class UniversalTemperatureSourceTimer : Timer
{
	override void OnTimer()
	{
		if (m_params)
		{
			GetGame().GameScript.CallFunctionParams(m_target, m_function, null, m_params);
		}
		else
		{
			GetGame().GameScript.CallFunction(m_target, m_function, null, 0);
		}
	}
	
	override void Stop()
	{
		SetRunning(false);
		m_time = 0;
	}
	
	void SetParams(Param params)
	{
		m_params = params;
	}
}

typedef UniversalTemperatureSource UTemperatureSource;

class UniversalTemperatureSource
{
	protected bool 										m_Active
	protected ref UniversalTemperatureSourceTimer 		m_Timer;
	protected UniversalTemperatureSourceSettings 		m_Settings;
	protected ref UniversalTemperatureSourceResult 		m_ResultValues;
	protected ref UniversalTemperatureSourceLambdaBase 	m_Lambda;

	void UniversalTemperatureSource(EntityAI pParent, UniversalTemperatureSourceSettings pSettings, UniversalTemperatureSourceLambdaBase pLambda)
	{
		m_Active			= false;
		m_Settings			= pSettings;
		m_Lambda			= pLambda;
		m_ResultValues 		= new UniversalTemperatureSourceResult();
		m_Timer				= new UniversalTemperatureSourceTimer();
		
		Init(pParent);
	}
	
	void ~UniversalTemperatureSource() {};
	
	void Init(EntityAI pParent)
	{
		if (pParent)
		{
			pParent.SetUniversalTemperatureSource(this);
	
			m_Settings.m_Parent 	= pParent;
			m_Settings.m_Position	= pParent.GetPosition();
		}
		
		if (!m_Settings.m_ManualUpdate)
		{
			auto params = new Param2<UniversalTemperatureSourceSettings, UniversalTemperatureSourceLambdaBase>(m_Settings, m_Lambda);
	
			m_Timer.Run(m_Settings.m_UpdateInterval, this, "Update", params, m_Settings.m_Updateable);
			SetActive(false);
		}
	}
	
	vector GetPosition()
	{
		return m_Settings.m_Position;
	}
	
	float GetFullRange()
	{
		return m_Settings.m_RangeFull;
	}

	float GetMaxRange()
	{
		return m_Settings.m_RangeMax;
	}
	
	float GetTemperature()
	{
		if (m_Settings.m_TemperatureCap != float.MAX)
		{
			return Math.Min(m_Settings.m_TemperatureCap, GetTemperatureRaw());
		}
		
		return GetTemperatureRaw();
	}
	
	float GetTemperatureRaw()
	{
		if (m_ResultValues)
		{
			return m_ResultValues.m_Temperature;
		}
		
		return 0;
	}
	
	float GetTemperatureMin()
	{
		return m_Settings.m_TemperatureMin;
	}
	
	float GetTemperatureMax()
	{
		return m_Settings.m_TemperatureMax;
	}
	
	EntityAI GetParent()
	{
		return m_Settings.m_Parent;
	}
	
	bool IsActive()
	{
		if (m_Settings.m_ManualUpdate)
		{
			return m_Active;
		}

		return m_Timer && m_Timer.IsRunning();
	}
	
	void SetActive(bool pActive)
	{
		if (m_Settings.m_ManualUpdate)
		{
			m_Active = pActive;
			return;
		}

		if (pActive && !m_Timer.IsRunning())
		{
			m_Timer.Continue();
		}
		else
		{
			m_Timer.Stop();
		}
	}
	
	void SetDefferedActive(bool pActive, float pSeconds)
	{
		GetGame().GetCallQueue(CALL_CATEGORY_GAMEPLAY).CallLaterByName(this, "SetActive", pSeconds * 1000, false, new Param1<bool>(pActive));
	}
	
	void ChangeSettings(UniversalTemperatureSourceSettings pSettings)
	{
		m_Timer.SetParams(new Param2<UniversalTemperatureSourceSettings, UniversalTemperatureSourceLambdaBase>(m_Settings, m_Lambda));
	}

	void Update(UniversalTemperatureSourceSettings settings, UniversalTemperatureSourceLambdaBase lambda)
	{
		if (!IsActive())
		{
			return;
		}

		if (lambda)
		{
			settings.m_Position = settings.m_Parent.GetUniversalTemperatureSourcePosition();
			lambda.Execute(settings, m_ResultValues);
		}

	}
}

typedef UniversalTemperatureSourceDebug UTemperatureSourceDebug

class UniversalTemperatureSourceDebug
{
	const string DELIMITER_DATA		= "|";
	const string DELIMITER_KEYPAIR	= ":";

	string m_Header;
	string m_Data;
	ref array<string> m_Names;		//! names parsed from m_Pairs
	ref array<string> m_Values;		//! values parsed from m_Pairs
	ref array<string> m_Pairs;		//! keeps first iteration of parsed data from the m_Data
	
	void UniversalTemperatureSourceDebug()
	{
		m_Header 		= "";
		m_Data 			= "";
		m_Pairs			= new array<string>();
		m_Names			= new array<string>();
		m_Values		= new array<string>();
	}
	
	void AddHeader(string header)
	{
		m_Header = header;
	}
	
	void Add(string name, string value)
	{
		m_Data = string.Format("%1%2:%3%4", m_Data, name, value, DELIMITER_DATA);	
	}
	
	void Commit()
	{
		m_Pairs = ParseData();
		ParseKeyPairs();
	}

	int PairsCount()
	{
		return m_Pairs.Count();
	}
	
	string GetHeader()
	{
		return m_Header;
	}
	
	string GetName(int pIndex)
	{
		if (m_Names.Count() - 1 < pIndex)
		{
			Debug.Log(string.Format("GetName index: %1 from data of length: %2", pIndex, m_Names.Count()), "UniversalTemperatureSourceDebug");
			return "";
		}
		
		return m_Names.Get(pIndex);
	}
	
	string GetValue(int pIndex)
	{
		if (m_Values.Count() - 1 < pIndex)
		{
			Debug.Log(string.Format("GetValue index: %1 from data of length: %2", pIndex, m_Values.Count()), "UniversalTemperatureSourceDebug");
			return "";
		}
		
		return m_Values.Get(pIndex);
	}
	
	protected array<string> ParseData()
	{
		
		array<string> parsed = new array<string>();
		if (m_Data)
		{
			m_Data.Split(DELIMITER_DATA, parsed);
		}
		
		return parsed;
	}
	
	protected void ParseKeyPairs()
	{
		m_Names.Clear();
		m_Values.Clear();

		if (m_Pairs)
		{
			for (int i = 0; i < m_Pairs.Count(); i++)
			{
				array<string> keypair = new array<string>();

				m_Pairs.Get(i).Split(DELIMITER_KEYPAIR, keypair);
				m_Names.Insert(keypair[0]);
				m_Values.Insert(keypair[1]);
			}
		}
	}
	
	void Debug()
	{
		for (int i = 0; i < m_Names.Count(); i++)
		{
			Debug.Log(string.Format("%1: %2", m_Names.Get(i), m_Values.Get(i)), "UniversalTemperatureSourceDebug");
		}
	}
}