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