DayZ 1.24
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)
39 else
41 }
42
44 {
45 m_valid = false;
46 }
47
48 bool IsValid()
49 {
50 return m_valid;
51 }
52};
53
54//--------------------------------------------------------------------------
64class CallQueue extends array<ref CallQueueContext>
65{
66 private bool m_processing;
67
68 void CallQueue()
69 {
70 m_processing = false;
71 }
72
76 void Tick()
77 {
78 if (m_processing) return;
79
80 m_processing = true;
81
82 while (Count() > 0)
83 {
84 CallQueueContext ctx = Get(0);
85 if (!ctx.IsValid())
86 Remove(0);
87 else
88 {
89 Remove(0);
90 ctx.Call();
91 }
92 }
93
94 m_processing = false;
95 }
96
104 void Call(Class obj, string fn_name, Param params = NULL)
105 {
106 Insert(new CallQueueContext(obj, fn_name, params));
107 }
108
109
115 {
116 if (Count())
117 {
118 for (int i = Count() - 1; i >= 0; i--)
119 {
120 CallQueueContext ctx = Get(i);
121 if (ctx.m_target == obj)
122 ctx.Invalidate();
123 }
124 }
125 }
126};
127
128//--------------------------------------------------------------------------
147class DragQueue extends CallQueue
148{
149 private ref Param3<int, int, bool> m_mouse_params; // x,y, is_holding_mb
150
152 {
153 m_mouse_params = new Param3<int, int, bool>(0, 0, true);
154 }
155
159 override void Tick()
160 {
161 if (m_processing) return;
162
163 m_processing = true;
164
165 int last_index = 0;
166 int mouse_x;
167 int mouse_y;
168 bool is_holding = false;
170
171 if (GetMouseState(MouseState.LEFT) & 0x80000000)
172 is_holding = true;
173
175
176 if (!is_holding || mouse_x != m_mouse_params.param1 || mouse_y != m_mouse_params.param2)
177 {
178 m_mouse_params.param1 = mouse_x;
179 m_mouse_params.param2 = mouse_y;
180 m_mouse_params.param3 = is_holding;
181
182 while (Count() > last_index)
183 {
184 ctx = Get(last_index);
185 if (!ctx.IsValid())
187 else
188 {
189 ctx.CallParams(m_mouse_params);
190 last_index++;
191 }
192 }
193 }
194
195 // clear queue when mouse button is released
196 if (!is_holding)
197 Clear();
198
199 m_processing = false;
200 }
201}
202
203//--------------------------------------------------------------------------
207class TimerBase: Managed
208{
209 protected bool m_running;
210 protected bool m_loop;
211 protected float m_duration;
212 protected float m_time;
214 protected float m_RunTime;
215
217 {
218 if (!m_timerQueue) return;
219
220 SetRunning(false);
221 }
222
223
227 void Pause()
228 {
229 SetRunning(false);
230 }
231
235 void Continue()
236 {
237 SetRunning(true);
238 }
239
243 void Stop()
244 {
245 SetRunning(false);
246 m_time = 0;
247 }
248
253 {
254 return m_running;
255 }
256
260 void Tick(float timeslice)
261 {
262 if (IsRunning())
263 {
266
267 if (m_time >= m_duration)
268 {
269 if (m_loop)
271 else
272 {
273 SetRunning(false);
274 m_time = 0;
275 }
276
277 OnTimer();
278 }
279 else
280 OnUpdate();
281 }
282 }
283
288 {
290 }
291
292 float GetTime()
293 {
294 return m_time;
295 }
296
298 {
299 return m_duration;
300 }
301
303 {
304 return m_duration - m_time;
305 }
306
308 {
309 return m_RunTime;
310 }
311
312 protected void OnInit(int category)
313 {
314 m_duration = 1;
315 m_loop = false;
316 m_time = 0;
317 m_running = false;
318 if (GetGame())
319 m_timerQueue = GetGame().GetTimerQueue(category);
320 else
321 ErrorEx("Attempting to Init a timer when the game does not exist (GetGame() == null)");
322 }
323
324 protected void OnStart(float duration, bool loop)
325 {
326 m_RunTime = 0;
328 m_loop = loop;
329 m_time = 0;
330 SetRunning(true);
331 }
332
333 protected void OnUpdate() {}
334 protected void OnTimer() {}
335 protected void SetRunning(bool running)
336 {
337 int index = -1;
338
339 if (m_running == running) return;
340
342 if (m_timerQueue == NULL) return;
343
344 if (running)
345 {
346 if (m_timerQueue.Find(this) == -1)
347 m_timerQueue.Insert(this);
348 }
349 else
350 {
351 index = m_timerQueue.Find(this);
352 if (index != -1)
353 m_timerQueue.Remove(index);
354 }
355 }
356};
357
358//--------------------------------------------------------------------------
362class TimerQueue extends array<TimerBase>
363{
364 private bool m_processing;
365
366 // -------------------------------------------------------------------------
368 {
369 m_processing = false;
370 }
371
372 // -------------------------------------------------------------------------
374 {
375 if (Count())
376 {
377 for (int i = Count() - 1; i >= 0; i--)
378 Get(i).OnTimerQueueDestoryed();
379
380 Clear();
381 }
382 }
383
384 // -------------------------------------------------------------------------
385 void Tick(float timeslice)
386 {
387 if (m_processing) return;
388
389 m_processing = true;
390
391 if (Count())
392 {
393 for (int i = Count() - 1; i >= 0; i--)
394 Get(i).Tick(timeslice);
395 }
396
397 m_processing = false;
398 }
399};
400
401//--------------------------------------------------------------------------
405class WidgetFadeTimer extends TimerBase
406{
409 float m_alpha;
410
412 {
414 m_fadeIn = true;
415 }
416
423 void FadeIn(Widget w, float time, bool continue_ = false)
424 {
425 m_alpha = w.GetAlpha();
426
427 if (continue_ && m_alpha > 0.95)
428 {
429 w.SetAlpha(1.0);
430 w.Show(true);
431 return;
432 }
433
434 m_widget = w;
435 m_fadeIn = true;
436
437 OnStart(time, false);
438
439 if (m_widget)
440 {
441 m_alpha = m_widget.GetAlpha();
442 m_widget.SetAlpha(0);
443 m_widget.Show(true);
444 }
445
446 if (continue_)
447 m_time = m_alpha * time;
448 }
449
456 void FadeOut(Widget w, float time, bool continue_ = false)
457 {
458 m_alpha = w.GetAlpha();
459
460 if (continue_ && m_alpha < 0.05)
461 {
462 w.SetAlpha(0);
463 w.Show(false);
464 return;
465 }
466
467 m_widget = w;
468 m_fadeIn = false;
469
470 OnStart(time, false);
471
472 if (m_widget && !continue_)
473 {
474 m_alpha = 1.0;
475 m_widget.SetAlpha(m_alpha);
476 m_widget.Show(true);
477 }
478
479 if (continue_)
480 m_time = (1.0 - m_alpha) * time;
481 }
482
483 override private void OnTimer()
484 {
485 if (m_widget)
486 {
487 if (m_fadeIn)
488 m_widget.SetAlpha(1);
489 else
490 {
491 m_widget.SetAlpha(0);
492 m_widget.Show(false);
493 }
494 }
495 }
496
497 override private void OnUpdate()
498 {
499 float timeDiff = m_time / m_duration;
500 float progress;
501 if (m_widget)
502 {
503 if (m_fadeIn)
505 else
506 {
507 progress = Math.Lerp(m_alpha, 0, timeDiff);
508 progress = Math.Clamp(progress, 0, 1);
509 }
510
511 m_widget.SetAlpha(progress);
512 }
513 }
514};
515
516//--------------------------------------------------------------------------
546class Timer extends TimerBase
547{
548 protected Managed m_target;
549 protected string m_function;
550 protected ref Param m_params;
551
553 {
555 }
556
565 void Run(float duration, Managed obj, string fn_name, Param params = NULL, bool loop = false)
566 {
567 m_target = obj;
568 m_function = fn_name;
569
570 m_params = params;
572 }
573
574 override protected void OnTimer()
575 {
576 if (m_params)
577 {
578 GetGame().GameScript.CallFunctionParams(m_target, m_function, NULL, m_params);
579 m_params = NULL;
580 }
581 else
582 GetGame().GameScript.CallFunction(m_target, m_function, NULL, 0);
583 }
584
585 override void Stop()
586 {
587 super.Stop();
588 m_params = NULL;
589 }
590};
591
592
593//--------------------------------------------------------------------------
618{
619 private bool m_Active;
620 private float m_TargetValue;
622 private float m_Value;
624 protected string m_UpdateFunction;
625 protected string m_FinishedFunction;
626 protected ref Param m_Params;
627
632
634 {
635 SetRunning(false);
636 }
637
638 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)
639 {
640 SetRunning(true);
641 m_TargetObject = obj;
646 m_time = speed;
647 m_loop = loop;
648 m_Active = true;
651 }
652
656 float GetValue()
657 {
658 return m_Value;
659 }
660
661 override bool IsRunning()
662 {
663 return m_Active;
664 }
668 override void Tick(float timeslice)
669 {
670 if (!m_Active)
671 return;
672
673
675 float step = m_time * timeslice;
676
677 if (diff < step)
678 {
680 if (!m_loop)
681 m_Active = false;
682 else
683 {
685 m_TargetValue = 0;
686 else
688
689 }
690 GetGame().GameScript.CallFunction(m_TargetObject, m_FinishedFunction, NULL, m_Params);
691 }
692 else
693 {
695 m_Value += step;
696 else
697 m_Value -= step;
698 }
699
700 GetGame().GameScript.CallFunction(m_TargetObject, m_UpdateFunction, NULL, m_Params);
701 }
702};
703
705{
706 private bool m_active = false;
707 private bool m_loop = false;
708 private float m_target_value = 0;
709 private float m_value = 0;
710 private float m_time = 0;
711
717 void Animate(float val, float speed = 1.0)
718 {
720 m_loop = false;
721 m_time = speed;
722 m_active = true;
723 }
724
729 void AnimateLoop(float speed = 1.0)
730 {
731 m_value = 0;
732 m_target_value = 0;
733 m_loop = true;
734 m_time = speed;
735 m_active = true;
736 }
737
741 float GetValue()
742 {
743 return m_value;
744 }
745
750 {
751 return m_target_value;
752 }
753
757 void SetValue(float val)
758 {
759 m_value = val;
761 }
762
764 {
765 return m_active;
766 }
770 void Tick(float timeslice)
771 {
772 if (!m_active) return;
773
774 if (m_loop)
775 {
778
780 }
781 else
782 {
784 float step = m_time * timeslice;
785
786 if (diff < step)
787 {
789 m_active = false;
790 }
791 else
792 {
794 m_value += step;
795 else
796 m_value -= step;
797 }
798 }
799 }
800};
801
823{
826
828 {
829 int index = -1;
830 if (m_keys)
831 index = m_keys.Find(key);
832
833 if (index != -1)
834 return true;
835
836 return false;
837 }
838
840 {
841 int index = -1;
842 if (m_keys)
843 index = m_keys.Find(key);
844
845 if (index != -1)
846 return m_values.Get(index);
847
848 return NULL;
849 }
850
852 {
853 return m_values.Get(index);
854 }
855
857 {
858 return m_keys.Get(index);
859 }
860
862 {
863 int index = -1;
864
865 if (!m_keys)
866 {
867 m_keys = new array<K>;
868 m_values = new array<ref array<V> >;
869 }
870 else
871 index = m_keys.Find(key);
872
873 if (index == -1)
874 {
876 value_array.Insert(value);
877
878 m_keys.Insert(key);
879 m_values.Insert(value_array);
880
881 }
882 else
883 m_values.Get(index).Insert(value);
884 }
885
887 {
888 m_keys.Remove(index);
889 m_values.Remove(index);
890 }
891
893 {
894 int index = -1;
895 if (m_keys)
896 index = m_keys.Find(key);
897
898 if (index != -1)
899 RemoveByIndex(index);
900 }
901
902 int Count()
903 {
904 if (m_keys)
905 return m_keys.Count();
906
907 return 0;
908 }
909
910 void Clear()
911 {
912 if (m_keys && m_values)
913 {
914 m_keys.Clear();
915 m_values.Clear();
916 }
917
918 }
919
921 {
922 Clear();
923 }
924};
925
926// at last one template definition should be here, for template initialization in this script module
928
930{
931 int alpha = 255;
932 int red = 153;
933 int green = 153;
934 int blue = 153;
935 if (temperature < 20)
936 {
938 temperature = Math.Clamp(temperature, -50, 50);
940
941 red = Math.Clamp(red - ((red / 50) * temperature), 0, 255);
942 green = Math.Clamp(green - ((green / 50) * temperature), 0, 255);
943 blue = Math.Clamp(blue + ((blue / 50) * temperature), 0, 255);
944 }
945 else if (temperature > 20)
946 {
947 temperature = Math.Clamp(temperature, -100, 100);
948 blue = Math.Clamp(blue - ((blue / 100) * temperature), 0, 255);
949 green = Math.Clamp(green - ((green / 100) * temperature), 0, 255);
950 red = Math.Clamp(red + ((red / 100) * temperature), 0, 255);
951 }
952
953 int color = ARGB(alpha, red, green, blue);
954 return color;
955}
956
958bool GetProfileValueBool(string name, bool def = false)
959{
960 string value;
961 if (GetGame().GetProfileString(name, value))
962 {
963 if (value == "true" || value == "1")
964 return true;
965 else
966 return false;
967 }
968 else
969 return def;
970}
971
974{
975 if (value)
976 GetGame().SetProfileString(name, "1");
977 else
978 GetGame().SetProfileString(name, "0");
979}
980
981
982int GetNumberOfSetBits(int i)//leaving here for legacy/modding reasons
983{
984 return Math.GetNumberOfSetBits(i);
985}
986
987
void OnInit()
Definition AIBehaviour.c:49
void Remove(Object object)
PlayerSpawnPresetDiscreteItemSetSlotData name
one set for cargo
void OnStart(Param par=null)
array< ref PlayerStatBase > Get()
void Clear(bool clearFile=false)
void Tick()
AnimationTimer class. This timer is for animating float value. usage:
Definition tools.c:618
void AnimationTimer(int category=CALL_CATEGORY_SYSTEM)
Definition tools.c:628
string m_UpdateFunction
Definition tools.c:624
override void Tick(float timeslice)
Ticks the timer, is called by timer subsystem.
Definition tools.c:668
float m_TargetValueOriginal
Definition tools.c:621
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:638
override bool IsRunning()
Definition tools.c:661
string m_FinishedFunction
Definition tools.c:625
float m_Value
Definition tools.c:622
ref Param m_Params
Definition tools.c:626
float m_TargetValue
Definition tools.c:620
Managed m_TargetObject
Definition tools.c:623
float GetValue()
Returns actual animated value.
Definition tools.c:656
bool m_Active
Definition tools.c:619
void ~AnimationTimer()
Definition tools.c:633
void SetValue(float val)
Sets both value and target value.
Definition tools.c:757
float GetValue()
Returns actual animated value.
Definition tools.c:741
bool m_loop
Definition tools.c:707
void Animate(float val, float speed=1.0)
Starts animate value until value reaches target value.
Definition tools.c:717
bool m_active
Definition tools.c:706
float m_time
Definition tools.c:710
void Tick(float timeslice)
Ticks the timer, is called by timer subsystem.
Definition tools.c:770
void AnimateLoop(float speed=1.0)
Starts infinite animation loop <-1,1>. Based on sinus transition.
Definition tools.c:729
float m_target_value
Definition tools.c:708
float m_value
Definition tools.c:709
float GetTargetValue()
Returns target value. While AnimateLoop returns angle of cycle in radians.
Definition tools.c:749
bool IsRunning()
Definition tools.c:763
ScriptModule GameScript
Definition Game.c:12
void CallQueueContext(Class target, string fn, Param params)
Definition tools.c:22
void Invalidate()
Definition tools.c:43
bool IsValid()
Definition tools.c:48
string m_function
Definition tools.c:18
void CallParams(Param params)
Definition tools.c:35
ref Param m_params
Definition tools.c:19
void Call()
Definition tools.c:30
Class m_target
Definition tools.c:17
DragQueue Class provide callbacks while mouse is dragging. Callback function must have exact argument...
Definition tools.c:148
override void Tick()
System function, don't call it.
Definition tools.c:159
void DragQueue()
Definition tools.c:151
ref Param3< int, int, bool > m_mouse_params
Definition tools.c:149
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:406
Managed m_target
Definition tools.c:548
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:456
void OnUpdate()
Definition tools.c:497
void Timer(int category=CALL_CATEGORY_SYSTEM)
Definition tools.c:552
float m_alpha
Definition tools.c:409
void Run(float duration, Managed obj, string fn_name, Param params=NULL, bool loop=false)
Starts timer.
Definition tools.c:565
bool m_fadeIn
Definition tools.c:408
ref Param m_params
Definition tools.c:550
string m_function
Definition tools.c:549
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:423
void WidgetFadeTimer()
Definition tools.c:411
Widget m_widget
Definition tools.c:407
void OnTimer()
Definition tools.c:483
override void Stop()
Definition tools.c:585
void TimerQueue()
Definition tools.c:367
void ~TimerQueue()
Definition tools.c:373
void Tick(float timeslice)
Definition tools.c:385
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:104
void RemoveCalls(Class obj)
Removes all queued calls for object (call this function when object is going to be deleted)
Definition tools.c:114
void Tick()
System function, don't call it.
Definition tools.c:76
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
ref array< ref array< V > > m_values
Definition tools.c:824
K GetKeyByIndex(int index)
Definition tools.c:856
void Insert(K key, V value)
Definition tools.c:861
array< V > Get(K key)
Definition tools.c:839
array< V > GetByIndex(int index)
Definition tools.c:851
ref array< K > m_keys
Definition tools.c:825
void RemoveByIndex(int index)
Definition tools.c:886
proto native CGame GetGame()
enum ShapeType ErrorEx
proto volatile int CallFunctionParams(Class inst, string function, out void returnVal, Class parms)
proto volatile int CallFunction(Class inst, string function, out void returnVal, void parm)
static const float PI2
Definition EnMath.c:13
static proto float Lerp(float a, float b, float time)
Linearly interpolates between 'a' and 'b' given 'time'.
static proto int AbsInt(int i)
Returns absolute value.
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 Sin(float angle)
Returns sinus of angle in radians.
static proto float AbsFloat(float f)
Returns absolute value.
static proto int GetNumberOfSetBits(int i)
returns the number of bits set in a bitmask i
MouseState
Definition EnSystem.c:311
proto native int GetMouseState(MouseState index)
proto void GetMousePos(out int x, out int y)
float m_time
Definition tools.c:212
void SetRunning(bool running)
Definition tools.c:335
map< string, string > TStringMap
Definition tools.c:927
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:214
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:958
void ~TimerBase()
Definition tools.c:216
const int CALL_CATEGORY_GAMEPLAY
Definition tools.c:10
bool m_loop
Definition tools.c:210
float m_duration
Definition tools.c:211
array< TimerBase > m_timerQueue
Definition tools.c:213
bool IsRunning()
Definition tools.c:252
void OnTimerQueueDestoryed()
System function, don't call.
Definition tools.c:287
void Pause()
Pause Timer, internal counter is not restarted, so timer can continue later. Can be unpaused via Cont...
Definition tools.c:227
void OnStart(float duration, bool loop)
Definition tools.c:324
float GetRemaining()
Definition tools.c:302
float GetTime()
Definition tools.c:292
void Continue()
Timer continue when it was paused.
Definition tools.c:235
const int CALL_CATEGORY_GUI
Definition tools.c:9
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8
float GetRunTime()
Definition tools.c:307
float GetDuration()
Definition tools.c:297
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:973
int GetTemperatureColor(int temperature)
Definition tools.c:929
int GetNumberOfSetBits(int i)
Definition tools.c:982
const int CALL_CATEGORY_COUNT
Definition tools.c:12
int ARGB(int a, int r, int g, int b)
Definition proto.c:322