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