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