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