new packet loss column ("pl")
[divverent/nexuiz.git] / data / qcsrc / client / sbar.qc
1
2 float last_weapon;
3 float activeweapon;
4 float weapontime;
5
6 float sb_lines; // still don't know what to do with that NOTE: check dp's sbar.c to see what that should be
7
8 vector sbar;
9 float sbar_alpha_fg;
10 float sbar_hudselector;
11 /*
12 entity sortedPlayers;
13 entity sortedTeams;
14
15 .float sb_frags;
16 .float sb_team;
17 .float sb_player;
18 .float sb_caps;
19 */
20 float ps_primary, ps_secondary;
21 float ts_primary, ts_secondary;
22
23 void CSQC_kh_hud();
24 void CSQC_ctf_hud();
25 void MapVote_Draw();
26 void Sbar_FinaleOverlay()
27 {
28         /*vector pos;
29         pos_x = (vid_conwidth - 1)/2;
30         pos_y = 16;
31         pos_z = 0;*/
32         
33         //drawpic(pos, "gfx/finale", '0 0 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL);
34         
35         //drawstring(pos, "END", sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
36         MapVote_Draw();
37 }
38
39 void Sbar_DrawWeapon(float nr, float fade, float active)
40 {
41         vector pos, vsize, color;
42         float value;
43         
44         value = (active) ? 1 : 0.6;
45         color_x = color_y = color_z = value;
46         
47         if(sbar_hudselector == 1)
48         {
49                 // width = 300, height = 100
50                 const float w_width = 32, w_height = 12, w_space = 2, font_size = 8;
51                 
52                 pos_x = (vid_conwidth - w_width * 9) * 0.5 + w_width * nr;
53                 pos_y = (vid_conheight - w_height);
54                 pos_z = 0;
55                 vsize_x = w_width;
56                 vsize_y = w_height;
57                 vsize_z = 0;
58                 drawpic(pos, strcat("gfx/inv_weapon", ftos(nr)), vsize, color, value * fade * sbar_alpha_fg, 0);
59                 pos_x += w_space;
60                 pos_y += w_space;
61                 vsize_x = font_size;
62                 vsize_y = font_size;
63                 vsize_z = 0;
64                 drawstring(pos, ftos(nr+1), vsize, '1 1 0', sbar_alpha_fg, 0);
65
66         }
67         else
68         {
69                 // width = 300, height = 100
70                 const float w2_width = 300, w2_height = 100, w2_space = 10;
71                 const float w2_scale = 0.4;
72
73                 pos_x = vid_conwidth - (w2_width + w2_space) * w2_scale;
74                 pos_y = (w2_height + w2_space) * w2_scale * nr + w2_space;
75                 pos_z = 0;
76                 vsize_x = w2_width * w2_scale;
77                 vsize_y = w2_height * w2_scale;
78                 vsize_z = 0;
79                 
80                 drawpic(pos, strcat("gfx/inv_weapon", ftos(nr)), vsize, color, value * fade * sbar_alpha_fg, 0);
81         }
82 }
83 void Sbar_DrawXNum (vector pos, float num, float digits, float lettersize, vector rgb, float a, float dflags)
84 {
85         float l, i;
86         string str, tmp;
87         float minus;
88         vector vsize;
89
90         vsize_x = vsize_y = lettersize;
91         vsize_z = 0;
92
93         if(num < 0)
94         {
95                 minus = true;
96                 num = -num;
97                 pos_x -= lettersize;
98         } else
99                 minus = false;
100         
101         if(digits < 0)
102         {
103                 tmp = ftos(num);
104                 digits = -digits;
105                 str = strcat(substring("0000000000", 0, digits - strlen(tmp)), tmp);
106         } else
107                 str = ftos(num);
108         
109         l = strlen(str);
110
111         if(l > digits)
112         {
113                 str = substring(str, l-digits, 999);
114                 l = strlen(str);
115         } else if(l < digits)
116                 pos_x += (digits-l) * lettersize;
117
118         if(minus)
119         {
120                 drawpic(sbar + pos, "gfx/num_minus", vsize, rgb, a * sbar_alpha_fg, dflags);
121                 pos_x += lettersize;
122         }
123
124         for(i = 0; i < l; ++i)
125         {
126                 drawpic(sbar + pos, strcat("gfx/num_", substring(str, i, 1)), vsize, rgb, a * sbar_alpha_fg, dflags);
127                 pos_x += lettersize;
128         }
129 }
130
131 void Cmd_Sbar_SetFields(float argc);
132 void Sbar_InitScores()
133 {
134         float i, f, primary_prio, secondary_prio;
135
136         ps_primary = ps_secondary = ts_primary = ts_secondary = -1;
137         for(i = 0; i < MAX_SCORE; ++i)
138         {
139                 f = (scores_flags[i] & SFL_SORT_PRIO_MASK);
140                 if(f == SFL_SORT_PRIO_PRIMARY)
141                         ps_primary = i;
142                 if(f == SFL_SORT_PRIO_SECONDARY)
143                         ps_secondary = i;
144         }
145         if(ps_secondary == -1)
146                 ps_secondary = ps_primary;
147         
148         for(i = 0; i < MAX_TEAMSCORE; ++i)
149         {
150                 f = (teamscores_flags[i] & SFL_SORT_PRIO_MASK);
151                 if(f == SFL_SORT_PRIO_PRIMARY)
152                         ts_primary = i;
153                 if(f == SFL_SORT_PRIO_SECONDARY)
154                         ts_secondary = i;
155         }
156         if(ts_secondary == -1)
157                 ts_secondary = ts_primary;
158
159         Cmd_Sbar_SetFields(0);
160 }
161
162 void Sbar_UpdatePlayerPos(entity pl);
163 float SetTeam(entity pl, float Team);
164 //float lastpnum;
165 void Sbar_UpdatePlayerTeams()
166 {
167         float Team;
168         entity pl, tmp;
169         float num;
170
171         num = 0;
172         for(pl = players.sort_next; pl; pl = pl.sort_next)
173         {
174                 num += 1;
175                 Team = GetPlayerColor(pl.sv_entnum);
176                 if(SetTeam(pl, Team))
177                 {
178                         tmp = pl.sort_prev;
179                         Sbar_UpdatePlayerPos(pl);
180                         if(tmp)
181                                 pl = tmp;
182                         else
183                                 pl = players.sort_next;
184                 }
185         }
186         /*
187         if(num != lastpnum)
188                 print(strcat("PNUM: ", ftos(num), "\n"));
189         lastpnum = num;
190         */
191 }
192
193 float Sbar_ComparePlayerScores(entity left, entity right)
194 {
195         float vl, vr;
196         vl = GetPlayerColor(left.sv_entnum);
197         vr = GetPlayerColor(right.sv_entnum);
198         
199         if(vl > vr)
200                 return true;
201         if(vl < vr)
202                 return false;
203
204         vl = left.scores[ps_primary];
205         vr = right.scores[ps_primary];
206         if(vl > vr)
207                 return IS_INCREASING(scores_flags[ps_primary]);
208         if(vl < vr)
209                 return IS_DECREASING(scores_flags[ps_primary]);
210         
211         vl = left.scores[ps_secondary];
212         vr = right.scores[ps_secondary];
213         if(vl > vr)
214                 return IS_INCREASING(scores_flags[ps_secondary]);
215         if(vl < vr)
216                 return IS_DECREASING(scores_flags[ps_secondary]);
217         
218         return false;
219 }
220
221 void Sbar_UpdatePlayerPos(entity player)
222 {
223         for(other = player.sort_next; other && Sbar_ComparePlayerScores(player, other); other = player.sort_next)
224         {
225                 SORT_SWAP(player, other);
226         }
227         for(other = player.sort_prev; other != players && Sbar_ComparePlayerScores(other, player); other = player.sort_prev)
228         {
229                 SORT_SWAP(other, player);
230         }
231 }
232
233 float Sbar_CompareTeamScores(entity left, entity right)
234 {
235         float vl, vr;
236
237         if(left.team == COLOR_SPECTATOR)
238                 return 1;
239         if(right.team == COLOR_SPECTATOR)
240                 return 0;
241         
242         vl = left.teamscores[ts_primary];
243         vr = right.teamscores[ts_primary];
244         if(vl > vr)
245                 return IS_INCREASING(teamscores_flags[ts_primary]);
246         if(vl < vr)
247                 return IS_DECREASING(teamscores_flags[ts_primary]);
248         
249         vl = left.teamscores[ts_secondary];
250         vr = right.teamscores[ts_secondary];
251         if(vl > vr)
252                 return IS_INCREASING(teamscores_flags[ts_secondary]);
253         if(vl < vr)
254                 return IS_DECREASING(teamscores_flags[ts_secondary]);
255
256         return false;
257 }
258
259 void Sbar_UpdateTeamPos(entity Team)
260 {
261         for(other = Team.sort_next; other && Sbar_CompareTeamScores(Team, other); other = Team.sort_next)
262         {
263                 SORT_SWAP(Team, other);
264         }
265         for(other = Team.sort_prev; other != teams && Sbar_CompareTeamScores(other, Team); other = Team.sort_prev)
266         {
267                 SORT_SWAP(other, Team);
268         }
269 }
270
271 void Cmd_Sbar_Help(float argc)
272 {
273         print("You can modify the scoreboard using the\n");
274         print("^3|---------------------------------------------------------------|\n");
275         print("^1 TO BE DONE\n");
276         print("Usage:\n");
277         print("^2sbar_columns_set default\n");
278         print("^2sbar_columns_set ^7filed1 field2 ...\n");
279         print("The following field names are recognized (case INsensitive):\n");
280         print("You can use a ^3|^7 to start the right-aligned fields.\n");
281         
282         print("^3name^7 or ^3nick^7             Name of a player\n");
283         print("^3ping^7                     Ping time\n\n");
284         print("^3kd^7 or ^3kdr^7 or ^3kdratio^7 or ^3k/d\n");
285         print("                         The kill-death ratio\n");
286
287         print("Before a field you can put a + or - sign, then a comma separated list\n");
288         print("of game types, then a slash, to make the field show up only in these\n");
289         print("or in all but these game types.\n");
290
291         local float i;
292         print("Additional columns:\n");
293         for(i = 0; i < MAX_SCORE; ++i)
294         {
295                 if(scores_label[i] != "")
296                         print(strcat(scores_label[i], "\n"));
297         }
298 }
299
300 #define MIN_NAMELEN 24
301 #define MAX_NAMELEN 24
302
303 string Sbar_DefaultColumnLayout()
304 {
305         return "ping pl name | -ctf,kh/kills -ctf,kh/deaths +lms/lives +lms/rank +kh,ctf/caps +kh/pushes +kh/destroyed -lms/score";
306 }
307
308 void Cmd_Sbar_SetFields(float argc)
309 {
310         float i, j, slash;
311         string str, pattern, subpattern;
312         float digit;
313         float have_name, have_primary, have_secondary, have_separator;
314         float missing;
315
316         // TODO: re enable with gametype dependant cvars?
317         if(argc < 2) // no arguments provided
318                 argc = tokenizebyseparator(strcat("x ", cvar_string("sbar_columns")), " ");
319
320         if(argc < 2)
321                 argc = tokenizebyseparator(strcat("x ", Sbar_DefaultColumnLayout()), " ");
322
323         if(argc == 2)
324         {
325                 if(argv(1) == "default")
326                         argc = tokenizebyseparator(strcat("x ", Sbar_DefaultColumnLayout()), " ");
327                 else if(argv(1) == "all")
328                 {
329                         string s;
330                         s = "ping name |";
331                         for(i = 0; i < MAX_SCORE; ++i)
332                         {
333                                 if(i != ps_primary)
334                                 if(i != ps_secondary)
335                                 if(scores_label[i] != "")
336                                         s = strcat(s, " ", scores_label[i]);
337                         }
338                         if(ps_secondary != ps_primary)
339                                 s = strcat(s, " ", scores_label[ps_secondary]);
340                         s = strcat(s, " ", scores_label[ps_primary]);
341                         argc = tokenizebyseparator(strcat("x ", s), " ");
342                 }
343         }
344                 
345         
346         argc = min(MAX_SBAR_FIELDS, argc);
347         sbar_num_fields = 0;
348
349         drawfont = sbar_font;
350         digit = stringwidth("0123456789", FALSE) / 10;
351
352         subpattern = strcat(",", GametypeNameFromType(gametype), ",");
353
354         argc = min(argc-1, MAX_SBAR_FIELDS-1);
355         for(i = 0; i < argc; ++i)
356         {
357                 str = argv(i+1);
358
359                 slash = strstrofs(str, "/", 0);
360                 if(slash >= 0)
361                 {
362                         pattern = substring(str, 0, slash);
363                         str = substring(str, slash + 1, strlen(str) - (slash + 1));
364
365                         if(substring(pattern, 0, 1) == "-")
366                         {
367                                 pattern = substring(pattern, 1, strlen(pattern) - 1);
368                                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) >= 0)
369                                         continue;
370                         }
371                         else
372                         {
373                                 if(substring(pattern, 0, 1) == "+")
374                                         pattern = substring(pattern, 1, strlen(pattern) - 1);
375                                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) < 0)
376                                         continue;
377                         }
378                 }
379
380                 strunzone(sbar_title[sbar_num_fields]);
381                 sbar_title[sbar_num_fields] = strzone(str);
382                 sbar_size[sbar_num_fields] = stringwidth(str, FALSE);
383                 str = strtolower(str);
384
385                 if(str == "ping") {
386                         sbar_field[sbar_num_fields] = SP_PING;
387                 } else if(str == "pl") {
388                         sbar_field[sbar_num_fields] = SP_PL;
389                 } else if(str == "name" || str == "nick") {
390                         sbar_field[sbar_num_fields] = SP_NAME;
391                         sbar_size[sbar_num_fields] = MIN_NAMELEN; // minimum size? any use?
392                         have_name = 1;
393                 } else if(str == "|") {
394                         sbar_field[sbar_num_fields] = SP_SEPARATOR;
395                         have_separator = 1;
396                 } else {
397                         for(j = 0; j < MAX_SCORE; ++j)
398                                 if(str == strtolower(scores_label[j]))
399                                         goto found; // sorry, but otherwise fteqcc -O3 miscompiles this and warns about "unreachable code"
400 :notfound
401                         print(strcat("^1Error:^7 Unknown score field: '", str, "'\n"));
402                         continue;
403 :found
404                         sbar_field[sbar_num_fields] = j;
405                         if(j == ps_primary)
406                                 have_primary = 1;
407                         if(j == ps_secondary)
408                                 have_secondary = 1;
409                 }
410                 ++sbar_num_fields;
411         }
412
413         if(scores_flags[ps_primary] & SFL_ALLOW_HIDE)
414                 have_primary = 1;
415         if(scores_flags[ps_secondary] & SFL_ALLOW_HIDE)
416                 have_secondary = 1;
417         if(ps_primary == ps_secondary)
418                 have_secondary = 1;
419         missing = !have_primary + !have_secondary + !have_separator + !have_name;
420
421         if(sbar_num_fields+missing < MAX_SBAR_FIELDS)
422         {
423                 if(!have_name)
424                 {
425                         strunzone(sbar_title[sbar_num_fields]);
426                         for(i = sbar_num_fields; i > 0; --i)
427                         {
428                                 sbar_title[i] = sbar_title[i-1];
429                                 sbar_size[i] = sbar_size[i-1];
430                                 sbar_field[i] = sbar_field[i-1];
431                         }
432                         sbar_title[0] = strzone("name");
433                         sbar_field[0] = SP_NAME;
434                         sbar_size[0] = MIN_NAMELEN; // minimum size? any use?
435                         ++sbar_num_fields;
436                         print("fixed missing field 'name'\n");
437
438                         if(!have_separator)
439                         {
440                                 strunzone(sbar_title[sbar_num_fields]);
441                                 for(i = sbar_num_fields; i > 1; --i)
442                                 {
443                                         sbar_title[i] = sbar_title[i-1];
444                                         sbar_size[i] = sbar_size[i-1];
445                                         sbar_field[i] = sbar_field[i-1];
446                                 }
447                                 sbar_title[1] = strzone("|");
448                                 sbar_field[1] = SP_SEPARATOR;
449                                 sbar_size[1] = stringwidth("|", FALSE);
450                                 ++sbar_num_fields;
451                                 print("fixed missing field '|'\n");
452                         }
453                 }
454                 else if(!have_separator)
455                 {
456                         strunzone(sbar_title[sbar_num_fields]);
457                         sbar_title[sbar_num_fields] = strzone("|");
458                         sbar_size[sbar_num_fields] = stringwidth("|", FALSE);
459                         sbar_field[sbar_num_fields] = SP_SEPARATOR;
460                         ++sbar_num_fields;
461                         print("fixed missing field '|'\n");
462                 }
463
464                 if(!have_secondary)
465                 {
466                         strunzone(sbar_title[sbar_num_fields]);
467                         sbar_title[sbar_num_fields] = strzone(scores_label[ps_secondary]);
468                         sbar_size[sbar_num_fields] = stringwidth(sbar_title[sbar_num_fields], FALSE);
469                         sbar_field[sbar_num_fields] = ps_secondary;
470                         ++sbar_num_fields;
471                         print("fixed missing field '", scores_label[ps_secondary], "'\n");
472                 }
473
474                 if(!have_primary)
475                 {
476                         strunzone(sbar_title[sbar_num_fields]);
477                         sbar_title[sbar_num_fields] = strzone(scores_label[ps_primary]);
478                         sbar_size[sbar_num_fields] = stringwidth(sbar_title[sbar_num_fields], FALSE);
479                         sbar_field[sbar_num_fields] = ps_primary;
480                         ++sbar_num_fields;
481                         print("fixed missing field '", scores_label[ps_primary], "'\n");
482                 }
483         }
484
485         sbar_field[sbar_num_fields] = SP_END;
486 }
487
488 // MOVEUP::
489 vector sbar_field_rgb;
490 string Sbar_GetField(entity pl, float field)
491 {
492         float tmp, num, denom, f;
493         string str;
494         sbar_field_rgb = '1 1 1';
495         switch(field)
496         {
497                 case SP_PING:
498                         if not(pl.gotscores)
499                                 return "N/A";
500                         str = getplayerkey(pl.sv_entnum, "ping");
501                         tmp = max(0, min(220, stof(str)-80)) / 220;
502                         sbar_field_rgb = '1 1 1' - '0 1 1'*tmp;
503                         return str;
504                 
505                 case SP_PL:
506                         if not(pl.gotscores)
507                                 return "N/A";
508                         str = getplayerkey(pl.sv_entnum, "pl");
509                         if(str == "0")
510                                 return "";
511                         tmp = bound(0, stof(str), 20) / 20; // 20% is REALLY BAD pl
512                         sbar_field_rgb = '1 0.5 0.5' - '0 0.5 0.5'*tmp;
513                         return str;
514                 
515                 case SP_NAME:
516                         return getplayerkey(pl.sv_entnum, "name");
517
518                 case SP_KDRATIO:
519                         num = pl.(scores[SP_KILLS]);
520                         denom = pl.(scores[SP_DEATHS]);
521
522                         if(denom == 0) {
523                                 sbar_field_rgb = '0 1 0';
524                                 str = ftos(num);
525                         } else if(num <= 0) {
526                                 sbar_field_rgb = '1 0 0';
527                                 str = ftos(num/denom);
528                         } else
529                                 str = ftos(num/denom);
530                 
531                         tmp = strstrofs(str, ".", 0);
532                         if(tmp > 0)
533                                 str = substring(str, 0, tmp+2);
534                         return str;
535                         
536                 default:
537                         tmp = pl.(scores[field]);
538                         f = scores_flags[field];
539                         if(field == ps_primary)
540                                 sbar_field_rgb = '1 1 0';
541                         else if(field == ps_secondary)
542                                 sbar_field_rgb = '0 1 1';
543                         else
544                                 sbar_field_rgb = '1 1 1';
545                         if(!tmp)
546                                 if(f & (SFL_HIDE_ZERO | SFL_RANK))
547                                         return "";
548                         if(f & SFL_RANK)
549                         {
550                                 str = ftos(floor(tmp + 0.5));
551                                 num = strlen(str);
552                                 if((num >= 2) && (substring(str, num - 2, 1) == "1"))
553                                         return strcat(str, "th");
554                                 else if(substring(str, num - 1, 1) == "1")
555                                         return strcat(str, "st");
556                                 else if(substring(str, num - 1, 1) == "2")
557                                         return strcat(str, "nd");
558                                 else if(substring(str, num - 1, 1) == "3")
559                                         return strcat(str, "rd");
560                                 else
561                                         return strcat(str, "th");
562                         }
563                         return ftos(tmp);
564         }
565         //return "error";
566 }
567
568 // shamelessly stolen from menu QC :P <- as if I would steal YOUR code pfft ;)
569 float textLengthUpToWidth(string theText, float maxWidth, float handleColors)
570 {
571         // STOP.
572         // The following function is SLOW.
573         // For your safety and for the protection of those around you...
574         // DO NOT CALL THIS AT HOME.
575         // No really, don't.
576         if(stringwidth(theText, handleColors) <= maxWidth)
577                 return strlen(theText); // yeah!
578
579         // binary search for right place to cut string
580         float left, right, middle; // this always works
581         left = 0;
582         right = strlen(theText); // this always fails
583         do
584         {
585                 middle = floor((left + right) / 2);
586                 if(stringwidth(substring(theText, 0, middle), handleColors) <= maxWidth)
587                         left = middle;
588                 else
589                         right = middle;
590         }
591         while(left < right - 1);
592
593         // NOTE: when color codes are involved, this binary search is,
594         // mathematically, BROKEN. However, it is obviously guaranteed to
595         // terminate, as the range still halves each time - but nevertheless, it is
596         // guaranteed that it finds ONE valid cutoff place (where "left" is in
597         // range, and "right" is outside).
598
599         return left;
600 }
601 string textShortenToWidth(string theText, float maxWidth, float handleColors)
602 {
603         if(stringwidth(theText, handleColors) <= maxWidth)
604                 return theText;
605         else
606                 return strcat(substring(theText, 0, textLengthUpToWidth(theText, maxWidth - stringwidth("...", handleColors), handleColors)), "...");
607 }
608
609 float xmin, xmax, ymin, ymax, sbwidth, sbheight;
610
611 void Sbar_PrintScoreboardItem(vector pos, entity pl, float is_self)
612 {
613         vector tmp;
614         string str;
615         float i, field, len;
616         float is_spec;
617         is_spec = (GetPlayerColor(pl.sv_entnum) == COLOR_SPECTATOR);
618
619         // Layout:
620         tmp_z = 0;
621         if(is_self)
622         {
623                 tmp_x = sbwidth;
624                 tmp_y = sbar_fontsize_y;
625                 drawfill(pos - '1 1 0', tmp + '2 2 0', '1 1 1', 0.3, DRAWFLAG_NORMAL);
626         }       
627         tmp_y = 0;
628         
629         for(i = 0; i < sbar_num_fields; ++i)
630         {
631                 field = sbar_field[i];
632                 if(field == SP_SEPARATOR)
633                         break;
634
635                 if(is_spec && field != SP_NAME && field != SP_PING) {
636                         pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
637                         continue;
638                 }
639                 str = Sbar_GetField(pl, field);
640
641                 if(field == SP_NAME)
642                 {
643                         float realsize;
644                         float j;
645                         realsize = sbar_size[i];
646                         if(i+1 < sbar_num_fields)
647                                 if(sbar_field[i+1] == SP_SEPARATOR)
648                                 {
649                                         realsize = (xmax - xmin) / sbar_fontsize_x;
650                                         for(j = 0; j < sbar_num_fields; ++j) if(j != i) if(sbar_field[j] != SP_SEPARATOR)
651                                                 realsize -= sbar_size[j] + 1;
652                                         realsize += 1;
653                                 }
654                         str = textShortenToWidth(str, realsize, TRUE);
655                 }
656                 len = stringwidth(str, TRUE);
657                 
658                 if(sbar_size[i] < len)
659                         sbar_size[i] = len;
660
661                 pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
662
663                 if(field == SP_NAME) {
664                         tmp_x = sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
665                         drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
666                 } else {
667                         tmp_x = len*sbar_fontsize_x + sbar_fontsize_x;
668                         drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
669                 }
670         }
671         
672         if(sbar_field[i] == SP_SEPARATOR)
673         {
674                 pos_x = xmax;
675                 for(i = sbar_num_fields-1; i > 0; --i)
676                 {
677                         field = sbar_field[i];
678                         if(field == SP_SEPARATOR)
679                                 break;
680                         
681                         if(is_spec && field != SP_NAME && field != SP_PING) {
682                                 pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
683                                 continue;
684                         }
685                         
686                         str = Sbar_GetField(pl, field);
687
688                         if(field == SP_NAME)
689                                 str = textShortenToWidth(str, sbar_size[i], TRUE);
690                         len = stringwidth(str, TRUE);
691
692                         if(sbar_size[i] < len)
693                                 sbar_size[i] = len;
694
695                         if(field == SP_NAME) {
696                                 tmp_x = sbar_fontsize_x*len; // left or right aligned? let's put it right...
697                                 drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
698                         } else {
699                                 tmp_x = sbar_fontsize_x*len; //strlen(str);
700                                 drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
701                         }
702                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
703                 }
704         }
705 }
706
707 float lastpingstime;
708 void Sbar_DrawScoreboard()
709 {
710         //float xmin, ymin, xmax, ymax;
711         vector rgb, pos, tmp, sbar_save;
712         entity pl, tm;
713         float specs, i;
714         float center_x;
715
716         if(time > lastpingstime + 10)
717         {
718                 localcmd("pings\n");
719                 lastpingstime = time;
720         }
721
722         sbar_fontsize = Sbar_GetFontsize();
723         if(sbar_fontsize_x == 0)
724                 sbar_fontsize = '8 8 0';
725         if(sbar_fontsize_y == 0)
726                 sbar_fontsize_y = sbar_fontsize_x;
727         
728         xmin = vid_conwidth / 5;
729         ymin = 20;
730
731         xmax = vid_conwidth - xmin;
732         ymax = vid_conheight - 0.2*vid_conheight;
733
734         sbwidth = xmax - xmin;
735         sbheight = ymax - ymin;
736
737         center_x = xmin + 0.5*sbwidth;
738
739         //Sbar_UpdateFields();
740
741         // Initializes position
742         //pos_x = xmin;
743         pos_y = ymin;
744         pos_z = 0;
745
746         // Heading
747         drawfont = sbar_bigfont;
748         pos_x = center_x - stringwidth("Scoreboard", TRUE)*0.5*24;
749         drawstring(pos, "Scoreboard", '24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
750         pos_x = xmin;
751         pos_y += 24 + 4;
752
753         // Titlebar background:
754         tmp_x = sbwidth;
755         tmp_y = sbar_fontsize_y;
756         drawfill(pos - '1 1 0', tmp + '2 2 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
757         
758         drawfont = sbar_font;
759
760         for(i = 0; i < sbar_num_fields; ++i)
761         {
762                 if(sbar_field[i] == SP_SEPARATOR)
763                         break;
764                 drawstring(pos, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
765                 pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
766         }
767         
768         if(sbar_field[i] == SP_SEPARATOR)
769         {
770                 pos_x = xmax + sbar_fontsize_x;
771                 tmp_y = tmp_z = 0;
772                 for(i = sbar_num_fields-1; i > 0; --i)
773                 {
774                         if(sbar_field[i] == SP_SEPARATOR)
775                                 break;
776
777                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
778                         /**
779                          * FTEQCC BUG!
780                          * Using the following line will fuck it all up:
781                          **
782                          * tmp_x = sbar_size[i] - strlen(sbar_title[i])*8;
783                          */
784                         tmp_x = sbar_fontsize_x*sbar_size[i];
785                         tmp_x -= stringwidth(sbar_title[i], FALSE)*sbar_fontsize_x;
786                         drawstring(pos + tmp, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
787                 }
788         }
789                 
790         pos_x = xmin;
791         pos_y += 1.5 * sbar_fontsize_y;
792
793         sbar_save = sbar;
794         sbar = '0 0 0';
795         
796         if(teamplay)
797         {
798                 //for(tm = sortedTeams.sort_next; tm; tm = tm.sort_next)
799                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
800                 {
801                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
802                                 continue;
803
804                         rgb = GetTeamRGB(tm.team);
805
806                         pos_x = xmin;
807
808                         Sbar_DrawXNum(
809                                 pos - '6.5 0 0' * sbar_fontsize_y,
810                                 tm.(teamscores[ts_primary]),
811                                 4, sbar_fontsize_y * 1.5, rgb, 1, DRAWFLAG_NORMAL);
812
813                         if(ts_primary != ts_secondary)
814                         Sbar_DrawXNum(
815                                 pos - '4.5 0 0' * sbar_fontsize_y + '0 1.5 0' * sbar_fontsize_y,
816                                 tm.(teamscores[ts_secondary]),
817                                 4, sbar_fontsize_y * 1, rgb, 1, DRAWFLAG_NORMAL);
818                         
819                         specs = tm.team_size;
820
821                         if(specs < 2)
822                                 specs = 2;
823                         
824                         tmp_x = sbwidth;
825                         tmp_y = 1.25 * sbar_fontsize_y * specs;
826                         drawfill(pos - '1 1 0', tmp + '2 0 0', rgb, 0.2, DRAWFLAG_NORMAL);
827                         
828                         for(pl = players.sort_next; pl; pl = pl.sort_next)
829                         {
830                                 if(pl.team != tm.team)
831                                         continue;
832                                 Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
833                                 pos_y += 1.25 * sbar_fontsize_y;
834                                 tmp_y -= 1.25 * sbar_fontsize_y;
835                         }
836                         pos_y += tmp_y + 1.5 * sbar_fontsize_y;
837                 }
838                 // rgb := tempvector :)
839                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
840                 pos_y += 3 * sbar_fontsize_y;
841                 specs = 0;
842                 for(pl = players.sort_next; pl; pl = pl.sort_next)
843                 {
844                         if(pl.team != COLOR_SPECTATOR)
845                                 continue;
846                         //drawcolorcodedstring(pos, getplayerkey(pl.sb_player, "name"), '8 8 0', 1, 0);
847                         Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
848                         pos += '0 1.25 0' * sbar_fontsize_y;
849                         ++specs;
850                 }
851                         
852                 if(specs)
853                         drawstring(rgb, "Spectators", sbar_fontsize, '1 1 1', 1, 0);
854         } else {
855                 pos_x = xmin;
856                 for(pl = players.sort_next; pl; pl = pl.sort_next)
857                 {
858                         if(pl.team == COLOR_SPECTATOR)
859                                 continue;
860                         Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
861                         pos_y += 1.25 * sbar_fontsize_y;
862                         tmp_y -= 1.25 * sbar_fontsize_y;
863                 }
864
865                 // rgb := tempvector :)
866                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
867                 pos_y += 3 * sbar_fontsize_y;
868                 specs = 0;
869                 for(pl = players.sort_next; pl; pl = pl.sort_next)
870                 {
871                         if(pl.team != COLOR_SPECTATOR)
872                                 continue;
873                         Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
874                         pos += '0 1.25 0' * sbar_fontsize_y;
875                         ++specs;
876                 }
877                         
878                 if(specs)
879                         drawstring(rgb, "Spectators", sbar_fontsize, '1 1 1', 1, 0);
880         }
881
882         string str;
883         float tl, fl;
884         str = strcat("playing on ^2", shortmapname, "^7");
885         tl = getstatf(STAT_TIMELIMIT);
886         fl = getstatf(STAT_FRAGLIMIT);
887         if(gametype == GAME_LMS)
888         {
889                 if(tl > 0)
890                         str = strcat(str, " for up to ^1", ftos(tl), " minutes^7");
891         }
892         else
893         {
894                 if(tl > 0)
895                         str = strcat(str, " for ^1", ftos(tl), " minutes^7");
896                 if(fl > 0)
897                 {
898                         if(tl > 0)
899                                 str = strcat(str, " or");
900                         str = strcat(str, " until ^3", ftos(fl));
901                         if(scores_label[ps_primary] == "score")
902                                 str = strcat(str, " points^7");
903                         else
904                                 str = strcat(str, " ", scores_label[ps_primary]);
905                 }
906         }
907         
908         pos_y += 1.5 * sbar_fontsize_y;
909         drawcolorcodedstring(pos + '0.5 0 0' * (sbwidth - sbar_fontsize_x * stringwidth(str, TRUE)), str, sbar_fontsize, 0.8, 0);
910
911         sbar = sbar_save;
912 }
913
914 void Sbar_Score(float margin)
915 {
916         float timelimit, timeleft, minutes, seconds, distribution, myplace, score;
917         vector sbar_save, place;
918         entity tm, pl, me;
919         sbar_save = sbar;
920         
921         myteam = GetPlayerColor(player_localentnum - 1);
922
923         sbar_y = vid_conheight - (32+12);
924         sbar_x -= margin;
925         
926         place = '-48 -12 0';
927         if(teamplay)
928         {
929                 // Layout:
930                 //
931                 //   team1 team3 team4
932                 //
933                 //         TEAM2
934                 //for(i = 0; i < 4; ++i)
935                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
936                 {
937                         if(tm.team == COLOR_SPECTATOR || !tm.team_size) // no players? don't display
938                                 continue;
939                         // -32*4 = -128
940                         score = tm.(teamscores[ts_primary]);
941                         if(tm.team == myteam)
942                                 Sbar_DrawXNum('-128 0 0', score, 4, 32, GetTeamRGB(tm.team), 1, DRAWFLAG_NORMAL);
943                         else
944                         {
945                                 Sbar_DrawXNum(place, score, 4, 12, GetTeamRGB(tm.team), 1, DRAWFLAG_NORMAL);
946                                 place_x -= 4*12;
947                         }
948                 }
949         } else {
950                 // me vector := [team/connected frags id]
951                 myplace = 0;
952                 for(me = players.sort_next; me; me = me.sort_next)
953                 {
954                         if(me.team != COLOR_SPECTATOR)
955                                 ++myplace;
956                         if(me.sv_entnum == player_localentnum - 1)
957                                 break;
958                 }
959                 pl = players;
960                 if(pl == me)
961                         pl = pl.sort_next;
962                 
963                 if(pl) {
964                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
965                 } else
966                         distribution = 0;
967                 
968                 if(myplace == 1)
969                         Sbar_DrawXNum('-36 -12 0', myplace, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
970                 else if(myplace == 2)
971                         Sbar_DrawXNum('-36 -12 0', myplace, 3, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
972                 else
973                         Sbar_DrawXNum('-36 -12 0', myplace, 3, 12, '1 0 0', 1, DRAWFLAG_NORMAL);
974
975                 score = me.(scores[ps_primary]);
976                 if(distribution >= 0)
977                 {
978                         Sbar_DrawXNum('-84 -12 0', distribution, 4, 12, ' 1 1 1', 1, DRAWFLAG_NORMAL);
979                         Sbar_DrawXNum('-128 0 0', score, 4, 32, '1 1 1', 1, DRAWFLAG_NORMAL);
980                 } else if(distribution >= -5)
981                 {
982                         Sbar_DrawXNum('-84 -12 0', distribution, 4, 12, ' 1 1 0', 1, DRAWFLAG_NORMAL);
983                         Sbar_DrawXNum('-128 0 0', score, 4, 32, '1 1 0', 1, DRAWFLAG_NORMAL);
984                 } else {
985                         Sbar_DrawXNum('-84 -12 0', distribution, 4, 12, ' 1 0 0', 1, DRAWFLAG_NORMAL);
986                         Sbar_DrawXNum('-128 0 0', score, 4, 32, '1 0 0', 1, DRAWFLAG_NORMAL);
987                 }
988         }
989         timelimit = getstatf(STAT_TIMELIMIT);
990         if(timelimit)
991         {
992                 timeleft = max(0, timelimit * 60 - time);
993                 minutes = floor(timeleft / 60);
994                 seconds = floor(timeleft - minutes*60);
995                 if(minutes >= 5)
996                 {
997                         Sbar_DrawXNum('-72 32 0', minutes, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
998                         drawpic(sbar + '-36 32 0', "gfx/num_colon", '12 12 0', '1 1 1', sbar_alpha_fg, 0);
999                         Sbar_DrawXNum('-24 32 0', seconds, -2, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
1000                 } else if(minutes >= 1)
1001                 {
1002                         Sbar_DrawXNum('-72 32 0', minutes, 3, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
1003                         drawpic(sbar + '-36 32 0', "gfx/num_colon", '12 12 0', '1 1 0', sbar_alpha_fg, 0);
1004                         Sbar_DrawXNum('-24 32 0', seconds, -2, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
1005                 } else {
1006                         Sbar_DrawXNum('-24 32 0', seconds, -2, 12, '1 0 0', 1, DRAWFLAG_NORMAL);
1007                 }
1008         } else {
1009                 minutes = floor(time / 60);
1010                 seconds = floor(time - minutes*60);
1011                 Sbar_DrawXNum('-72 32 0', minutes, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
1012                 drawpic(sbar + '-36 32 0', "gfx/num_colon", '12 12 0', '1 1 1', sbar_alpha_fg, 0);
1013                 Sbar_DrawXNum('-24 32 0', seconds, -2, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
1014         }
1015         sbar = sbar_save;
1016 }
1017
1018 void Sbar_MiniscoreItem(vector pos, entity pl, float is_self)
1019 {
1020         float x, score;
1021         pos_x += 72;
1022         
1023         if(teamplay)
1024                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(pl.team)*0.5, 1, DRAWFLAG_NORMAL);
1025         else
1026                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
1027         x = pos_x;
1028         pos_x += 5*8;
1029         score = pl.(scores[ps_primary]);
1030         pos_x -= stringwidth(ftos(score), FALSE)*8;
1031         drawstring(pos, ftos(score), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1032         pos_x = x;
1033         if(is_self)
1034         {
1035                 pos_x += 48;
1036                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1037                 pos_x += 8;
1038         } else
1039                 pos_x += 56;
1040         drawcolorcodedstring(pos, getplayerkey(pl.sv_entnum, "name"), '8 8 0', 1, 0);
1041 }
1042
1043 void Sbar_MiniscoreTeamItem(vector pos, float color, float frags, float is_self)
1044 {
1045         float x;
1046         pos_x += 72;
1047         
1048         if(teamplay)
1049                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(color)*0.5, 1, DRAWFLAG_NORMAL);
1050         else
1051                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
1052         x = pos_x;
1053         pos_x += 5*8;
1054         pos_x -= stringwidth(ftos(frags), FALSE)*8;
1055         drawstring(pos, ftos(frags), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1056         pos_x = x;
1057         if(is_self)
1058         {
1059                 pos_x += 48;
1060                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1061                 pos_x += 8;
1062         } else
1063                 pos_x += 56;
1064         drawstring(pos, GetTeamName(color), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1065 }
1066
1067 void Sbar_MiniDeathmatchOverlay(vector pos)
1068 {
1069         float numlines, up, down, score;
1070         entity me, tm, pl;
1071         float miniscoreboard_size;
1072         miniscoreboard_size = cvar("sbar_miniscoreboard_size");
1073         
1074         if(miniscoreboard_size == 0)
1075                 return;
1076         pos_y = vid_conheight - 8;
1077         
1078         if(miniscoreboard_size < 0)
1079                 numlines = (vid_conheight - sbar_y + 7) / 8;
1080         else
1081                 numlines = miniscoreboard_size;
1082
1083         // give up if there isn't enough room
1084         if(pos_x >= vid_conwidth || pos_y >= vid_conheight || numlines < 1)
1085                 return;
1086
1087         // me vector := [team/connected frags id]
1088         for(me = players.sort_next; me; me = me.sort_next)
1089         {
1090                 if(me.sv_entnum == player_localentnum - 1)
1091                         break;
1092         }
1093
1094         if(teamplay)
1095                 numlines -= numteams;
1096
1097         // figure out how many players above and below we can show
1098         up = floor(numlines/2);
1099         down = up;
1100         if((up + down) > numlines)
1101                 down = numlines - up;
1102
1103         // render bottom-up
1104         for(pl = me.sort_next; pl && down > 0; pl = pl.sort_next)
1105         {
1106                 if(pl.team == COLOR_SPECTATOR)
1107                         continue;
1108                 Sbar_MiniscoreItem(pos, pl, false);
1109                 pos_y -= 9;
1110                 --down;
1111         }
1112         Sbar_MiniscoreItem(pos, me, true);
1113         pos_y -= 9;
1114         up += down; // if there weren't enough lines below... add them
1115         for(pl = me.sort_prev; pl && up > 0; pl = pl.sort_prev)
1116         {
1117                 if(pl.team == COLOR_SPECTATOR)
1118                         continue;
1119                 Sbar_MiniscoreItem(pos, pl, false);
1120                 pos_y -= 9;
1121                 --up;
1122         }
1123
1124         if(teamplay)
1125         {
1126                 for(tm = teams.sort_next; tm.sort_next; tm = tm.sort_next);
1127                 for(; tm; tm = tm.sort_prev)
1128                 {
1129                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
1130                                 continue;
1131                         score = tm.(teamscores[ts_primary]);
1132                         Sbar_MiniscoreTeamItem(pos, tm.team, score, (tm.team == me.team));
1133                         pos_y -= 9;
1134                 }
1135         }
1136 }
1137
1138 float Sbar_WouldDrawScoreboard ()
1139 {
1140         if (sb_showscores)
1141                 return 1;
1142         else if (intermission == 1)
1143                 return 1;
1144         else if (intermission == 2)
1145                 return 1;
1146         return 0;
1147 }
1148
1149 void Sbar_Draw (void)
1150 {
1151         float i;
1152         float x, fade;
1153         float stat_items;
1154
1155         //Sbar_SortFrags();
1156         Sbar_UpdatePlayerTeams();
1157
1158         sb_lines = 24;
1159         
1160         if (sb_showscores)
1161                 Sbar_DrawScoreboard();
1162         else if (intermission == 1)
1163         {
1164                 Sbar_DrawScoreboard();
1165                 return;
1166         }
1167         else if (intermission == 2)
1168                 Sbar_FinaleOverlay();
1169         else
1170         {
1171                 if (sb_showscores || (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard")))
1172                 {
1173                         sbar_x = (vid_conwidth - 640.0)*0.5;
1174                         sbar_y = vid_conheight - 47;
1175                         //Sbar_DrawAlphaPic (sbar_x, sbar_y, sb_scorebar, sbar_alpha_bg.value);
1176                         //drawpic('0 0 0', "gfx/scorebar", '0 0 0', '1 1 1', cvar("sbar_alpha_bg"), 0);
1177                         Sbar_DrawScoreboard ();
1178                 }
1179                 else
1180                 {
1181                         if (sb_lines && sbar_hudselector == 1)
1182                         {
1183                                 stat_items = getstati(STAT_ITEMS);
1184
1185                                 sbar_x = (vid_conwidth - 320.0)*0.5;
1186                                 sbar_y = vid_conheight - 24.0 - 16.0;
1187                                 sbar_z = 0;
1188                         
1189                                 fade = 3.2 - 2 * (time - weapontime);
1190                                 fade = bound(0.7, fade, 1);
1191
1192                                 x = 1.0;
1193                                 for(i = 0; i < 8; ++i)
1194                                 {
1195                                         if(stat_items & x)
1196                                         {
1197                                                 Sbar_DrawWeapon(i+1, fade, (i + 2 == activeweapon));
1198                                         }
1199                                         x *= 2;
1200                                 }
1201                                 x *= 2*2*2*2;
1202                                 if(stat_items & x)
1203                                 {
1204                                         Sbar_DrawWeapon(0, fade, (activeweapon == 1));
1205                                 }
1206
1207                                 // armor
1208                                 x = getstati(STAT_ARMOR);
1209                                 if (x > 0)
1210                                 {
1211                                         // "gfx/sb_armor"
1212                                         //Sbar_DrawStretchPic (72, 0, sb_armor[0], sbar_alpha_fg.value, 24, 24);
1213                                         drawpic(sbar + '72 0 0', "gfx/sb_armor", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1214                                         if(x > 200)
1215                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '0 1 0', 1, 0);
1216                                         else if(x > 100)
1217                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '0.2 1 0', 1, 0);
1218                                         else if(x > 50)
1219                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1220                                         else if(x > 25)
1221                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '1 1 0.2', 1, 0);
1222                                         else
1223                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '0.7 0 0', 1, 0);
1224                                 }
1225
1226                                 // health
1227                                 x = getstati(STAT_HEALTH);
1228                                 if (x != 0)
1229                                 {
1230                                         // "gfx/sb_health"
1231                                         //Sbar_DrawStretchPic (184, 0, sb_health, sbar_alpha_fg.value, 24, 24);
1232                                         drawpic(sbar + '184 0 0', "gfx/sb_health", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1233                                         if(x > 200)
1234                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '0 1 0', 1, 0);
1235                                         else if(x > 100)
1236                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '0.2 1 0', 1, 0);
1237                                         else if(x > 50)
1238                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1239                                         else if(x > 25)
1240                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '1 1 0.2', 1, 0);
1241                                         else
1242                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '0.7 0 0', 1, 0);
1243                                 }
1244
1245                                 // ammo
1246                                 x = getstati(STAT_AMMO);
1247                                 if ((stat_items & (NEX_IT_SHELLS | NEX_IT_BULLETS | NEX_IT_ROCKETS | NEX_IT_CELLS)) || x != 0)
1248                                 {
1249                                         if (stat_items & NEX_IT_SHELLS)
1250                                                 drawpic(sbar + '296 0 0', "gfx/sb_shells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1251                                         else if (stat_items & NEX_IT_BULLETS)
1252                                                 drawpic(sbar + '296 0 0', "gfx/sb_bullets", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1253                                         else if (stat_items & NEX_IT_ROCKETS)
1254                                                 drawpic(sbar + '296 0 0', "gfx/sb_rocket", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1255                                         else if (stat_items & NEX_IT_CELLS)
1256                                                 drawpic(sbar + '296 0 0', "gfx/sb_cells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1257                                         if(x > 10)
1258                                                 Sbar_DrawXNum('224 0 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1259                                         else
1260                                                 Sbar_DrawXNum('224 0 0', x, 3, 24, '0.7 0 0', 1, 0);
1261                                 }
1262
1263                                 if (sbar_x + 320 + 160 <= vid_conwidth)
1264                                         Sbar_MiniDeathmatchOverlay(sbar + '320 0 0');
1265                                 if (sbar_x > 0)
1266                                         Sbar_Score(16);
1267                                 // The margin can be at most 8 to support 640x480 console size:
1268                                 //   320 + 2 * (144 + 16) = 640
1269                         }
1270                         else if (sb_lines)
1271                         {
1272                         
1273                                 stat_items = getstati(STAT_ITEMS);
1274                         
1275                                 sbar_x = (vid_conwidth - 640.0)*0.5;
1276                                 sbar_y = vid_conheight - 47;
1277                                 sbar_z = 0;
1278
1279                                 fade = 3 - 2 * (time - weapontime);
1280
1281                                 x = 1.0;
1282                                 for(i = 0; i < 8; ++i)
1283                                 {
1284                                         if(stat_items & x)
1285                                         {
1286                                                 Sbar_DrawWeapon(i+1, fade, (i + 2 == activeweapon));
1287                                         }
1288                                         x *= 2;
1289                                 }
1290                                 x *= 2*2*2*2;
1291                                 if(stat_items & x)
1292                                 {
1293                                         Sbar_DrawWeapon(0, fade, (activeweapon == 1));
1294                                 }
1295
1296                                 if (sb_lines > 24)
1297                                         drawpic(sbar, "gfx/sbar", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1298                                 else
1299                                         drawpic(sbar, "gfx/sbar_minimal", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1300
1301                                 // armor
1302                                 // (340-3*24) = 268
1303                                 Sbar_DrawXNum('268 12 0', getstati(STAT_ARMOR), 3, 24, '0.6 0.7 0.8', 1, 0);
1304
1305                                 // health
1306                                 // (154-3*24) = 82
1307                                 x = getstati(STAT_HEALTH);
1308                                 if(x > 100)
1309                                         Sbar_DrawXNum('82 12 0', x, 3, 24, '1 1 1', 1, 0);
1310                                 else if(x <= 25 && time - floor(time) > 0.5)
1311                                         Sbar_DrawXNum('82 12 0', x, 3, 24, '0.7 0 0', 1, 0);
1312                                 else
1313                                         Sbar_DrawXNum('81 12 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1314
1315                                 // AK dont draw ammo for the laser
1316                                 x = getstati(STAT_AMMO);
1317                                 if(activeweapon != 12)
1318                                 {
1319                                         // (519-3*24) = 447
1320                                         if (stat_items & NEX_IT_SHELLS)
1321                                                 drawpic(sbar + '519 0 0', "gfx/sb_shells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1322                                         else if (stat_items & NEX_IT_BULLETS)
1323                                                 drawpic(sbar + '519 0 0', "gfx/sb_bullets", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1324                                         else if (stat_items & NEX_IT_ROCKETS)
1325                                                 drawpic(sbar + '519 0 0', "gfx/sb_rocket", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1326                                         else if (stat_items & NEX_IT_CELLS)
1327                                                 drawpic(sbar + '519 0 0', "gfx/sb_cells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1328                                         if(x > 10)
1329                                                 Sbar_DrawXNum('447 12 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1330                                         else
1331                                                 Sbar_DrawXNum('447 12 0', x, 3, 24, '0.7 0 0', 1, 0);
1332                                 }
1333
1334                                 if (sb_lines > 24)
1335                                         drawpic(sbar, "gfx/sbar_overlay", '0 0 0', '1 1 1', 1, DRAWFLAG_MODULATE);
1336
1337                                 if (sbar_x + 600 + 160 <= vid_conwidth)
1338                                         Sbar_MiniDeathmatchOverlay (sbar + '600 0 0');
1339
1340                                 if (sbar_x > 0)
1341                                         Sbar_Score(-16);
1342                                 // Because:
1343                                 //   Mini scoreboard uses 12*4 per other team, that is, 144
1344                                 //   pixels when there are four teams...
1345                                 //   Nexuiz by default sets vid_conwidth to 800... makes
1346                                 //   sbar_x == 80...
1347                                 //   so we need to shift it by 64 pixels to the right to fit
1348                                 //   BUT: then it overlaps with the image that gets drawn
1349                                 //   for viewsize 100! Therefore, just account for 3 teams,
1350                                 //   that is, 96 pixels mini scoreboard size, needing 16 pixels
1351                                 //   to the right!
1352                         }
1353                 
1354                 
1355                         if(gametype == GAME_KEYHUNT)
1356                         {
1357                                 CSQC_kh_hud();
1358                         } else if(gametype == GAME_CTF)
1359                         {
1360                                 CSQC_ctf_hud();
1361                         }
1362                 }
1363         }
1364 }
1365
1366 void CSQC_ctf_hud(void)
1367 {
1368         // cvar("sbar_flagstatus_right") move the flag icons right
1369         // cvar("sbar_flagstatus_pos") pixel position of the nexuiz flagstatus icons
1370         float redflag, blueflag;
1371         float stat_items;
1372         vector pos;
1373         
1374         stat_items = getstati(STAT_ITEMS);
1375         redflag = (stat_items/32768) & 3;
1376         blueflag = (stat_items/131072) & 3;
1377
1378         /**
1379          * FTEQCC BUG!
1380          * For some reason now not even THAT works there...
1381          * Maybe the minus' precedence screws it up? The last one there, maybe I should use brackets
1382          **
1383          * pos_x = (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - sbar_x - 64 : 10 - sbar_x;
1384          ** Should try those later:
1385          * pos_x = (cvar("sbar_flagstatus_right")) ? (vid_conwidth - 10 - sbar_x - 64) : (10 - sbar_x);
1386          * pos_x = ( (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - 64 : 10 ) - sbar_x;
1387          */
1388         
1389         if(cvar("sbar_flagstatus_right"))
1390                 pos_x = vid_conwidth - 10 - sbar_x - 64;
1391         else
1392                 pos_x = 10 - sbar_x;
1393         
1394         pos_z = 0;
1395
1396         if(sbar_hudselector == 1)
1397                 pos_y = (vid_conheight - sbar_y) - cvar("sbar_flagstatus_pos") - 64;
1398         else
1399                 pos_y = -117;
1400
1401         pos += sbar;
1402
1403         switch(redflag)
1404         {
1405         case 1: drawpic(pos, "gfx/sb_flag_red_taken", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1406         case 2: drawpic(pos, "gfx/sb_flag_red_lost", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1407         case 3: drawpic(pos, "gfx/sb_flag_red_carrying", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1408         }
1409
1410         pos_y -= 64;
1411         
1412         switch(blueflag)
1413         {
1414         case 1: drawpic(pos, "gfx/sb_flag_blue_taken", '0 0 0', '1 1 1', 1, 0); break;
1415         case 2: drawpic(pos, "gfx/sb_flag_blue_lost", '0 0 0', '1 1 1', 1, 0); break;
1416         case 3: drawpic(pos, "gfx/sb_flag_blue_carrying", '0 0 0', '1 1 1', 1, 0); break;
1417         }
1418 }