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