]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/util.qh
Merge branch 'master' into terencehill/itemstime
[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 // 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
41 // NOTE: s IS allowed to be a tempstring
42 string wordwrap(string s, float l);
43 #ifndef MENUQC
44 #ifndef CSQC
45 void wordwrap_sprint(string s, float l);
46 #endif
47 #endif
48 void wordwrap_cb(string s, float l, void(string) callback)
49
50 #ifndef SVQC
51 string draw_currentSkin;
52 string draw_UseSkinFor(string pic);
53 #endif
54
55 // iterative depth-first search, with fields that go "up", "down left" and "right" in a tree
56 // for each element, funcPre is called first, then funcPre and funcPost for all its children, and funcPost last
57 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass);
58
59 float median(float a, float b, float c);
60
61 // converts a number to a string with the indicated number of decimals
62 // works for up to 10 decimals!
63 string ftos_decimals(float number, float decimals);
64
65 float fexists(string f);
66
67 vector colormapPaletteColor(float c, float isPants);
68
69 // unzone the string, and return it as tempstring. Safe to be called on string_null
70 string fstrunzone(string s);
71
72 // database (NOTE: keys are case sensitive)
73 void db_save(float db, string filename);
74 void db_dump(float db, string pFilename);
75 float db_create();
76 float db_load(string filename);
77 void db_close(float db);
78 string db_get(float db, string key);
79 void db_put(float db, string key, string value);
80
81 // stringbuffer loading/saving
82 float buf_load(string filename);
83 void buf_save(float buf, string filename);
84
85 // modulo function
86 #ifndef MENUQC
87 float mod(float a, float b) { return a - (floor(a / b) * b); }   
88 #endif
89
90 #define TIME_TO_NTHS(t,n) floor((t) * (n) + 0.4)
91 string mmsss(float t);
92 string mmssss(float t);
93
94 #define TIME_DECIMALS 2
95 #define TIME_FACTOR 100
96 #define TIME_ENCODED_TOSTRING(n) mmssss(n)
97 #define RACE_RECORD "/race100record/"
98 #define CTS_RECORD "/cts100record/"
99 #define TIME_ENCODE(t) TIME_TO_NTHS(t, TIME_FACTOR)
100 #define TIME_DECODE(n) ((n) / TIME_FACTOR)
101
102 string ScoreString(float vflags, float value);
103
104 float dotproduct(vector a, vector b);
105 vector cross(vector a, vector b);
106
107 void compressShortVector_init();
108 vector decompressShortVector(float data);
109 float compressShortVector(vector vec);
110
111 #ifndef MENUQC
112 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz);
113 #endif
114
115 string fixPriorityList(string pl, float from, float to, float subtract, float complete);
116 string mapPriorityList(string order, string(string) mapfunc);
117 string swapInPriorityList(string order, float i, float j);
118
119 float cvar_value_issafe(string s);
120
121 float cvar_settemp(string pKey, string pValue);
122 float cvar_settemp_restore();
123
124 #ifndef MENUQC
125 // modes: 0 = trust q3map2 (_mini images)
126 //        1 = trust tracebox (_radar images)
127 // in both modes, mapinfo's "size" overrides
128
129 string mi_shortname;
130 vector mi_min;
131 vector mi_max;
132 void get_mi_min_max(float mode);
133
134 vector mi_picmin; // adjusted mins that map to the picture (square)
135 vector mi_picmax; // adjusted maxs that map to the picture (square)
136 vector mi_pictexcoord0; // texcoords of the image corners (after transforming, these are 2D coords too)
137 vector mi_pictexcoord1; // texcoords of the image corners (after transforming, these are 2D coords too)
138 vector mi_pictexcoord2; // texcoords of the image corners (after transforming, these are 2D coords too)
139 vector mi_pictexcoord3; // texcoords of the image corners (after transforming, these are 2D coords too)
140 void get_mi_min_max_texcoords(float mode);
141 #endif
142
143 #define FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(x) void reference_##x() { x = x; }
144
145 float almost_equals(float a, float b);
146 float almost_in_bounds(float a, float b, float c);
147
148 float power2of(float e);
149 float log2of(float x);
150
151 string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
152 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
153 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
154 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))
155
156 vector rgb_to_hsl(vector rgb);
157 vector hsl_to_rgb(vector hsl);
158 vector rgb_to_hsv(vector rgb);
159 vector hsv_to_rgb(vector hsv);
160 string rgb_to_hexcolor(vector rgb);
161
162 float boxesoverlap(vector m1, vector m2, vector m3, vector m4);
163 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs);
164
165 typedef float(string s, vector size) textLengthUpToWidth_widthFunction_t;
166 typedef float(string s) textLengthUpToLength_lenFunction_t;
167 float textLengthUpToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
168 string textShortenToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
169 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
170 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
171
172 string getWrappedLine_remaining;
173 string getWrappedLine(float w, vector size, textLengthUpToWidth_widthFunction_t tw);
174 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw);
175
176 float isGametypeInFilter(float gt, float tp, float ts, string pattern);
177
178 typedef void(float i1, float i2, entity pass) swapfunc_t; // is only ever called for i1 < i2
179 typedef float(float i1, float i2, entity pass) comparefunc_t; // <0 for <, ==0 for ==, >0 for > (like strcmp)
180 void shuffle(float n, swapfunc_t swap, entity pass);
181 void heapsort(float n, swapfunc_t swap, comparefunc_t cmp, entity pass);
182
183 string swapwords(string str, float i, float j);
184 string shufflewords(string str);
185
186 string substring_range(string s, float b, float e);
187
188 vector solve_quadratic(float a, float b, float c);
189 // solution 1 -> x
190 // solution 2 -> y
191 // z = 1 if a real solution exists, 0 if not
192 // if no real solution exists, x contains the real part and y the imaginary part of the complex solutions x+iy and x-iy
193
194 vector solve_shotdirection(vector myorg, vector myvel, vector eorg, vector evel, float spd, float newton_style);
195 vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_style, float mi, float ma);
196
197 void check_unacceptable_compiler_bugs();
198
199 float compressShotOrigin(vector v);
200 vector decompressShotOrigin(float f);
201
202 #ifdef SVQC
203 string rankings_reply, ladder_reply, lsmaps_reply, lsnewmaps_reply, maplist_reply; // cached replies
204 string records_reply[10];
205 #endif
206
207 float RandomSelection_totalweight;
208 float RandomSelection_best_priority;
209 entity RandomSelection_chosen_ent;
210 float RandomSelection_chosen_float;
211 string RandomSelection_chosen_string;
212 void RandomSelection_Init();
213 void RandomSelection_Add(entity e, float f, string s, float weight, float priority);
214
215 vector healtharmor_maxdamage(float h, float a, float armorblock); // returns vector: maxdamage, armorideal, 1 if fully armored
216 vector healtharmor_applydamage(float a, float armorblock, float damage); // returns vector: take, save, 0
217
218 string getcurrentmod();
219
220 #ifndef MENUQC
221 #ifdef CSQC
222 float ReadInt24_t();
223 #else
224 void WriteInt24_t(float dest, float val);
225 #endif
226 #endif
227
228 // the NULL function
229 const var void func_null(void); FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(func_null)
230 const var string string_null;
231 float float2range11(float f);
232 float float2range01(float f);
233
234 float gsl_ran_gaussian(float sigma);
235
236 string car(string s); // returns first word
237 string cdr(string s); // returns all but first word
238 float matchacl(string acl, string str); // matches str against ACL acl (with entries +foo*, +foo, +*foo, +*foo*, and same with - for forbidding)
239 float startsWith(string haystack, string needle);
240 float startsWithNocase(string haystack, string needle);
241
242 string get_model_datafilename(string mod, float skn, string fil); // skin -1 will return wildcard, mod string_null will also put wildcard there
243 string get_model_parameters_modelname;
244 float get_model_parameters_modelskin;
245 string get_model_parameters_name;
246 float get_model_parameters_species;
247 string get_model_parameters_sex;
248 float get_model_parameters_weight;
249 float get_model_parameters_age;
250 string get_model_parameters_desc;
251 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
252
253 // stupid stupid stupid FTEQCC has a max limit on macro sizes, let's work around by splitting the macro into two macros! :(
254 #define HUD_Panel_GetName_Part2(id) \
255 switch(id) {\
256         case HUD_PANEL_ENGINEINFO: panel_name = HUD_PANELNAME_ENGINEINFO; break; \
257         case HUD_PANEL_INFOMESSAGES: panel_name = HUD_PANELNAME_INFOMESSAGES; break; \
258         case HUD_PANEL_PHYSICS: panel_name = HUD_PANELNAME_PHYSICS; break; \
259         case HUD_PANEL_CENTERPRINT: panel_name = HUD_PANELNAME_CENTERPRINT; break; \
260         case HUD_PANEL_ITEMSTIME: panel_name = HUD_PANELNAME_ITEMSTIME; break; \
261 } ENDS_WITH_CURLY_BRACE
262
263 // Get name of specified panel id
264 #define HUD_Panel_GetName(id) \
265 switch(id) { \
266         case HUD_PANEL_WEAPONS: panel_name = HUD_PANELNAME_WEAPONS; break; \
267         case HUD_PANEL_AMMO: panel_name = HUD_PANELNAME_AMMO; break; \
268         case HUD_PANEL_POWERUPS: panel_name = HUD_PANELNAME_POWERUPS; break; \
269         case HUD_PANEL_HEALTHARMOR: panel_name = HUD_PANELNAME_HEALTHARMOR; break; \
270         case HUD_PANEL_NOTIFY: panel_name = HUD_PANELNAME_NOTIFY; break; \
271         case HUD_PANEL_TIMER: panel_name = HUD_PANELNAME_TIMER; break; \
272         case HUD_PANEL_RADAR: panel_name = HUD_PANELNAME_RADAR; break; \
273         case HUD_PANEL_SCORE: panel_name = HUD_PANELNAME_SCORE; break; \
274         case HUD_PANEL_RACETIMER: panel_name = HUD_PANELNAME_RACETIMER; break; \
275         case HUD_PANEL_VOTE: panel_name = HUD_PANELNAME_VOTE; break; \
276         case HUD_PANEL_MODICONS: panel_name = HUD_PANELNAME_MODICONS; break; \
277         case HUD_PANEL_PRESSEDKEYS: panel_name = HUD_PANELNAME_PRESSEDKEYS; break; \
278         case HUD_PANEL_CHAT: panel_name = HUD_PANELNAME_CHAT; break; \
279     default: HUD_Panel_GetName_Part2(id)\
280 }
281
282 vector vec2(vector v);
283
284 #ifndef MENUQC
285 vector NearestPointOnBox(entity box, vector org);
286 #endif
287
288 float vercmp(string v1, string v2);
289
290 float u8_strsize(string s);
291
292 // translation helpers
293 string prvm_language;
294 string language_filename(string s);
295 string CTX(string s);
296 #define ZCTX(s) strzone(CTX(s))
297
298 // x-encoding (encoding as zero length invisible string)
299 // encodes approx. 14 bits into 5 bytes of color code string
300 const float XENCODE_MAX = 21295; // 2*22*22*22-1
301 const float XENCODE_LEN = 5;
302 string xencode(float f);
303 float xdecode(string s);
304
305 #ifndef COMPAT_XON010_CHANNELS
306 #define sound(e,c,s,v,a) sound7(e,c,s,v,a,0,0)
307 #endif
308
309 float lowestbit(float f);
310
311 #ifdef CSQC
312 entity ReadCSQCEntity()
313 #endif
314
315 #ifndef MENUQC
316 string strtolower(string s);
317 #endif
318
319 string MakeConsoleSafe(string input);
320
321 #ifndef MENUQC
322 float InterpretBoolean(string input);
323 #endif
324
325 // generic shutdown handler
326 void Shutdown();
327
328 #ifndef MENUQC
329 .float skeleton_bones;
330 void Skeleton_SetBones(entity e);
331 // loops through the tags of model v using counter tagnum
332 #define FOR_EACH_TAG(v) float tagnum; Skeleton_SetBones(v); for(tagnum = 0; tagnum < v.skeleton_bones; tagnum++, gettaginfo(v, tagnum))
333 #endif
334 #ifdef SVQC
335 void WriteApproxPastTime(float dst, float t);
336 #endif
337 #ifdef CSQC
338 float ReadApproxPastTime();
339 #endif
340
341 // execute-stuff-next-frame subsystem
342 void execute_next_frame();
343 void queue_to_execute_next_frame(string s);
344
345 // for marking written-to values as unused where it's a good idea to do this
346 noref float unused_float;
347
348
349
350 // a function f with:
351 // f(0) = 0
352 // f(1) = 1
353 // f'(0) = startspeedfactor
354 // f'(1) = endspeedfactor
355 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float x);
356
357 // checks whether f'(x) = 0 anywhere from 0 to 1
358 // because if this is the case, the function is not usable for platforms
359 // as it may exceed 0..1 bounds, or go in reverse
360 float cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor);
361
362 typedef entity(entity cur, entity near, entity pass) findNextEntityNearFunction_t;
363 typedef float(entity a, entity b, entity pass) isConnectedFunction_t;
364 void FindConnectedComponent(entity e, .entity fld, findNextEntityNearFunction_t nxt, isConnectedFunction_t iscon, entity pass);