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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
/**
 * \defgroup Tools
 * \desc Helpful functions & classes
 * @{
 */

//--------------------------------------------------------------------------
const int CALL_CATEGORY_SYSTEM = 0; // Runs always
const int CALL_CATEGORY_GUI = 1; // Runs always (on client)
const int CALL_CATEGORY_GAMEPLAY = 2; // Runs unless ingame menu is opened

const int CALL_CATEGORY_COUNT = 3;

// -------------------------------------------------------------------------
class CallQueueContext
{
	Class m_target;
	string m_function;
	ref Param m_params;
	bool m_valid;

	void CallQueueContext(Class target, string fn, Param params)
	{
		m_target = target;
		m_function = fn;
		m_params = params;
		m_valid = true;
	}
		
	void Call()
	{
		CallParams(m_params);
	}
		
	void CallParams(Param params)
	{
		if (params)
		{
			GetGame().GameScript.CallFunctionParams(m_target, m_function, NULL, params);
		}
		else
		{
			GetGame().GameScript.CallFunction(m_target, m_function, NULL, 0);
		}
	}
	
	void Invalidate() {
		m_valid = false;
	}

	bool IsValid(){
		return m_valid;
	}
};

//--------------------------------------------------------------------------
/**
 \brief CallQueue Class provide "lazy" calls - when we don't want to execute function immediately but later during frame update (used mainly in UI)
 \n usage:
 @code
 GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(this, "Refresh"); // calls "Refresh" function on "this" with no arguments
 GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(this, "Show", new Param1<bool>(true)); // calls "Show" function on "this" with one bool argument
 GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(this, "SetPos", new Param2<float, float>(0.2, 0.5)); // calls "SetPos" function on "this" with two float arguments
 @endcode
 */
class CallQueue extends array<ref CallQueueContext>
{
	private bool m_processing;

	void CallQueue()
	{
		m_processing = false;
	}
		
	/**
	\brief System function, don't call it
	*/
	void Tick()
	{
		if (m_processing) return;
	
		m_processing = true;
	
		while(Count() > 0)
		{
			CallQueueContext ctx = Get(0);
			if (!ctx.IsValid())
			{
				Remove(0);
			}
			else
			{
				Remove(0);
				ctx.Call();
			}
		}
	
		m_processing = false;
	}

	/**
  \brief Creates new call request, add it on queue and execute during frame update (depends on call category)
  @param obj target object on which function will be executed
	@param fn_name name of function (on object "obj") which will be executed
	@param params function arguments see Param for usage, default NULL (no arguments)
	\warning When object "obj" is deleted prior call execution, don't forget to remove calls for this object with RemoveCalls
	*/
	void Call(Class obj, string fn_name, Param params = NULL)
	{
		Insert(new CallQueueContext(obj, fn_name, params));
	}


	/**
  \brief Removes all queued calls for object (call this function when object is going to be deleted)
  @param obj object for which you want remove all "lazy" calls
	*/
	void RemoveCalls(Class obj)
	{
		if (Count())
		{
			for (int i = Count() - 1; i >= 0; i--)
			{
				CallQueueContext ctx = Get(i);
				if (ctx.m_target == obj)
				{
					ctx.Invalidate();
				}
			}
		}
	}
};

//--------------------------------------------------------------------------
/**
 \brief DragQueue Class provide callbacks while mouse is dragging. Callback function must have exact arguments: 
 @code
 void AnyFunctionName(int mouse_x, int mouse_y, bool is_dragging);
 @endcode
 When mouse button is released, callback function is called one more time with is_dragging = false. Then all callbacks are automatically removed from queue.

 \n usage:
 @code
 class XYZ
 {
	void UpdateDrag(int mouse_x, int mouse_y, bool is_dragging);
 }
...
 GetGame().GetDragQueue().Call(this, "UpdateDrag"); // calls "UpdateDrag" function on "this" 

 @endcode
 */
class DragQueue extends CallQueue
{
	private ref Param3<int, int, bool> m_mouse_params; // x,y, is_holding_mb
	
	void DragQueue()
	{
		m_mouse_params = new Param3<int, int, bool>(0,0,true);
	}

