]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
autocvar for hud_panel_score_rankings
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qc
1 /*
2 ==================
3 Misc HUD functions
4 ==================
5 */
6
7 // a border picture is a texture containing nine parts:
8 //   1/4 width: left part
9 //   1/2 width: middle part (stretched)
10 //   1/4 width: right part
11 // divided into
12 //   1/4 height: top part
13 //   1/2 height: middle part (stretched)
14 //   1/4 height: bottom part
15 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
16 {
17     if (theBorderSize_x < 0 && theBorderSize_y < 0) // draw whole image as it is
18     {
19                 drawpic(theOrigin, pic, theSize, theColor, theAlpha, 0);
20                 return;
21     }
22         if (theBorderSize_x == 0 && theBorderSize_y == 0) // no border
23         {
24                 // draw only the central part
25                 drawsubpic(theOrigin, theSize, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
26                 return;
27         }
28
29         vector dX, dY;
30         vector width, height;
31         vector bW, bH;
32         //pic = draw_UseSkinFor(pic);
33         width = eX * theSize_x;
34         height = eY * theSize_y;
35         if(theSize_x <= theBorderSize_x * 2)
36         {
37                 // not wide enough... draw just left and right then
38                 bW = eX * (0.25 * theSize_x / (theBorderSize_x * 2));
39                 if(theSize_y <= theBorderSize_y * 2)
40                 {
41                         // not high enough... draw just corners
42                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
43                         drawsubpic(theOrigin,                 width * 0.5 + height * 0.5, pic, '0 0 0',           bW + bH, theColor, theAlpha, 0);
44                         drawsubpic(theOrigin + width   * 0.5, width * 0.5 + height * 0.5, pic, eX - bW,           bW + bH, theColor, theAlpha, 0);
45                         drawsubpic(theOrigin + height  * 0.5, width * 0.5 + height * 0.5, pic, eY - bH,           bW + bH, theColor, theAlpha, 0);
46                         drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
47                 }
48                 else
49                 {
50                         dY = theBorderSize_x * eY;
51                         drawsubpic(theOrigin,                             width * 0.5          +     dY, pic, '0 0    0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
52                         drawsubpic(theOrigin + width * 0.5,               width * 0.5          +     dY, pic, '0 0    0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
53                         drawsubpic(theOrigin                        + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0',           '0 0.5  0' + bW, theColor, theAlpha, 0);
54                         drawsubpic(theOrigin + width * 0.5          + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0' + eX - bW, '0 0.5  0' + bW, theColor, theAlpha, 0);
55                         drawsubpic(theOrigin               + height - dY, width * 0.5          +     dY, pic, '0 0.75 0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
56                         drawsubpic(theOrigin + width * 0.5 + height - dY, width * 0.5          +     dY, pic, '0 0.75 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
57                 }
58         }
59         else
60         {
61                 if(theSize_y <= theBorderSize_y * 2)
62                 {
63                         // not high enough... draw just top and bottom then
64                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
65                         dX = theBorderSize_x * eX;
66                         drawsubpic(theOrigin,                                         dX + height * 0.5, pic, '0    0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
67                         drawsubpic(theOrigin + dX,                        width - 2 * dX + height * 0.5, pic, '0.25 0 0',           '0.5  0 0' + bH, theColor, theAlpha, 0);
68                         drawsubpic(theOrigin + width - dX,                            dX + height * 0.5, pic, '0.75 0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
69                         drawsubpic(theOrigin              + height * 0.5,             dX + height * 0.5, pic, '0    0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
70                         drawsubpic(theOrigin + dX         + height * 0.5, width - 2 * dX + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5  0 0' + bH, theColor, theAlpha, 0);
71                         drawsubpic(theOrigin + width - dX + height * 0.5,             dX + height * 0.5, pic, '0.75 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
72                 }
73                 else
74                 {
75                         dX = theBorderSize_x * eX;
76                         dY = theBorderSize_x * eY;
77                         drawsubpic(theOrigin,                                        dX          +     dY, pic, '0    0    0', '0.25 0.25 0', theColor, theAlpha, 0);
78                         drawsubpic(theOrigin                  + dX,      width - 2 * dX          +     dY, pic, '0.25 0    0', '0.5  0.25 0', theColor, theAlpha, 0);
79                         drawsubpic(theOrigin          + width - dX,                  dX          +     dY, pic, '0.75 0    0', '0.25 0.25 0', theColor, theAlpha, 0);
80                         drawsubpic(theOrigin          + dY,                          dX + height - 2 * dY, pic, '0    0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
81                         drawsubpic(theOrigin          + dY         + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5  0.5  0', theColor, theAlpha, 0);
82                         drawsubpic(theOrigin          + dY + width - dX,             dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
83                         drawsubpic(theOrigin + height - dY,                          dX          +     dY, pic, '0    0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
84                         drawsubpic(theOrigin + height - dY         + dX, width - 2 * dX          +     dY, pic, '0.25 0.75 0', '0.5  0.25 0', theColor, theAlpha, 0);
85                         drawsubpic(theOrigin + height - dY + width - dX,             dX          +     dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
86                 }
87         }
88 }
89
90 vector HUD_Get_Num_Color (float x, float maxvalue)
91 {
92         vector color;
93         if(x > maxvalue) {
94                 color_x = 0;
95                 color_y = 1;
96                 color_z = 0;
97         }
98         else if(x > maxvalue * 0.75) {
99                 color_x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
100                 color_y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
101                 color_z = 0;
102         }
103         else if(x > maxvalue * 0.5) {
104                 color_x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
105                 color_y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
106                 color_z = 1 - (x-100)*0.02; // blue value between 1 -> 0
107         }
108         else if(x > maxvalue * 0.25) {
109                 color_x = 1;
110                 color_y = 1;
111                 color_z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
112         }
113         else if(x > maxvalue * 0.1) {
114                 color_x = 1;
115                 color_y = (x-20)*90/27/100; // green value between 0 -> 1
116                 color_z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
117         }
118         else {
119                 color_x = 1;
120                 color_y = 0;
121                 color_z = 0;
122         }
123         return color;
124 }
125
126 float stringwidth_colors(string s, vector theSize)
127 {
128         return stringwidth(s, TRUE, theSize);
129 }
130
131 float stringwidth_nocolors(string s, vector theSize)
132 {
133         return stringwidth(s, FALSE, theSize);
134 }
135
136 #define CENTERPRINT_MAX_LINES 30
137 string centerprint_messages[CENTERPRINT_MAX_LINES];
138 float centerprint_width[CENTERPRINT_MAX_LINES];
139 float centerprint_time;
140 float centerprint_expire;
141 float centerprint_num;
142 float centerprint_offset_hint;
143 vector centerprint_fontsize;
144
145 void centerprint(string strMessage)
146 {
147         float i, j, n, hcount;
148         string s;
149
150         centerprint_fontsize = HUD_GetFontsize("scr_centersize");
151
152         centerprint_expire = min(centerprint_expire, time); // if any of the returns happens, this message will fade out
153
154         if(autocvar_scr_centertime <= 0)
155                 return;
156
157         if(strMessage == "")
158                 return;
159
160         // strip trailing newlines
161         j = strlen(strMessage) - 1;
162         while(substring(strMessage, j, 1) == "\n" && j >= 0)
163                 j = j - 1;
164         strMessage = substring(strMessage, 0, j + 1);
165
166         if(strMessage == "")
167                 return;
168
169         // strip leading newlines and remember them, they are a hint that the message should be lower on the screen
170         j = 0;
171         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
172                 j = j + 1;
173         strMessage = substring(strMessage, j, strlen(strMessage) - j);
174         centerprint_offset_hint = j;
175
176         if(strMessage == "")
177                 return;
178
179         // if we get here, we have a message. Initialize its height.
180         centerprint_num = 0;
181
182         n = tokenizebyseparator(strMessage, "\n");
183         i = hcount = 0;
184         for(j = 0; j < n; ++j)
185         {
186                 getWrappedLine_remaining = argv(j);
187                 while(getWrappedLine_remaining)
188                 {
189                         s = getWrappedLine(vid_conwidth * 0.75, centerprint_fontsize, stringwidth_colors);
190                         if(centerprint_messages[i] != s) // don't fade the same message in, looks stupid
191                                 centerprint_time = time;
192                         if(centerprint_messages[i])
193                                 strunzone(centerprint_messages[i]);
194                         centerprint_messages[i] = strzone(s);
195                         centerprint_width[i] = stringwidth(s, TRUE, centerprint_fontsize);
196                         ++i;
197
198                         // half height for empty lines looks better
199                         if(s == "")
200                                 hcount += 0.5;
201                         else
202                                 hcount += 1;
203
204                         if(i >= CENTERPRINT_MAX_LINES)
205                                 break;
206                 }
207         }
208
209         float h, havail;
210         h = centerprint_fontsize_y*hcount;
211
212         havail = vid_conheight;
213         if(autocvar_con_chatpos < 0)
214                 havail -= (-autocvar_con_chatpos + autocvar_con_chat) * autocvar_con_chatsize; // avoid overlapping chat
215         if(havail > vid_conheight - 70)
216                 havail = vid_conheight - 70; // avoid overlapping HUD
217
218 #if 0
219         float forbiddenmin, forbiddenmax, allowedmin, allowedmax, preferred;
220
221         // here, the centerprint would cover the crosshair. REALLY BAD.
222         forbiddenmin = vid_conheight * 0.5 - h - 16;
223         forbiddenmax = vid_conheight * 0.5 + 16;
224
225         allowedmin = scoreboard_bottom;
226         allowedmax = havail - h;
227         preferred = (havail - h)/2;
228
229
230         // possible orderings (total: 4! / 4 = 6)
231         //  allowedmin allowedmax forbiddenmin forbiddenmax
232         //  forbiddenmin forbiddenmax allowedmin allowedmax
233         if(allowedmax < forbiddenmin || allowedmin > forbiddenmax)
234         {
235                 // forbidden doesn't matter in this case
236                 centerprint_start_y = bound(allowedmin, preferred, allowedmax);
237         }
238         //  allowedmin forbiddenmin allowedmax forbiddenmax
239         else if(allowedmin < forbiddenmin && allowedmax < forbiddenmax)
240         {
241                 centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
242         }
243         //  allowedmin forbiddenmin forbiddenmax allowedmax
244         else if(allowedmin < forbiddenmin)
245         {
246                 // make sure the forbidden zone is not covered
247                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
248                         centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
249                 else
250                         centerprint_start_y = bound(forbiddenmax, preferred, allowedmin);
251         }
252         //  forbiddenmin allowedmin allowedmax forbiddenmax
253         else if(allowedmax < forbiddenmax)
254         {
255                 // it's better to leave the allowed zone (overlap with scoreboard) than
256                 // to cover the forbidden zone (crosshair)
257                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
258                         centerprint_start_y = forbiddenmax;
259                 else
260                         centerprint_start_y = forbiddenmin;
261         }
262         //  forbiddenmin allowedmin forbiddenmax allowedmax
263         else
264         {
265                 centerprint_start_y = bound(forbiddenmax, preferred, allowedmax);
266         }
267 #else
268 #endif
269
270         centerprint_num = i;
271
272         centerprint_expire = time + autocvar_scr_centertime;
273 }
274
275 void HUD_DrawCenterPrint (void)
276 {
277         float i;
278         vector pos;
279         string ts;
280         float a, sz;
281
282         if(time - centerprint_time < 0.25)
283                 a = (time - centerprint_time) / 0.25;
284         else
285                 a = bound(0, 1 - 4 * (time - centerprint_expire), 1);
286
287         if(a <= 0)
288                 return;
289
290         sz = 0.8 + (a / 5);
291
292         if(centerprint_num * autocvar_scr_centersize > 24 && scoreboard_active) // 24 = height of Scoreboard text
293                 centerprint_start_y = scoreboard_bottom + centerprint_fontsize_y;
294
295         pos = centerprint_start;
296         for (i=0; i<centerprint_num; i = i + 1)
297         {
298                 ts = centerprint_messages[i];
299                 drawfontscale = sz * '1 1 0';
300                 drawfont = hud_bigfont;
301                 pos_x = (vid_conwidth - stringwidth(ts, TRUE, centerprint_fontsize)) * 0.5;
302                 if (ts != "")
303                 {
304                         drawcolorcodedstring(pos + '0 1 0' * (1 - sz) * 0.5 *centerprint_fontsize_y, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
305                         pos_y = pos_y + centerprint_fontsize_y;
306                 }
307                 else
308                         // half height for empty lines looks better
309                         pos_y = pos_y + sz * centerprint_fontsize_y * 0.5;
310                 drawfontscale = '1 1 0';
311                 drawfont = hud_font;
312         }
313 }
314
315 void drawstringright(vector position, string text, vector scale, vector rgb, float alpha, float flag)
316 {
317         position_x -= 2 / 3 * strlen(text) * scale_x;
318         drawstring(position, text, scale, rgb, alpha, flag);
319 }
320
321 void drawstringcenter(vector position, string text, vector scale, vector rgb, float alpha, float flag)
322 {
323         position_x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * scale_x);
324         drawstring(position, text, scale, rgb, alpha, flag);
325 }
326
327 // return the string of the given race place
328 string race_PlaceName(float pos) {
329         if(pos == 1)
330                 return "1st";
331         else if(pos == 2)
332                 return "2nd";
333         else if(pos == 3)
334                 return "3rd";
335         else
336                 return strcat(ftos(pos), "th");
337 }
338
339 // return the string of the onscreen race timer
340 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
341 {
342         string col;
343         string timestr;
344         string cpname;
345         string lapstr;
346         lapstr = "";
347
348         if(histime == 0) // goal hit
349         {
350                 if(mytime > 0)
351                 {
352                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
353                         col = "^1";
354                 }
355                 else if(mytime == 0)
356                 {
357                         timestr = "+0.0";
358                         col = "^3";
359                 }
360                 else
361                 {
362                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
363                         col = "^2";
364                 }
365
366                 if(lapdelta > 0)
367                 {
368                         lapstr = strcat(" (-", ftos(lapdelta), "L)");
369                         col = "^2";
370                 }
371                 else if(lapdelta < 0)
372                 {
373                         lapstr = strcat(" (+", ftos(-lapdelta), "L)");
374                         col = "^1";
375                 }
376         }
377         else if(histime > 0) // anticipation
378         {
379                 if(mytime >= histime)
380                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
381                 else
382                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
383                 col = "^3";
384         }
385         else
386                 col = "^7";
387
388         if(cp == 254)
389                 cpname = "Start line";
390         else if(cp == 255)
391                 cpname = "Finish line";
392         else if(cp)
393                 cpname = strcat("Intermediate ", ftos(cp));
394         else
395                 cpname = "Finish line";
396
397         if(histime < 0)
398                 return strcat(col, cpname);
399         else if(hisname == "")
400                 return strcat(col, cpname, " (", timestr, ")");
401         else
402                 return strcat(col, cpname, " (", timestr, " ", strcat(hisname, col, lapstr), ")");
403 }
404
405 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
406 float race_CheckName(string net_name) {
407         float i;
408         for (i=RANKINGS_CNT-1;i>=0;--i)
409                 if(grecordholder[i] == net_name)
410                         return i+1;
411         return 0;
412 }
413
414 /*
415 ==================
416 HUD panels
417 ==================
418 */
419
420 #define HUD_Write(s) fputs(fh, s)
421 // q: quoted, n: not quoted
422 #define HUD_Write_Cvar_n(cvar) HUD_Write(strcat("seta ", cvar, " ", cvar_string(cvar), "\n"))
423 #define HUD_Write_Cvar_q(cvar) HUD_Write(strcat("seta ", cvar, " \"", cvar_string(cvar), "\"\n"))
424 #define HUD_Write_PanelCvar_n(cvar_suf) HUD_Write_Cvar_n(strcat("hud_panel_", panel_name, cvar_suf))
425 #define HUD_Write_PanelCvar_q(cvar_suf) HUD_Write_Cvar_q(strcat("hud_panel_", panel_name, cvar_suf))
426 // Save the config
427 void HUD_Panel_ExportCfg(string cfgname)
428 {
429         float fh;
430         string filename = strcat("hud_", autocvar_hud_skin, "_", cfgname, ".cfg");
431         fh = fopen(filename, FILE_WRITE);
432         if(fh >= 0)
433         {
434                 HUD_Write_Cvar_q("hud_skin");
435                 HUD_Write_Cvar_q("hud_panel_bg");
436                 HUD_Write_Cvar_q("hud_panel_bg_color");
437                 HUD_Write_Cvar_q("hud_panel_bg_color_team");
438                 HUD_Write_Cvar_q("hud_panel_bg_alpha");
439                 HUD_Write_Cvar_q("hud_panel_bg_border");
440                 HUD_Write_Cvar_q("hud_panel_bg_padding");
441                 HUD_Write_Cvar_q("hud_panel_fg_alpha");
442                 HUD_Write("\n");
443
444                 HUD_Write_Cvar_q("hud_dock");
445                 HUD_Write_Cvar_q("hud_dock_color");
446                 HUD_Write_Cvar_q("hud_dock_color_team");
447                 HUD_Write_Cvar_q("hud_dock_alpha");
448                 HUD_Write("\n");
449
450                 HUD_Write_Cvar_q("hud_progressbar_alpha");
451                 HUD_Write_Cvar_q("hud_progressbar_strength_color");
452                 HUD_Write_Cvar_q("hud_progressbar_shield_color");
453                 HUD_Write_Cvar_q("hud_progressbar_health_color");
454                 HUD_Write_Cvar_q("hud_progressbar_armor_color");
455                 HUD_Write_Cvar_q("hud_progressbar_fuel_color");
456                 HUD_Write_Cvar_q("hud_progressbar_nexball_color");
457                 HUD_Write("\n");
458
459                 HUD_Write_Cvar_q("_hud_panelorder");
460                 HUD_Write("\n");
461
462                 HUD_Write_Cvar_q("hud_configure_grid");
463                 HUD_Write_Cvar_q("hud_configure_grid_xsize");
464                 HUD_Write_Cvar_q("hud_configure_grid_ysize");
465                 HUD_Write("\n");
466
467                 HUD_Write_Cvar_q("scr_centerpos");
468                 HUD_Write("\n");
469
470                 // common cvars for all panels
471                 float i;
472                 for (i = 0; i < HUD_PANEL_NUM; ++i)
473                 {
474                         HUD_Panel_GetName(i);
475
476                         HUD_Write_PanelCvar_n("");
477                         HUD_Write_PanelCvar_q("_pos");
478                         HUD_Write_PanelCvar_q("_size");
479                         HUD_Write_PanelCvar_q("_bg");
480                         HUD_Write_PanelCvar_q("_bg_color");
481                         HUD_Write_PanelCvar_q("_bg_color_team");
482                         HUD_Write_PanelCvar_q("_bg_alpha");
483                         HUD_Write_PanelCvar_q("_bg_border");
484                         HUD_Write_PanelCvar_q("_bg_padding");
485                         switch(i) {
486                                 case HUD_PANEL_WEAPONS:
487                                         HUD_Write_PanelCvar_q("_complainbubble");
488                                         HUD_Write_PanelCvar_q("_complainbubble_padding");
489                                         HUD_Write_PanelCvar_q("_complainbubble_color_outofammo");
490                                         HUD_Write_PanelCvar_q("_complainbubble_color_donthave");
491                                         HUD_Write_PanelCvar_q("_complainbubble_color_unavailable");
492                                         HUD_Write_PanelCvar_q("_ammo_color");
493                                         HUD_Write_PanelCvar_q("_ammo_alpha");
494                                         HUD_Write_PanelCvar_q("_aspect");
495                                         break;
496                                 case HUD_PANEL_AMMO:
497                                         HUD_Write_PanelCvar_q("_onlycurrent");
498                                         HUD_Write_PanelCvar_q("_iconalign");
499                                         HUD_Write_PanelCvar_q("_progressbar");
500                                         HUD_Write_PanelCvar_q("_progressbar_name");
501                                         HUD_Write_PanelCvar_q("_text");
502                                         break;
503                                 case HUD_PANEL_POWERUPS:
504                                         HUD_Write_PanelCvar_q("_flip");
505                                         HUD_Write_PanelCvar_q("_iconalign");
506                                         HUD_Write_PanelCvar_q("_baralign");
507                                         HUD_Write_PanelCvar_q("_progressbar");
508                                         HUD_Write_PanelCvar_q("_progressbar_strength");
509                                         HUD_Write_PanelCvar_q("_progressbar_shield");
510                                         break;
511                                 case HUD_PANEL_HEALTHARMOR:
512                                         HUD_Write_PanelCvar_q("_flip");
513                                         HUD_Write_PanelCvar_q("_iconalign");
514                                         HUD_Write_PanelCvar_q("_baralign");
515                                         HUD_Write_PanelCvar_q("_progressbar");
516                                         HUD_Write_PanelCvar_q("_progressbar_health");
517                                         HUD_Write_PanelCvar_q("_progressbar_armor");
518                                         HUD_Write_PanelCvar_q("_text");
519                                         break;
520                                 case HUD_PANEL_NOTIFY:
521                                         HUD_Write_PanelCvar_q("_flip");
522                                         HUD_Write_PanelCvar_q("_print");
523                                         break;
524                                 case HUD_PANEL_RADAR:
525                                         HUD_Write_PanelCvar_q("_foreground_alpha");
526                                         break;
527                                 case HUD_PANEL_VOTE:
528                                         HUD_Write_PanelCvar_q("_alreadyvoted_alpha");
529                                         break;
530                                 case HUD_PANEL_PRESSEDKEYS:
531                                         HUD_Write_PanelCvar_q("_aspect");
532                                         break;
533                                 case HUD_PANEL_INFOMESSAGES:
534                                         HUD_Write_PanelCvar_q("_flip");
535                                         break;
536                         }
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 * autocvar_con_chatsize, mySize_x);
885                 mySize_y = max(2 * autocvar_con_chatsize + 2 * panel_bg_padding, mySize_y);
886         }
887
888         // collision testing|
889         // -----------------+
890
891         // we need to know pos at this stage, but it might still change later if we hit a screen edge/other panel (?)
892         if(resizeCorner == 1) {
893                 myPos_x = resizeorigin_x - mySize_x;
894                 myPos_y = resizeorigin_y - mySize_y;
895         } else if(resizeCorner == 2) {
896                 myPos_x = resizeorigin_x;
897                 myPos_y = resizeorigin_y - mySize_y;
898         } else if(resizeCorner == 3) {
899                 myPos_x = resizeorigin_x - mySize_x;
900                 myPos_y = resizeorigin_y;
901         } else { // resizeCorner == 4
902                 myPos_x = resizeorigin_x;
903                 myPos_y = resizeorigin_y;
904         }
905
906         // left/top screen edges
907         if(myPos_x < 0)
908                 mySize_x = mySize_x + myPos_x;
909         if(myPos_y < 0)
910                 mySize_y = mySize_y + myPos_y;
911
912         // bottom/right screen edges
913         if(myPos_x + mySize_x > vid_conwidth)
914                 mySize_x = vid_conwidth - myPos_x;
915         if(myPos_y + mySize_y > vid_conheight)
916                 mySize_y = vid_conheight - myPos_y;
917
918         //if(cvar("hud_configure_checkcollisions_debug"))
919                 //drawfill(myPos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
920
921         // before checkresize, otherwise panel can be snapped partially inside another panel or panel aspect ratio can be broken
922         if(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) && autocvar_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: ", ftos(autocvar__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() * autocvar_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) && autocvar_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/", autocvar_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/", autocvar_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/", autocvar_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/", autocvar_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 = autocvar_hud_panel_weapons_timeout;
1554         float timeout_effect_length, timein_effect_length;
1555         if (autocvar_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 (autocvar_hud_panel_weapons_timeout_effect)
1579                 {
1580                         panel_bg_alpha *= (1 - f);
1581                         panel_fg_alpha *= (1 - f);
1582                 }
1583                 if (autocvar_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 (autocvar_hud_panel_weapons_timeout_effect)
1610                 {
1611                         panel_bg_alpha *= (f);
1612                         panel_fg_alpha *= (f);
1613                 }
1614                 if (autocvar_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 != autocvar_cl_weaponpriority)
1649         {
1650                 if(weaponorder_bypriority)
1651                         strunzone(weaponorder_bypriority);
1652                 if(weaponorder_byimpulse)
1653                         strunzone(weaponorder_byimpulse);
1654
1655                 weaponorder_bypriority = strzone(autocvar_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(autocvar_hud_panel_weapons_fade)
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 = autocvar_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 = autocvar_hud_panel_weapons_complainbubble_time;
1702         float fadetime;
1703         fadetime = autocvar_hud_panel_weapons_complainbubble_fadetime;
1704
1705         vector color;
1706         vector wpnpos;
1707         vector wpnsize;
1708         
1709         vector ammo_color;
1710         float ammo_alpha;
1711         wpnsize = eX * panel_size_x*(1/columns) + eY * panel_size_y*(1/rows);
1712         float barsize_x, barsize_y, baroffset_x, baroffset_y;
1713         if (autocvar_hud_panel_weapons_ammo)
1714         {
1715                 ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
1716                 ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
1717
1718
1719                 if(wpnsize_x/wpnsize_y > aspect)
1720                 {
1721                         barsize_x = aspect * wpnsize_y;
1722                         barsize_y = wpnsize_y;
1723                         baroffset_x = (wpnsize_x - barsize_x) / 2;
1724                 }
1725                 else
1726                 {
1727                         barsize_y = 1/aspect * wpnsize_x;
1728                         barsize_x = wpnsize_x;
1729                         baroffset_y = (wpnsize_y - barsize_y) / 2;
1730                 }
1731         }
1732
1733         float show_accuracy;
1734         float weapon_stats;
1735         if(autocvar_hud_panel_weapons_accuracy && acc_levels)
1736         {
1737                 show_accuracy = true;
1738                 if (acc_col_x[0] == -1)
1739                         for (i = 0; i < acc_levels; ++i)
1740                                 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
1741         }
1742
1743         float weapons_st = getstati(STAT_WEAPONS);
1744         float label = autocvar_hud_panel_weapons_label;
1745
1746         for(i = 0; i < weapon_cnt; ++i)
1747         {
1748                 wpnpos = panel_pos + eX * column * wpnsize_x + eY * row * wpnsize_y;
1749
1750                 self = weaponorder[i];
1751                 weapid = self.impulse;
1752
1753                 // draw background behind currently selected weapon
1754                 if(self.weapon == activeweapon)
1755                         drawpic_aspect_skin(wpnpos, "weapon_current_bg", wpnsize, '1 1 1', wpnalpha, DRAWFLAG_NORMAL);
1756
1757                 // draw the weapon accuracy
1758                 if(show_accuracy)
1759                 {
1760                         weapon_stats = weapon_accuracy[self.weapon-WEP_FIRST];
1761                         if(weapon_stats >= 0)
1762                         {
1763                                 // find the max level lower than weapon_stats
1764                                 float j;
1765                                 j = acc_levels-1;
1766                                 while ( j && weapon_stats < acc_lev[j] )
1767                                         --j;
1768
1769                                 // inject color j+1 in color j, how much depending on how much weapon_stats is higher than level j
1770                                 float factor;
1771                                 factor = (weapon_stats - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
1772                                 color = acc_col[j];
1773                                 color = color + factor * (acc_col[j+1] - color);
1774
1775                                 drawpic_aspect_skin(wpnpos, "weapon_accuracy", wpnsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
1776                         }
1777                 }
1778
1779                 // draw the weapon icon
1780                 if((weapid >= 0) && (weapons_st & self.weapons))
1781                 {
1782                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '1 1 1', wpnalpha, DRAWFLAG_NORMAL);
1783
1784                         if(label == 1) // weapon number
1785                                 drawstring(wpnpos, ftos(weapid), '1 1 0' * 0.5 * wpnsize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1786                         else if(label == 2) // bind
1787                                 drawstring(wpnpos, getcommandkey(ftos(weapid), strcat("impulse ", ftos(weapid))), '1 1 0' * 0.5 * wpnsize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1788
1789                         // draw ammo status bar
1790                         if(autocvar_hud_panel_weapons_ammo && weapid != WEP_TUBA && weapid != WEP_LASER && weapid != WEP_PORTO)
1791                         {
1792                                 a = 0;
1793                                 type = GetAmmoTypeForWep(weapid);
1794                                 if(type != -1)
1795                                         a = getstati(GetAmmoStat(type)); // how much ammo do we have?
1796
1797                                 if(a > 0)
1798                                 {
1799                                         switch(type) {
1800                                                 case 0: fullammo = autocvar_hud_panel_weapons_ammo_full_shells; break;
1801                                                 case 1: fullammo = autocvar_hud_panel_weapons_ammo_full_nails; break;
1802                                                 case 2: fullammo = autocvar_hud_panel_weapons_ammo_full_rockets; break;
1803                                                 case 3: fullammo = autocvar_hud_panel_weapons_ammo_full_cells; break;
1804                                                 case 4: fullammo = autocvar_hud_panel_weapons_ammo_full_fuel; break;
1805                                                 default: fullammo = 60;
1806                                         }
1807
1808                                         drawsetcliparea(
1809                                                 wpnpos_x + baroffset_x,
1810                                                 wpnpos_y + baroffset_y,
1811                                                 barsize_x * bound(0, a/fullammo, 1),
1812                                                 barsize_y);
1813                                         drawpic_aspect_skin(wpnpos, "weapon_ammo", wpnsize, ammo_color, ammo_alpha, DRAWFLAG_NORMAL);
1814                                         drawresetcliparea();
1815                                 }
1816                         }
1817                 }
1818
1819                 // draw a "ghost weapon icon" if you don't have the weapon
1820                 else
1821                 {
1822                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '0 0 0', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
1823                 }
1824
1825                 // draw the complain message
1826                 if(time - complain_weapon_time < when + fadetime && self.weapon == complain_weapon && autocvar_hud_panel_weapons_complainbubble)
1827                 {
1828                         if(fadetime)
1829                         {
1830                                 if(complain_weapon_time + when > time)
1831                                         a = 1;
1832                                 else
1833                                         a = bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1);
1834                         }
1835                         else
1836                         {
1837                                 if(complain_weapon_time + when > time)
1838                                         a = 1;
1839                                 else
1840                                         a = 0;
1841                         }
1842
1843                         string s;
1844                         if(complain_weapon_type == 0) {
1845                                 s = "Out of ammo";
1846                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
1847                         }
1848                         else if(complain_weapon_type == 1) {
1849                                 s = "Don't have";
1850                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
1851                         }
1852                         else {
1853                                 s = "Unavailable";
1854                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
1855                         }
1856                         float padding = autocvar_hud_panel_weapons_complainbubble_padding;
1857                         drawpic_aspect_skin(wpnpos + '1 1 0' * padding, "weapon_complainbubble", wpnsize - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
1858                         drawstring_aspect(wpnpos + '1 1 0' * padding, s, wpnsize - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
1859                 }
1860
1861                 ++row;
1862                 if(row >= rows)
1863                 {
1864                         row = 0;
1865                         ++column;
1866                 }
1867         }
1868
1869 }
1870
1871 // Ammo (#1)
1872 //
1873 // TODO: macro
1874 float GetAmmoItemCode(float i)
1875 {
1876         switch(i)
1877         {
1878                 case 0: return IT_SHELLS;
1879                 case 1: return IT_NAILS;
1880                 case 2: return IT_ROCKETS;
1881                 case 3: return IT_CELLS;
1882                 case 4: return IT_FUEL;
1883                 default: return -1;
1884         }
1885 }
1886
1887 string GetAmmoPicture(float i)
1888 {
1889         switch(i)
1890         {
1891                 case 0: return "ammo_shells";
1892                 case 1: return "ammo_bullets";
1893                 case 2: return "ammo_rockets";
1894                 case 3: return "ammo_cells";
1895                 case 4: return "ammo_fuel";
1896                 default: return "";
1897         }
1898 }
1899
1900 void DrawAmmoItem(vector myPos, vector mySize, float itemcode, float currently_selected)
1901 {
1902         float a;
1903         if(autocvar__hud_configure)
1904         {
1905                 currently_selected = (itemcode == 2); //rockets always selected
1906                 a = 31 + mod(itemcode*93, 128);
1907         }
1908         else
1909                 a = getstati(GetAmmoStat(itemcode)); // how much ammo do we have of type itemcode?
1910
1911         vector color;
1912         if(a < 10)
1913                 color = '0.7 0 0';
1914         else
1915                 color = '1 1 1';
1916
1917         float alpha;
1918         if(currently_selected)
1919                 alpha = 1;
1920         else
1921                 alpha = 0.7;
1922
1923         vector picpos, numpos;
1924         if(autocvar_hud_panel_ammo_iconalign)
1925         {
1926                 numpos = myPos;
1927                 picpos = myPos + eX * 2 * mySize_y;
1928         }
1929         else
1930         {
1931                 numpos = myPos + eX * mySize_y;
1932                 picpos = myPos;
1933         }
1934
1935         if (currently_selected)
1936                 drawpic_aspect_skin(myPos, "ammo_current_bg", mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1937
1938     if(a > 0 && autocvar_hud_panel_ammo_progressbar)
1939         HUD_Panel_DrawProgressBar(myPos + eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize_x, mySize - eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize_x, autocvar_hud_panel_ammo_progressbar_name, 0, 0, a/autocvar_hud_panel_ammo_maxammo, color, autocvar_hud_progressbar_alpha * panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1940
1941     if(autocvar_hud_panel_ammo_text)
1942     {
1943         if(a > 0)
1944             drawstring_aspect(numpos, ftos(a), eX * (2/3) * mySize_x + eY * mySize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1945         else // "ghost" ammo count
1946             drawstring_aspect(numpos, ftos(a), eX * (2/3) * mySize_x + eY * mySize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1947     }
1948         if(a > 0)
1949                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1950         else // "ghost" ammo icon
1951                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * mySize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1952 }
1953
1954 void HUD_Ammo(void)
1955 {
1956         if(!autocvar__hud_configure)
1957         {
1958                 if(!autocvar_hud_panel_ammo) return;
1959                 if(spectatee_status == -1) return;
1960         }
1961
1962         active_panel = HUD_PANEL_AMMO;
1963         HUD_Panel_UpdateCvars(ammo);
1964         vector pos, mySize;
1965         pos = panel_pos;
1966         mySize = panel_size;
1967
1968         HUD_Panel_DrawBg(1);
1969         if(panel_bg_padding)
1970         {
1971                 pos += '1 1 0' * panel_bg_padding;
1972                 mySize -= '2 2 0' * panel_bg_padding;
1973         }
1974
1975         float rows, columns, row, column;
1976         vector ammo_size;
1977         if (autocvar_hud_panel_ammo_onlycurrent)
1978                 ammo_size = mySize;
1979         else
1980         {
1981                 rows = mySize_y/mySize_x;
1982                 rows = bound(1, floor((sqrt(4 * (3/1) * rows * AMMO_COUNT + rows * rows) + rows + 0.5) / 2), AMMO_COUNT);
1983                 //                               ^^^ ammo item aspect goes here
1984
1985                 columns = ceil(AMMO_COUNT/rows);
1986
1987                 ammo_size = eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows);
1988         }
1989
1990         vector offset;
1991         float newSize;
1992         if(ammo_size_x/ammo_size_y > 3)
1993         {
1994                 newSize = 3 * ammo_size_y;
1995                 offset_x = ammo_size_x - newSize;
1996                 pos_x += offset_x/2;
1997                 ammo_size_x = newSize;
1998         }
1999         else
2000         {
2001                 newSize = 1/3 * ammo_size_x;
2002                 offset_y = ammo_size_y - newSize;
2003                 pos_y += offset_y/2;
2004                 ammo_size_y = newSize;
2005         }
2006
2007         drawfont = hud_bigfont;
2008         float i, stat_items, currently_selected;
2009         if (autocvar_hud_panel_ammo_onlycurrent)
2010         {
2011                 if(autocvar__hud_configure)
2012                 {
2013                         DrawAmmoItem(pos, ammo_size, 2, true); //show rockets
2014                         return;
2015                 }
2016                 stat_items = getstati(STAT_ITEMS);
2017                 for (i = 0; i < AMMO_COUNT; ++i) {
2018                         currently_selected = stat_items & GetAmmoItemCode(i);
2019                         if (currently_selected)
2020                         {
2021                                 DrawAmmoItem(pos, ammo_size, i, true);
2022                                 return;
2023                         }
2024                 }
2025                 return; // nothing to display
2026         }
2027
2028         stat_items = getstati(STAT_ITEMS);
2029         for (i = 0; i < AMMO_COUNT; ++i) {
2030                 currently_selected = stat_items & GetAmmoItemCode(i);
2031                 DrawAmmoItem(pos + eX * column * (ammo_size_x + offset_x) + eY * row * (ammo_size_y + offset_y), ammo_size, i, currently_selected);
2032                 ++row;
2033                 if(row >= rows)
2034                 {
2035                         row = 0;
2036                         column = column + 1;
2037                 }
2038         }
2039         drawfont = hud_font;
2040 }
2041
2042 void DrawNumIcon(vector myPos, vector mySize, float x, string icon, float vertical, float icon_right_align, vector color, float alpha)
2043 {
2044         vector newPos, newSize;
2045         vector picpos, numpos;
2046
2047         if (vertical)
2048         {
2049                 if(mySize_y/mySize_x > 2)
2050                 {
2051                         newSize_y = 2 * mySize_x;
2052                         newSize_x = mySize_x;
2053
2054                         newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
2055                         newPos_x = myPos_x;
2056                 }
2057                 else
2058                 {
2059                         newSize_x = 1/2 * mySize_y;
2060                         newSize_y = mySize_y;
2061
2062                         newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
2063                         newPos_y = myPos_y;
2064                 }
2065
2066                 if(icon_right_align)
2067                 {
2068                         numpos = newPos;
2069                         picpos = newPos + eY * newSize_x;
2070                 }
2071                 else
2072                 {
2073                         picpos = newPos;
2074                         numpos = newPos + eY * newSize_x;
2075                 }
2076
2077                 newSize_y /= 2;
2078                 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2079                 // make number smaller than icon, it looks better
2080                 // reduce only y to draw numbers with different number of digits with the same y size
2081                 numpos_y += newSize_y * ((1 - 0.7) / 2);
2082                 newSize_y *= 0.7;
2083                 drawstring_aspect(numpos, ftos(x), newSize, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2084                 return;
2085         }
2086
2087         if(mySize_x/mySize_y > 3)
2088         {
2089                 newSize_x = 3 * mySize_y;
2090                 newSize_y = mySize_y;
2091
2092                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
2093                 newPos_y = myPos_y;
2094         }
2095         else
2096         {
2097                 newSize_y = 1/3 * mySize_x;
2098                 newSize_x = mySize_x;
2099
2100                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
2101                 newPos_x = myPos_x;
2102         }
2103
2104         if(icon_right_align) // right align
2105         {
2106                 numpos = newPos;
2107                 picpos = newPos + eX * 2 * newSize_y;
2108         }
2109         else // left align
2110         {
2111                 numpos = newPos + eX * newSize_y;
2112                 picpos = newPos;
2113         }
2114
2115         drawstring_aspect(numpos, ftos(x), '2 1 0' * newSize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2116         drawpic_aspect_skin(picpos, icon, '1 1 0' * newSize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2117 }
2118
2119 void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, float vertical, float icon_right_align, vector color, float fadelerp)
2120 {
2121         float sz;
2122         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
2123
2124         DrawNumIcon(myPos + expandingbox_resize_centered_box_offset(sz, mySize, 1), mySize * sz, x, icon, vertical, icon_right_align, color, (1 - fadelerp));
2125 }
2126
2127 // Powerups (#2)
2128 //
2129 void HUD_Powerups(void) {
2130         if(!autocvar__hud_configure)
2131         {
2132                 if(!autocvar_hud_panel_powerups) return;
2133                 if(spectatee_status == -1) return;
2134                 if not(getstati(STAT_ITEMS) & (IT_STRENGTH | IT_INVINCIBLE)) return;
2135                 if (getstati(STAT_HEALTH) <= 0) return;
2136         }
2137
2138         active_panel = HUD_PANEL_POWERUPS;
2139         HUD_Panel_UpdateCvars(powerups);
2140         vector pos, mySize;
2141         pos = panel_pos;
2142         mySize = panel_size;
2143
2144         float strength_time, shield_time;
2145         if(autocvar__hud_configure)
2146         {
2147                 strength_time = 15;
2148                 shield_time = 27;
2149         }
2150         else
2151         {
2152                 strength_time = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
2153                 shield_time = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
2154         }
2155
2156         HUD_Panel_DrawBg(bound(0, max(strength_time, shield_time), 1));
2157         if(panel_bg_padding)
2158         {
2159                 pos += '1 1 0' * panel_bg_padding;
2160                 mySize -= '2 2 0' * panel_bg_padding;
2161         }
2162
2163         vector barsize;
2164         vector picpos;
2165         vector numpos;
2166
2167         string leftname, rightname;
2168     string leftprogressname, rightprogressname;
2169         float leftcnt, rightcnt;
2170         float leftexact, rightexact;
2171         if (autocvar_hud_panel_powerups_flip) {
2172                 leftname = "strength";
2173         leftprogressname = autocvar_hud_panel_powerups_progressbar_strength;
2174                 leftcnt = ceil(strength_time);
2175                 leftexact = strength_time;
2176
2177                 rightname = "shield";
2178         rightprogressname = autocvar_hud_panel_powerups_progressbar_shield;
2179                 rightcnt = ceil(shield_time);
2180                 rightexact = shield_time;
2181         } else {
2182                 leftname = "shield";
2183         leftprogressname = autocvar_hud_panel_powerups_progressbar_shield;
2184                 leftcnt = ceil(shield_time);
2185                 leftexact = shield_time;
2186
2187                 rightname = "strength";
2188         rightprogressname = autocvar_hud_panel_powerups_progressbar_strength;
2189                 rightcnt = ceil(strength_time);
2190                 rightexact = strength_time;
2191         }
2192
2193         drawfont = hud_bigfont;
2194         float baralign = autocvar_hud_panel_powerups_baralign;
2195         float iconalign = autocvar_hud_panel_powerups_iconalign;
2196
2197         float panel_ar = mySize_x/mySize_y;
2198         float is_vertical = (panel_ar < 1);
2199         if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
2200         {
2201         barsize = eX * 0.5 * mySize_x + eY * mySize_y;
2202                 if(leftcnt)
2203                 {
2204                         if(autocvar_hud_panel_powerups_progressbar)
2205                         {
2206                                 HUD_Panel_GetProgressBarColorForString(leftname);
2207                                 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);
2208                         }
2209             if(autocvar_hud_panel_powerups_text)
2210             {
2211                                 if(leftcnt > 1)
2212                                         DrawNumIcon(pos, barsize, leftcnt, leftname, is_vertical, (iconalign == 1 || iconalign == 2), '1 1 1', 1);
2213                                 if(leftcnt <= 5)
2214                                         DrawNumIcon_expanding(pos, barsize, leftcnt, leftname, is_vertical, (iconalign == 1 || iconalign == 2), '1 1 1', bound(0, (leftcnt - leftexact) / 0.5, 1));
2215                         }
2216                 }
2217
2218                 if(rightcnt)
2219                 {
2220                         pos_x += barsize_x;
2221                         if(autocvar_hud_panel_powerups_progressbar)
2222                         {
2223                                 HUD_Panel_GetProgressBarColorForString(rightname);
2224                                 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);
2225                         }
2226             if(autocvar_hud_panel_powerups_text)
2227             {
2228                                 if(rightcnt > 1)
2229                                         DrawNumIcon(pos, barsize, rightcnt, rightname, is_vertical, (iconalign == 1 || iconalign == 3), '1 1 1', 1);
2230                                 if(rightcnt <= 5)
2231                                         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));
2232                         }
2233                 }
2234         }
2235         else
2236         {
2237         barsize = eX * mySize_x + eY * 0.5 * mySize_y;
2238                 if(leftcnt)
2239                 {
2240                         if(autocvar_hud_panel_powerups_progressbar)
2241                         {
2242                                 HUD_Panel_GetProgressBarColorForString(leftname);
2243                                 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);
2244                         }
2245             if(autocvar_hud_panel_powerups_text)
2246             {
2247                                 if(leftcnt > 1)
2248                                         DrawNumIcon(pos, barsize, leftcnt, leftname, is_vertical, (iconalign == 1 || iconalign == 2), '1 1 1', 1);
2249                                 if(leftcnt <= 5)
2250                                         DrawNumIcon_expanding(pos, barsize, leftcnt, leftname, is_vertical, (iconalign == 1 || iconalign == 2), '1 1 1', bound(0, (leftcnt - leftexact) / 0.5, 1));
2251                         }
2252                 }
2253
2254                 if(rightcnt)
2255                 {
2256                         pos_y += barsize_y;
2257                         if(autocvar_hud_panel_powerups_progressbar)
2258                         {
2259                                 HUD_Panel_GetProgressBarColorForString(rightname);
2260                                 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);
2261                         }
2262             if(autocvar_hud_panel_powerups_text)
2263             {
2264                                 if(rightcnt > 1)
2265                                         DrawNumIcon(pos, barsize, rightcnt, rightname, is_vertical, (iconalign == 1 || iconalign == 3), '1 1 1', 1);
2266                                 if(rightcnt <= 5)
2267                                         DrawNumIcon_expanding(pos, barsize, rightcnt, rightname, is_vertical, (iconalign == 1 || iconalign == 3), '1 1 1', bound(0, (rightcnt - rightexact) / 0.5, 1));
2268                         }
2269                 }
2270         }
2271         drawfont = hud_font;
2272 }
2273
2274 // Health/armor (#3)
2275 //
2276 void HUD_HealthArmor(void)
2277 {
2278         if(!autocvar__hud_configure)
2279         {
2280                 if(!autocvar_hud_panel_healtharmor) return;
2281                 if(spectatee_status == -1) return;
2282         }
2283
2284         active_panel = HUD_PANEL_HEALTHARMOR;
2285         HUD_Panel_UpdateCvars(healtharmor);
2286         vector pos, mySize;
2287         pos = panel_pos;
2288         mySize = panel_size;
2289
2290         HUD_Panel_DrawBg(1);
2291         if(panel_bg_padding)
2292         {
2293                 pos += '1 1 0' * panel_bg_padding;
2294                 mySize -= '2 2 0' * panel_bg_padding;
2295         }
2296
2297         float armor, health, fuel;
2298         armor = getstati(STAT_ARMOR);
2299         health = getstati(STAT_HEALTH);
2300         fuel = getstati(STAT_FUEL);
2301
2302         if(autocvar__hud_configure)
2303         {
2304                 armor = 75;
2305                 health = 150;
2306                 fuel = 20;
2307         }
2308
2309         if(health <= 0)
2310                 return;
2311
2312         vector barsize;
2313         vector picpos, numpos;
2314
2315         drawfont = hud_bigfont;
2316         float baralign = autocvar_hud_panel_healtharmor_baralign;
2317         float iconalign = autocvar_hud_panel_healtharmor_iconalign;
2318
2319     float maxhealth = autocvar_hud_panel_healtharmor_maxhealth;
2320     float maxarmor = autocvar_hud_panel_healtharmor_maxarmor;
2321         if(autocvar_hud_panel_healtharmor == 2) // combined health and armor display
2322         {
2323                 vector v;
2324                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
2325
2326                 float x;
2327                 x = floor(v_x + 1);
2328
2329         float maxtotal = maxhealth + maxarmor;
2330                 string biggercount;
2331                 if(v_z) // NOT fully armored
2332                 {
2333                         biggercount = "health";
2334                         if(autocvar_hud_panel_healtharmor_progressbar)
2335                         {
2336                                 HUD_Panel_GetProgressBarColor(health);
2337                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_health, x/maxtotal, 0, (baralign == 1 || baralign == 2), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2338                         }
2339                         if(armor)
2340             if(autocvar_hud_panel_healtharmor_text)
2341                                 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);
2342                 }
2343                 else
2344                 {
2345                         biggercount = "armor";
2346                         if(autocvar_hud_panel_healtharmor_progressbar)
2347                         {
2348                                 HUD_Panel_GetProgressBarColor(armor);
2349                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, x/maxtotal, 0, (baralign == 1 || baralign == 2), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2350                         }
2351                         if(health)
2352             if(autocvar_hud_panel_healtharmor_text)
2353                                 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);
2354                 }
2355         if(autocvar_hud_panel_healtharmor_text)
2356                         DrawNumIcon(pos, mySize, x, biggercount, 0, iconalign, HUD_Get_Num_Color(x, maxtotal), 1);
2357
2358                 if(fuel)
2359                 {
2360                         HUD_Panel_GetProgressBarColor(fuel);
2361                         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);
2362                 }
2363         }
2364         else
2365         {
2366                 string leftname, rightname;
2367         string leftprogressname, rightprogressname;
2368                 float leftcnt, rightcnt;
2369                 float leftmax, rightmax;
2370                 float leftactive, rightactive;
2371                 float leftalpha, rightalpha;
2372                 if (autocvar_hud_panel_healtharmor_flip) { // old style layout with armor left/top of health
2373                         leftname = "armor";
2374             leftprogressname = autocvar_hud_panel_healtharmor_progressbar_armor;
2375                         leftcnt = armor;
2376                         if(leftcnt)
2377                                 leftactive = 1;
2378                         leftalpha = min((armor+10)/55, 1);
2379             leftmax = maxarmor;
2380
2381                         rightname = "health";
2382             rightprogressname = autocvar_hud_panel_healtharmor_progressbar_health;
2383                         rightcnt = health;
2384                         rightactive = 1;
2385                         rightalpha = 1;
2386             rightmax = maxhealth;
2387                 } else {
2388                         leftname = "health";
2389             leftprogressname = autocvar_hud_panel_healtharmor_progressbar_health;
2390                         leftcnt = health;
2391                         leftactive = 1;
2392                         leftalpha = 1;
2393             leftmax = maxhealth;
2394
2395                         rightname = "armor";
2396             rightprogressname = autocvar_hud_panel_healtharmor_progressbar_armor;
2397                         rightcnt = armor;
2398                         if(rightcnt)
2399                                 rightactive = 1;
2400                         rightalpha = min((armor+10)/55, 1);
2401             rightmax = maxarmor;
2402                 }
2403
2404                 float panel_ar = mySize_x/mySize_y;
2405                 float is_vertical = (panel_ar < 1);
2406                 if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
2407                 {
2408             barsize = eX * 0.5 * mySize_x + eY * mySize_y;
2409                         if(leftactive)
2410                         {
2411                                 if(autocvar_hud_panel_healtharmor_progressbar)
2412                                 {
2413                                         HUD_Panel_GetProgressBarColorForString(leftname);
2414                                         HUD_Panel_DrawProgressBar(pos, barsize, leftprogressname, leftcnt/leftmax, is_vertical, (baralign == 1 || baralign == 2), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2415                                 }
2416                                 if(autocvar_hud_panel_healtharmor_text)
2417                                         DrawNumIcon(pos, barsize, leftcnt, leftname, is_vertical, (iconalign == 1 || iconalign == 2), HUD_Get_Num_Color(leftcnt, leftmax), 1);
2418                         }
2419
2420                         if(rightactive)
2421                         {
2422                                 if(autocvar_hud_panel_healtharmor_progressbar)
2423                                 {
2424                                         HUD_Panel_GetProgressBarColorForString(rightname);
2425                                         HUD_Panel_DrawProgressBar(pos + eX * 0.5 * mySize_x, barsize, rightprogressname, rightcnt/rightmax, is_vertical, (baralign == 1 || baralign == 3), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2426                                 }
2427                                 if(autocvar_hud_panel_healtharmor_text)
2428                                         DrawNumIcon(pos + eX * 0.5 * mySize_x, barsize, rightcnt, rightname, is_vertical, (iconalign == 1 || iconalign == 3), HUD_Get_Num_Color(rightcnt, rightmax), 1);
2429                         }
2430
2431                         if(fuel)
2432                         {
2433                                 HUD_Panel_GetProgressBarColor(fuel);
2434                                 if (is_vertical) //if vertical always halve x to not cover too much numbers with 3 digits
2435                                         mySize_x *= 0.2 * 0.5 / 2;
2436                                 else
2437                                         mySize_y *= 0.2;
2438                                 HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", fuel/100, is_vertical, (baralign == 1 || baralign == 3), progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2439                         }
2440                 }
2441                 else
2442                 {
2443             barsize = eX * mySize_x + eY * 0.5 * mySize_y;
2444                         if(leftactive)
2445                         {
2446                                 if(autocvar_hud_panel_healtharmor_progressbar)
2447                                 {
2448                                         HUD_Panel_GetProgressBarColorForString(leftname);
2449                                         HUD_Panel_DrawProgressBar(pos, barsize, leftprogressname, leftcnt/leftmax, is_vertical, (baralign == 1 || baralign == 2), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2450                                 }
2451                                 if(autocvar_hud_panel_healtharmor_text)
2452                                         DrawNumIcon(pos, barsize, leftcnt, leftname, is_vertical, (iconalign == 1 || iconalign == 2), HUD_Get_Num_Color(leftcnt, leftmax), 1);
2453                         }
2454
2455                         if(rightactive)
2456                         {
2457                                 if(autocvar_hud_panel_healtharmor_progressbar)
2458                                 {
2459                                         HUD_Panel_GetProgressBarColorForString(rightname);
2460                                         HUD_Panel_DrawProgressBar(pos + eY * 0.5 * mySize_y, barsize, rightprogressname, rightcnt/rightmax, is_vertical, (baralign == 1 || baralign == 3), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2461                                 }
2462                                 if(autocvar_hud_panel_healtharmor_text)
2463                                         DrawNumIcon(pos + eY * 0.5 * mySize_y, barsize, rightcnt, rightname, is_vertical, (iconalign == 1 || iconalign == 3), HUD_Get_Num_Color(rightcnt, rightmax), 1);
2464                         }
2465
2466                         if(fuel)
2467                         {
2468                                 HUD_Panel_GetProgressBarColor(fuel);
2469                                 if (is_vertical) //if vertical always halve x to not cover numbers with 3 digits
2470                                         mySize_x *= 0.2 / 2;
2471                                 else
2472                                         mySize_y *= 0.2 * 0.5;
2473                                 HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", fuel/100, is_vertical, (baralign == 1 || baralign == 3), progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2474                         }
2475                 }
2476         }
2477         drawfont = hud_font;
2478 }
2479
2480 // Notification area (#4)
2481 //
2482
2483 string Weapon_SuicideMessage(float deathtype)
2484 {
2485         w_deathtype = deathtype;
2486         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_SUICIDEMESSAGE);
2487         return w_deathtypestring;
2488 }
2489
2490 string Weapon_KillMessage(float deathtype)
2491 {
2492         w_deathtype = deathtype;
2493         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_KILLMESSAGE);
2494         return w_deathtypestring;
2495 }
2496
2497 #define KN_MAX_ENTRIES 10
2498 float kn_index;
2499 float killnotify_times[KN_MAX_ENTRIES];
2500 float killnotify_deathtype[KN_MAX_ENTRIES];
2501 float killnotify_actiontype[KN_MAX_ENTRIES]; // 0 = "Y [used by] X", 1 = "X [did action to] Y"
2502 string killnotify_attackers[KN_MAX_ENTRIES];
2503 string killnotify_victims[KN_MAX_ENTRIES];
2504 void HUD_KillNotify_Push(string attacker, string victim, float actiontype, float wpn)
2505 {
2506         --kn_index;
2507         if (kn_index == -1)
2508                 kn_index = KN_MAX_ENTRIES-1;
2509         killnotify_times[kn_index] = time;
2510         killnotify_deathtype[kn_index] = wpn;
2511         killnotify_actiontype[kn_index] = actiontype;
2512         if(killnotify_attackers[kn_index])
2513                 strunzone(killnotify_attackers[kn_index]);
2514         killnotify_attackers[kn_index] = strzone(attacker);
2515         if(killnotify_victims[kn_index])
2516                 strunzone(killnotify_victims[kn_index]);
2517         killnotify_victims[kn_index] = strzone(victim);
2518 }
2519
2520 void HUD_KillNotify(string s1, string s2, string s3, float type, float msg) // s1 = attacker, s2 = victim
2521 {
2522         float w;
2523         float alsoprint, gentle;
2524         alsoprint = (autocvar_hud_panel_notify_print || !panel_enabled); // print message to console if: notify panel disabled, or cvar to do so enabled
2525         gentle = (autocvar_cl_gentle || autocvar_cl_gentle_messages);
2526         
2527         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
2528                 return;
2529
2530         if(msg == MSG_SUICIDE) {
2531                 w = DEATH_WEAPONOF(type);
2532                 if(WEP_VALID(w)) {
2533                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2534                         if (alsoprint)
2535                                 print("^1", sprintf(Weapon_SuicideMessage(type), strcat(s1, "^1")), "\n");
2536                 } else if (type == DEATH_KILL) {
2537                         HUD_KillNotify_Push(s1, "", 0, DEATH_KILL);
2538                         if (alsoprint)
2539                                 print ("^1",s1, "^1 couldn't take it anymore\n");
2540                 } else if (type == DEATH_ROT) {
2541                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2542                         if (alsoprint)
2543                                 print ("^1",s1, "^1 died\n");
2544                 } else if (type == DEATH_NOAMMO) {
2545                         HUD_KillNotify_Push(s1, "", 0, DEATH_NOAMMO);
2546                         if (alsoprint)
2547                                 print ("^7",s1, "^7 committed suicide. What's the point of living without ammo?\n");
2548                 } else if (type == DEATH_CAMP) {
2549                         HUD_KillNotify_Push(s1, "", 0, DEATH_CAMP);
2550                         if (alsoprint)
2551                                 print ("^1",s1, "^1 thought they found a nice camping ground\n");
2552                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2553                         HUD_KillNotify_Push(s1, "", 0, type);
2554                         if (alsoprint)
2555                                 print ("^1",s1, "^1 didn't become friends with the Lord of Teamplay\n");
2556                 } else if (type == DEATH_CHEAT) {
2557                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2558                         if (alsoprint)
2559                                 print ("^1",s1, "^1 unfairly eliminated themself\n");
2560                 } else if (type == DEATH_FIRE) {
2561                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2562                         if (alsoprint)
2563                                 print ("^1",s1, "^1 burned to death\n");
2564                 } else if (type != DEATH_TEAMCHANGE && type != DEATH_QUIET) {
2565                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2566                         if (alsoprint)
2567                                 print ("^1",s1, "^1 couldn't resist the urge to self-destruct\n");
2568                 } 
2569                 
2570                 if (stof(s2) > 2) // killcount > 2
2571                         print ("^1",s1,"^1 ended it all after a ",s2," kill spree\n");
2572         } else if(msg == MSG_KILL) {
2573                 w = DEATH_WEAPONOF(type);
2574                 if(WEP_VALID(w)) {
2575                         if((w == WEP_CAMPINGRIFLE || w == WEP_MINSTANEX) && type & HITTYPE_HEADSHOT) // all headshot weapons go here
2576                                 HUD_KillNotify_Push(s1, s2, 1, DEATH_HEADSHOT);
2577                         else
2578                                 HUD_KillNotify_Push(s1, s2, 1, type);
2579
2580                         if (alsoprint)
2581                                 print("^1", sprintf(Weapon_KillMessage(type), strcat(s2, "^1"), strcat(s1, "^1")), "\n"); // default order: victim, killer
2582                 }
2583                 else if(type == KILL_TEAM_RED || type == KILL_TEAM_BLUE || type == KILL_TEAM_SPREE) {
2584                         HUD_KillNotify_Push(s1, s2, 1, type);
2585                         if(alsoprint)
2586                         {
2587                                 if(gentle) {
2588                                         print ("^1", s1, "^1 took action against a team mate\n");
2589                                 } else {
2590                                         print ("^1", s1, "^1 mows down a team mate\n");
2591                                 }
2592                         }
2593                         if (stof(s2) > 2 && type == KILL_TEAM_SPREE) {
2594                                 if(gentle)
2595                                         print ("^1",s1,"^1 ended a ",s3," scoring spree by going against a team mate\n");
2596                                 else
2597                                         print ("^1",s1,"^1 ended a ",s3," kill spree by killing a team mate\n");
2598                         }
2599                         else if (stof(s2) > 2) {
2600                                 if(gentle)
2601                                         print ("^1",s1,"'s ^1",s3," scoring spree was ended by a team mate!\n");
2602                                 else
2603                                         print ("^1",s1,"'s ^1",s3," kill spree was ended by a team mate!\n");
2604                         }
2605                 }
2606                 else if(type == KILL_FIRST_BLOOD)
2607                         print("^1",s1, "^1 drew first blood", "\n");
2608                 else if (type == DEATH_TELEFRAG) {
2609                         HUD_KillNotify_Push(s1, s2, 1, DEATH_TELEFRAG);
2610                         if(gentle)
2611                                 print ("^1",s2, "^1 tried to occupy ", s1, "^1's teleport destination space\n");
2612                         else
2613                                 print ("^1",s2, "^1 was telefragged by ", s1, "\n");
2614                 }
2615                 else if (type == DEATH_DROWN) {
2616                         HUD_KillNotify_Push(s1, s2, 1, DEATH_DROWN);
2617                         if(alsoprint)
2618                                 print ("^1",s2, "^1 was drowned by ", s1, "\n");
2619                 }
2620                 else if (type == DEATH_SLIME) {
2621                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SLIME);
2622                         if(alsoprint)
2623                                 print ("^1",s2, "^1 was slimed by ", s1, "\n");
2624                 }
2625                 else if (type == DEATH_LAVA) {
2626                         HUD_KillNotify_Push(s1, s2, 1, DEATH_LAVA);
2627                         if(alsoprint)
2628                                 print ("^1",s2, "^1 was cooked by ", s1, "\n");
2629                 }
2630                 else if (type == DEATH_FALL) {
2631                         HUD_KillNotify_Push(s1, s2, 1, DEATH_FALL);
2632                         if(alsoprint)
2633                                 print ("^1",s2, "^1 was grounded by ", s1, "\n");
2634                 }
2635                 else if (type == DEATH_SHOOTING_STAR) {
2636                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SHOOTING_STAR);
2637                         if(alsoprint)
2638                                 print ("^1",s2, "^1 was shot into space by ", s1, "\n");
2639                 }
2640                 else if (type == DEATH_SWAMP) {
2641                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2642                         if(alsoprint)
2643                                 print ("^1",s2, "^1 was conserved by ", s1, "\n");
2644                 }
2645                 else if (type == DEATH_HURTTRIGGER)
2646                 {
2647                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2648                         if(alsoprint)
2649                                 print("^1",s2, "^1 was thrown into a world of hurt by ", s1, "\n");
2650                 } else if(type == DEATH_SBCRUSH) {
2651                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2652                         if(alsoprint)
2653                                 print ("^1",s2, "^1 was crushed by ^1", s1, "\n");
2654                 } else if(type == DEATH_SBMINIGUN) {
2655                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2656                         if(alsoprint)
2657                                 print ("^1",s2, "^1 got shredded by ^1", s1, "\n");
2658                 } else if(type == DEATH_SBROCKET) {
2659                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2660                         if(alsoprint)
2661                                 print ("^1",s2, "^1 was blased to bits by ^1", s1, "\n");
2662                 } else if(type == DEATH_SBBLOWUP) {
2663                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2664                         if(alsoprint)
2665                                 print ("^1",s2, "^1 got caught in the destruction of ^1", s1, "'s vehicle\n");
2666                 } else if(type == DEATH_WAKIGUN) {
2667                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2668                         if(alsoprint)
2669                                 print ("^1",s2, "^1 was bolted down by ^1", s1, "\n");
2670                 } else if(type == DEATH_WAKIROCKET) {
2671                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2672                         if(alsoprint)
2673                                 print ("^1",s2, "^1 could find no shelter from ^1", s1, "'s rockets\n");
2674                 } else if(type == DEATH_WAKIBLOWUP) {
2675                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2676                         if(alsoprint)
2677                                 print ("^1",s2, "^1 dies when ^1", s1, "'s wakizashi dies.\n");
2678                 } else if(type == DEATH_TURRET) {
2679                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2680                         if(alsoprint)
2681                                 print ("^1",s2, "^1 was pushed into the line of fire by ^1", s1, "\n");
2682                 } else if(type == DEATH_TOUCHEXPLODE) {
2683                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2684                         if(alsoprint)
2685                                 print ("^1",s2, "^1 was pushed into an accident by ^1", s1, "\n");
2686                 } else if(type == DEATH_CHEAT) {
2687                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2688                         if(alsoprint)
2689                                 print ("^1",s2, "^1 was unfairly eliminated by ^1", s1, "\n");
2690                 } else if (type == DEATH_FIRE) {
2691                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2692                         if(alsoprint)
2693                                 print ("^1",s2, "^1 was burnt to death by ^1", s1, "\n");
2694                 } else if (type == DEATH_CUSTOM) {
2695                         HUD_KillNotify_Push(s1, s2, 1, DEATH_CUSTOM);
2696                         if(alsoprint)
2697                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2698                 } else if (type == DEATH_HURTTRIGGER) {
2699                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2700                         if(alsoprint)
2701                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2702                 } else {
2703                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2704                         if(alsoprint)
2705                                 print ("^1",s2, "^1 was fragged by ", s1, "\n");
2706                 }
2707         } else if(msg == MSG_SPREE) {
2708                 if(type == KILL_END_SPREE) {
2709                         if(gentle)
2710                                 print ("^1",s1,"'s ^1", s2, " scoring spree was ended by ", s3, "\n");
2711                         else
2712                                 print ("^1",s1,"'s ^1", s2, " kill spree was ended by ", s3, "\n");
2713                 } else if(type == KILL_SPREE) {
2714                         if(gentle)
2715                                 print ("^1",s1,"^1 made ",s2," scores in a row\n");
2716                         else
2717                                 print ("^1",s1,"^1 has ",s2," frags in a row\n");
2718                 } else if(type == KILL_SPREE_3) {
2719                         if(gentle)
2720                                 print (s1,"^7 made a ^1TRIPLE SCORE\n");
2721                         else
2722                                 print (s1,"^7 made a ^1TRIPLE FRAG\n");
2723                 } else if(type == KILL_SPREE_5) {
2724                         if(gentle)
2725                                 print (s1,"^7 unleashes ^1SCORING RAGE\n");
2726                         else
2727                                 print (s1,"^7 unleashes ^1RAGE\n");
2728                 } else if(type == KILL_SPREE_10) {
2729                         if(gentle)
2730                                 print (s1,"^7 made ^1TEN SCORES IN A ROW!\n");
2731                         else
2732                                 print (s1,"^7 starts the ^1MASSACRE!\n");
2733                 } else if(type == KILL_SPREE_15) {
2734                         if(gentle)
2735                                 print (s1,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
2736                         else
2737                                 print (s1,"^7 executes ^1MAYHEM!\n");
2738                 } else if(type == KILL_SPREE_20) {
2739                         if(gentle)
2740                                 print (s1,"^7 made ^1TWENTY SCORES IN A ROW!\n");
2741                         else
2742                                 print (s1,"^7 is a ^1BERSERKER!\n");
2743                 } else if(type == KILL_SPREE_25) {
2744                         if(gentle)
2745                                 print (s1,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
2746                         else
2747                                 print (s1,"^7 inflicts ^1CARNAGE!\n");
2748                 } else if(type == KILL_SPREE_30) {
2749                         if(gentle)
2750                                 print (s1,"^7 made ^1THIRTY SCORES IN A ROW!\n");
2751                         else
2752                                 print (s1,"^7 unleashes ^1ARMAGEDDON!\n");
2753                 }
2754         } else if(msg == MSG_KILL_ACTION) { // wtf is this? isnt it basically the same as MSG_SUICIDE?
2755                 if (type == DEATH_DROWN) {
2756                         HUD_KillNotify_Push(s1, "", 0, DEATH_DROWN);
2757                         if(alsoprint)
2758                         {
2759                                 if(gentle)
2760                                         print ("^1",s1, "^1 was in the water for too long\n");
2761                                 else
2762                                         print ("^1",s1, "^1 drowned\n");
2763                         }
2764                 } else if (type == DEATH_SLIME) {
2765                         HUD_KillNotify_Push(s1, "", 0, DEATH_SLIME);
2766                         if(alsoprint)
2767                                 print ("^1",s1, "^1 was slimed\n");
2768                 } else if (type == DEATH_LAVA) {
2769                         HUD_KillNotify_Push(s1, "", 0, DEATH_LAVA);
2770                         if(alsoprint)
2771                         {
2772                                 if(gentle)
2773                                         print ("^1",s1, "^1 found a hot place\n");
2774                                 else
2775                                         print ("^1",s1, "^1 turned into hot slag\n");
2776                         }
2777                 } else if (type == DEATH_FALL) {
2778                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2779                         if(alsoprint)
2780                         {
2781                                 if(gentle)
2782                                         print ("^1",s1, "^1 tested gravity (and it worked)\n");
2783                                 else
2784                                         print ("^1",s1, "^1 hit the ground with a crunch\n");
2785                         }
2786                 } else if (type == DEATH_SHOOTING_STAR) {
2787                         HUD_KillNotify_Push(s1, "", 0, DEATH_SHOOTING_STAR);
2788                         if(alsoprint)
2789                                 print ("^1",s1, "^1 became a shooting star\n");
2790                 } else if (type == DEATH_SWAMP) {
2791                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2792                         if(alsoprint)
2793                         {
2794                                 if(gentle)
2795                                         print ("^1",s1, "^1 discovered a swamp\n");
2796                                 else
2797                                         print ("^1",s1, "^1 is now conserved for centuries to come\n");
2798                         }
2799                 } else if(type == DEATH_TURRET) {
2800                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2801                         if(alsoprint)
2802                                 print ("^1",s1, "^1 was mowed down by a turret \n");
2803                 } else if (type == DEATH_CUSTOM) {
2804                         HUD_KillNotify_Push(s1, "", 0, DEATH_CUSTOM);
2805                         if(alsoprint)
2806                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
2807                 } else if (type == DEATH_HURTTRIGGER) {
2808                         HUD_KillNotify_Push(s1, "", 0, DEATH_HURTTRIGGER);
2809                         if(alsoprint)
2810                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
2811                 } else if(type == DEATH_TOUCHEXPLODE) {
2812                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2813                         if(alsoprint)
2814                                 print ("^1",s1, "^1 died in an accident\n");
2815                 } else if(type == DEATH_CHEAT) {
2816                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2817                         if(alsoprint)
2818                                 print ("^1",s1, "^1 was unfairly eliminated\n");
2819                 } else if(type == DEATH_FIRE) {
2820                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2821                         if(alsoprint)
2822                         {
2823                                 if(gentle)
2824                                         print ("^1",s1, "^1 felt a little hot\n");
2825                                 else
2826                                         print ("^1",s1, "^1 burnt to death\n");
2827                                 }
2828                 } else {
2829                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2830                         if(alsoprint)
2831                         {
2832                                 if(gentle)
2833                                         print ("^1",s1, "^1 needs a restart\n");
2834                                 else
2835                                         print ("^1",s1, "^1 died\n");
2836                         }
2837                 }
2838         } else if(msg == MSG_KILL_ACTION_SPREE) {
2839                 if(gentle)
2840                         print ("^1",s1,"^1 needs a restart after a ",s2," scoring spree\n");
2841                 else
2842                         print ("^1",s1,"^1 died with a ",s2," kill spree\n");
2843         } else if(msg == MSG_INFO) {
2844                 if(type == INFO_GOTFLAG) { // here, s2 is the flag name
2845                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2846                         print(s1, "^7 got the ", s2, "\n");
2847                 } else if(type == INFO_LOSTFLAG) {
2848                         HUD_KillNotify_Push(s1, s2, 0, INFO_LOSTFLAG);
2849                         print(s1, "^7 lost the ", s2, "\n");
2850                 } else if(type == INFO_PICKUPFLAG) {
2851                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2852                         print(s1, "^7 picked up the ", s2, "\n");
2853                 } else if(type == INFO_RETURNFLAG) {
2854                         HUD_KillNotify_Push(s1, s2, 0, INFO_RETURNFLAG);
2855                         print(s1, "^7 returned the ", s2, "\n");
2856                 } else if(type == INFO_CAPTUREFLAG) {
2857                         HUD_KillNotify_Push(s1, s2, 0, INFO_CAPTUREFLAG);
2858                         print(s1, "^7 captured the ", s2, s3, "\n");
2859                 }
2860         } else if(msg == MSG_RACE) {
2861                 if(type == RACE_SERVER_RECORD) {
2862                         HUD_KillNotify_Push(s1, s2, 1, RACE_SERVER_RECORD);
2863                 }
2864                 else if(type == RACE_NEW_RANK) {
2865                         HUD_KillNotify_Push(s1, s2, 1, RACE_NEW_RANK);
2866                 }
2867                 else if(type == RACE_NEW_TIME) {
2868                         HUD_KillNotify_Push(s1, s2, 1, RACE_NEW_TIME);
2869                 }
2870                 else if(type == RACE_FAIL) {
2871                         HUD_KillNotify_Push(s1, s2, 1, RACE_FAIL);
2872                 }
2873         } else if(msg == MSG_KA) {
2874                 if(type == KA_PICKUPBALL) {
2875                         HUD_KillNotify_Push(s1, s2, 0, KA_PICKUPBALL);
2876                         if(alsoprint)
2877                                 print (s1, "^7 has picked up the ball!\n");
2878                 }
2879                 else if(type == KA_DROPBALL) {
2880                         HUD_KillNotify_Push(s1, s2, 0, KA_DROPBALL);
2881                         if(alsoprint)
2882                                 print(s1, "^7 has dropped the ball!\n");
2883                 }
2884         }
2885 }
2886
2887 #define DAMAGE_CENTERPRINT_SPACER NEWLINES
2888
2889 void HUD_Centerprint(string s1, string s2, float type, float msg)
2890 {
2891         float gentle;
2892         gentle = (autocvar_cl_gentle || autocvar_cl_gentle_messages);
2893         if(msg == MSG_SUICIDE) {
2894                 if (type == DEATH_TEAMCHANGE) {
2895                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You are now on: ", s1));
2896                 } else if (type == DEATH_AUTOTEAMCHANGE) {
2897                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You have been moved into a different team to improve team balance\nYou are now on: ", s1));
2898                 } else if (type == DEATH_CAMP) {
2899                         if(gentle)
2900                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Reconsider your tactics, camper!"));
2901                         else
2902                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Die camper!"));
2903                 } else if (type == DEATH_NOAMMO) {
2904                         if(gentle)
2905                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You are reinserted into the game for running out of ammo..."));
2906                         else
2907                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were killed for running out of ammo..."));
2908                 } else if (type == DEATH_ROT) {
2909                         if(gentle)
2910                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to preserve your health"));
2911                         else
2912                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You grew too old without taking your medicine"));
2913                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2914                         if(gentle)
2915                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't go against team mates!"));
2916                         else
2917                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't shoot your team mates!"));
2918                 } else if (type == DEATH_QUIET) {
2919                         // do nothing
2920                 } else { // generic message
2921                         if(gentle)
2922                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to be more careful!"));
2923                         else
2924                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You killed your own dumb self!"));
2925                 }
2926         } else if(msg == MSG_KILL) {
2927                 if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2928                         if(gentle) {
2929                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You went against", s1, ",a team mate!"));
2930                         } else {
2931                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You fragged ", s1, ", a team mate!"));
2932                         }
2933                 } else if (type == KILL_FIRST_BLOOD) {
2934                         if(gentle) {
2935                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First score"));
2936                         } else {
2937                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First blood"));
2938                         }
2939                 } else if (type == KILL_FIRST_VICTIM) {
2940                         if(gentle) {
2941                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First casualty"));
2942                         } else {
2943                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First victim"));
2944                         }
2945                 } else if (type == KILL_TYPEFRAG) { // s2 contains "advanced kill messages" such as ping, handicap...
2946                         if(gentle) {
2947                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You scored against ^7", s1, "^1 who was typing!", s2));
2948                         } else {
2949                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You typefragged ^7", s1, s2));
2950                         }
2951                 } else if (type == KILL_TYPEFRAGGED) {
2952                         if(gentle) {
2953                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, "^1 while you were typing!", s2));
2954                         } else {
2955                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were typefragged by ^7", s1, s2));
2956                         }
2957                 } else if (type == KILL_FRAG) {
2958                         if(gentle) {
2959                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You scored against ^7", s1, s2));
2960                         } else {
2961                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You fragged ^7", s1, s2));
2962                         }
2963                 } else { // generic message
2964                         if(gentle) {
2965                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, s2));
2966                         } else {
2967                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were fragged by ^7", s1, s2));
2968                         }
2969                 }
2970         } else if(msg == MSG_KILL_ACTION) {
2971                 // TODO: invent more centerprints here?
2972                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
2973         }
2974 }
2975
2976 void HUD_Notify (void)
2977 {
2978         if(!autocvar_hud_panel_notify && !autocvar__hud_configure)
2979                 return;
2980
2981         active_panel = HUD_PANEL_NOTIFY;
2982         HUD_Panel_UpdateCvars(notify);
2983         vector pos, mySize;
2984         pos = panel_pos;
2985         mySize = panel_size;
2986
2987         HUD_Panel_DrawBg(1);
2988         if(panel_bg_padding)
2989         {
2990                 pos += '1 1 0' * panel_bg_padding;
2991                 mySize -= '2 2 0' * panel_bg_padding;
2992         }
2993
2994         float entries, height;
2995         entries = bound(1, floor(KN_MAX_ENTRIES * mySize_y/mySize_x), KN_MAX_ENTRIES);
2996         height = mySize_y/entries;
2997         
2998         vector fontsize;
2999         fontsize = '0.5 0.5 0' * height;
3000
3001         float a;
3002         float when;
3003         when = autocvar_hud_panel_notify_time;
3004         float fadetime;
3005         fadetime = autocvar_hud_panel_notify_fadetime;
3006
3007         string s;
3008
3009         vector pos_attacker, pos_victim;
3010         vector weap_pos;
3011         float width_attacker;
3012         string attacker, victim;
3013
3014         float i, j, w, step, limit;
3015         if(autocvar_hud_panel_notify_flip) //order items from the top down
3016         {
3017                 i = 0;
3018                 step = +1;
3019                 limit = entries;
3020         }
3021         else //order items from the bottom up
3022         {
3023                 i = entries - 1;
3024                 step = -1;
3025                 limit = -1;
3026         }
3027
3028         for(j = kn_index;  i != limit;  i += step, ++j)
3029         {
3030                 if(autocvar__hud_configure)
3031                 {
3032                         if (step == +1)
3033                                 a = i;
3034                         else // inverse order
3035                                 a = entries - 1 - i;
3036                         attacker = textShortenToWidth(strcat("Player", ftos(a+1)), 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3037                         victim = textShortenToWidth(strcat("Player", ftos(a+2)), 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3038                         s = strcat("weapon", get_weaponinfo(WEP_FIRST + mod(floor(a*2.4), WEP_LAST)).netname);
3039                         a = bound(0, (when - a) / 4, 1);
3040                         goto hud_config_notifyprint;
3041                 }
3042
3043                 if (j == KN_MAX_ENTRIES)
3044                         j = 0;
3045
3046                 if(killnotify_times[j] + when > time)
3047                         a = 1;
3048                 else if(fadetime)
3049                 {
3050                         a = bound(0, (killnotify_times[j] + when + fadetime - time) / fadetime, 1);
3051                         if(!a)
3052                         {
3053                                 break;
3054                         }
3055                 }
3056                 else
3057                 {
3058                         break;
3059                 }
3060
3061                 s = "";
3062
3063                 w = -1;
3064                 w = DEATH_WEAPONOF(killnotify_deathtype[j]);
3065
3066                 // TODO: maybe print in team colors?
3067                 //
3068                 // Y [used by] X
3069                 if(killnotify_actiontype[j] == 0) 
3070                 {
3071                         if(killnotify_deathtype[j] == DEATH_GENERIC)
3072                         {
3073                                 s = "notify_death";
3074                         }
3075                         else if(killnotify_deathtype[j] == DEATH_NOAMMO)
3076                         {
3077                                 s = "notify_outofammo";
3078                         }
3079                         else if(killnotify_deathtype[j] == DEATH_KILL)
3080                         {
3081                                 s = "notify_selfkill";
3082                         }
3083                         else if(killnotify_deathtype[j] == DEATH_CAMP)
3084                         {
3085                                 s = "notify_camping";
3086                         }
3087                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
3088                         {
3089                                 s = "notify_teamkill_red";
3090                         }
3091                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
3092                         {
3093                                 s = "notify_teamkill_blue";
3094                         }
3095                         else if(killnotify_deathtype[j] == DEATH_DROWN)
3096                         {
3097                                 s = "notify_water";
3098                         }
3099                         else if(killnotify_deathtype[j] == DEATH_SLIME)
3100                         {
3101                                 s = "notify_slime";
3102                         }
3103                         else if(killnotify_deathtype[j] == DEATH_LAVA)
3104                         {
3105                                 s = "notify_lava";
3106                         }
3107                         else if(killnotify_deathtype[j] == DEATH_FALL)
3108                         {
3109                                 s = "notify_fall";
3110                         }
3111                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
3112                         {
3113                                 s = "notify_shootingstar";
3114                         }
3115                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM)
3116                         {
3117                                 s = "notify_death";
3118                         }
3119                         else if(killnotify_deathtype[j] == INFO_GOTFLAG)
3120                         {
3121                                 if(killnotify_victims[j] == "^1RED^7 flag")
3122                                 {
3123                                         s = "notify_red_taken";
3124                                 }
3125                                 else
3126                                 {
3127                                         s = "notify_blue_taken";
3128                                 }
3129                         }
3130                         else if(killnotify_deathtype[j] == INFO_RETURNFLAG)
3131                         {
3132                                 if(killnotify_victims[j] == "^1RED^7 flag")
3133                                 {
3134                                         s = "notify_red_returned";
3135                                 }
3136                                 else
3137                                 {
3138                                         s = "notify_blue_returned";
3139                                 }
3140                         }
3141                         else if(killnotify_deathtype[j] == INFO_LOSTFLAG)
3142                         {
3143                                 if(killnotify_victims[j] == "^1RED^7 flag")
3144                                 {
3145                                         s = "notify_red_lost";
3146                                 }
3147                                 else
3148                                 {
3149                                         s = "notify_blue_lost";
3150                                 }
3151                         }
3152                         else if(killnotify_deathtype[j] == INFO_CAPTUREFLAG)
3153                         {
3154                                 if(killnotify_victims[j] == "^1RED^7 flag")
3155                                 {
3156                                         s = "notify_red_captured";
3157                                 }
3158                                 else
3159                                 {
3160                                         s = "notify_blue_captured";
3161                                 }
3162                         }
3163                         else if(killnotify_deathtype[j] == KA_DROPBALL)
3164                         {
3165                                 s = "notify_balldropped";
3166                         }
3167                         else if(killnotify_deathtype[j] == KA_PICKUPBALL)
3168                         {
3169                                 s = "notify_ballpickedup";
3170                         }
3171                         
3172                         attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3173                         pos_attacker = pos + eX * (0.52 * mySize_x + height) + eY * (0.5 * fontsize_y + i * height);
3174                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
3175
3176                         if(s != "")
3177                         {
3178                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3179                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3180                         }
3181                 }
3182                 // X [did action to] Y
3183                 else
3184                 {
3185                         if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_LASER)
3186                         {
3187                                 s = "notify_melee_laser";
3188                         }
3189                         else if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_SHOTGUN)
3190                         {
3191                                 s = "notify_melee_shotgun";
3192                         }
3193                         else if(WEP_VALID(w))
3194                         {
3195                                 self = get_weaponinfo(w);
3196                                 s = strcat("weapon", self.netname);
3197                         }
3198                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
3199                         {
3200                                 s = "notify_teamkill_red";
3201                         }
3202                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
3203                         {
3204                                 s = "notify_teamkill_red";
3205                         }
3206                         else if(killnotify_deathtype[j] == DEATH_TELEFRAG)
3207                         {
3208                                 s = "notify_telefrag";
3209                         }
3210                         else if(killnotify_deathtype[j] == DEATH_DROWN)
3211                         {
3212                                 s = "notify_water";
3213                         }
3214                         else if(killnotify_deathtype[j] == DEATH_SLIME)
3215                         {
3216                                 s = "notify_slime";
3217                         }
3218                         else if(killnotify_deathtype[j] == DEATH_LAVA)
3219                         {
3220                                 s = "notify_lava";
3221                         }
3222                         else if(killnotify_deathtype[j] == DEATH_FALL)
3223                         {
3224                                 s = "notify_fall";
3225                         }
3226                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
3227                         {
3228                                 s = "notify_shootingstar";
3229                         }
3230                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM) // DEATH_CUSTOM is also void, right?
3231                         {
3232                                 s = "notify_void";
3233                         }
3234                         else if(killnotify_deathtype[j] == DEATH_HEADSHOT)
3235                         {
3236                                 s = "notify_headshot";
3237                         }
3238                         else if(killnotify_deathtype[j] == RACE_SERVER_RECORD)
3239                         {
3240                                 s = "race_newrecordserver";
3241                         }
3242                         else if(killnotify_deathtype[j] == RACE_NEW_RANK)
3243                         {
3244                                 s = "race_newrankyellow";
3245                         }
3246                         else if(killnotify_deathtype[j] == RACE_NEW_TIME)
3247                         {
3248                                 s = "race_newtime";
3249                         }
3250                         else if(killnotify_deathtype[j] == RACE_FAIL)
3251                         {
3252                                 s = "race_newfail";
3253                         }
3254
3255                         attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3256                         victim = textShortenToWidth(killnotify_victims[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3257 :hud_config_notifyprint
3258                         width_attacker = stringwidth(attacker, TRUE, fontsize);
3259                         pos_attacker = pos + eX * (0.48 * mySize_x - height - width_attacker) + eY * (0.5 * fontsize_y + i * height);
3260                         pos_victim = pos + eX * (0.52 * mySize_x + height) + eY * (0.5 * fontsize_y + i * height);
3261                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
3262
3263                         if(s != "")
3264                         {
3265                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3266                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3267                                 drawcolorcodedstring(pos_victim, victim, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3268                         }
3269                 }
3270         }
3271 }
3272
3273 // Timer (#5)
3274 //
3275 // TODO: macro
3276 string seconds_tostring(float sec)
3277 {
3278         float minutes;
3279         minutes = floor(sec / 60);
3280
3281         sec -= minutes * 60;
3282
3283         string s;
3284         s = ftos(100 + sec);
3285
3286         return strcat(ftos(minutes), ":", substring(s, 1, 3));
3287 }
3288
3289 void HUD_Timer(void)
3290 {
3291         if(!autocvar_hud_panel_timer && !autocvar__hud_configure)
3292                 return;
3293
3294         active_panel = HUD_PANEL_TIMER;
3295         HUD_Panel_UpdateCvars(timer);
3296         vector pos, mySize;
3297         pos = panel_pos;
3298         mySize = panel_size;
3299
3300         HUD_Panel_DrawBg(1);
3301         if(panel_bg_padding)
3302         {
3303                 pos += '1 1 0' * panel_bg_padding;
3304                 mySize -= '2 2 0' * panel_bg_padding;
3305         }
3306
3307         string timer;
3308         float timelimit, elapsedTime, timeleft, minutesLeft;
3309
3310         timelimit = getstatf(STAT_TIMELIMIT);
3311
3312         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
3313         timeleft = ceil(timeleft);
3314
3315         minutesLeft = floor(timeleft / 60);
3316
3317         vector timer_color;
3318         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
3319                 timer_color = '1 1 1'; //white
3320         else if(minutesLeft >= 1)
3321                 timer_color = '1 1 0'; //yellow
3322         else
3323                 timer_color = '1 0 0'; //red
3324
3325         if (autocvar_hud_panel_timer_increment || timelimit == 0 || warmup_stage) {
3326                 if (time < getstatf(STAT_GAMESTARTTIME)) {
3327                         //while restart is still active, show 00:00
3328                         timer = seconds_tostring(0);
3329                 } else {
3330                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
3331                         timer = seconds_tostring(elapsedTime);
3332                 }
3333         } else {
3334                 timer = seconds_tostring(timeleft);
3335         }
3336
3337         drawfont = hud_bigfont;
3338         drawstring_aspect(pos, timer, mySize, timer_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3339         drawfont = hud_font;
3340 }
3341
3342 // Radar (#6)
3343 //
3344 void HUD_Radar(void)
3345 {
3346         if ((autocvar_hud_panel_radar == 0 || (autocvar_hud_panel_radar != 2 && !teamplay)) && !autocvar__hud_configure)
3347                 return;
3348
3349         active_panel = HUD_PANEL_RADAR;
3350         HUD_Panel_UpdateCvars(radar);
3351         vector pos, mySize;
3352         pos = panel_pos;
3353         mySize = panel_size;
3354
3355         HUD_Panel_DrawBg(1);
3356         if(panel_bg_padding)
3357         {
3358                 pos += '1 1 0' * panel_bg_padding;
3359                 mySize -= '2 2 0' * panel_bg_padding;
3360         }
3361
3362         local float color2;
3363         local entity tm;
3364         float scale2d, normalsize, bigsize;
3365         float f;
3366
3367         teamradar_origin2d = pos + 0.5 * mySize;
3368         teamradar_size2d = mySize;
3369
3370         if(minimapname == "")
3371                 return;
3372
3373         teamradar_loadcvars();
3374
3375         switch(hud_panel_radar_zoommode)
3376         {
3377                 default:
3378                 case 0:
3379                         f = current_zoomfraction;
3380                         break;
3381                 case 1:
3382                         f = 1 - current_zoomfraction;
3383                         break;
3384                 case 2:
3385                         f = 0;
3386                         break;
3387                 case 3:
3388                         f = 1;
3389                         break;
3390         }
3391
3392         switch(hud_panel_radar_rotation)
3393         {
3394                 case 0:
3395                         teamradar_angle = view_angles_y - 90;
3396                         break;
3397                 default:
3398                         teamradar_angle = 90 * hud_panel_radar_rotation;
3399                         break;
3400         }
3401
3402         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
3403         teamradar_size2d = mySize;
3404
3405         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
3406
3407         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
3408         if(hud_panel_radar_rotation == 0)
3409         {
3410                 // max-min distance must fit the radar in any rotation
3411                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
3412         }
3413         else
3414         {
3415                 vector c0, c1, c2, c3, span;
3416                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
3417                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
3418                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
3419                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
3420                 span = '0 0 0';
3421                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
3422                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
3423
3424                 // max-min distance must fit the radar in x=x, y=y
3425                 bigsize = min(
3426                         teamradar_size2d_x * scale2d / (1.05 * span_x),
3427                         teamradar_size2d_y * scale2d / (1.05 * span_y)
3428                 );
3429         }
3430
3431         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
3432         if(bigsize > normalsize)
3433                 normalsize = bigsize;
3434
3435         teamradar_size =
3436                   f * bigsize
3437                 + (1 - f) * normalsize;
3438         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
3439                   f * (mi_min + mi_max) * 0.5
3440                 + (1 - f) * view_origin);
3441
3442         drawsetcliparea(
3443                 pos_x,
3444                 pos_y,
3445                 mySize_x,
3446                 mySize_y
3447         );
3448
3449         draw_teamradar_background(hud_panel_radar_foreground_alpha);
3450
3451         for(tm = world; (tm = find(tm, classname, "radarlink")); )
3452                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
3453         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
3454                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, panel_fg_alpha);
3455         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
3456         {
3457                 color2 = GetPlayerColor(tm.sv_entnum);
3458                 //if(color == COLOR_SPECTATOR || color == color2)
3459                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
3460         }
3461         draw_teamradar_player(view_origin, view_angles, '1 1 1');
3462
3463         drawresetcliparea();
3464 };
3465
3466 // Score (#7)
3467 //
3468 void HUD_UpdatePlayerTeams();
3469 void HUD_Score(void)
3470 {
3471         if(!autocvar__hud_configure && !autocvar_hud_panel_score)
3472                 return;
3473
3474         active_panel = HUD_PANEL_SCORE;
3475         HUD_Panel_UpdateCvars(score);
3476         vector pos, mySize;
3477         pos = panel_pos;
3478         mySize = panel_size;
3479
3480         HUD_Panel_DrawBg(1);
3481         if(panel_bg_padding)
3482         {
3483                 pos += '1 1 0' * panel_bg_padding;
3484                 mySize -= '2 2 0' * panel_bg_padding;
3485         }
3486
3487         float score, distribution;
3488         string sign;
3489         vector distribution_color;
3490         entity tm, pl, me;
3491         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
3492
3493         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
3494                 string timer, distrtimer;
3495
3496                 pl = players.sort_next;
3497                 if(pl == me)
3498                         pl = pl.sort_next;
3499                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
3500                         if(pl.scores[ps_primary] == 0)
3501                                 pl = world;
3502
3503                 score = me.(scores[ps_primary]);
3504                 timer = TIME_ENCODED_TOSTRING(score);
3505
3506                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
3507                         // distribution display
3508                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3509
3510                         distrtimer = ftos_decimals(fabs(distribution/pow(10, TIME_DECIMALS)), TIME_DECIMALS);
3511
3512                         if (distribution <= 0) {
3513                                 distribution_color = '0 1 0';
3514                                 sign = "-";
3515                         }
3516                         else {
3517                                 distribution_color = '1 0 0';
3518                                 sign = "+";
3519                         }
3520                         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);
3521                 }
3522                 // race record display
3523                 if (distribution <= 0)
3524                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3525                 drawfont = hud_bigfont;
3526                 drawstring_aspect(pos, timer, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3527                 drawfont = hud_font;
3528         } else if (!teamplay) { // non-teamgames
3529                 if ((spectatee_status == -1 && !autocvar__hud_configure) || autocvar_hud_panel_score_rankings)
3530                 {
3531 #define SCOREPANEL_MAX_ENTRIES 6
3532 #define SCOREPANEL_ASPECTRATIO 2
3533                         float entries = bound(1, floor(SCOREPANEL_MAX_ENTRIES * mySize_y/mySize_x * SCOREPANEL_ASPECTRATIO), SCOREPANEL_MAX_ENTRIES);
3534                         float height = mySize_y/entries;
3535                         vector fontsize = '0.9 0.9 0' * height;
3536                         pos_y += height * (1 - 0.9) / 2;
3537
3538                         vector rgb;
3539                         rgb = '1 1 1';
3540
3541                         float name_size = mySize_x*0.75;
3542                         float i, me_printed;
3543                         string s;
3544                         if (autocvar__hud_configure)
3545                         {
3546                                 score = 10 + SCOREPANEL_MAX_ENTRIES * 3;
3547                                 for (i=0; i<entries; ++i)
3548                                 {
3549                                         //simulate my score is lower than all displayed players,
3550                                         //so that I don't appear at all showing pure rankings.
3551                                         //This is to better show the difference between the 2 ranking views
3552                                         if (i == entries-1 && autocvar_hud_panel_score_rankings == 1)
3553                                         {
3554                                                 rgb = '1 1 0';
3555                                                 drawfill(pos - eY * (height * (1 - 0.9) / 2), eX * mySize_x + eY * height, rgb, 0.3 * panel_fg_alpha, DRAWFLAG_NORMAL);
3556                                                 s = GetPlayerName(pl.sv_entnum);
3557                                                 score = 7;
3558                                         }
3559                                         else
3560                                         {
3561                                                 s = strcat("Player", ftos(i+1));
3562                                                 score -= 3;
3563                                         }
3564
3565                                         s = textShortenToWidth(s, name_size, fontsize, stringwidth_colors);
3566                                         drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, TRUE, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
3567                                         drawstring(pos + eX * mySize_x*0.79, ftos(score), fontsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3568                                         pos_y += height;
3569                                 }
3570                                 return;
3571                         }
3572
3573                         if (!scoreboard_fade_alpha) // the scoreboard too calls HUD_UpdatePlayerTeams
3574                                 HUD_UpdatePlayerTeams();
3575
3576                         for (pl = players.sort_next, i=0; pl && i<entries; pl = pl.sort_next, ++i)
3577                         {
3578                                 if (pl.team == COLOR_SPECTATOR)
3579                                         continue;
3580
3581                                 if (i == entries-1 && !me_printed && pl != me)
3582                                 if (autocvar_hud_panel_score_rankings == 1 && spectatee_status != -1)
3583                                 {
3584                                         for (pl = me.sort_next; pl; pl = pl.sort_next)
3585                                                 if (pl.team != COLOR_SPECTATOR)
3586                                                         break;
3587
3588                                         if (pl)
3589                                                 rgb = '1 1 0'; //not last but not among the leading players: yellow
3590                                         else
3591                                                 rgb = '1 0 0'; //last: red
3592                                         pl = me;
3593                                 }
3594
3595                                 if (pl == me)
3596                                 {
3597                                         if (i == 0)
3598                                                 rgb = '0 1 0'; //first: green
3599                                         me_printed = 1;
3600                                         drawfill(pos - eY * (height * (1 - 0.9) / 2), eX * mySize_x + eY * height, rgb, 0.3 * panel_fg_alpha, DRAWFLAG_NORMAL);
3601                                 }
3602                                 s = textShortenToWidth(GetPlayerName(pl.sv_entnum), name_size, fontsize, stringwidth_colors);
3603                                 drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, TRUE, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
3604                                 drawstring(pos + eX * mySize_x*0.79, ftos(pl.(scores[ps_primary])), fontsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3605                                 pos_y += height;
3606                         }
3607                         return;
3608                 }
3609                 // me vector := [team/connected frags id]
3610                 pl = players.sort_next;
3611                 if(pl == me)
3612                         pl = pl.sort_next;
3613
3614                 if(autocvar__hud_configure)
3615                         distribution = 42;
3616                 else if(pl)
3617                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3618                 else
3619                         distribution = 0;
3620
3621                 score = me.(scores[ps_primary]);
3622                 if(autocvar__hud_configure)
3623                         score = 123;
3624
3625                 if(distribution >= 5)
3626                         distribution_color = eY;
3627                 else if(distribution >= 0)
3628                         distribution_color = '1 1 1';
3629                 else if(distribution >= -5)
3630                         distribution_color = '1 1 0';
3631                 else
3632                         distribution_color = eX;
3633
3634                 string distribution_str;
3635                 distribution_str = ftos(distribution);
3636                 if (distribution >= 0)
3637                 {
3638                         if (distribution > 0)
3639                                 distribution_str = strcat("+", distribution_str);
3640                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3641                 }
3642                 drawfont = hud_bigfont;
3643                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3644                 drawfont = hud_font;
3645                 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);
3646         } else { // teamgames
3647                 float max_fragcount;
3648                 max_fragcount = -99;
3649
3650                 float scores_count, row, column, rows, columns;
3651                 vector offset;
3652                 vector score_pos, score_size; //for scores other than myteam
3653                 if (spectatee_status == -1)
3654                 {
3655                         if (autocvar__hud_configure)
3656                                 scores_count = 4;
3657                         else for(tm = teams.sort_next; tm; tm = tm.sort_next) {
3658                                 if(tm.team == COLOR_SPECTATOR)
3659                                         continue;
3660                                 ++scores_count;
3661                         }
3662                         rows = mySize_y/mySize_x;
3663                         rows = bound(1, floor((sqrt(4 * (3/1) * rows * scores_count + rows * rows) + rows + 0.5) / 2), scores_count);
3664                         //                               ^^^ ammo item aspect goes here
3665
3666                         columns = ceil(scores_count/rows);
3667
3668                         score_size = eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows);
3669
3670                         float newSize;
3671                         if(score_size_x/score_size_y > 3)
3672                         {
3673                                 newSize = 3 * score_size_y;
3674                                 offset_x = score_size_x - newSize;
3675                                 pos_x += offset_x/2;
3676                                 score_size_x = newSize;
3677                         }
3678                         else
3679                         {
3680                                 newSize = 1/3 * score_size_x;
3681                                 offset_y = score_size_y - newSize;
3682                                 pos_y += offset_y/2;
3683                                 score_size_y = newSize;
3684                         }
3685                 }
3686                 else
3687                         score_size = eX * mySize_x*(1/4) + eY * mySize_y*(1/3);
3688                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
3689                         if(tm.team == COLOR_SPECTATOR)
3690                                 continue;
3691                         score = tm.(teamscores[ts_primary]);
3692                         if(autocvar__hud_configure)
3693                                 score = 123;
3694                         
3695                         if (score > max_fragcount)
3696                                 max_fragcount = score;
3697
3698                         if (spectatee_status == -1)
3699                         {
3700                                 score_pos = pos + eX * column * (score_size_x + offset_x) + eY * row * (score_size_y + offset_y);
3701                                 if (max_fragcount == score)
3702                                         HUD_Panel_DrawHighlight(score_pos, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3703                                 drawfont = hud_bigfont;
3704                                 drawstring_aspect(score_pos, ftos(score), score_size, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
3705                                 drawfont = hud_font;
3706                                 ++row;
3707                                 if(row >= rows)
3708                                 {
3709                                         row = 0;
3710                                         ++column;
3711                                 }
3712                         }
3713                         else if(tm.team == myteam) {
3714                                 if (max_fragcount == score)
3715                                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3716                                 drawfont = hud_bigfont;
3717                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
3718                                 drawfont = hud_font;
3719                         } else {
3720                                 if (max_fragcount == score)
3721                                         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);
3722                                 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);
3723                                 ++rows;
3724                         }
3725                 }
3726         }
3727 }
3728
3729 // Race timer (#8)
3730 //
3731 void HUD_RaceTimer (void) {
3732         if(!autocvar__hud_configure)
3733         {
3734                 if(!autocvar_hud_panel_racetimer) return;
3735                 if(!(gametype == GAME_RACE || gametype == GAME_CTS)) return;
3736                 if(spectatee_status == -1) return;
3737         }
3738
3739         active_panel = HUD_PANEL_RACETIMER;
3740         HUD_Panel_UpdateCvars(racetimer);
3741         vector pos, mySize;
3742         pos = panel_pos;
3743         mySize = panel_size;
3744
3745         HUD_Panel_DrawBg(1);
3746         if(panel_bg_padding)
3747         {
3748                 pos += '1 1 0' * panel_bg_padding;
3749                 mySize -= '2 2 0' * panel_bg_padding;
3750         }
3751
3752         // always force 4:1 aspect
3753         vector newSize;
3754         if(mySize_x/mySize_y > 4)
3755         {
3756                 newSize_x = 4 * mySize_y;
3757                 newSize_y = mySize_y;
3758
3759                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3760         }
3761         else
3762         {
3763                 newSize_y = 1/4 * mySize_x;
3764                 newSize_x = mySize_x;
3765
3766                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3767         }
3768         mySize = newSize;
3769
3770         drawfont = hud_bigfont;
3771         float a, t;
3772         string s, forcetime;
3773
3774         if(autocvar__hud_configure)
3775         {
3776                 s = "0:13:37";
3777                 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);
3778                 s = "^1Intermediate 1 (+15.42)";
3779                 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);
3780                 s = strcat("^1PENALTY: ", ftos_decimals(20 * 0.1, 1), " (missing a checkpoint)");
3781                 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);
3782         }
3783         else if(race_checkpointtime)
3784         {
3785                 a = bound(0, 2 - (time - race_checkpointtime), 1);
3786                 s = "";
3787                 forcetime = "";
3788                 if(a > 0) // just hit a checkpoint?
3789                 {
3790                         if(race_checkpoint != 254)
3791                         {
3792                                 if(race_time && race_previousbesttime)
3793                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
3794                                 else
3795                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
3796                                 if(race_time)
3797                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
3798                         }
3799                 }
3800                 else
3801                 {
3802                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
3803                         {
3804                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
3805                                 if(a > 0) // next one?
3806                                 {
3807                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
3808                                 }
3809                         }
3810                 }
3811
3812                 if(s != "" && a > 0)
3813                 {
3814                         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);
3815                 }
3816
3817                 if(race_penaltytime)
3818                 {
3819                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
3820                         if(a > 0)
3821                         {
3822                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
3823                                 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);
3824                         }
3825                 }
3826
3827                 if(forcetime != "")
3828                 {
3829                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
3830                         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);
3831                 }
3832                 else
3833                         a = 1;
3834
3835                 if(race_laptime && race_checkpoint != 255)
3836                 {
3837                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
3838                         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);
3839                 }
3840         }
3841         else
3842         {
3843                 if(race_mycheckpointtime)
3844                 {
3845                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
3846                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
3847                         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);
3848                 }
3849                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
3850                 {
3851                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
3852                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
3853                         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);
3854                 }
3855
3856                 if(race_penaltytime && !race_penaltyaccumulator)
3857                 {
3858                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
3859                         a = bound(0, (1 + t - time), 1);
3860                         if(a > 0)
3861                         {
3862                                 if(time < t)
3863                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
3864                                 else
3865                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
3866                                 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);
3867                         }
3868                 }
3869         }
3870
3871         drawfont = hud_font;
3872 }
3873
3874 // Vote window (#9)
3875 //
3876 float vote_yescount;
3877 float vote_nocount;
3878 float vote_needed;
3879 float vote_highlighted; // currently selected vote
3880
3881 float vote_active; // is there an active vote?
3882 float vote_prev; // previous state of vote_active to check for a change
3883 float vote_alpha;
3884 float vote_change; // "time" when vote_active changed
3885
3886 void HUD_VoteWindow(void) 
3887 {
3888     uid2name_dialog = 0;
3889         if(autocvar_cl_allow_uid2name == -1 && (gametype == GAME_CTS || gametype == GAME_RACE))
3890         {
3891                 vote_active = 1;
3892                 vote_called_vote = strzone(strcat("^2Name ^7instead of \"^1Unregistered player\"", " ^7in stats"));
3893         uid2name_dialog = 1;
3894         }
3895
3896         if(!autocvar_hud_panel_vote && !autocvar__hud_configure)
3897                 return;
3898
3899         if(!autocvar__hud_configure)
3900         {
3901                 panel_fg_alpha = autocvar_hud_panel_fg_alpha;
3902                 panel_bg_alpha_str = autocvar_hud_panel_vote_bg_alpha;
3903
3904                 if(panel_bg_alpha_str == "") {
3905                         panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);
3906                 }
3907                 panel_bg_alpha = stof(panel_bg_alpha_str);
3908         }
3909
3910         string s;
3911         float a;
3912         if(vote_active != vote_prev) {
3913                 vote_change = time;
3914                 vote_prev = vote_active;
3915         }
3916
3917         if(vote_active || autocvar__hud_configure)
3918                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
3919         else
3920                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
3921
3922         if(autocvar__hud_configure)
3923         {
3924                 vote_yescount = 3;
3925                 vote_nocount = 2;
3926                 vote_needed = 4;
3927         }
3928
3929         if(!vote_alpha)
3930                 return;
3931
3932         active_panel = HUD_PANEL_VOTE;
3933         HUD_Panel_UpdateCvars(vote);
3934
3935         if(uid2name_dialog)
3936         {
3937                 panel_pos = eX * 0.3 * vid_conwidth + eY * 0.1 * vid_conheight;
3938                 panel_size = eX * 0.4 * vid_conwidth + eY * 0.3 * vid_conheight;
3939         }
3940
3941     // these must be below above block
3942         vector pos, mySize;
3943         pos = panel_pos;
3944         mySize = panel_size;
3945
3946         a = vote_alpha * bound(autocvar_hud_panel_vote_alreadyvoted_alpha, 1 - vote_highlighted, 1);
3947         HUD_Panel_DrawBg(a);
3948         a = panel_fg_alpha * a;
3949
3950         if(panel_bg_padding)
3951         {
3952                 pos += '1 1 0' * panel_bg_padding;
3953                 mySize -= '2 2 0' * panel_bg_padding;
3954         }
3955
3956         // always force 3:1 aspect
3957         vector newSize;
3958         if(mySize_x/mySize_y > 3)
3959         {
3960                 newSize_x = 3 * mySize_y;
3961                 newSize_y = mySize_y;
3962
3963                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3964         }
3965         else
3966         {
3967                 newSize_y = 1/3 * mySize_x;
3968                 newSize_x = mySize_x;
3969
3970                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3971         }
3972         mySize = newSize;
3973
3974         s = "A vote has been called for:";
3975         if(uid2name_dialog)
3976                 s = "Allow servers to store and display your name?";
3977         drawstring_aspect(pos, s, eX * mySize_x + eY * (2/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3978         s = textShortenToWidth(vote_called_vote, mySize_x, '1 1 0' * mySize_y * (1/8), stringwidth_colors);
3979         if(autocvar__hud_configure)
3980                 s = "^1Configure the HUD";
3981         drawcolorcodedstring_aspect(pos + eY * (2/8) * mySize_y, s, eX * mySize_x + eY * (1.75/8) * mySize_y, a, DRAWFLAG_NORMAL);
3982
3983         // print the yes/no counts
3984     s = strcat("Yes (", getcommandkey("vyes", "vyes"), "): ", ftos(vote_yescount));
3985         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);
3986     s = strcat("No (", getcommandkey("vno", "vno"), "): ", ftos(vote_nocount));
3987         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);
3988
3989         // draw the progress bar backgrounds
3990         drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_back", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3991
3992         // draw the highlights
3993         if(vote_highlighted == 1) {
3994                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
3995                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3996         }
3997         else if(vote_highlighted == 2) {
3998                 drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
3999                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
4000         }
4001
4002         // draw the progress bars
4003         if(vote_yescount && vote_needed)
4004         {
4005                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
4006                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
4007         }
4008
4009         if(vote_nocount && vote_needed)
4010         {
4011                 drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
4012                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
4013         }
4014
4015         drawresetcliparea();
4016
4017         if(!vote_active) {
4018                 vote_highlighted = 0;
4019         }
4020 }
4021
4022 // Mod icons panel (#10)
4023 //
4024
4025 float mod_active; // is there any active mod icon?
4026
4027 // Clan Arena HUD modicons
4028 void HUD_Mod_CA(vector pos, vector mySize)
4029 {
4030         mod_active = 1; // CA should never hide the mod icons panel
4031         float redalive, bluealive;
4032         redalive = getstati(STAT_REDALIVE);
4033         bluealive = getstati(STAT_BLUEALIVE);
4034
4035         drawfont = hud_bigfont;
4036         vector redpos, bluepos;
4037         if(mySize_x > mySize_y)
4038         {
4039                 redpos = pos;
4040                 bluepos = pos + eY * 0.5 * mySize_y;
4041                 drawpic_aspect_skin(redpos, "player_red.tga", 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4042                 drawstring_aspect(redpos + eX * 0.5 * mySize_x, ftos(redalive), 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4043                 drawpic_aspect_skin(bluepos, "player_blue.tga", 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4044                 drawstring_aspect(bluepos + eX * 0.5 * mySize_x, ftos(bluealive), 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4045         }
4046         else
4047         {
4048                 redpos = pos;
4049                 bluepos = pos + eY * 0.5 * mySize_y;
4050                 drawpic_aspect_skin(redpos, "player_red.tga", eX * mySize_x + eY * 0.3 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4051                 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);
4052                 drawpic_aspect_skin(bluepos, "player_blue.tga", eX * mySize_x + eY * 0.3 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4053                 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);
4054         }
4055         drawfont = hud_font;
4056 }
4057
4058 // CTF HUD modicon section
4059 float redflag_prevframe, blueflag_prevframe; // status during previous frame
4060 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
4061 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
4062
4063 void HUD_Mod_CTF_Reset(void)
4064 {
4065         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
4066 }
4067
4068 void HUD_Mod_CTF(vector pos, vector mySize)
4069 {
4070         vector redflag_pos, blueflag_pos;
4071         vector flag_size;
4072         float f; // every function should have that
4073
4074         float redflag, blueflag; // current status
4075         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
4076         float stat_items;
4077
4078         stat_items = getstati(STAT_ITEMS);
4079         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
4080         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
4081         
4082         if(redflag || blueflag)
4083                 mod_active = 1;
4084         else
4085                 mod_active = 0;
4086
4087         if(autocvar__hud_configure)
4088         {
4089                 redflag = 1;
4090                 blueflag = 2;
4091         }
4092
4093         // when status CHANGES, set old status into prevstatus and current status into status
4094         if (redflag != redflag_prevframe)
4095         {
4096                 redflag_statuschange_time = time;
4097                 redflag_prevstatus = redflag_prevframe;
4098                 redflag_prevframe = redflag;
4099         }
4100
4101         if (blueflag != blueflag_prevframe)
4102         {
4103                 blueflag_statuschange_time = time;
4104                 blueflag_prevstatus = blueflag_prevframe;
4105                 blueflag_prevframe = blueflag;
4106         }
4107
4108         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
4109         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
4110
4111         float BLINK_FACTOR = 0.15;
4112         float BLINK_BASE = 0.85;
4113         // note:
4114         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
4115         // thus
4116         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
4117         // ensure RMS == 1
4118         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
4119
4120         string red_icon, red_icon_prevstatus;
4121         float red_alpha, red_alpha_prevstatus;
4122         red_alpha = red_alpha_prevstatus = 1;
4123         switch(redflag) {
4124                 case 1: red_icon = "flag_red_taken"; break;
4125                 case 2: red_icon = "flag_red_lost"; break;
4126                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4127                 default:
4128                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
4129                                 red_icon = "flag_red_shielded";
4130                         else
4131                                 red_icon = string_null;
4132                         break;
4133         }
4134         switch(redflag_prevstatus) {
4135                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
4136                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
4137                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4138                 default:
4139                         if(redflag == 3)
4140                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
4141                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
4142                                 red_icon_prevstatus = "flag_red_shielded";
4143                         else
4144                                 red_icon_prevstatus = string_null;
4145                         break;
4146         }
4147
4148         string blue_icon, blue_icon_prevstatus;
4149         float blue_alpha, blue_alpha_prevstatus;
4150         blue_alpha = blue_alpha_prevstatus = 1;
4151         switch(blueflag) {
4152                 case 1: blue_icon = "flag_blue_taken"; break;
4153                 case 2: blue_icon = "flag_blue_lost"; break;
4154                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4155                 default:
4156                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
4157                                 blue_icon = "flag_blue_shielded";
4158                         else
4159                                 blue_icon = string_null;
4160                         break;
4161         }
4162         switch(blueflag_prevstatus) {
4163                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
4164                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
4165                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4166                 default:
4167                         if(blueflag == 3)
4168                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
4169                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
4170                                 blue_icon_prevstatus = "flag_blue_shielded";
4171                         else
4172                                 blue_icon_prevstatus = string_null;
4173                         break;
4174         }
4175
4176         if(mySize_x > mySize_y) {
4177                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
4178                         redflag_pos = pos;
4179                         blueflag_pos = pos + eX * 0.5 * mySize_x;
4180                 } else {
4181                         blueflag_pos = pos;
4182                         redflag_pos = pos + eX * 0.5 * mySize_x;
4183                 }
4184                 flag_size = eX * 0.5 * mySize_x + eY * mySize_y;
4185         } else {
4186                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
4187                         redflag_pos = pos;
4188                         blueflag_pos = pos + eY * 0.5 * mySize_y;
4189                 } else {
4190                         blueflag_pos = pos;
4191                         redflag_pos = pos + eY * 0.5 * mySize_y;
4192                 }
4193                 flag_size = eY * 0.5 * mySize_y + eX * mySize_x;
4194         }
4195
4196         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
4197         if(red_icon_prevstatus && f < 1)
4198                 drawpic_aspect_skin_expanding(redflag_pos, red_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
4199         if(red_icon)
4200                 drawpic_aspect_skin(redflag_pos, red_icon, flag_size, '1 1 1', panel_fg_alpha * red_alpha * f, DRAWFLAG_NORMAL);
4201
4202         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
4203         if(blue_icon_prevstatus && f < 1)
4204                 drawpic_aspect_skin_expanding(blueflag_pos, blue_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
4205         if(blue_icon)
4206                 drawpic_aspect_skin(blueflag_pos, blue_icon, flag_size, '1 1 1', panel_fg_alpha * blue_alpha * f, DRAWFLAG_NORMAL);
4207 }
4208
4209 // Keyhunt HUD modicon section
4210 float kh_runheretime;
4211
4212 void HUD_Mod_KH_Reset(void)
4213 {
4214         kh_runheretime = 0;
4215 }
4216
4217 void HUD_Mod_KH(vector pos, vector mySize)
4218 {
4219         mod_active = 1; // keyhunt should never hide the mod icons panel
4220         float kh_keys;
4221         float keyteam;
4222         float a, aa;
4223         vector p, pa, kh_size, kh_asize;
4224
4225         kh_keys = getstati(STAT_KH_KEYS);
4226
4227         p_x = pos_x;
4228         if(mySize_x > mySize_y)
4229         {
4230                 p_y = pos_y + 0.25 * mySize_y;
4231                 pa = p - eY * 0.25 * mySize_y;
4232
4233                 kh_size_x = mySize_x * 0.25;
4234                 kh_size_y = 0.75 * mySize_y;
4235                 kh_asize_x = mySize_x * 0.25;
4236                 kh_asize_y = mySize_y * 0.25;
4237         }
4238         else
4239         {
4240                 p_y = pos_y + 0.125 * mySize_y;
4241                 pa = p - eY * 0.125 * mySize_y;
4242
4243                 kh_size_x = mySize_x * 0.5;
4244                 kh_size_y = 0.375 * mySize_y;
4245                 kh_asize_x = mySize_x * 0.5;
4246                 kh_asize_y = mySize_y * 0.125;
4247         }
4248
4249         float i, key;
4250
4251         float keycount;
4252         keycount = 0;
4253         for(i = 0; i < 4; ++i)
4254         {
4255                 key = floor(kh_keys / pow(32, i)) & 31;
4256                 keyteam = key - 1;
4257                 if(keyteam == 30 && keycount <= 4)
4258                         keycount += 4;
4259                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
4260                         keycount += 1;
4261         }
4262
4263         // this yields 8 exactly if "RUN HERE" shows
4264
4265         if(keycount == 8)
4266         {
4267                 if(!kh_runheretime)
4268                         kh_runheretime = time;
4269                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
4270         }
4271         else
4272                 kh_runheretime = 0;
4273
4274         for(i = 0; i < 4; ++i)
4275         {
4276                 key = floor(kh_keys / pow(32, i)) & 31;
4277                 keyteam = key - 1;
4278                 switch(keyteam)
4279                 {
4280                         case 30: // my key
4281                                 keyteam = myteam;
4282                                 a = 1;
4283                                 aa = 1;
4284                                 break;
4285                         case -1: // no key
4286                                 a = 0;
4287                                 aa = 0;
4288                                 break;
4289                         default: // owned or dropped
4290                                 a = 0.2;
4291                                 aa = 0.5;
4292                                 break;
4293                 }
4294                 a = a * panel_fg_alpha;
4295                 aa = aa * panel_fg_alpha;
4296                 if(a > 0)
4297                 {
4298                         switch(keyteam)
4299                         {
4300                                 case COLOR_TEAM1:
4301                                         drawpic_aspect_skin(pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4302                                         break;
4303                                 case COLOR_TEAM2:
4304                                         drawpic_aspect_skin(pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4305                                         break;
4306                                 case COLOR_TEAM3:
4307                                         drawpic_aspect_skin(pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4308                                         break;
4309                                 case COLOR_TEAM4:
4310                                         drawpic_aspect_skin(pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4311                                         break;
4312                                 default:
4313                                         break;
4314                         }
4315                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
4316                         {
4317                                 case 0:
4318                                         drawpic_aspect_skin(p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4319                                         break;
4320                                 case 1:
4321                                         drawpic_aspect_skin(p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4322                                         break;
4323                                 case 2:
4324                                         drawpic_aspect_skin(p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4325                                         break;
4326                                 case 3:
4327                                         drawpic_aspect_skin(p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4328                                         break;
4329                         }
4330                 }
4331                 if(mySize_x > mySize_y)
4332                 {
4333                         p_x += 0.25 * mySize_x;
4334                         pa_x += 0.25 * mySize_x;
4335                 }
4336                 else
4337                 {
4338                         if(i == 1)
4339                         {
4340                                 p_y = pos_y + 0.625 * mySize_y;
4341                                 pa_y = pos_y + 0.5 * mySize_y;
4342                                 p_x = pos_x;
4343                                 pa_x = pos_x;
4344                         }
4345                         else
4346                         {
4347                                 p_x += 0.5 * mySize_x;
4348                                 pa_x += 0.5 * mySize_x;
4349                         }
4350                 }
4351         }
4352 }
4353
4354 // Keepaway HUD mod icon
4355 float kaball_prevstatus; // last remembered status
4356 float kaball_statuschange_time; // time when the status changed
4357
4358 // we don't need to reset for keepaway since it immediately 
4359 // autocorrects prevstatus as to if the player has the ball or not
4360
4361 void HUD_Mod_Keepaway(vector pos, vector mySize)
4362 {
4363         mod_active = 1; // keepaway should always show the mod HUD
4364         
4365         float BLINK_FACTOR = 0.15;
4366         float BLINK_BASE = 0.85;
4367         float BLINK_FREQ = 5; 
4368         float kaball_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
4369         
4370         float stat_items = getstati(STAT_ITEMS);
4371         float kaball = (stat_items/IT_KEY1) & 1;
4372         
4373         if(kaball != kaball_prevstatus)
4374         {
4375                 kaball_statuschange_time = time;
4376                 kaball_prevstatus = kaball;
4377         }
4378         
4379         // todo: Fix the sizing with the expanding image
4380         
4381         float kaball_statuschange_elapsedtime = time - kaball_statuschange_time;
4382         float f = bound(0, kaball_statuschange_elapsedtime*2, 1);
4383         
4384         if(kaball_prevstatus && f < 1)
4385                 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);
4386         
4387         if(kaball)
4388                 drawpic_aspect_skin(pos, "keepawayball_carrying", eX * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha * kaball_alpha * f, DRAWFLAG_NORMAL);
4389 }
4390
4391
4392 // Nexball HUD mod icon
4393 void HUD_Mod_NexBall(vector pos, vector mySize)
4394 {
4395         float stat_items, nb_pb_starttime, dt, p;
4396
4397         stat_items = getstati(STAT_ITEMS);
4398         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
4399
4400         if (stat_items & IT_KEY1)
4401                 mod_active = 1;
4402         else
4403                 mod_active = 0;
4404
4405         //Manage the progress bar if any
4406         if (nb_pb_starttime > 0)
4407         {
4408                 dt = mod(time - nb_pb_starttime, nb_pb_period);
4409                 // one period of positive triangle
4410                 p = 2 * dt / nb_pb_period;
4411                 if (p > 1)
4412                         p = 2 - p;
4413
4414                 //Draw the filling
4415                 HUD_Panel_GetProgressBarColor(nexball);
4416                 if(mySize_x > mySize_y)
4417                         HUD_Panel_DrawProgressBar(pos, mySize, "statusbar", p, 0, 0, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4418                 else
4419                         HUD_Panel_DrawProgressBar(pos, mySize, "statusbar", p, 1, 0, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4420         }
4421
4422         if (stat_items & IT_KEY1)
4423                 drawpic_aspect_skin(pos, "nexball_carrying", eX * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4424 }
4425
4426 // Race/CTS HUD mod icons
4427 float crecordtime_prev; // last remembered crecordtime
4428 float crecordtime_change_time; // time when crecordtime last changed
4429 float srecordtime_prev; // last remembered srecordtime
4430 float srecordtime_change_time; // time when srecordtime last changed
4431
4432 float race_status_time;
4433 float race_status_prev;
4434 string race_status_name_prev;
4435 void HUD_Mod_Race(vector pos, vector mySize)
4436 {
4437         mod_active = 1; // race should never hide the mod icons panel
4438         entity me;
4439         me = playerslots[player_localentnum - 1];
4440         float t, score;
4441         float f; // yet another function has this
4442         score = me.(scores[ps_primary]);
4443
4444         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
4445                 return; // no records in the actual race
4446
4447         drawfont = hud_bigfont;
4448
4449         // clientside personal record
4450         string rr;
4451         if(gametype == GAME_CTS)
4452                 rr = CTS_RECORD;
4453         else
4454                 rr = RACE_RECORD;
4455         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
4456
4457         if(score && (score < t || !t)) {
4458                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
4459                 if(autocvar_cl_autodemo_delete_keeprecords)
4460                 {
4461                         f = autocvar_cl_autodemo_delete;
4462                         f &~= 1;
4463                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
4464                 }
4465         }
4466
4467         if(t != crecordtime_prev) {
4468                 crecordtime_prev = t;
4469                 crecordtime_change_time = time;
4470         }
4471
4472         vector textPos, medalPos;
4473         float squareSize;
4474         if(mySize_x > mySize_y) {
4475                 // text on left side
4476                 squareSize = min(mySize_y, mySize_x/2);
4477                 textPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eY * 0.5 * (mySize_y - squareSize);
4478                 medalPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eX * 0.5 * mySize_x + eY * 0.5 * (mySize_y - squareSize);
4479         } else {
4480                 // text on top
4481                 squareSize = min(mySize_x, mySize_y/2);
4482                 textPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eX * 0.5 * (mySize_x - squareSize);
4483                 medalPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eY * 0.5 * mySize_y + eX * 0.5 * (mySize_x - squareSize);
4484         }
4485
4486         f = time - crecordtime_change_time;
4487
4488         if (f > 1) {
4489                 drawstring_aspect(textPos, "Personal best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4490                 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);
4491         } else {
4492                 drawstring_aspect(textPos, "Personal best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4493                 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);
4494                 drawstring_aspect_expanding(pos, "Personal best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
4495                 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);
4496         }
4497
4498         // server record
4499         t = race_server_record;
4500         if(t != srecordtime_prev) {
4501                 srecordtime_prev = t;
4502                 srecordtime_change_time = time;
4503         }
4504         f = time - srecordtime_change_time;
4505
4506         if (f > 1) {
4507                 drawstring_aspect(textPos + eY * 0.5 * squareSize, "Server best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4508                 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);
4509         } else {
4510                 drawstring_aspect(textPos + eY * 0.5 * squareSize, "Server best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4511                 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);
4512                 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);
4513                 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);
4514         }
4515
4516         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
4517                 race_status_time = time + 5;
4518                 race_status_prev = race_status;
4519                 if (race_status_name_prev)
4520                         strunzone(race_status_name_prev);
4521                 race_status_name_prev = strzone(race_status_name);
4522         }
4523
4524         // race "awards"
4525         float a;
4526         a = bound(0, race_status_time - time, 1);
4527
4528         string s;
4529         s = textShortenToWidth(race_status_name, squareSize, '1 1 0' * 0.1 * squareSize, stringwidth_colors);
4530
4531         float rank;
4532         if(race_status > 0)
4533                 rank = race_CheckName(race_status_name);
4534         string rankname;
4535         rankname = race_PlaceName(rank);
4536
4537         vector namepos;
4538         namepos = medalPos + '0 0.8 0' * squareSize;
4539         vector rankpos;
4540         rankpos = medalPos + '0 0.15 0' * squareSize;
4541
4542         if(race_status == 0)
4543                 drawpic_aspect_skin(medalPos, "race_newfail", '1 1 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4544         else if(race_status == 1) {
4545                 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);
4546                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4547                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4548         } else if(race_status == 2) {
4549                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
4550                         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);
4551                 else
4552                         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);
4553                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4554                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4555         } else if(race_status == 3) {
4556                 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);
4557                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4558                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4559         }
4560
4561         if (race_status_time - time <= 0) {
4562                 race_status_prev = -1;
4563                 race_status = -1;
4564                 if(race_status_name)
4565                         strunzone(race_status_name);
4566                 race_status_name = string_null;
4567                 if(race_status_name_prev)
4568                         strunzone(race_status_name_prev);
4569                 race_status_name_prev = string_null;
4570         }
4571         drawfont = hud_font;
4572 }
4573
4574 float mod_prev; // previous state of mod_active to check for a change
4575 float mod_alpha;
4576 float mod_change; // "time" when mod_active changed
4577
4578 void HUD_ModIcons(void)
4579 {
4580         if(!autocvar_hud_panel_modicons && !autocvar__hud_configure)
4581                 return;
4582
4583         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)
4584                 return;
4585
4586         active_panel = HUD_PANEL_MODICONS;
4587         HUD_Panel_UpdateCvars(modicons);
4588         vector pos, mySize;
4589         pos = panel_pos;
4590         mySize = panel_size;
4591
4592         if(mod_active != mod_prev) {
4593                 mod_change = time;
4594                 mod_prev = mod_active;
4595         }
4596
4597         if(mod_active || autocvar__hud_configure)
4598                 mod_alpha = bound(0, (time - mod_change) * 2, 1);
4599         else
4600                 mod_alpha = bound(0, 1 - (time - mod_change) * 2, 1);
4601
4602         if(mod_alpha)
4603                 HUD_Panel_DrawBg(mod_alpha);
4604
4605         if(panel_bg_padding)
4606         {
4607                 pos += '1 1 0' * panel_bg_padding;
4608                 mySize -= '2 2 0' * panel_bg_padding;
4609         }
4610
4611         // these MUST be ran in order to update mod_active
4612         if(gametype == GAME_KEYHUNT)
4613                 HUD_Mod_KH(pos, mySize);
4614         else if(gametype == GAME_CTF || autocvar__hud_configure)
4615                 HUD_Mod_CTF(pos, mySize); // forcealpha only needed for ctf icons, as only they are shown in config mode
4616         else if(gametype == GAME_NEXBALL)
4617                 HUD_Mod_NexBall(pos, mySize);
4618         else if(gametype == GAME_CTS || gametype == GAME_RACE)
4619                 HUD_Mod_Race(pos, mySize);
4620         else if(gametype == GAME_CA || gametype == GAME_FREEZETAG)
4621                 HUD_Mod_CA(pos, mySize);
4622         else if(gametype == GAME_KEEPAWAY)
4623                 HUD_Mod_Keepaway(pos, mySize);
4624 }
4625
4626 // Draw pressed keys (#11)
4627 //
4628 void HUD_DrawPressedKeys(void)
4629 {
4630         if(!autocvar_hud_panel_pressedkeys && !autocvar__hud_configure)
4631                 return;
4632
4633         if(!(spectatee_status > 0 || autocvar_hud_panel_pressedkeys >= 2 || autocvar__hud_configure))
4634                 return;
4635
4636         active_panel = HUD_PANEL_PRESSEDKEYS;
4637         HUD_Panel_UpdateCvars(pressedkeys);
4638         vector pos, mySize;
4639         pos = panel_pos;
4640         mySize = panel_size;
4641
4642         HUD_Panel_DrawBg(1);
4643         if(panel_bg_padding)
4644         {
4645                 pos += '1 1 0' * panel_bg_padding;
4646                 mySize -= '2 2 0' * panel_bg_padding;
4647         }
4648
4649         // force custom aspect
4650         float aspect = autocvar_hud_panel_pressedkeys_aspect;
4651         if(aspect)
4652         {
4653                 vector newSize;
4654                 if(mySize_x/mySize_y > aspect)
4655                 {
4656                         newSize_x = aspect * mySize_y;
4657                         newSize_y = mySize_y;
4658
4659                         pos_x = pos_x + (mySize_x - newSize_x) / 2;
4660                 }
4661                 else
4662                 {
4663                         newSize_y = 1/aspect * mySize_x;
4664                         newSize_x = mySize_x;
4665
4666                         pos_y = pos_y + (mySize_y - newSize_y) / 2;
4667                 }
4668                 mySize = newSize;
4669         }
4670
4671         vector keysize;
4672         keysize = eX * mySize_x * (1/3) + eY * mySize_y * 0.5;
4673         float pressedkeys;
4674         pressedkeys = getstatf(STAT_PRESSED_KEYS);
4675
4676         drawpic_aspect_skin(pos, ((pressedkeys & KEY_CROUCH) ? "key_crouch_inv.tga" : "key_crouch.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4677         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);
4678         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);
4679         pos_y += keysize_y;
4680         drawpic_aspect_skin(pos, ((pressedkeys & KEY_LEFT) ? "key_left_inv.tga" : "key_left.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4681         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);
4682         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);
4683 }
4684
4685 // Handle chat as a panel (#12)
4686 //
4687 void HUD_Chat(void)
4688 {
4689         if(!autocvar_hud_panel_chat && !autocvar__hud_configure)
4690         {
4691                 cvar_set("con_chatrect", "0");
4692                 return;
4693         }
4694
4695         active_panel = HUD_PANEL_CHAT;
4696         HUD_Panel_UpdateCvars(chat);
4697
4698         if(autocvar__con_chat_maximized && !autocvar__hud_configure) // draw at full screen height if maximized
4699         {
4700                 panel_pos_y = panel_bg_border;
4701                 panel_size_y = vid_conheight - panel_bg_border * 2;
4702                 if(panel_bg == "0") // force a border when maximized
4703                 {
4704                         if(precache_pic(panel_bg) == "") {
4705                                 panel_bg = strcat(hud_skin_path, "/border_default");
4706                                 if(precache_pic(panel_bg) == "") {
4707                                         panel_bg = "gfx/hud/default/border_default";
4708                                 }
4709                         }
4710                 }
4711                 panel_bg_alpha = max(0.75, panel_bg_alpha); // force an alpha of at least 0.75
4712         }
4713
4714         vector pos, mySize;
4715         pos = panel_pos;
4716         mySize = panel_size;
4717
4718         HUD_Panel_DrawBg(1);
4719
4720         if(panel_bg_padding)
4721         {
4722                 pos += '1 1 0' * panel_bg_padding;
4723                 mySize -= '2 2 0' * panel_bg_padding;
4724         }
4725
4726         cvar_set("con_chatrect", "1");
4727
4728         cvar_set("con_chatrect_x", ftos(pos_x/vid_conwidth));
4729         cvar_set("con_chatrect_y", ftos(pos_y/vid_conheight));
4730
4731         cvar_set("con_chatwidth", ftos(mySize_x/vid_conwidth));
4732         cvar_set("con_chat", ftos(floor(mySize_y/autocvar_con_chatsize - 0.5)));
4733
4734         if(autocvar__hud_configure)
4735         {
4736                 float chatsize;
4737                 chatsize = autocvar_con_chatsize;
4738                 cvar_set("con_chatrect_x", "9001"); // over 9000, we'll fake it instead for more control over alpha and such
4739                 float i, a;
4740                 for(i = 0; i < autocvar_con_chat; ++i)
4741                 {
4742                         if(i == autocvar_con_chat - 1)
4743                                 a = panel_fg_alpha;
4744                         else
4745                                 a = panel_fg_alpha * floor(((i + 1) * 7 + autocvar_con_chattime)/45);
4746                         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);
4747                 }
4748         }
4749 }
4750
4751 // Engine info panel (#13)
4752 //
4753 float prevfps;
4754 float prevfps_time;
4755 float framecounter;
4756
4757 float frametimeavg;
4758 float frametimeavg1; // 1 frame ago
4759 float frametimeavg2; // 2 frames ago
4760 void HUD_EngineInfo(void)
4761 {
4762         if(!autocvar_hud_panel_engineinfo && !autocvar__hud_configure)
4763                 return;
4764
4765         active_panel = HUD_PANEL_ENGINEINFO;
4766         HUD_Panel_UpdateCvars(engineinfo);
4767         vector pos, mySize;
4768         pos = panel_pos;
4769         mySize = panel_size;
4770
4771         HUD_Panel_DrawBg(1);
4772         if(panel_bg_padding)
4773         {
4774                 pos += '1 1 0' * panel_bg_padding;
4775                 mySize -= '2 2 0' * panel_bg_padding;
4776         }
4777
4778         float currentTime = gettime(GETTIME_REALTIME);
4779         if(cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage"))
4780         {
4781                 float currentframetime = currentTime - prevfps_time;
4782                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + currentframetime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
4783                 frametimeavg2 = frametimeavg1;
4784                 frametimeavg1 = frametimeavg;
4785                 
4786                 float weight;
4787                 weight = cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight");
4788                 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.
4789                 {
4790                         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
4791                                 prevfps = (1/currentframetime);
4792                         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"
4793                 }
4794                 prevfps_time = currentTime;
4795         }
4796         else
4797         {
4798                 framecounter += 1;
4799                 if(currentTime - prevfps_time > autocvar_hud_panel_engineinfo_framecounter_time)
4800                 {
4801                         prevfps = framecounter/(currentTime - prevfps_time);
4802                         framecounter = 0;
4803                         prevfps_time = currentTime;
4804                 }
4805         }
4806
4807         vector color;
4808         color = HUD_Get_Num_Color (prevfps, 100);
4809         drawfont = hud_bigfont;
4810         drawstring_aspect(pos, strcat("FPS: ", ftos_decimals(prevfps, autocvar_hud_panel_engineinfo_framecounter_decimals)), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
4811         drawfont = hud_font;
4812 }
4813
4814 // Info messages panel (#14)
4815 //
4816 #define drawInfoMessage(s)\
4817         if(autocvar_hud_panel_infomessages_flip)\
4818                 o_x = pos_x + mySize_x - stringwidth(s, TRUE, fontsize);\
4819         drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);\
4820         o_y += fontsize_y;
4821 void HUD_InfoMessages(void)
4822 {
4823         if(!autocvar_hud_panel_infomessages && !autocvar__hud_configure)
4824                 return;
4825
4826         active_panel = HUD_PANEL_INFOMESSAGES;
4827         HUD_Panel_UpdateCvars(infomessages);
4828         vector pos, mySize;
4829         pos = panel_pos;
4830         mySize = panel_size;
4831
4832         HUD_Panel_DrawBg(1);
4833         if(panel_bg_padding)
4834         {
4835                 pos += '1 1 0' * panel_bg_padding;
4836                 mySize -= '2 2 0' * panel_bg_padding;
4837         }
4838
4839         // always force 5:1 aspect
4840         vector newSize;
4841         if(mySize_x/mySize_y > 5)
4842         {
4843                 newSize_x = 5 * mySize_y;
4844                 newSize_y = mySize_y;
4845
4846                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
4847         }
4848         else
4849         {
4850                 newSize_y = 1/5 * mySize_x;
4851                 newSize_x = mySize_x;
4852
4853                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
4854         }
4855
4856         mySize = newSize;
4857         entity tm;
4858         vector o;
4859         o = pos;
4860
4861         vector fontsize;
4862         fontsize = '0.20 0.20 0' * mySize_y;
4863         
4864         float a;
4865         if(spectatee_status != 0)
4866                 a = 1;
4867         else
4868                 a = panel_fg_alpha;
4869
4870         string s;
4871         if(!autocvar__hud_configure)
4872         {
4873                 if(spectatee_status && !intermission)
4874                 {
4875                         if(spectatee_status == -1)
4876                                 s = "^1Observing";
4877                         else
4878                                 s = strcat("^1Spectating: ^7", GetPlayerName(spectatee_status - 1));
4879                         drawInfoMessage(s)
4880
4881                         if(spectatee_status == -1)
4882                                 s = strcat("^1Press ^3", getcommandkey("primary fire", "+fire"), "^1 to spectate");
4883                         else
4884                                 s = strcat("^1Press ^3", getcommandkey("primary fire", "+fire"), "^1 for another player");
4885                         drawInfoMessage(s)
4886
4887                         if(spectatee_status == -1)
4888                                 s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
4889                         else
4890                                 s = strcat("^1Press ^3", getcommandkey("secondary fire", "+fire2"), "^1 to observe");
4891                         drawInfoMessage(s)
4892
4893                         s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
4894                         drawInfoMessage(s)
4895
4896                         if(gametype == GAME_ARENA)
4897                                 s = "^1Wait for your turn to join";
4898                         else if(gametype == GAME_LMS)
4899                         {
4900                                 entity sk;
4901                                 sk = playerslots[player_localentnum - 1];
4902                                 if(sk.(scores[ps_primary]) >= 666)
4903                                         s = "^1Match has already begun";
4904                                 else if(sk.(scores[ps_primary]) > 0)
4905                                         s = "^1You have no more lives left";
4906                                 else
4907                                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
4908                         }
4909                         else
4910                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
4911                         drawInfoMessage(s)
4912
4913                         //show restart countdown:
4914                         if (time < getstatf(STAT_GAMESTARTTIME)) {
4915                                 float countdown;
4916                                 //we need to ceil, otherwise the countdown would be off by .5 when using round()
4917                                 countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
4918                                 s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
4919                                 drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);
4920                                 o_y += fontsize_y;
4921                         }
4922                 }
4923                 if(warmup_stage && !intermission)
4924                 {
4925                         s = "^2Currently in ^1warmup^2 stage!";
4926                         drawInfoMessage(s)
4927                 }
4928
4929                 string blinkcolor;
4930                 if(mod(time, 1) >= 0.5)
4931                         blinkcolor = "^1";
4932                 else
4933                         blinkcolor = "^3";
4934
4935                 if(ready_waiting && !intermission && !spectatee_status)
4936                 {
4937                         if(ready_waiting_for_me)
4938                         {
4939                                 if(warmup_stage)
4940                                         s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
4941                                 else
4942                                         s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
4943                         }
4944                         else
4945                         {
4946                                 if(warmup_stage)
4947                                         s = strcat("^2Waiting for others to ready up to end warmup...");
4948                                 else
4949                                         s = strcat("^2Waiting for others to ready up...");
4950                         }
4951                         drawInfoMessage(s)
4952                 }
4953                 else if(warmup_stage && !intermission && !spectatee_status)
4954                 {
4955                         s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
4956                         drawInfoMessage(s)
4957                 }
4958
4959                 if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
4960                 {
4961                         float ts_min, ts_max;
4962                         tm = teams.sort_next;
4963                         if (tm)
4964                         {
4965                                 for(; tm.sort_next; tm = tm.sort_next)
4966                                 {
4967                                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
4968                                                 continue;
4969                                         if(!ts_min) ts_min = tm.team_size;
4970                                         else ts_min = min(ts_min, tm.team_size);
4971                                         if(!ts_max) ts_max = tm.team_size;
4972                                         else ts_max = max(ts_max, tm.team_size);
4973                                 }
4974                                 if ((ts_max - ts_min) > 1)
4975                                 {
4976                                         s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
4977                                         tm = GetTeam(myteam, false);
4978                                         if (tm)
4979                                         if (tm.team != COLOR_SPECTATOR)
4980                                         if (tm.team_size == ts_max)
4981                                                 s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
4982                                         drawInfoMessage(s)
4983                                 }
4984                         }
4985                 }
4986         }
4987         else 
4988         {
4989                 s = "^7Press ^3ESC ^7to show HUD options.";
4990                 drawInfoMessage(s)
4991                 s = "^3Doubleclick ^7a panel for panel-specific options.";
4992                 drawInfoMessage(s)
4993                 s = "^3CTRL ^7to disable collision testing, ^3SHIFT ^7and";
4994                 drawInfoMessage(s)
4995                 s = "^3ALT ^7+ ^3ARROW KEYS ^7for fine adjustments.";
4996                 drawInfoMessage(s)
4997         }
4998 }
4999
5000 /*
5001 ==================
5002 Main HUD system
5003 ==================
5004 */
5005
5006 void HUD_ShowSpeed(void)
5007 {
5008         vector numsize;
5009         float pos, conversion_factor;
5010         string speed, zspeed, unit;
5011
5012         switch(autocvar_cl_showspeed_unit)
5013         {
5014                 default:
5015                 case 0:
5016                         unit = "";
5017                         conversion_factor = 1.0;
5018                         break;
5019                 case 1:
5020                         unit = " qu/s";
5021                         conversion_factor = 1.0;
5022                         break;
5023                 case 2:
5024                         unit = " m/s";
5025                         conversion_factor = 0.0254;
5026                         break;
5027                 case 3:
5028                         unit = " km/h";
5029                         conversion_factor = 0.0254 * 3.6;
5030                         break;
5031                 case 4:
5032                         unit = " mph";
5033                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
5034                         break;
5035                 case 5:
5036                         unit = " knots";
5037                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
5038                         break;
5039         }
5040
5041         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
5042
5043         numsize_x = numsize_y = autocvar_cl_showspeed_size;
5044         pos = (vid_conheight - numsize_y) * autocvar_cl_showspeed_position;
5045
5046         drawfont = hud_bigfont;
5047         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
5048
5049         if (autocvar_cl_showspeed_z == 1) {
5050                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
5051                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
5052         }
5053
5054         drawfont = hud_font;
5055 }
5056
5057 vector acc_prevspeed;
5058 float acc_prevtime;
5059 float acc_avg;
5060
5061 void HUD_ShowAcceleration(void)
5062 {
5063         float acceleration, sz, scale, alpha, f;
5064         vector pos, mySize, rgb;
5065
5066         f = time - acc_prevtime;
5067         if(autocvar_cl_showacceleration_z)
5068                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
5069         else
5070                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
5071         acc_prevspeed = pmove_vel;
5072         acc_prevtime = time;
5073
5074         f = bound(0, f * 10, 1);
5075         acc_avg = acc_avg * (1 - f) + acceleration * f;
5076         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
5077         if (acceleration == 0)
5078                 return;
5079
5080         sz = autocvar_cl_showacceleration_size;
5081         scale = autocvar_cl_showacceleration_scale;
5082         alpha = autocvar_cl_showacceleration_alpha;
5083         if (autocvar_cl_showacceleration_color_custom)
5084                 rgb = stov(autocvar_cl_showacceleration_color);
5085         else {
5086                 if (acceleration < 0)
5087                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
5088                 else
5089                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
5090         }
5091
5092         mySize_x = vid_conwidth/2;
5093         mySize_y = sz;
5094         pos_y = autocvar_cl_showacceleration_position * vid_conheight - sz/2;
5095         if (acceleration > 0)
5096         {
5097                 pos_x = mySize_x;
5098                 HUD_Panel_DrawProgressBar(pos, mySize, "statusbar", acceleration * scale, 0, 0, rgb, alpha * autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
5099         }
5100         else
5101         {
5102                 //pos_x = 0;
5103                 HUD_Panel_DrawProgressBar(pos, mySize, "statusbar", -acceleration * scale, 0, 1, rgb, alpha * autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
5104         }
5105 }
5106
5107 void HUD_Reset (void)
5108 {
5109         // reset gametype specific icons
5110         if(gametype == GAME_KEYHUNT)
5111                 HUD_Mod_KH_Reset();
5112         else if(gametype == GAME_CTF)
5113                 HUD_Mod_CTF_Reset();
5114 }
5115
5116 #define HUD_DrawPanel(id)\
5117 switch (id) {\
5118         case (HUD_PANEL_RADAR):\
5119                 HUD_Radar(); break;\
5120         case (HUD_PANEL_WEAPONS):\
5121                 HUD_Weapons(); break;\
5122         case (HUD_PANEL_AMMO):\
5123                 HUD_Ammo(); break;\
5124         case (HUD_PANEL_POWERUPS):\
5125                 HUD_Powerups(); break;\
5126         case (HUD_PANEL_HEALTHARMOR):\
5127                 HUD_HealthArmor(); break;\
5128         case (HUD_PANEL_NOTIFY):\
5129                 HUD_Notify(); break;\
5130         case (HUD_PANEL_TIMER):\
5131                 HUD_Timer(); break;\
5132         case (HUD_PANEL_SCORE):\
5133                 HUD_Score(); break;\
5134         case (HUD_PANEL_RACETIMER):\
5135                 HUD_RaceTimer(); break;\
5136         case (HUD_PANEL_VOTE):\
5137                 HUD_VoteWindow(); break;\
5138         case (HUD_PANEL_MODICONS):\
5139                 HUD_ModIcons(); break;\
5140         case (HUD_PANEL_PRESSEDKEYS):\
5141                 HUD_DrawPressedKeys(); break;\
5142         case (HUD_PANEL_CHAT):\
5143                 HUD_Chat(); break;\
5144         case (HUD_PANEL_ENGINEINFO):\
5145                 HUD_EngineInfo(); break;\
5146         case (HUD_PANEL_INFOMESSAGES):\
5147                  HUD_InfoMessages(); break;\
5148 } ENDS_WITH_CURLY_BRACE
5149
5150 void HUD_Main (void)
5151 {
5152         float i;
5153         // global hud alpha fade
5154         if(menu_enabled == 1)
5155                 hud_fade_alpha = 1;
5156         else
5157                 hud_fade_alpha = (1 - autocvar__menu_alpha);
5158
5159         if(scoreboard_fade_alpha)
5160                 hud_fade_alpha = (1 - scoreboard_fade_alpha);
5161
5162         if(intermission == 2) // no hud during mapvote
5163                 hud_fade_alpha = 0;
5164         else if(autocvar__menu_alpha == 0 && scoreboard_fade_alpha == 0)
5165                 hud_fade_alpha = 1;
5166
5167         if(!autocvar__hud_configure && !hud_fade_alpha)
5168                 return;
5169
5170         // Drawing stuff
5171         if (hud_skin_path != autocvar_hud_skin)
5172         {
5173                 if (hud_skin_path)
5174                         strunzone(hud_skin_path);
5175                 hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
5176         }
5177
5178         // HUD configure visible grid
5179         float hud_configure_grid_alpha;
5180         if(autocvar__hud_configure && cvar("hud_configure_grid") && (hud_configure_grid_alpha = cvar("hud_configure_grid_alpha")))
5181         {
5182                 hud_configure_gridSize_x = bound(0.005, cvar("hud_configure_grid_xsize"), 0.2);
5183                 hud_configure_gridSize_y = bound(0.005, cvar("hud_configure_grid_ysize"), 0.2);
5184                 hud_configure_realGridSize_x = hud_configure_gridSize_x * vid_conwidth;
5185                 hud_configure_realGridSize_y = hud_configure_gridSize_y * vid_conheight;
5186                 // x-axis
5187                 for(i = 0; i < 1/hud_configure_gridSize_x; ++i)
5188                         drawfill(eX * i * hud_configure_realGridSize_x, eX + eY * vid_conheight, '0.5 0.5 0.5', hud_configure_grid_alpha, DRAWFLAG_NORMAL);
5189                 // y-axis
5190                 for(i = 0; i < 1/hud_configure_gridSize_y; ++i)
5191                         drawfill(eY * i * hud_configure_realGridSize_y, eY + eX * vid_conwidth, '0.5 0.5 0.5', hud_configure_grid_alpha, DRAWFLAG_NORMAL);
5192         }
5193
5194         // draw the dock
5195         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
5196         {
5197                 float f;
5198                 vector color;
5199                 float hud_dock_color_team = autocvar_hud_dock_color_team;
5200                 if((teamplay) && hud_dock_color_team) {
5201                         f = stof(getplayerkey(player_localentnum - 1, "colors"));
5202                         color = colormapPaletteColor(mod(f, 16), 1) * hud_dock_color_team;
5203                 }
5204                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
5205                         color = '1 0 0' * hud_dock_color_team;
5206                 }
5207                 else
5208                 {
5209                         string hud_dock_color = autocvar_hud_dock_color;
5210                         if(hud_dock_color == "shirt") {
5211                                 f = stof(getplayerkey(player_localentnum - 1, "colors"));
5212                                 color = colormapPaletteColor(floor(f / 16), 0);
5213                         }
5214                         else if(hud_dock_color == "pants") {
5215                                 f = stof(getplayerkey(player_localentnum - 1, "colors"));
5216                                 color = colormapPaletteColor(mod(f, 16), 1);
5217                         }
5218                         else
5219                                 color = stov(hud_dock_color);
5220                 }
5221
5222                 string pic;
5223                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
5224                 if(precache_pic(pic) == "") {
5225                         pic = strcat(hud_skin_path, "/dock_medium");
5226                         if(precache_pic(pic) == "") {
5227                                 pic = "gfx/hud/default/dock_medium";
5228                         }
5229                 }
5230                 drawpic('0 0 0', pic, eX * vid_conwidth + eY * vid_conheight, color, autocvar_hud_dock_alpha * hud_fade_alpha, DRAWFLAG_NORMAL); // no aspect ratio forcing on dock...
5231         }
5232
5233         // cache the panel order into the panel_order array
5234         if(autocvar__hud_panelorder != hud_panelorder_prev) {
5235                 for(i = 0; i < HUD_PANEL_NUM; ++i)
5236                         panel_order[i] = -1;
5237                 string s;
5238                 float p_num, warning;
5239                 float argc = tokenize_console(autocvar__hud_panelorder);
5240                 if (argc > HUD_PANEL_NUM)
5241                         warning = true;
5242                 //first detect wrong/missing panel numbers
5243                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
5244                         p_num = stof(argv(i));
5245                         if (p_num >= 0 && p_num < HUD_PANEL_NUM) { //correct panel number?
5246                                 if (panel_order[p_num] == -1) //found for the first time?
5247                                         s = strcat(s, ftos(p_num), " ");
5248                                 panel_order[p_num] = 1; //mark as found
5249                         }
5250                         else
5251                                 warning = true;
5252                 }
5253                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
5254                         if (panel_order[i] == -1) {
5255                                 warning = true;
5256                                 s = strcat(s, ftos(i), " "); //add missing panel number
5257                         }
5258                 }
5259                 if (warning)
5260                         print("Automatically fixed wrong/missing panel numbers in _hud_panelorder\n");
5261
5262                 cvar_set("_hud_panelorder", s);
5263                 if(hud_panelorder_prev)
5264                         strunzone(hud_panelorder_prev);
5265                 hud_panelorder_prev = strzone(s);
5266
5267                 //now properly set panel_order
5268                 tokenize_console(s);
5269                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
5270                         panel_order[i] = stof(argv(i));
5271                 }
5272         }
5273         // draw panels in order specified by panel_order array
5274         for(i = HUD_PANEL_NUM - 1; i >= 0; --i) {
5275                 if(i != HUD_PANEL_CHAT || !autocvar__con_chat_maximized) // don't draw maximized chat panel twice!
5276                         HUD_DrawPanel(panel_order[i]);
5277         }
5278
5279         // draw chat panel on top if it is maximized
5280         if(autocvar__con_chat_maximized)
5281                 HUD_Chat(); // HUD_DrawPanel(HUD_PANEL_CHAT);
5282
5283         // TODO hud_'ify these
5284         if (autocvar_cl_showspeed)
5285                 HUD_ShowSpeed();
5286         if (autocvar_cl_showacceleration)
5287                 HUD_ShowAcceleration();
5288
5289         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)
5290                 localcmd("cmd selectteam auto; cmd join\n");
5291
5292         hud_configure_prev = autocvar__hud_configure;
5293
5294         if (!autocvar__hud_configure) // hud config mode disabled, enable normal alpha stuff again
5295                 if (menu_enabled)
5296                         menu_enabled = 0;
5297 }