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