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