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