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