DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
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{
18 string m_function;
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 {
33 }
34
35 void CallParams(Param params)
36 {
37 if (params)
38 {
40 }
41 else
42 {
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
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
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
228 {
229 if (!m_timerQueue) return;
230
231 SetRunning(false);
232 }
233
234
238 void Pause()
239 {
240 SetRunning(false);
241 }
242
246 void Continue()
247 {
248 SetRunning(true);
249 }
250
254 void Stop()
255 {
256 SetRunning(false);
257 m_time = 0;
258 }
259
264 {
265 return m_running;
266 }
267
271 void Tick(float timeslice)
272 {
273 if (IsRunning())
274 {
275 m_time = m_time + timeslice;
276
277 if (m_time >= m_duration)
278 {
279 if (m_loop)
280 {
282 }
283 else
284 {
285 SetRunning(false);
286 m_time = 0;
287 }
288
289 OnTimer();
290 }
291 else
292 {
293 OnUpdate();
294 }
295 }
296 }
297
302 {
303 m_timerQueue = NULL;
304 }
305
306 float GetTime() {
307 return m_time;
308 }
309
310 float GetDuration() {
311 return m_duration;
312 }
313
314 float GetRemaining() {
315 return m_duration - m_time;
316 }
317
318 protected void OnInit(int category)
319 {
320 m_duration = 1;
321 m_loop = false;
322 m_time = 0;
323 m_running = false;
324 if (GetGame())
325 m_timerQueue = GetGame().GetTimerQueue(category);
326 else
327 ErrorEx("Attempting to Init a timer when the game does not exist (GetGame() == null)");
328 }
329
330 protected void OnStart(float duration, bool loop)
331 {
332 m_duration = duration;
333 m_loop = loop;
334 m_time = 0;
335 SetRunning(true);
336 }
337
338 protected void OnUpdate() {}
339 protected void OnTimer() {}
340 protected void SetRunning(bool running)
341 {
342 int index = -1;
343
344 if (m_running == running) return;
345
346 m_running = running;
347 if (m_timerQueue == NULL) return;
348
349 if (running)
350 {
351 if (m_timerQueue.Find(this) == -1)
352 {
353 m_timerQueue.Insert(this);
354 }
355 }
356 else
357 {
358 index = m_timerQueue.Find(this);
359 if (index != -1)
360 {
361 m_timerQueue.Remove(index);
362 }
363 }
364 }
365};
366
367//--------------------------------------------------------------------------
371class TimerQueue extends array<TimerBase>
372{
373 private bool m_processing;
374
375 // -------------------------------------------------------------------------
377 {
378 m_processing = false;
379 }
380
381 // -------------------------------------------------------------------------
383 {
384 if (Count())
385 {
386 for (int i = Count() - 1; i >= 0; i--)
387 {
388 Get(i).OnTimerQueueDestoryed();
389 }
390
391 Clear();
392 }
393 }
394
395 // -------------------------------------------------------------------------
396 void Tick(float timeslice)
397 {
398 if (m_processing) return;
399
400 m_processing = true;
401
402 if (Count())
403 {
404 for (int i = Count() - 1; i >= 0; i--)
405 {
406 Get(i).Tick(timeslice);
407 }
408 }
409
410 m_processing = false;
411 }
412};
413
414//--------------------------------------------------------------------------
418class WidgetFadeTimer extends TimerBase
419{
422 float m_alpha;
423
425 {
427 m_fadeIn = true;
428 }
429
436 void FadeIn(Widget w, float time, bool continue_ = false)
437 {
438 float alpha = w.GetAlpha();
439
440 if (continue_ && alpha > 0.95)
441 {
442 w.SetAlpha(1.0);
443 w.Show(true);
444 return;
445 }
446
447 m_widget = w;
448 m_fadeIn = true;
449
450 OnStart(time, false);
451
452 if (m_widget)
453 {
454 alpha = m_widget.GetAlpha();
455 m_widget.SetAlpha(0);
456 m_widget.Show(true);
457 }
458
459 if (continue_)
460 {
461 m_time = alpha * time;
462 }
463 }
464
471 void FadeOut(Widget w, float time, bool continue_ = false)
472 {
473 m_alpha = w.GetAlpha();
474
475 if (continue_ && m_alpha < 0.05)
476 {
477 w.SetAlpha(0);
478 w.Show(false);
479 return;
480 }
481
482 m_widget = w;
483 m_fadeIn = false;
484
485 OnStart(time, false);
486
487 if (m_widget && !continue_)
488 {
489 m_alpha = 1.0;
490 m_widget.SetAlpha(m_alpha);
491 m_widget.Show(true);
492 }
493
494 if (continue_)
495 {
496 m_time = (1.0 - m_alpha) * time;
497 }
498 }
499
500 override private void OnTimer()
501 {
502 if (m_widget)
503 {
504 if (m_fadeIn)
505 {
506 m_widget.SetAlpha(1);
507 }
508 else
509 {
510 m_widget.SetAlpha(0);
511 m_widget.Show(false);
512 }
513 }
514 }
515
516 override private void OnUpdate()
517 {
518 float timeDiff = m_time / m_duration;
519 float progress = Math.Max( 0.0, timeDiff );
520
521 if ( m_widget )
522 {
523 if ( m_fadeIn )
524 {
525 m_widget.SetAlpha( timeDiff );
526 }
527 else
528 {
529 m_widget.SetAlpha( m_alpha - timeDiff );
530 }
531 }
532 }
533};
534
535//--------------------------------------------------------------------------
565class Timer extends TimerBase
566{
567 protected Managed m_target;
568 protected string m_function;
569 protected ref Param m_params;
570
571 void Timer(int category = CALL_CATEGORY_SYSTEM)
572 {
573 OnInit(category);
574 }
575
584 void Run(float duration, Managed obj, string fn_name, Param params = NULL, bool loop = false)
585 {
586 m_target = obj;
587 m_function = fn_name;
588
589 m_params = params;
590 OnStart(duration, loop);
591 }
592
593 override protected void OnTimer()
594 {
595 if (m_params)
596 {
597 GetGame().GameScript.CallFunctionParams(m_target, m_function, NULL, m_params);
598 m_params = NULL;
599 }
600 else
601 {
602 GetGame().GameScript.CallFunction(m_target, m_function, NULL, 0);
603 }
604 }
605
606 override void Stop()
607 {
608 super.Stop();
609 m_params = NULL;
610 }
611};
612
613
614
615//--------------------------------------------------------------------------
640{
641 private bool m_Active;
642 private float m_TargetValue;
644 private float m_Value;
646 protected string m_UpdateFunction;
647 protected string m_FinishedFunction;
648 protected ref Param m_Params;
649
651 {
652 OnInit(category);
653 }
654
656 {
657 SetRunning(false);
658 }
659
660 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)
661 {
662 SetRunning(true);
663 m_TargetObject = obj;
664 m_UpdateFunction = updateFunc;
665 m_FinishedFunction = finishedFunc;
666 m_TargetValueOriginal = targetVal;
667 m_TargetValue = targetVal;
668 m_time = speed;
669 m_loop = loop;
670 m_Active = true;
671 m_Params = params;
672 m_Value = startingVal;
673 }
674
678 float GetValue() {
679 return m_Value;
680 }
681
682 override bool IsRunning()
683 {
684 return m_Active;
685 }
689 override void Tick(float timeslice)
690 {
691 if ( !m_Active )
692 return;
693
694
695 float diff = Math.AbsFloat(m_TargetValue - m_Value);
696 float step = m_time * timeslice;
697
698 if (diff < step)
699 {
701 if (!m_loop)
702 {
703 m_Active = false;
704 }
705 else
706 {
708 {
709 m_TargetValue = 0;
710 }
711 else
712 {
714 }
715
716 }
718 }
719 else
720 {
721 if (m_TargetValue > m_Value)
722 {
723 m_Value += step;
724 }
725 else
726 {
727 m_Value -= step;
728 }
729 }
730
732 }
733};
734
736{
737 private bool m_active = false;
738 private bool m_loop = false;
739 private float m_target_value = 0;
740 private float m_value = 0;
741 private float m_time = 0;
742
748 void Animate(float val, float speed = 1.0)
749 {
750 m_target_value = val;
751 m_loop = false;
752 m_time = speed;
753 m_active = true;
754 }
755
760 void AnimateLoop(float speed = 1.0)
761 {
762 m_value = 0;
763 m_target_value = 0;
764 m_loop = true;
765 m_time = speed;
766 m_active = true;
767 }
768
772 float GetValue() {
773 return m_value;
774 }
775
780 return m_target_value;
781 }
782
786 void SetValue(float val) {
787 m_value = val;
788 m_target_value = val;
789 }
790
792 {
793 return m_active;
794 }
798 void Tick(float timeslice)
799 {
800 if ( !m_active ) return;
801
802 if (m_loop)
803 {
804 m_target_value += m_time * Math.PI2 * timeslice;
806
808 }
809 else
810 {
811 float diff = Math.AbsFloat(m_target_value - m_value);
812 float step = m_time * timeslice;
813
814 if (diff < step)
815 {
817 m_active = false;
818 }
819 else
820 {
821 if (m_target_value > m_value)
822 {
823 m_value += step;
824 }
825 else
826 {
827 m_value -= step;
828 }
829 }
830 }
831 }
832};
833
854class multiMap<Class K, Class V>
855{
857 private ref array<K> m_keys;
858
859 bool HasKey(K key)
860 {
861 int index = -1;
862 if (m_keys)
863 {
864 index = m_keys.Find(key);
865 }
866
867 if (index != -1)
868 {
869 return true;
870 }
871
872 return false;
873 }
874
876 {
877 int index = -1;
878 if (m_keys)
879 {
880 index = m_keys.Find(key);
881 }
882
883 if (index != -1)
884 {
885 return m_values.Get(index);
886 }
887
888 return NULL;
889 }
890
892 {
893 return m_values.Get(index);
894 }
895
896 K GetKeyByIndex(int index)
897 {
898 return m_keys.Get(index);
899 }
900
901 void Insert(K key, V value)
902 {
903 int index = -1;
904
905 if (!m_keys)
906 {
907 m_keys = new array<K>;
908 m_values = new array<ref array<V> >;
909 }
910 else
911 {
912 index = m_keys.Find(key);
913 }
914
915 if (index == -1)
916 {
917 array<V> value_array = new array<V>;
918 value_array.Insert(value);
919
920 m_keys.Insert(key);
921 m_values.Insert(value_array);
922
923 }
924 else
925 {
926 m_values.Get(index).Insert(value);
927 }
928 }
929
930 void RemoveByIndex(int index)
931 {
932 m_keys.Remove(index);
933 m_values.Remove(index);
934 }
935
936 void Remove(K key)
937 {
938 int index = -1;
939 if (m_keys)
940 {
941 index = m_keys.Find(key);
942 }
943
944 if (index != -1)
945 {
946 RemoveByIndex(index);
947 }
948 }
949
950 int Count()
951 {
952 if (m_keys)
953 {
954 return m_keys.Count();
955 }
956
957 return 0;
958 }
959
960 void Clear()
961 {
962 if ( m_keys && m_values)
963 {
964 m_keys.Clear();
965 m_values.Clear();
966 }
967
968 }
969
971 {
972 Clear();
973 }
974};
975
976// at last one template definition should be here, for template initialization in this script module
978
979int GetTemperatureColor( int temperature )
980{
981 int alpha = 255;
982 int red = 153;
983 int green = 153;
984 int blue = 153;
985 if ( temperature < 20 )
986 {
987 temperature = temperature - 20;
988 temperature = Math.Clamp( temperature, -50, 50);
989 temperature = Math.AbsInt(temperature);
990
991 red = Math.Clamp ( red - ((red/50 )*temperature), 0, 255 );
992 green = Math.Clamp ( green - ((green/50 )*temperature), 0, 255 );
993 blue = Math.Clamp ( blue+((blue/50)*temperature), 0, 255 );
994 }
995 else if ( temperature > 20 )
996 {
997 temperature = Math.Clamp( temperature, -100, 100);
998 blue = Math.Clamp ( blue - ((blue/100 )*temperature), 0, 255 );
999 green = Math.Clamp ( green - ((green/100 )*temperature), 0, 255 );
1000 red = Math.Clamp ( red+((red/100)*temperature), 0, 255 );
1001 }
1002
1003 int color = ARGB( alpha, red, green, blue );
1004 return color;
1005}
1006
1008bool GetProfileValueBool(string name, bool def = false)
1009{
1010 string value;
1011 if (GetGame().GetProfileString(name, value))
1012 {
1013 if (value == "true" || value == "1")
1014 {
1015 return true;
1016 }
1017 else
1018 {
1019 return false;
1020 }
1021 }
1022 else
1023 {
1024 return def;
1025 }
1026}
1027
1029void SetProfileValueBool(string name, bool value)
1030{
1031 if (value)
1032 {
1034 }
1035 else
1036 {
1038 }
1039}
1040
1041
1042int GetNumberOfSetBits(int i)//leaving here for legacy/modding reasons
1043{
1044 return Math.GetNumberOfSetBits(i);
1045}
1046
void OnInit()
Definition AIBehaviour.c:49
void Remove(Object object)
protected void OnStart(Param par=null)
array< ref PlayerStatBase > Get()
string name
void Tick()
AnimationTimer class. This timer is for animating float value. usage:
Definition tools.c:640
protected string m_UpdateFunction
Definition tools.c:646
private float m_TargetValue
Definition tools.c:642
protected Managed m_TargetObject
Definition tools.c:645
private float m_Value
Definition tools.c:644
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:660
protected string m_FinishedFunction
Definition tools.c:647
float GetValue()
Returns actual animated value.
Definition tools.c:678
protected ref Param m_Params
Definition tools.c:648
override void Tick(float timeslice)
Ticks the timer, is called by timer subsystem.
Definition tools.c:689
void ~AnimationTimer()
Definition tools.c:655
private float m_TargetValueOriginal
Definition tools.c:643
void AnimationTimer(int category=CALL_CATEGORY_SYSTEM)
Definition tools.c:650
private bool m_Active
Definition tools.c:641
override bool IsRunning()
Definition tools.c:682
bool IsRunning()
Definition tools.c:791
private bool m_loop
Definition tools.c:738
private bool m_active
Definition tools.c:737
private float m_time
Definition tools.c:741
float GetValue()
Returns actual animated value.
Definition tools.c:772
private float m_target_value
Definition tools.c:739
void AnimateLoop(float speed=1.0)
Starts infinite animation loop <-1,1>. Based on sinus transition.
Definition tools.c:760
float GetTargetValue()
Returns target value. While AnimateLoop returns angle of cycle in radians.
Definition tools.c:779
void Animate(float val, float speed=1.0)
Starts animate value until value reaches target value.
Definition tools.c:748
private float m_value
Definition tools.c:740
void SetValue(float val)
Sets both value and target value.
Definition tools.c:786
void Tick(float timeslice)
Ticks the timer, is called by timer subsystem.
Definition tools.c:798
override TimerQueue GetTimerQueue(int call_category)
Definition DayZGame.c:1168
proto native void SetProfileString(string name, string value)
Sets string to profile variable.
ScriptModule GameScript
Definition Game.c:12
void Call()
Definition tools.c:30
void CallParams(Param params)
Definition tools.c:35
ref Param m_params
Definition tools.c:19
string m_function
Definition tools.c:18
bool IsValid()
Definition tools.c:51
void Invalidate()
Definition tools.c:47
void CallQueueContext(Class target, string fn, Param params)
Definition tools.c:22
Class m_target
Definition tools.c:17
DragQueue Class provide callbacks while mouse is dragging. Callback function must have exact argument...
Definition tools.c:154
private ref Param3< int, int, bool > m_mouse_params
Definition tools.c:155
override void Tick()
System function, don't call it.
Definition tools.c:165
void DragQueue()
Definition tools.c:157
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:419
void Run(float duration, Managed obj, string fn_name, Param params=NULL, bool loop=false)
Starts timer.
Definition tools.c:584
override protected void OnTimer()
Definition tools.c:593
override private void OnTimer()
Definition tools.c:500
override private void OnUpdate()
Definition tools.c:516
void FadeOut(Widget w, float time, bool continue_=false)
Make "fade out" effect on Widget (transparency goes from 1.0 to 0.0)
Definition tools.c:471
void FadeIn(Widget w, float time, bool continue_=false)
Make "fade in" effect on Widget (transparency goes from 0.0 to 1.0)
Definition tools.c:436
protected ref Param m_params
Definition tools.c:569
protected Managed m_target
Definition tools.c:567
void Timer(int category=CALL_CATEGORY_SYSTEM)
Definition tools.c:571
override void Stop()
Definition tools.c:606
float m_alpha
Definition tools.c:422
void WidgetFadeTimer()
Definition tools.c:424
bool m_fadeIn
Definition tools.c:421
protected string m_function
Definition tools.c:568
private Widget m_widget
Definition tools.c:420
private bool m_processing
Definition tools.c:373
void TimerQueue()
Definition tools.c:376
void ~TimerQueue()
Definition tools.c:382
void Tick(float timeslice)
Definition tools.c:396
void Call(Class obj, string fn_name, Param params=NULL)
Creates new call request, add it on queue and execute during frame update (depends on call category)
Definition tools.c:108
private bool m_processing
Definition tools.c:68
void RemoveCalls(Class obj)
Removes all queued calls for object (call this function when object is going to be deleted)
Definition tools.c:118
void Tick()
System function, don't call it.
Definition tools.c:78
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
array< V > GetByIndex(int index)
Definition tools.c:891
K GetKeyByIndex(int index)
Definition tools.c:896
private ref array< ref array< V > > m_values
Definition tools.c:856
void Insert(K key, V value)
Definition tools.c:901
void RemoveByIndex(int index)
Definition tools.c:930
array< V > Get(K key)
Definition tools.c:875
private ref array< K > m_keys
Definition tools.c:857
proto native CGame GetGame()
enum ShapeType ErrorEx
proto volatile int CallFunction(Class inst, string function, out void returnVal, void parm)
proto volatile int CallFunctionParams(Class inst, string function, out void returnVal, Class parms)
static proto float Max(float x, float y)
Returns bigger of two given values.
static proto float Clamp(float value, float min, float max)
Clamps 'value' to 'min' if it is lower than 'min', or to 'max' if it is higher than 'max'.
static proto float AbsFloat(float f)
Returns absolute value.
static const float PI2
Definition EnMath.c:13
static proto int GetNumberOfSetBits(int i)
returns the number of bits set in a bitmask i
static proto float Sin(float angle)
Returns sinus of angle in radians.
static proto int AbsInt(int i)
Returns absolute value.
MouseState
Definition EnSystem.c:311
proto native int GetMouseState(MouseState index)
proto void GetMousePos(out int x, out int y)
protected array< TimerBase > m_timerQueue
Definition tools.c:225
protected void OnStart(float duration, bool loop)
Definition tools.c:330
map< string, string > TStringMap
Definition tools.c:977
class DragQueue extends CallQueue m_running
TimerBase Class provide just interface for all Timer classes. Don't instance this class,...
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:1008
void ~TimerBase()
Definition tools.c:227
protected float m_duration
Definition tools.c:223
protected void SetRunning(bool running)
Definition tools.c:340
protected float m_time
Definition tools.c:224
const int CALL_CATEGORY_GAMEPLAY
Definition tools.c:10
bool IsRunning()
Definition tools.c:263
void OnTimerQueueDestoryed()
System function, don't call.
Definition tools.c:301
void Pause()
Pause Timer, internal counter is not restarted, so timer can continue later. Can be unpaused via Cont...
Definition tools.c:238
protected bool m_loop
Definition tools.c:222
float GetRemaining()
Definition tools.c:314
float GetTime()
Definition tools.c:306
void Continue()
Timer continue when it was paused.
Definition tools.c:246
const int CALL_CATEGORY_GUI
Definition tools.c:9
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8
float GetDuration()
Definition tools.c:310
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:1029
int GetTemperatureColor(int temperature)
Definition tools.c:979
int GetNumberOfSetBits(int i)
Definition tools.c:1042
const int CALL_CATEGORY_COUNT
Definition tools.c:12
int ARGB(int a, int r, int g, int b)
Definition proto.c:322