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