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