]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/util.qh
Rename defs to qh
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qh
1 #ifndef COMMON_UTIL_H
2 #define COMMON_UTIL_H
3
4 #if defined(CSQC)
5         #include "util-pre.qh"
6     #include "../client/sys-pre.qh"
7     #include "../dpdefs/csprogsdefs.qh"
8     #include "../client/sys-post.qh"
9     #include "../client/defs.qh"
10     #include "../dpdefs/keycodes.qh"
11     #include "constants.qh"
12     #include "stats.qh"
13     #include "../warpzonelib/anglestransform.qh"
14     #include "../warpzonelib/mathlib.qh"
15     #include "../warpzonelib/common.qh"
16     #include "../warpzonelib/client.qh"
17     #include "playerstats.qh"
18     #include "teams.qh"
19 #elif defined(MENUQC)
20         #include "util-pre.qh"
21     #include "../menu/sys-pre.qh"
22     #include "../dpdefs/menudefs.qh"
23     #include "../dpdefs/keycodes.qh"
24     #include "../menu/sys-post.qh"
25     #include "../menu/config.qh"
26     #include "../warpzonelib/mathlib.qh"
27 #elif defined(SVQC)
28         #include "util-pre.qh"
29     #include "../server/sys-pre.qh"
30     #include "../dpdefs/progsdefs.qh"
31     #include "../dpdefs/dpextensions.qh"
32     #include "../server/sys-post.qh"
33     #include "../warpzonelib/anglestransform.qh"
34     #include "../warpzonelib/mathlib.qh"
35     #include "../warpzonelib/common.qh"
36     #include "../warpzonelib/util_server.qh"
37     #include "../warpzonelib/server.qh"
38     #include "constants.qh"
39     #include "stats.qh"
40     #include "teams.qh"
41 #endif
42
43 #ifdef QCC_SUPPORT_ACCUMULATE
44 # define ACCUMULATE_FUNCTION(func,otherfunc) \
45         [[accumulate]] void func() { otherfunc(); }
46 # define CALL_ACCUMULATED_FUNCTION(func) \
47         func()
48 #else
49 #ifdef HAVE_YO_DAWG_CPP
50 // TODO make ascii art pic of xzibit
51 // YO DAWG!
52 // I HERD YO LIEK MACROS
53 // SO I PUT A MACRO DEFINITION IN YO MACRO DEFINITION
54 // SO YO CAN EXPAND MACROS WHILE YO EXPAND MACROS
55 # define ACCUMULATE_FUNCTION(func,otherfunc) \
56         #ifdef func \
57         void __merge__##otherfunc() { func(); otherfunc(); } \
58         #undef func \
59         #define func __merge__##otherfunc \
60         #else \
61         #define func otherfunc \
62         #endif
63 # define CALL_ACCUMULATED_FUNCTION(func) \
64         func()
65 #else
66 # define ACCUMULATE_FUNCTION(func,otherfunc) \
67         .float _ACCUMULATE_##func##__##otherfunc
68 void ACCUMULATE_call(string func)
69 {
70         float i;
71         float n = numentityfields();
72         string funcprefix = strcat("_ACCUMULATE_", func, "__");
73         float funcprefixlen = strlen(funcprefix);
74         for(i = 0; i < n; ++i)
75         {
76                 string name = entityfieldname(i);
77                 if(substring(name, 0, funcprefixlen) == funcprefix)
78                         callfunction(substring(name, funcprefixlen, -1));
79         }
80 }
81 # define CALL_ACCUMULATED_FUNCTION(func) \
82         ACCUMULATE_call(#func)
83 #endif
84 #endif
85
86 // used for simplifying ACCUMULATE_FUNCTIONs
87 #define SET_FIRST_OR_LAST(input,first,count) if(!input) { input = (first + count); }
88 #define SET_FIELD_COUNT(field,first,count) if(!field) { field = (first + count); ++count; }
89 #define CHECK_MAX_COUNT(name,max,count,type) if(count > max) { error(strcat("Maximum ", type, " hit: ", #name, ": ", ftos(count), ".\n")); }
90
91 // 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
92 // NOTE: s IS allowed to be a tempstring
93 string wordwrap(string s, float l);
94 #ifndef MENUQC
95 #ifndef CSQC
96 void wordwrap_sprint(string s, float l);
97 #endif
98 #endif
99 void wordwrap_cb(string s, float l, void(string) callback);
100
101 #ifndef SVQC
102 string draw_currentSkin;
103 string draw_UseSkinFor(string pic);
104 #endif
105
106 // iterative depth-first search, with fields that go "up", "down left" and "right" in a tree
107 // for each element, funcPre is called first, then funcPre and funcPost for all its children, and funcPost last
108 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass);
109
110 float median(float a, float b, float c);
111
112 // converts a number to a string with the indicated number of decimals
113 // works for up to 10 decimals!
114 string ftos_decimals(float number, float decimals);
115
116 bool fexists(string f);
117
118 vector colormapPaletteColor(float c, float isPants);
119
120 // unzone the string, and return it as tempstring. Safe to be called on string_null
121 string fstrunzone(string s);
122
123 // database (NOTE: keys are case sensitive)
124 void db_save(int db, string filename);
125 void db_dump(int db, string pFilename);
126 int db_create();
127 int db_load(string filename);
128 void db_close(int db);
129 string db_get(int db, string key);
130 void db_put(int db, string key, string value);
131
132 // stringbuffer loading/saving
133 int buf_load(string filename);
134 void buf_save(int buf, string filename);
135
136 #define TIME_TO_NTHS(t,n) floor((t) * (n) + 0.4)
137 string format_time(float seconds);
138 string mmsss(float t);
139 string mmssss(float t);
140
141 const float TIME_DECIMALS = 2;
142 const float TIME_FACTOR = 100;
143 #define TIME_ENCODED_TOSTRING(n) mmssss(n)
144 #define RACE_RECORD "/race100record/"
145 #define CTS_RECORD "/cts100record/"
146 #define TIME_ENCODE(t) TIME_TO_NTHS(t, TIME_FACTOR)
147 #define TIME_DECODE(n) ((n) / TIME_FACTOR)
148
149 string ScoreString(float vflags, float value);
150
151 float dotproduct(vector a, vector b);
152 vector cross(vector a, vector b);
153
154 void compressShortVector_init();
155 vector decompressShortVector(float data);
156 float compressShortVector(vector vec);
157
158 #ifndef MENUQC
159 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz);
160 #endif
161
162 string fixPriorityList(string pl, float from, float to, float subtract, float complete);
163 string mapPriorityList(string order, string(string) mapfunc);
164 string swapInPriorityList(string order, float i, float j);
165
166 float cvar_value_issafe(string s);
167
168 float cvar_settemp(string pKey, string pValue);
169 float cvar_settemp_restore();
170
171 #ifndef MENUQC
172 // modes: 0 = trust q3map2 (_mini images)
173 //        1 = trust tracebox (_radar images)
174 // in both modes, mapinfo's "size" overrides
175
176 string mi_shortname;
177 vector mi_min;
178 vector mi_max;
179 void get_mi_min_max(float mode);
180
181 vector mi_picmin; // adjusted mins that map to the picture (square)
182 vector mi_picmax; // adjusted maxs that map to the picture (square)
183 vector mi_pictexcoord0; // texcoords of the image corners (after transforming, these are 2D coords too)
184 vector mi_pictexcoord1; // texcoords of the image corners (after transforming, these are 2D coords too)
185 vector mi_pictexcoord2; // texcoords of the image corners (after transforming, these are 2D coords too)
186 vector mi_pictexcoord3; // texcoords of the image corners (after transforming, these are 2D coords too)
187 void get_mi_min_max_texcoords(float mode);
188 #endif
189
190 float almost_equals(float a, float b);
191 float almost_in_bounds(float a, float b, float c);
192
193 float power2of(float e);
194 float log2of(float x);
195
196 const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
197 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
198 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
199 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))
200
201 vector rgb_to_hsl(vector rgb);
202 vector hsl_to_rgb(vector hsl);
203 vector rgb_to_hsv(vector rgb);
204 vector hsv_to_rgb(vector hsv);
205 string rgb_to_hexcolor(vector rgb);
206
207 float boxesoverlap(vector m1, vector m2, vector m3, vector m4);
208 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs);
209
210 typedef float(string s, vector size) textLengthUpToWidth_widthFunction_t;
211 typedef float(string s) textLengthUpToLength_lenFunction_t;
212 float textLengthUpToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
213 string textShortenToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
214 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
215 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
216
217 string getWrappedLine_remaining;
218 string getWrappedLine(float w, vector size, textLengthUpToWidth_widthFunction_t tw);
219 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw);
220
221 float isGametypeInFilter(float gt, float tp, float ts, string pattern);
222
223 typedef void(float i1, float i2, entity pass) swapfunc_t; // is only ever called for i1 < i2
224 typedef float(float i1, float i2, entity pass) comparefunc_t; // <0 for <, ==0 for ==, >0 for > (like strcmp)
225 void shuffle(float n, swapfunc_t swap, entity pass);
226 void heapsort(float n, swapfunc_t swap, comparefunc_t cmp, entity pass);
227
228 string swapwords(string str, float i, float j);
229 string shufflewords(string str);
230
231 string substring_range(string s, float b, float e);
232
233 vector solve_quadratic(float a, float b, float c);
234 // solution 1 -> x
235 // solution 2 -> y
236 // z = 1 if a real solution exists, 0 if not
237 // if no real solution exists, x contains the real part and y the imaginary part of the complex solutions x+iy and x-iy
238
239 vector solve_shotdirection(vector myorg, vector myvel, vector eorg, vector evel, float spd, float newton_style);
240 vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_style, float mi, float ma);
241
242 float compressShotOrigin(vector v);
243 vector decompressShotOrigin(float f);
244
245 #ifdef SVQC
246 string rankings_reply, ladder_reply, lsmaps_reply, maplist_reply, monsterlist_reply; // cached replies
247 string records_reply[10];
248 #endif
249
250 float RandomSelection_totalweight;
251 float RandomSelection_best_priority;
252 entity RandomSelection_chosen_ent;
253 float RandomSelection_chosen_float;
254 string RandomSelection_chosen_string;
255 void RandomSelection_Init();
256 void RandomSelection_Add(entity e, float f, string s, float weight, float priority);
257
258 #ifndef MENUQC
259 vector healtharmor_maxdamage(float h, float a, float armorblock, float deathtype); // returns vector: maxdamage, armorideal, 1 if fully armored
260 vector healtharmor_applydamage(float a, float armorblock, float deathtype, float damage); // returns vector: take, save, 0
261 #endif
262
263 string getcurrentmod();
264
265 #ifndef MENUQC
266 #ifdef CSQC
267 int ReadInt24_t();
268 vector ReadInt48_t();
269 vector ReadInt72_t();
270 #else
271 void WriteInt24_t(float dest, float val);
272 void WriteInt48_t(float dest, vector val);
273 void WriteInt72_t(float dest, vector val);
274 #endif
275 #endif
276
277 // the NULL function
278 #ifdef QCC_SUPPORT_NIL
279 #define func_null nil
280 #define string_null nil
281 #else
282 var void func_null(void);
283 string string_null;
284 #endif
285 float float2range11(float f);
286 float float2range01(float f);
287
288 float gsl_ran_gaussian(float sigma);
289
290 string car(string s); // returns first word
291 string cdr(string s); // returns all but first word
292 float matchacl(string acl, string str); // matches str against ACL acl (with entries +foo*, +foo, +*foo, +*foo*, and same with - for forbidding)
293 float startsWith(string haystack, string needle);
294 float startsWithNocase(string haystack, string needle);
295
296 string get_model_datafilename(string mod, float skn, string fil); // skin -1 will return wildcard, mod string_null will also put wildcard there
297 string get_model_parameters_modelname;
298 float get_model_parameters_modelskin;
299 string get_model_parameters_name;
300 float get_model_parameters_species;
301 string get_model_parameters_sex;
302 float get_model_parameters_weight;
303 float get_model_parameters_age;
304 string get_model_parameters_description;
305 string get_model_parameters_bone_upperbody;
306 string get_model_parameters_bone_weapon;
307 const int MAX_AIM_BONES = 4;
308 string get_model_parameters_bone_aim[MAX_AIM_BONES];
309 float get_model_parameters_bone_aimweight[MAX_AIM_BONES];
310 float get_model_parameters_fixbone;
311 string get_model_parameters_desc;
312 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
313
314 vector vec2(vector v);
315
316 #ifndef MENUQC
317 vector NearestPointOnBox(entity box, vector org);
318 #endif
319
320 float vercmp(string v1, string v2);
321
322 float u8_strsize(string s);
323
324 // translation helpers
325 string prvm_language;
326 string language_filename(string s);
327 string CTX(string s);
328 #define ZCTX(s) strzone(CTX(s))
329
330 // x-encoding (encoding as zero length invisible string)
331 // encodes approx. 14 bits into 5 bytes of color code string
332 const float XENCODE_MAX = 21295; // 2*22*22*22-1
333 const float XENCODE_LEN = 5;
334 string xencode(float f);
335 float xdecode(string s);
336
337 // Play all sounds via sound7, for access to the extra channels.
338 // Otherwise, channels 8 to 15 would be blocked for a weird QW feature.
339 #define sound(e,c,s,v,a) sound7(e,c,s,v,a,0,0)
340
341 float lowestbit(float f);
342
343 #ifdef CSQC
344 entity ReadCSQCEntity();
345 #endif
346
347 #ifndef MENUQC
348 string strtolower(string s);
349 #endif
350
351 string MakeConsoleSafe(string input);
352
353 #ifndef MENUQC
354 float InterpretBoolean(string input);
355 #endif
356
357 // generic shutdown handler
358 void Shutdown();
359
360 #ifndef MENUQC
361 .float skeleton_bones;
362 void Skeleton_SetBones(entity e);
363 // loops through the tags of model v using counter tagnum
364 #define FOR_EACH_TAG(v) float tagnum; Skeleton_SetBones(v); for(tagnum = 0; tagnum < v.skeleton_bones; tagnum++, gettaginfo(v, tagnum))
365 #endif
366 #ifdef SVQC
367 void WriteApproxPastTime(float dst, float t);
368 #endif
369 #ifdef CSQC
370 float ReadApproxPastTime();
371 #endif
372
373 // execute-stuff-next-frame subsystem
374 void execute_next_frame();
375 void queue_to_execute_next_frame(string s);
376
377 // for marking written-to values as unused where it's a good idea to do this
378 noref float unused_float;
379
380 // a function f with:
381 // f(0) = 0
382 // f(1) = 1
383 // f'(0) = startspeedfactor
384 // f'(1) = endspeedfactor
385 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float x);
386
387 // checks whether f'(x) = 0 anywhere from 0 to 1
388 // because if this is the case, the function is not usable for platforms
389 // as it may exceed 0..1 bounds, or go in reverse
390 float cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor);
391
392 typedef entity(entity cur, entity near, entity pass) findNextEntityNearFunction_t;
393 typedef float(entity a, entity b, entity pass) isConnectedFunction_t;
394 void FindConnectedComponent(entity e, .entity fld, findNextEntityNearFunction_t nxt, isConnectedFunction_t iscon, entity pass);
395
396 #ifdef SVQC
397 vector combine_to_vector(float x, float y, float z);
398 vector get_corner_position(entity box, float corner);
399 #endif
400
401 // expand multiple arguments into one argument by stripping parenthesis
402 #define XPD(...) __VA_ARGS__
403
404 // Some common varargs functions. Lowercase as they match C.
405 #define printf(...) print(sprintf(__VA_ARGS__))
406 #define dprintf(...) dprint(sprintf(__VA_ARGS__))
407 #define fprintf(file, ...) fputs(file, sprintf(__VA_ARGS__))
408 #define bprintf(...) bprint(sprintf(__VA_ARGS__))
409
410 //#ifndef MENUQC
411 void backtrace(string msg);
412 //#endif
413
414 // color code replace, place inside of sprintf and parse the string... defaults described as constants
415 // foreground/normal colors
416 string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green  // primary priority (important names, etc)
417 string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
418 string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue   // tertiary priority or relatively inconsequential text
419 string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red    // notice/attention grabbing texting
420 // "kill" colors
421 string autocvar_hud_colorset_kill_1 = "1"; // K1 - Red    // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
422 string autocvar_hud_colorset_kill_2 = "3"; // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
423 string autocvar_hud_colorset_kill_3 = "4"; // K3 - Blue   // "good" or "beneficial" text (you fragging someone, etc)
424 // background color
425 string autocvar_hud_colorset_background = "7"; // BG - White // neutral/unimportant text
426
427 string CCR(string input);
428
429 #ifndef MENUQC
430 #ifdef CSQC
431 #define GENTLE (autocvar_cl_gentle || autocvar_cl_gentle_messages)
432 #else
433 #define GENTLE autocvar_sv_gentle
434 #endif
435 #define normal_or_gentle(normal,gentle) (GENTLE ? ((gentle != "") ? gentle : normal) : normal)
436 #endif
437
438 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
439 #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
440 #define WRITESPECTATABLE_MSG_ONE(statement) WRITESPECTATABLE_MSG_ONE_VARNAME(oldmsg_entity, statement)
441 #define WRITESPECTATABLE(msg,statement) if(msg == MSG_ONE) { WRITESPECTATABLE_MSG_ONE(statement); } else statement float WRITESPECTATABLE_workaround = 0
442
443 vector vec3(float x, float y, float z);
444
445 #ifndef MENUQC
446 vector animfixfps(entity e, vector a, vector b);
447 #endif
448
449 #ifdef SVQC
450 void dedicated_print(string input);
451 #endif
452
453 // todo: better way to do this?
454 #ifdef MENUQC
455 #define PROGNAME "MENUQC"
456 #else
457 #ifdef SVQC
458 #define PROGNAME "SVQC"
459 #else
460 #define PROGNAME "CSQC"
461 #endif
462 #endif
463
464 #ifndef MENUQC
465 const float CNT_NORMAL = 1;
466 const float CNT_GAMESTART = 2;
467 const float CNT_IDLE = 3;
468 const float CNT_KILL = 4;
469 const float CNT_RESPAWN = 5;
470 const float CNT_ROUNDSTART = 6;
471 float Announcer_PickNumber(float type, float num);
472 #endif
473
474 #ifndef MENUQC
475 int Mod_Q1BSP_SuperContentsFromNativeContents(int nativecontents);
476 int Mod_Q1BSP_NativeContentsFromSuperContents(int supercontents);
477 #endif
478
479 // Quadratic splines (bezier)
480 vector bezier_quadratic_getpoint(vector a, vector p, vector b, float t);
481 vector bezier_quadratic_getderivative(vector a, vector p, vector b, float t);
482
483 #define APPEND_TO_STRING(list,sep,add) ((list) = (((list) != "") ? strcat(list, sep, add) : (add)))
484
485 // Returns the correct difference between two always increasing numbers
486 #define COMPARE_INCREASING(to,from) (to < from ? from + to + 2 : to - from)
487 #endif