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