]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/sbar.qc
CSQC stuff, map voting; menu; new CTF scoring option; impulse 911; trigger_impulse...
[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 == tm.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                                         print("remaining size: ", ftos(realsize), "\n");
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                                         print("remaining size: ", ftos(realsize), "\n");
655                                 }
656                         str = textShortenToWidth(str, realsize, TRUE);
657                 }
658                 len = stringwidth(str, TRUE);
659                 
660                 if(sbar_size[i] < len)
661                         sbar_size[i] = len;
662
663                 pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
664                 if(field == SBF_NAME) {
665                         tmp_x = sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
666                         drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
667                 } else {
668                         tmp_x = len*sbar_fontsize_x + sbar_fontsize_x;
669                         drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
670                 }
671         }
672         
673         if(sbar_field[i] == SBF_SEPARATOR)
674         {
675                 pos_x = xmax;
676                 for(i = sbar_num_fields-1; i > 0; --i)
677                 {
678                         field = sbar_field[i];
679                         if(field == SBF_SEPARATOR)
680                                 break;
681                         if(Sbar_IsFieldMasked(field, mask))
682                                 continue;
683                         
684                         str = Sbar_GetField(pl, field);
685
686                         if(field == SBF_NAME)
687                                 str = textShortenToWidth(str, sbar_size[i], TRUE);
688                         len = stringwidth(str, TRUE);
689
690                         if(sbar_size[i] < len)
691                                 sbar_size[i] = len;
692                         
693                         if(field == SBF_NAME) {
694                                 tmp_x = sbar_fontsize_x*len; // left or right aligned? let's put it right...
695                                 drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
696                         } else {
697                                 tmp_x = sbar_fontsize_x*len; //strlen(str);
698                                 drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
699                         }
700                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
701                 }
702         }
703 }
704
705 void Sbar_DrawScoreboard()
706 {
707         //float xmin, ymin, xmax, ymax;
708         vector rgb, pos, tmp, sbar_save;
709         entity pl, tm;
710         float specs, i;
711         float center_x;
712
713         sbar_fontsize = Sbar_GetFontsize();
714         if(sbar_fontsize_x == 0)
715                 sbar_fontsize = '8 8 0';
716         if(sbar_fontsize_y == 0)
717                 sbar_fontsize_y = sbar_fontsize_x;
718         
719         xmin = vid_conwidth / 5;
720         ymin = 20;
721
722         xmax = vid_conwidth - xmin;
723         ymax = vid_conheight - 0.2*vid_conheight;
724
725         sbwidth = xmax - xmin;
726         sbheight = ymax - ymin;
727
728         center_x = xmin + 0.5*sbwidth;
729
730         //Sbar_UpdateFields();
731
732         // Initializes position
733         //pos_x = xmin;
734         pos_y = ymin;
735         pos_z = 0;
736
737         // Heading
738         drawfont = sbar_font;
739         pos_x = center_x - stringwidth("Scoreboard", TRUE)*0.5*24;
740         drawstring(pos, "Scoreboard", '24 24', '1 1 1', 1, DRAWFLAG_NORMAL);
741         pos_x = xmin;
742         pos_y += 24 + 4;
743
744         // Titlebar background:
745         tmp_x = sbwidth;
746         tmp_y = sbar_fontsize_y;
747         drawfill(pos - '1 1', tmp + '2 2', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
748         
749         for(i = 0; i < sbar_num_fields; ++i)
750         {
751                 if(Sbar_IsFieldMasked(sbar_field[i], SBAR_DEFAULT_MASK))
752                         continue;
753                 if(sbar_field[i] == SBF_SEPARATOR)
754                         break;
755                 drawstring(pos, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
756                 pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
757         }
758         
759         if(sbar_field[i] == SBF_SEPARATOR)
760         {
761                 pos_x = xmax + sbar_fontsize_x;
762                 tmp_y = tmp_z = 0;
763                 for(i = sbar_num_fields-1; i > 0; --i)
764                 {
765                         if(Sbar_IsFieldMasked(sbar_field[i], SBAR_DEFAULT_MASK))
766                                 continue;
767                         if(sbar_field[i] == SBF_SEPARATOR)
768                                 break;
769                         
770                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
771                         /**
772                          * FTEQCC BUG!
773                          * Using the following line will fuck it all up:
774                          **
775                          * tmp_x = sbar_size[i] - strlen(sbar_title[i])*8;
776                          */
777                         tmp_x = sbar_fontsize_x*sbar_size[i];
778                         tmp_x -= stringwidth(sbar_title[i], FALSE)*sbar_fontsize_x;
779                         drawstring(pos + tmp, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
780                 }
781         }
782                 
783         pos_x = xmin;
784         pos_y += 1.5 * sbar_fontsize_y;
785
786         sbar_save = sbar;
787         sbar = '0 0 0';
788         
789         if(teamplay)
790         {
791                 for(tm = sortedTeams.sort_next; tm; tm = tm.sort_next)
792                 {
793                         if(!tm.sb_player || tm.sb_team == COLOR_SPECTATOR) // no players in it?
794                                 continue;
795
796                         rgb = GetTeamRGB(tm.sb_team);
797
798                         pos_x = xmin - 4*24;
799                         if(gametype == GAME_CTF)
800                         {
801                                 if(tm.sb_team == COLOR_TEAM1)
802                                         Sbar_DrawXNum(pos, caps_team1, 4, 24, rgb, 1, DRAWFLAG_NORMAL);
803                                 else if(tm.sb_team == COLOR_TEAM2)
804                                         Sbar_DrawXNum(pos, caps_team2, 4, 24, rgb, 1, DRAWFLAG_NORMAL);
805                                 pos_x = xmin - 4*10;
806                                 Sbar_DrawXNum(pos + '0 24', tm.sb_frags, 4, 10, rgb, 1, DRAWFLAG_NORMAL);
807                                 pos_x = xmin;
808                         } else
809                                 Sbar_DrawXNum(pos, tm.sb_frags, 4, 24, rgb, 1, DRAWFLAG_NORMAL);
810                         pos_x = xmin;
811
812                         // abuse specs as playerounter
813                         specs = 0;
814                         for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
815                         {
816                                 if(pl.sb_team == tm.sb_team)
817                                         ++specs;
818                         }
819
820                         if(specs < 2)
821                                 specs = 2;
822                         if(gametype == GAME_CTF && specs < 4)
823                                 specs = 4;
824                         
825                         tmp_x = sbwidth;
826                         tmp_y = 1.25 * sbar_fontsize_y * specs;
827                         drawfill(pos - '1 1', tmp + '2 0', rgb, 0.2, DRAWFLAG_NORMAL);
828                         
829                         for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
830                         {
831                                 if(pl.sb_team != tm.sb_team)
832                                         continue;
833                                 Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), SBAR_DEFAULT_MASK);
834                                 pos_y += 1.25 * sbar_fontsize_y;
835                                 tmp_y -= 1.25 * sbar_fontsize_y;
836                         }
837                         pos_y += tmp_y + 1.5 * sbar_fontsize_y;
838                 }
839                 // rgb := tempvector :)
840                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
841                 pos_y += 3 * sbar_fontsize_y;
842                 specs = 0;
843                 for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
844                 {
845                         if(pl.sb_team != COLOR_SPECTATOR)
846                                 continue;
847                         //drawcolorcodedstring(pos, getplayerkey(pl.sb_player, "name"), '8 8 0', 1, 0);
848                         Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), SBAR_MASK_SPECTATORS);
849                         pos += '0 1.25 0' * sbar_fontsize_y;
850                         ++specs;
851                 }
852                         
853                 if(specs)
854                         drawstring(rgb, "Spectators", sbar_fontsize, '1 1 1', 1, 0);
855         } else {
856                 pos_x = xmin;
857                 for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
858                 {
859                         if(pl.sb_team == COLOR_SPECTATOR)
860                                 continue;
861                         Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), SBAR_DEFAULT_MASK);
862                         pos_y += 1.25 * sbar_fontsize_y;
863                         tmp_y -= 1.25 * sbar_fontsize_y;
864                 }
865
866                 // rgb := tempvector :)
867                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
868                 pos_y += 3 * sbar_fontsize_y;
869                 specs = 0;
870                 for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
871                 {
872                         if(pl.sb_team != COLOR_SPECTATOR)
873                                 continue;
874                         //drawcolorcodedstring(pos, getplayerkey(pl.sb_player, "name"), '8 8 0', 1, 0);
875                         Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), SBAR_MASK_SPECTATORS);
876                         pos += '0 1.25 0' * sbar_fontsize_y;
877                         ++specs;
878                 }
879                         
880                 if(specs)
881                         drawstring(rgb, "Spectators", sbar_fontsize, '1 1 1', 1, 0);
882         }
883         sbar = sbar_save;
884 }
885
886 void Sbar_Score(float margin)
887 {
888         float timelimit, timeleft, minutes, seconds, distribution, myplace;
889         vector sbar_save, place;
890         entity tm, pl, me;
891         sbar_save = sbar;
892
893         sbar_y = vid_conheight - (32+12);
894         sbar_x -= margin;
895         
896         place = '-48 -12 0';
897         if(teamplay)
898         {
899                 // Layout:
900                 //
901                 //   team1 team3 team4
902                 //
903                 //         TEAM2
904                 //for(i = 0; i < 4; ++i)
905                 for(tm = sortedTeams.sort_next; tm; tm = tm.sort_next)
906                 {
907                         if(tm.sb_team == COLOR_SPECTATOR || !tm.sb_player) // no players? don't display
908                                 continue;
909                         // -32*4 = -128
910                         if(tm.sb_team == myteam)
911                                 Sbar_DrawXNum('-128 0', tm.sb_frags, 4, 32, GetTeamRGB(tm.sb_team), 1, DRAWFLAG_NORMAL);
912                         else
913                         {
914                                 Sbar_DrawXNum(place, tm.sb_frags, 4, 12, GetTeamRGB(tm.sb_team), 1, DRAWFLAG_NORMAL);
915                                 place_x -= 4*12;
916                         }
917                 }
918         } else {
919                 // me vector := [team/connected frags id]
920                 myplace = 0;
921                 for(me = sortedPlayers.sort_next; me; me = me.sort_next)
922                 {
923                         if(me.sb_team != COLOR_SPECTATOR)
924                                 ++myplace;
925                         if(me.sb_player == player_localentnum - 1)
926                                 break;
927                 }
928                 pl = sortedPlayers.sort_next;
929                 if(pl == me)
930                         pl = pl.sort_next;
931                 
932                 if(pl && myplace != 1)
933                 {
934                         distribution = me.sb_frags - pl.sb_frags;
935                 } else if(pl) {
936                         distribution = me.sb_frags - pl.sb_frags;
937                 } else
938                         distribution = 0;
939                 
940                 if(myplace == 1)
941                         Sbar_DrawXNum('-36 -12', myplace, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
942                 else if(myplace == 2)
943                         Sbar_DrawXNum('-36 -12', myplace, 3, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
944                 else
945                         Sbar_DrawXNum('-36 -12', myplace, 3, 12, '1 0 0', 1, DRAWFLAG_NORMAL);
946
947                 if(distribution >= 0)
948                 {
949                         Sbar_DrawXNum('-84 -12', distribution, 4, 12, ' 1 1 1', 1, DRAWFLAG_NORMAL);
950                         Sbar_DrawXNum('-128 0', me.sb_frags, 4, 32, '1 1 1', 1, DRAWFLAG_NORMAL);
951                 } else if(distribution >= -5)
952                 {
953                         Sbar_DrawXNum('-84 -12', distribution, 4, 12, ' 1 1 0', 1, DRAWFLAG_NORMAL);
954                         Sbar_DrawXNum('-128 0', me.sb_frags, 4, 32, '1 1 0', 1, DRAWFLAG_NORMAL);
955                 } else {
956                         Sbar_DrawXNum('-84 -12', distribution, 4, 12, ' 1 0 0', 1, DRAWFLAG_NORMAL);
957                         Sbar_DrawXNum('-128 0', me.sb_frags, 4, 32, '1 0 0', 1, DRAWFLAG_NORMAL);
958                 }
959         }
960         timelimit = getstatf(STAT_TIMELIMIT);
961         if(timelimit)
962         {
963                 timeleft = max(0, timelimit * 60 - time);
964                 minutes = floor(timeleft / 60);
965                 seconds = floor(timeleft - minutes*60);
966                 if(minutes >= 5)
967                 {
968                         Sbar_DrawXNum('-72 32', minutes, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
969                         drawpic(sbar + '-36 32', "gfx/num_colon", '12 12', '1 1 1', sbar_alpha_fg, 0);
970                         Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
971                 } else if(minutes >= 1)
972                 {
973                         Sbar_DrawXNum('-72 32', minutes, 3, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
974                         drawpic(sbar + '-36 32', "gfx/num_colon", '12 12', '1 1 0', sbar_alpha_fg, 0);
975                         Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
976                 } else {
977                         Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 0 0', 1, DRAWFLAG_NORMAL);
978                 }
979         } else {
980                 minutes = floor(time / 60);
981                 seconds = floor(time - minutes*60);
982                 Sbar_DrawXNum('-72 32', minutes, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
983                 drawpic(sbar + '-36 32', "gfx/num_colon", '12 12', '1 1 1', sbar_alpha_fg, 0);
984                 Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
985         }
986         sbar = sbar_save;
987 }
988
989 void Sbar_MiniscoreItem(vector pos, entity pl, float is_self)
990 {
991         float x;
992         pos_x += 72;
993         
994         if(teamplay)
995                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(pl.sb_team)*0.5, 1, DRAWFLAG_NORMAL);
996         else
997                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
998         x = pos_x;
999         pos_x += 5*8;
1000         pos_x -= stringwidth(ftos(pl.sb_frags), FALSE)*8;
1001         drawstring(pos, ftos(pl.sb_frags), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1002         pos_x = x;
1003         if(is_self)
1004         {
1005                 pos_x += 48;
1006                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1007                 pos_x += 8;
1008         } else
1009                 pos_x += 56;
1010         drawcolorcodedstring(pos, getplayerkey(pl.sb_player, "name"), '8 8 0', 1, 0);
1011 }
1012
1013 void Sbar_MiniscoreTeamItem(vector pos, float color, float frags, float is_self)
1014 {
1015         float x;
1016         pos_x += 72;
1017         
1018         if(teamplay)
1019                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(color)*0.5, 1, DRAWFLAG_NORMAL);
1020         else
1021                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
1022         x = pos_x;
1023         pos_x += 5*8;
1024         pos_x -= stringwidth(ftos(frags), FALSE)*8;
1025         drawstring(pos, ftos(frags), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1026         pos_x = x;
1027         if(is_self)
1028         {
1029                 pos_x += 48;
1030                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1031                 pos_x += 8;
1032         } else
1033                 pos_x += 56;
1034         drawstring(pos, GetTeamName(color), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1035 }
1036
1037 void Sbar_MiniDeathmatchOverlay(vector pos)
1038 {
1039         float numlines, up, down;
1040         entity me, tm, pl;
1041         float miniscoreboard_size;
1042         miniscoreboard_size = cvar("sbar_miniscoreboard_size");
1043         
1044         if(miniscoreboard_size == 0)
1045                 return;
1046         pos_y = vid_conheight - 8;
1047         
1048         if(miniscoreboard_size < 0)
1049                 numlines = (vid_conheight - sbar_y + 7) / 8;
1050         else
1051                 numlines = miniscoreboard_size;
1052
1053         // give up if there isn't enough room
1054         if(pos_x >= vid_conwidth || pos_y >= vid_conheight || numlines < 1)
1055                 return;
1056
1057         // me vector := [team/connected frags id]
1058         for(me = sortedPlayers.sort_next; me; me = me.sort_next)
1059         {
1060                 if(me.sb_player == player_localentnum - 1)
1061                         break;
1062         }
1063
1064         if(teamplay)
1065                 numlines -= numteams;
1066
1067         // figure out how many players above and below we can show
1068         up = floor(numlines/2);
1069         down = up;
1070         if((up + down) > numlines)
1071                 down = numlines - up;
1072
1073         // render bottom-up
1074         for(pl = me.sort_next; pl && down > 0; pl = pl.sort_next)
1075         {
1076                 if(pl.sb_team == COLOR_SPECTATOR)
1077                         continue;
1078                 Sbar_MiniscoreItem(pos, pl, false);
1079                 pos_y -= 9;
1080                 --down;
1081         }
1082         Sbar_MiniscoreItem(pos, me, true);
1083         pos_y -= 9;
1084         up += down; // if there weren't enough lines below... add them
1085         for(pl = me.sort_prev; pl != sortedPlayers && up > 0; pl = pl.sort_prev)
1086         {
1087                 if(pl.sb_team == COLOR_SPECTATOR)
1088                         continue;
1089                 Sbar_MiniscoreItem(pos, pl, false);
1090                 pos_y -= 9;
1091                 --up;
1092         }
1093
1094         if(teamplay)
1095         {
1096                 for(tm = sortedTeams.sort_next; tm.sort_next; tm = tm.sort_next);
1097                 for(; tm != sortedTeams; tm = tm.sort_prev)
1098                 {
1099                         if(!tm.sb_player || tm.sb_team == COLOR_SPECTATOR) // no players?
1100                                 continue;
1101                         Sbar_MiniscoreTeamItem(pos, tm.sb_team, tm.sb_frags, (tm.sb_team == me.sb_team));
1102                         pos_y -= 9;
1103                 }
1104         }
1105 }
1106
1107 void Sbar_Draw (void)
1108 {
1109         float i;
1110         float x, fade;
1111         float stat_items;
1112
1113         Sbar_SortFrags();
1114
1115         sb_lines = 24;
1116         
1117         if (sb_showscores)
1118                 Sbar_DrawScoreboard();
1119         else if (intermission == 1)
1120         {
1121                 Sbar_DrawScoreboard();
1122                 return;
1123         }
1124         else if (intermission == 2)
1125                 Sbar_FinaleOverlay();
1126         else
1127         {
1128                 if (sb_showscores || (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard")))
1129                 {
1130                         sbar_x = (vid_conwidth - 640.0)*0.5;
1131                         sbar_y = vid_conheight - 47;
1132                         //Sbar_DrawAlphaPic (sbar_x, sbar_y, sb_scorebar, sbar_alpha_bg.value);
1133                         //drawpic('0 0', "gfx/scorebar", '0 0 0', '1 1 1', cvar("sbar_alpha_bg"), 0);
1134                         Sbar_DrawScoreboard ();
1135                 }
1136                 else
1137                 {
1138                         if (sb_lines && sbar_hudselector == 1)
1139                         {
1140                                 stat_items = getstati(STAT_ITEMS);
1141
1142                                 sbar_x = (vid_conwidth - 320.0)*0.5;
1143                                 sbar_y = vid_conheight - 24.0 - 16.0;
1144                                 sbar_z = 0;
1145                         
1146                                 fade = 3.2 - 2 * (time - weapontime);
1147                                 fade = bound(0.7, fade, 1);
1148
1149                                 x = 1.0;
1150                                 for(i = 0; i < 8; ++i)
1151                                 {
1152                                         if(stat_items & x)
1153                                         {
1154                                                 Sbar_DrawWeapon(i+1, fade, (i + 2 == activeweapon));
1155                                         }
1156                                         x *= 2;
1157                                 }
1158                                 x *= 2*2*2*2;
1159                                 if(stat_items & x)
1160                                 {
1161                                         Sbar_DrawWeapon(0, fade, (activeweapon == 1));
1162                                 }
1163
1164                                 // armor
1165                                 x = getstati(STAT_ARMOR);
1166                                 if (x > 0)
1167                                 {
1168                                         // "gfx/sb_armor"
1169                                         //Sbar_DrawStretchPic (72, 0, sb_armor[0], sbar_alpha_fg.value, 24, 24);
1170                                         drawpic(sbar + '72 0', "gfx/sb_armor", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1171                                         if(x > 200)
1172                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0 1 0', 1, 0);
1173                                         else if(x > 100)
1174                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0.2 1 0', 1, 0);
1175                                         else if(x > 50)
1176                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1177                                         else if(x > 25)
1178                                                 Sbar_DrawXNum('0 0', x, 3, 24, '1 1 0.2', 1, 0);
1179                                         else
1180                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0.7 0 0', 1, 0);
1181                                 }
1182
1183                                 // health
1184                                 x = getstati(STAT_HEALTH);
1185                                 if (x != 0)
1186                                 {
1187                                         // "gfx/sb_health"
1188                                         //Sbar_DrawStretchPic (184, 0, sb_health, sbar_alpha_fg.value, 24, 24);
1189                                         drawpic(sbar + '184 0', "gfx/sb_health", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1190                                         if(x > 200)
1191                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0 1 0', 1, 0);
1192                                         else if(x > 100)
1193                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0.2 1 0', 1, 0);
1194                                         else if(x > 50)
1195                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1196                                         else if(x > 25)
1197                                                 Sbar_DrawXNum('112 0', x, 3, 24, '1 1 0.2', 1, 0);
1198                                         else
1199                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0.7 0 0', 1, 0);
1200                                 }
1201
1202                                 // ammo
1203                                 x = getstati(STAT_AMMO);
1204                                 if ((stat_items & (NEX_IT_SHELLS | NEX_IT_BULLETS | NEX_IT_ROCKETS | NEX_IT_CELLS)) || x != 0)
1205                                 {
1206                                         if (stat_items & NEX_IT_SHELLS)
1207                                                 drawpic(sbar + '296 0', "gfx/sb_shells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1208                                         else if (stat_items & NEX_IT_BULLETS)
1209                                                 drawpic(sbar + '296 0', "gfx/sb_bullets", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1210                                         else if (stat_items & NEX_IT_ROCKETS)
1211                                                 drawpic(sbar + '296 0', "gfx/sb_rocket", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1212                                         else if (stat_items & NEX_IT_CELLS)
1213                                                 drawpic(sbar + '296 0', "gfx/sb_cells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1214                                         if(x > 10)
1215                                                 Sbar_DrawXNum('224 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1216                                         else
1217                                                 Sbar_DrawXNum('224 0', x, 3, 24, '0.7 0 0', 1, 0);
1218                                 }
1219
1220                                 if (sbar_x + 320 + 160 <= vid_conwidth)
1221                                         Sbar_MiniDeathmatchOverlay(sbar + '320 0');
1222                                 if (sbar_x > 0)
1223                                         Sbar_Score(16);
1224                                 // The margin can be at most 8 to support 640x480 console size:
1225                                 //   320 + 2 * (144 + 16) = 640
1226                         }
1227                         else if (sb_lines)
1228                         {
1229                         
1230                                 stat_items = getstati(STAT_ITEMS);
1231                         
1232                                 sbar_x = (vid_conwidth - 640.0)*0.5;
1233                                 sbar_y = vid_conheight - 47;
1234                                 sbar_z = 0;
1235
1236                                 fade = 3 - 2 * (time - weapontime);
1237
1238                                 x = 1.0;
1239                                 for(i = 0; i < 8; ++i)
1240                                 {
1241                                         if(stat_items & x)
1242                                         {
1243                                                 Sbar_DrawWeapon(i+1, fade, (i + 2 == activeweapon));
1244                                         }
1245                                         x *= 2;
1246                                 }
1247                                 x *= 2*2*2*2;
1248                                 if(stat_items & x)
1249                                 {
1250                                         Sbar_DrawWeapon(0, fade, (activeweapon == 1));
1251                                 }
1252
1253                                 if (sb_lines > 24)
1254                                         drawpic(sbar, "gfx/sbar", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1255                                 else
1256                                         drawpic(sbar, "gfx/sbar_minimal", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1257
1258                                 // armor
1259                                 // (340-3*24) = 268
1260                                 Sbar_DrawXNum('268 12', getstati(STAT_ARMOR), 3, 24, '0.6 0.7 0.8', 1, 0);
1261
1262                                 // health
1263                                 // (154-3*24) = 82
1264                                 x = getstati(STAT_HEALTH);
1265                                 if(x > 100)
1266                                         Sbar_DrawXNum('82 12', x, 3, 24, '1 1 1', 1, 0);
1267                                 else if(x <= 25 && time - floor(time) > 0.5)
1268                                         Sbar_DrawXNum('82 12', x, 3, 24, '0.7 0 0', 1, 0);
1269                                 else
1270                                         Sbar_DrawXNum('81 12', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1271
1272                                 // AK dont draw ammo for the laser
1273                                 x = getstati(STAT_AMMO);
1274                                 if(activeweapon != 12)
1275                                 {
1276                                         // (519-3*24) = 447
1277                                         if (stat_items & NEX_IT_SHELLS)
1278                                                 drawpic(sbar + '519 0', "gfx/sb_shells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1279                                         else if (stat_items & NEX_IT_BULLETS)
1280                                                 drawpic(sbar + '519 0', "gfx/sb_bullets", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1281                                         else if (stat_items & NEX_IT_ROCKETS)
1282                                                 drawpic(sbar + '519 0', "gfx/sb_rocket", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1283                                         else if (stat_items & NEX_IT_CELLS)
1284                                                 drawpic(sbar + '519 0', "gfx/sb_cells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1285                                         if(x > 10)
1286                                                 Sbar_DrawXNum('447 12', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1287                                         else
1288                                                 Sbar_DrawXNum('447 12', x, 3, 24, '0.7 0 0', 1, 0);
1289                                 }
1290
1291                                 if (sb_lines > 24)
1292                                         drawpic(sbar, "gfx/sbar_overlay", '0 0 0', '1 1 1', 1, DRAWFLAG_MODULATE);
1293
1294                                 if (sbar_x + 600 + 160 <= vid_conwidth)
1295                                         Sbar_MiniDeathmatchOverlay (sbar + '600 0');
1296
1297                                 if (sbar_x > 0)
1298                                         Sbar_Score(-16);
1299                                 // Because:
1300                                 //   Mini scoreboard uses 12*4 per other team, that is, 144
1301                                 //   pixels when there are four teams...
1302                                 //   Nexuiz by default sets vid_conwidth to 800... makes
1303                                 //   sbar_x == 80...
1304                                 //   so we need to shift it by 64 pixels to the right to fit
1305                                 //   BUT: then it overlaps with the image that gets drawn
1306                                 //   for viewsize 100! Therefore, just account for 3 teams,
1307                                 //   that is, 96 pixels mini scoreboard size, needing 16 pixels
1308                                 //   to the right!
1309                         }
1310                 
1311                 
1312                         if(gametype == GAME_KEYHUNT)
1313                         {
1314                                 CSQC_kh_hud();
1315                         } else if(gametype == GAME_CTF)
1316                         {
1317                                 CSQC_ctf_hud();
1318                         }
1319                 }
1320         }
1321 }
1322
1323 void CSQC_ctf_hud(void)
1324 {
1325         // cvar("sbar_flagstatus_right") move the flag icons right
1326         // cvar("sbar_flagstatus_pos") pixel position of the nexuiz flagstatus icons
1327         float redflag, blueflag;
1328         float stat_items;
1329         vector pos;
1330         
1331         stat_items = getstati(STAT_ITEMS);
1332         redflag = (stat_items/32768) & 3;
1333         blueflag = (stat_items/131072) & 3;
1334
1335         /**
1336          * FTEQCC BUG!
1337          * For some reason now not even THAT works there...
1338          * Maybe the minus' precedence screws it up? The last one there, maybe I should use brackets
1339          **
1340          * pos_x = (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - sbar_x - 64 : 10 - sbar_x;
1341          ** Should try those later:
1342          * pos_x = (cvar("sbar_flagstatus_right")) ? (vid_conwidth - 10 - sbar_x - 64) : (10 - sbar_x);
1343          * pos_x = ( (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - 64 : 10 ) - sbar_x;
1344          */
1345         
1346         if(cvar("sbar_flagstatus_right"))
1347                 pos_x = vid_conwidth - 10 - sbar_x - 64;
1348         else
1349                 pos_x = 10 - sbar_x;
1350         
1351         pos_z = 0;
1352
1353         if(sbar_hudselector == 1)
1354                 pos_y = (vid_conheight - sbar_y) - cvar("sbar_flagstatus_pos") - 64;
1355         else
1356                 pos_y = -117;
1357
1358         pos += sbar;
1359
1360         switch(redflag)
1361         {
1362         case 1: drawpic(pos, "gfx/sb_flag_red_taken", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1363         case 2: drawpic(pos, "gfx/sb_flag_red_lost", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1364         case 3: drawpic(pos, "gfx/sb_flag_red_carrying", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1365         }
1366
1367         pos_y -= 64;
1368         
1369         switch(blueflag)
1370         {
1371         case 1: drawpic(pos, "gfx/sb_flag_blue_taken", '0 0 0', '1 1 1', 1, 0); break;
1372         case 2: drawpic(pos, "gfx/sb_flag_blue_lost", '0 0 0', '1 1 1', 1, 0); break;
1373         case 3: drawpic(pos, "gfx/sb_flag_blue_carrying", '0 0 0', '1 1 1', 1, 0); break;
1374         }
1375 }