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