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