]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/sbar.qc
fixed teamsorting, I guess
[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 vector sbar_fontsize;
10 float sbar_alpha_fg;
11 float sbar_hudselector;
12
13 entity sortedPlayers;
14 entity sortedTeams;
15
16 .float sb_frags;
17 .float sb_team;
18 .float sb_player;
19 .float sb_caps;
20
21 entity team1, team2, team3, team4, teamspec;
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', '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 float Sbar_PlayerCmp(entity l, entity r)
132 {
133         if(teamplay)
134         {
135                 if(l.sb_team > r.sb_team)
136                         return true;
137                 else if(l.sb_team > r.sb_team)
138                         return false;
139                 if(gametype == GAME_CTF)
140                 {
141                         if(l.sb_caps > r.sb_caps)
142                                 return true;
143                         else if(l.sb_caps < r.sb_caps)
144                                 return false;
145                 }
146         }
147         if(l.sb_frags > r.sb_frags)
148                 return true;
149         else if(l.sb_frags < r.sb_frags)
150                 return false;
151         return (l.sb_player > r.sb_player);
152 }
153 float Sbar_TeamCmp(entity l, entity r)
154 {
155         if(gametype == GAME_CTF)
156         {
157                 if(l.sb_caps > r.sb_caps)
158                         return true;
159                 else if(l.sb_caps < r.sb_caps)
160                         return false;
161         }
162         if(l.sb_frags > r.sb_frags)
163                 return true;
164         else if(l.sb_frags < r.sb_frags)
165                 return false;
166         return (l.sb_player > r.sb_player);
167 }
168
169 void Sbar_Init()
170 {
171         sortedPlayers = Sort_New(Sbar_PlayerCmp);
172         sortedTeams = Sort_New(Sbar_TeamCmp);
173         team1 = Sort_Next(sortedTeams);
174         team1.sb_team = COLOR_TEAM1;
175         team2 = Sort_Next(sortedTeams);
176         team2.sb_team = COLOR_TEAM2;
177         team3 = Sort_Next(sortedTeams);
178         team3.sb_team = COLOR_TEAM3;
179         team4 = Sort_Next(sortedTeams);
180         team4.sb_team = COLOR_TEAM4;
181         teamspec = Sort_Next(sortedTeams);
182         teamspec.sb_team = COLOR_SPECTATOR;
183 }
184
185 void Sbar_UpdatePosFrags(entity player)
186 {
187         other = player.sort_prev;
188         while(other != sortedPlayers && player.sb_frags > other.sb_frags)
189         {
190                 SORT_SWAP(other, player);
191                 other = player.sort_prev;
192         }
193
194         other = player.sort_next;
195         while(other && player.sb_frags < other.sb_frags)
196         {
197                 SORT_SWAP(player, other);
198                 other = player.sort_next;
199         }
200 }
201 void Sbar_UpdatePosFragsCTF(entity player)
202 {
203         other = player.sort_prev;
204         while(other != sortedPlayers && player.sb_frags > other.sb_frags && player.sb_caps == other.sb_caps)
205         {
206                 SORT_SWAP(other, player);
207                 other = player.sort_prev;
208         }
209
210         other = player.sort_next;
211         while(other && player.sb_frags < other.sb_frags && player.sb_caps == other.sb_caps)
212         {
213                 SORT_SWAP(player, other);
214                 other = player.sort_next;
215         }
216 }
217 void Sbar_UpdatePosCaps(entity player)
218 {
219         other = player.sort_prev;
220         while(other != sortedPlayers && player.sb_caps > other.sb_caps)
221         {
222                 SORT_SWAP(other, player);
223                 other = player.sort_prev;
224         }
225         other = player.sort_next;
226         while(other && player.sb_caps < other.sb_caps)
227         {
228                 SORT_SWAP(player, other);
229                 other = player.sort_next;
230         }
231         // let it update the frags now too, so if we have more frags then the next guy with the same caps
232         // we beat his ass :)
233         player.sb_frags -= 1;
234 }
235
236 void Sbar_UpdatePosTeam(entity player)
237 {
238         player.sb_caps -= 1; // team change needs a full update
239         other = player.sort_prev;
240         while(other != sortedPlayers && player.sb_team > other.sb_team)
241         {
242                 SORT_SWAP(other, player);
243                 other = player.sort_prev;
244         }
245
246         other = player.sort_next;
247         while(other && player.sb_team < other.sb_team)
248         {
249                 SORT_SWAP(player, other);
250                 other = player.sort_next;
251         }
252 }
253
254 void Sbar_UpdatePlayer(entity player)
255 {
256         float i;
257
258         if(player.sb_frags == -666)
259                 i = COLOR_SPECTATOR;
260         else
261                 i = GetPlayerColor(player.sb_player);
262         
263         if(player.sb_team != i)
264         {
265                 player.sb_team = i;
266                 Sbar_UpdatePosTeam(player);
267         }
268         
269         if(gametype == GAME_CTF)
270         {
271                 i = stof(bufstr_get(databuf, DATABUF_CAPTURES + player.sb_player));
272                 if(player.sb_caps != i)
273                 {
274                         player.sb_caps = i;
275                         Sbar_UpdatePosCaps(player);
276                 }
277                 i = stof(getplayerkey(player.sb_player, "frags"));
278                 if(player.sb_frags != i)
279                 {
280                         player.sb_frags = i;
281                         Sbar_UpdatePosFragsCTF(player);
282                 }
283         } else {
284                 i = stof(getplayerkey(player.sb_player, "frags"));
285                 if(player.sb_frags != i)
286                 {
287                         player.sb_frags = i;
288                         Sbar_UpdatePosFrags(player);
289                 }
290         }
291 }
292
293 void Sbar_UpdateTeamPosCaps(entity tm)
294 {
295         other = tm.sort_prev;
296         while(other != sortedTeams && tm.sb_caps > other.sb_caps)
297         {
298                 SORT_SWAP(other, tm);
299                 other = tm.sort_prev;
300         }
301
302         other = tm.sort_next;
303         while(other && tm.sb_caps < tm.sb_caps)
304         {
305                 SORT_SWAP(tm, other);
306                 other = tm.sort_next;
307         }
308 }
309 void Sbar_UpdateTeamPosFrags(entity tm)
310 {
311         other = tm.sort_prev;
312         while(other != sortedTeams && tm.sb_caps == other.sb_caps && tm.sb_frags > other.sb_frags)
313         {
314                 SORT_SWAP(other, tm);
315                 other = tm.sort_prev;
316         }
317
318         other = tm.sort_next;
319         while(other && tm.sb_caps == other.sb_caps && tm.sb_frags < other.sb_frags)
320         {
321                 SORT_SWAP(tm, other);
322                 other = tm.sort_next;
323         }
324 }
325
326 void Sbar_SortFrags()
327 {
328         float i;
329         entity tmp;
330         float t1f, t2f, t3f, t4f;
331         
332         Sort_Reset(sortedPlayers);
333
334         numteams = 0;
335         if(teamplay)
336         {
337                 Sort_Reset(sortedTeams);
338                 tmp = Sort_Next(sortedTeams);
339
340                 team1.sb_player = 0;
341                 team2.sb_player = 0;
342                 team3.sb_player = 0;
343                 team4.sb_player = 0;
344                 teamspec.sb_player = 0;
345                 
346                 t1f = team1.sb_frags;
347                 t2f = team2.sb_frags;
348                 t3f = team3.sb_frags;
349                 t4f = team4.sb_frags;
350
351                 team1.sb_frags = 0;
352                 team2.sb_frags = 0;
353                 team3.sb_frags = 0;
354                 team4.sb_frags = 0;
355                 
356                 for(i = 0; i < maxclients; ++i)
357                 {
358                         if(strlen(getplayerkey(i, "name")) <= 0)
359                                 continue;
360
361                         Sort_Reset(sortedPlayers);
362
363                         tmp = NULL;
364                         while(Sort_HasNext(sortedPlayers))
365                         {
366                                 tmp = Sort_Next(sortedPlayers);
367                                 if(tmp.sb_player == i)
368                                         break;
369                         }
370                         if(!tmp || tmp.sb_player != i)
371                                 tmp = Sort_Next(sortedPlayers);
372                         
373                         tmp.sb_player = i;
374                         tmp.frame = time;
375                         Sbar_UpdatePlayer(tmp);
376                         
377                         switch(tmp.sb_team)
378                         {
379                         case COLOR_TEAM1: team1.sb_frags += tmp.sb_frags; team1.sb_player++; break;
380                         case COLOR_TEAM2: team2.sb_frags += tmp.sb_frags; team2.sb_player++; break;
381                         case COLOR_TEAM3: team3.sb_frags += tmp.sb_frags; team3.sb_player++; break;
382                         case COLOR_TEAM4: team4.sb_frags += tmp.sb_frags; team4.sb_player++; break;
383                         case COLOR_SPECTATOR: teamspec.sb_frags += tmp.sb_frags; teamspec.sb_player++; break;
384                         }
385
386                         if(i == player_localentnum-1)
387                                 myteam = tmp.sb_team;
388                 }
389                 if(team1.sb_player) ++numteams;
390                 if(team2.sb_player) ++numteams;
391                 if(team3.sb_player) ++numteams;
392                 if(team4.sb_player) ++numteams;
393
394                 if(team1.sb_caps != caps_team1)
395                 {
396                         team1.sb_caps = caps_team1;
397                         Sbar_UpdateTeamPosCaps(team1);
398                 }
399                 if(team2.sb_caps != caps_team2)
400                 {
401                         team2.sb_caps = caps_team2;
402                         Sbar_UpdateTeamPosCaps(team2);
403                 }
404                 if(team1.sb_frags != t1f) Sbar_UpdateTeamPosFrags(team1);
405                 if(team2.sb_frags != t2f) Sbar_UpdateTeamPosFrags(team2);
406                 if(team3.sb_frags != t3f) Sbar_UpdateTeamPosFrags(team3);
407                 if(team4.sb_frags != t4f) Sbar_UpdateTeamPosFrags(team4);
408         } else {
409                 for(i = 0; i < maxclients; ++i)
410                 {
411                         if(strlen(getplayerkey(i, "name")) <= 0)
412                                 continue;
413                 
414                         Sort_Reset(sortedPlayers);
415                         tmp = NULL;
416                         while(Sort_HasNext(sortedPlayers))
417                         {
418                                 tmp = Sort_Next(sortedPlayers);
419                                 if(tmp.sb_player == i)
420                                         break;
421                         }
422                         if(!tmp || tmp.sb_player != i)
423                                 tmp = Sort_Next(sortedPlayers);
424                         
425                         tmp.sb_player = i;
426                         tmp.frame = time;
427                         Sbar_UpdatePlayer(tmp);
428                 }
429         }
430         Sort_RemoveOld(sortedPlayers);
431 }
432
433 void Cmd_Sbar_Help(float argc)
434 {
435         print("You can modify the scoreboard using the\n");
436         print("^3|---------------------------------------------------------------|\n");
437         print("^2sbar_columns^7 cvar and the ^2sbar_columns_set command.\n");
438         print("^2sbar_columns^7             specifies the default layout and\n");
439         print("^2sbar_columns_set^7         actually changes the layout.\n");
440         print("You can call ^2sbar_columns_set^7 with the new layout\n");
441         print("as parameters, or eithout parameters it will read the cvar.\n\n");
442         print("Usage:\n");
443         print("^2sbar_columns_set ^7filed1 field2 ...\n");
444         print("Fields which are not relevant to the current gametype\n");
445         print("won't be displayed\n\n");
446         print("The following field names are recognized (case INsensitive):\n");
447         print("^3name^7 or ^3nick^7             Name of a player\n");
448         print("^3caps^7 or ^3captures^7         Number of flags captured\n");
449         print("^3rets^7 or ^3returns^7          Number of flags returned\n");
450         print("^3frags^7 or ^3kills^7           Frags\n");
451         print("^3deaths^7 or ^3dths^7           Number of deaths\n");
452         print("^3kd^7 or ^3kdr^7 or ^3kdratio^7 or ^3k/d\n");
453         print("                         The kill-death ratio\n");
454         print("^3ping^7                     Ping time\n\n");
455         print("You can use a ^3|^7 to start the right-aligned fields.\n");
456         print("Example: ping name | caps rets frags k/d\n");
457         print("This will put the ping and the name on the left side.\n");
458         print("The captures, returns, frags and kill-death ratio will be\n");
459         print("rendered beginning on the right side.\n");
460
461 }
462
463 #define MIN_NAMELEN 24
464 #define MAX_NAMELEN 24
465
466 void Cmd_Sbar_SetFields(float argc)
467 {
468         float i;
469         string str;
470         float digit;
471
472         if(argc < 2)
473                 argc = tokenize(strcat("x ", cvar_string("sbar_columns")));
474         
475         argc = min(MAX_SBAR_FIELDS, argc);
476         sbar_num_fields = 0;
477         drawfont = sbar_font;
478         digit = stringwidth("0123456789", FALSE) / 10;
479         for(i = 0; i < argc-1; ++i)
480         {
481                 str = argv(i+1);
482                 strunzone(sbar_title[i]);
483                 sbar_title[i] = strzone(str);
484                 sbar_size[i] = stringwidth(str, FALSE);
485                 str = strtolower(str);
486                 if(str == "ping") {
487                         sbar_field[i] = SBF_PING;
488                 } else if(str == "name" || str == "nick") {
489                         sbar_field[i] = SBF_NAME;
490                         sbar_size[i] = MIN_NAMELEN; // minimum size? any use?
491                 } else if(str == "caps" || str == "captures") {
492                         if(sbar_size[i] < 3*digit)
493                                 sbar_size[i] = 3*digit;
494                         sbar_field[i] = SBF_CAPS;
495                 } else if(str == "rets" || str == "returns") {
496                         if(sbar_size[i] < 3*digit)
497                                 sbar_size[i] = 3*digit;
498                         sbar_field[i] = SBF_RETS;
499                 } else if(str == "frags" || str == "kills") {
500                         if(sbar_size[i] < 5*digit)
501                                 sbar_size[i] = 5*digit;
502                         sbar_field[i] = SBF_FRAGS;
503                 } else if(str == "deaths" || str == "dths") {
504                         if(sbar_size[i] < 5*digit)
505                                 sbar_size[i] = 5*digit;
506                         sbar_field[i] = SBF_DEATHS;
507                 } else if(str == "kdratio") {
508                         sbar_field[i] = SBF_KDRATIO;
509                 } else if(str == "kdr" || str == "k/d") {
510                         sbar_field[i] = SBF_KDRATIO;
511                 } else if(str == "kd") {
512                         sbar_field[i] = SBF_KDRATIO;
513                 } else if(str == "|") {
514                         sbar_field[i] = SBF_SEPARATOR;
515                 } else {
516                         print(strcat("^1Error:^7 Unknown score field: '", str, "'\n"));
517                         --sbar_num_fields;
518                 }
519                 ++sbar_num_fields;
520         }
521         sbar_field[i] = SBF_END;
522 }
523
524 vector sbar_field_rgb;
525 string Sbar_GetField(entity pl, float field)
526 {
527         float tmp;
528         string str;
529         sbar_field_rgb = '1 1 1';
530         switch(field)
531         {
532         case SBF_PING:
533                 str = bufstr_get(databuf, DATABUF_PING + pl.sb_player);
534                 tmp = max(0, min(220, stof(str)-80)) / 220;
535                 sbar_field_rgb = '1 1 1' - '0 1 1'*tmp;
536                 return str;
537         case SBF_NAME: return getplayerkey(pl.sb_player, "name");
538         case SBF_CAPS: return ftos(pl.sb_caps);
539         case SBF_RETS: return bufstr_get(databuf, DATABUF_RETURNS + pl.sb_player);
540         case SBF_FRAGS: return ftos(pl.sb_frags);
541         case SBF_DEATHS: return bufstr_get(databuf, DATABUF_DEATHS + pl.sb_player);
542         case SBF_KDRATIO:
543                 tmp = stof(bufstr_get(databuf, DATABUF_DEATHS + pl.sb_player));
544                 if(tmp == 0) {
545                         sbar_field_rgb = '0 1 0';
546                         str = ftos(pl.sb_frags);
547                 } else if(pl.sb_frags <= 0) {
548                         sbar_field_rgb = '1 0 0';
549                         str = ftos(pl.sb_frags / tmp);
550                 } else
551                         str = ftos(pl.sb_frags / tmp);
552                 
553                 tmp = strstrofs(str, ".", 0);
554                 if(tmp > 0)
555                         str = substring(str, 0, tmp+2);
556                 return str;
557         }
558 }
559 #define SBAR_DEFAULT_MASK 0
560 #define SBAR_MASK_SPECTATORS 1
561 float Sbar_IsFieldMasked(float field, float mask)
562 {
563         if(mask&1) // spectator
564                 return (field != SBF_NAME && field != SBF_PING);
565         if(gametype != GAME_CTF)
566         {
567                 if(field == SBF_CAPS || field == SBF_RETS)
568                         return true;
569         }
570         return false;
571 }
572
573 // shamelessly stolen from menu QC :P <- as if I would steal YOUR code pfft ;)
574 float textLengthUpToWidth(string theText, float maxWidth, float handleColors)
575 {
576         // STOP.
577         // The following function is SLOW.
578         // For your safety and for the protection of those around you...
579         // DO NOT CALL THIS AT HOME.
580         // No really, don't.
581         if(stringwidth(theText, handleColors) <= maxWidth)
582                 return strlen(theText); // yeah!
583
584         // binary search for right place to cut string
585         float left, right, middle; // this always works
586         left = 0;
587         right = strlen(theText); // this always fails
588         do
589         {
590                 middle = floor((left + right) / 2);
591                 if(stringwidth(substring(theText, 0, middle), handleColors) <= maxWidth)
592                         left = middle;
593                 else
594                         right = middle;
595         }
596         while(left < right - 1);
597
598         // NOTE: when color codes are involved, this binary search is,
599         // mathematically, BROKEN. However, it is obviously guaranteed to
600         // terminate, as the range still halves each time - but nevertheless, it is
601         // guaranteed that it finds ONE valid cutoff place (where "left" is in
602         // range, and "right" is outside).
603
604         return left;
605 }
606 string textShortenToWidth(string theText, float maxWidth, float handleColors)
607 {
608         if(stringwidth(theText, handleColors) <= maxWidth)
609                 return theText;
610         else
611                 return strcat(substring(theText, 0, textLengthUpToWidth(theText, maxWidth - stringwidth("...", handleColors), handleColors)), "...");
612 }
613
614 float xmin, xmax, ymin, ymax, sbwidth, sbheight;
615 void Sbar_PrintScoreboardItem(vector pos, entity pl, float is_self, float mask)
616 {
617         vector tmp;
618         string str;
619         float i, field, len;
620
621         // Layout:
622         tmp_z = 0;
623         if(is_self)
624         {
625                 tmp_x = sbwidth;
626                 tmp_y = sbar_fontsize_y;
627                 drawfill(pos - '1 1', tmp + '2 2', '1 1 1', 0.3, DRAWFLAG_NORMAL);
628         }       
629         tmp_y = 0;
630         
631         for(i = 0; i < sbar_num_fields; ++i)
632         {
633                 field = sbar_field[i];
634                 if(field == SBF_SEPARATOR)
635                         break;
636                 if(Sbar_IsFieldMasked(field, mask))
637                         continue;
638
639                 str = Sbar_GetField(pl, field);
640
641                 if(field == SBF_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] == SBF_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] != SBF_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                 if(field == SBF_NAME) {
663                         tmp_x = sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
664                         drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
665                 } else {
666                         tmp_x = len*sbar_fontsize_x + sbar_fontsize_x;
667                         drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
668                 }
669         }
670         
671         if(sbar_field[i] == SBF_SEPARATOR)
672         {
673                 pos_x = xmax;
674                 for(i = sbar_num_fields-1; i > 0; --i)
675                 {
676                         field = sbar_field[i];
677                         if(field == SBF_SEPARATOR)
678                                 break;
679                         if(Sbar_IsFieldMasked(field, mask))
680                                 continue;
681                         
682                         str = Sbar_GetField(pl, field);
683
684                         if(field == SBF_NAME)
685                                 str = textShortenToWidth(str, sbar_size[i], TRUE);
686                         len = stringwidth(str, TRUE);
687
688                         if(sbar_size[i] < len)
689                                 sbar_size[i] = len;
690                         
691                         if(field == SBF_NAME) {
692                                 tmp_x = sbar_fontsize_x*len; // left or right aligned? let's put it right...
693                                 drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
694                         } else {
695                                 tmp_x = sbar_fontsize_x*len; //strlen(str);
696                                 drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
697                         }
698                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
699                 }
700         }
701 }
702
703 void Sbar_DrawScoreboard()
704 {
705         //float xmin, ymin, xmax, ymax;
706         vector rgb, pos, tmp, sbar_save;
707         entity pl, tm;
708         float specs, i;
709         float center_x;
710
711         sbar_fontsize = Sbar_GetFontsize();
712         if(sbar_fontsize_x == 0)
713                 sbar_fontsize = '8 8 0';
714         if(sbar_fontsize_y == 0)
715                 sbar_fontsize_y = sbar_fontsize_x;
716         
717         xmin = vid_conwidth / 5;
718         ymin = 20;
719
720         xmax = vid_conwidth - xmin;
721         ymax = vid_conheight - 0.2*vid_conheight;
722
723         sbwidth = xmax - xmin;
724         sbheight = ymax - ymin;
725
726         center_x = xmin + 0.5*sbwidth;
727
728         //Sbar_UpdateFields();
729
730         // Initializes position
731         //pos_x = xmin;
732         pos_y = ymin;
733         pos_z = 0;
734
735         // Heading
736         drawfont = sbar_font;
737         pos_x = center_x - stringwidth("Scoreboard", TRUE)*0.5*24;
738         drawstring(pos, "Scoreboard", '24 24', '1 1 1', 1, DRAWFLAG_NORMAL);
739         pos_x = xmin;
740         pos_y += 24 + 4;
741
742         // Titlebar background:
743         tmp_x = sbwidth;
744         tmp_y = sbar_fontsize_y;
745         drawfill(pos - '1 1', tmp + '2 2', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
746         
747         for(i = 0; i < sbar_num_fields; ++i)
748         {
749                 if(Sbar_IsFieldMasked(sbar_field[i], SBAR_DEFAULT_MASK))
750                         continue;
751                 if(sbar_field[i] == SBF_SEPARATOR)
752                         break;
753                 drawstring(pos, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
754                 pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
755         }
756         
757         if(sbar_field[i] == SBF_SEPARATOR)
758         {
759                 pos_x = xmax + sbar_fontsize_x;
760                 tmp_y = tmp_z = 0;
761                 for(i = sbar_num_fields-1; i > 0; --i)
762                 {
763                         if(Sbar_IsFieldMasked(sbar_field[i], SBAR_DEFAULT_MASK))
764                                 continue;
765                         if(sbar_field[i] == SBF_SEPARATOR)
766                                 break;
767                         
768                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
769                         /**
770                          * FTEQCC BUG!
771                          * Using the following line will fuck it all up:
772                          **
773                          * tmp_x = sbar_size[i] - strlen(sbar_title[i])*8;
774                          */
775                         tmp_x = sbar_fontsize_x*sbar_size[i];
776                         tmp_x -= stringwidth(sbar_title[i], FALSE)*sbar_fontsize_x;
777                         drawstring(pos + tmp, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
778                 }
779         }
780                 
781         pos_x = xmin;
782         pos_y += 1.5 * sbar_fontsize_y;
783
784         sbar_save = sbar;
785         sbar = '0 0 0';
786         
787         if(teamplay)
788         {
789                 for(tm = sortedTeams.sort_next; tm; tm = tm.sort_next)
790                 {
791                         if(!tm.sb_player || tm.sb_team == COLOR_SPECTATOR) // no players in it?
792                                 continue;
793
794                         rgb = GetTeamRGB(tm.sb_team);
795
796                         pos_x = xmin - 4*24;
797                         if(gametype == GAME_CTF)
798                         {
799                                 if(tm.sb_team == COLOR_TEAM1)
800                                         Sbar_DrawXNum(pos, caps_team1, 4, 24, rgb, 1, DRAWFLAG_NORMAL);
801                                 else if(tm.sb_team == COLOR_TEAM2)
802                                         Sbar_DrawXNum(pos, caps_team2, 4, 24, rgb, 1, DRAWFLAG_NORMAL);
803                                 pos_x = xmin - 4*10;
804                                 Sbar_DrawXNum(pos + '0 24', tm.sb_frags, 4, 10, rgb, 1, DRAWFLAG_NORMAL);
805                                 pos_x = xmin;
806                         } else
807                                 Sbar_DrawXNum(pos, tm.sb_frags, 4, 24, rgb, 1, DRAWFLAG_NORMAL);
808                         pos_x = xmin;
809
810                         // abuse specs as playerounter
811                         specs = 0;
812                         for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
813                         {
814                                 if(pl.sb_team == tm.sb_team)
815                                         ++specs;
816                         }
817
818                         if(specs < 2)
819                                 specs = 2;
820                         if(gametype == GAME_CTF && specs < 4)
821                                 specs = 4;
822                         
823                         tmp_x = sbwidth;
824                         tmp_y = 1.25 * sbar_fontsize_y * specs;
825                         drawfill(pos - '1 1', tmp + '2 0', rgb, 0.2, DRAWFLAG_NORMAL);
826                         
827                         for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
828                         {
829                                 if(pl.sb_team != tm.sb_team)
830                                         continue;
831                                 Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), SBAR_DEFAULT_MASK);
832                                 pos_y += 1.25 * sbar_fontsize_y;
833                                 tmp_y -= 1.25 * sbar_fontsize_y;
834                         }
835                         pos_y += tmp_y + 1.5 * sbar_fontsize_y;
836                 }
837                 // rgb := tempvector :)
838                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
839                 pos_y += 3 * sbar_fontsize_y;
840                 specs = 0;
841                 for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
842                 {
843                         if(pl.sb_team != COLOR_SPECTATOR)
844                                 continue;
845                         //drawcolorcodedstring(pos, getplayerkey(pl.sb_player, "name"), '8 8 0', 1, 0);
846                         Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), SBAR_MASK_SPECTATORS);
847                         pos += '0 1.25 0' * sbar_fontsize_y;
848                         ++specs;
849                 }
850                         
851                 if(specs)
852                         drawstring(rgb, "Spectators", sbar_fontsize, '1 1 1', 1, 0);
853         } else {
854                 pos_x = xmin;
855                 for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
856                 {
857                         if(pl.sb_team == COLOR_SPECTATOR)
858                                 continue;
859                         Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), SBAR_DEFAULT_MASK);
860                         pos_y += 1.25 * sbar_fontsize_y;
861                         tmp_y -= 1.25 * sbar_fontsize_y;
862                 }
863
864                 // rgb := tempvector :)
865                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
866                 pos_y += 3 * sbar_fontsize_y;
867                 specs = 0;
868                 for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
869                 {
870                         if(pl.sb_team != COLOR_SPECTATOR)
871                                 continue;
872                         //drawcolorcodedstring(pos, getplayerkey(pl.sb_player, "name"), '8 8 0', 1, 0);
873                         Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), SBAR_MASK_SPECTATORS);
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         sbar = sbar_save;
882 }
883
884 void Sbar_Score(float margin)
885 {
886         float timelimit, timeleft, minutes, seconds, distribution, myplace;
887         vector sbar_save, place;
888         entity tm, pl, me;
889         sbar_save = sbar;
890
891         sbar_y = vid_conheight - (32+12);
892         sbar_x -= margin;
893         
894         place = '-48 -12 0';
895         if(teamplay)
896         {
897                 // Layout:
898                 //
899                 //   team1 team3 team4
900                 //
901                 //         TEAM2
902                 //for(i = 0; i < 4; ++i)
903                 for(tm = sortedTeams.sort_next; tm; tm = tm.sort_next)
904                 {
905                         if(tm.sb_team == COLOR_SPECTATOR || !tm.sb_player) // no players? don't display
906                                 continue;
907                         // -32*4 = -128
908                         if(tm.sb_team == myteam)
909                                 Sbar_DrawXNum('-128 0', tm.sb_frags, 4, 32, GetTeamRGB(tm.sb_team), 1, DRAWFLAG_NORMAL);
910                         else
911                         {
912                                 Sbar_DrawXNum(place, tm.sb_frags, 4, 12, GetTeamRGB(tm.sb_team), 1, DRAWFLAG_NORMAL);
913                                 place_x -= 4*12;
914                         }
915                 }
916         } else {
917                 // me vector := [team/connected frags id]
918                 myplace = 0;
919                 for(me = sortedPlayers.sort_next; me; me = me.sort_next)
920                 {
921                         if(me.sb_team != COLOR_SPECTATOR)
922                                 ++myplace;
923                         if(me.sb_player == player_localentnum - 1)
924                                 break;
925                 }
926                 pl = sortedPlayers.sort_next;
927                 if(pl == me)
928                         pl = pl.sort_next;
929                 
930                 if(pl && myplace != 1)
931                 {
932                         distribution = me.sb_frags - pl.sb_frags;
933                 } else if(pl) {
934                         distribution = me.sb_frags - pl.sb_frags;
935                 } else
936                         distribution = 0;
937                 
938                 if(myplace == 1)
939                         Sbar_DrawXNum('-36 -12', myplace, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
940                 else if(myplace == 2)
941                         Sbar_DrawXNum('-36 -12', myplace, 3, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
942                 else
943                         Sbar_DrawXNum('-36 -12', myplace, 3, 12, '1 0 0', 1, DRAWFLAG_NORMAL);
944
945                 if(distribution >= 0)
946                 {
947                         Sbar_DrawXNum('-84 -12', distribution, 4, 12, ' 1 1 1', 1, DRAWFLAG_NORMAL);
948                         Sbar_DrawXNum('-128 0', me.sb_frags, 4, 32, '1 1 1', 1, DRAWFLAG_NORMAL);
949                 } else if(distribution >= -5)
950                 {
951                         Sbar_DrawXNum('-84 -12', distribution, 4, 12, ' 1 1 0', 1, DRAWFLAG_NORMAL);
952                         Sbar_DrawXNum('-128 0', me.sb_frags, 4, 32, '1 1 0', 1, DRAWFLAG_NORMAL);
953                 } else {
954                         Sbar_DrawXNum('-84 -12', distribution, 4, 12, ' 1 0 0', 1, DRAWFLAG_NORMAL);
955                         Sbar_DrawXNum('-128 0', me.sb_frags, 4, 32, '1 0 0', 1, DRAWFLAG_NORMAL);
956                 }
957         }
958         timelimit = getstatf(STAT_TIMELIMIT);
959         if(timelimit)
960         {
961                 timeleft = max(0, timelimit * 60 - time);
962                 minutes = floor(timeleft / 60);
963                 seconds = floor(timeleft - minutes*60);
964                 if(minutes >= 5)
965                 {
966                         Sbar_DrawXNum('-72 32', minutes, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
967                         drawpic(sbar + '-36 32', "gfx/num_colon", '12 12', '1 1 1', sbar_alpha_fg, 0);
968                         Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
969                 } else if(minutes >= 1)
970                 {
971                         Sbar_DrawXNum('-72 32', minutes, 3, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
972                         drawpic(sbar + '-36 32', "gfx/num_colon", '12 12', '1 1 0', sbar_alpha_fg, 0);
973                         Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
974                 } else {
975                         Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 0 0', 1, DRAWFLAG_NORMAL);
976                 }
977         } else {
978                 minutes = floor(time / 60);
979                 seconds = floor(time - minutes*60);
980                 Sbar_DrawXNum('-72 32', minutes, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
981                 drawpic(sbar + '-36 32', "gfx/num_colon", '12 12', '1 1 1', sbar_alpha_fg, 0);
982                 Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
983         }
984         sbar = sbar_save;
985 }
986
987 void Sbar_MiniscoreItem(vector pos, entity pl, float is_self)
988 {
989         float x;
990         pos_x += 72;
991         
992         if(teamplay)
993                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(pl.sb_team)*0.5, 1, DRAWFLAG_NORMAL);
994         else
995                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
996         x = pos_x;
997         pos_x += 5*8;
998         pos_x -= stringwidth(ftos(pl.sb_frags), FALSE)*8;
999         drawstring(pos, ftos(pl.sb_frags), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1000         pos_x = x;
1001         if(is_self)
1002         {
1003                 pos_x += 48;
1004                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1005                 pos_x += 8;
1006         } else
1007                 pos_x += 56;
1008         drawcolorcodedstring(pos, getplayerkey(pl.sb_player, "name"), '8 8 0', 1, 0);
1009 }
1010
1011 void Sbar_MiniscoreTeamItem(vector pos, float color, float frags, float is_self)
1012 {
1013         float x;
1014         pos_x += 72;
1015         
1016         if(teamplay)
1017                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(color)*0.5, 1, DRAWFLAG_NORMAL);
1018         else
1019                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
1020         x = pos_x;
1021         pos_x += 5*8;
1022         pos_x -= stringwidth(ftos(frags), FALSE)*8;
1023         drawstring(pos, ftos(frags), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1024         pos_x = x;
1025         if(is_self)
1026         {
1027                 pos_x += 48;
1028                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1029                 pos_x += 8;
1030         } else
1031                 pos_x += 56;
1032         drawstring(pos, GetTeamName(color), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1033 }
1034
1035 void Sbar_MiniDeathmatchOverlay(vector pos)
1036 {
1037         float numlines, up, down;
1038         entity me, tm, pl;
1039         float miniscoreboard_size;
1040         miniscoreboard_size = cvar("sbar_miniscoreboard_size");
1041         
1042         if(miniscoreboard_size == 0)
1043                 return;
1044         pos_y = vid_conheight - 8;
1045         
1046         if(miniscoreboard_size < 0)
1047                 numlines = (vid_conheight - sbar_y + 7) / 8;
1048         else
1049                 numlines = miniscoreboard_size;
1050
1051         // give up if there isn't enough room
1052         if(pos_x >= vid_conwidth || pos_y >= vid_conheight || numlines < 1)
1053                 return;
1054
1055         // me vector := [team/connected frags id]
1056         for(me = sortedPlayers.sort_next; me; me = me.sort_next)
1057         {
1058                 if(me.sb_player == player_localentnum - 1)
1059                         break;
1060         }
1061
1062         if(teamplay)
1063                 numlines -= numteams;
1064
1065         // figure out how many players above and below we can show
1066         up = floor(numlines/2);
1067         down = up;
1068         if((up + down) > numlines)
1069                 down = numlines - up;
1070
1071         // render bottom-up
1072         for(pl = me.sort_next; pl && down > 0; pl = pl.sort_next)
1073         {
1074                 if(pl.sb_team == COLOR_SPECTATOR)
1075                         continue;
1076                 Sbar_MiniscoreItem(pos, pl, false);
1077                 pos_y -= 9;
1078                 --down;
1079         }
1080         Sbar_MiniscoreItem(pos, me, true);
1081         pos_y -= 9;
1082         up += down; // if there weren't enough lines below... add them
1083         for(pl = me.sort_prev; pl != sortedPlayers && up > 0; pl = pl.sort_prev)
1084         {
1085                 if(pl.sb_team == COLOR_SPECTATOR)
1086                         continue;
1087                 Sbar_MiniscoreItem(pos, pl, false);
1088                 pos_y -= 9;
1089                 --up;
1090         }
1091
1092         if(teamplay)
1093         {
1094                 for(tm = sortedTeams.sort_next; tm.sort_next; tm = tm.sort_next);
1095                 for(; tm != sortedTeams; tm = tm.sort_prev)
1096                 {
1097                         if(!tm.sb_player || tm.sb_team == COLOR_SPECTATOR) // no players?
1098                                 continue;
1099                         Sbar_MiniscoreTeamItem(pos, tm.sb_team, tm.sb_frags, (tm.sb_team == me.sb_team));
1100                         pos_y -= 9;
1101                 }
1102         }
1103 }
1104
1105 void Sbar_Draw (void)
1106 {
1107         float i;
1108         float x, fade;
1109         float stat_items;
1110
1111         Sbar_SortFrags();
1112
1113         sb_lines = 24;
1114         
1115         if (sb_showscores)
1116                 Sbar_DrawScoreboard();
1117         else if (intermission == 1)
1118         {
1119                 Sbar_DrawScoreboard();
1120                 return;
1121         }
1122         else if (intermission == 2)
1123                 Sbar_FinaleOverlay();
1124         else
1125         {
1126                 if (sb_showscores || (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard")))
1127                 {
1128                         sbar_x = (vid_conwidth - 640.0)*0.5;
1129                         sbar_y = vid_conheight - 47;
1130                         //Sbar_DrawAlphaPic (sbar_x, sbar_y, sb_scorebar, sbar_alpha_bg.value);
1131                         //drawpic('0 0', "gfx/scorebar", '0 0 0', '1 1 1', cvar("sbar_alpha_bg"), 0);
1132                         Sbar_DrawScoreboard ();
1133                 }
1134                 else
1135                 {
1136                         if (sb_lines && sbar_hudselector == 1)
1137                         {
1138                                 stat_items = getstati(STAT_ITEMS);
1139
1140                                 sbar_x = (vid_conwidth - 320.0)*0.5;
1141                                 sbar_y = vid_conheight - 24.0 - 16.0;
1142                                 sbar_z = 0;
1143                         
1144                                 fade = 3.2 - 2 * (time - weapontime);
1145                                 fade = bound(0.7, fade, 1);
1146
1147                                 x = 1.0;
1148                                 for(i = 0; i < 8; ++i)
1149                                 {
1150                                         if(stat_items & x)
1151                                         {
1152                                                 Sbar_DrawWeapon(i+1, fade, (i + 2 == activeweapon));
1153                                         }
1154                                         x *= 2;
1155                                 }
1156                                 x *= 2*2*2*2;
1157                                 if(stat_items & x)
1158                                 {
1159                                         Sbar_DrawWeapon(0, fade, (activeweapon == 1));
1160                                 }
1161
1162                                 // armor
1163                                 x = getstati(STAT_ARMOR);
1164                                 if (x > 0)
1165                                 {
1166                                         // "gfx/sb_armor"
1167                                         //Sbar_DrawStretchPic (72, 0, sb_armor[0], sbar_alpha_fg.value, 24, 24);
1168                                         drawpic(sbar + '72 0', "gfx/sb_armor", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1169                                         if(x > 200)
1170                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0 1 0', 1, 0);
1171                                         else if(x > 100)
1172                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0.2 1 0', 1, 0);
1173                                         else if(x > 50)
1174                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1175                                         else if(x > 25)
1176                                                 Sbar_DrawXNum('0 0', x, 3, 24, '1 1 0.2', 1, 0);
1177                                         else
1178                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0.7 0 0', 1, 0);
1179                                 }
1180
1181                                 // health
1182                                 x = getstati(STAT_HEALTH);
1183                                 if (x != 0)
1184                                 {
1185                                         // "gfx/sb_health"
1186                                         //Sbar_DrawStretchPic (184, 0, sb_health, sbar_alpha_fg.value, 24, 24);
1187                                         drawpic(sbar + '184 0', "gfx/sb_health", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1188                                         if(x > 200)
1189                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0 1 0', 1, 0);
1190                                         else if(x > 100)
1191                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0.2 1 0', 1, 0);
1192                                         else if(x > 50)
1193                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1194                                         else if(x > 25)
1195                                                 Sbar_DrawXNum('112 0', x, 3, 24, '1 1 0.2', 1, 0);
1196                                         else
1197                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0.7 0 0', 1, 0);
1198                                 }
1199
1200                                 // ammo
1201                                 x = getstati(STAT_AMMO);
1202                                 if ((stat_items & (NEX_IT_SHELLS | NEX_IT_BULLETS | NEX_IT_ROCKETS | NEX_IT_CELLS)) || x != 0)
1203                                 {
1204                                         if (stat_items & NEX_IT_SHELLS)
1205                                                 drawpic(sbar + '296 0', "gfx/sb_shells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1206                                         else if (stat_items & NEX_IT_BULLETS)
1207                                                 drawpic(sbar + '296 0', "gfx/sb_bullets", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1208                                         else if (stat_items & NEX_IT_ROCKETS)
1209                                                 drawpic(sbar + '296 0', "gfx/sb_rocket", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1210                                         else if (stat_items & NEX_IT_CELLS)
1211                                                 drawpic(sbar + '296 0', "gfx/sb_cells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1212                                         if(x > 10)
1213                                                 Sbar_DrawXNum('224 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1214                                         else
1215                                                 Sbar_DrawXNum('224 0', x, 3, 24, '0.7 0 0', 1, 0);
1216                                 }
1217
1218                                 if (sbar_x + 320 + 160 <= vid_conwidth)
1219                                         Sbar_MiniDeathmatchOverlay(sbar + '320 0');
1220                                 if (sbar_x > 0)
1221                                         Sbar_Score(16);
1222                                 // The margin can be at most 8 to support 640x480 console size:
1223                                 //   320 + 2 * (144 + 16) = 640
1224                         }
1225                         else if (sb_lines)
1226                         {
1227                         
1228                                 stat_items = getstati(STAT_ITEMS);
1229                         
1230                                 sbar_x = (vid_conwidth - 640.0)*0.5;
1231                                 sbar_y = vid_conheight - 47;
1232                                 sbar_z = 0;
1233
1234                                 fade = 3 - 2 * (time - weapontime);
1235
1236                                 x = 1.0;
1237                                 for(i = 0; i < 8; ++i)
1238                                 {
1239                                         if(stat_items & x)
1240                                         {
1241                                                 Sbar_DrawWeapon(i+1, fade, (i + 2 == activeweapon));
1242                                         }
1243                                         x *= 2;
1244                                 }
1245                                 x *= 2*2*2*2;
1246                                 if(stat_items & x)
1247                                 {
1248                                         Sbar_DrawWeapon(0, fade, (activeweapon == 1));
1249                                 }
1250
1251                                 if (sb_lines > 24)
1252                                         drawpic(sbar, "gfx/sbar", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1253                                 else
1254                                         drawpic(sbar, "gfx/sbar_minimal", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1255
1256                                 // armor
1257                                 // (340-3*24) = 268
1258                                 Sbar_DrawXNum('268 12', getstati(STAT_ARMOR), 3, 24, '0.6 0.7 0.8', 1, 0);
1259
1260                                 // health
1261                                 // (154-3*24) = 82
1262                                 x = getstati(STAT_HEALTH);
1263                                 if(x > 100)
1264                                         Sbar_DrawXNum('82 12', x, 3, 24, '1 1 1', 1, 0);
1265                                 else if(x <= 25 && time - floor(time) > 0.5)
1266                                         Sbar_DrawXNum('82 12', x, 3, 24, '0.7 0 0', 1, 0);
1267                                 else
1268                                         Sbar_DrawXNum('81 12', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1269
1270                                 // AK dont draw ammo for the laser
1271                                 x = getstati(STAT_AMMO);
1272                                 if(activeweapon != 12)
1273                                 {
1274                                         // (519-3*24) = 447
1275                                         if (stat_items & NEX_IT_SHELLS)
1276                                                 drawpic(sbar + '519 0', "gfx/sb_shells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1277                                         else if (stat_items & NEX_IT_BULLETS)
1278                                                 drawpic(sbar + '519 0', "gfx/sb_bullets", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1279                                         else if (stat_items & NEX_IT_ROCKETS)
1280                                                 drawpic(sbar + '519 0', "gfx/sb_rocket", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1281                                         else if (stat_items & NEX_IT_CELLS)
1282                                                 drawpic(sbar + '519 0', "gfx/sb_cells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1283                                         if(x > 10)
1284                                                 Sbar_DrawXNum('447 12', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1285                                         else
1286                                                 Sbar_DrawXNum('447 12', x, 3, 24, '0.7 0 0', 1, 0);
1287                                 }
1288
1289                                 if (sb_lines > 24)
1290                                         drawpic(sbar, "gfx/sbar_overlay", '0 0 0', '1 1 1', 1, DRAWFLAG_MODULATE);
1291
1292                                 if (sbar_x + 600 + 160 <= vid_conwidth)
1293                                         Sbar_MiniDeathmatchOverlay (sbar + '600 0');
1294
1295                                 if (sbar_x > 0)
1296                                         Sbar_Score(-16);
1297                                 // Because:
1298                                 //   Mini scoreboard uses 12*4 per other team, that is, 144
1299                                 //   pixels when there are four teams...
1300                                 //   Nexuiz by default sets vid_conwidth to 800... makes
1301                                 //   sbar_x == 80...
1302                                 //   so we need to shift it by 64 pixels to the right to fit
1303                                 //   BUT: then it overlaps with the image that gets drawn
1304                                 //   for viewsize 100! Therefore, just account for 3 teams,
1305                                 //   that is, 96 pixels mini scoreboard size, needing 16 pixels
1306                                 //   to the right!
1307                         }
1308                 
1309                 
1310                         if(gametype == GAME_KEYHUNT)
1311                         {
1312                                 CSQC_kh_hud();
1313                         } else if(gametype == GAME_CTF)
1314                         {
1315                                 CSQC_ctf_hud();
1316                         }
1317                 }
1318         }
1319 }
1320
1321 void CSQC_ctf_hud(void)
1322 {
1323         // cvar("sbar_flagstatus_right") move the flag icons right
1324         // cvar("sbar_flagstatus_pos") pixel position of the nexuiz flagstatus icons
1325         float redflag, blueflag;
1326         float stat_items;
1327         vector pos;
1328         
1329         stat_items = getstati(STAT_ITEMS);
1330         redflag = (stat_items/32768) & 3;
1331         blueflag = (stat_items/131072) & 3;
1332
1333         /**
1334          * FTEQCC BUG!
1335          * For some reason now not even THAT works there...
1336          * Maybe the minus' precedence screws it up? The last one there, maybe I should use brackets
1337          **
1338          * pos_x = (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - sbar_x - 64 : 10 - sbar_x;
1339          ** Should try those later:
1340          * pos_x = (cvar("sbar_flagstatus_right")) ? (vid_conwidth - 10 - sbar_x - 64) : (10 - sbar_x);
1341          * pos_x = ( (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - 64 : 10 ) - sbar_x;
1342          */
1343         
1344         if(cvar("sbar_flagstatus_right"))
1345                 pos_x = vid_conwidth - 10 - sbar_x - 64;
1346         else
1347                 pos_x = 10 - sbar_x;
1348         
1349         pos_z = 0;
1350
1351         if(sbar_hudselector == 1)
1352                 pos_y = (vid_conheight - sbar_y) - cvar("sbar_flagstatus_pos") - 64;
1353         else
1354                 pos_y = -117;
1355
1356         pos += sbar;
1357
1358         switch(redflag)
1359         {
1360         case 1: drawpic(pos, "gfx/sb_flag_red_taken", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1361         case 2: drawpic(pos, "gfx/sb_flag_red_lost", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1362         case 3: drawpic(pos, "gfx/sb_flag_red_carrying", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1363         }
1364
1365         pos_y -= 64;
1366         
1367         switch(blueflag)
1368         {
1369         case 1: drawpic(pos, "gfx/sb_flag_blue_taken", '0 0 0', '1 1 1', 1, 0); break;
1370         case 2: drawpic(pos, "gfx/sb_flag_blue_lost", '0 0 0', '1 1 1', 1, 0); break;
1371         case 3: drawpic(pos, "gfx/sb_flag_blue_carrying", '0 0 0', '1 1 1', 1, 0); break;
1372         }
1373 }