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