4 #include "../client/defs.qh"
5 #include "constants.qh"
6 #include "../client/mutators/events.qh"
8 #include "notifications.qh"
9 #include "deathtypes/all.qh"
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"
21 string wordwrap_buffer;
23 void wordwrap_buffer_put(string s)
25 wordwrap_buffer = strcat(wordwrap_buffer, s);
28 string wordwrap(string s, float l)
32 wordwrap_cb(s, l, wordwrap_buffer_put);
40 void wordwrap_buffer_sprint(string s)
42 wordwrap_buffer = strcat(wordwrap_buffer, s);
45 sprint(self, wordwrap_buffer);
50 void wordwrap_sprint(string s, float l)
53 wordwrap_cb(s, l, wordwrap_buffer_sprint);
54 if(wordwrap_buffer != "")
55 sprint(self, strcat(wordwrap_buffer, "\n"));
63 string draw_UseSkinFor(string pic)
65 if(substring(pic, 0, 1) == "/")
66 return substring(pic, 1, strlen(pic)-1);
68 return strcat(draw_currentSkin, "/", pic);
72 void wordwrap_cb(string s, float l, void(string) callback)
75 float lleft, i, j, wlen;
79 for (i = 0;i < strlen(s);++i)
81 if (substring(s, i, 2) == "\\n")
87 else if (substring(s, i, 1) == "\n")
92 else if (substring(s, i, 1) == " ")
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.
111 c = substring(s, j, 1);
118 // we need to keep this tempstring alive even if substring is
119 // called repeatedly, so call strcat even though we're not
129 callback(substring(s, i, wlen));
130 lleft = lleft - wlen;
137 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass)
166 // converts a number to a string with the indicated number of decimals
167 // works for up to 10 decimals!
168 string ftos_decimals(float number, float decimals)
170 // inhibit stupid negative zero
173 // we have sprintf...
174 return sprintf("%.*f", decimals, number);
177 // Databases (hash tables)
178 const float DB_BUCKETS = 8192;
179 void db_save(float db, string pFilename)
182 fh = fopen(pFilename, FILE_WRITE);
185 LOG_INFO(strcat("^1Can't write DB to ", pFilename));
189 fputs(fh, strcat(ftos(DB_BUCKETS), "\n"));
190 for(i = 0; i < n; ++i)
191 fputs(fh, strcat(bufstr_get(db, i), "\n"));
200 int db_load(string pFilename)
202 float db, fh, i, j, n;
207 fh = fopen(pFilename, FILE_READ);
211 if(stof(l) == DB_BUCKETS)
214 while((l = fgets(fh)))
217 bufstr_set(db, i, l);
223 // different count of buckets, or a dump?
224 // need to reorganize the database then (SLOW)
226 // note: we also parse the first line (l) in case the DB file is
227 // missing the bucket count
230 n = tokenizebyseparator(l, "\\");
231 for(j = 2; j < n; j += 2)
232 db_put(db, argv(j-1), uri_unescape(argv(j)));
234 while((l = fgets(fh)));
240 void db_dump(float db, string pFilename)
242 float fh, i, j, n, m;
243 fh = fopen(pFilename, FILE_WRITE);
245 error(strcat("Can't dump DB to ", pFilename));
248 for(i = 0; i < n; ++i)
250 m = tokenizebyseparator(bufstr_get(db, i), "\\");
251 for(j = 2; j < m; j += 2)
252 fputs(fh, strcat("\\", argv(j-1), "\\", argv(j), "\n"));
257 void db_close(float db)
262 string db_get(float db, string pKey)
265 h = crc16(false, pKey) % DB_BUCKETS;
266 return uri_unescape(infoget(bufstr_get(db, h), pKey));
269 void db_put(float db, string pKey, string pValue)
272 h = crc16(false, pKey) % DB_BUCKETS;
273 bufstr_set(db, h, infoadd(bufstr_get(db, h), pKey, uri_escape(pValue)));
279 LOG_INFO("LOAD...\n");
280 db = db_load("foo.db");
281 LOG_INFO("LOADED. FILL...\n");
282 for(i = 0; i < DB_BUCKETS; ++i)
283 db_put(db, ftos(random()), "X");
284 LOG_INFO("FILLED. SAVE...\n");
285 db_save(db, "foo.db");
286 LOG_INFO("SAVED. CLOSE...\n");
288 LOG_INFO("CLOSED.\n");
291 // Multiline text file buffers
292 int buf_load(string pFilename)
299 fh = fopen(pFilename, FILE_READ);
306 while((l = fgets(fh)))
308 bufstr_set(buf, i, l);
315 void buf_save(float buf, string pFilename)
318 fh = fopen(pFilename, FILE_WRITE);
320 error(strcat("Can't write buf to ", pFilename));
321 n = buf_getsize(buf);
322 for(i = 0; i < n; ++i)
323 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
327 string format_time(float seconds)
329 float days, hours, minutes;
330 seconds = floor(seconds + 0.5);
331 days = floor(seconds / 864000);
332 seconds -= days * 864000;
333 hours = floor(seconds / 36000);
334 seconds -= hours * 36000;
335 minutes = floor(seconds / 600);
336 seconds -= minutes * 600;
338 return sprintf(_("%d days, %02d:%02d:%02d"), days, hours, minutes, seconds);
340 return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds);
343 string mmsss(float tenths)
347 tenths = floor(tenths + 0.5);
348 minutes = floor(tenths / 600);
349 tenths -= minutes * 600;
350 s = ftos(1000 + tenths);
351 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
354 string mmssss(float hundredths)
358 hundredths = floor(hundredths + 0.5);
359 minutes = floor(hundredths / 6000);
360 hundredths -= minutes * 6000;
361 s = ftos(10000 + hundredths);
362 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2));
365 string ScoreString(int pFlags, float pValue)
370 pValue = floor(pValue + 0.5); // round
372 if((pValue == 0) && (pFlags & (SFL_HIDE_ZERO | SFL_RANK | SFL_TIME)))
374 else if(pFlags & SFL_RANK)
376 valstr = ftos(pValue);
378 if((l >= 2) && (substring(valstr, l - 2, 1) == "1"))
379 valstr = strcat(valstr, "th");
380 else if(substring(valstr, l - 1, 1) == "1")
381 valstr = strcat(valstr, "st");
382 else if(substring(valstr, l - 1, 1) == "2")
383 valstr = strcat(valstr, "nd");
384 else if(substring(valstr, l - 1, 1) == "3")
385 valstr = strcat(valstr, "rd");
387 valstr = strcat(valstr, "th");
389 else if(pFlags & SFL_TIME)
390 valstr = TIME_ENCODED_TOSTRING(pValue);
392 valstr = ftos(pValue);
397 // compressed vector format:
398 // like MD3, just even shorter
399 // 4 bit pitch (16 angles), 0 is -90, 8 is 0, 16 would be 90
400 // 5 bit yaw (32 angles), 0=0, 8=90, 16=180, 24=270
401 // 7 bit length (logarithmic encoding), 1/8 .. about 7844
402 // length = 2^(length_encoded/8) / 8
403 // if pitch is 90, yaw does nothing and therefore indicates the sign (yaw is then either 11111 or 11110); 11111 is pointing DOWN
404 // thus, valid values are from 0000.11110.0000000 to 1111.11111.1111111
405 // the special value 0 indicates the zero vector
407 float lengthLogTable[128];
409 float invertLengthLog(float x)
413 if(x >= lengthLogTable[127])
415 if(x <= lengthLogTable[0])
423 m = floor((l + r) / 2);
424 if(lengthLogTable[m] < x)
430 // now: r is >=, l is <
431 float lerr = (x - lengthLogTable[l]);
432 float rerr = (lengthLogTable[r] - x);
438 vector decompressShortVector(int data)
443 float p = (data & 0xF000) / 0x1000;
444 float y = (data & 0x0F80) / 0x80;
445 int len = (data & 0x007F);
447 //print("\ndecompress: p ", ftos(p)); print("y ", ftos(y)); print("len ", ftos(len), "\n");
460 y = .19634954084936207740 * y;
461 p = .19634954084936207740 * p - 1.57079632679489661922;
462 out.x = cos(y) * cos(p);
463 out.y = sin(y) * cos(p);
467 //print("decompressed: ", vtos(out), "\n");
469 return out * lengthLogTable[len];
472 float compressShortVector(vector vec)
478 //print("compress: ", vtos(vec), "\n");
479 ang = vectoangles(vec);
483 if(ang.x < -90 && ang.x > +90)
484 error("BOGUS vectoangles");
485 //print("angles: ", vtos(ang), "\n");
487 p = floor(0.5 + (ang.x + 90) * 16 / 180) & 15; // -90..90 to 0..14
496 y = floor(0.5 + ang.y * 32 / 360) & 31; // 0..360 to 0..32
497 len = invertLengthLog(vlen(vec));
499 //print("compressed: p ", ftos(p)); print("y ", ftos(y)); print("len ", ftos(len), "\n");
501 return (p * 0x1000) + (y * 0x80) + len;
504 void compressShortVector_init()
507 float f = pow(2, 1/8);
509 for(i = 0; i < 128; ++i)
511 lengthLogTable[i] = l;
515 if(cvar("developer"))
517 LOG_INFO("Verifying vector compression table...\n");
518 for(i = 0x0F00; i < 0xFFFF; ++i)
519 if(i != compressShortVector(decompressShortVector(i)))
521 LOG_INFO("BROKEN vector compression: ", ftos(i));
522 LOG_INFO(" -> ", vtos(decompressShortVector(i)));
523 LOG_INFO(" -> ", ftos(compressShortVector(decompressShortVector(i))));
532 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz)
534 traceline(v0, v0 + dvx, true, forent); if(trace_fraction < 1) return 0;
535 traceline(v0, v0 + dvy, true, forent); if(trace_fraction < 1) return 0;
536 traceline(v0, v0 + dvz, true, forent); if(trace_fraction < 1) return 0;
537 traceline(v0 + dvx, v0 + dvx + dvy, true, forent); if(trace_fraction < 1) return 0;
538 traceline(v0 + dvx, v0 + dvx + dvz, true, forent); if(trace_fraction < 1) return 0;
539 traceline(v0 + dvy, v0 + dvy + dvx, true, forent); if(trace_fraction < 1) return 0;
540 traceline(v0 + dvy, v0 + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
541 traceline(v0 + dvz, v0 + dvz + dvx, true, forent); if(trace_fraction < 1) return 0;
542 traceline(v0 + dvz, v0 + dvz + dvy, true, forent); if(trace_fraction < 1) return 0;
543 traceline(v0 + dvx + dvy, v0 + dvx + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
544 traceline(v0 + dvx + dvz, v0 + dvx + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
545 traceline(v0 + dvy + dvz, v0 + dvx + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
550 string fixPriorityList(string order, float from, float to, float subtract, float complete)
555 n = tokenize_console(order);
557 for(i = 0; i < n; ++i)
562 if(w >= from && w <= to)
563 neworder = strcat(neworder, ftos(w), " ");
567 if(w >= from && w <= to)
568 neworder = strcat(neworder, ftos(w), " ");
575 n = tokenize_console(neworder);
576 for(w = to; w >= from; --w)
578 for(i = 0; i < n; ++i)
579 if(stof(argv(i)) == w)
581 if(i == n) // not found
582 neworder = strcat(neworder, ftos(w), " ");
586 return substring(neworder, 0, strlen(neworder) - 1);
589 string mapPriorityList(string order, string(string) mapfunc)
594 n = tokenize_console(order);
596 for(i = 0; i < n; ++i)
597 neworder = strcat(neworder, mapfunc(argv(i)), " ");
599 return substring(neworder, 0, strlen(neworder) - 1);
602 string swapInPriorityList(string order, float i, float j)
607 n = tokenize_console(order);
609 if(i >= 0 && i < n && j >= 0 && j < n && i != j)
612 for(w = 0; w < n; ++w)
615 s = strcat(s, argv(j), " ");
617 s = strcat(s, argv(i), " ");
619 s = strcat(s, argv(w), " ");
621 return substring(s, 0, strlen(s) - 1);
627 float cvar_value_issafe(string s)
629 if(strstrofs(s, "\"", 0) >= 0)
631 if(strstrofs(s, "\\", 0) >= 0)
633 if(strstrofs(s, ";", 0) >= 0)
635 if(strstrofs(s, "$", 0) >= 0)
637 if(strstrofs(s, "\r", 0) >= 0)
639 if(strstrofs(s, "\n", 0) >= 0)
645 void get_mi_min_max(float mode)
650 strunzone(mi_shortname);
651 mi_shortname = mapname;
652 if(!strcasecmp(substring(mi_shortname, 0, 5), "maps/"))
653 mi_shortname = substring(mi_shortname, 5, strlen(mi_shortname) - 5);
654 if(!strcasecmp(substring(mi_shortname, strlen(mi_shortname) - 4, 4), ".bsp"))
655 mi_shortname = substring(mi_shortname, 0, strlen(mi_shortname) - 4);
656 mi_shortname = strzone(mi_shortname);
668 MapInfo_Get_ByName(mi_shortname, 0, 0);
669 if(MapInfo_Map_mins.x < MapInfo_Map_maxs.x)
671 mi_min = MapInfo_Map_mins;
672 mi_max = MapInfo_Map_maxs;
680 tracebox('1 0 0' * mi.x,
681 '0 1 0' * mi.y + '0 0 1' * mi.z,
682 '0 1 0' * ma.y + '0 0 1' * ma.z,
686 if(!trace_startsolid)
687 mi_min.x = trace_endpos.x;
689 tracebox('0 1 0' * mi.y,
690 '1 0 0' * mi.x + '0 0 1' * mi.z,
691 '1 0 0' * ma.x + '0 0 1' * ma.z,
695 if(!trace_startsolid)
696 mi_min.y = trace_endpos.y;
698 tracebox('0 0 1' * mi.z,
699 '1 0 0' * mi.x + '0 1 0' * mi.y,
700 '1 0 0' * ma.x + '0 1 0' * ma.y,
704 if(!trace_startsolid)
705 mi_min.z = trace_endpos.z;
707 tracebox('1 0 0' * ma.x,
708 '0 1 0' * mi.y + '0 0 1' * mi.z,
709 '0 1 0' * ma.y + '0 0 1' * ma.z,
713 if(!trace_startsolid)
714 mi_max.x = trace_endpos.x;
716 tracebox('0 1 0' * ma.y,
717 '1 0 0' * mi.x + '0 0 1' * mi.z,
718 '1 0 0' * ma.x + '0 0 1' * ma.z,
722 if(!trace_startsolid)
723 mi_max.y = trace_endpos.y;
725 tracebox('0 0 1' * ma.z,
726 '1 0 0' * mi.x + '0 1 0' * mi.y,
727 '1 0 0' * ma.x + '0 1 0' * ma.y,
731 if(!trace_startsolid)
732 mi_max.z = trace_endpos.z;
737 void get_mi_min_max_texcoords(float mode)
741 get_mi_min_max(mode);
746 // extend mi_picmax to get a square aspect ratio
747 // center the map in that area
748 extend = mi_picmax - mi_picmin;
749 if(extend.y > extend.x)
751 mi_picmin.x -= (extend.y - extend.x) * 0.5;
752 mi_picmax.x += (extend.y - extend.x) * 0.5;
756 mi_picmin.y -= (extend.x - extend.y) * 0.5;
757 mi_picmax.y += (extend.x - extend.y) * 0.5;
760 // add another some percent
761 extend = (mi_picmax - mi_picmin) * (1 / 64.0);
765 // calculate the texcoords
766 mi_pictexcoord0 = mi_pictexcoord1 = mi_pictexcoord2 = mi_pictexcoord3 = '0 0 0';
767 // first the two corners of the origin
768 mi_pictexcoord0_x = (mi_min.x - mi_picmin.x) / (mi_picmax.x - mi_picmin.x);
769 mi_pictexcoord0_y = (mi_min.y - mi_picmin.y) / (mi_picmax.y - mi_picmin.y);
770 mi_pictexcoord2_x = (mi_max.x - mi_picmin.x) / (mi_picmax.x - mi_picmin.x);
771 mi_pictexcoord2_y = (mi_max.y - mi_picmin.y) / (mi_picmax.y - mi_picmin.y);
772 // then the other corners
773 mi_pictexcoord1_x = mi_pictexcoord0_x;
774 mi_pictexcoord1_y = mi_pictexcoord2_y;
775 mi_pictexcoord3_x = mi_pictexcoord2_x;
776 mi_pictexcoord3_y = mi_pictexcoord0_y;
780 float cvar_settemp(string tmp_cvar, string tmp_value)
782 float created_saved_value;
785 created_saved_value = 0;
787 if (!(tmp_cvar || tmp_value))
789 LOG_TRACE("Error: Invalid usage of cvar_settemp(string, string); !\n");
793 if(!cvar_type(tmp_cvar))
795 LOG_INFOF("Error: cvar %s doesn't exist!\n", tmp_cvar);
799 for(e = world; (e = find(e, classname, "saved_cvar_value")); )
800 if(e.netname == tmp_cvar)
801 created_saved_value = -1; // skip creation
803 if(created_saved_value != -1)
805 // creating a new entity to keep track of this cvar
806 e = new(saved_cvar_value);
808 e.netname = strzone(tmp_cvar);
809 e.message = strzone(cvar_string(tmp_cvar));
810 created_saved_value = 1;
813 // update the cvar to the value given
814 cvar_set(tmp_cvar, tmp_value);
816 return created_saved_value;
819 float cvar_settemp_restore()
823 while((e = find(e, classname, "saved_cvar_value")))
825 if(cvar_type(e.netname))
827 cvar_set(e.netname, e.message);
832 LOG_INFOF("Error: cvar %s doesn't exist anymore! It can still be restored once it's manually recreated.\n", e.netname);
838 float rgb_mi_ma_to_hue(vector rgb, float mi, float ma)
845 return (rgb.y - rgb.z) / (ma - mi);
847 return (rgb.y - rgb.z) / (ma - mi) + 6;
850 return (rgb.z - rgb.x) / (ma - mi) + 2;
851 else // if(ma == rgb_z)
852 return (rgb.x - rgb.y) / (ma - mi) + 4;
855 vector hue_mi_ma_to_rgb(float hue, float mi, float ma)
859 hue -= 6 * floor(hue / 6);
861 //else if(ma == rgb_x)
862 // hue = 60 * (rgb_y - rgb_z) / (ma - mi);
866 rgb.y = hue * (ma - mi) + mi;
869 //else if(ma == rgb_y)
870 // hue = 60 * (rgb_z - rgb_x) / (ma - mi) + 120;
873 rgb.x = (2 - hue) * (ma - mi) + mi;
881 rgb.z = (hue - 2) * (ma - mi) + mi;
883 //else // if(ma == rgb_z)
884 // hue = 60 * (rgb_x - rgb_y) / (ma - mi) + 240;
888 rgb.y = (4 - hue) * (ma - mi) + mi;
893 rgb.x = (hue - 4) * (ma - mi) + mi;
897 //else if(ma == rgb_x)
898 // hue = 60 * (rgb_y - rgb_z) / (ma - mi);
903 rgb.z = (6 - hue) * (ma - mi) + mi;
909 vector rgb_to_hsv(vector rgb)
914 mi = min(rgb.x, rgb.y, rgb.z);
915 ma = max(rgb.x, rgb.y, rgb.z);
917 hsv.x = rgb_mi_ma_to_hue(rgb, mi, ma);
928 vector hsv_to_rgb(vector hsv)
930 return hue_mi_ma_to_rgb(hsv.x, hsv.z * (1 - hsv.y), hsv.z);
933 vector rgb_to_hsl(vector rgb)
938 mi = min(rgb.x, rgb.y, rgb.z);
939 ma = max(rgb.x, rgb.y, rgb.z);
941 hsl.x = rgb_mi_ma_to_hue(rgb, mi, ma);
943 hsl.z = 0.5 * (mi + ma);
946 else if(hsl.z <= 0.5)
947 hsl.y = (ma - mi) / (2*hsl.z);
948 else // if(hsl_z > 0.5)
949 hsl.y = (ma - mi) / (2 - 2*hsl.z);
954 vector hsl_to_rgb(vector hsl)
956 float mi, ma, maminusmi;
959 maminusmi = hsl.y * 2 * hsl.z;
961 maminusmi = hsl.y * (2 - 2 * hsl.z);
963 // hsl_z = 0.5 * mi + 0.5 * ma
964 // maminusmi = - mi + ma
965 mi = hsl.z - 0.5 * maminusmi;
966 ma = hsl.z + 0.5 * maminusmi;
968 return hue_mi_ma_to_rgb(hsl.x, mi, ma);
971 string rgb_to_hexcolor(vector rgb)
976 DEC_TO_HEXDIGIT(floor(rgb.x * 15 + 0.5)),
977 DEC_TO_HEXDIGIT(floor(rgb.y * 15 + 0.5)),
978 DEC_TO_HEXDIGIT(floor(rgb.z * 15 + 0.5))
982 float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
985 // The following function is SLOW.
986 // For your safety and for the protection of those around you...
987 // DO NOT CALL THIS AT HOME.
989 if(w(theText, theSize) <= maxWidth)
990 return strlen(theText); // yeah!
992 // binary search for right place to cut string
994 float left, right, middle; // this always works
996 right = strlen(theText); // this always fails
999 middle = floor((left + right) / 2);
1000 if(w(substring(theText, 0, middle), theSize) <= maxWidth)
1005 while(left < right - 1);
1007 if(w("^7", theSize) == 0) // detect color codes support in the width function
1009 // NOTE: when color codes are involved, this binary search is,
1010 // mathematically, BROKEN. However, it is obviously guaranteed to
1011 // terminate, as the range still halves each time - but nevertheless, it is
1012 // guaranteed that it finds ONE valid cutoff place (where "left" is in
1013 // range, and "right" is outside).
1015 // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4)
1016 // and decrease left on the basis of the chars detected of the truncated tag
1017 // Even if the ^xrgb tag is not complete/correct, left is decreased
1018 // (sometimes too much but with a correct result)
1019 // it fixes also ^[0-9]
1020 while(left >= 1 && substring(theText, left-1, 1) == "^")
1023 if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/
1025 else if (left >= 3 && substring(theText, left-3, 2) == "^x")
1027 ch = str2chr(theText, left-1);
1028 if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/
1031 else if (left >= 4 && substring(theText, left-4, 2) == "^x")
1033 ch = str2chr(theText, left-2);
1034 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
1036 ch = str2chr(theText, left-1);
1037 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/
1046 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t w)
1049 // The following function is SLOW.
1050 // For your safety and for the protection of those around you...
1051 // DO NOT CALL THIS AT HOME.
1052 // No really, don't.
1053 if(w(theText) <= maxWidth)
1054 return strlen(theText); // yeah!
1056 // binary search for right place to cut string
1058 float left, right, middle; // this always works
1060 right = strlen(theText); // this always fails
1063 middle = floor((left + right) / 2);
1064 if(w(substring(theText, 0, middle)) <= maxWidth)
1069 while(left < right - 1);
1071 if(w("^7") == 0) // detect color codes support in the width function
1073 // NOTE: when color codes are involved, this binary search is,
1074 // mathematically, BROKEN. However, it is obviously guaranteed to
1075 // terminate, as the range still halves each time - but nevertheless, it is
1076 // guaranteed that it finds ONE valid cutoff place (where "left" is in
1077 // range, and "right" is outside).
1079 // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4)
1080 // and decrease left on the basis of the chars detected of the truncated tag
1081 // Even if the ^xrgb tag is not complete/correct, left is decreased
1082 // (sometimes too much but with a correct result)
1083 // it fixes also ^[0-9]
1084 while(left >= 1 && substring(theText, left-1, 1) == "^")
1087 if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/
1089 else if (left >= 3 && substring(theText, left-3, 2) == "^x")
1091 ch = str2chr(theText, left-1);
1092 if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/
1095 else if (left >= 4 && substring(theText, left-4, 2) == "^x")
1097 ch = str2chr(theText, left-2);
1098 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
1100 ch = str2chr(theText, left-1);
1101 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/
1110 string find_last_color_code(string s)
1112 int start = strstrofs(s, "^", 0);
1113 if (start == -1) // no caret found
1115 int len = strlen(s)-1;
1117 for(i = len; i >= start; --i)
1119 if(substring(s, i, 1) != "^")
1123 while (i-carets >= start && substring(s, i-carets, 1) == "^")
1126 // check if carets aren't all escaped
1130 if(strstrofs("0123456789", substring(s, i+1, 1), 0) >= 0)
1131 return substring(s, i, 2);
1134 if(substring(s, i+1, 1) == "x")
1135 if(strstrofs("0123456789abcdefABCDEF", substring(s, i+2, 1), 0) >= 0)
1136 if(strstrofs("0123456789abcdefABCDEF", substring(s, i+3, 1), 0) >= 0)
1137 if(strstrofs("0123456789abcdefABCDEF", substring(s, i+4, 1), 0) >= 0)
1138 return substring(s, i, 5);
1140 i -= carets; // this also skips one char before the carets
1146 string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
1152 s = getWrappedLine_remaining;
1156 getWrappedLine_remaining = string_null;
1157 return s; // the line has no size ANYWAY, nothing would be displayed.
1160 cantake = textLengthUpToWidth(s, w, theFontSize, tw);
1161 if(cantake > 0 && cantake < strlen(s))
1164 while(take > 0 && substring(s, take, 1) != " ")
1168 getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
1169 if(getWrappedLine_remaining == "")
1170 getWrappedLine_remaining = string_null;
1171 else if (tw("^7", theFontSize) == 0)
1172 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
1173 return substring(s, 0, cantake);
1177 getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
1178 if(getWrappedLine_remaining == "")
1179 getWrappedLine_remaining = string_null;
1180 else if (tw("^7", theFontSize) == 0)
1181 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
1182 return substring(s, 0, take);
1187 getWrappedLine_remaining = string_null;
1192 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw)
1198 s = getWrappedLine_remaining;
1202 getWrappedLine_remaining = string_null;
1203 return s; // the line has no size ANYWAY, nothing would be displayed.
1206 cantake = textLengthUpToLength(s, w, tw);
1207 if(cantake > 0 && cantake < strlen(s))
1210 while(take > 0 && substring(s, take, 1) != " ")
1214 getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
1215 if(getWrappedLine_remaining == "")
1216 getWrappedLine_remaining = string_null;
1217 else if (tw("^7") == 0)
1218 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
1219 return substring(s, 0, cantake);
1223 getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
1224 if(getWrappedLine_remaining == "")
1225 getWrappedLine_remaining = string_null;
1226 else if (tw("^7") == 0)
1227 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
1228 return substring(s, 0, take);
1233 getWrappedLine_remaining = string_null;
1238 string textShortenToWidth(string theText, float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
1240 if(tw(theText, theFontSize) <= maxWidth)
1243 return strcat(substring(theText, 0, textLengthUpToWidth(theText, maxWidth - tw("...", theFontSize), theFontSize, tw)), "...");
1246 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw)
1248 if(tw(theText) <= maxWidth)
1251 return strcat(substring(theText, 0, textLengthUpToLength(theText, maxWidth - tw("..."), tw)), "...");
1254 float isGametypeInFilter(float gt, float tp, float ts, string pattern)
1256 string subpattern, subpattern2, subpattern3, subpattern4;
1257 subpattern = strcat(",", MapInfo_Type_ToString(gt), ",");
1259 subpattern2 = ",teams,";
1261 subpattern2 = ",noteams,";
1263 subpattern3 = ",teamspawns,";
1265 subpattern3 = ",noteamspawns,";
1266 if(gt == MAPINFO_TYPE_RACE || gt == MAPINFO_TYPE_CTS)
1267 subpattern4 = ",race,";
1269 subpattern4 = string_null;
1271 if(substring(pattern, 0, 1) == "-")
1273 pattern = substring(pattern, 1, strlen(pattern) - 1);
1274 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) >= 0)
1276 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) >= 0)
1278 if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) >= 0)
1280 if(subpattern4 && strstrofs(strcat(",", pattern, ","), subpattern4, 0) >= 0)
1285 if(substring(pattern, 0, 1) == "+")
1286 pattern = substring(pattern, 1, strlen(pattern) - 1);
1287 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) < 0)
1288 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) < 0)
1289 if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) < 0)
1293 if(strstrofs(strcat(",", pattern, ","), subpattern4, 0) < 0)
1300 vector solve_quadratic(float a, float b, float c) // ax^2 + bx + c = 0
1316 // actually, every number solves the equation!
1327 if(a > 0) // put the smaller solution first
1329 v.x = ((-b)-D) / (2*a);
1330 v.y = ((-b)+D) / (2*a);
1334 v.x = (-b+D) / (2*a);
1335 v.y = (-b-D) / (2*a);
1341 // complex solutions!
1354 vector solve_shotdirection(vector myorg, vector myvel, vector eorg, vector evel, float spd, float newton_style)
1358 // make origin and speed relative
1363 // now solve for ret, ret normalized:
1364 // eorg + t * evel == t * ret * spd
1365 // or, rather, solve for t:
1366 // |eorg + t * evel| == t * spd
1367 // eorg^2 + t^2 * evel^2 + 2 * t * (eorg * evel) == t^2 * spd^2
1368 // t^2 * (evel^2 - spd^2) + t * (2 * (eorg * evel)) + eorg^2 == 0
1369 vector solution = solve_quadratic(evel * evel - spd * spd, 2 * (eorg * evel), eorg * eorg);
1370 // p = 2 * (eorg * evel) / (evel * evel - spd * spd)
1371 // q = (eorg * eorg) / (evel * evel - spd * spd)
1372 if(!solution.z) // no real solution
1375 // (eorg * evel)^2 < (evel^2 - spd^2) * eorg^2
1376 // (eorg * evel)^2 / eorg^2 < evel^2 - spd^2
1377 // spd^2 < ((evel^2 * eorg^2) - (eorg * evel)^2) / eorg^2
1378 // spd^2 < evel^2 * (1 - cos^2 angle(evel, eorg))
1379 // spd^2 < evel^2 * sin^2 angle(evel, eorg)
1380 // spd < |evel| * sin angle(evel, eorg)
1383 else if(solution.x > 0)
1385 // both solutions > 0: take the smaller one
1386 // happens if p < 0 and q > 0
1387 ret = normalize(eorg + solution.x * evel);
1389 else if(solution.y > 0)
1391 // one solution > 0: take the larger one
1392 // happens if q < 0 or q == 0 and p < 0
1393 ret = normalize(eorg + solution.y * evel);
1397 // no solution > 0: reject
1398 // happens if p > 0 and q >= 0
1399 // 2 * (eorg * evel) / (evel * evel - spd * spd) > 0
1400 // (eorg * eorg) / (evel * evel - spd * spd) >= 0
1405 // "Enemy is moving away from me at more than spd"
1409 // NOTE: we always got a solution if spd > |evel|
1411 if(newton_style == 2)
1412 ret = normalize(ret * spd + myvel);
1417 vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_style, float mi, float ma)
1422 if(newton_style == 2)
1424 // true Newtonian projectiles with automatic aim adjustment
1426 // solve: |outspeed * mydir - myvel| = spd
1427 // outspeed^2 - 2 * outspeed * (mydir * myvel) + myvel^2 - spd^2 = 0
1428 // outspeed = (mydir * myvel) +- sqrt((mydir * myvel)^2 - myvel^2 + spd^2)
1432 // myvel^2 - (mydir * myvel)^2 > spd^2
1433 // velocity without mydir component > spd
1434 // fire at smallest possible spd that works?
1435 // |(mydir * myvel) * myvel - myvel| = spd
1437 vector solution = solve_quadratic(1, -2 * (mydir * myvel), myvel * myvel - spd * spd);
1441 outspeed = solution.y; // the larger one
1444 //outspeed = 0; // slowest possible shot
1445 outspeed = solution.x; // the real part (that is, the average!)
1446 //dprint("impossible shot, adjusting\n");
1449 outspeed = bound(spd * mi, outspeed, spd * ma);
1450 return mydir * outspeed;
1454 return myvel + spd * mydir;
1457 float compressShotOrigin(vector v)
1461 y = rint(v.y * 4) + 128;
1462 z = rint(v.z * 4) + 128;
1463 if(x > 255 || x < 0)
1465 LOG_INFO("shot origin ", vtos(v), " x out of bounds\n");
1466 x = bound(0, x, 255);
1468 if(y > 255 || y < 0)
1470 LOG_INFO("shot origin ", vtos(v), " y out of bounds\n");
1471 y = bound(0, y, 255);
1473 if(z > 255 || z < 0)
1475 LOG_INFO("shot origin ", vtos(v), " z out of bounds\n");
1476 z = bound(0, z, 255);
1478 return x * 0x10000 + y * 0x100 + z;
1480 vector decompressShotOrigin(int f)
1483 v.x = ((f & 0xFF0000) / 0x10000) / 2;
1484 v.y = ((f & 0xFF00) / 0x100 - 128) / 4;
1485 v.z = ((f & 0xFF) - 128) / 4;
1490 vector healtharmor_maxdamage(float h, float a, float armorblock, int deathtype)
1492 // NOTE: we'll always choose the SMALLER value...
1493 float healthdamage, armordamage, armorideal;
1494 if (DEATH_IS(deathtype, DEATH_DROWN)) // Why should armor help here...
1497 healthdamage = (h - 1) / (1 - armorblock); // damage we can take if we could use more health
1498 armordamage = a + (h - 1); // damage we can take if we could use more armor
1499 armorideal = healthdamage * armorblock;
1501 if(armordamage < healthdamage)
1514 vector healtharmor_applydamage(float a, float armorblock, int deathtype, float damage)
1517 if (DEATH_IS(deathtype, DEATH_DROWN)) // Why should armor help here...
1519 v.y = bound(0, damage * armorblock, a); // save
1520 v.x = bound(0, damage - v.y, damage); // take
1526 string getcurrentmod()
1530 m = cvar_string("fs_gamedir");
1531 n = tokenize_console(m);
1538 // from the GNU Scientific Library
1539 float gsl_ran_gaussian_lastvalue;
1540 float gsl_ran_gaussian_lastvalue_set;
1541 float gsl_ran_gaussian(float sigma)
1544 if(gsl_ran_gaussian_lastvalue_set)
1546 gsl_ran_gaussian_lastvalue_set = 0;
1547 return sigma * gsl_ran_gaussian_lastvalue;
1551 a = random() * 2 * M_PI;
1552 b = sqrt(-2 * log(random()));
1553 gsl_ran_gaussian_lastvalue = cos(a) * b;
1554 gsl_ran_gaussian_lastvalue_set = 1;
1555 return sigma * sin(a) * b;
1559 float matchacl(string acl, string str)
1566 t = car(acl); acl = cdr(acl);
1569 if(substring(t, 0, 1) == "-")
1572 t = substring(t, 1, strlen(t) - 1);
1574 else if(substring(t, 0, 1) == "+")
1575 t = substring(t, 1, strlen(t) - 1);
1577 if(substring(t, -1, 1) == "*")
1579 t = substring(t, 0, strlen(t) - 1);
1580 s = substring(str, 0, strlen(t));
1593 string get_model_datafilename(string m, float sk, string fil)
1598 m = "models/player/*_";
1600 m = strcat(m, ftos(sk));
1603 return strcat(m, ".", fil);
1606 float get_model_parameters(string m, float sk)
1608 get_model_parameters_modelname = string_null;
1609 get_model_parameters_modelskin = -1;
1610 get_model_parameters_name = string_null;
1611 get_model_parameters_species = -1;
1612 get_model_parameters_sex = string_null;
1613 get_model_parameters_weight = -1;
1614 get_model_parameters_age = -1;
1615 get_model_parameters_desc = string_null;
1616 get_model_parameters_bone_upperbody = string_null;
1617 get_model_parameters_bone_weapon = string_null;
1618 for(int i = 0; i < MAX_AIM_BONES; ++i)
1620 get_model_parameters_bone_aim[i] = string_null;
1621 get_model_parameters_bone_aimweight[i] = 0;
1623 get_model_parameters_fixbone = 0;
1626 MUTATOR_CALLHOOK(ClearModelParams);
1632 if(substring(m, -9, 5) == "_lod1" || substring(m, -9, 5) == "_lod2")
1633 m = strcat(substring(m, 0, -10), substring(m, -4, -1));
1637 if(substring(m, -4, -1) != ".txt")
1639 if(substring(m, -6, 1) != "_")
1641 sk = stof(substring(m, -5, 1));
1642 m = substring(m, 0, -7);
1645 string fn = get_model_datafilename(m, sk, "txt");
1646 int fh = fopen(fn, FILE_READ);
1650 fn = get_model_datafilename(m, sk, "txt");
1651 fh = fopen(fn, FILE_READ);
1656 get_model_parameters_modelname = m;
1657 get_model_parameters_modelskin = sk;
1659 while((s = fgets(fh)))
1662 break; // next lines will be description
1666 get_model_parameters_name = s;
1670 case "human": get_model_parameters_species = SPECIES_HUMAN; break;
1671 case "alien": get_model_parameters_species = SPECIES_ALIEN; break;
1672 case "robot_shiny": get_model_parameters_species = SPECIES_ROBOT_SHINY; break;
1673 case "robot_rusty": get_model_parameters_species = SPECIES_ROBOT_RUSTY; break;
1674 case "robot_solid": get_model_parameters_species = SPECIES_ROBOT_SOLID; break;
1675 case "animal": get_model_parameters_species = SPECIES_ANIMAL; break;
1676 case "reserved": get_model_parameters_species = SPECIES_RESERVED; break;
1679 get_model_parameters_sex = s;
1681 get_model_parameters_weight = stof(s);
1683 get_model_parameters_age = stof(s);
1684 if(c == "description")
1685 get_model_parameters_description = s;
1686 if(c == "bone_upperbody")
1687 get_model_parameters_bone_upperbody = s;
1688 if(c == "bone_weapon")
1689 get_model_parameters_bone_weapon = s;
1691 MUTATOR_CALLHOOK(GetModelParams, c, s);
1693 for(int i = 0; i < MAX_AIM_BONES; ++i)
1694 if(c == strcat("bone_aim", ftos(i)))
1696 get_model_parameters_bone_aimweight[i] = stof(car(s));
1697 get_model_parameters_bone_aim[i] = cdr(s);
1700 get_model_parameters_fixbone = stof(s);
1703 while((s = fgets(fh)))
1705 if(get_model_parameters_desc)
1706 get_model_parameters_desc = strcat(get_model_parameters_desc, "\n");
1708 get_model_parameters_desc = strcat(get_model_parameters_desc, s);
1716 float vercmp_recursive(string v1, string v2)
1722 dot1 = strstrofs(v1, ".", 0);
1723 dot2 = strstrofs(v2, ".", 0);
1727 s1 = substring(v1, 0, dot1);
1731 s2 = substring(v2, 0, dot2);
1733 r = stof(s1) - stof(s2);
1737 r = strcasecmp(s1, s2);
1750 return vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
1753 float vercmp(string v1, string v2)
1755 if(strcasecmp(v1, v2) == 0) // early out check
1764 return vercmp_recursive(v1, v2);
1767 // x-encoding (encoding as zero length invisible string)
1768 const string XENCODE_2 = "xX";
1769 const string XENCODE_22 = "0123456789abcdefABCDEF";
1770 string xencode(int f)
1773 d = f % 22; f = floor(f / 22);
1774 c = f % 22; f = floor(f / 22);
1775 b = f % 22; f = floor(f / 22);
1776 a = f % 2; // f = floor(f / 2);
1779 substring(XENCODE_2, a, 1),
1780 substring(XENCODE_22, b, 1),
1781 substring(XENCODE_22, c, 1),
1782 substring(XENCODE_22, d, 1)
1785 float xdecode(string s)
1788 if(substring(s, 0, 1) != "^")
1792 a = strstrofs(XENCODE_2, substring(s, 1, 1), 0);
1793 b = strstrofs(XENCODE_22, substring(s, 2, 1), 0);
1794 c = strstrofs(XENCODE_22, substring(s, 3, 1), 0);
1795 d = strstrofs(XENCODE_22, substring(s, 4, 1), 0);
1796 if(a < 0 || b < 0 || c < 0 || d < 0)
1798 return ((a * 22 + b) * 22 + c) * 22 + d;
1802 string strlimitedlen(string input, string truncation, float strip_colors, float limit)
1804 if(strlen((strip_colors ? strdecolorize(input) : input)) <= limit)
1807 return strcat(substring(input, 0, (strlen(input) - strlen(truncation))), truncation);
1811 entity ReadCSQCEntity()
1813 int f = ReadShort();
1816 return findfloat(world, entnum, f);
1820 float shutdown_running;
1825 void CSQC_Shutdown()
1831 if(shutdown_running)
1833 LOG_INFO("Recursive shutdown detected! Only restoring cvars...\n");
1837 shutdown_running = 1;
1840 cvar_settemp_restore(); // this must be done LAST, but in any case
1843 const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
1844 #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
1845 #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
1846 // this will use the value:
1848 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
1849 // accuracy at x is 1/derivative, i.e.
1850 // APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
1852 void WriteApproxPastTime(float dst, float t)
1854 float dt = time - t;
1856 // warning: this is approximate; do not resend when you don't have to!
1857 // be careful with sendflags here!
1858 // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
1861 dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
1864 dt = rint(bound(0, dt, 255));
1870 float ReadApproxPastTime()
1872 float dt = ReadByte();
1874 // map from range...PPROXPASTTIME_MAX / 256
1875 dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
1877 return servertime - dt;
1882 .float skeleton_bones_index;
1883 void Skeleton_SetBones(entity e)
1885 // set skeleton_bones to the total number of bones on the model
1886 if(e.skeleton_bones_index == e.modelindex)
1887 return; // same model, nothing to update
1890 skelindex = skel_create(e.modelindex);
1891 e.skeleton_bones = skel_get_numbones(skelindex);
1892 skel_delete(skelindex);
1893 e.skeleton_bones_index = e.modelindex;
1897 string to_execute_next_frame;
1898 void execute_next_frame()
1900 if(to_execute_next_frame)
1902 localcmd("\n", to_execute_next_frame, "\n");
1903 strunzone(to_execute_next_frame);
1904 to_execute_next_frame = string_null;
1907 void queue_to_execute_next_frame(string s)
1909 if(to_execute_next_frame)
1911 s = strcat(s, "\n", to_execute_next_frame);
1912 strunzone(to_execute_next_frame);
1914 to_execute_next_frame = strzone(s);
1917 .float FindConnectedComponent_processing;
1918 void FindConnectedComponent(entity e, .entity fld, findNextEntityNearFunction_t nxt, isConnectedFunction_t iscon, entity pass)
1920 entity queue_start, queue_end;
1922 // we build a queue of to-be-processed entities.
1923 // queue_start is the next entity to be checked for neighbors
1924 // queue_end is the last entity added
1926 if(e.FindConnectedComponent_processing)
1927 error("recursion or broken cleanup");
1929 // start with a 1-element queue
1930 queue_start = queue_end = e;
1931 queue_end.(fld) = world;
1932 queue_end.FindConnectedComponent_processing = 1;
1934 // for each queued item:
1935 for (; queue_start; queue_start = queue_start.(fld))
1937 // find all neighbors of queue_start
1939 for(t = world; (t = nxt(t, queue_start, pass)); )
1941 if(t.FindConnectedComponent_processing)
1943 if(iscon(t, queue_start, pass))
1945 // it is connected? ADD IT. It will look for neighbors soon too.
1946 queue_end.(fld) = t;
1948 queue_end.(fld) = world;
1949 queue_end.FindConnectedComponent_processing = 1;
1955 for (queue_start = e; queue_start; queue_start = queue_start.(fld))
1956 queue_start.FindConnectedComponent_processing = 0;
1960 vector animfixfps(entity e, vector a, vector b)
1962 // multi-frame anim: keep as-is
1966 dur = frameduration(e.modelindex, a.x);
1970 dur = frameduration(e.modelindex, a.x);
1980 void dedicated_print(string input) // print(), but only print if the server is not local
1982 if(server_is_dedicated) { LOG_INFO(input); }
1987 float Announcer_PickNumber(float type, float num)
1995 case 10: return ANNCE_NUM_GAMESTART_10;
1996 case 9: return ANNCE_NUM_GAMESTART_9;
1997 case 8: return ANNCE_NUM_GAMESTART_8;
1998 case 7: return ANNCE_NUM_GAMESTART_7;
1999 case 6: return ANNCE_NUM_GAMESTART_6;
2000 case 5: return ANNCE_NUM_GAMESTART_5;
2001 case 4: return ANNCE_NUM_GAMESTART_4;
2002 case 3: return ANNCE_NUM_GAMESTART_3;
2003 case 2: return ANNCE_NUM_GAMESTART_2;
2004 case 1: return ANNCE_NUM_GAMESTART_1;
2012 case 10: return ANNCE_NUM_IDLE_10;
2013 case 9: return ANNCE_NUM_IDLE_9;
2014 case 8: return ANNCE_NUM_IDLE_8;
2015 case 7: return ANNCE_NUM_IDLE_7;
2016 case 6: return ANNCE_NUM_IDLE_6;
2017 case 5: return ANNCE_NUM_IDLE_5;
2018 case 4: return ANNCE_NUM_IDLE_4;
2019 case 3: return ANNCE_NUM_IDLE_3;
2020 case 2: return ANNCE_NUM_IDLE_2;
2021 case 1: return ANNCE_NUM_IDLE_1;
2029 case 10: return ANNCE_NUM_KILL_10;
2030 case 9: return ANNCE_NUM_KILL_9;
2031 case 8: return ANNCE_NUM_KILL_8;
2032 case 7: return ANNCE_NUM_KILL_7;
2033 case 6: return ANNCE_NUM_KILL_6;
2034 case 5: return ANNCE_NUM_KILL_5;
2035 case 4: return ANNCE_NUM_KILL_4;
2036 case 3: return ANNCE_NUM_KILL_3;
2037 case 2: return ANNCE_NUM_KILL_2;
2038 case 1: return ANNCE_NUM_KILL_1;
2046 case 10: return ANNCE_NUM_RESPAWN_10;
2047 case 9: return ANNCE_NUM_RESPAWN_9;
2048 case 8: return ANNCE_NUM_RESPAWN_8;
2049 case 7: return ANNCE_NUM_RESPAWN_7;
2050 case 6: return ANNCE_NUM_RESPAWN_6;
2051 case 5: return ANNCE_NUM_RESPAWN_5;
2052 case 4: return ANNCE_NUM_RESPAWN_4;
2053 case 3: return ANNCE_NUM_RESPAWN_3;
2054 case 2: return ANNCE_NUM_RESPAWN_2;
2055 case 1: return ANNCE_NUM_RESPAWN_1;
2059 case CNT_ROUNDSTART:
2063 case 10: return ANNCE_NUM_ROUNDSTART_10;
2064 case 9: return ANNCE_NUM_ROUNDSTART_9;
2065 case 8: return ANNCE_NUM_ROUNDSTART_8;
2066 case 7: return ANNCE_NUM_ROUNDSTART_7;
2067 case 6: return ANNCE_NUM_ROUNDSTART_6;
2068 case 5: return ANNCE_NUM_ROUNDSTART_5;
2069 case 4: return ANNCE_NUM_ROUNDSTART_4;
2070 case 3: return ANNCE_NUM_ROUNDSTART_3;
2071 case 2: return ANNCE_NUM_ROUNDSTART_2;
2072 case 1: return ANNCE_NUM_ROUNDSTART_1;
2080 case 10: return ANNCE_NUM_10;
2081 case 9: return ANNCE_NUM_9;
2082 case 8: return ANNCE_NUM_8;
2083 case 7: return ANNCE_NUM_7;
2084 case 6: return ANNCE_NUM_6;
2085 case 5: return ANNCE_NUM_5;
2086 case 4: return ANNCE_NUM_4;
2087 case 3: return ANNCE_NUM_3;
2088 case 2: return ANNCE_NUM_2;
2089 case 1: return ANNCE_NUM_1;
2094 return NOTIF_ABORT; // abort sending if none of these numbers were right
2099 int Mod_Q1BSP_SuperContentsFromNativeContents(int nativecontents)
2101 switch(nativecontents)
2106 return DPCONTENTS_SOLID | DPCONTENTS_OPAQUE;
2108 return DPCONTENTS_WATER;
2110 return DPCONTENTS_SLIME;
2112 return DPCONTENTS_LAVA | DPCONTENTS_NODROP;
2114 return DPCONTENTS_SKY | DPCONTENTS_NODROP | DPCONTENTS_OPAQUE; // to match behaviour of Q3 maps, let sky count as opaque
2119 int Mod_Q1BSP_NativeContentsFromSuperContents(int supercontents)
2121 if(supercontents & (DPCONTENTS_SOLID | DPCONTENTS_BODY))
2122 return CONTENT_SOLID;
2123 if(supercontents & DPCONTENTS_SKY)
2125 if(supercontents & DPCONTENTS_LAVA)
2126 return CONTENT_LAVA;
2127 if(supercontents & DPCONTENTS_SLIME)
2128 return CONTENT_SLIME;
2129 if(supercontents & DPCONTENTS_WATER)
2130 return CONTENT_WATER;
2131 return CONTENT_EMPTY;