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