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