]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/util.qc
get rid of 'local' prefixes (does nothing)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qc
index 83ea8e3e5827fedbc29f9f7534aec14f8865ca80..d02fa1e2cfd65c652aba8bc3a89012875f18e5cf 100644 (file)
@@ -41,8 +41,8 @@ void wordwrap_sprint(string s, float l)
 
 string unescape(string in)
 {
-       local float i, len;
-       local string str, s;
+       float i, len;
+       string str, s;
 
        // but it doesn't seem to be necessary in my tests at least
        in = strzone(in);
@@ -72,8 +72,8 @@ string unescape(string in)
 
 void wordwrap_cb(string s, float l, void(string) callback)
 {
-       local string c;
-       local float lleft, i, j, wlen;
+       string c;
+       float lleft, i, j, wlen;
 
        s = strzone(s);
        lleft = l;
@@ -236,6 +236,16 @@ string fstrunzone(string s)
        return sc;
 }
 
+float fexists(string f)
+{
+    float fh;
+    fh = fopen(f, FILE_READ);
+    if (fh < 0)
+        return FALSE;
+    fclose(fh);
+    return TRUE;
+}
+
 // Databases (hash tables)
 #define DB_BUCKETS 8192
 void db_save(float db, string pFilename)
@@ -360,7 +370,10 @@ float buf_load(string pFilename)
                return -1;
        fh = fopen(pFilename, FILE_READ);
        if(fh < 0)
-               return buf;
+       {
+               buf_del(buf);
+               return -1;
+       }
        i = 0;
        while((l = fgets(fh)))
        {
@@ -1140,11 +1153,6 @@ float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs) {retu
 
 float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
 {
-       float ICanHasKallerz;
-
-       // detect color codes support in the width function
-       ICanHasKallerz = (w("^7", theSize) == 0);
-
        // STOP.
        // The following function is SLOW.
        // For your safety and for the protection of those around you...
@@ -1168,7 +1176,7 @@ float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLe
        }
        while(left < right - 1);
 
-       if(ICanHasKallerz)
+       if(w("^7", theSize) == 0) // detect color codes support in the width function
        {
                // NOTE: when color codes are involved, this binary search is,
                // mathematically, BROKEN. However, it is obviously guaranteed to
@@ -1209,11 +1217,6 @@ float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLe
 
 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t w)
 {
-       float ICanHasKallerz;
-
-       // detect color codes support in the width function
-       ICanHasKallerz = (w("^7") == 0);
-
        // STOP.
        // The following function is SLOW.
        // For your safety and for the protection of those around you...
@@ -1237,7 +1240,7 @@ float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_
        }
        while(left < right - 1);
 
-       if(ICanHasKallerz)
+       if(w("^7") == 0) // detect color codes support in the width function
        {
                // NOTE: when color codes are involved, this binary search is,
                // mathematically, BROKEN. However, it is obviously guaranteed to
@@ -1276,6 +1279,42 @@ float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_
        return left;
 }
 
+string find_last_color_code(string s)
+{
+       float start, len, i, carets;
+       start = strstrofs(s, "^", 0);
+       if (start == -1) // no caret found
+               return "";
+       len = strlen(s)-1;
+       for(i = len; i >= start; --i)
+       {
+               if(substring(s, i, 1) != "^")
+                       continue;
+
+               carets = 1;
+               while (i-carets >= start && substring(s, i-carets, 1) == "^")
+                       ++carets;
+
+               // check if carets aren't all escaped
+               if (carets == 1 || mod(carets, 2) == 1) // first check is just an optimization
+               {
+                       if(i+1 <= len)
+                       if(strstrofs("0123456789", substring(s, i+1, 1), 0) >= 0)
+                               return substring(s, i, 2);
+
+                       if(i+4 <= len)
+                       if(substring(s, i+1, 1) == "x")
+                       if(strstrofs("0123456789abcdefABCDEF", substring(s, i+2, 1), 0) >= 0)
+                       if(strstrofs("0123456789abcdefABCDEF", substring(s, i+3, 1), 0) >= 0)
+                       if(strstrofs("0123456789abcdefABCDEF", substring(s, i+4, 1), 0) >= 0)
+                               return substring(s, i, 5);
+               }
+               i -= carets; // this also skips one char before the carets
+       }
+
+       return "";
+}
+
 string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
 {
        float cantake;
@@ -1295,6 +1334,8 @@ string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunc
                        getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
                        if(getWrappedLine_remaining == "")
                                getWrappedLine_remaining = string_null;
+                       else if (tw("^7", theFontSize) == 0)
+                               getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
                        return substring(s, 0, cantake);
                }
                else
@@ -1302,6 +1343,8 @@ string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunc
                        getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
                        if(getWrappedLine_remaining == "")
                                getWrappedLine_remaining = string_null;
+                       else if (tw("^7", theFontSize) == 0)
+                               getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
                        return substring(s, 0, take);
                }
        }
@@ -1331,6 +1374,8 @@ string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw)
                        getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
                        if(getWrappedLine_remaining == "")
                                getWrappedLine_remaining = string_null;
+                       else if (tw("^7") == 0)
+                               getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
                        return substring(s, 0, cantake);
                }
                else
@@ -1338,6 +1383,8 @@ string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw)
                        getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
                        if(getWrappedLine_remaining == "")
                                getWrappedLine_remaining = string_null;
