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