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