]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/client/hud/panel/scoreboard.qc
Scoreboard: show number of spectators
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / scoreboard.qc
index 72a058d5feb7d7534bd74eb8f52363ae1ecc955f..52814acb08e91720c69d7cf9b2cbcfeac378ffa8 100644 (file)
@@ -8,19 +8,65 @@
 #include <common/stats.qh>
 #include <common/teams.qh>
 
-float scoreboard_alpha_bg;
-float scoreboard_alpha_fg;
-float scoreboard_highlight;
-float scoreboard_highlight_alpha;
-float scoreboard_highlight_alpha_self;
-float scoreboard_alpha_name;
-float scoreboard_alpha_name_self;
+// Scoreboard (#24)
+
+const int MAX_SBT_FIELDS = MAX_SCORE;
+
+PlayerScoreField sbt_field[MAX_SBT_FIELDS + 1];
+float sbt_field_size[MAX_SBT_FIELDS + 1];
+string sbt_field_title[MAX_SBT_FIELDS + 1];
+int sbt_num_fields;
+
+string autocvar_hud_fontsize;
+string hud_fontsize_str;
+float max_namesize;
+
+float sbt_bg_alpha;
+float sbt_fg_alpha;
+float sbt_fg_alpha_self;
+bool sbt_highlight;
+float sbt_highlight_alpha;
+float sbt_highlight_alpha_self;
+
+// provide basic panel cvars to old clients
+// TODO remove them after a future release (0.8.2+)
+string autocvar_hud_panel_scoreboard_pos = "0.150000 0.150000";
+string autocvar_hud_panel_scoreboard_size = "0.700000 0.700000";
+string autocvar_hud_panel_scoreboard_bg = "border_default";
+string autocvar_hud_panel_scoreboard_bg_color = "0 0.3 0.5";
+string autocvar_hud_panel_scoreboard_bg_color_team = "";
+string autocvar_hud_panel_scoreboard_bg_alpha = "0.7";
+string autocvar_hud_panel_scoreboard_bg_border = "";
+string autocvar_hud_panel_scoreboard_bg_padding = "";
+
+float autocvar_hud_panel_scoreboard_fadeinspeed = 10;
+float autocvar_hud_panel_scoreboard_fadeoutspeed = 5;
+float autocvar_hud_panel_scoreboard_respawntime_decimals = 1;
+float autocvar_hud_panel_scoreboard_table_bg_alpha = 0;
+float autocvar_hud_panel_scoreboard_table_bg_scale = 0.25;
+float autocvar_hud_panel_scoreboard_table_fg_alpha = 0.9;
+float autocvar_hud_panel_scoreboard_table_fg_alpha_self = 1;
+bool autocvar_hud_panel_scoreboard_table_highlight = true;
+float autocvar_hud_panel_scoreboard_table_highlight_alpha = 0.2;
+float autocvar_hud_panel_scoreboard_table_highlight_alpha_self = 0.4;
+float autocvar_hud_panel_scoreboard_bg_teams_color_team = 0;
+float autocvar_hud_panel_scoreboard_namesize = 15;
+
+bool autocvar_hud_panel_scoreboard_accuracy = true;
+bool autocvar_hud_panel_scoreboard_accuracy_doublerows = false;
+bool autocvar_hud_panel_scoreboard_accuracy_nocolors = false;
+
+bool autocvar_hud_panel_scoreboard_dynamichud = false;
+
+float autocvar_hud_panel_scoreboard_maxheight = 0.5;
+bool autocvar_hud_panel_scoreboard_others_showscore = true;
+bool autocvar_hud_panel_scoreboard_spectators_showping = true;
+float autocvar_hud_panel_scoreboard_minwidth = 0.4;
+
 
 void drawstringright(vector, string, vector, vector, float, float);
 void drawstringcenter(vector, string, vector, vector, float, float);
 
-const float SCOREBOARD_OFFSET = 50;
-
 // wrapper to put all possible scores titles through gettext
 string TranslateScoresLabel(string l)
 {
@@ -32,7 +78,7 @@ string TranslateScoresLabel(string l)
                case "captime": return CTX(_("SCO^captime"));
                case "deaths": return CTX(_("SCO^deaths"));
                case "destroyed": return CTX(_("SCO^destroyed"));
-               case "dmg": return CTX(_("SCO^dmg"));
+               case "dmg": return CTX(_("SCO^damage"));
                case "dmgtaken": return CTX(_("SCO^dmgtaken"));
                case "drops": return CTX(_("SCO^drops"));
                case "faults": return CTX(_("SCO^faults"));
@@ -40,8 +86,7 @@ string TranslateScoresLabel(string l)
                case "goals": return CTX(_("SCO^goals"));
                case "kckills": return CTX(_("SCO^kckills"));
                case "kdratio": return CTX(_("SCO^kdratio"));
-               case "k/d": return CTX(_("SCO^k/d"));
-               case "kd": return CTX(_("SCO^kd"));
+               case "kd": return CTX(_("SCO^k/d"));
                case "kdr": return CTX(_("SCO^kdr"));
                case "kills": return CTX(_("SCO^kills"));
                case "laps": return CTX(_("SCO^laps"));
@@ -58,6 +103,7 @@ string TranslateScoresLabel(string l)
                case "rank": return CTX(_("SCO^rank"));
                case "returns": return CTX(_("SCO^returns"));
                case "revivals": return CTX(_("SCO^revivals"));
+               case "rounds": return CTX(_("SCO^rounds won"));
                case "score": return CTX(_("SCO^score"));
                case "suicides": return CTX(_("SCO^suicides"));
                case "takes": return CTX(_("SCO^takes"));
@@ -66,25 +112,25 @@ string TranslateScoresLabel(string l)
        }
 }
 
