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