	/**
	\brief System function, don't call it
	*/
	override void Tick()
	{
		if (m_processing) return;
	
		m_processing = true;
	
		int last_index = 0;
		int mouse_x;
		int mouse_y;
		bool is_holding = false;
		CallQueueContext ctx;
	
		if (GetMouseState(MouseState.LEFT) & 0x80000000)
		{
			is_holding = true;
		}
	
		GetMousePos(mouse_x, mouse_y);
	
		if (!is_holding || mouse_x != m_mouse_params.param1 || mouse_y != m_mouse_params.param2)
		{
			m_mouse_params.param1 = mouse_x;
			m_mouse_params.param2 = mouse_y;
			m_mouse_params.param3 = is_holding;
	
			while (Count() > last_index)
			{
				ctx = Get(last_index);
				if (!ctx.IsValid())
				{
					Remove(last_index);
				}
				else
				{
					ctx.CallParams(m_mouse_params);
					last_index++;
				}
			}
		}
	
		// clear queue when mouse button is released
		if (!is_holding)
		{
			Clear();
		}
	
		m_processing = false;
	}
}

//--------------------------------------------------------------------------
/**
 \brief TimerBase Class provide just interface for all Timer classes. Don't instance this class, use Timer class instead.
 */
class TimerBase: Managed
{
	protected bool m_running;
	protected bool m_loop;
	protected float m_duration;
	protected float m_time;
	protected array<TimerBase> m_timerQueue;
	protected float m_RunTime;

	void ~TimerBase()
	{
		if (!m_timerQueue) return;
	
		SetRunning(false);
	}


	/**
  \brief Pause Timer, internal counter is not restarted, so timer can continue later. Can be unpaused via Continue.
	*/
	void Pause()
	{
		SetRunning(false);
	}

	/**
  \brief Timer continue when it was paused.
	*/
	void Continue()
	{
		SetRunning(true);
	}

	/**
  \brief Stop Timer and restart internal counter. Cannot be unpaused, must be started again.
	*/
	void Stop()
	{
		SetRunning(false);
		m_time = 0;
	}
		
	/**
  \return True when Timer is running (not stopped, not paused)
	*/
	bool IsRunning()
	{
		return m_running;
	}
		
	/**
  \brief System function, don't call.
	*/
	void Tick(float timeslice)
	{
		if (IsRunning())
		{
			m_RunTime += timeslice;
			m_time = m_time + timeslice;
	
			if (m_time >= m_duration)
			{
				if (m_loop)
				{
					m_time = m_time - m_duration;
				}
				else
				{
					SetRunning(false);
					m_time = 0;
				}
	
				OnTimer();			
			}
			else
			{
				OnUpdate();
			}
		}
	}

	/**
  \brief System function, don't call.
	*/
	void OnTimerQueueDestoryed()
	{
		m_timerQueue = NULL;
	}

	float GetTime()
	{
		return m_time;
	}
		
	float GetDuration()
	{
		return m_duration;
	}
		
	float GetRemaining()
	{
		return m_duration - m_time;
	}
	
	float GetRunTime()
	{
		return m_RunTime;
	}
		
	protected void OnInit(int category)
	{
		m_duration = 1;
		m_loop = false;
		m_time = 0;
		m_running = false;
		if (GetGame())
			m_timerQueue = GetGame().GetTimerQueue(category);
		else
			ErrorEx("Attempting to Init a timer when the game does not exist (GetGame() == null)");
	}
		
	protected void OnStart(float duration, bool loop)
	{
		m_RunTime = 0;
		m_duration = duration;
		m_loop = loop;
		m_time = 0;
		SetRunning(true);
	}
		
	protected void OnUpdate()	{}
	protected void OnTimer() {}
	protected void SetRunning(bool running)
	{
		int index = -1;
	
		if (m_running == running) return;
	
			m_running = running;
		if (m_timerQueue == NULL) return;
		
		if (running)
		{
			if (m_timerQueue.Find(this) == -1)
			{
				m_timerQueue.Insert(this);
			}
		}
		else
		{
			index = m_timerQueue.Find(this);
			if (index != -1)
			{
				m_timerQueue.Remove(index);
			}
		}
	}
};

//--------------------------------------------------------------------------
/**
 \brief TimerQueue Class using for system purpose only
 */
class TimerQueue extends array<TimerBase>
{
	private bool m_processing;

	// -------------------------------------------------------------------------
	void TimerQueue()
	{
		m_processing = false;
	}
	
	// -------------------------------------------------------------------------
	void ~TimerQueue()
	{
		if (Count())
		{
			for (int i = Count() - 1; i >= 0; i--)
			{
				Get(i).OnTimerQueueDestoryed();
			}
	
			Clear();
		}
	}
	
	// -------------------------------------------------------------------------
	void Tick(float timeslice)
	{
		if (m_processing) return;
	
		m_processing = true;
	
		if (Count())
		{
			for (int i = Count() - 1; i >= 0; i--)
			{
				Get(i).Tick(timeslice);
			}
		}
	
		m_processing = false;
	}
};

//--------------------------------------------------------------------------
/**
 \brief Simple class for fading Widgets
 */
