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