]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
d05894456ec1bcff591290ad71bc919487ff3122
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qc
1 /*
2 ==================
3 Misc HUD functions
4 ==================
5 */
6
7 // a border picture is a texture containing nine parts:
8 //   1/4 width: left part
9 //   1/2 width: middle part (stretched)
10 //   1/4 width: right part
11 // divided into
12 //   1/4 height: top part
13 //   1/2 height: middle part (stretched)
14 //   1/4 height: bottom part
15 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
16 {
17     if (theBorderSize_x < 0 && theBorderSize_y < 0) // draw whole image as it is
18     {
19                 drawpic(theOrigin, pic, theSize, theColor, theAlpha, 0);
20                 return;
21     }
22         if (theBorderSize_x == 0 && theBorderSize_y == 0) // no border
23         {
24                 // draw only the central part
25                 drawsubpic(theOrigin, theSize, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
26                 return;
27         }
28
29         vector dX, dY;
30         vector width, height;
31         vector bW, bH;
32         //pic = draw_UseSkinFor(pic);
33         width = eX * theSize_x;
34         height = eY * theSize_y;
35         if(theSize_x <= theBorderSize_x * 2)
36         {
37                 // not wide enough... draw just left and right then
38                 bW = eX * (0.25 * theSize_x / (theBorderSize_x * 2));
39                 if(theSize_y <= theBorderSize_y * 2)
40                 {
41                         // not high enough... draw just corners
42                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
43                         drawsubpic(theOrigin,                 width * 0.5 + height * 0.5, pic, '0 0 0',           bW + bH, theColor, theAlpha, 0);
44                         drawsubpic(theOrigin + width   * 0.5, width * 0.5 + height * 0.5, pic, eX - bW,           bW + bH, theColor, theAlpha, 0);
45                         drawsubpic(theOrigin + height  * 0.5, width * 0.5 + height * 0.5, pic, eY - bH,           bW + bH, theColor, theAlpha, 0);
46                         drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
47                 }
48                 else
49                 {
50                         dY = theBorderSize_x * eY;
51                         drawsubpic(theOrigin,                             width * 0.5          +     dY, pic, '0 0    0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
52                         drawsubpic(theOrigin + width * 0.5,               width * 0.5          +     dY, pic, '0 0    0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
53                         drawsubpic(theOrigin                        + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0',           '0 0.5  0' + bW, theColor, theAlpha, 0);
54                         drawsubpic(theOrigin + width * 0.5          + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0' + eX - bW, '0 0.5  0' + bW, theColor, theAlpha, 0);
55                         drawsubpic(theOrigin               + height - dY, width * 0.5          +     dY, pic, '0 0.75 0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
56                         drawsubpic(theOrigin + width * 0.5 + height - dY, width * 0.5          +     dY, pic, '0 0.75 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
57                 }
58         }
59         else
60         {
61                 if(theSize_y <= theBorderSize_y * 2)
62                 {
63                         // not high enough... draw just top and bottom then
64                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
65                         dX = theBorderSize_x * eX;
66                         drawsubpic(theOrigin,                                         dX + height * 0.5, pic, '0    0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
67                         drawsubpic(theOrigin + dX,                        width - 2 * dX + height * 0.5, pic, '0.25 0 0',           '0.5  0 0' + bH, theColor, theAlpha, 0);
68                         drawsubpic(theOrigin + width - dX,                            dX + height * 0.5, pic, '0.75 0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
69                         drawsubpic(theOrigin              + height * 0.5,             dX + height * 0.5, pic, '0    0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
70                         drawsubpic(theOrigin + dX         + height * 0.5, width - 2 * dX + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5  0 0' + bH, theColor, theAlpha, 0);
71                         drawsubpic(theOrigin + width - dX + height * 0.5,             dX + height * 0.5, pic, '0.75 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
72                 }
73                 else
74                 {
75                         dX = theBorderSize_x * eX;
76                         dY = theBorderSize_x * eY;
77                         drawsubpic(theOrigin,                                        dX          +     dY, pic, '0    0    0', '0.25 0.25 0', theColor, theAlpha, 0);
78                         drawsubpic(theOrigin                  + dX,      width - 2 * dX          +     dY, pic, '0.25 0    0', '0.5  0.25 0', theColor, theAlpha, 0);
79                         drawsubpic(theOrigin          + width - dX,                  dX          +     dY, pic, '0.75 0    0', '0.25 0.25 0', theColor, theAlpha, 0);
80                         drawsubpic(theOrigin          + dY,                          dX + height - 2 * dY, pic, '0    0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
81                         drawsubpic(theOrigin          + dY         + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5  0.5  0', theColor, theAlpha, 0);
82                         drawsubpic(theOrigin          + dY + width - dX,             dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
83                         drawsubpic(theOrigin + height - dY,                          dX          +     dY, pic, '0    0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
84                         drawsubpic(theOrigin + height - dY         + dX, width - 2 * dX          +     dY, pic, '0.25 0.75 0', '0.5  0.25 0', theColor, theAlpha, 0);
85                         drawsubpic(theOrigin + height - dY + width - dX,             dX          +     dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
86                 }
87         }
88 }
89
90 vector HUD_Get_Num_Color (float x, float maxvalue)
91 {
92         float blinkingamt;
93         vector color;
94         if(x >= maxvalue) {
95                 color_x = sin(2*M_PI*time);
96                 color_y = 1;
97                 color_z = sin(2*M_PI*time);
98         }
99         else if(x > maxvalue * 0.75) {
100                 color_x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
101                 color_y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
102                 color_z = 0;
103         }
104         else if(x > maxvalue * 0.5) {
105                 color_x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
106                 color_y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
107                 color_z = 1 - (x-100)*0.02; // blue value between 1 -> 0
108         }
109         else if(x > maxvalue * 0.25) {
110                 color_x = 1;
111                 color_y = 1;
112                 color_z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
113         }
114         else if(x > maxvalue * 0.1) {
115                 color_x = 1;
116                 color_y = (x-20)*90/27/100; // green value between 0 -> 1
117                 color_z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
118         }
119         else {
120                 color_x = 1;
121                 color_y = 0;
122                 color_z = 0;
123         }
124
125         blinkingamt = (1 - x/maxvalue/0.25);
126         if(blinkingamt > 0)
127         {
128                 color_x = color_x - color_x * blinkingamt * sin(2*M_PI*time);
129                 color_y = color_y - color_y * blinkingamt * sin(2*M_PI*time);
130                 color_z = color_z - color_z * blinkingamt * sin(2*M_PI*time);
131         }
132         return color;
133 }
134
135 float stringwidth_colors(string s, vector theSize)
136 {
137         return stringwidth(s, TRUE, theSize);
138 }
139
140 float stringwidth_nocolors(string s, vector theSize)
141 {
142         return stringwidth(s, FALSE, theSize);
143 }
144
145 #define CENTERPRINT_MAX_LINES 10
146 float cpm_index;
147 string centerprint_messages[CENTERPRINT_MAX_LINES];
148 float centerprint_width[CENTERPRINT_MAX_LINES];
149 float centerprint_time[CENTERPRINT_MAX_LINES];
150 float centerprint_expire;
151 float centerprint_num;
152 float centerprint_offset_hint;
153 vector centerprint_fontsize;
154
155 void centerprint(string strMessage)
156 {
157         float i, j, n, hcount;
158         string s;
159
160         centerprint_fontsize = HUD_GetFontsize("scr_centersize");
161
162         centerprint_expire = min(centerprint_expire, time); // if any of the returns happens, this message will fade out
163
164         if(autocvar_scr_centertime <= 0)
165                 return;
166
167         if(strMessage == "")
168                 return;
169
170         // strip trailing newlines
171         j = strlen(strMessage) - 1;
172         while(substring(strMessage, j, 1) == "\n" && j >= 0)
173                 j = j - 1;
174         strMessage = substring(strMessage, 0, j + 1);
175
176         if(strMessage == "")
177                 return;
178
179         // strip leading newlines and remember them, they are a hint that the message should be lower on the screen
180         j = 0;
181         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
182                 j = j + 1;
183         strMessage = substring(strMessage, j, strlen(strMessage) - j);
184         centerprint_offset_hint = j;
185
186         if(strMessage == "")
187                 return;
188
189         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_LINES; ++i, ++j)
190         {
191                 if (j == CENTERPRINT_MAX_LINES)
192                         j = 0;
193                 if(centerprint_messages[j] == strMessage)
194                 {
195                         centerprint_time[j] = time;
196                         return;
197                 }
198         }
199
200         --cpm_index;
201         if (cpm_index == -1)
202                 cpm_index = CENTERPRINT_MAX_LINES - 1;
203         if(centerprint_messages[cpm_index])
204                 strunzone(centerprint_messages[cpm_index]);
205         centerprint_messages[cpm_index] = strzone(strMessage);
206         centerprint_time[cpm_index] = time;
207
208
209 #if 0
210         i = hcount = 0;
211         for(j = 0; j < n; ++j)
212         {
213                 getWrappedLine_remaining = argv(j);
214                 while(getWrappedLine_remaining)
215                 {
216                         s = getWrappedLine(vid_conwidth * 0.75, centerprint_fontsize, stringwidth_colors);
217                         if(centerprint_messages[i] != s) // don't fade the same message in, looks stupid
218                                 centerprint_time = time;
219                         if(centerprint_messages[i])
220                                 strunzone(centerprint_messages[i]);
221                         centerprint_messages[i] = strzone(s);
222                         centerprint_width[i] = stringwidth(s, TRUE, centerprint_fontsize);
223                         ++i;
224
225                         // half height for empty lines looks better
226                         if(s == "")
227                                 hcount += 0.5;
228                         else
229                                 hcount += 1;
230
231                         if(i >= CENTERPRINT_MAX_LINES)
232                                 break;
233                 }
234         }
235 #endif
236
237 #if 0
238         float h, havail;
239         h = centerprint_fontsize_y*hcount;
240
241         havail = vid_conheight;
242         if(autocvar_con_chatpos < 0)
243                 havail -= (-autocvar_con_chatpos + autocvar_con_chat) * autocvar_con_chatsize; // avoid overlapping chat
244         if(havail > vid_conheight - 70)
245                 havail = vid_conheight - 70; // avoid overlapping HUD
246
247         float forbiddenmin, forbiddenmax, allowedmin, allowedmax, preferred;
248
249         // here, the centerprint would cover the crosshair. REALLY BAD.
250         forbiddenmin = vid_conheight * 0.5 - h - 16;
251         forbiddenmax = vid_conheight * 0.5 + 16;
252
253         allowedmin = scoreboard_bottom;
254         allowedmax = havail - h;
255         preferred = (havail - h)/2;
256
257
258         // possible orderings (total: 4! / 4 = 6)
259         //  allowedmin allowedmax forbiddenmin forbiddenmax
260         //  forbiddenmin forbiddenmax allowedmin allowedmax
261         if(allowedmax < forbiddenmin || allowedmin > forbiddenmax)
262         {
263                 // forbidden doesn't matter in this case
264                 centerprint_start_y = bound(allowedmin, preferred, allowedmax);
265         }
266         //  allowedmin forbiddenmin allowedmax forbiddenmax
267         else if(allowedmin < forbiddenmin && allowedmax < forbiddenmax)
268         {
269                 centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
270         }
271         //  allowedmin forbiddenmin forbiddenmax allowedmax
272         else if(allowedmin < forbiddenmin)
273         {
274                 // make sure the forbidden zone is not covered
275                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
276                         centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
277                 else
278                         centerprint_start_y = bound(forbiddenmax, preferred, allowedmin);
279         }
280         //  forbiddenmin allowedmin allowedmax forbiddenmax
281         else if(allowedmax < forbiddenmax)
282         {
283                 // it's better to leave the allowed zone (overlap with scoreboard) than
284                 // to cover the forbidden zone (crosshair)
285                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
286                         centerprint_start_y = forbiddenmax;
287                 else
288                         centerprint_start_y = forbiddenmin;
289         }
290         //  forbiddenmin allowedmin forbiddenmax allowedmax
291         else
292         {
293                 centerprint_start_y = bound(forbiddenmax, preferred, allowedmax);
294         }
295 #else
296 #endif
297
298         // centerprint_num = i;
299
300         centerprint_expire = time + autocvar_scr_centertime;
301 }
302
303 void HUD_DrawCenterPrint (void)
304 {
305         float i, j, k, n;
306         vector pos;
307         string ts;
308         float a, sz, fade;
309
310         // if(centerprint_num * autocvar_scr_centersize > 24 && scoreboard_active) // 24 = height of Scoreboard text
311                 // centerprint_start_y = scoreboard_bottom + centerprint_fontsize_y;
312
313         pos = centerprint_start;
314         fade = min(autocvar_scr_centertime/8, 0.25);
315         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_LINES; ++i, ++j)
316         {
317                 if (j == CENTERPRINT_MAX_LINES)
318                         j = 0;
319                 if (centerprint_time[j] + autocvar_scr_centertime < time)
320                         continue;
321                 else if (centerprint_time[j] + autocvar_scr_centertime - fade > time)
322                 {
323                         a = 1;
324                         sz = 1;
325                 }
326                 else if (centerprint_time[j] + autocvar_scr_centertime > time)
327                 {
328                         a = (centerprint_time[j] + autocvar_scr_centertime - time) / fade;
329                         sz = 0.8 + a * (1 - 0.8);
330                 }
331
332                 drawfontscale = sz * '1 1 0';
333                 n = tokenizebyseparator(centerprint_messages[j], "\n");
334                 for(k = 0; k < n; ++k)
335                 {
336                         getWrappedLine_remaining = argv(k);
337                         while(getWrappedLine_remaining)
338                         {
339                                 ts = getWrappedLine(vid_conwidth * 0.75, centerprint_fontsize, stringwidth_colors);
340                                 if (cvar("debug"))
341                                 {
342                                         ts = strcat(ftos(i), " -", ts, "-");
343                                         print(sprintf("^x8f0ts = %s\n", ts));
344                                 }
345                                 pos_x = (vid_conwidth - stringwidth(ts, TRUE, centerprint_fontsize)) * 0.5;
346                                 if (ts != "")
347                                 {
348                                         drawcolorcodedstring(pos + '0 1 0' * (1 - sz) * 0.5 *centerprint_fontsize_y, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
349                                         pos_y = pos_y + centerprint_fontsize_y;
350                                 }
351                                 else
352                                         // half height for empty lines looks better
353                                         pos_y = pos_y + sz * centerprint_fontsize_y * 0.5;
354                         }
355                 }
356                 drawfontscale = '1 1 0';
357         }
358 }
359
360 void drawstringright(vector position, string text, vector scale, vector rgb, float alpha, float flag)
361 {
362         position_x -= 2 / 3 * strlen(text) * scale_x;
363         drawstring(position, text, scale, rgb, alpha, flag);
364 }
365
366 void drawstringcenter(vector position, string text, vector scale, vector rgb, float alpha, float flag)
367 {
368         position_x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * scale_x);
369         drawstring(position, text, scale, rgb, alpha, flag);
370 }
371
372 // return the string of the given race place
373 string race_PlaceName(float pos) {
374         if(pos == 1)
375                 return _("1st");
376         else if(pos == 2)
377                 return _("2nd");
378         else if(pos == 3)
379                 return _("3rd");
380         else
381                 return sprintf(_("%dth"), pos);
382 }
383
384 // return the string of the onscreen race timer
385 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
386 {
387         string col;
388         string timestr;
389         string cpname;
390         string lapstr;
391         lapstr = "";
392
393         if(histime == 0) // goal hit
394         {
395                 if(mytime > 0)
396                 {
397                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
398                         col = "^1";
399                 }
400                 else if(mytime == 0)
401                 {
402                         timestr = "+0.0";
403                         col = "^3";
404                 }
405                 else
406                 {
407                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
408                         col = "^2";
409                 }
410
411                 if(lapdelta > 0)
412                 {
413                         lapstr = sprintf(_(" (-%dL)"), lapdelta);
414                         col = "^2";
415                 }
416                 else if(lapdelta < 0)
417                 {
418                         lapstr = sprintf(_(" (+%dL)"), -lapdelta);
419                         col = "^1";
420                 }
421         }
422         else if(histime > 0) // anticipation
423         {
424                 if(mytime >= histime)
425                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
426                 else
427                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
428                 col = "^3";
429         }
430         else
431                 col = "^7";
432
433         if(cp == 254)
434                 cpname = _("Start line");
435         else if(cp == 255)
436                 cpname = _("Finish line");
437         else if(cp)
438                 cpname = sprintf(_("Intermediate %d"), cp);
439         else
440                 cpname = _("Finish line");
441
442         if(histime < 0)
443                 return strcat(col, cpname);
444         else if(hisname == "")
445                 return strcat(col, sprintf(_("%s (%s)"), cpname, timestr));
446         else
447                 return strcat(col, sprintf(_("%s (%s %s)"), cpname, timestr, strcat(hisname, col, lapstr)));
448 }
449
450 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
451 float race_CheckName(string net_name) {
452         float i;
453         for (i=RANKINGS_CNT-1;i>=0;--i)
454                 if(grecordholder[i] == net_name)
455                         return i+1;
456         return 0;
457 }
458
459 /*
460 ==================
461 HUD panels
462 ==================
463 */
464
465 // draw the background/borders
466 #define HUD_Panel_DrawBg(alpha)\
467 if(panel_bg != "0")\
468         draw_BorderPicture(panel_pos - '1 1 0' * panel_bg_border, panel_bg, panel_size + '1 1 0' * 2 * panel_bg_border, panel_bg_color, panel_bg_alpha * alpha, '1 1 0' * (panel_bg_border/BORDER_MULTIPLIER));\
469 if(highlightedPanel == hud_configure_active_panel && autocvar__hud_configure)\
470 {\
471         HUD_Panel_HlBorder(panel_bg_border + 1.5 * hlBorderSize, '0 0.5 1', 0.25 * (1 - autocvar__menu_alpha) * alpha);\
472 } ENDS_WITH_CURLY_BRACE
473
474 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
475 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, float vertical, float baralign, vector theColor, float theAlpha, float drawflag)
476 {
477         if(!length_ratio || !theAlpha)
478                 return;
479         if(length_ratio > 1)
480                 length_ratio = 1;
481         if (baralign == 3)
482         {
483                 if(length_ratio < -1)
484                         length_ratio = -1;
485         }
486         else if(length_ratio < 0)
487                 return;
488
489         vector square;
490         vector width, height;
491         if(vertical) {
492                 pic = strcat(hud_skin_path, "/", pic, "_vertical");
493                 if(precache_pic(pic) == "") {
494                         pic = "gfx/hud/default/progressbar_vertical";
495                 }
496
497         if (baralign == 1) // bottom align
498                         theOrigin_y += (1 - length_ratio) * theSize_y;
499         else if (baralign == 2) // center align
500             theOrigin_y += 0.5 * (1 - length_ratio) * theSize_y;
501         else if (baralign == 3) // center align, positive values down, negative up
502                 {
503                         theSize_y *= 0.5;
504                         if (length_ratio > 0)
505                                 theOrigin_y += theSize_y;
506                         else
507                         {
508                                 theOrigin_y += (1 + length_ratio) * theSize_y;
509                                 length_ratio = -length_ratio;
510                         }
511                 }
512                 theSize_y *= length_ratio;
513
514                 vector bH;
515                 width = eX * theSize_x;
516                 height = eY * theSize_y;
517                 if(theSize_y <= theSize_x * 2)
518                 {
519                         // button not high enough
520                         // draw just upper and lower part then
521                         square = eY * theSize_y * 0.5;
522                         bH = eY * (0.25 * theSize_y / (theSize_x * 2));
523                         drawsubpic(theOrigin,          square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, drawflag);
524                         drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, drawflag);
525                 }
526                 else
527                 {
528                         square = eY * theSize_x;
529                         drawsubpic(theOrigin,                   width   +     square, pic, '0 0    0', '1 0.25 0', theColor, theAlpha, drawflag);
530                         drawsubpic(theOrigin +          square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5  0', theColor, theAlpha, drawflag);
531                         drawsubpic(theOrigin + height - square, width   +     square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, drawflag);
532                 }
533         } else {
534                 pic = strcat(hud_skin_path, "/", pic);
535                 if(precache_pic(pic) == "") {
536                         pic = "gfx/hud/default/progressbar";
537                 }
538
539                 if (baralign == 1) // right align
540                         theOrigin_x += (1 - length_ratio) * theSize_x;
541         else if (baralign == 2) // center align
542             theOrigin_x += 0.5 * (1 - length_ratio) * theSize_x;
543         else if (baralign == 3) // center align, positive values on the right, negative on the left
544                 {
545                         theSize_x *= 0.5;
546                         if (length_ratio > 0)
547                                 theOrigin_x += theSize_x;
548                         else
549                         {
550                                 theOrigin_x += (1 + length_ratio) * theSize_x;
551                                 length_ratio = -length_ratio;
552                         }
553                 }
554                 theSize_x *= length_ratio;
555
556                 vector bW;
557                 width = eX * theSize_x;
558                 height = eY * theSize_y;
559                 if(theSize_x <= theSize_y * 2)
560                 {
561                         // button not wide enough
562                         // draw just left and right part then
563                         square = eX * theSize_x * 0.5;
564                         bW = eX * (0.25 * theSize_x / (theSize_y * 2));
565                         drawsubpic(theOrigin,          square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, drawflag);
566                         drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, drawflag);
567                 }
568                 else
569                 {
570                         square = eX * theSize_y;
571                         drawsubpic(theOrigin,                  height  +     square, pic, '0    0 0', '0.25 1 0', theColor, theAlpha, drawflag);
572                         drawsubpic(theOrigin +         square, theSize - 2 * square, pic, '0.25 0 0', '0.5  1 0', theColor, theAlpha, drawflag);
573                         drawsubpic(theOrigin + width - square, height  +     square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
574                 }
575         }
576 }
577
578 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float alpha, float drawflag)
579 {
580         if(!alpha)
581                 return;
582
583         string pic;
584         pic = strcat(hud_skin_path, "/num_leading");
585         if(precache_pic(pic) == "") {
586                 pic = "gfx/hud/default/num_leading";
587         }
588
589         drawsubpic(pos, eX * min(mySize_x * 0.5, mySize_y) + eY * mySize_y, pic, '0 0 0', '0.25 1 0', color, alpha, drawflag);
590         if(mySize_x/mySize_y > 2)
591                 drawsubpic(pos + eX * mySize_y, eX * (mySize_x - 2 * mySize_y) + eY * mySize_y, pic, '0.25 0 0', '0.5 1 0', color, alpha, drawflag);
592         drawsubpic(pos + eX * mySize_x - eX * min(mySize_x * 0.5, mySize_y), eX * min(mySize_x * 0.5, mySize_y) + eY * mySize_y, pic, '0.75 0 0', '0.25 1 0', color, alpha, drawflag);
593 }
594
595 // Weapon icons (#0)
596 //
597 entity weaponorder[WEP_MAXCOUNT];
598 void weaponorder_swap(float i, float j, entity pass)
599 {
600         entity h;
601         h = weaponorder[i];
602         weaponorder[i] = weaponorder[j];
603         weaponorder[j] = h;
604 }
605
606 string weaponorder_cmp_str;
607 float weaponorder_cmp(float i, float j, entity pass)
608 {
609         float ai, aj;
610         ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
611         aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
612         return aj - ai; // the string is in REVERSE order (higher prio at the right is what we want, but higher prio first is the string)
613 }
614
615 float GetAmmoStat(float i)
616 {
617         switch(i)
618         {
619                 case 0: return STAT_SHELLS;
620                 case 1: return STAT_NAILS;
621                 case 2: return STAT_ROCKETS;
622                 case 3: return STAT_CELLS;
623                 case 4: return STAT_FUEL;
624                 default: return -1;
625         }
626 }
627
628 float GetAmmoTypeForWep(float i)
629 {
630         switch(i)
631         {
632                 case WEP_SHOTGUN: return 0;
633                 case WEP_UZI: return 1;
634                 case WEP_GRENADE_LAUNCHER: return 2;
635                 case WEP_MINE_LAYER: return 2;
636                 case WEP_ELECTRO: return 3;
637                 case WEP_CRYLINK: return 3;
638                 case WEP_HLAC: return 3;
639                 case WEP_MINSTANEX: return 3;
640                 case WEP_NEX: return 3;
641                 case WEP_SNIPERRIFLE: return 1;
642                 case WEP_HAGAR: return 2;
643                 case WEP_ROCKET_LAUNCHER: return 2;
644                 case WEP_SEEKER: return 2;
645                 case WEP_FIREBALL: return 4;
646                 case WEP_HOOK: return 3;
647                 default: return -1;
648         }
649 }
650
651 void HUD_Weapons(void)
652 {
653         float f, screen_ar;
654         float center_x, center_y;
655
656         if(!autocvar__hud_configure)
657         {
658                 if(!autocvar_hud_panel_weapons) return;
659                 if(spectatee_status == -1) return;
660         }
661         else
662                 hud_configure_active_panel = HUD_PANEL_WEAPONS;
663
664         float timeout = autocvar_hud_panel_weapons_timeout;
665         float timeout_effect_length, timein_effect_length;
666         if (autocvar_hud_panel_weapons_timeout_effect == 0)
667         {
668                 timeout_effect_length = 0;
669                 timein_effect_length = 0;
670         }
671         else
672         {
673                 timeout_effect_length = 0.75;
674                 timein_effect_length = 0.375;
675         }
676
677         if (timeout && time >= weapontime + timeout + timeout_effect_length && !autocvar__hud_configure)
678         {
679                 weaponprevtime = time;
680                 return;
681         }
682
683         HUD_Panel_UpdateCvars(weapons);
684
685         if (timeout && time >= weapontime + timeout && !autocvar__hud_configure)
686         {
687                 f = (time - (weapontime + timeout)) / timeout_effect_length;
688                 if (autocvar_hud_panel_weapons_timeout_effect == 1 || autocvar_hud_panel_weapons_timeout_effect == 3)
689                 {
690                         panel_bg_alpha *= (1 - f);
691                         panel_fg_alpha *= (1 - f);
692                 }
693                 if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
694                 {
695                         f *= f; // for a cooler movement
696                         center_x = panel_pos_x + panel_size_x/2;
697                         center_y = panel_pos_y + panel_size_y/2;
698                         screen_ar = vid_conwidth/vid_conheight;
699                         if (center_x/center_y < screen_ar) //bottom left
700                         {
701                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //bottom
702                                         panel_pos_y += f * (vid_conheight - panel_pos_y);
703                                 else //left
704                                         panel_pos_x -= f * (panel_pos_x + panel_size_x);
705                         }
706                         else //top right
707                         {
708                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //right
709                                         panel_pos_x += f * (vid_conwidth - panel_pos_x);
710                                 else //top
711                                         panel_pos_y -= f * (panel_pos_y + panel_size_y);
712                         }
713                 }
714                 weaponprevtime = time - (1 - f) * timein_effect_length;
715         }
716         else if (timeout && time < weaponprevtime + timein_effect_length && !autocvar__hud_configure)
717         {
718                 f = (time - weaponprevtime) / timein_effect_length;
719                 if (autocvar_hud_panel_weapons_timeout_effect == 1 || autocvar_hud_panel_weapons_timeout_effect == 3)
720                 {
721                         panel_bg_alpha *= (f);
722                         panel_fg_alpha *= (f);
723                 }
724                 if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
725                 {
726                         f *= f; // for a cooler movement
727                         f = 1 - f;
728                         center_x = panel_pos_x + panel_size_x/2;
729                         center_y = panel_pos_y + panel_size_y/2;
730                         screen_ar = vid_conwidth/vid_conheight;
731                         if (center_x/center_y < screen_ar) //bottom left
732                         {
733                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //bottom
734                                         panel_pos_y += f * (vid_conheight - panel_pos_y);
735                                 else //left
736                                         panel_pos_x -= f * (panel_pos_x + panel_size_x);
737                         }
738                         else //top right
739                         {
740                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //right
741                                         panel_pos_x += f * (vid_conwidth - panel_pos_x);
742                                 else //top
743                                         panel_pos_y -= f * (panel_pos_y + panel_size_y);
744                         }
745                 }
746         }
747
748         float i, weapid, wpnalpha, weapon_cnt;
749
750         // TODO make this configurable
751         if(weaponorder_bypriority != autocvar_cl_weaponpriority || !weaponorder[0])
752         {
753                 if(weaponorder_bypriority)
754                         strunzone(weaponorder_bypriority);
755                 if(weaponorder_byimpulse)
756                         strunzone(weaponorder_byimpulse);
757
758                 weaponorder_bypriority = strzone(autocvar_cl_weaponpriority);
759                 weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(W_FixWeaponOrder_ForceComplete(W_NumberWeaponOrder(weaponorder_bypriority))));
760                 weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
761
762                 weapon_cnt = 0;
763                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
764                 {
765                         self = get_weaponinfo(i);
766                         if(self.impulse >= 0)
767                         {
768                                 weaponorder[weapon_cnt] = self;
769                                 ++weapon_cnt;
770                         }
771                 }
772                 for(i = weapon_cnt; i < WEP_MAXCOUNT; ++i)
773                         weaponorder[i] = NULL;
774                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
775
776                 weaponorder_cmp_str = string_null;
777         }
778
779         HUD_Panel_DrawBg(1);
780         if(panel_bg_padding)
781         {
782                 panel_pos += '1 1 0' * panel_bg_padding;
783                 panel_size -= '2 2 0' * panel_bg_padding;
784         }
785
786         if(autocvar_hud_panel_weapons_fade)
787         {
788                 wpnalpha = 3.2 - 2 * (time - weapontime);
789                 wpnalpha = bound(0.7, wpnalpha, 1) * panel_fg_alpha;
790         }
791         else
792                 wpnalpha = panel_fg_alpha;
793
794         float rows, columns;
795         float aspect = autocvar_hud_panel_weapons_aspect;
796         rows = panel_size_y/panel_size_x;
797         rows = bound(1, floor((sqrt(4 * aspect * rows * WEP_COUNT + rows * rows) + rows + 0.5) / 2), WEP_COUNT);
798
799         columns = ceil(WEP_COUNT/rows);
800         float row, column;
801
802         float a, type, fullammo;
803         float when;
804         when = autocvar_hud_panel_weapons_complainbubble_time;
805         float fadetime;
806         fadetime = autocvar_hud_panel_weapons_complainbubble_fadetime;
807
808         vector color;
809         vector wpnpos;
810         vector wpnsize;
811         
812         vector ammo_color;
813         float ammo_alpha;
814         wpnsize = eX * panel_size_x*(1/columns) + eY * panel_size_y*(1/rows);
815         float barsize_x, barsize_y, baroffset_x, baroffset_y;
816         if (autocvar_hud_panel_weapons_ammo)
817         {
818                 ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
819                 ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
820
821
822                 if(wpnsize_x/wpnsize_y > aspect)
823                 {
824                         barsize_x = aspect * wpnsize_y;
825                         barsize_y = wpnsize_y;
826                         baroffset_x = (wpnsize_x - barsize_x) / 2;
827                 }
828                 else
829                 {
830                         barsize_y = 1/aspect * wpnsize_x;
831                         barsize_x = wpnsize_x;
832                         baroffset_y = (wpnsize_y - barsize_y) / 2;
833                 }
834         }
835
836         float show_accuracy;
837         float weapon_stats;
838         if(autocvar_hud_panel_weapons_accuracy && acc_levels)
839         {
840                 show_accuracy = true;
841                 if (acc_col_x[0] == -1)
842                         for (i = 0; i < acc_levels; ++i)
843                                 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
844         }
845
846         float weapons_st = getstati(STAT_WEAPONS);
847
848         for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
849         {
850                 self = weaponorder[i];
851                 if (!self || self.impulse < 0)
852                         continue;
853                 wpnpos = panel_pos + eX * column * wpnsize_x + eY * row * wpnsize_y;
854
855                 weapid = self.impulse;
856
857                 // draw background behind currently selected weapon
858                 if(self.weapon == activeweapon)
859                         drawpic_aspect_skin(wpnpos, "weapon_current_bg", wpnsize, '1 1 1', wpnalpha, DRAWFLAG_NORMAL);
860
861                 // draw the weapon accuracy
862                 if(show_accuracy)
863                 {
864                         weapon_stats = weapon_accuracy[self.weapon-WEP_FIRST];
865                         if(weapon_stats >= 0)
866                         {
867                                 // find the max level lower than weapon_stats
868                                 float j;
869                                 j = acc_levels-1;
870                                 while ( j && weapon_stats < acc_lev[j] )
871                                         --j;
872
873                                 // inject color j+1 in color j, how much depending on how much weapon_stats is higher than level j
874                                 float factor;
875                                 factor = (weapon_stats - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
876                                 color = acc_col[j];
877                                 color = color + factor * (acc_col[j+1] - color);
878
879                                 drawpic_aspect_skin(wpnpos, "weapon_accuracy", wpnsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
880                         }
881                 }
882
883                 // draw the weapon icon
884                 if(weapons_st & self.weapons)
885                 {
886                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '1 1 1', wpnalpha, DRAWFLAG_NORMAL);
887
888                         if(autocvar_hud_panel_weapons_label == 1) // weapon number
889                                 drawstring(wpnpos, ftos(weapid), '1 1 0' * 0.5 * wpnsize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
890                         else if(autocvar_hud_panel_weapons_label == 2) // bind
891                                 drawstring(wpnpos, getcommandkey(ftos(weapid), strcat("impulse ", ftos(weapid))), '1 1 0' * 0.5 * wpnsize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
892
893                         // draw ammo status bar
894                         if(autocvar_hud_panel_weapons_ammo && self.weapon != WEP_TUBA && self.weapon != WEP_LASER && self.weapon != WEP_PORTO)
895                         {
896                                 a = 0;
897                                 type = GetAmmoTypeForWep(self.weapon);
898                                 if(type != -1)
899                                         a = getstati(GetAmmoStat(type)); // how much ammo do we have?
900
901                                 if(a > 0)
902                                 {
903                                         switch(type) {
904                                                 case 0: fullammo = autocvar_hud_panel_weapons_ammo_full_shells; break;
905                                                 case 1: fullammo = autocvar_hud_panel_weapons_ammo_full_nails; break;
906                                                 case 2: fullammo = autocvar_hud_panel_weapons_ammo_full_rockets; break;
907                                                 case 3: fullammo = autocvar_hud_panel_weapons_ammo_full_cells; break;
908                                                 case 4: fullammo = autocvar_hud_panel_weapons_ammo_full_fuel; break;
909                                                 default: fullammo = 60;
910                                         }
911
912                                         drawsetcliparea(
913                                                 wpnpos_x + baroffset_x,
914                                                 wpnpos_y + baroffset_y,
915                                                 barsize_x * bound(0, a/fullammo, 1),
916                                                 barsize_y);
917                                         drawpic_aspect_skin(wpnpos, "weapon_ammo", wpnsize, ammo_color, ammo_alpha, DRAWFLAG_NORMAL);
918                                         drawresetcliparea();
919                                 }
920                         }
921                 }
922
923                 // draw a "ghost weapon icon" if you don't have the weapon
924                 else
925                 {
926                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '0 0 0', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
927                 }
928
929                 // draw the complain message
930                 if(time - complain_weapon_time < when + fadetime && self.weapon == complain_weapon && autocvar_hud_panel_weapons_complainbubble)
931                 {
932                         if(fadetime)
933                         {
934                                 if(complain_weapon_time + when > time)
935                                         a = 1;
936                                 else
937                                         a = bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1);
938                         }
939                         else
940                         {
941                                 if(complain_weapon_time + when > time)
942                                         a = 1;
943                                 else
944                                         a = 0;
945                         }
946
947                         string s;
948                         if(complain_weapon_type == 0) {
949                                 s = _("Out of ammo");
950                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
951                         }
952                         else if(complain_weapon_type == 1) {
953                                 s = _("Don't have");
954                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
955                         }
956                         else {
957                                 s = _("Unavailable");
958                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
959                         }
960                         float padding = autocvar_hud_panel_weapons_complainbubble_padding;
961                         drawpic_aspect_skin(wpnpos + '1 1 0' * padding, "weapon_complainbubble", wpnsize - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
962                         drawstring_aspect(wpnpos + '1 1 0' * padding, s, wpnsize - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
963                 }
964
965                 ++row;
966                 if(row >= rows)
967                 {
968                         row = 0;
969                         ++column;
970                 }
971         }
972
973 }
974
975 // Ammo (#1)
976 //
977 // TODO: macro
978 float GetAmmoItemCode(float i)
979 {
980         switch(i)
981         {
982                 case 0: return IT_SHELLS;
983                 case 1: return IT_NAILS;
984                 case 2: return IT_ROCKETS;
985                 case 3: return IT_CELLS;
986                 case 4: return IT_FUEL;
987                 default: return -1;
988         }
989 }
990
991 string GetAmmoPicture(float i)
992 {
993         switch(i)
994         {
995                 case 0: return "ammo_shells";
996                 case 1: return "ammo_bullets";
997                 case 2: return "ammo_rockets";
998                 case 3: return "ammo_cells";
999                 case 4: return "ammo_fuel";
1000                 default: return "";
1001         }
1002 }
1003
1004 void DrawAmmoItem(vector myPos, vector mySize, float itemcode, float currently_selected)
1005 {
1006         float a;
1007         if(autocvar__hud_configure)
1008         {
1009                 currently_selected = (itemcode == 2); //rockets always selected
1010                 a = 31 + mod(itemcode*93, 128);
1011         }
1012         else
1013                 a = getstati(GetAmmoStat(itemcode)); // how much ammo do we have of type itemcode?
1014
1015         vector color;
1016         if(a < 10)
1017                 color = '0.7 0 0';
1018         else
1019                 color = '1 1 1';
1020
1021         float alpha;
1022         if(currently_selected)
1023                 alpha = 1;
1024         else
1025                 alpha = 0.7;
1026
1027         vector picpos, numpos;
1028         if(autocvar_hud_panel_ammo_iconalign)
1029         {
1030                 numpos = myPos;
1031                 picpos = myPos + eX * 2 * mySize_y;
1032         }
1033         else
1034         {
1035                 numpos = myPos + eX * mySize_y;
1036                 picpos = myPos;
1037         }
1038
1039         if (currently_selected)
1040                 drawpic_aspect_skin(myPos, "ammo_current_bg", mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1041
1042     if(a > 0 && autocvar_hud_panel_ammo_progressbar)
1043         HUD_Panel_DrawProgressBar(myPos + eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize_x, mySize - eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize_x, autocvar_hud_panel_ammo_progressbar_name, a/autocvar_hud_panel_ammo_maxammo, 0, 0, color, autocvar_hud_progressbar_alpha * panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1044
1045     if(autocvar_hud_panel_ammo_text)
1046     {
1047         if(a > 0)
1048             drawstring_aspect(numpos, ftos(a), eX * (2/3) * mySize_x + eY * mySize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1049         else // "ghost" ammo count
1050             drawstring_aspect(numpos, ftos(a), eX * (2/3) * mySize_x + eY * mySize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1051     }
1052         if(a > 0)
1053                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1054         else // "ghost" ammo icon
1055                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * mySize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1056 }
1057
1058 void HUD_Ammo(void)
1059 {
1060         if(!autocvar__hud_configure)
1061         {
1062                 if(!autocvar_hud_panel_ammo) return;
1063                 if(spectatee_status == -1) return;
1064         }
1065         else
1066                 hud_configure_active_panel = HUD_PANEL_AMMO;
1067
1068         HUD_Panel_UpdateCvars(ammo);
1069         vector pos, mySize;
1070         pos = panel_pos;
1071         mySize = panel_size;
1072
1073         HUD_Panel_DrawBg(1);
1074         if(panel_bg_padding)
1075         {
1076                 pos += '1 1 0' * panel_bg_padding;
1077                 mySize -= '2 2 0' * panel_bg_padding;
1078         }
1079
1080         float rows, columns, row, column;
1081         vector ammo_size;
1082         if (autocvar_hud_panel_ammo_onlycurrent)
1083                 ammo_size = mySize;
1084         else
1085         {
1086                 rows = mySize_y/mySize_x;
1087                 rows = bound(1, floor((sqrt(4 * (3/1) * rows * AMMO_COUNT + rows * rows) + rows + 0.5) / 2), AMMO_COUNT);
1088                 //                               ^^^ ammo item aspect goes here
1089
1090                 columns = ceil(AMMO_COUNT/rows);
1091
1092                 ammo_size = eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows);
1093         }
1094
1095         vector offset;
1096         float newSize;
1097         if(ammo_size_x/ammo_size_y > 3)
1098         {
1099                 newSize = 3 * ammo_size_y;
1100                 offset_x = ammo_size_x - newSize;
1101                 pos_x += offset_x/2;
1102                 ammo_size_x = newSize;
1103         }
1104         else
1105         {
1106                 newSize = 1/3 * ammo_size_x;
1107                 offset_y = ammo_size_y - newSize;
1108                 pos_y += offset_y/2;
1109                 ammo_size_y = newSize;
1110         }
1111
1112         float i, stat_items, currently_selected;
1113         if (autocvar_hud_panel_ammo_onlycurrent)
1114         {
1115                 if(autocvar__hud_configure)
1116                 {
1117                         DrawAmmoItem(pos, ammo_size, 2, true); //show rockets
1118                         return;
1119                 }
1120                 stat_items = getstati(STAT_ITEMS);
1121                 for (i = 0; i < AMMO_COUNT; ++i) {
1122                         currently_selected = stat_items & GetAmmoItemCode(i);
1123                         if (currently_selected)
1124                         {
1125                                 DrawAmmoItem(pos, ammo_size, i, true);
1126                                 return;
1127                         }
1128                 }
1129                 return; // nothing to display
1130         }
1131
1132         stat_items = getstati(STAT_ITEMS);
1133         for (i = 0; i < AMMO_COUNT; ++i) {
1134                 currently_selected = stat_items & GetAmmoItemCode(i);
1135                 DrawAmmoItem(pos + eX * column * (ammo_size_x + offset_x) + eY * row * (ammo_size_y + offset_y), ammo_size, i, currently_selected);
1136                 ++row;
1137                 if(row >= rows)
1138                 {
1139                         row = 0;
1140                         column = column + 1;
1141                 }
1142         }
1143 }
1144
1145 void DrawNumIcon(vector myPos, vector mySize, float x, string icon, float vertical, float icon_right_align, vector color, float alpha)
1146 {
1147         vector newPos, newSize;
1148         vector picpos, numpos;
1149
1150         if (vertical)
1151         {
1152                 if(mySize_y/mySize_x > 2)
1153                 {
1154                         newSize_y = 2 * mySize_x;
1155                         newSize_x = mySize_x;
1156
1157                         newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
1158                         newPos_x = myPos_x;
1159                 }
1160                 else
1161                 {
1162                         newSize_x = 1/2 * mySize_y;
1163                         newSize_y = mySize_y;
1164
1165                         newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
1166                         newPos_y = myPos_y;
1167                 }
1168
1169                 if(icon_right_align)
1170                 {
1171                         numpos = newPos;
1172                         picpos = newPos + eY * newSize_x;
1173                 }
1174                 else
1175                 {
1176                         picpos = newPos;
1177                         numpos = newPos + eY * newSize_x;
1178                 }
1179
1180                 newSize_y /= 2;
1181                 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1182                 // make number smaller than icon, it looks better
1183                 // reduce only y to draw numbers with different number of digits with the same y size
1184                 numpos_y += newSize_y * ((1 - 0.7) / 2);
1185                 newSize_y *= 0.7;
1186                 drawstring_aspect(numpos, ftos(x), newSize, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1187                 return;
1188         }
1189
1190         if(mySize_x/mySize_y > 3)
1191         {
1192                 newSize_x = 3 * mySize_y;
1193                 newSize_y = mySize_y;
1194
1195                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
1196                 newPos_y = myPos_y;
1197         }
1198         else
1199         {
1200                 newSize_y = 1/3 * mySize_x;
1201                 newSize_x = mySize_x;
1202
1203                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
1204                 newPos_x = myPos_x;
1205         }
1206
1207         if(icon_right_align) // right align
1208         {
1209                 numpos = newPos;
1210                 picpos = newPos + eX * 2 * newSize_y;
1211         }
1212         else // left align
1213         {
1214                 numpos = newPos + eX * newSize_y;
1215                 picpos = newPos;
1216         }
1217
1218         drawstring_aspect(numpos, ftos(x), '2 1 0' * newSize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1219         drawpic_aspect_skin(picpos, icon, '1 1 0' * newSize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1220 }
1221
1222 void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, float vertical, float icon_right_align, vector color, float fadelerp)
1223 {
1224         float sz;
1225         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
1226
1227         DrawNumIcon(myPos + expandingbox_resize_centered_box_offset(sz, mySize, 1), mySize * sz, x, icon, vertical, icon_right_align, color, (1 - fadelerp));
1228 }
1229
1230 // Powerups (#2)
1231 //
1232 void HUD_Powerups(void)
1233 {
1234         float strength_time, shield_time;
1235         if(!autocvar__hud_configure)
1236         {
1237                 if(!autocvar_hud_panel_powerups) return;
1238                 if(spectatee_status == -1) return;
1239                 if not(getstati(STAT_ITEMS) & (IT_STRENGTH | IT_INVINCIBLE)) return;
1240                 if (getstati(STAT_HEALTH) <= 0) return;
1241
1242                 strength_time = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
1243                 shield_time = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
1244         }
1245         else
1246         {
1247                 hud_configure_active_panel = HUD_PANEL_POWERUPS;
1248
1249                 strength_time = 15;
1250                 shield_time = 27;
1251         }
1252
1253         HUD_Panel_UpdateCvars(powerups);
1254         vector pos, mySize;
1255         pos = panel_pos;
1256         mySize = panel_size;
1257
1258         HUD_Panel_DrawBg(bound(0, max(strength_time, shield_time), 1));
1259         if(panel_bg_padding)
1260         {
1261                 pos += '1 1 0' * panel_bg_padding;
1262                 mySize -= '2 2 0' * panel_bg_padding;
1263         }
1264
1265         float panel_ar = mySize_x/mySize_y;
1266         float is_vertical = (panel_ar < 1);
1267         vector shield_offset, strength_offset;
1268         if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
1269         {
1270                 mySize_x *= 0.5;
1271                 if (autocvar_hud_panel_powerups_flip)
1272                         shield_offset_x = mySize_x;
1273                 else
1274                         strength_offset_x = mySize_x;
1275         }
1276         else
1277         {
1278                 mySize_y *= 0.5;
1279                 if (autocvar_hud_panel_powerups_flip)
1280                         shield_offset_y = mySize_y;
1281                 else
1282                         strength_offset_y = mySize_y;
1283         }
1284
1285         float shield_baralign, strength_baralign;
1286         float shield_iconalign, strength_iconalign;
1287         if (autocvar_hud_panel_powerups_flip)
1288         {
1289                 strength_baralign = (autocvar_hud_panel_powerups_baralign == 2 || autocvar_hud_panel_powerups_baralign == 1);
1290                 shield_baralign = (autocvar_hud_panel_powerups_baralign == 3 || autocvar_hud_panel_powerups_baralign == 1);
1291                 strength_iconalign = (autocvar_hud_panel_powerups_iconalign == 2 || autocvar_hud_panel_powerups_iconalign == 1);
1292                 shield_iconalign = (autocvar_hud_panel_powerups_iconalign == 3 || autocvar_hud_panel_powerups_iconalign == 1);
1293         }
1294         else
1295         {
1296                 shield_baralign = (autocvar_hud_panel_powerups_baralign == 2 || autocvar_hud_panel_powerups_baralign == 1);
1297                 strength_baralign = (autocvar_hud_panel_powerups_baralign == 3 || autocvar_hud_panel_powerups_baralign == 1);
1298                 shield_iconalign = (autocvar_hud_panel_powerups_iconalign == 2 || autocvar_hud_panel_powerups_iconalign == 1);
1299                 strength_iconalign = (autocvar_hud_panel_powerups_iconalign == 3 || autocvar_hud_panel_powerups_iconalign == 1);
1300         }
1301
1302         if(shield_time)
1303         {
1304                 const float maxshield = 30;
1305                 float shield = ceil(shield_time);
1306                 if(autocvar_hud_panel_powerups_progressbar)
1307                 {
1308                         HUD_Panel_GetProgressBarColor(shield);
1309                         HUD_Panel_DrawProgressBar(pos + shield_offset, mySize, autocvar_hud_panel_powerups_progressbar_shield, shield/maxshield, is_vertical, shield_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1310                 }
1311                 if(autocvar_hud_panel_powerups_text)
1312                 {
1313                         if(shield > 1)
1314                                 DrawNumIcon(pos + shield_offset, mySize, shield, "shield", is_vertical, shield_iconalign, '1 1 1', 1);
1315                         if(shield <= 5)
1316                                 DrawNumIcon_expanding(pos + shield_offset, mySize, shield, "shield", is_vertical, shield_iconalign, '1 1 1', bound(0, (shield - shield_time) / 0.5, 1));
1317                 }
1318         }
1319
1320         if(strength_time)
1321         {
1322                 const float maxstrength = 30;
1323                 float strength = ceil(strength_time);
1324                 if(autocvar_hud_panel_powerups_progressbar)
1325                 {
1326                         HUD_Panel_GetProgressBarColor(strength);
1327                         HUD_Panel_DrawProgressBar(pos + strength_offset, mySize, autocvar_hud_panel_powerups_progressbar_strength, strength/maxstrength, is_vertical, strength_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1328                 }
1329                 if(autocvar_hud_panel_powerups_text)
1330                 {
1331                         if(strength > 1)
1332                                 DrawNumIcon(pos + strength_offset, mySize, strength, "strength", is_vertical, strength_iconalign, '1 1 1', 1);
1333                         if(strength <= 5)
1334                                 DrawNumIcon_expanding(pos + strength_offset, mySize, strength, "strength", is_vertical, strength_iconalign, '1 1 1', bound(0, (strength - strength_time) / 0.5, 1));
1335                 }
1336         }
1337 }
1338
1339 // Health/armor (#3)
1340 //
1341
1342 // prev_* vars contain the health/armor at the previous FRAME
1343 // set to -1 when player is dead or was not playing
1344 float prev_health, prev_armor;
1345 float health_damagetime, armor_damagetime;
1346 float health_beforedamage, armor_beforedamage;
1347 // old_p_* vars keep track of previous values when smoothing value changes of the progressbar
1348 float old_p_health, old_p_armor;
1349 float old_p_healthtime, old_p_armortime;
1350 // prev_p_* vars contain the health/armor progressbar value at the previous FRAME
1351 // set to -1 to forcedly stop effects when we switch spectated player (e.g. from playerX: 70h to playerY: 50h)
1352 float prev_p_health, prev_p_armor;
1353
1354 void HUD_HealthArmor(void)
1355 {
1356         float armor, health, fuel;
1357         if(!autocvar__hud_configure)
1358         {
1359                 if(!autocvar_hud_panel_healtharmor) return;
1360                 if(spectatee_status == -1) return;
1361
1362                 health = getstati(STAT_HEALTH);
1363                 if(health <= 0)
1364                 {
1365                         prev_health = -1;
1366                         return;
1367                 }
1368                 armor = getstati(STAT_ARMOR);
1369
1370                 // code to check for spectatee_status changes is in Ent_ClientData()
1371                 // prev_p_health and prev_health can be set to -1 there
1372
1373                 if (prev_p_health == -1)
1374                 {
1375                         // no effect
1376                         health_beforedamage = 0;
1377                         armor_beforedamage = 0;
1378                         health_damagetime = 0;
1379                         armor_damagetime = 0;
1380                         prev_health = health;
1381                         prev_armor = armor;
1382                         old_p_health = health;
1383                         old_p_armor = armor;
1384                         prev_p_health = health;
1385                         prev_p_armor = armor;
1386                 }
1387                 else if (prev_health == -1)
1388                 {
1389                         //start the load effect
1390                         health_damagetime = 0;
1391                         armor_damagetime = 0;
1392                         prev_health = 0;
1393                         prev_armor = 0;
1394                 }
1395                 fuel = getstati(STAT_FUEL);
1396         }
1397         else
1398         {
1399                 hud_configure_active_panel = HUD_PANEL_HEALTHARMOR;
1400
1401                 health = 150;
1402                 armor = 75;
1403                 fuel = 20;
1404         }
1405
1406         HUD_Panel_UpdateCvars(healtharmor);
1407         vector pos, mySize;
1408         pos = panel_pos;
1409         mySize = panel_size;
1410
1411         HUD_Panel_DrawBg(1);
1412         if(panel_bg_padding)
1413         {
1414                 pos += '1 1 0' * panel_bg_padding;
1415                 mySize -= '2 2 0' * panel_bg_padding;
1416         }
1417
1418         float baralign = autocvar_hud_panel_healtharmor_baralign;
1419         float iconalign = autocvar_hud_panel_healtharmor_iconalign;
1420
1421     float maxhealth = autocvar_hud_panel_healtharmor_maxhealth;
1422     float maxarmor = autocvar_hud_panel_healtharmor_maxarmor;
1423         if(autocvar_hud_panel_healtharmor == 2) // combined health and armor display
1424         {
1425                 vector v;
1426                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
1427
1428                 float x;
1429                 x = floor(v_x + 1);
1430
1431         float maxtotal = maxhealth + maxarmor;
1432                 string biggercount;
1433                 if(v_z) // NOT fully armored
1434                 {
1435                         biggercount = "health";
1436                         if(autocvar_hud_panel_healtharmor_progressbar)
1437                         {
1438                                 HUD_Panel_GetProgressBarColor(health);
1439                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_health, x/maxtotal, 0, (baralign == 1 || baralign == 2), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1440                         }
1441                         if(armor)
1442             if(autocvar_hud_panel_healtharmor_text)
1443                                 drawpic_aspect_skin(pos + eX * mySize_x - eX * 0.5 * mySize_y, "armor", '0.5 0.5 0' * mySize_y, '1 1 1', panel_fg_alpha * armor / health, DRAWFLAG_NORMAL);
1444                 }
1445                 else
1446                 {
1447                         biggercount = "armor";
1448                         if(autocvar_hud_panel_healtharmor_progressbar)
1449                         {
1450                                 HUD_Panel_GetProgressBarColor(armor);
1451                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, x/maxtotal, 0, (baralign == 1 || baralign == 2), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1452                         }
1453                         if(health)
1454             if(autocvar_hud_panel_healtharmor_text)
1455                                 drawpic_aspect_skin(pos + eX * mySize_x - eX * 0.5 * mySize_y, "health", '0.5 0.5 0' * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1456                 }
1457         if(autocvar_hud_panel_healtharmor_text)
1458                         DrawNumIcon(pos, mySize, x, biggercount, 0, iconalign, HUD_Get_Num_Color(x, maxtotal), 1);
1459
1460                 if(fuel)
1461                 {
1462                         HUD_Panel_GetProgressBarColor(fuel);
1463                         HUD_Panel_DrawProgressBar(pos, eX * mySize_x + eY * 0.2 * mySize_y, "progressbar", fuel/100, 0, (baralign == 1 || baralign == 3), progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
1464                 }
1465         }
1466         else
1467         {
1468                 float panel_ar = mySize_x/mySize_y;
1469                 float is_vertical = (panel_ar < 1);
1470                 vector health_offset, armor_offset;
1471                 if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
1472                 {
1473                         mySize_x *= 0.5;
1474                         if (autocvar_hud_panel_healtharmor_flip)
1475                                 health_offset_x = mySize_x;
1476                         else
1477                                 armor_offset_x = mySize_x;
1478                 }
1479                 else
1480                 {
1481                         mySize_y *= 0.5;
1482                         if (autocvar_hud_panel_healtharmor_flip)
1483                                 health_offset_y = mySize_y;
1484                         else
1485                                 armor_offset_y = mySize_y;
1486                 }
1487
1488                 float health_baralign, armor_baralign, fuel_baralign;
1489                 float health_iconalign, armor_iconalign;
1490                 if (autocvar_hud_panel_healtharmor_flip)
1491                 {
1492                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
1493                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
1494                         fuel_baralign = health_baralign;
1495                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
1496                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
1497                 }
1498                 else
1499                 {
1500                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
1501                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
1502                         fuel_baralign = armor_baralign;
1503                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
1504                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
1505                 }
1506
1507                 //if(health)
1508                 {
1509                         if(autocvar_hud_panel_healtharmor_progressbar)
1510                         {
1511                                 HUD_Panel_GetProgressBarColor(health);
1512                                 float p_health, pain_health_alpha;
1513                                 p_health = health;
1514                                 pain_health_alpha = 1;
1515                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
1516                                 {
1517                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
1518                                         {
1519                                                 if (fabs(prev_health - health) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
1520                                                 {
1521                                                         if (time - old_p_healthtime < 1)
1522                                                                 old_p_health = prev_p_health;
1523                                                         else
1524                                                                 old_p_health = prev_health;
1525                                                         old_p_healthtime = time;
1526                                                 }
1527                                                 if (time - old_p_healthtime < 1)
1528                                                 {
1529                                                         p_health += (old_p_health - health) * (1 - (time - old_p_healthtime));
1530                                                         prev_p_health = p_health;
1531                                                 }
1532                                         }
1533                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
1534                                         {
1535                                                 if (prev_health - health >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
1536                                                 {
1537                                                         if (time - health_damagetime >= 1)
1538                                                                 health_beforedamage = prev_health;
1539                                                         health_damagetime = time;
1540                                                 }
1541                                                 if (time - health_damagetime < 1)
1542                                                 {
1543                                                         float health_damagealpha = 1 - (time - health_damagetime)*(time - health_damagetime);
1544                                                         HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, health_beforedamage/maxhealth, is_vertical, health_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * health_damagealpha, DRAWFLAG_NORMAL);
1545                                                 }
1546                                         }
1547                                         prev_health = health;
1548
1549                                         if (health <= autocvar_hud_panel_healtharmor_progressbar_gfx_lowhealth)
1550                                         {
1551                                                 float BLINK_FACTOR = 0.15;
1552                                                 float BLINK_BASE = 0.85;
1553                                                 float BLINK_FREQ = 9; 
1554                                                 pain_health_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
1555                                         }
1556                                 }
1557                                 HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, p_health/maxhealth, is_vertical, health_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * pain_health_alpha, DRAWFLAG_NORMAL);
1558                         }
1559                         if(autocvar_hud_panel_healtharmor_text)
1560                                 DrawNumIcon(pos + health_offset, mySize, health, "health", is_vertical, health_iconalign, HUD_Get_Num_Color(health, maxhealth), 1);
1561                 }
1562
1563                 if(armor)
1564                 {
1565                         if(autocvar_hud_panel_healtharmor_progressbar)
1566                         {
1567                                 HUD_Panel_GetProgressBarColor(armor);
1568                                 float p_armor;
1569                                 p_armor = armor;
1570                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
1571                                 {
1572                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
1573                                         {
1574                                                 if (fabs(prev_armor - armor) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
1575                                                 {
1576                                                         if (time - old_p_armortime < 1)
1577                                                                 old_p_armor = prev_p_armor;
1578                                                         else
1579                                                                 old_p_armor = prev_armor;
1580                                                         old_p_armortime = time;
1581                                                 }
1582                                                 if (time - old_p_armortime < 1)
1583                                                 {
1584                                                         p_armor += (old_p_armor - armor) * (1 - (time - old_p_armortime));
1585                                                         prev_p_armor = p_armor;
1586                                                 }
1587                                         }
1588                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
1589                                         {
1590                                                 if (prev_armor - armor >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
1591                                                 {
1592                                                         if (time - armor_damagetime >= 1)
1593                                                                 armor_beforedamage = prev_armor;
1594                                                         armor_damagetime = time;
1595                                                 }
1596                                                 if (time - armor_damagetime < 1)
1597                                                 {
1598                                                         float armor_damagealpha = 1 - (time - armor_damagetime)*(time - armor_damagetime);
1599                                                         HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, armor_beforedamage/maxarmor, is_vertical, armor_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * armor_damagealpha, DRAWFLAG_NORMAL);
1600                                                 }
1601                                         }
1602                                         prev_armor = armor;
1603                                 }
1604                                 HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, p_armor/maxarmor, is_vertical, armor_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1605                         }
1606                         if(autocvar_hud_panel_healtharmor_text)
1607                                 DrawNumIcon(pos + armor_offset, mySize, armor, "armor", is_vertical, armor_iconalign, HUD_Get_Num_Color(armor, maxarmor), 1);
1608                 }
1609
1610                 if(fuel)
1611                 {
1612                         if (is_vertical)
1613                                 mySize_x *= 0.2 / 2; //if vertical always halve x to not cover too much numbers with 3 digits
1614                         else
1615                                 mySize_y *= 0.2;
1616                         if (panel_ar >= 4)
1617                                 mySize_x *= 2; //restore full panel size
1618                         else if (panel_ar < 1/4)
1619                                 mySize_y *= 2; //restore full panel size
1620                         HUD_Panel_GetProgressBarColor(fuel);
1621                         HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", fuel/100, is_vertical, fuel_baralign, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
1622                 }
1623         }
1624 }
1625
1626 // Notification area (#4)
1627 //
1628
1629 string Weapon_SuicideMessage(float deathtype)
1630 {
1631         w_deathtype = deathtype;
1632         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_SUICIDEMESSAGE);
1633         return w_deathtypestring;
1634 }
1635
1636 string Weapon_KillMessage(float deathtype)
1637 {
1638         w_deathtype = deathtype;
1639         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_KILLMESSAGE);
1640         return w_deathtypestring;
1641 }
1642
1643 #define KN_MAX_ENTRIES 10
1644 float kn_index;
1645 float killnotify_times[KN_MAX_ENTRIES];
1646 float killnotify_deathtype[KN_MAX_ENTRIES];
1647 float killnotify_actiontype[KN_MAX_ENTRIES]; // 0 = "Y [used by] X", 1 = "X [did action to] Y"
1648 string killnotify_attackers[KN_MAX_ENTRIES];
1649 string killnotify_victims[KN_MAX_ENTRIES];
1650 void HUD_KillNotify_Push(string attacker, string victim, float actiontype, float wpn)
1651 {
1652         --kn_index;
1653         if (kn_index == -1)
1654                 kn_index = KN_MAX_ENTRIES-1;
1655         killnotify_times[kn_index] = time;
1656         killnotify_deathtype[kn_index] = wpn;
1657         killnotify_actiontype[kn_index] = actiontype;
1658         if(killnotify_attackers[kn_index])
1659                 strunzone(killnotify_attackers[kn_index]);
1660         killnotify_attackers[kn_index] = strzone(attacker);
1661         if(killnotify_victims[kn_index])
1662                 strunzone(killnotify_victims[kn_index]);
1663         killnotify_victims[kn_index] = strzone(victim);
1664 }
1665
1666 void HUD_KillNotify(string s1, string s2, string s3, float type, float msg) // s1 = attacker, s2 = victim
1667 {
1668         float w;
1669         float alsoprint, gentle;
1670         alsoprint = (autocvar_hud_panel_notify_print || !panel_enabled); // print message to console if: notify panel disabled, or cvar to do so enabled
1671         gentle = (autocvar_cl_gentle || autocvar_cl_gentle_messages);
1672         
1673         if ((msg == MSG_SUICIDE || msg == MSG_KILL || msg == MSG_KILL_ACTION) && gametype == GAME_CTS) // selfkill isn't interesting in CTS and only spams up the notify panel
1674                 return;
1675
1676         if(msg == MSG_SUICIDE) {
1677                 w = DEATH_WEAPONOF(type);
1678                 if(WEP_VALID(w)) {
1679                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1680                         if (alsoprint)
1681                                 print("^1", sprintf(Weapon_SuicideMessage(type), strcat(s1, "^1")), "\n");
1682                 } else if (type == DEATH_KILL) {
1683                         HUD_KillNotify_Push(s1, "", 0, DEATH_KILL);
1684                         if (alsoprint)
1685                                 print (sprintf(_("^1%s^1 couldn't take it anymore\n"), s1));
1686                 } else if (type == DEATH_ROT) {
1687                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1688                         if (alsoprint)
1689                                 print (sprintf(_("^1%s^1 died\n"), s1));
1690                 } else if (type == DEATH_NOAMMO) {
1691                         HUD_KillNotify_Push(s1, "", 0, DEATH_NOAMMO);
1692                         if (alsoprint)
1693                                 print (sprintf(_("^7%s^7 committed suicide. What's the point of living without ammo?\n"), s1));
1694                 } else if (type == DEATH_CAMP) {
1695                         HUD_KillNotify_Push(s1, "", 0, DEATH_CAMP);
1696                         if (alsoprint)
1697                                 print (sprintf(_("^1%s^1 thought they found a nice camping ground\n"), s1));
1698                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
1699                         HUD_KillNotify_Push(s1, "", 0, type);
1700                         if (alsoprint)
1701                                 print (sprintf(_("^1%s^1 didn't become friends with the Lord of Teamplay\n"), s1));
1702                 } else if (type == DEATH_CHEAT) {
1703                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1704                         if (alsoprint)
1705                                 print (sprintf(_("^1%s^1 unfairly eliminated themself\n"), s1));
1706                 } else if (type == DEATH_FIRE) {
1707                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1708                         if (alsoprint)
1709                                 print (sprintf(_("^1%s^1 burned to death\n"), s1));
1710                 } else if (type != DEATH_TEAMCHANGE && type != DEATH_QUIET) {
1711                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1712                         if (alsoprint)
1713                                 print (sprintf(_("^1%s^1 couldn't resist the urge to self-destruct\n"), s1));
1714                 } 
1715                 
1716                 if (stof(s2) > 2) // killcount > 2
1717                         print (sprintf(_("^1%s^1 ended it all after a %d kill spree\n"), s1, stof(s2)));
1718         } else if(msg == MSG_KILL) {
1719                 w = DEATH_WEAPONOF(type);
1720                 if(WEP_VALID(w)) {
1721                         if((w == WEP_SNIPERRIFLE || w == WEP_MINSTANEX) && type & HITTYPE_HEADSHOT) // all headshot weapons go here
1722                                 HUD_KillNotify_Push(s1, s2, 1, DEATH_HEADSHOT);
1723                         else
1724                                 HUD_KillNotify_Push(s1, s2, 1, type);
1725
1726                         if (alsoprint)
1727                                 print("^1", sprintf(Weapon_KillMessage(type), strcat(s2, "^1"), strcat(s1, "^1")), "\n"); // default order: victim, killer
1728                 }
1729                 else if(type == KILL_TEAM_RED || type == KILL_TEAM_BLUE || type == KILL_TEAM_SPREE) {
1730                         HUD_KillNotify_Push(s1, s2, 1, type);
1731                         if(alsoprint)
1732                         {
1733                                 if(gentle) {
1734                                         print (sprintf(_("^1%s^1 took action against a team mate\n"), s1));
1735                                 } else {
1736                                         print (sprintf(_("^1%s^1 mows down a team mate\n"), s1));
1737                                 }
1738                         }
1739                         if (stof(s2) > 2 && type == KILL_TEAM_SPREE) {
1740                                 if(gentle)
1741                                         print (sprintf(_("^1%s^1 ended a %d scoring spree by going against a team mate\n"), s1, stof(s3)));
1742                                 else
1743                                         print (sprintf(_("^1%s^1 ended a %d kill spree by killing a team mate\n"), s1, stof(s3)));
1744                         }
1745                         else if (stof(s2) > 2) {
1746                                 if(gentle)
1747                                         print (sprintf(_("^1%s^1's %s scoring spree was ended by a team mate!\n"), s1, stof(s3)));
1748                                 else
1749                                         print (sprintf(_("^1%s^1's %s kill spree was ended by a team mate!\n"), s1, stof(s3)));
1750                         }
1751                 }
1752                 else if(type == KILL_FIRST_BLOOD)
1753                         print(sprintf(_("^1%s^1 drew first blood\n"), s1));
1754                 else if (type == DEATH_TELEFRAG) {
1755                         HUD_KillNotify_Push(s1, s2, 1, DEATH_TELEFRAG);
1756                         if(gentle)
1757                                 print (sprintf(_("^1%s^1 tried to occupy %s^1's teleport destination space\n"), s2, s1));
1758                         else
1759                                 print (sprintf(_("^1%s^1 was telefragged by %s\n"), s2, s1));
1760                 }
1761                 else if (type == DEATH_DROWN) {
1762                         HUD_KillNotify_Push(s1, s2, 1, DEATH_DROWN);
1763                         if(alsoprint)
1764                                 print (sprintf(_("^1%s^1 was drowned by %s\n"), s2, s1));
1765                 }
1766                 else if (type == DEATH_SLIME) {
1767                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SLIME);
1768                         if(alsoprint)
1769                                 print (sprintf(_("^1%s^1 was slimed by %s\n"), s2, s1));
1770                 }
1771                 else if (type == DEATH_LAVA) {
1772                         HUD_KillNotify_Push(s1, s2, 1, DEATH_LAVA);
1773                         if(alsoprint)
1774                                 print (sprintf(_("^1%s^1 was cooked by %s\n"), s2, s1));
1775                 }
1776                 else if (type == DEATH_FALL) {
1777                         HUD_KillNotify_Push(s1, s2, 1, DEATH_FALL);
1778                         if(alsoprint)
1779                                 print (sprintf(_("^1%s^1 was grounded by %s\n"), s2, s1));
1780                 }
1781                 else if (type == DEATH_SHOOTING_STAR) {
1782                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SHOOTING_STAR);
1783                         if(alsoprint)
1784                                 print (sprintf(_("^1%s^1 was shot into space by %s\n"), s2, s1));
1785                 }
1786                 else if (type == DEATH_SWAMP) {
1787                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1788                         if(alsoprint)
1789                                 print (sprintf(_("^1%s^1 was conserved by %s\n"), s2, s1));
1790                 }
1791                 else if (type == DEATH_HURTTRIGGER)
1792                 {
1793                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
1794                         if(alsoprint)
1795                                 print(sprintf(_("^1%s^1 was thrown into a world of hurt by %s\n"), s2, s1));
1796                 } else if(type == DEATH_SBCRUSH) {
1797                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1798                         if(alsoprint)
1799                                 print (sprintf(_("^1%s^1 was crushed by %s\n"), s2, s1));
1800                 } else if(type == DEATH_SBMINIGUN) {
1801                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1802                         if(alsoprint)
1803                                 print (sprintf(_("^1%s^1 got shredded by %s\n"), s2, s1));
1804                 } else if(type == DEATH_SBROCKET) {
1805                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1806                         if(alsoprint)
1807                                 print (sprintf(_("^1%s^1 was blasted to bits by %s\n"), s2, s1));
1808                 } else if(type == DEATH_SBBLOWUP) {
1809                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1810                         if(alsoprint)
1811                                 print (sprintf(_("^1%s^1 got caught in the destruction of %s^1's vehicle\n"), s2, s1));
1812                 } else if(type == DEATH_WAKIGUN) {
1813                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1814                         if(alsoprint)
1815                                 print (sprintf(_("^1%s^1 was bolted down by %s\n"), s2, s1));
1816                 } else if(type == DEATH_WAKIROCKET) {
1817                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1818                         if(alsoprint)
1819                                 print (sprintf(_("^1%s^1 could find no shelter from %s^1's rockets\n"), s2, s1));
1820                 } else if(type == DEATH_WAKIBLOWUP) {
1821                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1822                         if(alsoprint)
1823                                 print (sprintf(_("^1%s^1 dies when %s^1's wakizashi dies.\n"), s2, s1));
1824                 } else if(type == DEATH_TURRET) {
1825                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1826                         if(alsoprint)
1827                                 print (sprintf(_("^1%s^1 was pushed into the line of fire by %s\n"), s2, s1));
1828                 } else if(type == DEATH_TOUCHEXPLODE) {
1829                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1830                         if(alsoprint)
1831                                 print (sprintf(_("^1%s^1 was pushed into an accident by %s\n"), s2, s1));
1832                 } else if(type == DEATH_CHEAT) {
1833                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1834                         if(alsoprint)
1835                                 print (sprintf(_("^1%s^1 was unfairly eliminated by %s\n"), s2, s1));
1836                 } else if (type == DEATH_FIRE) {
1837                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1838                         if(alsoprint)
1839                                 print (sprintf(_("^1%s^1 was burnt to death by %s\n"), s2, s1));
1840                 } else if (type == DEATH_CUSTOM) {
1841                         HUD_KillNotify_Push(s1, s2, 1, DEATH_CUSTOM);
1842                         if(alsoprint)
1843                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
1844                 } else if (type == DEATH_HURTTRIGGER) {
1845                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
1846                         if(alsoprint)
1847                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
1848                 } else {
1849                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
1850                         if(alsoprint)
1851                                 print (sprintf(_("^1%s^1 was fragged by %s\n"), s2, s1));
1852                 }
1853         } else if(msg == MSG_SPREE) {
1854                 if(type == KILL_END_SPREE) {
1855                         if(gentle)
1856                                 print (sprintf(_("^1%s^1's %s scoring spree was ended by %s\n"), s1, s2, s3));
1857                         else
1858                                 print (sprintf(_("^1%s^1's %s kill spree was ended by %s\n"), s1, s2, s3));
1859                 } else if(type == KILL_SPREE) {
1860                         if(gentle)
1861                                 print (sprintf(_("^1%s^1 made %s scores in a row\n"), s1, s2));
1862                         else
1863                                 print (sprintf(_("^1%s^1 has %s frags in a row\n"), s1, s2));
1864                 } else if(type == KILL_SPREE_3) {
1865                         if(gentle)
1866                                 print (sprintf(_("%s^7 made a ^1TRIPLE SCORE\n"), s1));
1867                         else
1868                                 print (sprintf(_("%s^7 made a ^1TRIPLE FRAG\n"), s1));
1869                 } else if(type == KILL_SPREE_5) {
1870                         if(gentle)
1871                                 print (sprintf(_("%s^7 unleashes ^1SCORING RAGE\n"), s1));
1872                         else
1873                                 print (sprintf(_("%s^7 unleashes ^1RAGE\n"), s1));
1874                 } else if(type == KILL_SPREE_10) {
1875                         if(gentle)
1876                                 print (sprintf(_("%s^7 made ^1TEN SCORES IN A ROW!\n"), s1));
1877                         else
1878                                 print (sprintf(_("%s^7 starts the ^1MASSACRE!\n"), s1));
1879                 } else if(type == KILL_SPREE_15) {
1880                         if(gentle)
1881                                 print (sprintf(_("%s^7 made ^1FIFTEEN SCORES IN A ROW!\n"), s1));
1882                         else
1883                                 print (sprintf(_("%s^7 executes ^1MAYHEM!\n"), s1));
1884                 } else if(type == KILL_SPREE_20) {
1885                         if(gentle)
1886                                 print (sprintf(_("%s^7 made ^1TWENTY SCORES IN A ROW!\n"), s1));
1887                         else
1888                                 print (sprintf(_("%s^7 is a ^1BERSERKER!\n"), s1));
1889                 } else if(type == KILL_SPREE_25) {
1890                         if(gentle)
1891                                 print (sprintf(_("%s^7 made ^1TWENTY FIVE SCORES IN A ROW!\n"), s1));
1892                         else
1893                                 print (sprintf(_("%s^7 inflicts ^1CARNAGE!\n"), s1));
1894                 } else if(type == KILL_SPREE_30) {
1895                         if(gentle)
1896                                 print (sprintf(_("%s^7 made ^1THIRTY SCORES IN A ROW!\n"), s1));
1897                         else
1898                                 print (sprintf(_("%s^7 unleashes ^1ARMAGEDDON!\n"), s1));
1899                 }
1900         } else if(msg == MSG_KILL_ACTION) { // wtf is this? isnt it basically the same as MSG_SUICIDE?
1901                 if (type == DEATH_DROWN) {
1902                         HUD_KillNotify_Push(s1, "", 0, DEATH_DROWN);
1903                         if(alsoprint)
1904                         {
1905                                 if(gentle)
1906                                         print (sprintf(_("^1%s^1 was in the water for too long\n"), s1));
1907                                 else
1908                                         print (sprintf(_("^1%s^1 drowned\n"), s1));
1909                         }
1910                 } else if (type == DEATH_SLIME) {
1911                         HUD_KillNotify_Push(s1, "", 0, DEATH_SLIME);
1912                         if(alsoprint)
1913                                 print (sprintf(_("^1%s^1 was slimed\n"), s1));
1914                 } else if (type == DEATH_LAVA) {
1915                         HUD_KillNotify_Push(s1, "", 0, DEATH_LAVA);
1916                         if(alsoprint)
1917                         {
1918                                 if(gentle)
1919                                         print (sprintf(_("^1%s^1 found a hot place\n"), s1));
1920                                 else
1921                                         print (sprintf(_("^1%s^1 turned into hot slag\n"), s1));
1922                         }
1923                 } else if (type == DEATH_FALL) {
1924                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1925                         if(alsoprint)
1926                         {
1927                                 if(gentle)
1928                                         print (sprintf(_("^1%s^1 tested gravity (and it worked)\n"), s1));
1929                                 else
1930                                         print (sprintf(_("^1%s^1 hit the ground with a crunch\n"), s1));
1931                         }
1932                 } else if (type == DEATH_SHOOTING_STAR) {
1933                         HUD_KillNotify_Push(s1, "", 0, DEATH_SHOOTING_STAR);
1934                         if(alsoprint)
1935                                 print (sprintf(_("^1%s^1 became a shooting star\n"), s1));
1936                 } else if (type == DEATH_SWAMP) {
1937                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1938                         if(alsoprint)
1939                         {
1940                                 if(gentle)
1941                                         print (sprintf(_("^1%s^1 discovered a swamp\n"), s1));
1942                                 else
1943                                         print (sprintf(_("^1%s^1 is now conserved for centuries to come\n"), s1));
1944                         }
1945                 } else if(type == DEATH_TURRET) {
1946                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1947                         if(alsoprint)
1948                                 print (sprintf(_("^1%s^1 was mowed down by a turret \n"), s1));
1949                 } else if (type == DEATH_CUSTOM) {
1950                         HUD_KillNotify_Push(s1, "", 0, DEATH_CUSTOM);
1951                         if(alsoprint)
1952                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
1953                 } else if (type == DEATH_HURTTRIGGER) {
1954                         HUD_KillNotify_Push(s1, "", 0, DEATH_HURTTRIGGER);
1955                         if(alsoprint)
1956                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
1957                 } else if(type == DEATH_TOUCHEXPLODE) {
1958                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1959                         if(alsoprint)
1960                                 print (sprintf(_("^1%s^1 died in an accident\n"), s1));
1961                 } else if(type == DEATH_CHEAT) {
1962                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1963                         if(alsoprint)
1964                                 print (sprintf(_("^1%s^1 was unfairly eliminated\n"), s1));
1965                 } else if(type == DEATH_FIRE) {
1966                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1967                         if(alsoprint)
1968                         {
1969                                 if(gentle)
1970                                         print (sprintf(_("^1%s^1 felt a little hot\n"), s1));
1971                                 else
1972                                         print (sprintf(_("^1%s^1 burnt to death\n"), s1));
1973                                 }
1974                 } else {
1975                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
1976                         if(alsoprint)
1977                         {
1978                                 if(gentle)
1979                                         print (sprintf(_("^1%s^1 needs a restart\n"), s1));
1980                                 else
1981                                         print (sprintf(_("^1%s^1 died\n"), s1));
1982                         }
1983                 }
1984         } else if(msg == MSG_KILL_ACTION_SPREE) {
1985                 if(gentle)
1986                         print (sprintf(_("^1%s^1 needs a restart after a %d scoring spree\n"), s1, stof(s2)));
1987                 else
1988                         print (sprintf(_("^1%s^1 died with a %d kill spree\n"), s1, stof(s2)));
1989         } else if(msg == MSG_INFO) {
1990                 if(type == INFO_GOTFLAG) { // here, s2 is the flag name
1991                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
1992                         print(sprintf(_("%s^7 got the %s\n"), s1, s2));
1993                 } else if(type == INFO_LOSTFLAG) {
1994                         HUD_KillNotify_Push(s1, s2, 0, INFO_LOSTFLAG);
1995                         print(sprintf(_("%s^7 lost the %s\n"), s1, s2));
1996                 } else if(type == INFO_PICKUPFLAG) {
1997                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
1998                         print(sprintf(_("%s^7 picked up the %s\n"), s1, s2));
1999                 } else if(type == INFO_RETURNFLAG) {
2000                         HUD_KillNotify_Push(s1, s2, 0, INFO_RETURNFLAG);
2001                         print(sprintf(_("%s^7 returned the %s\n"), s1, s2));
2002                 } else if(type == INFO_CAPTUREFLAG) {
2003                         HUD_KillNotify_Push(s1, s2, 0, INFO_CAPTUREFLAG);
2004                         print(sprintf(_("%s^7 captured the %s%s\n"), s1, s2, s3));
2005                 }
2006         } else if(msg == MSG_RACE) {
2007                 if(type == RACE_SERVER_RECORD) {
2008                         HUD_KillNotify_Push(s1, s2, 1, RACE_SERVER_RECORD);
2009                 }
2010                 else if(type == RACE_NEW_RANK) {
2011                         HUD_KillNotify_Push(s1, s2, 1, RACE_NEW_RANK);
2012                 }
2013                 else if(type == RACE_NEW_TIME) {
2014                         HUD_KillNotify_Push(s1, s2, 1, RACE_NEW_TIME);
2015                 }
2016                 else if(type == RACE_FAIL) {
2017                         HUD_KillNotify_Push(s1, s2, 1, RACE_FAIL);
2018                 }
2019         } else if(msg == MSG_KA) {
2020                 if(type == KA_PICKUPBALL) {
2021                         HUD_KillNotify_Push(s1, s2, 0, KA_PICKUPBALL);
2022                         if(alsoprint)
2023                                 print (sprintf(_("%s^7 has picked up the ball!\n"), s1));
2024                 }
2025                 else if(type == KA_DROPBALL) {
2026                         HUD_KillNotify_Push(s1, s2, 0, KA_DROPBALL);
2027                         if(alsoprint)
2028                                 print(sprintf(_("%s^7 has dropped the ball!\n"), s1));
2029                 }
2030         }
2031 }
2032
2033 #define DAMAGE_CENTERPRINT_SPACER NEWLINES
2034
2035 void HUD_Centerprint(string s1, string s2, float type, float msg)
2036 {
2037         float gentle;
2038         gentle = (autocvar_cl_gentle || autocvar_cl_gentle_messages);
2039         if(msg == MSG_SUICIDE) {
2040                 if (type == DEATH_TEAMCHANGE) {
2041                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("You are now on: %s"), s1)));
2042                 } else if (type == DEATH_AUTOTEAMCHANGE) {
2043                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("You have been moved into a different team to improve team balance\nYou are now on: %s"), s1)));
2044                 } else if (type == DEATH_CAMP) {
2045                         if(gentle)
2046                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Reconsider your tactics, camper!")));
2047                         else
2048                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Die camper!")));
2049                 } else if (type == DEATH_NOAMMO) {
2050                         if(gentle)
2051                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You are reinserted into the game for running out of ammo...")));
2052                         else
2053                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You were killed for running out of ammo...")));
2054                 } else if (type == DEATH_ROT) {
2055                         if(gentle)
2056                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You need to preserve your health")));
2057                         else
2058                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You grew too old without taking your medicine")));
2059                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2060                         if(gentle)
2061                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Don't go against team mates!")));
2062                         else
2063                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Don't shoot your team mates!")));
2064                 } else if (type == DEATH_QUIET) {
2065                         // do nothing
2066                 } else { // generic message
2067                         if(gentle)
2068                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You need to be more careful!")));
2069                         else
2070                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You killed your own dumb self!")));
2071                 }
2072         } else if(msg == MSG_KILL) {
2073                 if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2074                         if(gentle) {
2075                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1Moron! You went against %s, a team mate!"), s1)));
2076                         } else {
2077                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1Moron! You fragged %s, a team mate!"), s1)));
2078                         }
2079                 } else if (type == KILL_FIRST_BLOOD) {
2080                         if(gentle) {
2081                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1First score")));
2082                         } else {
2083                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1First blood")));
2084                         }
2085                 } else if (type == KILL_FIRST_VICTIM) {
2086                         if(gentle) {
2087                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1First casualty")));
2088                         } else {
2089                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1First victim")));
2090                         }
2091                 } else if (type == KILL_TYPEFRAG) { // s2 contains "advanced kill messages" such as ping, handicap...
2092                         if(gentle) {
2093                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You scored against ^7%s^1 who was typing!"), s1), s2));
2094                         } else {
2095                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You typefragged ^7%s"), s1), s2));
2096                         }
2097                 } else if (type == KILL_TYPEFRAGGED) {
2098                         if(gentle) {
2099                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You were scored against by ^7%s^1 while you were typing!"), s1), s2));
2100                         } else {
2101                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You were typefragged by ^7%s"), s1), s2));
2102                         }
2103                 } else if (type == KILL_FRAG) {
2104                         if(gentle) {
2105                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^4You scored against ^7%s"), s1), s2));
2106                         } else {
2107                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^4You fragged ^7%s"), s1), s2));
2108                         }
2109                 } else { // generic message
2110                         if(gentle) {
2111                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You were scored against by ^7%s"), s1), s2));
2112                         } else {
2113                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You were fragged by ^7%s"), s1), s2));
2114                         }
2115                 }
2116         } else if(msg == MSG_KILL_ACTION) {
2117                 // TODO: invent more centerprints here?
2118                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Watch your step!")));
2119         }
2120 }
2121
2122 void HUD_Notify (void)
2123 {
2124         if(!autocvar__hud_configure)
2125         {
2126                 if(!autocvar_hud_panel_notify) return;
2127         }
2128         else
2129                 hud_configure_active_panel = HUD_PANEL_NOTIFY;
2130
2131         HUD_Panel_UpdateCvars(notify);
2132         vector pos, mySize;
2133         pos = panel_pos;
2134         mySize = panel_size;
2135
2136         HUD_Panel_DrawBg(1);
2137         if(panel_bg_padding)
2138         {
2139                 pos += '1 1 0' * panel_bg_padding;
2140                 mySize -= '2 2 0' * panel_bg_padding;
2141         }
2142
2143         float entries, height;
2144         entries = bound(1, floor(KN_MAX_ENTRIES * mySize_y/mySize_x), KN_MAX_ENTRIES);
2145         height = mySize_y/entries;
2146         
2147         vector fontsize;
2148         float fontheight = height * autocvar_hud_panel_notify_fontsize;
2149         fontsize = '0.5 0.5 0' * fontheight;
2150
2151         float a;
2152         float when;
2153         when = autocvar_hud_panel_notify_time;
2154         float fadetime;
2155         fadetime = autocvar_hud_panel_notify_fadetime;
2156
2157         string s;
2158
2159         vector pos_attacker, pos_victim;
2160         vector weap_pos;
2161         float width_attacker;
2162         string attacker, victim;
2163
2164         float i, j, w, step, limit;
2165         if(autocvar_hud_panel_notify_flip) //order items from the top down
2166         {
2167                 i = 0;
2168                 step = +1;
2169                 limit = entries;
2170         }
2171         else //order items from the bottom up
2172         {
2173                 i = entries - 1;
2174                 step = -1;
2175                 limit = -1;
2176         }
2177
2178         for(j = kn_index;  i != limit;  i += step, ++j)
2179         {
2180                 if(autocvar__hud_configure)
2181                 {
2182                         if (step == +1)
2183                                 a = i;
2184                         else // inverse order
2185                                 a = entries - 1 - i;
2186                         attacker = textShortenToWidth(sprintf(_("Player %d"), a+1), 0.48 * mySize_x - height, fontsize, stringwidth_colors);
2187                         victim = textShortenToWidth(sprintf(_("Player %d"), a+2), 0.48 * mySize_x - height, fontsize, stringwidth_colors);
2188                         s = strcat("weapon", get_weaponinfo(WEP_FIRST + mod(floor(a*2.4), WEP_LAST)).netname);
2189                         a = bound(0, (when - a) / 4, 1);
2190                         goto hud_config_notifyprint;
2191                 }
2192
2193                 if (j == KN_MAX_ENTRIES)
2194                         j = 0;
2195
2196                 if(killnotify_times[j] + when > time)
2197                         a = 1;
2198                 else if(fadetime)
2199                 {
2200                         a = bound(0, (killnotify_times[j] + when + fadetime - time) / fadetime, 1);
2201                         if(!a)
2202                         {
2203                                 break;
2204                         }
2205                 }
2206                 else
2207                 {
2208                         break;
2209                 }
2210
2211                 s = "";
2212
2213                 w = -1;
2214                 w = DEATH_WEAPONOF(killnotify_deathtype[j]);
2215
2216                 // TODO: maybe print in team colors?
2217                 //
2218                 // Y [used by] X
2219                 if(killnotify_actiontype[j] == 0) 
2220                 {
2221                         if(killnotify_deathtype[j] == DEATH_GENERIC)
2222                         {
2223                                 s = "notify_death";
2224                         }
2225                         else if(killnotify_deathtype[j] == DEATH_NOAMMO)
2226                         {
2227                                 s = "notify_outofammo";
2228                         }
2229                         else if(killnotify_deathtype[j] == DEATH_KILL)
2230                         {
2231                                 s = "notify_selfkill";
2232                         }
2233                         else if(killnotify_deathtype[j] == DEATH_CAMP)
2234                         {
2235                                 s = "notify_camping";
2236                         }
2237                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
2238                         {
2239                                 s = "notify_teamkill_red";
2240                         }
2241                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
2242                         {
2243                                 s = "notify_teamkill_blue";
2244                         }
2245                         else if(killnotify_deathtype[j] == DEATH_DROWN)
2246                         {
2247                                 s = "notify_water";
2248                         }
2249                         else if(killnotify_deathtype[j] == DEATH_SLIME)
2250                         {
2251                                 s = "notify_slime";
2252                         }
2253                         else if(killnotify_deathtype[j] == DEATH_LAVA)
2254                         {
2255                                 s = "notify_lava";
2256                         }
2257                         else if(killnotify_deathtype[j] == DEATH_FALL)
2258                         {
2259                                 s = "notify_fall";
2260                         }
2261                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
2262                         {
2263                                 s = "notify_shootingstar";
2264                         }
2265                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM)
2266                         {
2267                                 s = "notify_death";
2268                         }
2269                         else if(killnotify_deathtype[j] == INFO_GOTFLAG)
2270                         {
2271                                 if(killnotify_victims[j] == "^1RED^7 flag")
2272                                 {
2273                                         s = "notify_red_taken";
2274                                 }
2275                                 else
2276                                 {
2277                                         s = "notify_blue_taken";
2278                                 }
2279                         }
2280                         else if(killnotify_deathtype[j] == INFO_RETURNFLAG)
2281                         {
2282                                 if(killnotify_victims[j] == "^1RED^7 flag")
2283                                 {
2284                                         s = "notify_red_returned";
2285                                 }
2286                                 else
2287                                 {
2288                                         s = "notify_blue_returned";
2289                                 }
2290                         }
2291                         else if(killnotify_deathtype[j] == INFO_LOSTFLAG)
2292                         {
2293                                 if(killnotify_victims[j] == "^1RED^7 flag")
2294                                 {
2295                                         s = "notify_red_lost";
2296                                 }
2297                                 else
2298                                 {
2299                                         s = "notify_blue_lost";
2300                                 }
2301                         }
2302                         else if(killnotify_deathtype[j] == INFO_CAPTUREFLAG)
2303                         {
2304                                 if(killnotify_victims[j] == "^1RED^7 flag")
2305                                 {
2306                                         s = "notify_red_captured";
2307                                 }
2308                                 else
2309                                 {
2310                                         s = "notify_blue_captured";
2311                                 }
2312                         }
2313                         else if(killnotify_deathtype[j] == KA_DROPBALL)
2314                         {
2315                                 s = "notify_balldropped";
2316                         }
2317                         else if(killnotify_deathtype[j] == KA_PICKUPBALL)
2318                         {
2319                                 s = "notify_ballpickedup";
2320                         }
2321                         
2322                         attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
2323                         pos_attacker = pos + eX * (0.52 * mySize_x + height) + eY * ((0.5 * fontsize_y + i * height) + (0.5 * (height - fontheight)));
2324                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
2325
2326                         if(s != "")
2327                         {
2328                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
2329                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2330                         }
2331                 }
2332                 // X [did action to] Y
2333                 else
2334                 {
2335                         if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_LASER)
2336                         {
2337                                 s = "notify_melee_laser";
2338                         }
2339                         else if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_SHOTGUN)
2340                         {
2341                                 s = "notify_melee_shotgun";
2342                         }
2343                         else if(WEP_VALID(w))
2344                         {
2345                                 self = get_weaponinfo(w);
2346                                 s = strcat("weapon", self.netname);
2347                         }
2348                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
2349                         {
2350                                 s = "notify_teamkill_red";
2351                         }
2352                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
2353                         {
2354                                 s = "notify_teamkill_red";
2355                         }
2356                         else if(killnotify_deathtype[j] == DEATH_TELEFRAG)
2357                         {
2358                                 s = "notify_telefrag";
2359                         }
2360                         else if(killnotify_deathtype[j] == DEATH_DROWN)
2361                         {
2362                                 s = "notify_water";
2363                         }
2364                         else if(killnotify_deathtype[j] == DEATH_SLIME)
2365                         {
2366                                 s = "notify_slime";
2367                         }
2368                         else if(killnotify_deathtype[j] == DEATH_LAVA)
2369                         {
2370                                 s = "notify_lava";
2371                         }
2372                         else if(killnotify_deathtype[j] == DEATH_FALL)
2373                         {
2374                                 s = "notify_fall";
2375                         }
2376                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
2377                         {
2378                                 s = "notify_shootingstar";
2379                         }
2380                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM) // DEATH_CUSTOM is also void, right?
2381                         {
2382                                 s = "notify_void";
2383                         }
2384                         else if(killnotify_deathtype[j] == DEATH_HEADSHOT)
2385                         {
2386                                 s = "notify_headshot";
2387                         }
2388                         else if(killnotify_deathtype[j] == RACE_SERVER_RECORD)
2389                         {
2390                                 s = "race_newrecordserver";
2391                         }
2392                         else if(killnotify_deathtype[j] == RACE_NEW_RANK)
2393                         {
2394                                 s = "race_newrankyellow";
2395                         }
2396                         else if(killnotify_deathtype[j] == RACE_NEW_TIME)
2397                         {
2398                                 s = "race_newtime";
2399                         }
2400                         else if(killnotify_deathtype[j] == RACE_FAIL)
2401                         {
2402                                 s = "race_newfail";
2403                         }
2404
2405                         attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
2406                         victim = textShortenToWidth(killnotify_victims[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
2407 :hud_config_notifyprint
2408                         width_attacker = stringwidth(attacker, TRUE, fontsize);
2409                         pos_attacker = pos + eX * (0.48 * mySize_x - height - width_attacker) + eY * ((0.5 * fontsize_y + i * height) + (0.5 * (height - fontheight)));
2410                         pos_victim = pos + eX * (0.52 * mySize_x + height) + eY * ((0.5 * fontsize_y + i * height) + (0.5 * (height - fontheight)));
2411                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
2412
2413                         if(s != "")
2414                         {
2415                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
2416                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2417                                 drawcolorcodedstring(pos_victim, victim, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2418                         }
2419                 }
2420         }
2421 }
2422
2423 // Timer (#5)
2424 //
2425 // TODO: macro
2426 string seconds_tostring(float sec)
2427 {
2428         float minutes;
2429         minutes = floor(sec / 60);
2430
2431         sec -= minutes * 60;
2432         return sprintf("%d:%02d", minutes, sec);
2433 }
2434
2435 void HUD_Timer(void)
2436 {
2437         if(!autocvar__hud_configure)
2438         {
2439                 if(!autocvar_hud_panel_timer) return;
2440         }
2441         else
2442                 hud_configure_active_panel = HUD_PANEL_TIMER;
2443
2444         HUD_Panel_UpdateCvars(timer);
2445         vector pos, mySize;
2446         pos = panel_pos;
2447         mySize = panel_size;
2448
2449         HUD_Panel_DrawBg(1);
2450         if(panel_bg_padding)
2451         {
2452                 pos += '1 1 0' * panel_bg_padding;
2453                 mySize -= '2 2 0' * panel_bg_padding;
2454         }
2455
2456         string timer;
2457         float timelimit, elapsedTime, timeleft, minutesLeft;
2458
2459         timelimit = getstatf(STAT_TIMELIMIT);
2460
2461         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
2462         timeleft = ceil(timeleft);
2463
2464         minutesLeft = floor(timeleft / 60);
2465
2466         vector timer_color;
2467         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
2468                 timer_color = '1 1 1'; //white
2469         else if(minutesLeft >= 1)
2470                 timer_color = '1 1 0'; //yellow
2471         else
2472                 timer_color = '1 0 0'; //red
2473
2474         if (autocvar_hud_panel_timer_increment || timelimit == 0 || warmup_stage) {
2475                 if (time < getstatf(STAT_GAMESTARTTIME)) {
2476                         //while restart is still active, show 00:00
2477                         timer = seconds_tostring(0);
2478                 } else {
2479                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
2480                         timer = seconds_tostring(elapsedTime);
2481                 }
2482         } else {
2483                 timer = seconds_tostring(timeleft);
2484         }
2485
2486         drawstring_aspect(pos, timer, mySize, timer_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2487 }
2488
2489 // Radar (#6)
2490 //
2491 void HUD_Radar(void)
2492 {
2493         if (!autocvar__hud_configure)
2494         {
2495                 if (autocvar_hud_panel_radar == 0) return;
2496                 if (autocvar_hud_panel_radar != 2 && !teamplay) return;
2497         }
2498         else
2499                 hud_configure_active_panel = HUD_PANEL_RADAR;
2500
2501         HUD_Panel_UpdateCvars(radar);
2502         vector pos, mySize;
2503         pos = panel_pos;
2504         mySize = panel_size;
2505
2506         HUD_Panel_DrawBg(1);
2507         if(panel_bg_padding)
2508         {
2509                 pos += '1 1 0' * panel_bg_padding;
2510                 mySize -= '2 2 0' * panel_bg_padding;
2511         }
2512
2513         local float color2;
2514         local entity tm;
2515         float scale2d, normalsize, bigsize;
2516         float f;
2517
2518         teamradar_origin2d = pos + 0.5 * mySize;
2519         teamradar_size2d = mySize;
2520
2521         if(minimapname == "")
2522                 return;
2523
2524         teamradar_loadcvars();
2525
2526         switch(hud_panel_radar_zoommode)
2527         {
2528                 default:
2529                 case 0:
2530                         f = current_zoomfraction;
2531                         break;
2532                 case 1:
2533                         f = 1 - current_zoomfraction;
2534                         break;
2535                 case 2:
2536                         f = 0;
2537                         break;
2538                 case 3:
2539                         f = 1;
2540                         break;
2541         }
2542
2543         switch(hud_panel_radar_rotation)
2544         {
2545                 case 0:
2546                         teamradar_angle = view_angles_y - 90;
2547                         break;
2548                 default:
2549                         teamradar_angle = 90 * hud_panel_radar_rotation;
2550                         break;
2551         }
2552
2553         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
2554         teamradar_size2d = mySize;
2555
2556         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
2557
2558         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
2559         if(hud_panel_radar_rotation == 0)
2560         {
2561                 // max-min distance must fit the radar in any rotation
2562                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_scale));
2563         }
2564         else
2565         {
2566                 vector c0, c1, c2, c3, span;
2567                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
2568                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
2569                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
2570                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
2571                 span = '0 0 0';
2572                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
2573                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
2574
2575                 // max-min distance must fit the radar in x=x, y=y
2576                 bigsize = min(
2577                         teamradar_size2d_x * scale2d / (1.05 * span_x),
2578                         teamradar_size2d_y * scale2d / (1.05 * span_y)
2579                 );
2580         }
2581
2582         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
2583         if(bigsize > normalsize)
2584                 normalsize = bigsize;
2585
2586         teamradar_size =
2587                   f * bigsize
2588                 + (1 - f) * normalsize;
2589         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
2590                   f * mi_center
2591                 + (1 - f) * view_origin);
2592
2593         drawsetcliparea(
2594                 pos_x,
2595                 pos_y,
2596                 mySize_x,
2597                 mySize_y
2598         );
2599
2600         draw_teamradar_background(hud_panel_radar_foreground_alpha);
2601
2602         for(tm = world; (tm = find(tm, classname, "radarlink")); )
2603                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
2604         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
2605                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, panel_fg_alpha);
2606         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
2607         {
2608                 color2 = GetPlayerColor(tm.sv_entnum);
2609                 //if(color == COLOR_SPECTATOR || color == color2)
2610                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
2611         }
2612         draw_teamradar_player(view_origin, view_angles, '1 1 1');
2613
2614         drawresetcliparea();
2615 };
2616
2617 // Score (#7)
2618 //
2619 void HUD_UpdatePlayerTeams();
2620 void HUD_Score_Rankings(vector pos, vector mySize, entity me, float team_count)
2621 {
2622         float score;
2623         entity tm, pl;
2624 #define SCOREPANEL_MAX_ENTRIES 6
2625 #define SCOREPANEL_ASPECTRATIO 2
2626         const float entries = bound(1, floor(SCOREPANEL_MAX_ENTRIES * mySize_y/mySize_x * SCOREPANEL_ASPECTRATIO), SCOREPANEL_MAX_ENTRIES);
2627         const vector fontsize = '1 1 0' * (mySize_y/entries);
2628
2629         vector rgb, score_color;
2630         rgb = '1 1 1';
2631         score_color = '1 1 1';
2632
2633         const float name_size = mySize_x*0.75;
2634         const float spacing_size = mySize_x*0.04;
2635         const float highlight_alpha = 0.2;
2636         float i, me_printed, first_pl;
2637         string s;
2638         i, first_pl = 0;
2639         if (autocvar__hud_configure)
2640         {
2641                 float players_per_team;
2642                 if (team_count)
2643                 {
2644                         // show team scores in the first line
2645                         float score_size = mySize_x / team_count;
2646                         players_per_team = max(2, ceil((entries - 1) / team_count));
2647                         for(i=0; i<team_count; ++i) {
2648                                 if (i == floor((entries - 2) / players_per_team) || (entries == 1 && i == 0))
2649                                         HUD_Panel_DrawHighlight(pos + eX * score_size * i, eX * score_size + eY * fontsize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2650                                 drawstring_aspect(pos + eX * score_size * i, ftos(175 - 23*i), eX * score_size + eY * fontsize_y, GetTeamRGB(ColorByTeam(i)) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2651                         }
2652                         first_pl = 1;
2653                         pos_y += fontsize_y;
2654                 }
2655                 score = 10 + SCOREPANEL_MAX_ENTRIES * 3;
2656                 for (i=first_pl; i<entries; ++i)
2657                 {
2658                         //simulate my score is lower than all displayed players,
2659                         //so that I don't appear at all showing pure rankings.
2660                         //This is to better show the difference between the 2 ranking views
2661                         if (i == entries-1 && autocvar_hud_panel_score_rankings == 1)
2662                         {
2663                                 rgb = '1 1 0';
2664                                 drawfill(pos, eX * mySize_x + eY * fontsize_y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2665                                 s = GetPlayerName(pl.sv_entnum);
2666                                 score = 7;
2667                         }
2668                         else
2669                         {
2670                                 s = sprintf(_("Player %d"), i + 1 - first_pl);
2671                                 score -= 3;
2672                         }
2673
2674                         if (team_count)
2675                                 score_color = GetTeamRGB(ColorByTeam(floor((i - first_pl) / players_per_team))) * 0.8;
2676                         s = textShortenToWidth(s, name_size, fontsize, stringwidth_colors);
2677                         drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, TRUE, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
2678                         drawstring(pos + eX * (name_size + spacing_size), ftos(score), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2679                         pos_y += fontsize_y;
2680                 }
2681                 return;
2682         }
2683
2684         if (!scoreboard_fade_alpha) // the scoreboard too calls HUD_UpdatePlayerTeams
2685                 HUD_UpdatePlayerTeams();
2686         if (team_count)
2687         {
2688                 // show team scores in the first line
2689                 float score_size = mySize_x / team_count;
2690                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2691                         if(tm.team == COLOR_SPECTATOR)
2692                                 continue;
2693                         if (tm.team == myteam)
2694                                 drawfill(pos + eX * score_size * i, eX * score_size + eY * fontsize_y, '1 1 1', highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2695                         drawstring_aspect(pos + eX * score_size * i, ftos(tm.(teamscores[ts_primary])), eX * score_size + eY * fontsize_y, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2696                         ++i;
2697                 }
2698                 first_pl = 1;
2699                 pos_y += fontsize_y;
2700                 tm = teams.sort_next;
2701         }
2702         i = first_pl;
2703
2704         do
2705         for (pl = players.sort_next; pl && i<entries; pl = pl.sort_next)
2706         {
2707                 if ((team_count && pl.team != tm.team) || pl.team == COLOR_SPECTATOR)
2708                         continue;
2709
2710                 if (i == entries-1 && !me_printed && pl != me)
2711                 if (autocvar_hud_panel_score_rankings == 1 && spectatee_status != -1)
2712                 {
2713                         for (pl = me.sort_next; pl; pl = pl.sort_next)
2714                                 if (pl.team != COLOR_SPECTATOR)
2715                                         break;
2716
2717                         if (pl)
2718                                 rgb = '1 1 0'; //not last but not among the leading players: yellow
2719                         else
2720                                 rgb = '1 0 0'; //last: red
2721                         pl = me;
2722                 }
2723
2724                 if (pl == me)
2725                 {
2726                         if (i == first_pl)
2727                                 rgb = '0 1 0'; //first: green
2728                         me_printed = 1;
2729                         drawfill(pos, eX * mySize_x + eY * fontsize_y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2730                 }
2731                 if (team_count)
2732                         score_color = GetTeamRGB(pl.team) * 0.8;
2733                 s = textShortenToWidth(GetPlayerName(pl.sv_entnum), name_size, fontsize, stringwidth_colors);
2734                 drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, TRUE, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
2735                 drawstring(pos + eX * (name_size + spacing_size), ftos(pl.(scores[ps_primary])), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2736                 pos_y += fontsize_y;
2737                 ++i;
2738         }
2739         while (i<entries && team_count && (tm = tm.sort_next) && (tm.team != COLOR_SPECTATOR || (tm = tm.sort_next)));
2740 }
2741
2742 void HUD_Score(void)
2743 {
2744         if(!autocvar__hud_configure)
2745         {
2746                 if(!autocvar_hud_panel_score) return;
2747                 if(spectatee_status == -1 && (gametype == GAME_RACE || gametype == GAME_CTS)) return;
2748         }
2749         else
2750                 hud_configure_active_panel = HUD_PANEL_SCORE;
2751
2752         HUD_Panel_UpdateCvars(score);
2753         vector pos, mySize;
2754         pos = panel_pos;
2755         mySize = panel_size;
2756
2757         HUD_Panel_DrawBg(1);
2758         if(panel_bg_padding)
2759         {
2760                 pos += '1 1 0' * panel_bg_padding;
2761                 mySize -= '2 2 0' * panel_bg_padding;
2762         }
2763
2764         float score, distribution;
2765         string sign;
2766         vector distribution_color;
2767         entity tm, pl, me;
2768         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
2769
2770         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
2771                 string timer, distrtimer;
2772
2773                 pl = players.sort_next;
2774                 if(pl == me)
2775                         pl = pl.sort_next;
2776                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
2777                         if(pl.scores[ps_primary] == 0)
2778                                 pl = world;
2779
2780                 score = me.(scores[ps_primary]);
2781                 timer = TIME_ENCODED_TOSTRING(score);
2782
2783                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
2784                         // distribution display
2785                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2786
2787                         distrtimer = ftos_decimals(fabs(distribution/pow(10, TIME_DECIMALS)), TIME_DECIMALS);
2788
2789                         if (distribution <= 0) {
2790                                 distribution_color = '0 1 0';
2791                                 sign = "-";
2792                         }
2793                         else {
2794                                 distribution_color = '1 0 0';
2795                                 sign = "+";
2796                         }
2797                         drawstring_aspect(pos + eX * 0.75 * mySize_x, strcat(sign, distrtimer), eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2798                 }
2799                 // race record display
2800                 if (distribution <= 0)
2801                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2802                 drawstring_aspect(pos, timer, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2803         } else if (!teamplay) { // non-teamgames
2804                 if ((spectatee_status == -1 && !autocvar__hud_configure) || autocvar_hud_panel_score_rankings)
2805                 {
2806                         HUD_Score_Rankings(pos, mySize, me, 0);
2807                         return;
2808                 }
2809                 // me vector := [team/connected frags id]
2810                 pl = players.sort_next;
2811                 if(pl == me)
2812                         pl = pl.sort_next;
2813
2814                 if(autocvar__hud_configure)
2815                         distribution = 42;
2816                 else if(pl)
2817                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2818                 else
2819                         distribution = 0;
2820
2821                 score = me.(scores[ps_primary]);
2822                 if(autocvar__hud_configure)
2823                         score = 123;
2824
2825                 if(distribution >= 5)
2826                         distribution_color = eY;
2827                 else if(distribution >= 0)
2828                         distribution_color = '1 1 1';
2829                 else if(distribution >= -5)
2830                         distribution_color = '1 1 0';
2831                 else
2832                         distribution_color = eX;
2833
2834                 string distribution_str;
2835                 distribution_str = ftos(distribution);
2836                 if (distribution >= 0)
2837                 {
2838                         if (distribution > 0)
2839                                 distribution_str = strcat("+", distribution_str);
2840                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2841                 }
2842                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2843                 drawstring_aspect(pos + eX * 0.75 * mySize_x, distribution_str, eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2844         } else { // teamgames
2845                 float scores_count, row, column, rows, columns;
2846                 vector offset;
2847                 vector score_pos, score_size; //for scores other than myteam
2848                 if (spectatee_status == -1 || autocvar_hud_panel_score_rankings)
2849                 {
2850                         for(tm = teams.sort_next; tm, tm.team != COLOR_SPECTATOR; tm = tm.sort_next)
2851                                 ++scores_count;
2852                         if (autocvar_hud_panel_score_rankings)
2853                         {
2854                                 HUD_Score_Rankings(pos, mySize, me, scores_count);
2855                                 return;
2856                         }
2857                         rows = mySize_y/mySize_x;
2858                         rows = bound(1, floor((sqrt(4 * (3/1) * rows * scores_count + rows * rows) + rows + 0.5) / 2), scores_count);
2859                         //                               ^^^ ammo item aspect goes here
2860
2861                         columns = ceil(scores_count/rows);
2862
2863                         score_size = eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows);
2864
2865                         float newSize;
2866                         if(score_size_x/score_size_y > 3)
2867                         {
2868                                 newSize = 3 * score_size_y;
2869                                 offset_x = score_size_x - newSize;
2870                                 pos_x += offset_x/2;
2871                                 score_size_x = newSize;
2872                         }
2873                         else
2874                         {
2875                                 newSize = 1/3 * score_size_x;
2876                                 offset_y = score_size_y - newSize;
2877                                 pos_y += offset_y/2;
2878                                 score_size_y = newSize;
2879                         }
2880                 }
2881                 else
2882                         score_size = eX * mySize_x*(1/4) + eY * mySize_y*(1/3);
2883
2884                 float max_fragcount;
2885                 max_fragcount = -99;
2886                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2887                         if(tm.team == COLOR_SPECTATOR)
2888                                 continue;
2889                         score = tm.(teamscores[ts_primary]);
2890                         if(autocvar__hud_configure)
2891                                 score = 123;
2892                         
2893                         if (score > max_fragcount)
2894                                 max_fragcount = score;
2895
2896                         if (spectatee_status == -1)
2897                         {
2898                                 score_pos = pos + eX * column * (score_size_x + offset_x) + eY * row * (score_size_y + offset_y);
2899                                 if (max_fragcount == score)
2900                                         HUD_Panel_DrawHighlight(score_pos, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2901                                 drawstring_aspect(score_pos, ftos(score), score_size, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2902                                 ++row;
2903                                 if(row >= rows)
2904                                 {
2905                                         row = 0;
2906                                         ++column;
2907                                 }
2908                         }
2909                         else if(tm.team == myteam) {
2910                                 if (max_fragcount == score)
2911                                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2912                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2913                         } else {
2914                                 if (max_fragcount == score)
2915                                         HUD_Panel_DrawHighlight(pos + eX * 0.75 * mySize_x + eY * (1/3) * rows * mySize_y, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2916                                 drawstring_aspect(pos + eX * 0.75 * mySize_x + eY * (1/3) * rows * mySize_y, ftos(score), score_size, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2917                                 ++rows;
2918                         }
2919                 }
2920         }
2921 }
2922
2923 // Race timer (#8)
2924 //
2925 void HUD_RaceTimer (void)
2926 {
2927         if(!autocvar__hud_configure)
2928         {
2929                 if(!autocvar_hud_panel_racetimer) return;
2930                 if(!(gametype == GAME_RACE || gametype == GAME_CTS)) return;
2931                 if(spectatee_status == -1) return;
2932         }
2933         else
2934                 hud_configure_active_panel = HUD_PANEL_RACETIMER;
2935
2936         HUD_Panel_UpdateCvars(racetimer);
2937         vector pos, mySize;
2938         pos = panel_pos;
2939         mySize = panel_size;
2940
2941         HUD_Panel_DrawBg(1);
2942         if(panel_bg_padding)
2943         {
2944                 pos += '1 1 0' * panel_bg_padding;
2945                 mySize -= '2 2 0' * panel_bg_padding;
2946         }
2947
2948         // always force 4:1 aspect
2949         vector newSize;
2950         if(mySize_x/mySize_y > 4)
2951         {
2952                 newSize_x = 4 * mySize_y;
2953                 newSize_y = mySize_y;
2954
2955                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
2956         }
2957         else
2958         {
2959                 newSize_y = 1/4 * mySize_x;
2960                 newSize_x = mySize_x;
2961
2962                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
2963         }
2964         mySize = newSize;
2965
2966         float a, t;
2967         string s, forcetime;
2968
2969         if(autocvar__hud_configure)
2970         {
2971                 s = "0:13:37";
2972                 drawstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, FALSE, '0.60 0.60 0' * mySize_y), s, '0.60 0.60 0' * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2973                 s = _("^1Intermediate 1 (+15.42)");
2974                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.20 * mySize_y) + eY * 0.60 * mySize_y, s, '1 1 0' * 0.20 * mySize_y, panel_fg_alpha, DRAWFLAG_NORMAL);
2975                 s = sprintf(_("^1PENALTY: %.1f (%s)"), 2, "missing a checkpoint");
2976                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.20 * mySize_y) + eY * 0.80 * mySize_y, s, '1 1 0' * 0.20 * mySize_y, panel_fg_alpha, DRAWFLAG_NORMAL);
2977         }
2978         else if(race_checkpointtime)
2979         {
2980                 a = bound(0, 2 - (time - race_checkpointtime), 1);
2981                 s = "";
2982                 forcetime = "";
2983                 if(a > 0) // just hit a checkpoint?
2984                 {
2985                         if(race_checkpoint != 254)
2986                         {
2987                                 if(race_time && race_previousbesttime)
2988                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
2989                                 else
2990                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
2991                                 if(race_time)
2992                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
2993                         }
2994                 }
2995                 else
2996                 {
2997                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
2998                         {
2999                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
3000                                 if(a > 0) // next one?
3001                                 {
3002                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
3003                                 }
3004                         }
3005                 }
3006
3007                 if(s != "" && a > 0)
3008                 {
3009                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3010                 }
3011
3012                 if(race_penaltytime)
3013                 {
3014                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
3015                         if(a > 0)
3016                         {
3017                                 s = sprintf(_("^1PENALTY: %.1f (%s)"), race_penaltytime * 0.1, race_penaltyreason);
3018                                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.8 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3019                         }
3020                 }
3021
3022                 if(forcetime != "")
3023                 {
3024                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
3025                         drawstring_expanding(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(forcetime, FALSE, '1 1 0' * 0.6 * mySize_y), forcetime, '1 1 0' * 0.6 * mySize_y, '1 1 1', panel_fg_alpha, 0, a);
3026                 }
3027                 else
3028                         a = 1;
3029
3030                 if(race_laptime && race_checkpoint != 255)
3031                 {
3032                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
3033                         drawstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, FALSE, '0.6 0.6 0' * mySize_y), s, '0.6 0.6 0' * mySize_y, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3034                 }
3035         }
3036         else
3037         {
3038                 if(race_mycheckpointtime)
3039                 {
3040                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
3041                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
3042                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3043                 }
3044                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
3045                 {
3046                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
3047                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
3048                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3049                 }
3050
3051                 if(race_penaltytime && !race_penaltyaccumulator)
3052                 {
3053                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
3054                         a = bound(0, (1 + t - time), 1);
3055                         if(a > 0)
3056                         {
3057                                 if(time < t)
3058                                         s = sprintf(_("^1PENALTY: %.1f (%s)"), (t - time) * 0.1, race_penaltyreason);
3059                                 else
3060                                         s = sprintf(_("^2PENALTY: %.1f (%s)"), 0, race_penaltyreason);
3061                                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3062                         }
3063                 }
3064         }
3065 }
3066
3067 // Vote window (#9)
3068 //
3069 float vote_yescount;
3070 float vote_nocount;
3071 float vote_needed;
3072 float vote_highlighted; // currently selected vote
3073
3074 float vote_active; // is there an active vote?
3075 float vote_prev; // previous state of vote_active to check for a change
3076 float vote_alpha;
3077 float vote_change; // "time" when vote_active changed
3078
3079 void HUD_VoteWindow(void) 
3080 {
3081         if(autocvar_cl_allow_uid2name == -1 && (gametype == GAME_CTS || gametype == GAME_RACE))
3082         {
3083                 vote_active = 1;
3084                 if (autocvar__hud_configure)
3085                 {
3086                         vote_yescount = 0;
3087                         vote_nocount = 0;
3088                         print(_("^1You must answer before entering hud configure mode\n"));
3089                         cvar_set("_hud_configure", "0");
3090                 }
3091                 vote_called_vote = strzone(_("^2Name ^7instead of \"^1Unregistered player^7\" in stats"));
3092                 uid2name_dialog = 1;
3093         }
3094
3095         if(!autocvar__hud_configure)
3096         {
3097                 if(!autocvar_hud_panel_vote) return;
3098
3099                 panel_fg_alpha = autocvar_hud_panel_fg_alpha;
3100                 panel_bg_alpha_str = autocvar_hud_panel_vote_bg_alpha;
3101
3102                 if(panel_bg_alpha_str == "") {
3103                         panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);
3104                 }
3105                 panel_bg_alpha = stof(panel_bg_alpha_str);
3106         }
3107         else
3108         {
3109                 hud_configure_active_panel = HUD_PANEL_VOTE;
3110
3111                 vote_yescount = 3;
3112                 vote_nocount = 2;
3113                 vote_needed = 4;
3114         }
3115
3116         string s;
3117         float a;
3118         if(vote_active != vote_prev) {
3119                 vote_change = time;
3120                 vote_prev = vote_active;
3121         }
3122
3123         if(vote_active || autocvar__hud_configure)
3124                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
3125         else
3126                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
3127
3128         if(!vote_alpha)
3129                 return;
3130
3131         HUD_Panel_UpdateCvars(vote);
3132
3133         if(uid2name_dialog)
3134         {
3135                 panel_pos = eX * 0.3 * vid_conwidth + eY * 0.1 * vid_conheight;
3136                 panel_size = eX * 0.4 * vid_conwidth + eY * 0.3 * vid_conheight;
3137         }
3138
3139     // these must be below above block
3140         vector pos, mySize;
3141         pos = panel_pos;
3142         mySize = panel_size;
3143
3144         a = vote_alpha * bound(autocvar_hud_panel_vote_alreadyvoted_alpha, 1 - vote_highlighted, 1);
3145         HUD_Panel_DrawBg(a);
3146         a = panel_fg_alpha * a;
3147
3148         if(panel_bg_padding)
3149         {
3150                 pos += '1 1 0' * panel_bg_padding;
3151                 mySize -= '2 2 0' * panel_bg_padding;
3152         }
3153
3154         // always force 3:1 aspect
3155         vector newSize;
3156         if(mySize_x/mySize_y > 3)
3157         {
3158                 newSize_x = 3 * mySize_y;
3159                 newSize_y = mySize_y;
3160
3161                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3162         }
3163         else
3164         {
3165                 newSize_y = 1/3 * mySize_x;
3166                 newSize_x = mySize_x;
3167
3168                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3169         }
3170         mySize = newSize;
3171
3172         s = _("A vote has been called for:");
3173         if(uid2name_dialog)
3174                 s = _("Allow servers to store and display your name?");
3175         drawstring_aspect(pos, s, eX * mySize_x + eY * (2/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3176         s = textShortenToWidth(vote_called_vote, mySize_x, '1 1 0' * mySize_y * (1/8), stringwidth_colors);
3177         if(autocvar__hud_configure)
3178                 s = _("^1Configure the HUD");
3179         drawcolorcodedstring_aspect(pos + eY * (2/8) * mySize_y, s, eX * mySize_x + eY * (1.75/8) * mySize_y, a, DRAWFLAG_NORMAL);
3180
3181         // print the yes/no counts
3182     s = sprintf(_("Yes (%s): %d"), getcommandkey("vyes", "vyes"), vote_yescount);
3183         drawstring_aspect(pos + eY * (4/8) * mySize_y, s, eX * 0.5 * mySize_x + eY * (1.5/8) * mySize_y, '0 1 0', a, DRAWFLAG_NORMAL);
3184     s = sprintf(_("No (%s): %d"), getcommandkey("vno", "vno"), vote_nocount);
3185         drawstring_aspect(pos + eX * 0.5 * mySize_x + eY * (4/8) * mySize_y, s, eX * 0.5 * mySize_x + eY * (1.5/8) * mySize_y, '1 0 0', a, DRAWFLAG_NORMAL);
3186
3187         // draw the progress bar backgrounds
3188         drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_back", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3189
3190         // draw the highlights
3191         if(vote_highlighted == 1) {
3192                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
3193                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3194         }
3195         else if(vote_highlighted == 2) {
3196                 drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
3197                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3198         }
3199
3200         // draw the progress bars
3201         if(vote_yescount && vote_needed)
3202         {
3203                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
3204                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3205         }
3206
3207         if(vote_nocount && vote_needed)
3208         {
3209                 drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
3210                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3211         }
3212
3213         drawresetcliparea();
3214
3215         if(!vote_active) {
3216                 vote_highlighted = 0;
3217         }
3218 }
3219
3220 // Mod icons panel (#10)
3221 //
3222
3223 float mod_active; // is there any active mod icon?
3224
3225 // Clan Arena HUD modicons
3226 void HUD_Mod_CA(vector pos, vector mySize)
3227 {
3228         mod_active = 1; // CA should never hide the mod icons panel
3229         float redalive, bluealive;
3230         redalive = getstati(STAT_REDALIVE);
3231         bluealive = getstati(STAT_BLUEALIVE);
3232
3233         vector redpos, bluepos;
3234         if(mySize_x > mySize_y)
3235         {
3236                 redpos = pos;
3237                 bluepos = pos + eY * 0.5 * mySize_y;
3238                 drawpic_aspect_skin(redpos, "player_red.tga", 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3239                 drawstring_aspect(redpos + eX * 0.5 * mySize_x, ftos(redalive), 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3240                 drawpic_aspect_skin(bluepos, "player_blue.tga", 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3241                 drawstring_aspect(bluepos + eX * 0.5 * mySize_x, ftos(bluealive), 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3242         }
3243         else
3244         {
3245                 redpos = pos;
3246                 bluepos = pos + eY * 0.5 * mySize_y;
3247                 drawpic_aspect_skin(redpos, "player_red.tga", eX * mySize_x + eY * 0.3 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3248                 drawstring_aspect(redpos + eY * 0.3 * mySize_y, ftos(redalive), eX * mySize_x + eY * 0.2 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3249                 drawpic_aspect_skin(bluepos, "player_blue.tga", eX * mySize_x + eY * 0.3 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3250                 drawstring_aspect(bluepos + eY * 0.3 * mySize_y, ftos(bluealive), eX * mySize_x + eY * 0.2 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3251         }
3252 }
3253
3254 // CTF HUD modicon section
3255 float redflag_prevframe, blueflag_prevframe; // status during previous frame
3256 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
3257 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
3258
3259 void HUD_Mod_CTF_Reset(void)
3260 {
3261         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
3262 }
3263
3264 void HUD_Mod_CTF(vector pos, vector mySize)
3265 {
3266         vector redflag_pos, blueflag_pos;
3267         vector flag_size;
3268         float f; // every function should have that
3269
3270         float redflag, blueflag; // current status
3271         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
3272         float stat_items;
3273
3274         stat_items = getstati(STAT_ITEMS);
3275         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
3276         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
3277         
3278         if(redflag || blueflag)
3279                 mod_active = 1;
3280         else
3281                 mod_active = 0;
3282
3283         if(autocvar__hud_configure)
3284         {
3285                 redflag = 1;
3286                 blueflag = 2;
3287         }
3288
3289         // when status CHANGES, set old status into prevstatus and current status into status
3290         if (redflag != redflag_prevframe)
3291         {
3292                 redflag_statuschange_time = time;
3293                 redflag_prevstatus = redflag_prevframe;
3294                 redflag_prevframe = redflag;
3295         }
3296
3297         if (blueflag != blueflag_prevframe)
3298         {
3299                 blueflag_statuschange_time = time;
3300                 blueflag_prevstatus = blueflag_prevframe;
3301                 blueflag_prevframe = blueflag;
3302         }
3303
3304         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
3305         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
3306
3307         float BLINK_FACTOR = 0.15;
3308         float BLINK_BASE = 0.85;
3309         // note:
3310         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
3311         // thus
3312         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
3313         // ensure RMS == 1
3314         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
3315
3316         string red_icon, red_icon_prevstatus;
3317         float red_alpha, red_alpha_prevstatus;
3318         red_alpha = red_alpha_prevstatus = 1;
3319         switch(redflag) {
3320                 case 1: red_icon = "flag_red_taken"; break;
3321                 case 2: red_icon = "flag_red_lost"; break;
3322                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3323                 default:
3324                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
3325                                 red_icon = "flag_red_shielded";
3326                         else
3327                                 red_icon = string_null;
3328                         break;
3329         }
3330         switch(redflag_prevstatus) {
3331                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
3332                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
3333                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3334                 default:
3335                         if(redflag == 3)
3336                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
3337                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
3338                                 red_icon_prevstatus = "flag_red_shielded";
3339                         else
3340                                 red_icon_prevstatus = string_null;
3341                         break;
3342         }
3343
3344         string blue_icon, blue_icon_prevstatus;
3345         float blue_alpha, blue_alpha_prevstatus;
3346         blue_alpha = blue_alpha_prevstatus = 1;
3347         switch(blueflag) {
3348                 case 1: blue_icon = "flag_blue_taken"; break;
3349                 case 2: blue_icon = "flag_blue_lost"; break;
3350                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3351                 default:
3352                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
3353                                 blue_icon = "flag_blue_shielded";
3354                         else
3355                                 blue_icon = string_null;
3356                         break;
3357         }
3358         switch(blueflag_prevstatus) {
3359                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
3360                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
3361                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3362                 default:
3363                         if(blueflag == 3)
3364                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
3365                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
3366                                 blue_icon_prevstatus = "flag_blue_shielded";
3367                         else
3368                                 blue_icon_prevstatus = string_null;
3369                         break;
3370         }
3371
3372         if(mySize_x > mySize_y) {
3373                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
3374                         redflag_pos = pos;
3375                         blueflag_pos = pos + eX * 0.5 * mySize_x;
3376                 } else {
3377                         blueflag_pos = pos;
3378                         redflag_pos = pos + eX * 0.5 * mySize_x;
3379                 }
3380                 flag_size = eX * 0.5 * mySize_x + eY * mySize_y;
3381         } else {
3382                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
3383                         redflag_pos = pos;
3384                         blueflag_pos = pos + eY * 0.5 * mySize_y;
3385                 } else {
3386                         blueflag_pos = pos;
3387                         redflag_pos = pos + eY * 0.5 * mySize_y;
3388                 }
3389                 flag_size = eY * 0.5 * mySize_y + eX * mySize_x;
3390         }
3391
3392         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
3393         if(red_icon_prevstatus && f < 1)
3394                 drawpic_aspect_skin_expanding(redflag_pos, red_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
3395         if(red_icon)
3396                 drawpic_aspect_skin(redflag_pos, red_icon, flag_size, '1 1 1', panel_fg_alpha * red_alpha * f, DRAWFLAG_NORMAL);
3397
3398         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
3399         if(blue_icon_prevstatus && f < 1)
3400                 drawpic_aspect_skin_expanding(blueflag_pos, blue_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
3401         if(blue_icon)
3402                 drawpic_aspect_skin(blueflag_pos, blue_icon, flag_size, '1 1 1', panel_fg_alpha * blue_alpha * f, DRAWFLAG_NORMAL);
3403 }
3404
3405 // Keyhunt HUD modicon section
3406 float kh_runheretime;
3407
3408 void HUD_Mod_KH_Reset(void)
3409 {
3410         kh_runheretime = 0;
3411 }
3412
3413 void HUD_Mod_KH(vector pos, vector mySize)
3414 {
3415         mod_active = 1; // keyhunt should never hide the mod icons panel
3416         float kh_keys;
3417         float keyteam;
3418         float a, aa;
3419         vector p, pa, kh_size, kh_asize;
3420
3421         kh_keys = getstati(STAT_KH_KEYS);
3422
3423         p_x = pos_x;
3424         if(mySize_x > mySize_y)
3425         {
3426                 p_y = pos_y + 0.25 * mySize_y;
3427                 pa = p - eY * 0.25 * mySize_y;
3428
3429                 kh_size_x = mySize_x * 0.25;
3430                 kh_size_y = 0.75 * mySize_y;
3431                 kh_asize_x = mySize_x * 0.25;
3432                 kh_asize_y = mySize_y * 0.25;
3433         }
3434         else
3435         {
3436                 p_y = pos_y + 0.125 * mySize_y;
3437                 pa = p - eY * 0.125 * mySize_y;
3438
3439                 kh_size_x = mySize_x * 0.5;
3440                 kh_size_y = 0.375 * mySize_y;
3441                 kh_asize_x = mySize_x * 0.5;
3442                 kh_asize_y = mySize_y * 0.125;
3443         }
3444
3445         float i, key;
3446
3447         float keycount;
3448         keycount = 0;
3449         for(i = 0; i < 4; ++i)
3450         {
3451                 key = floor(kh_keys / pow(32, i)) & 31;
3452                 keyteam = key - 1;
3453                 if(keyteam == 30 && keycount <= 4)
3454                         keycount += 4;
3455                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
3456                         keycount += 1;
3457         }
3458
3459         // this yields 8 exactly if "RUN HERE" shows
3460
3461         if(keycount == 8)
3462         {
3463                 if(!kh_runheretime)
3464                         kh_runheretime = time;
3465                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
3466         }
3467         else
3468                 kh_runheretime = 0;
3469
3470         for(i = 0; i < 4; ++i)
3471         {
3472                 key = floor(kh_keys / pow(32, i)) & 31;
3473                 keyteam = key - 1;
3474                 switch(keyteam)
3475                 {
3476                         case 30: // my key
3477                                 keyteam = myteam;
3478                                 a = 1;
3479                                 aa = 1;
3480                                 break;
3481                         case -1: // no key
3482                                 a = 0;
3483                                 aa = 0;
3484                                 break;
3485                         default: // owned or dropped
3486                                 a = 0.2;
3487                                 aa = 0.5;
3488                                 break;
3489                 }
3490                 a = a * panel_fg_alpha;
3491                 aa = aa * panel_fg_alpha;
3492                 if(a > 0)
3493                 {
3494                         switch(keyteam)
3495                         {
3496                                 case COLOR_TEAM1:
3497                                         drawpic_aspect_skin(pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3498                                         break;
3499                                 case COLOR_TEAM2:
3500                                         drawpic_aspect_skin(pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3501                                         break;
3502                                 case COLOR_TEAM3:
3503                                         drawpic_aspect_skin(pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3504                                         break;
3505                                 case COLOR_TEAM4:
3506                                         drawpic_aspect_skin(pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3507                                         break;
3508                                 default:
3509                                         break;
3510                         }
3511                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
3512                         {
3513                                 case 0:
3514                                         drawpic_aspect_skin(p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3515                                         break;
3516                                 case 1:
3517                                         drawpic_aspect_skin(p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3518                                         break;
3519                                 case 2:
3520                                         drawpic_aspect_skin(p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3521                                         break;
3522                                 case 3:
3523                                         drawpic_aspect_skin(p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3524                                         break;
3525                         }
3526                 }
3527                 if(mySize_x > mySize_y)
3528                 {
3529                         p_x += 0.25 * mySize_x;
3530                         pa_x += 0.25 * mySize_x;
3531                 }
3532                 else
3533                 {
3534                         if(i == 1)
3535                         {
3536                                 p_y = pos_y + 0.625 * mySize_y;
3537                                 pa_y = pos_y + 0.5 * mySize_y;
3538                                 p_x = pos_x;
3539                                 pa_x = pos_x;
3540                         }
3541                         else
3542                         {
3543                                 p_x += 0.5 * mySize_x;
3544                                 pa_x += 0.5 * mySize_x;
3545                         }
3546                 }
3547         }
3548 }
3549
3550 // Keepaway HUD mod icon
3551 float kaball_prevstatus; // last remembered status
3552 float kaball_statuschange_time; // time when the status changed
3553
3554 // we don't need to reset for keepaway since it immediately 
3555 // autocorrects prevstatus as to if the player has the ball or not
3556
3557 void HUD_Mod_Keepaway(vector pos, vector mySize)
3558 {
3559         mod_active = 1; // keepaway should always show the mod HUD
3560         
3561         float BLINK_FACTOR = 0.15;
3562         float BLINK_BASE = 0.85;
3563         float BLINK_FREQ = 5; 
3564         float kaball_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
3565         
3566         float stat_items = getstati(STAT_ITEMS);
3567         float kaball = (stat_items/IT_KEY1) & 1;
3568         
3569         if(kaball != kaball_prevstatus)
3570         {
3571                 kaball_statuschange_time = time;
3572                 kaball_prevstatus = kaball;
3573         }
3574         
3575         vector kaball_pos, kaball_size;
3576         
3577         if(mySize_x > mySize_y) {
3578                 kaball_pos = pos + eX * 0.25 * mySize_x;
3579                 kaball_size = eX * 0.5 * mySize_x + eY * mySize_y;
3580         } else {
3581                 kaball_pos = pos + eY * 0.25 * mySize_y;
3582                 kaball_size = eY * 0.5 * mySize_y + eX * mySize_x;
3583         }
3584         
3585         float kaball_statuschange_elapsedtime = time - kaball_statuschange_time;
3586         float f = bound(0, kaball_statuschange_elapsedtime*2, 1);
3587         
3588         if(kaball_prevstatus && f < 1)
3589                 drawpic_aspect_skin_expanding(kaball_pos, "keepawayball_carrying", kaball_size, '1 1 1', panel_fg_alpha * kaball_alpha, DRAWFLAG_NORMAL, f);
3590         
3591         if(kaball)
3592                 drawpic_aspect_skin(pos, "keepawayball_carrying", eX * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha * kaball_alpha * f, DRAWFLAG_NORMAL);
3593 }
3594
3595
3596 // Nexball HUD mod icon
3597 void HUD_Mod_NexBall(vector pos, vector mySize)
3598 {
3599         float stat_items, nb_pb_starttime, dt, p;
3600
3601         stat_items = getstati(STAT_ITEMS);
3602         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
3603
3604         if (stat_items & IT_KEY1)
3605                 mod_active = 1;
3606         else
3607                 mod_active = 0;
3608
3609         //Manage the progress bar if any
3610         if (nb_pb_starttime > 0)
3611         {
3612                 dt = mod(time - nb_pb_starttime, nb_pb_period);
3613                 // one period of positive triangle
3614                 p = 2 * dt / nb_pb_period;
3615                 if (p > 1)
3616                         p = 2 - p;
3617
3618                 //Draw the filling
3619                 HUD_Panel_GetProgressBarColor(nexball);
3620                 if(mySize_x > mySize_y)
3621                         HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", p, 0, 0, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
3622                 else
3623                         HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", p, 1, 0, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
3624         }
3625
3626         if (stat_items & IT_KEY1)
3627                 drawpic_aspect_skin(pos, "nexball_carrying", eX * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3628 }
3629
3630 // Race/CTS HUD mod icons
3631 float crecordtime_prev; // last remembered crecordtime
3632 float crecordtime_change_time; // time when crecordtime last changed
3633 float srecordtime_prev; // last remembered srecordtime
3634 float srecordtime_change_time; // time when srecordtime last changed
3635
3636 float race_status_time;
3637 float race_status_prev;
3638 string race_status_name_prev;
3639 void HUD_Mod_Race(vector pos, vector mySize)
3640 {
3641         mod_active = 1; // race should never hide the mod icons panel
3642         entity me;
3643         me = playerslots[player_localentnum - 1];
3644         float t, score;
3645         float f; // yet another function has this
3646         score = me.(scores[ps_primary]);
3647
3648         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
3649                 return; // no records in the actual race
3650
3651         // clientside personal record
3652         string rr;
3653         if(gametype == GAME_CTS)
3654                 rr = CTS_RECORD;
3655         else
3656                 rr = RACE_RECORD;
3657         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
3658
3659         if(score && (score < t || !t)) {
3660                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
3661                 if(autocvar_cl_autodemo_delete_keeprecords)
3662                 {
3663                         f = autocvar_cl_autodemo_delete;
3664                         f &~= 1;
3665                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
3666                 }
3667         }
3668
3669         if(t != crecordtime_prev) {
3670                 crecordtime_prev = t;
3671                 crecordtime_change_time = time;
3672         }
3673
3674         vector textPos, medalPos;
3675         float squareSize;
3676         if(mySize_x > mySize_y) {
3677                 // text on left side
3678                 squareSize = min(mySize_y, mySize_x/2);
3679                 textPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eY * 0.5 * (mySize_y - squareSize);
3680                 medalPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eX * 0.5 * mySize_x + eY * 0.5 * (mySize_y - squareSize);
3681         } else {
3682                 // text on top
3683                 squareSize = min(mySize_x, mySize_y/2);
3684                 textPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eX * 0.5 * (mySize_x - squareSize);
3685                 medalPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eY * 0.5 * mySize_y + eX * 0.5 * (mySize_x - squareSize);
3686         }
3687
3688         f = time - crecordtime_change_time;
3689
3690         if (f > 1) {
3691                 drawstring_aspect(textPos, _("Personal best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3692                 drawstring_aspect(textPos + eY * 0.25 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3693         } else {
3694                 drawstring_aspect(textPos, _("Personal best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3695                 drawstring_aspect(textPos + eY * 0.25 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3696                 drawstring_aspect_expanding(pos, _("Personal best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
3697                 drawstring_aspect_expanding(pos + eY * 0.25 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
3698         }
3699
3700         // server record
3701         t = race_server_record;
3702         if(t != srecordtime_prev) {
3703                 srecordtime_prev = t;
3704                 srecordtime_change_time = time;
3705         }
3706         f = time - srecordtime_change_time;
3707
3708         if (f > 1) {
3709                 drawstring_aspect(textPos + eY * 0.5 * squareSize, _("Server best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3710                 drawstring_aspect(textPos + eY * 0.75 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3711         } else {
3712                 drawstring_aspect(textPos + eY * 0.5 * squareSize, _("Server best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3713                 drawstring_aspect(textPos + eY * 0.75 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3714                 drawstring_aspect_expanding(textPos + eY * 0.5 * squareSize, _("Server best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
3715                 drawstring_aspect_expanding(textPos + eY * 0.75 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
3716         }
3717
3718         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
3719                 race_status_time = time + 5;
3720                 race_status_prev = race_status;
3721                 if (race_status_name_prev)
3722                         strunzone(race_status_name_prev);
3723                 race_status_name_prev = strzone(race_status_name);
3724         }
3725
3726         // race "awards"
3727         float a;
3728         a = bound(0, race_status_time - time, 1);
3729
3730         string s;
3731         s = textShortenToWidth(race_status_name, squareSize, '1 1 0' * 0.1 * squareSize, stringwidth_colors);
3732
3733         float rank;
3734         if(race_status > 0)
3735                 rank = race_CheckName(race_status_name);
3736         string rankname;
3737         rankname = race_PlaceName(rank);
3738
3739         vector namepos;
3740         namepos = medalPos + '0 0.8 0' * squareSize;
3741         vector rankpos;
3742         rankpos = medalPos + '0 0.15 0' * squareSize;
3743
3744         if(race_status == 0)
3745                 drawpic_aspect_skin(medalPos, "race_newfail", '1 1 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3746         else if(race_status == 1) {
3747                 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newtime", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3748                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3749                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3750         } else if(race_status == 2) {
3751                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
3752                         drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrankgreen", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3753                 else
3754                         drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrankyellow", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3755                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3756                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3757         } else if(race_status == 3) {
3758                 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrecordserver", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3759                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3760                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3761         }
3762
3763         if (race_status_time - time <= 0) {
3764                 race_status_prev = -1;
3765                 race_status = -1;
3766                 if(race_status_name)
3767                         strunzone(race_status_name);
3768                 race_status_name = string_null;
3769                 if(race_status_name_prev)
3770                         strunzone(race_status_name_prev);
3771                 race_status_name_prev = string_null;
3772         }
3773 }
3774
3775 void DrawDomItem(vector myPos, vector mySize, float aspect_ratio, float layout, float i)
3776 {
3777         float stat, pps_ratio;
3778         string pic;
3779         vector color;
3780         switch(i)
3781         {
3782                 case 0:
3783                         stat = getstatf(STAT_DOM_PPS_RED);
3784                         pic = "dom_icon_red";
3785                         color = '1 0 0';
3786                         break;
3787                 case 1:
3788                         stat = getstatf(STAT_DOM_PPS_BLUE);
3789                         pic = "dom_icon_blue";
3790                         color = '0 0 1';
3791                         break;
3792                 case 2:
3793                         stat = getstatf(STAT_DOM_PPS_YELLOW);
3794                         pic = "dom_icon_yellow";
3795                         color = '1 1 0';
3796                         break;
3797                 case 3:
3798                         stat = getstatf(STAT_DOM_PPS_PINK);
3799                         pic = "dom_icon_pink";
3800                         color = '1 0 1';
3801         }
3802         pps_ratio = stat / getstatf(STAT_DOM_TOTAL_PPS);
3803
3804         if(mySize_x/mySize_y > aspect_ratio)
3805         {
3806                 i = aspect_ratio * mySize_y;
3807                 myPos_x = myPos_x + (mySize_x - i) / 2;
3808                 mySize_x = i;
3809         }
3810         else
3811         {
3812                 i = 1/aspect_ratio * mySize_x;
3813                 myPos_y = myPos_y + (mySize_y - i) / 2;
3814                 mySize_y = i;
3815         }
3816
3817         if (layout) // show text too
3818         {
3819                 //draw the text
3820                 color *= 0.5 + pps_ratio * (1 - 0.5); // half saturated color at min, full saturated at max
3821                 if (layout == 2) // average pps
3822                         drawstring_aspect(myPos + eX * mySize_y, ftos_decimals(stat, 2), eX * (2/3) * mySize_x + eY * mySize_y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
3823                 else // percentage of average pps
3824                         drawstring_aspect(myPos + eX * mySize_y, strcat( ftos(floor(pps_ratio*100 + 0.5)), "%" ), eX * (2/3) * mySize_x + eY * mySize_y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
3825         }
3826
3827         //draw the icon
3828         drawpic_aspect_skin(myPos, pic, '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3829         if (stat > 0)
3830         {
3831                 drawsetcliparea(myPos_x, myPos_y + mySize_y * (1 - pps_ratio), mySize_y, mySize_y * pps_ratio);
3832                 drawpic_aspect_skin(myPos, strcat(pic, "-highlighted"), '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3833                 drawresetcliparea();
3834         }
3835 }
3836
3837 void HUD_Mod_Dom(vector myPos, vector mySize)
3838 {
3839         mod_active = 1; // required in each mod function that always shows something
3840         entity tm;
3841         float teams_count;
3842         for(tm = teams.sort_next; tm; tm = tm.sort_next)
3843                 if(tm.team != COLOR_SPECTATOR)
3844                         ++teams_count;
3845
3846         float layout = autocvar_hud_panel_modicons_dom_layout;
3847         float rows, columns, aspect_ratio;
3848         rows = mySize_y/mySize_x;
3849         aspect_ratio = (layout) ? 3 : 1;
3850         rows = bound(1, floor((sqrt((4 * aspect_ratio * teams_count + rows) * rows) + rows + 0.5) / 2), teams_count);
3851         columns = ceil(teams_count/rows);
3852
3853         int i;
3854         float row, column;
3855         for(i=0; i<teams_count; ++i)
3856         {
3857                 vector pos, itemSize;
3858                 pos = myPos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows);
3859                 itemSize = eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows);
3860
3861                 DrawDomItem(pos, itemSize, aspect_ratio, layout, i);
3862
3863                 ++row;
3864                 if(row >= rows)
3865                 {
3866                         row = 0;
3867                         ++column;
3868                 }
3869         }
3870 }
3871
3872 float mod_prev; // previous state of mod_active to check for a change
3873 float mod_alpha;
3874 float mod_change; // "time" when mod_active changed
3875
3876 void HUD_ModIcons(void)
3877 {
3878         if(!autocvar__hud_configure)
3879         {
3880                 if(!autocvar_hud_panel_modicons) return;
3881                 if (gametype != GAME_CTF && gametype != GAME_KEYHUNT && gametype != GAME_NEXBALL && gametype != GAME_CTS && gametype != GAME_RACE && gametype != GAME_CA && gametype != GAME_FREEZETAG && gametype != GAME_KEEPAWAY && gametype != GAME_DOMINATION) return;
3882         }
3883         else
3884                 hud_configure_active_panel = HUD_PANEL_MODICONS;
3885
3886         HUD_Panel_UpdateCvars(modicons);
3887         vector pos, mySize;
3888         pos = panel_pos;
3889         mySize = panel_size;
3890
3891         if(mod_active != mod_prev) {
3892                 mod_change = time;
3893                 mod_prev = mod_active;
3894         }
3895
3896         if(mod_active || autocvar__hud_configure)
3897                 mod_alpha = bound(0, (time - mod_change) * 2, 1);
3898         else
3899                 mod_alpha = bound(0, 1 - (time - mod_change) * 2, 1);
3900
3901         if(mod_alpha)
3902                 HUD_Panel_DrawBg(mod_alpha);
3903
3904         if(panel_bg_padding)
3905         {
3906                 pos += '1 1 0' * panel_bg_padding;
3907                 mySize -= '2 2 0' * panel_bg_padding;
3908         }
3909
3910         // these MUST be ran in order to update mod_active
3911         if(gametype == GAME_KEYHUNT)
3912                 HUD_Mod_KH(pos, mySize);
3913         else if(gametype == GAME_CTF || autocvar__hud_configure)
3914                 HUD_Mod_CTF(pos, mySize); // forcealpha only needed for ctf icons, as only they are shown in config mode
3915         else if(gametype == GAME_NEXBALL)
3916                 HUD_Mod_NexBall(pos, mySize);
3917         else if(gametype == GAME_CTS || gametype == GAME_RACE)
3918                 HUD_Mod_Race(pos, mySize);
3919         else if(gametype == GAME_CA || gametype == GAME_FREEZETAG)
3920                 HUD_Mod_CA(pos, mySize);
3921         else if(gametype == GAME_DOMINATION)
3922                 HUD_Mod_Dom(pos, mySize);
3923         else if(gametype == GAME_KEEPAWAY)
3924                 HUD_Mod_Keepaway(pos, mySize);
3925 }
3926
3927 // Draw pressed keys (#11)
3928 //
3929 void HUD_DrawPressedKeys(void)
3930 {
3931         if(!autocvar__hud_configure)
3932         {
3933                 if(!autocvar_hud_panel_pressedkeys) return;
3934                 if(spectatee_status <= 0 && autocvar_hud_panel_pressedkeys < 2) return;
3935         }
3936         else
3937                 hud_configure_active_panel = HUD_PANEL_PRESSEDKEYS;
3938
3939
3940         HUD_Panel_UpdateCvars(pressedkeys);
3941         vector pos, mySize;
3942         pos = panel_pos;
3943         mySize = panel_size;
3944
3945         HUD_Panel_DrawBg(1);
3946         if(panel_bg_padding)
3947         {
3948                 pos += '1 1 0' * panel_bg_padding;
3949                 mySize -= '2 2 0' * panel_bg_padding;
3950         }
3951
3952         // force custom aspect
3953         float aspect = autocvar_hud_panel_pressedkeys_aspect;
3954         if(aspect)
3955         {
3956                 vector newSize;
3957                 if(mySize_x/mySize_y > aspect)
3958                 {
3959                         newSize_x = aspect * mySize_y;
3960                         newSize_y = mySize_y;
3961
3962                         pos_x = pos_x + (mySize_x - newSize_x) / 2;
3963                 }
3964                 else
3965                 {
3966                         newSize_y = 1/aspect * mySize_x;
3967                         newSize_x = mySize_x;
3968
3969                         pos_y = pos_y + (mySize_y - newSize_y) / 2;
3970                 }
3971                 mySize = newSize;
3972         }
3973
3974         vector keysize;
3975         keysize = eX * mySize_x * (1/3) + eY * mySize_y * 0.5;
3976         float pressedkeys;
3977         pressedkeys = getstatf(STAT_PRESSED_KEYS);
3978
3979         drawpic_aspect_skin(pos, ((pressedkeys & KEY_CROUCH) ? "key_crouch_inv.tga" : "key_crouch.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3980         drawpic_aspect_skin(pos + eX * keysize_x, ((pressedkeys & KEY_FORWARD) ? "key_forward_inv.tga" : "key_forward.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3981         drawpic_aspect_skin(pos + eX * keysize_x * 2, ((pressedkeys & KEY_JUMP) ? "key_jump_inv.tga" : "key_jump.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3982         pos_y += keysize_y;
3983         drawpic_aspect_skin(pos, ((pressedkeys & KEY_LEFT) ? "key_left_inv.tga" : "key_left.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3984         drawpic_aspect_skin(pos + eX * keysize_x, ((pressedkeys & KEY_BACKWARD) ? "key_backward_inv.tga" : "key_backward.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3985         drawpic_aspect_skin(pos + eX * keysize_x * 2, ((pressedkeys & KEY_RIGHT) ? "key_right_inv.tga" : "key_right.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3986 }
3987
3988 // Handle chat as a panel (#12)
3989 //
3990 void HUD_Chat(void)
3991 {
3992         if(!autocvar__hud_configure)
3993         {
3994                 if (!autocvar_hud_panel_chat)
3995                 {
3996                         if (!autocvar_con_chatrect)
3997                                 cvar_set("con_chatrect", "0");
3998                         return;
3999                 }
4000         }
4001         else
4002                 hud_configure_active_panel = HUD_PANEL_CHAT;
4003
4004         HUD_Panel_UpdateCvars(chat);
4005
4006         if(autocvar__con_chat_maximized && !autocvar__hud_configure) // draw at full screen height if maximized
4007         {
4008                 panel_pos_y = panel_bg_border;
4009                 panel_size_y = vid_conheight - panel_bg_border * 2;
4010                 if(panel_bg == "0") // force a border when maximized
4011                 {
4012                         if(precache_pic(panel_bg) == "") {
4013                                 panel_bg = strcat(hud_skin_path, "/border_default");
4014                                 if(precache_pic(panel_bg) == "") {
4015                                         panel_bg = "gfx/hud/default/border_default";
4016                                 }
4017                         }
4018                 }
4019                 panel_bg_alpha = max(0.75, panel_bg_alpha); // force an alpha of at least 0.75
4020         }
4021
4022         vector pos, mySize;
4023         pos = panel_pos;
4024         mySize = panel_size;
4025
4026         HUD_Panel_DrawBg(1);
4027
4028         if(panel_bg_padding)
4029         {
4030                 pos += '1 1 0' * panel_bg_padding;
4031                 mySize -= '2 2 0' * panel_bg_padding;
4032         }
4033
4034         if (!autocvar_con_chatrect)
4035                 cvar_set("con_chatrect", "1");
4036
4037         cvar_set("con_chatrect_x", ftos(pos_x/vid_conwidth));
4038         cvar_set("con_chatrect_y", ftos(pos_y/vid_conheight));
4039
4040         cvar_set("con_chatwidth", ftos(mySize_x/vid_conwidth));
4041         cvar_set("con_chat", ftos(floor(mySize_y/autocvar_con_chatsize - 0.5)));
4042
4043         if(autocvar__hud_configure)
4044         {
4045                 vector chatsize;
4046                 chatsize = '1 1 0' * autocvar_con_chatsize;
4047                 cvar_set("con_chatrect_x", "9001"); // over 9000, we'll fake it instead for more control over alpha and such
4048                 float i, a;
4049                 for(i = 0; i < autocvar_con_chat; ++i)
4050                 {
4051                         if(i == autocvar_con_chat - 1)
4052                                 a = panel_fg_alpha;
4053                         else
4054                                 a = panel_fg_alpha * floor(((i + 1) * 7 + autocvar_con_chattime)/45);
4055                         drawcolorcodedstring(pos, textShortenToWidth(_("^3Player^7: This is the chat area."), mySize_x, chatsize, stringwidth_colors), chatsize, a, DRAWFLAG_NORMAL);
4056                         pos_y += chatsize_y;
4057                 }
4058         }
4059 }
4060
4061 // Engine info panel (#13)
4062 //
4063 float prevfps;
4064 float prevfps_time;
4065 float framecounter;
4066
4067 float frametimeavg;
4068 float frametimeavg1; // 1 frame ago
4069 float frametimeavg2; // 2 frames ago
4070 void HUD_EngineInfo(void)
4071 {
4072         if(!autocvar__hud_configure)
4073         {
4074                 if(!autocvar_hud_panel_engineinfo) return;
4075         }
4076         else
4077                 hud_configure_active_panel = HUD_PANEL_ENGINEINFO;
4078
4079         HUD_Panel_UpdateCvars(engineinfo);
4080         vector pos, mySize;
4081         pos = panel_pos;
4082         mySize = panel_size;
4083
4084         HUD_Panel_DrawBg(1);
4085         if(panel_bg_padding)
4086         {
4087                 pos += '1 1 0' * panel_bg_padding;
4088                 mySize -= '2 2 0' * panel_bg_padding;
4089         }
4090
4091         float currentTime = gettime(GETTIME_REALTIME);
4092         if(cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage"))
4093         {
4094                 float currentframetime = currentTime - prevfps_time;
4095                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + currentframetime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
4096                 frametimeavg2 = frametimeavg1;
4097                 frametimeavg1 = frametimeavg;
4098                 
4099                 float weight;
4100                 weight = cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight");
4101                 if(currentframetime > 0.0001) // filter out insane values which sometimes seem to occur and throw off the average? If you are getting 10,000 fps or more, then you don't need a framerate counter.
4102                 {
4103                         if(fabs(prevfps - (1/frametimeavg)) > prevfps * cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage_instantupdate_change_threshold")) // if there was a big jump in fps, just force prevfps at current (1/currentframetime) to make big updates instant
4104                                 prevfps = (1/currentframetime);
4105                         prevfps = (1 - weight) * prevfps + weight * (1/frametimeavg); // framecounter just used so there's no need for a new variable, think of it as "frametime average"
4106                 }
4107                 prevfps_time = currentTime;
4108         }
4109         else
4110         {
4111                 framecounter += 1;
4112                 if(currentTime - prevfps_time > autocvar_hud_panel_engineinfo_framecounter_time)
4113                 {
4114                         prevfps = framecounter/(currentTime - prevfps_time);
4115                         framecounter = 0;
4116                         prevfps_time = currentTime;
4117                 }
4118         }
4119
4120         vector color;
4121         color = HUD_Get_Num_Color (prevfps, 100);
4122         drawstring_aspect(pos, sprintf(_("FPS: %.*f"), autocvar_hud_panel_engineinfo_framecounter_decimals, prevfps), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
4123 }
4124
4125 // Info messages panel (#14)
4126 //
4127 #define drawInfoMessage(s)\
4128         if(autocvar_hud_panel_infomessages_flip)\
4129                 o_x = pos_x + mySize_x - stringwidth(s, TRUE, fontsize);\
4130         drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);\
4131         o_y += fontsize_y;
4132 void HUD_InfoMessages(void)
4133 {
4134         if(!autocvar__hud_configure)
4135         {
4136                 if(!autocvar_hud_panel_infomessages) return;
4137         }
4138         else
4139                 hud_configure_active_panel = HUD_PANEL_INFOMESSAGES;
4140
4141         HUD_Panel_UpdateCvars(infomessages);
4142         vector pos, mySize;
4143         pos = panel_pos;
4144         mySize = panel_size;
4145
4146         HUD_Panel_DrawBg(1);
4147         if(panel_bg_padding)
4148         {
4149                 pos += '1 1 0' * panel_bg_padding;
4150                 mySize -= '2 2 0' * panel_bg_padding;
4151         }
4152
4153         // always force 5:1 aspect
4154         vector newSize;
4155         if(mySize_x/mySize_y > 5)
4156         {
4157                 newSize_x = 5 * mySize_y;
4158                 newSize_y = mySize_y;
4159
4160                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
4161         }
4162         else
4163         {
4164                 newSize_y = 1/5 * mySize_x;
4165                 newSize_x = mySize_x;
4166
4167                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
4168         }
4169
4170         mySize = newSize;
4171         entity tm;
4172         vector o;
4173         o = pos;
4174
4175         vector fontsize;
4176         fontsize = '0.20 0.20 0' * mySize_y;
4177         
4178         float a;
4179         a = panel_fg_alpha;
4180
4181         string s;
4182         if(!autocvar__hud_configure)
4183         {
4184                 if(spectatee_status && !intermission)
4185                 {
4186                         a = 1;
4187                         if(spectatee_status == -1)
4188                                 s = _("^1Observing");
4189                         else
4190                                 s = sprintf(_("^1Spectating: ^7%s"), GetPlayerName(spectatee_status - 1));
4191                         drawInfoMessage(s)
4192
4193                         if(spectatee_status == -1)
4194                                 s = sprintf(_("^1Press ^3%s^1 to spectate"), getcommandkey("primary fire", "+fire"));
4195                         else
4196                                 s = sprintf(_("^1Press ^3%s^1 for another player"), getcommandkey("primary fire", "+fire"));
4197                         drawInfoMessage(s)
4198
4199                         if(spectatee_status == -1)
4200                                 s = sprintf(_("^1Use ^3%s^1 or ^3%s^1 to change the speed"), getcommandkey("next weapon", "weapnext"), getcommandkey("previous weapon", "weapprev"));
4201                         else
4202                                 s = sprintf(_("^1Press ^3%s^1 to observe"), getcommandkey("secondary fire", "+fire2"));
4203                         drawInfoMessage(s)
4204
4205                         s = sprintf(_("^1Press ^3%s^1 for gamemode info"), getcommandkey("server info", "+show_info"));
4206                         drawInfoMessage(s)
4207
4208                         if(gametype == GAME_ARENA)
4209                                 s = _("^1Wait for your turn to join");
4210                         else if(gametype == GAME_LMS)
4211                         {
4212                                 entity sk;
4213                                 sk = playerslots[player_localentnum - 1];
4214                                 if(sk.(scores[ps_primary]) >= 666)
4215                                         s = _("^1Match has already begun");
4216                                 else if(sk.(scores[ps_primary]) > 0)
4217                                         s = _("^1You have no more lives left");
4218                                 else
4219                                         s = sprintf(_("^1Press ^3%s^1 to join"), getcommandkey("jump", "+jump"));
4220                         }
4221                         else
4222                                 s = sprintf(_("^1Press ^3%s^1 to join"), getcommandkey("jump", "+jump"));
4223                         drawInfoMessage(s)
4224
4225                         //show restart countdown:
4226                         if (time < getstatf(STAT_GAMESTARTTIME)) {
4227                                 float countdown;
4228                                 //we need to ceil, otherwise the countdown would be off by .5 when using round()
4229                                 countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
4230                                 s = sprintf(_("^1Game starts in ^3%d^1 seconds"), countdown);
4231                                 drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);
4232                                 o_y += fontsize_y;
4233                         }
4234                 }
4235                 if(warmup_stage && !intermission)
4236                 {
4237                         s = _("^2Currently in ^1warmup^2 stage!");
4238                         drawInfoMessage(s)
4239                 }
4240
4241                 string blinkcolor;
4242                 if(mod(time, 1) >= 0.5)
4243                         blinkcolor = "^1";
4244                 else
4245                         blinkcolor = "^3";
4246
4247                 if(ready_waiting && !intermission && !spectatee_status)
4248                 {
4249                         if(ready_waiting_for_me)
4250                         {
4251                                 if(warmup_stage)
4252                                         s = sprintf(_("%sPress ^3%s%s to end warmup"), blinkcolor, getcommandkey("ready", "ready"), blinkcolor);
4253                                 else
4254                                         s = sprintf(_("%sPress ^3%s%s once you are ready"), blinkcolor, getcommandkey("ready", "ready"), blinkcolor);
4255                         }
4256                         else
4257                         {
4258                                 if(warmup_stage)
4259                                         s = _("^2Waiting for others to ready up to end warmup...");
4260                                 else
4261                                         s = _("^2Waiting for others to ready up...");
4262                         }
4263                         drawInfoMessage(s)
4264                 }
4265                 else if(warmup_stage && !intermission && !spectatee_status)
4266                 {
4267                         s = sprintf(_("^2Press ^3%s^2 to end warmup"), getcommandkey("ready", "ready"));
4268                         drawInfoMessage(s)
4269                 }
4270
4271                 if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
4272                 {
4273                         float ts_min, ts_max;
4274                         tm = teams.sort_next;
4275                         if (tm)
4276                         {
4277                                 for(; tm.sort_next; tm = tm.sort_next)
4278                                 {
4279                                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
4280                                                 continue;
4281                                         if(!ts_min) ts_min = tm.team_size;
4282                                         else ts_min = min(ts_min, tm.team_size);
4283                                         if(!ts_max) ts_max = tm.team_size;
4284                                         else ts_max = max(ts_max, tm.team_size);
4285                                 }
4286                                 if ((ts_max - ts_min) > 1)
4287                                 {
4288                                         s = strcat(blinkcolor, _("Teamnumbers are unbalanced!"));
4289                                         tm = GetTeam(myteam, false);
4290                                         if (tm)
4291                                         if (tm.team != COLOR_SPECTATOR)
4292                                         if (tm.team_size == ts_max)
4293                                                 s = strcat(s, sprintf(_(" Press ^3%s%s to adjust"), getcommandkey("team menu", "menu_showteamselect"), blinkcolor));
4294                                         drawInfoMessage(s)
4295                                 }
4296                         }
4297                 }
4298         }
4299         else 
4300         {
4301                 s = _("^7Press ^3ESC ^7to show HUD options.");
4302                 drawInfoMessage(s)
4303                 s = _("^3Doubleclick ^7a panel for panel-specific options.");
4304                 drawInfoMessage(s)
4305                 s = _("^3CTRL ^7to disable collision testing, ^3SHIFT ^7and");
4306                 drawInfoMessage(s)
4307                 s = _("^3ALT ^7+ ^3ARROW KEYS ^7for fine adjustments.");
4308                 drawInfoMessage(s)
4309         }
4310 }
4311
4312 // Physics panel (#15)
4313 //
4314 vector acc_prevspeed;
4315 float acc_prevtime, acc_avg, top_speed, top_speed_time;
4316 void HUD_Physics(void)
4317 {
4318         if(!autocvar__hud_configure)
4319         {
4320                 if(!autocvar_hud_panel_physics) return;
4321                 if(spectatee_status == -1 && autocvar_hud_panel_physics < 2) return;
4322         }
4323         else
4324                 hud_configure_active_panel = HUD_PANEL_PHYSICS;
4325
4326         HUD_Panel_UpdateCvars(physics);
4327
4328         HUD_Panel_DrawBg(1);
4329         if(panel_bg_padding)
4330         {
4331                 panel_pos += '1 1 0' * panel_bg_padding;
4332                 panel_size -= '2 2 0' * panel_bg_padding;
4333         }
4334
4335         //compute speed
4336         float speed, conversion_factor;
4337         string unit;
4338
4339         switch(autocvar_hud_panel_physics_speed_unit)
4340         {
4341                 default:
4342                 case 1:
4343                         unit = _(" qu/s");
4344                         conversion_factor = 1.0;
4345                         break;
4346                 case 2:
4347                         unit = _(" m/s");
4348                         conversion_factor = 0.0254;
4349                         break;
4350                 case 3:
4351                         unit = _(" km/h");
4352                         conversion_factor = 0.0254 * 3.6;
4353                         break;
4354                 case 4:
4355                         unit = _(" mph");
4356                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
4357                         break;
4358                 case 5:
4359                         unit = _(" knots");
4360                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
4361                         break;
4362         }
4363
4364         float max_speed = floor( autocvar_hud_panel_physics_speed_max * conversion_factor + 0.5 );
4365         if (autocvar__hud_configure)
4366                 speed = floor( max_speed * 0.65 + 0.5 );
4367         else if(autocvar_hud_panel_physics_speed_vertical)
4368                 speed = floor( vlen(pmove_vel) * conversion_factor + 0.5 );
4369         else
4370                 speed = floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 );
4371
4372         //compute acceleration
4373         float acceleration, f;
4374         if (autocvar__hud_configure)
4375                 acceleration = autocvar_hud_panel_physics_acceleration_max * 0.3;
4376         else
4377         {
4378                 // 1 m/s = 0.0254 qu/s; 1 g = 9.80665 m/s^2
4379                 f = time - acc_prevtime;
4380                 if(autocvar_hud_panel_physics_acceleration_vertical)
4381                         acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f) * (0.0254 / 9.80665);
4382                 else
4383                         acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f) * (0.0254 / 9.80665);
4384                 acc_prevspeed = pmove_vel;
4385                 acc_prevtime = time;
4386
4387                 f = bound(0, f * 10, 1);
4388                 acc_avg = acc_avg * (1 - f) + acceleration * f;
4389         }
4390
4391         //compute layout
4392         float panel_ar = panel_size_x/panel_size_y;
4393         vector speed_offset, acceleration_offset;
4394         if (panel_ar >= 5)
4395         {
4396                 panel_size_x *= 0.5;
4397                 if (autocvar_hud_panel_physics_flip)
4398                         speed_offset_x = panel_size_x;
4399                 else
4400                         acceleration_offset_x = panel_size_x;
4401         }
4402         else
4403         {
4404                 panel_size_y *= 0.5;
4405                 if (autocvar_hud_panel_physics_flip)
4406                         speed_offset_y = panel_size_y;
4407                 else
4408                         acceleration_offset_y = panel_size_y;
4409         }
4410         float speed_baralign, acceleration_baralign;
4411         if (autocvar_hud_panel_physics_baralign == 1)
4412                 acceleration_baralign = speed_baralign = 1;
4413     else if(autocvar_hud_panel_physics_baralign == 4)
4414                 acceleration_baralign = speed_baralign = 2;
4415         else if (autocvar_hud_panel_physics_flip)
4416         {
4417                 acceleration_baralign = (autocvar_hud_panel_physics_baralign == 2);
4418                 speed_baralign = (autocvar_hud_panel_physics_baralign == 3);
4419         }
4420         else
4421         {
4422                 speed_baralign = (autocvar_hud_panel_physics_baralign == 2);
4423                 acceleration_baralign = (autocvar_hud_panel_physics_baralign == 3);
4424         }
4425         if (autocvar_hud_panel_physics_acceleration_progressbar_mode == 0)
4426                 acceleration_baralign = 3; //override hud_panel_physics_baralign value for acceleration
4427
4428         //draw speed
4429         if(speed)
4430         if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 2)
4431         {
4432                 HUD_Panel_GetProgressBarColor(speed);
4433                 HUD_Panel_DrawProgressBar(panel_pos + speed_offset, panel_size, "progressbar", speed/max_speed, 0, speed_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4434         }
4435         vector tmp_offset, tmp_size;
4436         if (autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 2)
4437         {
4438                 tmp_size_x = panel_size_x * 0.75;
4439                 tmp_size_y = panel_size_y;
4440                 if (speed_baralign)
4441                         tmp_offset_x = panel_size_x - tmp_size_x;
4442                 //else
4443                         //tmp_offset_x = 0;
4444                 drawstring_aspect(panel_pos + speed_offset + tmp_offset, ftos(speed), tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4445
4446                 //draw speed unit
4447                 if (speed_baralign)
4448                         tmp_offset_x = 0;
4449                 else
4450                         tmp_offset_x = tmp_size_x;
4451                 if (autocvar_hud_panel_physics_speed_unit_show)
4452                 {
4453                         //tmp_offset_y = 0;
4454                         tmp_size_x = panel_size_x * (1 - 0.75);
4455                         tmp_size_y = panel_size_y * 0.4;
4456                         drawstring_aspect(panel_pos + speed_offset + tmp_offset, unit, tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4457                 }
4458         }
4459
4460         //compute and draw top speed
4461         if (autocvar_hud_panel_physics_topspeed)
4462         if (autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 2)
4463         {
4464                 if (autocvar__hud_configure)
4465                 {
4466                         top_speed = floor( max_speed * 0.75 + 0.5 );
4467                         f = 1;
4468                 }
4469                 else
4470                 {
4471                         if (speed >= top_speed)
4472                         {
4473                                 top_speed = speed;
4474                                 top_speed_time = time;
4475                         }
4476                         if (top_speed != 0)
4477                         {
4478                                 f = max(1, autocvar_hud_panel_physics_topspeed_time);
4479                                 // divide by f to make it start from 1
4480                                 f = cos( ((time - top_speed_time) / f) * PI/2 );
4481                         }
4482             else //hide top speed 0, it would be stupid
4483                                 f = 0;
4484                 }
4485                 if (f > 0)
4486                 {
4487                         //top speed progressbar peak
4488                         if(speed < top_speed)
4489                         if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 2)
4490                         {
4491                                 float peak_offset_x;
4492                                 vector peak_size;
4493                                 if (speed_baralign == 0)
4494                                         peak_offset_x = min(top_speed, max_speed)/max_speed * panel_size_x;
4495                 else if (speed_baralign == 1)
4496                                         peak_offset_x = (1 - min(top_speed, max_speed)/max_speed) * panel_size_x;
4497                 else if (speed_baralign == 2)
4498                     peak_offset_x = min(top_speed, max_speed)/max_speed * panel_size_x * 0.5;
4499                                 //if speed is not 0 the speed progressbar already fetched the color
4500                                 if (speed == 0)
4501                                         HUD_Panel_GetProgressBarColor(speed);
4502                                 peak_size_x = floor(panel_size_x * 0.01 + 1.5);
4503                 peak_size_y = panel_size_y;
4504                 if (speed_baralign == 2) // draw two peaks, on both sides
4505                 {
4506                     drawfill(panel_pos + speed_offset + eX * (0.5 * panel_size_x + peak_offset_x - peak_size_x), peak_size, progressbar_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4507                     drawfill(panel_pos + speed_offset + eX * (0.5 * panel_size_x - peak_offset_x + peak_size_x), peak_size, progressbar_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4508                 }
4509                 else
4510                     drawfill(panel_pos + speed_offset + eX * (peak_offset_x - peak_size_x), peak_size, progressbar_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4511                         }
4512
4513                         //top speed
4514                         tmp_offset_y = panel_size_y * 0.4;
4515                         tmp_size_x = panel_size_x * (1 - 0.75);
4516                         tmp_size_y = panel_size_y - tmp_offset_y;
4517                         drawstring_aspect(panel_pos + speed_offset + tmp_offset, ftos(top_speed), tmp_size, '1 0 0', f * panel_fg_alpha, DRAWFLAG_NORMAL);
4518                 }
4519                 else
4520                         top_speed = 0;
4521         }
4522
4523         //draw acceleration
4524         if(acceleration)
4525         if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 3)
4526         {
4527                 if (acceleration < 0)
4528                         HUD_Panel_GetProgressBarColor(acceleration_neg);
4529                 else
4530                         HUD_Panel_GetProgressBarColor(acceleration);
4531                 HUD_Panel_DrawProgressBar(panel_pos + acceleration_offset, panel_size, "accelbar", acceleration/autocvar_hud_panel_physics_acceleration_max, 0, acceleration_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4532         }
4533         if (autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 3)
4534                 drawstring_aspect(panel_pos + acceleration_offset, strcat(ftos_decimals(acceleration, 2), "g"), panel_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4535 }
4536
4537 /*
4538 ==================
4539 Main HUD system
4540 ==================
4541 */
4542
4543 void HUD_Reset (void)
4544 {
4545         // reset gametype specific icons
4546         if(gametype == GAME_KEYHUNT)
4547                 HUD_Mod_KH_Reset();
4548         else if(gametype == GAME_CTF)
4549                 HUD_Mod_CTF_Reset();
4550 }
4551
4552 #define HUD_DrawPanel(id)\
4553 switch (id) {\
4554         case (HUD_PANEL_RADAR):\
4555                 HUD_Radar(); break;\
4556         case (HUD_PANEL_WEAPONS):\
4557                 HUD_Weapons(); break;\
4558         case (HUD_PANEL_AMMO):\
4559                 HUD_Ammo(); break;\
4560         case (HUD_PANEL_POWERUPS):\
4561                 HUD_Powerups(); break;\
4562         case (HUD_PANEL_HEALTHARMOR):\
4563                 HUD_HealthArmor(); break;\
4564         case (HUD_PANEL_NOTIFY):\
4565                 HUD_Notify(); break;\
4566         case (HUD_PANEL_TIMER):\
4567                 HUD_Timer(); break;\
4568         case (HUD_PANEL_SCORE):\
4569                 HUD_Score(); break;\
4570         case (HUD_PANEL_RACETIMER):\
4571                 HUD_RaceTimer(); break;\
4572         case (HUD_PANEL_VOTE):\
4573                 HUD_VoteWindow(); break;\
4574         case (HUD_PANEL_MODICONS):\
4575                 HUD_ModIcons(); break;\
4576         case (HUD_PANEL_PRESSEDKEYS):\
4577                 HUD_DrawPressedKeys(); break;\
4578         case (HUD_PANEL_CHAT):\
4579                 HUD_Chat(); break;\
4580         case (HUD_PANEL_ENGINEINFO):\
4581                 HUD_EngineInfo(); break;\
4582         case (HUD_PANEL_INFOMESSAGES):\
4583                  HUD_InfoMessages(); break;\
4584         case (HUD_PANEL_PHYSICS):\
4585                  HUD_Physics(); break;\
4586 } ENDS_WITH_CURLY_BRACE
4587
4588 void HUD_Main (void)
4589 {
4590         float i;
4591         // global hud alpha fade
4592         if(menu_enabled == 1)
4593                 hud_fade_alpha = 1;
4594         else
4595                 hud_fade_alpha = (1 - autocvar__menu_alpha);
4596
4597         if(scoreboard_fade_alpha)
4598                 hud_fade_alpha = (1 - scoreboard_fade_alpha);
4599
4600         if(intermission == 2) // no hud during mapvote
4601         {
4602                 if (autocvar__hud_configure) //force exit from hud config
4603                 {
4604                         if (menu_enabled)
4605                         {
4606                                 menu_enabled = 0;
4607                                 localcmd("togglemenu\n");
4608                         }
4609                         cvar_set("_hud_configure", "0");
4610                 }
4611                 hud_fade_alpha = 0;
4612         }
4613         else if(autocvar__menu_alpha == 0 && scoreboard_fade_alpha == 0)
4614                 hud_fade_alpha = 1;
4615
4616         if(!autocvar__hud_configure && !hud_fade_alpha)
4617                 return;
4618
4619         // Drawing stuff
4620         if (hud_skin_path != autocvar_hud_skin)
4621         {
4622                 if (hud_skin_path)
4623                         strunzone(hud_skin_path);
4624                 hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
4625         }
4626
4627         // HUD configure visible grid
4628         if(autocvar__hud_configure && autocvar_hud_configure_grid && autocvar_hud_configure_grid_alpha)
4629         {
4630                 hud_configure_gridSize_x = bound(0.005, cvar("hud_configure_grid_xsize"), 0.2);
4631                 hud_configure_gridSize_y = bound(0.005, cvar("hud_configure_grid_ysize"), 0.2);
4632                 hud_configure_realGridSize_x = hud_configure_gridSize_x * vid_conwidth;
4633                 hud_configure_realGridSize_y = hud_configure_gridSize_y * vid_conheight;
4634                 // x-axis
4635                 for(i = 0; i < 1/hud_configure_gridSize_x; ++i)
4636                         drawfill(eX * i * hud_configure_realGridSize_x, eX + eY * vid_conheight, '0.5 0.5 0.5', autocvar_hud_configure_grid_alpha, DRAWFLAG_NORMAL);
4637                 // y-axis
4638                 for(i = 0; i < 1/hud_configure_gridSize_y; ++i)
4639                         drawfill(eY * i * hud_configure_realGridSize_y, eY + eX * vid_conwidth, '0.5 0.5 0.5', autocvar_hud_configure_grid_alpha, DRAWFLAG_NORMAL);
4640         }
4641
4642     current_player = (spectatee_status > 0) ? spectatee_status : player_localentnum;
4643
4644         // draw the dock
4645         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
4646         {
4647                 float f;
4648                 vector color;
4649                 float hud_dock_color_team = autocvar_hud_dock_color_team;
4650                 if((teamplay) && hud_dock_color_team) {
4651                         f = stof(getplayerkey(current_player - 1, "colors"));
4652                         color = colormapPaletteColor(mod(f, 16), 1) * hud_dock_color_team;
4653                 }
4654                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
4655                         color = '1 0 0' * hud_dock_color_team;
4656                 }
4657                 else
4658                 {
4659                         string hud_dock_color = autocvar_hud_dock_color;
4660                         if(hud_dock_color == "shirt") {
4661                                 f = stof(getplayerkey(current_player - 1, "colors"));
4662                                 color = colormapPaletteColor(floor(f / 16), 0);
4663                         }
4664                         else if(hud_dock_color == "pants") {
4665                                 f = stof(getplayerkey(current_player - 1, "colors"));
4666                                 color = colormapPaletteColor(mod(f, 16), 1);
4667                         }
4668                         else
4669                                 color = stov(hud_dock_color);
4670                 }
4671
4672                 string pic;
4673                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
4674                 if(precache_pic(pic) == "") {
4675                         pic = strcat(hud_skin_path, "/dock_medium");
4676                         if(precache_pic(pic) == "") {
4677                                 pic = "gfx/hud/default/dock_medium";
4678                         }
4679                 }
4680                 drawpic('0 0 0', pic, eX * vid_conwidth + eY * vid_conheight, color, autocvar_hud_dock_alpha * hud_fade_alpha, DRAWFLAG_NORMAL); // no aspect ratio forcing on dock...
4681         }
4682
4683         // cache the panel order into the panel_order array
4684         if(autocvar__hud_panelorder != hud_panelorder_prev) {
4685                 for(i = 0; i < HUD_PANEL_NUM; ++i)
4686                         panel_order[i] = -1;
4687                 string s;
4688                 float p_num, warning;
4689                 float argc = tokenize_console(autocvar__hud_panelorder);
4690                 if (argc > HUD_PANEL_NUM)
4691                         warning = true;
4692                 //first detect wrong/missing panel numbers
4693                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
4694                         p_num = stof(argv(i));
4695                         if (p_num >= 0 && p_num < HUD_PANEL_NUM) { //correct panel number?
4696                                 if (panel_order[p_num] == -1) //found for the first time?
4697                                         s = strcat(s, ftos(p_num), " ");
4698                                 panel_order[p_num] = 1; //mark as found
4699                         }
4700                         else
4701                                 warning = true;
4702                 }
4703                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
4704                         if (panel_order[i] == -1) {
4705                                 warning = true;
4706                                 s = strcat(s, ftos(i), " "); //add missing panel number
4707                         }
4708                 }
4709                 if (warning)
4710                         print(_("Automatically fixed wrong/missing panel numbers in _hud_panelorder\n"));
4711
4712                 cvar_set("_hud_panelorder", s);
4713                 if(hud_panelorder_prev)
4714                         strunzone(hud_panelorder_prev);
4715                 hud_panelorder_prev = strzone(s);
4716
4717                 //now properly set panel_order
4718                 tokenize_console(s);
4719                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
4720                         panel_order[i] = stof(argv(i));
4721                 }
4722         }
4723
4724         // draw panels in order specified by panel_order array
4725         for(i = HUD_PANEL_NUM - 1; i >= 0; --i) {
4726                 if(i != HUD_PANEL_CHAT || !autocvar__con_chat_maximized) // don't draw maximized chat panel twice!
4727                         HUD_DrawPanel(panel_order[i]);
4728         }
4729
4730         // draw chat panel on top if it is maximized
4731         if(autocvar__con_chat_maximized)
4732                 HUD_Chat(); // HUD_DrawPanel(HUD_PANEL_CHAT);
4733
4734         if (autocvar__hud_configure && spectatee_status && hud_configure_prev == -1) // try to join if we are in hud_configure mode, but still spectating, and in the first frame (in order to get rid of motd when launching a server via the menu "HUD Setup" button)
4735                 localcmd("cmd selectteam auto; cmd join\n");
4736
4737         if(autocvar__hud_configure && tab_panel != -1)
4738         {
4739                 HUD_Panel_UpdatePosSizeForId(tab_panel)
4740                 drawfill(panel_pos - '1 1 0' * panel_bg_border, panel_size + '2 2 0' * panel_bg_border, '1 1 1', .2, DRAWFLAG_NORMAL);
4741         }
4742
4743         hud_configure_prev = autocvar__hud_configure;
4744
4745         if (!autocvar__hud_configure) // hud config mode disabled, enable normal alpha stuff again
4746                 if (menu_enabled)
4747                         menu_enabled = 0;
4748 }