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