]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/util.qc
try working around fteqcc -r3178 bug
[divverent/nexuiz.git] / data / 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         string result;
190         string tmp;
191         float len;
192
193         // if negative, cut off the sign first
194         if(number < 0)
195                 return strcat("-", ftos_decimals(-number, decimals));
196         // it now is always positive!
197
198         // 3.516 -> 352
199         number = floor(number * pow(10, decimals) + 0.5);
200
201         // 352 -> "352"
202         result = ftos(number);
203         len = strlen(result);
204         // does it have a decimal point (should not happen)? If there is one, it is always at len-7)
205                 // if ftos had messed it up, which should never happen: "34278.000000"
206         if(len >= 7)
207                 if(substring(result, len - 7, 1) == ".")
208                 {
209                         dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
210                         result = substring(result, 0, len - 7);
211                         len -= 7;
212                 }
213                 // "34278"
214         if(decimals == 0)
215                 return result; // don't insert a point for zero decimals
216         // is it too short? If yes, insert leading zeroes
217         if(len <= decimals)
218         {
219                 result = strcat(substring("0000000000", 0, decimals - len + 1), result);
220                 len = decimals + 1;
221         }
222         // and now... INSERT THE POINT!
223         tmp = substring(result, len - decimals, decimals);
224         result = strcat(substring(result, 0, len - decimals), ".", tmp);
225         return result;
226 }
227
228 float time;
229 vector colormapPaletteColor(float c, float isPants)
230 {
231         switch(c)
232         {
233                 case  0: return '0.800000 0.800000 0.800000';
234                 case  1: return '0.600000 0.400000 0.000000';
235                 case  2: return '0.000000 1.000000 0.501961';
236                 case  3: return '0.000000 1.000000 0.000000';
237                 case  4: return '1.000000 0.000000 0.000000';
238                 case  5: return '0.000000 0.501961 1.000000';
239                 case  6: return '0.000000 1.000000 1.000000';
240                 case  7: return '0.501961 1.000000 0.000000';
241                 case  8: return '0.501961 0.000000 1.000000';
242                 case  9: return '1.000000 0.000000 1.000000';
243                 case 10: return '1.000000 0.000000 0.501961';
244                 case 11: return '0.600000 0.600000 0.600000';
245                 case 12: return '1.000000 1.000000 0.000000';
246                 case 13: return '0.000000 0.000000 1.000000';
247                 case 14: return '1.000000 0.501961 0.000000';
248                 case 15:
249                         if(isPants)
250                                 return
251                                           '1 0 0' * (0.502 + 0.498 * sin(time / 2.7182818285 + 0.0000000000))
252                                         + '0 1 0' * (0.502 + 0.498 * sin(time / 2.7182818285 + 2.0943951024))
253                                         + '0 0 1' * (0.502 + 0.498 * sin(time / 2.7182818285 + 4.1887902048));
254                         else
255                                 return
256                                           '1 0 0' * (0.502 + 0.498 * sin(time / 3.1415926536 + 5.2359877560))
257                                         + '0 1 0' * (0.502 + 0.498 * sin(time / 3.1415926536 + 3.1415926536))
258                                         + '0 0 1' * (0.502 + 0.498 * sin(time / 3.1415926536 + 1.0471975512));
259                 default: return '0.000 0.000 0.000';
260         }
261 }
262
263 // unzone the string, and return it as tempstring. Safe to be called on string_null
264 string fstrunzone(string s)
265 {
266         string sc;
267         if not(s)
268                 return s;
269         sc = strcat(s, "");
270         strunzone(s);
271         return sc;
272 }
273
274 // Databases (hash tables)
275 #define DB_BUCKETS 8192
276 void db_save(float db, string pFilename)
277 {
278         float fh, i, n;
279         fh = fopen(pFilename, FILE_WRITE);
280         if(fh < 0) 
281         {
282                 print(strcat("^1Can't write DB to ", pFilename));
283                 return;
284         }
285         n = buf_getsize(db);
286         fputs(fh, strcat(ftos(DB_BUCKETS), "\n"));
287         for(i = 0; i < n; ++i)
288                 fputs(fh, strcat(bufstr_get(db, i), "\n"));
289         fclose(fh);
290 }
291
292 float db_create()
293 {
294         return buf_create();
295 }
296
297 float db_load(string pFilename)
298 {
299         float db, fh, i, j, n;
300         string l;
301         db = buf_create();
302         if(db < 0)
303                 return -1;
304         fh = fopen(pFilename, FILE_READ);
305         if(fh < 0)
306                 return db;
307         if(stof(fgets(fh)) == DB_BUCKETS)
308         {
309                 i = 0;
310                 while((l = fgets(fh)))
311                 {
312                         if(l != "")
313                                 bufstr_set(db, i, l);
314                         ++i;
315                 }
316         }
317         else
318         {
319                 // different count of buckets?
320                 // need to reorganize the database then (SLOW)
321                 while((l = fgets(fh)))
322                 {
323                         n = tokenizebyseparator(l, "\\");
324                         for(j = 2; j < n; j += 2)
325                                 db_put(db, argv(j-1), uri_unescape(argv(j)));
326                 }
327         }
328         fclose(fh);
329         return db;
330 }
331
332 void db_dump(float db, string pFilename)
333 {
334         float fh, i, j, n, m;
335         fh = fopen(pFilename, FILE_WRITE);
336         if(fh < 0)
337                 error(strcat("Can't dump DB to ", pFilename));
338         n = buf_getsize(db);
339         fputs(fh, "0\n");
340         for(i = 0; i < n; ++i)
341         {
342                 m = tokenizebyseparator(bufstr_get(db, i), "\\");
343                 for(j = 2; j < m; j += 2)
344                         fputs(fh, strcat("\\", argv(j-1), "\\", argv(j), "\n"));
345         }
346         fclose(fh);
347 }
348
349 void db_close(float db)
350 {
351         buf_del(db);
352 }
353
354 string db_get(float db, string pKey)
355 {
356         float h;
357         h = mod(crc16(FALSE, pKey), DB_BUCKETS);
358         return uri_unescape(infoget(bufstr_get(db, h), pKey));
359 }
360
361 void db_put(float db, string pKey, string pValue)
362 {
363         float h;
364         h = mod(crc16(FALSE, pKey), DB_BUCKETS);
365         bufstr_set(db, h, infoadd(bufstr_get(db, h), pKey, uri_escape(pValue)));
366 }
367
368 void db_test()
369 {
370         float db, i;
371         print("LOAD...\n");
372         db = db_load("foo.db");
373         print("LOADED. FILL...\n");
374         for(i = 0; i < DB_BUCKETS; ++i)
375                 db_put(db, ftos(random()), "X");
376         print("FILLED. SAVE...\n");
377         db_save(db, "foo.db");
378         print("SAVED. CLOSE...\n");
379         db_close(db);
380         print("CLOSED.\n");
381 }
382
383 // Multiline text file buffers
384 float buf_load(string pFilename)
385 {
386         float buf, fh, i;
387         string l;
388         buf = buf_create();
389         if(buf < 0)
390                 return -1;
391         fh = fopen(pFilename, FILE_READ);
392         if(fh < 0)
393                 return buf;
394         i = 0;
395         while((l = fgets(fh)))
396         {
397                 bufstr_set(buf, i, l);
398                 ++i;
399         }
400         fclose(fh);
401         return buf;
402 }
403
404 void buf_save(float buf, string pFilename)
405 {
406         float fh, i, n;
407         fh = fopen(pFilename, FILE_WRITE);
408         if(fh < 0)
409                 error(strcat("Can't write buf to ", pFilename));
410         n = buf_getsize(buf);
411         for(i = 0; i < n; ++i)
412                 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
413         fclose(fh);
414 }
415
416 string GametypeNameFromType(float g)
417 {
418         if      (g == GAME_DEATHMATCH) return "dm";
419         else if (g == GAME_TEAM_DEATHMATCH) return "tdm";
420         else if (g == GAME_DOMINATION) return "dom";
421         else if (g == GAME_CTF) return "ctf";
422         else if (g == GAME_RUNEMATCH) return "rune";
423         else if (g == GAME_LMS) return "lms";
424         else if (g == GAME_ARENA) return "arena";
425         else if (g == GAME_KEYHUNT) return "kh";
426         else if (g == GAME_ONSLAUGHT) return "ons";
427         else if (g == GAME_ASSAULT) return "as";
428         else if (g == GAME_RACE) return "race";
429         else if (g == GAME_NEXBALL) return "nexball";
430         return "dm";
431 }
432
433 string mmsss(float tenths)
434 {
435         float minutes;
436         string s;
437         tenths = floor(tenths + 0.5);
438         minutes = floor(tenths / 600);
439         tenths -= minutes * 600;
440         s = ftos(1000 + tenths);
441         return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
442 }
443
444 string ScoreString(float pFlags, float pValue)
445 {
446         string valstr;
447         float l;
448
449         pValue = floor(pValue + 0.5); // round
450
451         if((pValue == 0) && (pFlags & (SFL_HIDE_ZERO | SFL_RANK | SFL_TIME)))
452                 valstr = "";
453         else if(pFlags & SFL_RANK)
454         {
455                 valstr = ftos(pValue);
456                 l = strlen(valstr);
457                 if((l >= 2) && (substring(valstr, l - 2, 1) == "1"))
458                         valstr = strcat(valstr, "th");
459                 else if(substring(valstr, l - 1, 1) == "1")
460                         valstr = strcat(valstr, "st");
461                 else if(substring(valstr, l - 1, 1) == "2")
462                         valstr = strcat(valstr, "nd");
463                 else if(substring(valstr, l - 1, 1) == "3")
464                         valstr = strcat(valstr, "rd");
465                 else
466                         valstr = strcat(valstr, "th");
467         }
468         else if(pFlags & SFL_TIME)
469                 valstr = mmsss(pValue);
470         else
471                 valstr = ftos(pValue);
472         
473         return valstr;
474 }
475
476 vector cross(vector a, vector b)
477 {
478         return
479                 '1 0 0' * (a_y * b_z - a_z * b_y)
480         +       '0 1 0' * (a_z * b_x - a_x * b_z)
481         +       '0 0 1' * (a_x * b_y - a_y * b_x);
482 }
483
484 // compressed vector format:
485 // like MD3, just even shorter
486 //   4 bit pitch (16 angles), 0 is -90, 8 is 0, 16 would be 90
487 //   5 bit yaw (32 angles), 0=0, 8=90, 16=180, 24=270
488 //   7 bit length (logarithmic encoding), 1/8 .. about 7844
489 //     length = 2^(length_encoded/8) / 8
490 // if pitch is 90, yaw does nothing and therefore indicates the sign (yaw is then either 11111 or 11110); 11111 is pointing DOWN
491 // thus, valid values are from 0000.11110.0000000 to 1111.11111.1111111
492 // the special value 0 indicates the zero vector
493
494 float lengthLogTable[128];
495
496 float invertLengthLog(float x)
497 {
498         float l, r, m, lerr, rerr;
499
500         if(x >= lengthLogTable[127])
501                 return 127;
502         if(x <= lengthLogTable[0])
503                 return 0;
504
505         l = 0;
506         r = 127;
507
508         while(r - l > 1)
509         {
510                 m = floor((l + r) / 2);
511                 if(lengthLogTable[m] < x)
512                         l = m;
513                 else
514                         r = m;
515         }
516
517         // now: r is >=, l is <
518         lerr = (x - lengthLogTable[l]);
519         rerr = (lengthLogTable[r] - x);
520         if(lerr < rerr)
521                 return l;
522         return r;
523 }
524
525 vector decompressShortVector(float data)
526 {
527         vector out;
528         float pitch, yaw, len;
529         if(data == 0)
530                 return '0 0 0';
531         pitch = (data & 0xF000) / 0x1000;
532         yaw =   (data & 0x0F80) / 0x80;
533         len =   (data & 0x007F);
534
535         //print("\ndecompress: pitch ", ftos(pitch)); print("yaw ", ftos(yaw)); print("len ", ftos(len), "\n");
536
537         if(pitch == 0)
538         {
539                 out_x = 0;
540                 out_y = 0;
541                 if(yaw == 31)
542                         out_z = -1;
543                 else
544                         out_z = +1;
545         }
546         else
547         {
548                 yaw   = .19634954084936207740 * yaw;
549                 pitch = .19634954084936207740 * pitch - 1.57079632679489661922;
550                 out_x = cos(yaw) *  cos(pitch);
551                 out_y = sin(yaw) *  cos(pitch);
552                 out_z =            -sin(pitch);
553         }
554
555         //print("decompressed: ", vtos(out), "\n");
556
557         return out * lengthLogTable[len];
558 }
559
560 float compressShortVector(vector vec)
561 {
562         vector ang;
563         float pitch, yaw, len;
564         if(vlen(vec) == 0)
565                 return 0;
566         //print("compress: ", vtos(vec), "\n");
567         ang = vectoangles(vec);
568         ang_x = -ang_x;
569         if(ang_x < -90)
570                 ang_x += 360;
571         if(ang_x < -90 && ang_x > +90)
572                 error("BOGUS vectoangles");
573         //print("angles: ", vtos(ang), "\n");
574
575         pitch = floor(0.5 + (ang_x + 90) * 16 / 180) & 15; // -90..90 to 0..14
576         if(pitch == 0)
577         {
578                 if(vec_z < 0)
579                         yaw = 31;
580                 else
581                         yaw = 30;
582         }
583         else
584                 yaw = floor(0.5 + ang_y * 32 / 360)          & 31; // 0..360 to 0..32
585         len = invertLengthLog(vlen(vec));
586
587         //print("compressed: pitch ", ftos(pitch)); print("yaw ", ftos(yaw)); print("len ", ftos(len), "\n");
588
589         return (pitch * 0x1000) + (yaw * 0x80) + len;
590 }
591
592 void compressShortVector_init()
593 {
594         float l, f, i;
595         l = 1;
596         f = pow(2, 1/8);
597         for(i = 0; i < 128; ++i)
598         {
599                 lengthLogTable[i] = l;
600                 l *= f;
601         }
602
603         if(cvar("developer"))
604         {
605                 print("Verifying vector compression table...\n");
606                 for(i = 0x0F00; i < 0xFFFF; ++i)
607                         if(i != compressShortVector(decompressShortVector(i)))
608                         {
609                                 print("BROKEN vector compression: ", ftos(i));
610                                 print(" -> ", vtos(decompressShortVector(i)));
611                                 print(" -> ", ftos(compressShortVector(decompressShortVector(i))));
612                                 print("\n");
613                                 error("b0rk");
614                         }
615                 print("Done.\n");
616         }
617 }
618
619 #ifndef MENUQC
620 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz)
621 {
622         traceline(v0, v0 + dvx, TRUE, forent); if(trace_fraction < 1) return 0;
623         traceline(v0, v0 + dvy, TRUE, forent); if(trace_fraction < 1) return 0;
624         traceline(v0, v0 + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
625         traceline(v0 + dvx, v0 + dvx + dvy, TRUE, forent); if(trace_fraction < 1) return 0;
626         traceline(v0 + dvx, v0 + dvx + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
627         traceline(v0 + dvy, v0 + dvy + dvx, TRUE, forent); if(trace_fraction < 1) return 0;
628         traceline(v0 + dvy, v0 + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
629         traceline(v0 + dvz, v0 + dvz + dvx, TRUE, forent); if(trace_fraction < 1) return 0;
630         traceline(v0 + dvz, v0 + dvz + dvy, TRUE, forent); if(trace_fraction < 1) return 0;
631         traceline(v0 + dvx + dvy, v0 + dvx + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
632         traceline(v0 + dvx + dvz, v0 + dvx + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
633         traceline(v0 + dvy + dvz, v0 + dvx + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
634         return 1;
635 }
636
637 void fixedmakevectors(vector a)
638 {
639         // a makevectors that actually inverts vectoangles
640         a_x = -a_x;
641         makevectors(a);
642 }
643 #endif
644
645 string fixPriorityList(string order, float from, float to, float subtract, float complete)
646 {
647         string neworder;
648         float i, n, w;
649
650         n = tokenize_console(order);
651         for(i = 0; i < n; ++i)
652         {
653                 w = stof(argv(i));
654                 if(w == floor(w))
655                 {
656                         if(w >= from && w <= to)
657                                 neworder = strcat(neworder, ftos(w), " ");
658                         else
659                         {
660                                 w -= subtract;
661                                 if(w >= from && w <= to)
662                                         neworder = strcat(neworder, ftos(w), " ");
663                         }
664                 }
665         }
666
667         if(complete)
668         {
669                 n = tokenize_console(neworder);
670                 for(w = to; w >= from; --w)
671                 {
672                         for(i = 0; i < n; ++i)
673                                 if(stof(argv(i)) == w)
674                                         break;
675                         if(i == n) // not found
676                                 neworder = strcat(neworder, ftos(w), " ");
677                 }
678         }
679         
680         return substring(neworder, 0, strlen(neworder) - 1);
681 }
682
683 string swapInPriorityList(string order, float i, float j)
684 {
685         string s;
686         float w, n;
687
688         n = tokenize_console(order);
689
690         if(i >= 0 && i < n && j >= 0 && j < n && i != j)
691         {
692                 s = "";
693                 for(w = 0; w < n; ++w)
694                 {
695                         if(w == i)
696                                 s = strcat(s, argv(j), " ");
697                         else if(w == j)
698                                 s = strcat(s, argv(i), " ");
699                         else
700                                 s = strcat(s, argv(w), " ");
701                 }
702                 return substring(s, 0, strlen(s) - 1);
703         }
704         
705         return order;
706 }
707
708 float cvar_value_issafe(string s)
709 {
710         if(strstrofs(s, "\"", 0) >= 0)
711                 return 0;
712         if(strstrofs(s, "\\", 0) >= 0)
713                 return 0;
714         if(strstrofs(s, ";", 0) >= 0)
715                 return 0;
716         if(strstrofs(s, "$", 0) >= 0)
717                 return 0;
718         if(strstrofs(s, "\r", 0) >= 0)
719                 return 0;
720         if(strstrofs(s, "\n", 0) >= 0)
721                 return 0;
722         return 1;
723 }
724
725 #ifndef MENUQC
726 void get_mi_min_max(float mode)
727 {
728         vector mi, ma;
729
730         if(mi_shortname)
731                 strunzone(mi_shortname);
732         mi_shortname = mapname;
733         if(!strcasecmp(substring(mi_shortname, 0, 5), "maps/"))
734                 mi_shortname = substring(mi_shortname, 5, strlen(mi_shortname) - 5);
735         if(!strcasecmp(substring(mi_shortname, strlen(mi_shortname) - 4, 4), ".bsp"))
736                 mi_shortname = substring(mi_shortname, 0, strlen(mi_shortname) - 4);
737         mi_shortname = strzone(mi_shortname);
738
739 #ifdef CSQC
740         mi = world.mins;
741         ma = world.maxs;
742 #else
743         mi = world.absmin;
744         ma = world.absmax;
745 #endif
746
747         mi_min = mi;
748         mi_max = ma;
749         MapInfo_Get_ByName(mi_shortname, 0, 0);
750         if(MapInfo_Map_mins_x < MapInfo_Map_maxs_x)
751         {
752                 mi_min = MapInfo_Map_mins;
753                 mi_max = MapInfo_Map_maxs;
754         }
755         else
756         {
757                 // not specified
758                 if(mode)
759                 {
760                         // be clever
761                         tracebox('1 0 0' * mi_x,
762                                          '0 1 0' * mi_y + '0 0 1' * mi_z,
763                                          '0 1 0' * ma_y + '0 0 1' * ma_z,
764                                          '1 0 0' * ma_x,
765                                          MOVE_WORLDONLY,
766                                          world);
767                         if(!trace_startsolid)
768                                 mi_min_x = trace_endpos_x;
769
770                         tracebox('0 1 0' * mi_y,
771                                          '1 0 0' * mi_x + '0 0 1' * mi_z,
772                                          '1 0 0' * ma_x + '0 0 1' * ma_z,
773                                          '0 1 0' * ma_y,
774                                          MOVE_WORLDONLY,
775                                          world);
776                         if(!trace_startsolid)
777                                 mi_min_y = trace_endpos_y;
778
779                         tracebox('0 0 1' * mi_z,
780                                          '1 0 0' * mi_x + '0 1 0' * mi_y,
781                                          '1 0 0' * ma_x + '0 1 0' * ma_y,
782                                          '0 0 1' * ma_z,
783                                          MOVE_WORLDONLY,
784                                          world);
785                         if(!trace_startsolid)
786                                 mi_min_z = trace_endpos_z;
787
788                         tracebox('1 0 0' * ma_x,
789                                          '0 1 0' * mi_y + '0 0 1' * mi_z,
790                                          '0 1 0' * ma_y + '0 0 1' * ma_z,
791                                          '1 0 0' * mi_x,
792                                          MOVE_WORLDONLY,
793                                          world);
794                         if(!trace_startsolid)
795                                 mi_max_x = trace_endpos_x;
796
797                         tracebox('0 1 0' * ma_y,
798                                          '1 0 0' * mi_x + '0 0 1' * mi_z,
799                                          '1 0 0' * ma_x + '0 0 1' * ma_z,
800                                          '0 1 0' * mi_y,
801                                          MOVE_WORLDONLY,
802                                          world);
803                         if(!trace_startsolid)
804                                 mi_max_y = trace_endpos_y;
805
806                         tracebox('0 0 1' * ma_z,
807                                          '1 0 0' * mi_x + '0 1 0' * mi_y,
808                                          '1 0 0' * ma_x + '0 1 0' * ma_y,
809                                          '0 0 1' * mi_z,
810                                          MOVE_WORLDONLY,
811                                          world);
812                         if(!trace_startsolid)
813                                 mi_max_z = trace_endpos_z;
814                 }
815         }
816 }
817
818 void get_mi_min_max_texcoords(float mode)
819 {
820         vector extend;
821
822         get_mi_min_max(mode);
823
824         mi_picmin = mi_min;
825         mi_picmax = mi_max;
826
827         // extend mi_picmax to get a square aspect ratio
828         // center the map in that area
829         extend = mi_picmax - mi_picmin;
830         if(extend_y > extend_x)
831         {
832                 mi_picmin_x -= (extend_y - extend_x) * 0.5;
833                 mi_picmax_x += (extend_y - extend_x) * 0.5;
834         }
835         else
836         {
837                 mi_picmin_y -= (extend_x - extend_y) * 0.5;
838                 mi_picmax_y += (extend_x - extend_y) * 0.5;
839         }
840
841         // add another some percent
842         extend = (mi_picmax - mi_picmin) * (1 / 64.0);
843         mi_picmin -= extend;
844         mi_picmax += extend;
845
846         // calculate the texcoords
847         mi_pictexcoord0 = mi_pictexcoord1 = mi_pictexcoord2 = mi_pictexcoord3 = '0 0 0';
848         // first the two corners of the origin
849         mi_pictexcoord0_x = (mi_min_x - mi_picmin_x) / (mi_picmax_x - mi_picmin_x);
850         mi_pictexcoord0_y = (mi_min_y - mi_picmin_y) / (mi_picmax_y - mi_picmin_y);
851         mi_pictexcoord2_x = (mi_max_x - mi_picmin_x) / (mi_picmax_x - mi_picmin_x);
852         mi_pictexcoord2_y = (mi_max_y - mi_picmin_y) / (mi_picmax_y - mi_picmin_y);
853         // then the other corners
854         mi_pictexcoord1_x = mi_pictexcoord0_x;
855         mi_pictexcoord1_y = mi_pictexcoord2_y;
856         mi_pictexcoord3_x = mi_pictexcoord2_x;
857         mi_pictexcoord3_y = mi_pictexcoord0_y;
858 }
859 #endif
860
861 #ifdef CSQC
862 void cvar_settemp(string pKey, string pValue)
863 {
864         error("cvar_settemp called from CSQC - use cvar_clientsettemp instead!");
865 }
866 void cvar_settemp_restore()
867 {
868         error("cvar_settemp_restore called from CSQC - use cvar_clientsettemp instead!");
869 }
870 #else
871 void cvar_settemp(string pKey, string pValue)
872 {
873         cvar_set("settemp_list", strcat("1 ", pKey, " ", cvar_string("settemp_var"), " ", cvar_string("settemp_list")));
874 #ifdef MENUQC
875         registercvar(cvar_string("settemp_var"), "", 0);
876 #else
877         registercvar(cvar_string("settemp_var"), "");
878 #endif
879         cvar_set(cvar_string("settemp_var"), cvar_string(pKey));
880         cvar_set("settemp_var", strcat(cvar_string("settemp_var"), "x"));
881         cvar_set(pKey, pValue);
882 }
883
884 void cvar_settemp_restore()
885 {
886         // undo what cvar_settemp did
887         string s1, s2;
888         float n, i;
889         n = tokenize_console(cvar_string("settemp_list"));
890         for(i = 0; i < n - 3; i += 3)
891         {
892                 s1 = argv(i + 1);
893                 s2 = argv(i + 2);
894                 cvar_set(s1, s2); // fteqcc sucks
895         }
896         cvar_set("settemp_list", "0");
897 }
898 #endif
899
900 float almost_equals(float a, float b)
901 {
902         float eps;
903         eps = (max(a, -a) + max(b, -b)) * 0.001;
904         if(a - b < eps && b - a < eps)
905                 return TRUE;
906         return FALSE;
907 }
908
909 float almost_in_bounds(float a, float b, float c)
910 {
911         float eps;
912         eps = (max(a, -a) + max(c, -c)) * 0.001;
913         return b == median(a - eps, b, c + eps);
914 }
915
916 float power2of(float e)
917 {
918         return pow(2, e);
919 }
920 float log2of(float x)
921 {
922         // NOTE: generated code
923         if(x > 2048)
924                 if(x > 131072)
925                         if(x > 1048576)
926                                 if(x > 4194304)
927                                         return 23;
928                                 else
929                                         if(x > 2097152)
930                                                 return 22;
931                                         else
932                                                 return 21;
933                         else
934                                 if(x > 524288)
935                                         return 20;
936                                 else
937                                         if(x > 262144)
938                                                 return 19;
939                                         else
940                                                 return 18;
941                 else
942                         if(x > 16384)
943                                 if(x > 65536)
944                                         return 17;
945                                 else
946                                         if(x > 32768)
947                                                 return 16;
948                                         else
949                                                 return 15;
950                         else
951                                 if(x > 8192)
952                                         return 14;
953                                 else
954                                         if(x > 4096)
955                                                 return 13;
956                                         else
957                                                 return 12;
958         else
959                 if(x > 32)
960                         if(x > 256)
961                                 if(x > 1024)
962                                         return 11;
963                                 else
964                                         if(x > 512)
965                                                 return 10;
966                                         else
967                                                 return 9;
968                         else
969                                 if(x > 128)
970                                         return 8;
971                                 else
972                                         if(x > 64)
973                                                 return 7;
974                                         else
975                                                 return 6;
976                 else
977                         if(x > 4)
978                                 if(x > 16)
979                                         return 5;
980                                 else
981                                         if(x > 8)
982                                                 return 4;
983                                         else
984                                                 return 3;
985                         else
986                                 if(x > 2)
987                                         return 2;
988                                 else
989                                         if(x > 1)
990                                                 return 1;
991                                         else
992                                                 return 0;
993 }
994
995 float rgb_mi_ma_to_hue(vector rgb, float mi, float ma)
996 {
997         if(mi == ma)
998                 return 0;
999         else if(ma == rgb_x)
1000         {
1001                 if(rgb_y >= rgb_z)
1002                         return (rgb_y - rgb_z) / (ma - mi);
1003                 else
1004                         return (rgb_y - rgb_z) / (ma - mi) + 6;
1005         }
1006         else if(ma == rgb_y)
1007                 return (rgb_z - rgb_x) / (ma - mi) + 2;
1008         else // if(ma == rgb_z)
1009                 return (rgb_x - rgb_y) / (ma - mi) + 4;
1010 }
1011
1012 vector hue_mi_ma_to_rgb(float hue, float mi, float ma)
1013 {
1014         vector rgb;
1015
1016         hue -= 6 * floor(hue / 6);
1017
1018         //else if(ma == rgb_x)
1019         //      hue = 60 * (rgb_y - rgb_z) / (ma - mi);
1020         if(hue <= 1)
1021         {
1022                 rgb_x = ma;
1023                 rgb_y = hue * (ma - mi) + mi;
1024                 rgb_z = mi;
1025         }
1026         //else if(ma == rgb_y)
1027         //      hue = 60 * (rgb_z - rgb_x) / (ma - mi) + 120;
1028         else if(hue <= 2)
1029         {
1030                 rgb_x = (2 - hue) * (ma - mi) + mi;
1031                 rgb_y = ma;
1032                 rgb_z = mi;
1033         }
1034         else if(hue <= 3)
1035         {
1036                 rgb_x = mi;
1037                 rgb_y = ma;
1038                 rgb_z = (hue - 2) * (ma - mi) + mi;
1039         }
1040         //else // if(ma == rgb_z)
1041         //      hue = 60 * (rgb_x - rgb_y) / (ma - mi) + 240;
1042         else if(hue <= 4)
1043         {
1044                 rgb_x = mi;
1045                 rgb_y = (4 - hue) * (ma - mi) + mi;
1046                 rgb_z = ma;
1047         }
1048         else if(hue <= 5)
1049         {
1050                 rgb_x = (hue - 4) * (ma - mi) + mi;
1051                 rgb_y = mi;
1052                 rgb_z = ma;
1053         }
1054         //else if(ma == rgb_x)
1055         //      hue = 60 * (rgb_y - rgb_z) / (ma - mi);
1056         else // if(hue <= 6)
1057         {
1058                 rgb_x = ma;
1059                 rgb_y = mi;
1060                 rgb_z = (6 - hue) * (ma - mi) + mi;
1061         }
1062
1063         return rgb;
1064 }
1065
1066 vector rgb_to_hsv(vector rgb)
1067 {
1068         float mi, ma;
1069         vector hsv;
1070
1071         mi = min3(rgb_x, rgb_y, rgb_z);
1072         ma = max3(rgb_x, rgb_y, rgb_z);
1073
1074         hsv_x = rgb_mi_ma_to_hue(rgb, mi, ma);
1075         hsv_z = ma;
1076
1077         if(ma == 0)
1078                 hsv_y = 0;
1079         else
1080                 hsv_y = 1 - mi/ma;
1081         
1082         return hsv;
1083 }
1084
1085 vector hsv_to_rgb(vector hsv)
1086 {
1087         return hue_mi_ma_to_rgb(hsv_x, hsv_z * (1 - hsv_y), hsv_z);
1088 }
1089
1090 vector rgb_to_hsl(vector rgb)
1091 {
1092         float mi, ma;
1093         vector hsl;
1094
1095         mi = min3(rgb_x, rgb_y, rgb_z);
1096         ma = max3(rgb_x, rgb_y, rgb_z);
1097
1098         hsl_x = rgb_mi_ma_to_hue(rgb, mi, ma);
1099         
1100         hsl_z = 0.5 * (mi + ma);
1101         if(mi == ma)
1102                 hsl_y = 0;
1103         else if(hsl_z <= 0.5)
1104                 hsl_y = (ma - mi) / (2*hsl_z);
1105         else // if(hsl_z > 0.5)
1106                 hsl_y = (ma - mi) / (2 - 2*hsl_z);
1107         
1108         return hsl;
1109 }
1110
1111 vector hsl_to_rgb(vector hsl)
1112 {
1113         float mi, ma, maminusmi;
1114
1115         if(hsl_z <= 0.5)
1116                 maminusmi = hsl_y * 2 * hsl_z;
1117         else
1118                 maminusmi = hsl_y * (2 - 2 * hsl_z);
1119         
1120         // hsl_z     = 0.5 * mi + 0.5 * ma
1121         // maminusmi =     - mi +       ma
1122         mi = hsl_z - 0.5 * maminusmi;
1123         ma = hsl_z + 0.5 * maminusmi;
1124
1125         return hue_mi_ma_to_rgb(hsl_x, mi, ma);
1126 }
1127
1128 string rgb_to_hexcolor(vector rgb)
1129 {
1130         return
1131                 strcat(
1132                         "^x",
1133                         DEC_TO_HEXDIGIT(floor(rgb_x * 15 + 0.5)),
1134                         DEC_TO_HEXDIGIT(floor(rgb_y * 15 + 0.5)),
1135                         DEC_TO_HEXDIGIT(floor(rgb_z * 15 + 0.5))
1136                 );
1137 }
1138
1139 // requires that m2>m1 in all coordinates, and that m4>m3
1140 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;};
1141
1142 // requires the same, but is a stronger condition
1143 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;};
1144
1145 #ifndef MENUQC
1146 // angles transforms
1147 // angles in fixedmakevectors/fixedvectoangles space
1148 vector AnglesTransform_Apply(vector transform, vector v)
1149 {
1150         fixedmakevectors(transform);
1151         return v_forward * v_x
1152              + v_right   * (-v_y)
1153                  + v_up      * v_z;
1154 }
1155
1156 vector AnglesTransform_Multiply(vector t1, vector t2)
1157 {
1158         vector m_forward, m_up;
1159         fixedmakevectors(t2); m_forward = v_forward; m_up = v_up;
1160         m_forward = AnglesTransform_Apply(t1, m_forward); m_up = AnglesTransform_Apply(t1, m_up);
1161         return fixedvectoangles2(m_forward, m_up);
1162 }
1163
1164 vector AnglesTransform_Invert(vector transform)
1165 {
1166         vector i_forward, i_up;
1167         fixedmakevectors(transform);
1168         // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
1169         // but these are orthogonal unit vectors!
1170         // so to invert, we can simply fixedvectoangles the TRANSPOSED matrix
1171         // TODO is this always -transform?
1172         i_forward_x = v_forward_x;
1173         i_forward_y = -v_right_x;
1174         i_forward_z = v_up_x;
1175         i_up_x = v_forward_z;
1176         i_up_y = -v_right_z;
1177         i_up_z = v_up_z;
1178         return fixedvectoangles2(i_forward, i_up);
1179 }
1180
1181 vector AnglesTransform_TurnDirection(vector transform)
1182 {
1183         // turn 180 degrees around v_up
1184         // changes in-direction to out-direction
1185         fixedmakevectors(transform);
1186         return fixedvectoangles2(-1 * v_forward, 1 * v_up);
1187 }
1188
1189 vector AnglesTransform_Divide(vector to_transform, vector from_transform)
1190 {
1191         return AnglesTransform_Multiply(to_transform, AnglesTransform_Invert(from_transform));
1192 }
1193 #endif
1194
1195 float textLengthUpToWidth(string theText, float maxWidth, textLengthUpToWidth_widthFunction_t w)
1196 {
1197         float ICanHasKallerz;
1198
1199         // detect color codes support in the width function
1200         ICanHasKallerz = (w("^7") == 0);
1201
1202         // STOP.
1203         // The following function is SLOW.
1204         // For your safety and for the protection of those around you...
1205         // DO NOT CALL THIS AT HOME.
1206         // No really, don't.
1207         if(w(theText) <= maxWidth)
1208                 return strlen(theText); // yeah!
1209
1210         // binary search for right place to cut string
1211         float ch;
1212         float left, right, middle; // this always works
1213         left = 0;
1214         right = strlen(theText); // this always fails
1215         do
1216         {
1217                 middle = floor((left + right) / 2);
1218                 if(w(substring(theText, 0, middle)) <= maxWidth)
1219                         left = middle;
1220                 else
1221                         right = middle;
1222         }
1223         while(left < right - 1);
1224
1225         if(ICanHasKallerz)
1226         {
1227                 // NOTE: when color codes are involved, this binary search is,
1228                 // mathematically, BROKEN. However, it is obviously guaranteed to
1229                 // terminate, as the range still halves each time - but nevertheless, it is
1230                 // guaranteed that it finds ONE valid cutoff place (where "left" is in
1231                 // range, and "right" is outside).
1232                 
1233                 // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4)
1234                 // and decrease left on the basis of the chars detected of the truncated tag
1235                 // Even if the ^xrgb tag is not complete/correct, left is decreased
1236                 // (sometimes too much but with a correct result)
1237                 // it fixes also ^[0-9]
1238                 while(left >= 1 && substring(theText, left-1, 1) == "^")
1239                         left-=1;
1240
1241                 if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/
1242                         left-=2;
1243                 else if (left >= 3 && substring(theText, left-3, 2) == "^x")
1244                         {
1245                                 ch = str2chr(theText, left-1);
1246                                 if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/
1247                                         left-=3;
1248                         }
1249                 else if (left >= 4 && substring(theText, left-4, 2) == "^x")
1250                         {
1251                                 ch = str2chr(theText, left-2);
1252                                 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
1253                                 {
1254                                         ch = str2chr(theText, left-1);
1255                                         if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/
1256                                                 left-=4;
1257                                 }
1258                         }
1259         }
1260         
1261         return left;
1262 }
1263
1264 string getWrappedLine(float w, textLengthUpToWidth_widthFunction_t tw)
1265 {
1266         float cantake;
1267         float take;
1268         string s;
1269
1270         s = getWrappedLine_remaining;
1271
1272         cantake = textLengthUpToWidth(s, w, tw);
1273         if(cantake > 0 && cantake < strlen(s))
1274         {
1275                 take = cantake - 1;
1276                 while(take > 0 && substring(s, take, 1) != " ")
1277                         --take;
1278                 if(take == 0)
1279                 {
1280                         getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
1281                         if(getWrappedLine_remaining == "")
1282                                 getWrappedLine_remaining = string_null;
1283                         return substring(s, 0, cantake);
1284                 }
1285                 else
1286                 {
1287                         getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
1288                         if(getWrappedLine_remaining == "")
1289                                 getWrappedLine_remaining = string_null;
1290                         return substring(s, 0, take);
1291                 }
1292         }
1293         else
1294         {
1295                 getWrappedLine_remaining = string_null;
1296                 return s;
1297         }
1298 }
1299
1300 string textShortenToWidth(string theText, float maxWidth, textLengthUpToWidth_widthFunction_t tw)
1301 {
1302         if(tw(theText) <= maxWidth)
1303                 return theText;
1304         else
1305                 return strcat(substring(theText, 0, textLengthUpToWidth(theText, maxWidth - tw("..."), tw)), "...");
1306 }
1307
1308 float isGametypeInFilter(float gt, float tp, string pattern)
1309 {
1310         string subpattern, subpattern2;
1311         subpattern = strcat(",", GametypeNameFromType(gt), ",");
1312         if(tp)
1313                 subpattern2 = ",teams,";
1314         else
1315                 subpattern2 = ",noteams,";
1316
1317         if(substring(pattern, 0, 1) == "-")
1318         {
1319                 pattern = substring(pattern, 1, strlen(pattern) - 1);
1320                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) >= 0)
1321                         return 0;
1322                 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) >= 0)
1323                         return 0;
1324         }
1325         else
1326         {
1327                 if(substring(pattern, 0, 1) == "+")
1328                         pattern = substring(pattern, 1, strlen(pattern) - 1);
1329                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) < 0)
1330                 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) < 0)
1331                         return 0;
1332         }
1333         return 1;
1334 }
1335
1336 void shuffle(float n, shuffle_swapfunc_t swap)
1337 {
1338         float i, j;
1339         for(i = 1; i < n; ++i)
1340         {
1341                 // swap i-th item at a random position from 0 to i
1342                 // proof for even distribution:
1343                 //   n = 1: obvious
1344                 //   n -> n+1:
1345                 //     item n+1 gets at any position with chance 1/(n+1)
1346                 //     all others will get their 1/n chance reduced by factor n/(n+1)
1347                 //     to be on place n+1, their chance will be 1/(n+1)
1348                 //     1/n * n/(n+1) = 1/(n+1)
1349                 //     q.e.d.
1350                 j = floor(random() * (i + 1));
1351                 if(j != i)
1352                         swap(j, i);
1353         }
1354 }
1355
1356 string substring_range(string s, float b, float e)
1357 {
1358         return substring(s, b, e - b);
1359 }
1360
1361 string swapwords(string str, float i, float j)
1362 {
1363         float n;
1364         string s1, s2, s3, s4, s5;
1365         float si, ei, sj, ej, s0, en;
1366         n = tokenizebyseparator(str, " "); // must match g_maplist processing in ShuffleMaplist and "shuffle"
1367         si = argv_start_index(i);
1368         sj = argv_start_index(j);
1369         ei = argv_end_index(i);
1370         ej = argv_end_index(j);
1371         s0 = argv_start_index(0);
1372         en = argv_end_index(n-1);
1373         s1 = substring_range(str, s0, si);
1374         s2 = substring_range(str, si, ei);
1375         s3 = substring_range(str, ei, sj);
1376         s4 = substring_range(str, sj, ej);
1377         s5 = substring_range(str, ej, en);
1378         return strcat(s1, s4, s3, s2, s5);
1379 }
1380
1381 string _shufflewords_str;
1382 void _shufflewords_swapfunc(float i, float j)
1383 {
1384         _shufflewords_str = swapwords(_shufflewords_str, i, j);
1385 }
1386 string shufflewords(string str)
1387 {
1388         float n;
1389         _shufflewords_str = str;
1390         n = tokenizebyseparator(str, " ");
1391         shuffle(n, _shufflewords_swapfunc);
1392         str = _shufflewords_str;
1393         _shufflewords_str = string_null;
1394         return str;
1395 }
1396
1397 vector solve_quadratic(float a, float b, float c) // ax^2 + bx + c = 0
1398 {
1399         vector v;
1400         float D;
1401         v = '0 0 0';
1402         if(a == 0)
1403         {
1404                 if(b != 0)
1405                 {
1406                         v_x = v_y = -c / b;
1407                         v_z = 1;
1408                 }
1409                 else
1410                 {
1411                         if(c == 0)
1412                         {
1413                                 // actually, every number solves the equation!
1414                                 v_z = 1;
1415                         }
1416                 }
1417         }
1418         else
1419         {
1420                 D = b*b - 4*a*c;
1421                 if(D >= 0)
1422                 {
1423                         D = sqrt(D);
1424                         if(a > 0) // put the smaller solution first
1425                         {
1426                                 v_x = ((-b)-D) / (2*a);
1427                                 v_y = ((-b)+D) / (2*a);
1428                         }
1429                         else
1430                         {
1431                                 v_x = (-b+D) / (2*a);
1432                                 v_y = (-b-D) / (2*a);
1433                         }
1434                         v_z = 1;
1435                 }
1436                 else
1437                 {
1438                         // complex solutions!
1439                         D = sqrt(-D);
1440                         v_x = -b / (2*a);
1441                         if(a > 0)
1442                                 v_y =  D / (2*a);
1443                         else
1444                                 v_y = -D / (2*a);
1445                         v_z = 0;
1446                 }
1447         }
1448         return v;
1449 }
1450
1451
1452 float _unacceptable_compiler_bug_1_a(float b, float c) { return b == c; }
1453 float _unacceptable_compiler_bug_1_b() { return 1; }
1454 float _unacceptable_compiler_bug_1_c(float d) { return 2 * d; }
1455 float _unacceptable_compiler_bug_1_d() { return 1; }
1456
1457 void check_unacceptable_compiler_bugs()
1458 {
1459         if(cvar("_allow_unacceptable_compiler_bugs"))
1460                 return;
1461         tokenize_console("foo bar");
1462         if(strcat(argv(0), substring("foo bar", 4, 7 - argv_start_index(1))) == "barbar")
1463                 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.");
1464 }