Merge branch 'master' into fruitiex/gamemode_freezetag
[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', scoreboard_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 ^2scoreboard_columns_set command.\n");
199         print("^3|---------------------------------------------------------------|\n");
200         print("Usage:\n");
201         print("^2scoreboard_columns_set default\n");
202         print("^2scoreboard_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: scoreboard_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("scoreboard_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         hud_fontsize = HUD_GetFontsize("hud_fontsize"); 
301
302         for(i = 0; i < argc - 1; ++i)
303         {
304                 float nocomplain;
305                 str = argv(i+1);
306
307                 nocomplain = FALSE;
308                 if(substring(str, 0, 1) == "?")
309                 {
310                         nocomplain = TRUE;
311                         str = substring(str, 1, strlen(str) - 1);
312                 }
313
314                 slash = strstrofs(str, "/", 0);
315                 if(slash >= 0)
316                 {
317                         pattern = substring(str, 0, slash);
318                         str = substring(str, slash + 1, strlen(str) - (slash + 1));
319
320                         if not(isGametypeInFilter(gametype, teamplay, pattern))
321                                 continue;
322                 }
323
324                 strunzone(hud_title[hud_num_fields]);
325                 hud_title[hud_num_fields] = strzone(str);
326                 hud_size[hud_num_fields] = stringwidth(str, FALSE, hud_fontsize);
327                 str = strtolower(str);
328
329                 if(str == "ping") {
330                         hud_field[hud_num_fields] = SP_PING;
331                 } else if(str == "pl") {
332                         hud_field[hud_num_fields] = SP_PL;
333                 } else if(str == "kd" || str == "kdr" || str == "kdratio" || str == "k/d") {
334                         hud_field[hud_num_fields] = SP_KDRATIO;
335                 } else if(str == "name" || str == "nick") {
336                         hud_field[hud_num_fields] = SP_NAME;
337                         have_name = 1;
338                 } else if(str == "|") {
339                         hud_field[hud_num_fields] = SP_SEPARATOR;
340                         have_separator = 1;
341                 } else {
342                         for(j = 0; j < MAX_SCORE; ++j)
343                                 if(str == strtolower(scores_label[j]))
344                                         goto found; // sorry, but otherwise fteqcc -O3 miscompiles this and warns about "unreachable code"
345 :notfound
346                         if(str == "frags")
347                         {
348                                 j = SP_FRAGS;
349                         }
350                         else
351                         {
352                                 if not(nocomplain)
353                                         print(strcat("^1Error:^7 Unknown score field: '", str, "'\n"));
354                                 continue;
355                         }
356 :found
357                         hud_field[hud_num_fields] = j;
358                         if(j == ps_primary)
359                                 have_primary = 1;
360                         if(j == ps_secondary)
361                                 have_secondary = 1;
362                 }
363                 ++hud_num_fields;
364                 if(hud_num_fields >= MAX_HUD_FIELDS)
365                         break;
366         }
367
368         if(scores_flags[ps_primary] & SFL_ALLOW_HIDE)
369                 have_primary = 1;
370         if(scores_flags[ps_secondary] & SFL_ALLOW_HIDE)
371                 have_secondary = 1;
372         if(ps_primary == ps_secondary)
373                 have_secondary = 1;
374         missing = (!have_primary) + (!have_secondary) + (!have_separator) + (!have_name);
375
376         if(hud_num_fields+missing < MAX_HUD_FIELDS)
377         {
378                 if(!have_name)
379                 {
380                         strunzone(hud_title[hud_num_fields]);
381                         for(i = hud_num_fields; i > 0; --i)
382                         {
383                                 hud_title[i] = hud_title[i-1];
384                                 hud_size[i] = hud_size[i-1];
385                                 hud_field[i] = hud_field[i-1];
386                         }
387                         hud_title[0] = strzone("name");
388                         hud_field[0] = SP_NAME;
389                         ++hud_num_fields;
390                         print("fixed missing field 'name'\n");
391
392                         if(!have_separator)
393                         {
394                                 strunzone(hud_title[hud_num_fields]);
395                                 for(i = hud_num_fields; i > 1; --i)
396                                 {
397                                         hud_title[i] = hud_title[i-1];
398                                         hud_size[i] = hud_size[i-1];
399                                         hud_field[i] = hud_field[i-1];
400                                 }
401                                 hud_title[1] = strzone("|");
402                                 hud_field[1] = SP_SEPARATOR;
403                                 hud_size[1] = stringwidth("|", FALSE, hud_fontsize);
404                                 ++hud_num_fields;
405                                 print("fixed missing field '|'\n");
406                         }
407                 }
408                 else if(!have_separator)
409                 {
410                         strunzone(hud_title[hud_num_fields]);
411                         hud_title[hud_num_fields] = strzone("|");
412                         hud_size[hud_num_fields] = stringwidth("|", FALSE, hud_fontsize);
413                         hud_field[hud_num_fields] = SP_SEPARATOR;
414                         ++hud_num_fields;
415                         print("fixed missing field '|'\n");
416                 }
417                 if(!have_secondary)
418                 {
419                         strunzone(hud_title[hud_num_fields]);
420                         hud_title[hud_num_fields] = strzone(scores_label[ps_secondary]);
421                         hud_size[hud_num_fields] = stringwidth(hud_title[hud_num_fields], FALSE, hud_fontsize);
422                         hud_field[hud_num_fields] = ps_secondary;
423                         ++hud_num_fields;
424                         print("fixed missing field '", scores_label[ps_secondary], "'\n");
425                 }
426                 if(!have_primary)
427                 {
428                         strunzone(hud_title[hud_num_fields]);
429                         hud_title[hud_num_fields] = strzone(scores_label[ps_primary]);
430                         hud_size[hud_num_fields] = stringwidth(hud_title[hud_num_fields], FALSE, hud_fontsize);
431                         hud_field[hud_num_fields] = ps_primary;
432                         ++hud_num_fields;
433                         print("fixed missing field '", scores_label[ps_primary], "'\n");
434                 }
435         }
436
437         hud_field[hud_num_fields] = SP_END;
438 }
439
440 // MOVEUP::
441 vector hud_field_rgb;
442 string hud_field_icon0;
443 string hud_field_icon1;
444 string hud_field_icon2;
445 vector hud_field_icon0_rgb;
446 vector hud_field_icon1_rgb;
447 vector hud_field_icon2_rgb;
448 float hud_field_icon0_alpha;
449 float hud_field_icon1_alpha;
450 float hud_field_icon2_alpha;
451 string HUD_GetField(entity pl, float field)
452 {
453         float tmp, num, denom, f;
454         string str;
455         hud_field_rgb = '1 1 1';
456         hud_field_icon0 = "";
457         hud_field_icon1 = "";
458         hud_field_icon2 = "";
459         hud_field_icon0_rgb = '1 1 1';
460         hud_field_icon1_rgb = '1 1 1';
461         hud_field_icon2_rgb = '1 1 1';
462         hud_field_icon0_alpha = 1;
463         hud_field_icon1_alpha = 1;
464         hud_field_icon2_alpha = 1;
465         switch(field)
466         {
467                 case SP_PING:
468                         if not(pl.gotscores)
469                                 return "\x8D\x8D\x8D"; // >>> sign
470                         //str = getplayerkey(pl.sv_entnum, "ping");
471                         f = pl.ping;
472                         if(f == 0)
473                                 return "N/A";
474                         tmp = max(0, min(220, f-80)) / 220;
475                         hud_field_rgb = '1 1 1' - '0 1 1'*tmp;
476                         return ftos(f);
477
478                 case SP_PL:
479                         if not(pl.gotscores)
480                                 return "N/A";
481                         f = pl.ping_packetloss;
482                         tmp = pl.ping_movementloss;
483                         if(f == 0 && tmp == 0)
484                                 return "";
485                         str = ftos(ceil(f * 100));
486                         if(tmp != 0)
487                                 str = strcat(str, "~", ftos(ceil(tmp * 100)));
488                         tmp = bound(0, f / 0.2 + tmp / 0.04, 1); // 20% is REALLY BAD pl
489                         hud_field_rgb = '1 0.5 0.5' - '0 0.5 0.5'*tmp;
490                         return str;
491
492                 case SP_NAME:
493                         if(ready_waiting && pl.ready)
494                         {
495                                 hud_field_icon0 = "gfx/scoreboard/player_ready";
496                         }
497                         else if(!teamplay)
498                         {
499                                 f = stof(getplayerkey(pl.sv_entnum, "colors"));
500                                 {
501                                         hud_field_icon0 = "gfx/scoreboard/playercolor_base";
502                                         hud_field_icon1 = "gfx/scoreboard/playercolor_shirt";
503                                         hud_field_icon1_rgb = colormapPaletteColor(floor(f / 16), 0);
504                                         hud_field_icon2 = "gfx/scoreboard/playercolor_pants";
505                                         hud_field_icon2_rgb = colormapPaletteColor(mod(f, 16), 1);
506                                 }
507                         }
508                         return GetPlayerName(pl.sv_entnum);
509
510                 case SP_FRAGS:
511                         f = pl.(scores[SP_KILLS]);
512                         f -= pl.(scores[SP_SUICIDES]);
513                         return ftos(f);
514
515                 case SP_KDRATIO:
516                         num = pl.(scores[SP_KILLS]);
517                         denom = pl.(scores[SP_DEATHS]);
518
519                         if(denom == 0) {
520                                 hud_field_rgb = '0 1 0';
521                                 str = ftos(num);
522                         } else if(num <= 0) {
523                                 hud_field_rgb = '1 0 0';
524                                 str = ftos(num/denom);
525                         } else
526                                 str = ftos(num/denom);
527
528                         tmp = strstrofs(str, ".", 0);
529                         if(tmp > 0)
530                                 str = substring(str, 0, tmp+2);
531                         return str;
532
533                 default:
534                         tmp = pl.(scores[field]);
535                         f = scores_flags[field];
536                         if(field == ps_primary)
537                                 hud_field_rgb = '1 1 0';
538                         else if(field == ps_secondary)
539                                 hud_field_rgb = '0 1 1';
540                         else
541                                 hud_field_rgb = '1 1 1';
542                         return ScoreString(f, tmp);
543         }
544         //return "error";
545 }
546
547 float xmin, xmax, ymin, ymax, sbwidth;
548 float hud_fixscoreboardcolumnwidth_len;
549 float hud_fixscoreboardcolumnwidth_iconlen;
550 float hud_fixscoreboardcolumnwidth_marginlen;
551
552 string HUD_FixScoreboardColumnWidth(float i, string str)
553 {
554         float field, f;
555         vector sz;
556         field = hud_field[i];
557
558         hud_fixscoreboardcolumnwidth_iconlen = 0;
559
560         if(hud_field_icon0 != "")
561         {
562                 sz = drawgetimagesize(hud_field_icon0);
563                 f = sz_x / sz_y;
564                 if(hud_fixscoreboardcolumnwidth_iconlen < f)
565                         hud_fixscoreboardcolumnwidth_iconlen = f;
566         }
567
568         if(hud_field_icon1 != "")
569         {
570                 sz = drawgetimagesize(hud_field_icon1);
571                 f = sz_x / sz_y;
572                 if(hud_fixscoreboardcolumnwidth_iconlen < f)
573                         hud_fixscoreboardcolumnwidth_iconlen = f;
574         }
575
576         if(hud_field_icon2 != "")
577         {
578                 sz = drawgetimagesize(hud_field_icon2);
579                 f = sz_x / sz_y;
580                 if(hud_fixscoreboardcolumnwidth_iconlen < f)
581                         hud_fixscoreboardcolumnwidth_iconlen = f;
582         }
583
584         hud_fixscoreboardcolumnwidth_iconlen *= hud_fontsize_y / hud_fontsize_x; // fix icon aspect
585
586         if(hud_fixscoreboardcolumnwidth_iconlen != 0)
587                 hud_fixscoreboardcolumnwidth_marginlen = stringwidth(" ", FALSE, hud_fontsize);
588         else
589                 hud_fixscoreboardcolumnwidth_marginlen = 0;
590
591         if(field == SP_NAME) // name gets all remaining space
592         {
593                 float namesize, j;
594                 namesize = sbwidth;// / hud_fontsize_x;
595                 for(j = 0; j < hud_num_fields; ++j)
596                         if(j != i)
597                                 if (hud_field[i] != SP_SEPARATOR)
598                                         namesize -= hud_size[j] + hud_fontsize_x;
599                 namesize += hud_fontsize_x;
600                 hud_size[i] = namesize;
601
602                 if (hud_fixscoreboardcolumnwidth_iconlen != 0)
603                         namesize -= hud_fixscoreboardcolumnwidth_marginlen + hud_fixscoreboardcolumnwidth_iconlen;
604                 str = textShortenToWidth(str, namesize, hud_fontsize, stringwidth_colors);
605                 hud_fixscoreboardcolumnwidth_len = stringwidth(str, TRUE, hud_fontsize);
606         }
607         else
608                 hud_fixscoreboardcolumnwidth_len = stringwidth(str, FALSE, hud_fontsize);
609
610         f = hud_fixscoreboardcolumnwidth_len + hud_fixscoreboardcolumnwidth_marginlen + hud_fixscoreboardcolumnwidth_iconlen;
611         if(hud_size[i] < f)
612                 hud_size[i] = f;
613
614         return str;
615 }
616
617 void HUD_PrintScoreboardItem(vector pos, entity pl, float is_self, float pl_number)
618 {
619         vector tmp, rgb;
620         rgb = GetTeamRGB(pl.team);
621         string str;
622         float i, field;
623         float is_spec;
624         is_spec = (GetPlayerColor(pl.sv_entnum) == COLOR_SPECTATOR);
625
626         if((rgb == '1 1 1') && (!is_spec)) {
627                 rgb_x = cvar("scoreboard_color_bg_r") + 0.5;
628                 rgb_y = cvar("scoreboard_color_bg_g") + 0.5;
629                 rgb_z = cvar("scoreboard_color_bg_b") + 0.5; }
630
631         // Layout:
632         tmp_x = sbwidth;
633         tmp_y = hud_fontsize_y * 1.25;
634         tmp_z = 0;
635
636         // alternated rows highlighting
637         if(is_self)
638                 drawfill(pos - '1 1 0', tmp + '2 0 0', rgb, scoreboard_highlight_alpha_self, DRAWFLAG_NORMAL);
639         else if((scoreboard_highlight) && (!mod(pl_number,2)))
640                 drawfill(pos - '1 1 0', tmp + '2 0 0', rgb, scoreboard_highlight_alpha, DRAWFLAG_NORMAL);
641
642         tmp_y = 0;
643
644         for(i = 0; i < hud_num_fields; ++i)
645         {
646                 field = hud_field[i];
647                 if(field == SP_SEPARATOR)
648                         break;
649
650                 if(is_spec && field != SP_NAME && field != SP_PING) {
651                         pos_x += hud_size[i] + hud_fontsize_x;
652                         continue;
653                 }
654                 str = HUD_GetField(pl, field);
655                 str = HUD_FixScoreboardColumnWidth(i, str);
656
657                 pos_x += hud_size[i] + hud_fontsize_x;
658
659                 if(field == SP_NAME) {
660                         tmp_x = hud_size[i] - hud_fontsize_x*hud_fixscoreboardcolumnwidth_iconlen - hud_fixscoreboardcolumnwidth_marginlen + hud_fontsize_x;
661                         if (is_self)
662                                 drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
663                         else
664                                 drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name, DRAWFLAG_NORMAL);
665                 } else {
666                         tmp_x = hud_fixscoreboardcolumnwidth_len + hud_fontsize_x;
667                         if (is_self)
668                                 drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
669                         else
670                                 drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name, DRAWFLAG_NORMAL);
671                 }
672
673                 tmp_x = hud_size[i] + hud_fontsize_x;
674                 if(hud_field_icon0 != "")
675                         if (is_self)
676                                 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);
677                         else
678                                 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);
679                 if(hud_field_icon1 != "")
680                         if (is_self)
681                                 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);
682                         else
683                                 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);
684                 if(hud_field_icon2 != "")
685                         if (is_self)
686                                 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);
687                         else
688                                 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);
689         }
690
691         if(hud_field[i] == SP_SEPARATOR)
692         {
693                 pos_x = xmax;
694                 for(i = hud_num_fields-1; i > 0; --i)
695                 {
696                         field = hud_field[i];
697                         if(field == SP_SEPARATOR)
698                                 break;
699
700                         if(is_spec && field != SP_NAME && field != SP_PING) {
701                                 pos_x -= hud_size[i] + hud_fontsize_x;
702                                 continue;
703                         }
704
705                         str = HUD_GetField(pl, field);
706                         str = HUD_FixScoreboardColumnWidth(i, str);
707
708                         if(field == SP_NAME) {
709                                 tmp_x = hud_fixscoreboardcolumnwidth_len; // left or right aligned? let's put it right...
710                                 if(is_self)
711                                         drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
712                                 else
713                                         drawcolorcodedstring(pos - tmp, str, hud_fontsize, scoreboard_alpha_name, DRAWFLAG_NORMAL);
714                         } else {
715                                 tmp_x = hud_fixscoreboardcolumnwidth_len;
716                                 if(is_self)
717                                         drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name_self, DRAWFLAG_NORMAL);
718                                 else
719                                         drawstring(pos - tmp, str, hud_fontsize, hud_field_rgb, scoreboard_alpha_name, DRAWFLAG_NORMAL);
720                         }
721
722                         tmp_x = hud_size[i];
723                         if(hud_field_icon0 != "")
724                                 if (is_self)
725                                         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);
726                                 else
727                                         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);
728                         if(hud_field_icon1 != "")
729                                 if (is_self)
730                                         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);
731                                 else
732                                         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);
733                         if(hud_field_icon2 != "")
734                                 if (is_self)
735                                         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);
736                                 else
737                                         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);
738                         pos_x -= hud_size[i] + hud_fontsize_x;
739                 }
740         }
741 }
742
743 /*
744  * HUD_Scoreboard_MakeTable
745  *
746  * Makes a table for a team (for all playing players in DM) and fills it
747  */
748
749 vector HUD_Scoreboard_MakeTable(vector pos, entity tm, vector rgb, vector bg_size)
750 {
751         float body_table_height, i;
752         vector tmp, column_dim;
753         entity pl;
754
755         body_table_height = 1.25 * hud_fontsize_y * max(1, tm.team_size); // no player? show 1 empty line
756
757         pos -= '1 1 0';
758
759         tmp_x = sbwidth + 2;
760         tmp_y = 1.25 * hud_fontsize_y;
761
762         // rounded header
763         if (teamplay)
764                 drawpic(pos, "gfx/scoreboard/scoreboard_tableheader", tmp, (rgb * autocvar_scoreboard_color_bg_team) + '0.5 0.5 0.5', scoreboard_alpha_bg, DRAWFLAG_NORMAL);
765         else
766                 drawpic(pos, "gfx/scoreboard/scoreboard_tableheader", tmp, rgb + '0.5 0.5 0.5', scoreboard_alpha_bg, DRAWFLAG_NORMAL);
767
768         // table border
769         tmp_y += autocvar_scoreboard_border_thickness;
770         tmp_y += body_table_height;
771         drawborderlines(autocvar_scoreboard_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg, DRAWFLAG_NORMAL); // more transparency for the scoreboard
772
773         // separator header/table
774         pos_y += 1.25 * hud_fontsize_y;
775         tmp_y = autocvar_scoreboard_border_thickness;
776         drawfill(pos, tmp, '0 0 0', scoreboard_alpha_bg, DRAWFLAG_NORMAL);
777
778         pos_y += autocvar_scoreboard_border_thickness;
779
780         // table background
781         tmp_y = body_table_height;
782         if (teamplay)
783                 drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * autocvar_scoreboard_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
784         else
785                 drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
786
787         // anyway, apply some color
788         //drawfill(pos, tmp + '2 0 0', rgb, 0.1, DRAWFLAG_NORMAL);
789
790         // go back to the top to make alternated columns highlighting and to print the strings
791         pos_y -= 1.25 * hud_fontsize_y;
792         pos_y -= autocvar_scoreboard_border_thickness;
793
794         pos += '1 1 0';
795
796         if (scoreboard_highlight)
797         {
798                 column_dim_y = 1.25 * hud_fontsize_y; // header
799                 column_dim_y += autocvar_scoreboard_border_thickness;
800                 column_dim_y += body_table_height;
801         }
802
803         // print the strings of the columns headers and draw the columns
804         for(i = 0; i < hud_num_fields; ++i)
805         {
806                 if(hud_field[i] == SP_SEPARATOR)
807                         break;
808                 column_dim_x = hud_size[i] + hud_fontsize_x;
809                 if (scoreboard_highlight)
810                 {
811                         if (mod(i,2))
812                                 drawfill(pos - '0 1 0' - hud_fontsize_x / 2 * '1 0 0', column_dim, '0 0 0', scoreboard_alpha_bg * 0.2, DRAWFLAG_NORMAL);
813                 }
814                 drawstring(pos, hud_title[i], hud_fontsize, rgb * 1.5, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
815                 pos_x += column_dim_x;
816         }
817         if(hud_field[i] == SP_SEPARATOR)
818         {
819                 pos_x = xmax;
820                 tmp_y = 0;
821                 for(i = hud_num_fields-1; i > 0; --i)
822                 {
823                         if(hud_field[i] == SP_SEPARATOR)
824                                 break;
825
826                         pos_x -= hud_size[i];
827
828                         if (scoreboard_highlight)
829                         {
830                                 if (!mod(i,2))
831                                 {
832                                         if (i == hud_num_fields-1)
833                                                 column_dim_x = hud_size[i] + hud_fontsize_x / 2 + 1;
834                                         else
835                                                 column_dim_x = hud_size[i] + hud_fontsize_x;
836                                         drawfill(pos - '0 1 0' - hud_fontsize_x / 2 * '1 0 0', column_dim, '0 0 0', scoreboard_alpha_bg * 0.2, DRAWFLAG_NORMAL);
837                                 }
838                         }
839
840                         tmp_x = stringwidth(hud_title[i], FALSE, hud_fontsize);
841                         tmp_x = (hud_size[i] - tmp_x);
842                         drawstring(pos + tmp, hud_title[i], hud_fontsize, rgb * 1.5, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
843                         pos_x -= hud_fontsize_x;
844                 }
845         }
846
847         pos_x = xmin;
848         pos_y += 1.25 * hud_fontsize_y; // skip the header
849         pos_y += autocvar_scoreboard_border_thickness;
850
851         // fill the table and draw the rows
852         i = 0;
853         if (teamplay)
854                 for(pl = players.sort_next; pl; pl = pl.sort_next)
855                 {
856                         if(pl.team != tm.team)
857                                 continue;
858                         HUD_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1), i);
859                         pos_y += 1.25 * hud_fontsize_y;
860                         ++i;
861                 }
862         else
863                 for(pl = players.sort_next; pl; pl = pl.sort_next)
864                 {
865                         if(pl.team == COLOR_SPECTATOR)
866                                 continue;
867                         HUD_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1), i);
868                         pos_y += 1.25 * hud_fontsize_y;
869                         ++i;
870                 }
871
872         if (i == 0)
873                 pos_y += 1.25 * hud_fontsize_y; // move to the end of the table
874         pos_y += 1.25 * hud_fontsize_y; // move empty row (out of the table)
875
876         return pos;
877 }
878
879 float HUD_WouldDrawScoreboard() {
880         if (autocvar__hud_configure)
881                 return 0;
882         else if (scoreboard_showscores)
883                 return 1;
884         else if (intermission == 1)
885                 return 1;
886         else if (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard"))
887                 return 1;
888         else if (scoreboard_showscores_force)
889                 return 1;
890         return 0;
891 }
892
893 float g_minstagib;
894 float average_accuracy;
895 vector HUD_DrawScoreboardAccuracyStats(vector pos, vector rgb, vector bg_size)
896 {
897         float i;
898         float weapon_cnt = WEP_COUNT - 3; // either minstanex/nex are hidden, no port-o-launch, no tuba
899         float rows;
900         if(cvar("scoreboard_accuracy_doublerows"))
901                 rows = 2;
902         else
903                 rows = 1;
904         float height = 40;
905         float fontsize = height * 1/3;
906         float weapon_height = height * 2/3;
907         float weapon_width = sbwidth / weapon_cnt;
908
909         drawstring(pos, strcat("Accuracy stats (average ", ftos(average_accuracy), "%)"), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
910         pos_y += 1.25 * hud_fontsize_y;
911         vector tmp;
912         tmp_x = sbwidth;
913         tmp_y = height * rows;
914
915         if (teamplay)
916                 drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * autocvar_scoreboard_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
917         else
918                 drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
919         drawborderlines(autocvar_scoreboard_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg * 0.75, DRAWFLAG_NORMAL);
920
921         // column highlighting
922         for(i = 0; i < weapon_cnt/rows; ++i)
923         {
924                 if(!mod(i, 2))
925                         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);
926         }
927
928         // row highlighting
929         for(i = 0; i < rows; ++i)
930         {
931                 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);
932         }
933
934         drawfont = hud_bigfont;
935         average_accuracy = 0;
936         float weapons_with_stats;
937         weapons_with_stats = 0;
938         if(rows == 2)
939                 pos_x += weapon_width / 2;
940
941         if(getstati(STAT_SWITCHWEAPON) == WEP_MINSTANEX)
942                 g_minstagib = 1; // TODO: real detection for minstagib?
943
944         float weapon_hit, weapon_damage, weapon_stats, weapon_number;
945         // hits
946         weapon_stats = getstati(STAT_DAMAGE_HITS);
947         weapon_number = weapon_stats & 63;
948         weapon_hits[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
949         // fired
950         weapon_stats = getstati(STAT_DAMAGE_FIRED);
951         weapon_number = weapon_stats & 63;
952         weapon_fired[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
953
954         if (!acc_levels)
955                 rgb = '1 1 1';
956         else if (acc_col_x[0] == -1)
957                 for (i = 0; i < acc_levels; ++i)
958                         acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
959
960         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
961         {
962                 self = get_weaponinfo(i);
963                 if not(self.weapons)
964                         continue;
965                 if ((i == WEP_NEX && g_minstagib) || i == WEP_PORTO || (i == WEP_MINSTANEX && !g_minstagib) || i == WEP_TUBA) // skip port-o-launch, nex || minstanex and tuba
966                         continue;
967                 weapon_hit = weapon_hits[i-WEP_FIRST];
968                 weapon_damage = weapon_fired[i-WEP_FIRST];
969
970                 float weapon_alpha;
971                 if(weapon_damage)
972                 {
973                         weapon_stats = bound(0, floor(100 * weapon_hit / weapon_damage), 100);
974                         weapon_alpha = scoreboard_alpha_fg;
975                 }
976                 else
977                         weapon_alpha = 0.2 * scoreboard_alpha_fg;
978
979                 // weapon icon
980                 drawpic_aspect_skin(pos, strcat("weapon", self.netname), '1 0 0' * weapon_width + '0 1 0' * weapon_height, '1 1 1', weapon_alpha, DRAWFLAG_NORMAL);
981                 // the accuracy
982                 if(weapon_damage) {
983                         weapons_with_stats += 1;
984                         average_accuracy += weapon_stats; // store sum of all accuracies in average_accuracy
985
986                         string s;
987                         s = strcat(ftos(weapon_stats),"%");
988
989                         float padding;
990                         padding = (weapon_width - stringwidth(s, FALSE, '1 0 0' * fontsize)) / 2; // center the accuracy value
991
992                         weapon_damage = weapon_fired[self.weapon-WEP_FIRST];
993                         if(weapon_damage)
994                         {
995                                 weapon_hit = weapon_hits[self.weapon-WEP_FIRST];
996                                 weapon_stats = floor(100 * weapon_hit / weapon_damage);
997                         }
998
999                         if (acc_levels)
1000                         {
1001                                 // find the max level lower than weapon_stats
1002                                 float j;
1003                                 j = acc_levels-1;
1004                                 while ( j && weapon_stats < acc_lev[j] )
1005                                         --j;
1006
1007                                 // inject color j+1 in color j, how much depending on how much weapon_stats is higher than level j
1008                                 float factor;
1009                                 factor = (weapon_stats - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
1010                                 rgb = acc_col[j];
1011                                 rgb = rgb + factor * (acc_col[j+1] - rgb);
1012                         }
1013
1014                         drawstring(pos + '1 0 0' * padding + '0 1 0' * weapon_height, s, '1 1 0' * fontsize, rgb, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1015                 }
1016                 pos_x += weapon_width * rows;
1017                 if(rows == 2 && i == 6) {
1018                         pos_x -= sbwidth;
1019                         pos_y += height;
1020                 }
1021         }
1022         drawfont = hud_font;
1023
1024         if(weapons_with_stats)
1025                 average_accuracy = floor(average_accuracy / weapons_with_stats);
1026
1027         if(rows == 2)
1028                 pos_x -= weapon_width / 2;
1029         pos_x -= sbwidth;
1030         pos_y += height;
1031
1032         pos_y +=  1.25 * hud_fontsize_y;
1033         return pos;
1034 }
1035
1036 vector HUD_DrawScoreboardRankings(vector pos, entity pl,  vector rgb, vector bg_size)
1037 {
1038         float i;
1039         RANKINGS_RECEIVED_CNT = 0;
1040         for (i=RANKINGS_CNT-1; i>=0; --i)
1041                 if (grecordtime[i])
1042                         ++RANKINGS_RECEIVED_CNT;
1043
1044         if (RANKINGS_RECEIVED_CNT == 0)
1045                 return pos;
1046
1047         float is_spec;
1048         is_spec = (GetPlayerColor(pl.sv_entnum) == COLOR_SPECTATOR);
1049         vector hl_rgb;
1050         hl_rgb_x = cvar("scoreboard_color_bg_r") + 0.5;
1051         hl_rgb_y = cvar("scoreboard_color_bg_g") + 0.5;
1052         hl_rgb_z = cvar("scoreboard_color_bg_b") + 0.5;
1053
1054         pos_y += hud_fontsize_y;
1055         drawstring(pos, strcat("Rankings"), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1056         pos_y += hud_fontsize_y;
1057         vector tmp;
1058         tmp_x = sbwidth;
1059         tmp_y = 1.25 * hud_fontsize_y * RANKINGS_RECEIVED_CNT;
1060
1061         if (teamplay)
1062                 drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb * autocvar_scoreboard_color_bg_team, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
1063         else
1064                 drawpic_tiled(pos, "gfx/scoreboard/scoreboard_bg", bg_size, tmp, rgb, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
1065         drawborderlines(autocvar_scoreboard_border_thickness, pos, tmp, '0 0 0', scoreboard_alpha_bg * 0.75, DRAWFLAG_NORMAL);
1066
1067         // row highlighting
1068         for(i = 0; i<RANKINGS_RECEIVED_CNT; ++i)
1069         {
1070                 string n, p;
1071                 float t;
1072                 t = grecordtime[i];
1073                 if (t == 0)
1074                         continue;
1075                 n = grecordholder[i];
1076                 p = race_PlaceName(i+1);
1077                 if(grecordholder[i] == GetPlayerName(player_localentnum - 1))
1078                         drawfill(pos, '1 0 0' * sbwidth + '0 1.25 0' * hud_fontsize_y, hl_rgb, scoreboard_highlight_alpha_self, DRAWFLAG_NORMAL);
1079                 else if(!mod(i, 2) && scoreboard_highlight)
1080                         drawfill(pos, '1 0 0' * sbwidth + '0 1.25 0' * hud_fontsize_y, hl_rgb, scoreboard_highlight_alpha, DRAWFLAG_NORMAL);
1081                 drawstring(pos, p, '1 1 0' * hud_fontsize_y, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1082                 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);
1083                 drawcolorcodedstring(pos + '8 0 0' * hud_fontsize_y, n, '1 1 0' * hud_fontsize_y, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1084                 pos_y += 1.25 * hud_fontsize_y;
1085         }
1086
1087         return pos;
1088 }
1089
1090 float hud_woulddrawscoreboard_prev;
1091 float hud_woulddrawscoreboard_change; // "time" at which HUD_WouldDrawScoreboard() changed
1092 void HUD_DrawScoreboard()
1093 {
1094         float hud_woulddrawscoreboard;
1095         hud_woulddrawscoreboard = scoreboard_active;
1096         if(hud_woulddrawscoreboard != hud_woulddrawscoreboard_prev) {
1097                 hud_woulddrawscoreboard_change = time;
1098                 hud_woulddrawscoreboard_prev = hud_woulddrawscoreboard;
1099         }
1100
1101         if(hud_woulddrawscoreboard) {
1102                 float scoreboard_fadeinspeed = cvar_or("scoreboard_fadeinspeed", 10);
1103                 if (scoreboard_fadeinspeed)
1104                         scoreboard_fade_alpha = bound (0, (time - hud_woulddrawscoreboard_change) * scoreboard_fadeinspeed, 1);
1105                 else
1106                         scoreboard_fade_alpha = 1;
1107         }
1108         else {
1109                 float scoreboard_fadeoutspeed = cvar_or("scoreboard_fadeoutspeed", 5);
1110                 if (scoreboard_fadeoutspeed)
1111                         scoreboard_fade_alpha = bound (0, (1/scoreboard_fadeoutspeed - (time - hud_woulddrawscoreboard_change)) * scoreboard_fadeoutspeed, 1);
1112                 else
1113                         scoreboard_fade_alpha = 0;
1114         }
1115
1116         if not(scoreboard_fade_alpha)
1117                 return;
1118
1119         HUD_UpdatePlayerTeams();
1120
1121         scoreboard_alpha_bg = cvar("scoreboard_alpha_bg") * scoreboard_fade_alpha * (1 - autocvar__menu_alpha);
1122         scoreboard_alpha_fg = cvar_or("scoreboard_alpha_fg", 1.0) * scoreboard_fade_alpha * (1 - autocvar__menu_alpha);
1123         scoreboard_highlight = cvar("scoreboard_highlight");
1124         scoreboard_highlight_alpha = cvar_or("scoreboard_highlight_alpha", 0.10) * scoreboard_alpha_fg;
1125         scoreboard_highlight_alpha_self = cvar_or("scoreboard_highlight_alpha_self", 0.25) * scoreboard_alpha_fg;
1126         scoreboard_alpha_name = cvar_or("scoreboard_alpha_name", 0.9) * scoreboard_alpha_fg;
1127         scoreboard_alpha_name_self = cvar_or("scoreboard_alpha_name_self", 1) * scoreboard_alpha_fg;
1128
1129         vector rgb, pos, tmp;
1130         entity pl, tm;
1131
1132         xmin = cvar("scoreboard_offset_left") * vid_conwidth;
1133         ymin = cvar("con_notify") * cvar("con_notifysize");
1134
1135         xmax = (1 - cvar("scoreboard_offset_right")) * vid_conwidth;
1136         ymax = vid_conheight - ymin;
1137
1138         sbwidth = xmax - xmin;
1139
1140         // Initializes position
1141         pos_x = xmin;
1142         pos_y = ymin;
1143         pos_z = 0;
1144
1145         // Heading
1146         drawfont = hud_bigfont;
1147         drawstring(pos, "Scoreboard", '24 24 0', '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1148         
1149         centerprint_start_x = vid_conwidth - 0.5 * (pos_x + stringwidth("Scoreboard", FALSE, '24 24 0'));
1150         centerprint_start_y = pos_y;
1151
1152         pos_y += 24;
1153
1154         drawfont = hud_font;
1155
1156         // Draw the scoreboard
1157         vector bg_size = drawgetimagesize("gfx/scoreboard/scoreboard_bg") * cvar("scoreboard_bg_scale");
1158
1159         if(teamplay)
1160         {
1161                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
1162                 {
1163                         if(tm.team == COLOR_SPECTATOR)
1164                                 continue;
1165
1166                         rgb = GetTeamRGB(tm.team);
1167                         drawstring(pos - '2 0 0' * hud_fontsize_x + '0 1 0' * hud_fontsize_y, ftos(tm.(teamscores[ts_primary])), '1 1 0' * hud_fontsize_y * 1.5, rgb, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1168
1169                         if(ts_primary != ts_secondary)
1170                                 drawstring(pos - '2 0 0' * hud_fontsize_x + '0 2.5 0' * hud_fontsize_y, ftos(tm.(teamscores[ts_secondary])), '1 1 0' * hud_fontsize_y * 1, rgb, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1171
1172                         pos = HUD_Scoreboard_MakeTable(pos, tm, rgb, bg_size);
1173                 }
1174         }
1175         else
1176         {
1177                 rgb_x = cvar("scoreboard_color_bg_r");
1178                 rgb_y = cvar("scoreboard_color_bg_g");
1179                 rgb_z = cvar("scoreboard_color_bg_b");
1180
1181                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
1182                 {
1183                         if(tm.team == COLOR_SPECTATOR)
1184                                 continue;
1185
1186                         pos = HUD_Scoreboard_MakeTable(pos, tm, rgb, bg_size);
1187                 }
1188         }
1189
1190         if(gametype == GAME_CTS || gametype == GAME_RACE) {
1191                 if(race_speedaward) {
1192                         drawcolorcodedstring(pos, strcat("Speed award: ", ftos(race_speedaward), " ^7(", race_speedaward_holder, "^7)"), hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1193                         pos_y += 1.25 * hud_fontsize_y;
1194                 }
1195                 if(race_speedaward_alltimebest) {
1196                         drawcolorcodedstring(pos, strcat("All-time fastest: ", ftos(race_speedaward_alltimebest), " ^7(", race_speedaward_alltimebest_holder, "^7)"), hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1197                         pos_y += 1.25 * hud_fontsize_y;
1198                 }
1199                 pos = HUD_DrawScoreboardRankings(pos, pl, rgb, bg_size);
1200         }
1201         else if(cvar("scoreboard_accuracy") && spectatee_status != -1 && !warmup_stage) {
1202                 if(teamplay)
1203                         pos = HUD_DrawScoreboardAccuracyStats(pos, GetTeamRGB(myteam), bg_size);
1204                 else
1205                         pos = HUD_DrawScoreboardAccuracyStats(pos, rgb, bg_size);
1206         }
1207
1208         // List spectators
1209         float specs;
1210         specs = 0;
1211         tmp = pos;
1212         for(pl = players.sort_next; pl; pl = pl.sort_next)
1213         {
1214                 if(pl.team != COLOR_SPECTATOR)
1215                         continue;
1216                 pos_y += 1.25 * hud_fontsize_y;
1217                 HUD_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1), specs);
1218                 ++specs;
1219         }
1220
1221         if(specs)
1222         {
1223                 drawstring(tmp, "Spectators", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1224                 pos_y += 1.25 * hud_fontsize_y;
1225         }
1226
1227         // Print info string
1228         string str;
1229         float tl, fl, ll;
1230         str = strcat("playing on ^2", shortmapname, "^7");
1231         tl = getstatf(STAT_TIMELIMIT);
1232         fl = getstatf(STAT_FRAGLIMIT);
1233         ll = getstatf(STAT_LEADLIMIT);
1234         if(gametype == GAME_LMS)
1235         {
1236                 if(tl > 0)
1237                         str = strcat(str, " for up to ^1", ftos(tl), " minutes^7");
1238         }
1239         else
1240         {
1241                 if(tl > 0)
1242                         str = strcat(str, " for ^1", ftos(tl), " minutes^7");
1243                 if(fl > 0)
1244                 {
1245                         if(tl > 0)
1246                                 str = strcat(str, " or");
1247                         if(teamplay)
1248                         {
1249                                 str = strcat(str, " until ^3", ScoreString(teamscores_flags[ts_primary], fl));
1250                                 if(teamscores_label[ts_primary] == "score")
1251                                         str = strcat(str, " points^7");
1252                                 else if(teamscores_label[ts_primary] == "fastest")
1253                                         str = strcat(str, " is beaten^7");
1254                                 else
1255                                         str = strcat(str, " ", teamscores_label[ts_primary]);
1256                         }
1257                         else
1258                         {
1259                                 str = strcat(str, " until ^3", ScoreString(scores_flags[ps_primary], fl));
1260                                 if(scores_label[ps_primary] == "score")
1261                                         str = strcat(str, " points^7");
1262                                 else if(scores_label[ps_primary] == "fastest")
1263                                         str = strcat(str, " is beaten^7");
1264                                 else
1265                                         str = strcat(str, " ", scores_label[ps_primary]);
1266                         }
1267                 }
1268                 if(ll > 0)
1269                 {
1270                         if(tl > 0 || fl > 0)
1271                                 str = strcat(str, " or");
1272                         if(teamplay)
1273                         {
1274                                 str = strcat(str, " until a lead of ^3", ScoreString(teamscores_flags[ts_primary], ll));
1275                                 if(teamscores_label[ts_primary] == "score")
1276                                         str = strcat(str, " points^7");
1277                                 else if(teamscores_label[ts_primary] == "fastest")
1278                                         str = strcat(str, " is beaten^7");
1279                                 else
1280                                         str = strcat(str, " ", teamscores_label[ts_primary]);
1281                         }
1282                         else
1283                         {
1284                                 str = strcat(str, " until a lead of ^3", ScoreString(scores_flags[ps_primary], ll));
1285                                 if(scores_label[ps_primary] == "score")
1286                                         str = strcat(str, " points^7");
1287                                 else if(scores_label[ps_primary] == "fastest")
1288                                         str = strcat(str, " is beaten^7");
1289                                 else
1290                                         str = strcat(str, " ", scores_label[ps_primary]);
1291                         }
1292                 }
1293         }
1294
1295
1296         pos_y += 1.2 * hud_fontsize_y;
1297         drawcolorcodedstring(pos + '0.5 0 0' * (sbwidth - stringwidth(str, TRUE, hud_fontsize)), str, hud_fontsize, scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1298
1299         scoreboard_bottom = pos_y + 2 * hud_fontsize_y;
1300 }
1301
1302 void HUD_DrawAccuracyStats_Description_Hitscan(vector position)
1303 {
1304         drawstring(position + '0 3 0' * hud_fontsize_y, "Shots fired:", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1305         drawstring(position + '0 5 0' * hud_fontsize_y, "Shots hit:", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1306         drawstring(position + '0 7 0' * hud_fontsize_y, "Accuracy:", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1307         drawstring(position + '0 9 0' * hud_fontsize_y, "Shots missed:", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1308 }
1309
1310 void HUD_DrawAccuracyStats_Description_Splash(vector position)
1311 {
1312         drawstring(position + '0 3 0' * hud_fontsize_y, "Maximum damage:", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1313         drawstring(position + '0 5 0' * hud_fontsize_y, "Actual damage:", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1314         drawstring(position + '0 7 0' * hud_fontsize_y, "Accuracy:", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1315         drawstring(position + '0 9 0' * hud_fontsize_y, "Damage wasted:", hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1316 }
1317
1318 void HUD_DrawAccuracyStats()
1319 {
1320         float i, count_hitscan, count_splash, row;  // count is the number of 'colums'
1321         float weapon_hit, weapon_damage, weapon_stats;
1322         float left_border;  // position where the weapons start, the description is in the border
1323         vector fill_colour, fill_size;
1324         vector pos;
1325         vector border_colour;
1326
1327         float col_margin = 20;  // pixels between the columns
1328         float row_margin = 20;  // pixels between the rows
1329
1330         fill_size_x = 5 * hud_fontsize_x;  // width of the background
1331         fill_size_y = 10 * hud_fontsize_y;  // height of the background
1332
1333         drawfont = hud_bigfont;
1334         pos_x = 0;
1335         pos_y = SCOREBOARD_OFFSET;
1336         pos_z = 0;
1337         drawstringcenter(pos, "Weapon Accuracy", 2 * hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1338
1339         left_border = col_margin + 11 * hud_fontsize_x;
1340
1341         drawfont = hud_font;
1342
1343         if(warmup_stage)
1344         {
1345                 pos_y += 40;
1346                 if(mod(time, 1) >= 0.4)
1347                         drawstringcenter(pos, "Stats are not tracked during warmup stage", hud_fontsize, '1 1 0', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1348
1349                 return;
1350         }
1351
1352         if(gametype == GAME_RACE || gametype == GAME_CTS)
1353         {
1354                 pos_y += 40;
1355                 if(mod(time, 1) >= 0.4)
1356                         drawstringcenter(pos, "Stats are not tracked in Race/CTS", hud_fontsize, '1 1 0', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1357
1358                 return;
1359         }
1360
1361         float top_border_hitscan = SCOREBOARD_OFFSET + 55;  // position where the hitscan row starts: pixels down the screen
1362         HUD_DrawAccuracyStats_Description_Hitscan('1 0 0' * col_margin + '0 1 0' * top_border_hitscan);
1363
1364         float top_border_splash = SCOREBOARD_OFFSET + 175;  // position where the splash row starts: pixels down the screen
1365         HUD_DrawAccuracyStats_Description_Splash('1 0 0' * col_margin + '0 1 0' * top_border_splash);
1366
1367         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1368         {
1369                 self = get_weaponinfo(i);
1370                 if not(self.weapons)
1371                         continue;
1372                 weapon_hit = weapon_hits[i-WEP_FIRST];
1373                 weapon_damage = weapon_fired[i-WEP_FIRST];
1374                 border_colour = (i == activeweapon) ? '1 1 1' : '0 0 0';  // white or black border
1375
1376                 if (weapon_damage) {
1377                         if (self.spawnflags & WEP_TYPE_SPLASH) {
1378                                 weapon_stats = bound(0, floor(100 * weapon_hit / weapon_damage), 100);
1379
1380                                 fill_colour_x = 1 - 0.015 * weapon_stats;
1381                                 fill_colour_y = 1 - 0.015 * (100 - weapon_stats);
1382
1383                                 // how the background colour is calculated
1384                                 // %    red             green   red_2                   green_2
1385                                 // 0    1               0               1 - % * 0.015   1 - (100 - %) * 0.015
1386                                 // 10   0.85    0               1 - % * 0.015   1 - (100 - %) * 0.015
1387                                 // 20   0.70    0               1 - % * 0.015   1 - (100 - %) * 0.015
1388                                 // 30   0.55    0               1 - % * 0.015   1 - (100 - %) * 0.015
1389                                 // 40   0.40    0.10    1 - % * 0.015   1 - (100 - %) * 0.015
1390                                 // 50   0.25    0.25    1 - % * 0.015   1 - (100 - %) * 0.015
1391                                 // 60   0.10    0.40    1 - % * 0.015   1 - (100 - %) * 0.015
1392                                 // 70   0               0.55    1 - % * 0.015   1 - (100 - %) * 0.015
1393                                 // 80   0               0.70    1 - % * 0.015   1 - (100 - %) * 0.015
1394                                 // 90   0               0.85    1 - % * 0.015   1 - (100 - %) * 0.015
1395                                 // 100  0               1               1 - % * 0.015   1 - (100 - %) * 0.015
1396
1397                                 if ((left_border + count_splash * (fill_size_x + col_margin) + fill_size_x) >= vid_conwidth)
1398                                 {
1399                                         count_splash = 0;
1400                                         ++row;
1401                                         HUD_DrawAccuracyStats_Description_Splash('1 0 0' * col_margin + '0 1 0' * (top_border_splash + row * (fill_size_y + row_margin)));
1402                                 }
1403
1404                                 pos_x = left_border + count_splash * (fill_size_x + col_margin);
1405                                 pos_y = top_border_splash + row * (fill_size_y + row_margin);
1406
1407                                 // background
1408                                 drawpic(pos, "gfx/scoreboard/accuracy_bg", fill_size , fill_colour, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
1409                                 drawborderlines(autocvar_scoreboard_border_thickness, pos, fill_size, border_colour, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
1410
1411                                 // the weapon
1412                                 drawpic(pos, strcat("gfx/weapons/weapon", self.netname), '1 0.5 0' * fill_size_x , '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1413
1414                                 // the amount of shots fired or max damage
1415                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 3 0' * hud_fontsize_y, ftos(weapon_damage), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1416
1417                                 // the amount of hits or actual damage
1418                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 5 0' * hud_fontsize_y, ftos(weapon_hit), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1419
1420                                 // the accuracy
1421                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 7 0' * hud_fontsize_y, strcat(ftos(weapon_stats),"%"), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1422
1423                                 // the amount of shots missed or damage wasted
1424                                 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', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1425
1426                                 ++count_splash;
1427                         } else if (self.spawnflags & WEP_TYPE_HITSCAN) {
1428                                 weapon_stats = bound(0, floor(100 * weapon_hit / weapon_damage), 100);
1429
1430                                 fill_colour_x = 1 - 0.015 * weapon_stats;
1431                                 fill_colour_y = 1 - 0.015 * (100 - weapon_stats);
1432
1433                                 // how the background colour is calculated
1434                                 // %    red             green   red_2                   green_2
1435                                 // 0    1               0               1 - % * 0.015   1 - (100 - %) * 0.015
1436                                 // 10   0.850   0               1 - % * 0.015   1 - (100 - %) * 0.015
1437                                 // 20   0.70    0               1 - % * 0.015   1 - (100 - %) * 0.015
1438                                 // 30   0.55    0               1 - % * 0.015   1 - (100 - %) * 0.015
1439                                 // 40   0.40    0.10    1 - % * 0.015   1 - (100 - %) * 0.015
1440                                 // 50   0.25    0.25    1 - % * 0.015   1 - (100 - %) * 0.015
1441                                 // 60   0.10    0.40    1 - % * 0.015   1 - (100 - %) * 0.015
1442                                 // 70   0               0.55    1 - % * 0.015   1 - (100 - %) * 0.015
1443                                 // 80   0               0.70    1 - % * 0.015   1 - (100 - %) * 0.015
1444                                 // 90   0               0.85    1 - % * 0.015   1 - (100 - %) * 0.015
1445                                 // 100  0               1               1 - % * 0.015   1 - (100 - %) * 0.015
1446
1447                                 if ((left_border + count_hitscan * (fill_size_x + col_margin) + fill_size_x + cvar("stats_right_margin")) >= vid_conwidth)
1448                                 {
1449                                         count_hitscan = 0;
1450                                         ++row;
1451                                         HUD_DrawAccuracyStats_Description_Hitscan('1 0 0' * col_margin + '0 1 0' * (top_border_hitscan + row * (fill_size_y + row_margin)));
1452                                 }
1453
1454                                 pos_x = left_border + count_hitscan * (fill_size_x + col_margin);
1455                                 pos_y = top_border_hitscan + row * (fill_size_y + row_margin);
1456
1457                                 // background
1458                                 drawpic(pos, "gfx/scoreboard/accuracy_bg", fill_size , fill_colour, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
1459                                 drawborderlines(autocvar_scoreboard_border_thickness, pos, fill_size, border_colour, scoreboard_alpha_bg, DRAWFLAG_NORMAL);
1460
1461                                 // the weapon
1462                                 drawpic(pos, strcat("gfx/weapons/weapon", self.netname), '1 0.5 0' * fill_size_x , '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1463
1464                                 // the amount of shots fired or max damage
1465                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 3 0' * hud_fontsize_y, ftos(weapon_damage), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1466
1467                                 // the amount of hits or actual damage
1468                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 5 0' * hud_fontsize_y, ftos(weapon_hit), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1469
1470                                 // the accuracy
1471                                 drawstringright(pos + '4.5 0 0' * hud_fontsize_x + '0 7 0' * hud_fontsize_y, strcat(ftos(weapon_stats),"%"), hud_fontsize, '1 1 1', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1472
1473                                 // the amount of shots missed or damage wasted
1474                                 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', scoreboard_alpha_fg, DRAWFLAG_NORMAL);
1475
1476                                 ++count_hitscan;
1477                         }
1478                 }
1479         }
1480 }