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