]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/util.qc
Declare more ints as ints
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qc
index 15cde708ab84978e148be46ae7abf388c7ea33b7..e5eadc4525bf77e1f7d201abbfdb303dcfd749ef 100644 (file)
@@ -259,7 +259,7 @@ float fexists(string f)
 }
 
 // Databases (hash tables)
-#define DB_BUCKETS 8192
+const float DB_BUCKETS = 8192;
 void db_save(float db, string pFilename)
 {
        float fh, i, n;
@@ -346,14 +346,14 @@ void db_close(float db)
 string db_get(float db, string pKey)
 {
        float h;
-       h = mod(crc16(FALSE, pKey), DB_BUCKETS);
+       h = crc16(FALSE, pKey) % DB_BUCKETS;
        return uri_unescape(infoget(bufstr_get(db, h), pKey));
 }
 
 void db_put(float db, string pKey, string pValue)
 {
        float h;
-       h = mod(crc16(FALSE, pKey), DB_BUCKETS);
+       h = crc16(FALSE, pKey) % DB_BUCKETS;
        bufstr_set(db, h, infoadd(bufstr_get(db, h), pKey, uri_escape(pValue)));
 }
 
@@ -408,6 +408,22 @@ void buf_save(float buf, string pFilename)
        fclose(fh);
 }
 
+string format_time(float seconds)
+{
+       float days, hours, minutes;
+       seconds = floor(seconds + 0.5);
+        days = floor(seconds / 864000);
+        seconds -= days * 864000;
+        hours = floor(seconds / 36000);
+        seconds -= hours * 36000;
+       minutes = floor(seconds / 600);
+       seconds -= minutes * 600;
+        if (days > 0)
+                return sprintf(_("%d days, %02d:%02d:%02d"), days, hours, minutes, seconds);
+        else
+                return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds);
+}
+
 string mmsss(float tenths)
 {
        float minutes;
@@ -430,7 +446,7 @@ string mmssss(float hundredths)
        return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2));
 }
 
-string ScoreString(float pFlags, float pValue)
+string ScoreString(int pFlags, float pValue)
 {
        string valstr;
        float l;
@@ -489,7 +505,7 @@ float lengthLogTable[128];
 
 float invertLengthLog(float x)
 {
-       float l, r, m, lerr, rerr;
+       int l, r, m;
 
        if(x >= lengthLogTable[127])
                return 127;
@@ -509,22 +525,21 @@ float invertLengthLog(float x)
        }
 
        // now: r is >=, l is <
-       lerr = (x - lengthLogTable[l]);
-       rerr = (lengthLogTable[r] - x);
+       float lerr = (x - lengthLogTable[l]);
+       float rerr = (lengthLogTable[r] - x);
        if(lerr < rerr)
                return l;
        return r;
 }
 
