]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/sbar.qc
Keep track of CSQC compatibility.
[divverent/nexuiz.git] / data / qcsrc / client / sbar.qc
1
2 float sb_lines; // still don't know what to do with that NOTE: check dp's sbar.c to see what that should be
3
4 vector sbar;
5
6 entity sortedPlayers;
7 entity sortedTeams;
8
9 .float sb_frags;
10 .float sb_team;
11 .float sb_player;
12 .float sb_caps;
13
14 void Sbar_FinaleOverlay()
15 {
16         vector pos;
17         pos_x = (vid_conwidth - 1)/2;
18         pos_y = 16;
19         pos_z = 0;
20         drawpic(pos, "gfx/finale", '0 0', '1 1 1', sbar_alpha_fg, DRAWFLAG_NORMAL);
21 }
22
23 void Sbar_DrawWeapon(float nr, float fade, float active)
24 {
25         vector pos, vsize, color;
26         float value;
27         
28         value = (active) ? 1 : 0.6;
29         color_x = color_y = color_z = value;
30         
31         if(sbar_hudselector == 1)
32         {
33                 // width = 300, height = 100
34                 const float w_width = 32, w_height = 12, w_space = 2, font_size = 8;
35                 
36                 pos_x = (vid_conwidth - w_width * 9) * 0.5 + w_width * nr;
37                 pos_y = (vid_conheight - w_height);
38                 pos_z = 0;
39                 vsize_x = w_width;
40                 vsize_y = w_height;
41                 vsize_z = 0;
42                 drawpic(pos, strcat("gfx/inv_weapon", ftos(nr)), vsize, color, value * fade * sbar_alpha_fg, 0);
43                 pos_x += w_space;
44                 pos_y += w_space;
45                 vsize_x = font_size;
46                 vsize_y = font_size;
47                 vsize_z = 0;
48                 drawstring(pos, ftos(nr+1), vsize, '1 1 0', sbar_alpha_fg, 0);
49
50         }
51         else
52         {
53                 // width = 300, height = 100
54                 const float w2_width = 300, w2_height = 100, w2_space = 10;
55                 const float w2_scale = 0.4;
56
57                 pos_x = vid_conwidth - (w2_width + w2_space) * w2_scale;
58                 pos_y = (w2_height + w2_space) * w2_scale * nr + w2_space;
59                 pos_z = 0;
60                 vsize_x = w2_width * w2_scale;
61                 vsize_y = w2_height * w2_scale;
62                 vsize_z = 0;
63                 
64                 drawpic(pos, strcat("gfx/inv_weapon", ftos(nr)), vsize, color, value * fade * sbar_alpha_fg, 0);
65         }
66 }
67 void Sbar_DrawXNum (vector pos, float num, float digits, float lettersize, vector rgb, float a, float dflags)
68 {
69         float l, i;
70         string str, tmp;
71         float minus;
72         vector vsize;
73
74         vsize_x = vsize_y = lettersize;
75         vsize_z = 0;
76
77         if(num < 0)
78         {
79                 minus = true;
80                 num = -num;
81                 pos_x -= lettersize;
82         } else
83                 minus = false;
84         
85         if(digits < 0)
86         {
87                 tmp = ftos(num);
88                 digits = -digits;
89                 str = strcat(substring("0000000000", 0, digits - strlen(tmp)), tmp);
90         } else
91                 str = ftos(num);
92         
93         l = strlen(str);
94
95         if(l > digits)
96         {
97                 str = substring(str, l-digits, 999);
98                 l = strlen(str);
99         } else if(l < digits)
100                 pos_x += (digits-l) * lettersize;
101
102         if(minus)
103         {
104                 drawpic(sbar + pos, "gfx/num_minus", vsize, rgb, a * sbar_alpha_fg, dflags);
105                 pos_x += lettersize;
106         }
107
108         for(i = 0; i < l; ++i)
109         {
110                 drawpic(sbar + pos, strcat("gfx/num_", substring(str, i, 1)), vsize, rgb, a * sbar_alpha_fg, dflags);
111                 pos_x += lettersize;
112         }
113 }
114
115 float Sbar_PlayerCmp(entity l, entity r)
116 {
117         if(teamplay)
118         {
119                 if(l.sb_team > r.sb_team)
120                         return true;
121                 else if(l.sb_team > r.sb_team)
122                         return false;
123                 if(gametype == GAME_CTF)
124                 {
125                         if(l.sb_caps > r.sb_caps)
126                                 return true;
127                         else if(l.sb_caps < r.sb_caps)
128                                 return false;
129                 }
130         }
131         if(l.sb_frags > r.sb_frags)
132                 return true;
133         else if(l.sb_frags < r.sb_frags)
134                 return false;
135         return (l.sb_player > r.sb_player);
136 }
137 float Sbar_TeamCmp(entity l, entity r)
138 {
139         if(gametype == GAME_CTF)
140         {
141                 if(l.sb_caps > r.sb_caps)
142                         return true;
143                 else if(l.sb_caps < r.sb_caps)
144                         return false;
145         }
146         if(l.sb_frags > r.sb_frags)
147                 return true;
148         else if(l.sb_frags < r.sb_frags)
149                 return false;
150         return (l.sb_player > r.sb_player);
151 }
152
153 void Sbar_SortFrags()
154 {
155         float i;
156         entity tmp;
157         entity t1, t2, t3, t4, ts;
158         
159         Sort_Remove(sortedPlayers);
160         sortedPlayers = Sort_New(Sbar_PlayerCmp);
161
162         numteams = 0;
163         if(teamplay)
164         {
165                 Sort_Remove(sortedTeams);
166         
167                 t1 = spawn();
168                 t2 = spawn();
169                 t3 = spawn();
170                 t4 = spawn();
171                 ts = spawn();
172         
173                 t1.sb_team = COLOR_TEAM1;
174                 t2.sb_team = COLOR_TEAM2;
175                 t3.sb_team = COLOR_TEAM3;
176                 t4.sb_team = COLOR_TEAM4;
177                 ts.sb_team = COLOR_SPECTATOR;
178
179                 t1.sb_player = t2.sb_player = t3.sb_player = t4.sb_player = ts.sb_player = 0;
180                 t1.sb_frags = t2.sb_frags = t3.sb_frags = t4.sb_frags = 0;
181                 t1.sb_caps = caps_team1;
182                 t2.sb_caps = caps_team2;
183                 sortedTeams = Sort_New(Sbar_TeamCmp);
184                 
185                 for(i = 0; i < maxclients; ++i)
186                 {
187                         if(strlen(getplayerkey(i, "name")) <= 0)
188                                 continue;
189                 
190                         tmp = spawn();
191                         tmp.sb_player = i;
192                         tmp.sb_frags = stof(getplayerkey(i, "frags"));
193                         tmp.sb_caps = stof(bufstr_get(databuf, DATABUF_CAPTURES + tmp.sb_player));
194                         
195                         if(tmp.sb_frags == -666)
196                                 tmp.sb_team = COLOR_SPECTATOR;
197                         else
198                                 tmp.sb_team = GetPlayerColor(i);
199
200                         switch(tmp.sb_team)
201                         {
202                         case COLOR_TEAM1: t1.sb_frags += tmp.sb_frags; t1.sb_player++; break;
203                         case COLOR_TEAM2: t2.sb_frags += tmp.sb_frags; t2.sb_player++; break;
204                         case COLOR_TEAM3: t3.sb_frags += tmp.sb_frags; t3.sb_player++; break;
205                         case COLOR_TEAM4: t4.sb_frags += tmp.sb_frags; t4.sb_player++; break;
206                         case COLOR_SPECTATOR: ts.sb_frags += tmp.sb_frags; ts.sb_player++; break;
207                         }
208
209                         if(i == player_localentnum-1)
210                                 myteam = tmp.sb_team;
211
212                         Sort_Add(sortedPlayers, tmp);
213                 }
214                 if(t1.sb_player) ++numteams;
215                 if(t2.sb_player) ++numteams;
216                 if(t3.sb_player) ++numteams;
217                 if(t4.sb_player) ++numteams;
218
219                 Sort_Add(sortedTeams, t1);
220                 Sort_Add(sortedTeams, t2);
221                 Sort_Add(sortedTeams, t3);
222                 Sort_Add(sortedTeams, t4);
223                 Sort_Add(sortedTeams, ts);
224
225         } else {
226                 for(i = 0; i < maxclients; ++i)
227                 {
228                         if(strlen(getplayerkey(i, "name")) <= 0)
229                                 continue;
230                 
231                         tmp = spawn();
232                         tmp.sb_player = i;
233                         tmp.sb_frags = stof(getplayerkey(i, "frags"));
234                         if(tmp.sb_frags == -666)
235                                 tmp.sb_team = COLOR_SPECTATOR;
236                         else
237                                 tmp.sb_team = COLOR_TEAM1;
238                         Sort_Add(sortedPlayers, tmp);
239                 }
240         }
241 }
242
243 void Cmd_Sbar_Help(float argc)
244 {
245         print("You can modify the scoreboard using the\n");
246         print("^3|---------------------------------------------------------------|\n");
247         print("^2sbar_columns^7 cvar and the ^2sbar_columns_set command.\n");
248         print("^2sbar_columns^7             specifies the default layout and\n");
249         print("^2sbar_columns_set^7         actually changes the layout.\n");
250         print("You can call ^2sbar_columns_set^7 with the new layout\n");
251         print("as parameters, or eithout parameters it will read the cvar.\n\n");
252         print("Usage:\n");
253         print("^2sbar_columns_set ^7filed1 field2 ...\n");
254         print("Fields which are not relevant to the current gametype\n");
255         print("won't be displayed\n\n");
256         print("The following field names are recognized (case INsensitive):\n");
257         print("^3name^7 or ^3nick^7             Name of a player\n");
258         print("^3caps^7 or ^3captures^7         Number of flags captured\n");
259         print("^3rets^7 or ^3returns^7          Number of flags returned\n");
260         print("^3frags^7 or ^3kills^7           Frags\n");
261         print("^3deaths^7 or ^3dths^7           Number of deaths\n");
262         print("^3kd^7 or ^3kdr^7 or ^3kdratio^7 or ^3k/d\n");
263         print("                         The kill-death ratio\n");
264         print("^3ping^7                     Ping time\n\n");
265         print("You can use a ^3|^7 to start the right-aligned fields.\n");
266         print("Example: ping name | caps rets frags k/d\n");
267         print("This will put the ping and the name on the left side.\n");
268         print("The captures, returns, frags and kill-death ratio will be\n");
269         print("rendered beginning on the right side.\n");
270
271 }
272
273 void Cmd_Sbar_SetFields(float argc)
274 {
275         float i;
276         string str;
277
278         if(argc < 2)
279                 argc = tokenize(strcat("x ", cvar_string("sbar_columns")));
280         
281         argc = min(MAX_SBAR_FIELDS, argc);
282         sbar_num_fields = 0;
283         for(i = 0; i < argc-1; ++i)
284         {
285                 str = argv(i+1);
286                 strunzone(sbar_title[i]);
287                 sbar_title[i] = strzone(str);
288                 sbar_size[i] = strlen(str)*8;
289                 str = strtolower(str);
290                 if(str == "ping") {
291                         sbar_field[i] = SBF_PING;
292                 } else if(str == "name" || str == "nick") {
293                         sbar_field[i] = SBF_NAME;
294                         sbar_size[i] = 24*8; // minimum size? any use?
295                 } else if(str == "caps" || str == "captures") {
296                         sbar_field[i] = SBF_CAPS;
297                 } else if(str == "rets" || str == "returns") {
298                         sbar_field[i] = SBF_RETS;
299                 } else if(str == "frags" || str == "kills") {
300                         sbar_field[i] = SBF_FRAGS;
301                 } else if(str == "deaths" || str == "dths") {
302                         sbar_field[i] = SBF_DEATHS;
303                 } else if(str == "kdratio") {
304                         sbar_field[i] = SBF_KDRATIO;
305                 } else if(str == "kdr" || str == "k/d") {
306                         sbar_field[i] = SBF_KDRATIO;
307                 } else if(str == "kd") {
308                         sbar_field[i] = SBF_KDRATIO;
309                 } else if(str == "|") {
310                         sbar_field[i] = SBF_SEPARATOR;
311                 } else {
312                         print(strcat("^1Error:^7 Unknown score field: '", str, "'\n"));
313                         --sbar_num_fields;
314                 }
315                 ++sbar_num_fields;
316         }
317         sbar_field[i] = SBF_END;
318 }
319
320 vector sbar_field_rgb;
321 string Sbar_GetField(entity pl, float field)
322 {
323         float tmp;
324         string str;
325         sbar_field_rgb = '1 1 1';
326         switch(field)
327         {
328         case SBF_PING:
329                 str = bufstr_get(databuf, DATABUF_PING + pl.sb_player);
330                 tmp = max(0, min(220, stof(str)-80)) / 220;
331                 sbar_field_rgb = '1 1 1' - '0 1 1'*tmp;
332                 return str;
333         case SBF_NAME: return getplayerkey(pl.sb_player, "name");
334         case SBF_CAPS: return ftos(pl.sb_caps);
335         case SBF_RETS: return bufstr_get(databuf, DATABUF_RETURNS + pl.sb_player);
336         case SBF_FRAGS: return ftos(pl.sb_frags);
337         case SBF_DEATHS: return bufstr_get(databuf, DATABUF_DEATHS + pl.sb_player);
338         case SBF_KDRATIO:
339                 tmp = stof(bufstr_get(databuf, DATABUF_DEATHS + pl.sb_player));
340                 if(tmp == 0) {
341                         sbar_field_rgb = '0 1 0';
342                         str = ftos(pl.sb_frags);
343                 } else if(pl.sb_frags <= 0) {
344                         sbar_field_rgb = '1 0 0';
345                         str = ftos(pl.sb_frags / tmp);
346                 } else
347                         str = ftos(pl.sb_frags / tmp);
348                 
349                 tmp = strstrofs(str, ".", 0);
350                 if(tmp > 0)
351                         str = substring(str, 0, tmp+2);
352                 return str;
353         }
354 }
355
356 float Sbar_IsFieldMasked(float field, float mask)
357 {
358         if(mask&1) // spectator
359                 return (field != SBF_NAME && field != SBF_PING);
360         if(gametype != GAME_CTF)
361                 return (field == SBF_CAPS || field == SBF_RETS);
362         return false;
363 }
364
365 #define MAX_NAMELEN 24
366
367 float xmin, xmax, ymin, ymax, sbwidth, sbheight;
368 void Sbar_PrintScoreboardItem(vector pos, entity pl, float is_self, float mask)
369 {
370         vector tmp;
371         string str, tempstr;
372         float i, field, len;
373
374         // Layout:
375         tmp_z = 0;
376         if(is_self)
377         {
378                 tmp_x = sbwidth;
379                 tmp_y = 8;
380                 drawfill(pos - '1 1', tmp + '2 2', '1 1 1', 0.3, DRAWFLAG_NORMAL);
381         }       
382         tmp_y = 0;
383         
384         for(i = 0; i < sbar_num_fields; ++i)
385         {
386                 field = sbar_field[i];
387                 if(field == SBF_SEPARATOR)
388                         break;
389                 if(Sbar_IsFieldMasked(field, mask))
390                         continue;
391
392                 str = Sbar_GetField(pl, field);
393
394                 if(field == SBF_NAME) {
395                         len = strlen(strdecolorize(str));
396                         if(len > MAX_NAMELEN)
397                         {
398                                 while(len > MAX_NAMELEN)
399                                 {
400                                         // this way should be the fastest with 100% safety :P
401                                         // worst case: decolored length maxnamelen+1, and then only color codes =)
402                                         // cutting of reallength - (decolored-length - maxnamelen) characters
403                                         str = substring(str, 0, strlen(str) - (len-MAX_NAMELEN));
404                                         len = strlen(strdecolorize(str));
405                                 }
406                                 str = strcat(str, "^7...");
407                                 len += 3;
408                         }
409                         len *= 8;
410                 } else
411                         len = 8*strlen(str);
412                 
413                 if(sbar_size[i] < len)
414                         sbar_size[i] = len;
415
416                 pos_x += sbar_size[i] + 8;
417                 if(field == SBF_NAME) {
418                         tmp_x = sbar_size[i] + 8;
419                         drawcolorcodedstring(pos - tmp, str, '8 8', 1, DRAWFLAG_NORMAL);
420                 } else {
421                         tmp_x = 8*strlen(str) + 8;
422                         drawstring(pos - tmp, str, '8 8', sbar_field_rgb, 1, DRAWFLAG_NORMAL);
423                 }
424         }
425         
426         if(sbar_field[i] == SBF_SEPARATOR)
427         {
428                 pos_x = xmax;
429                 for(i = sbar_num_fields-1; i > 0; --i)
430                 {
431                         field = sbar_field[i];
432                         if(field == SBF_SEPARATOR)
433                                 break;
434                         if(Sbar_IsFieldMasked(field, mask))
435                                 continue;
436                         
437                         str = Sbar_GetField(pl, field);
438
439                         if(field == SBF_NAME) {
440                                 len = strlen(strdecolorize(str));
441                                 if(len > MAX_NAMELEN)
442                                 {
443                                         while(len > MAX_NAMELEN)
444                                         {
445                                                 str = substring(str, 0, strlen(str) - (len-MAX_NAMELEN));
446                                                 len = strlen(strdecolorize(str));
447                                         }
448                                         str = strcat(str, "^7...");
449                                         len += 3;
450                                 }
451                                 len *= 8;
452                         } else
453                                 len = 8*strlen(str);
454                         //len = 8*strlen(str);
455                         if(sbar_size[i] < len)
456                                 sbar_size[i] = len;
457                         
458                         if(field == SBF_NAME) {
459                                 tmp_x = len; // left or right aligned? let's put it right...
460                                 drawcolorcodedstring(pos - tmp, str, '8 8', 1, DRAWFLAG_NORMAL);
461                         } else {
462                                 tmp_x = len; //strlen(str);
463                                 drawstring(pos - tmp, str, '8 8', sbar_field_rgb, 1, DRAWFLAG_NORMAL);
464                         }
465                         pos_x -= sbar_size[i] + 8;
466                 }
467         }
468 }
469
470 void Sbar_DrawScoreboard()
471 {
472         //float xmin, ymin, xmax, ymax;
473         vector rgb, pos, tmp, sbar_save;
474         entity pl, tm;
475         float specs, i;
476         float center_x;
477         string str;
478         
479         xmin = vid_conwidth / 5;
480         ymin = 20;
481
482         xmax = vid_conwidth - xmin;
483         ymax = vid_conheight - 0.2*vid_conheight;
484
485         sbwidth = xmax - xmin;
486         sbheight = ymax - ymin;
487
488         center_x = xmin + 0.5*sbwidth;
489
490         //Sbar_UpdateFields();
491
492         // Initializes position
493         //pos_x = xmin;
494         pos_y = ymin;
495         pos_z = 0;
496
497         // Heading
498         drawfont = FONT_USER+0;
499         pos_x = center_x - 4*24;
500         drawstring(pos, "Scoreboard", '24 24', '1 1 1', 1, DRAWFLAG_NORMAL);
501         pos_x = xmin;
502         pos_y += 24 + 4;
503         drawfont = FONT_DEFAULT;
504
505         // Titlebar background:
506         tmp_x = sbwidth;
507         tmp_y = 8;
508         drawfill(pos - '1 1', tmp + '2 2', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
509         
510         for(i = 0; i < sbar_num_fields; ++i)
511         {
512                 if(sbar_field[i] == SBF_SEPARATOR)
513                         break;
514                 drawstring(pos, sbar_title[i], '8 8', '1 1 1', 1, DRAWFLAG_NORMAL);
515                 pos_x += sbar_size[i] + 8;
516         }
517         
518         if(sbar_field[i] == SBF_SEPARATOR)
519         {
520                 pos_x = xmax + 8;
521                 tmp_y = tmp_z = 0;
522                 for(i = sbar_num_fields-1; i > 0; --i)
523                 {
524                         if(sbar_field[i] == SBF_SEPARATOR)
525                                 break;
526                         
527                         pos_x -= sbar_size[i] + 8;
528                         /**
529                          * FTEQCC BUG!
530                          * Using the following line will fuck it all up:
531                          **
532                          * tmp_x = sbar_size[i] - strlen(sbar_title[i])*8;
533                          */
534                         tmp_x = sbar_size[i];
535                         tmp_x -= strlen(sbar_title[i])*8;
536                         drawstring(pos + tmp, sbar_title[i], '8 8', '1 1 1', 1, DRAWFLAG_NORMAL);
537                 }
538         }
539                 
540         pos_x = xmin;
541         pos_y += 12;
542
543         sbar_save = sbar;
544         sbar = '0 0 0';
545         
546         if(teamplay)
547         {
548                 for(tm = sortedTeams.sort_next; tm; tm = tm.sort_next)
549                 {
550                         if(!tm.sb_player || tm.sb_team == COLOR_SPECTATOR) // no players in it?
551                                 continue;
552
553                         rgb = GetTeamRGB(tm.sb_team);
554
555                         pos_x = xmin - 4*24;
556                         if(gametype == GAME_CTF)
557                         {
558                                 if(tm.sb_team == COLOR_TEAM1)
559                                         Sbar_DrawXNum(pos, caps_team1, 4, 24, rgb, 1, DRAWFLAG_NORMAL);
560                                 else if(tm.sb_team == COLOR_TEAM2)
561                                         Sbar_DrawXNum(pos, caps_team2, 4, 24, rgb, 1, DRAWFLAG_NORMAL);
562                                 pos_x = xmin - 4*10;
563                                 Sbar_DrawXNum(pos + '0 24', tm.sb_frags, 4, 10, rgb, 1, DRAWFLAG_NORMAL);
564                                 pos_x = xmin;
565                         } else
566                                 Sbar_DrawXNum(pos, tm.sb_frags, 4, 24, rgb, 1, DRAWFLAG_NORMAL);
567                         pos_x = xmin;
568
569                         // abuse specs as playerounter
570                         specs = 0;
571                         for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
572                         {
573                                 if(pl.sb_team == tm.sb_team)
574                                         ++specs;
575                         }
576
577                         if(specs < 2)
578                                 specs = 2;
579                         if(gametype == GAME_CTF && specs < 4)
580                                 specs = 4;
581                         
582                         tmp_x = sbwidth;
583                         tmp_y = 10 * specs;
584                         drawfill(pos - '1 1', tmp + '2 0', rgb, 0.2, DRAWFLAG_NORMAL);
585                         
586                         for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
587                         {
588                                 if(pl.sb_team != tm.sb_team)
589                                         continue;
590                                 Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), 0);
591                                 pos_y += 10;
592                                 tmp_y -= 10;
593                         }
594                         pos_y += tmp_y + 12;
595                 }
596                 // rgb := tempvector :)
597                 rgb = pos + '0 12 0';
598                 pos_y += 24;
599                 specs = 0;
600                 for(pl = sortedPlayers.sort_next; pl; pl = pl.sort_next)
601                 {
602                         if(pl.sb_team != COLOR_SPECTATOR)
603                                 continue;
604                         //drawcolorcodedstring(pos, getplayerkey(pl.sb_player, "name"), '8 8 0', 1, 0);
605                         Sbar_PrintScoreboardItem(pos, pl, (pl.sb_player == player_localentnum - 1), 1);
606                         pos += '0 10 0';
607                         ++specs;
608                 }
609                         
610                 if(specs)
611                         drawstring(rgb, "Spectators", '8 8 0', '1 1 1', 1, 0);
612         }
613         sbar = sbar_save;
614 }
615
616 void Sbar_Score(float margin)
617 {
618         float timelimit, timeleft, minutes, seconds, distribution, myplace;
619         vector sbar_save, place;
620         entity tm, pl, me;
621         sbar_save = sbar;
622
623         sbar_y = vid_conheight - (32+12);
624         sbar_x -= margin;
625         
626         place = '-48 -12 0';
627         if(teamplay)
628         {
629                 // Layout:
630                 //
631                 //   team1 team3 team4
632                 //
633                 //         TEAM2
634                 //for(i = 0; i < 4; ++i)
635                 for(tm = sortedTeams.sort_next; tm; tm = tm.sort_next)
636                 {
637                         if(tm.sb_team == COLOR_SPECTATOR || !tm.sb_player) // no players? don't display
638                                 continue;
639                         // -32*4 = -128
640                         if(tm.sb_team == myteam)
641                                 Sbar_DrawXNum('-128 0', tm.sb_frags, 4, 32, GetTeamRGB(tm.sb_team), 1, DRAWFLAG_NORMAL);
642                         else
643                         {
644                                 Sbar_DrawXNum(place, tm.sb_frags, 4, 12, GetTeamRGB(tm.sb_team), 1, DRAWFLAG_NORMAL);
645                                 place_x -= 4*12;
646                         }
647                 }
648         } else {
649                 // me vector := [team/connected frags id]
650                 myplace = 0;
651                 for(me = sortedPlayers.sort_next; me; me = me.sort_next)
652                 {
653                         if(me.sb_team != COLOR_SPECTATOR)
654                                 ++myplace;
655                         if(me.sb_player == player_localentnum - 1)
656                                 break;
657                 }
658                 pl = sortedPlayers.sort_next;
659                 if(pl == me)
660                         pl = pl.sort_next;
661                 
662                 if(pl && myplace != 1)
663                 {
664                         distribution = me.sb_frags - pl.sb_frags;
665                 } else if(pl) {
666                         distribution = me.sb_frags - pl.sb_frags;
667                 } else
668                         distribution = 0;
669                 
670                 if(myplace == 1)
671                         Sbar_DrawXNum('-36 -12', myplace, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
672                 else if(myplace == 2)
673                         Sbar_DrawXNum('-36 -12', myplace, 3, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
674                 else
675                         Sbar_DrawXNum('-36 -12', myplace, 3, 12, '1 0 0', 1, DRAWFLAG_NORMAL);
676
677                 if(distribution >= 0)
678                 {
679                         Sbar_DrawXNum('-84 -12', distribution, 4, 12, ' 1 1 1', 1, DRAWFLAG_NORMAL);
680                         Sbar_DrawXNum('-128 0', me.sb_frags, 4, 32, '1 1 1', 1, DRAWFLAG_NORMAL);
681                 } else if(distribution >= -5)
682                 {
683                         Sbar_DrawXNum('-84 -12', distribution, 4, 12, ' 1 1 0', 1, DRAWFLAG_NORMAL);
684                         Sbar_DrawXNum('-128 0', me.sb_frags, 4, 32, '1 1 0', 1, DRAWFLAG_NORMAL);
685                 } else {
686                         Sbar_DrawXNum('-84 -12', distribution, 4, 12, ' 1 0 0', 1, DRAWFLAG_NORMAL);
687                         Sbar_DrawXNum('-128 0', me.sb_frags, 4, 32, '1 0 0', 1, DRAWFLAG_NORMAL);
688                 }
689         }
690         timelimit = getstatf(STAT_TIMELIMIT);
691         if(timelimit)
692         {
693                 timeleft = max(0, timelimit * 60 - time);
694                 minutes = floor(timeleft / 60);
695                 seconds = floor(timeleft - minutes*60);
696                 if(minutes >= 5)
697                 {
698                         Sbar_DrawXNum('-72 32', minutes, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
699                         drawpic(sbar + '-36 32', "gfx/num_colon", '12 12', '1 1 1', sbar_alpha_fg, 0);
700                         Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
701                 } else if(minutes >= 1)
702                 {
703                         Sbar_DrawXNum('-72 32', minutes, 3, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
704                         drawpic(sbar + '-36 32', "gfx/num_colon", '12 12', '1 1 0', sbar_alpha_fg, 0);
705                         Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 1 0', 1, DRAWFLAG_NORMAL);
706                 } else {
707                         Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 0 0', 1, DRAWFLAG_NORMAL);
708                 }
709         } else {
710                 minutes = floor(time / 60);
711                 seconds = floor(time - minutes*60);
712                 Sbar_DrawXNum('-72 32', minutes, 3, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
713                 drawpic(sbar + '-36 32', "gfx/num_colon", '12 12', '1 1 1', sbar_alpha_fg, 0);
714                 Sbar_DrawXNum('-24 32', seconds, -2, 12, '1 1 1', 1, DRAWFLAG_NORMAL);
715         }
716         sbar = sbar_save;
717 }
718
719 void Sbar_MiniscoreItem(vector pos, entity pl, float is_self)
720 {
721         float x;
722         pos_x += 72;
723         
724         if(teamplay)
725                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(pl.sb_team)*0.5, 1, DRAWFLAG_NORMAL);
726         else
727                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
728         x = pos_x;
729         pos_x += 5*8;
730         pos_x -= strlen(ftos(pl.sb_frags))*8;
731         drawstring(pos, ftos(pl.sb_frags), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
732         pos_x = x;
733         if(is_self)
734         {
735                 pos_x += 48;
736                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
737                 pos_x += 8;
738         } else
739                 pos_x += 56;
740         drawcolorcodedstring(pos, getplayerkey(pl.sb_player, "name"), '8 8 0', 1, 0);
741 }
742
743 void Sbar_MiniscoreTeamItem(vector pos, float color, float frags, float is_self)
744 {
745         float x;
746         pos_x += 72;
747         
748         if(teamplay)
749                 drawfill(pos + '0 1 0', '40 6 0', GetTeamRGB(color)*0.5, 1, DRAWFLAG_NORMAL);
750         else
751                 drawfill(pos + '0 1 0', '40 6 0', '0.5 0.5 0.5', 0.5, DRAWFLAG_NORMAL);
752         x = pos_x;
753         pos_x += 5*8;
754         pos_x -= strlen(ftos(frags))*8;
755         drawstring(pos, ftos(frags), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
756         pos_x = x;
757         if(is_self)
758         {
759                 pos_x += 48;
760                 drawstring(pos, "\x0D", '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
761                 pos_x += 8;
762         } else
763                 pos_x += 56;
764         drawstring(pos, GetTeamName(color), '8 8 0', '1 1 1', 1, DRAWFLAG_NORMAL);
765 }
766
767 void Sbar_MiniDeathmatchOverlay(vector pos)
768 {
769         float numlines, up, down;
770         entity me, tm, pl;
771         float miniscoreboard_size;
772         miniscoreboard_size = cvar("sbar_miniscoreboard_size");
773         
774         if(miniscoreboard_size == 0)
775                 return;
776         pos_y = vid_conheight - 8;
777         
778         if(miniscoreboard_size < 0)
779                 numlines = (vid_conheight - sbar_y + 7) / 8;
780         else
781                 numlines = miniscoreboard_size;
782
783         // give up if there isn't enough room
784         if(pos_x >= vid_conwidth || pos_y >= vid_conheight || numlines < 1)
785                 return;
786
787         // me vector := [team/connected frags id]
788         for(me = sortedPlayers.sort_next; me; me = me.sort_next)
789         {
790                 if(me.sb_player == player_localentnum - 1)
791                         break;
792         }
793
794         if(teamplay)
795                 numlines -= numteams;
796
797         // figure out how many players above and below we can show
798         up = floor(numlines/2);
799         down = up;
800         if((up + down) > numlines)
801                 down = numlines - up;
802
803         // render bottom-up
804         for(pl = me.sort_next; pl && down > 0; pl = pl.sort_next)
805         {
806                 if(pl.sb_team == COLOR_SPECTATOR)
807                         continue;
808                 Sbar_MiniscoreItem(pos, pl, false);
809                 pos_y -= 9;
810                 --down;
811         }
812         Sbar_MiniscoreItem(pos, me, true);
813         pos_y -= 9;
814         up += down; // if there weren't enough lines below... add them
815         for(pl = me.sort_prev; pl != sortedPlayers && up > 0; pl = pl.sort_prev)
816         {
817                 if(pl.sb_team == COLOR_SPECTATOR)
818                         continue;
819                 Sbar_MiniscoreItem(pos, pl, false);
820                 pos_y -= 9;
821                 --up;
822         }
823
824         if(teamplay)
825         {
826                 for(tm = sortedTeams.sort_next; tm.sort_next; tm = tm.sort_next);
827                 for(; tm != sortedTeams; tm = tm.sort_prev)
828                 {
829                         if(!tm.sb_player || tm.sb_team == COLOR_SPECTATOR) // no players?
830                                 continue;
831                         Sbar_MiniscoreTeamItem(pos, tm.sb_team, tm.sb_frags, (tm.sb_team == me.sb_team));
832                         pos_y -= 9;
833                 }
834         }
835 }
836
837 void Sbar_Draw (void)
838 {
839         float i;
840         float x, fade;
841         float stat_items;
842
843         Sbar_SortFrags();
844
845         sb_lines = 24;
846         
847         if (sb_showscores)
848                 Sbar_DrawScoreboard();
849         else if (intermission == 1)
850         {
851                 Sbar_DrawScoreboard();
852                 return;
853         }
854         else if (intermission == 2)
855                 Sbar_FinaleOverlay();
856         else
857         {
858                 if (sb_showscores || (getstati(STAT_HEALTH) <= 0 && cvar("cl_deathscoreboard")))
859                 {
860                         sbar_x = (vid_conwidth - 640.0)*0.5;
861                         sbar_y = vid_conheight - 47;
862                         //Sbar_DrawAlphaPic (sbar_x, sbar_y, sb_scorebar, sbar_alpha_bg.value);
863                         //drawpic('0 0', "gfx/scorebar", '0 0 0', '1 1 1', cvar("sbar_alpha_bg"), 0);
864                         Sbar_DrawScoreboard ();
865                 }
866                 else
867                 {
868                         if (sb_lines && sbar_hudselector == 1)
869                         {
870                                 stat_items = getstati(STAT_ITEMS);
871
872                                 sbar_x = (vid_conwidth - 320.0)*0.5;
873                                 sbar_y = vid_conheight - 24.0 - 16.0;
874                                 sbar_z = 0;
875                         
876                                 fade = 3.2 - 2 * (time - weapontime);
877                                 fade = bound(0.7, fade, 1);
878
879                                 x = 1.0;
880                                 for(i = 0; i < 8; ++i)
881                                 {
882                                         if(stat_items & x)
883                                         {
884                                                 Sbar_DrawWeapon(i+1, fade, (i + 2 == activeweapon));
885                                         }
886                                         x *= 2;
887                                 }
888                                 x *= 2*2*2*2;
889                                 if(stat_items & x)
890                                 {
891                                         Sbar_DrawWeapon(0, fade, (activeweapon == 1));
892                                 }
893
894                                 // armor
895                                 x = getstati(STAT_ARMOR);
896                                 if (x > 0)
897                                 {
898                                         // "gfx/sb_armor"
899                                         //Sbar_DrawStretchPic (72, 0, sb_armor[0], sbar_alpha_fg.value, 24, 24);
900                                         drawpic(sbar + '72 0', "gfx/sb_armor", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
901                                         if(x > 200)
902                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0 1 0', 1, 0);
903                                         else if(x > 100)
904                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0.2 1 0', 1, 0);
905                                         else if(x > 50)
906                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
907                                         else if(x > 25)
908                                                 Sbar_DrawXNum('0 0', x, 3, 24, '1 1 0.2', 1, 0);
909                                         else
910                                                 Sbar_DrawXNum('0 0', x, 3, 24, '0.7 0 0', 1, 0);
911                                 }
912
913                                 // health
914                                 x = getstati(STAT_HEALTH);
915                                 if (x != 0)
916                                 {
917                                         // "gfx/sb_health"
918                                         //Sbar_DrawStretchPic (184, 0, sb_health, sbar_alpha_fg.value, 24, 24);
919                                         drawpic(sbar + '184 0', "gfx/sb_health", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
920                                         if(x > 200)
921                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0 1 0', 1, 0);
922                                         else if(x > 100)
923                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0.2 1 0', 1, 0);
924                                         else if(x > 50)
925                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
926                                         else if(x > 25)
927                                                 Sbar_DrawXNum('112 0', x, 3, 24, '1 1 0.2', 1, 0);
928                                         else
929                                                 Sbar_DrawXNum('112 0', x, 3, 24, '0.7 0 0', 1, 0);
930                                 }
931
932                                 // ammo
933                                 x = getstati(STAT_AMMO);
934                                 if ((stat_items & (NEX_IT_SHELLS | NEX_IT_BULLETS | NEX_IT_ROCKETS | NEX_IT_CELLS)) || x != 0)
935                                 {
936                                         if (stat_items & NEX_IT_SHELLS)
937                                                 drawpic(sbar + '296 0', "gfx/sb_shells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
938                                         else if (stat_items & NEX_IT_BULLETS)
939                                                 drawpic(sbar + '296 0', "gfx/sb_bullets", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
940                                         else if (stat_items & NEX_IT_ROCKETS)
941                                                 drawpic(sbar + '296 0', "gfx/sb_rocket", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
942                                         else if (stat_items & NEX_IT_CELLS)
943                                                 drawpic(sbar + '296 0', "gfx/sb_cells", '24 24 0', '1 1 1', sbar_alpha_fg, 0);
944                                         if(x > 10)
945                                                 Sbar_DrawXNum('224 0', x, 3, 24, '0.6 0.7 0.8', 1, 0);
946                                         else
947                                                 Sbar_DrawXNum('224 0', x, 3, 24, '0.7 0 0', 1, 0);
948                                 }
949
950                                 if (sbar_x + 320 + 160 <= vid_conwidth)
951                                         Sbar_MiniDeathmatchOverlay(sbar + '320 0');
952                                 if (sbar_x > 0)
953                                         Sbar_Score(16);
954                                 // The margin can be at most 8 to support 640x480 console size:
955                                 //   320 + 2 * (144 + 16) = 640
956                         }
957                         else if (sb_lines)
958                         {
959                         
960                                 stat_items = getstati(STAT_ITEMS);
961                         
962                                 sbar_x = (vid_conwidth - 640.0)*0.5;
963                                 sbar_y = vid_conheight - 47;
964                                 sbar_z = 0;
965
966                                 fade = 3 - 2 * (time - weapontime);
967
968                                 x = 1.0;
969                                 for(i = 0; i < 8; ++i)
970                                 {
971                                         if(stat_items & x)
972                                         {
973                                                 Sbar_DrawWeapon(i+1, fade, (i + 2 == activeweapon));
974                                         }
975                                         x *= 2;
976                                 }
977                                 x *= 2*2*2*2;
978                                 if(stat_items & x)
979                                 {
980                                         Sbar_DrawWeapon(0, fade, (activeweapon == 1));
981                                 }
982
983                                 if (sb_lines > 24)
984                                         drawpic(sbar, "gfx/sbar", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
985                                 else
986                                         drawpic(sbar, "gfx/sbar_minimal", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
987
988                                 // armor
989                                 // (340-3*24) = 268
990                                 Sbar_DrawXNum('268 12', getstati(STAT_ARMOR), 3, 24, '0.6 0.7 0.8', 1, 0);
991
992                                 // health
993                                 // (154-3*24) = 82
994                                 x = getstati(STAT_HEALTH);
995                                 if(x > 100)
996                                         Sbar_DrawXNum('82 12', x, 3, 24, '1 1 1', 1, 0);
997                                 else if(x <= 25 && time - floor(time) > 0.5)
998                                         Sbar_DrawXNum('82 12', x, 3, 24, '0.7 0 0', 1, 0);
999                                 else
1000                                         Sbar_DrawXNum('81 12', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1001
1002                                 // AK dont draw ammo for the laser
1003                                 x = getstati(STAT_AMMO);
1004                                 if(activeweapon != 12)
1005                                 {
1006                                         // (519-3*24) = 447
1007                                         if (stat_items & NEX_IT_SHELLS)
1008                                                 drawpic(sbar + '519 0', "gfx/sb_shells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1009                                         else if (stat_items & NEX_IT_BULLETS)
1010                                                 drawpic(sbar + '519 0', "gfx/sb_bullets", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1011                                         else if (stat_items & NEX_IT_ROCKETS)
1012                                                 drawpic(sbar + '519 0', "gfx/sb_rocket", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1013                                         else if (stat_items & NEX_IT_CELLS)
1014                                                 drawpic(sbar + '519 0', "gfx/sb_cells", '0 0 0', '1 1 1', sbar_alpha_fg, 0);
1015                                         if(x > 10)
1016                                                 Sbar_DrawXNum('447 12', x, 3, 24, '0.6 0.7 0.8', 1, 0);
1017                                         else
1018                                                 Sbar_DrawXNum('447 12', x, 3, 24, '0.7 0 0', 1, 0);
1019                                 }
1020
1021                                 if (sb_lines > 24)
1022                                         drawpic(sbar, "gfx/sbar_overlay", '0 0 0', '1 1 1', 1, DRAWFLAG_MODULATE);
1023
1024                                 if (sbar_x + 600 + 160 <= vid_conwidth)
1025                                         Sbar_MiniDeathmatchOverlay (sbar + '600 0');
1026
1027                                 if (sbar_x > 0)
1028                                         Sbar_Score(-16);
1029                                 // Because:
1030                                 //   Mini scoreboard uses 12*4 per other team, that is, 144
1031                                 //   pixels when there are four teams...
1032                                 //   Nexuiz by default sets vid_conwidth to 800... makes
1033                                 //   sbar_x == 80...
1034                                 //   so we need to shift it by 64 pixels to the right to fit
1035                                 //   BUT: then it overlaps with the image that gets drawn
1036                                 //   for viewsize 100! Therefore, just account for 3 teams,
1037                                 //   that is, 96 pixels mini scoreboard size, needing 16 pixels
1038                                 //   to the right!
1039                         }
1040                 
1041                 
1042                         if(gametype == GAME_KEYHUNT)
1043                         {
1044                                 CSQC_kh_hud();
1045                         } else if(gametype == GAME_CTF)
1046                         {
1047                                 CSQC_ctf_hud();
1048                         }
1049                 }
1050         }
1051 }
1052
1053 void CSQC_ctf_hud(void)
1054 {
1055         // cvar("sbar_flagstatus_right") move the flag icons right
1056         // cvar("sbar_flagstatus_pos") pixel position of the nexuiz flagstatus icons
1057         float redflag, blueflag;
1058         float stat_items;
1059         vector pos;
1060         
1061         stat_items = getstati(STAT_ITEMS);
1062         redflag = (stat_items/32768) & 3;
1063         blueflag = (stat_items/131072) & 3;
1064
1065         /**
1066          * FTEQCC BUG!
1067          * For some reason now not even THAT works there...
1068          * Maybe the minus' precedence screws it up? The last one there, maybe I should use brackets
1069          **
1070          * pos_x = (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - sbar_x - 64 : 10 - sbar_x;
1071          ** Should try those later:
1072          * pos_x = (cvar("sbar_flagstatus_right")) ? (vid_conwidth - 10 - sbar_x - 64) : (10 - sbar_x);
1073          * pos_x = ( (cvar("sbar_flagstatus_right")) ? vid_conwidth - 10 - 64 : 10 ) - sbar_x;
1074          */
1075         
1076         if(cvar("sbar_flagstatus_right"))
1077                 pos_x = vid_conwidth - 10 - sbar_x - 64;
1078         else
1079                 pos_x = 10 - sbar_x;
1080         
1081         pos_z = 0;
1082
1083         if(sbar_hudselector == 1)
1084                 pos_y = (vid_conheight - sbar_y) - cvar("sbar_flagstatus_pos") - 64;
1085         else
1086                 pos_y = -117;
1087
1088         pos += sbar;
1089
1090         switch(redflag)
1091         {
1092         case 1: drawpic(pos, "gfx/sb_flag_red_taken", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1093         case 2: drawpic(pos, "gfx/sb_flag_red_lost", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1094         case 3: drawpic(pos, "gfx/sb_flag_red_carrying", '0 0 0', '1 1 1', 1, DRAWFLAG_NORMAL); break;
1095         }
1096
1097         pos_y -= 64;
1098         
1099         switch(blueflag)
1100         {
1101         case 1: drawpic(pos, "gfx/sb_flag_blue_taken", '0 0 0', '1 1 1', 1, 0); break;
1102         case 2: drawpic(pos, "gfx/sb_flag_blue_lost", '0 0 0', '1 1 1', 1, 0); break;
1103         case 3: drawpic(pos, "gfx/sb_flag_blue_carrying", '0 0 0', '1 1 1', 1, 0); break;
1104         }
1105 }