]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/sbar.qc
[ 2555913 ] Strength and invincibility timer
[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(!left.gotscores)
221                 vl = COLOR_SPECTATOR;
222         if(!right.gotscores)
223                 vr = COLOR_SPECTATOR;
224         
225         if(vl > vr)
226                 return true;
227         if(vl < vr)
228                 return false;
229
230         if(vl == COLOR_SPECTATOR)
231         {
232                 // FIRST the one with scores (spectators), THEN the ones without (downloaders)
233                 // no other sorting
234                 if(!left.gotscores && right.gotscores)
235                         return true;
236                 return false;
237         }
238
239         vl = left.scores[ps_primary];
240         vr = right.scores[ps_primary];
241         if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
242         {
243                 if(vl == 0 && vr != 0)
244                         return 1;
245                 if(vl != 0 && vr == 0)
246                         return 0;
247         }
248         if(vl > vr)
249                 return IS_INCREASING(scores_flags[ps_primary]);
250         if(vl < vr)
251                 return IS_DECREASING(scores_flags[ps_primary]);
252         
253         vl = left.scores[ps_secondary];
254         vr = right.scores[ps_secondary];
255         if(scores_flags[ps_secondary] & SFL_ZERO_IS_WORST)
256         {
257                 if(vl == 0 && vr != 0)
258                         return 1;
259                 if(vl != 0 && vr == 0)
260                         return 0;
261         }
262         if(vl > vr)
263                 return IS_INCREASING(scores_flags[ps_secondary]);
264         if(vl < vr)
265                 return IS_DECREASING(scores_flags[ps_secondary]);
266         
267         return false;
268 }
269
270 void Sbar_UpdatePlayerPos(entity player)
271 {
272         for(other = player.sort_next; other && Sbar_ComparePlayerScores(player, other); other = player.sort_next)
273         {
274                 SORT_SWAP(player, other);
275         }
276         for(other = player.sort_prev; other != players && Sbar_ComparePlayerScores(other, player); other = player.sort_prev)
277         {
278                 SORT_SWAP(other, player);
279         }
280 }
281
282 float Sbar_CompareTeamScores(entity left, entity right)
283 {
284         float vl, vr;
285
286         if(left.team == COLOR_SPECTATOR)
287                 return 1;
288         if(right.team == COLOR_SPECTATOR)
289                 return 0;
290         
291         vl = left.teamscores[ts_primary];
292         vr = right.teamscores[ts_primary];
293         if(vl > vr)
294                 return IS_INCREASING(teamscores_flags[ts_primary]);
295         if(vl < vr)
296                 return IS_DECREASING(teamscores_flags[ts_primary]);
297         
298         vl = left.teamscores[ts_secondary];
299         vr = right.teamscores[ts_secondary];
300         if(vl > vr)
301                 return IS_INCREASING(teamscores_flags[ts_secondary]);
302         if(vl < vr)
303                 return IS_DECREASING(teamscores_flags[ts_secondary]);
304
305         return false;
306 }
307
308 void Sbar_UpdateTeamPos(entity Team)
309 {
310         for(other = Team.sort_next; other && Sbar_CompareTeamScores(Team, other); other = Team.sort_next)
311         {
312                 SORT_SWAP(Team, other);
313         }
314         for(other = Team.sort_prev; other != teams && Sbar_CompareTeamScores(other, Team); other = Team.sort_prev)
315         {
316                 SORT_SWAP(other, Team);
317         }
318 }
319
320 void Cmd_Sbar_Help(float argc)
321 {
322         print("You can modify the scoreboard using the\n");
323         print("^3|---------------------------------------------------------------|\n");
324         print("^1 TO BE DONE\n");
325         print("Usage:\n");
326         print("^2sbar_columns_set default\n");
327         print("^2sbar_columns_set ^7filed1 field2 ...\n");
328         print("The following field names are recognized (case INsensitive):\n");
329         print("You can use a ^3|^7 to start the right-aligned fields.\n");
330         
331         print("^3name^7 or ^3nick^7             Name of a player\n");
332         print("^3ping^7                     Ping time\n\n");
333         print("^3kd^7 or ^3kdr^7 or ^3kdratio^7 or ^3k/d\n");
334         print("                         The kill-death ratio\n");
335
336         print("Before a field you can put a + or - sign, then a comma separated list\n");
337         print("of game types, then a slash, to make the field show up only in these\n");
338         print("or in all but these game types.\n");
339
340         print("The special game type names 'teams' and 'noteams' can be used to\n");
341         print("include/exclude ALL teams/noteams game modes.\n");
342
343         local float i;
344         print("Additional columns:\n");
345         for(i = 0; i < MAX_SCORE; ++i)
346         {
347                 if(scores_label[i] != "")
348                         print(strcat(scores_label[i], "\n"));
349         }
350 }
351
352 #define MIN_NAMELEN 24
353 #define MAX_NAMELEN 24
354
355 string Sbar_DefaultColumnLayout()
356 {
357         return strcat( // fteqcc sucks
358                 "ping pl name | ",
359                 "+noteams/kills +noteams/deaths +noteams/suicides -noteams,tdm/frags ", // tdm already has this in "score"
360                 "+kh/caps +kh/pushes +kh/destroyed ",
361                 "+ctf/caps +ctf/pickups +ctf/fckills +ctf/returns ",
362                 "-lms/score");
363 }
364
365 void Cmd_Sbar_SetFields(float argc)
366 {
367         float i, j, slash;
368         string str, pattern, subpattern, subpattern2;
369         float digit;
370         float have_name, have_primary, have_secondary, have_separator;
371         float missing;
372
373         // TODO: re enable with gametype dependant cvars?
374         if(argc < 2) // no arguments provided
375                 argc = tokenizebyseparator(strcat("x ", cvar_string("sbar_columns")), " ");
376
377         if(argc < 2)
378                 argc = tokenizebyseparator(strcat("x ", Sbar_DefaultColumnLayout()), " ");
379
380         if(argc == 2)
381         {
382                 if(argv(1) == "default")
383                         argc = tokenizebyseparator(strcat("x ", Sbar_DefaultColumnLayout()), " ");
384                 else if(argv(1) == "all")
385                 {
386                         string s;
387                         s = "ping pl color name |";
388                         for(i = 0; i < MAX_SCORE; ++i)
389                         {
390                                 if(i != ps_primary)
391                                 if(i != ps_secondary)
392                                 if(scores_label[i] != "")
393                                         s = strcat(s, " ", scores_label[i]);
394                         }
395                         if(ps_secondary != ps_primary)
396                                 s = strcat(s, " ", scores_label[ps_secondary]);
397                         s = strcat(s, " ", scores_label[ps_primary]);
398                         argc = tokenizebyseparator(strcat("x ", s), " ");
399                 }
400         }
401                 
402         
403         sbar_num_fields = 0;
404
405         drawfont = sbar_font;
406         digit = stringwidth("0123456789", FALSE) / 10;
407
408         subpattern = strcat(",", GametypeNameFromType(gametype), ",");
409         if(teamplay)
410                 subpattern2 = ",teams,";
411         else
412                 subpattern2 = ",noteams,";
413
414         for(i = 0; i < argc - 1; ++i)
415         {
416                 str = argv(i+1);
417
418                 slash = strstrofs(str, "/", 0);
419                 if(slash >= 0)
420                 {
421                         pattern = substring(str, 0, slash);
422                         str = substring(str, slash + 1, strlen(str) - (slash + 1));
423
424                         if(substring(pattern, 0, 1) == "-")
425                         {
426                                 pattern = substring(pattern, 1, strlen(pattern) - 1);
427                                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) >= 0)
428                                         continue;
429                                 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) >= 0)
430                                         continue;
431                         }
432                         else
433                         {
434                                 if(substring(pattern, 0, 1) == "+")
435                                         pattern = substring(pattern, 1, strlen(pattern) - 1);
436                                 if(strstrofs(strcat(",", pattern, ","), subpattern, 0) < 0)
437                                 if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) < 0)
438                                         continue;
439                         }
440                 }
441
442                 strunzone(sbar_title[sbar_num_fields]);
443                 sbar_title[sbar_num_fields] = strzone(str);
444                 sbar_size[sbar_num_fields] = stringwidth(str, FALSE);
445                 str = strtolower(str);
446
447                 if(str == "ping") {
448                         sbar_field[sbar_num_fields] = SP_PING;
449                 } else if(str == "pl") {
450                         sbar_field[sbar_num_fields] = SP_PL;
451                 } else if(str == "kd" || str == "kdr" || str == "kdratio" || str == "k/d") {
452                         sbar_field[sbar_num_fields] = SP_KDRATIO;
453                 } else if(str == "name" || str == "nick") {
454                         sbar_field[sbar_num_fields] = SP_NAME;
455                         sbar_size[sbar_num_fields] = MIN_NAMELEN; // minimum size? any use?
456                         have_name = 1;
457                 } else if(str == "|") {
458                         sbar_field[sbar_num_fields] = SP_SEPARATOR;
459                         have_separator = 1;
460                 } else {
461                         for(j = 0; j < MAX_SCORE; ++j)
462                                 if(str == strtolower(scores_label[j]))
463                                         goto found; // sorry, but otherwise fteqcc -O3 miscompiles this and warns about "unreachable code"
464 :notfound
465                         if(str == "frags")
466                         {
467                                 j = SP_FRAGS;
468                         }
469                         else
470                         {
471                                 print(strcat("^1Error:^7 Unknown score field: '", str, "'\n"));
472                                 continue;
473                         }
474 :found
475                         sbar_field[sbar_num_fields] = j;
476                         if(j == ps_primary)
477                                 have_primary = 1;
478                         if(j == ps_secondary)
479                                 have_secondary = 1;
480                 }
481                 ++sbar_num_fields;
482                 if(sbar_num_fields >= MAX_SBAR_FIELDS)
483                         break;
484         }
485
486         if(scores_flags[ps_primary] & SFL_ALLOW_HIDE)
487                 have_primary = 1;
488         if(scores_flags[ps_secondary] & SFL_ALLOW_HIDE)
489                 have_secondary = 1;
490         if(ps_primary == ps_secondary)
491                 have_secondary = 1;
492         missing = !have_primary + !have_secondary + !have_separator + !have_name;
493
494         if(sbar_num_fields+missing < MAX_SBAR_FIELDS)
495         {
496                 if(!have_name)
497                 {
498                         strunzone(sbar_title[sbar_num_fields]);
499                         for(i = sbar_num_fields; i > 0; --i)
500                         {
501                                 sbar_title[i] = sbar_title[i-1];
502                                 sbar_size[i] = sbar_size[i-1];
503                                 sbar_field[i] = sbar_field[i-1];
504                         }
505                         sbar_title[0] = strzone("name");
506                         sbar_field[0] = SP_NAME;
507                         sbar_size[0] = MIN_NAMELEN; // minimum size? any use?
508                         ++sbar_num_fields;
509                         print("fixed missing field 'name'\n");
510
511                         if(!have_separator)
512                         {
513                                 strunzone(sbar_title[sbar_num_fields]);
514                                 for(i = sbar_num_fields; i > 1; --i)
515                                 {
516                                         sbar_title[i] = sbar_title[i-1];
517                                         sbar_size[i] = sbar_size[i-1];
518                                         sbar_field[i] = sbar_field[i-1];
519                                 }
520                                 sbar_title[1] = strzone("|");
521                                 sbar_field[1] = SP_SEPARATOR;
522                                 sbar_size[1] = stringwidth("|", FALSE);
523                                 ++sbar_num_fields;
524                                 print("fixed missing field '|'\n");
525                         }
526                 }
527                 else if(!have_separator)
528                 {
529                         strunzone(sbar_title[sbar_num_fields]);
530                         sbar_title[sbar_num_fields] = strzone("|");
531                         sbar_size[sbar_num_fields] = stringwidth("|", FALSE);
532                         sbar_field[sbar_num_fields] = SP_SEPARATOR;
533                         ++sbar_num_fields;
534                         print("fixed missing field '|'\n");
535                 }
536
537                 if(!have_secondary)
538                 {
539                         strunzone(sbar_title[sbar_num_fields]);
540                         sbar_title[sbar_num_fields] = strzone(scores_label[ps_secondary]);
541                         sbar_size[sbar_num_fields] = stringwidth(sbar_title[sbar_num_fields], FALSE);
542                         sbar_field[sbar_num_fields] = ps_secondary;
543                         ++sbar_num_fields;
544                         print("fixed missing field '", scores_label[ps_secondary], "'\n");
545                 }
546
547                 if(!have_primary)
548                 {
549                         strunzone(sbar_title[sbar_num_fields]);
550                         sbar_title[sbar_num_fields] = strzone(scores_label[ps_primary]);
551                         sbar_size[sbar_num_fields] = stringwidth(sbar_title[sbar_num_fields], FALSE);
552                         sbar_field[sbar_num_fields] = ps_primary;
553                         ++sbar_num_fields;
554                         print("fixed missing field '", scores_label[ps_primary], "'\n");
555                 }
556         }
557
558         sbar_field[sbar_num_fields] = SP_END;
559 }
560
561 // MOVEUP::
562 vector sbar_field_rgb;
563 string sbar_field_icon0;
564 string sbar_field_icon1;
565 string sbar_field_icon2;
566 vector sbar_field_icon0_rgb;
567 vector sbar_field_icon1_rgb;
568 vector sbar_field_icon2_rgb;
569 float sbar_field_icon0_alpha;
570 float sbar_field_icon1_alpha;
571 float sbar_field_icon2_alpha;
572 string Sbar_GetField(entity pl, float field)
573 {
574         float tmp, num, denom, f;
575         string str;
576         sbar_field_rgb = '1 1 1';
577         sbar_field_icon0 = "";
578         sbar_field_icon1 = "";
579         sbar_field_icon2 = "";
580         sbar_field_icon0_rgb = '1 1 1';
581         sbar_field_icon1_rgb = '1 1 1';
582         sbar_field_icon2_rgb = '1 1 1';
583         sbar_field_icon0_alpha = 1;
584         sbar_field_icon1_alpha = 1;
585         sbar_field_icon2_alpha = 1;
586         switch(field)
587         {
588                 case SP_PING:
589                         if not(pl.gotscores)
590                                 return "\x8D\x8D\x8D"; // >>> sign
591                         str = getplayerkey(pl.sv_entnum, "ping");
592                         if(str == "0")
593                                 return "N/A";
594                         tmp = max(0, min(220, stof(str)-80)) / 220;
595                         sbar_field_rgb = '1 1 1' - '0 1 1'*tmp;
596                         return str;
597                 
598                 case SP_PL:
599                         if not(pl.gotscores)
600                                 return "N/A";
601                         str = getplayerkey(pl.sv_entnum, "pl");
602                         if(str == "0")
603                                 return "";
604                         tmp = bound(0, stof(str), 20) / 20; // 20% is REALLY BAD pl
605                         sbar_field_rgb = '1 0.5 0.5' - '0 0.5 0.5'*tmp;
606                         return str;
607                 
608                 case SP_NAME:
609                         if(!teamplay)
610                         {
611                                 f = stof(getplayerkey(pl.sv_entnum, "colors"));
612                                 if(ready_waiting && pl.ready)
613                                 {
614                                         sbar_field_icon0 = "gfx/sb_player_ready";
615                                 }
616                                 else
617                                 {
618                                         sbar_field_icon0 = "gfx/sb_playercolor_base";
619                                         sbar_field_icon1 = "gfx/sb_playercolor_shirt";
620                                         sbar_field_icon1_rgb = colormapPaletteColor(floor(f / 16), 0);
621                                         sbar_field_icon2 = "gfx/sb_playercolor_pants";
622                                         sbar_field_icon2_rgb = colormapPaletteColor(mod(f, 16), 1);
623                                 }
624                         }
625                         return GetPlayerName(pl.sv_entnum);
626
627                 case SP_FRAGS:
628                         f = pl.(scores[SP_KILLS]);
629                         f -= pl.(scores[SP_SUICIDES]);
630                         return ftos(f);
631
632                 case SP_KDRATIO:
633                         num = pl.(scores[SP_KILLS]);
634                         denom = pl.(scores[SP_DEATHS]);
635
636                         if(denom == 0) {
637                                 sbar_field_rgb = '0 1 0';
638                                 str = ftos(num);
639                         } else if(num <= 0) {
640                                 sbar_field_rgb = '1 0 0';
641                                 str = ftos(num/denom);
642                         } else
643                                 str = ftos(num/denom);
644                 
645                         tmp = strstrofs(str, ".", 0);
646                         if(tmp > 0)
647                                 str = substring(str, 0, tmp+2);
648                         return str;
649                         
650                 default:
651                         tmp = pl.(scores[field]);
652                         f = scores_flags[field];
653                         if(field == ps_primary)
654                                 sbar_field_rgb = '1 1 0';
655                         else if(field == ps_secondary)
656                                 sbar_field_rgb = '0 1 1';
657                         else
658                                 sbar_field_rgb = '1 1 1';
659                         return ScoreString(f, tmp);
660         }
661         //return "error";
662 }
663
664 // shamelessly stolen from menu QC :P <- as if I would steal YOUR code pfft ;)
665 float textLengthUpToWidth(string theText, float maxWidth, float handleColors)
666 {
667         // STOP.
668         // The following function is SLOW.
669         // For your safety and for the protection of those around you...
670         // DO NOT CALL THIS AT HOME.
671         // No really, don't.
672         if(stringwidth(theText, handleColors) <= maxWidth)
673                 return strlen(theText); // yeah!
674
675         // binary search for right place to cut string
676         float left, right, middle; // this always works
677         left = 0;
678         right = strlen(theText); // this always fails
679         do
680         {
681                 middle = floor((left + right) / 2);
682                 if(stringwidth(substring(theText, 0, middle), handleColors) <= maxWidth)
683                         left = middle;
684                 else
685                         right = middle;
686         }
687         while(left < right - 1);
688
689         // NOTE: when color codes are involved, this binary search is,
690         // mathematically, BROKEN. However, it is obviously guaranteed to
691         // terminate, as the range still halves each time - but nevertheless, it is
692         // guaranteed that it finds ONE valid cutoff place (where "left" is in
693         // range, and "right" is outside).
694
695         return left;
696 }
697 string textShortenToWidth(string theText, float maxWidth, float handleColors)
698 {
699         if(stringwidth(theText, handleColors) <= maxWidth)
700                 return theText;
701         else
702                 return strcat(substring(theText, 0, textLengthUpToWidth(theText, maxWidth - stringwidth("...", handleColors), handleColors)), "...");
703 }
704
705 float xmin, xmax, ymin, ymax, sbwidth;
706 float sbar_fixscoreboardcolumnwidth_len;
707 float sbar_fixscoreboardcolumnwidth_iconlen;
708 float sbar_fixscoreboardcolumnwidth_marginlen;
709
710 string Sbar_FixScoreboardColumnWidth(float i, string str)
711 {
712         float field, maxsize, j, f;
713         vector sz;
714         field = sbar_field[i];
715
716         if(field == SP_NAME) // name gets all remaining space
717         {
718                 maxsize = (xmax - xmin) / sbar_fontsize_x;
719                 for(j = 0; j < sbar_num_fields; ++j) if(j != i) if(sbar_field[j] != SP_SEPARATOR)
720                         maxsize -= sbar_size[j] + 1;
721                 maxsize += 1;
722                 str = textShortenToWidth(str, maxsize, TRUE);
723                 sbar_fixscoreboardcolumnwidth_len = stringwidth(str, TRUE);
724         }
725         else
726                 sbar_fixscoreboardcolumnwidth_len = stringwidth(str, FALSE);
727         
728         sbar_fixscoreboardcolumnwidth_iconlen = 0;
729
730         if(sbar_field_icon0 != "")
731         {
732                 sz = drawgetimagesize(sbar_field_icon0);
733                 f = sz_x / sz_y;
734                 if(sbar_fixscoreboardcolumnwidth_iconlen < f)
735                         sbar_fixscoreboardcolumnwidth_iconlen = f;
736         }
737
738         if(sbar_field_icon1 != "")
739         {
740                 sz = drawgetimagesize(sbar_field_icon1);
741                 f = sz_x / sz_y;
742                 if(sbar_fixscoreboardcolumnwidth_iconlen < f)
743                         sbar_fixscoreboardcolumnwidth_iconlen = f;
744         }
745
746         if(sbar_field_icon2 != "")
747         {
748                 sz = drawgetimagesize(sbar_field_icon2);
749                 f = sz_x / sz_y;
750                 if(sbar_fixscoreboardcolumnwidth_iconlen < f)
751                         sbar_fixscoreboardcolumnwidth_iconlen = f;
752         }
753
754         sbar_fixscoreboardcolumnwidth_iconlen *= sbar_fontsize_y / sbar_fontsize_x; // fix icon aspect
755
756         if(sbar_fixscoreboardcolumnwidth_iconlen != 0 && sbar_fixscoreboardcolumnwidth_len != 0)
757                 sbar_fixscoreboardcolumnwidth_marginlen = stringwidth(" ", FALSE);
758         else
759                 sbar_fixscoreboardcolumnwidth_marginlen = 0;
760
761         f = sbar_fixscoreboardcolumnwidth_len + sbar_fixscoreboardcolumnwidth_marginlen + sbar_fixscoreboardcolumnwidth_iconlen;
762         if(sbar_size[i] < f)
763                 sbar_size[i] = f;
764
765         return str;
766 }
767
768 void Sbar_PrintScoreboardItem(vector pos, entity pl, float is_self)
769 {
770         vector tmp;
771         string str;
772         float i, field;
773         float is_spec;
774         is_spec = (GetPlayerColor(pl.sv_entnum) == COLOR_SPECTATOR);
775
776         // Layout:
777         tmp_z = 0;
778         if(is_self)
779         {
780                 tmp_x = sbwidth;
781                 tmp_y = sbar_fontsize_y;
782                 drawfill(pos - '1 1 0', tmp + '2 2 0', '1 1 1', 0.3, DRAWFLAG_NORMAL);
783         }       
784         tmp_y = 0;
785         
786         for(i = 0; i < sbar_num_fields; ++i)
787         {
788                 field = sbar_field[i];
789                 if(field == SP_SEPARATOR)
790                         break;
791
792                 if(is_spec && field != SP_NAME && field != SP_PING) {
793                         pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
794                         continue;
795                 }
796                 str = Sbar_GetField(pl, field);
797                 str = Sbar_FixScoreboardColumnWidth(i, str);
798
799                 pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
800
801                 if(field == SP_NAME) {
802                         tmp_x = sbar_fontsize_x*(sbar_size[i] - sbar_fixscoreboardcolumnwidth_iconlen - sbar_fixscoreboardcolumnwidth_marginlen) + sbar_fontsize_x;
803                         drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
804                 } else {
805                         tmp_x = sbar_fixscoreboardcolumnwidth_len*sbar_fontsize_x + sbar_fontsize_x;
806                         drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
807                 }
808
809                 tmp_x = sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
810                 if(sbar_field_icon0 != "")
811                         drawpic(pos - tmp, sbar_field_icon0, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon1_rgb, sbar_field_icon0_alpha, 0);
812                 if(sbar_field_icon1 != "")
813                         drawpic(pos - tmp, sbar_field_icon1, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon1_rgb, sbar_field_icon1_alpha, 0);
814                 if(sbar_field_icon2 != "")
815                         drawpic(pos - tmp, sbar_field_icon2, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon2_rgb, sbar_field_icon2_alpha, 0);
816         }
817         
818         if(sbar_field[i] == SP_SEPARATOR)
819         {
820                 pos_x = xmax;
821                 for(i = sbar_num_fields-1; i > 0; --i)
822                 {
823                         field = sbar_field[i];
824                         if(field == SP_SEPARATOR)
825                                 break;
826                         
827                         if(is_spec && field != SP_NAME && field != SP_PING) {
828                                 pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
829                                 continue;
830                         }
831                         
832                         str = Sbar_GetField(pl, field);
833                         str = Sbar_FixScoreboardColumnWidth(i, str);
834
835                         if(field == SP_NAME) {
836                                 tmp_x = sbar_fontsize_x*sbar_fixscoreboardcolumnwidth_len; // left or right aligned? let's put it right...
837                                 drawcolorcodedstring(pos - tmp, str, sbar_fontsize, 1, DRAWFLAG_NORMAL);
838                         } else {
839                                 tmp_x = sbar_fontsize_x*sbar_fixscoreboardcolumnwidth_len; //strlen(str);
840                                 drawstring(pos - tmp, str, sbar_fontsize, sbar_field_rgb, 1, DRAWFLAG_NORMAL);
841                         }
842
843                         tmp_x = sbar_fontsize_x*sbar_size[i];
844                         if(sbar_field_icon0 != "")
845                                 drawpic(pos - tmp, sbar_field_icon0, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon1_rgb, sbar_field_icon0_alpha, 0);
846                         if(sbar_field_icon1 != "")
847                                 drawpic(pos - tmp, sbar_field_icon1, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon1_rgb, sbar_field_icon1_alpha, 0);
848                         if(sbar_field_icon2 != "")
849                                 drawpic(pos - tmp, sbar_field_icon2, '0 1 0' * sbar_fontsize_y + '1 0 0' * sbar_fontsize_x * sbar_fixscoreboardcolumnwidth_iconlen, sbar_field_icon2_rgb, sbar_field_icon2_alpha, 0);
850
851                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
852                 }
853         }
854 }
855
856 float lastpingstime;
857 void Sbar_DrawScoreboard()
858 {
859         //float xmin, ymin, xmax, ymax;
860         vector rgb, pos, tmp, sbar_save;
861         entity pl, tm;
862         float specs, i;
863         float center_x;
864
865         if(time > lastpingstime + 10)
866         {
867                 localcmd("pings\n");
868                 lastpingstime = time;
869         }
870
871         sbwidth = Sbar_GetWidth(6.5 * sbar_fontsize_y);
872
873         xmin = 0.5 * (vid_conwidth - sbwidth);
874         ymin = 20;
875
876         xmax = vid_conwidth - xmin;
877     ymax = vid_conheight - 0.2*vid_conheight;
878
879         center_x = xmin + 0.5*sbwidth;
880
881         //Sbar_UpdateFields();
882
883         // Initializes position
884         //pos_x = xmin;
885         pos_y = ymin;
886         pos_z = 0;
887
888         // Heading
889         drawfont = sbar_bigfont;
890         pos_x = center_x - stringwidth("Scoreboard", TRUE)*0.5*24;
891         drawstring(pos, "Scoreboard", '24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
892         pos_x = xmin;
893         pos_y += 24 + 4;
894
895         // Titlebar background:
896         tmp_x = sbwidth;
897         tmp_y = sbar_fontsize_y;
898         drawfill(pos - '1 1 0', tmp + '2 2 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
899         
900         drawfont = sbar_font;
901
902         for(i = 0; i < sbar_num_fields; ++i)
903         {
904                 if(sbar_field[i] == SP_SEPARATOR)
905                         break;
906                 drawstring(pos, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
907                 pos_x += sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
908         }
909         
910         if(sbar_field[i] == SP_SEPARATOR)
911         {
912                 pos_x = xmax + sbar_fontsize_x;
913                 tmp_y = tmp_z = 0;
914                 for(i = sbar_num_fields-1; i > 0; --i)
915                 {
916                         if(sbar_field[i] == SP_SEPARATOR)
917                                 break;
918
919                         pos_x -= sbar_fontsize_x*sbar_size[i] + sbar_fontsize_x;
920                         /**
921                          * FTEQCC BUG!
922                          * Using the following line will mess it all up:
923                          **
924                          * tmp_x = sbar_size[i] - strlen(sbar_title[i])*8;
925                          */
926                         tmp_x = sbar_fontsize_x*sbar_size[i];
927                         tmp_x -= stringwidth(sbar_title[i], FALSE)*sbar_fontsize_x;
928                         drawstring(pos + tmp, sbar_title[i], sbar_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
929                 }
930         }
931                 
932         pos_x = xmin;
933         pos_y += 1.5 * sbar_fontsize_y;
934
935         sbar_save = sbar;
936         sbar = '0 0 0';
937         
938         if(teamplay)
939         {
940                 //for(tm = sortedTeams.sort_next; tm; tm = tm.sort_next)
941                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
942                 {
943                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
944                                 continue;
945
946                         rgb = GetTeamRGB(tm.team);
947
948                         pos_x = xmin;
949
950                         Sbar_DrawXNum(
951                                 pos - '6.5 0 0' * sbar_fontsize_y,
952                                 tm.(teamscores[ts_primary]),
953                                 4, sbar_fontsize_y * 1.5, rgb, 1, DRAWFLAG_NORMAL);
954
955                         if(ts_primary != ts_secondary)
956                         Sbar_DrawXNum(
957                                 pos - '4.5 0 0' * sbar_fontsize_y + '0 1.5 0' * sbar_fontsize_y,
958                                 tm.(teamscores[ts_secondary]),
959                                 4, sbar_fontsize_y * 1, rgb, 1, DRAWFLAG_NORMAL);
960                         
961                         specs = tm.team_size;
962
963                         if(specs < 2)
964                                 specs = 2;
965                         
966                         tmp_x = sbwidth;
967                         tmp_y = 1.25 * sbar_fontsize_y * specs;
968                         drawfill(pos - '1 1 0', tmp + '2 0 0', rgb, 0.2, DRAWFLAG_NORMAL);
969                         
970                         for(pl = players.sort_next; pl; pl = pl.sort_next)
971                         {
972                                 if(pl.team != tm.team)
973                                         continue;
974                                 Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
975                                 pos_y += 1.25 * sbar_fontsize_y;
976                                 tmp_y -= 1.25 * sbar_fontsize_y;
977                         }
978                         pos_y += tmp_y + 1.5 * sbar_fontsize_y;
979                 }
980                 // rgb := tempvector :)
981                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
982                 pos_y += 3 * sbar_fontsize_y;
983                 specs = 0;
984                 for(pl = players.sort_next; pl; pl = pl.sort_next)
985                 {
986                         if(pl.team != COLOR_SPECTATOR)
987                                 continue;
988                         Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
989                         pos += '0 1.25 0' * sbar_fontsize_y;
990                         ++specs;
991                 }
992                         
993                 if(specs)
994                         drawstring(rgb, "Spectators", sbar_fontsize, '1 1 1', 1, 0);
995         } else {
996                 pos_x = xmin;
997                 for(pl = players.sort_next; pl; pl = pl.sort_next)
998                 {
999                         if(pl.team == COLOR_SPECTATOR)
1000                                 continue;
1001                         Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
1002                         pos_y += 1.25 * sbar_fontsize_y;
1003                         tmp_y -= 1.25 * sbar_fontsize_y;
1004                 }
1005
1006                 // rgb := tempvector :)
1007                 rgb = pos + '0 1.5 0' * sbar_fontsize_y;
1008                 pos_y += 3 * sbar_fontsize_y;
1009                 specs = 0;
1010                 for(pl = players.sort_next; pl; pl = pl.sort_next)
1011                 {
1012                         if(pl.team != COLOR_SPECTATOR)
1013                                 continue;
1014                         Sbar_PrintScoreboardItem(pos, pl, (pl.sv_entnum == player_localentnum - 1));
1015                         pos += '0 1.25 0' * sbar_fontsize_y;
1016                         ++specs;
1017                 }
1018                         
1019                 if(specs)
1020                         drawstring(rgb, "Spectators", sbar_fontsize, '1 1 1', 1, 0);
1021         }
1022
1023         string str;
1024         float tl, fl;
1025         str = strcat("playing on ^2", shortmapname, "^7");
1026         tl = getstatf(STAT_TIMELIMIT);
1027         fl = getstatf(STAT_FRAGLIMIT);
1028         if(gametype == GAME_LMS)
1029         {
1030                 if(tl > 0)
1031                         str = strcat(str, " for up to ^1", ftos(tl), " minutes^7");
1032         }
1033         else
1034         {
1035                 if(tl > 0)
1036                         str = strcat(str, " for ^1", ftos(tl), " minutes^7");
1037                 if(fl > 0)
1038                 {
1039                         if(tl > 0)
1040                                 str = strcat(str, " or");
1041                         if(teamplay)
1042                         {
1043                                 str = strcat(str, " until ^3", ScoreString(teamscores_flags[ts_primary], fl));
1044                                 if(teamscores_label[ts_primary] == "score")
1045                                         str = strcat(str, " points^7");
1046                                 else if(teamscores_label[ts_primary] == "fastest")
1047                                         str = strcat(str, " is beaten^7");
1048                                 else
1049                                         str = strcat(str, " ", teamscores_label[ts_primary]);
1050                         }
1051                         else
1052                         {
1053                                 str = strcat(str, " until ^3", ScoreString(scores_flags[ps_primary], fl));
1054                                 if(scores_label[ps_primary] == "score")
1055                                         str = strcat(str, " points^7");
1056                                 else if(scores_label[ps_primary] == "fastest")
1057                                         str = strcat(str, " is beaten^7");
1058                                 else
1059                                         str = strcat(str, " ", scores_label[ps_primary]);
1060                         }
1061                 }
1062         }
1063         
1064
1065         pos_y += 1.5 * sbar_fontsize_y;
1066         drawcolorcodedstring(pos + '0.5 0 0' * (sbwidth - sbar_fontsize_x * stringwidth(str, TRUE)), str, sbar_fontsize, 0.8, 0);
1067         
1068         sbar = sbar_save;
1069 }
1070
1071 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
1072 {
1073         string col;
1074         string timestr;
1075         string cpname;
1076         string lapstr;
1077         lapstr = "";
1078
1079         if(histime == 0) // goal hit
1080         {
1081                 if(mytime > 0)
1082                 {
1083                         timestr = strcat("+", ftos_decimals(+mytime, 1));
1084                         col = "^1";
1085                 }
1086                 else if(mytime == 0)
1087                 {
1088                         timestr = "+0.0";
1089                         col = "^3";
1090                 }
1091                 else
1092                 {
1093                         timestr = strcat("-", ftos_decimals(-mytime, 1));
1094                         col = "^2";
1095                 }
1096
1097                 if(lapdelta > 0)
1098                 {
1099                         lapstr = strcat(" (-", ftos(lapdelta), "L)");
1100                         col = "^2";
1101                 }
1102                 else if(lapdelta < 0)
1103                 {
1104                         lapstr = strcat(" (+", ftos(-lapdelta), "L)");
1105                         col = "^1";
1106                 }
1107         }
1108         else if(histime > 0) // anticipation
1109         {
1110                 if(mytime >= histime)
1111                         timestr = strcat("+", ftos_decimals(mytime - histime, 1));
1112                 else
1113                         timestr = mmsss(histime * 10);
1114                 col = "^3";
1115         }
1116         else
1117                 col = "^7";
1118
1119         if(cp)
1120                 cpname = strcat("Intermediate ", ftos(cp));
1121         else
1122                 cpname = "Finish line";
1123         
1124         if(histime < 0)
1125                 return strcat(col, cpname);
1126         else if(hisname == "")
1127                 return strcat(col, cpname, " (", timestr, ")");
1128         else
1129                 return strcat(col, cpname, " (", timestr, " ", strcat(hisname, col, lapstr), ")");
1130 }
1131
1132 void dummyfunction(float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
1133 {
1134 }
1135
1136 void Sbar_Score(float margin)
1137 {
1138         float timelimit, minutes, seconds, timeleft, minutesLeft, secondsLeft, distribution, myplace, score;
1139         vector sbar_save, place, timer_color;
1140         entity tm, pl, me;
1141         sbar_save = sbar;
1142         
1143         myteam = GetPlayerColor(player_localentnum - 1);
1144
1145         sbar_y = vid_conheight - (32+12);
1146         sbar_x -= margin;
1147         
1148         place = '-48 -12 0';
1149         if(teamplay)
1150         {
1151                 // Layout:
1152                 //
1153                 //   team1 team3 team4
1154                 //
1155                 //         TEAM2
1156                 //for(i = 0; i < 4; ++i)
1157                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
1158                 {
1159                         if(tm.team == COLOR_SPECTATOR || !tm.team_size) // no players? don't display
1160                                 continue;
1161                         // -32*4 = -128
1162                         score = tm.(teamscores[ts_primary]);
1163                         if(tm.team == myteam)
1164                                 Sbar_DrawXNum('-128 0 0', score, 4, 32, GetTeamRGB(tm.team), 1, DRAWFLAG_NORMAL);
1165                         else
1166                         {
1167                                 Sbar_DrawXNum(place, score, 4, 12, GetTeamRGB(tm.team), 1, DRAWFLAG_NORMAL);
1168                                 place_x -= 4*12;
1169                         }
1170                 }
1171         } else {
1172                 // me vector := [team/connected frags id]
1173                 myplace = 0;
1174                 for(me = players.sort_next; me; me = me.sort_next)
1175                 {
1176                         if(me.team != COLOR_SPECTATOR)
1177                                 ++myplace;
1178                         if(me.sv_entnum == player_localentnum - 1)
1179                                 break;
1180                 }
1181                 pl = players;
1182                 if(pl == me)
1183                         pl = pl.sort_next;
1184                 
1185                 if(pl) {
1186                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
1187                 } else
1188                         distribution = 0;
1189                 
1190                 if(myplace == 1)
1191                         Sbar_DrawXNum('-36 -12 0', myplace, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
1192                 else if(myplace == 2)
1193                         Sbar_DrawXNum('-36 -12 0', myplace, 3, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
1194                 else
1195                         Sbar_DrawXNum('-36 -12 0', myplace, 3, 12, '1 0 0', 1, DRAWFLAG_NORMAL);
1196
1197                 score = me.(scores[ps_primary]);
1198                 if(distribution >= 0)
1199                 {
1200                         Sbar_DrawXNum('-84 -12 0', distribution, 4, 12, ' 1 1 1', 1, DRAWFLAG_NORMAL);
1201                         Sbar_DrawXNum('-128 0 0', score, 4, 32, '1 1 1', 1, DRAWFLAG_NORMAL);
1202                 } else if(distribution >= -5)
1203                 {
1204                         Sbar_DrawXNum('-84 -12 0', distribution, 4, 12, ' 1 1 0', 1, DRAWFLAG_NORMAL);
1205                         Sbar_DrawXNum('-128 0 0', score, 4, 32, '1 1 0', 1, DRAWFLAG_NORMAL);
1206                 } else {
1207                         Sbar_DrawXNum('-84 -12 0', distribution, 4, 12, ' 1 0 0', 1, DRAWFLAG_NORMAL);
1208                         Sbar_DrawXNum('-128 0 0', score, 4, 32, '1 0 0', 1, DRAWFLAG_NORMAL);
1209                 }
1210         }
1211         
1212         //draw the remaining or elapsed time
1213         timelimit = getstatf(STAT_TIMELIMIT);
1214         if(timelimit > 0)
1215         {
1216                 timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
1217                 timeleft = ceil(timeleft);
1218                 minutesLeft = floor(timeleft / 60);
1219                 secondsLeft = timeleft - minutesLeft*60;
1220                 
1221                 if(minutesLeft >= 5) {
1222                         timer_color = '1 1 1'; //white
1223                 } else if(minutesLeft >= 1) {
1224                         timer_color = '1 1 0'; //yellow
1225                 } else {
1226                         timer_color = '1 0 0'; //red
1227                 }
1228                 
1229                 if (warmup_stage) {
1230                         timer_color = '1 1 1'; //don't use red or yellow for timer during warmup
1231                 }
1232                 
1233                 if (cvar("sbar_increment_maptime")) {
1234                         if (time < getstatf(STAT_GAMESTARTTIME)) {
1235                                 //while restart is still active, show negative counter
1236                                 minutes = 0;
1237                                 seconds = ceil(getstatf(STAT_GAMESTARTTIME) - time);
1238                         } else {
1239                                 float elapsedTime;
1240                                 elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
1241                                 minutes = floor(elapsedTime / 60);
1242                                 seconds = elapsedTime - minutes*60;
1243                         }
1244                 } else {
1245                         minutes = minutesLeft;
1246                         seconds = secondsLeft;
1247                 }
1248                 
1249                 if(minutesLeft >= 1 || (cvar("sbar_increment_maptime") && minutes >= 1) ) {
1250                         Sbar_DrawXNum('-72 32 0', minutes, 3, 12, timer_color, 1, DRAWFLAG_NORMAL);
1251                         drawpic(sbar + '-36 32 0', "gfx/num_colon", '12 12 0', timer_color, sbar_alpha_fg, 0);
1252                 }
1253                 Sbar_DrawXNum('-24 32 0', seconds, -2, 12, timer_color, 1, DRAWFLAG_NORMAL);
1254         } else {
1255                 timer_color = '1 1 1'; //white
1256                 minutes = floor(time / 60);
1257                 seconds = floor(time - minutes*60);
1258                 Sbar_DrawXNum('-72 32 0', minutes, 3, 12, timer_color, 1, DRAWFLAG_NORMAL);
1259                 drawpic(sbar + '-36 32 0', "gfx/num_colon", '12 12 0', timer_color, sbar_alpha_fg, 0);
1260                 Sbar_DrawXNum('-24 32 0', seconds, -2, 12, timer_color, 1, DRAWFLAG_NORMAL);
1261         }
1262
1263         if(gametype == GAME_RACE)
1264         {
1265                 drawfont = sbar_bigfont;
1266                 float a;
1267                 vector m;
1268                 string s, forcetime;
1269
1270                 m = '0.5 0 0' * vid_conwidth + '0 0.25 0' * vid_conheight;
1271
1272                 if(race_checkpointtime)
1273                 {
1274
1275                         a = bound(0, 2 - (time - race_checkpointtime), 1);
1276                         s = "";
1277                         forcetime = "";
1278                         if(a > 0) // just hit a checkpoint?
1279                         {
1280                                 if(race_time && race_previousbesttime)
1281                                         s = MakeRaceString(race_checkpoint, race_time / 10 - race_previousbesttime / 10, 0, 0, race_previousbestname);
1282                                 else
1283                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
1284                                 if(race_time)
1285                                         forcetime = mmsss(race_time);
1286                         }
1287                         else
1288                         {
1289                                 if(race_laptime && race_nextbesttime)
1290                                 {
1291                                         a = bound(0, 2 - ((race_laptime + race_nextbesttime/10) - time), 1);
1292                                         if(a > 0) // next one?
1293                                         {
1294                                                 s = MakeRaceString(race_nextcheckpoint, time - race_laptime, race_nextbesttime / 10, 0, race_nextbestname);
1295                                         }
1296                                 }
1297                         }
1298
1299                         if(s != "" && a > 0)
1300                         {
1301                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
1302                                 drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', sbar_alpha_fg * a, 0);
1303                         }
1304
1305                         if(forcetime != "")
1306                         {
1307                                 float sz;
1308                                 a = bound(0, 1 - 2 * (time - race_checkpointtime), 1);
1309                                 sz = 1.2 / (a + 0.2);
1310                                 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);
1311                                 a = 1 - a;
1312                         }
1313                         else
1314                                 a = 1;
1315
1316                         if(race_laptime)
1317                         {
1318                                 s = mmsss(10*(time - race_laptime));
1319                                 drawstring(m - '0 0 0' - '16 0 0' * stringwidth(s, FALSE), s, '32 32 0', '1 1 1', sbar_alpha_fg * a, 0);
1320                         }
1321                 }
1322                 else
1323                 {
1324                         if(race_mycheckpointtime)
1325                         {
1326                                 a = bound(0, 2 - (time - race_mycheckpointtime), 1);
1327                                 s = MakeRaceString(race_mycheckpoint, race_mycheckpointdelta / 10, -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
1328                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
1329                                 drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', sbar_alpha_fg * a, 0);
1330                         }
1331                         if(race_othercheckpointtime && race_othercheckpointenemy != "")
1332                         {
1333                                 a = bound(0, 2 - (time - race_othercheckpointtime), 1);
1334                                 s = MakeRaceString(race_othercheckpoint, -race_othercheckpointdelta / 10, -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
1335                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
1336                                 drawcolorcodedstring(m - '0 0 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', sbar_alpha_fg * a, 0);
1337                         }
1338                 }
1339
1340                 drawfont = sbar_font;
1341         }
1342
1343         sbar = sbar_save;
1344 }
1345
1346 void Sbar_MiniscoreItem(vector pos, entity pl, float is_self)
1347 {
1348         float x, score;
1349         pos_x += 72;
1350         
1351         if(teamplay)
1352                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(pl.team)*0.5, 1, DRAWFLAG_NORMAL);
1353         else
1354                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
1355         x = pos_x;
1356         pos_x += 5*8;
1357         score = pl.(scores[ps_primary]);
1358         pos_x -= stringwidth(ftos(score), FALSE)*8;
1359         drawstring(pos, ftos(score), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1360         pos_x = x;
1361         if(is_self)
1362         {
1363                 pos_x += 48;
1364                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1365                 pos_x += 8;
1366         } else
1367                 pos_x += 56;
1368         drawcolorcodedstring(pos, GetPlayerName(pl.sv_entnum), '8 8 0', 1, 0);
1369 }
1370
1371 void Sbar_MiniscoreTeamItem(vector pos, float color, float frags, float is_self)
1372 {
1373         float x;
1374         pos_x += 72;
1375         
1376         if(teamplay)
1377                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(color)*0.5, 1, DRAWFLAG_NORMAL);
1378         else
1379                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
1380         x = pos_x;
1381         pos_x += 5*8;
1382         pos_x -= stringwidth(ftos(frags), FALSE)*8;
1383         drawstring(pos, ftos(frags), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1384         pos_x = x;
1385         if(is_self)
1386         {
1387                 pos_x += 48;
1388                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1389                 pos_x += 8;
1390         } else
1391                 pos_x += 56;
1392         drawstring(pos, GetTeamName(color), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1393 }
1394
1395 void Sbar_MiniDeathmatchOverlay(vector pos)
1396 {
1397         float numlines, up, down, score;
1398         entity me, tm, pl;
1399         float miniscoreboard_size;
1400         miniscoreboard_size = cvar("sbar_miniscoreboard_size");
1401         
1402         if(miniscoreboard_size == 0)
1403                 return;
1404         pos_y = vid_conheight - 8;
1405         
1406         if(miniscoreboard_size < 0)
1407                 numlines = (vid_conheight - sbar_y + 7) / 8;
1408         else
1409                 numlines = miniscoreboard_size;
1410
1411         // give up if there isn't enough room
1412         if(pos_x >= vid_conwidth || pos_y >= vid_conheight || numlines < 1)
1413                 return;
1414
1415         // me vector := [team/connected frags id]
1416         for(me = players.sort_next; me; me = me.sort_next)
1417         {
1418                 if(me.sv_entnum == player_localentnum - 1)
1419                         break;
1420         }
1421
1422         if(teamplay)
1423                 numlines -= numteams;
1424
1425         // figure out how many players above and below we can show
1426         up = floor(numlines/2);
1427         down = up;
1428         if((up + down) > numlines)
1429                 down = numlines - up;
1430
1431         // render bottom-up
1432         for(pl = me.sort_next; pl && down > 0; pl = pl.sort_next)
1433         {
1434                 if(pl.team == COLOR_SPECTATOR)
1435                         continue;
1436                 Sbar_MiniscoreItem(pos, pl, false);
1437                 pos_y -= 9;
1438                 --down;
1439         }
1440         Sbar_MiniscoreItem(pos, me, true);
1441         pos_y -= 9;
1442         up += down; // if there weren't enough lines below... add them
1443         for(pl = me.sort_prev; pl && up > 0; pl = pl.sort_prev)
1444         {
1445                 if(pl.team == COLOR_SPECTATOR)
1446                         continue;
1447                 Sbar_MiniscoreItem(pos, pl, false);
1448                 pos_y -= 9;
1449                 --up;
1450         }
1451
1452         if(teamplay)
1453         {
1454                 for(tm = teams.sort_next; tm.sort_next; tm = tm.sort_next);
1455                 for(; tm; tm = tm.sort_prev)
1456                 {
1457                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
1458                                 continue;
1459                         score = tm.(teamscores[ts_primary]);
1460                         Sbar_MiniscoreTeamItem(pos, tm.team, score, (tm.team == me.team));
1461                         pos_y -= 9;
1462                 }
1463         }
1464 }
1465
1466 float Sbar_WouldDrawScoreboard ()
1467 {
1468         if (sb_showscores)
1469                 return 1;
1470         else if (intermission == 1)
1471                 return 1;
1472         else if (intermission == 2)
1473                 return 1;
1474         else if (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard"))
1475                 return 1;
1476         else if(sb_showscores_force)
1477                 return 1;
1478         return 0;
1479 }
1480
1481 vector Sbar_DrawNoteLine(vector offset, string s)
1482 {
1483         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
1484         drawcolorcodedstring(
1485                 offset - sbar_fontsize_x * '1 0 0' * stringwidth(s, TRUE),
1486                 s,
1487                 sbar_fontsize,
1488                 sbar_alpha_fg,
1489                 0
1490         );
1491         return offset + sbar_fontsize_y * '0 1 0';
1492 }
1493
1494 void CSQC_Strength_Timer() {
1495         float stat_items;
1496         stat_items = getstati(STAT_ITEMS);
1497         if not(stat_items & IT_STRENGTH)
1498                 if not(stat_items & IT_INVINCIBLE)
1499                         return;
1500         
1501         if (getstati(STAT_HEALTH) <= 0)
1502                 return;
1503         
1504         vector pos, picsize, number_position;
1505         float strength_time, invincibility_time, countdown_fontsize;
1506
1507         picsize = '40 40 0';
1508         countdown_fontsize = 22;
1509         
1510         //element will be positioned on the right side of the screen:
1511         pos_x = vid_conwidth - picsize_x - 10; //margin 10 from right border
1512         number_position_x = pos_x - 5/*margin to the left from image*/ - countdown_fontsize*2/*width of text*/;
1513         
1514         pos_y = 180;
1515         number_position_y = pos_y;
1516         
1517         //now calculate the values Sbar_DrawXNum() will be called with:
1518         number_position_x = number_position_x - sbar_x; //relative to sbar coordinates
1519         number_position_y = number_position_y - sbar_y; //relative to sbar coordinates
1520         number_position_y += 10; //center number vertically to the icon
1521         
1522         pos_z = number_position_z = 0;
1523         
1524         //strength
1525         if (stat_items & IT_STRENGTH) {
1526                 drawpic(pos, "gfx/sb_str", picsize, '0 0.086 0.953'/*strength color*/, 1, DRAWFLAG_NORMAL);
1527                 strength_time = getstatf(STAT_STRENGTH_FINISHED) - time;
1528                 if (strength_time > 0) { //show countdown once we received the valid stats-value
1529                         Sbar_DrawXNum(number_position, ceil(strength_time), 2, countdown_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
1530                 }
1531                 
1532                 //add some margin to the invincibility icon
1533                 pos_y             += picsize_y + 10;
1534                 number_position_y += picsize_y + 10;
1535         }
1536         
1537         //invincibility
1538         if (stat_items & IT_INVINCIBLE) {
1539                 drawpic(pos, "gfx/sb_invinc", picsize, '0.976 0 0.165'/*invincibility color*/, 1, DRAWFLAG_NORMAL);
1540                 invincibility_time = getstatf(STAT_INVINCIBLE_FINISHED) - time;
1541                 if (invincibility_time > 0) { //show countdown once we received the valid stats-value
1542                         Sbar_DrawXNum(number_position, ceil(invincibility_time), 2, countdown_fontsize, '1 1 1', 1, DRAWFLAG_NORMAL);
1543                 }
1544         }
1545 }
1546
1547 void Sbar_Draw (void)
1548 {
1549         float i;
1550         float x, fade;
1551         float stat_items, stat_weapons;
1552         vector o; o = '1 0 0' * vid_conwidth;
1553         string s;
1554
1555         sbar_fontsize = Sbar_GetFontsize();
1556         
1557         if(spectatee_status)
1558         {
1559                 if(spectatee_status == -1)
1560                         s = "^1Observing";
1561                 else
1562                         s = strcat("^1Spectating ^7", GetPlayerName(spectatee_status - 1));
1563                 o = Sbar_DrawNoteLine(o, s);
1564
1565                 if(spectatee_status == -1)
1566                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 to spectate");
1567                 else
1568                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 for another player");
1569                 o = Sbar_DrawNoteLine(o, s);
1570
1571                 if(spectatee_status == -1)
1572                         s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
1573                 else
1574                         s = strcat("^1Press ^3", getcommandkey("secondary fire", "+attack2"), "^1 to observe");
1575                 o = Sbar_DrawNoteLine(o, s);
1576
1577                 if(gametype == GAME_ARENA)
1578                         s = "^1Wait for your turn to join";
1579                 else if(gametype == GAME_LMS)
1580                 {
1581                         entity sk;
1582                         sk = playerslots[player_localentnum - 1];
1583                         if(sk.(scores[ps_primary]) >= 666)
1584                                 s = "^1Match has already begun";
1585                         else if(sk.(scores[ps_primary]) > 0)
1586                                 s = "^1You have no more lives left";
1587                         else
1588                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1589                 }
1590                 else
1591                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1592                 o = Sbar_DrawNoteLine(o, s);
1593         }
1594         if(warmup_stage)
1595         {
1596                 s = "^2Currently in ^1warmup^2 stage!";
1597                 o = Sbar_DrawNoteLine(o, s);
1598         }
1599
1600         // move more important stuff more to the middle so its more visible
1601         o_y = vid_conheight * 0.66;
1602
1603         string blinkcolor;
1604         if(mod(time, 1) >= 0.5)
1605                 blinkcolor = "^1";
1606         else
1607                 blinkcolor = "^3";
1608
1609         if(ready_waiting)
1610         {
1611                 if(ready_waiting_for_me)
1612                 {
1613                         if(warmup_stage) 
1614                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
1615                         else
1616                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
1617                 }
1618                 else
1619                 {
1620                         if(warmup_stage) 
1621                                 s = strcat("^2Waiting for others to ready up to end warmup...");
1622                         else
1623                                 s = strcat("^2Waiting for others to ready up...");
1624                 }
1625                 o = Sbar_DrawNoteLine(o, s);
1626         }
1627         else if(warmup_stage) 
1628         {
1629                 s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
1630                 o = Sbar_DrawNoteLine(o, s);
1631         }
1632         if(vote_waiting)
1633         {
1634                 s = strcat("^2A vote has been called for ^1", vote_called_vote);
1635                 o = Sbar_DrawNoteLine(o, s);
1636
1637                 if(vote_waiting_for_me)
1638                 {
1639                         s = strcat(blinkcolor, "Press ^3", getcommandkey("vote yes", "vyes"), blinkcolor, " to accept");
1640                         o = Sbar_DrawNoteLine(o, s);
1641
1642                         s = strcat(blinkcolor, "Press ^3", getcommandkey("vote no", "vno"), blinkcolor, " to reject");
1643                         o = Sbar_DrawNoteLine(o, s);
1644
1645                         s = strcat(blinkcolor, "Press ^3", getcommandkey("vote abstain", "vabstain"), blinkcolor, " to abstain");
1646                         o = Sbar_DrawNoteLine(o, s);
1647                 }
1648                 else
1649                 {
1650                         s = strcat("^2Waiting for others to vote...");
1651                         o = Sbar_DrawNoteLine(o, s);
1652                 }
1653         }
1654         if(teamplay)
1655         {
1656                 entity tm;
1657                 float ts_min, ts_max;
1658                 tm = teams.sort_next;
1659                 if (tm)
1660                 {
1661                         for(; tm.sort_next; tm = tm.sort_next)
1662                         {
1663                                 if(!tm.team_size || tm.team == COLOR_SPECTATOR)
1664                                         continue;
1665                                 if(!ts_min) ts_min = tm.team_size;
1666                                 else ts_min = min(ts_min, tm.team_size);
1667                                 if(!ts_max) ts_max = tm.team_size;
1668                                 else ts_max = max(ts_max, tm.team_size);
1669                         }
1670                         if ((ts_max - ts_min) > 1)
1671                         {
1672                                 s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
1673                                 tm = GetTeam(myteam, false);
1674                                 if (tm)
1675                                 if (tm.team != COLOR_SPECTATOR)
1676                                 if (tm.team_size == ts_max)
1677                                         s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
1678
1679                                 o = Sbar_DrawNoteLine(o, s);
1680                         }
1681                 }
1682         }
1683
1684         //Sbar_SortFrags();
1685         Sbar_UpdatePlayerTeams();
1686
1687         sb_lines = 24;
1688
1689         if (sb_showscores)
1690                 Sbar_DrawScoreboard();
1691         else if (intermission == 1)
1692         {
1693                 Sbar_DrawScoreboard();
1694                 return;
1695         }
1696         else if (intermission == 2)
1697                 Sbar_FinaleOverlay();
1698         else
1699         {
1700                 if (sb_showscores || sb_showscores_force || (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard")))
1701                 {
1702                         sbar_x = (vid_conwidth - 640.0)*0.5;
1703                         sbar_y = vid_conheight - 47;
1704                         //Sbar_DrawAlphaPic (sbar_x, sbar_y, sb_scorebar, sbar_alpha_bg.value);
1705                         //drawpic('0 0 0', "gfx/scorebar", '0 0 0', '1 1 1', cvar("sbar_alpha_bg"), 0);
1706                         Sbar_DrawScoreboard ();
1707                 }
1708                 else
1709                 {
1710                         if (sb_lines && sbar_hudselector == 1)
1711                         {
1712                                 stat_items = getstati(STAT_ITEMS);
1713                                 stat_weapons = getstati(STAT_WEAPONS);
1714
1715                                 sbar_x = (vid_conwidth - 320.0)*0.5;
1716                                 sbar_y = vid_conheight - 24.0 - 16.0;
1717                                 sbar_z = 0;
1718                         
1719                                 fade = 3.2 - 2 * (time - weapontime);
1720                                 fade = bound(0.7, fade, 1);
1721
1722                                 x = 1.0;
1723                                 Sbar_DrawWeapon_Clear();
1724                                 for(i = 1; i <= 24; ++i)
1725                                 {
1726                                         if(weaponimpulse[i-1] >= 0)
1727                                         if(stat_weapons & x)
1728                                         {
1729                                                 Sbar_DrawWeapon(i-1, fade, (i == activeweapon));
1730                                         }
1731                                         x *= 2;
1732                                 }
1733
1734                                 // armor
1735                                 x = getstati(STAT_ARMOR);
1736                                 if (x > 0)
1737                                 {
1738                                         // "gfx/sb_armor"
1739                                         //Sbar_DrawStretchPic (72, 0, sb_armor[0], sbar_alpha_fg.value, 24, 24);
1740                                         drawpic(sbar + '72 0 0', "gfx/sb_armor", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1741                                         if(x > 200)
1742                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '0 1 0', 1, 0);
1743                                         else if(x > 100)
1744                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '0.2 1 0', 1, 0);
1745                                         else if(x > 50)
1746                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1747                                         else if(x > 25)
1748                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '1 1 0.2', 1, 0);
1749                                         else
1750                                                 Sbar_DrawXNum('0 0 0', x, 3, 24, '0.7 0 0', 1, 0);
1751                                 }
1752
1753                                 // health
1754                                 x = getstati(STAT_HEALTH);
1755                                 if (x != 0)
1756                                 {
1757                                         // "gfx/sb_health"
1758                                         //Sbar_DrawStretchPic (184, 0, sb_health, sbar_alpha_fg.value, 24, 24);
1759                                         drawpic(sbar + '184 0 0', "gfx/sb_health", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1760                                         if(x > 200)
1761                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '0 1 0', 1, 0);
1762                                         else if(x > 100)
1763                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '0.2 1 0', 1, 0);
1764                                         else if(x > 50)
1765                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1766                                         else if(x > 25)
1767                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '1 1 0.2', 1, 0);
1768                                         else
1769                                                 Sbar_DrawXNum('112 0 0', x, 3, 24, '0.7 0 0', 1, 0);
1770                                 }
1771
1772                                 // ammo
1773                                 x = getstati(STAT_AMMO);
1774                                 if ((stat_items & IT_AMMO) || x != 0)
1775                                 {
1776                                         if (stat_items & IT_SHELLS)
1777                                                 drawpic(sbar + '296 0 0', "gfx/sb_shells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1778                                         else if (stat_items & IT_NAILS)
1779                                                 drawpic(sbar + '296 0 0', "gfx/sb_bullets", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1780                                         else if (stat_items & IT_ROCKETS)
1781                                                 drawpic(sbar + '296 0 0', "gfx/sb_rocket", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1782                                         else if (stat_items & IT_CELLS)
1783                                                 drawpic(sbar + '296 0 0', "gfx/sb_cells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
1784                                         if(x > 10)
1785                                                 Sbar_DrawXNum('224 0 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1786                                         else
1787                                                 Sbar_DrawXNum('224 0 0', x, 3, 24, '0.7 0 0', 1, 0);
1788                                 }
1789
1790                                 if (sbar_x + 320 + 160 <= vid_conwidth)
1791                                         Sbar_MiniDeathmatchOverlay(sbar + '320 0 0');
1792                                 if (sbar_x > 0)
1793                                         Sbar_Score(16);
1794                                 // The margin can be at most 8 to support 640x480 console size:
1795                                 //   320 + 2 * (144 + 16) = 640
1796                         }
1797                         else if (sb_lines)
1798                         {
1799                         
1800                                 stat_items = getstati(STAT_ITEMS);
1801                                 stat_weapons = getstati(STAT_WEAPONS);
1802                         
1803                                 sbar_x = (vid_conwidth - 640.0)*0.5;
1804                                 sbar_y = vid_conheight - 47;
1805                                 sbar_z = 0;
1806
1807                                 fade = 3 - 2 * (time - weapontime);
1808
1809                                 x = 1.0;
1810                                 Sbar_DrawWeapon_Clear();
1811                                 for(i = 1; i <= 24; ++i)
1812                                 {
1813                                         if(weaponimpulse[i-1] >= 0)
1814                                         if(stat_weapons & x)
1815                                         {
1816                                                 Sbar_DrawWeapon(i-1, fade, (i == activeweapon));
1817                                         }
1818                                         x *= 2;
1819                                 }
1820
1821                                 if (sb_lines > 24)
1822                                         drawpic(sbar, "gfx/sbar", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1823                                 else
1824                                         drawpic(sbar, "gfx/sbar_minimal", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1825
1826                                 // armor
1827                                 // (340-3*24) = 268
1828                                 Sbar_DrawXNum('268 12 0', getstati(STAT_ARMOR), 3, 24, '0.6 0.7 0.8', 1, 0);
1829
1830                                 // health
1831                                 // (154-3*24) = 82
1832                                 x = getstati(STAT_HEALTH);
1833                                 if(x > 100)
1834                                         Sbar_DrawXNum('82 12 0', x, 3, 24, '1 1 1', 1, 0);
1835                                 else if(x <= 25 && time - floor(time) > 0.5)
1836                                         Sbar_DrawXNum('82 12 0', x, 3, 24, '0.7 0 0', 1, 0);
1837                                 else
1838                                         Sbar_DrawXNum('81 12 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1839
1840                                 x = getstati(STAT_AMMO);
1841                                 if ((stat_items & IT_AMMO) || x != 0)
1842                                 {
1843                                         // (519-3*24) = 447
1844                                         if (stat_items & IT_SHELLS)
1845                                                 drawpic(sbar + '519 0 0', "gfx/sb_shells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1846                                         else if (stat_items & IT_NAILS)
1847                                                 drawpic(sbar + '519 0 0', "gfx/sb_bullets", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1848                                         else if (stat_items & IT_ROCKETS)
1849                                                 drawpic(sbar + '519 0 0', "gfx/sb_rocket", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1850                                         else if (stat_items & IT_CELLS)
1851                                                 drawpic(sbar + '519 0 0', "gfx/sb_cells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1852                                         if(x > 10)
1853                                                 Sbar_DrawXNum('447 12 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1854                                         else
1855                                                 Sbar_DrawXNum('447 12 0', x, 3, 24, '0.7 0 0', 1, 0);
1856                                 }
1857
1858                                 if (sb_lines > 24)
1859                                         drawpic(sbar, "gfx/sbar_overlay", '0 0 0', '1 1 1', 1, DRAWFLAG_MODULATE);
1860
1861                                 if (sbar_x + 600 + 160 <= vid_conwidth)
1862                                         Sbar_MiniDeathmatchOverlay (sbar + '600 0 0');
1863
1864                                 if (sbar_x > 0)
1865                                         Sbar_Score(-16);
1866                                 // Because:
1867                                 //   Mini scoreboard uses 12*4 per other team, that is, 144
1868                                 //   pixels when there are four teams...
1869                                 //   Nexuiz by default sets vid_conwidth to 800... makes
1870                                 //   sbar_x == 80...
1871                                 //   so we need to shift it by 64 pixels to the right to fit
1872                                 //   BUT: then it overlaps with the image that gets drawn
1873                                 //   for viewsize 100! Therefore, just account for 3 teams,
1874                                 //   that is, 96 pixels mini scoreboard size, needing 16 pixels
1875                                 //   to the right!
1876                         }
1877                         
1878                         //show strength/invincibility ICON and timer:
1879                         CSQC_Strength_Timer();
1880
1881                         if(gametype == GAME_KEYHUNT)
1882                         {
1883                                 CSQC_kh_hud();
1884                         } else if(gametype == GAME_CTF)
1885                         {
1886                                 CSQC_ctf_hud();
1887                         }
1888                 }
1889         }
1890 }
1891
1892 void CSQC_ctf_hud(void)
1893 {
1894         // cvar("sbar_flagstatus_right") move the flag icons right
1895         // cvar("sbar_flagstatus_pos") pixel position of the nexuiz flagstatus icons
1896         float redflag, blueflag;
1897         float stat_items;
1898         vector pos;
1899         
1900         stat_items = getstati(STAT_ITEMS);
1901         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
1902         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
1903
1904         /**
1905          * FTEQCC BUG!
1906          * For some reason now not even THAT works there...
1907          * Maybe the minus' precedence screws it up? The last one there, maybe I should use brackets
1908          **
1909          * pos_x = (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - sbar_x - 64 : 10 - sbar_x;
1910          ** Should try those later:
1911          * pos_x = (cvar("sbar_flagstatus_right")) ? (vid_conwidth - 10 - sbar_x - 64) : (10 - sbar_x);
1912          * pos_x = ( (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - 64 : 10 ) - sbar_x;
1913          */
1914         
1915         if(cvar("sbar_flagstatus_right"))
1916                 pos_x = vid_conwidth - 10 - sbar_x - 64;
1917         else
1918                 pos_x = 10 - sbar_x;
1919         
1920         pos_z = 0;
1921
1922         if(sbar_hudselector == 1)
1923                 pos_y = (vid_conheight - sbar_y) - cvar("sbar_flagstatus_pos") - 64;
1924         else
1925                 pos_y = -117;
1926
1927         pos += sbar;
1928
1929         switch(redflag)
1930         {
1931         case 1: drawpic(pos, "gfx/sb_flag_red_taken", '128 64 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1932         case 2: drawpic(pos, "gfx/sb_flag_red_lost", '128 64 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1933         case 3: drawpic(pos, "gfx/sb_flag_red_carrying", '128 64 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1934         default:
1935                 if(stat_items & IT_CTF_SHIELDED)
1936                         if(myteam == COLOR_TEAM2)
1937                                 drawpic(pos, "gfx/sb_flag_red_shielded", '128 64 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1938         }
1939
1940         pos_y -= 64;
1941         
1942         switch(blueflag)
1943         {
1944         case 1: drawpic(pos, "gfx/sb_flag_blue_taken", '128 64 0', '1 1 1', 1, 0); break;
1945         case 2: drawpic(pos, "gfx/sb_flag_blue_lost", '128 64 0', '1 1 1', 1, 0); break;
1946         case 3: drawpic(pos, "gfx/sb_flag_blue_carrying", '128 64 0', '1 1 1', 1, 0); break;
1947         default:
1948                 if(stat_items & IT_CTF_SHIELDED)
1949                         if(myteam == COLOR_TEAM1)
1950                                 drawpic(pos, "gfx/sb_flag_blue_shielded", '128 64 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1951         }
1952 }