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