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