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