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