]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/util.qh
Move time processing and counting to counting.qh
[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 #ifdef HAVE_YO_DAWG_CPP
5 // TODO make ascii art pic of xzibit
6 // YO DAWG!
7 // I HERD YO LIEK MACROS
8 // SO I PUT A MACRO DEFINITION IN YO MACRO DEFINITION
9 // SO YO CAN EXPAND MACROS WHILE YO EXPAND MACROS
10 # define ACCUMULATE_FUNCTION(func,otherfunc) \
11         #ifdef func \
12         void __merge__##otherfunc() { func(); otherfunc(); } \
13         #undef func \
14         #define func __merge__##otherfunc \
15         #else \
16         #define func otherfunc \
17         #endif
18 # define CALL_ACCUMULATED_FUNCTION(func) \
19         func()
20 #else
21 # define ACCUMULATE_FUNCTION(func,otherfunc) \
22         .void _ACCUMULATE_##func##__##otherfunc;
23 void ACCUMULATE_call(string func)
24 {
25         float i;
26         float n = numentityfields();
27         string funcprefix = strcat("_ACCUMULATE_", func, "__");
28         float funcprefixlen = strlen(funcprefix);
29         for(i = 0; i < n; ++i)
30         {
31                 string name = entityfieldname(i);
32                 if(substring(name, 0, funcprefixlen) == funcprefix)
33                         callfunction(substring(name, funcprefixlen, -1));
34         }
35 }
36 # define CALL_ACCUMULATED_FUNCTION(func) \
37         ACCUMULATE_call(#func)
38 #endif
39
40 // used for simplifying ACCUMULATE_FUNCTIONs
41 #define SET_FIRST_OR_LAST(input,first,count) if(!input) { input = (first + count); }
42 #define SET_FIELD_COUNT(field,first,count) if(!field) { field = (first + count); ++count; }
43 #define CHECK_MAX_COUNT(name,max,count,type) if(count == max) { error(strcat("Maximum ", type, " hit: ", #name, ": ", ftos(count), ".\n")); }
44
45 // 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
46 // NOTE: s IS allowed to be a tempstring
47 string wordwrap(string s, float l);
48 #ifndef MENUQC
49 #ifndef CSQC
50 void wordwrap_sprint(string s, float l);
51 #endif
52 #endif
53 void wordwrap_cb(string s, float l, void(string) callback);
54
55 #ifndef SVQC
56 string draw_currentSkin;
57 string draw_UseSkinFor(string pic);
58 #endif
59
60 // iterative depth-first search, with fields that go "up", "down left" and "right" in a tree
61 // for each element, funcPre is called first, then funcPre and funcPost for all its children, and funcPost last
62 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass);
63
64 float median(float a, float b, float c);
65
66 // converts a number to a string with the indicated number of decimals
67 // works for up to 10 decimals!
68 string ftos_decimals(float number, float decimals);
69
70 float fexists(string f);
71
72 vector colormapPaletteColor(float c, float isPants);
73
74 // unzone the string, and return it as tempstring. Safe to be called on string_null
75 string fstrunzone(string s);
76
77 // database (NOTE: keys are case sensitive)
78 void db_save(float db, string filename);
79 void db_dump(float db, string pFilename);
80 float db_create();
81 float db_load(string filename);
82 void db_close(float db);
83 string db_get(float db, string key);
84 void db_put(float db, string key, string value);
85
86 // stringbuffer loading/saving
87 float buf_load(string filename);
88 void buf_save(float buf, string filename);
89
90 // modulo function
91 #ifndef MENUQC
92 float mod(float a, float b) { return a - (floor(a / b) * b); }   
93 #endif
94
95 #define TIME_TO_NTHS(t,n) floor((t) * (n) + 0.4)
96 string mmsss(float t);
97 string mmssss(float t);
98
99 #define TIME_DECIMALS 2
100 #define TIME_FACTOR 100
101 #define TIME_ENCODED_TOSTRING(n) mmssss(n)
102 #define RACE_RECORD "/race100record/"
103 #define CTS_RECORD "/cts100record/"
104 #define TIME_ENCODE(t) TIME_TO_NTHS(t, TIME_FACTOR)
105 #define TIME_DECODE(n) ((n) / TIME_FACTOR)
106
107 string ScoreString(float vflags, float value);
108
109 float dotproduct(vector a, vector b);
110 vector cross(vector a, vector b);
111
112 void compressShortVector_init();
113 vector decompressShortVector(float data);
114 float compressShortVector(vector vec);
115
116 #ifndef MENUQC
117 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz);
118 #endif
119
120 string fixPriorityList(string pl, float from, float to, float subtract, float complete);
121 string mapPriorityList(string order, string(string) mapfunc);
122 string swapInPriorityList(string order, float i, float j);
123
124 float cvar_value_issafe(string s);
125
126 float cvar_settemp(string pKey, string pValue);
127 float cvar_settemp_restore();
128
129 #ifndef MENUQC
130 // modes: 0 = trust q3map2 (_mini images)
131 //        1 = trust tracebox (_radar images)
132 // in both modes, mapinfo's "size" overrides
133
134 string mi_shortname;
135 vector mi_min;
136 vector mi_max;
137 void get_mi_min_max(float mode);
138
139 vector mi_picmin; // adjusted mins that map to the picture (square)
140 vector mi_picmax; // adjusted maxs that map to the picture (square)
141 vector mi_pictexcoord0; // texcoords of the image corners (after transforming, these are 2D coords too)
142 vector mi_pictexcoord1; // texcoords of the image corners (after transforming, these are 2D coords too)
143 vector mi_pictexcoord2; // texcoords of the image corners (after transforming, these are 2D coords too)
144 vector mi_pictexcoord3; // texcoords of the image corners (after transforming, these are 2D coords too)
145 void get_mi_min_max_texcoords(float mode);
146 #endif
147
148 float almost_equals(float a, float b);
149 float almost_in_bounds(float a, float b, float c);
150
151 float power2of(float e);
152 float log2of(float x);
153
154 string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
155 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
156 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
157 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))
158
159 vector rgb_to_hsl(vector rgb);
160 vector hsl_to_rgb(vector hsl);
161 vector rgb_to_hsv(vector rgb);
162 vector hsv_to_rgb(vector hsv);
163 string rgb_to_hexcolor(vector rgb);
164
165 float boxesoverlap(vector m1, vector m2, vector m3, vector m4);
166 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs);
167
168 typedef float(string s, vector size) textLengthUpToWidth_widthFunction_t;
169 typedef float(string s) textLengthUpToLength_lenFunction_t;
170 float textLengthUpToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
171 string textShortenToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
172 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
173 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
174
175 string getWrappedLine_remaining;
176 string getWrappedLine(float w, vector size, textLengthUpToWidth_widthFunction_t tw);
177 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw);
178
179 float isGametypeInFilter(float gt, float tp, float ts, string pattern);
180
181 typedef void(float i1, float i2, entity pass) swapfunc_t; // is only ever called for i1 < i2
182 typedef float(float i1, float i2, entity pass) comparefunc_t; // <0 for <, ==0 for ==, >0 for > (like strcmp)
183 void shuffle(float n, swapfunc_t swap, entity pass);
184 void heapsort(float n, swapfunc_t swap, comparefunc_t cmp, entity pass);
185
186 string swapwords(string str, float i, float j);
187 string shufflewords(string str);
188
189 string substring_range(string s, float b, float e);
190
191 vector solve_quadratic(float a, float b, float c);
192 // solution 1 -> x
193 // solution 2 -> y
194 // z = 1 if a real solution exists, 0 if not
195 // if no real solution exists, x contains the real part and y the imaginary part of the complex solutions x+iy and x-iy
196
197 vector solve_shotdirection(vector myorg, vector myvel, vector eorg, vector evel, float spd, float newton_style);
198 vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_style, float mi, float ma);
199
200 void check_unacceptable_compiler_bugs();
201
202 float compressShotOrigin(vector v);
203 vector decompressShotOrigin(float f);
204
205 #ifdef SVQC
206 string rankings_reply, ladder_reply, lsmaps_reply, lsnewmaps_reply, maplist_reply; // cached replies
207 string records_reply[10];
208 #endif
209
210 float RandomSelection_totalweight;
211 float RandomSelection_best_priority;
212 entity RandomSelection_chosen_ent;
213 float RandomSelection_chosen_float;
214 string RandomSelection_chosen_string;
215 void RandomSelection_Init();
216 void RandomSelection_Add(entity e, float f, string s, float weight, float priority);
217
218 vector healtharmor_maxdamage(float h, float a, float armorblock); // returns vector: maxdamage, armorideal, 1 if fully armored
219 vector healtharmor_applydamage(float a, float armorblock, float damage); // returns vector: take, save, 0
220
221 string getcurrentmod();
222
223 #ifndef MENUQC
224 #ifdef CSQC
225 float ReadInt24_t();
226 #else
227 void WriteInt24_t(float dest, float val);
228 #endif
229 #endif
230
231 // the NULL function
232 #ifdef GMQCC
233 #define func_null nil
234 #define string_null nil
235 #else
236 var void func_null(void);
237 var string string_null;
238 #endif
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_bone_upperbody;
259 string get_model_parameters_bone_weapon;
260 #define MAX_AIM_BONES 4
261 string get_model_parameters_bone_aim[MAX_AIM_BONES];
262 float get_model_parameters_bone_aimweight[MAX_AIM_BONES];
263 float get_model_parameters_fixbone;
264 string get_model_parameters_desc;
265 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
266
267 // stupid stupid stupid FTEQCC has a max limit on macro sizes, let's work around by splitting the macro into two macros! :(
268 #define HUD_Panel_GetName_Part2(id) \
269 switch(id) {\
270         case HUD_PANEL_ENGINEINFO: panel_name = HUD_PANELNAME_ENGINEINFO; break; \
271         case HUD_PANEL_INFOMESSAGES: panel_name = HUD_PANELNAME_INFOMESSAGES; break; \
272         case HUD_PANEL_PHYSICS: panel_name = HUD_PANELNAME_PHYSICS; break; \
273         case HUD_PANEL_CENTERPRINT: panel_name = HUD_PANELNAME_CENTERPRINT; break; \
274 } ENDS_WITH_CURLY_BRACE
275
276 // Get name of specified panel id
277 #define HUD_Panel_GetName(id) \
278 switch(id) { \
279         case HUD_PANEL_WEAPONS: panel_name = HUD_PANELNAME_WEAPONS; break; \
280         case HUD_PANEL_AMMO: panel_name = HUD_PANELNAME_AMMO; break; \
281         case HUD_PANEL_POWERUPS: panel_name = HUD_PANELNAME_POWERUPS; break; \
282         case HUD_PANEL_HEALTHARMOR: panel_name = HUD_PANELNAME_HEALTHARMOR; break; \
283         case HUD_PANEL_NOTIFY: panel_name = HUD_PANELNAME_NOTIFY; break; \
284         case HUD_PANEL_TIMER: panel_name = HUD_PANELNAME_TIMER; break; \
285         case HUD_PANEL_RADAR: panel_name = HUD_PANELNAME_RADAR; break; \
286         case HUD_PANEL_SCORE: panel_name = HUD_PANELNAME_SCORE; break; \
287         case HUD_PANEL_RACETIMER: panel_name = HUD_PANELNAME_RACETIMER; break; \
288         case HUD_PANEL_VOTE: panel_name = HUD_PANELNAME_VOTE; break; \
289         case HUD_PANEL_MODICONS: panel_name = HUD_PANELNAME_MODICONS; break; \
290         case HUD_PANEL_PRESSEDKEYS: panel_name = HUD_PANELNAME_PRESSEDKEYS; break; \
291         case HUD_PANEL_CHAT: panel_name = HUD_PANELNAME_CHAT; break; \
292     default: HUD_Panel_GetName_Part2(id)\
293 }
294
295 vector vec2(vector v);
296
297 #ifndef MENUQC
298 vector NearestPointOnBox(entity box, vector org);
299 #endif
300
301 float vercmp(string v1, string v2);
302
303 float u8_strsize(string s);
304
305 // translation helpers
306 string prvm_language;
307 string language_filename(string s);
308 string CTX(string s);
309 #define ZCTX(s) strzone(CTX(s))
310
311 // x-encoding (encoding as zero length invisible string)
312 // encodes approx. 14 bits into 5 bytes of color code string
313 const float XENCODE_MAX = 21295; // 2*22*22*22-1
314 const float XENCODE_LEN = 5;
315 string xencode(float f);
316 float xdecode(string s);
317
318 #ifndef COMPAT_XON010_CHANNELS
319 #define sound(e,c,s,v,a) sound7(e,c,s,v,a,0,0)
320 #endif
321
322 float lowestbit(float f);
323
324 #ifdef CSQC
325 entity ReadCSQCEntity();
326 #endif
327
328 #ifndef MENUQC
329 string strtolower(string s);
330 #endif
331
332 string MakeConsoleSafe(string input);
333
334 #ifndef MENUQC
335 float InterpretBoolean(string input);
336 #endif
337
338 // generic shutdown handler
339 void Shutdown();
340
341 #ifndef MENUQC
342 .float skeleton_bones;
343 void Skeleton_SetBones(entity e);
344 // loops through the tags of model v using counter tagnum
345 #define FOR_EACH_TAG(v) float tagnum; Skeleton_SetBones(v); for(tagnum = 0; tagnum < v.skeleton_bones; tagnum++, gettaginfo(v, tagnum))
346 #endif
347 #ifdef SVQC
348 void WriteApproxPastTime(float dst, float t);
349 #endif
350 #ifdef CSQC
351 float ReadApproxPastTime();
352 #endif
353
354 // execute-stuff-next-frame subsystem
355 void execute_next_frame();
356 void queue_to_execute_next_frame(string s);
357
358 // for marking written-to values as unused where it's a good idea to do this
359 noref float unused_float;
360
361 // a function f with:
362 // f(0) = 0
363 // f(1) = 1
364 // f'(0) = startspeedfactor
365 // f'(1) = endspeedfactor
366 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float x);
367
368 // checks whether f'(x) = 0 anywhere from 0 to 1
369 // because if this is the case, the function is not usable for platforms
370 // as it may exceed 0..1 bounds, or go in reverse
371 float cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor);
372
373 typedef entity(entity cur, entity near, entity pass) findNextEntityNearFunction_t;
374 typedef float(entity a, entity b, entity pass) isConnectedFunction_t;
375 void FindConnectedComponent(entity e, .entity fld, findNextEntityNearFunction_t nxt, isConnectedFunction_t iscon, entity pass);
376
377 // expand multiple arguments into one argument by stripping parenthesis
378 #define XPD(...) __VA_ARGS__
379
380 #ifndef MENUQC
381 void backtrace(string msg);
382 #endif
383
384 // color code replace, place inside of sprintf and parse the string... defaults described as constants
385 // foreground/normal colors
386 var string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green  // primary priority (important names, etc)
387 var string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
388 var string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue   // tertiary priority or relatively inconsequential text
389 var string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red    // notice/attention grabbing texting
390 // "kill" colors
391 var string autocvar_hud_colorset_kill_1 = "1"; // K1 - Red    // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
392 var string autocvar_hud_colorset_kill_2 = "3"; // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
393 var string autocvar_hud_colorset_kill_3 = "4"; // K3 - Blue   // "good" or "beneficial" text (you fragging someone, etc)
394 // background color
395 var string autocvar_hud_colorset_background = "7"; // BG - White // neutral/unimportant text
396
397 string CCR(string input);
398
399 #ifndef MENUQC
400 #ifdef CSQC
401 #define GENTLE (autocvar_cl_gentle || autocvar_cl_gentle_messages)
402 #else
403 #define GENTLE autocvar_sv_gentle
404 #endif
405 #define normal_or_gentle(normal,gentle) (GENTLE ? ((gentle != "") ? gentle : normal) : normal)
406 #endif
407
408 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
409 #define WRITESPECTATABLE_MSG_ONE_VARNAME(varname,statement) entity varname; varname = msg_entity; FOR_EACH_REALCLIENT(msg_entity) if(msg_entity == varname || (msg_entity.classname == STR_SPECTATOR && msg_entity.enemy == varname)) statement msg_entity = varname
410 #define WRITESPECTATABLE_MSG_ONE(statement) WRITESPECTATABLE_MSG_ONE_VARNAME(oldmsg_entity, statement)
411 #define WRITESPECTATABLE(msg,statement) if(msg == MSG_ONE) { WRITESPECTATABLE_MSG_ONE(statement); } else statement float WRITESPECTATABLE_workaround = 0
412
413 vector vec3(float x, float y, float z);
414
415 #ifndef MENUQC
416 vector animfixfps(entity e, vector a, vector b);
417 #endif
418
419 #ifdef SVQC
420 void dedicated_print(string input);
421 #endif
422
423 // todo: better way to do this?
424 #ifdef MENUQC
425 #define PROGNAME "MENUQC"
426 #else
427 #ifdef SVQC
428 #define PROGNAME "SVQC"
429 #else
430 #define PROGNAME "CSQC"
431 #endif
432 #endif