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