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