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