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