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