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