]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/util.qh
c6627c2bdc3cac8a20929b2bee331bf882fb88e6
[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 format_time(float seconds);
97 string mmsss(float t);
98 string mmssss(float t);
99
100 #define TIME_DECIMALS 2
101 #define TIME_FACTOR 100
102 #define TIME_ENCODED_TOSTRING(n) mmssss(n)
103 #define RACE_RECORD "/race100record/"
104 #define CTS_RECORD "/cts100record/"
105 #define TIME_ENCODE(t) TIME_TO_NTHS(t, TIME_FACTOR)
106 #define TIME_DECODE(n) ((n) / TIME_FACTOR)
107
108 string ScoreString(float vflags, float value);
109
110 float dotproduct(vector a, vector b);
111 vector cross(vector a, vector b);
112
113 void compressShortVector_init();
114 vector decompressShortVector(float data);
115 float compressShortVector(vector vec);
116
117 #ifndef MENUQC
118 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz);
119 #endif
120
121 string fixPriorityList(string pl, float from, float to, float subtract, float complete);
122 string mapPriorityList(string order, string(string) mapfunc);
123 string swapInPriorityList(string order, float i, float j);
124
125 float cvar_value_issafe(string s);
126
127 float cvar_settemp(string pKey, string pValue);
128 float cvar_settemp_restore();
129
130 #ifndef MENUQC
131 // modes: 0 = trust q3map2 (_mini images)
132 //        1 = trust tracebox (_radar images)
133 // in both modes, mapinfo's "size" overrides
134
135 string mi_shortname;
136 vector mi_min;
137 vector mi_max;
138 void get_mi_min_max(float mode);
139
140 vector mi_picmin; // adjusted mins that map to the picture (square)
141 vector mi_picmax; // adjusted maxs that map to the picture (square)
142 vector mi_pictexcoord0; // texcoords of the image corners (after transforming, these are 2D coords too)
143 vector mi_pictexcoord1; // texcoords of the image corners (after transforming, these are 2D coords too)
144 vector mi_pictexcoord2; // texcoords of the image corners (after transforming, these are 2D coords too)
145 vector mi_pictexcoord3; // texcoords of the image corners (after transforming, these are 2D coords too)
146 void get_mi_min_max_texcoords(float mode);
147 #endif
148
149 float almost_equals(float a, float b);
150 float almost_in_bounds(float a, float b, float c);
151
152 float power2of(float e);
153 float log2of(float x);
154
155 string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
156 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
157 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
158 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))
159
160 vector rgb_to_hsl(vector rgb);
161 vector hsl_to_rgb(vector hsl);
162 vector rgb_to_hsv(vector rgb);
163 vector hsv_to_rgb(vector hsv);
164 string rgb_to_hexcolor(vector rgb);
165
166 float boxesoverlap(vector m1, vector m2, vector m3, vector m4);
167 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs);
168
169 typedef float(string s, vector size) textLengthUpToWidth_widthFunction_t;
170 typedef float(string s) textLengthUpToLength_lenFunction_t;
171 float textLengthUpToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
172 string textShortenToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
173 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
174 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
175
176 string getWrappedLine_remaining;
177 string getWrappedLine(float w, vector size, textLengthUpToWidth_widthFunction_t tw);
178 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw);
179
180 float isGametypeInFilter(float gt, float tp, float ts, string pattern);
181
182 typedef void(float i1, float i2, entity pass) swapfunc_t; // is only ever called for i1 < i2
183 typedef float(float i1, float i2, entity pass) comparefunc_t; // <0 for <, ==0 for ==, >0 for > (like strcmp)
184 void shuffle(float n, swapfunc_t swap, entity pass);
185 void heapsort(float n, swapfunc_t swap, comparefunc_t cmp, entity pass);
186
187 string swapwords(string str, float i, float j);
188 string shufflewords(string str);
189
190 string substring_range(string s, float b, float e);
191
192 vector solve_quadratic(float a, float b, float c);
193 // solution 1 -> x
194 // solution 2 -> y
195 // z = 1 if a real solution exists, 0 if not
196 // if no real solution exists, x contains the real part and y the imaginary part of the complex solutions x+iy and x-iy
197
198 vector solve_shotdirection(vector myorg, vector myvel, vector eorg, vector evel, float spd, float newton_style);
199 vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_style, float mi, float ma);
200
201 void check_unacceptable_compiler_bugs();
202
203 float compressShotOrigin(vector v);
204 vector decompressShotOrigin(float f);
205
206 #ifdef SVQC
207 string rankings_reply, ladder_reply, lsmaps_reply, lsnewmaps_reply, maplist_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 vector healtharmor_maxdamage(float h, float a, float armorblock); // returns vector: maxdamage, armorideal, 1 if fully armored
220 vector healtharmor_applydamage(float a, float armorblock, float damage); // returns vector: take, save, 0
221
222 string getcurrentmod();
223
224 #ifndef MENUQC
225 #ifdef CSQC
226 float ReadInt24_t();
227 #else
228 void WriteInt24_t(float dest, float val);
229 #endif
230 #endif
231
232 // the NULL function
233 #ifdef GMQCC
234 #define func_null nil
235 #define string_null nil
236 #else
237 var void func_null(void);
238 var string string_null;
239 #endif
240 float float2range11(float f);
241 float float2range01(float f);
242
243 float gsl_ran_gaussian(float sigma);
244
245 string car(string s); // returns first word
246 string cdr(string s); // returns all but first word
247 float matchacl(string acl, string str); // matches str against ACL acl (with entries +foo*, +foo, +*foo, +*foo*, and same with - for forbidding)
248 float startsWith(string haystack, string needle);
249 float startsWithNocase(string haystack, string needle);
250
251 string get_model_datafilename(string mod, float skn, string fil); // skin -1 will return wildcard, mod string_null will also put wildcard there
252 string get_model_parameters_modelname;
253 float get_model_parameters_modelskin;
254 string get_model_parameters_name;
255 float get_model_parameters_species;
256 string get_model_parameters_sex;
257 float get_model_parameters_weight;
258 float get_model_parameters_age;
259 string get_model_parameters_bone_upperbody;
260 string get_model_parameters_bone_weapon;
261 #define MAX_AIM_BONES 4
262 string get_model_parameters_bone_aim[MAX_AIM_BONES];
263 float get_model_parameters_bone_aimweight[MAX_AIM_BONES];
264 float get_model_parameters_fixbone;
265 string get_model_parameters_desc;
266 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
267
268 vector vec2(vector v);
269
270 #ifndef MENUQC
271 vector NearestPointOnBox(entity box, vector org);
272 #endif
273
274 float vercmp(string v1, string v2);
275
276 float u8_strsize(string s);
277
278 // translation helpers
279 string prvm_language;
280 string language_filename(string s);
281 string CTX(string s);
282 #define ZCTX(s) strzone(CTX(s))
283
284 // x-encoding (encoding as zero length invisible string)
285 // encodes approx. 14 bits into 5 bytes of color code string
286 const float XENCODE_MAX = 21295; // 2*22*22*22-1
287 const float XENCODE_LEN = 5;
288 string xencode(float f);
289 float xdecode(string s);
290
291 #ifndef COMPAT_XON010_CHANNELS
292 #define sound(e,c,s,v,a) sound7(e,c,s,v,a,0,0)
293 #endif
294
295 float lowestbit(float f);
296
297 #ifdef CSQC
298 entity ReadCSQCEntity();
299 #endif
300
301 #ifndef MENUQC
302 string strtolower(string s);
303 #endif
304
305 string MakeConsoleSafe(string input);
306
307 #ifndef MENUQC
308 float InterpretBoolean(string input);
309 #endif
310
311 // generic shutdown handler
312 void Shutdown();
313
314 #ifndef MENUQC
315 .float skeleton_bones;
316 void Skeleton_SetBones(entity e);
317 // loops through the tags of model v using counter tagnum
318 #define FOR_EACH_TAG(v) float tagnum; Skeleton_SetBones(v); for(tagnum = 0; tagnum < v.skeleton_bones; tagnum++, gettaginfo(v, tagnum))
319 #endif
320 #ifdef SVQC
321 void WriteApproxPastTime(float dst, float t);
322 #endif
323 #ifdef CSQC
324 float ReadApproxPastTime();
325 #endif
326
327 // execute-stuff-next-frame subsystem
328 void execute_next_frame();
329 void queue_to_execute_next_frame(string s);
330
331 // for marking written-to values as unused where it's a good idea to do this
332 noref float unused_float;
333
334 // a function f with:
335 // f(0) = 0
336 // f(1) = 1
337 // f'(0) = startspeedfactor
338 // f'(1) = endspeedfactor
339 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float x);
340
341 // checks whether f'(x) = 0 anywhere from 0 to 1
342 // because if this is the case, the function is not usable for platforms
343 // as it may exceed 0..1 bounds, or go in reverse
344 float cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor);
345
346 typedef entity(entity cur, entity near, entity pass) findNextEntityNearFunction_t;
347 typedef float(entity a, entity b, entity pass) isConnectedFunction_t;
348 void FindConnectedComponent(entity e, .entity fld, findNextEntityNearFunction_t nxt, isConnectedFunction_t iscon, entity pass);
349
350 // expand multiple arguments into one argument by stripping parenthesis
351 #define XPD(...) __VA_ARGS__
352
353 #ifndef MENUQC
354 void backtrace(string msg);
355 #endif
356
357 // color code replace, place inside of sprintf and parse the string... defaults described as constants
358 // foreground/normal colors
359 var string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green  // primary priority (important names, etc)
360 var string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
361 var string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue   // tertiary priority or relatively inconsequential text
362 var string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red    // notice/attention grabbing texting
363 // "kill" colors
364 var string autocvar_hud_colorset_kill_1 = "1"; // K1 - Red    // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
365 var string autocvar_hud_colorset_kill_2 = "3"; // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
366 var string autocvar_hud_colorset_kill_3 = "4"; // K3 - Blue   // "good" or "beneficial" text (you fragging someone, etc)
367 // background color
368 var string autocvar_hud_colorset_background = "7"; // BG - White // neutral/unimportant text
369
370 string CCR(string input);
371
372 #ifndef MENUQC
373 #ifdef CSQC
374 #define GENTLE (autocvar_cl_gentle || autocvar_cl_gentle_messages)
375 #else
376 #define GENTLE autocvar_sv_gentle
377 #endif
378 #define normal_or_gentle(normal,gentle) (GENTLE ? ((gentle != "") ? gentle : normal) : normal)
379 #endif
380
381 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
382 #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
383 #define WRITESPECTATABLE_MSG_ONE(statement) WRITESPECTATABLE_MSG_ONE_VARNAME(oldmsg_entity, statement)
384 #define WRITESPECTATABLE(msg,statement) if(msg == MSG_ONE) { WRITESPECTATABLE_MSG_ONE(statement); } else statement float WRITESPECTATABLE_workaround = 0
385
386 vector vec3(float x, float y, float z);
387
388 #ifndef MENUQC
389 vector animfixfps(entity e, vector a, vector b);
390 #endif
391
392 #ifdef SVQC
393 void dedicated_print(string input);
394 #endif
395
396 // todo: better way to do this?
397 #ifdef MENUQC
398 #define PROGNAME "MENUQC"
399 #else
400 #ifdef SVQC
401 #define PROGNAME "SVQC"
402 #else
403 #define PROGNAME "CSQC"
404 #endif
405 #endif
406
407 #ifndef MENUQC
408 float Announcer_PickNumber(float num);
409 #endif