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