]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/util.qh
Merge branch 'fruitiex/nexrun'
[divverent/nexuiz.git] / data / qcsrc / common / util.qh
1 #define ACCUMULATE_FUNCTION(func,otherfunc) \
2         #ifdef func \
3         void __merge__##otherfunc() { func(); otherfunc(); } \
4         #undef func \
5         #define func __merge__##otherfunc \
6         #else \
7         #define func otherfunc \
8         #endif
9
10 // 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
11 // NOTE: s IS allowed to be a tempstring
12 string wordwrap(string s, float l);
13 #ifndef MENUQC
14 #ifndef CSQC
15 void wordwrap_sprint(string s, float l);
16 #endif
17 #endif
18 void wordwrap_cb(string s, float l, void(string) callback)
19
20 float GameCommand_Generic(string cmd);
21 // returns TRUE if handled, FALSE otherwise
22 // uses tokenize on its argument!
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 // works for up to 10 decimals!
32 string ftos_decimals(float number, float decimals);
33
34 vector colormapPaletteColor(float c, float isPants);
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(float db, string filename);
41 void db_dump(float db, string pFilename);
42 float db_create();
43 float db_load(string filename);
44 void db_close(float db);
45 string db_get(float db, string key);
46 void db_put(float db, string key, string value);
47
48 // stringbuffer loading/saving
49 float buf_load(string filename);
50 void buf_save(float buf, string filename);
51
52 // modulo function
53 #ifndef MENUQC
54 float mod(float a, float b) { return a - (floor(a / b) * b); }   
55 #endif
56
57 string GametypeNameFromType(float g);
58 #define TIME_TO_NTHS(t,n) floor((t) * (n) + 0.4)
59 string mmsss(float t);
60 string mmssss(float t);
61
62 #define TIME_DECIMALS 2
63 #define TIME_FACTOR 100
64 #define TIME_ENCODED_TOSTRING(n) mmssss(n)
65 #define RACE_RECORD "/race100record/"
66 #define CTS_RECORD "/cts100record/"
67 #define TIME_ENCODE(t) TIME_TO_NTHS(t, TIME_FACTOR)
68 #define TIME_DECODE(n) ((n) / TIME_FACTOR)
69
70 string ScoreString(float vflags, float value);
71
72 vector cross(vector a, vector b);
73
74 void compressShortVector_init();
75 vector decompressShortVector(float data);
76 float compressShortVector(vector vec);
77
78 #ifndef MENUQC
79 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz);
80
81 void fixedmakevectors(vector a);
82 #define fixedvectoangles2 vectoangles2
83 #define fixedvectoangles vectoangles
84 #endif
85
86 string fixPriorityList(string pl, float from, float to, float subtract, float complete);
87 string swapInPriorityList(string order, float i, float j);
88
89 float cvar_value_issafe(string s);
90
91 void cvar_settemp(string pKey, string pValue);
92 void cvar_settemp_restore();
93
94 #ifndef MENUQC
95 // modes: 0 = trust q3map2 (_mini images)
96 //        1 = trust tracebox (_radar images)
97 // in both modes, mapinfo's "size" overrides
98
99 string mi_shortname;
100 vector mi_min;
101 vector mi_max;
102 void get_mi_min_max(float mode);
103
104 vector mi_picmin; // adjusted mins that map to the picture (square)
105 vector mi_picmax; // adjusted maxs that map to the picture (square)
106 vector mi_pictexcoord0; // texcoords of the image corners (after transforming, these are 2D coords too)
107 vector mi_pictexcoord1; // texcoords of the image corners (after transforming, these are 2D coords too)
108 vector mi_pictexcoord2; // texcoords of the image corners (after transforming, these are 2D coords too)
109 vector mi_pictexcoord3; // texcoords of the image corners (after transforming, these are 2D coords too)
110 void get_mi_min_max_texcoords(float mode);
111 #endif
112
113 #define FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(x) void reference_##x() { x = x; }
114
115 float almost_equals(float a, float b);
116 float almost_in_bounds(float a, float b, float c);
117
118 float power2of(float e);
119 float log2of(float x);
120
121 string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
122 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
123 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
124 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))
125
126 vector rgb_to_hsl(vector rgb);
127 vector hsl_to_rgb(vector hsl);
128 vector rgb_to_hsv(vector rgb);
129 vector hsv_to_rgb(vector hsv);
130 string rgb_to_hexcolor(vector rgb);
131
132 float boxesoverlap(vector m1, vector m2, vector m3, vector m4);
133 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs);
134
135 #ifndef MENUQC
136 vector AnglesTransform_Apply(vector transform, vector v);
137 vector AnglesTransform_Multiply(vector t1, vector t2);
138 vector AnglesTransform_Invert(vector transform);
139 vector AnglesTransform_TurnDirection(vector transform);
140 vector AnglesTransform_Divide(vector to_transform, vector from_transform);
141 #endif
142
143 typedef float(string s, vector size) textLengthUpToWidth_widthFunction_t;
144 typedef float(string s) textLengthUpToLength_lenFunction_t;
145 float textLengthUpToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
146 string textShortenToWidth(string theText, float maxWidth, vector size, textLengthUpToWidth_widthFunction_t tw);
147 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
148 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw);
149
150 string getWrappedLine_remaining;
151 string getWrappedLine(float w, vector size, textLengthUpToWidth_widthFunction_t tw);
152 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw);
153
154 float isGametypeInFilter(float gt, float tp, string pattern);
155
156 typedef void(float i1, float i2, entity pass) swapfunc_t; // is only ever called for i1 < i2
157 typedef float(float i1, float i2, entity pass) comparefunc_t; // <0 for <, ==0 for ==, >0 for > (like strcmp)
158 void shuffle(float n, swapfunc_t swap, entity pass);
159 void heapsort(float n, swapfunc_t swap, comparefunc_t cmp, entity pass);
160
161 string swapwords(string str, float i, float j);
162 string shufflewords(string str);
163
164 string substring_range(string s, float b, float e);
165
166 vector solve_quadratic(float a, float b, float c);
167 // solution 1 -> x
168 // solution 2 -> y
169 // z = 1 if a real solution exists, 0 if not
170 // if no real solution exists, x contains the real part and y the imaginary part of the complex solutions x+iy and x-iy
171
172 void check_unacceptable_compiler_bugs();
173
174 float compressShotOrigin(vector v);
175 vector decompressShotOrigin(float f);
176
177 string rankings_reply, lsmaps_reply, lsnewmaps_reply, maplist_reply; // cached replies
178 string records_reply[10];
179
180 float RandomSelection_totalweight;
181 float RandomSelection_best_priority;
182 entity RandomSelection_chosen_ent;
183 float RandomSelection_chosen_float;
184 string RandomSelection_chosen_string;
185 void RandomSelection_Init();
186 void RandomSelection_Add(entity e, float f, string s, float weight, float priority);
187
188 vector healtharmor_maxdamage(float h, float a, float armorblock); // returns vector: maxdamage, armorideal, 1 if fully armored
189 vector healtharmor_applydamage(float a, float armorblock, float damage); // returns vector: take, save, 0
190
191 string getcurrentmod();
192
193 #ifndef MENUQC
194 #ifdef CSQC
195 float ReadInt24_t();
196 #else
197 void WriteInt24_t(float dest, float val);
198 #endif
199 #endif
200
201 // the NULL function
202 const var void func_null(void); FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(func_null)
203 const var string string_null;
204 float float2range11(float f);
205 float float2range01(float f);
206
207 float gsl_ran_gaussian(float sigma);
208
209 string car(string s); // returns first word
210 string cdr(string s); // returns all but first word
211 float matchacl(string acl, string str); // matches str against ACL acl (with entries +foo*, +foo, +*foo, +*foo*, and same with - for forbidding)
212 float startsWith(string haystack, string needle);
213 float startsWithNocase(string haystack, string needle);