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