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