class WidgetFadeTimer extends TimerBase
{
	private Widget m_widget;
	bool m_fadeIn;
	float m_alpha;

	void WidgetFadeTimer()
	{
		OnInit(CALL_CATEGORY_GUI);
		m_fadeIn = true;
	}

	/**
  \brief Make "fade in" effect on Widget (transparency goes from 0.0 to 1.0)
  @param w widget which will be faded
	@param time duration of effect
	@param continue - if True continue from current alpha value, otherwise always begin from 0.0 alpha
	*/
	void FadeIn(Widget w, float time, bool continue_ = false)
	{
		m_alpha = w.GetAlpha();
		
		if (continue_ && m_alpha > 0.95)
		{
			w.SetAlpha(1.0);
			w.Show(true);
			return;
		}	
		
		m_widget = w;
		m_fadeIn = true;
	
		OnStart(time, false);
	
		if (m_widget)
		{
			m_alpha = m_widget.GetAlpha();
			m_widget.SetAlpha(0);
			m_widget.Show(true);
		}
	
		if (continue_)
		{
			m_time = m_alpha * time;
		}
	}

	/**
  \brief Make "fade out" effect on Widget (transparency goes from 1.0 to 0.0)
  @param w widget which will be faded
	@param time duration of effect
	@param continue - if True continue from current alpha value, otherwise always begin from 1.0 alpha
	*/
	void FadeOut(Widget w, float time, bool continue_ = false)
	{
		m_alpha = w.GetAlpha();
		
		if (continue_ && m_alpha < 0.05)
		{
			w.SetAlpha(0);
			w.Show(false);
			return;
		}	
		
		m_widget = w;
		m_fadeIn = false;
	
		OnStart(time, false);
		
		if (m_widget && !continue_)
		{
			m_alpha = 1.0;
			m_widget.SetAlpha(m_alpha);
			m_widget.Show(true);
		}	
	
		if (continue_)
		{
			m_time = (1.0 - m_alpha) * time;
		}
	}
	
	override private void OnTimer()
	{
		if (m_widget)
		{
			if (m_fadeIn)
			{
				m_widget.SetAlpha(1);
			}
			else
			{
				m_widget.SetAlpha(0);
				m_widget.Show(false);
			}
		}
	}
	
	override private void OnUpdate()
	{
		float timeDiff = m_time / m_duration;
		float progress;
		if (m_widget)
		{
			if (m_fadeIn)
			{
				progress = timeDiff;
			}
			else
			{
				progress = Math.Lerp(m_alpha,0,timeDiff);
				progress = Math.Clamp(progress,0,1);
			}
			
			m_widget.SetAlpha(progress);
		}
	}
};

//--------------------------------------------------------------------------
/**
 \brief Timer class. Use when you want call function after some time, or repeatedly in time intervals. Call is not executed after the Timer object is deleted.
 \n usage:
 @code
 class MyObject
 {
	ref Timer myTimer1;
	ref Timer myTimer2;
	ref Timer myTimer3;

	void MyObject()
	{
		myTimer1 = new Timer();
		myTimer1.Run(10, this, "Refresh"); // calls "Refresh" on "this" after 10 seconds

		myTimer2 = new Timer();
		myTimer2.Run(10, this, "Refresh", NULL, true); // calls "Refresh" on "this" every 10 seconds, until Pause or Stop is called

		myTimer3 = new Timer();
		myTimer3.Run(15, this, "Show", new Param1<bool>(false)); // calls "Show" on "this" with one bool argument after 15 seconds
	}

	void Refresh();
	void Show(bool visible);
 }
 
 @endcode

 */
class Timer extends TimerBase
{
	protected Managed m_target;
	protected string m_function;
	protected ref Param m_params;

	void Timer(int category = CALL_CATEGORY_SYSTEM)
	{
		OnInit(category);
	}

	/**
  \brief Starts timer.
	@param duration function is executed after this time (in seconds).
	@param obj target object on which function will be executed
	@param fn_name name of function (on object "obj") which will be executed
	@param params function arguments see Param for usage, default NULL (no arguments)
	@param loop when true, timer is looped endlessly and function is executed after every loop.
	*/
	void Run(float duration, Managed obj, string fn_name, Param params = NULL, bool loop = false)
	{
		m_target = obj;
		m_function = fn_name;
			
		m_params = params;
		OnStart(duration, loop);
	}
	
	override protected void OnTimer()
	{
		if (m_params)
		{
			GetGame().GameScript.CallFunctionParams(m_target, m_function, NULL, m_params);
			m_params = NULL;
		}
		else
		{
			GetGame().GameScript.CallFunction(m_target, m_function, NULL, 0);
		}
	}
		
