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