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