	override void Stop()
	{
		super.Stop();
		m_params = NULL;
	}
};



//--------------------------------------------------------------------------
/**
 \brief AnimationTimer class. This timer is for animating float value.
 \n usage:
 @code
 class MyObject
 {
	ref AnimationTimer myAnim;
	
	void MyObject()
	{
		myAnim = new AnimationTimer();
		myAnim.Run(60, this, "Refresh");
	}

	void Refresh()
	{
		Print(myAnim.GetValue());
	}
 }; 
 @endcode

 */

class AnimationTimer : TimerBase
{
	private bool m_Active;
	private float m_TargetValue;
	private float m_TargetValueOriginal;
	private float m_Value;
	protected Managed m_TargetObject;
	protected string m_UpdateFunction;
	protected string m_FinishedFunction;
	protected ref Param m_Params;
	
	void AnimationTimer(int category = CALL_CATEGORY_SYSTEM)
	{
		OnInit(category);
	}
	
	void ~AnimationTimer()
	{
		SetRunning(false);
	}
	
	void Run(float targetVal, Managed obj, string updateFunc, string finishedFunc, float startingVal = 0, bool loop = false, float speed = 1.0, Param params = null, int category = CALL_CATEGORY_SYSTEM)
	{
		SetRunning(true);
		m_TargetObject = obj;
		m_UpdateFunction = updateFunc;
		m_FinishedFunction = finishedFunc;
		m_TargetValueOriginal = targetVal;
		m_TargetValue = targetVal;
		m_time = speed;
		m_loop = loop;
		m_Active = true;
		m_Params = params;
		m_Value = startingVal;
	}

	/**
  \brief Returns actual animated value.
	*/
	float GetValue() {
		return m_Value;
	}	

	override bool IsRunning()
	{
		return m_Active;
	}
	/**
  \brief Ticks the timer, is called by timer subsystem.
	*/
	override void Tick(float timeslice)
	{
		if ( !m_Active ) 
			return;
		

		float diff = Math.AbsFloat(m_TargetValue - m_Value);
		float step = m_time * timeslice;

		if (diff < step)
		{
			m_Value = m_TargetValue;
			if (!m_loop)
			{
				m_Active = false;
			}
			else
			{
				if (m_TargetValue == m_TargetValueOriginal)
				{
					m_TargetValue = 0;
				}
				else
				{
					m_TargetValue = m_TargetValueOriginal;
				}
				
			}
			GetGame().GameScript.CallFunction(m_TargetObject, m_FinishedFunction, NULL, m_Params);
		}
		else
		{
			if (m_TargetValue > m_Value) 
			{
				m_Value += step;
			}
			else
			{
				m_Value -= step;
			}
		}
		
		GetGame().GameScript.CallFunction(m_TargetObject, m_UpdateFunction, NULL, m_Params);
	}
};

class AnimatorTimer
{
	private bool m_active = false;
	private bool m_loop = false;
	private float m_target_value = 0;
	private float m_value = 0;
	private float m_time = 0;

	/**
  \brief Starts animate value until value reaches target value.
	@param val \p float target value
	@param speed \p float speed of animating, units per second
	*/
	void Animate(float val, float speed = 1.0)
	{
		m_target_value = val;
		m_loop = false;
		m_time = speed;
		m_active = true;
	}

	/**
  \brief Starts infinite animation loop <-1,1>. Based on sinus transition.
	@param speed \p float speed of animating , cycles per second (1.0 means one 360 degree sinus cycle per second: 0..1..0..-1..0)
	*/
	void AnimateLoop(float speed = 1.0)
	{
		m_value = 0;
		m_target_value = 0;
		m_loop = true;
		m_time = speed;
		m_active = true;
	}

	/**
  \brief Returns actual animated value.
	*/
	float GetValue() {
		return m_value;
	}	

	/**
  \brief Returns target value. While AnimateLoop returns angle of cycle in radians.
	*/
	float GetTargetValue() {
		return m_target_value;
	}

	/**
  \brief Sets both value and target value.
	*/
	void SetValue(float val) {
		m_value = val;
		m_target_value = val;
	}	

	bool IsRunning()
	{
		return m_active;
	}
	/**
  \brief Ticks the timer, is called by timer subsystem.
	*/
	void Tick(float timeslice)
	{
		if ( !m_active ) return;
		
		if (m_loop)
		{
			m_target_value += m_time * Math.PI2 * timeslice;
			while (m_target_value > Math.PI2) m_target_value -= Math.PI2;
			
			m_value = Math.Sin(m_target_value);
		}
		else
		{
			float diff = Math.AbsFloat(m_target_value - m_value);
			float step = m_time * timeslice;
	
			if (diff < step)
			{
				m_value = m_target_value;
				m_active = false;
			}
			else
			{
				if (m_target_value > m_value) 
				{
					m_value += step;
				}
				else
				{
					m_value -= step;
				}
			}
		}
	}
};

