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