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