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