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