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