]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/util.qh
Merge branch 'master' into fruitiex/fruitbalance
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qh
1 #define ACCUMULATE_FUNCTION(func,otherfunc) \
2         #ifdef func \
3         void __merge__##otherfunc() { func(); otherfunc(); } \
4         #undef func \
5         #define func __merge__##otherfunc \
6         #else \
7         #define func otherfunc \
8         #endif
9
10 // this returns a tempstring containing a copy of s with additional \n newlines added, it also replaces \n in the text with a real newline
11 // NOTE: s IS allowed to be a tempstring
12 string wordwrap(string s, float l);
13 #ifndef MENUQC
14 #ifndef CSQC
15 void wordwrap_sprint(string s, float l);
16 #endif
17 #endif
18 void wordwrap_cb(string s, float l, void(string) callback)
19
20 float GameCommand_Generic(string cmd);
21 // returns TRUE if handled, FALSE otherwise
22 // uses tokenize on its argument!
23
24 // iterative depth-first search, with fields that go "up", "down left" and "right" in a tree
25 // for each element, funcPre is called first, then funcPre and funcPost for all its children, and funcPost last
26 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass);
27
28 float median(float a, float b, float c);
29
30 // converts a number to a string with the indicated number of decimals
31 // works for up to 10 decimals!
32 string ftos_decimals(float number, float decimals);
33
34 vector colormapPaletteColor(float c, float isPants);
35
36 // unzone the string, and return it as tempstring. Safe to be called on string_null
37 string fstrunzone(string s);
38
39 // database (NOTE: keys are case sensitive)
40 void db_save(float db, string filename);
41 void db_dump(float db, string pFilename);
42 float db_create();
43 float db_load(string filename);
44 void db_close(float db);
45 string db_get(float db, string key);
46 void db_put(float db, string key, string value);
47
48 // stringbuffer loading/saving
49 float buf_load(string filename);
50 void buf_save(float buf, string filename);
51
52 // modulo function
53 #ifndef MENUQC
54 float mod(float a, float b) { return a - (floor(a / b) * b); }   
55 #endif
56
57 string GametypeNameFromType(float g);
58 #define TIME_TO_NTHS(t,n) floor((t) * (n) + 0.4)
59 string mmsss(float t);
60 string mmssss(float t);
61
62 #define TIME_DECIMALS 2
63 #define TIME_FACTOR 100
64 #define TIME_ENCODED_TOSTRING(n) mmssss(n)
65 #define RACE_RECORD "/race100record/"
66 #define CTS_RECORD "/cts100record/"
67 #define TIME_ENCODE(t) TIME_TO_NTHS(t, TIME_FACTOR)
68 #define TIME_DECODE(n) ((n) / TIME_FACTOR)
69
70 string ScoreString(float vflags, float value);
71
72 vector cross(vector a, vector b);
73
74 void compressShortVector_init();
75 vector decompressShortVector(float data);
76 float compressShortVector(vector vec);
77
78 #ifndef MENUQC
79 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz);
80 #endif
81
82 string fixPriorityList(string pl, float from, float to, float subtract, float complete);
83 string swapInPriorityList(string order, float i, float j);
84
85 float cvar_value_issafe(string s);
86
87 void cvar_settemp(string pKey, string pValue);
88 void cvar_settemp_restore();
89
90 #ifndef MENUQC
91 // modes: 0 = trust q3map2 (_mini images)
92 //        1 = trust tracebox (_radar images)
93 // in both modes, mapinfo's "size" overrides
94
95 string mi_shortname;
96 vector mi_min;
97 vector mi_max;
98 void get_mi_min_max(float mode);
99
100 vector mi_picmin; // adjusted mins that map to the picture (square)
101 vector mi_picmax; // adjusted maxs that map to the picture (square)
102 vector mi_pictexcoord0; // texcoords of the image corners (after transforming, these are 2D coords too)
103 vector mi_pictexcoord1; // texcoords of the image corners (after transforming, these are 2D coords too)
104 vector mi_pictexcoord2; // texcoords of the image corners (after transforming, these are 2D coords too)
105 vector mi_pictexcoord3; // texcoords of the image corners (after transforming, these are 2D coords too)
106 void get_mi_min_max_texcoords(float mode);
107 #endif
108
109 #define FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(x) void reference_##x() { x = x; }
110
111 float almost_equals(float a, float b);
112 float almost_in_bounds(float a, float b, float c);
113
114 float power2of(float e);
115 float log2of(float x);
116
117 string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
118 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
119 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
120 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))
121
122 vector rgb_to_hsl(vector rgb);
123 vector hsl_to_rgb(vector hsl);
124 vector rgb_to_hsv(vector rgb);
125 vector hsv_to_rgb(vector hsv);
126 string rgb_to_hexcolor(vector rgb);
127
128 float boxesoverlap(vector m1, vector m2, vector m3, vector m4);
129 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs);
130
131 typedef float(string s, vector size) textLengthUpToWidth_widthFunction_t;
132 typedef float(string s) textLengthUpToLength_lenFunction_t;
133 float textLengthUpToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
134 string textShortenToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
135 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
136 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
137
138 string getWrappedLine_remaining;
139 string getWrappedLine(float w, vector size, textLengthUpToWidth_widthFunction_t tw);
140 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw);
141
142 float isGametypeInFilter(float gt, float tp, string pattern);
143
144 typedef void(float i1, float i2, entity pass) swapfunc_t; // is only ever called for i1 < i2
145 typedef float(float i1, float i2, entity pass) comparefunc_t; // <0 for <, ==0 for ==, >0 for > (like strcmp)
146 void shuffle(float n, swapfunc_t swap, entity pass);
147 void heapsort(float n, swapfunc_t swap, comparefunc_t cmp, entity pass);
148
149 string swapwords(string str, float i, float j);
150 string shufflewords(string str);
151
152 string substring_range(string s, float b, float e);
153
154 vector solve_quadratic(float a, float b, float c);
155 // solution 1 -> x
156 // solution 2 -> y
157 // z = 1 if a real solution exists, 0 if not
158 // if no real solution exists, x contains the real part and y the imaginary part of the complex solutions x+iy and x-iy
159
160 void check_unacceptable_compiler_bugs();
161
162 float compressShotOrigin(vector v);
163 vector decompressShotOrigin(float f);
164
165 string rankings_reply, lsmaps_reply, lsnewmaps_reply, maplist_reply; // cached replies
166 string records_reply[10];
167
168 float RandomSelection_totalweight;
169 float RandomSelection_best_priority;
170 entity RandomSelection_chosen_ent;
171 float RandomSelection_chosen_float;
172 string RandomSelection_chosen_string;
173 void RandomSelection_Init();
174 void RandomSelection_Add(entity e, float f, string s, float weight, float priority);
175
176 vector healtharmor_maxdamage(float h, float a, float armorblock); // returns vector: maxdamage, armorideal, 1 if fully armored
177 vector healtharmor_applydamage(float a, float armorblock, float damage); // returns vector: take, save, 0
178
179 string getcurrentmod();
180
181 #ifndef MENUQC
182 #ifdef CSQC
183 float ReadInt24_t();
184 #else
185 void WriteInt24_t(float dest, float val);
186 #endif
187 #endif
188
189 // the NULL function
190 const var void func_null(void); FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(func_null)
191 const var string string_null;
192 float float2range11(float f);
193 float float2range01(float f);
194
195 float gsl_ran_gaussian(float sigma);
196
197 string car(string s); // returns first word
198 string cdr(string s); // returns all but first word
199 float matchacl(string acl, string str); // matches str against ACL acl (with entries +foo*, +foo, +*foo, +*foo*, and same with - for forbidding)
200 float startsWith(string haystack, string needle);
201 float startsWithNocase(string haystack, string needle);
202
203 string get_model_datafilename(string mod, float skn, string fil); // skin -1 will return wildcard, mod string_null will also put wildcard there
204 string get_model_parameters_modelname;
205 float get_model_parameters_modelskin;
206 string get_model_parameters_name;
207 float get_model_parameters_species;
208 string get_model_parameters_sex;
209 float get_model_parameters_weight;
210 float get_model_parameters_age;
211 string get_model_parameters_desc;
212 float get_model_parameters(string mod, float skn); // call with string_null to clear; skin -1 means mod is the filename of the txt file and is to be split
213
214 // stupid stupid stupid FTEQCC has a max limit on macro sizes, let's work around by splitting the macro into two macros! :(
215 #define HUD_Panel_GetName_Part2(id) \
216 switch(id) {\
217         case HUD_PANEL_ENGINEINFO: panel_name = HUD_PANELNAME_ENGINEINFO; break; \
218         case HUD_PANEL_INFOMESSAGES: panel_name = HUD_PANELNAME_INFOMESSAGES; break; \
219 }
220
221 // Get name of specified panel id
222 #define HUD_Panel_GetName(id) \
223 switch(id) { \
224         case HUD_PANEL_WEAPONICONS: panel_name = HUD_PANELNAME_WEAPONICONS; break; \
225         case HUD_PANEL_INVENTORY: panel_name = HUD_PANELNAME_INVENTORY; break; \
226         case HUD_PANEL_POWERUPS: panel_name = HUD_PANELNAME_POWERUPS; break; \
227         case HUD_PANEL_HEALTHARMOR: panel_name = HUD_PANELNAME_HEALTHARMOR; break; \
228         case HUD_PANEL_NOTIFY: panel_name = HUD_PANELNAME_NOTIFY; break; \
229         case HUD_PANEL_TIMER: panel_name = HUD_PANELNAME_TIMER; break; \
230         case HUD_PANEL_RADAR: panel_name = HUD_PANELNAME_RADAR; break; \
231         case HUD_PANEL_SCORE: panel_name = HUD_PANELNAME_SCORE; break; \
232         case HUD_PANEL_RACETIMER: panel_name = HUD_PANELNAME_RACETIMER; break; \
233         case HUD_PANEL_VOTE: panel_name = HUD_PANELNAME_VOTE; break; \
234         case HUD_PANEL_MODICONS: panel_name = HUD_PANELNAME_MODICONS; break; \
235         case HUD_PANEL_PRESSEDKEYS: panel_name = HUD_PANELNAME_PRESSEDKEYS; break; \
236         case HUD_PANEL_CHAT: panel_name = HUD_PANELNAME_CHAT; break; \
237 }\
238 HUD_Panel_GetName_Part2(id)