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