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