]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/util.qc
warning fix; fix loading ons-reborn.mapinfo
[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 fucked 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         return "dm";
430 }
431
432 string mmsss(float tenths)
433 {
434         float minutes;
435         string s;
436         tenths = floor(tenths + 0.5);
437         minutes = floor(tenths / 600);
438         tenths -= minutes * 600;
439         s = ftos(1000 + tenths);
440         return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
441 }
442
443 string ScoreString(float pFlags, float pValue)
444 {
445         string valstr;
446         float l;
447
448         pValue = floor(pValue + 0.5); // round
449
450         if((pValue == 0) && (pFlags & (SFL_HIDE_ZERO | SFL_RANK | SFL_TIME)))
451                 valstr = "";
452         else if(pFlags & SFL_RANK)
453         {
454                 valstr = ftos(pValue);
455                 l = strlen(valstr);
456                 if((l >= 2) && (substring(valstr, l - 2, 1) == "1"))
457                         valstr = strcat(valstr, "th");
458                 else if(substring(valstr, l - 1, 1) == "1")
459                         valstr = strcat(valstr, "st");
460                 else if(substring(valstr, l - 1, 1) == "2")
461                         valstr = strcat(valstr, "nd");
462                 else if(substring(valstr, l - 1, 1) == "3")
463                         valstr = strcat(valstr, "rd");
464                 else
465                         valstr = strcat(valstr, "th");
466         }
467         else if(pFlags & SFL_TIME)
468                 valstr = mmsss(pValue);
469         else
470                 valstr = ftos(pValue);
471         
472         return valstr;
473 }
474
475 vector cross(vector a, vector b)
476 {
477         return
478                 '1 0 0' * (a_y * b_z - a_z * b_y)
479         +       '0 1 0' * (a_z * b_x - a_x * b_z)
480         +       '0 0 1' * (a_x * b_y - a_y * b_x);
481 }
482
483 // compressed vector format:
484 // like MD3, just even shorter
485 //   4 bit pitch (16 angles), 0 is -90, 8 is 0, 16 would be 90
486 //   5 bit yaw (32 angles), 0=0, 8=90, 16=180, 24=270
487 //   7 bit length (logarithmic encoding), 1/8 .. about 7844
488 //     length = 2^(length_encoded/8) / 8
489 // if pitch is 90, yaw does nothing and therefore indicates the sign (yaw is then either 11111 or 11110); 11111 is pointing DOWN
490 // thus, valid values are from 0000.11110.0000000 to 1111.11111.1111111
491 // the special value 0 indicates the zero vector
492
493 float lengthLogTable[128];
494
495 float invertLengthLog(float x)
496 {
497         float l, r, m, lerr, rerr;
498
499         if(x >= lengthLogTable[127])
500                 return 127;
501         if(x <= lengthLogTable[0])
502                 return 0;
503
504         l = 0;
505         r = 127;
506
507         while(r - l > 1)
508         {
509                 m = floor((l + r) / 2);
510                 if(lengthLogTable[m] < x)
511                         l = m;
512                 else
513                         r = m;
514         }
515
516         // now: r is >=, l is <
517         lerr = (x - lengthLogTable[l]);
518         rerr = (lengthLogTable[r] - x);
519         if(lerr < rerr)
520                 return l;
521         return r;
522 }
523
524 vector decompressShortVector(float data)
525 {
526         vector out;
527         float pitch, yaw, len;
528         if(data == 0)
529                 return '0 0 0';
530         pitch = (data & 0xF000) / 0x1000;
531         yaw =   (data & 0x0F80) / 0x80;
532         len =   (data & 0x007F);
533
534         //print("\ndecompress: pitch ", ftos(pitch)); print("yaw ", ftos(yaw)); print("len ", ftos(len), "\n");
535
536         if(pitch == 0)
537         {
538                 out_x = 0;
539                 out_y = 0;
540                 if(yaw == 31)
541                         out_z = -1;
542                 else
543                         out_z = +1;
544         }
545         else
546         {
547                 yaw   = .19634954084936207740 * yaw;
548                 pitch = .19634954084936207740 * pitch - 1.57079632679489661922;
549                 out_x = cos(yaw) *  cos(pitch);
550                 out_y = sin(yaw) *  cos(pitch);
551                 out_z =            -sin(pitch);
552         }
553
554         //print("decompressed: ", vtos(out), "\n");
555
556         return out * lengthLogTable[len];
557 }
558
559 float compressShortVector(vector vec)
560 {
561         vector ang;
562         float pitch, yaw, len;
563         if(vlen(vec) == 0)
564                 return 0;
565         //print("compress: ", vtos(vec), "\n");
566         ang = vectoangles(vec);
567         ang_x = -ang_x;
568         if(ang_x < -90)
569                 ang_x += 360;
570         if(ang_x < -90 && ang_x > +90)
571                 error("BOGUS vectoangles");
572         //print("angles: ", vtos(ang), "\n");
573
574         pitch = floor(0.5 + (ang_x + 90) * 16 / 180) & 15; // -90..90 to 0..14
575         if(pitch == 0)
576         {
577                 if(vec_z < 0)
578                         yaw = 31;
579                 else
580                         yaw = 30;
581         }
582         else
583                 yaw = floor(0.5 + ang_y * 32 / 360)          & 31; // 0..360 to 0..32
584         len = invertLengthLog(vlen(vec));
585
586         //print("compressed: pitch ", ftos(pitch)); print("yaw ", ftos(yaw)); print("len ", ftos(len), "\n");
587
588         return (pitch * 0x1000) + (yaw * 0x80) + len;
589 }
590
591 void compressShortVector_init()
592 {
593         float l, f, i;
594         l = 1;
595         f = pow(2, 1/8);
596         for(i = 0; i < 128; ++i)
597         {
598                 lengthLogTable[i] = l;
599                 l *= f;
600         }
601
602         if(cvar("developer"))
603         {
604                 print("Verifying vector compression table...\n");
605                 for(i = 0x0F00; i < 0xFFFF; ++i)
606                         if(i != compressShortVector(decompressShortVector(i)))
607                         {
608                                 print("BROKEN vector compression: ", ftos(i));
609                                 print(" -> ", vtos(decompressShortVector(i)));
610                                 print(" -> ", ftos(compressShortVector(decompressShortVector(i))));
611                                 print("\n");
612                                 error("b0rk");
613                         }
614                 print("Done.\n");
615         }
616 }
617
618 #ifndef MENUQC
619 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz)
620 {
621         traceline(v0, v0 + dvx, TRUE, forent); if(trace_fraction < 1) return 0;
622         traceline(v0, v0 + dvy, TRUE, forent); if(trace_fraction < 1) return 0;
623         traceline(v0, v0 + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
624         traceline(v0 + dvx, v0 + dvx + dvy, TRUE, forent); if(trace_fraction < 1) return 0;
625         traceline(v0 + dvx, v0 + dvx + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
626         traceline(v0 + dvy, v0 + dvy + dvx, TRUE, forent); if(trace_fraction < 1) return 0;
627         traceline(v0 + dvy, v0 + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
628         traceline(v0 + dvz, v0 + dvz + dvx, TRUE, forent); if(trace_fraction < 1) return 0;
629         traceline(v0 + dvz, v0 + dvz + dvy, TRUE, forent); if(trace_fraction < 1) return 0;
630         traceline(v0 + dvx + dvy, v0 + dvx + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
631         traceline(v0 + dvx + dvz, v0 + dvx + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
632         traceline(v0 + dvy + dvz, v0 + dvx + dvy + dvz, TRUE, forent); if(trace_fraction < 1) return 0;
633         return 1;
634 }
635
636 void fixedmakevectors(vector a)
637 {
638         // a makevectors that actually inverts vectoangles
639         a_x = -a_x;
640         makevectors(a);
641 }
642 #endif
643
644 string fixPriorityList(string order, float from, float to, float complete)
645 {
646         string neworder;
647         float i, n, w;
648
649         n = tokenize(order);
650         for(i = 0; i < n; ++i)
651         {
652                 w = stof(argv(i));
653                 if(w >= from && w <= to && w == floor(w))
654                         neworder = strcat(neworder, ftos(w), " ");
655         }
656
657         if(complete)
658         {
659                 n = tokenize(neworder);
660                 for(w = to; w >= from; --w)
661                 {
662                         for(i = 0; i < n; ++i)
663                                 if(stof(argv(i)) == w)
664                                         break;
665                         if(i == n) // not found
666                                 neworder = strcat(neworder, ftos(w), " ");
667                 }
668         }
669         
670         return substring(neworder, 0, strlen(neworder) - 1);
671 }
672
673 string swapInPriorityList(string order, float i, float j)
674 {
675         string s;
676         float w, n;
677
678         n = tokenize(order);
679
680         if(i >= 0 && i < n && j >= 0 && j < n && i != j)
681         {
682                 s = "";
683                 for(w = 0; w < n; ++w)
684                 {
685                         if(w == i)
686                                 s = strcat(s, argv(j), " ");
687                         else if(w == j)
688                                 s = strcat(s, argv(i), " ");
689                         else
690                                 s = strcat(s, argv(w), " ");
691                 }
692                 return substring(s, 0, strlen(s) - 1);
693         }
694         
695         return order;
696 }
697
698 float cvar_value_issafe(string s)
699 {
700         if(strstrofs(s, "\"", 0) >= 0)
701                 return 0;
702         if(strstrofs(s, "\\", 0) >= 0)
703                 return 0;
704         if(strstrofs(s, ";", 0) >= 0)
705                 return 0;
706         if(strstrofs(s, "$", 0) >= 0)
707                 return 0;
708         if(strstrofs(s, "\r", 0) >= 0)
709                 return 0;
710         if(strstrofs(s, "\n", 0) >= 0)
711                 return 0;
712         return 1;
713 }
714
715 #ifndef MENUQC
716 void get_mi_min_max()
717 {
718         vector extend;
719
720         if(mi_shortname)
721                 strunzone(mi_shortname);
722         mi_shortname = mapname;
723         if(!strcasecmp(substring(mi_shortname, 0, 5), "maps/"))
724                 mi_shortname = substring(mi_shortname, 5, strlen(mi_shortname) - 5);
725         if(!strcasecmp(substring(mi_shortname, strlen(mi_shortname) - 4, 4), ".bsp"))
726                 mi_shortname = substring(mi_shortname, 0, strlen(mi_shortname) - 4);
727         mi_shortname = strzone(mi_shortname);
728
729         mi_min = world.mins;
730         mi_max = world.maxs;
731         MapInfo_Get_ByName(mi_shortname, 0, 0);
732         if(MapInfo_Map_mins_x < MapInfo_Map_maxs_x)
733         {
734                 mi_min = MapInfo_Map_mins;
735                 mi_max = MapInfo_Map_maxs;
736         }
737
738         mi_picmin = mi_min;
739         mi_picmax = mi_max;
740
741         // extend mi_picmax to get a square aspect ratio
742         // center the map in that area
743         extend = mi_picmax - mi_picmin;
744         if(extend_y > extend_x)
745         {
746                 mi_picmin_x -= (extend_y - extend_x) * 0.5;
747                 mi_picmax_x += (extend_y - extend_x) * 0.5;
748         }
749         else
750         {
751                 mi_picmin_y -= (extend_x - extend_y) * 0.5;
752                 mi_picmax_y += (extend_x - extend_y) * 0.5;
753         }
754
755         // add another some percent
756         extend = (mi_picmax - mi_picmin) * (1 / 64.0);
757         mi_picmin -= extend;
758         mi_picmax += extend;
759
760         // calculate the texcoords
761         mi_pictexcoord0 = mi_pictexcoord1 = mi_pictexcoord2 = mi_pictexcoord3 = '0 0 0';
762         // first the two corners of the origin
763         mi_pictexcoord0_x = (mi_min_x - mi_picmin_x) / (mi_picmax_x - mi_picmin_x);
764         mi_pictexcoord0_y = (mi_min_y - mi_picmin_y) / (mi_picmax_y - mi_picmin_y);
765         mi_pictexcoord2_x = (mi_max_x - mi_picmin_x) / (mi_picmax_x - mi_picmin_x);
766         mi_pictexcoord2_y = (mi_max_y - mi_picmin_y) / (mi_picmax_y - mi_picmin_y);
767         // then the other corners
768         mi_pictexcoord1_x = mi_pictexcoord0_x;
769         mi_pictexcoord1_y = mi_pictexcoord2_y;
770         mi_pictexcoord3_x = mi_pictexcoord2_x;
771         mi_pictexcoord3_y = mi_pictexcoord0_y;
772 }
773 #endif
774
775 #ifdef CSQC
776 void cvar_settemp(string pKey, string pValue)
777 {
778         error("cvar_settemp called from CSQC - use cvar_clientsettemp instead!");
779 }
780 void cvar_settemp_restore()
781 {
782         error("cvar_settemp_restore called from CSQC - use cvar_clientsettemp instead!");
783 }
784 #else
785 void cvar_settemp(string pKey, string pValue)
786 {
787         cvar_set("settemp_list", strcat("1 ", pKey, " ", cvar_string("settemp_var"), " ", cvar_string("settemp_list")));
788 #ifdef MENUQC
789         registercvar(cvar_string("settemp_var"), "", 0);
790 #else
791         registercvar(cvar_string("settemp_var"), "");
792 #endif
793         cvar_set(cvar_string("settemp_var"), cvar_string(pKey));
794         cvar_set("settemp_var", strcat(cvar_string("settemp_var"), "x"));
795         cvar_set(pKey, pValue);
796 }
797
798 void cvar_settemp_restore()
799 {
800         // undo what cvar_settemp did
801         float n, i;
802         n = tokenize(cvar_string("settemp_list"));
803         for(i = 0; i < n - 3; i += 3)
804                 cvar_set(argv(i + 1), cvar_string(argv(i + 2)));
805         cvar_set("settemp_list", "0");
806 }
807 #endif