/**
 \brief Associative array template, with multiple values per key
 \n usage:
 @code
 ref multiMap<string, int>	myMap = new multiMap<string, int>;
 myMap.Insert("jozo", 7);
 myMap.Insert("jozo", 3);
 myMap.Insert("jozo", 5);
 myMap.Insert("fero", 98);
 myMap.Insert("fero", 35);
 myMap.Insert("fero", 17);

 Print("Jozo:");
 myMap.Get("jozo").Debug();
 Print("Fero:");
 myMap.Get("fero").Debug();

 @endcode

 */
class multiMap<Class K, Class V>
{
	private ref array<ref array<V> > m_values;
	private ref array<K> m_keys;

	bool HasKey(K key)
	{
		int index = -1;
		if (m_keys)
		{
			index = m_keys.Find(key);
		}

		if (index != -1)
		{
			return true;
		}

		return false;
	}

	array<V> Get(K key)
	{
		int index = -1;
		if (m_keys)
		{
			index = m_keys.Find(key);
		}

		if (index != -1)
		{
			return m_values.Get(index);
		}

		return NULL;
	}

	array<V> GetByIndex(int index)
	{
		return m_values.Get(index);
	}

	K GetKeyByIndex(int index)
	{
		return m_keys.Get(index);
	}

	void Insert(K key, V value)
	{
		int index = -1;
		
		if (!m_keys)
		{
			m_keys = new array<K>;
			m_values = new array<ref array<V> >;
		}
		else
		{
			index = m_keys.Find(key);
		}

		if (index == -1)
		{
			array<V> value_array = new array<V>;
			value_array.Insert(value);

			m_keys.Insert(key);
			m_values.Insert(value_array);

		}
		else
		{
			m_values.Get(index).Insert(value);
		}
	}

	void RemoveByIndex(int index)
	{
		m_keys.Remove(index);
		m_values.Remove(index);
	}

	void Remove(K key)
	{
		int index = -1;
		if (m_keys)
		{
			index = m_keys.Find(key);
		}

		if (index != -1)
		{
			RemoveByIndex(index);
		}
	}

	int Count()
	{
		if (m_keys)
		{
			return m_keys.Count();
		}

		return 0;
	}

	void Clear()
	{
		if ( m_keys && m_values)
		{
			m_keys.Clear();
			m_values.Clear();
		}
		
	}

	void ~multiMap()
	{
		Clear();
	}
};

// at last one template definition should be here, for template initialization in this script module
typedef map<string, string> TStringMap;

int GetTemperatureColor( int temperature )
{
	int alpha = 255;
	int red = 153;
	int green = 153;
	int blue = 153;
	if ( temperature < 20 )
	{
		temperature = temperature - 20;
		temperature = Math.Clamp( temperature, -50, 50);
		temperature = Math.AbsInt(temperature);

		red = Math.Clamp ( red - ((red/50 )*temperature), 0, 255 );
		green = Math.Clamp ( green - ((green/50 )*temperature), 0, 255 );
		blue = Math.Clamp ( blue+((blue/50)*temperature), 0, 255 );
	}
	else if ( temperature > 20 )
	{
		temperature = Math.Clamp( temperature, -100, 100);
		blue = Math.Clamp ( blue - ((blue/100 )*temperature), 0, 255 );
		green = Math.Clamp ( green - ((green/100 )*temperature), 0, 255 );
		red = Math.Clamp ( red+((red/100)*temperature), 0, 255 );
	}

	int color = ARGB( alpha, red, green, blue );
	return color;
}

//! Return value from profile variable, if variable with given name is not present, default value is returned
bool GetProfileValueBool(string name, bool def = false)
{
	string value;
	if (GetGame().GetProfileString(name, value))
	{
		if (value == "true" || value == "1")
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return def;
	}
}
	
//! Writes bool variable to profile, after write don't forget to call CGame::SaveProfile() to save profile to storage!
void SetProfileValueBool(string name, bool value)
{
	if (value)
	{
		GetGame().SetProfileString(name, "1");
	}
	else
	{
		GetGame().SetProfileString(name, "0");
	}
}


int GetNumberOfSetBits(int i)//leaving here for legacy/modding reasons
{
	return Math.GetNumberOfSetBits(i);
}

/** @}*/