]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/util.qh
Merge remote-tracking branch 'origin/master' into samual/notification_rewrite
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qh
1 // a dummy macro that prevents the "hanging ;" warning
2 #define ENDS_WITH_CURLY_BRACE
3
4 // return the actual code name of a var as a string
5 #define VAR_TO_TEXT(var) #var
6
7 #ifdef HAVE_YO_DAWG_CPP
8 // TODO make ascii art pic of xzibit
9 // YO DAWG!
10 // I HERD YO LIEK MACROS
11 // SO I PUT A MACRO DEFINITION IN YO MACRO DEFINITION
12 // SO YO CAN EXPAND MACROS WHILE YO EXPAND MACROS
13 # define ACCUMULATE_FUNCTION(func,otherfunc) \
14         #ifdef func \
15         void __merge__##otherfunc() { func(); otherfunc(); } \
16         #undef func \
17         #define func __merge__##otherfunc \
18         #else \
19         #define func otherfunc \
20         #endif
21 # define CALL_ACCUMULATED_FUNCTION(func) \
22         func()
23 #else
24 # define ACCUMULATE_FUNCTION(func,otherfunc) \
25         .void _ACCUMULATE_##func##__##otherfunc;
26 void ACCUMULATE_call(string func)
27 {
28         float i;
29         float n = numentityfields();
30         string funcprefix = strcat("_ACCUMULATE_", func, "__");
31         float funcprefixlen = strlen(funcprefix);
32         for(i = 0; i < n; ++i)
33         {
34                 string name = entityfieldname(i);
35                 if(substring(name, 0, funcprefixlen) == funcprefix)
36                         callfunction(substring(name, funcprefixlen, -1));
37         }
38 }
39 # define CALL_ACCUMULATED_FUNCTION(func) \
40         ACCUMULATE_call(#func)
41 #endif
42
43 // used for simplifying ACCUMULATE_FUNCTIONs
44 #define SET_FIRST_OR_LAST(input,first,count) if(!input) { input = (first + count); }
45 #define SET_FIELD_COUNT(field,first,count) if(!field) { field = (first + count); ++count; }
46 #define CHECK_MAX_COUNT(name,max,count,type) if(count == max) { error(strcat("Maximum ", type, " hit: ", VAR_TO_TEXT(name), ": ", ftos(count), ".\n")); }
47
48 // 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
49 // NOTE: s IS allowed to be a tempstring
50 string wordwrap(string s, float l);
51 #ifndef MENUQC
52 #ifndef CSQC
53 void wordwrap_sprint(string s, float l);
54 #endif
55 #endif
56 void wordwrap_cb(string s, float l, void(string) callback)
57
58 #ifndef SVQC
59 string draw_currentSkin;
60 string draw_UseSkinFor(string pic);
61 #endif
62
63 // iterative depth-first search, with fields that go "up", "down left" and "right" in a tree
64 // for each element, funcPre is called first, then funcPre and funcPost for all its children, and funcPost last
65 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass);
66
67 float median(float a, float b, float c);
68
69 // converts a number to a string with the indicated number of decimals
70 // works for up to 10 decimals!
71 string ftos_decimals(float number, float decimals);
72
73 float fexists(string f);
74
75 vector colormapPaletteColor(float c, float isPants);
76
77 // unzone the string, and return it as tempstring. Safe to be called on string_null
78 string fstrunzone(string s);
79
80 // database (NOTE: keys are case sensitive)
81 void db_save(float db, string filename);
82 void db_dump(float db, string pFilename);
83 float db_create();
84 float db_load(string filename);
85 void db_close(float db);
86 string db_get(float db, string key);
87 void db_put(float db, string key, string value);
88
89 // stringbuffer loading/saving
90 float buf_load(string filename);
91 void buf_save(float buf, string filename);
92
93 // modulo function
94 #ifndef MENUQC
95 float mod(float a, float b) { return a - (floor(a / b) * b); }   
96 #endif
97
98 #define TIME_TO_NTHS(t,n) floor((t) * (n) + 0.4)
99 string mmsss(float t);
100 string mmssss(float t);
101
102 #define TIME_DECIMALS 2
103 #define TIME_FACTOR 100
104 #define TIME_ENCODED_TOSTRING(n) mmssss(n)
105 #define RACE_RECORD "/race100record/"
106 #define CTS_RECORD "/cts100record/"
107 #define TIME_ENCODE(t) TIME_TO_NTHS(t, TIME_FACTOR)
108 #define TIME_DECODE(n) ((n) / TIME_FACTOR)
109
110 string ScoreString(float vflags, float value);
111
112 float dotproduct(vector a, vector b);
113 vector cross(vector a, vector b);
114
115 void compressShortVector_init();
116 vector decompressShortVector(float data);
117 float compressShortVector(vector vec);
118
119 #ifndef MENUQC
120 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz);
121 #endif
122
123 string fixPriorityList(string pl, float from, float to, float subtract, float complete);
124 string mapPriorityList(string order, string(string) mapfunc);
125 string swapInPriorityList(string order, float i, float j);
126
127 float cvar_value_issafe(string s);
128
129 float cvar_settemp(string pKey, string pValue);
130 float cvar_settemp_restore();
131
132 #ifndef MENUQC
133 // modes: 0 = trust q3map2 (_mini images)
134 //        1 = trust tracebox (_radar images)
135 // in both modes, mapinfo's "size" overrides
136
137 string mi_shortname;
138 vector mi_min;
139 vector mi_max;
140 void get_mi_min_max(float mode);
141
142 vector mi_picmin; // adjusted mins that map to the picture (square)
143 vector mi_picmax; // adjusted maxs that map to the picture (square)
144 vector mi_pictexcoord0; // texcoords of the image corners (after transforming, these are 2D coords too)
145 vector mi_pictexcoord1; // texcoords of the image corners (after transforming, these are 2D coords too)
146 vector mi_pictexcoord2; // texcoords of the image corners (after transforming, these are 2D coords too)
147 vector mi_pictexcoord3; // texcoords of the image corners (after transforming, these are 2D coords too)
148 void get_mi_min_max_texcoords(float mode);
149 #endif
150
151 #define FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(x) void reference_##x() { x = x; }
152
153 float almost_equals(float a, float b);
154 float almost_in_bounds(float a, float b, float c);
155
156 float power2of(float e);
157 float log2of(float x);
158
159 string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
160 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
161 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
162 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))
163
164 vector rgb_to_hsl(vector rgb);
165 vector hsl_to_rgb(vector hsl);
166 vector rgb_to_hsv(vector rgb);
167 vector hsv_to_rgb(vector hsv);
168 string rgb_to_hexcolor(vector rgb);
169
170 float boxesoverlap(vector m1, vector m2, vector m3, vector m4);
171 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs);
172
173 typedef float(string s, vector size) textLengthUpToWidth_widthFunction_t;
174 typedef float(string s) textLengthUpToLength_lenFunction_t;
175 float textLengthUpToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
176 string textShortenToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
177 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
178 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
179
180 string getWrappedLine_remaining;
181 string getWrappedLine(float w, vector size, textLengthUpToWidth_widthFunction_t tw);
182 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw);
183
184 float isGametypeInFilter(float gt, float tp, float ts, string pattern);
185
186 typedef void(float i1, float i2, entity pass) swapfunc_t; // is only ever called for i1 < i2
187 typedef float(float i1, float i2, entity pass) comparefunc_t; // <0 for <, ==0 for ==, >0 for > (like strcmp)
188 void shuffle(float n, swapfunc_t swap, entity pass);
189 void heapsort(float n, swapfunc_t swap, comparefunc_t cmp, entity pass);
190
191 string swapwords(string str, float i, float j);
192 string shufflewords(string str);
193
194 string substring_range(string s, float b, float e);
195
196 vector solve_quadratic(float a, float b, float c);
197 // solution 1 -> x
198 // solution 2 -> y
199 // z = 1 if a real solution exists, 0 if not
200 // if no real solution exists, x contains the real part and y the imaginary part of the complex solutions x+iy and x-iy
201
202 vector solve_shotdirection(vector myorg, vector myvel, vector eorg, vector evel, float spd, float newton_style);
203 vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_style, float mi, float ma);
204
205 void check_unacceptable_compiler_bugs();
206
207 float compressShotOrigin(vector v);
208 vector decompressShotOrigin(float f);
209
210 #ifdef SVQC
211 string rankings_reply, ladder_reply, lsmaps_reply, lsnewmaps_reply, maplist_reply; // cached replies
212 string records_reply[10];
213 #endif
214
215 float RandomSelection_totalweight;
216 float RandomSelection_best_priority;
217 entity RandomSelection_chosen_ent;
218 float RandomSelection_chosen_float;
219 string RandomSelection_chosen_string;
220 void RandomSelection_Init();
221 void RandomSelection_Add(entity e, float f, string s, float weight, float priority);
222
223 vector healtharmor_maxdamage(float h, float a, float armorblock); // returns vector: maxdamage, armorideal, 1 if fully armored
224 vector healtharmor_applydamage(float a, float armorblock, float damage); // returns vector: take, save, 0
225
226 string getcurrentmod();
227
228 #ifndef MENUQC
229 #ifdef CSQC
230 float ReadInt24_t();
231 #else
232 void WriteInt24_t(float dest, float val);
233 #endif
234 #endif
235
236 // the NULL function
237 const var void func_null(void); FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(func_null)
238 const var string string_null;
239 float float2range11(float f);
240 float float2range01(float f);
241
242 float gsl_ran_gaussian(float sigma);
243
244 string car(string s); // returns first word
245 string cdr(string s); // returns all but first word
246 float matchacl(string acl, string str); // matches str against ACL acl (with entries +foo*, +foo, +*foo, +*foo*, and same with - for forbidding)
247 float startsWith(string haystack, string needle);
248 float startsWithNocase(string haystack, string needle);
249
250 string get_model_datafilename(string mod, float skn, string fil); // skin -1 will return wildcard, mod string_null will also put wildcard there
251 string get_model_parameters_modelname;
252 float get_model_parameters_modelskin;
253 string get_model_parameters_name;
254 float get_model_parameters_species;
255 string get_model_parameters_sex;
256 float get_model_parameters_weight;
257 float get_model_parameters_age;
258 string get_model_parameters_desc;
259 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
260
261 // stupid stupid stupid FTEQCC has a max limit on macro sizes, let's work around by splitting the macro into two macros! :(
262 #define HUD_Panel_GetName_Part2(id) \
263 switch(id) {\
264         case HUD_PANEL_ENGINEINFO: panel_name = HUD_PANELNAME_ENGINEINFO; break; \
265         case HUD_PANEL_INFOMESSAGES: panel_name = HUD_PANELNAME_INFOMESSAGES; break; \
266         case HUD_PANEL_PHYSICS: panel_name = HUD_PANELNAME_PHYSICS; break; \
267         case HUD_PANEL_CENTERPRINT: panel_name = HUD_PANELNAME_CENTERPRINT; break; \
268 } ENDS_WITH_CURLY_BRACE
269
270 // Get name of specified panel id
271 #define HUD_Panel_GetName(id) \
272 switch(id) { \
273         case HUD_PANEL_WEAPONS: panel_name = HUD_PANELNAME_WEAPONS; break; \
274         case HUD_PANEL_AMMO: panel_name = HUD_PANELNAME_AMMO; break; \
275         case HUD_PANEL_POWERUPS: panel_name = HUD_PANELNAME_POWERUPS; break; \
276         case HUD_PANEL_HEALTHARMOR: panel_name = HUD_PANELNAME_HEALTHARMOR; break; \
277         case HUD_PANEL_NOTIFY: panel_name = HUD_PANELNAME_NOTIFY; break; \
278         case HUD_PANEL_TIMER: panel_name = HUD_PANELNAME_TIMER; break; \
279         case HUD_PANEL_RADAR: panel_name = HUD_PANELNAME_RADAR; break; \
280         case HUD_PANEL_SCORE: panel_name = HUD_PANELNAME_SCORE; break; \
281         case HUD_PANEL_RACETIMER: panel_name = HUD_PANELNAME_RACETIMER; break; \
282         case HUD_PANEL_VOTE: panel_name = HUD_PANELNAME_VOTE; break; \
283         case HUD_PANEL_MODICONS: panel_name = HUD_PANELNAME_MODICONS; break; \
284         case HUD_PANEL_PRESSEDKEYS: panel_name = HUD_PANELNAME_PRESSEDKEYS; break; \
285         case HUD_PANEL_CHAT: panel_name = HUD_PANELNAME_CHAT; break; \
286     default: HUD_Panel_GetName_Part2(id)\
287 }
288
289 vector vec2(vector v);
290
291 #ifndef MENUQC
292 vector NearestPointOnBox(entity box, vector org);
293 #endif
294
295 float vercmp(string v1, string v2);
296
297 float u8_strsize(string s);
298
299 // translation helpers
300 string prvm_language;
301 string language_filename(string s);
302 string CTX(string s);
303 #define ZCTX(s) strzone(CTX(s))
304
305 // x-encoding (encoding as zero length invisible string)
306 // encodes approx. 14 bits into 5 bytes of color code string
307 const float XENCODE_MAX = 21295; // 2*22*22*22-1
308 const float XENCODE_LEN = 5;
309 string xencode(float f);
310 float xdecode(string s);
311
312 #ifndef COMPAT_XON010_CHANNELS
313 #define sound(e,c,s,v,a) sound7(e,c,s,v,a,0,0)
314 #endif
315
316 float lowestbit(float f);
317
318 #ifdef CSQC
319 entity ReadCSQCEntity()
320 #endif
321
322 #ifndef MENUQC
323 string strtolower(string s);
324 #endif
325
326 string MakeConsoleSafe(string input);
327
328 #ifndef MENUQC
329 float InterpretBoolean(string input);
330 #endif
331
332 // generic shutdown handler
333 void Shutdown();
334
335 #ifndef MENUQC
336 .float skeleton_bones;
337 void Skeleton_SetBones(entity e);
338 // loops through the tags of model v using counter tagnum
339 #define FOR_EACH_TAG(v) float tagnum; Skeleton_SetBones(v); for(tagnum = 0; tagnum < v.skeleton_bones; tagnum++, gettaginfo(v, tagnum))
340 #endif
341 #ifdef SVQC
342 void WriteApproxPastTime(float dst, float t);
343 #endif
344 #ifdef CSQC
345 float ReadApproxPastTime();
346 #endif
347
348 // execute-stuff-next-frame subsystem
349 void execute_next_frame();
350 void queue_to_execute_next_frame(string s);
351
352 // for marking written-to values as unused where it's a good idea to do this
353 noref float unused_float;
354
355
356
357 // a function f with:
358 // f(0) = 0
359 // f(1) = 1
360 // f'(0) = startspeedfactor
361 // f'(1) = endspeedfactor
362 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float x);
363
364 // checks whether f'(x) = 0 anywhere from 0 to 1
365 // because if this is the case, the function is not usable for platforms
366 // as it may exceed 0..1 bounds, or go in reverse
367 float cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor);
368
369 typedef entity(entity cur, entity near, entity pass) findNextEntityNearFunction_t;
370 typedef float(entity a, entity b, entity pass) isConnectedFunction_t;
371 void FindConnectedComponent(entity e, .entity fld, findNextEntityNearFunction_t nxt, isConnectedFunction_t iscon, entity pass);
372
373 // expand multiple arguments into one argument
374 #define XPND5(a,b,c,d,e) a, b, c, d, e
375 #define XPND4(a,b,c,d) a, b, c, d
376 #define XPND3(a,b,c) a, b, c
377 #define XPND2(a,b) a, b