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