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