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