Dayz Explorer 1.29.162510
Loading...
Searching...
No Matches
tools.c
Go to the documentation of this file.
1
6
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// -------------------------------------------------------------------------
15class CallQueueContext
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 g_Game.GameScript.CallFunctionParams(m_target, m_function, NULL, params);
40 }
41 else
42 {
43 g_Game.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 (g_Game)
335 m_timerQueue = g_Game.GetTimerQueue(category);
336 else
337 ErrorEx("Attempting to Init a timer when the game does not exist (g_Game == 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 int count = Count();
414 for (int i = count - 1; i >= 0; i--)
415 {
416 Get(i).Tick(timeslice);
417 }
418
419 m_processing = false;
420 }
421};
422
423//--------------------------------------------------------------------------
427class WidgetFadeTimer extends TimerBase
428{
429 private Widget m_widget;
430 bool m_fadeIn;
431 float m_alpha;
432
433 void WidgetFadeTimer()
434 {
436 m_fadeIn = true;
437 }
438
445 void FadeIn(Widget w, float time, bool continue_ = false)
446 {
447 m_alpha = w.GetAlpha();
448
449 if (continue_ && m_alpha > 0.95)
450 {
451 w.SetAlpha(1.0);
452 w.Show(true);
453 return;
454 }
455
456 m_widget = w;
457 m_fadeIn = true;
458
459 OnStart(time, false);
460
461 if (m_widget)
462 {
463 m_alpha = m_widget.GetAlpha();
464 m_widget.SetAlpha(0);
465 m_widget.Show(true);
466 }
467
468 if (continue_)
469 {
470 m_time = m_alpha * time;
471 }
472 }
473
480 void FadeOut(Widget w, float time, bool continue_ = false)
481 {
482 m_alpha = w.GetAlpha();
483
484 if (continue_ && m_alpha < 0.05)
485 {
486 w.SetAlpha(0);
487 w.Show(false);
488 return;
489 }
490
491 m_widget = w;
492 m_fadeIn = false;
493
494 OnStart(time, false);
495
496 if (m_widget && !continue_)
497 {
498 m_alpha = 1.0;
499 m_widget.SetAlpha(m_alpha);
500 m_widget.Show(true);
501 }
502
503 if (continue_)
504 {
505 m_time = (1.0 - m_alpha) * time;
506 }
507 }
508
509 override private void OnTimer()
510 {
511 if (m_widget)
512 {
513 if (m_fadeIn)
514 {
515 m_widget.SetAlpha(1);
516 }
517 else
518 {
519 m_widget.SetAlpha(0);
520 m_widget.Show(false);
521 }
522 }
523 }
524
525 override private void OnUpdate()
526 {
527 float timeDiff = m_time / m_duration;
528 float progress;
529 if (m_widget)
530 {
531 if (m_fadeIn)
532 {
533 progress = timeDiff;
534 }
535 else
536 {
537 progress = Math.Lerp(m_alpha,0,timeDiff);
538 progress = Math.Clamp(progress,0,1);
539 }
540
541 m_widget.SetAlpha(progress);
542 }
543 }
544};
545
546//--------------------------------------------------------------------------
576class Timer extends TimerBase
577{
578 protected Managed m_target;
579 protected string m_function;
580 protected ref Param m_params;
581
582 void Timer(int category = CALL_CATEGORY_SYSTEM)
583 {
584 OnInit(category);
585 }
586
595 void Run(float duration, Managed obj, string fn_name, Param params = NULL, bool loop = false)
596 {
597 m_target = obj;
598 m_function = fn_name;
599
600 m_params = params;
601 OnStart(duration, loop);
602 }
603
604 override protected void OnTimer()
605 {
606 if (m_params)
607 {
608 g_Game.GameScript.CallFunctionParams(m_target, m_function, NULL, m_params);
609 m_params = NULL;
610 }
611 else
612 {
613 g_Game.GameScript.CallFunction(m_target, m_function, NULL, 0);
614 }
615 }
616
617 override void Stop()
618 {
619 super.Stop();
620 m_params = NULL;
621 }
622};
623
624
625
626//--------------------------------------------------------------------------
649
651{
652 private bool m_Active;
653 private float m_TargetValue;
654 private float m_TargetValueOriginal;
655 private float m_Value;
657 protected string m_UpdateFunction;
658 protected string m_FinishedFunction;
659 protected ref Param m_Params;
660
662 {
663 OnInit(category);
664 }
665
667 {
668 SetRunning(false);
669 }
670
671 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)
672 {
673 SetRunning(true);
674 m_TargetObject = obj;
675 m_UpdateFunction = updateFunc;
676 m_FinishedFunction = finishedFunc;
677 m_TargetValueOriginal = targetVal;
678 m_TargetValue = targetVal;
679 m_time = speed;
680 m_loop = loop;
681 m_Active = true;
682 m_Params = params;
683 m_Value = startingVal;
684 }
685
689 float GetValue() {
690 return m_Value;
691 }
692
693 override bool IsRunning()
694 {
695 return m_Active;
696 }
697
700 override void Tick(float timeslice)
701 {
702 if ( !m_Active )
703 return;
704
705
706 float diff = Math.AbsFloat(m_TargetValue - m_Value);
707 float step = m_time * timeslice;
708
709 if (diff < step)
710 {
711 m_Value = m_TargetValue;
712 if (!m_loop)
713 {
714 m_Active = false;
715 }
716 else
717 {
718 if (m_TargetValue == m_TargetValueOriginal)
719 {
720 m_TargetValue = 0;
721 }
722 else
723 {
724 m_TargetValue = m_TargetValueOriginal;
725 }
726
727 }
728 g_Game.GameScript.CallFunction(m_TargetObject, m_FinishedFunction, NULL, m_Params);
729 }
730 else
731 {
732 if (m_TargetValue > m_Value)
733 {
734 m_Value += step;
735 }
736 else
737 {
738 m_Value -= step;
739 }
740 }
741
742 g_Game.GameScript.CallFunction(m_TargetObject, m_UpdateFunction, NULL, m_Params);
743 }
744};
745
747{
748 private bool m_active = false;
749 private bool m_loop = false;
750 private float m_target_value = 0;
751 private float m_value = 0;
752 private float m_time = 0;
753
759 void Animate(float val, float speed = 1.0)
760 {
761 m_target_value = val;
762 m_loop = false;
763 m_time = speed;
764 m_active = true;
765 }
766
771 void AnimateLoop(float speed = 1.0)
772 {
773 m_value = 0;
774 m_target_value = 0;
775 m_loop = true;
776 m_time = speed;
777 m_active = true;
778 }
779
783 float GetValue() {
784 return m_value;
785 }
786
790 float GetTargetValue() {
791 return m_target_value;
792 }
793
797 void SetValue(float val) {
798 m_value = val;
799 m_target_value = val;
800 }
801
802 bool IsRunning()
803 {
804 return m_active;
805 }
809 void Tick(float timeslice)
810 {
811 if ( !m_active ) return;
812
813 if (m_loop)
814 {
815 m_target_value += m_time * Math.PI2 * timeslice;
816 while (m_target_value > Math.PI2) m_target_value -= Math.PI2;
817
818 m_value = Math.Sin(m_target_value);
819 }
820 else
821 {
822 float diff = Math.AbsFloat(m_target_value - m_value);
823 float step = m_time * timeslice;
824
825 if (diff < step)
826 {
827 m_value = m_target_value;
828 m_active = false;
829 }
830 else
831 {
832 if (m_target_value > m_value)
833 {
834 m_value += step;
835 }
836 else
837 {
838 m_value -= step;
839 }
840 }
841 }
842 }
843};
844
865class multiMap<Class K, Class V>
866{
867 private ref array<ref array<V> > m_values;
868 private ref array<K> m_keys;
869
870 bool HasKey(K key)
871 {
872 int index = -1;
873 if (m_keys)
874 {
875 index = m_keys.Find(key);
876 }
877
878 if (index != -1)
879 {
880 return true;
881 }
882
883 return false;
884 }
885
886 array<V> Get(K key)
887 {
888 int index = -1;
889 if (m_keys)
890 {
891 index = m_keys.Find(key);
892 }
893
894 if (index != -1)
895 {
896 return m_values.Get(index);
897 }
898
899 return NULL;
900 }
901
902 array<V> GetByIndex(int index)
903 {
904 return m_values.Get(index);
905 }
906
907 K GetKeyByIndex(int index)
908 {
909 return m_keys.Get(index);
910 }
911
912 void Insert(K key, V value)
913 {
914 int index = -1;
915
916 if (!m_keys)
917 {
918 m_keys = new array<K>;
919 m_values = new array<ref array<V> >;
920 }
921 else
922 {
923 index = m_keys.Find(key);
924 }
925
926 if (index == -1)
927 {
928 array<V> value_array = new array<V>;
929 value_array.Insert(value);
930
931 m_keys.Insert(key);
932 m_values.Insert(value_array);
933
934 }
935 else
936 {
937 m_values.Get(index).Insert(value);
938 }
939 }
940
941 void RemoveByIndex(int index)
942 {
943 m_keys.Remove(index);
944 m_values.Remove(index);
945 }
946
947 void Remove(K key)
948 {
949 int index = -1;
950 if (m_keys)
951 {
952 index = m_keys.Find(key);
953 }
954
955 if (index != -1)
956 {
957 RemoveByIndex(index);
958 }
959 }
960
961 int Count()
962 {
963 if (m_keys)
964 {
965 return m_keys.Count();
966 }
967
968 return 0;
969 }
970
971 void Clear()
972 {
973 if ( m_keys && m_values)
974 {
975 m_keys.Clear();
976 m_values.Clear();
977 }
978
979 }
980
981 void ~multiMap()
982 {
983 Clear();
984 }
985};
986
987// at last one template definition should be here, for template initialization in this script module
989
990int GetTemperatureColor(int temperature)
991{
992 int alpha = 255;
993 int red = 153;
994 int green = 153;
995 int blue = 153;
996
997 if (temperature < GameConstants.ITEM_TEMPERATURE_NEUTRAL_ZONE_LOWER_LIMIT)
998 {
999 temperature = temperature - 20;
1000 temperature = Math.Clamp( temperature, -50, 50);
1001 temperature = Math.AbsInt(temperature);
1002
1003 red = Math.Clamp ( red - ((red/50 )*temperature), 0, 255);
1004 green = Math.Clamp ( green - ((green/50 )*temperature), 0, 255);
1005 blue = Math.Clamp ( blue+((blue/50)*temperature), 0, 255);
1006 }
1007 else if (temperature > GameConstants.ITEM_TEMPERATURE_NEUTRAL_ZONE_UPPER_LIMIT)
1008 {
1009 temperature = Math.Clamp(temperature, -100, 100);
1010 blue = Math.Clamp (blue - ((blue / 100) * temperature), 0, 255);
1011 green = Math.Clamp (green - ((green / 100) * temperature), 0, 255);
1012 red = Math.Clamp (red + ((red / 100) * temperature), 0, 255);
1013 }
1014
1015 return ARGB(alpha, red, green, blue);
1016}
1017
1019bool GetProfileValueBool(string name, bool def = false)
1020{
1021 string value;
1022 if (g_Game.GetProfileString(name, value))
1023 {
1024 if (value == "true" || value == "1")
1025 {
1026 return true;
1027 }
1028 else
1029 {
1030 return false;
1031 }
1032 }
1033 else
1034 {
1035 return def;
1036 }
1037}
1038
1040void SetProfileValueBool(string name, bool value)
1041{
1042 if (value)
1043 {
1044 g_Game.SetProfileString(name, "1");
1045 }
1046 else
1047 {
1048 g_Game.SetProfileString(name, "0");
1049 }
1050}
1051
1052
1053int GetNumberOfSetBits(int i)//leaving here for legacy/modding reasons
1054{
1055 return Math.GetNumberOfSetBits(i);
1056}
1057
proto bool IsValid()
Checks if the ScriptCaller is valid.
PlayerSpawnPresetDiscreteItemSetSlotData name
one set for cargo
void AnimationTimer(int category=CALL_CATEGORY_SYSTEM)
Definition tools.c:661
string m_UpdateFunction
Definition tools.c:657
override void Tick(float timeslice)
Ticks the timer, is called by timer subsystem.
Definition tools.c:700
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:671
override bool IsRunning()
Definition tools.c:693
string m_FinishedFunction
Definition tools.c:658
ref Param m_Params
Definition tools.c:659
Managed m_TargetObject
Definition tools.c:656
float GetValue()
Returns actual animated value.
Definition tools.c:689
void ~AnimationTimer()
Definition tools.c:666
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:428
Managed m_target
Definition tools.c:578
void Timer(int category=CALL_CATEGORY_SYSTEM)
Definition tools.c:582
void Run(float duration, Managed obj, string fn_name, Param params=NULL, bool loop=false)
Starts timer.
Definition tools.c:595
ref Param m_params
Definition tools.c:580
string m_function
Definition tools.c:579
void OnTimer()
Definition tools.c:604
override void Stop()
Definition tools.c:617
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
override void Tick()
DayZGame g_Game
Definition dayzgame.c:3942
enum ShapeType ErrorEx
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:988
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:1019
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 OnUpdate()
Definition tools.c:349
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:1040
int GetTemperatureColor(int temperature)
Definition tools.c:990
int GetNumberOfSetBits(int i)
Definition tools.c:1053
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)
float GetValue()
Definition syncedvalue.c:55