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