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