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