-void HUD_InitScores()
+void Scoreboard_InitScores()
 {
        int i, f;
 
-       ps_primary = ps_secondary = ts_primary = ts_secondary = -1;
-       for(i = 0; i < MAX_SCORE; ++i)
-       {
-               f = (scores_flags[i] & SFL_SORT_PRIO_MASK);
+       ps_primary = ps_secondary = NULL;
+       ts_primary = ts_secondary = -1;
+       FOREACH(Scores, true, {
+               f = (scores_flags(it) & SFL_SORT_PRIO_MASK);
                if(f == SFL_SORT_PRIO_PRIMARY)
-                       ps_primary = i;
+                       ps_primary = it;
                if(f == SFL_SORT_PRIO_SECONDARY)
-                       ps_secondary = i;
-       }
-       if(ps_secondary == -1)
+                       ps_secondary = it;
+       });
+       if(ps_secondary == NULL)
                ps_secondary = ps_primary;
 
        for(i = 0; i < MAX_TEAMSCORE; ++i)
        {
-               f = (teamscores_flags[i] & SFL_SORT_PRIO_MASK);
+               f = (teamscores_flags(i) & SFL_SORT_PRIO_MASK);
                if(f == SFL_SORT_PRIO_PRIMARY)
                        ts_primary = i;
                if(f == SFL_SORT_PRIO_SECONDARY)
@@ -93,26 +139,24 @@ void HUD_InitScores()
        if(ts_secondary == -1)
                ts_secondary = ts_primary;
 
-       Cmd_HUD_SetFields(0);
+       Cmd_Scoreboard_SetFields(0);
 }
 
 float SetTeam(entity pl, float Team);
 //float lastpnum;
-void HUD_UpdatePlayerTeams()
+void Scoreboard_UpdatePlayerTeams()
 {
        float Team;
        entity pl, tmp;
-       float num;
-
-       num = 0;
+       //int num = 0;
        for(pl = players.sort_next; pl; pl = pl.sort_next)
        {
-               num += 1;
+               //num += 1;
                Team = entcs_GetScoreTeam(pl.sv_entnum);
                if(SetTeam(pl, Team))
                {
                        tmp = pl.sort_prev;
-                       HUD_UpdatePlayerPos(pl);
+                       Scoreboard_UpdatePlayerPos(pl);
                        if(tmp)
                                pl = tmp;
                        else
@@ -126,7 +170,7 @@ void HUD_UpdatePlayerTeams()
        */
 }
 
-int HUD_CompareScore(int vl, int vr, int f)
+int Scoreboard_CompareScore(int vl, int vr, int f)
 {
     TC(int, vl); TC(int, vr); TC(int, f);
        if(f & SFL_ZERO_IS_WORST)
@@ -143,7 +187,7 @@ int HUD_CompareScore(int vl, int vr, int f)
        return -1;
 }
 
-float HUD_ComparePlayerScores(entity left, entity right)
+float Scoreboard_ComparePlayerScores(entity left, entity right)
 {
        float vl, vr, r;
        vl = entcs_GetTeam(left.sv_entnum);
@@ -168,21 +212,18 @@ float HUD_ComparePlayerScores(entity left, entity right)
                return false;
        }
 
-       r = HUD_CompareScore(left.scores[ps_primary], right.scores[ps_primary], scores_flags[ps_primary]);
+       r = Scoreboard_CompareScore(left.scores(ps_primary), right.scores(ps_primary), scores_flags(ps_primary));
        if (r >= 0)
                return r;
 
-       r = HUD_CompareScore(left.scores[ps_secondary], right.scores[ps_secondary], scores_flags[ps_secondary]);
+       r = Scoreboard_CompareScore(left.scores(ps_secondary), right.scores(ps_secondary), scores_flags(ps_secondary));
        if (r >= 0)
                return r;
 
-       int i;
-       for(i = 0; i < MAX_SCORE; ++i)
-       {
-               r = HUD_CompareScore(left.scores[i], right.scores[i], scores_flags[i]);
-               if (r >= 0)
-                       return r;
-       }
+       FOREACH(Scores, true, {
+               r = Scoreboard_CompareScore(left.scores(it), right.scores(it), scores_flags(it));
+               if (r >= 0) return r;
+       });
 
        if (left.sv_entnum < right.sv_entnum)
                return true;
@@ -190,20 +231,20 @@ float HUD_ComparePlayerScores(entity left, entity right)
        return false;
 }
 
-void HUD_UpdatePlayerPos(entity player)
+void Scoreboard_UpdatePlayerPos(entity player)
 {
        entity ent;
-       for(ent = player.sort_next; ent && HUD_ComparePlayerScores(player, ent); ent = player.sort_next)
+       for(ent = player.sort_next; ent && Scoreboard_ComparePlayerScores(player, ent); ent = player.sort_next)
        {
                SORT_SWAP(player, ent);
        }
-       for(ent = player.sort_prev; ent != players && HUD_ComparePlayerScores(ent, player); ent = player.sort_prev)
+       for(ent = player.sort_prev; ent != players && Scoreboard_ComparePlayerScores(ent, player); ent = player.sort_prev)
        {
                SORT_SWAP(ent, player);
        }
 }
 
-float HUD_CompareTeamScores(entity left, entity right)
+float Scoreboard_CompareTeamScores(entity left, entity right)
 {
        int i, r;
 
@@ -212,17 +253,17 @@ float HUD_CompareTeamScores(entity left, entity right)
        if(right.team == NUM_SPECTATOR)
                return 0;
 
-       r = HUD_CompareScore(left.teamscores[ts_primary], right.teamscores[ts_primary], teamscores_flags[ts_primary]);
+       r = Scoreboard_CompareScore(left.teamscores(ts_primary), right.teamscores(ts_primary), teamscores_flags(ts_primary));
        if (r >= 0)
                return r;
 
-       r = HUD_CompareScore(left.teamscores[ts_secondary], right.teamscores[ts_secondary], teamscores_flags[ts_secondary]);
+       r = Scoreboard_CompareScore(left.teamscores(ts_secondary), right.teamscores(ts_secondary), teamscores_flags(ts_secondary));
        if (r >= 0)
                return r;
 
-       for(i = 0; i < MAX_SCORE; ++i)
+       for(i = 0; i < MAX_TEAMSCORE; ++i)
        {
-               r = HUD_CompareScore(left.teamscores[i], right.teamscores[i], teamscores_flags[i]);
+               r = Scoreboard_CompareScore(left.teamscores(i), right.teamscores(i), teamscores_flags(i));
                if (r >= 0)
                        return r;
        }
@@ -233,20 +274,20 @@ float HUD_CompareTeamScores(entity left, entity right)
        return false;
 }
 
-void HUD_UpdateTeamPos(entity Team)
+void Scoreboard_UpdateTeamPos(entity Team)
 {
        entity ent;
-       for(ent = Team.sort_next; ent && HUD_CompareTeamScores(Team, ent); ent = Team.sort_next)
+       for(ent = Team.sort_next; ent && Scoreboard_CompareTeamScores(Team, ent); ent = Team.sort_next)
        {
                SORT_SWAP(Team, ent);
        }
-       for(ent = Team.sort_prev; ent != teams && HUD_CompareTeamScores(ent, Team); ent = Team.sort_prev)
+       for(ent = Team.sort_prev; ent != teams && Scoreboard_CompareTeamScores(ent, Team); ent = Team.sort_prev)
        {
                SORT_SWAP(ent, Team);
        }
 }
 
-void Cmd_HUD_Help()
+void Cmd_Scoreboard_Help()
 {
        LOG_INFO(_("You can modify the scoreboard using the ^2scoreboard_columns_set command.\n"));
        LOG_INFO(_("^3|---------------------------------------------------------------|\n"));
@@ -254,11 +295,13 @@ void Cmd_HUD_Help()
        LOG_INFO(_("^2scoreboard_columns_set default\n"));
        LOG_INFO(_("^2scoreboard_columns_set ^7field1 field2 ...\n"));
        LOG_INFO(_("The following field names are recognized (case insensitive):\n"));
-       LOG_INFO(_("You can use a ^3|^7 to start the right-aligned fields.\n\n"));
+       LOG_INFO(_("You can use a ^3|^7 to start the right-aligned fields.\n"));
+       LOG_INFO("\n");
 
        LOG_INFO(_("^3name^7 or ^3nick^7             Name of a player\n"));
        LOG_INFO(_("^3ping^7                     Ping time\n"));
        LOG_INFO(_("^3pl^7                       Packet loss\n"));
+       LOG_INFO(_("^3elo^7                      Player ELO\n"));
        LOG_INFO(_("^3kills^7                    Number of kills\n"));
        LOG_INFO(_("^3deaths^7                   Number of deaths\n"));
        LOG_INFO(_("^3suicides^7                 Number of suicides\n"));
@@ -286,7 +329,8 @@ void Cmd_HUD_Help()
        LOG_INFO(_("^3takes^7                    Number of domination points taken (DOM)\n"));
        LOG_INFO(_("^3bckills^7                  Number of ball carrier kills\n"));
        LOG_INFO(_("^3bctime^7                   Total amount of time holding the ball in Keepaway\n"));
-       LOG_INFO(_("^3score^7                    Total score\n\n"));
+       LOG_INFO(_("^3score^7                    Total score\n"));
+       LOG_INFO("\n");
 
        LOG_INFO(_("Before a field you can put a + or - sign, then a comma separated list\n"
                "of game types, then a slash, to make the field show up only in these\n"
@@ -322,23 +366,21 @@ void Cmd_HUD_Help()
 " +ka/pickups +ka/bckills +ka/bctime +ft/revivals" \
 " -lms,rc,cts,inv,nb/score"
 
-void Cmd_HUD_SetFields(int argc)
+void Cmd_Scoreboard_SetFields(int argc)
 {
     TC(int, argc);
-       int i, j, slash;
+       int i, slash;
        string str, pattern;
-       float have_name = 0, have_primary = 0, have_secondary = 0, have_separator = 0;
-       float missing;
+       bool have_name = false, have_primary = false, have_secondary = false, have_separator = false;
+       int missing;
 
        if(!gametype)
-       {
-               // set up a temporary scoreboard layout
-               // no layout can be properly set up until score_info data haven't been received
-               argc = tokenizebyseparator("0 1 ping pl name | score", " ");
-               ps_primary = 0;
-               scores_label[ps_primary] = strzone("score");
-               scores_flags[ps_primary] = SFL_ALLOW_HIDE;
-       }
+               return; // do nothing, we don't know gametype and scores yet
+
+       // sbt_fields uses strunzone on the titles!
+       if(!sbt_field_title[0])
+               for(i = 0; i < MAX_SBT_FIELDS; ++i)
+                       sbt_field_title[i] = strzone("(null)");
 
        // TODO: re enable with gametype dependant cvars?
        if(argc < 3) // no arguments provided
@@ -355,22 +397,21 @@ void Cmd_HUD_SetFields(int argc)
                {
                        string s;
                        s = "ping pl name |";
-                       for(i = 0; i < MAX_SCORE; ++i)
-                       {
-                               if(i != ps_primary)
-                               if(i != ps_secondary)
-                               if(scores_label[i] != "")
-                                       s = strcat(s, " ", scores_label[i]);
-                       }
+                       FOREACH(Scores, true, {
+                               if(it != ps_primary)
+                               if(it != ps_secondary)
+                               if(scores_label(it) != "")
+                                       s = strcat(s, " ", scores_label(it));
+                       });
                        if(ps_secondary != ps_primary)
-                               s = strcat(s, " ", scores_label[ps_secondary]);
-                       s = strcat(s, " ", scores_label[ps_primary]);
+                               s = strcat(s, " ", scores_label(ps_secondary));
+                       s = strcat(s, " ", scores_label(ps_primary));
                        argc = tokenizebyseparator(strcat("0 1 ", s), " ");
                }
        }
 
 
-       hud_num_fields = 0;
+       sbt_num_fields = 0;
 
        hud_fontsize = HUD_GetFontsize("hud_fontsize");
 
@@ -396,26 +437,31 @@ void Cmd_HUD_SetFields(int argc)
                                continue;
                }
 
-               strunzone(hud_title[hud_num_fields]);
-               hud_title[hud_num_fields] = strzone(TranslateScoresLabel(str));
-               hud_size[hud_num_fields] = stringwidth(hud_title[hud_num_fields], false, hud_fontsize);
+               strunzone(sbt_field_title[sbt_num_fields]);
+               sbt_field_title[sbt_num_fields] = strzone(TranslateScoresLabel(str));
+               sbt_field_size[sbt_num_fields] = stringwidth(sbt_field_title[sbt_num_fields], false, hud_fontsize);
                str = strtolower(str);
 
+               PlayerScoreField j;
                switch(str)
                {
-                       case "ping": hud_field[hud_num_fields] = SP_PING; break;
-                       case "pl": hud_field[hud_num_fields] = SP_PL; break;
-                       case "kd": case "kdr": case "kdratio": case "k/d": hud_field[hud_num_fields] = SP_KDRATIO; break;
-                       case "sum": case "diff": case "k-d": hud_field[hud_num_fields] = SP_SUM; break;
-                       case "name": case "nick": hud_field[hud_num_fields] = SP_NAME; have_name = true; break;
-                       case "|": hud_field[hud_num_fields] = SP_SEPARATOR; have_separator = true; break;
-                       case "dmg": hud_field[hud_num_fields] = SP_DMG; break;
-                       case "dmgtaken": hud_field[hud_num_fields] = SP_DMGTAKEN; break;
+                       case "ping": sbt_field[sbt_num_fields] = SP_PING; break;
+                       case "pl": sbt_field[sbt_num_fields] = SP_PL; break;
+                       case "kd": case "kdr": case "kdratio": sbt_field[sbt_num_fields] = SP_KDRATIO; break;
+                       case "sum": case "diff": case "k-d": sbt_field[sbt_num_fields] = SP_SUM; break;
+                       case "name": case "nick": sbt_field[sbt_num_fields] = SP_NAME; have_name = true; break;
+                       case "|": sbt_field[sbt_num_fields] = SP_SEPARATOR; have_separator = true; break;
+                       case "elo": sbt_field[sbt_num_fields] = SP_ELO; break;
+                       case "dmg": case "damage": sbt_field[sbt_num_fields] = SP_DMG; break;
+                       case "dmgtaken": case "damagetaken": sbt_field[sbt_num_fields] = SP_DMGTAKEN; break;
                        default:
                        {
-                               for(j = 0; j < MAX_SCORE; ++j)
-                                       if(str == strtolower(scores_label[j]))
+                               FOREACH(Scores, true, {
+                                       if (str == strtolower(scores_label(it))) {
+                                               j = it;
                                                goto found; // sorry, but otherwise fteqcc -O3 miscompiles this and warns about "unreachable code"
+                                       }
+                               });
 
 LABEL(notfound)
                                if(str == "frags")
@@ -427,118 +473,111 @@ LABEL(notfound)
                                        continue;
                                }
 LABEL(found)
-                               hud_field[hud_num_fields] = j;
+                               sbt_field[sbt_num_fields] = j;
                                if(j == ps_primary)
-                                       have_primary = 1;
+                                       have_primary = true;
                                if(j == ps_secondary)
-                                       have_secondary = 1;
+                                       have_secondary = true;
 
                        }
                }
-               ++hud_num_fields;
-               if(hud_num_fields >= MAX_HUD_FIELDS)
+               ++sbt_num_fields;
+               if(sbt_num_fields >= MAX_SBT_FIELDS)
                        break;
        }
 
-       if(scores_flags[ps_primary] & SFL_ALLOW_HIDE)
-               have_primary = 1;
-       if(scores_flags[ps_secondary] & SFL_ALLOW_HIDE)
-               have_secondary = 1;
+       if(scores_flags(ps_primary) & SFL_ALLOW_HIDE)
+               have_primary = true;
+       if(scores_flags(ps_secondary) & SFL_ALLOW_HIDE)
+               have_secondary = true;
        if(ps_primary == ps_secondary)
-               have_secondary = 1;
+               have_secondary = true;
        missing = (!have_primary) + (!have_secondary) + (!have_separator) + (!have_name);
 
-       if(hud_num_fields+missing < MAX_HUD_FIELDS)
+       if(sbt_num_fields + missing < MAX_SBT_FIELDS)
        {
                if(!have_name)
                {
-                       strunzone(hud_title[hud_num_fields]);
-                       for(i = hud_num_fields; i > 0; --i)
+                       strunzone(sbt_field_title[sbt_num_fields]);
+                       for(i = sbt_num_fields; i > 0; --i)
                        {
-                               hud_title[i] = hud_title[i-1];
-                               hud_size[i] = hud_size[i-1];
-                               hud_field[i] = hud_field[i-1];
+                               sbt_field_title[i] = sbt_field_title[i-1];
+                               sbt_field_size[i] = sbt_field_size[i-1];
+                               sbt_field[i] = sbt_field[i-1];
                        }
-                       hud_title[0] = strzone(TranslateScoresLabel("name"));
-                       hud_field[0] = SP_NAME;
-                       ++hud_num_fields;
+                       sbt_field_title[0] = strzone(TranslateScoresLabel("name"));
+                       sbt_field[0] = SP_NAME;
+                       ++sbt_num_fields;
                        LOG_INFO("fixed missing field 'name'\n");
 
                        if(!have_separator)
                        {
-                               strunzone(hud_title[hud_num_fields]);
-                               for(i = hud_num_fields; i > 1; --i)
+                               strunzone(sbt_field_title[sbt_num_fields]);
+                               for(i = sbt_num_fields; i > 1; --i)
                                {
-                                       hud_title[i] = hud_title[i-1];
-                                       hud_size[i] = hud_size[i-1];
-                                       hud_field[i] = hud_field[i-1];
+                                       sbt_field_title[i] = sbt_field_title[i-1];
+                                       sbt_field_size[i] = sbt_field_size[i-1];
+                                       sbt_field[i] = sbt_field[i-1];
                                }
-                               hud_title[1] = strzone("|");
-                               hud_field[1] = SP_SEPARATOR;
-                               hud_size[1] = stringwidth("|", false, hud_fontsize);
-                               ++hud_num_fields;
+                               sbt_field_title[1] = strzone("|");
+                               sbt_field[1] = SP_SEPARATOR;
+                               sbt_field_size[1] = stringwidth("|", false, hud_fontsize);
+                               ++sbt_num_fields;
                                LOG_INFO("fixed missing field '|'\n");
                        }
                }
                else if(!have_separator)
                {
-                       strunzone(hud_title[hud_num_fields]);
-                       hud_title[hud_num_fields] = strzone("|");
-                       hud_size[hud_num_fields] = stringwidth("|", false, hud_fontsize);
-                       hud_field[hud_num_fields] = SP_SEPARATOR;
-                       ++hud_num_fields;
+                       strunzone(sbt_field_title[sbt_num_fields]);
+                       sbt_field_title[sbt_num_fields] = strzone("|");
+                       sbt_field_size[sbt_num_fields] = stringwidth("|", false, hud_fontsize);
+                       sbt_field[sbt_num_fields] = SP_SEPARATOR;
+                       ++sbt_num_fields;
                        LOG_INFO("fixed missing field '|'\n");
                }
                if(!have_secondary)
                {
-                       strunzone(hud_title[hud_num_fields]);
-                       hud_title[hud_num_fields] = strzone(TranslateScoresLabel(scores_label[ps_secondary]));
-                       hud_size[hud_num_fields] = stringwidth(hud_title[hud_num_fields], false, hud_fontsize);
-                       hud_field[hud_num_fields] = ps_secondary;
-                       ++hud_num_fields;
-                       LOG_INFOF("fixed missing field '%s'\n", scores_label[ps_secondary]);
+                       strunzone(sbt_field_title[sbt_num_fields]);
+                       sbt_field_title[sbt_num_fields] = strzone(TranslateScoresLabel(scores_label(ps_secondary)));
+                       sbt_field_size[sbt_num_fields] = stringwidth(sbt_field_title[sbt_num_fields], false, hud_fontsize);
+                       sbt_field[sbt_num_fields] = ps_secondary;
+                       ++sbt_num_fields;
+                       LOG_INFOF("fixed missing field '%s'\n", scores_label(ps_secondary));
                }
                if(!have_primary)
                {
-                       strunzone(hud_title[hud_num_fields]);
-                       hud_title[hud_num_fields] = strzone(TranslateScoresLabel(scores_label[ps_primary]));
-                       hud_size[hud_num_fields] = stringwidth(hud_title[hud_num_fields], false, hud_fontsize);
-                       hud_field[hud_num_fields] = ps_primary;
-                       ++hud_num_fields;
-                       LOG_INFOF("fixed missing field '%s'\n", scores_label[ps_primary]);
+                       strunzone(sbt_field_title[sbt_num_fields]);
+                       sbt_field_title[sbt_num_fields] = strzone(TranslateScoresLabel(scores_label(ps_primary)));
+                       sbt_field_size[sbt_num_fields] = stringwidth(sbt_field_title[sbt_num_fields], false, hud_fontsize);
+                       sbt_field[sbt_num_fields] = ps_primary;
+                       ++sbt_num_fields;
+                       LOG_INFOF("fixed missing field '%s'\n", scores_label(ps_primary));
                }
        }
 
-       hud_field[hud_num_fields] = SP_END;
+       sbt_field[sbt_num_fields] = SP_END;
 }
 
 // MOVEUP::
-vector hud_field_rgb;
-string hud_field_icon0;
-string hud_field_icon1;
-string hud_field_icon2;
-vector hud_field_icon0_rgb;
-vector hud_field_icon1_rgb;
-vector hud_field_icon2_rgb;
-float hud_field_icon0_alpha;
-float hud_field_icon1_alpha;
-float hud_field_icon2_alpha;
-string HUD_GetField(entity pl, int field)
+vector sbt_field_rgb;
+string sbt_field_icon0;
+string sbt_field_icon1;
+string sbt_field_icon2;
+vector sbt_field_icon0_rgb;
+vector sbt_field_icon1_rgb;
+vector sbt_field_icon2_rgb;
+string Scoreboard_GetField(entity pl, PlayerScoreField field)
 {
-    TC(int, field);
        float tmp, num, denom;
        int f;
        string str;
-       hud_field_rgb = '1 1 1';
-       hud_field_icon0 = "";
-       hud_field_icon1 = "";
-       hud_field_icon2 = "";
-       hud_field_icon0_rgb = '1 1 1';
-       hud_field_icon1_rgb = '1 1 1';
-       hud_field_icon2_rgb = '1 1 1';
-       hud_field_icon0_alpha = 1;
-       hud_field_icon1_alpha = 1;
-       hud_field_icon2_alpha = 1;
+       sbt_field_rgb = '1 1 1';
+       sbt_field_icon0 = "";
+       sbt_field_icon1 = "";
+       sbt_field_icon2 = "";
+       sbt_field_icon0_rgb = '1 1 1';
+       sbt_field_icon1_rgb = '1 1 1';
+       sbt_field_icon2_rgb = '1 1 1';
        switch(field)
        {
                case SP_PING:
@@ -549,7 +588,7 @@ string HUD_GetField(entity pl, int field)
                        if(f == 0)
                                return _("N/A");
                        tmp = max(0, min(220, f-80)) / 220;
-                       hud_field_rgb = '1 1 1' - '0 1 1'*tmp;
+                       sbt_field_rgb = '1 1 1' - '0 1 1' * tmp;
                        return ftos(f);
 
                case SP_PL:
@@ -563,498 +602,547 @@ string HUD_GetField(entity pl, int field)
                        if(tmp != 0)
                                str = strcat(str, "~", ftos(ceil(tmp * 100)));
                        tmp = bound(0, f / 0.2 + tmp / 0.04, 1); // 20% is REALLY BAD pl
-                       hud_field_rgb = '1 0.5 0.5' - '0 0.5 0.5'*tmp;
+                       sbt_field_rgb = '1 0.5 0.5' - '0 0.5 0.5' * tmp;
                        return str;
 
                case SP_NAME:
                        if(ready_waiting && pl.ready)
                        {
-                               hud_field_icon0 = "gfx/scoreboard/player_ready";
+                               sbt_field_icon0 = "gfx/scoreboard/player_ready";
                        }
                        else if(!teamplay)
                        {
-                               f = stof(getplayerkeyvalue(pl.sv_entnum, "colors"));
+                               f = entcs_GetClientColors(pl.sv_entnum);
                                {
-                                       hud_field_icon0 = "gfx/scoreboard/playercolor_base";
-                                       hud_field_icon1 = "gfx/scoreboard/playercolor_shirt";
-                                       hud_field_icon1_rgb = colormapPaletteColor(floor(f / 16), 0);
-                                       hud_field_icon2 = "gfx/scoreboard/playercolor_pants";
-                                       hud_field_icon2_rgb = colormapPaletteColor(f % 16, 1);
+                                       sbt_field_icon0 = "gfx/scoreboard/playercolor_base";
+                                       sbt_field_icon1 = "gfx/scoreboard/playercolor_shirt";
+                                       sbt_field_icon1_rgb = colormapPaletteColor(floor(f / 16), 0);
+                                       sbt_field_icon2 = "gfx/scoreboard/playercolor_pants";
+                                       sbt_field_icon2_rgb = colormapPaletteColor(f % 16, 1);
                                }
                        }
                        return entcs_GetName(pl.sv_entnum);
 
                case SP_FRAGS:
-                       f = pl.(scores[SP_KILLS]);
-                       f -= pl.(scores[SP_SUICIDES]);
+                       f = pl.(scores(SP_KILLS));
+                       f -= pl.(scores(SP_SUICIDES));
                        return ftos(f);
 
                case SP_KDRATIO:
-                       num = pl.(scores[SP_KILLS]);
-                       denom = pl.(scores[SP_DEATHS]);
+                       num = pl.(scores(SP_KILLS));
+                       denom = pl.(scores(SP_DEATHS));
 
                        if(denom == 0) {
-                               hud_field_rgb = '0 1 0';
+                               sbt_field_rgb = '0 1 0';
                                str = sprintf("%d", num);
                        } else if(num <= 0) {
-                               hud_field_rgb = '1 0 0';
+                               sbt_field_rgb = '1 0 0';
                                str = sprintf("%.1f", num/denom);
                        } else
                                str = sprintf("%.1f", num/denom);
                        return str;
 
                case SP_SUM:
-                       f = pl.(scores[SP_KILLS]);
-                       f -= pl.(scores[SP_DEATHS]);
+                       f = pl.(scores(SP_KILLS));
+                       f -= pl.(scores(SP_DEATHS));
 
                        if(f > 0) {
-                               hud_field_rgb = '0 1 0';
+                               sbt_field_rgb = '0 1 0';
                        } else if(f == 0) {
-                               hud_field_rgb = '1 1 1';
+                               sbt_field_rgb = '1 1 1';
                        } else {
-                               hud_field_rgb = '1 0 0';
+                               sbt_field_rgb = '1 0 0';
                        }
                        return ftos(f);
 
-               case SP_DMG:
-                       num = pl.(scores[SP_DMG]);
-                       denom = 1000;
-
-                       str = sprintf("%.1f k", num/denom);
-                       return str;
-
-               case SP_DMGTAKEN:
-                       num = pl.(scores[SP_DMGTAKEN]);
-                       denom = 1000;
+               case SP_ELO:
+               {
+                       float elo = pl.(scores(SP_ELO));
+                       switch (elo) {
+                               case -1: return "...";
+                               case -2: return _("N/A");
+                               default: return ftos(elo);
+                       }
+               }
 
-                       str = sprintf("%.1f k", num/denom);
-                       return str;
+               case SP_DMG: case SP_DMGTAKEN:
+                       return sprintf("%.1f k", pl.(scores(field)) / 1000);
 
                default:
-                       tmp = pl.(scores[field]);
-                       f = scores_flags[field];
+                       tmp = pl.(scores(field));
+                       f = scores_flags(field);
                        if(field == ps_primary)
-                               hud_field_rgb = '1 1 0';
+                               sbt_field_rgb = '1 1 0';
                        else if(field == ps_secondary)
-                               hud_field_rgb = '0 1 1';
+                               sbt_field_rgb = '0 1 1';
                        else
-                               hud_field_rgb = '1 1 1';
+                               sbt_field_rgb = '1 1 1';
                        return ScoreString(f, tmp);
        }
        //return "error";
 }
 
-float hud_fixscoreboardcolumnwidth_len;
-float hud_fixscoreboardcolumnwidth_iconlen;
-float hud_fixscoreboardcolumnwidth_marginlen;
+float sbt_fixcolumnwidth_len;
+float sbt_fixcolumnwidth_iconlen;
+float sbt_fixcolumnwidth_marginlen;
 
-string HUD_FixScoreboardColumnWidth(int i, string str)
+string Scoreboard_FixColumnWidth(int i, string str)
 {
     TC(int, i);
-       float field, f;
+       float f;
        vector sz;
-       field = hud_field[i];
 
-       hud_fixscoreboardcolumnwidth_iconlen = 0;
+       sbt_fixcolumnwidth_iconlen = 0;
 
-       if(hud_field_icon0 != "")
+       if(sbt_field_icon0 != "")
        {
-               sz = draw_getimagesize(hud_field_icon0);
+               sz = draw_getimagesize(sbt_field_icon0);
                f = sz.x / sz.y;
-               if(hud_fixscoreboardcolumnwidth_iconlen < f)
-                       hud_fixscoreboardcolumnwidth_iconlen = f;
+               if(sbt_fixcolumnwidth_iconlen < f)
+                       sbt_fixcolumnwidth_iconlen = f;
        }
 
-       if(hud_field_icon1 != "")
+       if(sbt_field_icon1 != "")
        {
-               sz = draw_getimagesize(hud_field_icon1);
+               sz = draw_getimagesize(sbt_field_icon1);
                f = sz.x / sz.y;
-               if(hud_fixscoreboardcolumnwidth_iconlen < f)
-                       hud_fixscoreboardcolumnwidth_iconlen = f;
+               if(sbt_fixcolumnwidth_iconlen < f)
+                       sbt_fixcolumnwidth_iconlen = f;
        }
 
-       if(hud_field_icon2 != "")
+       if(sbt_field_icon2 != "")
        {
-               sz = draw_getimagesize(hud_field_icon2);
+               sz = draw_getimagesize(sbt_field_icon2);
                f = sz.x / sz.y;
-               if(hud_fixscoreboardcolumnwidth_iconlen < f)
-                       hud_fixscoreboardcolumnwidth_iconlen = f;
+               if(sbt_fixcolumnwidth_iconlen < f)
+                       sbt_fixcolumnwidth_iconlen = f;
        }
 
-       hud_fixscoreboardcolumnwidth_iconlen *= hud_fontsize.y / hud_fontsize.x; // fix icon aspect
+       sbt_fixcolumnwidth_iconlen *= hud_fontsize.y / hud_fontsize.x; // fix icon aspect
 
-       if(hud_fixscoreboardcolumnwidth_iconlen != 0)
-               hud_fixscoreboardcolumnwidth_marginlen = stringwidth(" ", false, hud_fontsize);
+       if(sbt_fixcolumnwidth_iconlen != 0)
+               sbt_fixcolumnwidth_marginlen = stringwidth(" ", false, hud_fontsize);
        else
-               hud_fixscoreboardcolumnwidth_marginlen = 0;
+               sbt_fixcolumnwidth_marginlen = 0;
 
-       if(field == SP_NAME) // name gets all remaining space
+       if(sbt_field[i] == SP_NAME) // name gets all remaining space
        {
                int j;
-               float namesize;
-               namesize = sbwidth;// / hud_fontsize_x;
-               for(j = 0; j < hud_num_fields; ++j)
+               float remaining_space = 0;
+               for(j = 0; j < sbt_num_fields; ++j)
                        if(j != i)
-                               if (hud_field[i] != SP_SEPARATOR)
-                                       namesize -= hud_size[j] + hud_fontsize.x;
-               namesize += hud_fontsize.x;
-               hud_size[i] = namesize;
+                               if (sbt_field[i] != SP_SEPARATOR)
+                                       remaining_space += sbt_field_size[j] + hud_fontsize.x;
+               sbt_field_size[i] = panel_size.x - remaining_space;
 
-               if (hud_fixscoreboardcolumnwidth_iconlen != 0)
-                       namesize -= hud_fixscoreboardcolumnwidth_marginlen + hud_fixscoreboardcolumnwidth_iconlen;
+               if (sbt_fixcolumnwidth_iconlen != 0)
+                       remaining_space += sbt_fixcolumnwidth_marginlen + sbt_fixcolumnwidth_iconlen * hud_fontsize.x;
+               float namesize = panel_size.x - remaining_space;
                str = textShortenToWidth(str, namesize, hud_fontsize, stringwidth_colors);
-               hud_fixscoreboardcolumnwidth_len = stringwidth(str, true, hud_fontsize);
+               sbt_fixcolumnwidth_len = stringwidth(str, true, hud_fontsize);
+
+               max_namesize = vid_conwidth - remaining_space;
        }
        else
-               hud_fixscoreboardcolumnwidth_len = stringwidth(str, false, hud_fontsize);
+               sbt_fixcolumnwidth_len = stringwidth(str, false, hud_fontsize);
 
-       f = hud_fixscoreboardcolumnwidth_len + hud_fixscoreboardcolumnwidth_marginlen + hud_fixscoreboardcolumnwidth_iconlen;
-       if(hud_size[i] < f)
-               hud_size[i] = f;
+       f = sbt_fixcolumnwidth_len + sbt_fixcolumnwidth_marginlen + sbt_fixcolumnwidth_iconlen * hud_fontsize.x;
+       if(sbt_field_size[i] < f)
+               sbt_field_size[i] = f;
 
        return str;
 }
 
-void HUD_PrintScoreboardItem(vector pos, vector item_size, entity pl, bool is_self, int pl_number)
+void Scoreboard_initFieldSizes()
+{
+       for(int i = 0; i < sbt_num_fields; ++i)
+       {
+               sbt_field_size[i] = stringwidth(sbt_field_title[i], false, hud_fontsize);
+               Scoreboard_FixColumnWidth(i, "");
+       }
+}
+
+vector Scoreboard_DrawHeader(vector pos, vector rgb, bool other_players)
+{
+       int i;
+       vector column_dim = eY * panel_size.y;
+       if(other_players)
+               column_dim.y -= 1.25 * hud_fontsize.y;
+       vector text_offset = eY * (1.25 - 1) / 2 * hud_fontsize.y;
+       pos.x += hud_fontsize.x * 0.5;
+       for(i = 0; i < sbt_num_fields; ++i)
+       {
+               if(sbt_field[i] == SP_SEPARATOR)
+                       break;
+               column_dim.x = sbt_field_size[i] + hud_fontsize.x;
+               if (sbt_highlight)
+                       if (i % 2)
+                               drawfill(pos - eX * hud_fontsize.x * 0.5, column_dim, '0 0 0', sbt_highlight_alpha, DRAWFLAG_NORMAL);
+               drawstring(pos + text_offset, sbt_field_title[i], hud_fontsize, rgb * 1.5, sbt_fg_alpha, DRAWFLAG_NORMAL);
+               pos.x += column_dim.x;
+       }
+       if(sbt_field[i] == SP_SEPARATOR)
+       {
+               pos.x = panel_pos.x + panel_size.x - hud_fontsize.x * 0.5;
+               for(i = sbt_num_fields - 1; i > 0; --i)
+               {
+                       if(sbt_field[i] == SP_SEPARATOR)
+                               break;
+
+                       pos.x -= sbt_field_size[i];
+
+                       if (sbt_highlight)
+                               if (!(i % 2))
+                               {
+                                       column_dim.x = sbt_field_size[i] + hud_fontsize.x;
+                                       drawfill(pos - eX * hud_fontsize.x * 0.5, column_dim, '0 0 0', sbt_highlight_alpha, DRAWFLAG_NORMAL);
+                               }
+
+                       text_offset.x = sbt_field_size[i] - stringwidth(sbt_field_title[i], false, hud_fontsize);
+                       drawstring(pos + text_offset, sbt_field_title[i], hud_fontsize, rgb * 1.5, sbt_fg_alpha, DRAWFLAG_NORMAL);
+                       pos.x -= hud_fontsize.x;
+               }
+       }
+
+       pos.x = panel_pos.x;
+       pos.y += 1.25 * hud_fontsize.y;
+       return pos;
+}
+
+void Scoreboard_DrawItem(vector item_pos, vector rgb, entity pl, bool is_self, int pl_number)
 {
     TC(bool, is_self); TC(int, pl_number);
-       vector tmp, rgb;
-       rgb = Team_ColorRGB(pl.team);
        string str;
-       int field;
-       float is_spec;
-       is_spec = (entcs_GetTeam(pl.sv_entnum) == NUM_SPECTATOR);
-
-       if((rgb == '1 1 1') && (!is_spec)) {
-               rgb.x = autocvar_scoreboard_color_bg_r + 0.5;
-               rgb.y = autocvar_scoreboard_color_bg_g + 0.5;
-               rgb.z = autocvar_scoreboard_color_bg_b + 0.5; }
+       bool is_spec = (entcs_GetTeam(pl.sv_entnum) == NUM_SPECTATOR);
 
-       vector h_pos = pos - '1 1 0';
-       vector h_size = item_size + '2 0 0';
+       vector h_pos = item_pos;
+       vector h_size = eX * panel_size.x + eY * hud_fontsize.y * 1.25;
        // alternated rows highlighting
        if(is_self)
-               drawfill(h_pos, h_size, rgb, scoreboard_highlight_alpha_self, DRAWFLAG_NORMAL);
-       else if((scoreboard_highlight) && (!(pl_number % 2)))
-               drawfill(h_pos, h_size, rgb, scoreboard_highlight_alpha, DRAWFLAG_NORMAL);
+               drawfill(h_pos, h_size, rgb, sbt_highlight_alpha_self, DRAWFLAG_NORMAL);
+       else if((sbt_highlight) && (!(pl_number % 2)))
+               drawfill(h_pos, h_size, rgb, sbt_highlight_alpha, DRAWFLAG_NORMAL);
 
-       tmp.x = item_size.x;
-       tmp.y = 0;
-       tmp.z = 0;
+       float fg_alpha = (is_self ? sbt_fg_alpha_self : sbt_fg_alpha);
+
+       vector pos = item_pos;
+       pos.x += hud_fontsize.x * 0.5;
+       pos.y += (1.25 - 1) / 2 * hud_fontsize.y; // center text vertically
+       vector tmp = '0 0 0';
        int i;
-       for(i = 0; i < hud_num_fields; ++i)
+       PlayerScoreField field;
+       for(i = 0; i < sbt_num_fields; ++i)
        {
-               field = hud_field[i];
+               field = sbt_field[i];
                if(field == SP_SEPARATOR)
                        break;
 
                if(is_spec && field != SP_NAME && field != SP_PING) {
-                       pos.x += hud_size[i] + hud_fontsize.x;
+                       pos.x += sbt_field_size[i] + hud_fontsize.x;
                        continue;
                }
-               str = HUD_GetField(pl, field);
-               str = HUD_FixScoreboardColumnWidth(i, str);
+               str = Scoreboard_GetField(pl, field);
+               str = Scoreboard_FixColumnWidth(i, str);
 
-               pos.x += hud_size[i] + hud_fontsize.x;
+               pos.x += sbt_field_size[i] + hud_fontsize.x;
 
                if(field == SP_NAME) {
-                       tmp.x = hud_size[i] - hud_fontsize.x*hud_fixscoreboardcolumnwidth_iconlen - hud_fixscoreboardcolumnwidth_marginlen + hud_fontsize.x;
-                       if (is_self)
-                               drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                       else
-                               drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name, DRAWFLAG_NORMAL);
+                       tmp.x = sbt_field_size[i] - hud_fontsize.x * sbt_fixcolumnwidth_iconlen - sbt_fixcolumnwidth_marginlen + hud_fontsize.x;
+                       drawcolorcodedstring(pos - tmp, str, hud_fontsize, fg_alpha, DRAWFLAG_NORMAL);
                } else {
-                       tmp.x = hud_fixscoreboardcolumnwidth_len + hud_fontsize.x;
-                       if (is_self)
-                               drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                       else
-                               drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name, DRAWFLAG_NORMAL);
+                       tmp.x = sbt_fixcolumnwidth_len + hud_fontsize.x;
+                       drawstring(pos - tmp, str, hud_fontsize, sbt_field_rgb, fg_alpha, DRAWFLAG_NORMAL);
                }
 
-               tmp.x = hud_size[i] + hud_fontsize.x;
-               if(hud_field_icon0 != "")
-                       if (is_self)
-                               drawpic(pos - tmp, hud_field_icon0, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon1_rgb, hud_field_icon0_alpha * scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                       else
-                               drawpic(pos - tmp, hud_field_icon0, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon1_rgb, hud_field_icon0_alpha * scoreboard_alpha_name, DRAWFLAG_NORMAL);
-               if(hud_field_icon1 != "")
-                       if (is_self)
-                               drawpic(pos - tmp, hud_field_icon1, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon1_rgb, hud_field_icon1_alpha * scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                       else
-                               drawpic(pos - tmp, hud_field_icon1, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon1_rgb, hud_field_icon1_alpha * scoreboard_alpha_name, DRAWFLAG_NORMAL);
-               if(hud_field_icon2 != "")
-                       if (is_self)
-                               drawpic(pos - tmp, hud_field_icon2, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon2_rgb, hud_field_icon2_alpha * scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                       else
-                               drawpic(pos - tmp, hud_field_icon2, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon2_rgb, hud_field_icon2_alpha * scoreboard_alpha_name, DRAWFLAG_NORMAL);
+               tmp.x = sbt_field_size[i] + hud_fontsize.x;
+               if(sbt_field_icon0 != "")
+                       drawpic(pos - tmp, sbt_field_icon0, eY * hud_fontsize.y + eX * hud_fontsize.x * sbt_fixcolumnwidth_iconlen, sbt_field_icon1_rgb, fg_alpha, DRAWFLAG_NORMAL);
+               if(sbt_field_icon1 != "")
+                       drawpic(pos - tmp, sbt_field_icon1, eY * hud_fontsize.y + eX * hud_fontsize.x * sbt_fixcolumnwidth_iconlen, sbt_field_icon1_rgb, fg_alpha, DRAWFLAG_NORMAL);
+               if(sbt_field_icon2 != "")
+                       drawpic(pos - tmp, sbt_field_icon2, eY * hud_fontsize.y + eX * hud_fontsize.x * sbt_fixcolumnwidth_iconlen, sbt_field_icon2_rgb, fg_alpha, DRAWFLAG_NORMAL);
        }
 
-       if(hud_field[i] == SP_SEPARATOR)
+       if(sbt_field[i] == SP_SEPARATOR)
        {
-               pos.x = xmax;
-               for(i = hud_num_fields-1; i > 0; --i)
+               pos.x = item_pos.x + panel_size.x - hud_fontsize.x * 0.5;
+               for(i = sbt_num_fields-1; i > 0; --i)
                {
-                       field = hud_field[i];
+                       field = sbt_field[i];
                        if(field == SP_SEPARATOR)
                                break;
 
                        if(is_spec && field != SP_NAME && field != SP_PING) {
-                               pos.x -= hud_size[i] + hud_fontsize.x;
+                               pos.x -= sbt_field_size[i] + hud_fontsize.x;
                                continue;
                        }
 
-                       str = HUD_GetField(pl, field);
-                       str = HUD_FixScoreboardColumnWidth(i, str);
+                       str = Scoreboard_GetField(pl, field);
+                       str = Scoreboard_FixColumnWidth(i, str);
 
                        if(field == SP_NAME) {
-                               tmp.x = hud_fixscoreboardcolumnwidth_len; // left or right aligned? let's put it right...
-                               if(is_self)
-                                       drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                               else
-                                       drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name, DRAWFLAG_NORMAL);
+                               tmp.x = sbt_fixcolumnwidth_len; // left or right aligned? let's put it right...
+                               drawcolorcodedstring(pos - tmp, str, hud_fontsize, fg_alpha, DRAWFLAG_NORMAL);
                        } else {
-                               tmp.x = hud_fixscoreboardcolumnwidth_len;
-                               if(is_self)
-                                       drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                               else
-                                       drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name, DRAWFLAG_NORMAL);
+                               tmp.x = sbt_fixcolumnwidth_len;
+                               drawstring(pos - tmp, str, hud_fontsize, sbt_field_rgb, fg_alpha, DRAWFLAG_NORMAL);
                        }
 
-                       tmp.x = hud_size[i];
-                       if(hud_field_icon0 != "")
-                               if (is_self)
-                                       drawpic(pos - tmp, hud_field_icon0, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon1_rgb, hud_field_icon0_alpha * scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                               else
-                                       drawpic(pos - tmp, hud_field_icon0, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon1_rgb, hud_field_icon0_alpha * scoreboard_alpha_name, DRAWFLAG_NORMAL);
-                       if(hud_field_icon1 != "")
-                               if (is_self)
-                                       drawpic(pos - tmp, hud_field_icon1, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon1_rgb, hud_field_icon1_alpha * scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                               else
-                                       drawpic(pos - tmp, hud_field_icon1, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon1_rgb, hud_field_icon1_alpha * scoreboard_alpha_name, DRAWFLAG_NORMAL);
-                       if(hud_field_icon2 != "")
-                               if (is_self)
-                                       drawpic(pos - tmp, hud_field_icon2, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon2_rgb, hud_field_icon2_alpha * scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
-                               else
-                                       drawpic(pos - tmp, hud_field_icon2, '0 1 0' * hud_fontsize.y + '1 0 0' * hud_fontsize.x * hud_fixscoreboardcolumnwidth_iconlen, hud_field_icon2_rgb, hud_field_icon2_alpha * scoreboard_alpha_name, DRAWFLAG_NORMAL);
-                       pos.x -= hud_size[i] + hud_fontsize.x;
+                       tmp.x = sbt_field_size[i];
+                       if(sbt_field_icon0 != "")
+                               drawpic(pos - tmp, sbt_field_icon0, eY * hud_fontsize.y + eX * hud_fontsize.x * sbt_fixcolumnwidth_iconlen, sbt_field_icon1_rgb, fg_alpha, DRAWFLAG_NORMAL);
+                       if(sbt_field_icon1 != "")
+                               drawpic(pos - tmp, sbt_field_icon1, eY * hud_fontsize.y + eX * hud_fontsize.x * sbt_fixcolumnwidth_iconlen, sbt_field_icon1_rgb, fg_alpha, DRAWFLAG_NORMAL);
+                       if(sbt_field_icon2 != "")
+                               drawpic(pos - tmp, sbt_field_icon2, eY * hud_fontsize.y + eX * hud_fontsize.x * sbt_fixcolumnwidth_iconlen, sbt_field_icon2_rgb, fg_alpha, DRAWFLAG_NORMAL);
+                       pos.x -= sbt_field_size[i] + hud_fontsize.x;
                }
        }
 
        if(pl.eliminated)
-               drawfill(h_pos, h_size, '0 0 0', 0.5, DRAWFLAG_NORMAL);
+               drawfill(h_pos, h_size, '0 0 0', 0.5 * panel_fg_alpha, DRAWFLAG_NORMAL);
 }
 
-/*
- * HUD_Scoreboard_MakeTable
- *
- * Makes a table for a team (for all playing players in DM) and fills it
- */
+vector Scoreboard_DrawOthers(vector item_pos, vector rgb, int this_team, entity ignored_pl, entity pl, int pl_number)
+{
+       int i = 0;
+       vector h_pos = item_pos;
+       vector h_size = eX * panel_size.x + eY * hud_fontsize.y * 1.25;
+
+       bool complete = (this_team == NUM_SPECTATOR);
+
+       if(!complete)
+       if((sbt_highlight) && (!(pl_number % 2)))
+               drawfill(h_pos, h_size, rgb, sbt_highlight_alpha, DRAWFLAG_NORMAL);
+
+       vector pos = item_pos;
+       pos.x += hud_fontsize.x * 0.5;
+       pos.y += (1.25 - 1) / 2 * hud_fontsize.y; // center text vertically
+
+       float width_limit = item_pos.x + panel_size.x - hud_fontsize.x;
+       if(!complete)
+               width_limit -= stringwidth("...", false, hud_fontsize);
+       float namesize = autocvar_hud_panel_scoreboard_namesize * hud_fontsize.x;
+       float ping_padding = 0;
+       float min_pingsize = stringwidth("999", false, hud_fontsize);
+       for(i = 0; pl; pl = pl.sort_next)
+       {
+               if(pl.team != this_team)
+                       continue;
+               if(pl == ignored_pl)
+                       continue;
 
-vector HUD_Scoreboard_MakeTable(vector pos, entity tm, vector rgb, vector bg_size)
+               ping_padding = 0;
+               string str = textShortenToWidth(entcs_GetName(pl.sv_entnum), namesize, hud_fontsize, stringwidth_colors);
+               if(this_team == NUM_SPECTATOR)
+               {
+                       if(autocvar_hud_panel_scoreboard_spectators_showping)
+                       {
+                               string ping = Scoreboard_GetField(pl, SP_PING);
+                               float pingsize = stringwidth(ping, false, hud_fontsize);
+                               if(min_pingsize > pingsize)
+                                       ping_padding = min_pingsize - pingsize;
+                               string col = rgb_to_hexcolor(sbt_field_rgb);
+                               str = sprintf("%s ^7[%s%s^7]", str, col, ping);
+                       }
+               }
+               else if(autocvar_hud_panel_scoreboard_others_showscore)
+                       str = sprintf("%s ^7(^3%s^7)", str, ftos(pl.(scores(ps_primary))));
+               float str_width = stringwidth(str, true, hud_fontsize);
+               if(pos.x + str_width > width_limit)
+               {
+                       ++i;
+                       if(!complete)
+                       {
+                               drawstring(pos, "...", hud_fontsize, '1 1 1', sbt_fg_alpha, DRAWFLAG_NORMAL);
+                               break;
+                       }
+                       else
+                       {
+                               pos.x = item_pos.x + hud_fontsize.x * 0.5;
+                               pos.y = item_pos.y + i * (hud_fontsize.y * 1.25);
+                       }
+               }
+               drawcolorcodedstring(pos, str, hud_fontsize, sbt_fg_alpha, DRAWFLAG_NORMAL);
+               pos.x += str_width + hud_fontsize.x * 0.5;
+               pos.x += ping_padding;
+       }
+       return eX * item_pos.x + eY * (item_pos.y + i * hud_fontsize.y * 1.25);
+}
+
+vector Scoreboard_MakeTable(vector pos, entity tm, vector rgb, vector bg_size)
 {
-       float body_table_height;
-       vector tmp = '0 0 0', column_dim = '0 0 0';
+       int max_players = 999;
+       if(autocvar_hud_panel_scoreboard_maxheight > 0)
+       {
+               max_players = autocvar_hud_panel_scoreboard_maxheight * vid_conheight;
+               if(teamplay)
+                       max_players = (max_players - hud_fontsize.y * 1.25 - panel_bg_padding * 2) / 2;
+               max_players = floor(max_players / (hud_fontsize.y * 1.25));
+               if(max_players <= 1)
+                       max_players = 1;
+               if(max_players == tm.team_size)
+                       max_players = 999;
+       }
+
        entity pl;
+       entity me = playerslots[current_player];
+       panel_pos = pos;
+       panel_size.y = 1.25 * hud_fontsize.y * (1 + bound(1, tm.team_size, max_players));
+       panel_size.y += panel_bg_padding * 2;
+       HUD_Panel_DrawBg();
 
-       body_table_height = 1.25 * hud_fontsize.y * max(1, tm.team_size); // no player? show 1 empty line
+       vector end_pos = panel_pos + eY * (panel_size.y + hud_fontsize.y);
+       if(panel.current_panel_bg != "0")
+               end_pos.y += panel_bg_border * 2;
 
-       pos.y += autocvar_scoreboard_border_thickness;
-       pos -= '1 1 0';
+       if(panel_bg_padding)
+       {
+               panel_pos += '1 1 0' * panel_bg_padding;
+               panel_size -= '2 2 0' * panel_bg_padding;
+       }
 
-       tmp.x = sbwidth + 2;
-       tmp.y = 1.25 * hud_fontsize.y;
+       pos = panel_pos;
+       vector tmp = eX * panel_size.x + eY * 1.25 * hud_fontsize.y;
 
        // rounded header
-       if (teamplay)
-               drawpic(pos, "gfx/scoreboard/scoreboard_tableheader", tmp, (rgb * autocvar_scoreboard_color_bg_team) + '0.5 0.5 0.5', scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-       else
-               drawpic(pos, "gfx/scoreboard/scoreboard_tableheader", tmp, rgb + '0.5 0.5 0.5', scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-
-       // table border
-       tmp.y += autocvar_scoreboard_border_thickness;
-       tmp.y += body_table_height;
-       drawborderlines(autocvar_scoreboard_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg, DRAWFLAG_NORMAL); // more transparency for the scoreboard
+       if (sbt_bg_alpha)
+               drawpic(pos, "gfx/scoreboard/scoreboard_tableheader", tmp, rgb + '0.5 0.5 0.5', sbt_bg_alpha, DRAWFLAG_NORMAL);
 
-       // separator header/table
        pos.y += 1.25 * hud_fontsize.y;
-       tmp.y = autocvar_scoreboard_border_thickness;
-       drawfill(pos, tmp, '0 0 0', scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-
-       pos.y += autocvar_scoreboard_border_thickness;
 
        // table background
-       tmp.y = body_table_height;
-       if (teamplay)
-               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * autocvar_scoreboard_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-       else
-               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-
-       // anyway, apply some color
-       //drawfill(pos, tmp + '2 0 0', rgb, 0.1, DRAWFLAG_NORMAL);
+       tmp.y = panel_size.y - 1.25 * hud_fontsize.y;
+       if (sbt_bg_alpha)
+               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, sbt_bg_alpha, DRAWFLAG_NORMAL);
 
-       // go back to the top to make alternated columns highlighting and to print the strings
-       pos.y -= 1.25 * hud_fontsize.y;
-       pos.y -= autocvar_scoreboard_border_thickness;
 
-       pos += '1 1 0';
+       // print header row and highlight columns
+       pos = Scoreboard_DrawHeader(panel_pos, rgb, (max_players < tm.team_size));
 
-       if (scoreboard_highlight)
-       {
-               column_dim.y = 1.25 * hud_fontsize.y; // header
-               column_dim.y += autocvar_scoreboard_border_thickness;
-               column_dim.y += body_table_height;
-       }
-
-       // print the strings of the columns headers and draw the columns
-       int i;
-       for(i = 0; i < hud_num_fields; ++i)
-       {
-               if(hud_field[i] == SP_SEPARATOR)
-                       break;
-               column_dim.x = hud_size[i] + hud_fontsize.x;
-               if (scoreboard_highlight)
-               {
-                       if (i % 2)
-                               drawfill(pos - '0 1 0' - hud_fontsize.x / 2 * '1 0 0', column_dim, '0 0 0', scoreboard_alpha_bg * 0.2, DRAWFLAG_NORMAL);
-               }
-               drawstring(pos, hud_title[i], hud_fontsize, rgb * 1.5, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
-               pos.x += column_dim.x;
-       }
-       if(hud_field[i] == SP_SEPARATOR)
+       // fill the table and draw the rows
+       bool is_self = false;
+       bool self_shown = false;
+       int i = 0;
+       for(pl = players.sort_next; pl; pl = pl.sort_next)
        {
-               pos.x = xmax;
-               tmp.y = 0;
-               for(i = hud_num_fields-1; i > 0; --i)
+               if(pl.team != tm.team)
+                       continue;
+               if(i == max_players - 2 && pl != me)
                {
-                       if(hud_field[i] == SP_SEPARATOR)
-                               break;
-
-                       pos.x -= hud_size[i];
-
-                       if (scoreboard_highlight)
+                       if(!self_shown && me.team == tm.team)
                        {
-                               if (!(i % 2))
-                               {
-                                       if (i == hud_num_fields-1)
-                                               column_dim.x = hud_size[i] + hud_fontsize.x / 2 + 1;
-                                       else
-                                               column_dim.x = hud_size[i] + hud_fontsize.x;
-                                       drawfill(pos - '0 1 0' - hud_fontsize.x / 2 * '1 0 0', column_dim, '0 0 0', scoreboard_alpha_bg * 0.2, DRAWFLAG_NORMAL);
-                               }
+                               Scoreboard_DrawItem(pos, rgb, me, true, i);
+                               self_shown = true;
+                               pos.y += 1.25 * hud_fontsize.y;
+                               ++i;
                        }
-
-                       tmp.x = stringwidth(hud_title[i], false, hud_fontsize);
-                       tmp.x = (hud_size[i] - tmp.x);
-                       drawstring(pos + tmp, hud_title[i], hud_fontsize, rgb * 1.5, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
-                       pos.x -= hud_fontsize.x;
                }
-       }
-
-       pos.x = xmin;
-       pos.y += 1.25 * hud_fontsize.y; // skip the header
-       pos.y += autocvar_scoreboard_border_thickness;
-
-       // item size
-       tmp.x = sbwidth;
-       tmp.y = hud_fontsize.y * 1.25;
-
-       // fill the table and draw the rows
-       i = 0;
-       if (teamplay)
-               for(pl = players.sort_next; pl; pl = pl.sort_next)
+               if(i >= max_players - 1)
                {
-                       if(pl.team != tm.team)
-                               continue;
-                       HUD_PrintScoreboardItem(pos, tmp, pl, (pl.sv_entnum == player_localnum), i);
-                       pos.y += 1.25 * hud_fontsize.y;
-                       ++i;
-               }
-       else
-               for(pl = players.sort_next; pl; pl = pl.sort_next)
-               {
-                       if(pl.team == NUM_SPECTATOR)
-                               continue;
-                       HUD_PrintScoreboardItem(pos, tmp, pl, (pl.sv_entnum == player_localnum), i);
-                       pos.y += 1.25 * hud_fontsize.y;
-                       ++i;
+                       pos = Scoreboard_DrawOthers(pos, rgb, tm.team, (self_shown ? me : NULL), pl, i);
+                       break;
                }
+               is_self = (pl.sv_entnum == current_player);
+               Scoreboard_DrawItem(pos, rgb, pl, is_self, i);
+               if(is_self)
+                       self_shown = true;
+               pos.y += 1.25 * hud_fontsize.y;
+               ++i;
+       }
 
-       if (i == 0)
-               pos.y += 1.25 * hud_fontsize.y; // move to the end of the table
-       pos.y += 1.25 * hud_fontsize.y; // move empty row (out of the table)
-
-       return pos;
+       panel_size.x += panel_bg_padding * 2; // restore initial width
+       return end_pos;
 }
 
-float HUD_WouldDrawScoreboard() {
+bool Scoreboard_WouldDraw()
+{
        if (QuickMenu_IsOpened())
-               return 0;
+               return false;
        else if (HUD_Radar_Clickable())
-               return 0;
+               return false;
        else if (scoreboard_showscores)
-               return 1;
+               return true;
        else if (intermission == 1)
-               return 1;
+               return true;
        else if (intermission == 2)
-               return 0;
+               return false;
        else if (spectatee_status != -1 && STAT(HEALTH) <= 0 && autocvar_cl_deathscoreboard && gametype != MAPINFO_TYPE_CTS && !active_minigame)
-               return 1;
+               return true;
        else if (scoreboard_showscores_force)
-               return 1;
-       return 0;
+               return true;
+       return false;
 }
 
 float average_accuracy;
-vector HUD_DrawScoreboardAccuracyStats(vector pos, vector rgb, vector bg_size)
+vector Scoreboard_AccuracyStats_Draw(vector pos, vector rgb, vector bg_size)
 {
        WepSet weapons_stat = WepSet_GetFromStat();
        WepSet weapons_inmap = WepSet_GetFromStat_InMap();
-       float initial_posx = pos.x;
        int disownedcnt = 0;
+       int nHidden = 0;
        FOREACH(Weapons, it != WEP_Null, {
                int weapon_stats = weapon_accuracy[i - WEP_FIRST];
 
                WepSet set = it.m_wepset;
-               if (weapon_stats < 0 && !(weapons_stat & set || weapons_inmap & set))
-                       ++disownedcnt;
+               if (weapon_stats < 0)
+               {
+                       if (!(weapons_stat & set) && (it.spawnflags & WEP_FLAG_HIDDEN || it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
+                               nHidden += 1;
+                       else if (!(weapons_stat & set || weapons_inmap & set))
+                               ++disownedcnt;
+               }
        });
 
-       int weapon_cnt = (Weapons_COUNT - 1) - disownedcnt;
+       int weapon_cnt = (Weapons_COUNT - 1) - disownedcnt - nHidden;
        if (weapon_cnt <= 0) return pos;
 
        int rows = 1;
-       if (autocvar_scoreboard_accuracy_doublerows && weapon_cnt >= floor((Weapons_COUNT - 1) * 0.5))
+       if (autocvar_hud_panel_scoreboard_accuracy_doublerows && weapon_cnt >= floor((Weapons_COUNT - nHidden - 1) * 0.5))
                rows = 2;
        int columnns = ceil(weapon_cnt / rows);
 
-       float height = 40;
-       float fontsize = height * 1/3;
-       float weapon_height = height * 2/3;
-       float weapon_width = sbwidth / columnns / rows;
+       float weapon_height = 29;
+       float height = hud_fontsize.y + weapon_height;
 
-       drawstring(pos, sprintf(_("Accuracy stats (average %d%%)"), average_accuracy), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
-       pos.y += 1.25 * hud_fontsize.y + autocvar_scoreboard_border_thickness;
-       vector tmp = '0 0 0';
-       tmp.x = sbwidth;
-       tmp.y = height * rows;
+       drawstring(pos + eX * panel_bg_padding, sprintf(_("Accuracy stats (average %d%%)"), average_accuracy), hud_fontsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
+       pos.y += 1.25 * hud_fontsize.y;
+       if(panel.current_panel_bg != "0")
+               pos.y += panel_bg_border;
 
-       if (teamplay)
-               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * autocvar_scoreboard_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-       else
-               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-       drawborderlines(autocvar_scoreboard_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg * 0.75, DRAWFLAG_NORMAL);
+       panel_pos = pos;
+       panel_size.y = height * rows;
+       panel_size.y += panel_bg_padding * 2;
+       HUD_Panel_DrawBg();
 
-       // column highlighting
-       for (int i = 0; i < columnns; ++i)
+       vector end_pos = panel_pos + eY * (panel_size.y + hud_fontsize.y);
+       if(panel.current_panel_bg != "0")
+               end_pos.y += panel_bg_border * 2;
+
+       if(panel_bg_padding)
        {
-               if ((i % 2) == 0)
-                       drawfill(pos + '1 0 0' * weapon_width * rows * i, '0 1 0' * height * rows + '1 0 0' * weapon_width * rows, '0 0 0', scoreboard_alpha_bg * 0.2, DRAWFLAG_NORMAL);
+               panel_pos += '1 1 0' * panel_bg_padding;
+               panel_size -= '2 2 0' * panel_bg_padding;
        }
 
-       // row highlighting
-       for (int i = 0; i < rows; ++i)
+       pos = panel_pos;
+       vector tmp = panel_size;
+
+       float weapon_width = tmp.x / columnns / rows;
+
+       if (sbt_bg_alpha)
+               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, sbt_bg_alpha, DRAWFLAG_NORMAL);
+
+       if(sbt_highlight)
        {
-               drawfill(pos + '0 1 0' * weapon_height + '0 1 0' * height * i, '1 0 0' * sbwidth + '0 1 0' * fontsize, '1 1 1', scoreboard_highlight_alpha, DRAWFLAG_NORMAL);
+               // column highlighting
+               for (int i = 0; i < columnns; ++i)
+                       if ((i % 2) == 0)
+                               drawfill(pos + eX * weapon_width * rows * i, eY * height * rows + eX * weapon_width * rows, '0 0 0', sbt_highlight_alpha, DRAWFLAG_NORMAL);
+
+               // row highlighting
+               for (int i = 0; i < rows; ++i)
+                       drawfill(pos + eY * weapon_height + eY * height * i, eX * tmp.x + eY * hud_fontsize.y, rgb, sbt_highlight_alpha, DRAWFLAG_NORMAL);
        }
 
        average_accuracy = 0;
@@ -1062,7 +1150,7 @@ vector HUD_DrawScoreboardAccuracyStats(vector pos, vector rgb, vector bg_size)
        if (rows == 2)
                pos.x += weapon_width / 2;
 
-       if (autocvar_scoreboard_accuracy_nocolors)
+       if (autocvar_hud_panel_scoreboard_accuracy_nocolors)
                rgb = '1 1 1';
        else
                Accuracy_LoadColors();
@@ -1080,27 +1168,27 @@ vector HUD_DrawScoreboardAccuracyStats(vector pos, vector rgb, vector bg_size)
 
                float weapon_alpha;
                if (weapon_stats >= 0)
-                       weapon_alpha = scoreboard_alpha_fg;
+                       weapon_alpha = sbt_fg_alpha;
                else
-                       weapon_alpha = 0.2 * scoreboard_alpha_fg;
+                       weapon_alpha = 0.2 * sbt_fg_alpha;
 
                // weapon icon
-               drawpic_aspect_skin(tmpos, it.model2, '1 0 0' * weapon_width + '0 1 0' * weapon_height, '1 1 1', weapon_alpha, DRAWFLAG_NORMAL);
+               drawpic_aspect_skin(tmpos, it.model2, eX * weapon_width + eY * weapon_height, '1 1 1', weapon_alpha, DRAWFLAG_NORMAL);
                // the accuracy
                if (weapon_stats >= 0) {
                        weapons_with_stats += 1;
                        average_accuracy += weapon_stats; // store sum of all accuracies in average_accuracy
 
                        string s;
-                       s = sprintf("%d%%", weapon_stats*100);
+                       s = sprintf("%d%%", weapon_stats * 100);
 
                        float padding;
-                       padding = (weapon_width - stringwidth(s, false, '1 0 0' * fontsize)) / 2; // center the accuracy value
+                       padding = (weapon_width - stringwidth(s, false, hud_fontsize)) / 2; // center the accuracy value
 
-                       if(!autocvar_scoreboard_accuracy_nocolors)
+                       if(!autocvar_hud_panel_scoreboard_accuracy_nocolors)
                                rgb = Accuracy_GetColor(weapon_stats);
 
-                       drawstring(tmpos + '1 0 0' * padding + '0 1 0' * weapon_height, s, '1 1 0' * fontsize, rgb, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+                       drawstring(tmpos + eX * padding + eY * weapon_height, s, hud_fontsize, rgb, sbt_fg_alpha, DRAWFLAG_NORMAL);
                }
                tmpos.x += weapon_width * rows;
                pos.x += weapon_width * rows;
@@ -1115,25 +1203,23 @@ vector HUD_DrawScoreboardAccuracyStats(vector pos, vector rgb, vector bg_size)
        if (weapons_with_stats)
                average_accuracy = floor((average_accuracy * 100 / weapons_with_stats) + 0.5);
 
-       pos.y += height;
-       pos.y += 1.25 * hud_fontsize.y;
-       pos.x = initial_posx;
-       return pos;
+       panel_size.x += panel_bg_padding * 2; // restore initial width
+       return end_pos;
 }
 
-vector HUD_DrawKeyValue(vector pos, string key, string value) {
+vector MapStats_DrawKeyValue(vector pos, string key, string value) {
        float px = pos.x;
        pos.x += hud_fontsize.x * 0.25;
-       drawstring(pos, key, hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
-       pos.x = xmax - stringwidth(value, false, hud_fontsize) - hud_fontsize.x * 0.25;
-       drawstring(pos, value, hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+       drawstring(pos, key, hud_fontsize, '1 1 1', sbt_fg_alpha, DRAWFLAG_NORMAL);
+       pos.x = panel_pos.x + panel_size.x - stringwidth(value, false, hud_fontsize) - hud_fontsize.x * 0.25;
+       drawstring(pos, value, hud_fontsize, '1 1 1', sbt_fg_alpha, DRAWFLAG_NORMAL);
        pos.x = px;
-       pos.y+= hud_fontsize.y;
+       pos.y += hud_fontsize.y;
 
        return pos;
 }
 
-vector HUD_DrawMapStats(vector pos, vector rgb, vector bg_size) {
+vector Scoreboard_MapStats_Draw(vector pos, vector rgb, vector bg_size) {
        float stat_secrets_found, stat_secrets_total;
        float stat_monsters_killed, stat_monsters_total;
        float rows = 0;
@@ -1158,41 +1244,52 @@ vector HUD_DrawMapStats(vector pos, vector rgb, vector bg_size) {
                return pos;
 
        //  draw table header
-       drawstring(pos, _("Map stats:"), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
-       pos.y += 1.25 * hud_fontsize.y + autocvar_scoreboard_border_thickness;
+       drawstring(pos + eX * panel_bg_padding, _("Map stats:"), hud_fontsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
+       pos.y += 1.25 * hud_fontsize.y;
+       if(panel.current_panel_bg != "0")
+               pos.y += panel_bg_border;
 
-       // draw table
-       vector tmp = '0 0 0';
-       tmp.x = sbwidth;
-       tmp.y = hud_fontsize.y * rows;
+       panel_pos = pos;
+       panel_size.y = hud_fontsize.y * rows;
+       panel_size.y += panel_bg_padding * 2;
+       HUD_Panel_DrawBg();
 
-       if (teamplay)
-               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * autocvar_scoreboard_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-       else
-               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-       drawborderlines(autocvar_scoreboard_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg * 0.75, DRAWFLAG_NORMAL);
+       vector end_pos = panel_pos + eY * (panel_size.y + hud_fontsize.y);
+       if(panel.current_panel_bg != "0")
+               end_pos.y += panel_bg_border * 2;
+
+       if(panel_bg_padding)
+       {
+               panel_pos += '1 1 0' * panel_bg_padding;
+               panel_size -= '2 2 0' * panel_bg_padding;
+       }
+
+       pos = panel_pos;
+       vector tmp = panel_size;
+
+       if (sbt_bg_alpha)
+               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, sbt_bg_alpha, DRAWFLAG_NORMAL);
 
        // draw monsters
        if(stat_monsters_total)
        {
                val = sprintf("%d/%d", stat_monsters_killed, stat_monsters_total);
-               pos = HUD_DrawKeyValue(pos, _("Monsters killed:"), val);
+               pos = MapStats_DrawKeyValue(pos, _("Monsters killed:"), val);
        }
 
        // draw secrets
        if(stat_secrets_total)
        {
                val = sprintf("%d/%d", stat_secrets_found, stat_secrets_total);
-               pos = HUD_DrawKeyValue(pos, _("Secrets found:"), val);
+               pos = MapStats_DrawKeyValue(pos, _("Secrets found:"), val);
        }
 
-       // update position
-       pos.y += 1.25 * hud_fontsize.y;
-       return pos;
+       panel_size.x += panel_bg_padding * 2; // restore initial width
+       return end_pos;
 }
 
 
-vector HUD_DrawScoreboardRankings(vector pos, entity pl,  vector rgb, vector bg_size)
+vector Scoreboard_Rankings_Draw(vector pos, entity pl, vector rgb, vector bg_size)
 {
        int i;
        RANKINGS_RECEIVED_CNT = 0;
@@ -1203,76 +1300,127 @@ vector HUD_DrawScoreboardRankings(vector pos, entity pl,  vector rgb, vector bg_
        if (RANKINGS_RECEIVED_CNT == 0)
                return pos;
 
-       float is_spec;
-       is_spec = (entcs_GetTeam(pl.sv_entnum) == NUM_SPECTATOR);
-       vector hl_rgb;
-       hl_rgb.x = autocvar_scoreboard_color_bg_r + 0.5;
-       hl_rgb.y = autocvar_scoreboard_color_bg_g + 0.5;
-       hl_rgb.z = autocvar_scoreboard_color_bg_b + 0.5;
+       vector hl_rgb = rgb + '0.5 0.5 0.5';
 
        pos.y += hud_fontsize.y;
-       drawstring(pos, _("Rankings"), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
-       pos.y += hud_fontsize.y + autocvar_scoreboard_border_thickness;
-       vector tmp = '0 0 0';
-       tmp.x = sbwidth;
-       tmp.y = 1.25 * hud_fontsize.y * RANKINGS_RECEIVED_CNT;
+       drawstring(pos + eX * panel_bg_padding, _("Rankings"), hud_fontsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
+       pos.y += 1.25 * hud_fontsize.y;
+       if(panel.current_panel_bg != "0")
+               pos.y += panel_bg_border;
 
-       if (teamplay)
-               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * autocvar_scoreboard_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-       else
-               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
-       drawborderlines(autocvar_scoreboard_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg * 0.75, DRAWFLAG_NORMAL);
+       panel_pos = pos;
 
-       // row highlighting
-       for(i = 0; i<RANKINGS_RECEIVED_CNT; ++i)
+       float namesize = 0;
+       for(i = 0; i < RANKINGS_RECEIVED_CNT; ++i)
+       {
+               float f = stringwidth(grecordholder[i], true, hud_fontsize);
+               if(f > namesize)
+                       namesize = f;
+       }
+       bool cut = false;
+       if(namesize > autocvar_hud_panel_scoreboard_namesize * hud_fontsize.x)
+       {
+               namesize = autocvar_hud_panel_scoreboard_namesize * hud_fontsize.x;
+               cut = true;
+       }
+
+       float ranksize = 3 * hud_fontsize.x;
+       float timesize = 5 * hud_fontsize.x;
+       vector columnsize = eX * (ranksize + timesize + namesize + hud_fontsize.x) + eY * 1.25 * hud_fontsize.y;
+       int columns = max(1, floor((panel_size.x - 2 * panel_bg_padding) / columnsize.x));
+       columns = min(columns, RANKINGS_RECEIVED_CNT);
+
+       // expand name column to fill the entire row
+       float available_space = (panel_size.x - 2 * panel_bg_padding - columnsize.x * columns) / columns;
+       namesize += available_space;
+       columnsize.x += available_space;
+
+       panel_size.y = ceil(RANKINGS_RECEIVED_CNT / columns) * 1.25 * hud_fontsize.y;
+       panel_size.y += panel_bg_padding * 2;
+
+       HUD_Panel_DrawBg();
+
+       vector end_pos = panel_pos + eY * (panel_size.y + hud_fontsize.y);
+       if(panel.current_panel_bg != "0")
+               end_pos.y += panel_bg_border * 2;
+
+       if(panel_bg_padding)
+       {
+               panel_pos += '1 1 0' * panel_bg_padding;
+               panel_size -= '2 2 0' * panel_bg_padding;
+       }
+
+       pos = panel_pos;
+
+       if (sbt_bg_alpha)
+               drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, panel_size, rgb, sbt_bg_alpha, DRAWFLAG_NORMAL);
+
+       vector text_ofs = eX * 0.5 * hud_fontsize.x + eY * (1.25 - 1) / 2 * hud_fontsize.y; // center text vertically
+       string str = "";
+       int column = 0, j = 0;
+       for(i = 0; i < RANKINGS_RECEIVED_CNT; ++i)
        {
-               string n, p;
                float t;
                t = grecordtime[i];
                if (t == 0)
                        continue;
-               n = grecordholder[i];
-               p = count_ordinal(i+1);
+
                if(grecordholder[i] == entcs_GetName(player_localnum))
-                       drawfill(pos, '1 0 0' * sbwidth + '0 1.25 0' * hud_fontsize.y, hl_rgb, scoreboard_highlight_alpha_self, DRAWFLAG_NORMAL);
-               else if(!(i % 2) && scoreboard_highlight)
-                       drawfill(pos, '1 0 0' * sbwidth + '0 1.25 0' * hud_fontsize.y, hl_rgb, scoreboard_highlight_alpha, DRAWFLAG_NORMAL);
-               drawstring(pos, p, '1 1 0' * hud_fontsize.y, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
-               drawstring(pos + '3 0 0' * hud_fontsize.y, TIME_ENCODED_TOSTRING(t), '1 1 0' * hud_fontsize.y, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
-               drawcolorcodedstring(pos + '8 0 0' * hud_fontsize.y, n, '1 1 0' * hud_fontsize.y, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+                       drawfill(pos, columnsize, hl_rgb, sbt_highlight_alpha_self, DRAWFLAG_NORMAL);
+               else if(!((j + column) & 1) && sbt_highlight)
+                       drawfill(pos, columnsize, hl_rgb, sbt_highlight_alpha, DRAWFLAG_NORMAL);
+
+               str = count_ordinal(i+1);
+               drawstring(pos + text_ofs, str, hud_fontsize, '1 1 1', sbt_fg_alpha, DRAWFLAG_NORMAL);
+               drawstring(pos + text_ofs + eX * ranksize, TIME_ENCODED_TOSTRING(t), hud_fontsize, '1 1 1', sbt_fg_alpha, DRAWFLAG_NORMAL);
+               str = grecordholder[i];
+               if(cut)
+                       str = textShortenToWidth(str, namesize, hud_fontsize, stringwidth_colors);
+               drawcolorcodedstring(pos + text_ofs + eX * (ranksize + timesize), str, hud_fontsize, sbt_fg_alpha, DRAWFLAG_NORMAL);
+
                pos.y += 1.25 * hud_fontsize.y;
+               j++;
+               if(j >= ceil(RANKINGS_RECEIVED_CNT / columns))
+               {
+                       column++;
+                       j = 0;
+                       pos.x += panel_size.x / columns;
+                       pos.y = panel_pos.y;
+               }
        }
-       pos.y += autocvar_scoreboard_border_thickness;
 
-       return pos;
+       panel_size.x += panel_bg_padding * 2; // restore initial width
+       return end_pos;
 }
 
-float hud_woulddrawscoreboard_prev;
-float hud_woulddrawscoreboard_change; // "time" at which HUD_WouldDrawScoreboard() changed
-void HUD_DrawScoreboard()
+void Scoreboard_Draw()
 {
        if(!autocvar__hud_configure)
        {
-               float hud_woulddrawscoreboard;
-               hud_woulddrawscoreboard = scoreboard_active;
-               if(hud_woulddrawscoreboard != hud_woulddrawscoreboard_prev) {
-                       hud_woulddrawscoreboard_change = time;
-                       hud_woulddrawscoreboard_prev = hud_woulddrawscoreboard;
-               }
-       
-               if(hud_woulddrawscoreboard) {
-                       if(menu_enabled == 1)
+               if(!hud_draw_maximized) return;
+
+               // frametime checks allow to toggle the scoreboard even when the game is paused
+               if(scoreboard_active) {
+                       if(hud_configure_menu_open == 1)
                                scoreboard_fade_alpha = 1;
-                       float scoreboard_fadeinspeed = autocvar_scoreboard_fadeinspeed;
-                       if (scoreboard_fadeinspeed)
-                               scoreboard_fade_alpha = bound (0, (time - hud_woulddrawscoreboard_change) * scoreboard_fadeinspeed, 1);
+                       float scoreboard_fadeinspeed = autocvar_hud_panel_scoreboard_fadeinspeed;
+                       if (scoreboard_fadeinspeed && frametime)
+                               scoreboard_fade_alpha = min(1, scoreboard_fade_alpha + frametime * scoreboard_fadeinspeed);
                        else
                                scoreboard_fade_alpha = 1;
+                       if(hud_fontsize_str != autocvar_hud_fontsize)
+                       {
+                               hud_fontsize = HUD_GetFontsize("hud_fontsize");
+                               Scoreboard_initFieldSizes();
+                               if(hud_fontsize_str)
+                                       strunzone(hud_fontsize_str);
+                               hud_fontsize_str = strzone(autocvar_hud_fontsize);
+                       }
                }
                else {
-                       float scoreboard_fadeoutspeed = autocvar_scoreboard_fadeoutspeed;
-                       if (scoreboard_fadeoutspeed)
-                               scoreboard_fade_alpha = bound (0, (1/scoreboard_fadeoutspeed - (time - hud_woulddrawscoreboard_change)) * scoreboard_fadeoutspeed, 1);
+                       float scoreboard_fadeoutspeed = autocvar_hud_panel_scoreboard_fadeoutspeed;
+                       if (scoreboard_fadeoutspeed && frametime)
+                               scoreboard_fade_alpha = max(0, scoreboard_fade_alpha - frametime * scoreboard_fadeoutspeed);
                        else
                                scoreboard_fade_alpha = 0;
                }
@@ -1283,164 +1431,133 @@ void HUD_DrawScoreboard()
        else
                scoreboard_fade_alpha = 0;
 
-       if (autocvar_scoreboard_dynamichud)
+       if (autocvar_hud_panel_scoreboard_dynamichud)
                HUD_Scale_Enable();
        else
                HUD_Scale_Disable();
 
-       float hud_fade_alpha_save = hud_fade_alpha;
-       if(menu_enabled == 1)
-               hud_fade_alpha = 1;
-       else
-               hud_fade_alpha = scoreboard_fade_alpha * (1 - autocvar__menu_alpha);
-       HUD_Panel_UpdateCvars();
-
-       scoreboard_alpha_bg = autocvar_scoreboard_alpha_bg * panel_fg_alpha;
-       scoreboard_alpha_fg = autocvar_scoreboard_alpha_fg * panel_fg_alpha;
-       scoreboard_highlight = autocvar_scoreboard_highlight;
-       scoreboard_highlight_alpha = autocvar_scoreboard_highlight_alpha * panel_fg_alpha;
-       scoreboard_highlight_alpha_self = autocvar_scoreboard_highlight_alpha_self * panel_fg_alpha;
-       scoreboard_alpha_name = autocvar_scoreboard_alpha_name * panel_fg_alpha;
-       scoreboard_alpha_name_self = autocvar_scoreboard_alpha_name_self * panel_fg_alpha;
+       if(scoreboard_fade_alpha <= 0)
+               return;
+       panel_fade_alpha *= scoreboard_fade_alpha;
+       HUD_Panel_LoadCvars();
 
-       hud_fade_alpha = hud_fade_alpha_save;
+       sbt_bg_alpha = autocvar_hud_panel_scoreboard_table_bg_alpha * panel_fg_alpha;
+       sbt_highlight = autocvar_hud_panel_scoreboard_table_highlight;
+       sbt_highlight_alpha = autocvar_hud_panel_scoreboard_table_highlight_alpha * panel_fg_alpha;
+       sbt_highlight_alpha_self = autocvar_hud_panel_scoreboard_table_highlight_alpha_self * panel_fg_alpha;
+       sbt_fg_alpha = autocvar_hud_panel_scoreboard_table_fg_alpha * panel_fg_alpha;
+       sbt_fg_alpha_self = autocvar_hud_panel_scoreboard_table_fg_alpha_self * panel_fg_alpha;
 
        // don't overlap with con_notify
        if(!autocvar__hud_configure)
-               panel_pos_y = max((autocvar_con_notify * autocvar_con_notifysize), panel_pos_y);
-
-       HUD_Panel_DrawBg(scoreboard_fade_alpha);
+               panel_pos.y = max((autocvar_con_notify * autocvar_con_notifysize), panel_pos.y);
 
-       if(panel_bg_padding)
-       {
-               panel_pos += '1 1 0' * panel_bg_padding;
-               panel_size -= '2 2 0' * panel_bg_padding;
-       }
+       float excess = max(0, max_namesize - autocvar_hud_panel_scoreboard_namesize * hud_fontsize.x);
+       float fixed_scoreboard_width = bound(vid_conwidth * autocvar_hud_panel_scoreboard_minwidth, vid_conwidth - excess, vid_conwidth * 0.93);
+       panel_pos.x = 0.5 * (vid_conwidth - fixed_scoreboard_width);
+       panel_size.x = fixed_scoreboard_width;
 
-       HUD_UpdatePlayerTeams();
+       Scoreboard_UpdatePlayerTeams();
 
-       vector rgb, pos, tmp;
+       vector pos = panel_pos;
        entity pl, tm;
        string str;
 
-       xmin = panel_pos_x;
-       ymin = panel_pos_y;
-
-       xmax = panel_pos_x + panel_size_x;
-       ymax = panel_pos_y + panel_size_y;
-
-       sbwidth = panel_size_x;
-
-       // Initializes position
-       pos.x = xmin;
-       pos.y = ymin;
-       pos.z = 0;
-
        // Heading
        vector sb_heading_fontsize;
        sb_heading_fontsize = hud_fontsize * 2;
        draw_beginBoldFont();
-       drawstring(pos, _("Scoreboard"), sb_heading_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+       drawstring(pos + eX * panel_bg_padding, _("Scoreboard"), sb_heading_fontsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
        draw_endBoldFont();
 
-       pos.y += sb_heading_fontsize.y + hud_fontsize.y * 0.25;
+       pos.y += sb_heading_fontsize.y;
+       if(panel.current_panel_bg != "0")
+               pos.y += panel_bg_border;
 
        // Draw the scoreboard
-       vector bg_size = draw_getimagesize("gfx/scoreboard/scoreboard_bg") * ((autocvar_scoreboard_bg_scale > 0) ? autocvar_scoreboard_bg_scale : 0.25);
+       float scale = autocvar_hud_panel_scoreboard_table_bg_scale;
+       if(scale <= 0)
+               scale = 0.25;
+       vector bg_size = draw_getimagesize("gfx/scoreboard/scoreboard_bg") * scale;
 
        if(teamplay)
        {
-               vector team_score_baseoffset;
-               team_score_baseoffset = eY * (2 * autocvar_scoreboard_border_thickness + hud_fontsize.y) - eX * (autocvar_scoreboard_border_thickness + hud_fontsize.x * 0.25);
+               vector panel_bg_color_save = panel_bg_color;
+               vector team_score_baseoffset = eY * hud_fontsize.y - eX * hud_fontsize.x * 0.5;
+               if(panel.current_panel_bg != "0")
+                       team_score_baseoffset.x -= panel_bg_border;
                for(tm = teams.sort_next; tm; tm = tm.sort_next)
                {
                        if(tm.team == NUM_SPECTATOR)
                                continue;
-                       if(!tm.team && teamplay)
+                       if(!tm.team)
                                continue;
 
                        draw_beginBoldFont();
-                       rgb = Team_ColorRGB(tm.team);
-                       str = ftos(tm.(teamscores[ts_primary]));
-                       drawstring(pos + team_score_baseoffset - eX * stringwidth(str, false, hud_fontsize * 1.5), str, hud_fontsize * 1.5, rgb, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+                       vector rgb = Team_ColorRGB(tm.team);
+                       str = ftos(tm.(teamscores(ts_primary)));
+                       drawstring(pos + team_score_baseoffset - eX * stringwidth(str, false, hud_fontsize * 1.5), str, hud_fontsize * 1.5, rgb, panel_fg_alpha, DRAWFLAG_NORMAL);
 
                        if(ts_primary != ts_secondary)
                        {
-                               str = ftos(tm.(teamscores[ts_secondary]));
-                               drawstring(pos + team_score_baseoffset - eX * stringwidth(str, false, hud_fontsize) + eY * hud_fontsize.y * 1.5, str, hud_fontsize, rgb, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+                               str = ftos(tm.(teamscores(ts_secondary)));
+                               drawstring(pos + team_score_baseoffset - eX * stringwidth(str, false, hud_fontsize) + eY * hud_fontsize.y * 1.5, str, hud_fontsize, rgb, panel_fg_alpha, DRAWFLAG_NORMAL);
                        }
                        draw_endBoldFont();
-
-                       pos = HUD_Scoreboard_MakeTable(pos, tm, rgb, bg_size);
+                       if(autocvar_hud_panel_scoreboard_bg_teams_color_team > 0)
+                               panel_bg_color = rgb * autocvar_hud_panel_scoreboard_bg_teams_color_team;
+                       else if(panel_bg_color_team > 0)
+                               panel_bg_color = rgb * panel_bg_color_team;
+                       else
+                               panel_bg_color = rgb;
+                       pos = Scoreboard_MakeTable(pos, tm, panel_bg_color, bg_size);
                }
-               rgb.x = autocvar_scoreboard_color_bg_r;
-               rgb.y = autocvar_scoreboard_color_bg_g;
-               rgb.z = autocvar_scoreboard_color_bg_b;
+               panel_bg_color = panel_bg_color_save;
        }
        else
        {
-               rgb.x = autocvar_scoreboard_color_bg_r;
-               rgb.y = autocvar_scoreboard_color_bg_g;
-               rgb.z = autocvar_scoreboard_color_bg_b;
-
                for(tm = teams.sort_next; tm; tm = tm.sort_next)
-               {
-                       if(tm.team == NUM_SPECTATOR)
-                               continue;
-                       if(!tm.team && teamplay)
-                               continue;
-
-                       pos = HUD_Scoreboard_MakeTable(pos, tm, rgb, bg_size);
-               }
+                       if(tm.team != NUM_SPECTATOR)
+                               break;
+               // display it anyway
+               pos = Scoreboard_MakeTable(pos, tm, panel_bg_color, bg_size);
        }
 
        if(gametype == MAPINFO_TYPE_CTS || gametype == MAPINFO_TYPE_RACE) {
                if(race_speedaward) {
-                       drawcolorcodedstring(pos, sprintf(_("Speed award: %d%s ^7(%s^7)"), race_speedaward, race_speedaward_unit, race_speedaward_holder), hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+                       drawcolorcodedstring(pos, sprintf(_("Speed award: %d%s ^7(%s^7)"), race_speedaward, race_speedaward_unit, race_speedaward_holder), hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
                        pos.y += 1.25 * hud_fontsize.y;
                }
                if(race_speedaward_alltimebest) {
-                       drawcolorcodedstring(pos, sprintf(_("All-time fastest: %d%s ^7(%s^7)"), race_speedaward_alltimebest, race_speedaward_alltimebest_unit, race_speedaward_alltimebest_holder), hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+                       drawcolorcodedstring(pos, sprintf(_("All-time fastest: %d%s ^7(%s^7)"), race_speedaward_alltimebest, race_speedaward_alltimebest_unit, race_speedaward_alltimebest_holder), hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
                        pos.y += 1.25 * hud_fontsize.y;
                }
-               pos = HUD_DrawScoreboardRankings(pos, playerslots[player_localnum], rgb, bg_size);
-       }
-       else if (autocvar_scoreboard_accuracy && !warmup_stage && gametype != MAPINFO_TYPE_NEXBALL) {
-               if(teamplay)
-                       pos = HUD_DrawScoreboardAccuracyStats(pos, Team_ColorRGB(myteam), bg_size);
-               else
-                       pos = HUD_DrawScoreboardAccuracyStats(pos, rgb, bg_size);
+               pos = Scoreboard_Rankings_Draw(pos, playerslots[player_localnum], panel_bg_color, bg_size);
        }
+       else if (autocvar_hud_panel_scoreboard_accuracy && !warmup_stage && gametype != MAPINFO_TYPE_NEXBALL)
+               pos = Scoreboard_AccuracyStats_Draw(pos, panel_bg_color, bg_size);
 
-
-       if(teamplay)
-               pos = HUD_DrawMapStats(pos, Team_ColorRGB(myteam), bg_size);
-       else
-               pos = HUD_DrawMapStats(pos, rgb, bg_size);
+       pos = Scoreboard_MapStats_Draw(pos, panel_bg_color, bg_size);
 
        // List spectators
-       float specs;
-       specs = 0;
-       tmp = pos;
-       vector item_size;
-       item_size.x = sbwidth;
-       item_size.y = hud_fontsize.y * 1.25;
-       item_size.z = 0;
        for(pl = players.sort_next; pl; pl = pl.sort_next)
        {
-               if(pl.team != NUM_SPECTATOR)
-                       continue;
-               pos.y += 1.25 * hud_fontsize.y;
-               HUD_PrintScoreboardItem(pos, item_size, pl, (pl.sv_entnum == player_localnum), specs);
-               ++specs;
-       }
+               if(pl.team == NUM_SPECTATOR)
+               {
+                       for(tm = teams.sort_next; tm; tm = tm.sort_next)
+                               if(tm.team == NUM_SPECTATOR)
+                                       break;
+                       str = sprintf("%s (%d)", _("Spectators"), tm.team_size);
+                       draw_beginBoldFont();
+                       drawstring(pos, str, hud_fontsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
+                       draw_endBoldFont();
+                       pos.y += 1.25 * hud_fontsize.y;
 
-       if(specs)
-       {
-               draw_beginBoldFont();
-               drawstring(tmp, _("Spectators"), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
-               draw_endBoldFont();
-               pos.y += 1.25 * hud_fontsize.y;
+                       pos = Scoreboard_DrawOthers(pos, '0 0 0', pl.team, NULL, pl, 0);
+                       pos.y += 1.25 * hud_fontsize.y;
+
+                       break;
+               }
        }
 
        // Print info string
@@ -1464,17 +1581,17 @@ void HUD_DrawScoreboard()
                                str = strcat(str, _(" or"));
                        if(teamplay)
                        {
-                               str = strcat(str, sprintf(_(" until ^3%s %s^7"), ScoreString(teamscores_flags[ts_primary], fl),
-                                       (teamscores_label[ts_primary] == "score")   ? CTX(_("SCO^points")) :
-                                       (teamscores_label[ts_primary] == "fastest") ? CTX(_("SCO^is beaten")) :
-                                       TranslateScoresLabel(teamscores_label[ts_primary])));
+                               str = strcat(str, sprintf(_(" until ^3%s %s^7"), ScoreString(teamscores_flags(ts_primary), fl),
+                                       (teamscores_label(ts_primary) == "score")   ? CTX(_("SCO^points")) :
+                                       (teamscores_label(ts_primary) == "fastest") ? CTX(_("SCO^is beaten")) :
+                                       TranslateScoresLabel(teamscores_label(ts_primary))));
                        }
                        else
                        {
-                               str = strcat(str, sprintf(_(" until ^3%s %s^7"), ScoreString(scores_flags[ps_primary], fl),
-                                       (scores_label[ps_primary] == "score")   ? CTX(_("SCO^points")) :
-                                       (scores_label[ps_primary] == "fastest") ? CTX(_("SCO^is beaten")) :
-                                       TranslateScoresLabel(scores_label[ps_primary])));
+                               str = strcat(str, sprintf(_(" until ^3%s %s^7"), ScoreString(scores_flags(ps_primary), fl),
+                                       (scores_label(ps_primary) == "score")   ? CTX(_("SCO^points")) :
+                                       (scores_label(ps_primary) == "fastest") ? CTX(_("SCO^is beaten")) :
+                                       TranslateScoresLabel(scores_label(ps_primary))));
                        }
                }
                if(ll > 0)
@@ -1483,23 +1600,23 @@ void HUD_DrawScoreboard()
                                str = strcat(str, _(" or"));
                        if(teamplay)
                        {
-                               str = strcat(str, sprintf(_(" until a lead of ^3%s %s^7"), ScoreString(teamscores_flags[ts_primary], ll),
-                                       (teamscores_label[ts_primary] == "score")   ? CTX(_("SCO^points")) :
-                                       (teamscores_label[ts_primary] == "fastest") ? CTX(_("SCO^is beaten")) :
-                                       TranslateScoresLabel(teamscores_label[ts_primary])));
+                               str = strcat(str, sprintf(_(" until a lead of ^3%s %s^7"), ScoreString(teamscores_flags(ts_primary), ll),
+                                       (teamscores_label(ts_primary) == "score")   ? CTX(_("SCO^points")) :
+                                       (teamscores_label(ts_primary) == "fastest") ? CTX(_("SCO^is beaten")) :
+                                       TranslateScoresLabel(teamscores_label(ts_primary))));
                        }
                        else
                        {
-                               str = strcat(str, sprintf(_(" until a lead of ^3%s %s^7"), ScoreString(scores_flags[ps_primary], ll),
-                                       (scores_label[ps_primary] == "score")   ? CTX(_("SCO^points")) :
-                                       (scores_label[ps_primary] == "fastest") ? CTX(_("SCO^is beaten")) :
-                                       TranslateScoresLabel(scores_label[ps_primary])));
+                               str = strcat(str, sprintf(_(" until a lead of ^3%s %s^7"), ScoreString(scores_flags(ps_primary), ll),
+                                       (scores_label(ps_primary) == "score")   ? CTX(_("SCO^points")) :
+                                       (scores_label(ps_primary) == "fastest") ? CTX(_("SCO^is beaten")) :
+                                       TranslateScoresLabel(scores_label(ps_primary))));
                        }
                }
        }
 
        pos.y += 1.2 * hud_fontsize.y;
-       drawcolorcodedstring(pos + '0.5 0 0' * (sbwidth - stringwidth(str, true, hud_fontsize)), str, hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+       drawcolorcodedstring(pos + '0.5 0 0' * (panel_size.x - stringwidth(str, true, hud_fontsize)), str, hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
 
        // print information about respawn status
        float respawn_time = STAT(RESPAWN_TIME);
@@ -1510,23 +1627,25 @@ void HUD_DrawScoreboard()
                {
                        // a negative number means we are awaiting respawn, time value is still the same
                        respawn_time *= -1; // remove mark now that we checked it
-                       respawn_time = max(time, respawn_time); // don't show a negative value while the server is respawning the player (lag)
 
-                       str = sprintf(_("^1Respawning in ^3%s^1..."),
-                               (autocvar_scoreboard_respawntime_decimals ?
-                                       count_seconds_decs(respawn_time - time, autocvar_scoreboard_respawntime_decimals)
-                                       :
-                                       count_seconds(respawn_time - time)
-                               )
-                       );
+                       if(respawn_time < time) // it happens for a few frames when server is respawning the player
+                               str = ""; // draw an empty string to not change suddenly scoreboard_bottom
+                       else
+                               str = sprintf(_("^1Respawning in ^3%s^1..."),
+                                       (autocvar_hud_panel_scoreboard_respawntime_decimals ?
+                                               count_seconds_decs(respawn_time - time, autocvar_hud_panel_scoreboard_respawntime_decimals)
+                                               :
+                                               count_seconds(ceil(respawn_time - time))
+                                       )
+                               );
                }
                else if(time < respawn_time)
                {
                        str = sprintf(_("You are dead, wait ^3%s^7 before respawning"),
-                               (autocvar_scoreboard_respawntime_decimals ?
-                                       count_seconds_decs(respawn_time - time, autocvar_scoreboard_respawntime_decimals)
+                               (autocvar_hud_panel_scoreboard_respawntime_decimals ?
+                                       count_seconds_decs(respawn_time - time, autocvar_hud_panel_scoreboard_respawntime_decimals)
                                        :
-                                       count_seconds(respawn_time - time)
+                                       count_seconds(ceil(respawn_time - time))
                                )
                        );
                }
@@ -1534,7 +1653,7 @@ void HUD_DrawScoreboard()
                        str = sprintf(_("You are dead, press ^2%s^7 to respawn"), getcommandkey("jump", "+jump"));
 
                pos.y += 1.2 * hud_fontsize.y;
-               drawcolorcodedstring(pos + '0.5 0 0' * (sbwidth - stringwidth(str, true, hud_fontsize)), str, hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
+               drawcolorcodedstring(pos + '0.5 0 0' * (panel_size.x - stringwidth(str, true, hud_fontsize)), str, hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
        }
 
        scoreboard_bottom = pos.y + 2 * hud_fontsize.y;