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