+                       else if (tw("^7") == 0)
+                               getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
                        return substring(s, 0, take);
                }
        }
@@ -1364,18 +1411,22 @@ string textShortenToLength(string theText, float maxWidth, textLengthUpToLength_
                return strcat(substring(theText, 0, textLengthUpToLength(theText, maxWidth - tw("..."), tw)), "...");
 }
 
-float isGametypeInFilter(float gt, float tp, string pattern)
+float isGametypeInFilter(float gt, float tp, float ts, string pattern)
 {
-       string subpattern, subpattern2, subpattern3;
+       string subpattern, subpattern2, subpattern3, subpattern4;
        subpattern = strcat(",", GametypeNameFromType(gt), ",");
        if(tp)
                subpattern2 = ",teams,";
        else
                subpattern2 = ",noteams,";
+       if(ts)
+               subpattern3 = ",teamspawns,";
+       else
+               subpattern3 = ",noteamspawns,";
        if(gt == GAME_RACE || gt == GAME_CTS)
-               subpattern3 = ",race,";
+               subpattern4 = ",race,";
        else
-               subpattern3 = string_null;
+               subpattern4 = string_null;
 
        if(substring(pattern, 0, 1) == "-")
        {
@@ -1384,7 +1435,9 @@ float isGametypeInFilter(float gt, float tp, string pattern)
                        return 0;
                if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) >= 0)
                        return 0;
-               if(subpattern3 && strstrofs(strcat(",", pattern, ","), subpattern3, 0) >= 0)
+               if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) >= 0)
+                       return 0;
+               if(subpattern4 && strstrofs(strcat(",", pattern, ","), subpattern4, 0) >= 0)
                        return 0;
        }
        else
@@ -1393,7 +1446,8 @@ float isGametypeInFilter(float gt, float tp, string pattern)
                        pattern = substring(pattern, 1, strlen(pattern) - 1);
                if(strstrofs(strcat(",", pattern, ","), subpattern, 0) < 0)
                if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) < 0)
-               if((!subpattern3) || strstrofs(strcat(",", pattern, ","), subpattern3, 0) < 0)
+               if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) < 0)
+               if((!subpattern4) || strstrofs(strcat(",", pattern, ","), subpattern4, 0) < 0)
                        return 0;
        }
        return 1;
@@ -1514,12 +1568,6 @@ vector solve_quadratic(float a, float b, float c) // ax^2 + bx + c = 0
        return v;
 }
 
-
-float _unacceptable_compiler_bug_1_a(float b, float c) { return b == c; }
-float _unacceptable_compiler_bug_1_b() { return 1; }
-float _unacceptable_compiler_bug_1_c(float d) { return 2 * d; }
-float _unacceptable_compiler_bug_1_d() { return 1; }
-
 void check_unacceptable_compiler_bugs()
 {
        if(cvar("_allow_unacceptable_compiler_bugs"))
@@ -1837,7 +1885,13 @@ float get_model_parameters(string m, float sk)
        fn = get_model_datafilename(m, sk, "txt");
        fh = fopen(fn, FILE_READ);
        if(fh < 0)
-               return 0;
+       {
+               sk = 0;
+               fn = get_model_datafilename(m, sk, "txt");
+               fh = fopen(fn, FILE_READ);
+               if(fh < 0)
+                       return 0;
+       }
 
        get_model_parameters_modelname = m;
        get_model_parameters_modelskin = sk;
@@ -1973,3 +2027,61 @@ float u8_strsize(string s)
        }
        return l;
 }
+
+// translation helpers
+string language_filename(string s)
+{
+       string fn;
+       float fh;
+       fn = prvm_language;
+       if(fn == "" || fn == "dump")
+               return s;
+       fn = strcat(s, ".", fn);
+       if((fh = fopen(fn, FILE_READ)) >= 0)
+       {
+               fclose(fh);
+               return fn;
+       }
+       return s;
+}
+string CTX(string s)
+{
+       float p = strstrofs(s, "^", 0);
+       if(p < 0)
+               return s;
+       return substring(s, p+1, -1);
+}
+
+// x-encoding (encoding as zero length invisible string)
+const string XENCODE_2  = "xX";
+const string XENCODE_22 = "0123456789abcdefABCDEF";
+string xencode(float f)
+{
+       float a, b, c, d;
+       d = mod(f, 22); f = floor(f / 22);
+       c = mod(f, 22); f = floor(f / 22);
+       b = mod(f, 22); f = floor(f / 22);
+       a = mod(f,  2); // f = floor(f /  2);
+       return strcat(
+               "^",
+               substring(XENCODE_2,  a, 1),
+               substring(XENCODE_22, b, 1),
+               substring(XENCODE_22, c, 1),
+               substring(XENCODE_22, d, 1)
+       );
+}
+float xdecode(string s)
+{
+       float a, b, c, d;
+       if(substring(s, 0, 1) != "^")
+               return -1;
+       if(strlen(s) < 5)
+               return -1;
+       a = strstrofs(XENCODE_2,  substring(s, 1, 1), 0);
+       b = strstrofs(XENCODE_22, substring(s, 2, 1), 0);
+       c = strstrofs(XENCODE_22, substring(s, 3, 1), 0);
+       d = strstrofs(XENCODE_22, substring(s, 4, 1), 0);
+       if(a < 0 || b < 0 || c < 0 || d < 0)
+               return -1;
+       return ((a * 22 + b) * 22 + c) * 22 + d;
+}