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