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