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