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