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