]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/util.qh
Fix compile
[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 // unzone the string, and return it as tempstring. Safe to be called on string_null
32 string fstrunzone(string s);
33
34 // database (NOTE: keys are case sensitive)
35 void db_save(int db, string filename);
36 void db_dump(int db, string pFilename);
37 int db_create();
38 int db_load(string filename);
39 void db_close(int db);
40 string db_get(int db, string key);
41 void db_put(int db, string key, string value);
42
43 // stringbuffer loading/saving
44 int buf_load(string filename);
45 void buf_save(int buf, string filename);
46
47 #define TIME_TO_NTHS(t,n) floor((t) * (n) + 0.4)
48 string format_time(float seconds);
49 string mmsss(float t);
50 string mmssss(float t);
51
52 const float TIME_DECIMALS = 2;
53 const float TIME_FACTOR = 100;
54 #define TIME_ENCODED_TOSTRING(n) mmssss(n)
55 #define RACE_RECORD "/race100record/"
56 #define CTS_RECORD "/cts100record/"
57 #define TIME_ENCODE(t) TIME_TO_NTHS(t, TIME_FACTOR)
58 #define TIME_DECODE(n) ((n) / TIME_FACTOR)
59
60 string ScoreString(float vflags, float value);
61
62 void compressShortVector_init();
63 vector decompressShortVector(float data);
64 float compressShortVector(vector vec);
65
66 #ifndef MENUQC
67 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz);
68 #endif
69
70 string fixPriorityList(string pl, float from, float to, float subtract, float complete);
71 string mapPriorityList(string order, string(string) mapfunc);
72 string swapInPriorityList(string order, float i, float j);
73
74 float cvar_value_issafe(string s);
75
76 float cvar_settemp(string pKey, string pValue);
77 float cvar_settemp_restore();
78
79 #ifndef MENUQC
80 // modes: 0 = trust q3map2 (_mini images)
81 //        1 = trust tracebox (_radar images)
82 // in both modes, mapinfo's "size" overrides
83
84 string mi_shortname;
85 vector mi_min;
86 vector mi_max;
87 void get_mi_min_max(float mode);
88
89 vector mi_picmin; // adjusted mins that map to the picture (square)
90 vector mi_picmax; // adjusted maxs that map to the picture (square)
91 vector mi_pictexcoord0; // texcoords of the image corners (after transforming, these are 2D coords too)
92 vector mi_pictexcoord1; // texcoords of the image corners (after transforming, these are 2D coords too)
93 vector mi_pictexcoord2; // texcoords of the image corners (after transforming, these are 2D coords too)
94 vector mi_pictexcoord3; // texcoords of the image corners (after transforming, these are 2D coords too)
95 void get_mi_min_max_texcoords(float mode);
96 #endif
97
98 float almost_equals(float a, float b);
99 float almost_in_bounds(float a, float b, float c);
100
101 float power2of(float e);
102 float log2of(float x);
103
104 vector rgb_to_hsl(vector rgb);
105 vector hsl_to_rgb(vector hsl);
106 vector rgb_to_hsv(vector rgb);
107 vector hsv_to_rgb(vector hsv);
108 string rgb_to_hexcolor(vector rgb);
109
110 float boxesoverlap(vector m1, vector m2, vector m3, vector m4);
111 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs);
112
113 typedef float(string s, vector size) textLengthUpToWidth_widthFunction_t;
114 typedef float(string s) textLengthUpToLength_lenFunction_t;
115 float textLengthUpToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
116 string textShortenToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
117 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
118 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
119
120 string getWrappedLine_remaining;
121 string getWrappedLine(float w, vector size, textLengthUpToWidth_widthFunction_t tw);
122 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw);
123
124 float isGametypeInFilter(float gt, float tp, float ts, string pattern);
125
126 string swapwords(string str, float i, float j);
127 string shufflewords(string str);
128
129 string substring_range(string s, float b, float e);
130
131 vector solve_quadratic(float a, float b, float c);
132 // solution 1 -> x
133 // solution 2 -> y
134 // z = 1 if a real solution exists, 0 if not
135 // if no real solution exists, x contains the real part and y the imaginary part of the complex solutions x+iy and x-iy
136
137 vector solve_shotdirection(vector myorg, vector myvel, vector eorg, vector evel, float spd, float newton_style);
138 vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_style, float mi, float ma);
139
140 float compressShotOrigin(vector v);
141 vector decompressShotOrigin(float f);
142
143 #ifdef SVQC
144 string rankings_reply, ladder_reply, lsmaps_reply, maplist_reply, monsterlist_reply; // cached replies
145 string records_reply[10];
146 #endif
147
148 #ifndef MENUQC
149 vector healtharmor_maxdamage(float h, float a, float armorblock, int deathtype); // returns vector: maxdamage, armorideal, 1 if fully armored
150 vector healtharmor_applydamage(float a, float armorblock, int deathtype, float damage); // returns vector: take, save, 0
151 #endif
152
153 string getcurrentmod();
154
155 #ifndef MENUQC
156 #ifdef CSQC
157 int ReadInt24_t();
158 vector ReadInt48_t();
159 vector ReadInt72_t();
160 #else
161 void WriteInt24_t(float dest, float val);
162 void WriteInt48_t(float dest, vector val);
163 void WriteInt72_t(float dest, vector val);
164 #endif
165 #endif
166
167 float float2range11(float f);
168 float float2range01(float f);
169
170 float gsl_ran_gaussian(float sigma);
171
172 float matchacl(string acl, string str); // matches str against ACL acl (with entries +foo*, +foo, +*foo, +*foo*, and same with - for forbidding)
173
174 string get_model_datafilename(string mod, float skn, string fil); // skin -1 will return wildcard, mod string_null will also put wildcard there
175 string get_model_parameters_modelname;
176 float get_model_parameters_modelskin;
177 string get_model_parameters_name;
178 float get_model_parameters_species;
179 string get_model_parameters_sex;
180 float get_model_parameters_weight;
181 float get_model_parameters_age;
182 string get_model_parameters_description;
183 string get_model_parameters_bone_upperbody;
184 string get_model_parameters_bone_weapon;
185 const int MAX_AIM_BONES = 4;
186 string get_model_parameters_bone_aim[MAX_AIM_BONES];
187 float get_model_parameters_bone_aimweight[MAX_AIM_BONES];
188 float get_model_parameters_fixbone;
189 string get_model_parameters_desc;
190 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
191
192 #ifndef MENUQC
193 vector NearestPointOnBox(entity box, vector org);
194 #endif
195
196 float vercmp(string v1, string v2);
197
198 float u8_strsize(string s);
199
200 // x-encoding (encoding as zero length invisible string)
201 // encodes approx. 14 bits into 5 bytes of color code string
202 const float XENCODE_MAX = 21295; // 2*22*22*22-1
203 const float XENCODE_LEN = 5;
204 string xencode(float f);
205 float xdecode(string s);
206
207 #ifndef MENUQC
208 string strtolower(string s);
209 #endif
210
211 string MakeConsoleSafe(string input);
212
213 // generic shutdown handler
214 void Shutdown();
215
216 #ifndef MENUQC
217 .float skeleton_bones;
218 void Skeleton_SetBones(entity e);
219 // loops through the tags of model v using counter tagnum
220 #define FOR_EACH_TAG(v) float tagnum; Skeleton_SetBones(v); for(tagnum = 0; tagnum < v.skeleton_bones; tagnum++, gettaginfo(v, tagnum))
221 #endif
222 #ifdef SVQC
223 void WriteApproxPastTime(float dst, float t);
224 #endif
225 #ifdef CSQC
226 float ReadApproxPastTime();
227 #endif
228
229 // execute-stuff-next-frame subsystem
230 void execute_next_frame();
231 void queue_to_execute_next_frame(string s);
232
233 // for marking written-to values as unused where it's a good idea to do this
234 noref float unused_float;
235
236 // a function f with:
237 // f(0) = 0
238 // f(1) = 1
239 // f'(0) = startspeedfactor
240 // f'(1) = endspeedfactor
241 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float x);
242
243 // checks whether f'(x) = 0 anywhere from 0 to 1
244 // because if this is the case, the function is not usable for platforms
245 // as it may exceed 0..1 bounds, or go in reverse
246 float cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor);
247
248 typedef entity(entity cur, entity near, entity pass) findNextEntityNearFunction_t;
249 typedef float(entity a, entity b, entity pass) isConnectedFunction_t;
250 void FindConnectedComponent(entity e, .entity fld, findNextEntityNearFunction_t nxt, isConnectedFunction_t iscon, entity pass);
251
252 // expand multiple arguments into one argument by stripping parenthesis
253 #define XPD(...) __VA_ARGS__
254
255 // Some common varargs functions. Lowercase as they match C.
256 #define fprintf(file, ...) fputs(file, sprintf(__VA_ARGS__))
257 #define bprintf(...) bprint(sprintf(__VA_ARGS__))
258
259 string CCR(string input);
260
261 #ifndef MENUQC
262         #ifdef CSQC
263                 #define GENTLE (autocvar_cl_gentle || autocvar_cl_gentle_messages)
264         #else
265                 #define GENTLE autocvar_sv_gentle
266         #endif
267         #define normal_or_gentle(normal, gentle) (GENTLE ? ((gentle != "") ? gentle : normal) : normal)
268 #endif
269
270 #ifndef MENUQC
271 vector animfixfps(entity e, vector a, vector b);
272 #endif
273
274 #ifdef SVQC
275 void dedicated_print(string input);
276 #endif
277
278 #ifndef MENUQC
279 const float CNT_NORMAL = 1;
280 const float CNT_GAMESTART = 2;
281 const float CNT_IDLE = 3;
282 const float CNT_KILL = 4;
283 const float CNT_RESPAWN = 5;
284 const float CNT_ROUNDSTART = 6;
285 float Announcer_PickNumber(float type, float num);
286 #endif
287
288 #ifndef MENUQC
289 int Mod_Q1BSP_SuperContentsFromNativeContents(int nativecontents);
290 int Mod_Q1BSP_NativeContentsFromSuperContents(int supercontents);
291 #endif
292
293 // Quadratic splines (bezier)
294 vector bezier_quadratic_getpoint(vector a, vector p, vector b, float t);
295 vector bezier_quadratic_getderivative(vector a, vector p, vector b, float t);
296
297 #define APPEND_TO_STRING(list,sep,add) ((list) = (((list) != "") ? strcat(list, sep, add) : (add)))
298
299 // Returns the correct difference between two always increasing numbers
300 #define COMPARE_INCREASING(to,from) (to < from ? from + to + 2 : to - from)
301 #endif