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