]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
minelayer model with placeholder texture
[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;
1696         if(cvar("hud_panel_weapons_accuracy") && acc_levels)
1697         {
1698                 show_accuracy = true;
1699                 if (acc_col_x[0] == -1)
1700                         for (i = 0; i < acc_levels; ++i)
1701                                 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
1702         }
1703
1704         float weapons_st = getstati(STAT_WEAPONS);
1705         float label = cvar("hud_panel_weapons_label");
1706
1707         for(i = 0; i < weapon_cnt; ++i)
1708         {
1709                 wpnpos = panel_pos + eX * column * wpnsize_x + eY * row * wpnsize_y;
1710
1711                 self = weaponorder[i];
1712                 weapid = self.impulse;
1713
1714                 // draw background behind currently selected weapon
1715                 if(self.weapon == activeweapon)
1716                         drawpic_aspect_skin(wpnpos, "weapon_current_bg", wpnsize, '1 1 1', wpnalpha, DRAWFLAG_NORMAL);
1717
1718                 // draw the weapon accuracy
1719                 if(show_accuracy)
1720                 {
1721                         weapon_stats = weapon_accuracy[self.weapon-WEP_FIRST];
1722                         if(weapon_stats >= 0)
1723                         {
1724                                 // find the max level lower than weapon_stats
1725                                 float j;
1726                                 j = acc_levels-1;
1727                                 while ( j && weapon_stats < acc_lev[j] )
1728                                         --j;
1729
1730                                 // inject color j+1 in color j, how much depending on how much weapon_stats is higher than level j
1731                                 float factor;
1732                                 factor = (weapon_stats - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
1733                                 color = acc_col[j];
1734                                 color = color + factor * (acc_col[j+1] - color);
1735
1736                                 drawpic_aspect_skin(wpnpos, "weapon_accuracy", wpnsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
1737                         }
1738                 }
1739
1740                 // draw the weapon icon
1741                 if((weapid >= 0) && (weapons_st & self.weapons))
1742                 {
1743                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '1 1 1', wpnalpha, DRAWFLAG_NORMAL);
1744
1745                         if(label == 1) // weapon number
1746                                 drawstring(wpnpos, ftos(weapid), '1 1 0' * 0.5 * wpnsize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1747                         else if(label == 2) // bind
1748                                 drawstring(wpnpos, getcommandkey(ftos(weapid), strcat("impulse ", ftos(weapid))), '1 1 0' * 0.5 * wpnsize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1749
1750                         // draw ammo status bar
1751                         if(show_ammo && weapid != WEP_TUBA && weapid != WEP_LASER && weapid != WEP_PORTO)
1752                         {
1753                                 a = 0;
1754                                 type = GetAmmoTypeForWep(weapid);
1755                                 if(type != -1)
1756                                         a = getstati(GetAmmoStat(type)); // how much ammo do we have?
1757
1758                                 if(a > 0)
1759                                 {
1760                                         switch(type) {
1761                                                 case 0: fullammo = fullammo_shells; break;
1762                                                 case 1: fullammo = fullammo_nails; break;
1763                                                 case 2: fullammo = fullammo_rockets; break;
1764                                                 case 3: fullammo = fullammo_cells; break;
1765                                                 case 4: fullammo = fullammo_fuel; break;
1766                                                 default: fullammo = 60;
1767                                         }
1768
1769                                         drawsetcliparea(
1770                                                 wpnpos_x + baroffset_x,
1771                                                 wpnpos_y + baroffset_y,
1772                                                 barsize_x * bound(0, a/fullammo, 1),
1773                                                 barsize_y);
1774                                         drawpic_aspect_skin(wpnpos, "weapon_ammo", wpnsize, ammo_color, ammo_alpha, DRAWFLAG_NORMAL);
1775                                         drawresetcliparea();
1776                                 }
1777                         }
1778                 }
1779
1780                 // draw a "ghost weapon icon" if you don't have the weapon
1781                 else
1782                 {
1783                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '0 0 0', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
1784                 }
1785
1786                 // draw the complain message
1787                 if(time - complain_weapon_time < when + fadetime && self.weapon == complain_weapon && cvar("hud_panel_weapons_complainbubble"))
1788                 {
1789                         if(fadetime)
1790                         {
1791                                 if(complain_weapon_time + when > time)
1792                                         a = 1;
1793                                 else
1794                                         a = bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1);
1795                         }
1796                         else
1797                         {
1798                                 if(complain_weapon_time + when > time)
1799                                         a = 1;
1800                                 else
1801                                         a = 0;
1802                         }
1803
1804                         string s;
1805                         if(complain_weapon_type == 0) {
1806                                 s = "Out of ammo";
1807                                 color = stov(cvar_string("hud_panel_weapons_complainbubble_color_outofammo"));
1808                         }
1809                         else if(complain_weapon_type == 1) {
1810                                 s = "Don't have";
1811                                 color = stov(cvar_string("hud_panel_weapons_complainbubble_color_donthave"));
1812                         }
1813                         else {
1814                                 s = "Unavailable";
1815                                 color = stov(cvar_string("hud_panel_weapons_complainbubble_color_unavailable"));
1816                         }
1817                         float padding = cvar("hud_panel_weapons_complainbubble_padding");
1818                         drawpic_aspect_skin(wpnpos + '1 1 0' * padding, "weapon_complainbubble", wpnsize - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
1819                         drawstring_aspect(wpnpos + '1 1 0' * padding, s, wpnsize - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
1820                 }
1821
1822                 ++row;
1823                 if(row >= rows)
1824                 {
1825                         row = 0;
1826                         ++column;
1827                 }
1828         }
1829
1830 }
1831
1832 // Ammo (#1)
1833 //
1834 // TODO: macro
1835 float GetAmmoItemCode(float i)
1836 {
1837         switch(i)
1838         {
1839                 case 0: return IT_SHELLS;
1840                 case 1: return IT_NAILS;
1841                 case 2: return IT_ROCKETS;
1842                 case 3: return IT_CELLS;
1843                 case 4: return IT_FUEL;
1844                 default: return -1;
1845         }
1846 }
1847
1848 string GetAmmoPicture(float i)
1849 {
1850         switch(i)
1851         {
1852                 case 0: return "ammo_shells";
1853                 case 1: return "ammo_bullets";
1854                 case 2: return "ammo_rockets";
1855                 case 3: return "ammo_cells";
1856                 case 4: return "ammo_fuel";
1857                 default: return "";
1858         }
1859 }
1860
1861 void DrawAmmoItem(vector myPos, vector mySize, float itemcode, float currently_selected)
1862 {
1863         float a;
1864         if(autocvar__hud_configure)
1865         {
1866                 currently_selected = (itemcode == 2); //rockets always selected
1867                 a = 100;
1868         }
1869         else
1870                 a = getstati(GetAmmoStat(itemcode)); // how much ammo do we have of type itemcode?
1871
1872         vector color;
1873         if(a < 10)
1874                 color = '0.7 0 0';
1875         else
1876                 color = '1 1 1';
1877
1878         float alpha;
1879         if(currently_selected)
1880                 alpha = 1;
1881         else
1882                 alpha = 0.7;
1883
1884         vector picpos, numpos;
1885         if(autocvar_hud_panel_ammo_iconalign)
1886         {
1887                 numpos = myPos;
1888                 picpos = myPos + eX * 2 * mySize_y;
1889         }
1890         else
1891         {
1892                 numpos = myPos + eX * mySize_y;
1893                 picpos = myPos;
1894         }
1895
1896         if (currently_selected)
1897                 drawpic_aspect_skin(myPos, "ammo_current_bg", mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1898
1899         if(a > 0)
1900                 drawstring_aspect(numpos, ftos(a), eX * (2/3) * mySize_x + eY * mySize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1901         else // "ghost" ammo count
1902                 drawstring_aspect(numpos, ftos(a), eX * (2/3) * mySize_x + eY * mySize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1903         if(a > 0)
1904                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1905         else // "ghost" ammo icon
1906                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * mySize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1907 }
1908
1909 void HUD_Ammo(void)
1910 {
1911         if(!autocvar_hud_panel_ammo && !autocvar__hud_configure)
1912                 return;
1913
1914         active_panel = HUD_PANEL_AMMO;
1915         HUD_Panel_UpdateCvars(ammo);
1916         vector pos, mySize;
1917         pos = panel_pos;
1918         mySize = panel_size;
1919
1920         HUD_Panel_DrawBg(1);
1921         if(panel_bg_padding)
1922         {
1923                 pos += '1 1 0' * panel_bg_padding;
1924                 mySize -= '2 2 0' * panel_bg_padding;
1925         }
1926
1927         float rows, columns, row, column;
1928         vector ammo_size;
1929         if (autocvar_hud_panel_ammo_onlycurrent)
1930                 ammo_size = mySize;
1931         else
1932         {
1933                 rows = mySize_y/mySize_x;
1934                 rows = bound(1, floor((sqrt(4 * (3/1) * rows * AMMO_COUNT + rows * rows) + rows + 0.5) / 2), AMMO_COUNT);
1935                 //                               ^^^ ammo item aspect goes here
1936
1937                 columns = ceil(AMMO_COUNT/rows);
1938
1939                 ammo_size = eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows);
1940         }
1941
1942         vector offset;
1943         float newSize;
1944         if(ammo_size_x/ammo_size_y > 3)
1945         {
1946                 newSize = 3 * ammo_size_y;
1947                 offset_x = ammo_size_x - newSize;
1948                 pos_x += offset_x/2;
1949                 ammo_size_x = newSize;
1950         }
1951         else
1952         {
1953                 newSize = 1/3 * ammo_size_x;
1954                 offset_y = ammo_size_y - newSize;
1955                 pos_y += offset_y/2;
1956                 ammo_size_y = newSize;
1957         }
1958
1959         drawfont = hud_bigfont;
1960         float i, stat_items, currently_selected;
1961         if (autocvar_hud_panel_ammo_onlycurrent)
1962         {
1963                 if(autocvar__hud_configure)
1964                 {
1965                         DrawAmmoItem(pos, ammo_size, 2, true); //show rockets
1966                         return;
1967                 }
1968                 stat_items = getstati(STAT_ITEMS);
1969                 for (i = 0; i < AMMO_COUNT; ++i) {
1970                         currently_selected = stat_items & GetAmmoItemCode(i);
1971                         if (currently_selected)
1972                         {
1973                                 DrawAmmoItem(pos, ammo_size, i, true);
1974                                 return;
1975                         }
1976                 }
1977                 return; // nothing to display
1978         }
1979
1980         stat_items = getstati(STAT_ITEMS);
1981         for (i = 0; i < AMMO_COUNT; ++i) {
1982                 currently_selected = stat_items & GetAmmoItemCode(i);
1983                 DrawAmmoItem(pos + eX * column * (ammo_size_x + offset_x) + eY * row * (ammo_size_y + offset_y), ammo_size, i, currently_selected);
1984                 ++row;
1985                 if(row >= rows)
1986                 {
1987                         row = 0;
1988                         column = column + 1;
1989                 }
1990         }
1991         drawfont = hud_font;
1992 }
1993
1994 void DrawNumIcon(float iconalign, vector myPos, vector mySize, float x, string icon, float left, vector color, float alpha)
1995 {
1996         vector newPos;
1997         float newSize_x, newSize_y;
1998         if(mySize_x/mySize_y > 3)
1999         {
2000                 newSize_x = 3 * mySize_y;
2001                 newSize_y = mySize_y;
2002
2003                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
2004                 newPos_y = myPos_y;
2005         }
2006         else
2007         {
2008                 newSize_y = 1/3 * mySize_x;
2009                 newSize_x = mySize_x;
2010
2011                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
2012                 newPos_x = myPos_x;
2013         }
2014
2015         vector picpos, numpos;
2016         if(left)
2017         {
2018                 if(iconalign == 1 || iconalign == 3) // right align
2019                 {
2020                         numpos = newPos;
2021                         picpos = newPos + eX * 2 * newSize_y;
2022                 }
2023                 else // left align
2024                 {
2025                         numpos = newPos + eX * newSize_y;
2026                         picpos = newPos;
2027                 }
2028         }
2029         else
2030         {
2031                 if(iconalign == 0 || iconalign == 3) // left align
2032                 {
2033                         numpos = newPos + eX * newSize_y;
2034                         picpos = newPos;
2035                 } 
2036                 else // right align
2037                 {
2038                         numpos = newPos;
2039                         picpos = newPos + eX * 2 * newSize_y;
2040                 }
2041         }
2042
2043         drawfont = hud_bigfont;
2044         drawstring_aspect(numpos, ftos(x), eX * (2/3) * newSize_x + eY * newSize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2045         drawfont = hud_font;
2046         drawpic_aspect_skin(picpos, icon, '1 1 0' * newSize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2047 }
2048
2049 void DrawNumIcon_expanding(float iconalign, vector myPos, vector mySize, float x, string icon, float left, vector color, float fadelerp)
2050 {
2051         float sz;
2052         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
2053
2054         DrawNumIcon(iconalign, myPos + expandingbox_resize_centered_box_offset(sz, mySize, 1), mySize * sz, x, icon, left, color, (1 - fadelerp));
2055 }
2056
2057 // Powerups (#2)
2058 //
2059 void HUD_Powerups(void) {
2060         if(!autocvar_hud_panel_powerups && !autocvar__hud_configure)
2061                 return;
2062
2063         if(!autocvar__hud_configure)
2064         {
2065                 if not(getstati(STAT_ITEMS) & (IT_STRENGTH | IT_INVINCIBLE))
2066                         return;
2067
2068                 if (getstati(STAT_HEALTH) <= 0)
2069                         return;
2070         }
2071
2072         active_panel = HUD_PANEL_POWERUPS;
2073         HUD_Panel_UpdateCvars(powerups);
2074         vector pos, mySize;
2075         pos = panel_pos;
2076         mySize = panel_size;
2077
2078         float strength_time, shield_time;
2079         if(autocvar__hud_configure)
2080         {
2081                 strength_time = 15;
2082                 shield_time = 27;
2083         }
2084         else
2085         {
2086                 strength_time = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
2087                 shield_time = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
2088         }
2089
2090         HUD_Panel_DrawBg(bound(0, max(strength_time, shield_time), 1));
2091         if(panel_bg_padding)
2092         {
2093                 pos += '1 1 0' * panel_bg_padding;
2094                 mySize -= '2 2 0' * panel_bg_padding;
2095         }
2096
2097         vector barpos, barsize;
2098         vector picpos;
2099         vector numpos;
2100
2101         string leftname, rightname;
2102         float leftcnt, rightcnt;
2103         float leftexact, rightexact;
2104         float flip = cvar("hud_panel_powerups_flip");
2105         if (flip) {
2106                 leftname = "strength";
2107                 leftcnt = ceil(strength_time);
2108                 leftexact = strength_time;
2109
2110                 rightname = "shield";
2111                 rightcnt = ceil(shield_time);
2112                 rightexact = shield_time;
2113         } else {
2114                 leftname = "shield";
2115                 leftcnt = ceil(shield_time);
2116                 leftexact = shield_time;
2117
2118                 rightname = "strength";
2119                 rightcnt = ceil(strength_time);
2120                 rightexact = strength_time;
2121         }
2122
2123         drawfont = hud_bigfont;
2124         float baralign = cvar("hud_panel_powerups_baralign");
2125         float iconalign = cvar("hud_panel_powerups_iconalign");
2126         float progressbar = cvar("hud_panel_powerups_progressbar");
2127         if (mySize_x/mySize_y > 4)
2128         {
2129                 if(leftcnt)
2130                 {
2131                         if(baralign == 1 || baralign == 3) { // right align
2132                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/30);
2133                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
2134                         } else { // left align
2135                                 barpos = pos;
2136                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
2137                         }
2138
2139                         if(progressbar)
2140                         {
2141                                 HUD_Panel_GetProgressBarColorForString(leftname);
2142                                 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);
2143                         }
2144                         if(leftcnt > 1)
2145                                 DrawNumIcon(iconalign, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, '1 1 1', 1);
2146                         if(leftcnt <= 5)
2147                                 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));
2148                 }
2149
2150                 if(rightcnt)
2151                 {
2152                         if(baralign == 0 || baralign == 3) { // left align
2153                                 barpos = pos + eX * 0.5 * mySize_x;
2154                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
2155                         } else { // right align
2156                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/30);
2157                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
2158                         }
2159
2160                         if(progressbar)
2161                         {
2162                                 HUD_Panel_GetProgressBarColorForString(rightname);
2163                                 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);
2164                         }
2165                         if(rightcnt > 1)
2166                                 DrawNumIcon(iconalign, pos + eX * 0.5 * mySize_x, eX * 0.5 * mySize_x + eY * mySize_y, rightcnt, rightname, 0, '1 1 1', 1);
2167                         if(rightcnt <= 5)
2168                                 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));
2169                 }
2170         }
2171         else if (mySize_x/mySize_y > 1.5)
2172         {
2173                 if(leftcnt)
2174                 {
2175                         if(baralign == 1 || baralign == 3) { // right align
2176                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/30);
2177                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
2178                         } else { // left align
2179                                 barpos = pos;
2180                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
2181                         }
2182
2183                         if(progressbar)
2184                         {
2185                                 HUD_Panel_GetProgressBarColorForString(leftname);
2186                                 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);
2187                         }
2188                         if(leftcnt > 1)
2189                                 DrawNumIcon(iconalign, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, '1 1 1', 1);
2190                         if(leftcnt <= 5)
2191                                 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));
2192                 }
2193
2194                 if(rightcnt)
2195                 {
2196                         if(baralign == 0 || baralign == 3) { // left align
2197                                 barpos = pos + eY * 0.5 * mySize_y;
2198                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
2199                         } else { // right align
2200                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
2201                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
2202                         }
2203
2204                         if(progressbar)
2205                         {
2206                                 HUD_Panel_GetProgressBarColorForString(rightname);
2207                                 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);
2208                         }
2209                         if(rightcnt > 1)
2210                                 DrawNumIcon(iconalign, pos + eY * 0.5 * mySize_y, eX * mySize_x + eY * 0.5 * mySize_y, rightcnt, rightname, 0, '1 1 1', 1);
2211                         if(rightcnt <= 5)
2212                                 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));
2213                 }
2214         }
2215         else
2216         {
2217                 if(leftcnt)
2218                 {
2219                         if(baralign == 1 || baralign == 3) { // down align
2220                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/30);
2221                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
2222                         } else { // up align
2223                                 barpos = pos;
2224                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
2225                         }
2226
2227                         if(iconalign == 1 || iconalign == 3) { // down align
2228                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
2229                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
2230                         } else { // up align
2231                                 picpos = pos + eX * 0.05 * mySize_x;
2232                                 numpos = pos + eY * 0.4 * mySize_x;
2233                         }
2234
2235                         if(progressbar)
2236                         {
2237                                 HUD_Panel_GetProgressBarColorForString(leftname);
2238                                 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);
2239                         }
2240                         if(leftcnt <= 5)
2241                                 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));
2242                         if(leftcnt > 1)
2243                                 drawpic_aspect_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2244                         drawstring_aspect(numpos, ftos(leftcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2245                 }
2246
2247                 if(rightcnt)
2248                 {
2249                         if(baralign == 0 || baralign == 3) { // up align
2250                                 barpos = pos + eX * 0.5 * mySize_x;
2251                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
2252                         } else { // down align
2253                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/30) + eX * 0.5 * mySize_x;
2254                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
2255                         }
2256
2257                         if(iconalign == 0 || iconalign == 3) { // up align
2258                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2259                                 numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2260                         } else { // down align
2261                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
2262                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
2263                         }
2264
2265                         if(progressbar)
2266                         {
2267                                 HUD_Panel_GetProgressBarColorForString(rightname);
2268                                 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);
2269                         }
2270                         if(rightcnt <= 5)
2271                                 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));
2272                         if(rightcnt > 1)
2273                                 drawpic_aspect_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2274                         drawstring_aspect(numpos, ftos(rightcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2275                 }
2276         }
2277         drawfont = hud_font;
2278 }
2279
2280 // Health/armor (#3)
2281 //
2282 void HUD_HealthArmor(void)
2283 {
2284         if(!autocvar_hud_panel_healtharmor && !autocvar__hud_configure)
2285                 return;
2286
2287         active_panel = HUD_PANEL_HEALTHARMOR;
2288         HUD_Panel_UpdateCvars(healtharmor);
2289         vector pos, mySize;
2290         pos = panel_pos;
2291         mySize = panel_size;
2292
2293         HUD_Panel_DrawBg(1);
2294         if(panel_bg_padding)
2295         {
2296                 pos += '1 1 0' * panel_bg_padding;
2297                 mySize -= '2 2 0' * panel_bg_padding;
2298         }
2299
2300         float armor, health, fuel;
2301         armor = getstati(STAT_ARMOR);
2302         health = getstati(STAT_HEALTH);
2303         fuel = getstati(STAT_FUEL);
2304
2305         if(autocvar__hud_configure)
2306         {
2307                 armor = 150;
2308                 health = 100;
2309                 fuel = 70;
2310         }
2311
2312         if(health <= 0)
2313                 return;
2314
2315         vector barpos, barsize;
2316         vector picpos;
2317         vector numpos;
2318
2319         drawfont = hud_bigfont;
2320         float baralign = cvar("hud_panel_healtharmor_baralign");
2321         float iconalign = cvar("hud_panel_healtharmor_iconalign");
2322         float progressbar = cvar("hud_panel_healtharmor_progressbar");
2323         if(autocvar_hud_panel_healtharmor == 2) // combined health and armor display
2324         {
2325                 vector v;
2326                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
2327
2328                 float x;
2329                 x = floor(v_x + 1);
2330
2331                 if(baralign == 1 || baralign == 3) { // right align
2332                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, x/400);
2333                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
2334                 } else { // left align
2335                         barpos = pos;
2336                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
2337                 }
2338
2339                 string biggercount;
2340                 if(v_z) // NOT fully armored
2341                 {
2342                         biggercount = "health";
2343                         if(progressbar)
2344                         {
2345                                 HUD_Panel_GetProgressBarColor(health);
2346                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2347                         }
2348                         if(armor)
2349                                 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);
2350                 }
2351                 else
2352                 {
2353                         biggercount = "armor";
2354                         if(progressbar)
2355                         {
2356                                 HUD_Panel_GetProgressBarColor(armor);
2357                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2358                         }
2359                         if(health)
2360                                 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);
2361                 }
2362                 DrawNumIcon(iconalign, pos, mySize, x, biggercount, 1, HUD_Get_Num_Color(x, 2 * 200), 1);
2363
2364                 // fuel
2365                 if(fuel)
2366                 {
2367                         if(baralign == 0 || baralign == 3) { // left align
2368                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2369                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2370                         } else {
2371                                 barpos = pos;
2372                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2373                         }
2374                         HUD_Panel_GetProgressBarColor(fuel);
2375                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2376                 }
2377         }
2378         else
2379         {
2380                 string leftname, rightname;
2381                 float leftcnt, rightcnt;
2382                 float leftactive, rightactive;
2383                 float leftalpha, rightalpha;
2384                 float flip = cvar("hud_panel_healtharmor_flip");
2385                 if (flip) { // old style layout with armor left/top of health
2386                         leftname = "armor";
2387                         leftcnt = armor;
2388                         if(leftcnt)
2389                                 leftactive = 1;
2390                         leftalpha = min((armor+10)/55, 1);
2391
2392                         rightname = "health";
2393                         rightcnt = health;
2394                         rightactive = 1;
2395                         rightalpha = 1;
2396                 } else {
2397                         leftname = "health";
2398                         leftcnt = health;
2399                         leftactive = 1;
2400                         leftalpha = 1;
2401
2402                         rightname = "armor";
2403                         rightcnt = armor;
2404                         if(rightcnt)
2405                                 rightactive = 1;
2406                         rightalpha = min((armor+10)/55, 1);
2407                 }
2408
2409                 if (mySize_x/mySize_y > 4)
2410                 {
2411                         if(leftactive)
2412                         {
2413                                 if(baralign == 1 || baralign == 3) { // right align
2414                                         barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/200);
2415                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
2416                                 } else { // left align
2417                                         barpos = pos;
2418                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
2419                                 }
2420
2421                                 if(progressbar)
2422                                 {
2423                                         HUD_Panel_GetProgressBarColorForString(leftname);
2424                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2425                                 }
2426                                 DrawNumIcon(iconalign, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, HUD_Get_Num_Color(leftcnt, 200), 1);
2427                         }
2428
2429                         if(rightactive)
2430                         {
2431                                 if(baralign == 0 || baralign == 3) { // left align
2432                                         barpos = pos + eX * 0.5 * mySize_x;
2433                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
2434                                 } else { // right align
2435                                         barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/200);
2436                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
2437                                 }
2438
2439                                 if(progressbar)
2440                                 {
2441                                         HUD_Panel_GetProgressBarColorForString(rightname);
2442                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2443                                 }
2444                                 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);
2445                         }
2446
2447                         if(fuel)
2448                         {
2449                                 if(baralign == 0 || baralign == 3) { // left align
2450                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2451                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2452                                 } else {
2453                                         barpos = pos;
2454                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2455                                 }
2456                                 HUD_Panel_GetProgressBarColor(fuel);
2457                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2458                         }
2459                 }
2460                 else if (mySize_x/mySize_y > 1.5)
2461                 {
2462                         if(leftactive)
2463                         {
2464                                 if(baralign == 1 || baralign == 3) { // right align
2465                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/200);
2466                                         barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
2467                                 } else { // left align
2468                                         barpos = pos;
2469                                         barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
2470                                 }
2471
2472                                 if(progressbar)
2473                                 {
2474                                         HUD_Panel_GetProgressBarColorForString(leftname);
2475                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2476                                 }
2477                                 DrawNumIcon(iconalign, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, HUD_Get_Num_Color(leftcnt, 200), 1);
2478                         }
2479
2480                         if(rightactive)
2481                         {
2482                                 if(baralign == 0 || baralign == 3) { // left align
2483                                         barpos = pos + eY * 0.5 * mySize_y;
2484                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2485                                 } else { // right align
2486                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2487                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2488                                 }
2489
2490                                 if(progressbar)
2491                                 {
2492                                         HUD_Panel_GetProgressBarColorForString(rightname);
2493                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2494                                 }
2495                                 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);
2496                         }
2497
2498                         if(fuel)
2499                         {
2500                                 if(baralign == 0 || baralign == 3) { // left align
2501                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2502                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2503                                 } else {
2504                                         barpos = pos;
2505                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2506                                 }
2507                                 HUD_Panel_GetProgressBarColor(fuel);
2508                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2509                         }
2510                 }
2511                 else
2512                 {
2513                         if(leftactive)
2514                         {
2515                                 if(baralign == 1 || baralign == 3) { // down align
2516                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/200);
2517                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2518                                 } else { // up align
2519                                         barpos = pos;
2520                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2521                                 }
2522
2523                                 if(iconalign == 1 || iconalign == 3) { // down align
2524                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
2525                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
2526                                 } else { // up align
2527                                         picpos = pos + eX * 0.05 * mySize_x;
2528                                         numpos = pos + eY * 0.4 * mySize_x;
2529                                 }
2530
2531                                 if(progressbar)
2532                                 {
2533                                         HUD_Panel_GetProgressBarColorForString(leftname);
2534                                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2535                                 }
2536                                 drawpic_aspect_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2537                                 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);
2538                         }
2539
2540                         if(rightactive)
2541                         {
2542                                 if(baralign == 0 || baralign == 3) { // up align
2543                                         barpos = pos + eX * 0.5 * mySize_x;
2544                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2545                                 } else { // down align
2546                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/200) + eX * 0.5 * mySize_x;
2547                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2548                                 }
2549
2550                                 if(iconalign == 0 || iconalign == 3) { // up align
2551                                         picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2552                                         numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2553                                 } else { // down align
2554                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
2555                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
2556                                 }
2557
2558                                 if(progressbar)
2559                                 {
2560                                         HUD_Panel_GetProgressBarColorForString(rightname);
2561                                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2562                                 }
2563                                 drawpic_aspect_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2564                                 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);
2565                         }
2566
2567                         if(fuel)
2568                         {
2569                                 if(baralign == 0 || baralign == 3) { // left align
2570                                         barpos = pos;
2571                                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2572                                 } else {
2573                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, fuel/100);
2574                                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2575                                 }
2576                                 HUD_Panel_GetProgressBarColor(fuel);
2577                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2578                         }
2579                 }
2580         }
2581         drawfont = hud_font;
2582 }
2583
2584 // Notification area (#4)
2585 //
2586
2587 string Weapon_SuicideMessage(float deathtype)
2588 {
2589         w_deathtype = deathtype;
2590         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_SUICIDEMESSAGE);
2591         return w_deathtypestring;
2592 }
2593
2594 string Weapon_KillMessage(float deathtype)
2595 {
2596         w_deathtype = deathtype;
2597         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_KILLMESSAGE);
2598         return w_deathtypestring;
2599 }
2600
2601 #define KN_MAX_ENTRIES 10
2602 float kn_index;
2603 float killnotify_times[KN_MAX_ENTRIES];
2604 float killnotify_deathtype[KN_MAX_ENTRIES];
2605 float killnotify_actiontype[KN_MAX_ENTRIES]; // 0 = "Y [used by] X", 1 = "X [did action to] Y"
2606 string killnotify_attackers[KN_MAX_ENTRIES];
2607 string killnotify_victims[KN_MAX_ENTRIES];
2608 void HUD_KillNotify_Push(string attacker, string victim, float actiontype, float wpn)
2609 {
2610         --kn_index;
2611         if (kn_index == -1)
2612                 kn_index = KN_MAX_ENTRIES-1;
2613         killnotify_times[kn_index] = time;
2614         killnotify_deathtype[kn_index] = wpn;
2615         killnotify_actiontype[kn_index] = actiontype;
2616         if(killnotify_attackers[kn_index])
2617                 strunzone(killnotify_attackers[kn_index]);
2618         killnotify_attackers[kn_index] = strzone(attacker);
2619         if(killnotify_victims[kn_index])
2620                 strunzone(killnotify_victims[kn_index]);
2621         killnotify_victims[kn_index] = strzone(victim);
2622 }
2623
2624 void HUD_KillNotify(string s1, string s2, string s3, float type, float msg) // s1 = attacker, s2 = victim
2625 {
2626         float w;
2627         float alsoprint, gentle;
2628         alsoprint = (cvar("hud_panel_notify_print") || !panel_enabled); // print message to console if: notify panel disabled, or cvar to do so enabled
2629         gentle = (cvar("cl_gentle") || cvar("cl_gentle_messages"));
2630         
2631         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
2632                 return;
2633
2634         if(msg == MSG_SUICIDE) {
2635                 w = DEATH_WEAPONOF(type);
2636                 if(WEP_VALID(w)) {
2637                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2638                         if (alsoprint)
2639                                 print("^1", sprintf(Weapon_SuicideMessage(type), strcat(s1, "^1")), "\n");
2640                 } else if (type == DEATH_KILL) {
2641                         HUD_KillNotify_Push(s1, "", 0, DEATH_KILL);
2642                         if (alsoprint)
2643                                 print ("^1",s1, "^1 couldn't take it anymore\n");
2644                 } else if (type == DEATH_ROT) {
2645                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2646                         if (alsoprint)
2647                                 print ("^1",s1, "^1 died\n");
2648                 } else if (type == DEATH_NOAMMO) {
2649                         HUD_KillNotify_Push(s1, "", 0, DEATH_NOAMMO);
2650                         if (alsoprint)
2651                                 print ("^7",s1, "^7 committed suicide. What's the point of living without ammo?\n");
2652                 } else if (type == DEATH_CAMP) {
2653                         HUD_KillNotify_Push(s1, "", 0, DEATH_CAMP);
2654                         if (alsoprint)
2655                                 print ("^1",s1, "^1 thought they found a nice camping ground\n");
2656                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2657                         HUD_KillNotify_Push(s1, "", 0, type);
2658                         if (alsoprint)
2659                                 print ("^1",s1, "^1 didn't become friends with the Lord of Teamplay\n");
2660                 } else if (type == DEATH_CHEAT) {
2661                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2662                         if (alsoprint)
2663                                 print ("^1",s1, "^1 unfairly eliminated themself\n");
2664                 } else if (type == DEATH_FIRE) {
2665                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2666                         if (alsoprint)
2667                                 print ("^1",s1, "^1 burned to death\n");
2668                 } else if (type != DEATH_TEAMCHANGE && type != DEATH_QUIET) {
2669                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2670                         if (alsoprint)
2671                                 print ("^1",s1, "^1 couldn't resist the urge to self-destruct\n");
2672                 } 
2673                 
2674                 if (stof(s2) > 2) // killcount > 2
2675                         print ("^1",s1,"^1 ended it all after a ",s2," kill spree\n");
2676         } else if(msg == MSG_KILL) {
2677                 w = DEATH_WEAPONOF(type);
2678                 if(WEP_VALID(w)) {
2679                         if((w == WEP_CAMPINGRIFLE || w == WEP_MINSTANEX) && type & HITTYPE_HEADSHOT) // all headshot weapons go here
2680                                 HUD_KillNotify_Push(s1, s2, 1, DEATH_HEADSHOT);
2681                         else
2682                                 HUD_KillNotify_Push(s1, s2, 1, type);
2683
2684                         if (alsoprint)
2685                                 print("^1", sprintf(Weapon_KillMessage(type), strcat(s2, "^1"), strcat(s1, "^1")), "\n"); // default order: victim, killer
2686                 }
2687                 else if(type == KILL_TEAM_RED || type == KILL_TEAM_BLUE || type == KILL_TEAM_SPREE) {
2688                         HUD_KillNotify_Push(s1, s2, 1, type);
2689                         if(alsoprint)
2690                         {
2691                                 if(gentle) {
2692                                         print ("^1", s1, "^1 took action against a team mate\n");
2693                                 } else {
2694                                         print ("^1", s1, "^1 mows down a team mate\n");
2695                                 }
2696                         }
2697                         if (stof(s2) > 2 && type == KILL_TEAM_SPREE) {
2698                                 if(gentle)
2699                                         print ("^1",s1,"^1 ended a ",s3," scoring spree by going against a team mate\n");
2700                                 else
2701                                         print ("^1",s1,"^1 ended a ",s3," kill spree by killing a team mate\n");
2702                         }
2703                         else if (stof(s2) > 2) {
2704                                 if(gentle)
2705                                         print ("^1",s1,"'s ^1",s3," scoring spree was ended by a team mate!\n");
2706                                 else
2707                                         print ("^1",s1,"'s ^1",s3," kill spree was ended by a team mate!\n");
2708                         }
2709                 }
2710                 else if(type == KILL_FIRST_BLOOD)
2711                         print("^1",s1, "^1 drew first blood", "\n");
2712                 else if (type == DEATH_TELEFRAG) {
2713                         HUD_KillNotify_Push(s1, s2, 1, DEATH_TELEFRAG);
2714                         if(gentle)
2715                                 print ("^1",s2, "^1 tried to occupy ", s1, "^1's teleport destination space\n");
2716                         else
2717                                 print ("^1",s2, "^1 was telefragged by ", s1, "\n");
2718                 }
2719                 else if (type == DEATH_DROWN) {
2720                         HUD_KillNotify_Push(s1, s2, 1, DEATH_DROWN);
2721                         if(alsoprint)
2722                                 print ("^1",s2, "^1 was drowned by ", s1, "\n");
2723                 }
2724                 else if (type == DEATH_SLIME) {
2725                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SLIME);
2726                         if(alsoprint)
2727                                 print ("^1",s2, "^1 was slimed by ", s1, "\n");
2728                 }
2729                 else if (type == DEATH_LAVA) {
2730                         HUD_KillNotify_Push(s1, s2, 1, DEATH_LAVA);
2731                         if(alsoprint)
2732                                 print ("^1",s2, "^1 was cooked by ", s1, "\n");
2733                 }
2734                 else if (type == DEATH_FALL) {
2735                         HUD_KillNotify_Push(s1, s2, 1, DEATH_FALL);
2736                         if(alsoprint)
2737                                 print ("^1",s2, "^1 was grounded by ", s1, "\n");
2738                 }
2739                 else if (type == DEATH_SHOOTING_STAR) {
2740                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SHOOTING_STAR);
2741                         if(alsoprint)
2742                                 print ("^1",s2, "^1 was shot into space by ", s1, "\n");
2743                 }
2744                 else if (type == DEATH_SWAMP) {
2745                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2746                         if(alsoprint)
2747                                 print ("^1",s2, "^1 was conserved by ", s1, "\n");
2748                 }
2749                 else if (type == DEATH_HURTTRIGGER)
2750                 {
2751                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2752                         if(alsoprint)
2753                                 print("^1",s2, "^1 was thrown into a world of hurt by ", s1, "\n");
2754                 } else if(type == DEATH_SBCRUSH) {
2755                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2756                         if(alsoprint)
2757                                 print ("^1",s2, "^1 was crushed by ^1", s1, "\n");
2758                 } else if(type == DEATH_SBMINIGUN) {
2759                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2760                         if(alsoprint)
2761                                 print ("^1",s2, "^1 got shredded by ^1", s1, "\n");
2762                 } else if(type == DEATH_SBROCKET) {
2763                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2764                         if(alsoprint)
2765                                 print ("^1",s2, "^1 was blased to bits by ^1", s1, "\n");
2766                 } else if(type == DEATH_SBBLOWUP) {
2767                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2768                         if(alsoprint)
2769                                 print ("^1",s2, "^1 got caught in the destruction of ^1", s1, "'s vehicle\n");
2770                 } else if(type == DEATH_WAKIGUN) {
2771                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2772                         if(alsoprint)
2773                                 print ("^1",s2, "^1 was bolted down by ^1", s1, "\n");
2774                 } else if(type == DEATH_WAKIROCKET) {
2775                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2776                         if(alsoprint)
2777                                 print ("^1",s2, "^1 could find no shelter from ^1", s1, "'s rockets\n");
2778                 } else if(type == DEATH_WAKIBLOWUP) {
2779                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2780                         if(alsoprint)
2781                                 print ("^1",s2, "^1 dies when ^1", s1, "'s wakizashi dies.\n");
2782                 } else if(type == DEATH_TURRET) {
2783                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2784                         if(alsoprint)
2785                                 print ("^1",s2, "^1 was pushed into the line of fire by ^1", s1, "\n");
2786                 } else if(type == DEATH_TOUCHEXPLODE) {
2787                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2788                         if(alsoprint)
2789                                 print ("^1",s2, "^1 was pushed into an accident by ^1", s1, "\n");
2790                 } else if(type == DEATH_CHEAT) {
2791                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2792                         if(alsoprint)
2793                                 print ("^1",s2, "^1 was unfairly eliminated by ^1", s1, "\n");
2794                 } else if (type == DEATH_FIRE) {
2795                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2796                         if(alsoprint)
2797                                 print ("^1",s2, "^1 was burnt to death by ^1", s1, "\n");
2798                 } else if (type == DEATH_CUSTOM) {
2799                         HUD_KillNotify_Push(s1, s2, 1, DEATH_CUSTOM);
2800                         if(alsoprint)
2801                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2802                 } else if (type == DEATH_HURTTRIGGER) {
2803                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2804                         if(alsoprint)
2805                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2806                 } else {
2807                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2808                         if(alsoprint)
2809                                 print ("^1",s2, "^1 was fragged by ", s1, "\n");
2810                 }
2811         } else if(msg == MSG_SPREE) {
2812                 if(type == KILL_END_SPREE) {
2813                         if(gentle)
2814                                 print ("^1",s1,"'s ^1", s2, " scoring spree was ended by ", s3, "\n");
2815                         else
2816                                 print ("^1",s1,"'s ^1", s2, " kill spree was ended by ", s3, "\n");
2817                 } else if(type == KILL_SPREE) {
2818                         if(gentle)
2819                                 print ("^1",s1,"^1 made ",s2," scores in a row\n");
2820                         else
2821                                 print ("^1",s1,"^1 has ",s2," frags in a row\n");
2822                 } else if(type == KILL_SPREE_3) {
2823                         if(gentle)
2824                                 print (s1,"^7 made a ^1TRIPLE SCORE\n");
2825                         else
2826                                 print (s1,"^7 made a ^1TRIPLE FRAG\n");
2827                 } else if(type == KILL_SPREE_5) {
2828                         if(gentle)
2829                                 print (s1,"^7 unleashes ^1SCORING RAGE\n");
2830                         else
2831                                 print (s1,"^7 unleashes ^1RAGE\n");
2832                 } else if(type == KILL_SPREE_10) {
2833                         if(gentle)
2834                                 print (s1,"^7 made ^1TEN SCORES IN A ROW!\n");
2835                         else
2836                                 print (s1,"^7 starts the ^1MASSACRE!\n");
2837                 } else if(type == KILL_SPREE_15) {
2838                         if(gentle)
2839                                 print (s1,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
2840                         else
2841                                 print (s1,"^7 executes ^1MAYHEM!\n");
2842                 } else if(type == KILL_SPREE_20) {
2843                         if(gentle)
2844                                 print (s1,"^7 made ^1TWENTY SCORES IN A ROW!\n");
2845                         else
2846                                 print (s1,"^7 is a ^1BERSERKER!\n");
2847                 } else if(type == KILL_SPREE_25) {
2848                         if(gentle)
2849                                 print (s1,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
2850                         else
2851                                 print (s1,"^7 inflicts ^1CARNAGE!\n");
2852                 } else if(type == KILL_SPREE_30) {
2853                         if(gentle)
2854                                 print (s1,"^7 made ^1THIRTY SCORES IN A ROW!\n");
2855                         else
2856                                 print (s1,"^7 unleashes ^1ARMAGEDDON!\n");
2857                 }
2858         } else if(msg == MSG_KILL_ACTION) { // wtf is this? isnt it basically the same as MSG_SUICIDE?
2859                 if (type == DEATH_DROWN) {
2860                         HUD_KillNotify_Push(s1, "", 0, DEATH_DROWN);
2861                         if(alsoprint)
2862                         {
2863                                 if(gentle)
2864                                         print ("^1",s1, "^1 was in the water for too long\n");
2865                                 else
2866                                         print ("^1",s1, "^1 drowned\n");
2867                         }
2868                 } else if (type == DEATH_SLIME) {
2869                         HUD_KillNotify_Push(s1, "", 0, DEATH_SLIME);
2870                         if(alsoprint)
2871                                 print ("^1",s1, "^1 was slimed\n");
2872                 } else if (type == DEATH_LAVA) {
2873                         HUD_KillNotify_Push(s1, "", 0, DEATH_LAVA);
2874                         if(alsoprint)
2875                         {
2876                                 if(gentle)
2877                                         print ("^1",s1, "^1 found a hot place\n");
2878                                 else
2879                                         print ("^1",s1, "^1 turned into hot slag\n");
2880                         }
2881                 } else if (type == DEATH_FALL) {
2882                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2883                         if(alsoprint)
2884                         {
2885                                 if(gentle)
2886                                         print ("^1",s1, "^1 tested gravity (and it worked)\n");
2887                                 else
2888                                         print ("^1",s1, "^1 hit the ground with a crunch\n");
2889                         }
2890                 } else if (type == DEATH_SHOOTING_STAR) {
2891                         HUD_KillNotify_Push(s1, "", 0, DEATH_SHOOTING_STAR);
2892                         if(alsoprint)
2893                                 print ("^1",s1, "^1 became a shooting star\n");
2894                 } else if (type == DEATH_SWAMP) {
2895                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2896                         if(alsoprint)
2897                         {
2898                                 if(gentle)
2899                                         print ("^1",s1, "^1 discovered a swamp\n");
2900                                 else
2901                                         print ("^1",s1, "^1 is now conserved for centuries to come\n");
2902                         }
2903                 } else if(type == DEATH_TURRET) {
2904                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2905                         if(alsoprint)
2906                                 print ("^1",s1, "^1 was mowed down by a turret \n");
2907                 } else if (type == DEATH_CUSTOM) {
2908                         HUD_KillNotify_Push(s1, "", 0, DEATH_CUSTOM);
2909                         if(alsoprint)
2910                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
2911                 } else if (type == DEATH_HURTTRIGGER) {
2912                         HUD_KillNotify_Push(s1, "", 0, DEATH_HURTTRIGGER);
2913                         if(alsoprint)
2914                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
2915                 } else if(type == DEATH_TOUCHEXPLODE) {
2916                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2917                         if(alsoprint)
2918                                 print ("^1",s1, "^1 died in an accident\n");
2919                 } else if(type == DEATH_CHEAT) {
2920                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2921                         if(alsoprint)
2922                                 print ("^1",s1, "^1 was unfairly eliminated\n");
2923                 } else if(type == DEATH_FIRE) {
2924                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2925                         if(alsoprint)
2926                         {
2927                                 if(gentle)
2928                                         print ("^1",s1, "^1 felt a little hot\n");
2929                                 else
2930                                         print ("^1",s1, "^1 burnt to death\n");
2931                                 }
2932                 } else {
2933                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2934                         if(alsoprint)
2935                         {
2936                                 if(gentle)
2937                                         print ("^1",s1, "^1 needs a restart\n");
2938                                 else
2939                                         print ("^1",s1, "^1 died\n");
2940                         }
2941                 }
2942         } else if(msg == MSG_KILL_ACTION_SPREE) {
2943                 if(gentle)
2944                         print ("^1",s1,"^1 needs a restart after a ",s2," scoring spree\n");
2945                 else
2946                         print ("^1",s1,"^1 died with a ",s2," kill spree\n");
2947         } else if(msg == MSG_INFO) {
2948                 if(type == INFO_GOTFLAG) { // here, s2 is the flag name
2949                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2950                         print(s1, "^7 got the ", s2, "\n");
2951                 } else if(type == INFO_LOSTFLAG) {
2952                         HUD_KillNotify_Push(s1, s2, 0, INFO_LOSTFLAG);
2953                         print(s1, "^7 lost the ", s2, "\n");
2954                 } else if(type == INFO_PICKUPFLAG) {
2955                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2956                         print(s1, "^7 picked up the ", s2, "\n");
2957                 } else if(type == INFO_RETURNFLAG) {
2958                         HUD_KillNotify_Push(s1, s2, 0, INFO_RETURNFLAG);
2959                         print(s1, "^7 returned the ", s2, "\n");
2960                 } else if(type == INFO_CAPTUREFLAG) {
2961                         HUD_KillNotify_Push(s1, s2, 0, INFO_CAPTUREFLAG);
2962                         print(s1, "^7 captured the ", s2, s3, "\n");
2963                 }
2964         } else if(msg == MSG_RACE) {
2965                 if(type == RACE_SERVER_RECORD) {
2966                         HUD_KillNotify_Push(s1, s2, 1, RACE_SERVER_RECORD);
2967                 }
2968                 else if(type == RACE_NEW_RANK) {
2969                         HUD_KillNotify_Push(s1, s2, 1, RACE_NEW_RANK);
2970                 }
2971                 else if(type == RACE_NEW_TIME) {
2972                         HUD_KillNotify_Push(s1, s2, 1, RACE_NEW_TIME);
2973                 }
2974                 else if(type == RACE_FAIL) {
2975                         HUD_KillNotify_Push(s1, s2, 1, RACE_FAIL);
2976                 }
2977         }
2978 }
2979
2980 #define DAMAGE_CENTERPRINT_SPACER NEWLINES
2981
2982 void HUD_Centerprint(string s1, string s2, float type, float msg)
2983 {
2984         float gentle;
2985         gentle = (cvar("cl_gentle") || cvar("cl_gentle_messages"));
2986         if(msg == MSG_SUICIDE) {
2987                 if (type == DEATH_TEAMCHANGE) {
2988                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You are now on: ", s1));
2989                 } else if (type == DEATH_AUTOTEAMCHANGE) {
2990                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You have been moved into a different team to improve team balance\nYou are now on: ", s1));
2991                 } else if (type == DEATH_CAMP) {
2992                         if(gentle)
2993                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Reconsider your tactics, camper!"));
2994                         else
2995                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Die camper!"));
2996                 } else if (type == DEATH_NOAMMO) {
2997                         if(gentle)
2998                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You are reinserted into the game for running out of ammo..."));
2999                         else
3000                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were killed for running out of ammo..."));
3001                 } else if (type == DEATH_ROT) {
3002                         if(gentle)
3003                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to preserve your health"));
3004                         else
3005                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You grew too old without taking your medicine"));
3006                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
3007                         if(gentle)
3008                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't go against team mates!"));
3009                         else
3010                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't shoot your team mates!"));
3011                 } else if (type == DEATH_QUIET) {
3012                         // do nothing
3013                 } else { // generic message
3014                         if(gentle)
3015                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to be more careful!"));
3016                         else
3017                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You killed your own dumb self!"));
3018                 }
3019         } else if(msg == MSG_KILL) {
3020                 if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
3021                         if(gentle) {
3022                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You went against", s1, ",a team mate!"));
3023                         } else {
3024                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You fragged ", s1, ", a team mate!"));
3025                         }
3026                 } else if (type == KILL_FIRST_BLOOD) {
3027                         if(gentle) {
3028                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First score"));
3029                         } else {
3030                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First blood"));
3031                         }
3032                 } else if (type == KILL_FIRST_VICTIM) {
3033                         if(gentle) {
3034                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First casualty"));
3035                         } else {
3036                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First victim"));
3037                         }
3038                 } else if (type == KILL_TYPEFRAG) { // s2 contains "advanced kill messages" such as ping, handicap...
3039                         if(gentle) {
3040                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You scored against ^7", s1, "^1 who was typing!", s2));
3041                         } else {
3042                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You typefragged ^7", s1, s2));
3043                         }
3044                 } else if (type == KILL_TYPEFRAGGED) {
3045                         if(gentle) {
3046                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, "^1 while you were typing!", s2));
3047                         } else {
3048                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were typefragged by ^7", s1, s2));
3049                         }
3050                 } else if (type == KILL_FRAG) {
3051                         if(gentle) {
3052                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You scored against ^7", s1, s2));
3053                         } else {
3054                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You fragged ^7", s1, s2));
3055                         }
3056                 } else { // generic message
3057                         if(gentle) {
3058                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, s2));
3059                         } else {
3060                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were fragged by ^7", s1, s2));
3061                         }
3062                 }
3063         } else if(msg == MSG_KILL_ACTION) {
3064                 // TODO: invent more centerprints here?
3065                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
3066         }
3067 }
3068
3069 void HUD_Notify (void)
3070 {
3071         if(!autocvar_hud_panel_notify && !autocvar__hud_configure)
3072                 return;
3073
3074         active_panel = HUD_PANEL_NOTIFY;
3075         HUD_Panel_UpdateCvars(notify);
3076         vector pos, mySize;
3077         pos = panel_pos;
3078         mySize = panel_size;
3079
3080         HUD_Panel_DrawBg(1);
3081         if(panel_bg_padding)
3082         {
3083                 pos += '1 1 0' * panel_bg_padding;
3084                 mySize -= '2 2 0' * panel_bg_padding;
3085         }
3086
3087         float entries, height;
3088         entries = bound(1, floor(KN_MAX_ENTRIES * mySize_y/mySize_x), KN_MAX_ENTRIES);
3089         height = mySize_y/entries;
3090         
3091         vector fontsize;
3092         fontsize = '0.5 0.5 0' * height;
3093
3094         float a;
3095         float when;
3096         when = cvar("hud_panel_notify_time");
3097         float fadetime;
3098         fadetime = cvar("hud_panel_notify_fadetime");
3099
3100         string s;
3101
3102         vector pos_attacker, pos_victim;
3103         vector weap_pos;
3104         float width_attacker;
3105         string attacker, victim;
3106
3107         float i, j, w, step, limit;
3108         if(cvar("hud_panel_notify_flip")) //order items from the top down
3109         {
3110                 i = 0;
3111                 step = +1;
3112                 limit = entries;
3113         }
3114         else //order items from the bottom up
3115         {
3116                 i = entries - 1;
3117                 step = -1;
3118                 limit = -1;
3119         }
3120
3121         for(j = kn_index;  i != limit;  i += step, ++j)
3122         {
3123                 if(autocvar__hud_configure)
3124                 {
3125                         if (step == +1)
3126                                 a = i;
3127                         else // inverse order
3128                                 a = entries - 1 - i;
3129                         attacker = textShortenToWidth(strcat("Player", ftos(a+1)), 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3130                         victim = textShortenToWidth(strcat("Player", ftos(a+2)), 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3131                         s = strcat("weapon", get_weaponinfo(WEP_FIRST + mod(floor(a*2.4), WEP_LAST)).netname);
3132                         a = bound(0, (when - a) / 4, 1);
3133                         goto hud_config_notifyprint;
3134                 }
3135
3136                 if (j == KN_MAX_ENTRIES)
3137                         j = 0;
3138
3139                 if(killnotify_times[j] + when > time)
3140                         a = 1;
3141                 else if(fadetime)
3142                 {
3143                         a = bound(0, (killnotify_times[j] + when + fadetime - time) / fadetime, 1);
3144                         if(!a)
3145                         {
3146                                 break;
3147                         }
3148                 }
3149                 else
3150                 {
3151                         break;
3152                 }
3153
3154                 s = "";
3155
3156                 w = -1;
3157                 w = DEATH_WEAPONOF(killnotify_deathtype[j]);
3158
3159                 // TODO: maybe print in team colors?
3160                 //
3161                 // Y [used by] X
3162                 if(killnotify_actiontype[j] == 0) 
3163                 {
3164                         if(killnotify_deathtype[j] == DEATH_GENERIC)
3165                         {
3166                                 s = "notify_death";
3167                         }
3168                         else if(killnotify_deathtype[j] == DEATH_NOAMMO)
3169                         {
3170                                 s = "notify_outofammo";
3171                         }
3172                         else if(killnotify_deathtype[j] == DEATH_KILL)
3173                         {
3174                                 s = "notify_selfkill";
3175                         }
3176                         else if(killnotify_deathtype[j] == DEATH_CAMP)
3177                         {
3178                                 s = "notify_camping";
3179                         }
3180                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
3181                         {
3182                                 s = "notify_teamkill_red";
3183                         }
3184                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
3185                         {
3186                                 s = "notify_teamkill_blue";
3187                         }
3188                         else if(killnotify_deathtype[j] == DEATH_DROWN)
3189                         {
3190                                 s = "notify_water";
3191                         }
3192                         else if(killnotify_deathtype[j] == DEATH_SLIME)
3193                         {
3194                                 s = "notify_slime";
3195                         }
3196                         else if(killnotify_deathtype[j] == DEATH_LAVA)
3197                         {
3198                                 s = "notify_lava";
3199                         }
3200                         else if(killnotify_deathtype[j] == DEATH_FALL)
3201                         {
3202                                 s = "notify_fall";
3203                         }
3204                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
3205                         {
3206                                 s = "notify_shootingstar";
3207                         }
3208                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM)
3209                         {
3210                                 s = "notify_death";
3211                         }
3212                         else if(killnotify_deathtype[j] == INFO_GOTFLAG)
3213                         {
3214                                 if(killnotify_victims[j] == "^1RED^7 flag")
3215                                 {
3216                                         s = "notify_red_taken";
3217                                 }
3218                                 else
3219                                 {
3220                                         s = "notify_blue_taken";
3221                                 }
3222                         }
3223                         else if(killnotify_deathtype[j] == INFO_RETURNFLAG)
3224                         {
3225                                 if(killnotify_victims[j] == "^1RED^7 flag")
3226                                 {
3227                                         s = "notify_red_returned";
3228                                 }
3229                                 else
3230                                 {
3231                                         s = "notify_blue_returned";
3232                                 }
3233                         }
3234                         else if(killnotify_deathtype[j] == INFO_LOSTFLAG)
3235                         {
3236                                 if(killnotify_victims[j] == "^1RED^7 flag")
3237                                 {
3238                                         s = "notify_red_lost";
3239                                 }
3240                                 else
3241                                 {
3242                                         s = "notify_blue_lost";
3243                                 }
3244                         }
3245                         else if(killnotify_deathtype[j] == INFO_CAPTUREFLAG)
3246                         {
3247                                 if(killnotify_victims[j] == "^1RED^7 flag")
3248                                 {
3249                                         s = "notify_red_captured";
3250                                 }
3251                                 else
3252                                 {
3253                                         s = "notify_blue_captured";
3254                                 }
3255                         }
3256                         attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3257                         pos_attacker = pos + eX * (0.52 * mySize_x + height) + eY * (0.5 * fontsize_y + i * height);
3258                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
3259
3260                         if(s != "")
3261                         {
3262                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3263                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3264                         }
3265                 }
3266                 // X [did action to] Y
3267                 else
3268                 {
3269                         if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_LASER)
3270                         {
3271                                 s = "notify_melee_laser";
3272                         }
3273                         else if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_SHOTGUN)
3274                         {
3275                                 s = "notify_melee_shotgun";
3276                         }
3277                         else if(WEP_VALID(w))
3278                         {
3279                                 self = get_weaponinfo(w);
3280                                 s = strcat("weapon", self.netname);
3281                         }
3282                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
3283                         {
3284                                 s = "notify_teamkill_red";
3285                         }
3286                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
3287                         {
3288                                 s = "notify_teamkill_red";
3289                         }
3290                         else if(killnotify_deathtype[j] == DEATH_TELEFRAG)
3291                         {
3292                                 s = "notify_telefrag";
3293                         }
3294                         else if(killnotify_deathtype[j] == DEATH_DROWN)
3295                         {
3296                                 s = "notify_water";
3297                         }
3298                         else if(killnotify_deathtype[j] == DEATH_SLIME)
3299                         {
3300                                 s = "notify_slime";
3301                         }
3302                         else if(killnotify_deathtype[j] == DEATH_LAVA)
3303                         {
3304                                 s = "notify_lava";
3305                         }
3306                         else if(killnotify_deathtype[j] == DEATH_FALL)
3307                         {
3308                                 s = "notify_fall";
3309                         }
3310                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
3311                         {
3312                                 s = "notify_shootingstar";
3313                         }
3314                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM) // DEATH_CUSTOM is also void, right?
3315                         {
3316                                 s = "notify_void";
3317                         }
3318                         else if(killnotify_deathtype[j] == DEATH_HEADSHOT)
3319                         {
3320                                 s = "notify_headshot";
3321                         }
3322                         else if(killnotify_deathtype[j] == RACE_SERVER_RECORD)
3323                         {
3324                                 s = "race_newrecordserver";
3325                         }
3326                         else if(killnotify_deathtype[j] == RACE_NEW_RANK)
3327                         {
3328                                 s = "race_newrankyellow";
3329                         }
3330                         else if(killnotify_deathtype[j] == RACE_NEW_TIME)
3331                         {
3332                                 s = "race_newtime";
3333                         }
3334                         else if(killnotify_deathtype[j] == RACE_FAIL)
3335                         {
3336                                 s = "race_newfail";
3337                         }
3338
3339                         attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3340                         victim = textShortenToWidth(killnotify_victims[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3341 :hud_config_notifyprint
3342                         width_attacker = stringwidth(attacker, TRUE, fontsize);
3343                         pos_attacker = pos + eX * (0.48 * mySize_x - height - width_attacker) + eY * (0.5 * fontsize_y + i * height);
3344                         pos_victim = pos + eX * (0.52 * mySize_x + height) + eY * (0.5 * fontsize_y + i * height);
3345                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
3346
3347                         if(s != "")
3348                         {
3349                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3350                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3351                                 drawcolorcodedstring(pos_victim, victim, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3352                         }
3353                 }
3354         }
3355 }
3356
3357 // Timer (#5)
3358 //
3359 // TODO: macro
3360 string seconds_tostring(float sec)
3361 {
3362         float minutes;
3363         minutes = floor(sec / 60);
3364
3365         sec -= minutes * 60;
3366
3367         string s;
3368         s = ftos(100 + sec);
3369
3370         return strcat(ftos(minutes), ":", substring(s, 1, 3));
3371 }
3372
3373 void HUD_Timer(void)
3374 {
3375         if(!autocvar_hud_panel_timer && !autocvar__hud_configure)
3376                 return;
3377
3378         active_panel = HUD_PANEL_TIMER;
3379         HUD_Panel_UpdateCvars(timer);
3380         vector pos, mySize;
3381         pos = panel_pos;
3382         mySize = panel_size;
3383
3384         HUD_Panel_DrawBg(1);
3385         if(panel_bg_padding)
3386         {
3387                 pos += '1 1 0' * panel_bg_padding;
3388                 mySize -= '2 2 0' * panel_bg_padding;
3389         }
3390
3391         string timer;
3392         float timelimit, elapsedTime, timeleft, minutesLeft;
3393
3394         timelimit = getstatf(STAT_TIMELIMIT);
3395
3396         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
3397         timeleft = ceil(timeleft);
3398
3399         minutesLeft = floor(timeleft / 60);
3400
3401         vector timer_color;
3402         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
3403                 timer_color = '1 1 1'; //white
3404         else if(minutesLeft >= 1)
3405                 timer_color = '1 1 0'; //yellow
3406         else
3407                 timer_color = '1 0 0'; //red
3408
3409         if (autocvar_hud_panel_timer_increment || timelimit == 0 || warmup_stage) {
3410                 if (time < getstatf(STAT_GAMESTARTTIME)) {
3411                         //while restart is still active, show 00:00
3412                         timer = seconds_tostring(0);
3413                 } else {
3414                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
3415                         timer = seconds_tostring(elapsedTime);
3416                 }
3417         } else {
3418                 timer = seconds_tostring(timeleft);
3419         }
3420
3421         drawfont = hud_bigfont;
3422         drawstring_aspect(pos, timer, mySize, timer_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3423         drawfont = hud_font;
3424 }
3425
3426 // Radar (#6)
3427 //
3428 void HUD_Radar(void)
3429 {
3430         if ((autocvar_hud_panel_radar == 0 || (autocvar_hud_panel_radar != 2 && !teamplay)) && !autocvar__hud_configure)
3431                 return;
3432
3433         active_panel = HUD_PANEL_RADAR;
3434         HUD_Panel_UpdateCvars(radar);
3435         vector pos, mySize;
3436         pos = panel_pos;
3437         mySize = panel_size;
3438
3439         HUD_Panel_DrawBg(1);
3440         if(panel_bg_padding)
3441         {
3442                 pos += '1 1 0' * panel_bg_padding;
3443                 mySize -= '2 2 0' * panel_bg_padding;
3444         }
3445
3446         local float color1, color2; // color already declared as a global in hud.qc
3447         local vector rgb;
3448         local entity tm;
3449         float scale2d, normalsize, bigsize;
3450         float f;
3451
3452         teamradar_origin2d = pos + 0.5 * mySize;
3453         teamradar_size2d = mySize;
3454
3455         if(minimapname == "")
3456                 return;
3457
3458         teamradar_loadcvars();
3459
3460         switch(hud_panel_radar_zoommode)
3461         {
3462                 default:
3463                 case 0:
3464                         f = current_zoomfraction;
3465                         break;
3466                 case 1:
3467                         f = 1 - current_zoomfraction;
3468                         break;
3469                 case 2:
3470                         f = 0;
3471                         break;
3472                 case 3:
3473                         f = 1;
3474                         break;
3475         }
3476
3477         switch(hud_panel_radar_rotation)
3478         {
3479                 case 0:
3480                         teamradar_angle = view_angles_y - 90;
3481                         break;
3482                 default:
3483                         teamradar_angle = 90 * hud_panel_radar_rotation;
3484                         break;
3485         }
3486
3487         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
3488         teamradar_size2d = mySize;
3489
3490         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
3491
3492         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
3493         if(hud_panel_radar_rotation == 0)
3494         {
3495                 // max-min distance must fit the radar in any rotation
3496                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
3497         }
3498         else
3499         {
3500                 vector c0, c1, c2, c3, span;
3501                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
3502                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
3503                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
3504                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
3505                 span = '0 0 0';
3506                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
3507                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
3508
3509                 // max-min distance must fit the radar in x=x, y=y
3510                 bigsize = min(
3511                         teamradar_size2d_x * scale2d / (1.05 * span_x),
3512                         teamradar_size2d_y * scale2d / (1.05 * span_y)
3513                 );
3514         }
3515
3516         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
3517         if(bigsize > normalsize)
3518                 normalsize = bigsize;
3519
3520         teamradar_size =
3521                   f * bigsize
3522                 + (1 - f) * normalsize;
3523         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
3524                   f * (mi_min + mi_max) * 0.5
3525                 + (1 - f) * view_origin);
3526
3527         color1 = GetPlayerColor(player_localentnum-1);
3528         rgb = GetTeamRGB(color1);
3529
3530         drawsetcliparea(
3531                 pos_x,
3532                 pos_y,
3533                 mySize_x,
3534                 mySize_y
3535         );
3536
3537         draw_teamradar_background(hud_panel_radar_foreground_alpha);
3538
3539         for(tm = world; (tm = find(tm, classname, "radarlink")); )
3540                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
3541         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
3542                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, panel_fg_alpha);
3543         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
3544         {
3545                 color2 = GetPlayerColor(tm.sv_entnum);
3546                 //if(color == COLOR_SPECTATOR || color == color2)
3547                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
3548         }
3549         draw_teamradar_player(view_origin, view_angles, '1 1 1');
3550
3551         drawresetcliparea();
3552 };
3553
3554 // Score (#7)
3555 //
3556 void HUD_Score(void)
3557 {
3558         if(!autocvar_hud_panel_score && !autocvar__hud_configure)
3559                 return;
3560
3561         active_panel = HUD_PANEL_SCORE;
3562         HUD_Panel_UpdateCvars(score);
3563         vector pos, mySize;
3564         pos = panel_pos;
3565         mySize = panel_size;
3566
3567         HUD_Panel_DrawBg(1);
3568         if(panel_bg_padding)
3569         {
3570                 pos += '1 1 0' * panel_bg_padding;
3571                 mySize -= '2 2 0' * panel_bg_padding;
3572         }
3573
3574         float score, distribution, leader;
3575         string sign;
3576         vector distribution_color;
3577         entity tm, pl, me;
3578         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
3579
3580         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
3581                 string timer, distrtimer;
3582
3583                 pl = players.sort_next;
3584                 if(pl == me)
3585                         pl = pl.sort_next;
3586                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
3587                         if(pl.scores[ps_primary] == 0)
3588                                 pl = world;
3589
3590                 score = me.(scores[ps_primary]);
3591                 timer = TIME_ENCODED_TOSTRING(score);
3592
3593                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
3594                         // distribution display
3595                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3596
3597                         distrtimer = ftos_decimals(fabs(distribution/pow(10, TIME_DECIMALS)), TIME_DECIMALS);
3598
3599                         if (distribution <= 0) {
3600                                 distribution_color = '0 1 0';
3601                                 sign = "-";
3602                         }
3603                         else {
3604                                 distribution_color = '1 0 0';
3605                                 sign = "+";
3606                         }
3607                         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);
3608                 }
3609                 // race record display
3610                 if (distribution <= 0)
3611                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3612                 drawfont = hud_bigfont;
3613                 drawstring_aspect(pos, timer, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3614                 drawfont = hud_font;
3615         } else if (!teamplay) { // non-teamgames
3616                 // me vector := [team/connected frags id]
3617                 pl = players.sort_next;
3618                 if(pl == me)
3619                         pl = pl.sort_next;
3620
3621                 if(autocvar__hud_configure)
3622                         distribution = 42;
3623                 else if(pl)
3624                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3625                 else
3626                         distribution = 0;
3627
3628                 score = me.(scores[ps_primary]);
3629                 if(autocvar__hud_configure)
3630                         score = 123;
3631
3632                 if(distribution >= 5) {
3633                         distribution_color = eY;
3634                         leader = 1;
3635                 } else if(distribution >= 0) {
3636                         distribution_color = '1 1 1';
3637                         leader = 1;
3638                 } else if(distribution >= -5)
3639                         distribution_color = '1 1 0';
3640                 else
3641                         distribution_color = eX;
3642
3643                 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);
3644                 if (leader)
3645                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3646                 drawfont = hud_bigfont;
3647                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3648                 drawfont = hud_font;
3649         } else { // teamgames
3650                 float max_fragcount;
3651                 max_fragcount = -99;
3652
3653                 float teamnum;
3654                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
3655                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !autocvar__hud_configure)) // no players? don't display
3656                                 continue;
3657                         score = tm.(teamscores[ts_primary]);
3658                         if(autocvar__hud_configure)
3659                                 score = 123;
3660                         leader = 0;
3661                         
3662                         if (score > max_fragcount)
3663                                 max_fragcount = score;
3664
3665                         if(tm.team == myteam) {
3666                                 if (max_fragcount == score)
3667                                         leader = 1;
3668                                 if (leader)
3669                                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3670                                 drawfont = hud_bigfont;
3671                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
3672                                 drawfont = hud_font;
3673                         } else {
3674                                 if (max_fragcount == score)
3675                                         leader = 1;
3676                                 if (leader)
3677                                         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);
3678                                 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);
3679                                 teamnum += 1;
3680                         }
3681                 }
3682         }
3683 }
3684
3685 // Race timer (#8)
3686 //
3687 void HUD_RaceTimer (void) {
3688         if(!autocvar_hud_panel_racetimer && !(gametype == GAME_RACE || gametype == GAME_CTS) && !autocvar__hud_configure)
3689                 return;
3690
3691         active_panel = HUD_PANEL_RACETIMER;
3692         HUD_Panel_UpdateCvars(racetimer);
3693         vector pos, mySize;
3694         pos = panel_pos;
3695         mySize = panel_size;
3696
3697         HUD_Panel_DrawBg(1);
3698         if(panel_bg_padding)
3699         {
3700                 pos += '1 1 0' * panel_bg_padding;
3701                 mySize -= '2 2 0' * panel_bg_padding;
3702         }
3703
3704         // always force 4:1 aspect
3705         vector newSize;
3706         if(mySize_x/mySize_y > 4)
3707         {
3708                 newSize_x = 4 * mySize_y;
3709                 newSize_y = mySize_y;
3710
3711                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3712         }
3713         else
3714         {
3715                 newSize_y = 1/4 * mySize_x;
3716                 newSize_x = mySize_x;
3717
3718                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3719         }
3720         mySize = newSize;
3721
3722         drawfont = hud_bigfont;
3723         float a, t;
3724         string s, forcetime;
3725
3726         if(autocvar__hud_configure)
3727         {
3728                 s = "0:13:37";
3729                 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);
3730                 s = "^1Intermediate 1 (+15.42)";
3731                 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);
3732                 s = strcat("^1PENALTY: ", ftos_decimals(20 * 0.1, 1), " (missing a checkpoint)");
3733                 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);
3734         }
3735         else if(race_checkpointtime)
3736         {
3737                 a = bound(0, 2 - (time - race_checkpointtime), 1);
3738                 s = "";
3739                 forcetime = "";
3740                 if(a > 0) // just hit a checkpoint?
3741                 {
3742                         if(race_checkpoint != 254)
3743                         {
3744                                 if(race_time && race_previousbesttime)
3745                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
3746                                 else
3747                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
3748                                 if(race_time)
3749                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
3750                         }
3751                 }
3752                 else
3753                 {
3754                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
3755                         {
3756                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
3757                                 if(a > 0) // next one?
3758                                 {
3759                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
3760                                 }
3761                         }
3762                 }
3763
3764                 if(s != "" && a > 0)
3765                 {
3766                         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);
3767                 }
3768
3769                 if(race_penaltytime)
3770                 {
3771                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
3772                         if(a > 0)
3773                         {
3774                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
3775                                 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);
3776                         }
3777                 }
3778
3779                 if(forcetime != "")
3780                 {
3781                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
3782                         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);
3783                 }
3784                 else
3785                         a = 1;
3786
3787                 if(race_laptime && race_checkpoint != 255)
3788                 {
3789                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
3790                         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);
3791                 }
3792         }
3793         else
3794         {
3795                 if(race_mycheckpointtime)
3796                 {
3797                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
3798                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
3799                         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);
3800                 }
3801                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
3802                 {
3803                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
3804                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
3805                         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);
3806                 }
3807
3808                 if(race_penaltytime && !race_penaltyaccumulator)
3809                 {
3810                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
3811                         a = bound(0, (1 + t - time), 1);
3812                         if(a > 0)
3813                         {
3814                                 if(time < t)
3815                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
3816                                 else
3817                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
3818                                 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);
3819                         }
3820                 }
3821         }
3822
3823         drawfont = hud_font;
3824 }
3825
3826 // Vote window (#9)
3827 //
3828 float vote_yescount;
3829 float vote_nocount;
3830 float vote_needed;
3831 float vote_highlighted; // currently selected vote
3832
3833 float vote_active; // is there an active vote?
3834 float vote_prev; // previous state of vote_active to check for a change
3835 float vote_alpha;
3836 float vote_change; // "time" when vote_active changed
3837
3838 void HUD_VoteWindow(void) 
3839 {
3840         if(autocvar_cl_allow_uid2name == -1 && (gametype == GAME_CTS || gametype == GAME_RACE))
3841         {
3842                 vote_active = 1;
3843                 vote_called_vote = strzone(strcat("^2Name ^7instead of \"^1Unregistered player\"", " ^7in stats"));
3844         }
3845
3846         if(!autocvar_hud_panel_vote && !autocvar__hud_configure)
3847                 return;
3848
3849         if(!autocvar__hud_configure)
3850         {
3851                 panel_fg_alpha = autocvar_hud_panel_fg_alpha;
3852                 panel_bg_alpha_str = cvar_string("hud_panel_vote_bg_alpha");
3853
3854                 if(panel_bg_alpha_str == "") {
3855                         panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);
3856                 }
3857                 panel_bg_alpha = stof(panel_bg_alpha_str);
3858         }
3859
3860         string s;
3861         float a;
3862         if(vote_active != vote_prev) {
3863                 vote_change = time;
3864                 vote_prev = vote_active;
3865         }
3866
3867         if(vote_active || autocvar__hud_configure)
3868                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
3869         else
3870                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
3871
3872         if(autocvar__hud_configure)
3873         {
3874                 vote_yescount = 3;
3875                 vote_nocount = 2;
3876                 vote_needed = 4;
3877         }
3878
3879         if(!vote_alpha)
3880                 return;
3881
3882         active_panel = HUD_PANEL_VOTE;
3883         HUD_Panel_UpdateCvars(vote);
3884         vector pos, mySize;
3885         pos = panel_pos;
3886         mySize = panel_size;
3887
3888         if(autocvar_cl_allow_uid2name == -1 && (gametype == GAME_CTS || gametype == GAME_RACE))
3889         {
3890                 panel_pos = eX * 0.3 * vid_conwidth + eY * 0.1 * vid_conheight;
3891                 panel_size = eX * 0.4 * vid_conwidth + eY * 0.3 * vid_conheight;
3892         }
3893
3894         a = vote_alpha * bound(cvar("hud_panel_vote_alreadyvoted_alpha"), 1 - vote_highlighted, 1);
3895         HUD_Panel_DrawBg(a);
3896         a = panel_fg_alpha * a;
3897
3898         if(panel_bg_padding)
3899         {
3900                 pos += '1 1 0' * panel_bg_padding;
3901                 mySize -= '2 2 0' * panel_bg_padding;
3902         }
3903
3904         // always force 3:1 aspect
3905         vector newSize;
3906         if(mySize_x/mySize_y > 3)
3907         {
3908                 newSize_x = 3 * mySize_y;
3909                 newSize_y = mySize_y;
3910
3911                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3912         }
3913         else
3914         {
3915                 newSize_y = 1/3 * mySize_x;
3916                 newSize_x = mySize_x;
3917
3918                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3919         }
3920         mySize = newSize;
3921
3922         s = "A vote has been called for:";
3923         if(autocvar_cl_allow_uid2name == -1 && (gametype == GAME_CTS || gametype == GAME_RACE))
3924                 s = "Allow servers to store and display your name?";
3925         drawstring_aspect(pos, s, eX * mySize_x + eY * (2/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3926         s = textShortenToWidth(vote_called_vote, mySize_x, '1 1 0' * mySize_y * (1/8), stringwidth_colors);
3927         if(autocvar__hud_configure)
3928                 s = "^1Configure the HUD";
3929         drawcolorcodedstring_aspect(pos + eY * (2/8) * mySize_y, s, eX * mySize_x + eY * (1.75/8) * mySize_y, a, DRAWFLAG_NORMAL);
3930
3931         // print the yes/no counts
3932         s = strcat("Yes (", getcommandkey("vyes", "vyes"), "): ", ftos(vote_yescount));
3933         if(autocvar_cl_allow_uid2name == -1 && (gametype == GAME_CTS || gametype == GAME_RACE))
3934                 s = strcat("Yes: (press y)");
3935         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);
3936         s = strcat("No (", getcommandkey("vno", "vno"), "): ", ftos(vote_nocount));
3937         if(autocvar_cl_allow_uid2name == -1 && (gametype == GAME_CTS || gametype == GAME_RACE))
3938                 s = strcat("No: (press n)");
3939         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);
3940
3941         // draw the progress bar backgrounds
3942         drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_back", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3943
3944         // draw the highlights
3945         if(vote_highlighted == 1) {
3946                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
3947                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3948         }
3949         else if(vote_highlighted == 2) {
3950                 drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
3951                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3952         }
3953
3954         // draw the progress bars
3955         if(vote_yescount && vote_needed)
3956         {
3957                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
3958                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3959         }
3960
3961         if(vote_nocount && vote_needed)
3962         {
3963                 drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
3964                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3965         }
3966
3967         drawresetcliparea();
3968
3969         if(!vote_active) {
3970                 vote_highlighted = 0;
3971         }
3972 }
3973
3974 // Mod icons panel (#10)
3975 //
3976
3977 float mod_active; // is there any active mod icon?
3978
3979 // Clan Arena HUD modicons
3980 void HUD_Mod_CA(vector pos, vector mySize)
3981 {
3982         mod_active = 1; // CA should never hide the mod icons panel
3983         float redalive, bluealive;
3984         redalive = getstati(STAT_REDALIVE);
3985         bluealive = getstati(STAT_BLUEALIVE);
3986
3987         drawfont = hud_bigfont;
3988         vector redpos, bluepos;
3989         if(mySize_x > mySize_y)
3990         {
3991                 redpos = pos;
3992                 bluepos = pos + eY * 0.5 * mySize_y;
3993                 drawpic_aspect_skin(redpos, "player_red.tga", 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3994                 drawstring_aspect(redpos + eX * 0.5 * mySize_x, ftos(redalive), 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3995                 drawpic_aspect_skin(bluepos, "player_blue.tga", 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3996                 drawstring_aspect(bluepos + eX * 0.5 * mySize_x, ftos(bluealive), 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3997         }
3998         else
3999         {
4000                 redpos = pos;
4001                 bluepos = pos + eY * 0.5 * mySize_y;
4002                 drawpic_aspect_skin(redpos, "player_red.tga", eX * mySize_x + eY * 0.3 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4003                 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);
4004                 drawpic_aspect_skin(bluepos, "player_blue.tga", eX * mySize_x + eY * 0.3 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4005                 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);
4006         }
4007         drawfont = hud_font;
4008 }
4009
4010 // CTF HUD modicon section
4011 float redflag_prevframe, blueflag_prevframe; // status during previous frame
4012 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
4013 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
4014
4015 void HUD_Mod_CTF_Reset(void)
4016 {
4017         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
4018 }
4019
4020 void HUD_Mod_CTF(vector pos, vector mySize)
4021 {
4022         vector redflag_pos, blueflag_pos;
4023         vector flag_size;
4024         float f; // every function should have that
4025
4026         float redflag, blueflag; // current status
4027         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
4028         float stat_items;
4029
4030         stat_items = getstati(STAT_ITEMS);
4031         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
4032         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
4033         
4034         if(redflag || blueflag)
4035                 mod_active = 1;
4036         else
4037                 mod_active = 0;
4038
4039         if(autocvar__hud_configure)
4040         {
4041                 redflag = 1;
4042                 blueflag = 2;
4043         }
4044
4045         // when status CHANGES, set old status into prevstatus and current status into status
4046         if (redflag != redflag_prevframe)
4047         {
4048                 redflag_statuschange_time = time;
4049                 redflag_prevstatus = redflag_prevframe;
4050                 redflag_prevframe = redflag;
4051         }
4052
4053         if (blueflag != blueflag_prevframe)
4054         {
4055                 blueflag_statuschange_time = time;
4056                 blueflag_prevstatus = blueflag_prevframe;
4057                 blueflag_prevframe = blueflag;
4058         }
4059
4060         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
4061         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
4062
4063         float BLINK_FACTOR = 0.15;
4064         float BLINK_BASE = 0.85;
4065         // note:
4066         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
4067         // thus
4068         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
4069         // ensure RMS == 1
4070         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
4071
4072         string red_icon, red_icon_prevstatus;
4073         float red_alpha, red_alpha_prevstatus;
4074         red_alpha = red_alpha_prevstatus = 1;
4075         switch(redflag) {
4076                 case 1: red_icon = "flag_red_taken"; break;
4077                 case 2: red_icon = "flag_red_lost"; break;
4078                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4079                 default:
4080                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
4081                                 red_icon = "flag_red_shielded";
4082                         else
4083                                 red_icon = string_null;
4084                         break;
4085         }
4086         switch(redflag_prevstatus) {
4087                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
4088                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
4089                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4090                 default:
4091                         if(redflag == 3)
4092                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
4093                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
4094                                 red_icon_prevstatus = "flag_red_shielded";
4095                         else
4096                                 red_icon_prevstatus = string_null;
4097                         break;
4098         }
4099
4100         string blue_icon, blue_icon_prevstatus;
4101         float blue_alpha, blue_alpha_prevstatus;
4102         blue_alpha = blue_alpha_prevstatus = 1;
4103         switch(blueflag) {
4104                 case 1: blue_icon = "flag_blue_taken"; break;
4105                 case 2: blue_icon = "flag_blue_lost"; break;
4106                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4107                 default:
4108                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
4109                                 blue_icon = "flag_blue_shielded";
4110                         else
4111                                 blue_icon = string_null;
4112                         break;
4113         }
4114         switch(blueflag_prevstatus) {
4115                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
4116                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
4117                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4118                 default:
4119                         if(blueflag == 3)
4120                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
4121                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
4122                                 blue_icon_prevstatus = "flag_blue_shielded";
4123                         else
4124                                 blue_icon_prevstatus = string_null;
4125                         break;
4126         }
4127
4128         if(mySize_x > mySize_y) {
4129                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
4130                         redflag_pos = pos;
4131                         blueflag_pos = pos + eX * 0.5 * mySize_x;
4132                 } else {
4133                         blueflag_pos = pos;
4134                         redflag_pos = pos + eX * 0.5 * mySize_x;
4135                 }
4136                 flag_size = eX * 0.5 * mySize_x + eY * mySize_y;
4137         } else {
4138                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
4139                         redflag_pos = pos;
4140                         blueflag_pos = pos + eY * 0.5 * mySize_y;
4141                 } else {
4142                         blueflag_pos = pos;
4143                         redflag_pos = pos + eY * 0.5 * mySize_y;
4144                 }
4145                 flag_size = eY * 0.5 * mySize_y + eX * mySize_x;
4146         }
4147
4148         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
4149         if(red_icon_prevstatus && f < 1)
4150                 drawpic_aspect_skin_expanding(redflag_pos, red_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
4151         if(red_icon)
4152                 drawpic_aspect_skin(redflag_pos, red_icon, flag_size, '1 1 1', panel_fg_alpha * red_alpha * f, DRAWFLAG_NORMAL);
4153
4154         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
4155         if(blue_icon_prevstatus && f < 1)
4156                 drawpic_aspect_skin_expanding(blueflag_pos, blue_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
4157         if(blue_icon)
4158                 drawpic_aspect_skin(blueflag_pos, blue_icon, flag_size, '1 1 1', panel_fg_alpha * blue_alpha * f, DRAWFLAG_NORMAL);
4159 }
4160
4161 // Keyhunt HUD modicon section
4162 float kh_runheretime;
4163
4164 void HUD_Mod_KH_Reset(void)
4165 {
4166         kh_runheretime = 0;
4167 }
4168
4169 void HUD_Mod_KH(vector pos, vector mySize)
4170 {
4171         mod_active = 1; // keyhunt should never hide the mod icons panel
4172         float kh_keys;
4173         float keyteam;
4174         float a, aa;
4175         vector p, pa, kh_size, kh_asize;
4176
4177         kh_keys = getstati(STAT_KH_KEYS);
4178
4179         p_x = pos_x;
4180         if(mySize_x > mySize_y)
4181         {
4182                 p_y = pos_y + 0.25 * mySize_y;
4183                 pa = p - eY * 0.25 * mySize_y;
4184
4185                 kh_size_x = mySize_x * 0.25;
4186                 kh_size_y = 0.75 * mySize_y;
4187                 kh_asize_x = mySize_x * 0.25;
4188                 kh_asize_y = mySize_y * 0.25;
4189         }
4190         else
4191         {
4192                 p_y = pos_y + 0.125 * mySize_y;
4193                 pa = p - eY * 0.125 * mySize_y;
4194
4195                 kh_size_x = mySize_x * 0.5;
4196                 kh_size_y = 0.375 * mySize_y;
4197                 kh_asize_x = mySize_x * 0.5;
4198                 kh_asize_y = mySize_y * 0.125;
4199         }
4200
4201         float i, key;
4202
4203         float keycount;
4204         keycount = 0;
4205         for(i = 0; i < 4; ++i)
4206         {
4207                 key = floor(kh_keys / pow(32, i)) & 31;
4208                 keyteam = key - 1;
4209                 if(keyteam == 30 && keycount <= 4)
4210                         keycount += 4;
4211                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
4212                         keycount += 1;
4213         }
4214
4215         // this yields 8 exactly if "RUN HERE" shows
4216
4217         if(keycount == 8)
4218         {
4219                 if(!kh_runheretime)
4220                         kh_runheretime = time;
4221                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
4222         }
4223         else
4224                 kh_runheretime = 0;
4225
4226         for(i = 0; i < 4; ++i)
4227         {
4228                 key = floor(kh_keys / pow(32, i)) & 31;
4229                 keyteam = key - 1;
4230                 switch(keyteam)
4231                 {
4232                         case 30: // my key
4233                                 keyteam = myteam;
4234                                 a = 1;
4235                                 aa = 1;
4236                                 break;
4237                         case -1: // no key
4238                                 a = 0;
4239                                 aa = 0;
4240                                 break;
4241                         default: // owned or dropped
4242                                 a = 0.2;
4243                                 aa = 0.5;
4244                                 break;
4245                 }
4246                 a = a * panel_fg_alpha;
4247                 aa = aa * panel_fg_alpha;
4248                 if(a > 0)
4249                 {
4250                         switch(keyteam)
4251                         {
4252                                 case COLOR_TEAM1:
4253                                         drawpic_aspect_skin(pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4254                                         break;
4255                                 case COLOR_TEAM2:
4256                                         drawpic_aspect_skin(pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4257                                         break;
4258                                 case COLOR_TEAM3:
4259                                         drawpic_aspect_skin(pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4260                                         break;
4261                                 case COLOR_TEAM4:
4262                                         drawpic_aspect_skin(pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4263                                         break;
4264                                 default:
4265                                         break;
4266                         }
4267                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
4268                         {
4269                                 case 0:
4270                                         drawpic_aspect_skin(p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4271                                         break;
4272                                 case 1:
4273                                         drawpic_aspect_skin(p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4274                                         break;
4275                                 case 2:
4276                                         drawpic_aspect_skin(p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4277                                         break;
4278                                 case 3:
4279                                         drawpic_aspect_skin(p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4280                                         break;
4281                         }
4282                 }
4283                 if(mySize_x > mySize_y)
4284                 {
4285                         p_x += 0.25 * mySize_x;
4286                         pa_x += 0.25 * mySize_x;
4287                 }
4288                 else
4289                 {
4290                         if(i == 1)
4291                         {
4292                                 p_y = pos_y + 0.625 * mySize_y;
4293                                 pa_y = pos_y + 0.5 * mySize_y;
4294                                 p_x = pos_x;
4295                                 pa_x = pos_x;
4296                         }
4297                         else
4298                         {
4299                                 p_x += 0.5 * mySize_x;
4300                                 pa_x += 0.5 * mySize_x;
4301                         }
4302                 }
4303         }
4304 }
4305
4306 // Nexball HUD mod icon
4307 void HUD_Mod_NexBall(vector pos, vector mySize)
4308 {
4309         float stat_items, nb_pb_starttime, dt, p;
4310
4311         stat_items = getstati(STAT_ITEMS);
4312         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
4313
4314         if (stat_items & IT_KEY1)
4315                 mod_active = 1;
4316         else
4317                 mod_active = 0;
4318
4319         //Manage the progress bar if any
4320         if (nb_pb_starttime > 0)
4321         {
4322                 dt = mod(time - nb_pb_starttime, nb_pb_period);
4323                 // one period of positive triangle
4324                 p = 2 * dt / nb_pb_period;
4325                 if (p > 1)
4326                         p = 2 - p;
4327
4328                 //Draw the filling
4329                 vector barsize;
4330                 float vertical;
4331                 if(mySize_x > mySize_y)
4332                 {
4333                         barsize = eX * p * mySize_x + eY * mySize_y;
4334                         vertical = 0;
4335                 }
4336                 else
4337                 {
4338                         barsize = eX * mySize_x + eY * p * mySize_y;
4339                         vertical = 1;
4340                 }
4341                 HUD_Panel_GetProgressBarColor(nexball);
4342                 HUD_Panel_DrawProgressBar(pos, vertical, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4343         }
4344
4345         if (stat_items & IT_KEY1)
4346                 drawpic_aspect_skin(pos, "nexball_carrying", eX * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4347 }
4348
4349 // Race/CTS HUD mod icons
4350 float crecordtime_prev; // last remembered crecordtime
4351 float crecordtime_change_time; // time when crecordtime last changed
4352 float srecordtime_prev; // last remembered srecordtime
4353 float srecordtime_change_time; // time when srecordtime last changed
4354
4355 float race_status_time;
4356 float race_status_prev;
4357 string race_status_name_prev;
4358 void HUD_Mod_Race(vector pos, vector mySize)
4359 {
4360         mod_active = 1; // race should never hide the mod icons panel
4361         entity me;
4362         me = playerslots[player_localentnum - 1];
4363         float t, score;
4364         float f; // yet another function has this
4365         score = me.(scores[ps_primary]);
4366
4367         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
4368                 return; // no records in the actual race
4369
4370         drawfont = hud_bigfont;
4371
4372         // clientside personal record
4373         string rr;
4374         if(gametype == GAME_CTS)
4375                 rr = CTS_RECORD;
4376         else
4377                 rr = RACE_RECORD;
4378         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
4379
4380         if(score && (score < t || !t)) {
4381                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
4382                 if(cvar("cl_autodemo_delete_keeprecords"))
4383                 {
4384                         f = cvar("cl_autodemo_delete");
4385                         f &~= 1;
4386                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
4387                 }
4388         }
4389
4390         if(t != crecordtime_prev) {
4391                 crecordtime_prev = t;
4392                 crecordtime_change_time = time;
4393         }
4394
4395         vector textPos, medalPos;
4396         float squareSize;
4397         if(mySize_x > mySize_y) {
4398                 // text on left side
4399                 squareSize = min(mySize_y, mySize_x/2);
4400                 textPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eY * 0.5 * (mySize_y - squareSize);
4401                 medalPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eX * 0.5 * mySize_x + eY * 0.5 * (mySize_y - squareSize);
4402         } else {
4403                 // text on top
4404                 squareSize = min(mySize_x, mySize_y/2);
4405                 textPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eX * 0.5 * (mySize_x - squareSize);
4406                 medalPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eY * 0.5 * mySize_y + eX * 0.5 * (mySize_x - squareSize);
4407         }
4408
4409         f = time - crecordtime_change_time;
4410
4411         if (f > 1) {
4412                 drawstring_aspect(textPos, "Personal best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4413                 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);
4414         } else {
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                 drawstring_aspect_expanding(pos, "Personal best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
4418                 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);
4419         }
4420
4421         // server record
4422         t = race_server_record;
4423         if(t != srecordtime_prev) {
4424                 srecordtime_prev = t;
4425                 srecordtime_change_time = time;
4426         }
4427         f = time - srecordtime_change_time;
4428
4429         if (f > 1) {
4430                 drawstring_aspect(textPos + eY * 0.5 * squareSize, "Server best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4431                 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);
4432         } else {
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                 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);
4436                 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);
4437         }
4438
4439         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
4440                 race_status_time = time + 5;
4441                 race_status_prev = race_status;
4442                 if (race_status_name_prev)
4443                         strunzone(race_status_name_prev);
4444                 race_status_name_prev = strzone(race_status_name);
4445         }
4446
4447         // race "awards"
4448         float a;
4449         a = bound(0, race_status_time - time, 1);
4450
4451         string s;
4452         s = textShortenToWidth(race_status_name, squareSize, '1 1 0' * 0.1 * squareSize, stringwidth_colors);
4453
4454         float rank;
4455         if(race_status > 0)
4456                 rank = race_CheckName(race_status_name);
4457         string rankname;
4458         rankname = race_PlaceName(rank);
4459
4460         vector namepos;
4461         namepos = medalPos + '0 0.8 0' * squareSize;
4462         vector rankpos;
4463         rankpos = medalPos + '0 0.15 0' * squareSize;
4464
4465         if(race_status == 0)
4466                 drawpic_aspect_skin(medalPos, "race_newfail", '1 1 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4467         else if(race_status == 1) {
4468                 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);
4469                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4470                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4471         } else if(race_status == 2) {
4472                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
4473                         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);
4474                 else
4475                         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);
4476                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4477                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4478         } else if(race_status == 3) {
4479                 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);
4480                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4481                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4482         }
4483
4484         if (race_status_time - time <= 0) {
4485                 race_status_prev = -1;
4486                 race_status = -1;
4487                 if(race_status_name)
4488                         strunzone(race_status_name);
4489                 race_status_name = string_null;
4490                 if(race_status_name_prev)
4491                         strunzone(race_status_name_prev);
4492                 race_status_name_prev = string_null;
4493         }
4494         drawfont = hud_font;
4495 }
4496
4497 float mod_prev; // previous state of mod_active to check for a change
4498 float mod_alpha;
4499 float mod_change; // "time" when mod_active changed
4500
4501 void HUD_ModIcons(void)
4502 {
4503         if(!autocvar_hud_panel_modicons && !autocvar__hud_configure)
4504                 return;
4505
4506         if (gametype != GAME_KEYHUNT && gametype != GAME_CTF && gametype != GAME_NEXBALL && gametype != GAME_CTS && gametype != GAME_RACE && gametype != GAME_CA && !autocvar__hud_configure)
4507                 return;
4508
4509         active_panel = HUD_PANEL_MODICONS;
4510         HUD_Panel_UpdateCvars(modicons);
4511         vector pos, mySize;
4512         pos = panel_pos;
4513         mySize = panel_size;
4514
4515         if(mod_active != mod_prev) {
4516                 mod_change = time;
4517                 mod_prev = mod_active;
4518         }
4519
4520         if(mod_active || autocvar__hud_configure)
4521                 mod_alpha = bound(0, (time - mod_change) * 2, 1);
4522         else
4523                 mod_alpha = bound(0, 1 - (time - mod_change) * 2, 1);
4524
4525         if(mod_alpha)
4526                 HUD_Panel_DrawBg(mod_alpha);
4527
4528         if(panel_bg_padding)
4529         {
4530                 pos += '1 1 0' * panel_bg_padding;
4531                 mySize -= '2 2 0' * panel_bg_padding;
4532         }
4533
4534         // these MUST be ran in order to update mod_active
4535         if(gametype == GAME_KEYHUNT)
4536                 HUD_Mod_KH(pos, mySize);
4537         else if(gametype == GAME_CTF || autocvar__hud_configure)
4538                 HUD_Mod_CTF(pos, mySize); // forcealpha only needed for ctf icons, as only they are shown in config mode
4539         else if(gametype == GAME_NEXBALL)
4540                 HUD_Mod_NexBall(pos, mySize);
4541         else if(gametype == GAME_CTS || gametype == GAME_RACE)
4542                 HUD_Mod_Race(pos, mySize);
4543         else if(gametype == GAME_CA)
4544                 HUD_Mod_CA(pos, mySize);
4545 }
4546
4547 // Draw pressed keys (#11)
4548 //
4549 void HUD_DrawPressedKeys(void)
4550 {
4551         if(!autocvar_hud_panel_pressedkeys && !autocvar__hud_configure)
4552                 return;
4553
4554         if(!(spectatee_status > 0 || autocvar_hud_panel_pressedkeys >= 2 || autocvar__hud_configure))
4555                 return;
4556
4557         active_panel = HUD_PANEL_PRESSEDKEYS;
4558         HUD_Panel_UpdateCvars(pressedkeys);
4559         vector pos, mySize;
4560         pos = panel_pos;
4561         mySize = panel_size;
4562
4563         HUD_Panel_DrawBg(1);
4564         if(panel_bg_padding)
4565         {
4566                 pos += '1 1 0' * panel_bg_padding;
4567                 mySize -= '2 2 0' * panel_bg_padding;
4568         }
4569
4570         // force custom aspect
4571         float aspect = cvar("hud_panel_pressedkeys_aspect");
4572         if(aspect)
4573         {
4574                 vector newSize;
4575                 if(mySize_x/mySize_y > aspect)
4576                 {
4577                         newSize_x = aspect * mySize_y;
4578                         newSize_y = mySize_y;
4579
4580                         pos_x = pos_x + (mySize_x - newSize_x) / 2;
4581                 }
4582                 else
4583                 {
4584                         newSize_y = 1/aspect * mySize_x;
4585                         newSize_x = mySize_x;
4586
4587                         pos_y = pos_y + (mySize_y - newSize_y) / 2;
4588                 }
4589                 mySize = newSize;
4590         }
4591
4592         vector keysize;
4593         keysize = eX * mySize_x * (1/3) + eY * mySize_y * 0.5;
4594         float pressedkeys;
4595
4596         pressedkeys = getstatf(STAT_PRESSED_KEYS);
4597         drawpic_aspect_skin(pos, ((pressedkeys & KEY_CROUCH) ? "key_crouch_inv.tga" : "key_crouch.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4598         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);
4599         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);
4600         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);
4601         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);
4602         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);
4603 }
4604
4605 // Handle chat as a panel (#12)
4606 //
4607 void HUD_Chat(void)
4608 {
4609         if(!autocvar_hud_panel_chat && !autocvar__hud_configure)
4610         {
4611                 cvar_set("con_chatrect", "0");
4612                 return;
4613         }
4614
4615         active_panel = HUD_PANEL_CHAT;
4616         HUD_Panel_UpdateCvars(chat);
4617
4618         if(autocvar__con_chat_maximized && !autocvar__hud_configure) // draw at full screen height if maximized
4619         {
4620                 panel_pos_y = panel_bg_border;
4621                 panel_size_y = vid_conheight - panel_bg_border * 2;
4622                 if(panel_bg == "0") // force a border when maximized
4623                 {
4624                         if(precache_pic(panel_bg) == "") {
4625                                 panel_bg = strcat(hud_skin_path, "/border_default");
4626                                 if(precache_pic(panel_bg) == "") {
4627                                         panel_bg = "gfx/hud/default/border_default";
4628                                 }
4629                         }
4630                 }
4631                 panel_bg_alpha = max(0.75, panel_bg_alpha); // force an alpha of at least 0.75
4632         }
4633
4634         vector pos, mySize;
4635         pos = panel_pos;
4636         mySize = panel_size;
4637
4638         HUD_Panel_DrawBg(1);
4639
4640         if(panel_bg_padding)
4641         {
4642                 pos += '1 1 0' * panel_bg_padding;
4643                 mySize -= '2 2 0' * panel_bg_padding;
4644         }
4645
4646         cvar_set("con_chatrect", "1");
4647
4648         cvar_set("con_chatrect_x", ftos(pos_x/vid_conwidth));
4649         cvar_set("con_chatrect_y", ftos(pos_y/vid_conheight));
4650
4651         cvar_set("con_chatwidth", ftos(mySize_x/vid_conwidth));
4652         cvar_set("con_chat", ftos(floor(mySize_y/cvar("con_chatsize") - 0.5)));
4653
4654         if(autocvar__hud_configure)
4655         {
4656                 float chatsize;
4657                 chatsize = cvar("con_chatsize");
4658                 cvar_set("con_chatrect_x", "9001"); // over 9000, we'll fake it instead for more control over alpha and such
4659                 float i, a;
4660                 for(i = 0; i < cvar("con_chat"); ++i)
4661                 {
4662                         if(i == cvar("con_chat") - 1)
4663                                 a = panel_fg_alpha;
4664                         else
4665                                 a = panel_fg_alpha * floor(((i + 1) * 7 + cvar("con_chattime"))/45);
4666                         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);
4667                 }
4668         }
4669 }
4670
4671 // Engine info panel (#13)
4672 //
4673 float prevfps;
4674 float prevfps_time;
4675 float framecounter;
4676
4677 float frametimeavg;
4678 float frametimeavg1; // 1 frame ago
4679 float frametimeavg2; // 2 frames ago
4680 void HUD_EngineInfo(void)
4681 {
4682         if(!autocvar_hud_panel_engineinfo && !autocvar__hud_configure)
4683                 return;
4684
4685         active_panel = HUD_PANEL_ENGINEINFO;
4686         HUD_Panel_UpdateCvars(engineinfo);
4687         vector pos, mySize;
4688         pos = panel_pos;
4689         mySize = panel_size;
4690
4691         HUD_Panel_DrawBg(1);
4692         if(panel_bg_padding)
4693         {
4694                 pos += '1 1 0' * panel_bg_padding;
4695                 mySize -= '2 2 0' * panel_bg_padding;
4696         }
4697
4698         float currentTime = gettime(GETTIME_REALTIME);
4699         if(cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage"))
4700         {
4701                 float currentframetime = currentTime - prevfps_time;
4702                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + currentframetime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
4703                 frametimeavg2 = frametimeavg1;
4704                 frametimeavg1 = frametimeavg;
4705                 
4706                 float weight;
4707                 weight = cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight");
4708                 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.
4709                 {
4710                         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
4711                                 prevfps = (1/currentframetime);
4712                         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"
4713                 }
4714                 prevfps_time = currentTime;
4715         }
4716         else
4717         {
4718                 framecounter += 1;
4719                 if(currentTime - prevfps_time > cvar("hud_panel_engineinfo_framecounter_time"))
4720                 {
4721                         prevfps = framecounter/(currentTime - prevfps_time);
4722                         framecounter = 0;
4723                         prevfps_time = currentTime;
4724                 }
4725         }
4726
4727         vector color;
4728         color = HUD_Get_Num_Color (prevfps, 100);
4729         drawfont = hud_bigfont;
4730         drawstring_aspect(pos, strcat("FPS: ", ftos_decimals(prevfps, cvar("hud_panel_engineinfo_framecounter_decimals"))), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
4731         drawfont = hud_font;
4732 }
4733
4734 // Info messages panel (#14)
4735 //
4736 #define drawInfoMessage(s)\
4737         if(autocvar_hud_panel_infomessages_flip)\
4738                 o_x = pos_x + mySize_x - stringwidth(s, TRUE, fontsize);\
4739         drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);\
4740         o_y += fontsize_y;
4741 void HUD_InfoMessages(void)
4742 {
4743         if(!autocvar_hud_panel_infomessages && !autocvar__hud_configure)
4744                 return;
4745
4746         active_panel = HUD_PANEL_INFOMESSAGES;
4747         HUD_Panel_UpdateCvars(infomessages);
4748         vector pos, mySize;
4749         pos = panel_pos;
4750         mySize = panel_size;
4751
4752         HUD_Panel_DrawBg(1);
4753         if(panel_bg_padding)
4754         {
4755                 pos += '1 1 0' * panel_bg_padding;
4756                 mySize -= '2 2 0' * panel_bg_padding;
4757         }
4758
4759         // always force 5:1 aspect
4760         vector newSize;
4761         if(mySize_x/mySize_y > 5)
4762         {
4763                 newSize_x = 5 * mySize_y;
4764                 newSize_y = mySize_y;
4765
4766                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
4767         }
4768         else
4769         {
4770                 newSize_y = 1/5 * mySize_x;
4771                 newSize_x = mySize_x;
4772
4773                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
4774         }
4775
4776         mySize = newSize;
4777         entity tm;
4778         vector o;
4779         o = pos;
4780
4781         vector fontsize;
4782         fontsize = '0.20 0.20 0' * mySize_y;
4783         
4784         float a;
4785         if(spectatee_status != 0)
4786                 a = 1;
4787         else
4788                 a = panel_fg_alpha;
4789
4790         string s;
4791         if(!autocvar__hud_configure)
4792         {
4793                 if(spectatee_status && !intermission)
4794                 {
4795                         if(spectatee_status == -1)
4796                                 s = "^1Observing";
4797                         else
4798                                 s = strcat("^1Spectating: ^7", GetPlayerName(spectatee_status - 1));
4799                         drawInfoMessage(s)
4800
4801                         if(spectatee_status == -1)
4802                                 s = strcat("^1Press ^3", getcommandkey("primary fire", "+fire"), "^1 to spectate");
4803                         else
4804                                 s = strcat("^1Press ^3", getcommandkey("primary fire", "+fire"), "^1 for another player");
4805                         drawInfoMessage(s)
4806
4807                         if(spectatee_status == -1)
4808                                 s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
4809                         else
4810                                 s = strcat("^1Press ^3", getcommandkey("secondary fire", "+fire2"), "^1 to observe");
4811                         drawInfoMessage(s)
4812
4813                         s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
4814                         drawInfoMessage(s)
4815
4816                         if(gametype == GAME_ARENA)
4817                                 s = "^1Wait for your turn to join";
4818                         else if(gametype == GAME_LMS)
4819                         {
4820                                 entity sk;
4821                                 sk = playerslots[player_localentnum - 1];
4822                                 if(sk.(scores[ps_primary]) >= 666)
4823                                         s = "^1Match has already begun";
4824                                 else if(sk.(scores[ps_primary]) > 0)
4825                                         s = "^1You have no more lives left";
4826                                 else
4827                                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
4828                         }
4829                         else
4830                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
4831                         drawInfoMessage(s)
4832
4833                         //show restart countdown:
4834                         if (time < getstatf(STAT_GAMESTARTTIME)) {
4835                                 float countdown;
4836                                 //we need to ceil, otherwise the countdown would be off by .5 when using round()
4837                                 countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
4838                                 s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
4839                                 drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);
4840                                 o_y += fontsize_y;
4841                         }
4842                 }
4843                 if(warmup_stage && !intermission)
4844                 {
4845                         s = "^2Currently in ^1warmup^2 stage!";
4846                         drawInfoMessage(s)
4847                 }
4848
4849                 string blinkcolor;
4850                 if(mod(time, 1) >= 0.5)
4851                         blinkcolor = "^1";
4852                 else
4853                         blinkcolor = "^3";
4854
4855                 if(ready_waiting && !intermission && !spectatee_status)
4856                 {
4857                         if(ready_waiting_for_me)
4858                         {
4859                                 if(warmup_stage)
4860                                         s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
4861                                 else
4862                                         s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
4863                         }
4864                         else
4865                         {
4866                                 if(warmup_stage)
4867                                         s = strcat("^2Waiting for others to ready up to end warmup...");
4868                                 else
4869                                         s = strcat("^2Waiting for others to ready up...");
4870                         }
4871                         drawInfoMessage(s)
4872                 }
4873                 else if(warmup_stage && !intermission && !spectatee_status)
4874                 {
4875                         s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
4876                         drawInfoMessage(s)
4877                 }
4878
4879                 if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
4880                 {
4881                         float ts_min, ts_max;
4882                         tm = teams.sort_next;
4883                         if (tm)
4884                         {
4885                                 for(; tm.sort_next; tm = tm.sort_next)
4886                                 {
4887                                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
4888                                                 continue;
4889                                         if(!ts_min) ts_min = tm.team_size;
4890                                         else ts_min = min(ts_min, tm.team_size);
4891                                         if(!ts_max) ts_max = tm.team_size;
4892                                         else ts_max = max(ts_max, tm.team_size);
4893                                 }
4894                                 if ((ts_max - ts_min) > 1)
4895                                 {
4896                                         s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
4897                                         tm = GetTeam(myteam, false);
4898                                         if (tm)
4899                                         if (tm.team != COLOR_SPECTATOR)
4900                                         if (tm.team_size == ts_max)
4901                                                 s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
4902                                         drawInfoMessage(s)
4903                                 }
4904                         }
4905                 }
4906         }
4907         else 
4908         {
4909                 s = "^7Press ^3ESC ^7to show HUD options.";
4910                 drawInfoMessage(s)
4911                 s = "^3Doubleclick ^7a panel for panel-specific options.";
4912                 drawInfoMessage(s)
4913                 s = "^3CTRL ^7to disable collision testing, ^3SHIFT ^7and";
4914                 drawInfoMessage(s)
4915                 s = "^3ALT ^7+ ^3ARROW KEYS ^7for fine adjustments.";
4916                 drawInfoMessage(s)
4917         }
4918 }
4919
4920 /*
4921 ==================
4922 Main HUD system
4923 ==================
4924 */
4925
4926 void HUD_ShowSpeed(void)
4927 {
4928         vector numsize;
4929         float pos, conversion_factor;
4930         string speed, zspeed, unit;
4931
4932         switch(cvar("cl_showspeed_unit"))
4933         {
4934                 default:
4935                 case 0:
4936                         unit = "";
4937                         conversion_factor = 1.0;
4938                         break;
4939                 case 1:
4940                         unit = " qu/s";
4941                         conversion_factor = 1.0;
4942                         break;
4943                 case 2:
4944                         unit = " m/s";
4945                         conversion_factor = 0.0254;
4946                         break;
4947                 case 3:
4948                         unit = " km/h";
4949                         conversion_factor = 0.0254 * 3.6;
4950                         break;
4951                 case 4:
4952                         unit = " mph";
4953                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
4954                         break;
4955                 case 5:
4956                         unit = " knots";
4957                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
4958                         break;
4959         }
4960
4961         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
4962
4963         numsize_x = numsize_y = cvar("cl_showspeed_size");
4964         pos = (vid_conheight - numsize_y) * cvar("cl_showspeed_position");
4965
4966         drawfont = hud_bigfont;
4967         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
4968
4969         if (cvar("cl_showspeed_z") == 1) {
4970                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
4971                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
4972         }
4973
4974         drawfont = hud_font;
4975 }
4976
4977 vector acc_prevspeed;
4978 float acc_prevtime;
4979 float acc_avg;
4980
4981 void HUD_ShowAcceleration(void)
4982 {
4983         float acceleration, sz, scale, alpha, f;
4984         vector pos, top, rgb;
4985         top_x = vid_conwidth/2;
4986         top_y = 0;
4987
4988         f = time - acc_prevtime;
4989         if(cvar("cl_showacceleration_z"))
4990                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
4991         else
4992                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
4993         acc_prevspeed = pmove_vel;
4994         acc_prevtime = time;
4995
4996         f = bound(0, f * 10, 1);
4997         acc_avg = acc_avg * (1 - f) + acceleration * f;
4998         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
4999         if (acceleration == 0)
5000                 return;
5001
5002         pos = top - sz/2 * eY + (cvar("cl_showacceleration_position") * vid_conheight) * eY;
5003
5004         sz = cvar("cl_showacceleration_size");
5005         scale = cvar("cl_showacceleration_scale");
5006         alpha = cvar("cl_showacceleration_alpha");
5007         if (cvar("cl_showacceleration_color_custom"))
5008                 rgb = stov(cvar_string("cl_showacceleration_color"));
5009         else {
5010                 if (acceleration < 0)
5011                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
5012                 else
5013                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
5014         }
5015
5016         if (acceleration > 0)
5017                 HUD_Panel_DrawProgressBar(pos, 0, acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
5018         else
5019                 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);
5020 }
5021
5022 void HUD_Reset (void)
5023 {
5024         // reset gametype specific icons
5025         if(gametype == GAME_KEYHUNT)
5026                 HUD_Mod_KH_Reset();
5027         else if(gametype == GAME_CTF)
5028                 HUD_Mod_CTF_Reset();
5029 }
5030
5031 #define HUD_DrawPanel(id)\
5032 switch (id) {\
5033         case (HUD_PANEL_RADAR):\
5034                 HUD_Radar(); break;\
5035         case (HUD_PANEL_WEAPONS):\
5036                 HUD_Weapons(); break;\
5037         case (HUD_PANEL_AMMO):\
5038                 HUD_Ammo(); break;\
5039         case (HUD_PANEL_POWERUPS):\
5040                 HUD_Powerups(); break;\
5041         case (HUD_PANEL_HEALTHARMOR):\
5042                 HUD_HealthArmor(); break;\
5043         case (HUD_PANEL_NOTIFY):\
5044                 HUD_Notify(); break;\
5045         case (HUD_PANEL_TIMER):\
5046                 HUD_Timer(); break;\
5047         case (HUD_PANEL_SCORE):\
5048                 HUD_Score(); break;\
5049         case (HUD_PANEL_RACETIMER):\
5050                 HUD_RaceTimer(); break;\
5051         case (HUD_PANEL_VOTE):\
5052                 HUD_VoteWindow(); break;\
5053         case (HUD_PANEL_MODICONS):\
5054                 HUD_ModIcons(); break;\
5055         case (HUD_PANEL_PRESSEDKEYS):\
5056                 HUD_DrawPressedKeys(); break;\
5057         case (HUD_PANEL_CHAT):\
5058                 HUD_Chat(); break;\
5059         case (HUD_PANEL_ENGINEINFO):\
5060                 HUD_EngineInfo(); break;\
5061         case (HUD_PANEL_INFOMESSAGES):\
5062                  HUD_InfoMessages(); break;\
5063 } ENDS_WITH_CURLY_BRACE
5064
5065 void HUD_Main (void)
5066 {
5067         float i;
5068         // global hud alpha fade
5069         if(menu_enabled == 1)
5070                 hud_fade_alpha = 1;
5071         else
5072                 hud_fade_alpha = (1 - autocvar__menu_alpha);
5073
5074         if(scoreboard_fade_alpha)
5075                 hud_fade_alpha = (1 - scoreboard_fade_alpha);
5076
5077         if(intermission == 2) // no hud during mapvote
5078                 hud_fade_alpha = 0;
5079         else if(autocvar__menu_alpha == 0 && scoreboard_fade_alpha == 0)
5080                 hud_fade_alpha = 1;
5081
5082         hud_fontsize = HUD_GetFontsize("hud_fontsize");
5083
5084         if(!autocvar__hud_configure && !hud_fade_alpha)
5085                 return;
5086
5087         // Drawing stuff
5088         if (hud_skin_path != cvar_string("hud_skin"))
5089         {
5090                 if (hud_skin_path)
5091                         strunzone(hud_skin_path);
5092                 hud_skin_path = strzone(strcat("gfx/hud/", cvar_string("hud_skin")));
5093         }
5094
5095         // HUD configure visible grid
5096         if(autocvar__hud_configure && autocvar_hud_configure_grid && autocvar_hud_configure_grid_alpha)
5097         {
5098                 // x-axis
5099                 for(i = 0; i < 1/bound(0.005, autocvar_hud_configure_grid_xsize, 0.2); ++i)
5100                 {
5101                         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);
5102                 }
5103                 // y-axis
5104                 for(i = 0; i < 1/bound(0.005, autocvar_hud_configure_grid_ysize, 0.2); ++i)
5105                 {
5106                         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);
5107                 }
5108         }
5109
5110         // draw the dock
5111         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
5112         {
5113                 float f;
5114                 vector color;
5115                 float hud_dock_color_team = cvar("hud_dock_color_team");
5116                 if((teamplay) && hud_dock_color_team) {
5117                         f = stof(getplayerkey(player_localentnum - 1, "colors"));
5118                         color = colormapPaletteColor(mod(f, 16), 1) * hud_dock_color_team;
5119                 }
5120                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
5121                         color = '1 0 0' * hud_dock_color_team;
5122                 }
5123                 else
5124                 {
5125                         string hud_dock_color = cvar_string("hud_dock_color");
5126                         if(hud_dock_color == "shirt") {
5127                                 f = stof(getplayerkey(player_localentnum - 1, "colors"));
5128                                 color = colormapPaletteColor(floor(f / 16), 0);
5129                         }
5130                         else if(hud_dock_color == "pants") {
5131                                 f = stof(getplayerkey(player_localentnum - 1, "colors"));
5132                                 color = colormapPaletteColor(mod(f, 16), 1);
5133                         }
5134                         else
5135                                 color = stov(hud_dock_color);
5136                 }
5137
5138                 string pic;
5139                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
5140                 if(precache_pic(pic) == "") {
5141                         pic = strcat(hud_skin_path, "/dock_medium");
5142                         if(precache_pic(pic) == "") {
5143                                 pic = "gfx/hud/default/dock_medium";
5144                         }
5145                 }
5146                 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...
5147         }
5148
5149         // cache the panel order into the panel_order array
5150         if(autocvar__hud_panelorder != hud_panelorder_prev) {
5151                 if(hud_panelorder_prev)
5152                         strunzone(hud_panelorder_prev);
5153                 hud_panelorder_prev = strzone(autocvar__hud_panelorder);
5154                 tokenize_console(autocvar__hud_panelorder);
5155                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
5156                         panel_order[i] = stof(argv(i));
5157                 }
5158         }
5159         // draw panels in order specified by panel_order array
5160         for(i = HUD_PANEL_NUM - 1; i >= 0; --i) {
5161                 if(i != HUD_PANEL_CHAT || !autocvar__con_chat_maximized) // don't draw maximized chat panel twice!
5162                         HUD_DrawPanel(panel_order[i]);
5163         }
5164
5165         // draw chat panel on top if it is maximized
5166         if(autocvar__con_chat_maximized)
5167                 HUD_Chat(); // HUD_DrawPanel(HUD_PANEL_CHAT);
5168
5169         // TODO hud_'ify these
5170         if (cvar("cl_showspeed"))
5171                 HUD_ShowSpeed();
5172         if (cvar("cl_showacceleration"))
5173                 HUD_ShowAcceleration();
5174
5175         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)
5176                 localcmd("cmd selectteam auto; cmd join\n");
5177
5178         hud_configure_prev = autocvar__hud_configure;
5179
5180         if (!autocvar__hud_configure) // hud config mode disabled, enable normal alpha stuff again
5181                 if (menu_enabled)
5182                         menu_enabled = 0;
5183 }