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