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