]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/util.qc
net_ cvars also are generally safe
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qc
index 87db7a3f4de28daae6c4afabd33203c4745da931..42be955f0c48d0b851c56077442b4c3cfecb9f59 100644 (file)
@@ -226,43 +226,8 @@ float median(float a, float b, float c)
 // works for up to 10 decimals!
 string ftos_decimals(float number, float decimals)
 {
-       string result;
-       string tmp;
-       float len;
-
-       // if negative, cut off the sign first
-       if(number < 0)
-               return strcat("-", ftos_decimals(-number, decimals));
-       // it now is always positive!
-
-       // 3.516 -> 352
-       number = floor(number * pow(10, decimals) + 0.5);
-
-       // 352 -> "352"
-       result = ftos(number);
-       len = strlen(result);
-       // does it have a decimal point (should not happen)? If there is one, it is always at len-7)
-               // if ftos had messed it up, which should never happen: "34278.000000"
-       if(len >= 7)
-               if(substring(result, len - 7, 1) == ".")
-               {
-                       dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
-                       result = substring(result, 0, len - 7);
-                       len -= 7;
-               }
-               // "34278"
-       if(decimals == 0)
-               return result; // don't insert a point for zero decimals
-       // is it too short? If yes, insert leading zeroes
-       if(len <= decimals)
-       {
-               result = strcat(substring("0000000000", 0, decimals - len + 1), result);
-               len = decimals + 1;
-       }
-       // and now... INSERT THE POINT!
-       tmp = substring(result, len - decimals, decimals);
-       result = strcat(substring(result, 0, len - decimals), ".", tmp);
-       return result;
+       // we have sprintf...
+       return sprintf("%.*f", decimals, number);
 }
 
 float time;
@@ -344,7 +309,8 @@ float db_load(string pFilename)
        fh = fopen(pFilename, FILE_READ);
        if(fh < 0)
                return db;
-       if(stof(fgets(fh)) == DB_BUCKETS)
+       l = fgets(fh);
+       if(stof(l) == DB_BUCKETS)
        {
                i = 0;
                while((l = fgets(fh)))
@@ -356,14 +322,18 @@ float db_load(string pFilename)
        }
        else
        {
-               // different count of buckets?
+               // different count of buckets, or a dump?
                // need to reorganize the database then (SLOW)
-               while((l = fgets(fh)))
+               //
+               // note: we also parse the first line (l) in case the DB file is
+               // missing the bucket count
+               do
                {
                        n = tokenizebyseparator(l, "\\");
                        for(j = 2; j < n; j += 2)
                                db_put(db, argv(j-1), uri_unescape(argv(j)));
                }
+               while((l = fgets(fh)));
        }
        fclose(fh);
        return db;
@@ -1949,41 +1919,24 @@ float get_model_parameters(string m, float sk)
        return 1;
 }
 
-// Get name of specified panel id
-#define HUD_Panel_GetName(id) \
-switch(id) { \
-       case HUD_PANEL_WEAPONICONS: panel_name = HUD_PANELNAME_WEAPONICONS; return; \
-       case HUD_PANEL_INVENTORY: panel_name = HUD_PANELNAME_INVENTORY; return; \
-       case HUD_PANEL_POWERUPS: panel_name = HUD_PANELNAME_POWERUPS; return; \
-       case HUD_PANEL_HEALTHARMOR: panel_name = HUD_PANELNAME_HEALTHARMOR; return; \
-       case HUD_PANEL_NOTIFY: panel_name = HUD_PANELNAME_NOTIFY; return; \
-       case HUD_PANEL_TIMER: panel_name = HUD_PANELNAME_TIMER; return; \
-       case HUD_PANEL_RADAR: panel_name = HUD_PANELNAME_RADAR; return; \
-       case HUD_PANEL_SCORE: panel_name = HUD_PANELNAME_SCORE; return; \
-       case HUD_PANEL_RACETIMER: panel_name = HUD_PANELNAME_RACETIMER; return; \
-       case HUD_PANEL_VOTE: panel_name = HUD_PANELNAME_VOTE; return; \
-       case HUD_PANEL_MODICONS: panel_name = HUD_PANELNAME_MODICONS; return; \
-       case HUD_PANEL_PRESSEDKEYS: panel_name = HUD_PANELNAME_PRESSEDKEYS; return; \
-       case HUD_PANEL_CHAT: panel_name = HUD_PANELNAME_CHAT; return; \
-       case HUD_PANEL_ENGINEINFO: panel_name = HUD_PANELNAME_ENGINEINFO; return; \
-}
-
-// Update all cvars of given panel
-#define HUD_Panel_UpdateCvars(name) \
-panel_enabled = autocvar_hud_##name; \
-panel_pos = autocvar_hud_##name_pos; \
-panel_size = autocvar_hud_##name_size; \
-panel_bg = autocvar_hud_##name_bg; \
-panel_bg_color = autocvar_hud_##name_bg_color; \
-panel_bg_color_team = autocvar_hud_##name_bg_color_team; \
-panel_bg_alpha = autocvar_hud_##name_bg_alpha; \
-panel_bg_border = autocvar_hud_##name_bg_border; \
-panel_bg_padding = autocvar_hud_##name_bg_padding; \
-\
-panel_accuracy_yellow = autocvar_hud_##name_accuracy_yellow; \
-panel_onlycurrent = autocvar_hud_##name_onlycurrent; \
-panel_flip = autocvar_hud_##name_flip; \
-panel_iconalign = autocvar_hud_##name_iconalign; \
-panel_baralign = autocvar_hud_##name_baralign; \
-panel_info_top = autocvar_hud_##name_info_top; \
-panel_alreadyvoted_alpha = autocvar_hud_##name_alreadyvoted_alpha;
+vector vec2(vector v)
+{
+       v_z = 0;
+       return v;
+}
+
+#ifndef MENUQC
+vector NearestPointOnBox(entity box, vector org)
+{
+       vector m1, m2, nearest;
+
+       m1 = box.mins + box.origin;
+       m2 = box.maxs + box.origin;
+
+       nearest_x = bound(m1_x, org_x, m2_x);
+       nearest_y = bound(m1_y, org_y, m2_y);
+       nearest_z = bound(m1_z, org_z, m2_z);
+
+       return nearest;
+}
+#endif