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