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