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