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