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