]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/util.qc
GetProgramCommandPrefix() -- allows me to have enhanced usage and such
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qc
index 10e4e345b28298ead91a5420e4c0c11da92dc36b..5e7845bbc4f3c42d80cb06a5e006df8af2330c17 100644 (file)
@@ -523,31 +523,31 @@ float invertLengthLog(float x)
 vector decompressShortVector(float data)
 {
        vector out;
-       float pitch, yaw, len;
+       float p, y, len;
        if(data == 0)
                return '0 0 0';
-       pitch = (data & 0xF000) / 0x1000;
-       yaw =   (data & 0x0F80) / 0x80;
-       len =   (data & 0x007F);
+       p   = (data & 0xF000) / 0x1000;
+       y   = (data & 0x0F80) / 0x80;
+       len = (data & 0x007F);
 
-       //print("\ndecompress: pitch ", ftos(pitch)); print("yaw ", ftos(yaw)); print("len ", ftos(len), "\n");
+       //print("\ndecompress: p ", ftos(p)); print("y ", ftos(y)); print("len ", ftos(len), "\n");
 
-       if(pitch == 0)
+       if(p == 0)
        {
                out_x = 0;
                out_y = 0;
-               if(yaw == 31)
+               if(y == 31)
                        out_z = -1;
                else
                        out_z = +1;
        }
        else
        {
-               yaw   = .19634954084936207740 * yaw;
-               pitch = .19634954084936207740 * pitch - 1.57079632679489661922;
-               out_x = cos(yaw) *  cos(pitch);
-               out_y = sin(yaw) *  cos(pitch);
-               out_z =            -sin(pitch);
+               y   = .19634954084936207740 * y;
+               p = .19634954084936207740 * p - 1.57079632679489661922;
+               out_x = cos(y) *  cos(p);
+               out_y = sin(y) *  cos(p);
+               out_z =          -sin(p);
        }
 
        //print("decompressed: ", vtos(out), "\n");
@@ -558,7 +558,7 @@ vector decompressShortVector(float data)
 float compressShortVector(vector vec)
 {
        vector ang;
-       float pitch, yaw, len;
+       float p, y, len;
        if(vlen(vec) == 0)
                return 0;
        //print("compress: ", vtos(vec), "\n");
@@ -570,21 +570,21 @@ float compressShortVector(vector vec)
                error("BOGUS vectoangles");
        //print("angles: ", vtos(ang), "\n");
 
-       pitch = floor(0.5 + (ang_x + 90) * 16 / 180) & 15; // -90..90 to 0..14
-       if(pitch == 0)
+       p = floor(0.5 + (ang_x + 90) * 16 / 180) & 15; // -90..90 to 0..14
+       if(p == 0)
        {
                if(vec_z < 0)
-                       yaw = 31;
+                       y = 31;
                else
-                       yaw = 30;
+                       y = 30;
        }
        else
-               yaw = floor(0.5 + ang_y * 32 / 360)          & 31; // 0..360 to 0..32
+               y = floor(0.5 + ang_y * 32 / 360)          & 31; // 0..360 to 0..32
        len = invertLengthLog(vlen(vec));
 
-       //print("compressed: pitch ", ftos(pitch)); print("yaw ", ftos(yaw)); print("len ", ftos(len), "\n");
+       //print("compressed: p ", ftos(p)); print("y ", ftos(y)); print("len ", ftos(len), "\n");
 
-       return (pitch * 0x1000) + (yaw * 0x80) + len;
+       return (p * 0x1000) + (y * 0x80) + len;
 }
 
 void compressShortVector_init()
@@ -863,45 +863,49 @@ void get_mi_min_max_texcoords(float mode)
 }
 #endif
 
