]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/sbar.qc
sbar_color_bg_team
[divverent/nexuiz.git] / data / qcsrc / client / sbar.qc
1 float last_weapon;
2 float weapontime;
3
4 float sbar_alpha_fg;
5 float sbar_alpha_bg;
6 float sbar_hudselector;
7
8 float ps_primary, ps_secondary;
9 float ts_primary, ts_secondary;
10
11 vector sbar, color;
12 vector element_offset = '0 6 0'; // global item offset from the bottom edge
13
14 void CSQC_kh_hud();
15 void CSQC_ctf_hud();
16 void CSQC_nb_hud();
17 void MapVote_Draw();
18 void Sbar_FinaleOverlay()
19 {
20         /*vector pos;
21         pos_x = (vid_conwidth - 1)/2;
22         pos_y = 16;
23         pos_z = 0;*/
24         
25         //drawpic(pos, "gfx/finale", '0 0 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL);
26         
27         //drawstring(pos, "END", sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
28         MapVote_Draw();
29 }
30
31 float weaponspace[10];
32 float weapon_first, weapon_last;
33 void Sbar_DrawWeapon_Clear()
34 {
35         float idx;
36         weapon_first = -2;
37         weapon_last = -1;
38         for(idx = 0; idx < 10; ++idx)
39                 weaponspace[idx] = 0;
40         for(idx = 0; idx <= 23; ++idx)
41         {
42                 if(weaponimpulse[idx] >= 0)
43                 {
44                         if(weapon_first < 0)
45                                 weapon_first = idx;
46                         weapon_last = idx;
47                 }
48         }
49 }
50 void Sbar_DrawWeapon(float nr, float fade, float active)
51 {
52         vector pos, vsize;
53         float value, idx, imp, sp;
54
55         imp = weaponimpulse[nr];
56         if(imp == 0)
57                 idx = 9;
58         else
59                 idx = imp - 1;
60         
61         value = (active) ? 1 : 0.6;
62         color_x = color_y = color_z = value;
63
64         // width = 300, height = 100
65         const float w_width = 25, w_height = 12, w_space = 2, font_size = 8;
66
67         sp = weaponspace[idx] + 1;
68         weaponspace[idx] = sp;
69         
70         pos_x = (vid_conwidth + 10 - w_width * 9) * 0.5 + w_width * idx;
71         pos_y = (vid_conheight - w_height * sp) - 38; // move 38 pixels up
72         pos_z = 0;
73         vsize_x = w_width;
74         vsize_y = w_height;
75         vsize_z = 0;
76         if (active)
77                 drawpic(pos, "gfx/hud/sb_ammobg", vsize, color, value * fade * sbar_alpha_fg, 0);
78         drawpic(pos, strcat("gfx/hud/inv_weapon", ftos(nr)), vsize, color, value * fade * sbar_alpha_fg, 0);
79         pos_x += w_space;
80         pos_y += w_space;
81         vsize_x = font_size;
82         vsize_y = font_size;
83         vsize_z = 0;
84         drawstring(pos, ftos(imp), vsize, '1 1 1', sbar_alpha_fg, 0);
85 }
86
87 void Sbar_DrawXNum (vector pos, float num, float digits, float lettersize, vector rgb, float highlighted, float a, float dflags)
88 {
89         float l, i;
90         string str, tmp, l_length;
91         float minus;
92         vector vsize;
93
94         vsize_x = vsize_y = lettersize;
95         vsize_z = 0;
96
97         if(num < 0)
98         {
99                 minus = true;
100                 num = -num;
101                 pos_x -= lettersize;
102         } else
103                 minus = false;
104         
105         if(digits < 0)
106         {
107                 tmp = ftos(num);
108                 digits = -digits;
109                 str = strcat(substring("0000000000", 0, digits - strlen(tmp)), tmp);
110         } else
111                 str = ftos(num);
112         
113         l = strlen(str);
114         l_length = ftos(l);
115         
116         if(l > digits)
117         {
118                 str = substring(str, l-digits, 999);
119                 l = strlen(str);
120         } else if(l < digits)
121                 pos_x += (digits-l) * lettersize;
122
123         if (highlighted == 1) {
124                 vector hl_size;
125                 hl_size_x = vsize_x * l + vsize_x * 0.2;
126                 hl_size_y = vsize_y * 1.1;
127                 hl_size_z = 0;
128                 if(minus)
129                         hl_size_x = hl_size_x + vsize_x;
130                 
131                 vector hl_pos;
132                 hl_pos_x = pos_x - lettersize/10;
133                 hl_pos_y = pos_y - lettersize/30;
134                 hl_pos_z = 0;
135                 
136                 drawpic(hl_pos, strcat("gfx/hud/sb_highlight_", l_length), hl_size, '1 1 1', sbar_alpha_fg, dflags);
137         }
138         
139         if(minus)
140         {
141                 drawpic(pos, "gfx/hud/num_minus", vsize, rgb, a * sbar_alpha_fg, dflags);
142                 pos_x += lettersize;
143         }
144         
145         for(i = 0; i < l; ++i)
146         {
147                 drawpic(pos, strcat("gfx/hud/num_", substring(str, i, 1)), vsize, rgb, a * sbar_alpha_fg, dflags);
148                 pos_x += lettersize;
149         }
150 }
151
152 void Sbar_DrawXNum_Colored (vector pos, float x, float lettersize)
153 {
154         if(x > 200) {
155                 color_x = 0;
156                 color_y = 1;
157                 color_z = 0;
158                 Sbar_DrawXNum(pos, x, 3, lettersize, color, 0, 1, 0);
159         }
160         else if(x > 150) {
161                 color_x = 0.4 - ((x-150)*0.02 * 0.4); //red value between 0.4 -> 0
162                 color_y = 0.9 + ((x-150)*0.02 * 0.1); // green value between 0.9 -> 1
163                 color_z = 0;
164                 Sbar_DrawXNum(pos, x, 3, lettersize, color, 0, 1, 0);
165         }
166         else if(x > 100) {
167                 color_x = 0.6 - ((x-100)*0.02 * 0.2); //red value between 0.6 -> 0.4
168                 color_y = 0.7 + ((x-100)*0.02 * 0.2); // green value between 0.7 -> 0.9
169                 color_z = 0.8 - ((x-100)*0.02 * 0.8); // blue value between 0.8 -> 0
170                 Sbar_DrawXNum(pos, x, 3, lettersize, color, 0, 1, 0);
171         }
172         else if(x > 50) {
173                 color_x = 1 - ((x-50)*0.02 * 0.4); //red value between 1 -> 0.6
174                 color_y = 1 - ((x-50)*0.02 * 0.3); // green value between 1 -> 0.7
175                 color_z = 0.2 + ((x-50)*0.02 * 0.6); // blue value between 0.2 -> 0.8
176                 Sbar_DrawXNum(pos, x, 3, lettersize, color, 0, 1, 0);
177         }
178         else if(x > 20) {
179                 color_x = 1;
180                 color_y = (x-20)*90/27/100; // green value between 0 -> 1
181                 color_z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
182                 Sbar_DrawXNum(pos, x, 3, lettersize, color, 0, 1, 0);
183         }
184         else
185                 Sbar_DrawXNum(pos, x, 3, lettersize, '1 0 0', 0, 1, 0);
186 }
187
188 void Cmd_Sbar_SetFields(float argc);
189 void Sbar_InitScores()
190 {
191         float i, f;
192
193         ps_primary = ps_secondary = ts_primary = ts_secondary = -1;
194         for(i = 0; i < MAX_SCORE; ++i)
195         {
196                 f = (scores_flags[i] & SFL_SORT_PRIO_MASK);
197                 if(f == SFL_SORT_PRIO_PRIMARY)
198                         ps_primary = i;
199                 if(f == SFL_SORT_PRIO_SECONDARY)
200                         ps_secondary = i;
201         }
202         if(ps_secondary == -1)
203                 ps_secondary = ps_primary;
204         
205         for(i = 0; i < MAX_TEAMSCORE; ++i)
206         {
207                 f = (teamscores_flags[i] & SFL_SORT_PRIO_MASK);
208                 if(f == SFL_SORT_PRIO_PRIMARY)
209                         ts_primary = i;
210                 if(f == SFL_SORT_PRIO_SECONDARY)
211                         ts_secondary = i;
212         }
213         if(ts_secondary == -1)
214                 ts_secondary = ts_primary;
215
216         Cmd_Sbar_SetFields(0);
217 }
218
219 void Sbar_UpdatePlayerPos(entity pl);
220 float SetTeam(entity pl, float Team);
221 //float lastpnum;
222 void Sbar_UpdatePlayerTeams()
223 {
224         float Team;
225         entity pl, tmp;
226         float num;
227
228         num = 0;
229         for(pl = players.sort_next; pl; pl = pl.sort_next)
230         {
231                 num += 1;
232                 Team = GetPlayerColor(pl.sv_entnum);
233                 if(SetTeam(pl, Team))
234                 {
235                         tmp = pl.sort_prev;
236                         Sbar_UpdatePlayerPos(pl);
237                         if(tmp)
238                                 pl = tmp;
239                         else
240                                 pl = players.sort_next;
241                 }
242         }
243         /*
244         if(num != lastpnum)
245                 print(strcat("PNUM: ", ftos(num), "\n"));
246         lastpnum = num;
247         */
248 }
249
250 float Sbar_ComparePlayerScores(entity left, entity right)
251 {
252         float vl, vr;
253         vl = GetPlayerColor(left.sv_entnum);
254         vr = GetPlayerColor(right.sv_entnum);
255
256         if(!left.gotscores)
257                 vl = COLOR_SPECTATOR;
258         if(!right.gotscores)
259                 vr = COLOR_SPECTATOR;
260         
261         if(vl > vr)
262                 return true;
263         if(vl < vr)
264                 return false;
265
266         if(vl == COLOR_SPECTATOR)
267         {
268                 // FIRST the one with scores (spectators), THEN the ones without (downloaders)
269                 // no other sorting
270                 if(!left.gotscores && right.gotscores)
271                         return true;
272                 return false;
273         }
274
275         vl = left.scores[ps_primary];
276         vr = right.scores[ps_primary];
277         if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
278         {
279                 if(vl == 0 && vr != 0)
280                         return 1;
281                 if(vl != 0 && vr == 0)
282                         return 0;
283         }
284         if(vl > vr)
285                 return IS_INCREASING(scores_flags[ps_primary]);
286         if(vl < vr)
287                 return IS_DECREASING(scores_flags[ps_primary]);
288         
289         vl = left.scores[ps_secondary];
290         vr = right.scores[ps_secondary];
291         if(scores_flags[ps_secondary] & SFL_ZERO_IS_WORST)
292         {
293                 if(vl == 0 && vr != 0)
294                         return 1;
295                 if(vl != 0 && vr == 0)
296                         return 0;
297         }
298         if(vl > vr)
299                 return IS_INCREASING(scores_flags[ps_secondary]);
300         if(vl < vr)
301                 return IS_DECREASING(scores_flags[ps_secondary]);
302         
303         return false;
304 }
305
306 void Sbar_UpdatePlayerPos(entity player)
307 {
308         for(other = player.sort_next; other && Sbar_ComparePlayerScores(player, other); other = player.sort_next)
309         {
310                 SORT_SWAP(player, other);
311         }
312         for(other = player.sort_prev; other != players && Sbar_ComparePlayerScores(other, player); other = player.sort_prev)
313         {
314                 SORT_SWAP(other, player);
315         }
316 }
317
318 float Sbar_CompareTeamScores(entity left, entity right)
319 {
320         float vl, vr;
321
322         if(left.team == COLOR_SPECTATOR)
323                 return 1;
324         if(right.team == COLOR_SPECTATOR)
325                 return 0;
326         
327         vl = left.teamscores[ts_primary];
328         vr = right.teamscores[ts_primary];
329         if(vl > vr)
330                 return IS_INCREASING(teamscores_flags[ts_primary]);
331         if(vl < vr)
332                 return IS_DECREASING(teamscores_flags[ts_primary]);
333         
334         vl = left.teamscores[ts_secondary];
335         vr = right.teamscores[ts_secondary];
336         if(vl > vr)
337                 return IS_INCREASING(teamscores_flags[ts_secondary]);
338         if(vl < vr)
339                 return IS_DECREASING(teamscores_flags[ts_secondary]);
340
341         return false;
342 }
343
344 void Sbar_UpdateTeamPos(entity Team)
345 {
346         for(other = Team.sort_next; other && Sbar_CompareTeamScores(Team, other); other = Team.sort_next)
347         {
348                 SORT_SWAP(Team, other);
349         }
350         for(other = Team.sort_prev; other != teams && Sbar_CompareTeamScores(other, Team); other = Team.sort_prev)
351         {
352                 SORT_SWAP(other, Team);
353         }
354 }
355
356 void Cmd_Sbar_Help(float argc)
357 {
358         print("You can modify the scoreboard using the\n");
359         print("^3|---------------------------------------------------------------|\n");
360         print("^1 TO BE DONE\n");
361         print("Usage:\n");
362         print("^2sbar_columns_set default\n");
363         print("^2sbar_columns_set ^7filed1 field2 ...\n");
364         print("The following field names are recognized (case INsensitive):\n");
365         print("You can use a ^3|^7 to start the right-aligned fields.\n");
366         
367         print("^3name^7 or ^3nick^7             Name of a player\n");
368         print("^3ping^7                     Ping time\n\n");
369         print("^3kd^7 or ^3kdr^7 or ^3kdratio^7 or ^3k/d\n");
370         print("                         The kill-death ratio\n");
371
372         print("Before a field you can put a + or - sign, then a comma separated list\n");
373         print("of game types, then a slash, to make the field show up only in these\n");
374         print("or in all but these game types.\n");
375
376         print("The special game type names 'teams' and 'noteams' can be used to\n");
377         print("include/exclude ALL teams/noteams game modes.\n");
378
379         local float i;
380         print("Additional columns:\n");
381         for(i = 0; i < MAX_SCORE; ++i)
382         {
383                 if(scores_label[i] != "")
384                         print(strcat(scores_label[i], "\n"));
385         }
386 }
387
388 #define MIN_NAMELEN 24
389 #define MAX_NAMELEN 24
390
391 string Sbar_DefaultColumnLayout()
392 {
393         return strcat( // fteqcc sucks
394                 "ping pl name | ",
395                 "-teams,race,lms/kills -teams,lms/deaths -teams,lms,race/suicides -race,dm,tdm/frags ", // tdm already has this in "score"
396                 "+ctf/caps +ctf/pickups +ctf/fckills +ctf/returns ",
397                 "+lms/lives +lms/rank ",
398                 "+/caps +kh/pushes +kh/destroyed ",
399                 "?+race/laps ?+race/time ?+race/fastest ",
400                 "+as/objectives +nexball/faults +nexball/goals ",
401                 "-lms,race,nexball/score");
402 }
403
404 void Cmd_Sbar_SetFields(float argc)
405 {
406         float i, j, slash;
407         string str, pattern;
408         float digit;
409         float have_name, have_primary, have_secondary, have_separator;
410         float missing;
411
412         // TODO: re enable with gametype dependant cvars?
413         if(argc < 2) // no arguments provided
414                 argc = tokenizebyseparator(strcat("x ", cvar_string("sbar_columns")), " ");
415
416         if(argc < 2)
417                 argc = tokenizebyseparator(strcat("x ", Sbar_DefaultColumnLayout()), " ");
418
419         if(argc == 2)
420         {
421                 if(argv(1) == "default")
422                         argc = tokenizebyseparator(strcat("x ", Sbar_DefaultColumnLayout()), " ");
423                 else if(argv(1) == "all")
424                 {
425                         string s;
426                         s = "ping pl color name |";
427                         for(i = 0; i < MAX_SCORE; ++i)
428                         {
429                                 if(i != ps_primary)
430                                 if(i != ps_secondary)
431                                 if(scores_label[i] != "")
432                                         s = strcat(s, " ", scores_label[i]);
433                         }
434                         if(ps_secondary != ps_primary)
435                                 s = strcat(s, " ", scores_label[ps_secondary]);
436                         s = strcat(s, " ", scores_label[ps_primary]);
437                         argc = tokenizebyseparator(strcat("x ", s), " ");
438                 }
439         }
440                 
441         
442         sbar_num_fields = 0;
443
444         drawfont = sbar_font;
445         digit = stringwidth("0123456789", FALSE) / 10;
446
447         for(i = 0; i < argc - 1; ++i)
448         {
449                 float nocomplain;
450                 str = argv(i+1);
451
452                 nocomplain = FALSE;
453                 if(substring(str, 0, 1) == "?")
454                 {
455                         nocomplain = TRUE;
456                         str = substring(str, 1, strlen(str) - 1);
457                 }
458
459                 slash = strstrofs(str, "/", 0);
460                 if(slash >= 0)
461                 {
462                         pattern = substring(str, 0, slash);
463                         str = substring(str, slash + 1, strlen(str) - (slash + 1));
464
465                         if not(isGametypeInFilter(gametype, teamplay, pattern))
466                                 continue;
467                 }
468
469                 strunzone(sbar_title[sbar_num_fields]);
470                 sbar_title[sbar_num_fields] = strzone(str);
471                 sbar_size[sbar_num_fields] = stringwidth(str, FALSE);
472                 str = strtolower(str);
473
474                 if(str == "ping") {
475                         sbar_field[sbar_num_fields] = SP_PING;
476                 } else if(str == "pl") {
477                         sbar_field[sbar_num_fields] = SP_PL;
478                 } else if(str == "kd" || str == "kdr" || str == "kdratio" || str == "k/d") {
479                         sbar_field[sbar_num_fields] = SP_KDRATIO;
480                 } else if(str == "name" || str == "nick") {
481                         sbar_field[sbar_num_fields] = SP_NAME;
482                         sbar_size[sbar_num_fields] = MIN_NAMELEN; // minimum size? any use?
483                         have_name = 1;
484                 } else if(str == "|") {
485                         sbar_field[sbar_num_fields] = SP_SEPARATOR;
486                         have_separator = 1;
487                 } else {
488                         for(j = 0; j < MAX_SCORE; ++j)
489                                 if(str == strtolower(scores_label[j]))
490                                         goto found; // sorry, but otherwise fteqcc -O3 miscompiles this and warns about "unreachable code"
491 :notfound
492                         if(str == "frags")
493                         {
494                                 j = SP_FRAGS;
495                         }
496                         else
497                         {
498                                 if not(nocomplain)
499                                         print(strcat("^1Error:^7 Unknown score field: '", str, "'\n"));
500                                 continue;
501                         }
502 :found
503                         sbar_field[sbar_num_fields] = j;
504                         if(j == ps_primary)
505                                 have_primary = 1;
506                         if(j == ps_secondary)
507                                 have_secondary = 1;
508                 }
509                 ++sbar_num_fields;
510                 if(sbar_num_fields >= MAX_SBAR_FIELDS)
511                         break;
512         }
513
514         if(scores_flags[ps_primary] & SFL_ALLOW_HIDE)
515                 have_primary = 1;
516         if(scores_flags[ps_secondary] & SFL_ALLOW_HIDE)
517                 have_secondary = 1;
518         if(ps_primary == ps_secondary)
519                 have_secondary = 1;
520         missing = !have_primary + !have_secondary + !have_separator + !have_name;
521
522         if(sbar_num_fields+missing < MAX_SBAR_FIELDS)
523         {
524                 if(!have_name)
525                 {
526                         strunzone(sbar_title[sbar_num_fields]);
527                         for(i = sbar_num_fields; i > 0; --i)
528                         {
529                                 sbar_title[i] = sbar_title[i-1];
530                                 sbar_size[i] = sbar_size[i-1];
531                                 sbar_field[i] = sbar_field[i-1];
532                         }
533                         sbar_title[0] = strzone("name");
534                         sbar_field[0] = SP_NAME;
535                         sbar_size[0] = MIN_NAMELEN; // minimum size? any use?
536                         ++sbar_num_fields;
537                         print("fixed missing field 'name'\n");
538
539                         if(!have_separator)
540                         {
541                                 strunzone(sbar_title[sbar_num_fields]);
542                                 for(i = sbar_num_fields; i > 1; --i)
543                                 {
544                                         sbar_title[i] = sbar_title[i-1];
545                                         sbar_size[i] = sbar_size[i-1];
546                                         sbar_field[i] = sbar_field[i-1];
547                                 }
548                                 sbar_title[1] = strzone("|");
549                                 sbar_field[1] = SP_SEPARATOR;
550                                 sbar_size[1] = stringwidth("|", FALSE);
551                                 ++sbar_num_fields;
552                                 print("fixed missing field '|'\n");
553                         }
554                 }
555                 else if(!have_separator)
556                 {
557                         strunzone(sbar_title[sbar_num_fields]);
558                         sbar_title[sbar_num_fields] = strzone("|");
559                         sbar_size[sbar_num_fields] = stringwidth("|", FALSE);
560                         sbar_field[sbar_num_fields] = SP_SEPARATOR;
561                         ++sbar_num_fields;
562                         print("fixed missing field '|'\n");
563                 }
564
565                 if(!have_secondary)
566                 {
567                         strunzone(sbar_title[sbar_num_fields]);
568                         sbar_title[sbar_num_fields] = strzone(scores_label[ps_secondary]);
569                         sbar_size[sbar_num_fields] = stringwidth(sbar_title[sbar_num_fields], FALSE);
570                         sbar_field[sbar_num_fields] = ps_secondary;
571                         ++sbar_num_fields;
572                         print("fixed missing field '", scores_label[ps_secondary], "'\n");
573                 }
574
575                 if(!have_primary)
576                 {
577                         strunzone(sbar_title[sbar_num_fields]);
578                         sbar_title[sbar_num_fields] = strzone(scores_label[ps_primary]);
579                         sbar_size[sbar_num_fields] = stringwidth(sbar_title[sbar_num_fields], FALSE);
580                         sbar_field[sbar_num_fields] = ps_primary;
581                         ++sbar_num_fields;
582                         print("fixed missing field '", scores_label[ps_primary], "'\n");
583                 }
584         }
585
586         sbar_field[sbar_num_fields] = SP_END;
587 }
588
589 // MOVEUP::
590 vector sbar_field_rgb;
591 string sbar_field_icon0;
592 string sbar_field_icon1;
593 string sbar_field_icon2;
594 vector sbar_field_icon0_rgb;
595 vector sbar_field_icon1_rgb;
596 vector sbar_field_icon2_rgb;
597 float sbar_field_icon0_alpha;
598 float sbar_field_icon1_alpha;
599 float sbar_field_icon2_alpha;
600 string Sbar_GetField(entity pl, float field)
601 {
602         float tmp, num, denom, f;
603         string str;
604         sbar_field_rgb = '1 1 1';
605         sbar_field_icon0 = "";
606         sbar_field_icon1 = "";
607         sbar_field_icon2 = "";
608         sbar_field_icon0_rgb = '1 1 1';
609         sbar_field_icon1_rgb = '1 1 1';
610         sbar_field_icon2_rgb = '1 1 1';
611         sbar_field_icon0_alpha = 1;
612         sbar_field_icon1_alpha = 1;
613         sbar_field_icon2_alpha = 1;
614         switch(field)
615         {
616                 case SP_PING:
617                         if not(pl.gotscores)
618                                 return "\x8D\x8D\x8D"; // >>> sign
619                         str = getplayerkey(pl.sv_entnum, "ping");
620                         if(str == "0")
621                                 return "N/A";
622                         tmp = max(0, min(220, stof(str)-80)) / 220;
623                         sbar_field_rgb = '1 1 1' - '0 1 1'*tmp;
624                         return str;
625                 
626                 case SP_PL:
627                         if not(pl.gotscores)
628                                 return "N/A";
629                         str = getplayerkey(pl.sv_entnum, "pl");
630                         if(str == "0")
631                                 return "";
632                         tmp = bound(0, stof(str), 20) / 20; // 20% is REALLY BAD pl
633                         sbar_field_rgb = '1 0.5 0.5' - '0 0.5 0.5'*tmp;
634                         return str;
635                 
636                 case SP_NAME:
637                         if(ready_waiting && pl.ready)
638                         {
639                                 sbar_field_icon0 = "gfx/sb_player_ready";
640                         }
641                         else if(!teamplay)
642                         {
643                                 f = stof(getplayerkey(pl.sv_entnum, "colors"));
644                                 {
645                                         sbar_field_icon0 = "gfx/sb_playercolor_base";
646                                         sbar_field_icon1 = "gfx/sb_playercolor_shirt";
647                                         sbar_field_icon1_rgb = colormapPaletteColor(floor(f / 16), 0);
648                                         sbar_field_icon2 = "gfx/sb_playercolor_pants";
649                                         sbar_field_icon2_rgb = colormapPaletteColor(mod(f, 16), 1);
650                                 }
651                         }
652                         return GetPlayerName(pl.sv_entnum);
653
654                 case SP_FRAGS:
655                         f = pl.(scores[SP_KILLS]);
656                         f -= pl.(scores[SP_SUICIDES]);
657                         return ftos(f);
658
659                 case SP_KDRATIO:
660                         num = pl.(scores[SP_KILLS]);
661                         denom = pl.(scores[SP_DEATHS]);
662
663                         if(denom == 0) {
664                                 sbar_field_rgb = '0 1 0';
665                                 str = ftos(num);
666                         } else if(num <= 0) {
667                                 sbar_field_rgb = '1 0 0';
668                                 str = ftos(num/denom);
669                         } else
670                                 str = ftos(num/denom);
671                 
672                         tmp = strstrofs(str, ".", 0);
673                         if(tmp > 0)
674                                 str = substring(str, 0, tmp+2);
675                         return str;
676                         
677                 default:
678                         tmp = pl.(scores[field]);
679                         f = scores_flags[field];
680                         if(field == ps_primary)
681                                 sbar_field_rgb = '1 1 0';
682                         else if(field == ps_secondary)
683                                 sbar_field_rgb = '0 1 1';
684                         else
685                                 sbar_field_rgb = '1 1 1';
686                         return ScoreString(f, tmp);
687         }
688         //return "error";
689 }
690
691 float xmin, xmax, ymin, ymax, sbwidth;
692 float sbar_fixscoreboardcolumnwidth_len;
693 float sbar_fixscoreboardcolumnwidth_iconlen;
694 float sbar_fixscoreboardcolumnwidth_marginlen;
695
696 float stringwidth_colors(string s)
697 {
698         return stringwidth(s, TRUE);
699 }
700
701 float stringwidth_nocolors(string s)
702 {
703         return stringwidth(s, FALSE);
704 }
705
706 string Sbar_FixScoreboardColumnWidth(float i, string str)
707 {
708         float field, maxsize, j, f;
709         vector sz;
710         field = sbar_field[i];
711
712         if(field == SP_NAME) // name gets all remaining space
713         {
714                 maxsize = (xmax - xmin) / sbar_fontsize_x;
715                 for(j = 0; j < sbar_num_fields; ++j) if(j != i) if(sbar_field[j] != SP_SEPARATOR)
716                         maxsize -= sbar_size[j] + 1;
717                 maxsize += 1;
718                 str = textShortenToWidth(str, maxsize, stringwidth_colors);
719                 sbar_fixscoreboardcolumnwidth_len = stringwidth(str, TRUE);
720         }
721         else
722                 sbar_fixscoreboardcolumnwidth_len = stringwidth(str, FALSE);
723         
724         sbar_fixscoreboardcolumnwidth_iconlen = 0;
725
726         if(sbar_field_icon0 != "")
727         {
728                 sz = drawgetimagesize(sbar_field_icon0);
729                 f = sz_x / sz_y;
730                 if(sbar_fixscoreboardcolumnwidth_iconlen < f)
731                         sbar_fixscoreboardcolumnwidth_iconlen = f;
732         }
733
734         if(sbar_field_icon1 != "")
735         {
736                 sz = drawgetimagesize(sbar_field_icon1);
737                 f = sz_x / sz_y;
738                 if(sbar_fixscoreboardcolumnwidth_iconlen < f)
739                         sbar_fixscoreboardcolumnwidth_iconlen = f;
740         }
741
742         if(sbar_field_icon2 != "")
743         {
744                 sz = drawgetimagesize(sbar_field_icon2);
745                 f = sz_x / sz_y;
746                 if(sbar_fixscoreboardcolumnwidth_iconlen < f)
747                         sbar_fixscoreboardcolumnwidth_iconlen = f;
748         }
749
750         sbar_fixscoreboardcolumnwidth_iconlen *= sbar_fontsize_y / sbar_fontsize_x; // fix icon aspect
751
752         if(sbar_fixscoreboardcolumnwidth_iconlen != 0 && sbar_fixscoreboardcolumnwidth_len != 0)
753                 sbar_fixscoreboardcolumnwidth_marginlen = stringwidth(" ", FALSE);
754         else
755                 sbar_fixscoreboardcolumnwidth_marginlen = 0;
756
757         f = sbar_fixscoreboardcolumnwidth_len + sbar_fixscoreboardcolumnwidth_marginlen + sbar_fixscoreboardcolumnwidth_iconlen;
758         if(sbar_size[i] < f)
759                 sbar_size[i] = f;
760
761         return str;
762 }
763
764 void Sbar_PrintScoreboardItem(vector pos, entity pl, float is_self)
765 {
766         vector tmp;
767         string str;
768         float i, field;
769         float is_spec;
770         is_spec = (GetPlayerColor(pl.sv_entnum) == COLOR_SPECTATOR);
771
772         // Layout:
773         tmp_z = 0;
774         if(is_self)
775         {
776                 tmp_x = sbwidth;
777                 tmp_y = sbar_fontsize_y;
778                 drawfill(pos - '1 1 0', tmp + '2 2 0', '1 1 1', 0.3, DRAWFLAG_NORMAL);
779         }       
780         tmp_y = 0;
781         
782         for(i = 0; i < sbar_num_fields; ++i)
783         {
784                 field = sbar_field[i];
785                 if(field == SP_SEPARATOR)
786                         break;
787
788                 if(is_spec && field != SP_NAME && field != SP_PING) {
789                         pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
790                         continue;
791                 }
792                 str = Sbar_GetField(pl, field);
793                 str = Sbar_FixScoreboardColumnWidth(i, str);
794
795                 pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
796
797                 if(field == SP_NAME) {
798                         tmp_x = sbar_fontsize_x*(sbar_size[i] - sbar_fixscoreboardcolumnwidth_iconlen - sbar_fixscoreboardcolumnwidth_marginlen) + sbar_fontsize_x;
799                         drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
800                 } else {
801                         tmp_x = sbar_fixscoreboardcolumnwidth_len*sbar_fontsize_x + sbar_fontsize_x;
802                         drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
803                 }
804
805                 tmp_x = sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
806                 if(sbar_field_icon0 != "")
807                         drawpic(pos - tmp, sbar_field_icon0, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon1_rgb, sbar_field_icon0_alpha, 0);
808                 if(sbar_field_icon1 != "")
809                         drawpic(pos - tmp, sbar_field_icon1, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon1_rgb, sbar_field_icon1_alpha, 0);
810                 if(sbar_field_icon2 != "")
811                         drawpic(pos - tmp, sbar_field_icon2, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon2_rgb, sbar_field_icon2_alpha, 0);
812         }
813         
814         if(sbar_field[i] == SP_SEPARATOR)
815         {
816                 pos_x = xmax;
817                 for(i = sbar_num_fields-1; i > 0; --i)
818                 {
819                         field = sbar_field[i];
820                         if(field == SP_SEPARATOR)
821                                 break;
822                         
823                         if(is_spec && field != SP_NAME && field != SP_PING) {
824                                 pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
825                                 continue;
826                         }
827                         
828                         str = Sbar_GetField(pl, field);
829                         str = Sbar_FixScoreboardColumnWidth(i, str);
830
831                         if(field == SP_NAME) {
832                                 tmp_x = sbar_fontsize_x*sbar_fixscoreboardcolumnwidth_len; // left or right aligned? let's put it right...
833                                 drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
834                         } else {
835                                 tmp_x = sbar_fontsize_x*sbar_fixscoreboardcolumnwidth_len; //strlen(str);
836                                 drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
837                         }
838
839                         tmp_x = sbar_fontsize_x*sbar_size[i];
840                         if(sbar_field_icon0 != "")
841                                 drawpic(pos - tmp, sbar_field_icon0, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon1_rgb, sbar_field_icon0_alpha, 0);
842                         if(sbar_field_icon1 != "")
843                                 drawpic(pos - tmp, sbar_field_icon1, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon1_rgb, sbar_field_icon1_alpha, 0);
844                         if(sbar_field_icon2 != "")
845                                 drawpic(pos - tmp, sbar_field_icon2, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon2_rgb, sbar_field_icon2_alpha, 0);
846
847                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
848                 }
849         }
850 }
851
852 float lastpingstime;
853 float scoreboard_bottom;
854 void Sbar_DrawScoreboard()
855 {
856         //float xmin, ymin, xmax, ymax;
857         vector rgb, pos, tmp, sbar_save;
858         entity pl, tm;
859         float specs, i;
860         float center_x;
861
862         if(time > lastpingstime + 10)
863         {
864                 localcmd("pings\n");
865                 lastpingstime = time;
866         }
867
868         sbwidth = Sbar_GetWidth(6.5 * sbar_fontsize_y);
869
870         xmin = 0.5 * (vid_conwidth - sbwidth);
871         ymin = 45;
872
873         xmax = vid_conwidth - xmin;
874     ymax = vid_conheight - 0.2*vid_conheight;
875
876         center_x = xmin + 0.5*sbwidth;
877
878         //Sbar_UpdateFields();
879
880         // Initializes position
881         //pos_x = xmin;
882         pos_y = ymin;
883         pos_z = 0;
884
885         // Heading
886         drawfont = sbar_bigfont;
887         pos_x = center_x - stringwidth("Scoreboard", TRUE)*0.5*24;
888         drawstring(pos, "Scoreboard", '24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
889         pos_x = xmin;
890         pos_y += 24 + 4;
891
892         // Titlebar background:
893         tmp_x = sbwidth;
894         tmp_y = sbar_fontsize_y;
895         drawfill(pos - '1 1 0', tmp + '2 2 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
896         
897         drawfont = sbar_font;
898
899         for(i = 0; i < sbar_num_fields; ++i)
900         {
901                 if(sbar_field[i] == SP_SEPARATOR)
902                         break;
903                 drawstring(pos, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
904                 pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
905         }
906         
907         if(sbar_field[i] == SP_SEPARATOR)
908         {
909                 pos_x = xmax + sbar_fontsize_x;
910                 tmp_y = tmp_z = 0;
911                 for(i = sbar_num_fields-1; i > 0; --i)
912                 {
913                         if(sbar_field[i] == SP_SEPARATOR)
914                                 break;
915
916                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
917                         /**
918                          * FTEQCC BUG!
919                          * Using the following line will mess it all up:
920                          **
921                          * tmp_x = sbar_size[i] - strlen(sbar_title[i])*8;
922                          */
923                         tmp_x = sbar_fontsize_x*sbar_size[i];
924                         tmp_x -= stringwidth(sbar_title[i], FALSE)*sbar_fontsize_x;
925                         drawstring(pos + tmp, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
926                 }
927         }
928                 
929         pos_x = xmin;
930         pos_y += 1.5 * sbar_fontsize_y;
931
932         sbar_save = sbar;
933         sbar = '0 0 0';
934         
935         if(teamplay)
936         {
937                 //for(tm = sortedTeams.sort_next; tm; tm = tm.sort_next)
938                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
939                 {
940                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
941                                 continue;
942
943                         rgb = GetTeamRGB(tm.team);
944
945                         pos_x = xmin;
946
947                         Sbar_DrawXNum(sbar + pos - '6.5 0 0' * sbar_fontsize_y, tm.(teamscores[ts_primary]), 4, sbar_fontsize_y * 1.5, rgb, 0, 1, DRAWFLAG_NORMAL);
948
949                         if(ts_primary != ts_secondary)
950                         Sbar_DrawXNum(sbar + pos - '4.5 0 0' * sbar_fontsize_y + '0 1.5 0' * sbar_fontsize_y, tm.(teamscores[ts_secondary]), 4, sbar_fontsize_y * 1, rgb, 0, 1, DRAWFLAG_NORMAL);
951                         
952                         specs = tm.team_size;
953
954                         if(specs < 2)
955                                 specs = 2;
956                         
957                         tmp_x = sbwidth;
958                         tmp_y = 1.25 * sbar_fontsize_y * specs;
959                         drawfill(pos - '1 1 0', tmp + '2 0 0', rgb, 0.2, DRAWFLAG_NORMAL);
960                         
961                         for(pl = players.sort_next; pl; pl = pl.sort_next)
962                         {
963                                 if(pl.team != tm.team)
964                                         continue;
965                                 Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
966                                 pos_y += 1.25 * sbar_fontsize_y;
967                                 tmp_y -= 1.25 * sbar_fontsize_y;
968                         }
969                         pos_y += tmp_y + 1.5 * sbar_fontsize_y;
970                 }
971                 // rgb := tempvector :)
972                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
973                 pos_y += 3 * sbar_fontsize_y;
974                 specs = 0;
975                 for(pl = players.sort_next; pl; pl = pl.sort_next)
976                 {
977                         if(pl.team != COLOR_SPECTATOR)
978                                 continue;
979                         Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
980                         pos += '0 1.25 0' * sbar_fontsize_y;
981                         ++specs;
982                 }
983                         
984                 if(specs)
985                         drawstring(rgb, "Spectators", sbar_fontsize, '1 1 1', 1, 0);
986         } else {
987                 pos_x = xmin;
988                 for(pl = players.sort_next; pl; pl = pl.sort_next)
989                 {
990                         if(pl.team == COLOR_SPECTATOR)
991                                 continue;
992                         Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
993                         pos_y += 1.25 * sbar_fontsize_y;
994                         tmp_y -= 1.25 * sbar_fontsize_y;
995                 }
996
997                 // rgb := tempvector :)
998                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
999                 pos_y += 3 * sbar_fontsize_y;
1000                 specs = 0;
1001                 for(pl = players.sort_next; pl; pl = pl.sort_next)
1002                 {
1003                         if(pl.team != COLOR_SPECTATOR)
1004                                 continue;
1005                         Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
1006                         pos += '0 1.25 0' * sbar_fontsize_y;
1007                         ++specs;
1008                 }
1009                         
1010                 if(specs)
1011                         drawstring(rgb, "Spectators", sbar_fontsize, '1 1 1', 1, 0);
1012         }
1013
1014         string str;
1015         float tl, fl;
1016         str = strcat("playing on ^2", shortmapname, "^7");
1017         tl = getstatf(STAT_TIMELIMIT);
1018         fl = getstatf(STAT_FRAGLIMIT);
1019         if(gametype == GAME_LMS)
1020         {
1021                 if(tl > 0)
1022                         str = strcat(str, " for up to ^1", ftos(tl), " minutes^7");
1023         }
1024         else
1025         {
1026                 if(tl > 0)
1027                         str = strcat(str, " for ^1", ftos(tl), " minutes^7");
1028                 if(fl > 0)
1029                 {
1030                         if(tl > 0)
1031                                 str = strcat(str, " or");
1032                         if(teamplay)
1033                         {
1034                                 str = strcat(str, " until ^3", ScoreString(teamscores_flags[ts_primary], fl));
1035                                 if(teamscores_label[ts_primary] == "score")
1036                                         str = strcat(str, " points^7");
1037                                 else if(teamscores_label[ts_primary] == "fastest")
1038                                         str = strcat(str, " is beaten^7");
1039                                 else
1040                                         str = strcat(str, " ", teamscores_label[ts_primary]);
1041                         }
1042                         else
1043                         {
1044                                 str = strcat(str, " until ^3", ScoreString(scores_flags[ps_primary], fl));
1045                                 if(scores_label[ps_primary] == "score")
1046                                         str = strcat(str, " points^7");
1047                                 else if(scores_label[ps_primary] == "fastest")
1048                                         str = strcat(str, " is beaten^7");
1049                                 else
1050                                         str = strcat(str, " ", scores_label[ps_primary]);
1051                         }
1052                 }
1053         }
1054         
1055
1056         pos_y += 1.2 * sbar_fontsize_y;
1057         drawcolorcodedstring(pos + '0.5 0 0' * (sbwidth - sbar_fontsize_x * stringwidth(str, TRUE)), str, sbar_fontsize, 0.8, 0);
1058         
1059         sbar = sbar_save;
1060         scoreboard_bottom = pos_y + 2 * sbar_fontsize_y;
1061 }
1062
1063 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
1064 {
1065         string col;
1066         string timestr;
1067         string cpname;
1068         string lapstr;
1069         lapstr = "";
1070
1071         if(histime == 0) // goal hit
1072         {
1073                 if(mytime > 0)
1074                 {
1075                         timestr = strcat("+", ftos_decimals(+mytime, 1));
1076                         col = "^1";
1077                 }
1078                 else if(mytime == 0)
1079                 {
1080                         timestr = "+0.0";
1081                         col = "^3";
1082                 }
1083                 else
1084                 {
1085                         timestr = strcat("-", ftos_decimals(-mytime, 1));
1086                         col = "^2";
1087                 }
1088
1089                 if(lapdelta > 0)
1090                 {
1091                         lapstr = strcat(" (-", ftos(lapdelta), "L)");
1092                         col = "^2";
1093                 }
1094                 else if(lapdelta < 0)
1095                 {
1096                         lapstr = strcat(" (+", ftos(-lapdelta), "L)");
1097                         col = "^1";
1098                 }
1099         }
1100         else if(histime > 0) // anticipation
1101         {
1102                 if(mytime >= histime)
1103                         timestr = strcat("+", ftos_decimals(mytime - histime, 1));
1104                 else
1105                         timestr = mmsss(histime * 10);
1106                 col = "^3";
1107         }
1108         else
1109                 col = "^7";
1110
1111         if(cp)
1112                 cpname = strcat("Intermediate ", ftos(cp));
1113         else
1114                 cpname = "Finish line";
1115         
1116         if(histime < 0)
1117                 return strcat(col, cpname);
1118         else if(hisname == "")
1119                 return strcat(col, cpname, " (", timestr, ")");
1120         else
1121                 return strcat(col, cpname, " (", timestr, " ", strcat(hisname, col, lapstr), ")");
1122 }
1123
1124 void Sbar_Score(float margin)
1125 {
1126         float timelimit, minutes, seconds, timeleft, minutesLeft, secondsLeft, distribution, myplace, score, desiredPlayerId;
1127         vector sbar_save, place, timer_color, offset;
1128         entity tm, pl, me;
1129         sbar_save = sbar;\r
1130         
1131         vector bottomright;
1132         bottomright_x = vid_conwidth;
1133         bottomright_y = vid_conheight;
1134         bottomright_z = 0;
1135         
1136         vector topright;
1137         topright_x = vid_conwidth;
1138         topright_y = 0;
1139         topright_z = 0;
1140         
1141         sbar_alpha_bg = cvar("sbar_alpha_bg");\r
1142         //get the ID (could be "me", or the player I'm spectating)\r
1143         desiredPlayerId = player_localentnum - 1;\r
1144         if (spectatee_status) {\r
1145                 if (spectatee_status != -1) {\r
1146                         desiredPlayerId = spectatee_status - 1;\r
1147                 }\r
1148         }
1149         \r
1150         myteam = GetPlayerColor(desiredPlayerId);
1151
1152         sbar_y = vid_conheight - (32+12);
1153         sbar_x -= margin;
1154         
1155         offset = '0 0 0';
1156         place_z = 0;
1157         if(teamplay)
1158         {
1159                 // Layout:
1160                 //
1161                 //   team1 team3 team4
1162                 //
1163                 //         TEAM2
1164                 //for(i = 0; i < 4; ++i)
1165                 if (cvar("vid_conwidth") >= 800) {
1166                         place_x = 196;
1167                         place_y = 36;
1168                         
1169                 }
1170                 else {
1171                         place_x = 196;
1172                         place_y = 86;
1173                 }
1174                 
1175                 float max_fragcount;
1176                 max_fragcount = -999;
1177
1178                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
1179                 {
1180                         if(tm.team == COLOR_SPECTATOR || !tm.team_size) // no players? don't display
1181                                 continue;
1182                         // -32*4 = -128
1183                         score = tm.(teamscores[ts_primary]);
1184                         
1185                         if (score > max_fragcount)
1186                                 max_fragcount = score;
1187                                 
1188                         if(tm.team == myteam) {
1189                                 if (max_fragcount == score) 
1190                                         Sbar_DrawXNum(bottomright - element_offset - place, score, 4, 34, GetTeamRGB(tm.team) * 0.8, 1, 1, DRAWFLAG_NORMAL);
1191                                 else
1192                                         Sbar_DrawXNum(bottomright - element_offset - place, score, 4, 34, GetTeamRGB(tm.team) * 0.8, 0, 1, DRAWFLAG_NORMAL);
1193                         }
1194                         else
1195                         {
1196                                 if (max_fragcount == score)
1197                                         Sbar_DrawXNum(bottomright - element_offset - place + '132 -6 0' - offset, score, 4, 16, GetTeamRGB(tm.team) * 0.8, 1, 1, DRAWFLAG_NORMAL);
1198                                 else
1199                                         Sbar_DrawXNum(bottomright - element_offset - place + '132 -6 0' - offset, score, 4, 16, GetTeamRGB(tm.team) * 0.8, 0, 1, DRAWFLAG_NORMAL);
1200                                 offset_y -= 16;
1201                         }
1202                 }
1203         } else {
1204                 // me vector := [team/connected frags id]
1205                 
1206                 myplace = 0;
1207                 for(me = players.sort_next; me; me = me.sort_next)
1208                 {
1209                         if(me.team != COLOR_SPECTATOR)
1210                                 ++myplace;
1211                         if(me.sv_entnum == desiredPlayerId)
1212                                 break;
1213                 }
1214                 
1215                 pl = players.sort_next;
1216                 if(pl == me)
1217                         pl = pl.sort_next;
1218                 
1219                 if(pl) {
1220                         distribution = me.(scores[ps_primary]);
1221                         distribution -= pl.(scores[ps_primary]);
1222                 } else
1223                         distribution = 0;
1224
1225                 if (cvar("vid_conwidth") >= 800) {
1226                         place_x = 196;
1227                         place_y = 36;
1228                         
1229                 }
1230                 else { // move the scores if vid_conwidth < 800
1231                         place_x = 196;
1232                         place_y = 86;
1233                 }
1234                 score = me.(scores[ps_primary]);
1235                 
1236                 if(distribution >= 0)
1237                 {
1238                         if (distribution != 0) { 
1239                                 // draw a + sign in front of the score
1240                                 if (distribution < 10) { drawpic(bottomright - element_offset - place + '132 -5 0' + '32 0 0', "gfx/hud/num_plus", '16 16 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_ADDITIVE); }
1241                                 else if (distribution < 100) { drawpic(bottomright - element_offset - place + '132 -5 0' + '16 0 0', "gfx/hud/num_plus", '16 16 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_ADDITIVE); }
1242                                 else if (distribution < 1000) { drawpic(bottomright - element_offset - place + '132 -5 0', "gfx/hud/num_plus", '16 16 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_ADDITIVE); }
1243                         }
1244                                 
1245                         Sbar_DrawXNum(bottomright - element_offset - place + '132 -6 0', distribution, 4, 16, ' 1 1 1', 0, 1, DRAWFLAG_NORMAL);
1246                         Sbar_DrawXNum(bottomright - element_offset - place, score, 4, 34, '1 1 1', 1, 1, DRAWFLAG_NORMAL);
1247                 }       
1248                 else if(distribution >= -5)
1249                 {
1250                         Sbar_DrawXNum(bottomright - element_offset - place + '132 -6 0', distribution, 4, 16, ' 1 1 0', 0, 1, DRAWFLAG_NORMAL);
1251                         Sbar_DrawXNum(bottomright - element_offset - place, score, 4, 34, '1 1 0', 0, 1, DRAWFLAG_NORMAL);
1252                 } 
1253                 else {
1254                         Sbar_DrawXNum(bottomright - element_offset - place + '132 -6 0', distribution, 4, 16, ' 1 0 0', 0, 1, DRAWFLAG_NORMAL);
1255                         Sbar_DrawXNum(bottomright - element_offset - place, score, 4, 34, '1 0 0', 0, 1, DRAWFLAG_NORMAL);
1256                 }
1257         }
1258         
1259         //draw the remaining or elapsed time
1260         timelimit = getstatf(STAT_TIMELIMIT);
1261         
1262         vector bgpos;
1263         if(timelimit > 0)
1264         {
1265                 timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
1266                 timeleft = ceil(timeleft);
1267                 minutesLeft = floor(timeleft / 60);
1268                 secondsLeft = timeleft - minutesLeft*60;
1269                 
1270                 if(minutesLeft >= 5) {
1271                         timer_color = '1 1 1'; //white
1272                 } else if(minutesLeft >= 1) {
1273                         timer_color = '1 1 0'; //yellow
1274                 } else {
1275                         timer_color = '1 0 0'; //red
1276                 }
1277                 
1278                 if (warmup_stage) {
1279                         timer_color = '1 1 1'; //don't use red or yellow for timer during warmup
1280                 }
1281                 
1282                 if (cvar("sbar_increment_maptime")) {
1283                         if (time < getstatf(STAT_GAMESTARTTIME)) {
1284                                 //while restart is still active, show negative counter
1285                                 minutes = 0;
1286                                 seconds = ceil(getstatf(STAT_GAMESTARTTIME) - time);
1287                         } else {
1288                                 float elapsedTime;
1289                                 elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
1290                                 minutes = floor(elapsedTime / 60);
1291                                 seconds = elapsedTime - minutes*60;
1292                         }
1293                 } else {
1294                         minutes = minutesLeft;
1295                         seconds = secondsLeft;
1296                 }
1297                 
1298                 if (minutes == 0)
1299                         bgpos_x = topright_x - 36 - 7 - 12;
1300                 else if (minutes < 10) // nudge the timer background left if more digits are drawn
1301                         bgpos_x = topright_x - 54 - 17 - 12;
1302                 else if (minutes < 100) 
1303                         bgpos_x = topright_x - 72 - 17 - 12;
1304                 else
1305                         bgpos_x = topright_x - 90 - 17 - 12;
1306                 bgpos_y = 0;
1307                 bgpos_z = 0;
1308                 
1309                 if (cvar("viewsize") <= 100) { // draw timer background when viewsize <= 100
1310                         if (teamplay)
1311                                 drawpic(bgpos, "gfx/hud/sb_timerbg", '120 30 0', GetTeamRGB(myteam) * cvar("sbar_color_bg_team"), sbar_alpha_bg, 0); // timer bg color = myteam color
1312                         else {
1313                                 color_x = cvar("sbar_color_bg_r");
1314                                 color_y = cvar("sbar_color_bg_g");
1315                                 color_z = cvar("sbar_color_bg_b");
1316                                 
1317                                 drawpic(bgpos, "gfx/hud/sb_timerbg", '120 30 0', color, sbar_alpha_bg, 0);
1318                         }
1319                 }
1320                 
1321                 if(minutesLeft >= 1 || (cvar("sbar_increment_maptime") && minutes >= 1) ) {
1322                         Sbar_DrawXNum(topright - '103 0 0', minutes, 3, 18, timer_color, 0, sbar_alpha_fg, 0);
1323                         drawpic(topright - '53 0 0', "gfx/hud/num_colon", '18 18 0', timer_color, sbar_alpha_fg, 0);
1324                 }
1325                 Sbar_DrawXNum(topright - '36 0 0' - '3 0 0', seconds, -2, 18, timer_color, 0, 1, DRAWFLAG_NORMAL);
1326
1327         } else {
1328                 timer_color = '1 1 1'; //white
1329                 minutes = floor(time / 60);
1330                 seconds = floor(time - minutes*60);
1331                 
1332                 if (minutes < 10) 
1333                         bgpos_x = topright_x - 54 - 17 - 12;
1334                 else if (minutes < 100) // nudge the timer background left if more digits are drawn
1335                         bgpos_x = topright_x - 72 - 17 - 12;
1336                 else
1337                         bgpos_x = topright_x - 90 - 17 - 12;
1338                 bgpos_y = 0;
1339                 bgpos_z = 0;
1340                 
1341                 if (cvar("viewsize") <= 100) { // draw timer background when viewsize <= 100
1342                         if (teamplay)
1343                                 drawpic(bgpos, "gfx/hud/sb_timerbg", '120 30 0', GetTeamRGB(myteam), sbar_alpha_bg, 0); // timer bg color = myteam color
1344                         else {
1345                                 color_x = cvar("sbar_color_bg_r");
1346                                 color_y = cvar("sbar_color_bg_g");
1347                                 color_z = cvar("sbar_color_bg_b");
1348                                 
1349                                 drawpic(bgpos, "gfx/hud/sb_timerbg", '120 30 0', color, sbar_alpha_bg, 0);
1350                         }
1351                 }
1352                 
1353                 Sbar_DrawXNum(topright - '103 0 0', minutes, 3, 18, timer_color, 0, 1, DRAWFLAG_NORMAL);
1354                 drawpic(topright - '53 0 0', "gfx/hud/num_colon", '18 18 0', timer_color, sbar_alpha_fg, 0);
1355                 Sbar_DrawXNum(topright - '36 0 0' - '3 0 0', seconds, -2, 18, timer_color, 0, 1, DRAWFLAG_NORMAL);
1356         }
1357
1358         if(gametype == GAME_RACE)
1359         {
1360                 drawfont = sbar_bigfont;
1361                 float a;
1362                 vector m;
1363                 string s, forcetime;
1364
1365                 m = '0.5 0 0' * vid_conwidth + '0 0.25 0' * vid_conheight;
1366
1367                 if(race_checkpointtime)
1368                 {
1369                         a = bound(0, 2 - (time - race_checkpointtime), 1);
1370                         s = "";
1371                         forcetime = "";
1372                         if(a > 0) // just hit a checkpoint?
1373                         {
1374                                 if(race_time && race_previousbesttime)
1375                                         s = MakeRaceString(race_checkpoint, race_time / 10 - race_previousbesttime / 10, 0, 0, race_previousbestname);
1376                                 else
1377                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
1378                                 if(race_time)
1379                                         forcetime = mmsss(race_time);
1380                         }
1381                         else
1382                         {
1383                                 if(race_laptime && race_nextbesttime)
1384                                 {
1385                                         a = bound(0, 2 - ((race_laptime + race_nextbesttime/10) - time), 1);
1386                                         if(a > 0) // next one?
1387                                         {
1388                                                 s = MakeRaceString(race_nextcheckpoint, time - race_laptime, race_nextbesttime / 10, 0, race_nextbestname);
1389                                         }
1390                                 }
1391                         }
1392
1393                         if(s != "" && a > 0)
1394                         {
1395                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
1396                                 drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', sbar_alpha_fg * a, 0);
1397                         }
1398
1399                         if(forcetime != "")
1400                         {
1401                                 a = bound(0, (time - race_checkpointtime) / 0.5, 1);
1402                                 drawstring_expanding(m - '16 0 0' * stringwidth(forcetime, FALSE), forcetime, '32 32 0', '1 1 1', sbar_alpha_fg, 0, a);
1403                         }
1404                         else
1405                                 a = 1;
1406
1407                         if(race_laptime)
1408                         {
1409                                 s = mmsss(10*(time - race_laptime));
1410                                 drawstring(m - '16 0 0' * stringwidth(s, FALSE), s, '32 32 0', '1 1 1', sbar_alpha_fg * a, 0);
1411                         }
1412                 }
1413                 else
1414                 {
1415                         if(race_mycheckpointtime)
1416                         {
1417                                 a = bound(0, 2 - (time - race_mycheckpointtime), 1);
1418                                 s = MakeRaceString(race_mycheckpoint, race_mycheckpointdelta / 10, -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
1419                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
1420                                 drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', sbar_alpha_fg * a, 0);
1421                         }
1422                         if(race_othercheckpointtime && race_othercheckpointenemy != "")
1423                         {
1424                                 a = bound(0, 2 - (time - race_othercheckpointtime), 1);
1425                                 s = MakeRaceString(race_othercheckpoint, -race_othercheckpointdelta / 10, -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
1426                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
1427                                 drawcolorcodedstring(m - '0 0 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', sbar_alpha_fg * a, 0);
1428                         }
1429                 }
1430
1431                 drawfont = sbar_font;
1432         }
1433
1434         sbar = sbar_save;
1435 }
1436
1437 float Sbar_WouldDrawScoreboard ()
1438 {
1439         if (sb_showscores)
1440                 return 1;
1441         else if (intermission == 1)
1442                 return 1;
1443         else if (intermission == 2)
1444                 return 1;
1445         else if (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard"))
1446                 return 1;
1447         else if(sb_showscores_force)
1448                 return 1;
1449         return 0;
1450 }
1451
1452 void CSQC_Strength_Timer() {
1453         vector bottom;
1454
1455         bottom_x = vid_conwidth/2;
1456         bottom_y = vid_conheight;
1457         bottom_z = 0;
1458
1459         float stat_items, dt;
1460         stat_items = getstati(STAT_ITEMS);
1461         /*
1462         if not(stat_items & IT_STRENGTH)
1463                 if not(stat_items & IT_INVINCIBLE)
1464                         return;
1465         */
1466         
1467         if (getstati(STAT_HEALTH) <= 0)
1468                 return;
1469         
1470         vector picsize;
1471         float strength_time, invincibility_time, countdown_fontsize;
1472
1473         picsize = '22 22 0';
1474         countdown_fontsize = 18;
1475
1476         //strength
1477         strength_time = getstatf(STAT_STRENGTH_FINISHED);
1478         invincibility_time = getstatf(STAT_INVINCIBLE_FINISHED);
1479
1480         if (strength_time) {
1481                 dt = strength_time - time;
1482                 if(dt > 0)
1483                 {
1484                         if(dt < 5)
1485                         {
1486                                 drawpic_expanding_two(bottom + '192 -46 0', "gfx/hud/sb_str", picsize, '1 1 1', sbar_alpha_fg, DRAWFLAG_ADDITIVE,
1487                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1488                         }
1489                         else
1490                         {
1491                                 drawpic(bottom + '192 -46 0', "gfx/hud/sb_str", picsize, '1 1 1', sbar_alpha_fg, DRAWFLAG_ADDITIVE);
1492                         }
1493                         Sbar_DrawXNum(bottom + '152 -44 0', ceil(dt), 2, countdown_fontsize, '1 1 1', 0, 1, DRAWFLAG_NORMAL);
1494                 }
1495                 else if(dt > -1)
1496                 {
1497                         drawpic_expanding(bottom + '192 -46 0', "gfx/hud/sb_str", picsize, '1 1 1', sbar_alpha_fg, DRAWFLAG_ADDITIVE,
1498                                 bound(0, -dt / 0.5, 1));
1499                 }
1500         }
1501
1502         //invincibility
1503         if (invincibility_time) {
1504                 dt = invincibility_time - time;
1505                 if(dt > 0)
1506                 {
1507                         if(dt < 5)
1508                         {
1509                                 drawpic_expanding_two(bottom + '192 -24 0', "gfx/hud/sb_invinc", picsize, '1 1 1', sbar_alpha_fg, DRAWFLAG_ADDITIVE,
1510                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1511                         }
1512                         else
1513                         {
1514                                 drawpic(bottom + '192 -24 0', "gfx/hud/sb_invinc", picsize, '1 1 1', sbar_alpha_fg, DRAWFLAG_ADDITIVE);
1515                         }
1516                         Sbar_DrawXNum(bottom + '152 -22 0', ceil(dt), 2, countdown_fontsize, '1 1 1', 0, 1, DRAWFLAG_NORMAL);
1517                 }
1518                 else if(dt > -1)
1519                 {
1520                         drawpic_expanding(bottom + '192 -24 0', "gfx/hud/sb_invinc", picsize, '1 1 1', sbar_alpha_fg, DRAWFLAG_ADDITIVE,
1521                                 bound(0, -dt / 0.5, 1));
1522                 }
1523         }
1524 }
1525
1526 #define CENTERPRINT_MAX_LINES 30
1527 string centerprint_messages[CENTERPRINT_MAX_LINES]; 
1528 float centerprint_width[CENTERPRINT_MAX_LINES]; 
1529 vector centerprint_start;
1530 float centerprint_expire;
1531 float centerprint_num;
1532 float centerprint_offset_hint;
1533 vector centerprint_fontsize;
1534
1535 void centerprint(string strMessage)
1536 {
1537         float i, j, n, hcount;
1538         string s;
1539
1540         centerprint_fontsize = Sbar_GetFontsize("scr_centersize");
1541
1542         centerprint_expire = min(centerprint_expire, time); // if any of the returns happens, this message will fade out
1543
1544         if(cvar("scr_centertime") <= 0)
1545                 return;
1546         
1547         if(strMessage == "")
1548                 return;
1549
1550         // strip trailing newlines
1551         j = strlen(strMessage) - 1;
1552         while(substring(strMessage, j, 1) == "\n" && j >= 0)
1553                 j = j - 1;
1554         strMessage = substring(strMessage, 0, j + 1);
1555         
1556         if(strMessage == "")
1557                 return;
1558
1559         // strip leading newlines and remember them, they are a hint that the message should be lower on the screen
1560         j = 0;
1561         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
1562                 j = j + 1;
1563         strMessage = substring(strMessage, j, strlen(strMessage) - j);
1564         centerprint_offset_hint = j;
1565
1566         if(strMessage == "")
1567                 return;
1568
1569         // if we get here, we have a message. Initialize its height.
1570         centerprint_num = 0;
1571         
1572         n = tokenizebyseparator(strMessage, "\n");
1573         i = hcount = 0;
1574         for(j = 0; j < n; ++j)
1575         {
1576                 getWrappedLine_remaining = argv(j);
1577                 while(getWrappedLine_remaining)
1578                 {
1579                         s = getWrappedLine(vid_conwidth * 0.75 / centerprint_fontsize_x, stringwidth_colors);
1580                         if(centerprint_messages[i])
1581                                 strunzone(centerprint_messages[i]);
1582                         centerprint_messages[i] = strzone(s);
1583                         centerprint_width[i] = stringwidth(s, TRUE);
1584                         ++i;
1585
1586                         // half height for empty lines looks better
1587                         if(s == "")
1588                                 hcount += 0.5;
1589                         else
1590                                 hcount += 1;
1591
1592                         if(i >= CENTERPRINT_MAX_LINES)
1593                                 break;
1594                 }
1595         }
1596
1597         float h, havail;
1598         h = centerprint_fontsize_y*hcount;
1599
1600         havail = vid_conheight;
1601         if(cvar("con_chatpos") < 0)
1602                 havail -= (-cvar("con_chatpos") + cvar("con_chat")) * cvar("con_chatsize"); // avoid overlapping chat
1603
1604         centerprint_start_x = 0;
1605
1606 #if 0
1607         float forbiddenmin, forbiddenmax, allowedmin, allowedmax, preferred;
1608
1609         // here, the centerprint would cover the crosshair. REALLY BAD.
1610         forbiddenmin = vid_conheight * 0.5 - h - 16;
1611         forbiddenmax = vid_conheight * 0.5 + 16;
1612
1613         allowedmin = scoreboard_bottom;
1614         allowedmax = havail - h;
1615         preferred = (havail - h)/2;
1616
1617
1618         // possible orderings (total: 4! / 4 = 6)
1619         //  allowedmin allowedmax forbiddenmin forbiddenmax
1620         //  forbiddenmin forbiddenmax allowedmin allowedmax
1621         if(allowedmax < forbiddenmin || allowedmin > forbiddenmax)
1622         {
1623                 // forbidden doesn't matter in this case
1624                 centerprint_start_y = bound(allowedmin, preferred, allowedmax);
1625         }
1626         //  allowedmin forbiddenmin allowedmax forbiddenmax
1627         else if(allowedmin < forbiddenmin && allowedmax < forbiddenmax)
1628         {
1629                 centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
1630         }
1631         //  allowedmin forbiddenmin forbiddenmax allowedmax
1632         else if(allowedmin < forbiddenmin)
1633         {
1634                 // make sure the forbidden zone is not covered
1635                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
1636                         centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
1637                 else
1638                         centerprint_start_y = bound(forbiddenmax, preferred, allowedmin);
1639         }
1640         //  forbiddenmin allowedmin allowedmax forbiddenmax
1641         else if(allowedmax < forbiddenmax)
1642         {
1643                 // it's better to leave the allowed zone (overlap with scoreboard) than
1644                 // to cover the forbidden zone (crosshair)
1645                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
1646                         centerprint_start_y = forbiddenmax;
1647                 else
1648                         centerprint_start_y = forbiddenmin;
1649         }
1650         //  forbiddenmin allowedmin forbiddenmax allowedmax
1651         else
1652         {
1653                 centerprint_start_y = bound(forbiddenmax, preferred, allowedmax);
1654         }
1655 #else
1656         centerprint_start_y =
1657                 min(
1658                         max(
1659                                 max(scoreboard_bottom, vid_conheight * 0.5 + 16),
1660                                 (havail - h)/2
1661                         ),
1662                         havail - h
1663                 );
1664 #endif
1665
1666         centerprint_num = i;
1667         centerprint_expire = time + cvar("scr_centertime");
1668 }
1669
1670 void Sbar_DrawCenterPrint (void)
1671 {
1672         float i;
1673         vector pos;
1674         string ts;
1675         float a;
1676
1677         //if(time > centerprint_expire)
1678         //      return;
1679         
1680         //a = bound(0, 1 - 2 * (time - centerprint_expire), 1);
1681         a = bound(0, 1 - 4 * (time - centerprint_expire), 1);
1682         //sz = 1.2 / (a + 0.2);
1683
1684         if(a <= 0)
1685                 return;
1686         
1687         pos = centerprint_start;
1688         for (i=0; i<centerprint_num; i = i + 1)
1689         {
1690                 pos_x = (vid_conwidth - centerprint_fontsize_x * centerprint_width[i]) * 0.5;
1691                 ts = centerprint_messages[i];
1692                 if (ts != "")
1693                 {
1694                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
1695                         drawcolorcodedstring(pos, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
1696                         //  - '0 0.5 0' * (sz - 1) * centerprint_fontsize_x - '0.5 0 0' * (sz - 1) * centerprint_width[i] * centerprint_fontsize_y, centerprint_fontsize * sz
1697                         pos_y = pos_y + centerprint_fontsize_y;
1698                 }
1699                 else
1700                         // half height for empty lines looks better
1701                         pos_y = pos_y + centerprint_fontsize_y * 0.5;
1702         }
1703 }
1704
1705 vector Sbar_DrawNoteLine(vector offset, string s)
1706 {
1707         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
1708         drawcolorcodedstring(
1709                 offset - sbar_fontsize_x * '1 0 0' * stringwidth(s, TRUE),
1710                 s,
1711                 sbar_fontsize,
1712                 sbar_alpha_fg,
1713                 0
1714         );
1715         return offset + sbar_fontsize_y * '0 1 0';
1716 }
1717
1718 void Sbar_DrawPressedKeys(void)
1719 {
1720         vector pos, bgsize;
1721         float pressedkeys;
1722         
1723         pos = stov(cvar_string("cl_showpressedkeys_position"));
1724
1725         bgsize = '126 75 0';
1726
1727         pos = '1 0 0' * (vid_conwidth - bgsize_x) * pos_x
1728             + '0 1 0' * (vid_conheight - bgsize_y) * pos_y;
1729         pos -= '-15 -6 0'; // adjust to the origin of these numbers
1730         
1731         pressedkeys = getstatf(STAT_PRESSED_KEYS);
1732         drawpic(pos + '-15   -6   0', "gfx/hud/keys/key_bg.tga",           bgsize, '1 1 1', .1, DRAWFLAG_NORMAL);
1733         drawpic(pos + ' 83.5  9   0', ((pressedkeys & KEY_CROUCH) ? "gfx/hud/keys/key_crouch_inv.tga" : "gfx/hud/keys/key_crouch.tga"), ' 24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1734         drawpic(pos + ' 32   -1.5 0', ((pressedkeys & KEY_FORWARD) ? "gfx/hud/keys/key_forward_inv.tga" : "gfx/hud/keys/key_forward.tga"),  ' 32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1735         drawpic(pos + '-11.5  9   0', ((pressedkeys & KEY_JUMP) ? "gfx/hud/keys/key_jump_inv.tga" : "gfx/hud/keys/key_jump.tga"),     ' 24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1736         drawpic(pos + ' -1   32   0', ((pressedkeys & KEY_LEFT) ? "gfx/hud/keys/key_left_inv.tga" : "gfx/hud/keys/key_left.tga"),     ' 32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1737         drawpic(pos + ' 32   32   0', ((pressedkeys & KEY_BACKWARD) ? "gfx/hud/keys/key_backward_inv.tga" : "gfx/hud/keys/key_backward.tga"), ' 32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1738         drawpic(pos + ' 65   32   0', ((pressedkeys & KEY_RIGHT) ? "gfx/hud/keys/key_right_inv.tga" : "gfx/hud/keys/key_right.tga"),    ' 32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1739 }
1740
1741 float GetAmmoStat(float i)
1742 {
1743         switch(i)
1744         {
1745                 case 0: return STAT_SHELLS;
1746                 case 1: return STAT_NAILS;
1747                 case 2: return STAT_ROCKETS;
1748                 case 3: return STAT_CELLS;
1749                 case 4: return STAT_FUEL;
1750                 default: return -1;
1751         }
1752 }
1753
1754 float GetAmmoItemCode(float i)
1755 {
1756         switch(i)
1757         {
1758                 case 0: return IT_SHELLS;
1759                 case 1: return IT_NAILS;
1760                 case 2: return IT_ROCKETS;
1761                 case 3: return IT_CELLS;
1762                 case 4: return IT_FUEL;
1763                 default: return -1;
1764         }
1765 }
1766
1767 string GetAmmoPicture(float i)
1768 {
1769         switch(i)
1770         {
1771                 case 0: return "gfx/hud/sb_shells";
1772                 case 1: return "gfx/hud/sb_bullets";
1773                 case 2: return "gfx/hud/sb_rocket";
1774                 case 3: return "gfx/hud/sb_cells";
1775                 case 4: return "gfx/hud/sb_fuel";
1776                 default: return "";
1777         }
1778 }
1779
1780 void Sbar_Draw (void)
1781 {
1782         // vectors for top right, bottom right, bottom and bottom left corners
1783
1784         vector topright;
1785         vector bottom;
1786         vector bottomright;
1787         vector bottomleft;
1788         
1789         topright_x = vid_conwidth;
1790         topright_y = 0;
1791         topright_z = 0;
1792
1793         bottom_x = vid_conwidth/2;
1794         bottom_y = vid_conheight;
1795         bottom_z = 0;
1796         
1797         bottomright_x = vid_conwidth;
1798         bottomright_y = vid_conheight;
1799         bottomright_z = 0;
1800
1801         bottomleft_x = 0;
1802         bottomleft_y = vid_conheight;
1803         bottomleft_z = 0;
1804         
1805         sbar_alpha_bg = cvar("sbar_alpha_bg");
1806         
1807         float i;
1808         float x, fade;
1809         float stat_items, stat_weapons;
1810         
1811         vector o; o = '1 0 0' * vid_conwidth;
1812         o_y = 28; // move spectator text slightly down to prevent overlapping the timer
1813         
1814         string s;
1815
1816         sbar_fontsize = Sbar_GetFontsize("sbar_fontsize");
1817         
1818         if(spectatee_status && !intermission)
1819         {
1820                 if(spectatee_status == -1)
1821                         s = "^1Observing";
1822                 else
1823                         s = strcat("^1Spectating ^7", GetPlayerName(spectatee_status - 1));
1824                 o = Sbar_DrawNoteLine(o, s);
1825
1826                 if(spectatee_status == -1)
1827                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 to spectate");
1828                 else
1829                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 for another player");
1830                 o = Sbar_DrawNoteLine(o, s);
1831
1832                 if(spectatee_status == -1)
1833                         s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
1834                 else
1835                         s = strcat("^1Press ^3", getcommandkey("secondary fire", "+attack2"), "^1 to observe");
1836                 o = Sbar_DrawNoteLine(o, s);
1837
1838                 s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
1839                 o = Sbar_DrawNoteLine(o, s);
1840
1841                 if(gametype == GAME_ARENA)
1842                         s = "^1Wait for your turn to join";
1843                 else if(gametype == GAME_LMS)
1844                 {
1845                         entity sk;
1846                         sk = playerslots[player_localentnum - 1];
1847                         if(sk.(scores[ps_primary]) >= 666)
1848                                 s = "^1Match has already begun";
1849                         else if(sk.(scores[ps_primary]) > 0)
1850                                 s = "^1You have no more lives left";
1851                         else
1852                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1853                 }
1854                 else
1855                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1856                 o = Sbar_DrawNoteLine(o, s);
1857                 
1858                 //show restart countdown:
1859                 if (time < getstatf(STAT_GAMESTARTTIME)) {
1860                         float countdown;
1861                         //we need to ceil, otherwise the countdown would be off by .5 when using round()
1862                         countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
1863                         s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
1864                         o = Sbar_DrawNoteLine(o, s);
1865                 }
1866         }
1867         if(warmup_stage && !intermission)
1868         {
1869                 s = "^2Currently in ^1warmup^2 stage!";
1870                 o = Sbar_DrawNoteLine(o, s);
1871         }
1872
1873         // move more important stuff more to the middle so its more visible
1874         o_y = vid_conheight * 0.66;
1875
1876         string blinkcolor;
1877         if(mod(time, 1) >= 0.5)
1878                 blinkcolor = "^1";
1879         else
1880                 blinkcolor = "^3";
1881
1882         if(ready_waiting && !intermission)
1883         {
1884                 if(ready_waiting_for_me)
1885                 {
1886                         if(warmup_stage) 
1887                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
1888                         else
1889                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
1890                 }
1891                 else
1892                 {
1893                         if(warmup_stage) 
1894                                 s = strcat("^2Waiting for others to ready up to end warmup...");
1895                         else
1896                                 s = strcat("^2Waiting for others to ready up...");
1897                 }
1898                 o = Sbar_DrawNoteLine(o, s);
1899         }
1900         else if(warmup_stage && !intermission) 
1901         {
1902                 s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
1903                 o = Sbar_DrawNoteLine(o, s);
1904         }
1905         if(vote_waiting)
1906         {
1907                 s = strcat("^2A vote has been called for ^1", vote_called_vote);
1908                 o = Sbar_DrawNoteLine(o, s);
1909
1910                 if(vote_waiting_for_me)
1911                 {
1912                         s = strcat(blinkcolor, "Press ^3", getcommandkey("vote yes", "vyes"), blinkcolor, " to accept");
1913                         o = Sbar_DrawNoteLine(o, s);
1914
1915                         s = strcat(blinkcolor, "Press ^3", getcommandkey("vote no", "vno"), blinkcolor, " to reject");
1916                         o = Sbar_DrawNoteLine(o, s);
1917
1918                         s = strcat(blinkcolor, "Press ^3", getcommandkey("vote abstain", "vabstain"), blinkcolor, " to abstain");
1919                         o = Sbar_DrawNoteLine(o, s);
1920                 }
1921                 else
1922                 {
1923                         s = strcat("^2Waiting for others to vote...");
1924                         o = Sbar_DrawNoteLine(o, s);
1925                 }
1926         }
1927         if(teamplay && !intermission)
1928         {
1929                 entity tm;
1930                 float ts_min, ts_max;
1931                 tm = teams.sort_next;
1932                 if (tm)
1933                 {
1934                         for(; tm.sort_next; tm = tm.sort_next)
1935                         {
1936                                 if(!tm.team_size || tm.team == COLOR_SPECTATOR)
1937                                         continue;
1938                                 if(!ts_min) ts_min = tm.team_size;
1939                                 else ts_min = min(ts_min, tm.team_size);
1940                                 if(!ts_max) ts_max = tm.team_size;
1941                                 else ts_max = max(ts_max, tm.team_size);
1942                         }
1943                         if ((ts_max - ts_min) > 1)
1944                         {
1945                                 s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
1946                                 tm = GetTeam(myteam, false);
1947                                 if (tm)
1948                                 if (tm.team != COLOR_SPECTATOR)
1949                                 if (tm.team_size == ts_max)
1950                                         s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
1951
1952                                 o = Sbar_DrawNoteLine(o, s);
1953                         }
1954                 }
1955         }
1956
1957         Sbar_UpdatePlayerTeams();
1958
1959         if (intermission == 1)
1960         {
1961                 Sbar_DrawScoreboard();
1962                 Sbar_DrawCenterPrint();
1963                 return;
1964         }
1965         else if (intermission == 2)
1966         {
1967                 Sbar_FinaleOverlay();
1968                 Sbar_DrawCenterPrint();
1969         }
1970         else
1971         {
1972                 if (sb_showscores_force || (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard")))
1973                 {
1974                         sbar_x = (vid_conwidth - 640.0)*0.5;
1975                         sbar_y = vid_conheight - 47;
1976                         //Sbar_DrawAlphaPic (sbar_x, sbar_y, sb_scorebar, sbar_alpha_bg.value);
1977                         //drawpic('0 0 0', "gfx/hud/scorebar", '0 0 0', '1 1 1', cvar("sbar_alpha_bg"), 0);
1978                         Sbar_DrawScoreboard ();
1979                 }
1980                 else
1981                 {
1982                         if (sb_showscores) // do not hide the hud if sb_showscores is shown
1983                         {
1984                                 Sbar_DrawScoreboard();
1985                                 Sbar_DrawCenterPrint();
1986                         }
1987                         
1988                         float armor, health;
1989                         armor = getstati(STAT_ARMOR);
1990                         health = getstati(STAT_HEALTH);
1991
1992                         stat_items = getstati(STAT_ITEMS);
1993                         stat_weapons = getstati(STAT_WEAPONS);
1994
1995                         fade = 3.2 - 2 * (time - weapontime);
1996                         fade = bound(0.7, fade, 1);
1997                         
1998                         vector bg_size; // hud background size
1999                         bg_size_x = 800;
2000                         if (vid_conwidth > 800) // if conwidth > 800, resize the background image
2001                                 bg_size_x = vid_conwidth;
2002                         bg_size_y = 58;
2003                         bg_size_z = 0;
2004                         
2005                         vector bgoffset; // hud background offset
2006                         bgoffset_x = 0;
2007                         bgoffset_y = 0;
2008                         bgoffset_z = 0;
2009                         if (vid_conwidth < 800) // if conwidth < 800 we need to offset the background image to keep it centered, as it will be scaled up
2010                                 bgoffset_x = (vid_conwidth - 800) / 2;
2011                         
2012                         if (cvar("viewsize") <= 100) {
2013                                 if (teamplay) {
2014                                         //if (myteam == 13) // 13 = blue shirt color
2015                                         //      drawpic(bottomleft - '0 58 0' + bgoffset, "gfx/hud/sbar", bg_size, '0 0.3 1', sbar_alpha_bg, 0); // blue hud is too dark at '0 0 1'
2016                                         //else
2017                                                 drawpic(bottomleft - '0 58 0' + bgoffset, "gfx/hud/sbar", bg_size, GetTeamRGB(myteam) * cvar("sbar_color_bg_team"), sbar_alpha_bg, 0); // hud color = myteam color
2018                                         }
2019                                 else {
2020                                         // allow for custom HUD colors in non-teamgames
2021                                         color_x = cvar("sbar_color_bg_r");
2022                                         color_y = cvar("sbar_color_bg_g");
2023                                         color_z = cvar("sbar_color_bg_b");
2024                                 
2025                                         drawpic(bottomleft - '0 58 0' + bgoffset, "gfx/hud/sbar", bg_size, color, sbar_alpha_bg, 0);
2026                                 }
2027                         }
2028                         
2029                         if(health != 0)
2030                         {
2031                                 if(sbar_hudselector == 2) // combined health and armor display
2032                                 {
2033                                         // NOTE: we'll always choose the SMALLER value...
2034                                         float healthdamage, armordamage, armorideal;
2035                                         healthdamage = (health - 1) / (1 - armorblockpercent); // damage we can take if we could use more health
2036                                         armordamage = armor + (health - 1); // damage we can take if we could use more armor
2037                                         armorideal = healthdamage * armorblockpercent;
2038                                         
2039                                         vector num_pos;
2040                                         num_pos = bottom - element_offset - '0 24 0' + '-96 0 0';
2041                                         
2042                                         if(armordamage < healthdamage)
2043                                         {
2044                                                 // here, armorideal > armor
2045                                                 x = floor(armordamage + 1);
2046                                                 drawpic(num_pos + '78 -3 0', "gfx/hud/sb_health", '32 32 0', '1 1 1', sbar_alpha_fg, 0);
2047                                                 drawpic(num_pos + '108 -3 0', "gfx/hud/sb_armor", '20 20 0', '1 1 1', sbar_alpha_fg * armor / armorideal, 0);
2048                                         }
2049                                         else
2050                                         {
2051                                                 x = floor(healthdamage + 1);
2052                                                 drawpic(num_pos + '108 -3 0', "gfx/hud/sb_health", '20 20 0', '1 1 1', sbar_alpha_fg * armorideal / armor, 0);
2053                                                 drawpic(num_pos + '78 -3 0', "gfx/hud/sb_armor", '32 32 0', '1 1 1', sbar_alpha_fg, 0);
2054                                         }
2055                                         Sbar_DrawXNum_Colored(num_pos, x, 24); // draw the combined health and armor
2056                                 }
2057                                 
2058                                 else
2059                                 {
2060                                         vector health_pos, armor_pos;
2061                                         
2062                                         if (sbar_hudselector == 0) { // old style layout with armor left of health
2063                                                 health_pos = bottom - element_offset - '0 24 0' + '14 0 0';
2064                                                 armor_pos = bottom - element_offset - '0 24 0' + '-96 0 0';
2065                                         }
2066                                         
2067                                         else {
2068                                                 health_pos = bottom - element_offset - '0 24 0' + '-96 0 0';
2069                                                 armor_pos = bottom - element_offset - '0 24 0' + '14 0 0';
2070                                         }
2071                                         
2072                                         // armor
2073                                         x = armor;
2074                                         if (x > 0)
2075                                         {
2076                                                 drawpic(armor_pos + '78 -3 0', "gfx/hud/sb_armor", '32 32 0', '1 1 1', sbar_alpha_fg, 0);       
2077                                                 
2078                                                 Sbar_DrawXNum_Colored(armor_pos, x, 24);
2079                                         }
2080                                         
2081                                         // health
2082                                         x = health;
2083                                         drawpic(health_pos + '78 -3 0', "gfx/hud/sb_health", '32 32 0', '1 1 1', sbar_alpha_fg, 0);
2084                                         
2085                                         Sbar_DrawXNum_Colored(health_pos, x, 24);
2086                                 }
2087                                 
2088                                 // weapon icons
2089                                 x = 1.0;
2090                                 Sbar_DrawWeapon_Clear();
2091                                 for(i = 1; i <= 24; ++i)
2092                                 {
2093                                         if(weaponimpulse[i-1] >= 0)
2094                                         if(stat_weapons & x)
2095                                         {
2096                                                 Sbar_DrawWeapon(i-1, fade, (i == activeweapon));
2097                                         }
2098                                         x *= 2;
2099                                 }
2100                                 
2101                                 // ammo
2102                                 float a; // i will be the ammo type (already declared), a will contain how much ammo there is of type i
2103                                 vector pos;
2104                                 pos_z = 0;
2105                                 
2106                                 for (i = 0; i < 4; ++i) {
2107                                         a = getstati(GetAmmoStat(i)); // how much ammo do we have of type i? 
2108                                         
2109                                         if (a > 0) {
2110                                                 pos = '0 0 0';
2111                                                 if (cvar("vid_conwidth") >= 800)
2112                                                         switch (i) {
2113                                                                 case 0: pos_x = 114; pos_y = -48; break; // shells
2114                                                                 case 1: pos_x = 114; pos_y = -26; break; // bullets
2115                                                                 case 2: pos_x = 200; pos_y = -48; break; // rockets
2116                                                                 case 3: pos_x = 200; pos_y = -26; break; // cells
2117                                                         }
2118                                                 else // if vid_conwidth is lower than 800, ammo will overlap with weapon icons and health so we'll move it to the right
2119                                                         switch (i) {
2120                                                                 case 0: pos_x = vid_conwidth - 158; pos_y = -150; break; // shells
2121                                                                 case 1: pos_x = vid_conwidth - 158; pos_y = -128; break; // bullets
2122                                                                 case 2: pos_x = vid_conwidth - 84; pos_y = -150; break; // rockets
2123                                                                 case 3: pos_x = vid_conwidth - 84; pos_y = -128; break; // cells
2124                                                         }
2125                                                         
2126                                                 if (stat_items & GetAmmoItemCode(i))
2127                                                         drawpic(bottomleft + pos + '0 1.5 0', "gfx/hud/sb_ammobg", '80 22 0', '1 1 1', sbar_alpha_fg, 0);
2128                                                 drawpic(bottomleft + pos + '56 3 0', GetAmmoPicture(i), '18 18 0', '1 1 1', sbar_alpha_fg, 0);
2129                                                 if (a > 10)
2130                                                         Sbar_DrawXNum(bottomleft + pos + '6 4 0', a, 3, 16, '0.6 0.7 0.8', 0, 1, 0);
2131                                                 else
2132                                                         Sbar_DrawXNum(bottomleft + pos + '6 4 0', a, 3, 16, '0.7 0 0', 0, 1, 0);
2133                                         }
2134                                 }
2135                                         
2136                                 // fuel ammo
2137                                 a = getstati(GetAmmoStat(4)); // how much fuel do we have? 
2138                                 
2139                                 if (a > 0) { // if we have fuel, draw the amount
2140                                         float invincibility_time, dt;
2141                                         invincibility_time = getstatf(STAT_INVINCIBLE_FINISHED);
2142                                         dt = invincibility_time - time;
2143                                         if (dt > 0) { // if the invincibility timer is active, draw fuel ammo elsewhere
2144                                                 pos_x = bottom_x + 140;
2145                                                 pos_y = bottom_y - 72;
2146                                         }
2147                                         else { // if the invincibility timer is inactive, draw the fuel ammo there (it's rare to have invincibility + fuel anyway)
2148                                                 pos_x = bottom_x + 140;
2149                                                 pos_y = bottom_y - 22;
2150                                         }
2151                                         drawpic(pos - '0 2 0' + '52 0 0', GetAmmoPicture(4), '20 20 0', '1 1 1', sbar_alpha_fg, 0);
2152                                         if (a > 10)
2153                                                 Sbar_DrawXNum(pos, a, 3, 16, '0.6 0.7 0.8', 0, 1, 0);
2154                                         else
2155                                                 Sbar_DrawXNum(pos, a, 3, 16, '0.7 0 0', 0, 1, 0);
2156                                 }
2157                                 
2158                                 // draw scores and timer
2159                                 Sbar_Score(16); 
2160                         }
2161                         
2162                         //show strength/invincibility ICON and timer:
2163                         CSQC_Strength_Timer();
2164
2165                         if(gametype == GAME_KEYHUNT)
2166                         {
2167                                 CSQC_kh_hud();
2168                         } else if(gametype == GAME_CTF)
2169                         {
2170                                 CSQC_ctf_hud();
2171                         } else if(gametype == GAME_NEXBALL)
2172                         {
2173                                 CSQC_nb_hud();
2174                         }
2175                 }
2176         }
2177 }
2178
2179 // CTF HUD
2180 void CSQC_ctf_hud(void)
2181 {
2182         vector bottomleft;
2183         bottomleft_x = 0;
2184         bottomleft_y = vid_conheight;
2185         bottomleft_z = 0;
2186
2187         float redflag, blueflag;
2188         float stat_items;
2189         
2190         stat_items = getstati(STAT_ITEMS);
2191         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
2192         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
2193         
2194         if (myteam == COLOR_TEAM1) { // always draw own flag on left
2195                 switch(redflag)
2196                 {
2197                 case 1: drawpic(bottomleft - element_offset - '-4 36 0', "gfx/hud/sb_flag_red_taken", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2198                 case 2: drawpic(bottomleft - element_offset - '-4 36 0', "gfx/hud/sb_flag_red_lost", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2199                 case 3: drawpic(bottomleft - element_offset - '-4 36 0', "gfx/hud/sb_flag_red_carrying", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2200                 default:
2201                         if(stat_items & IT_CTF_SHIELDED)
2202                                 if(myteam == COLOR_TEAM2)
2203                                         drawpic(bottomleft - element_offset - '-4 36 0', "gfx/hud/sb_flag_red_shielded", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2204                 }
2205
2206                 switch(blueflag)
2207                 {
2208                 case 1: drawpic(bottomleft - element_offset - '-72 36 0', "gfx/hud/sb_flag_blue_taken", '48 48 0', '1 1 1', sbar_alpha_fg, 0); break;
2209                 case 2: drawpic(bottomleft - element_offset - '-72 36 0', "gfx/hud/sb_flag_blue_lost", '48 48 0', '1 1 1', sbar_alpha_fg, 0); break;
2210                 case 3: drawpic(bottomleft - element_offset - '-72 36 0', "gfx/hud/sb_flag_blue_carrying", '48 48 0', '1 1 1', sbar_alpha_fg, 0); break;
2211                 default:
2212                         if(stat_items & IT_CTF_SHIELDED)
2213                                 if(myteam == COLOR_TEAM1)
2214                                         drawpic(bottomleft - element_offset - '-72 36 0', "gfx/hud/sb_flag_blue_shielded", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2215                 }
2216         }
2217         
2218         else {
2219                 switch(blueflag)
2220                 {
2221                 case 1: drawpic(bottomleft - element_offset - '-4 36 0', "gfx/hud/sb_flag_blue_taken", '48 48 0', '1 1 1', sbar_alpha_fg, 0); break;
2222                 case 2: drawpic(bottomleft - element_offset - '-4 36 0', "gfx/hud/sb_flag_blue_lost", '48 48 0', '1 1 1', sbar_alpha_fg, 0); break;
2223                 case 3: drawpic(bottomleft - element_offset - '-4 36 0', "gfx/hud/sb_flag_blue_carrying", '48 48 0', '1 1 1', sbar_alpha_fg, 0); break;
2224                 default:
2225                         if(stat_items & IT_CTF_SHIELDED)
2226                                 if(myteam == COLOR_TEAM1)
2227                                         drawpic(bottomleft - element_offset - '-4 36 0', "gfx/hud/sb_flag_blue_shielded", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2228                 }
2229                 
2230                 switch(redflag)
2231                 {
2232                 case 1: drawpic(bottomleft - element_offset - '-72 36 0', "gfx/hud/sb_flag_red_taken", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2233                 case 2: drawpic(bottomleft - element_offset - '-72 36 0', "gfx/hud/sb_flag_red_lost", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2234                 case 3: drawpic(bottomleft - element_offset - '-72 36 0', "gfx/hud/sb_flag_red_carrying", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2235                 default:
2236                         if(stat_items & IT_CTF_SHIELDED)
2237                                 if(myteam == COLOR_TEAM2)
2238                                         drawpic(bottomleft - element_offset - '-72 36 0', "gfx/hud/sb_flag_red_shielded", '48 48 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL); break;
2239                 }
2240         }
2241 }
2242
2243 // Keyhunt HUD
2244 void CSQC_kh_hud(void)
2245 {
2246         float kh_keys, kh_keys_status, kh_teams_set;
2247         vector red_pos, blue_pos, yellow_pos, pink_pos, kh_size;
2248         
2249         vector bottomleft;
2250         bottomleft_x = 0;
2251         bottomleft_y = vid_conheight;
2252         bottomleft_z = 0;
2253
2254         red_pos_x = 6;
2255         red_pos_y = vid_conheight - 35 - 6;
2256         red_pos_z = 0;
2257         
2258         blue_pos_x = 6 + (24 * 1);
2259         blue_pos_y = vid_conheight - 35 - 6;
2260         blue_pos_z = 0;
2261         
2262         yellow_pos_x = 6 + (24 * 2);
2263         yellow_pos_y = vid_conheight - 35 - 6;
2264         yellow_pos_z = 0;
2265         
2266         pink_pos_x = 6 + (24 * 3);
2267         pink_pos_y = vid_conheight - 35 - 6;
2268         pink_pos_z = 0;
2269
2270         kh_keys = getstati(STAT_KH_KEYS);
2271         kh_keys_status = kh_keys / 256;
2272         kh_teams_set = cvar("_teams_available");  // set in keyhunt.qc
2273
2274         kh_size = '22 35 0';
2275
2276         if (kh_keys_status & 1)  // red
2277                 drawpic (red_pos, "gfx/hud/sb_kh_red", kh_size, '1 1 1', 0.3, 0);  // show 30% alpha key
2278         if (kh_keys & 1)
2279                 drawpic (red_pos, "gfx/hud/sb_kh_red", kh_size, '1 1 1', 1.0, 0);  // show solid key 100% alpha
2280
2281         if (kh_keys_status & 2)  // blue
2282                 drawpic (blue_pos, "gfx/hud/sb_kh_blue", kh_size, '1 1 1', 0.3, 0);
2283         if (kh_keys & 2)
2284                 drawpic (blue_pos, "gfx/hud/sb_kh_blue", kh_size, '1 1 1', 1.0, 0);
2285
2286         if (kh_teams_set & 4)  // yellow
2287         {
2288                 if (kh_keys_status & 4)
2289                         drawpic (yellow_pos, "gfx/hud/sb_kh_yellow", kh_size, '1 1 1', 0.3, 0);
2290                 if (kh_keys & 4)
2291                         drawpic (yellow_pos, "gfx/hud/sb_kh_yellow", kh_size, '1 1 1', 1.0, 0);
2292         }
2293
2294         if (kh_teams_set & 8)  // pink
2295         {
2296                 if (kh_keys_status & 8)
2297                         drawpic (pink_pos, "gfx/hud/sb_kh_pink", kh_size, '1 1 1', 0.3, 0);
2298                 if (kh_keys & 8)
2299                         drawpic (pink_pos, "gfx/hud/sb_kh_pink", kh_size, '1 1 1', 1.0, 0);
2300         }
2301 }
2302
2303 //Nexball HUD
2304 #define NBPB_SIZE '96 38 0'
2305 #define NBPB_BT 2                   //thickness
2306 #define NBPB_BRGB '1 1 1'
2307 #define NBPB_BALPH 1                //alpha
2308 #define NBPB_BFLAG DRAWFLAG_NORMAL
2309 #define NBPB_IALPH 0.4
2310 #define NBPB_IFLAG DRAWFLAG_NORMAL
2311 #define NBPB_IRGB '0.7 0.1 0'
2312
2313 void CSQC_nb_hud(void)
2314 {
2315         float stat_items, nb_pb_starttime, dt, p;
2316         vector pos;
2317         
2318         stat_items = getstati(STAT_ITEMS);
2319         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
2320         
2321         pos_x = 4;
2322         pos_y = vid_conheight - 42;
2323         pos_z = 0;
2324
2325         //Manage the progress bar if any
2326         if (nb_pb_starttime > 0)
2327         {
2328                 vector s;
2329                 dt = mod(time - nb_pb_starttime, nb_pb_period);
2330                 // one period of positive triangle
2331                 p = 2 * dt / nb_pb_period;
2332                 if (p > 1)
2333                         p = 2 - p;
2334
2335                 s = NBPB_SIZE;
2336                 //Draw the filling
2337                 drawfill(pos, p * s_x * '1 0 0' + s_y * '0 1 0', NBPB_IRGB, NBPB_IALPH, NBPB_IFLAG);
2338
2339                 //Draw the box
2340                 s = NBPB_SIZE;
2341                 drawline(NBPB_BT, pos    , pos + '1 0 0' * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2342                 drawline(NBPB_BT, pos    , pos + '0 1 0' * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2343                 drawline(NBPB_BT, pos + s, pos + '1 0 0' * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2344                 drawline(NBPB_BT, pos + s, pos + '0 1 0' * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2345         }
2346         
2347         pos_x += 12; //horizontal margin to the picture
2348         pos_y += 2; //vertical margin to the picture
2349         
2350         if (stat_items & IT_KEY1)
2351                 drawpic(pos, "gfx/hud/sb_nexball_carrying", '80 34 0', '1 1 1', 1, DRAWFLAG_NORMAL);
2352 }