4 #include "../client/defs.qh"
5 #include "constants.qh"
6 #include "../client/mutators/events.qh"
8 #include "notifications/all.qh"
9 #include <common/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/all.qh"
17 #include <common/deathtypes/all.qh>
23 * Get "real" origin, in worldspace, even if ent is attached to something else.
25 vector real_origin(entity ent)
28 vector v = ((ent.absmin + ent.absmax) * 0.5);
33 v = v + ((e.absmin + e.absmax) * 0.5);
41 string wordwrap_buffer;
43 void wordwrap_buffer_put(string s)
45 wordwrap_buffer = strcat(wordwrap_buffer, s);
48 string wordwrap(string s, float l)
52 wordwrap_cb(s, l, wordwrap_buffer_put);
59 entity _wordwrap_buffer_sprint_ent;
60 void wordwrap_buffer_sprint(string s)
62 wordwrap_buffer = strcat(wordwrap_buffer, s);
65 sprint(_wordwrap_buffer_sprint_ent, wordwrap_buffer);
70 void wordwrap_sprint(entity to, string s, float l)
73 _wordwrap_buffer_sprint_ent = to;
74 wordwrap_cb(s, l, wordwrap_buffer_sprint);
75 _wordwrap_buffer_sprint_ent = NULL;
76 if(wordwrap_buffer != "")
77 sprint(to, strcat(wordwrap_buffer, "\n"));
84 string draw_UseSkinFor(string pic)
86 if(substring(pic, 0, 1) == "/")
87 return substring(pic, 1, strlen(pic)-1);
89 return strcat(draw_currentSkin, "/", pic);
93 void wordwrap_cb(string s, float l, void(string) callback)
96 float lleft, i, j, wlen;
100 for (i = 0;i < strlen(s);++i)
102 if (substring(s, i, 2) == "\\n")
108 else if (substring(s, i, 1) == "\n")
113 else if (substring(s, i, 1) == " ")
123 for (j = i+1;j < strlen(s);++j)
124 // ^^ this skips over the first character of a word, which
125 // is ALWAYS part of the word
126 // this is safe since if i+1 == strlen(s), i will become
127 // strlen(s)-1 at the end of this block and the function
128 // will terminate. A space can't be the first character we
129 // read here, and neither can a \n be the start, since these
130 // two cases have been handled above.
132 c = substring(s, j, 1);
139 // we need to keep this tempstring alive even if substring is
140 // called repeatedly, so call strcat even though we're not
150 callback(substring(s, i, wlen));
151 lleft = lleft - wlen;
158 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass)
187 string ScoreString(int pFlags, float pValue)
192 pValue = floor(pValue + 0.5); // round
194 if((pValue == 0) && (pFlags & (SFL_HIDE_ZERO | SFL_RANK | SFL_TIME)))
196 else if(pFlags & SFL_RANK)
198 valstr = ftos(pValue);
200 if((l >= 2) && (substring(valstr, l - 2, 1) == "1"))
201 valstr = strcat(valstr, "th");
202 else if(substring(valstr, l - 1, 1) == "1")
203 valstr = strcat(valstr, "st");
204 else if(substring(valstr, l - 1, 1) == "2")
205 valstr = strcat(valstr, "nd");
206 else if(substring(valstr, l - 1, 1) == "3")
207 valstr = strcat(valstr, "rd");
209 valstr = strcat(valstr, "th");
211 else if(pFlags & SFL_TIME)
212 valstr = TIME_ENCODED_TOSTRING(pValue);
214 valstr = ftos(pValue);
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
229 float lengthLogTable[128];
231 float invertLengthLog(float x)
235 if(x >= lengthLogTable[127])
237 if(x <= lengthLogTable[0])
245 m = floor((l + r) / 2);
246 if(lengthLogTable[m] < x)
252 // now: r is >=, l is <
253 float lerr = (x - lengthLogTable[l]);
254 float rerr = (lengthLogTable[r] - x);
260 vector decompressShortVector(int data)
265 float p = (data & 0xF000) / 0x1000;
266 float y = (data & 0x0F80) / 0x80;
267 int len = (data & 0x007F);
269 //print("\ndecompress: p ", ftos(p)); print("y ", ftos(y)); print("len ", ftos(len), "\n");
282 y = .19634954084936207740 * y;
283 p = .19634954084936207740 * p - 1.57079632679489661922;
284 out.x = cos(y) * cos(p);
285 out.y = sin(y) * cos(p);
289 //print("decompressed: ", vtos(out), "\n");
291 return out * lengthLogTable[len];
294 float compressShortVector(vector vec)
300 //print("compress: ", vtos(vec), "\n");
301 ang = vectoangles(vec);
305 if(ang.x < -90 && ang.x > +90)
306 error("BOGUS vectoangles");
307 //print("angles: ", vtos(ang), "\n");
309 p = floor(0.5 + (ang.x + 90) * 16 / 180) & 15; // -90..90 to 0..14
318 y = floor(0.5 + ang.y * 32 / 360) & 31; // 0..360 to 0..32
319 len = invertLengthLog(vlen(vec));
321 //print("compressed: p ", ftos(p)); print("y ", ftos(y)); print("len ", ftos(len), "\n");
323 return (p * 0x1000) + (y * 0x80) + len;
326 STATIC_INIT(compressShortVector)
329 float f = pow(2, 1/8);
331 for(i = 0; i < 128; ++i)
333 lengthLogTable[i] = l;
337 if(cvar("developer"))
339 LOG_INFO("Verifying vector compression table...\n");
340 for(i = 0x0F00; i < 0xFFFF; ++i)
341 if(i != compressShortVector(decompressShortVector(i)))
343 LOG_INFO("BROKEN vector compression: ", ftos(i));
344 LOG_INFO(" -> ", vtos(decompressShortVector(i)));
345 LOG_INFO(" -> ", ftos(compressShortVector(decompressShortVector(i))));
354 float CheckWireframeBox(entity forent, vector v0, vector dvx, vector dvy, vector dvz)
356 traceline(v0, v0 + dvx, true, forent); if(trace_fraction < 1) return 0;
357 traceline(v0, v0 + dvy, true, forent); if(trace_fraction < 1) return 0;
358 traceline(v0, v0 + dvz, true, forent); if(trace_fraction < 1) return 0;
359 traceline(v0 + dvx, v0 + dvx + dvy, true, forent); if(trace_fraction < 1) return 0;
360 traceline(v0 + dvx, v0 + dvx + dvz, true, forent); if(trace_fraction < 1) return 0;
361 traceline(v0 + dvy, v0 + dvy + dvx, true, forent); if(trace_fraction < 1) return 0;
362 traceline(v0 + dvy, v0 + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
363 traceline(v0 + dvz, v0 + dvz + dvx, true, forent); if(trace_fraction < 1) return 0;
364 traceline(v0 + dvz, v0 + dvz + dvy, true, forent); if(trace_fraction < 1) return 0;
365 traceline(v0 + dvx + dvy, v0 + dvx + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
366 traceline(v0 + dvx + dvz, v0 + dvx + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
367 traceline(v0 + dvy + dvz, v0 + dvx + dvy + dvz, true, forent); if(trace_fraction < 1) return 0;
372 string fixPriorityList(string order, float from, float to, float subtract, float complete)
377 n = tokenize_console(order);
379 for(i = 0; i < n; ++i)
384 if(w >= from && w <= to)
385 neworder = strcat(neworder, ftos(w), " ");
389 if(w >= from && w <= to)
390 neworder = strcat(neworder, ftos(w), " ");
397 n = tokenize_console(neworder);
398 for(w = to; w >= from; --w)
400 float wflags = Weapons_from(w).spawnflags;
401 if(wflags & WEP_FLAG_HIDDEN && wflags & WEP_FLAG_MUTATORBLOCKED && !(wflags & WEP_FLAG_NORMAL))
403 for(i = 0; i < n; ++i)
404 if(stof(argv(i)) == w)
406 if(i == n) // not found
407 neworder = strcat(neworder, ftos(w), " ");
411 return substring(neworder, 0, strlen(neworder) - 1);
414 string mapPriorityList(string order, string(string) mapfunc)
419 n = tokenize_console(order);
421 for(i = 0; i < n; ++i)
422 neworder = strcat(neworder, mapfunc(argv(i)), " ");
424 return substring(neworder, 0, strlen(neworder) - 1);
427 string swapInPriorityList(string order, float i, float j)
432 n = tokenize_console(order);
434 if(i >= 0 && i < n && j >= 0 && j < n && i != j)
437 for(w = 0; w < n; ++w)
440 s = strcat(s, argv(j), " ");
442 s = strcat(s, argv(i), " ");
444 s = strcat(s, argv(w), " ");
446 return substring(s, 0, strlen(s) - 1);
453 void get_mi_min_max(float mode)
458 strunzone(mi_shortname);
459 mi_shortname = mapname;
460 if(!strcasecmp(substring(mi_shortname, 0, 5), "maps/"))
461 mi_shortname = substring(mi_shortname, 5, strlen(mi_shortname) - 5);
462 if(!strcasecmp(substring(mi_shortname, strlen(mi_shortname) - 4, 4), ".bsp"))
463 mi_shortname = substring(mi_shortname, 0, strlen(mi_shortname) - 4);
464 mi_shortname = strzone(mi_shortname);
476 MapInfo_Get_ByName(mi_shortname, 0, NULL);
477 if(MapInfo_Map_mins.x < MapInfo_Map_maxs.x)
479 mi_min = MapInfo_Map_mins;
480 mi_max = MapInfo_Map_maxs;
488 tracebox('1 0 0' * mi.x,
489 '0 1 0' * mi.y + '0 0 1' * mi.z,
490 '0 1 0' * ma.y + '0 0 1' * ma.z,
494 if(!trace_startsolid)
495 mi_min.x = trace_endpos.x;
497 tracebox('0 1 0' * mi.y,
498 '1 0 0' * mi.x + '0 0 1' * mi.z,
499 '1 0 0' * ma.x + '0 0 1' * ma.z,
503 if(!trace_startsolid)
504 mi_min.y = trace_endpos.y;
506 tracebox('0 0 1' * mi.z,
507 '1 0 0' * mi.x + '0 1 0' * mi.y,
508 '1 0 0' * ma.x + '0 1 0' * ma.y,
512 if(!trace_startsolid)
513 mi_min.z = trace_endpos.z;
515 tracebox('1 0 0' * ma.x,
516 '0 1 0' * mi.y + '0 0 1' * mi.z,
517 '0 1 0' * ma.y + '0 0 1' * ma.z,
521 if(!trace_startsolid)
522 mi_max.x = trace_endpos.x;
524 tracebox('0 1 0' * ma.y,
525 '1 0 0' * mi.x + '0 0 1' * mi.z,
526 '1 0 0' * ma.x + '0 0 1' * ma.z,
530 if(!trace_startsolid)
531 mi_max.y = trace_endpos.y;
533 tracebox('0 0 1' * ma.z,
534 '1 0 0' * mi.x + '0 1 0' * mi.y,
535 '1 0 0' * ma.x + '0 1 0' * ma.y,
539 if(!trace_startsolid)
540 mi_max.z = trace_endpos.z;
545 void get_mi_min_max_texcoords(float mode)
549 get_mi_min_max(mode);
554 // extend mi_picmax to get a square aspect ratio
555 // center the map in that area
556 extend = mi_picmax - mi_picmin;
557 if(extend.y > extend.x)
559 mi_picmin.x -= (extend.y - extend.x) * 0.5;
560 mi_picmax.x += (extend.y - extend.x) * 0.5;
564 mi_picmin.y -= (extend.x - extend.y) * 0.5;
565 mi_picmax.y += (extend.x - extend.y) * 0.5;
568 // add another some percent
569 extend = (mi_picmax - mi_picmin) * (1 / 64.0);
573 // calculate the texcoords
574 mi_pictexcoord0 = mi_pictexcoord1 = mi_pictexcoord2 = mi_pictexcoord3 = '0 0 0';
575 // first the two corners of the origin
576 mi_pictexcoord0_x = (mi_min.x - mi_picmin.x) / (mi_picmax.x - mi_picmin.x);
577 mi_pictexcoord0_y = (mi_min.y - mi_picmin.y) / (mi_picmax.y - mi_picmin.y);
578 mi_pictexcoord2_x = (mi_max.x - mi_picmin.x) / (mi_picmax.x - mi_picmin.x);
579 mi_pictexcoord2_y = (mi_max.y - mi_picmin.y) / (mi_picmax.y - mi_picmin.y);
580 // then the other corners
581 mi_pictexcoord1_x = mi_pictexcoord0_x;
582 mi_pictexcoord1_y = mi_pictexcoord2_y;
583 mi_pictexcoord3_x = mi_pictexcoord2_x;
584 mi_pictexcoord3_y = mi_pictexcoord0_y;
588 float cvar_settemp(string tmp_cvar, string tmp_value)
590 float created_saved_value;
592 created_saved_value = 0;
594 if (!(tmp_cvar || tmp_value))
596 LOG_TRACE("Error: Invalid usage of cvar_settemp(string, string); !");
600 if(!cvar_type(tmp_cvar))
602 LOG_INFOF("Error: cvar %s doesn't exist!\n", tmp_cvar);
606 IL_EACH(g_saved_cvars, it.netname == tmp_cvar,
608 created_saved_value = -1; // skip creation
609 break; // no need to continue
612 if(created_saved_value != -1)
614 // creating a new entity to keep track of this cvar
615 entity e = new_pure(saved_cvar_value);
616 IL_PUSH(g_saved_cvars, e);
617 e.netname = strzone(tmp_cvar);
618 e.message = strzone(cvar_string(tmp_cvar));
619 created_saved_value = 1;
622 // update the cvar to the value given
623 cvar_set(tmp_cvar, tmp_value);
625 return created_saved_value;
628 int cvar_settemp_restore()
631 // FIXME this new-style loop fails!
633 FOREACH_ENTITY_CLASS("saved_cvar_value", true,
635 if(cvar_type(it.netname))
637 cvar_set(it.netname, it.message);
638 strunzone(it.netname);
639 strunzone(it.message);
644 LOG_INFOF("Error: cvar %s doesn't exist anymore! It can still be restored once it's manually recreated.\n", it.netname);
649 while((e = find(e, classname, "saved_cvar_value")))
651 if(cvar_type(e.netname))
653 cvar_set(e.netname, e.message);
658 print(sprintf("Error: cvar %s doesn't exist anymore! It can still be restored once it's manually recreated.\n", e.netname));
665 float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
668 // The following function is SLOW.
669 // For your safety and for the protection of those around you...
670 // DO NOT CALL THIS AT HOME.
672 if(w(theText, theSize) <= maxWidth)
673 return strlen(theText); // yeah!
675 // binary search for right place to cut string
677 float left, right, middle; // this always works
679 right = strlen(theText); // this always fails
682 middle = floor((left + right) / 2);
683 if(w(substring(theText, 0, middle), theSize) <= maxWidth)
688 while(left < right - 1);
690 if(w("^7", theSize) == 0) // detect color codes support in the width function
692 // NOTE: when color codes are involved, this binary search is,
693 // mathematically, BROKEN. However, it is obviously guaranteed to
694 // terminate, as the range still halves each time - but nevertheless, it is
695 // guaranteed that it finds ONE valid cutoff place (where "left" is in
696 // range, and "right" is outside).
698 // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4)
699 // and decrease left on the basis of the chars detected of the truncated tag
700 // Even if the ^xrgb tag is not complete/correct, left is decreased
701 // (sometimes too much but with a correct result)
702 // it fixes also ^[0-9]
703 while(left >= 1 && substring(theText, left-1, 1) == "^")
706 if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/
708 else if (left >= 3 && substring(theText, left-3, 2) == "^x")
710 ch = str2chr(theText, left-1);
711 if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/
714 else if (left >= 4 && substring(theText, left-4, 2) == "^x")
716 ch = str2chr(theText, left-2);
717 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
719 ch = str2chr(theText, left-1);
720 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/
729 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t w)
732 // The following function is SLOW.
733 // For your safety and for the protection of those around you...
734 // DO NOT CALL THIS AT HOME.
736 if(w(theText) <= maxWidth)
737 return strlen(theText); // yeah!
739 // binary search for right place to cut string
741 float left, right, middle; // this always works
743 right = strlen(theText); // this always fails
746 middle = floor((left + right) / 2);
747 if(w(substring(theText, 0, middle)) <= maxWidth)
752 while(left < right - 1);
754 if(w("^7") == 0) // detect color codes support in the width function
756 // NOTE: when color codes are involved, this binary search is,
757 // mathematically, BROKEN. However, it is obviously guaranteed to
758 // terminate, as the range still halves each time - but nevertheless, it is
759 // guaranteed that it finds ONE valid cutoff place (where "left" is in
760 // range, and "right" is outside).
762 // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4)
763 // and decrease left on the basis of the chars detected of the truncated tag
764 // Even if the ^xrgb tag is not complete/correct, left is decreased
765 // (sometimes too much but with a correct result)
766 // it fixes also ^[0-9]
767 while(left >= 1 && substring(theText, left-1, 1) == "^")
770 if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/
772 else if (left >= 3 && substring(theText, left-3, 2) == "^x")
774 ch = str2chr(theText, left-1);
775 if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/
778 else if (left >= 4 && substring(theText, left-4, 2) == "^x")
780 ch = str2chr(theText, left-2);
781 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
783 ch = str2chr(theText, left-1);
784 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/
793 string find_last_color_code(string s)
795 int start = strstrofs(s, "^", 0);
796 if (start == -1) // no caret found
798 int len = strlen(s)-1;
800 for(i = len; i >= start; --i)
802 if(substring(s, i, 1) != "^")
806 while (i-carets >= start && substring(s, i-carets, 1) == "^")
809 // check if carets aren't all escaped
813 if(strstrofs("0123456789", substring(s, i+1, 1), 0) >= 0)
814 return substring(s, i, 2);
817 if(substring(s, i+1, 1) == "x")
818 if(strstrofs("0123456789abcdefABCDEF", substring(s, i+2, 1), 0) >= 0)
819 if(strstrofs("0123456789abcdefABCDEF", substring(s, i+3, 1), 0) >= 0)
820 if(strstrofs("0123456789abcdefABCDEF", substring(s, i+4, 1), 0) >= 0)
821 return substring(s, i, 5);
823 i -= carets; // this also skips one char before the carets
829 string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
835 s = getWrappedLine_remaining;
839 getWrappedLine_remaining = string_null;
840 return s; // the line has no size ANYWAY, nothing would be displayed.
843 cantake = textLengthUpToWidth(s, w, theFontSize, tw);
844 if(cantake > 0 && cantake < strlen(s))
847 while(take > 0 && substring(s, take, 1) != " ")
851 getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
852 if(getWrappedLine_remaining == "")
853 getWrappedLine_remaining = string_null;
854 else if (tw("^7", theFontSize) == 0)
855 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
856 return substring(s, 0, cantake);
860 getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
861 if(getWrappedLine_remaining == "")
862 getWrappedLine_remaining = string_null;
863 else if (tw("^7", theFontSize) == 0)
864 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
865 return substring(s, 0, take);
870 getWrappedLine_remaining = string_null;
875 string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw)
881 s = getWrappedLine_remaining;
885 getWrappedLine_remaining = string_null;
886 return s; // the line has no size ANYWAY, nothing would be displayed.
889 cantake = textLengthUpToLength(s, w, tw);
890 if(cantake > 0 && cantake < strlen(s))
893 while(take > 0 && substring(s, take, 1) != " ")
897 getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
898 if(getWrappedLine_remaining == "")
899 getWrappedLine_remaining = string_null;
900 else if (tw("^7") == 0)
901 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
902 return substring(s, 0, cantake);
906 getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
907 if(getWrappedLine_remaining == "")
908 getWrappedLine_remaining = string_null;
909 else if (tw("^7") == 0)
910 getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
911 return substring(s, 0, take);
916 getWrappedLine_remaining = string_null;
921 string textShortenToWidth(string theText, float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
923 if(tw(theText, theFontSize) <= maxWidth)
926 return strcat(substring(theText, 0, textLengthUpToWidth(theText, maxWidth - tw("...", theFontSize), theFontSize, tw)), "...");
929 string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t tw)
931 if(tw(theText) <= maxWidth)
934 return strcat(substring(theText, 0, textLengthUpToLength(theText, maxWidth - tw("..."), tw)), "...");
937 float isGametypeInFilter(Gametype gt, float tp, float ts, string pattern)
939 string subpattern, subpattern2, subpattern3, subpattern4;
940 subpattern = strcat(",", MapInfo_Type_ToString(gt), ",");
942 subpattern2 = ",teams,";
944 subpattern2 = ",noteams,";
946 subpattern3 = ",teamspawns,";
948 subpattern3 = ",noteamspawns,";
949 if(gt == MAPINFO_TYPE_RACE || gt == MAPINFO_TYPE_CTS)
950 subpattern4 = ",race,";
952 subpattern4 = string_null;
954 if(substring(pattern, 0, 1) == "-")
956 pattern = substring(pattern, 1, strlen(pattern) - 1);
957 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) >= 0)
959 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) >= 0)
961 if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) >= 0)
963 if(subpattern4 && strstrofs(strcat(",", pattern, ","), subpattern4, 0) >= 0)
968 if(substring(pattern, 0, 1) == "+")
969 pattern = substring(pattern, 1, strlen(pattern) - 1);
970 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) < 0)
971 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) < 0)
972 if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) < 0)
976 if(strstrofs(strcat(",", pattern, ","), subpattern4, 0) < 0)
983 vector solve_shotdirection(vector myorg, vector myvel, vector eorg, vector evel, float spd, float newton_style)
987 // make origin and speed relative
992 // now solve for ret, ret normalized:
993 // eorg + t * evel == t * ret * spd
994 // or, rather, solve for t:
995 // |eorg + t * evel| == t * spd
996 // eorg^2 + t^2 * evel^2 + 2 * t * (eorg * evel) == t^2 * spd^2
997 // t^2 * (evel^2 - spd^2) + t * (2 * (eorg * evel)) + eorg^2 == 0
998 vector solution = solve_quadratic(evel * evel - spd * spd, 2 * (eorg * evel), eorg * eorg);
999 // p = 2 * (eorg * evel) / (evel * evel - spd * spd)
1000 // q = (eorg * eorg) / (evel * evel - spd * spd)
1001 if(!solution.z) // no real solution
1004 // (eorg * evel)^2 < (evel^2 - spd^2) * eorg^2
1005 // (eorg * evel)^2 / eorg^2 < evel^2 - spd^2
1006 // spd^2 < ((evel^2 * eorg^2) - (eorg * evel)^2) / eorg^2
1007 // spd^2 < evel^2 * (1 - cos^2 angle(evel, eorg))
1008 // spd^2 < evel^2 * sin^2 angle(evel, eorg)
1009 // spd < |evel| * sin angle(evel, eorg)
1012 else if(solution.x > 0)
1014 // both solutions > 0: take the smaller one
1015 // happens if p < 0 and q > 0
1016 ret = normalize(eorg + solution.x * evel);
1018 else if(solution.y > 0)
1020 // one solution > 0: take the larger one
1021 // happens if q < 0 or q == 0 and p < 0
1022 ret = normalize(eorg + solution.y * evel);
1026 // no solution > 0: reject
1027 // happens if p > 0 and q >= 0
1028 // 2 * (eorg * evel) / (evel * evel - spd * spd) > 0
1029 // (eorg * eorg) / (evel * evel - spd * spd) >= 0
1034 // "Enemy is moving away from me at more than spd"
1038 // NOTE: we always got a solution if spd > |evel|
1040 if(newton_style == 2)
1041 ret = normalize(ret * spd + myvel);
1046 vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_style, float mi, float ma)
1051 if(newton_style == 2)
1053 // true Newtonian projectiles with automatic aim adjustment
1055 // solve: |outspeed * mydir - myvel| = spd
1056 // outspeed^2 - 2 * outspeed * (mydir * myvel) + myvel^2 - spd^2 = 0
1057 // outspeed = (mydir * myvel) +- sqrt((mydir * myvel)^2 - myvel^2 + spd^2)
1061 // myvel^2 - (mydir * myvel)^2 > spd^2
1062 // velocity without mydir component > spd
1063 // fire at smallest possible spd that works?
1064 // |(mydir * myvel) * myvel - myvel| = spd
1066 vector solution = solve_quadratic(1, -2 * (mydir * myvel), myvel * myvel - spd * spd);
1070 outspeed = solution.y; // the larger one
1073 //outspeed = 0; // slowest possible shot
1074 outspeed = solution.x; // the real part (that is, the average!)
1075 //dprint("impossible shot, adjusting\n");
1078 outspeed = bound(spd * mi, outspeed, spd * ma);
1079 return mydir * outspeed;
1083 return myvel + spd * mydir;
1086 float compressShotOrigin(vector v)
1090 y = rint(v.y * 4) + 128;
1091 z = rint(v.z * 4) + 128;
1092 if(x > 255 || x < 0)
1094 LOG_INFO("shot origin ", vtos(v), " x out of bounds\n");
1095 x = bound(0, x, 255);
1097 if(y > 255 || y < 0)
1099 LOG_INFO("shot origin ", vtos(v), " y out of bounds\n");
1100 y = bound(0, y, 255);
1102 if(z > 255 || z < 0)
1104 LOG_INFO("shot origin ", vtos(v), " z out of bounds\n");
1105 z = bound(0, z, 255);
1107 return x * 0x10000 + y * 0x100 + z;
1109 vector decompressShotOrigin(int f)
1112 v.x = ((f & 0xFF0000) / 0x10000) / 2;
1113 v.y = ((f & 0xFF00) / 0x100 - 128) / 4;
1114 v.z = ((f & 0xFF) - 128) / 4;
1119 vector healtharmor_maxdamage(float h, float a, float armorblock, int deathtype)
1121 // NOTE: we'll always choose the SMALLER value...
1122 float healthdamage, armordamage, armorideal;
1123 if (DEATH_IS(deathtype, DEATH_DROWN)) // Why should armor help here...
1126 healthdamage = (h - 1) / (1 - armorblock); // damage we can take if we could use more health
1127 armordamage = a + (h - 1); // damage we can take if we could use more armor
1128 armorideal = healthdamage * armorblock;
1130 if(armordamage < healthdamage)
1143 vector healtharmor_applydamage(float a, float armorblock, int deathtype, float damage)
1146 if (DEATH_IS(deathtype, DEATH_DROWN)) // Why should armor help here...
1148 v.y = bound(0, damage * armorblock, a); // save
1149 v.x = bound(0, damage - v.y, damage); // take
1155 string getcurrentmod()
1159 m = cvar_string("fs_gamedir");
1160 n = tokenize_console(m);
1167 float matchacl(string acl, string str)
1174 t = car(acl); acl = cdr(acl);
1177 if(substring(t, 0, 1) == "-")
1180 t = substring(t, 1, strlen(t) - 1);
1182 else if(substring(t, 0, 1) == "+")
1183 t = substring(t, 1, strlen(t) - 1);
1185 if(substring(t, -1, 1) == "*")
1187 t = substring(t, 0, strlen(t) - 1);
1188 s = substring(str, 0, strlen(t));
1201 string get_model_datafilename(string m, float sk, string fil)
1206 m = "models/player/*_";
1208 m = strcat(m, ftos(sk));
1211 return strcat(m, ".", fil);
1214 float get_model_parameters(string m, float sk)
1216 get_model_parameters_modelname = string_null;
1217 get_model_parameters_modelskin = -1;
1218 get_model_parameters_name = string_null;
1219 get_model_parameters_species = -1;
1220 get_model_parameters_sex = string_null;
1221 get_model_parameters_weight = -1;
1222 get_model_parameters_age = -1;
1223 get_model_parameters_desc = string_null;
1224 get_model_parameters_bone_upperbody = string_null;
1225 get_model_parameters_bone_weapon = string_null;
1226 for(int i = 0; i < MAX_AIM_BONES; ++i)
1228 get_model_parameters_bone_aim[i] = string_null;
1229 get_model_parameters_bone_aimweight[i] = 0;
1231 get_model_parameters_fixbone = 0;
1234 MUTATOR_CALLHOOK(ClearModelParams);
1240 if(substring(m, -9, 5) == "_lod1" || substring(m, -9, 5) == "_lod2")
1241 m = strcat(substring(m, 0, -10), substring(m, -4, -1));
1245 if(substring(m, -4, -1) != ".txt")
1247 if(substring(m, -6, 1) != "_")
1249 sk = stof(substring(m, -5, 1));
1250 m = substring(m, 0, -7);
1253 string fn = get_model_datafilename(m, sk, "txt");
1254 int fh = fopen(fn, FILE_READ);
1258 fn = get_model_datafilename(m, sk, "txt");
1259 fh = fopen(fn, FILE_READ);
1264 get_model_parameters_modelname = m;
1265 get_model_parameters_modelskin = sk;
1267 while((s = fgets(fh)))
1270 break; // next lines will be description
1274 get_model_parameters_name = s;
1278 case "human": get_model_parameters_species = SPECIES_HUMAN; break;
1279 case "alien": get_model_parameters_species = SPECIES_ALIEN; break;
1280 case "robot_shiny": get_model_parameters_species = SPECIES_ROBOT_SHINY; break;
1281 case "robot_rusty": get_model_parameters_species = SPECIES_ROBOT_RUSTY; break;
1282 case "robot_solid": get_model_parameters_species = SPECIES_ROBOT_SOLID; break;
1283 case "animal": get_model_parameters_species = SPECIES_ANIMAL; break;
1284 case "reserved": get_model_parameters_species = SPECIES_RESERVED; break;
1287 get_model_parameters_sex = s;
1289 get_model_parameters_weight = stof(s);
1291 get_model_parameters_age = stof(s);
1292 if(c == "description")
1293 get_model_parameters_description = s;
1294 if(c == "bone_upperbody")
1295 get_model_parameters_bone_upperbody = s;
1296 if(c == "bone_weapon")
1297 get_model_parameters_bone_weapon = s;
1299 MUTATOR_CALLHOOK(GetModelParams, c, s);
1301 for(int i = 0; i < MAX_AIM_BONES; ++i)
1302 if(c == strcat("bone_aim", ftos(i)))
1304 get_model_parameters_bone_aimweight[i] = stof(car(s));
1305 get_model_parameters_bone_aim[i] = cdr(s);
1308 get_model_parameters_fixbone = stof(s);
1311 while((s = fgets(fh)))
1313 if(get_model_parameters_desc)
1314 get_model_parameters_desc = strcat(get_model_parameters_desc, "\n");
1316 get_model_parameters_desc = strcat(get_model_parameters_desc, s);
1324 // x-encoding (encoding as zero length invisible string)
1325 const string XENCODE_2 = "xX";
1326 const string XENCODE_22 = "0123456789abcdefABCDEF";
1327 string xencode(int f)
1330 d = f % 22; f = floor(f / 22);
1331 c = f % 22; f = floor(f / 22);
1332 b = f % 22; f = floor(f / 22);
1333 a = f % 2; // f = floor(f / 2);
1336 substring(XENCODE_2, a, 1),
1337 substring(XENCODE_22, b, 1),
1338 substring(XENCODE_22, c, 1),
1339 substring(XENCODE_22, d, 1)
1342 float xdecode(string s)
1345 if(substring(s, 0, 1) != "^")
1349 a = strstrofs(XENCODE_2, substring(s, 1, 1), 0);
1350 b = strstrofs(XENCODE_22, substring(s, 2, 1), 0);
1351 c = strstrofs(XENCODE_22, substring(s, 3, 1), 0);
1352 d = strstrofs(XENCODE_22, substring(s, 4, 1), 0);
1353 if(a < 0 || b < 0 || c < 0 || d < 0)
1355 return ((a * 22 + b) * 22 + c) * 22 + d;
1359 string strlimitedlen(string input, string truncation, float strip_colors, float limit)
1361 if(strlen((strip_colors ? strdecolorize(input) : input)) <= limit)
1364 return strcat(substring(input, 0, (strlen(input) - strlen(truncation))), truncation);
1367 float shutdown_running;
1372 void CSQC_Shutdown()
1378 if(shutdown_running)
1380 LOG_INFO("Recursive shutdown detected! Only restoring cvars...\n");
1384 shutdown_running = 1;
1388 cvar_settemp_restore(); // this must be done LAST, but in any case
1392 .float skeleton_bones_index;
1393 void Skeleton_SetBones(entity e)
1395 // set skeleton_bones to the total number of bones on the model
1396 if(e.skeleton_bones_index == e.modelindex)
1397 return; // same model, nothing to update
1400 skelindex = skel_create(e.modelindex);
1401 e.skeleton_bones = skel_get_numbones(skelindex);
1402 skel_delete(skelindex);
1403 e.skeleton_bones_index = e.modelindex;
1407 string to_execute_next_frame;
1408 void execute_next_frame()
1410 if(to_execute_next_frame)
1412 localcmd("\n", to_execute_next_frame, "\n");
1413 strunzone(to_execute_next_frame);
1414 to_execute_next_frame = string_null;
1417 void queue_to_execute_next_frame(string s)
1419 if(to_execute_next_frame)
1421 s = strcat(s, "\n", to_execute_next_frame);
1422 strunzone(to_execute_next_frame);
1424 to_execute_next_frame = strzone(s);
1427 .float FindConnectedComponent_processing;
1428 void FindConnectedComponent(entity e, .entity fld, findNextEntityNearFunction_t nxt, isConnectedFunction_t iscon, entity pass)
1430 entity queue_start, queue_end;
1432 // we build a queue of to-be-processed entities.
1433 // queue_start is the next entity to be checked for neighbors
1434 // queue_end is the last entity added
1436 if(e.FindConnectedComponent_processing)
1437 error("recursion or broken cleanup");
1439 // start with a 1-element queue
1440 queue_start = queue_end = e;
1441 queue_end.(fld) = NULL;
1442 queue_end.FindConnectedComponent_processing = 1;
1444 // for each queued item:
1445 for (; queue_start; queue_start = queue_start.(fld))
1447 // find all neighbors of queue_start
1449 for(t = NULL; (t = nxt(t, queue_start, pass)); )
1451 if(t.FindConnectedComponent_processing)
1453 if(iscon(t, queue_start, pass))
1455 // it is connected? ADD IT. It will look for neighbors soon too.
1456 queue_end.(fld) = t;
1458 queue_end.(fld) = NULL;
1459 queue_end.FindConnectedComponent_processing = 1;
1465 for (queue_start = e; queue_start; queue_start = queue_start.(fld))
1466 queue_start.FindConnectedComponent_processing = 0;
1470 vector animfixfps(entity e, vector a, vector b)
1472 // multi-frame anim: keep as-is
1475 float dur = frameduration(e.modelindex, a.x);
1476 if (dur <= 0 && b.y)
1479 dur = frameduration(e.modelindex, a.x);
1489 Notification Announcer_PickNumber(int type, int num)
1498 case 10: return ANNCE_NUM_GAMESTART_10;
1499 case 9: return ANNCE_NUM_GAMESTART_9;
1500 case 8: return ANNCE_NUM_GAMESTART_8;
1501 case 7: return ANNCE_NUM_GAMESTART_7;
1502 case 6: return ANNCE_NUM_GAMESTART_6;
1503 case 5: return ANNCE_NUM_GAMESTART_5;
1504 case 4: return ANNCE_NUM_GAMESTART_4;
1505 case 3: return ANNCE_NUM_GAMESTART_3;
1506 case 2: return ANNCE_NUM_GAMESTART_2;
1507 case 1: return ANNCE_NUM_GAMESTART_1;
1515 case 10: return ANNCE_NUM_IDLE_10;
1516 case 9: return ANNCE_NUM_IDLE_9;
1517 case 8: return ANNCE_NUM_IDLE_8;
1518 case 7: return ANNCE_NUM_IDLE_7;
1519 case 6: return ANNCE_NUM_IDLE_6;
1520 case 5: return ANNCE_NUM_IDLE_5;
1521 case 4: return ANNCE_NUM_IDLE_4;
1522 case 3: return ANNCE_NUM_IDLE_3;
1523 case 2: return ANNCE_NUM_IDLE_2;
1524 case 1: return ANNCE_NUM_IDLE_1;
1532 case 10: return ANNCE_NUM_KILL_10;
1533 case 9: return ANNCE_NUM_KILL_9;
1534 case 8: return ANNCE_NUM_KILL_8;
1535 case 7: return ANNCE_NUM_KILL_7;
1536 case 6: return ANNCE_NUM_KILL_6;
1537 case 5: return ANNCE_NUM_KILL_5;
1538 case 4: return ANNCE_NUM_KILL_4;
1539 case 3: return ANNCE_NUM_KILL_3;
1540 case 2: return ANNCE_NUM_KILL_2;
1541 case 1: return ANNCE_NUM_KILL_1;
1549 case 10: return ANNCE_NUM_RESPAWN_10;
1550 case 9: return ANNCE_NUM_RESPAWN_9;
1551 case 8: return ANNCE_NUM_RESPAWN_8;
1552 case 7: return ANNCE_NUM_RESPAWN_7;
1553 case 6: return ANNCE_NUM_RESPAWN_6;
1554 case 5: return ANNCE_NUM_RESPAWN_5;
1555 case 4: return ANNCE_NUM_RESPAWN_4;
1556 case 3: return ANNCE_NUM_RESPAWN_3;
1557 case 2: return ANNCE_NUM_RESPAWN_2;
1558 case 1: return ANNCE_NUM_RESPAWN_1;
1562 case CNT_ROUNDSTART:
1566 case 10: return ANNCE_NUM_ROUNDSTART_10;
1567 case 9: return ANNCE_NUM_ROUNDSTART_9;
1568 case 8: return ANNCE_NUM_ROUNDSTART_8;
1569 case 7: return ANNCE_NUM_ROUNDSTART_7;
1570 case 6: return ANNCE_NUM_ROUNDSTART_6;
1571 case 5: return ANNCE_NUM_ROUNDSTART_5;
1572 case 4: return ANNCE_NUM_ROUNDSTART_4;
1573 case 3: return ANNCE_NUM_ROUNDSTART_3;
1574 case 2: return ANNCE_NUM_ROUNDSTART_2;
1575 case 1: return ANNCE_NUM_ROUNDSTART_1;
1583 case 10: return ANNCE_NUM_10;
1584 case 9: return ANNCE_NUM_9;
1585 case 8: return ANNCE_NUM_8;
1586 case 7: return ANNCE_NUM_7;
1587 case 6: return ANNCE_NUM_6;
1588 case 5: return ANNCE_NUM_5;
1589 case 4: return ANNCE_NUM_4;
1590 case 3: return ANNCE_NUM_3;
1591 case 2: return ANNCE_NUM_2;
1592 case 1: return ANNCE_NUM_1;
1601 int Mod_Q1BSP_SuperContentsFromNativeContents(int nativecontents)
1603 switch(nativecontents)
1608 return DPCONTENTS_SOLID | DPCONTENTS_OPAQUE;
1610 return DPCONTENTS_WATER;
1612 return DPCONTENTS_SLIME;
1614 return DPCONTENTS_LAVA | DPCONTENTS_NODROP;
1616 return DPCONTENTS_SKY | DPCONTENTS_NODROP | DPCONTENTS_OPAQUE; // to match behaviour of Q3 maps, let sky count as opaque
1621 int Mod_Q1BSP_NativeContentsFromSuperContents(int supercontents)
1623 if(supercontents & (DPCONTENTS_SOLID | DPCONTENTS_BODY))
1624 return CONTENT_SOLID;
1625 if(supercontents & DPCONTENTS_SKY)
1627 if(supercontents & DPCONTENTS_LAVA)
1628 return CONTENT_LAVA;
1629 if(supercontents & DPCONTENTS_SLIME)
1630 return CONTENT_SLIME;
1631 if(supercontents & DPCONTENTS_WATER)
1632 return CONTENT_WATER;
1633 return CONTENT_EMPTY;