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