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