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