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