Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
tools.c
Go to the documentation of this file.
1
7//--------------------------------------------------------------------------
8const int CALL_CATEGORY_SYSTEM = 0; // Runs always
9const int CALL_CATEGORY_GUI = 1; // Runs always (on client)
10const int CALL_CATEGORY_GAMEPLAY = 2; // Runs unless ingame menu is opened
11
12const int CALL_CATEGORY_COUNT = 3;
13
14// -------------------------------------------------------------------------
16{
17 Class m_target;
18 string m_function;
19 ref Param m_params;
20 bool m_valid;
21
22 void CallQueueContext(Class target, string fn, Param params)
23 {
24 m_target = target;
25 m_function = fn;
26 m_params = params;
27 m_valid = true;
28 }
29
30 void Call()
31 {
32 CallParams(m_params);
33 }
34
35 void CallParams(Param params)
36 {
37 if (params)
38 {
39 GetGame().GameScript.CallFunctionParams(m_target, m_function, NULL, params);
40 }
41 else
42 {
43 GetGame().GameScript.CallFunction(m_target, m_function, NULL, 0);
44 }
45 }
46
47 void Invalidate() {
48 m_valid = false;
49 }
50
51 bool IsValid(){
52 return m_valid;
53 }
54};
55
56//--------------------------------------------------------------------------
66class CallQueue extends array<ref CallQueueContext>
67{
68 private bool m_processing;
69
70 void CallQueue()
71 {
72 m_processing = false;
73 }
74
78 void Tick()
79 {
80 if (m_processing) return;
81
82 m_processing = true;
83
84 while(Count() > 0)
85 {
86 CallQueueContext ctx = Get(0);
87 if (!ctx.IsValid())
88 {
89 Remove(0);
90 }
91 else
92 {
93 Remove(0);
94 ctx.Call();
95 }
96 }
97
98 m_processing = false;
99 }
100
108 void Call(Class obj, string fn_name, Param params = NULL)
109 {
110 Insert(new CallQueueContext(obj, fn_name, params));
111 }
112
113
118 void RemoveCalls(Class obj)
119 {
120 if (Count())
121 {
122 for (int i = Count() - 1; i >= 0; i--)
123 {
124 CallQueueContext ctx = Get(i);
125 if (ctx.m_target == obj)
126 {
127 ctx.Invalidate();
128 }
129 }
130 }
131 }
132};
133
134//--------------------------------------------------------------------------
153class DragQueue extends CallQueue
154{
155 private ref Param3<int, int, bool> m_mouse_params; // x,y, is_holding_mb
156
157 void DragQueue()
158 {
159 m_mouse_params = new Param3<int, int, bool>(0,0,true);
160 }
161
165 override void Tick()
166 {
167 if (m_processing) return;
168
169 m_processing = true;
170
171 int last_index = 0;
172 int mouse_x;
173 int mouse_y;
174 bool is_holding = false;
176
177 if (GetMouseState(MouseState.LEFT) & 0x80000000)
178 {
179 is_holding = true;
180 }
181
182 GetMousePos(mouse_x, mouse_y);
183
184 if (!is_holding || mouse_x != m_mouse_params.param1 || mouse_y != m_mouse_params.param2)
185 {
186 m_mouse_params.param1 = mouse_x;
187 m_mouse_params.param2 = mouse_y;
188 m_mouse_params.param3 = is_holding;
189
190 while (Count() > last_index)
191 {
192 ctx = Get(last_index);
193 if (!ctx.IsValid())
194 {
195 Remove(last_index);
196 }
197 else
198 {
199 ctx.CallParams(m_mouse_params);
200 last_index++;
201 }
202 }
203 }
204
205 // clear queue when mouse button is released
206 if (!is_holding)
207 {
208 Clear();
209 }
210
211 m_processing = false;
212 }
213}
214
215//--------------------------------------------------------------------------
219class TimerBase: Managed
220{
221 protected bool m_running;
222 protected bool m_loop;
223 protected float m_duration;
224 protected float m_time;
226 protected float m_RunTime;
227
229 {
230 if (!m_timerQueue) return;
231
232 SetRunning(false);
233 }
234
235
239 void Pause()
240 {
241 SetRunning(false);
242 }
243
247 void Continue()
248 {
249 SetRunning(true);
250 }
251
255 void Stop()
256 {
257 SetRunning(false);
258 m_time = 0;
259 }
260
265 {
266 return m_running;
267 }
268
272 void Tick(float timeslice)
273 {
274 if (IsRunning())
275 {
276 m_RunTime += timeslice;
277 m_time = m_time + timeslice;
278
279 if (m_time >= m_duration)
280 {
281 if (m_loop)
282 {
284 }
285 else
286 {
287 SetRunning(false);
288 m_time = 0;
289 }
290
291 OnTimer();
292 }
293 else
294 {
295 OnUpdate();
296 }
297 }
298 }
299
304 {
305 m_timerQueue = NULL;
306 }
307
308 float GetTime()
309 {
310 return m_time;
311 }
312
314 {
315 return m_duration;
316 }
317
319 {
320 return m_duration - m_time;
321 }
322
324 {
325 return m_RunTime;
326 }
327
328 protected void OnInit(int category)
329 {
330 m_duration = 1;
331 m_loop = false;
332 m_time = 0;
333 m_running = false;
334 if (GetGame())
335 m_timerQueue = GetGame().GetTimerQueue(category);
336 else
337 ErrorEx("Attempting to Init a timer when the game does not exist (GetGame() == null)");
338 }
339
340 protected void OnStart(float duration, bool loop)
341 {
342 m_RunTime = 0;
343 m_duration = duration;
344 m_loop = loop;
345 m_time = 0;
346 SetRunning(true);
347 }
348
349 protected void OnUpdate() {}
350 protected void OnTimer() {}
351 protected void SetRunning(bool running)
352 {
353 int index = -1;
354
355 if (m_running == running) return;
356
357 m_running = running;
358 if (m_timerQueue == NULL) return;
359
360 if (running)
361 {
362 if (m_timerQueue.Find(this) == -1)
363 {
364 m_timerQueue.Insert(this);
365 }
366 }
367 else
368 {
369 index = m_timerQueue.Find(this);
370 if (index != -1)
371 {
372 m_timerQueue.Remove(index);
373 }
374 }
375 }
376};
377
378//--------------------------------------------------------------------------
382class TimerQueue extends array<TimerBase>
383{
384 private bool m_processing;
385
386 // -------------------------------------------------------------------------
387 void TimerQueue()
388 {
389 m_processing = false;
390 }
391
392 // -------------------------------------------------------------------------
393 void ~TimerQueue()
394 {
395 if (Count())
396 {
397 for (int i = Count() - 1; i >= 0; i--)
398 {
399 Get(i).OnTimerQueueDestoryed();
400 }
401
402 Clear();
403 }
404 }
405
406 // -------------------------------------------------------------------------
407 void Tick(float timeslice)
408 {
409 if (m_processing) return;
410
411 m_processing = true;
412
413 if (Count())
414 {
415 for (int i = Count() - 1; i >= 0; i--)
416 {
417 Get(i).Tick(timeslice);
418 }
419 }
420
421 m_processing = false;
422 }
423};
424
425//--------------------------------------------------------------------------
429class WidgetFadeTimer extends TimerBase
430{
431 private Widget m_widget;
432 bool m_fadeIn;
433 float m_alpha;
434
435 void WidgetFadeTimer()
436 {
438 m_fadeIn = true;
439 }
440
447 void FadeIn(Widget w, float time, bool continue_ = false)
448 {
449 m_alpha = w.GetAlpha();
450
451 if (continue_ && m_alpha > 0.95)
452 {
453 w.SetAlpha(1.0);
454 w.Show(true);
455 return;
456 }
457
458 m_widget = w;
459 m_fadeIn = true;
460
461 OnStart(time, false);
462
463 if (m_widget)
464 {
465 m_alpha = m_widget.GetAlpha();
466 m_widget.SetAlpha(0);
467 m_widget.Show(true);
468 }
469
470 if (continue_)
471 {
472 m_time = m_alpha * time;
473 }
474 }
475
482 void FadeOut(Widget w, float time, bool continue_ = false)
483 {
484 m_alpha = w.GetAlpha();
485
486 if (continue_ && m_alpha < 0.05)
487 {
488 w.SetAlpha(0);
489 w.Show(false);
490 return;
491 }
492
493 m_widget = w;
494 m_fadeIn = false;
495
496 OnStart(time, false);
497
498 if (m_widget && !continue_)
499 {
500 m_alpha = 1.0;
501 m_widget.SetAlpha(m_alpha);
502 m_widget.Show(true);
503 }
504
505 if (continue_)
506 {
507 m_time = (1.0 - m_alpha) * time;
508 }
509 }
510
511 override private void OnTimer()
512 {
513 if (m_widget)
514 {
515 if (m_fadeIn)
516 {
517 m_widget.SetAlpha(1);
518 }
519 else
520 {
521 m_widget.SetAlpha(0);
522 m_widget.Show(false);
523 }
524 }
525 }
526
527 override private void OnUpdate()
528 {
529 float timeDiff = m_time / m_duration;
530 float progress;
531 if (m_widget)
532 {
533 if (m_fadeIn)
534 {
535 progress = timeDiff;
536 }
537 else
538 {
539 progress = Math.Lerp(m_alpha,0,timeDiff);
540 progress = Math.Clamp(progress,0,1);
541 }
542
543 m_widget.SetAlpha(progress);
544 }
545 }
546};
547
548//--------------------------------------------------------------------------
578class Timer extends TimerBase
579{
580 protected Managed m_target;
581 protected string m_function;
582 protected ref Param m_params;
583
584 void Timer(int category = CALL_CATEGORY_SYSTEM)
585 {
586 OnInit(category);
587 }
588
597 void Run(float duration, Managed obj, string fn_name, Param params = NULL, bool loop = false)
598 {
599 m_target = obj;
600 m_function = fn_name;
601
602 m_params = params;
603 OnStart(duration, loop);
604 }
605
606 override protected void OnTimer()
607 {
608 if (m_params)
609 {
610 GetGame().GameScript.CallFunctionParams(m_target, m_function, NULL, m_params);
611 m_params = NULL;
612 }
613 else
614 {
615 GetGame().GameScript.CallFunction(m_target, m_function, NULL, 0);
616 }
617 }
618
619 override void Stop()
620 {
621 super.Stop();
622 m_params = NULL;
623 }
624};
625
626
627
628//--------------------------------------------------------------------------
653{
654 private bool m_Active;
655 private float m_TargetValue;
656 private float m_TargetValueOriginal;
657 private float m_Value;
659 protected string m_UpdateFunction;
660 protected string m_FinishedFunction;
661 protected ref Param m_Params;
662
664 {
665 OnInit(category);
666 }
667
669 {
670 SetRunning(false);
671 }
672
673 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)
674 {
675 SetRunning(true);
676 m_TargetObject = obj;
677 m_UpdateFunction = updateFunc;
678 m_FinishedFunction = finishedFunc;
679 m_TargetValueOriginal = targetVal;
680 m_TargetValue = targetVal;
681 m_time = speed;
682 m_loop = loop;
683 m_Active = true;
684 m_Params = params;
685 m_Value = startingVal;
686 }
687
691 float GetValue() {
692 return m_Value;
693 }
694
695 override bool IsRunning()
696 {
697 return m_Active;
698 }
702 override void Tick(float timeslice)
703 {
704 if ( !m_Active )
705 return;
706
707
708 float diff = Math.AbsFloat(m_TargetValue - m_Value);
709 float step = m_time * timeslice;
710
711 if (diff < step)
712 {
713 m_Value = m_TargetValue;
714 if (!m_loop)
715 {
716 m_Active = false;
717 }
718 else
719 {
720 if (m_TargetValue == m_TargetValueOriginal)
721 {
722 m_TargetValue = 0;
723 }
724 else
725 {
726 m_TargetValue = m_TargetValueOriginal;
727 }
728
729 }
730 GetGame().GameScript.CallFunction(m_TargetObject, m_FinishedFunction, NULL, m_Params);
731 }
732 else
733 {
734 if (m_TargetValue > m_Value)
735 {
736 m_Value += step;
737 }
738 else
739 {
740 m_Value -= step;
741 }
742 }
743
744 GetGame().GameScript.CallFunction(m_TargetObject, m_UpdateFunction, NULL, m_Params);
745 }
746};
747
749{
750 private bool m_active = false;
751 private bool m_loop = false;
752 private float m_target_value = 0;
753 private float m_value = 0;
754 private float m_time = 0;
755
761 void Animate(float val, float speed = 1.0)
762 {
763 m_target_value = val;
764 m_loop = false;
765 m_time = speed;
766 m_active = true;
767 }
768
773 void AnimateLoop(float speed = 1.0)
774 {
775 m_value = 0;
776 m_target_value = 0;
777 m_loop = true;
778 m_time = speed;
779 m_active = true;
780 }
781
785 float GetValue() {
786 return m_value;
787 }
788
792 float GetTargetValue() {
793 return m_target_value;
794 }
795
799 void SetValue(float val) {
800 m_value = val;
801 m_target_value = val;
802 }
803
804 bool IsRunning()
805 {
806 return m_active;
807 }
811 void Tick(float timeslice)
812 {
813 if ( !m_active ) return;
814
815 if (m_loop)
816 {
817 m_target_value += m_time * Math.PI2 * timeslice;
818 while (m_target_value > Math.PI2) m_target_value -= Math.PI2;
819
820 m_value = Math.Sin(m_target_value);
821 }
822 else
823 {
824 float diff = Math.AbsFloat(m_target_value - m_value);
825 float step = m_time * timeslice;
826
827 if (diff < step)
828 {
829 m_value = m_target_value;
830 m_active = false;
831 }
832 else
833 {
834 if (m_target_value > m_value)
835 {
836 m_value += step;
837 }
838 else
839 {
840 m_value -= step;
841 }
842 }
843 }
844 }
845};
846
867class multiMap<Class K, Class V>
868{
869 private ref array<ref array<V> > m_values;
870 private ref array<K> m_keys;
871
872 bool HasKey(K key)
873 {
874 int index = -1;
875 if (m_keys)
876 {
877 index = m_keys.Find(key);
878 }
879
880 if (index != -1)
881 {
882 return true;
883 }
884
885 return false;
886 }
887
888 array<V> Get(K key)
889 {
890 int index = -1;
891 if (m_keys)
892 {
893 index = m_keys.Find(key);
894 }
895
896 if (index != -1)
897 {
898 return m_values.Get(index);
899 }
900
901 return NULL;
902 }
903
904 array<V> GetByIndex(int index)
905 {
906 return m_values.Get(index);
907 }
908
909 K GetKeyByIndex(int index)
910 {
911 return m_keys.Get(index);
912 }
913
914 void Insert(K key, V value)
915 {
916 int index = -1;
917
918 if (!m_keys)
919 {
920 m_keys = new array<K>;
921 m_values = new array<ref array<V> >;
922 }
923 else
924 {
925 index = m_keys.Find(key);
926 }
927
928 if (index == -1)
929 {
930 array<V> value_array = new array<V>;
931 value_array.Insert(value);
932
933 m_keys.Insert(key);
934 m_values.Insert(value_array);
935
936 }
937 else
938 {
939 m_values.Get(index).Insert(value);
940 }
941 }
942
943 void RemoveByIndex(int index)
944 {
945 m_keys.Remove(index);
946 m_values.Remove(index);
947 }
948
949 void Remove(K key)
950 {
951 int index = -1;
952 if (m_keys)
953 {
954 index = m_keys.Find(key);
955 }
956
957 if (index != -1)
958 {
959 RemoveByIndex(index);
960 }
961 }
962
963 int Count()
964 {
965 if (m_keys)
966 {
967 return m_keys.Count();
968 }
969
970 return 0;
971 }
972
973 void Clear()
974 {
975 if ( m_keys && m_values)
976 {
977 m_keys.Clear();
978 m_values.Clear();
979 }
980
981 }
982
983 void ~multiMap()
984 {
985 Clear();
986 }
987};
988
989// at last one template definition should be here, for template initialization in this script module
991
992int GetTemperatureColor(int temperature)
993{
994 int alpha = 255;
995 int red = 153;
996 int green = 153;
997 int blue = 153;
998
999 if (temperature < GameConstants.ITEM_TEMPERATURE_NEUTRAL_ZONE_LOWER_LIMIT)
1000 {
1001 temperature = temperature - 20;
1002 temperature = Math.Clamp( temperature, -50, 50);
1003 temperature = Math.AbsInt(temperature);
1004
1005 red = Math.Clamp ( red - ((red/50 )*temperature), 0, 255);
1006 green = Math.Clamp ( green - ((green/50 )*temperature), 0, 255);
1007 blue = Math.Clamp ( blue+((blue/50)*temperature), 0, 255);
1008 }
1009 else if (temperature > GameConstants.ITEM_TEMPERATURE_NEUTRAL_ZONE_UPPER_LIMIT)
1010 {
1011 temperature = Math.Clamp(temperature, -100, 100);
1012 blue = Math.Clamp (blue - ((blue / 100) * temperature), 0, 255);
1013 green = Math.Clamp (green - ((green / 100) * temperature), 0, 255);
1014 red = Math.Clamp (red + ((red / 100) * temperature), 0, 255);
1015 }
1016
1017 return ARGB(alpha, red, green, blue);
1018}
1019
1021bool GetProfileValueBool(string name, bool def = false)
1022{
1023 string value;
1024 if (GetGame().GetProfileString(name, value))
1025 {
1026 if (value == "true" || value == "1")
1027 {
1028 return true;
1029 }
1030 else
1031 {
1032 return false;
1033 }
1034 }
1035 else
1036 {
1037 return def;
1038 }
1039}
1040
1042void SetProfileValueBool(string name, bool value)
1043{
1044 if (value)
1045 {
1046 GetGame().SetProfileString(name, "1");
1047 }
1048 else
1049 {
1050 GetGame().SetProfileString(name, "0");
1051 }
1052}
1053
1054
1055int GetNumberOfSetBits(int i)//leaving here for legacy/modding reasons
1056{
1057 return Math.GetNumberOfSetBits(i);
1058}
1059
PlayerSpawnPresetDiscreteItemSetSlotData name
one set for cargo
AnimationTimer class. This timer is for animating float value. usage:
Definition tools.c:653
void AnimationTimer(int category=CALL_CATEGORY_SYSTEM)
Definition tools.c:663
string m_UpdateFunction
Definition tools.c:659
override void Tick(float timeslice)
Ticks the timer, is called by timer subsystem.
Definition tools.c:702
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)
Definition tools.c:673
override bool IsRunning()
Definition tools.c:695
string m_FinishedFunction
Definition tools.c:660
ref Param m_Params
Definition tools.c:661
Managed m_TargetObject
Definition tools.c:658
float GetValue()
Returns actual animated value.
Definition tools.c:691
void ~AnimationTimer()
Definition tools.c:668
DragQueue Class provide callbacks while mouse is dragging. Callback function must have exact argument...
Definition tools.c:154
Super root of all classes in Enforce script.
Definition enscript.c:11
TODO doc.
Definition enscript.c:118
Definition enmath.c:7
Base Param Class with no parameters. Used as general purpose parameter overloaded with Param1 to Para...
Definition param.c:12
Simple class for fading Widgets.
Definition tools.c:430
Managed m_target
Definition tools.c:580
void Timer(int category=CALL_CATEGORY_SYSTEM)
Definition tools.c:584
void Run(float duration, Managed obj, string fn_name, Param params=NULL, bool loop=false)
Starts timer.
Definition tools.c:597
ref Param m_params
Definition tools.c:582
string m_function
Definition tools.c:581
void OnTimer()
Definition tools.c:606
override void Stop()
Definition tools.c:619
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
override void Tick()
proto native CGame GetGame()
enum ShapeType ErrorEx
string m_Value
Definition enentity.c:808
MouseState
Definition ensystem.c:311
proto native int GetMouseState(MouseState index)
proto void GetMousePos(out int x, out int y)
void OnInit()
Callback for user defined initialization. Called for all suites during TestHarness....
Definition freezestate.c:81
float m_time
Definition tools.c:224
void SetRunning(bool running)
Definition tools.c:351
map< string, string > TStringMap
Definition tools.c:990
class DragQueue extends CallQueue m_running
TimerBase Class provide just interface for all Timer classes. Don't instance this class,...
float m_RunTime
Definition tools.c:226
bool GetProfileValueBool(string name, bool def=false)
Return value from profile variable, if variable with given name is not present, default value is retu...
Definition tools.c:1021
void ~TimerBase()
Definition tools.c:228
const int CALL_CATEGORY_GAMEPLAY
Definition tools.c:10
bool m_loop
Definition tools.c:222
float m_duration
Definition tools.c:223
array< TimerBase > m_timerQueue
Definition tools.c:225
bool IsRunning()
Definition tools.c:264
void OnTimerQueueDestoryed()
System function, don't call.
Definition tools.c:303
void Pause()
Pause Timer, internal counter is not restarted, so timer can continue later. Can be unpaused via Cont...
Definition tools.c:239
void OnStart(float duration, bool loop)
Definition tools.c:340
float GetRemaining()
Definition tools.c:318
float GetTime()
Definition tools.c:308
void Continue()
Timer continue when it was paused.
Definition tools.c:247
const int CALL_CATEGORY_GUI
Definition tools.c:9
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8
float GetRunTime()
Definition tools.c:323
float GetDuration()
Definition tools.c:313
void SetProfileValueBool(string name, bool value)
Writes bool variable to profile, after write don't forget to call CGame::SaveProfile() to save profil...
Definition tools.c:1042
void OnTimer()
DEPRECATED.
Definition tools.c:350
int GetTemperatureColor(int temperature)
Definition tools.c:992
int GetNumberOfSetBits(int i)
Definition tools.c:1055
const int CALL_CATEGORY_COUNT
Definition tools.c:12
override float Get()
void OnStart(Param par=null)
int ARGB(int a, int r, int g, int b)
Definition proto.c:322
void Clear(bool clearFile=false)