]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/scoreboard.qc
Merge branch 'master' into fruitiex/newpanelhud_stable
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / scoreboard.qc
1 float scoreboard_alpha_bg;
2 float scoreboard_alpha_fg;
3 float scoreboard_highlight;
4 float scoreboard_highlight_alpha;
5 float scoreboard_highlight_alpha_self;
6 float scoreboard_alpha_name;
7 float scoreboard_alpha_name_self;
8
9 void drawstringright(vector, string, vector, vector, float, float);
10 void drawstringcenter(vector, string, vector, vector, float, float);
11
12 float SCOREBOARD_OFFSET = 50;
13
14 void MapVote_Draw();
15 void HUD_FinaleOverlay()
16 {
17         /*vector pos;
18         pos_x = (vid_conwidth - 1)/2;
19         pos_y = 16;
20         pos_z = 0;*/
21
22         //drawpic(pos, "gfx/finale", '0 0 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
23
24         //drawstring(pos, "END", hud_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
25         MapVote_Draw();
26 }
27
28 void Cmd_HUD_SetFields(float argc);
29 void HUD_InitScores()
30 {
31         float i, f;
32
33         ps_primary = ps_secondary = ts_primary = ts_secondary = -1;
34         for(i = 0; i < MAX_SCORE; ++i)
35         {
36                 f = (scores_flags[i] & SFL_SORT_PRIO_MASK);
37                 if(f == SFL_SORT_PRIO_PRIMARY)
38                         ps_primary = i;
39                 if(f == SFL_SORT_PRIO_SECONDARY)
40                         ps_secondary = i;
41         }
42         if(ps_secondary == -1)
43                 ps_secondary = ps_primary;
44
45         for(i = 0; i < MAX_TEAMSCORE; ++i)
46         {
47                 f = (teamscores_flags[i] & SFL_SORT_PRIO_MASK);
48                 if(f == SFL_SORT_PRIO_PRIMARY)
49                         ts_primary = i;
50                 if(f == SFL_SORT_PRIO_SECONDARY)
51                         ts_secondary = i;
52         }
53         if(ts_secondary == -1)
54                 ts_secondary = ts_primary;
55
56         Cmd_HUD_SetFields(0);
57 }
58
59 void HUD_UpdatePlayerPos(entity pl);
60 float SetTeam(entity pl, float Team);
61 //float lastpnum;
62 void HUD_UpdatePlayerTeams()
63 {
64         float Team;
65         entity pl, tmp;
66         float num;
67
68         num = 0;
69         for(pl = players.sort_next; pl; pl = pl.sort_next)
70         {
71                 num += 1;
72                 Team = GetPlayerColor(pl.sv_entnum);
73                 if(SetTeam(pl, Team))
74                 {
75                         tmp = pl.sort_prev;
76                         HUD_UpdatePlayerPos(pl);
77                         if(tmp)
78                                 pl = tmp;
79                         else
80                                 pl = players.sort_next;
81                 }
82         }
83         /*
84         if(num != lastpnum)
85                 print(strcat("PNUM: ", ftos(num), "\n"));
86         lastpnum = num;
87         */
88 }
89
90 float HUD_ComparePlayerScores(entity left, entity right)
91 {
92         float vl, vr;
93         vl = GetPlayerColor(left.sv_entnum);
94         vr = GetPlayerColor(right.sv_entnum);
95
96         if(!left.gotscores)
97                 vl = COLOR_SPECTATOR;
98         if(!right.gotscores)
99                 vr = COLOR_SPECTATOR;
100
101         if(vl > vr)
102                 return true;
103         if(vl < vr)
104                 return false;
105
106         if(vl == COLOR_SPECTATOR)
107         {
108                 // FIRST the one with scores (spectators), THEN the ones without (downloaders)
109                 // no other sorting
110                 if(!left.gotscores && right.gotscores)
111                         return true;
112                 return false;
113         }
114
115         vl = left.scores[ps_primary];
116         vr = right.scores[ps_primary];
117         if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
118         {
119                 if(vl == 0 && vr != 0)
120                         return 1;
121                 if(vl != 0 && vr == 0)
122                         return 0;
123         }
124         if(vl > vr)
125                 return IS_INCREASING(scores_flags[ps_primary]);
126         if(vl < vr)
127                 return IS_DECREASING(scores_flags[ps_primary]);
128
129         vl = left.scores[ps_secondary];
130         vr = right.scores[ps_secondary];
131         if(scores_flags[ps_secondary] & SFL_ZERO_IS_WORST)
132         {
133                 if(vl == 0 && vr != 0)
134                         return 1;
135                 if(vl != 0 && vr == 0)
136                         return 0;
137         }
138         if(vl > vr)
139                 return IS_INCREASING(scores_flags[ps_secondary]);
140         if(vl < vr)
141                 return IS_DECREASING(scores_flags[ps_secondary]);
142
143         return false;
144 }
145
146 void HUD_UpdatePlayerPos(entity player)
147 {
148         for(other = player.sort_next; other && HUD_ComparePlayerScores(player, other); other = player.sort_next)
149         {
150                 SORT_SWAP(player, other);
151         }
152         for(other = player.sort_prev; other != players && HUD_ComparePlayerScores(other, player); other = player.sort_prev)
153         {
154                 SORT_SWAP(other, player);
155         }
156 }
157
158 float HUD_CompareTeamScores(entity left, entity right)
159 {
160         float vl, vr;
161
162         if(left.team == COLOR_SPECTATOR)
163                 return 1;
164         if(right.team == COLOR_SPECTATOR)
165                 return 0;
166
167         vl = left.teamscores[ts_primary];
168         vr = right.teamscores[ts_primary];
169         if(vl > vr)
170                 return IS_INCREASING(teamscores_flags[ts_primary]);
171         if(vl < vr)
172                 return IS_DECREASING(teamscores_flags[ts_primary]);
173
174         vl = left.teamscores[ts_secondary];
175         vr = right.teamscores[ts_secondary];
176         if(vl > vr)
177                 return IS_INCREASING(teamscores_flags[ts_secondary]);
178         if(vl < vr)
179                 return IS_DECREASING(teamscores_flags[ts_secondary]);
180
181         return false;
182 }
183
184 void HUD_UpdateTeamPos(entity Team)
185 {
186         for(other = Team.sort_next; other && HUD_CompareTeamScores(Team, other); other = Team.sort_next)
187         {
188                 SORT_SWAP(Team, other);
189         }
190         for(other = Team.sort_prev; other != teams && HUD_CompareTeamScores(other, Team); other = Team.sort_prev)
191         {
192                 SORT_SWAP(other, Team);
193         }
194 }
195
196 void Cmd_HUD_Help(float argc)
197 {
198         print("You can modify the scoreboard using the ^2hud_columns_set command.\n");
199         print("^3|---------------------------------------------------------------|\n");
200         print("Usage:\n");
201         print("^2hud_columns_set default\n");
202         print("^2hud_columns_set ^7filed1 field2 ...\n");
203         print("The following field names are recognized (case insensitive):\n");
204         print("You can use a ^3|^7 to start the right-aligned fields.\n\n");
205
206         print("^3name^7 or ^3nick^7         Name of a player\n");
207         print("^3ping^7                     Ping time\n");
208         print("^3pl^7                       Packet loss\n");
209         print("^3kills^7                    Number of kills\n");
210         print("^3deaths^7                   Number of deaths\n");
211         print("^3suicides^7                 Number of suicides\n");
212         print("^3frags^7                    kills - suicides\n");
213         print("^3kd^7                       The kill-death ratio\n");
214         print("^3caps^7                     How often a flag (CTF) or a key (KeyHunt) was captured\n");
215         print("^3pickups^7                  How often a flag (CTF) or a key (KeyHunt) was picked up\n");
216         print("^3fckills^7                  Number of flag carrier kills\n");
217         print("^3returns^7                  Number of flag returns\n");
218         print("^3drops^7                    Number of flag drops\n");
219         print("^3lives^7                    Number of lives (LMS)\n");
220         print("^3rank^7                     Player rank\n");
221         print("^3pushes^7                   Number of players pushed into void\n");
222         print("^3destroyed^7                Number of keys destroyed by pushing them into void\n");
223         print("^3kckills^7                  Number of keys carrier kills\n");
224         print("^3losses^7                   Number of times a key was lost\n");
225         print("^3laps^7                     Number of laps finished (race/cts)\n");
226         print("^3time^7                     Total time raced (race/cts)\n");
227         print("^3fastest^7                  Time of fastest lap (race/cts)\n");
228         print("^3ticks^7                    Number of ticks (DOM)\n");
229         print("^3takes^7                    Number of domination points taken (DOM)\n");
230         print("^3score^7                    Total score\n\n");
231
232         print("Before a field you can put a + or - sign, then a comma separated list\n");
233         print("of game types, then a slash, to make the field show up only in these\n");
234         print("or in all but these game types. You can also specify 'all' as a\n");
235         print("field to show all fields available for the current game mode.\n\n");
236
237         print("The special game type names 'teams' and 'noteams' can be used to\n");
238         print("include/exclude ALL teams/noteams game modes.\n\n");
239
240         print("Example: hud_columns_set name ping pl | +ctf/field3 -dm/field4\n");
241         print("will display name, ping and pl aligned to the left, and the fields\n");
242         print("right of the vertical bar aligned to the right.\n");
243         print("'field3' will only be shown in CTF, and 'field4' will be shown in all\n");
244         print("other gamemodes except DM.\n");
245 }
246
247 string HUD_DefaultColumnLayout()
248 {
249         return strcat( // fteqcc sucks
250                 "ping pl name | ",
251                 "-teams,race,lms/kills -teams,lms/deaths -teams,lms,race/suicides -race,dm,tdm/frags ", // tdm already has this in "score"
252                 "+ctf/caps +ctf/pickups +ctf/fckills +ctf/returns ",
253                 "+lms/lives +lms/rank ",
254                 "+kh/caps +kh/pushes +kh/destroyed ",
255                 "?+race/laps ?+race/time ?+race/fastest ",
256                 "+as/objectives +nexball/faults +nexball/goals ",
257                 "-lms,race,nexball/score");
258 }
259
260 void Cmd_HUD_SetFields(float argc)
261 {
262         float i, j, slash;
263         string str, pattern;
264         float have_name, have_primary, have_secondary, have_separator;
265         float missing;
266
267         // TODO: re enable with gametype dependant cvars?
268         if(argc < 2) // no arguments provided
269                 argc = tokenizebyseparator(strcat("x ", cvar_string("hud_columns")), " ");
270
271         if(argc < 2)
272                 argc = tokenizebyseparator(strcat("x ", HUD_DefaultColumnLayout()), " ");
273
274         if(argc == 2)
275         {
276                 if(argv(1) == "default")
277                         argc = tokenizebyseparator(strcat("x ", HUD_DefaultColumnLayout()), " ");
278                 else if(argv(1) == "all")
279                 {
280                         string s;
281                         s = "ping pl color name |";
282                         for(i = 0; i < MAX_SCORE; ++i)
283                         {
284                                 if(i != ps_primary)
285                                 if(i != ps_secondary)
286                                 if(scores_label[i] != "")
287                                         s = strcat(s, " ", scores_label[i]);
288                         }
289                         if(ps_secondary != ps_primary)
290                                 s = strcat(s, " ", scores_label[ps_secondary]);
291                         s = strcat(s, " ", scores_label[ps_primary]);
292                         argc = tokenizebyseparator(strcat("x ", s), " ");
293                 }
294         }
295
296
297         hud_num_fields = 0;
298
299         drawfont = hud_font;
300
301         for(i = 0; i < argc - 1; ++i)
302         {
303                 float nocomplain;
304                 str = argv(i+1);
305
306                 nocomplain = FALSE;
307                 if(substring(str, 0, 1) == "?")
308                 {
309                         nocomplain = TRUE;
310                         str = substring(str, 1, strlen(str) - 1);
311                 }
312
313                 slash = strstrofs(str, "/", 0);
314                 if(slash >= 0)
315                 {
316                         pattern = substring(str, 0, slash);
317                         str = substring(str, slash + 1, strlen(str) - (slash + 1));
318
319                         if not(isGametypeInFilter(gametype, teamplay, pattern))
320                                 continue;
321                 }
322
323                 strunzone(hud_title[hud_num_fields]);
324                 hud_title[hud_num_fields] = strzone(str);
325                 hud_size[hud_num_fields] = stringwidth(str, FALSE, hud_fontsize);
326                 str = strtolower(str);
327
328                 if(str == "ping") {
329                         hud_field[hud_num_fields] = SP_PING;
330                 } else if(str == "pl") {
331                         hud_field[hud_num_fields] = SP_PL;
332                 } else if(str == "kd" || str == "kdr" || str == "kdratio" || str == "k/d") {
333                         hud_field[hud_num_fields] = SP_KDRATIO;
334                 } else if(str == "name" || str == "nick") {
335                         hud_field[hud_num_fields] = SP_NAME;
336                         have_name = 1;
337                 } else if(str == "|") {
338                         hud_field[hud_num_fields] = SP_SEPARATOR;
339                         have_separator = 1;
340                 } else {
341                         for(j = 0; j < MAX_SCORE; ++j)
342                                 if(str == strtolower(scores_label[j]))
343                                         goto found; // sorry, but otherwise fteqcc -O3 miscompiles this and warns about "unreachable code"
344 :notfound
345                         if(str == "frags")
346                         {
347                                 j = SP_FRAGS;
348                         }
349                         else
350                         {
351                                 if not(nocomplain)
352                                         print(strcat("^1Error:^7 Unknown score field: '", str, "'\n"));
353                                 continue;
354                         }
355 :found
356                         hud_field[hud_num_fields] = j;
357                         if(j == ps_primary)
358                                 have_primary = 1;
359                         if(j == ps_secondary)
360                                 have_secondary = 1;
361                 }
362                 ++hud_num_fields;
363                 if(hud_num_fields >= MAX_HUD_FIELDS)
364                         break;
365         }
366
367         if(scores_flags[ps_primary] & SFL_ALLOW_HIDE)
368                 have_primary = 1;
369         if(scores_flags[ps_secondary] & SFL_ALLOW_HIDE)
370                 have_secondary = 1;
371         if(ps_primary == ps_secondary)
372                 have_secondary = 1;
373         missing = (!have_primary) + (!have_secondary) + (!have_separator) + (!have_name);
374
375         if(hud_num_fields+missing < MAX_HUD_FIELDS)
376         {
377                 if(!have_name)
378                 {
379                         strunzone(hud_title[hud_num_fields]);
380                         for(i = hud_num_fields; i > 0; --i)
381                         {
382                                 hud_title[i] = hud_title[i-1];
383                                 hud_size[i] = hud_size[i-1];
384                                 hud_field[i] = hud_field[i-1];
385                         }
386                         hud_title[0] = strzone("name");
387                         hud_field[0] = SP_NAME;
388                         ++hud_num_fields;
389                         print("fixed missing field 'name'\n");
390
391                         if(!have_separator)
392                         {
393                                 strunzone(hud_title[hud_num_fields]);
394                                 for(i = hud_num_fields; i > 1; --i)
395                                 {
396                                         hud_title[i] = hud_title[i-1];
397                                         hud_size[i] = hud_size[i-1];
398                                         hud_field[i] = hud_field[i-1];
399                                 }
400                                 hud_title[1] = strzone("|");
401                                 hud_field[1] = SP_SEPARATOR;
402                                 hud_size[1] = stringwidth("|", FALSE, hud_fontsize);
403                                 ++hud_num_fields;
404                                 print("fixed missing field '|'\n");
405                         }
406                 }
407                 else if(!have_separator)
408                 {
409                         strunzone(hud_title[hud_num_fields]);
410                         hud_title[hud_num_fields] = strzone("|");
411                         hud_size[hud_num_fields] = stringwidth("|", FALSE, hud_fontsize);
412                         hud_field[hud_num_fields] = SP_SEPARATOR;
413                         ++hud_num_fields;
414                         print("fixed missing field '|'\n");
415                 }
416                 if(!have_secondary)
417                 {
418                         strunzone(hud_title[hud_num_fields]);
419                         hud_title[hud_num_fields] = strzone(scores_label[ps_secondary]);
420                         hud_size[hud_num_fields] = stringwidth(hud_title[hud_num_fields], FALSE, hud_fontsize);
421                         hud_field[hud_num_fields] = ps_secondary;
422                         ++hud_num_fields;
423                         print("fixed missing field '", scores_label[ps_secondary], "'\n");
424                 }
425                 if(!have_primary)
426                 {
427                         strunzone(hud_title[hud_num_fields]);
428                         hud_title[hud_num_fields] = strzone(scores_label[ps_primary]);
429                         hud_size[hud_num_fields] = stringwidth(hud_title[hud_num_fields], FALSE, hud_fontsize);
430                         hud_field[hud_num_fields] = ps_primary;
431                         ++hud_num_fields;
432                         print("fixed missing field '", scores_label[ps_primary], "'\n");
433                 }
434         }
435
436         hud_field[hud_num_fields] = SP_END;
437 }
438
439 // MOVEUP::
440 vector hud_field_rgb;
441 string hud_field_icon0;
442 string hud_field_icon1;
443 string hud_field_icon2;
444 vector hud_field_icon0_rgb;
445 vector hud_field_icon1_rgb;
446 vector hud_field_icon2_rgb;
447 float hud_field_icon0_alpha;
448 float hud_field_icon1_alpha;
449 float hud_field_icon2_alpha;
450 string HUD_GetField(entity pl, float field)
451 {
452         float tmp, num, denom, f;
453         string str;
454         hud_field_rgb = '1 1 1';
455         hud_field_icon0 = "";
456         hud_field_icon1 = "";
457         hud_field_icon2 = "";
458         hud_field_icon0_rgb = '1 1 1';
459         hud_field_icon1_rgb = '1 1 1';
460         hud_field_icon2_rgb = '1 1 1';
461         hud_field_icon0_alpha = 1;
462         hud_field_icon1_alpha = 1;
463         hud_field_icon2_alpha = 1;
464         switch(field)
465         {
466                 case SP_PING:
467                         if not(pl.gotscores)
468                                 return "\x8D\x8D\x8D"; // >>> sign
469                         //str = getplayerkey(pl.sv_entnum, "ping");
470                         f = pl.ping;
471                         if(f == 0)
472                                 return "N/A";
473                         tmp = max(0, min(220, f-80)) / 220;
474                         hud_field_rgb = '1 1 1' - '0 1 1'*tmp;
475                         return ftos(f);
476
477                 case SP_PL:
478                         if not(pl.gotscores)
479                                 return "N/A";
480                         f = pl.ping_packetloss;
481                         tmp = pl.ping_movementloss;
482                         if(f == 0 && tmp == 0)
483                                 return "";
484                         str = ftos(ceil(f * 100));
485                         if(tmp != 0)
486                                 str = strcat(str, "~", ftos(ceil(tmp * 100)));
487                         tmp = bound(0, f / 0.2 + tmp / 0.04, 1); // 20% is REALLY BAD pl
488                         hud_field_rgb = '1 0.5 0.5' - '0 0.5 0.5'*tmp;
489                         return str;
490
491                 case SP_NAME:
492                         if(ready_waiting && pl.ready)
493                         {
494                                 hud_field_icon0 = "gfx/scoreboard/player_ready";
495                         }
496                         else if(!teamplay)
497                         {
498                                 f = stof(getplayerkey(pl.sv_entnum, "colors"));
499                                 {
500                                         hud_field_icon0 = "gfx/scoreboard/playercolor_base";
501                                         hud_field_icon1 = "gfx/scoreboard/playercolor_shirt";
502                                         hud_field_icon1_rgb = colormapPaletteColor(floor(f / 16), 0);
503                                         hud_field_icon2 = "gfx/scoreboard/playercolor_pants";
504                                         hud_field_icon2_rgb = colormapPaletteColor(mod(f, 16), 1);
505                                 }
506                         }
507                         return GetPlayerName(pl.sv_entnum);
508
509                 case SP_FRAGS:
510                         f = pl.(scores[SP_KILLS]);
511                         f -= pl.(scores[SP_SUICIDES]);
512                         return ftos(f);
513
514                 case SP_KDRATIO:
515                         num = pl.(scores[SP_KILLS]);
516                         denom = pl.(scores[SP_DEATHS]);
517
518                         if(denom == 0) {
519                                 hud_field_rgb = '0 1 0';
520                                 str = ftos(num);
521                         } else if(num <= 0) {
522                                 hud_field_rgb = '1 0 0';
523                                 str = ftos(num/denom);
524                         } else
525                                 str = ftos(num/denom);
526
527                         tmp = strstrofs(str, ".", 0);
528                         if(tmp > 0)
529                                 str = substring(str, 0, tmp+2);
530                         return str;
531
532                 default:
533                         tmp = pl.(scores[field]);
534                         f = scores_flags[field];
535                         if(field == ps_primary)
536                                 hud_field_rgb = '1 1 0';
537                         else if(field == ps_secondary)
538                                 hud_field_rgb = '0 1 1';
539                         else
540                                 hud_field_rgb = '1 1 1';
541                         return ScoreString(f, tmp);
542         }
543         //return "error";
544 }
545
546 float xmin, xmax, ymin, ymax, sbwidth;
547 float hud_fixscoreboardcolumnwidth_len;
548 float hud_fixscoreboardcolumnwidth_iconlen;
549 float hud_fixscoreboardcolumnwidth_marginlen;
550
551 string HUD_FixScoreboardColumnWidth(float i, string str)
552 {
553         float field, f;
554         vector sz;
555         field = hud_field[i];
556
557         hud_fixscoreboardcolumnwidth_iconlen = 0;
558
559         if(hud_field_icon0 != "")
560         {
561                 sz = drawgetimagesize(hud_field_icon0);
562                 f = sz_x / sz_y;
563                 if(hud_fixscoreboardcolumnwidth_iconlen < f)
564                         hud_fixscoreboardcolumnwidth_iconlen = f;
565         }
566
567         if(hud_field_icon1 != "")
568         {
569                 sz = drawgetimagesize(hud_field_icon1);
570                 f = sz_x / sz_y;
571                 if(hud_fixscoreboardcolumnwidth_iconlen < f)
572                         hud_fixscoreboardcolumnwidth_iconlen = f;
573         }
574
575         if(hud_field_icon2 != "")
576         {
577                 sz = drawgetimagesize(hud_field_icon2);
578                 f = sz_x / sz_y;
579                 if(hud_fixscoreboardcolumnwidth_iconlen < f)
580                         hud_fixscoreboardcolumnwidth_iconlen = f;
581         }
582
583         hud_fixscoreboardcolumnwidth_iconlen *= hud_fontsize_y / hud_fontsize_x; // fix icon aspect
584
585         if(hud_fixscoreboardcolumnwidth_iconlen != 0)
586                 hud_fixscoreboardcolumnwidth_marginlen = stringwidth(" ", FALSE, hud_fontsize);
587         else
588                 hud_fixscoreboardcolumnwidth_marginlen = 0;
589
590         if(field == SP_NAME) // name gets all remaining space
591         {
592                 float namesize, j;
593                 namesize = sbwidth;// / hud_fontsize_x;
594                 for(j = 0; j < hud_num_fields; ++j)
595                         if(j != i)
596                                 if (hud_field[i] != SP_SEPARATOR)
597                                         namesize -= hud_size[j] + 1;
598                 namesize += 1;
599                 hud_size[i] = namesize;
600
601                 if (hud_fixscoreboardcolumnwidth_iconlen != 0)
602                         namesize -= hud_fixscoreboardcolumnwidth_marginlen + hud_fixscoreboardcolumnwidth_iconlen;
603                 str = textShortenToWidth(str, namesize, hud_fontsize, stringwidth_colors);
604                 hud_fixscoreboardcolumnwidth_len = stringwidth(str, TRUE, hud_fontsize);
605         }
606         else
607                 hud_fixscoreboardcolumnwidth_len = stringwidth(str, FALSE, hud_fontsize);
608
609         f = hud_fixscoreboardcolumnwidth_len + hud_fixscoreboardcolumnwidth_marginlen + hud_fixscoreboardcolumnwidth_iconlen;
610         if(hud_size[i] < f)
611                 hud_size[i] = f;
612
613         return str;
614 }
615
616 void HUD_PrintScoreboardItem(vector pos, entity pl, float is_self, float pl_number)
617 {
618         vector tmp, rgb;
619         rgb = GetTeamRGB(pl.team);
620         string str;
621         float i, field;
622         float is_spec;
623         is_spec = (GetPlayerColor(pl.sv_entnum) == COLOR_SPECTATOR);
624
625         if((rgb == '1 1 1') && (!is_spec)) {
626                 rgb_x = cvar("hud_color_bg_r") + 0.5;
627                 rgb_y = cvar("hud_color_bg_g") + 0.5;
628                 rgb_z = cvar("hud_color_bg_b") + 0.5; }
629
630         // Layout:
631         tmp_x = sbwidth;
632         tmp_y = hud_fontsize_y * 1.25;
633         tmp_z = 0;
634
635         // alternated rows highlighting
636         if(is_self)
637                 drawfill(pos - '1 1 0', tmp + '2 0 0', rgb, scoreboard_highlight_alpha_self, DRAWFLAG_NORMAL);
638         else if((scoreboard_highlight) && (!mod(pl_number,2)))
639                 drawfill(pos - '1 1 0', tmp + '2 0 0', rgb, scoreboard_highlight_alpha, DRAWFLAG_NORMAL);
640
641         tmp_y = 0;
642
643         for(i = 0; i < hud_num_fields; ++i)
644         {
645                 field = hud_field[i];
646                 if(field == SP_SEPARATOR)
647                         break;
648
649                 if(is_spec && field != SP_NAME && field != SP_PING) {
650                         pos_x += hud_size[i] + hud_fontsize_x;
651                         continue;
652                 }
653                 str = HUD_GetField(pl, field);
654                 str = HUD_FixScoreboardColumnWidth(i, str);
655
656                 pos_x += hud_size[i] + hud_fontsize_x;
657
658                 if(field == SP_NAME) {
659                         tmp_x = hud_size[i] - hud_fontsize_x*hud_fixscoreboardcolumnwidth_iconlen - hud_fixscoreboardcolumnwidth_marginlen + hud_fontsize_x;
660                         if (is_self)
661                                 drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
662                         else
663                                 drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name, DRAWFLAG_NORMAL);
664                 } else {
665                         tmp_x = hud_fixscoreboardcolumnwidth_len + hud_fontsize_x;
666                         if (is_self)
667                                 drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
668                         else
669                                 drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name, DRAWFLAG_NORMAL);
670                 }
671
672                 tmp_x = hud_size[i] + hud_fontsize_x;
673                 if(hud_field_icon0 != "")
674                         if (is_self)
675                                 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);
676                         else
677                                 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);
678                 if(hud_field_icon1 != "")
679                         if (is_self)
680                                 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);
681                         else
682                                 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);
683                 if(hud_field_icon2 != "")
684                         if (is_self)
685                                 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);
686                         else
687                                 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);
688         }
689
690         if(hud_field[i] == SP_SEPARATOR)
691         {
692                 pos_x = xmax;
693                 for(i = hud_num_fields-1; i > 0; --i)
694                 {
695                         field = hud_field[i];
696                         if(field == SP_SEPARATOR)
697                                 break;
698
699                         if(is_spec && field != SP_NAME && field != SP_PING) {
700                                 pos_x -= hud_size[i] + hud_fontsize_x;
701                                 continue;
702                         }
703
704                         str = HUD_GetField(pl, field);
705                         str = HUD_FixScoreboardColumnWidth(i, str);
706
707                         if(field == SP_NAME) {
708                                 tmp_x = hud_fixscoreboardcolumnwidth_len; // left or right aligned? let's put it right...
709                                 if(is_self)
710                                         drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
711                                 else
712                                         drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name, DRAWFLAG_NORMAL);
713                         } else {
714                                 tmp_x = hud_fixscoreboardcolumnwidth_len;
715                                 if(is_self)
716                                         drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
717                                 else
718                                         drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name, DRAWFLAG_NORMAL);
719                         }
720
721                         tmp_x = hud_size[i];
722                         if(hud_field_icon0 != "")
723                                 if (is_self)
724                                         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);
725                                 else
726                                         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);
727                         if(hud_field_icon1 != "")
728                                 if (is_self)
729                                         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);
730                                 else
731                                         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);
732                         if(hud_field_icon2 != "")
733                                 if (is_self)
734                                         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);
735                                 else
736                                         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);
737                         pos_x -= hud_size[i] + hud_fontsize_x;
738                 }
739         }
740 }
741
742 /*
743  * HUD_Scoreboard_MakeTable
744  *
745  * Makes a table for a team (for all playing players in DM) and fills it
746  */
747
748 vector HUD_Scoreboard_MakeTable(vector pos, entity tm, vector rgb, vector bg_size)
749 {
750         float body_table_height, i;
751         vector tmp, column_dim;
752         entity pl;
753
754         body_table_height = 1.25 * hud_fontsize_y * max(1, tm.team_size); // no player? show 1 empty line
755
756         pos -= '1 1 0';
757
758         tmp_x = sbwidth + 2;
759         tmp_y = 1.25 * hud_fontsize_y;
760
761         // rounded header
762         drawpic(pos, "gfx/scoreboard/scoreboard_tableheader", tmp, (rgb * hud_color_bg_team) + '0.5 0.5 0.5', scoreboard_alpha_bg, DRAWFLAG_NORMAL);
763
764         // table border
765         tmp_y += hud_border_thickness;
766         tmp_y += body_table_height;
767         drawborderlines(hud_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg, DRAWFLAG_NORMAL); // more transparency for the scoreboard
768
769         // separator header/table
770         pos_y += 1.25 * hud_fontsize_y;
771         tmp_y = hud_border_thickness;
772         drawfill(pos, tmp, '0 0 0', scoreboard_alpha_bg, DRAWFLAG_NORMAL);
773
774         pos_y += hud_border_thickness;
775
776         // table background
777         tmp_y = body_table_height;
778         drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * hud_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
779
780         // anyway, apply some color
781         //drawfill(pos, tmp + '2 0 0', rgb, 0.1, DRAWFLAG_NORMAL);
782
783         // go back to the top to make alternated columns highlighting and to print the strings
784         pos_y -= 1.25 * hud_fontsize_y;
785         pos_y -= hud_border_thickness;
786
787         pos += '1 1 0';
788
789         if (scoreboard_highlight)
790         {
791                 column_dim_y = 1.25 * hud_fontsize_y; // header
792                 column_dim_y += hud_border_thickness;
793                 column_dim_y += body_table_height;
794         }
795
796         // print the strings of the columns headers and draw the columns
797         for(i = 0; i < hud_num_fields; ++i)
798         {
799                 if(hud_field[i] == SP_SEPARATOR)
800                         break;
801                 column_dim_x = hud_size[i] + hud_fontsize_x;
802                 if (scoreboard_highlight)
803                 {
804                         if (mod(i,2))
805                                 drawfill(pos - '0 1 0' - hud_fontsize_x / 2 * '1 0 0', column_dim, '0 0 0', scoreboard_alpha_bg * 0.2, DRAWFLAG_NORMAL);
806                 }
807                 drawstring(pos, hud_title[i], hud_fontsize, rgb * 1.5, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
808                 pos_x += column_dim_x;
809         }
810         if(hud_field[i] == SP_SEPARATOR)
811         {
812                 pos_x = xmax;
813                 tmp_y = 0;
814                 for(i = hud_num_fields-1; i > 0; --i)
815                 {
816                         if(hud_field[i] == SP_SEPARATOR)
817                                 break;
818
819                         pos_x -= hud_size[i];
820
821                         if (scoreboard_highlight)
822                         {
823                                 if (!mod(i,2))
824                                 {
825                                         if (i == hud_num_fields-1)
826                                                 column_dim_x = hud_size[i] + hud_fontsize_x / 2 + 1;
827                                         else
828                                                 column_dim_x = hud_size[i] + hud_fontsize_x;
829                                         drawfill(pos - '0 1 0' - hud_fontsize_x / 2 * '1 0 0', column_dim, '0 0 0', scoreboard_alpha_bg * 0.2, DRAWFLAG_NORMAL);
830                                 }
831                         }
832
833                         tmp_x = stringwidth(hud_title[i], FALSE, hud_fontsize);
834                         tmp_x = (hud_size[i] - tmp_x) * hud_fontsize_x;
835                         drawstring(pos + tmp, hud_title[i], hud_fontsize, rgb * 1.5, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
836                         pos_x -= hud_fontsize_x;
837                 }
838         }
839
840         pos_x = xmin;
841         pos_y += 1.25 * hud_fontsize_y; // skip the header
842         pos_y += hud_border_thickness;
843
844         // fill the table and draw the rows
845         i = 0;
846         if (teamplay)
847                 for(pl = players.sort_next; pl; pl = pl.sort_next)
848                 {
849                         if(pl.team != tm.team)
850                                 continue;
851                         HUD_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1), i);
852                         pos_y += 1.25 * hud_fontsize_y;
853                         ++i;
854                 }
855         else
856                 for(pl = players.sort_next; pl; pl = pl.sort_next)
857                 {
858                         if(pl.team == COLOR_SPECTATOR)
859                                 continue;
860                         HUD_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1), i);
861                         pos_y += 1.25 * hud_fontsize_y;
862                         ++i;
863                 }
864
865         if (i == 0)
866                 pos_y += 1.25 * hud_fontsize_y; // move to the end of the table
867         pos_y += 1.25 * hud_fontsize_y; // move empty row (out of the table)
868
869         return pos;
870 }
871
872 float HUD_WouldDrawScoreboard() {
873         if (scoreboard_showscores)
874                 return 1;
875         else if (intermission == 1)
876                 return 1;
877         else if (intermission == 2)
878                 return 1;
879         else if (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard"))
880                 return 1;
881         else if(scoreboard_showscores_force)
882                 return 1;
883         return 0;
884 }
885
886 float g_minstagib;
887 float average_accuracy;
888 vector HUD_DrawScoreboardAccuracyStats(vector pos, vector rgb, vector bg_size)
889 {
890         float i;
891         float weapon_hit, weapon_damage, weapon_stats;
892         float fontsize = 40 * 1/3;
893         float weapon_cnt = 12;
894         float rows;
895         if(cvar("hud_accuracy_doublerows"))
896                 rows = 2;
897         else
898                 rows = 1;
899         float height = 40;
900
901         if(warmup_stage)
902         {
903                 return pos;
904         }
905
906         drawstring(pos, strcat("Accuracy stats (average ", ftos(average_accuracy), "%)"), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
907         pos_y += 18;
908         vector tmp;
909         tmp_x = sbwidth;
910         tmp_y = height * rows;
911
912         drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * hud_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
913         drawborderlines(hud_accuracy_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg * 0.75, DRAWFLAG_NORMAL);
914
915         // column highlighting
916         for(i = 0; i < weapon_cnt/rows; ++i)
917         {
918                 if(!mod(i, 2))
919                         drawfill(pos + '1 0 0' * (sbwidth/weapon_cnt) * rows * i, '0 1 0' * height * rows + '1 0 0' * (sbwidth/weapon_cnt) * rows, '0 0 0', scoreboard_alpha_bg * 0.2, DRAWFLAG_NORMAL);
920         }
921
922         // row highlighting
923         for(i = 0; i < rows; ++i)
924         {
925                 drawfill(pos + '0 1 0' * height * (2/3) + '0 1 0' * height * i, '1 0 0' * sbwidth + '0 1 0' * fontsize, '1 1 1', scoreboard_highlight_alpha, DRAWFLAG_NORMAL);
926         }
927
928         drawfont = hud_bigfont;
929         average_accuracy = 0;
930         float weapons_with_stats;
931         weapons_with_stats = 0;
932         if(rows == 2)
933                 pos_x += sbwidth/weapon_cnt / 2;
934
935         if(getstati(STAT_SWITCHWEAPON) == WEP_MINSTANEX)
936                 g_minstagib = 1; // TODO: real detection for minstagib?
937
938         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
939         {
940                 self = get_weaponinfo(i);
941                 if not(self.weapons)
942                         continue;
943                 if ((i == WEP_NEX && g_minstagib) || i == WEP_PORTO || (i == WEP_MINSTANEX && !g_minstagib) || i == WEP_TUBA || i == WEP_FIREBALL) // skip port-o-launch, nex || minstanex, tuba and fireball
944                         continue;
945                 weapon_hit = weapon_hits[i-WEP_FIRST];
946                 weapon_damage = weapon_fired[i-WEP_FIRST];
947                 if(weapon_damage)
948                         weapon_stats = bound(0, floor(100 * weapon_hit / weapon_damage), 100);
949                 float weapon_alpha;
950
951                 if(weapon_damage)
952                         weapon_alpha = scoreboard_alpha_fg;
953                 else
954                         weapon_alpha = 0.2 * scoreboard_alpha_fg;
955
956                 // weapon icon
957                 drawpic(pos, strcat("gfx/weapons/weapon", self.netname), '1 0 0' * sbwidth * (1/weapon_cnt) + '0 1 0' * height * (2/3), '1 1 1', weapon_alpha, DRAWFLAG_NORMAL);
958                 // the accuracy
959                 if(weapon_damage) {
960                         weapons_with_stats += 1;
961                         average_accuracy += weapon_stats; // store sum of all accuracies in average_accuracy
962
963                         string s;
964                         s = strcat(ftos(weapon_stats),"%");
965
966                         float padding;
967                         padding = ((sbwidth/weapon_cnt) - stringwidth(s, FALSE, '1 0 0' * fontsize)) / 2; // center the accuracy value
968
969                         rgb = HUD_AccuracyColor(weapon_stats);
970                         drawstring(pos + '1 0 0' * padding + '0 1 0' * height * (2/3), s, '1 1 0' * fontsize, rgb, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
971                 }
972                 pos_x += sbwidth/weapon_cnt * rows;
973                 if(rows == 2 && i == 6) {
974                         pos_x -= sbwidth;
975                         pos_y += height;
976                 }
977         }
978         drawfont = hud_font;
979
980         if(weapons_with_stats)
981                 average_accuracy = floor(average_accuracy / weapons_with_stats);
982
983         if(rows == 2)
984                 pos_x -= sbwidth/weapon_cnt / 2;
985         pos_x -= sbwidth;
986         pos_y += height;
987         return pos;
988 }
989
990 vector HUD_DrawScoreboardRankings(vector pos, entity pl,  vector rgb, vector bg_size)
991 {
992         float i;
993         RANKINGS_RECEIVED_CNT = 0;
994                 for (i=RANKINGS_CNT-1; i>=0; --i)
995                         if (grecordtime[i])
996                                 RANKINGS_RECEIVED_CNT = RANKINGS_RECEIVED_CNT + 1;
997
998         if (RANKINGS_RECEIVED_CNT == 0)
999                 return pos;
1000
1001         float is_spec;
1002         is_spec = (GetPlayerColor(pl.sv_entnum) == COLOR_SPECTATOR);
1003         vector hl_rgb;
1004                 hl_rgb_x = cvar("hud_color_bg_r") + 0.5;
1005                 hl_rgb_y = cvar("hud_color_bg_g") + 0.5;
1006                 hl_rgb_z = cvar("hud_color_bg_b") + 0.5;
1007
1008         pos_y += hud_fontsize_y;
1009         drawstring(pos, strcat("Rankings"), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1010         pos_y += hud_fontsize_y;
1011         vector tmp;
1012         tmp_x = sbwidth;
1013         tmp_y = hud_fontsize_y * RANKINGS_RECEIVED_CNT;
1014
1015         drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * hud_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
1016         drawborderlines(hud_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg * 0.75, DRAWFLAG_NORMAL);
1017
1018         // row highlighting
1019         for(i = 0; i<RANKINGS_RECEIVED_CNT; ++i)
1020         {
1021                 string n, p;
1022                 float t;
1023                 t = grecordtime[i];
1024                 if (t == 0)
1025                         continue;
1026                 n = grecordholder[i];
1027                 p = race_PlaceName(i+1);
1028                 if(grecordholder[i] == GetPlayerName(player_localentnum - 1))
1029                         drawfill(pos, '1 0 0' * sbwidth + '0 1 0' * hud_fontsize_y, hl_rgb, scoreboard_highlight_alpha_self, DRAWFLAG_NORMAL);
1030                 else if(!mod(i, 2) && scoreboard_highlight)
1031                         drawfill(pos, '1 0 0' * sbwidth + '0 1 0' * hud_fontsize_y, hl_rgb, scoreboard_highlight_alpha, DRAWFLAG_NORMAL);
1032                 drawstring(pos, p, hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1033                 drawstring(pos + '3 0 0' * hud_fontsize_x, TIME_ENCODED_TOSTRING(t), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1034                 drawcolorcodedstring(pos + '8 0 0' * hud_fontsize_x, n, hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1035                 pos += '0 1 0' * hud_fontsize_y;
1036         }
1037
1038         return pos;
1039 }
1040
1041 float scoreboard_fade_alpha;
1042 float hud_woulddrawscoreboard_prev;
1043 float hud_woulddrawscoreboard_change; // "time" at which HUD_WouldDrawScoreboard() changed
1044 void HUD_DrawScoreboard()
1045 {
1046         HUD_UpdatePlayerTeams();
1047
1048         float hud_woulddrawscoreboard;
1049         hud_woulddrawscoreboard = HUD_WouldDrawScoreboard();
1050         if(hud_woulddrawscoreboard != hud_woulddrawscoreboard_prev) {
1051                 hud_woulddrawscoreboard_change = time;
1052                 hud_woulddrawscoreboard_prev = hud_woulddrawscoreboard;
1053         }
1054
1055         float scoreboard_fadeinspeed = cvar_or("scoreboard_fadeinspeed", 10);
1056         float scoreboard_fadeoutspeed = cvar_or("scoreboard_fadeoutspeed", 5);
1057         if(hud_woulddrawscoreboard) {
1058                 if (scoreboard_fadeinspeed)
1059                         scoreboard_fade_alpha = bound (0, (time - hud_woulddrawscoreboard_change) * scoreboard_fadeinspeed, 1);
1060                 else
1061                         scoreboard_fade_alpha = 1;
1062         }
1063         else
1064                 if (scoreboard_fadeoutspeed)
1065                         scoreboard_fade_alpha = bound (0, (1/scoreboard_fadeoutspeed - (time - hud_woulddrawscoreboard_change)) * scoreboard_fadeoutspeed, 1);
1066                 else
1067                         scoreboard_fade_alpha = 0;
1068
1069         if not(scoreboard_fade_alpha)
1070                 return;
1071
1072         scoreboard_alpha_bg = cvar("scoreboard_alpha_bg") * scoreboard_fade_alpha;
1073         scoreboard_alpha_fg = cvar_or("scoreboard_alpha_fg", 1.0) * scoreboard_fade_alpha;
1074         scoreboard_highlight = cvar("scoreboard_highlight");
1075         scoreboard_highlight_alpha = cvar_or("scoreboard_highlight_alpha", 0.10) * scoreboard_alpha_fg;
1076         scoreboard_highlight_alpha_self = cvar_or("scoreboard_highlight_alpha_self", 0.25) * scoreboard_alpha_fg;
1077         scoreboard_alpha_name = cvar_or("scoreboard_alpha_name", 0.9) * scoreboard_alpha_fg;
1078         scoreboard_alpha_name_self = cvar_or("scoreboard_alpha_name_self", 1) * scoreboard_alpha_fg;
1079
1080         vector rgb, pos, tmp;
1081         entity pl, tm;
1082
1083         sbwidth = HUD_GetWidth(6.5 * hud_fontsize_y);
1084
1085         xmin = 0.5 * (vid_conwidth - sbwidth);
1086         ymin = SCOREBOARD_OFFSET;
1087
1088         xmax = vid_conwidth - xmin;
1089         ymax = vid_conheight - 0.2*vid_conheight;
1090
1091         // Initializes position
1092         pos_x = xmin;
1093         pos_y = ymin;
1094         pos_z = 0;
1095
1096         // Heading
1097         drawfont = hud_bigfont;
1098         drawstringcenter('0 1 0' * ymin, "Scoreboard", '24 24 0', '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1099
1100         pos_y += 24 + 4;
1101         pos_y += hud_fontsize_y;
1102
1103         drawfont = hud_font;
1104
1105         // Draw the scoreboard
1106         vector bg_size;
1107         bg_size = drawgetimagesize("gfx/hud/scoreboard_scoreboard_bg");
1108
1109         if(teamplay)
1110         {
1111                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
1112                 {
1113                         if(tm.team == COLOR_SPECTATOR)
1114                                 continue;
1115
1116                         rgb = GetTeamRGB(tm.team);
1117                         HUD_DrawXNum(pos - '9.5 0 0' * hud_fontsize_y + '0 1 0' * hud_fontsize_y, tm.(teamscores[ts_primary]), 6, 0, hud_fontsize_y * 1.5, rgb, 0, 1, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1118
1119                         if(ts_primary != ts_secondary)
1120                                 HUD_DrawXNum(pos - '7.5 0 0' * hud_fontsize_y + '0 2.5 0' * hud_fontsize_y, tm.(teamscores[ts_secondary]), 6, 0, hud_fontsize_y * 1, rgb, 0, 1, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1121
1122                         pos = HUD_Scoreboard_MakeTable(pos, tm, rgb, bg_size);
1123                 }
1124         }
1125         else
1126         {
1127                 rgb_x = cvar("hud_color_bg_r");
1128                 rgb_y = cvar("hud_color_bg_g");
1129                 rgb_z = cvar("hud_color_bg_b");
1130
1131                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
1132                 {
1133                         if(tm.team == COLOR_SPECTATOR)
1134                                 continue;
1135
1136                         pos = HUD_Scoreboard_MakeTable(pos, tm, rgb, bg_size);
1137                 }
1138         }
1139
1140         if(gametype == GAME_CTS || gametype == GAME_RACE) {
1141                 if(race_speedaward) {
1142                         drawcolorcodedstring(pos, strcat("Speed award: ", ftos(race_speedaward), " (", race_speedaward_holder, ")"), hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1143                         pos_y += 1.25 * hud_fontsize_y;
1144                 }
1145                 if(race_speedaward_alltimebest) {
1146                         drawcolorcodedstring(pos, strcat("All-time fastest: ", ftos(race_speedaward_alltimebest), " (", race_speedaward_alltimebest_holder, ")"), hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1147                         pos_y += 1.25 * hud_fontsize_y;
1148                 }
1149                 pos = HUD_DrawScoreboardRankings(pos, pl, rgb, bg_size);
1150         }
1151         else if(cvar("hud_accuracy") && spectatee_status != -1) {
1152                 if(teamplay)
1153                         pos = HUD_DrawScoreboardAccuracyStats(pos, GetTeamRGB(myteam), bg_size);
1154                 else
1155                         pos = HUD_DrawScoreboardAccuracyStats(pos, rgb, bg_size);
1156         }
1157
1158         tmp = pos + '0 1.5 0' * hud_fontsize_y;
1159         pos_y += 3 * hud_fontsize_y;
1160
1161         // List spectators
1162         float specs;
1163         specs = 0;
1164         for(pl = players.sort_next; pl; pl = pl.sort_next)
1165         {
1166                 if(pl.team != COLOR_SPECTATOR)
1167                         continue;
1168                 HUD_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1), specs);
1169                 pos_y += 1.25 * hud_fontsize_y;
1170                 ++specs;
1171         }
1172
1173         if(specs)
1174                 drawstring(tmp, "Spectators", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1175
1176         // Print info string
1177         string str;
1178         float tl, fl, ll;
1179         str = strcat("playing on ^2", shortmapname, "^7");
1180         tl = getstatf(STAT_TIMELIMIT);
1181         fl = getstatf(STAT_FRAGLIMIT);
1182         ll = getstatf(STAT_LEADLIMIT);
1183         if(gametype == GAME_LMS)
1184         {
1185                 if(tl > 0)
1186                         str = strcat(str, " for up to ^1", ftos(tl), " minutes^7");
1187         }
1188         else
1189         {
1190                 if(tl > 0)
1191                         str = strcat(str, " for ^1", ftos(tl), " minutes^7");
1192                 if(fl > 0)
1193                 {
1194                         if(tl > 0)
1195                                 str = strcat(str, " or");
1196                         if(teamplay)
1197                         {
1198                                 str = strcat(str, " until ^3", ScoreString(teamscores_flags[ts_primary], fl));
1199                                 if(teamscores_label[ts_primary] == "score")
1200                                         str = strcat(str, " points^7");
1201                                 else if(teamscores_label[ts_primary] == "fastest")
1202                                         str = strcat(str, " is beaten^7");
1203                                 else
1204                                         str = strcat(str, " ", teamscores_label[ts_primary]);
1205                         }
1206                         else
1207                         {
1208                                 str = strcat(str, " until ^3", ScoreString(scores_flags[ps_primary], fl));
1209                                 if(scores_label[ps_primary] == "score")
1210                                         str = strcat(str, " points^7");
1211                                 else if(scores_label[ps_primary] == "fastest")
1212                                         str = strcat(str, " is beaten^7");
1213                                 else
1214                                         str = strcat(str, " ", scores_label[ps_primary]);
1215                         }
1216                 }
1217                 if(ll > 0)
1218                 {
1219                         if(tl > 0 || fl > 0)
1220                                 str = strcat(str, " or");
1221                         if(teamplay)
1222                         {
1223                                 str = strcat(str, " until a lead of ^3", ScoreString(teamscores_flags[ts_primary], ll));
1224                                 if(teamscores_label[ts_primary] == "score")
1225                                         str = strcat(str, " points^7");
1226                                 else if(teamscores_label[ts_primary] == "fastest")
1227                                         str = strcat(str, " is beaten^7");
1228                                 else
1229                                         str = strcat(str, " ", teamscores_label[ts_primary]);
1230                         }
1231                         else
1232                         {
1233                                 str = strcat(str, " until a lead of ^3", ScoreString(scores_flags[ps_primary], ll));
1234                                 if(scores_label[ps_primary] == "score")
1235                                         str = strcat(str, " points^7");
1236                                 else if(scores_label[ps_primary] == "fastest")
1237                                         str = strcat(str, " is beaten^7");
1238                                 else
1239                                         str = strcat(str, " ", scores_label[ps_primary]);
1240                         }
1241                 }
1242         }
1243
1244
1245         pos_y += 1.2 * hud_fontsize_y;
1246         drawcolorcodedstring(pos + '0.5 0 0' * (sbwidth - stringwidth(str, TRUE, hud_fontsize)), str, hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1247
1248         scoreboard_bottom = pos_y + 2 * hud_fontsize_y;
1249 }
1250
1251 void HUD_DrawAccuracyStats_Description_Hitscan(vector position)
1252 {
1253         drawstring(position + '0 3 0' * hud_fontsize_y, "Shots fired:", hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1254         drawstring(position + '0 5 0' * hud_fontsize_y, "Shots hit:", hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1255         drawstring(position + '0 7 0' * hud_fontsize_y, "Accuracy:", hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1256         drawstring(position + '0 9 0' * hud_fontsize_y, "Shots missed:", hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1257 }
1258
1259 void HUD_DrawAccuracyStats_Description_Splash(vector position)
1260 {
1261         drawstring(position + '0 3 0' * hud_fontsize_y, "Maximum damage:", hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1262         drawstring(position + '0 5 0' * hud_fontsize_y, "Actual damage:", hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1263         drawstring(position + '0 7 0' * hud_fontsize_y, "Accuracy:", hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1264         drawstring(position + '0 9 0' * hud_fontsize_y, "Damage wasted:", hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1265 }
1266
1267 void HUD_DrawAccuracyStats()
1268 {
1269         float i, count_hitscan, count_splash, row;  // count is the number of 'colums'
1270         float weapon_hit, weapon_damage, weapon_stats;
1271         float left_border;  // position where the weapons start, the description is in the border
1272         vector fill_colour, fill_size;
1273         vector pos;
1274         vector border_colour;
1275
1276         float col_margin = 20;  // pixels between the columns
1277         float row_margin = 20;  // pixels between the rows
1278
1279         fill_size_x = 5 * hud_fontsize_x;  // width of the background
1280         fill_size_y = 10 * hud_fontsize_y;  // height of the background
1281
1282         drawfont = hud_bigfont;
1283         pos_x = 0;
1284         pos_y = SCOREBOARD_OFFSET;
1285         pos_z = 0;
1286         drawstringcenter(pos, "Weapon Accuracy", 2 * hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1287
1288         left_border = col_margin + 11 * hud_fontsize_x;
1289
1290         drawfont = hud_font;
1291
1292         if(warmup_stage)
1293         {
1294                 pos_y += 40;
1295                 if(mod(time, 1) >= 0.4)
1296                         drawstringcenter(pos, "Stats are not tracked during warmup stage", hud_fontsize, '1 1 0', hud_alpha_fg, DRAWFLAG_NORMAL);
1297
1298                 return;
1299         }
1300
1301         if(gametype == GAME_RACE || gametype == GAME_CTS)
1302         {
1303                 pos_y += 40;
1304                 if(mod(time, 1) >= 0.4)
1305                         drawstringcenter(pos, "Stats are not tracked in Race/CTS", hud_fontsize, '1 1 0', hud_alpha_fg, DRAWFLAG_NORMAL);
1306
1307                 return;
1308         }
1309
1310         float top_border_hitscan = SCOREBOARD_OFFSET + 55;  // position where the hitscan row starts: pixels down the screen
1311         HUD_DrawAccuracyStats_Description_Hitscan('1 0 0' * col_margin + '0 1 0' * top_border_hitscan);
1312
1313         float top_border_splash = SCOREBOARD_OFFSET + 175;  // position where the splash row starts: pixels down the screen
1314         HUD_DrawAccuracyStats_Description_Splash('1 0 0' * col_margin + '0 1 0' * top_border_splash);
1315
1316         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1317         {
1318                 self = get_weaponinfo(i);
1319                 if not(self.weapons)
1320                         continue;
1321                 weapon_hit = weapon_hits[i-WEP_FIRST];
1322                 weapon_damage = weapon_fired[i-WEP_FIRST];
1323                 border_colour = (i == activeweapon) ? '1 1 1' : '0 0 0';  // white or black border
1324
1325                 if (weapon_damage) {
1326                         if (self.spawnflags & WEP_TYPE_SPLASH) {
1327                                 weapon_stats = bound(0, floor(100 * weapon_hit / weapon_damage), 100);
1328
1329                                 fill_colour_x = 1 - 0.015 * weapon_stats;
1330                                 fill_colour_y = 1 - 0.015 * (100 - weapon_stats);
1331
1332                                 // how the background colour is calculated
1333                                 // %    red             green   red_2                   green_2
1334                                 // 0    1               0               1 - % * 0.015   1 - (100 - %) * 0.015
1335                                 // 10   0.85    0               1 - % * 0.015   1 - (100 - %) * 0.015
1336                                 // 20   0.70    0               1 - % * 0.015   1 - (100 - %) * 0.015
1337                                 // 30   0.55    0               1 - % * 0.015   1 - (100 - %) * 0.015
1338                                 // 40   0.40    0.10    1 - % * 0.015   1 - (100 - %) * 0.015
1339                                 // 50   0.25    0.25    1 - % * 0.015   1 - (100 - %) * 0.015
1340                                 // 60   0.10    0.40    1 - % * 0.015   1 - (100 - %) * 0.015
1341                                 // 70   0               0.55    1 - % * 0.015   1 - (100 - %) * 0.015
1342                                 // 80   0               0.70    1 - % * 0.015   1 - (100 - %) * 0.015
1343                                 // 90   0               0.85    1 - % * 0.015   1 - (100 - %) * 0.015
1344                                 // 100  0               1               1 - % * 0.015   1 - (100 - %) * 0.015
1345
1346                                 if ((left_border + count_splash * (fill_size_x + col_margin) + fill_size_x) >= vid_conwidth)
1347                                 {
1348                                         count_splash = 0;
1349                                         ++row;
1350                                         HUD_DrawAccuracyStats_Description_Splash('1 0 0' * col_margin + '0 1 0' * (top_border_splash + row * (fill_size_y + row_margin)));
1351                                 }
1352
1353                                 pos_x = left_border + count_splash * (fill_size_x + col_margin);
1354                                 pos_y = top_border_splash + row * (fill_size_y + row_margin);
1355
1356                                 // background
1357                                 drawpic(pos, "gfx/scoreboard/accuracy_bg", fill_size , fill_colour, hud_alpha_bg, DRAWFLAG_NORMAL);
1358                                 drawborderlines(hud_border_thickness, pos, fill_size, border_colour, hud_alpha_bg, DRAWFLAG_NORMAL);
1359
1360                                 // the weapon
1361                                 drawpic(pos, strcat("gfx/weapons/weapon", self.netname), '1 0.5 0' * fill_size_x , '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1362
1363                                 // the amount of shots fired or max damage
1364                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 3 0' * hud_fontsize_y, ftos(weapon_damage), hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1365
1366                                 // the amount of hits or actual damage
1367                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 5 0' * hud_fontsize_y, ftos(weapon_hit), hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1368
1369                                 // the accuracy
1370                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 7 0' * hud_fontsize_y, strcat(ftos(weapon_stats),"%"), hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1371
1372                                 // the amount of shots missed or damage wasted
1373                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 9 0' * hud_fontsize_y, ftos(max(0, weapon_damage - weapon_hit)), hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1374
1375                                 ++count_splash;
1376                         } else if (self.spawnflags & WEP_TYPE_HITSCAN) {
1377                                 weapon_stats = bound(0, floor(100 * weapon_hit / weapon_damage), 100);
1378
1379                                 fill_colour_x = 1 - 0.015 * weapon_stats;
1380                                 fill_colour_y = 1 - 0.015 * (100 - weapon_stats);
1381
1382                                 // how the background colour is calculated
1383                                 // %    red             green   red_2                   green_2
1384                                 // 0    1               0               1 - % * 0.015   1 - (100 - %) * 0.015
1385                                 // 10   0.850   0               1 - % * 0.015   1 - (100 - %) * 0.015
1386                                 // 20   0.70    0               1 - % * 0.015   1 - (100 - %) * 0.015
1387                                 // 30   0.55    0               1 - % * 0.015   1 - (100 - %) * 0.015
1388                                 // 40   0.40    0.10    1 - % * 0.015   1 - (100 - %) * 0.015
1389                                 // 50   0.25    0.25    1 - % * 0.015   1 - (100 - %) * 0.015
1390                                 // 60   0.10    0.40    1 - % * 0.015   1 - (100 - %) * 0.015
1391                                 // 70   0               0.55    1 - % * 0.015   1 - (100 - %) * 0.015
1392                                 // 80   0               0.70    1 - % * 0.015   1 - (100 - %) * 0.015
1393                                 // 90   0               0.85    1 - % * 0.015   1 - (100 - %) * 0.015
1394                                 // 100  0               1               1 - % * 0.015   1 - (100 - %) * 0.015
1395
1396                                 if ((left_border + count_hitscan * (fill_size_x + col_margin) + fill_size_x + cvar("stats_right_margin")) >= vid_conwidth)
1397                                 {
1398                                         count_hitscan = 0;
1399                                         ++row;
1400                                         HUD_DrawAccuracyStats_Description_Hitscan('1 0 0' * col_margin + '0 1 0' * (top_border_hitscan + row * (fill_size_y + row_margin)));
1401                                 }
1402
1403                                 pos_x = left_border + count_hitscan * (fill_size_x + col_margin);
1404                                 pos_y = top_border_hitscan + row * (fill_size_y + row_margin);
1405
1406                                 // background
1407                                 drawpic(pos, "gfx/scoreboard/accuracy_bg", fill_size , fill_colour, hud_alpha_bg, DRAWFLAG_NORMAL);
1408                                 drawborderlines(hud_border_thickness, pos, fill_size, border_colour, hud_alpha_bg, DRAWFLAG_NORMAL);
1409
1410                                 // the weapon
1411                                 drawpic(pos, strcat("gfx/weapons/weapon", self.netname), '1 0.5 0' * fill_size_x , '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1412
1413                                 // the amount of shots fired or max damage
1414                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 3 0' * hud_fontsize_y, ftos(weapon_damage), hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1415
1416                                 // the amount of hits or actual damage
1417                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 5 0' * hud_fontsize_y, ftos(weapon_hit), hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1418
1419                                 // the accuracy
1420                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 7 0' * hud_fontsize_y, strcat(ftos(weapon_stats),"%"), hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1421
1422                                 // the amount of shots missed or damage wasted
1423                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 9 0' * hud_fontsize_y, ftos(max(0, weapon_damage - weapon_hit)), hud_fontsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1424
1425                                 ++count_hitscan;
1426                         }
1427                 }
1428         }
1429 }