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