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