]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/util.qc
fix compile
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qc
1 string wordwrap_buffer;
2
3 void wordwrap_buffer_put(string s)
4 {
5         wordwrap_buffer = strcat(wordwrap_buffer, s);
6 }
7
8 string wordwrap(string s, float l)
9 {
10         string r;
11         wordwrap_buffer = "";
12         wordwrap_cb(s, l, wordwrap_buffer_put);
13         r = wordwrap_buffer;
14         wordwrap_buffer = "";
15         return r;
16 }
17
18 #ifndef MENUQC
19 #ifndef CSQC
20 void wordwrap_buffer_sprint(string s)
21 {
22         wordwrap_buffer = strcat(wordwrap_buffer, s);
23         if(s == "\n")
24         {
25                 sprint(self, wordwrap_buffer);
26                 wordwrap_buffer = "";
27         }
28 }
29
30 void wordwrap_sprint(string s, float l)
31 {
32         wordwrap_buffer = "";
33         wordwrap_cb(s, l, wordwrap_buffer_sprint);
34         if(wordwrap_buffer != "")
35                 sprint(self, strcat(wordwrap_buffer, "\n"));
36         wordwrap_buffer = "";
37         return;
38 }
39 #endif
40 #endif
41
42 string unescape(string in)
43 {
44         local float i, len;
45         local string str, s;
46
47         // but it doesn't seem to be necessary in my tests at least
48         in = strzone(in);
49
50         len = strlen(in);
51         str = "";
52         for(i = 0; i < len; ++i)
53         {
54                 s = substring(in, i, 1);
55                 if(s == "\\")
56                 {
57                         s = substring(in, i+1, 1);
58                         if(s == "n")
59                                 str = strcat(str, "\n");
60                         else if(s == "\\")
61                                 str = strcat(str, "\\");
62                         else
63                                 str = strcat(str, substring(in, i, 2));
64                         ++i;
65                 } else
66                         str = strcat(str, s);
67         }
68
69         strunzone(in);
70         return str;
71 }
72
73 void wordwrap_cb(string s, float l, void(string) callback)
74 {
75         local string c;
76         local float lleft, i, j, wlen;
77
78         s = strzone(s);
79         lleft = l;
80         for (i = 0;i < strlen(s);++i)
81         {
82                 if (substring(s, i, 2) == "\\n")
83                 {
84                         callback("\n");
85                         lleft = l;
86                         ++i;
87                 }
88                 else if (substring(s, i, 1) == "\n")
89                 {
90                         callback("\n");
91                         lleft = l;
92                 }
93                 else if (substring(s, i, 1) == " ")
94                 {
95                         if (lleft > 0)
96                         {
97                                 callback(" ");
98                                 lleft = lleft - 1;
99                         }
100                 }
101                 else
102                 {
103                         for (j = i+1;j < strlen(s);++j)
104                                 //    ^^ this skips over the first character of a word, which
105                                 //       is ALWAYS part of the word
106                                 //       this is safe since if i+1 == strlen(s), i will become
107                                 //       strlen(s)-1 at the end of this block and the function
108                                 //       will terminate. A space can't be the first character we
109                                 //       read here, and neither can a \n be the start, since these
110                                 //       two cases have been handled above.
111                         {
112                                 c = substring(s, j, 1);
113                                 if (c == " ")
114                                         break;
115                                 if (c == "\\")
116                                         break;
117                                 if (c == "\n")
118                                         break;
119                                 // we need to keep this tempstring alive even if substring is
120                                 // called repeatedly, so call strcat even though we're not
121                                 // doing anything
122                                 callback("");
123                         }
124                         wlen = j - i;
125                         if (lleft < wlen)
126                         {
127                                 callback("\n");
128                                 lleft = l;
129                         }
130                         callback(substring(s, i, wlen));
131                         lleft = lleft - wlen;
132                         i = j - 1;
133                 }
134         }
135         strunzone(s);
136 }
137
138 float dist_point_line(vector p, vector l0, vector ldir)
139 {
140         ldir = normalize(ldir);
141         
142         // remove the component in line direction
143         p = p - (p * ldir) * ldir;
144
145         // vlen of the remaining vector
146         return vlen(p);
147 }
148
149 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass)
150 {
151         entity e;
152         e = start;
153         funcPre(pass, e);
154         while(e.downleft)
155         {
156                 e = e.downleft;
157                 funcPre(pass, e);
158         }
159         funcPost(pass, e);
160         while(e != start)
161         {
162                 if(e.right)
163                 {
164                         e = e.right;
165                         funcPre(pass, e);
166                         while(e.downleft)
167                         {
168                                 e = e.downleft;
169                                 funcPre(pass, e);
170                         }
171                 }
172                 else
173                         e = e.up;
174                 funcPost(pass, e);
175         }
176 }
177
178 float median(float a, float b, float c)
179 {
180         if(a < c)
181                 return bound(a, b, c);
182         return bound(c, b, a);
183 }
184
185 // converts a number to a string with the indicated number of decimals
186 // works for up to 10 decimals!
187 string ftos_decimals(float number, float decimals)
188 {
189         // we have sprintf...
190         return sprintf("%.*f", decimals, number);
191 }
192
193 float time;
194 vector colormapPaletteColor(float c, float isPants)
195 {
196         switch(c)
197         {
198                 case  0: return '0.800000 0.800000 0.800000';
199                 case  1: return '0.600000 0.400000 0.000000';
200                 case  2: return '0.000000 1.000000 0.501961';
201                 case  3: return '0.000000 1.000000 0.000000';
202                 case  4: return '1.000000 0.000000 0.000000';
203                 case  5: return '0.000000 0.658824 1.000000';
204                 case  6: return '0.000000 1.000000 1.000000';
205                 case  7: return '0.501961 1.000000 0.000000';
206                 case  8: return '0.501961 0.000000 1.000000';
207                 case  9: return '1.000000 0.000000 1.000000';
208                 case 10: return '1.000000 0.000000 0.501961';
209                 case 11: return '0.600000 0.600000 0.600000';
210                 case 12: return '1.000000 1.000000 0.000000';
211                 case 13: return '0.000000 0.313725 1.000000';
212                 case 14: return '1.000000 0.501961 0.000000';
213                 case 15:
214                         if(isPants)
215                                 return
216                                           '1 0 0' * (0.502 + 0.498 * sin(time / 2.7182818285 + 0.0000000000))
217                                         + '0 1 0' * (0.502 + 0.498 * sin(time / 2.7182818285 + 2.0943951024))
218                                         + '0 0 1' * (0.502 + 0.498 * sin(time / 2.7182818285 + 4.1887902048));
219                         else
220                                 return
221                                           '1 0 0' * (0.502 + 0.498 * sin(time / 3.1415926536 + 5.2359877560))
222                                         + '0 1 0' * (0.502 + 0.498 * sin(time / 3.1415926536 + 3.1415926536))
223                                         + '0 0 1' * (0.502 + 0.498 * sin(time / 3.1415926536 + 1.0471975512));
224                 default: return '0.000 0.000 0.000';
225         }
226 }
227
228 // unzone the string, and return it as tempstring. Safe to be called on string_null
229 string fstrunzone(string s)
230 {
231         string sc;
232         if not(s)
233                 return s;
234         sc = strcat(s, "");
235         strunzone(s);
236         return sc;
237 }
238
239 // Databases (hash tables)
240 #define DB_BUCKETS 8192
241 void db_save(float db, string pFilename)
242 {
243         float fh, i, n;
244         fh = fopen(pFilename, FILE_WRITE);
245         if(fh < 0) 
246         {
247                 print(strcat("^1Can't write DB to ", pFilename));
248                 return;
249         }
250         n = buf_getsize(db);
251         fputs(fh, strcat(ftos(DB_BUCKETS), "\n"));
252         for(i = 0; i < n; ++i)
253                 fputs(fh, strcat(bufstr_get(db, i), "\n"));
254         fclose(fh);
255 }
256
257 float db_create()
258 {
259         return buf_create();
260 }
261
262 float db_load(string pFilename)
263 {
264         float db, fh, i, j, n;
265         string l;
266         db = buf_create();
267         if(db < 0)
268                 return -1;
269         fh = fopen(pFilename, FILE_READ);
270         if(fh < 0)
271                 return db;
272         l = fgets(fh);
273         if(stof(l) == DB_BUCKETS)
274         {
275                 i = 0;
276                 while((l = fgets(fh)))
277                 {
278                         if(l != "")
279                                 bufstr_set(db, i, l);
280                         ++i;
281                 }
282         }
283         else
284         {
285                 // different count of buckets, or a dump?
286                 // need to reorganize the database then (SLOW)
287                 //
288                 // note: we also parse the first line (l) in case the DB file is
289                 // missing the bucket count
290                 do
291                 {
292                         n = tokenizebyseparator(l, "\\");
293                         for(j = 2; j < n; j += 2)
294                                 db_put(db, argv(j-1), uri_unescape(argv(j)));
295                 }
296                 while((l = fgets(fh)));
297         }
298         fclose(fh);
299         return db;
300 }
301
302 void db_dump(float db, string pFilename)
303 {
304         float fh, i, j, n, m;
305         fh = fopen(pFilename, FILE_WRITE);
306         if(fh < 0)
307                 error(strcat("Can't dump DB to ", pFilename));
308         n = buf_getsize(db);
309         fputs(fh, "0\n");
310         for(i = 0; i < n; ++i)
311         {
312                 m = tokenizebyseparator(bufstr_get(db, i), "\\");
313                 for(j = 2; j < m; j += 2)
314                         fputs(fh, strcat("\\", argv(j-1), "\\", argv(j), "\n"));
315         }
316         fclose(fh);
317 }
318
319 void db_close(float db)
320 {
321         buf_del(db);
322 }
323
324 string db_get(float db, string pKey)
325 {
326         float h;
327         h = mod(crc16(FALSE, pKey), DB_BUCKETS);
328         return uri_unescape(infoget(bufstr_get(db, h), pKey));
329 }
330
331 void db_put(float db, string pKey, string pValue)
332 {
333         float h;
334         h = mod(crc16(FALSE, pKey), DB_BUCKETS);
335         bufstr_set(db, h, infoadd(bufstr_get(db, h), pKey, uri_escape(pValue)));
336 }
337
338 void db_test()
339 {
340         float db, i;
341         print("LOAD...\n");
342         db = db_load("foo.db");
343         print("LOADED. FILL...\n");
344         for(i = 0; i < DB_BUCKETS; ++i)
345                 db_put(db, ftos(random()), "X");
346         print("FILLED. SAVE...\n");
347         db_save(db, "foo.db");
348         print("SAVED. CLOSE...\n");
349         db_close(db);
350         print("CLOSED.\n");
351 }
352
353 // Multiline text file buffers
354 float buf_load(string pFilename)
355 {
356         float buf, fh, i;
357         string l;
358         buf = buf_create();
359         if(buf < 0)
360                 return -1;
361         fh = fopen(pFilename, FILE_READ);
362         if(fh < 0)
363         {
364                 buf_del(buf);
365                 return -1;
366         }
367         i = 0;
368         while((l = fgets(fh)))
369         {
370                 bufstr_set(buf, i, l);
371                 ++i;
372         }
373         fclose(fh);
374         return buf;
375 }
376
377 void buf_save(float buf, string pFilename)
378 {
379         float fh, i, n;
380         fh = fopen(pFilename, FILE_WRITE);
381         if(fh < 0)
382                 error(strcat("Can't write buf to ", pFilename));
383         n = buf_getsize(buf);
384         for(i = 0; i < n; ++i)
385                 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
386         fclose(fh);
387 }
388
389 string GametypeNameFromType(float g)
390 {
391         if      (g == GAME_DEATHMATCH) return "dm";
392         else if (g == GAME_TEAM_DEATHMATCH) return "tdm";
393         else if (g == GAME_DOMINATION) return "dom";
394         else if (g == GAME_CTF) return "ctf";
395         else if (g == GAME_RUNEMATCH) return "rune";
396         else if (g == GAME_LMS) return "lms";
397         else if (g == GAME_ARENA) return "arena";
398         else if (g == GAME_CA) return "ca";
399         else if (g == GAME_KEYHUNT) return "kh";
400         else if (g == GAME_ONSLAUGHT) return "ons";
401         else if (g == GAME_ASSAULT) return "as";
402         else if (g == GAME_RACE) return "rc";
403         else if (g == GAME_NEXBALL) return "nexball";
404         else if (g == GAME_CTS) return "cts";
405         else if (g == GAME_FREEZETAG) return "freezetag";
406         else if (g == GAME_KEEPAWAY) return "ka";
407         return "dm";
408 }
409
410 string mmsss(float tenths)
411 {
412         float minutes;
413         string s;
414         tenths = floor(tenths + 0.5);
415         minutes = floor(tenths / 600);
416         tenths -= minutes * 600;
417         s = ftos(1000 + tenths);
418         return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
419 }
420
421 string mmssss(float hundredths)
422 {
423         float minutes;
424         string s;
425         hundredths = floor(hundredths + 0.5);
426         minutes = floor(hundredths / 6000);
427         hundredths -= minutes * 6000;
428         s = ftos(10000 + hundredths);
429         return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2));
430 }
431
432 string ScoreString(float pFlags, float pValue)
433 {
434         string valstr;
435         float l;
436
437         pValue = floor(pValue + 0.5); // round
438
439         if((pValue == 0) && (pFlags & (SFL_HIDE_ZERO | SFL_RANK | SFL_TIME)))
440                 valstr = "";
441         else if(pFlags & SFL_RANK)
442         {
443                 valstr = ftos(pValue);
444                 l = strlen(valstr);
445                 if((l >= 2) && (substring(valstr, l - 2, 1) == "1"))
446                         valstr = strcat(valstr, "th");
447                 else if(substring(valstr, l - 1, 1) == "1")
448                         valstr = strcat(valstr, "st");
449                 else if(substring(valstr, l - 1, 1) == "2")
450                         valstr = strcat(valstr, "nd");
451                 else if(substring(valstr, l - 1, 1) == "3")
452                         valstr = strcat(valstr, "rd");
453                 else
454                         valstr = strcat(valstr, "th");
455         }
456         else if(pFlags & SFL_TIME)
457                 valstr = TIME_ENCODED_TOSTRING(pValue);
458         else
459                 valstr = ftos(pValue);
460         
461         return valstr;
462 }
463
464 vector cross(vector a, vector b)
465 {
466         return
467                 '1 0 0' * (a_y * b_z - a_z * b_y)
468         +       '0 1 0' * (a_z * b_x - a_x * b_z)
469         +       '0 0 1' * (a_x * b_y - a_y * b_x);
470 }
471
472 // compressed vector format:
473 // like MD3, just even shorter
474 //   4 bit pitch (16 angles), 0 is -90, 8 is 0, 16 would be 90
475 //   5 bit yaw (32 angles), 0=0, 8=90, 16=180, 24=270
476 //   7 bit length (logarithmic encoding), 1/8 .. about 7844
477 //     length = 2^(length_encoded/8) / 8
478 // if pitch is 90, yaw does nothing and therefore indicates the sign (yaw is then either 11111 or 11110); 11111 is pointing DOWN
479 // thus, valid values are from 0000.11110.0000000 to 1111.11111.1111111
480 // the special value 0 indicates the zero vector
481
482 float lengthLogTable[128];
483
484 float invertLengthLog(float x)
485 {
486         float l, r, m, lerr, rerr;
487
488         if(x >= lengthLogTable[127])
489                 return 127;
490         if(x <= lengthLogTable[0])
491                 return 0;
492
493         l = 0;
494         r = 127;
495
496         while(r - l > 1)
497         {
498                 m = floor((l + r) / 2);
499                 if(lengthLogTable[m] < x)
500                         l = m;
501                 else
502                         r = m;
503         }
504
505         // now: r is >=, l is <
506         lerr = (x - lengthLogTable[l]);
507         rerr = (lengthLogTable[r] - x);
508         if(lerr < rerr)
509                 return l;
510         return r;
511 }
512
513 vector decompressShortVector(float data)
514 {
515         vector out;
516         float pitch, yaw, len;
517         if(data == 0)
518                 return '0 0 0';
519         pitch = (data & 0xF000) / 0x1000;
520         yaw =   (data & 0x0F80) / 0x80;
521         len =   (data & 0x007F);
522
523         //print("\ndecompress: pitch ", ftos(pitch)); print("yaw ", ftos(yaw)); print("len ", ftos(len), "\n");
524
525         if(pitch == 0)
526         {
527                 out_x = 0;
528                 out_y = 0;
529                 if(yaw == 31)
530                         out_z = -1;
531                 else
532                         out_z = +1;
533         }
534         else
535         {
536                 yaw   = .19634954084936207740 * yaw;
537                 pitch = .19634954084936207740 * pitch - 1.57079632679489661922;
538                 out_x = cos(yaw) *  cos(pitch);
539                 out_y = sin(yaw) *  cos(pitch);
540                 out_z =            -sin(pitch);
541         }
542
543         //print("decompressed: ", vtos(out), "\n");
544
545         return out * lengthLogTable[len];
546 }
547
548 float compressShortVector(vector vec)
549 {
550         vector ang;
551         float pitch, yaw, len;
552         if(vlen(vec) == 0)
553                 return 0;
554         //print("compress: ", vtos(vec), "\n");
555         ang = vectoangles(vec);
556         ang_x = -ang_x;
557         if(ang_x < -90)
558                 ang_x += 360;
559         if(ang_x < -90 && ang_x > +90)
560                 error("BOGUS vectoangles");
561         //print("angles: ", vtos(ang), "\n");
562
563         pitch = floor(0.5 + (ang_x + 90) * 16 / 180) & 15; // -90..90 to 0..14
564         if(pitch == 0)
565         {
566                 if(vec_z < 0)
567                         yaw = 31;
568                 else
569                         yaw = 30;
570         }
571         else
572                 yaw = floor(0.5 + ang_y * 32 / 360)          & 31; // 0..360 to 0..32
573         len = invertLengthLog(vlen(vec));
574
575         //print("compressed: pitch ", ftos(pitch)); print("yaw ", ftos(yaw)); print("len ", ftos(len), "\n");
576
577         return (pitch * 0x1000) + (yaw * 0x80) + len;
578 }
579
580 void compressShortVector_init()
581 {
582         float l, f, i;
583         l = 1;
584         f = pow(2, 1/8);
585         for(i = 0; i < 128; ++i)
586         {
587                 lengthLogTable[i] = l;
588                 l *= f;
589         }
590
591         if(cvar("developer"))
592         {
593                 print("Verifying vector compression table...\n");
594                 for(i = 0x0F00; i < 0xFFFF; ++i)
595                         if(i != compressShortVector(decompressShortVector(i)))
596                         {
597                                 print("BROKEN vector compression: ", ftos(i));
598                                 print(" -> ", vtos(decompressShortVector(i)));
599                                 print(" -> ", ftos(compressShortVector(decompressShortVector(i))));
600                                 print("\n");
601                                 error("b0rk");
602                         }
603                 print("Done.\n");
604         }
605 }
606
607 #ifndef MENUQC
608 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz)
609 {
610         traceline(v0, v0 + dvx, TRUE, forent); if(trace_fraction < 1) return 0;
611         traceline(v0, v0 + dvy, TRUE, forent); if(trace_fraction < 1) return 0;
612         traceline(v0, v0 + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
613         traceline(v0 + dvx, v0 + dvx + dvy, TRUE, forent); if(trace_fraction < 1) return 0;
614         traceline(v0 + dvx, v0 + dvx + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
615         traceline(v0 + dvy, v0 + dvy + dvx, TRUE, forent); if(trace_fraction < 1) return 0;
616         traceline(v0 + dvy, v0 + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
617         traceline(v0 + dvz, v0 + dvz + dvx, TRUE, forent); if(trace_fraction < 1) return 0;
618         traceline(v0 + dvz, v0 + dvz + dvy, TRUE, forent); if(trace_fraction < 1) return 0;
619         traceline(v0 + dvx + dvy, v0 + dvx + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
620         traceline(v0 + dvx + dvz, v0 + dvx + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
621         traceline(v0 + dvy + dvz, v0 + dvx + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
622         return 1;
623 }
624 #endif
625
626 string fixPriorityList(string order, float from, float to, float subtract, float complete)
627 {
628         string neworder;
629         float i, n, w;
630
631         n = tokenize_console(order);
632         neworder = "";
633         for(i = 0; i < n; ++i)
634         {
635                 w = stof(argv(i));
636                 if(w == floor(w))
637                 {
638                         if(w >= from && w <= to)
639                                 neworder = strcat(neworder, ftos(w), " ");
640                         else
641                         {
642                                 w -= subtract;
643                                 if(w >= from && w <= to)
644                                         neworder = strcat(neworder, ftos(w), " ");
645                         }
646                 }
647         }
648
649         if(complete)
650         {
651                 n = tokenize_console(neworder);
652                 for(w = to; w >= from; --w)
653                 {
654                         for(i = 0; i < n; ++i)
655                                 if(stof(argv(i)) == w)
656                                         break;
657                         if(i == n) // not found
658                                 neworder = strcat(neworder, ftos(w), " ");
659                 }
660         }
661         
662         return substring(neworder, 0, strlen(neworder) - 1);
663 }
664
665 string mapPriorityList(string order, string(string) mapfunc)
666 {
667         string neworder;
668         float i, n;
669
670         n = tokenize_console(order);
671         neworder = "";
672         for(i = 0; i < n; ++i)
673                 neworder = strcat(neworder, mapfunc(argv(i)), " ");
674         
675         return substring(neworder, 0, strlen(neworder) - 1);
676 }
677
678 string swapInPriorityList(string order, float i, float j)
679 {
680         string s;
681         float w, n;
682
683         n = tokenize_console(order);
684
685         if(i >= 0 && i < n && j >= 0 && j < n && i != j)
686         {
687                 s = "";
688                 for(w = 0; w < n; ++w)
689                 {
690                         if(w == i)
691                                 s = strcat(s, argv(j), " ");
692                         else if(w == j)
693                                 s = strcat(s, argv(i), " ");
694                         else
695                                 s = strcat(s, argv(w), " ");
696                 }
697                 return substring(s, 0, strlen(s) - 1);
698         }
699         
700         return order;
701 }
702
703 float cvar_value_issafe(string s)
704 {
705         if(strstrofs(s, "\"", 0) >= 0)
706                 return 0;
707         if(strstrofs(s, "\\", 0) >= 0)
708                 return 0;
709         if(strstrofs(s, ";", 0) >= 0)
710                 return 0;
711         if(strstrofs(s, "$", 0) >= 0)
712                 return 0;
713         if(strstrofs(s, "\r", 0) >= 0)
714                 return 0;
715         if(strstrofs(s, "\n", 0) >= 0)
716                 return 0;
717         return 1;
718 }
719
720 #ifndef MENUQC
721 void get_mi_min_max(float mode)
722 {
723         vector mi, ma;
724
725         if(mi_shortname)
726                 strunzone(mi_shortname);
727         mi_shortname = mapname;
728         if(!strcasecmp(substring(mi_shortname, 0, 5), "maps/"))
729                 mi_shortname = substring(mi_shortname, 5, strlen(mi_shortname) - 5);
730         if(!strcasecmp(substring(mi_shortname, strlen(mi_shortname) - 4, 4), ".bsp"))
731                 mi_shortname = substring(mi_shortname, 0, strlen(mi_shortname) - 4);
732         mi_shortname = strzone(mi_shortname);
733
734 #ifdef CSQC
735         mi = world.mins;
736         ma = world.maxs;
737 #else
738         mi = world.absmin;
739         ma = world.absmax;
740 #endif
741
742         mi_min = mi;
743         mi_max = ma;
744         MapInfo_Get_ByName(mi_shortname, 0, 0);
745         if(MapInfo_Map_mins_x < MapInfo_Map_maxs_x)
746         {
747                 mi_min = MapInfo_Map_mins;
748                 mi_max = MapInfo_Map_maxs;
749         }
750         else
751         {
752                 // not specified
753                 if(mode)
754                 {
755                         // be clever
756                         tracebox('1 0 0' * mi_x,
757                                          '0 1 0' * mi_y + '0 0 1' * mi_z,
758                                          '0 1 0' * ma_y + '0 0 1' * ma_z,
759                                          '1 0 0' * ma_x,
760                                          MOVE_WORLDONLY,
761                                          world);
762                         if(!trace_startsolid)
763                                 mi_min_x = trace_endpos_x;
764
765                         tracebox('0 1 0' * mi_y,
766                                          '1 0 0' * mi_x + '0 0 1' * mi_z,
767                                          '1 0 0' * ma_x + '0 0 1' * ma_z,
768                                          '0 1 0' * ma_y,
769                                          MOVE_WORLDONLY,
770                                          world);
771                         if(!trace_startsolid)
772                                 mi_min_y = trace_endpos_y;
773
774                         tracebox('0 0 1' * mi_z,
775                                          '1 0 0' * mi_x + '0 1 0' * mi_y,
776                                          '1 0 0' * ma_x + '0 1 0' * ma_y,
777                                          '0 0 1' * ma_z,
778                                          MOVE_WORLDONLY,
779                                          world);
780                         if(!trace_startsolid)
781                                 mi_min_z = trace_endpos_z;
782
783                         tracebox('1 0 0' * ma_x,
784                                          '0 1 0' * mi_y + '0 0 1' * mi_z,
785                                          '0 1 0' * ma_y + '0 0 1' * ma_z,
786                                          '1 0 0' * mi_x,
787                                          MOVE_WORLDONLY,
788                                          world);
789                         if(!trace_startsolid)
790                                 mi_max_x = trace_endpos_x;
791
792                         tracebox('0 1 0' * ma_y,
793                                          '1 0 0' * mi_x + '0 0 1' * mi_z,
794                                          '1 0 0' * ma_x + '0 0 1' * ma_z,
795                                          '0 1 0' * mi_y,
796                                          MOVE_WORLDONLY,
797                                          world);
798                         if(!trace_startsolid)
799                                 mi_max_y = trace_endpos_y;
800
801                         tracebox('0 0 1' * ma_z,
802                                          '1 0 0' * mi_x + '0 1 0' * mi_y,
803                                          '1 0 0' * ma_x + '0 1 0' * ma_y,
804                                          '0 0 1' * mi_z,
805                                          MOVE_WORLDONLY,
806                                          world);
807                         if(!trace_startsolid)
808                                 mi_max_z = trace_endpos_z;
809                 }
810         }
811 }
812
813 void get_mi_min_max_texcoords(float mode)
814 {
815         vector extend;
816
817         get_mi_min_max(mode);
818
819         mi_picmin = mi_min;
820         mi_picmax = mi_max;
821
822         // extend mi_picmax to get a square aspect ratio
823         // center the map in that area
824         extend = mi_picmax - mi_picmin;
825         if(extend_y > extend_x)
826         {
827                 mi_picmin_x -= (extend_y - extend_x) * 0.5;
828                 mi_picmax_x += (extend_y - extend_x) * 0.5;
829         }
830         else
831         {
832                 mi_picmin_y -= (extend_x - extend_y) * 0.5;
833                 mi_picmax_y += (extend_x - extend_y) * 0.5;
834         }
835
836         // add another some percent
837         extend = (mi_picmax - mi_picmin) * (1 / 64.0);
838         mi_picmin -= extend;
839         mi_picmax += extend;
840
841         // calculate the texcoords
842         mi_pictexcoord0 = mi_pictexcoord1 = mi_pictexcoord2 = mi_pictexcoord3 = '0 0 0';
843         // first the two corners of the origin
844         mi_pictexcoord0_x = (mi_min_x - mi_picmin_x) / (mi_picmax_x - mi_picmin_x);
845         mi_pictexcoord0_y = (mi_min_y - mi_picmin_y) / (mi_picmax_y - mi_picmin_y);
846         mi_pictexcoord2_x = (mi_max_x - mi_picmin_x) / (mi_picmax_x - mi_picmin_x);
847         mi_pictexcoord2_y = (mi_max_y - mi_picmin_y) / (mi_picmax_y - mi_picmin_y);
848         // then the other corners
849         mi_pictexcoord1_x = mi_pictexcoord0_x;
850         mi_pictexcoord1_y = mi_pictexcoord2_y;
851         mi_pictexcoord3_x = mi_pictexcoord2_x;
852         mi_pictexcoord3_y = mi_pictexcoord0_y;
853 }
854 #endif
855
856 #ifdef CSQC
857 void cvar_settemp(string pKey, string pValue)
858 {
859         error("cvar_settemp called from CSQC - use cvar_clientsettemp instead!");
860 }
861 void cvar_settemp_restore()
862 {
863         error("cvar_settemp_restore called from CSQC - use cvar_clientsettemp instead!");
864 }
865 #else
866 void cvar_settemp(string pKey, string pValue)
867 {
868         float i;
869         string settemp_var;
870         if(cvar_string(pKey) == pValue)
871                 return;
872         i = cvar("settemp_idx");
873         cvar_set("settemp_idx", ftos(i+1));
874         settemp_var = strcat("_settemp_x", ftos(i));
875 #ifdef MENUQC
876         registercvar(settemp_var, "", 0);
877 #else
878         registercvar(settemp_var, "");
879 #endif
880         cvar_set("settemp_list", strcat("1 ", pKey, " ", settemp_var, " ", cvar_string("settemp_list")));
881         cvar_set(settemp_var, cvar_string(pKey));
882         cvar_set(pKey, pValue);
883 }
884
885 void cvar_settemp_restore()
886 {
887         // undo what cvar_settemp did
888         float n, i;
889         n = tokenize_console(cvar_string("settemp_list"));
890         for(i = 0; i < n - 3; i += 3)
891                 cvar_set(argv(i + 1), cvar_string(argv(i + 2)));
892         cvar_set("settemp_list", "0");
893 }
894 #endif
895
896 float almost_equals(float a, float b)
897 {
898         float eps;
899         eps = (max(a, -a) + max(b, -b)) * 0.001;
900         if(a - b < eps && b - a < eps)
901                 return TRUE;
902         return FALSE;
903 }
904
905 float almost_in_bounds(float a, float b, float c)
906 {
907         float eps;
908         eps = (max(a, -a) + max(c, -c)) * 0.001;
909         return b == median(a - eps, b, c + eps);
910 }
911
912 float power2of(float e)
913 {
914         return pow(2, e);
915 }
916 float log2of(float x)
917 {
918         // NOTE: generated code
919         if(x > 2048)
920                 if(x > 131072)
921                         if(x > 1048576)
922                                 if(x > 4194304)
923                                         return 23;
924                                 else
925                                         if(x > 2097152)
926                                                 return 22;
927                                         else
928                                                 return 21;
929                         else
930                                 if(x > 524288)
931                                         return 20;
932                                 else
933                                         if(x > 262144)
934                                                 return 19;
935                                         else
936                                                 return 18;
937                 else
938                         if(x > 16384)
939                                 if(x > 65536)
940                                         return 17;
941                                 else
942                                         if(x > 32768)
943                                                 return 16;
944                                         else
945                                                 return 15;
946                         else
947                                 if(x > 8192)
948                                         return 14;
949                                 else
950                                         if(x > 4096)
951                                                 return 13;
952                                         else
953                                                 return 12;
954         else
955                 if(x > 32)
956                         if(x > 256)
957                                 if(x > 1024)
958                                         return 11;
959                                 else
960                                         if(x > 512)
961                                                 return 10;
962                                         else
963                                                 return 9;
964                         else
965                                 if(x > 128)
966                                         return 8;
967                                 else
968                                         if(x > 64)
969                                                 return 7;
970                                         else
971                                                 return 6;
972                 else
973                         if(x > 4)
974                                 if(x > 16)
975                                         return 5;
976                                 else
977                                         if(x > 8)
978                                                 return 4;
979                                         else
980                                                 return 3;
981                         else
982                                 if(x > 2)
983                                         return 2;
984                                 else
985                                         if(x > 1)
986                                                 return 1;
987                                         else
988                                                 return 0;
989 }
990
991 float rgb_mi_ma_to_hue(vector rgb, float mi, float ma)
992 {
993         if(mi == ma)
994                 return 0;
995         else if(ma == rgb_x)
996         {
997                 if(rgb_y >= rgb_z)
998                         return (rgb_y - rgb_z) / (ma - mi);
999                 else
1000                         return (rgb_y - rgb_z) / (ma - mi) + 6;
1001         }
1002         else if(ma == rgb_y)
1003                 return (rgb_z - rgb_x) / (ma - mi) + 2;
1004         else // if(ma == rgb_z)
1005                 return (rgb_x - rgb_y) / (ma - mi) + 4;
1006 }
1007
1008 vector hue_mi_ma_to_rgb(float hue, float mi, float ma)
1009 {
1010         vector rgb;
1011
1012         hue -= 6 * floor(hue / 6);
1013
1014         //else if(ma == rgb_x)
1015         //      hue = 60 * (rgb_y - rgb_z) / (ma - mi);
1016         if(hue <= 1)
1017         {
1018                 rgb_x = ma;
1019                 rgb_y = hue * (ma - mi) + mi;
1020                 rgb_z = mi;
1021         }
1022         //else if(ma == rgb_y)
1023         //      hue = 60 * (rgb_z - rgb_x) / (ma - mi) + 120;
1024         else if(hue <= 2)
1025         {
1026                 rgb_x = (2 - hue) * (ma - mi) + mi;
1027                 rgb_y = ma;
1028                 rgb_z = mi;
1029         }
1030         else if(hue <= 3)
1031         {
1032                 rgb_x = mi;
1033                 rgb_y = ma;
1034                 rgb_z = (hue - 2) * (ma - mi) + mi;
1035         }
1036         //else // if(ma == rgb_z)
1037         //      hue = 60 * (rgb_x - rgb_y) / (ma - mi) + 240;
1038         else if(hue <= 4)
1039         {
1040                 rgb_x = mi;
1041                 rgb_y = (4 - hue) * (ma - mi) + mi;
1042                 rgb_z = ma;
1043         }
1044         else if(hue <= 5)
1045         {
1046                 rgb_x = (hue - 4) * (ma - mi) + mi;
1047                 rgb_y = mi;
1048                 rgb_z = ma;
1049         }
1050         //else if(ma == rgb_x)
1051         //      hue = 60 * (rgb_y - rgb_z) / (ma - mi);
1052         else // if(hue <= 6)
1053         {
1054                 rgb_x = ma;
1055                 rgb_y = mi;
1056                 rgb_z = (6 - hue) * (ma - mi) + mi;
1057         }
1058
1059         return rgb;
1060 }
1061
1062 vector rgb_to_hsv(vector rgb)
1063 {
1064         float mi, ma;
1065         vector hsv;
1066
1067         mi = min3(rgb_x, rgb_y, rgb_z);
1068         ma = max3(rgb_x, rgb_y, rgb_z);
1069
1070         hsv_x = rgb_mi_ma_to_hue(rgb, mi, ma);
1071         hsv_z = ma;
1072
1073         if(ma == 0)
1074                 hsv_y = 0;
1075         else
1076                 hsv_y = 1 - mi/ma;
1077         
1078         return hsv;
1079 }
1080
1081 vector hsv_to_rgb(vector hsv)
1082 {
1083         return hue_mi_ma_to_rgb(hsv_x, hsv_z * (1 - hsv_y), hsv_z);
1084 }
1085
1086 vector rgb_to_hsl(vector rgb)
1087 {
1088         float mi, ma;
1089         vector hsl;
1090
1091         mi = min3(rgb_x, rgb_y, rgb_z);
1092         ma = max3(rgb_x, rgb_y, rgb_z);
1093
1094         hsl_x = rgb_mi_ma_to_hue(rgb, mi, ma);
1095         
1096         hsl_z = 0.5 * (mi + ma);
1097         if(mi == ma)
1098                 hsl_y = 0;
1099         else if(hsl_z <= 0.5)
1100                 hsl_y = (ma - mi) / (2*hsl_z);
1101         else // if(hsl_z > 0.5)
1102                 hsl_y = (ma - mi) / (2 - 2*hsl_z);
1103         
1104         return hsl;
1105 }
1106
1107 vector hsl_to_rgb(vector hsl)
1108 {
1109         float mi, ma, maminusmi;
1110
1111         if(hsl_z <= 0.5)
1112                 maminusmi = hsl_y * 2 * hsl_z;
1113         else
1114                 maminusmi = hsl_y * (2 - 2 * hsl_z);
1115         
1116         // hsl_z     = 0.5 * mi + 0.5 * ma
1117         // maminusmi =     - mi +       ma
1118         mi = hsl_z - 0.5 * maminusmi;
1119         ma = hsl_z + 0.5 * maminusmi;
1120
1121         return hue_mi_ma_to_rgb(hsl_x, mi, ma);
1122 }
1123
1124 string rgb_to_hexcolor(vector rgb)
1125 {
1126         return
1127                 strcat(
1128                         "^x",
1129                         DEC_TO_HEXDIGIT(floor(rgb_x * 15 + 0.5)),
1130                         DEC_TO_HEXDIGIT(floor(rgb_y * 15 + 0.5)),
1131                         DEC_TO_HEXDIGIT(floor(rgb_z * 15 + 0.5))
1132                 );
1133 }
1134
1135 // requires that m2>m1 in all coordinates, and that m4>m3
1136 float boxesoverlap(vector m1, vector m2, vector m3, vector m4) {return m2_x >= m3_x && m1_x <= m4_x && m2_y >= m3_y && m1_y <= m4_y && m2_z >= m3_z && m1_z <= m4_z;};
1137
1138 // requires the same, but is a stronger condition
1139 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs) {return smins_x >= bmins_x && smaxs_x <= bmaxs_x && smins_y >= bmins_y && smaxs_y <= bmaxs_y && smins_z >= bmins_z && smaxs_z <= bmaxs_z;};
1140
1141 #ifndef MENUQC
1142 #endif
1143
1144 float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
1145 {
1146         float ICanHasKallerz;
1147
1148         // detect color codes support in the width function
1149         ICanHasKallerz = (w("^7", theSize) == 0);
1150
1151         // STOP.
1152         // The following function is SLOW.
1153         // For your safety and for the protection of those around you...
1154         // DO NOT CALL THIS AT HOME.
1155         // No really, don't.
1156         if(w(theText, theSize) <= maxWidth)
1157                 return strlen(theText); // yeah!
1158
1159         // binary search for right place to cut string
1160         float ch;
1161         float left, right, middle; // this always works
1162         left = 0;
1163         right = strlen(theText); // this always fails
1164         do
1165         {
1166                 middle = floor((left + right) / 2);
1167                 if(w(substring(theText, 0, middle), theSize) <= maxWidth)
1168                         left = middle;
1169                 else
1170                         right = middle;
1171         }
1172         while(left < right - 1);
1173
1174         if(ICanHasKallerz)
1175         {
1176                 // NOTE: when color codes are involved, this binary search is,
1177                 // mathematically, BROKEN. However, it is obviously guaranteed to
1178                 // terminate, as the range still halves each time - but nevertheless, it is
1179                 // guaranteed that it finds ONE valid cutoff place (where "left" is in
1180                 // range, and "right" is outside).
1181                 
1182                 // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4)
1183                 // and decrease left on the basis of the chars detected of the truncated tag
1184                 // Even if the ^xrgb tag is not complete/correct, left is decreased
1185                 // (sometimes too much but with a correct result)
1186                 // it fixes also ^[0-9]
1187                 while(left >= 1 && substring(theText, left-1, 1) == "^")
1188                         left-=1;
1189
1190                 if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/
1191                         left-=2;
1192                 else if (left >= 3 && substring(theText, left-3, 2) == "^x")
1193                         {
1194                                 ch = str2chr(theText, left-1);
1195                                 if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/
1196                                         left-=3;
1197                         }
1198                 else if (left >= 4 && substring(theText, left-4, 2) == "^x")
1199                         {
1200                                 ch = str2chr(theText, left-2);
1201                                 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
1202                                 {
1203                                         ch = str2chr(theText, left-1);
1204                                         if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/
1205                                                 left-=4;
1206                                 }
1207                         }
1208         }
1209         
1210         return left;
1211 }
1212
1213 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t w)
1214 {
1215         float ICanHasKallerz;
1216
1217         // detect color codes support in the width function
1218         ICanHasKallerz = (w("^7") == 0);
1219
1220         // STOP.
1221         // The following function is SLOW.
1222         // For your safety and for the protection of those around you...
1223         // DO NOT CALL THIS AT HOME.
1224         // No really, don't.
1225         if(w(theText) <= maxWidth)
1226                 return strlen(theText); // yeah!
1227
1228         // binary search for right place to cut string
1229         float ch;
1230         float left, right, middle; // this always works
1231         left = 0;
1232         right = strlen(theText); // this always fails
1233         do
1234         {
1235                 middle = floor((left + right) / 2);
1236                 if(w(substring(theText, 0, middle)) <= maxWidth)
1237                         left = middle;
1238                 else
1239                         right = middle;
1240         }
1241         while(left < right - 1);
1242
1243         if(ICanHasKallerz)
1244         {
1245                 // NOTE: when color codes are involved, this binary search is,
1246                 // mathematically, BROKEN. However, it is obviously guaranteed to
1247                 // terminate, as the range still halves each time - but nevertheless, it is
1248                 // guaranteed that it finds ONE valid cutoff place (where "left" is in
1249                 // range, and "right" is outside).
1250                 
1251                 // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4)
1252                 // and decrease left on the basis of the chars detected of the truncated tag
1253                 // Even if the ^xrgb tag is not complete/correct, left is decreased
1254                 // (sometimes too much but with a correct result)
1255                 // it fixes also ^[0-9]
1256                 while(left >= 1 && substring(theText, left-1, 1) == "^")
1257                         left-=1;
1258
1259                 if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/
1260                         left-=2;
1261                 else if (left >= 3 && substring(theText, left-3, 2) == "^x")
1262                         {
1263                                 ch = str2chr(theText, left-1);
1264                                 if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/
1265                                         left-=3;
1266                         }
1267                 else if (left >= 4 && substring(theText, left-4, 2) == "^x")
1268                         {
1269                                 ch = str2chr(theText, left-2);
1270                                 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
1271                                 {
1272                                         ch = str2chr(theText, left-1);
1273                                         if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/
1274                                                 left-=4;
1275                                 }
1276                         }
1277         }
1278         
1279         return left;
1280 }
1281
1282 string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
1283 {
1284         float cantake;
1285         float take;
1286         string s;
1287
1288         s = getWrappedLine_remaining;
1289
1290         cantake = textLengthUpToWidth(s, w, theFontSize, tw);
1291         if(cantake > 0 && cantake < strlen(s))
1292         {
1293                 take = cantake - 1;
1294                 while(take > 0 && substring(s, take, 1) != " ")
1295                         --take;
1296                 if(take == 0)
1297                 {
1298                         getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
1299                         if(getWrappedLine_remaining == "")
1300                                 getWrappedLine_remaining = string_null;
1301                         return substring(s, 0, cantake);
1302                 }
1303                 else
1304                 {
1305                         getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
1306                         if(getWrappedLine_remaining == "")
1307                                 getWrappedLine_remaining = string_null;
1308                         return substring(s, 0, take);
1309                 }
1310         }
1311         else
1312         {
1313                 getWrappedLine_remaining = string_null;
1314                 return s;
1315         }
1316 }
1317
1318 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw)
1319 {
1320         float cantake;
1321         float take;
1322         string s;
1323
1324         s = getWrappedLine_remaining;
1325
1326         cantake = textLengthUpToLength(s, w, tw);
1327         if(cantake > 0 && cantake < strlen(s))
1328         {
1329                 take = cantake - 1;
1330                 while(take > 0 && substring(s, take, 1) != " ")
1331                         --take;
1332                 if(take == 0)
1333                 {
1334                         getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
1335                         if(getWrappedLine_remaining == "")
1336                                 getWrappedLine_remaining = string_null;
1337                         return substring(s, 0, cantake);
1338                 }
1339                 else
1340                 {
1341                         getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
1342                         if(getWrappedLine_remaining == "")
1343                                 getWrappedLine_remaining = string_null;
1344                         return substring(s, 0, take);
1345                 }
1346         }
1347         else
1348         {
1349                 getWrappedLine_remaining = string_null;
1350                 return s;
1351         }
1352 }
1353
1354 string textShortenToWidth(string theText, float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
1355 {
1356         if(tw(theText, theFontSize) <= maxWidth)
1357                 return theText;
1358         else
1359                 return strcat(substring(theText, 0, textLengthUpToWidth(theText, maxWidth - tw("...", theFontSize), theFontSize, tw)), "...");
1360 }
1361
1362 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw)
1363 {
1364         if(tw(theText) <= maxWidth)
1365                 return theText;
1366         else
1367                 return strcat(substring(theText, 0, textLengthUpToLength(theText, maxWidth - tw("..."), tw)), "...");
1368 }
1369
1370 float isGametypeInFilter(float gt, float tp, string pattern)
1371 {
1372         string subpattern, subpattern2, subpattern3;
1373         subpattern = strcat(",", GametypeNameFromType(gt), ",");
1374         if(tp)
1375                 subpattern2 = ",teams,";
1376         else
1377                 subpattern2 = ",noteams,";
1378         if(gt == GAME_RACE || gt == GAME_CTS)
1379                 subpattern3 = ",race,";
1380         else
1381                 subpattern3 = string_null;
1382
1383         if(substring(pattern, 0, 1) == "-")
1384         {
1385                 pattern = substring(pattern, 1, strlen(pattern) - 1);
1386                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) >= 0)
1387                         return 0;
1388                 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) >= 0)
1389                         return 0;
1390                 if(subpattern3 && strstrofs(strcat(",", pattern, ","), subpattern3, 0) >= 0)
1391                         return 0;
1392         }
1393         else
1394         {
1395                 if(substring(pattern, 0, 1) == "+")
1396                         pattern = substring(pattern, 1, strlen(pattern) - 1);
1397                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) < 0)
1398                 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) < 0)
1399                 if((!subpattern3) || strstrofs(strcat(",", pattern, ","), subpattern3, 0) < 0)
1400                         return 0;
1401         }
1402         return 1;
1403 }
1404
1405 void shuffle(float n, swapfunc_t swap, entity pass)
1406 {
1407         float i, j;
1408         for(i = 1; i < n; ++i)
1409         {
1410                 // swap i-th item at a random position from 0 to i
1411                 // proof for even distribution:
1412                 //   n = 1: obvious
1413                 //   n -> n+1:
1414                 //     item n+1 gets at any position with chance 1/(n+1)
1415                 //     all others will get their 1/n chance reduced by factor n/(n+1)
1416                 //     to be on place n+1, their chance will be 1/(n+1)
1417                 //     1/n * n/(n+1) = 1/(n+1)
1418                 //     q.e.d.
1419                 j = floor(random() * (i + 1));
1420                 if(j != i)
1421                         swap(j, i, pass);
1422         }
1423 }
1424
1425 string substring_range(string s, float b, float e)
1426 {
1427         return substring(s, b, e - b);
1428 }
1429
1430 string swapwords(string str, float i, float j)
1431 {
1432         float n;
1433         string s1, s2, s3, s4, s5;
1434         float si, ei, sj, ej, s0, en;
1435         n = tokenizebyseparator(str, " "); // must match g_maplist processing in ShuffleMaplist and "shuffle"
1436         si = argv_start_index(i);
1437         sj = argv_start_index(j);
1438         ei = argv_end_index(i);
1439         ej = argv_end_index(j);
1440         s0 = argv_start_index(0);
1441         en = argv_end_index(n-1);
1442         s1 = substring_range(str, s0, si);
1443         s2 = substring_range(str, si, ei);
1444         s3 = substring_range(str, ei, sj);
1445         s4 = substring_range(str, sj, ej);
1446         s5 = substring_range(str, ej, en);
1447         return strcat(s1, s4, s3, s2, s5);
1448 }
1449
1450 string _shufflewords_str;
1451 void _shufflewords_swapfunc(float i, float j, entity pass)
1452 {
1453         _shufflewords_str = swapwords(_shufflewords_str, i, j);
1454 }
1455 string shufflewords(string str)
1456 {
1457         float n;
1458         _shufflewords_str = str;
1459         n = tokenizebyseparator(str, " ");
1460         shuffle(n, _shufflewords_swapfunc, world);
1461         str = _shufflewords_str;
1462         _shufflewords_str = string_null;
1463         return str;
1464 }
1465
1466 vector solve_quadratic(float a, float b, float c) // ax^2 + bx + c = 0
1467 {
1468         vector v;
1469         float D;
1470         v = '0 0 0';
1471         if(a == 0)
1472         {
1473                 if(b != 0)
1474                 {
1475                         v_x = v_y = -c / b;
1476                         v_z = 1;
1477                 }
1478                 else
1479                 {
1480                         if(c == 0)
1481                         {
1482                                 // actually, every number solves the equation!
1483                                 v_z = 1;
1484                         }
1485                 }
1486         }
1487         else
1488         {
1489                 D = b*b - 4*a*c;
1490                 if(D >= 0)
1491                 {
1492                         D = sqrt(D);
1493                         if(a > 0) // put the smaller solution first
1494                         {
1495                                 v_x = ((-b)-D) / (2*a);
1496                                 v_y = ((-b)+D) / (2*a);
1497                         }
1498                         else
1499                         {
1500                                 v_x = (-b+D) / (2*a);
1501                                 v_y = (-b-D) / (2*a);
1502                         }
1503                         v_z = 1;
1504                 }
1505                 else
1506                 {
1507                         // complex solutions!
1508                         D = sqrt(-D);
1509                         v_x = -b / (2*a);
1510                         if(a > 0)
1511                                 v_y =  D / (2*a);
1512                         else
1513                                 v_y = -D / (2*a);
1514                         v_z = 0;
1515                 }
1516         }
1517         return v;
1518 }
1519
1520
1521 float _unacceptable_compiler_bug_1_a(float b, float c) { return b == c; }
1522 float _unacceptable_compiler_bug_1_b() { return 1; }
1523 float _unacceptable_compiler_bug_1_c(float d) { return 2 * d; }
1524 float _unacceptable_compiler_bug_1_d() { return 1; }
1525
1526 void check_unacceptable_compiler_bugs()
1527 {
1528         if(cvar("_allow_unacceptable_compiler_bugs"))
1529                 return;
1530         tokenize_console("foo bar");
1531         if(strcat(argv(0), substring("foo bar", 4, 7 - argv_start_index(1))) == "barbar")
1532                 error("fteqcc bug introduced with revision 3178 detected. Please upgrade fteqcc to a later revision, downgrade fteqcc to revision 3177, or pester Spike until he fixes it. You can set _allow_unacceptable_compiler_bugs 1 to skip this check, but expect stuff to be horribly broken then.");
1533 }
1534
1535 float compressShotOrigin(vector v)
1536 {
1537         float x, y, z;
1538         x = rint(v_x * 2);
1539         y = rint(v_y * 4) + 128;
1540         z = rint(v_z * 4) + 128;
1541         if(x > 255 || x < 0)
1542         {
1543                 print("shot origin ", vtos(v), " x out of bounds\n");
1544                 x = bound(0, x, 255);
1545         }
1546         if(y > 255 || y < 0)
1547         {
1548                 print("shot origin ", vtos(v), " y out of bounds\n");
1549                 y = bound(0, y, 255);
1550         }
1551         if(z > 255 || z < 0)
1552         {
1553                 print("shot origin ", vtos(v), " z out of bounds\n");
1554                 z = bound(0, z, 255);
1555         }
1556         return x * 0x10000 + y * 0x100 + z;
1557 }
1558 vector decompressShotOrigin(float f)
1559 {
1560         vector v;
1561         v_x = ((f & 0xFF0000) / 0x10000) / 2;
1562         v_y = ((f & 0xFF00) / 0x100 - 128) / 4;
1563         v_z = ((f & 0xFF) - 128) / 4;
1564         return v;
1565 }
1566
1567 void heapsort(float n, swapfunc_t swap, comparefunc_t cmp, entity pass)
1568 {
1569         float start, end, root, child;
1570
1571         // heapify
1572         start = floor((n - 2) / 2);
1573         while(start >= 0)
1574         {
1575                 // siftdown(start, count-1);
1576                 root = start;
1577                 while(root * 2 + 1 <= n-1)
1578                 {
1579                         child = root * 2 + 1;
1580                         if(child < n-1)
1581                                 if(cmp(child, child+1, pass) < 0)
1582                                         ++child;
1583                         if(cmp(root, child, pass) < 0)
1584                         {
1585                                 swap(root, child, pass);
1586                                 root = child;
1587                         }
1588                         else
1589                                 break;
1590                 }
1591                 // end of siftdown
1592                 --start;
1593         }
1594
1595         // extract
1596         end = n - 1;
1597         while(end > 0)
1598         {
1599                 swap(0, end, pass);
1600                 --end;
1601                 // siftdown(0, end);
1602                 root = 0;
1603                 while(root * 2 + 1 <= end)
1604                 {
1605                         child = root * 2 + 1;
1606                         if(child < end && cmp(child, child+1, pass) < 0)
1607                                 ++child;
1608                         if(cmp(root, child, pass) < 0)
1609                         {
1610                                 swap(root, child, pass);
1611                                 root = child;
1612                         }
1613                         else
1614                                 break;
1615                 }
1616                 // end of siftdown
1617         }
1618 }
1619
1620 void RandomSelection_Init()
1621 {
1622         RandomSelection_totalweight = 0;
1623         RandomSelection_chosen_ent = world;
1624         RandomSelection_chosen_float = 0;
1625         RandomSelection_chosen_string = string_null;
1626         RandomSelection_best_priority = -1;
1627 }
1628 void RandomSelection_Add(entity e, float f, string s, float weight, float priority)
1629 {
1630         if(priority > RandomSelection_best_priority)
1631         {
1632                 RandomSelection_best_priority = priority;
1633                 RandomSelection_chosen_ent = e;
1634                 RandomSelection_chosen_float = f;
1635                 RandomSelection_chosen_string = s;
1636                 RandomSelection_totalweight = weight;
1637         }
1638         else if(priority == RandomSelection_best_priority)
1639         {
1640                 RandomSelection_totalweight += weight;
1641                 if(random() * RandomSelection_totalweight <= weight)
1642                 {
1643                         RandomSelection_chosen_ent = e;
1644                         RandomSelection_chosen_float = f;
1645                         RandomSelection_chosen_string = s;
1646                 }
1647         }
1648 }
1649
1650 vector healtharmor_maxdamage(float h, float a, float armorblock)
1651 {
1652         // NOTE: we'll always choose the SMALLER value...
1653         float healthdamage, armordamage, armorideal;
1654         vector v;
1655         healthdamage = (h - 1) / (1 - armorblock); // damage we can take if we could use more health
1656         armordamage = a + (h - 1); // damage we can take if we could use more armor
1657         armorideal = healthdamage * armorblock;
1658         v_y = armorideal;
1659         if(armordamage < healthdamage)
1660         {
1661                 v_x = armordamage;
1662                 v_z = 1;
1663         }
1664         else
1665         {
1666                 v_x = healthdamage;
1667                 v_z = 0;
1668         }
1669         return v;
1670 }
1671
1672 vector healtharmor_applydamage(float a, float armorblock, float damage)
1673 {
1674         vector v;
1675         v_y = bound(0, damage * armorblock, a); // save
1676         v_x = bound(0, damage - v_y, damage); // take
1677         v_z = 0;
1678         return v;
1679 }
1680
1681 string getcurrentmod()
1682 {
1683         float n;
1684         string m;
1685         m = cvar_string("fs_gamedir");
1686         n = tokenize_console(m);
1687         if(n == 0)
1688                 return "data";
1689         else
1690                 return argv(n - 1);
1691 }
1692
1693 #ifndef MENUQC
1694 #ifdef CSQC
1695 float ReadInt24_t()
1696 {
1697         float v;
1698         v = ReadShort() * 256; // note: this is signed
1699         v += ReadByte(); // note: this is unsigned
1700         return v;
1701 }
1702 #else
1703 void WriteInt24_t(float dest, float val)
1704 {
1705         float v;
1706         WriteShort(dest, (v = floor(val / 256)));
1707         WriteByte(dest, val - v * 256); // 0..255
1708 }
1709 #endif
1710 #endif
1711
1712 float float2range11(float f)
1713 {
1714         // continuous function mapping all reals into -1..1
1715         return f / (fabs(f) + 1);
1716 }
1717
1718 float float2range01(float f)
1719 {
1720         // continuous function mapping all reals into 0..1
1721         return 0.5 + 0.5 * float2range11(f);
1722 }
1723
1724 // from the GNU Scientific Library
1725 float gsl_ran_gaussian_lastvalue;
1726 float gsl_ran_gaussian_lastvalue_set;
1727 float gsl_ran_gaussian(float sigma)
1728 {
1729         float a, b;
1730         if(gsl_ran_gaussian_lastvalue_set)
1731         {
1732                 gsl_ran_gaussian_lastvalue_set = 0;
1733                 return sigma * gsl_ran_gaussian_lastvalue;
1734         }
1735         else
1736         {
1737                 a = random() * 2 * M_PI;
1738                 b = sqrt(-2 * log(random()));
1739                 gsl_ran_gaussian_lastvalue = cos(a) * b;
1740                 gsl_ran_gaussian_lastvalue_set = 1;
1741                 return sigma * sin(a) * b;
1742         }
1743 }
1744
1745 string car(string s)
1746 {
1747         float o;
1748         o = strstrofs(s, " ", 0);
1749         if(o < 0)
1750                 return s;
1751         return substring(s, 0, o);
1752 }
1753 string cdr(string s)
1754 {
1755         float o;
1756         o = strstrofs(s, " ", 0);
1757         if(o < 0)
1758                 return string_null;
1759         return substring(s, o + 1, strlen(s) - (o + 1));
1760 }
1761 float matchacl(string acl, string str)
1762 {
1763         string t, s;
1764         float r, d;
1765         r = 0;
1766         while(acl)
1767         {
1768                 t = car(acl); acl = cdr(acl);
1769                 d = 1;
1770                 if(substring(t, 0, 1) == "-")
1771                 {
1772                         d = -1;
1773                         t = substring(t, 1, strlen(t) - 1);
1774                 }
1775                 else if(substring(t, 0, 1) == "+")
1776                         t = substring(t, 1, strlen(t) - 1);
1777                 if(substring(t, -1, 1) == "*")
1778                 {
1779                         t = substring(t, 0, strlen(t) - 1);
1780                         s = substring(s, 0, strlen(t));
1781                 }
1782                 else
1783                         s = str;
1784
1785                 if(s == t)
1786                 {
1787                         r = d;
1788                 }
1789         }
1790         return r;
1791 }
1792 float startsWith(string haystack, string needle)
1793 {
1794         return substring(haystack, 0, strlen(needle)) == needle;
1795 }
1796 float startsWithNocase(string haystack, string needle)
1797 {
1798         return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
1799 }
1800
1801 string get_model_datafilename(string m, float sk, string fil)
1802 {
1803         if(m)
1804                 m = strcat(m, "_");
1805         else
1806                 m = "models/player/*_";
1807         if(sk >= 0)
1808                 m = strcat(m, ftos(sk));
1809         else
1810                 m = strcat(m, "*");
1811         return strcat(m, ".", fil);
1812 }
1813
1814 float get_model_parameters(string m, float sk)
1815 {
1816         string fn, s, c;
1817         float fh;
1818
1819         get_model_parameters_modelname = string_null;
1820         get_model_parameters_modelskin = -1;
1821         get_model_parameters_name = string_null;
1822         get_model_parameters_species = -1;
1823         get_model_parameters_sex = string_null;
1824         get_model_parameters_weight = -1;
1825         get_model_parameters_age = -1;
1826         get_model_parameters_desc = string_null;
1827
1828         if not(m)
1829                 return 1;
1830         if(sk < 0)
1831         {
1832                 if(substring(m, -4, -1) != ".txt")
1833                         return 0;
1834                 if(substring(m, -6, 1) != "_")
1835                         return 0;
1836                 sk = stof(substring(m, -5, 1));
1837                 m = substring(m, 0, -7);
1838         }
1839
1840         fn = get_model_datafilename(m, sk, "txt");
1841         fh = fopen(fn, FILE_READ);
1842         if(fh < 0)
1843         {
1844                 sk = 0;
1845                 fn = get_model_datafilename(m, sk, "txt");
1846                 fh = fopen(fn, FILE_READ);
1847                 if(fh < 0)
1848                         return 0;
1849         }
1850
1851         get_model_parameters_modelname = m;
1852         get_model_parameters_modelskin = sk;
1853         while((s = fgets(fh)))
1854         {
1855                 if(s == "")
1856                         break; // next lines will be description
1857                 c = car(s);
1858                 s = cdr(s);
1859                 if(c == "name")
1860                         get_model_parameters_name = s;
1861                 if(c == "species")
1862                         switch(s)
1863                         {
1864                                 case "human":       get_model_parameters_species = SPECIES_HUMAN;       break;
1865                                 case "alien":       get_model_parameters_species = SPECIES_ALIEN;       break;
1866                                 case "robot_shiny": get_model_parameters_species = SPECIES_ROBOT_SHINY; break;
1867                                 case "robot_rusty": get_model_parameters_species = SPECIES_ROBOT_RUSTY; break;
1868                                 case "robot_solid": get_model_parameters_species = SPECIES_ROBOT_SOLID; break;
1869                                 case "animal":      get_model_parameters_species = SPECIES_ANIMAL;      break;
1870                                 case "reserved":    get_model_parameters_species = SPECIES_RESERVED;    break;
1871                         }
1872                 if(c == "sex")
1873                         get_model_parameters_sex = s;
1874                 if(c == "weight")
1875                         get_model_parameters_weight = stof(s);
1876                 if(c == "age")
1877                         get_model_parameters_age = stof(s);
1878         }
1879
1880         while((s = fgets(fh)))
1881         {
1882                 if(get_model_parameters_desc)
1883                         get_model_parameters_desc = strcat(get_model_parameters_desc, "\n");
1884                 if(s != "")
1885                         get_model_parameters_desc = strcat(get_model_parameters_desc, s);
1886         }
1887
1888         fclose(fh);
1889
1890         return 1;
1891 }
1892
1893 vector vec2(vector v)
1894 {
1895         v_z = 0;
1896         return v;
1897 }
1898
1899 #ifndef MENUQC
1900 vector NearestPointOnBox(entity box, vector org)
1901 {
1902         vector m1, m2, nearest;
1903
1904         m1 = box.mins + box.origin;
1905         m2 = box.maxs + box.origin;
1906
1907         nearest_x = bound(m1_x, org_x, m2_x);
1908         nearest_y = bound(m1_y, org_y, m2_y);
1909         nearest_z = bound(m1_z, org_z, m2_z);
1910
1911         return nearest;
1912 }
1913 #endif
1914
1915 float vercmp_recursive(string v1, string v2)
1916 {
1917         float dot1, dot2;
1918         string s1, s2;
1919         float r;
1920
1921         dot1 = strstrofs(v1, ".", 0);
1922         dot2 = strstrofs(v2, ".", 0);
1923         if(dot1 == -1)
1924                 s1 = v1;
1925         else
1926                 s1 = substring(v1, 0, dot1);
1927         if(dot2 == -1)
1928                 s2 = v2;
1929         else
1930                 s2 = substring(v2, 0, dot2);
1931
1932         r = stof(s1) - stof(s2);
1933         if(r != 0)
1934                 return r;
1935
1936         r = strcasecmp(s1, s2);
1937         if(r != 0)
1938                 return r;
1939
1940         if(dot1 == -1)
1941                 if(dot2 == -1)
1942                         return 0;
1943                 else
1944                         return -1;
1945         else
1946                 if(dot2 == -1)
1947                         return 1;
1948                 else
1949                         return vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
1950 }
1951
1952 float vercmp(string v1, string v2)
1953 {
1954         if(strcasecmp(v1, v2) == 0) // early out check
1955                 return 0;
1956
1957         // "git" beats all
1958         if(v1 == "git")
1959                 return 1;
1960         if(v2 == "git")
1961                 return -1;
1962
1963         return vercmp_recursive(v1, v2);
1964 }
1965
1966 float u8_strsize(string s)
1967 {
1968         float l, i, c;
1969         l = 0;
1970         for(i = 0; ; ++i)
1971         {
1972                 c = str2chr(s, i);
1973                 if(c <= 0)
1974                         break;
1975                 ++l;
1976                 if(c >= 0x80)
1977                         ++l;
1978                 if(c >= 0x800)
1979                         ++l;
1980                 if(c >= 0x10000)
1981                         ++l;
1982         }
1983         return l;
1984 }
1985
1986 // translation helpers
1987 string language_filename(string s)
1988 {
1989         string fn;
1990         float fh;
1991         fn = prvm_language;
1992         if(fn == "" || fn == "dump")
1993                 return s;
1994         fn = strcat(s, ".", fn);
1995         if((fh = fopen(fn, FILE_READ)) >= 0)
1996         {
1997                 fclose(fh);
1998                 return fn;
1999         }
2000         return s;
2001 }
2002 string CTX(string s)
2003 {
2004         float p = strstrofs(s, "^", 0);
2005         if(p < 0)
2006                 return s;
2007         return substring(s, p+1, -1);
2008 }