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