]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/util.qc
9b6d433621066cd36349d1ae793f9ece062d3cd7
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qc
1 #include "util.qh"
2
3 #if defined(CSQC)
4     #include "../client/defs.qh"
5     #include "constants.qh"
6         #include "../client/mutators/events.qh"
7     #include "mapinfo.qh"
8     #include "notifications.qh"
9     #include "deathtypes/all.qh"
10 #elif defined(MENUQC)
11 #elif defined(SVQC)
12     #include "constants.qh"
13     #include "../server/autocvars.qh"
14     #include "../server/defs.qh"
15         #include "../server/mutators/events.qh"
16     #include "notifications.qh"
17     #include "deathtypes/all.qh"
18     #include "mapinfo.qh"
19 #endif
20
21 string wordwrap_buffer;
22
23 void wordwrap_buffer_put(string s)
24 {
25         wordwrap_buffer = strcat(wordwrap_buffer, s);
26 }
27
28 string wordwrap(string s, float l)
29 {
30         string r;
31         wordwrap_buffer = "";
32         wordwrap_cb(s, l, wordwrap_buffer_put);
33         r = wordwrap_buffer;
34         wordwrap_buffer = "";
35         return r;
36 }
37
38 #ifndef MENUQC
39 #ifndef CSQC
40 void wordwrap_buffer_sprint(string s)
41 {SELFPARAM();
42         wordwrap_buffer = strcat(wordwrap_buffer, s);
43         if(s == "\n")
44         {
45                 sprint(self, wordwrap_buffer);
46                 wordwrap_buffer = "";
47         }
48 }
49
50 void wordwrap_sprint(string s, float l)
51 {SELFPARAM();
52         wordwrap_buffer = "";
53         wordwrap_cb(s, l, wordwrap_buffer_sprint);
54         if(wordwrap_buffer != "")
55                 sprint(self, strcat(wordwrap_buffer, "\n"));
56         wordwrap_buffer = "";
57         return;
58 }
59 #endif
60 #endif
61
62 #ifndef SVQC
63 string draw_UseSkinFor(string pic)
64 {
65         if(substring(pic, 0, 1) == "/")
66                 return substring(pic, 1, strlen(pic)-1);
67         else
68                 return strcat(draw_currentSkin, "/", pic);
69 }
70 #endif
71
72 void wordwrap_cb(string s, float l, void(string) callback)
73 {
74         string c;
75         float lleft, i, j, wlen;
76
77         s = strzone(s);
78         lleft = l;
79         for (i = 0;i < strlen(s);++i)
80         {
81                 if (substring(s, i, 2) == "\\n")
82                 {
83                         callback("\n");
84                         lleft = l;
85                         ++i;
86                 }
87                 else if (substring(s, i, 1) == "\n")
88                 {
89                         callback("\n");
90                         lleft = l;
91                 }
92                 else if (substring(s, i, 1) == " ")
93                 {
94                         if (lleft > 0)
95                         {
96                                 callback(" ");
97                                 lleft = lleft - 1;
98                         }
99                 }
100                 else
101                 {
102                         for (j = i+1;j < strlen(s);++j)
103                                 //    ^^ this skips over the first character of a word, which
104                                 //       is ALWAYS part of the word
105                                 //       this is safe since if i+1 == strlen(s), i will become
106                                 //       strlen(s)-1 at the end of this block and the function
107                                 //       will terminate. A space can't be the first character we
108                                 //       read here, and neither can a \n be the start, since these
109                                 //       two cases have been handled above.
110                         {
111                                 c = substring(s, j, 1);
112                                 if (c == " ")
113                                         break;
114                                 if (c == "\\")
115                                         break;
116                                 if (c == "\n")
117                                         break;
118                                 // we need to keep this tempstring alive even if substring is
119                                 // called repeatedly, so call strcat even though we're not
120                                 // doing anything
121                                 callback("");
122                         }
123                         wlen = j - i;
124                         if (lleft < wlen)
125                         {
126                                 callback("\n");
127                                 lleft = l;
128                         }
129                         callback(substring(s, i, wlen));
130                         lleft = lleft - wlen;
131                         i = j - 1;
132                 }
133         }
134         strunzone(s);
135 }
136
137 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass)
138 {
139         entity e;
140         e = start;
141         funcPre(pass, e);
142         while (e.(downleft))
143         {
144                 e = e.(downleft);
145                 funcPre(pass, e);
146         }
147         funcPost(pass, e);
148         while(e != start)
149         {
150                 if (e.(right))
151                 {
152                         e = e.(right);
153                         funcPre(pass, e);
154                         while (e.(downleft))
155                         {
156                                 e = e.(downleft);
157                                 funcPre(pass, e);
158                         }
159                 }
160                 else
161                         e = e.(up);
162                 funcPost(pass, e);
163         }
164 }
165
166 string ScoreString(int pFlags, float pValue)
167 {
168         string valstr;
169         float l;
170
171         pValue = floor(pValue + 0.5); // round
172
173         if((pValue == 0) && (pFlags & (SFL_HIDE_ZERO | SFL_RANK | SFL_TIME)))
174                 valstr = "";
175         else if(pFlags & SFL_RANK)
176         {
177                 valstr = ftos(pValue);
178                 l = strlen(valstr);
179                 if((l >= 2) && (substring(valstr, l - 2, 1) == "1"))
180                         valstr = strcat(valstr, "th");
181                 else if(substring(valstr, l - 1, 1) == "1")
182                         valstr = strcat(valstr, "st");
183                 else if(substring(valstr, l - 1, 1) == "2")
184                         valstr = strcat(valstr, "nd");
185                 else if(substring(valstr, l - 1, 1) == "3")
186                         valstr = strcat(valstr, "rd");
187                 else
188                         valstr = strcat(valstr, "th");
189         }
190         else if(pFlags & SFL_TIME)
191                 valstr = TIME_ENCODED_TOSTRING(pValue);
192         else
193                 valstr = ftos(pValue);
194
195         return valstr;
196 }
197
198 // compressed vector format:
199 // like MD3, just even shorter
200 //   4 bit pitch (16 angles), 0 is -90, 8 is 0, 16 would be 90
201 //   5 bit yaw (32 angles), 0=0, 8=90, 16=180, 24=270
202 //   7 bit length (logarithmic encoding), 1/8 .. about 7844
203 //     length = 2^(length_encoded/8) / 8
204 // if pitch is 90, yaw does nothing and therefore indicates the sign (yaw is then either 11111 or 11110); 11111 is pointing DOWN
205 // thus, valid values are from 0000.11110.0000000 to 1111.11111.1111111
206 // the special value 0 indicates the zero vector
207
208 float lengthLogTable[128];
209
210 float invertLengthLog(float x)
211 {
212         int l, r, m;
213
214         if(x >= lengthLogTable[127])
215                 return 127;
216         if(x <= lengthLogTable[0])
217                 return 0;
218
219         l = 0;
220         r = 127;
221
222         while(r - l > 1)
223         {
224                 m = floor((l + r) / 2);
225                 if(lengthLogTable[m] < x)
226                         l = m;
227                 else
228                         r = m;
229         }
230
231         // now: r is >=, l is <
232         float lerr = (x - lengthLogTable[l]);
233         float rerr = (lengthLogTable[r] - x);
234         if(lerr < rerr)
235                 return l;
236         return r;
237 }
238
239 vector decompressShortVector(int data)
240 {
241         vector out;
242         if(data == 0)
243                 return '0 0 0';
244         float p = (data & 0xF000) / 0x1000;
245         float y = (data & 0x0F80) / 0x80;
246         int len = (data & 0x007F);
247
248         //print("\ndecompress: p ", ftos(p)); print("y ", ftos(y)); print("len ", ftos(len), "\n");
249
250         if(p == 0)
251         {
252                 out.x = 0;
253                 out.y = 0;
254                 if(y == 31)
255                         out.z = -1;
256                 else
257                         out.z = +1;
258         }
259         else
260         {
261                 y   = .19634954084936207740 * y;
262                 p = .19634954084936207740 * p - 1.57079632679489661922;
263                 out.x = cos(y) *  cos(p);
264                 out.y = sin(y) *  cos(p);
265                 out.z =          -sin(p);
266         }
267
268         //print("decompressed: ", vtos(out), "\n");
269
270         return out * lengthLogTable[len];
271 }
272
273 float compressShortVector(vector vec)
274 {
275         vector ang;
276         float p, y, len;
277         if(vlen(vec) == 0)
278                 return 0;
279         //print("compress: ", vtos(vec), "\n");
280         ang = vectoangles(vec);
281         ang.x = -ang.x;
282         if(ang.x < -90)
283                 ang.x += 360;
284         if(ang.x < -90 && ang.x > +90)
285                 error("BOGUS vectoangles");
286         //print("angles: ", vtos(ang), "\n");
287
288         p = floor(0.5 + (ang.x + 90) * 16 / 180) & 15; // -90..90 to 0..14
289         if(p == 0)
290         {
291                 if(vec.z < 0)
292                         y = 31;
293                 else
294                         y = 30;
295         }
296         else
297                 y = floor(0.5 + ang.y * 32 / 360)          & 31; // 0..360 to 0..32
298         len = invertLengthLog(vlen(vec));
299
300         //print("compressed: p ", ftos(p)); print("y ", ftos(y)); print("len ", ftos(len), "\n");
301
302         return (p * 0x1000) + (y * 0x80) + len;
303 }
304
305 void compressShortVector_init()
306 {
307         float l = 1;
308         float f = pow(2, 1/8);
309         int i;
310         for(i = 0; i < 128; ++i)
311         {
312                 lengthLogTable[i] = l;
313                 l *= f;
314         }
315
316         if(cvar("developer"))
317         {
318                 LOG_INFO("Verifying vector compression table...\n");
319                 for(i = 0x0F00; i < 0xFFFF; ++i)
320                         if(i != compressShortVector(decompressShortVector(i)))
321                         {
322                                 LOG_INFO("BROKEN vector compression: ", ftos(i));
323                                 LOG_INFO(" -> ", vtos(decompressShortVector(i)));
324                                 LOG_INFO(" -> ", ftos(compressShortVector(decompressShortVector(i))));
325                                 LOG_INFO("\n");
326                                 error("b0rk");
327                         }
328                 LOG_INFO("Done.\n");
329         }
330 }
331
332 #ifndef MENUQC
333 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz)
334 {
335         traceline(v0, v0 + dvx, true, forent); if(trace_fraction < 1) return 0;
336         traceline(v0, v0 + dvy, true, forent); if(trace_fraction < 1) return 0;
337         traceline(v0, v0 + dvz, true, forent); if(trace_fraction < 1) return 0;
338         traceline(v0 + dvx, v0 + dvx + dvy, true, forent); if(trace_fraction < 1) return 0;
339         traceline(v0 + dvx, v0 + dvx + dvz, true, forent); if(trace_fraction < 1) return 0;
340         traceline(v0 + dvy, v0 + dvy + dvx, true, forent); if(trace_fraction < 1) return 0;
341         traceline(v0 + dvy, v0 + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
342         traceline(v0 + dvz, v0 + dvz + dvx, true, forent); if(trace_fraction < 1) return 0;
343         traceline(v0 + dvz, v0 + dvz + dvy, true, forent); if(trace_fraction < 1) return 0;
344         traceline(v0 + dvx + dvy, v0 + dvx + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
345         traceline(v0 + dvx + dvz, v0 + dvx + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
346         traceline(v0 + dvy + dvz, v0 + dvx + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
347         return 1;
348 }
349 #endif
350
351 string fixPriorityList(string order, float from, float to, float subtract, float complete)
352 {
353         string neworder;
354         float i, n, w;
355
356         n = tokenize_console(order);
357         neworder = "";
358         for(i = 0; i < n; ++i)
359         {
360                 w = stof(argv(i));
361                 if(w == floor(w))
362                 {
363                         if(w >= from && w <= to)
364                                 neworder = strcat(neworder, ftos(w), " ");
365                         else
366                         {
367                                 w -= subtract;
368                                 if(w >= from && w <= to)
369                                         neworder = strcat(neworder, ftos(w), " ");
370                         }
371                 }
372         }
373
374         if(complete)
375         {
376                 n = tokenize_console(neworder);
377                 for(w = to; w >= from; --w)
378                 {
379                         for(i = 0; i < n; ++i)
380                                 if(stof(argv(i)) == w)
381                                         break;
382                         if(i == n) // not found
383                                 neworder = strcat(neworder, ftos(w), " ");
384                 }
385         }
386
387         return substring(neworder, 0, strlen(neworder) - 1);
388 }
389
390 string mapPriorityList(string order, string(string) mapfunc)
391 {
392         string neworder;
393         float i, n;
394
395         n = tokenize_console(order);
396         neworder = "";
397         for(i = 0; i < n; ++i)
398                 neworder = strcat(neworder, mapfunc(argv(i)), " ");
399
400         return substring(neworder, 0, strlen(neworder) - 1);
401 }
402
403 string swapInPriorityList(string order, float i, float j)
404 {
405         string s;
406         float w, n;
407
408         n = tokenize_console(order);
409
410         if(i >= 0 && i < n && j >= 0 && j < n && i != j)
411         {
412                 s = "";
413                 for(w = 0; w < n; ++w)
414                 {
415                         if(w == i)
416                                 s = strcat(s, argv(j), " ");
417                         else if(w == j)
418                                 s = strcat(s, argv(i), " ");
419                         else
420                                 s = strcat(s, argv(w), " ");
421                 }
422                 return substring(s, 0, strlen(s) - 1);
423         }
424
425         return order;
426 }
427
428 #ifndef MENUQC
429 void get_mi_min_max(float mode)
430 {
431         vector mi, ma;
432
433         if(mi_shortname)
434                 strunzone(mi_shortname);
435         mi_shortname = mapname;
436         if(!strcasecmp(substring(mi_shortname, 0, 5), "maps/"))
437                 mi_shortname = substring(mi_shortname, 5, strlen(mi_shortname) - 5);
438         if(!strcasecmp(substring(mi_shortname, strlen(mi_shortname) - 4, 4), ".bsp"))
439                 mi_shortname = substring(mi_shortname, 0, strlen(mi_shortname) - 4);
440         mi_shortname = strzone(mi_shortname);
441
442 #ifdef CSQC
443         mi = world.mins;
444         ma = world.maxs;
445 #else
446         mi = world.absmin;
447         ma = world.absmax;
448 #endif
449
450         mi_min = mi;
451         mi_max = ma;
452         MapInfo_Get_ByName(mi_shortname, 0, 0);
453         if(MapInfo_Map_mins.x < MapInfo_Map_maxs.x)
454         {
455                 mi_min = MapInfo_Map_mins;
456                 mi_max = MapInfo_Map_maxs;
457         }
458         else
459         {
460                 // not specified
461                 if(mode)
462                 {
463                         // be clever
464                         tracebox('1 0 0' * mi.x,
465                                          '0 1 0' * mi.y + '0 0 1' * mi.z,
466                                          '0 1 0' * ma.y + '0 0 1' * ma.z,
467                                          '1 0 0' * ma.x,
468                                          MOVE_WORLDONLY,
469                                          world);
470                         if(!trace_startsolid)
471                                 mi_min.x = trace_endpos.x;
472
473                         tracebox('0 1 0' * mi.y,
474                                          '1 0 0' * mi.x + '0 0 1' * mi.z,
475                                          '1 0 0' * ma.x + '0 0 1' * ma.z,
476                                          '0 1 0' * ma.y,
477                                          MOVE_WORLDONLY,
478                                          world);
479                         if(!trace_startsolid)
480                                 mi_min.y = trace_endpos.y;
481
482                         tracebox('0 0 1' * mi.z,
483                                          '1 0 0' * mi.x + '0 1 0' * mi.y,
484                                          '1 0 0' * ma.x + '0 1 0' * ma.y,
485                                          '0 0 1' * ma.z,
486                                          MOVE_WORLDONLY,
487                                          world);
488                         if(!trace_startsolid)
489                                 mi_min.z = trace_endpos.z;
490
491                         tracebox('1 0 0' * ma.x,
492                                          '0 1 0' * mi.y + '0 0 1' * mi.z,
493                                          '0 1 0' * ma.y + '0 0 1' * ma.z,
494                                          '1 0 0' * mi.x,
495                                          MOVE_WORLDONLY,
496                                          world);
497                         if(!trace_startsolid)
498                                 mi_max.x = trace_endpos.x;
499
500                         tracebox('0 1 0' * ma.y,
501                                          '1 0 0' * mi.x + '0 0 1' * mi.z,
502                                          '1 0 0' * ma.x + '0 0 1' * ma.z,
503                                          '0 1 0' * mi.y,
504                                          MOVE_WORLDONLY,
505                                          world);
506                         if(!trace_startsolid)
507                                 mi_max.y = trace_endpos.y;
508
509                         tracebox('0 0 1' * ma.z,
510                                          '1 0 0' * mi.x + '0 1 0' * mi.y,
511                                          '1 0 0' * ma.x + '0 1 0' * ma.y,
512                                          '0 0 1' * mi.z,
513                                          MOVE_WORLDONLY,
514                                          world);
515                         if(!trace_startsolid)
516                                 mi_max.z = trace_endpos.z;
517                 }
518         }
519 }
520
521 void get_mi_min_max_texcoords(float mode)
522 {
523         vector extend;
524
525         get_mi_min_max(mode);
526
527         mi_picmin = mi_min;
528         mi_picmax = mi_max;
529
530         // extend mi_picmax to get a square aspect ratio
531         // center the map in that area
532         extend = mi_picmax - mi_picmin;
533         if(extend.y > extend.x)
534         {
535                 mi_picmin.x -= (extend.y - extend.x) * 0.5;
536                 mi_picmax.x += (extend.y - extend.x) * 0.5;
537         }
538         else
539         {
540                 mi_picmin.y -= (extend.x - extend.y) * 0.5;
541                 mi_picmax.y += (extend.x - extend.y) * 0.5;
542         }
543
544         // add another some percent
545         extend = (mi_picmax - mi_picmin) * (1 / 64.0);
546         mi_picmin -= extend;
547         mi_picmax += extend;
548
549         // calculate the texcoords
550         mi_pictexcoord0 = mi_pictexcoord1 = mi_pictexcoord2 = mi_pictexcoord3 = '0 0 0';
551         // first the two corners of the origin
552         mi_pictexcoord0_x = (mi_min.x - mi_picmin.x) / (mi_picmax.x - mi_picmin.x);
553         mi_pictexcoord0_y = (mi_min.y - mi_picmin.y) / (mi_picmax.y - mi_picmin.y);
554         mi_pictexcoord2_x = (mi_max.x - mi_picmin.x) / (mi_picmax.x - mi_picmin.x);
555         mi_pictexcoord2_y = (mi_max.y - mi_picmin.y) / (mi_picmax.y - mi_picmin.y);
556         // then the other corners
557         mi_pictexcoord1_x = mi_pictexcoord0_x;
558         mi_pictexcoord1_y = mi_pictexcoord2_y;
559         mi_pictexcoord3_x = mi_pictexcoord2_x;
560         mi_pictexcoord3_y = mi_pictexcoord0_y;
561 }
562 #endif
563
564 float cvar_settemp(string tmp_cvar, string tmp_value)
565 {
566         float created_saved_value;
567         entity e;
568
569         created_saved_value = 0;
570
571         if (!(tmp_cvar || tmp_value))
572         {
573                 LOG_TRACE("Error: Invalid usage of cvar_settemp(string, string); !\n");
574                 return 0;
575         }
576
577         if(!cvar_type(tmp_cvar))
578         {
579                 LOG_INFOF("Error: cvar %s doesn't exist!\n", tmp_cvar);
580                 return 0;
581         }
582
583         for(e = world; (e = find(e, classname, "saved_cvar_value")); )
584                 if(e.netname == tmp_cvar)
585                         created_saved_value = -1; // skip creation
586
587         if(created_saved_value != -1)
588         {
589                 // creating a new entity to keep track of this cvar
590                 e = new(saved_cvar_value);
591                 make_pure(e);
592                 e.netname = strzone(tmp_cvar);
593                 e.message = strzone(cvar_string(tmp_cvar));
594                 created_saved_value = 1;
595         }
596
597         // update the cvar to the value given
598         cvar_set(tmp_cvar, tmp_value);
599
600         return created_saved_value;
601 }
602
603 float cvar_settemp_restore()
604 {
605         float i = 0;
606         entity e = world;
607         while((e = find(e, classname, "saved_cvar_value")))
608         {
609                 if(cvar_type(e.netname))
610                 {
611                         cvar_set(e.netname, e.message);
612                         remove(e);
613                         ++i;
614                 }
615                 else
616                         LOG_INFOF("Error: cvar %s doesn't exist anymore! It can still be restored once it's manually recreated.\n", e.netname);
617         }
618
619         return i;
620 }
621
622 float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
623 {
624         // STOP.
625         // The following function is SLOW.
626         // For your safety and for the protection of those around you...
627         // DO NOT CALL THIS AT HOME.
628         // No really, don't.
629         if(w(theText, theSize) <= maxWidth)
630                 return strlen(theText); // yeah!
631
632         // binary search for right place to cut string
633         float ch;
634         float left, right, middle; // this always works
635         left = 0;
636         right = strlen(theText); // this always fails
637         do
638         {
639                 middle = floor((left + right) / 2);
640                 if(w(substring(theText, 0, middle), theSize) <= maxWidth)
641                         left = middle;
642                 else
643                         right = middle;
644         }
645         while(left < right - 1);
646
647         if(w("^7", theSize) == 0) // detect color codes support in the width function
648         {
649                 // NOTE: when color codes are involved, this binary search is,
650                 // mathematically, BROKEN. However, it is obviously guaranteed to
651                 // terminate, as the range still halves each time - but nevertheless, it is
652                 // guaranteed that it finds ONE valid cutoff place (where "left" is in
653                 // range, and "right" is outside).
654
655                 // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4)
656                 // and decrease left on the basis of the chars detected of the truncated tag
657                 // Even if the ^xrgb tag is not complete/correct, left is decreased
658                 // (sometimes too much but with a correct result)
659                 // it fixes also ^[0-9]
660                 while(left >= 1 && substring(theText, left-1, 1) == "^")
661                         left-=1;
662
663                 if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/
664                         left-=2;
665                 else if (left >= 3 && substring(theText, left-3, 2) == "^x")
666                         {
667                                 ch = str2chr(theText, left-1);
668                                 if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/
669                                         left-=3;
670                         }
671                 else if (left >= 4 && substring(theText, left-4, 2) == "^x")
672                         {
673                                 ch = str2chr(theText, left-2);
674                                 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
675                                 {
676                                         ch = str2chr(theText, left-1);
677                                         if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/
678                                                 left-=4;
679                                 }
680                         }
681         }
682
683         return left;
684 }
685
686 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t w)
687 {
688         // STOP.
689         // The following function is SLOW.
690         // For your safety and for the protection of those around you...
691         // DO NOT CALL THIS AT HOME.
692         // No really, don't.
693         if(w(theText) <= maxWidth)
694                 return strlen(theText); // yeah!
695
696         // binary search for right place to cut string
697         float ch;
698         float left, right, middle; // this always works
699         left = 0;
700         right = strlen(theText); // this always fails
701         do
702         {
703                 middle = floor((left + right) / 2);
704                 if(w(substring(theText, 0, middle)) <= maxWidth)
705                         left = middle;
706                 else
707                         right = middle;
708         }
709         while(left < right - 1);
710
711         if(w("^7") == 0) // detect color codes support in the width function
712         {
713                 // NOTE: when color codes are involved, this binary search is,
714                 // mathematically, BROKEN. However, it is obviously guaranteed to
715                 // terminate, as the range still halves each time - but nevertheless, it is
716                 // guaranteed that it finds ONE valid cutoff place (where "left" is in
717                 // range, and "right" is outside).
718
719                 // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4)
720                 // and decrease left on the basis of the chars detected of the truncated tag
721                 // Even if the ^xrgb tag is not complete/correct, left is decreased
722                 // (sometimes too much but with a correct result)
723                 // it fixes also ^[0-9]
724                 while(left >= 1 && substring(theText, left-1, 1) == "^")
725                         left-=1;
726
727                 if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/
728                         left-=2;
729                 else if (left >= 3 && substring(theText, left-3, 2) == "^x")
730                         {
731                                 ch = str2chr(theText, left-1);
732                                 if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/
733                                         left-=3;
734                         }
735                 else if (left >= 4 && substring(theText, left-4, 2) == "^x")
736                         {
737                                 ch = str2chr(theText, left-2);
738                                 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
739                                 {
740                                         ch = str2chr(theText, left-1);
741                                         if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/
742                                                 left-=4;
743                                 }
744                         }
745         }
746
747         return left;
748 }
749
750 string find_last_color_code(string s)
751 {
752         int start = strstrofs(s, "^", 0);
753         if (start == -1) // no caret found
754                 return "";
755         int len = strlen(s)-1;
756         int i;
757         for(i = len; i >= start; --i)
758         {
759                 if(substring(s, i, 1) != "^")
760                         continue;
761
762                 int carets = 1;
763                 while (i-carets >= start && substring(s, i-carets, 1) == "^")
764                         ++carets;
765
766                 // check if carets aren't all escaped
767                 if (carets & 1)
768                 {
769                         if(i+1 <= len)
770                         if(strstrofs("0123456789", substring(s, i+1, 1), 0) >= 0)
771                                 return substring(s, i, 2);
772
773                         if(i+4 <= len)
774                         if(substring(s, i+1, 1) == "x")
775                         if(strstrofs("0123456789abcdefABCDEF", substring(s, i+2, 1), 0) >= 0)
776                         if(strstrofs("0123456789abcdefABCDEF", substring(s, i+3, 1), 0) >= 0)
777                         if(strstrofs("0123456789abcdefABCDEF", substring(s, i+4, 1), 0) >= 0)
778                                 return substring(s, i, 5);
779                 }
780                 i -= carets; // this also skips one char before the carets
781         }
782
783         return "";
784 }
785
786 string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
787 {
788         float cantake;
789         float take;
790         string s;
791
792         s = getWrappedLine_remaining;
793
794         if(w <= 0)
795         {
796                 getWrappedLine_remaining = string_null;
797                 return s; // the line has no size ANYWAY, nothing would be displayed.
798         }
799
800         cantake = textLengthUpToWidth(s, w, theFontSize, tw);
801         if(cantake > 0 && cantake < strlen(s))
802         {
803                 take = cantake - 1;
804                 while(take > 0 && substring(s, take, 1) != " ")
805                         --take;
806                 if(take == 0)
807                 {
808                         getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
809                         if(getWrappedLine_remaining == "")
810                                 getWrappedLine_remaining = string_null;
811                         else if (tw("^7", theFontSize) == 0)
812                                 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
813                         return substring(s, 0, cantake);
814                 }
815                 else
816                 {
817                         getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
818                         if(getWrappedLine_remaining == "")
819                                 getWrappedLine_remaining = string_null;
820                         else if (tw("^7", theFontSize) == 0)
821                                 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
822                         return substring(s, 0, take);
823                 }
824         }
825         else
826         {
827                 getWrappedLine_remaining = string_null;
828                 return s;
829         }
830 }
831
832 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw)
833 {
834         float cantake;
835         float take;
836         string s;
837
838         s = getWrappedLine_remaining;
839
840         if(w <= 0)
841         {
842                 getWrappedLine_remaining = string_null;
843                 return s; // the line has no size ANYWAY, nothing would be displayed.
844         }
845
846         cantake = textLengthUpToLength(s, w, tw);
847         if(cantake > 0 && cantake < strlen(s))
848         {
849                 take = cantake - 1;
850                 while(take > 0 && substring(s, take, 1) != " ")
851                         --take;
852                 if(take == 0)
853                 {
854                         getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
855                         if(getWrappedLine_remaining == "")
856                                 getWrappedLine_remaining = string_null;
857                         else if (tw("^7") == 0)
858                                 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
859                         return substring(s, 0, cantake);
860                 }
861                 else
862                 {
863                         getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
864                         if(getWrappedLine_remaining == "")
865                                 getWrappedLine_remaining = string_null;
866                         else if (tw("^7") == 0)
867                                 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
868                         return substring(s, 0, take);
869                 }
870         }
871         else
872         {
873                 getWrappedLine_remaining = string_null;
874                 return s;
875         }
876 }
877
878 string textShortenToWidth(string theText, float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
879 {
880         if(tw(theText, theFontSize) <= maxWidth)
881                 return theText;
882         else
883                 return strcat(substring(theText, 0, textLengthUpToWidth(theText, maxWidth - tw("...", theFontSize), theFontSize, tw)), "...");
884 }
885
886 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw)
887 {
888         if(tw(theText) <= maxWidth)
889                 return theText;
890         else
891                 return strcat(substring(theText, 0, textLengthUpToLength(theText, maxWidth - tw("..."), tw)), "...");
892 }
893
894 float isGametypeInFilter(float gt, float tp, float ts, string pattern)
895 {
896         string subpattern, subpattern2, subpattern3, subpattern4;
897         subpattern = strcat(",", MapInfo_Type_ToString(gt), ",");
898         if(tp)
899                 subpattern2 = ",teams,";
900         else
901                 subpattern2 = ",noteams,";
902         if(ts)
903                 subpattern3 = ",teamspawns,";
904         else
905                 subpattern3 = ",noteamspawns,";
906         if(gt == MAPINFO_TYPE_RACE || gt == MAPINFO_TYPE_CTS)
907                 subpattern4 = ",race,";
908         else
909                 subpattern4 = string_null;
910
911         if(substring(pattern, 0, 1) == "-")
912         {
913                 pattern = substring(pattern, 1, strlen(pattern) - 1);
914                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) >= 0)
915                         return 0;
916                 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) >= 0)
917                         return 0;
918                 if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) >= 0)
919                         return 0;
920                 if(subpattern4 && strstrofs(strcat(",", pattern, ","), subpattern4, 0) >= 0)
921                         return 0;
922         }
923         else
924         {
925                 if(substring(pattern, 0, 1) == "+")
926                         pattern = substring(pattern, 1, strlen(pattern) - 1);
927                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) < 0)
928                 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) < 0)
929                 if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) < 0)
930                 {
931                         if (!subpattern4)
932                                 return 0;
933                         if(strstrofs(strcat(",", pattern, ","), subpattern4, 0) < 0)
934                                 return 0;
935                 }
936         }
937         return 1;
938 }
939
940 vector solve_shotdirection(vector myorg, vector myvel, vector eorg, vector evel, float spd, float newton_style)
941 {
942         vector ret;
943
944         // make origin and speed relative
945         eorg -= myorg;
946         if(newton_style)
947                 evel -= myvel;
948
949         // now solve for ret, ret normalized:
950         //   eorg + t * evel == t * ret * spd
951         // or, rather, solve for t:
952         //   |eorg + t * evel| == t * spd
953         //   eorg^2 + t^2 * evel^2 + 2 * t * (eorg * evel) == t^2 * spd^2
954         //   t^2 * (evel^2 - spd^2) + t * (2 * (eorg * evel)) + eorg^2 == 0
955         vector solution = solve_quadratic(evel * evel - spd * spd, 2 * (eorg * evel), eorg * eorg);
956         // p = 2 * (eorg * evel) / (evel * evel - spd * spd)
957         // q = (eorg * eorg) / (evel * evel - spd * spd)
958         if(!solution.z) // no real solution
959         {
960                 // happens if D < 0
961                 // (eorg * evel)^2 < (evel^2 - spd^2) * eorg^2
962                 // (eorg * evel)^2 / eorg^2 < evel^2 - spd^2
963                 // spd^2 < ((evel^2 * eorg^2) - (eorg * evel)^2) / eorg^2
964                 // spd^2 < evel^2 * (1 - cos^2 angle(evel, eorg))
965                 // spd^2 < evel^2 * sin^2 angle(evel, eorg)
966                 // spd < |evel| * sin angle(evel, eorg)
967                 return '0 0 0';
968         }
969         else if(solution.x > 0)
970         {
971                 // both solutions > 0: take the smaller one
972                 // happens if p < 0 and q > 0
973                 ret = normalize(eorg + solution.x * evel);
974         }
975         else if(solution.y > 0)
976         {
977                 // one solution > 0: take the larger one
978                 // happens if q < 0 or q == 0 and p < 0
979                 ret = normalize(eorg + solution.y * evel);
980         }
981         else
982         {
983                 // no solution > 0: reject
984                 // happens if p > 0 and q >= 0
985                 // 2 * (eorg * evel) / (evel * evel - spd * spd) > 0
986                 // (eorg * eorg) / (evel * evel - spd * spd) >= 0
987                 //
988                 // |evel| >= spd
989                 // eorg * evel > 0
990                 //
991                 // "Enemy is moving away from me at more than spd"
992                 return '0 0 0';
993         }
994
995         // NOTE: we always got a solution if spd > |evel|
996
997         if(newton_style == 2)
998                 ret = normalize(ret * spd + myvel);
999
1000         return ret;
1001 }
1002
1003 vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_style, float mi, float ma)
1004 {
1005         if(!newton_style)
1006                 return spd * mydir;
1007
1008         if(newton_style == 2)
1009         {
1010                 // true Newtonian projectiles with automatic aim adjustment
1011                 //
1012                 // solve: |outspeed * mydir - myvel| = spd
1013                 // outspeed^2 - 2 * outspeed * (mydir * myvel) + myvel^2 - spd^2 = 0
1014                 // outspeed = (mydir * myvel) +- sqrt((mydir * myvel)^2 - myvel^2 + spd^2)
1015                 // PLUS SIGN!
1016                 // not defined?
1017                 // then...
1018                 // myvel^2 - (mydir * myvel)^2 > spd^2
1019                 // velocity without mydir component > spd
1020                 // fire at smallest possible spd that works?
1021                 // |(mydir * myvel) * myvel - myvel| = spd
1022
1023                 vector solution = solve_quadratic(1, -2 * (mydir * myvel), myvel * myvel - spd * spd);
1024
1025                 float outspeed;
1026                 if(solution.z)
1027                         outspeed = solution.y; // the larger one
1028                 else
1029                 {
1030                         //outspeed = 0; // slowest possible shot
1031                         outspeed = solution.x; // the real part (that is, the average!)
1032                         //dprint("impossible shot, adjusting\n");
1033                 }
1034
1035                 outspeed = bound(spd * mi, outspeed, spd * ma);
1036                 return mydir * outspeed;
1037         }
1038
1039         // real Newtonian
1040         return myvel + spd * mydir;
1041 }
1042
1043 float compressShotOrigin(vector v)
1044 {
1045         float x, y, z;
1046         x = rint(v.x * 2);
1047         y = rint(v.y * 4) + 128;
1048         z = rint(v.z * 4) + 128;
1049         if(x > 255 || x < 0)
1050         {
1051                 LOG_INFO("shot origin ", vtos(v), " x out of bounds\n");
1052                 x = bound(0, x, 255);
1053         }
1054         if(y > 255 || y < 0)
1055         {
1056                 LOG_INFO("shot origin ", vtos(v), " y out of bounds\n");
1057                 y = bound(0, y, 255);
1058         }
1059         if(z > 255 || z < 0)
1060         {
1061                 LOG_INFO("shot origin ", vtos(v), " z out of bounds\n");
1062                 z = bound(0, z, 255);
1063         }
1064         return x * 0x10000 + y * 0x100 + z;
1065 }
1066 vector decompressShotOrigin(int f)
1067 {
1068         vector v;
1069         v.x = ((f & 0xFF0000) / 0x10000) / 2;
1070         v.y = ((f & 0xFF00) / 0x100 - 128) / 4;
1071         v.z = ((f & 0xFF) - 128) / 4;
1072         return v;
1073 }
1074
1075 #ifndef MENUQC
1076 vector healtharmor_maxdamage(float h, float a, float armorblock, int deathtype)
1077 {
1078         // NOTE: we'll always choose the SMALLER value...
1079         float healthdamage, armordamage, armorideal;
1080         if (DEATH_IS(deathtype, DEATH_DROWN))  // Why should armor help here...
1081                 armorblock = 0;
1082         vector v;
1083         healthdamage = (h - 1) / (1 - armorblock); // damage we can take if we could use more health
1084         armordamage = a + (h - 1); // damage we can take if we could use more armor
1085         armorideal = healthdamage * armorblock;
1086         v.y = armorideal;
1087         if(armordamage < healthdamage)
1088         {
1089                 v.x = armordamage;
1090                 v.z = 1;
1091         }
1092         else
1093         {
1094                 v.x = healthdamage;
1095                 v.z = 0;
1096         }
1097         return v;
1098 }
1099
1100 vector healtharmor_applydamage(float a, float armorblock, int deathtype, float damage)
1101 {
1102         vector v;
1103         if (DEATH_IS(deathtype, DEATH_DROWN))  // Why should armor help here...
1104                 armorblock = 0;
1105         v.y = bound(0, damage * armorblock, a); // save
1106         v.x = bound(0, damage - v.y, damage); // take
1107         v.z = 0;
1108         return v;
1109 }
1110 #endif
1111
1112 string getcurrentmod()
1113 {
1114         float n;
1115         string m;
1116         m = cvar_string("fs_gamedir");
1117         n = tokenize_console(m);
1118         if(n == 0)
1119                 return "data";
1120         else
1121                 return argv(n - 1);
1122 }
1123
1124 float matchacl(string acl, string str)
1125 {
1126         string t, s;
1127         float r, d;
1128         r = 0;
1129         while(acl)
1130         {
1131                 t = car(acl); acl = cdr(acl);
1132
1133                 d = 1;
1134                 if(substring(t, 0, 1) == "-")
1135                 {
1136                         d = -1;
1137                         t = substring(t, 1, strlen(t) - 1);
1138                 }
1139                 else if(substring(t, 0, 1) == "+")
1140                         t = substring(t, 1, strlen(t) - 1);
1141
1142                 if(substring(t, -1, 1) == "*")
1143                 {
1144                         t = substring(t, 0, strlen(t) - 1);
1145                         s = substring(str, 0, strlen(t));
1146                 }
1147                 else
1148                         s = str;
1149
1150                 if(s == t)
1151                 {
1152                         r = d;
1153                 }
1154         }
1155         return r;
1156 }
1157
1158 string get_model_datafilename(string m, float sk, string fil)
1159 {
1160         if(m)
1161                 m = strcat(m, "_");
1162         else
1163                 m = "models/player/*_";
1164         if(sk >= 0)
1165                 m = strcat(m, ftos(sk));
1166         else
1167                 m = strcat(m, "*");
1168         return strcat(m, ".", fil);
1169 }
1170
1171 float get_model_parameters(string m, float sk)
1172 {
1173         get_model_parameters_modelname = string_null;
1174         get_model_parameters_modelskin = -1;
1175         get_model_parameters_name = string_null;
1176         get_model_parameters_species = -1;
1177         get_model_parameters_sex = string_null;
1178         get_model_parameters_weight = -1;
1179         get_model_parameters_age = -1;
1180         get_model_parameters_desc = string_null;
1181         get_model_parameters_bone_upperbody = string_null;
1182         get_model_parameters_bone_weapon = string_null;
1183         for(int i = 0; i < MAX_AIM_BONES; ++i)
1184         {
1185                 get_model_parameters_bone_aim[i] = string_null;
1186                 get_model_parameters_bone_aimweight[i] = 0;
1187         }
1188         get_model_parameters_fixbone = 0;
1189
1190 #ifndef MENUQC
1191         MUTATOR_CALLHOOK(ClearModelParams);
1192 #endif
1193
1194         if (!m)
1195                 return 1;
1196
1197         if(substring(m, -9, 5) == "_lod1" || substring(m, -9, 5) == "_lod2")
1198                 m = strcat(substring(m, 0, -10), substring(m, -4, -1));
1199
1200         if(sk < 0)
1201         {
1202                 if(substring(m, -4, -1) != ".txt")
1203                         return 0;
1204                 if(substring(m, -6, 1) != "_")
1205                         return 0;
1206                 sk = stof(substring(m, -5, 1));
1207                 m = substring(m, 0, -7);
1208         }
1209
1210         string fn = get_model_datafilename(m, sk, "txt");
1211         int fh = fopen(fn, FILE_READ);
1212         if(fh < 0)
1213         {
1214                 sk = 0;
1215                 fn = get_model_datafilename(m, sk, "txt");
1216                 fh = fopen(fn, FILE_READ);
1217                 if(fh < 0)
1218                         return 0;
1219         }
1220
1221         get_model_parameters_modelname = m;
1222         get_model_parameters_modelskin = sk;
1223         string s, c;
1224         while((s = fgets(fh)))
1225         {
1226                 if(s == "")
1227                         break; // next lines will be description
1228                 c = car(s);
1229                 s = cdr(s);
1230                 if(c == "name")
1231                         get_model_parameters_name = s;
1232                 if(c == "species")
1233                         switch(s)
1234                         {
1235                                 case "human":       get_model_parameters_species = SPECIES_HUMAN;       break;
1236                                 case "alien":       get_model_parameters_species = SPECIES_ALIEN;       break;
1237                                 case "robot_shiny": get_model_parameters_species = SPECIES_ROBOT_SHINY; break;
1238                                 case "robot_rusty": get_model_parameters_species = SPECIES_ROBOT_RUSTY; break;
1239                                 case "robot_solid": get_model_parameters_species = SPECIES_ROBOT_SOLID; break;
1240                                 case "animal":      get_model_parameters_species = SPECIES_ANIMAL;      break;
1241                                 case "reserved":    get_model_parameters_species = SPECIES_RESERVED;    break;
1242                         }
1243                 if(c == "sex")
1244                         get_model_parameters_sex = s;
1245                 if(c == "weight")
1246                         get_model_parameters_weight = stof(s);
1247                 if(c == "age")
1248                         get_model_parameters_age = stof(s);
1249                 if(c == "description")
1250                         get_model_parameters_description = s;
1251                 if(c == "bone_upperbody")
1252                         get_model_parameters_bone_upperbody = s;
1253                 if(c == "bone_weapon")
1254                         get_model_parameters_bone_weapon = s;
1255         #ifndef MENUQC
1256                 MUTATOR_CALLHOOK(GetModelParams, c, s);
1257         #endif
1258                 for(int i = 0; i < MAX_AIM_BONES; ++i)
1259                         if(c == strcat("bone_aim", ftos(i)))
1260                         {
1261                                 get_model_parameters_bone_aimweight[i] = stof(car(s));
1262                                 get_model_parameters_bone_aim[i] = cdr(s);
1263                         }
1264                 if(c == "fixbone")
1265                         get_model_parameters_fixbone = stof(s);
1266         }
1267
1268         while((s = fgets(fh)))
1269         {
1270                 if(get_model_parameters_desc)
1271                         get_model_parameters_desc = strcat(get_model_parameters_desc, "\n");
1272                 if(s != "")
1273                         get_model_parameters_desc = strcat(get_model_parameters_desc, s);
1274         }
1275
1276         fclose(fh);
1277
1278         return 1;
1279 }
1280
1281 // x-encoding (encoding as zero length invisible string)
1282 const string XENCODE_2  = "xX";
1283 const string XENCODE_22 = "0123456789abcdefABCDEF";
1284 string xencode(int f)
1285 {
1286         float a, b, c, d;
1287         d = f % 22; f = floor(f / 22);
1288         c = f % 22; f = floor(f / 22);
1289         b = f % 22; f = floor(f / 22);
1290         a = f %  2; // f = floor(f /  2);
1291         return strcat(
1292                 "^",
1293                 substring(XENCODE_2,  a, 1),
1294                 substring(XENCODE_22, b, 1),
1295                 substring(XENCODE_22, c, 1),
1296                 substring(XENCODE_22, d, 1)
1297         );
1298 }
1299 float xdecode(string s)
1300 {
1301         float a, b, c, d;
1302         if(substring(s, 0, 1) != "^")
1303                 return -1;
1304         if(strlen(s) < 5)
1305                 return -1;
1306         a = strstrofs(XENCODE_2,  substring(s, 1, 1), 0);
1307         b = strstrofs(XENCODE_22, substring(s, 2, 1), 0);
1308         c = strstrofs(XENCODE_22, substring(s, 3, 1), 0);
1309         d = strstrofs(XENCODE_22, substring(s, 4, 1), 0);
1310         if(a < 0 || b < 0 || c < 0 || d < 0)
1311                 return -1;
1312         return ((a * 22 + b) * 22 + c) * 22 + d;
1313 }
1314
1315 /*
1316 string strlimitedlen(string input, string truncation, float strip_colors, float limit)
1317 {
1318         if(strlen((strip_colors ? strdecolorize(input) : input)) <= limit)
1319                 return input;
1320         else
1321                 return strcat(substring(input, 0, (strlen(input) - strlen(truncation))), truncation);
1322 }*/
1323
1324 float shutdown_running;
1325 #ifdef SVQC
1326 void SV_Shutdown()
1327 #endif
1328 #ifdef CSQC
1329 void CSQC_Shutdown()
1330 #endif
1331 #ifdef MENUQC
1332 void m_shutdown()
1333 #endif
1334 {
1335         if(shutdown_running)
1336         {
1337                 LOG_INFO("Recursive shutdown detected! Only restoring cvars...\n");
1338         }
1339         else
1340         {
1341                 shutdown_running = 1;
1342                 Shutdown();
1343                 shutdownhooks();
1344         }
1345         cvar_settemp_restore(); // this must be done LAST, but in any case
1346 }
1347
1348 #ifndef MENUQC
1349 .float skeleton_bones_index;
1350 void Skeleton_SetBones(entity e)
1351 {
1352         // set skeleton_bones to the total number of bones on the model
1353         if(e.skeleton_bones_index == e.modelindex)
1354                 return; // same model, nothing to update
1355
1356         float skelindex;
1357         skelindex = skel_create(e.modelindex);
1358         e.skeleton_bones = skel_get_numbones(skelindex);
1359         skel_delete(skelindex);
1360         e.skeleton_bones_index = e.modelindex;
1361 }
1362 #endif
1363
1364 string to_execute_next_frame;
1365 void execute_next_frame()
1366 {
1367         if(to_execute_next_frame)
1368         {
1369                 localcmd("\n", to_execute_next_frame, "\n");
1370                 strunzone(to_execute_next_frame);
1371                 to_execute_next_frame = string_null;
1372         }
1373 }
1374 void queue_to_execute_next_frame(string s)
1375 {
1376         if(to_execute_next_frame)
1377         {
1378                 s = strcat(s, "\n", to_execute_next_frame);
1379                 strunzone(to_execute_next_frame);
1380         }
1381         to_execute_next_frame = strzone(s);
1382 }
1383
1384 .float FindConnectedComponent_processing;
1385 void FindConnectedComponent(entity e, .entity fld, findNextEntityNearFunction_t nxt, isConnectedFunction_t iscon, entity pass)
1386 {
1387         entity queue_start, queue_end;
1388
1389         // we build a queue of to-be-processed entities.
1390         // queue_start is the next entity to be checked for neighbors
1391         // queue_end is the last entity added
1392
1393         if(e.FindConnectedComponent_processing)
1394                 error("recursion or broken cleanup");
1395
1396         // start with a 1-element queue
1397         queue_start = queue_end = e;
1398         queue_end.(fld) = world;
1399         queue_end.FindConnectedComponent_processing = 1;
1400
1401         // for each queued item:
1402         for (; queue_start; queue_start = queue_start.(fld))
1403         {
1404                 // find all neighbors of queue_start
1405                 entity t;
1406                 for(t = world; (t = nxt(t, queue_start, pass)); )
1407                 {
1408                         if(t.FindConnectedComponent_processing)
1409                                 continue;
1410                         if(iscon(t, queue_start, pass))
1411                         {
1412                                 // it is connected? ADD IT. It will look for neighbors soon too.
1413                                 queue_end.(fld) = t;
1414                                 queue_end = t;
1415                                 queue_end.(fld) = world;
1416                                 queue_end.FindConnectedComponent_processing = 1;
1417                         }
1418                 }
1419         }
1420
1421         // unmark
1422         for (queue_start = e; queue_start; queue_start = queue_start.(fld))
1423                 queue_start.FindConnectedComponent_processing = 0;
1424 }
1425
1426 #ifndef MENUQC
1427 vector animfixfps(entity e, vector a, vector b)
1428 {
1429         // multi-frame anim: keep as-is
1430         if(a.y == 1)
1431         {
1432                 float dur;
1433                 dur = frameduration(e.modelindex, a.x);
1434                 if(dur <= 0 && b.y)
1435                 {
1436                         a = b;
1437                         dur = frameduration(e.modelindex, a.x);
1438                 }
1439                 if(dur > 0)
1440                         a.z = 1.0 / dur;
1441         }
1442         return a;
1443 }
1444 #endif
1445
1446 #ifdef SVQC
1447 void dedicated_print(string input) // print(), but only print if the server is not local
1448 {
1449         if(server_is_dedicated) { LOG_INFO(input); }
1450 }
1451 #endif
1452
1453 #ifndef MENUQC
1454 float Announcer_PickNumber(float type, float num)
1455 {
1456         switch(type)
1457         {
1458                 case CNT_GAMESTART:
1459                 {
1460                         switch(num)
1461                         {
1462                                 case 10: return ANNCE_NUM_GAMESTART_10;
1463                                 case 9:  return ANNCE_NUM_GAMESTART_9;
1464                                 case 8:  return ANNCE_NUM_GAMESTART_8;
1465                                 case 7:  return ANNCE_NUM_GAMESTART_7;
1466                                 case 6:  return ANNCE_NUM_GAMESTART_6;
1467                                 case 5:  return ANNCE_NUM_GAMESTART_5;
1468                                 case 4:  return ANNCE_NUM_GAMESTART_4;
1469                                 case 3:  return ANNCE_NUM_GAMESTART_3;
1470                                 case 2:  return ANNCE_NUM_GAMESTART_2;
1471                                 case 1:  return ANNCE_NUM_GAMESTART_1;
1472                         }
1473                         break;
1474                 }
1475                 case CNT_IDLE:
1476                 {
1477                         switch(num)
1478                         {
1479                                 case 10: return ANNCE_NUM_IDLE_10;
1480                                 case 9:  return ANNCE_NUM_IDLE_9;
1481                                 case 8:  return ANNCE_NUM_IDLE_8;
1482                                 case 7:  return ANNCE_NUM_IDLE_7;
1483                                 case 6:  return ANNCE_NUM_IDLE_6;
1484                                 case 5:  return ANNCE_NUM_IDLE_5;
1485                                 case 4:  return ANNCE_NUM_IDLE_4;
1486                                 case 3:  return ANNCE_NUM_IDLE_3;
1487                                 case 2:  return ANNCE_NUM_IDLE_2;
1488                                 case 1:  return ANNCE_NUM_IDLE_1;
1489                         }
1490                         break;
1491                 }
1492                 case CNT_KILL:
1493                 {
1494                         switch(num)
1495                         {
1496                                 case 10: return ANNCE_NUM_KILL_10;
1497                                 case 9:  return ANNCE_NUM_KILL_9;
1498                                 case 8:  return ANNCE_NUM_KILL_8;
1499                                 case 7:  return ANNCE_NUM_KILL_7;
1500                                 case 6:  return ANNCE_NUM_KILL_6;
1501                                 case 5:  return ANNCE_NUM_KILL_5;
1502                                 case 4:  return ANNCE_NUM_KILL_4;
1503                                 case 3:  return ANNCE_NUM_KILL_3;
1504                                 case 2:  return ANNCE_NUM_KILL_2;
1505                                 case 1:  return ANNCE_NUM_KILL_1;
1506                         }
1507                         break;
1508                 }
1509                 case CNT_RESPAWN:
1510                 {
1511                         switch(num)
1512                         {
1513                                 case 10: return ANNCE_NUM_RESPAWN_10;
1514                                 case 9:  return ANNCE_NUM_RESPAWN_9;
1515                                 case 8:  return ANNCE_NUM_RESPAWN_8;
1516                                 case 7:  return ANNCE_NUM_RESPAWN_7;
1517                                 case 6:  return ANNCE_NUM_RESPAWN_6;
1518                                 case 5:  return ANNCE_NUM_RESPAWN_5;
1519                                 case 4:  return ANNCE_NUM_RESPAWN_4;
1520                                 case 3:  return ANNCE_NUM_RESPAWN_3;
1521                                 case 2:  return ANNCE_NUM_RESPAWN_2;
1522                                 case 1:  return ANNCE_NUM_RESPAWN_1;
1523                         }
1524                         break;
1525                 }
1526                 case CNT_ROUNDSTART:
1527                 {
1528                         switch(num)
1529                         {
1530                                 case 10: return ANNCE_NUM_ROUNDSTART_10;
1531                                 case 9:  return ANNCE_NUM_ROUNDSTART_9;
1532                                 case 8:  return ANNCE_NUM_ROUNDSTART_8;
1533                                 case 7:  return ANNCE_NUM_ROUNDSTART_7;
1534                                 case 6:  return ANNCE_NUM_ROUNDSTART_6;
1535                                 case 5:  return ANNCE_NUM_ROUNDSTART_5;
1536                                 case 4:  return ANNCE_NUM_ROUNDSTART_4;
1537                                 case 3:  return ANNCE_NUM_ROUNDSTART_3;
1538                                 case 2:  return ANNCE_NUM_ROUNDSTART_2;
1539                                 case 1:  return ANNCE_NUM_ROUNDSTART_1;
1540                         }
1541                         break;
1542                 }
1543                 default:
1544                 {
1545                         switch(num)
1546                         {
1547                                 case 10: return ANNCE_NUM_10;
1548                                 case 9:  return ANNCE_NUM_9;
1549                                 case 8:  return ANNCE_NUM_8;
1550                                 case 7:  return ANNCE_NUM_7;
1551                                 case 6:  return ANNCE_NUM_6;
1552                                 case 5:  return ANNCE_NUM_5;
1553                                 case 4:  return ANNCE_NUM_4;
1554                                 case 3:  return ANNCE_NUM_3;
1555                                 case 2:  return ANNCE_NUM_2;
1556                                 case 1:  return ANNCE_NUM_1;
1557                         }
1558                         break;
1559                 }
1560         }
1561         return NOTIF_ABORT; // abort sending if none of these numbers were right
1562 }
1563 #endif
1564
1565 #ifndef MENUQC
1566 int Mod_Q1BSP_SuperContentsFromNativeContents(int nativecontents)
1567 {
1568         switch(nativecontents)
1569         {
1570                 case CONTENT_EMPTY:
1571                         return 0;
1572                 case CONTENT_SOLID:
1573                         return DPCONTENTS_SOLID | DPCONTENTS_OPAQUE;
1574                 case CONTENT_WATER:
1575                         return DPCONTENTS_WATER;
1576                 case CONTENT_SLIME:
1577                         return DPCONTENTS_SLIME;
1578                 case CONTENT_LAVA:
1579                         return DPCONTENTS_LAVA | DPCONTENTS_NODROP;
1580                 case CONTENT_SKY:
1581                         return DPCONTENTS_SKY | DPCONTENTS_NODROP | DPCONTENTS_OPAQUE; // to match behaviour of Q3 maps, let sky count as opaque
1582         }
1583         return 0;
1584 }
1585
1586 int Mod_Q1BSP_NativeContentsFromSuperContents(int supercontents)
1587 {
1588         if(supercontents & (DPCONTENTS_SOLID | DPCONTENTS_BODY))
1589                 return CONTENT_SOLID;
1590         if(supercontents & DPCONTENTS_SKY)
1591                 return CONTENT_SKY;
1592         if(supercontents & DPCONTENTS_LAVA)
1593                 return CONTENT_LAVA;
1594         if(supercontents & DPCONTENTS_SLIME)
1595                 return CONTENT_SLIME;
1596         if(supercontents & DPCONTENTS_WATER)
1597                 return CONTENT_WATER;
1598         return CONTENT_EMPTY;
1599 }
1600 #endif