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