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