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