-vector decompressShortVector(float data)
+vector decompressShortVector(int data)
 {
        vector out;
-       float p, y, len;
        if(data == 0)
                return '0 0 0';
-       p   = (data & 0xF000) / 0x1000;
-       y   = (data & 0x0F80) / 0x80;
-       len = (data & 0x007F);
+       float p = (data & 0xF000) / 0x1000;
+       float y = (data & 0x0F80) / 0x80;
+       int len = (data & 0x007F);
 
        //print("\ndecompress: p ", ftos(p)); print("y ", ftos(y)); print("len ", ftos(len), "\n");
 
@@ -585,9 +600,9 @@ float compressShortVector(vector vec)
 
 void compressShortVector_init()
 {
-       float l, f, i;
-       l = 1;
-       f = pow(2, 1/8);
+       float l = 1;
+       float f = pow(2, 1/8);
+       int i;
        for(i = 0; i < 128; ++i)
        {
                lengthLogTable[i] = l;
@@ -1297,17 +1312,17 @@ float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_
 
 string find_last_color_code(string s)
 {
-       float start, len, i, carets;
-       start = strstrofs(s, "^", 0);
+       int start = strstrofs(s, "^", 0);
        if (start == -1) // no caret found
                return "";
-       len = strlen(s)-1;
+       int len = strlen(s)-1;
+       int i;
        for(i = len; i >= start; --i)
        {
                if(substring(s, i, 1) != "^")
                        continue;
 
-               carets = 1;
+               int carets = 1;
                while (i-carets >= start && substring(s, i-carets, 1) == "^")
                        ++carets;
 
@@ -1703,19 +1718,6 @@ vector get_shotvelocity(vector myvel, vector mydir, float spd, float newton_styl
        return myvel + spd * mydir;
 }
 
-void check_unacceptable_compiler_bugs()
-{
-       if(cvar("_allow_unacceptable_compiler_bugs"))
-               return;
-       tokenize_console("foo bar");
-       if(strcat(argv(0), substring("foo bar", 4, 7 - argv_start_index(1))) == "barbar")
-               error("fteqcc bug introduced with revision 3178 detected. Please upgrade fteqcc to a later revision, downgrade fteqcc to revision 3177, or pester Spike until he fixes it. You can set _allow_unacceptable_compiler_bugs 1 to skip this check, but expect stuff to be horribly broken then.");
-
-       string s = "";
-       if (!s)
-               error("The empty string counts as false. We do not want that!");
-}
-
 float compressShotOrigin(vector v)
 {
        float x, y, z;
@@ -1739,7 +1741,7 @@ float compressShotOrigin(vector v)
        }
        return x * 0x10000 + y * 0x100 + z;
 }
-vector decompressShotOrigin(float f)
+vector decompressShotOrigin(int f)
 {
        vector v;
        v_x = ((f & 0xFF0000) / 0x10000) / 2;
@@ -2255,13 +2257,13 @@ string CTX(string s)
 // x-encoding (encoding as zero length invisible string)
 const string XENCODE_2  = "xX";
 const string XENCODE_22 = "0123456789abcdefABCDEF";
-string xencode(float f)
+string xencode(int 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);
+       d = f % 22; f = floor(f / 22);
+       c = f % 22; f = floor(f / 22);
+       b = f % 22; f = floor(f / 22);
+       a = f %  2; // f = floor(f /  2);
        return strcat(
                "^",
                substring(XENCODE_2,  a, 1),
@@ -2286,7 +2288,7 @@ float xdecode(string s)
        return ((a * 22 + b) * 22 + c) * 22 + d;
 }
 
-float lowestbit(float f)
+float lowestbit(int f)
 {
        f &= ~(f * 2);
        f &= ~(f * 4);
@@ -2370,7 +2372,7 @@ void m_shutdown()
        cvar_settemp_restore(); // this must be done LAST, but in any case
 }
 
-#define APPROXPASTTIME_ACCURACY_REQUIREMENT 0.05
+const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
 #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
 #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
 // this will use the value:
@@ -2585,7 +2587,6 @@ vector get_corner_position(entity box, float corner)
 #endif
 
 // todo: this sucks, lets find a better way to do backtraces?
-#ifndef MENUQC
 void backtrace(string msg)
 {
        float dev, war;
@@ -2607,7 +2608,6 @@ void backtrace(string msg)
        cvar_set("developer", ftos(dev));
        cvar_set("prvm_backtraceforwarnings", ftos(war));
 }
-#endif
 
 // color code replace, place inside of sprintf and parse the string
 string CCR(string input)
@@ -2800,7 +2800,7 @@ float Mod_Q1BSP_SuperContentsFromNativeContents(float nativecontents)
        return 0;
 }
 
-float Mod_Q1BSP_NativeContentsFromSuperContents(float supercontents)
+float Mod_Q1BSP_NativeContentsFromSuperContents(int supercontents)
 {
        if(supercontents & (DPCONTENTS_SOLID | DPCONTENTS_BODY))
                return CONTENT_SOLID;