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