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