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