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