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