-#ifdef CSQC
-void cvar_settemp(string pKey, string pValue)
-{
-       error("cvar_settemp called from CSQC - use cvar_clientsettemp instead!");
-}
-void cvar_settemp_restore()
-{
-       error("cvar_settemp_restore called from CSQC - use cvar_clientsettemp instead!");
-}
-#else
-void cvar_settemp(string pKey, string pValue)
+float cvar_settemp(string tmp_cvar, string tmp_value)
 {
-       float i;
-       string settemp_var;
-       if(cvar_string(pKey) == pValue)
-               return;
-       i = cvar("settemp_idx");
-       cvar_set("settemp_idx", ftos(i+1));
-       settemp_var = strcat("_settemp_x", ftos(i));
-#ifdef MENUQC
-       registercvar(settemp_var, "", 0);
-#else
-       registercvar(settemp_var, "");
-#endif
-       cvar_set("settemp_list", strcat("1 ", pKey, " ", settemp_var, " ", cvar_string("settemp_list")));
-       cvar_set(settemp_var, cvar_string(pKey));
-       cvar_set(pKey, pValue);
+       float created_saved_value;
+       entity e;
+       
+       if not(tmp_cvar || tmp_value)
+       {
+               dprint("Error: Invalid usage of cvar_settemp(string, string); !\n");
+               return FALSE;
+       }
+       
+       for(e = world; (e = find(e, classname, "saved_cvar_value")); )
+               if(e.netname == tmp_cvar)
+                       goto saved; // skip creation
+                       
+       // creating a new entity to keep track of this cvar
+       e = spawn();
+       e.classname = "saved_cvar_value";
+       e.netname = strzone(tmp_cvar);
+       e.message = strzone(cvar_string(tmp_cvar));
+       created_saved_value = TRUE;
+       
+       // an entity for this cvar already exists
+       :saved
+       
+       // update the cvar to the value given
+       cvar_set(tmp_cvar, tmp_value);
+       
+       return created_saved_value;
 }
 
-void cvar_settemp_restore()
+float cvar_settemp_restore()
 {
-       // undo what cvar_settemp did
-       float n, i;
-       n = tokenize_console(cvar_string("settemp_list"));
-       for(i = 0; i < n - 3; i += 3)
-               cvar_set(argv(i + 1), cvar_string(argv(i + 2)));
-       cvar_set("settemp_list", "0");
+       float i;
+       entity e;
+       while((e = find(world, classname, "saved_cvar_value")))
+       {
+               cvar_set(e.netname, e.message);
+               remove(e);
+       }
+       
+       return i;
 }
-#endif
 
 float almost_equals(float a, float b)
 {
@@ -1757,11 +1761,11 @@ float ReadInt24_t()
        return v;
 }
 #else
-void WriteInt24_t(float dest, float val)
+void WriteInt24_t(float dst, float val)
 {
        float v;
-       WriteShort(dest, (v = floor(val / 256)));
-       WriteByte(dest, val - v * 256); // 0..255
+       WriteShort(dst, (v = floor(val / 256)));
+       WriteByte(dst, val - v * 256); // 0..255
 }
 #endif
 #endif
@@ -2107,3 +2111,77 @@ float lowestbit(float f)
        f &~= f * 65536;
        return f;
 }
+
+/*
+string strlimitedlen(string input, string truncation, float strip_colors, float limit)
+{
+       if(strlen((strip_colors ? strdecolorize(input) : input)) <= limit)
+               return input;
+       else
+               return strcat(substring(input, 0, (strlen(input) - strlen(truncation))), truncation);
+}*/
+
+// escape the string to make it safe for consoles
+string MakeConsoleSafe(string input)
+{
+       input = strreplace("\n", "", input);
+       input = strreplace("\\", "\\\\", input);
+       input = strreplace("$", "$$", input);
+       input = strreplace("\"", "\\\"", input);
+       return input;
+}
+
+#ifndef MENUQC
+// get true/false value of a string with multiple different inputs
+float InterpretBoolean(string input)
+{
+       switch(strtolower(input))
+       {
+               case "yes":
+               case "true":
+               case "on":
+                       return TRUE;
+               
+               case "no":
+               case "false":
+               case "off":
+                       return FALSE;
+               
+               default: return stof(input);
+       }
+}
+#endif
+
+#ifdef CSQC
+entity ReadCSQCEntity()
+{
+       float f;
+       f = ReadShort();
+       if(f == 0)
+               return world;
+       return findfloat(world, entnum, f);
+}
+#endif
+
+float shutdown_running;
+#ifdef SVQC
+void SV_Shutdown()
+#endif
+#ifdef CSQC
+void CSQC_Shutdown()
+#endif
+#ifdef MENUQC
+void m_shutdown()
+#endif
+{
+       if(shutdown_running)
+       {
+               print("Recursive shutdown detected! Only restoring cvars...\n");
+       }
+       else
+       {
+               shutdown_running = 1;
+               Shutdown();
+       }
+       cvar_settemp_restore(); // this must be done LAST, but in any case
+}