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