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