]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
Merge branch 'master' into fruitiex/newpanelhud
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qc
1 /*
2 ==================
3 Misc HUD functions
4 ==================
5 */
6
7 // a border picture is a texture containing nine parts:
8 //   1/4 width: left part
9 //   1/2 width: middle part (stretched)
10 //   1/4 width: right part
11 // divided into
12 //   1/4 height: top part
13 //   1/2 height: middle part (stretched)
14 //   1/4 height: bottom part
15 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
16 {
17         vector dX, dY;
18         vector width, height;
19         vector bW, bH;
20         //pic = draw_UseSkinFor(pic);
21         width = eX * theSize_x;
22         height = eY * theSize_y;
23         if(theSize_x <= theBorderSize_x * 2)
24         {
25                 // not wide enough... draw just left and right then
26                 bW = eX * (0.25 * theSize_x / (theBorderSize_x * 2));
27                 if(theSize_y <= theBorderSize_y * 2)
28                 {
29                         // not high enough... draw just corners
30                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
31                         drawsubpic(theOrigin,                 width * 0.5 + height * 0.5, pic, '0 0 0',           bW + bH, theColor, theAlpha, 0);
32                         drawsubpic(theOrigin + width   * 0.5, width * 0.5 + height * 0.5, pic, eX - bW,           bW + bH, theColor, theAlpha, 0);
33                         drawsubpic(theOrigin + height  * 0.5, width * 0.5 + height * 0.5, pic, eY - bH,           bW + bH, theColor, theAlpha, 0);
34                         drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
35                 }
36                 else
37                 {
38                         dY = theBorderSize_x * eY;
39                         drawsubpic(theOrigin,                             width * 0.5          +     dY, pic, '0 0    0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
40                         drawsubpic(theOrigin + width * 0.5,               width * 0.5          +     dY, pic, '0 0    0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
41                         drawsubpic(theOrigin                        + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0',           '0 0.5  0' + bW, theColor, theAlpha, 0);
42                         drawsubpic(theOrigin + width * 0.5          + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0' + eX - bW, '0 0.5  0' + bW, theColor, theAlpha, 0);
43                         drawsubpic(theOrigin               + height - dY, width * 0.5          +     dY, pic, '0 0.75 0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
44                         drawsubpic(theOrigin + width * 0.5 + height - dY, width * 0.5          +     dY, pic, '0 0.75 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
45                 }
46         }
47         else
48         {
49                 if(theSize_y <= theBorderSize_y * 2)
50                 {
51                         // not high enough... draw just top and bottom then
52                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
53                         dX = theBorderSize_x * eX;
54                         drawsubpic(theOrigin,                                         dX + height * 0.5, pic, '0    0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
55                         drawsubpic(theOrigin + dX,                        width - 2 * dX + height * 0.5, pic, '0.25 0 0',           '0.5  0 0' + bH, theColor, theAlpha, 0);
56                         drawsubpic(theOrigin + width - dX,                            dX + height * 0.5, pic, '0.75 0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
57                         drawsubpic(theOrigin              + height * 0.5,             dX + height * 0.5, pic, '0    0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
58                         drawsubpic(theOrigin + dX         + height * 0.5, width - 2 * dX + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5  0 0' + bH, theColor, theAlpha, 0);
59                         drawsubpic(theOrigin + width - dX + height * 0.5,             dX + height * 0.5, pic, '0.75 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
60                 }
61                 else
62                 {
63                         dX = theBorderSize_x * eX;
64                         dY = theBorderSize_x * eY;
65                         drawsubpic(theOrigin,                                        dX          +     dY, pic, '0    0    0', '0.25 0.25 0', theColor, theAlpha, 0);
66                         drawsubpic(theOrigin                  + dX,      width - 2 * dX          +     dY, pic, '0.25 0    0', '0.5  0.25 0', theColor, theAlpha, 0);
67                         drawsubpic(theOrigin          + width - dX,                  dX          +     dY, pic, '0.75 0    0', '0.25 0.25 0', theColor, theAlpha, 0);
68                         drawsubpic(theOrigin          + dY,                          dX + height - 2 * dY, pic, '0    0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
69                         drawsubpic(theOrigin          + dY         + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5  0.5  0', theColor, theAlpha, 0);
70                         drawsubpic(theOrigin          + dY + width - dX,             dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
71                         drawsubpic(theOrigin + height - dY,                          dX          +     dY, pic, '0    0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
72                         drawsubpic(theOrigin + height - dY         + dX, width - 2 * dX          +     dY, pic, '0.25 0.75 0', '0.5  0.25 0', theColor, theAlpha, 0);
73                         drawsubpic(theOrigin + height - dY + width - dX,             dX          +     dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
74                 }
75         }
76 }
77
78 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
79 void drawpic_skin(vector pos, string pic, vector sz, vector color, float alpha, float drawflag) {
80         drawpic(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, color, alpha, drawflag);
81 }
82
83 void drawpic_skin_expanding(vector pos, string pic, vector sz, vector rgb, float alpha, float flag, float fadelerp) {
84         drawpic_expanding(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, rgb, alpha, flag, fadelerp);
85 }
86
87 // return HUD background color
88 vector HUD_GetBgColor()
89 {
90         vector color;
91         if (teamplay)
92                 GetTeamRGB(myteam) * hud_color_bg_team;
93         else {
94                 // allow custom HUD colors in non-teamgames
95                 color_x = cvar("hud_color_bg_r");
96                 color_y = cvar("hud_color_bg_g");
97                 color_z = cvar("hud_color_bg_b");
98         }
99         return color;
100 }
101
102 // return accuracy text color
103 vector HUD_AccuracyColor(float accuracy)
104 {
105         vector rgb;
106         float yellow_accuracy = cvar("hud_weaponicons_accuracy_yellow"); // value at which this function returns yellow
107         if(accuracy >= 100) {
108                 rgb_x = 0;
109                 rgb_y = 1;
110         }
111         else if(accuracy > yellow_accuracy) {
112                 rgb_x = 1 - (accuracy-yellow_accuracy)/(100-yellow_accuracy); // red value between 1 -> 0
113                 rgb_y = 1;
114         }
115         else {
116                 rgb_x = 1;
117                 rgb_y = accuracy/yellow_accuracy; // green value between 0 -> 1
118         }
119         rgb_z = 0;
120         return rgb;
121 }
122
123 // draw number in the XSCALE font
124 void HUD_DrawXNum (vector pos, float num, float digits, float showsign, float lettersize, vector rgb, float highlighted, float stroke, float alpha, float dflags)
125 {
126         float l, i;
127         string str, tmp, l_length;
128         float minus, plus;
129         vector vsize, num_color;
130
131         vsize_x = vsize_y = lettersize;
132         vsize_z = 0;
133
134         // showsign 1: always prefix with minus sign (useful in race distribution display)
135         // showsign 2: always prefix with plus sign (useful in race distribution display)
136         // showsign 3: prefix with minus sign if negative, plus sign if positive (useful in score distribution display)
137
138         if((showsign == 2 && num >= 0) || (num > 0 && showsign == 3))
139         {
140                 plus = true;
141                 pos_x -= lettersize;
142         } else
143                 plus = false;
144
145         if(num < 0 || (num < 0 && showsign == 3) || (showsign == 1 && num <= 0))
146         {
147                 minus = true;
148                 num = -num;
149                 pos_x -= lettersize;
150         } else
151                 minus = false;
152
153         if(digits < 0)
154         {
155                 tmp = ftos(num);
156                 digits = -digits;
157                 str = strcat(substring("0000000000", 0, digits - strlen(tmp)), tmp);
158         } else
159                 str = ftos(num);
160
161         l = strlen(str);
162         l_length = ftos(l);
163
164         if(l > digits)
165         {
166                 str = substring(str, l-digits, 999);
167                 l = strlen(str);
168         } else if(l < digits)
169                 pos_x += (digits-l) * lettersize;
170
171         if (highlighted == 1) {
172                 vector hl_size;
173                 hl_size_x = vsize_x * l + vsize_x * 0.2;
174                 hl_size_y = vsize_y * 1.1;
175                 hl_size_z = 0;
176                 if(minus)
177                         hl_size_x = hl_size_x + vsize_x;
178
179                 vector hl_pos;
180                 hl_pos_x = pos_x - lettersize/10;
181                 hl_pos_y = pos_y - lettersize/20;
182                 hl_pos_z = 0;
183
184                 drawpic_skin(hl_pos, strcat("highlight_", l_length), hl_size, '1 1 1', alpha, dflags);
185         }
186
187         if (stroke == 1)
188                 num_color = '1 1 1';
189         else
190                 num_color = rgb;
191
192         if(minus)
193         {
194                 if (stroke == 1)
195                         drawpic_skin(pos, "num_minus_stroke", vsize, rgb, alpha, dflags);
196                 drawpic_skin(pos, "num_minus", vsize, num_color, alpha, dflags);
197                 pos_x += lettersize;
198         } else if(plus)
199         {
200                 if (stroke == 1)
201                         drawpic_skin(pos, "num_plus_stroke", vsize, rgb, alpha, dflags);
202                 drawpic_skin(pos, "num_plus", vsize, num_color, alpha, dflags);
203                 pos_x += lettersize;
204         }
205
206         for(i = 0; i < l; ++i)
207         {
208                 tmp = substring(str, i, 1);
209                 if (stroke == 1)
210                         drawpic_skin(pos, strcat("num_", tmp, "_stroke"), vsize, rgb, alpha, dflags);
211                 drawpic_skin(pos, strcat("num_", tmp), vsize, num_color, alpha, dflags);
212                 pos_x += lettersize;
213         }
214 }
215
216 // color the number differently based on how big it is (used in the health/armor panel)
217 void HUD_DrawXNum_Colored (vector pos, float x, float digits, float lettersize, float alpha)
218 {
219         vector color;
220         if(x > 200) {
221                 color_x = 0;
222                 color_y = 1;
223                 color_z = 0;
224         }
225         else if(x > 150) {
226                 color_x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
227                 color_y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
228                 color_z = 0;
229         }
230         else if(x > 100) {
231                 color_x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
232                 color_y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
233                 color_z = 1 - (x-100)*0.02; // blue value between 1 -> 0
234         }
235         else if(x > 50) {
236                 color_x = 1;
237                 color_y = 1;
238                 color_z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
239         }
240         else if(x > 20) {
241                 color_x = 1;
242                 color_y = (x-20)*90/27/100; // green value between 0 -> 1
243                 color_z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
244         }
245         else {
246                 color_x = 1;
247                 color_y = 0;
248                 color_z = 0;
249         }
250         HUD_DrawXNum(pos, x, digits, 0, lettersize, color, 0, 0, alpha, DRAWFLAG_NORMAL);
251 }
252
253 float stringwidth_colors(string s, vector theSize)
254 {
255         return stringwidth(s, TRUE, theSize);
256 }
257
258 float stringwidth_nocolors(string s, vector theSize)
259 {
260         return stringwidth(s, FALSE, theSize);
261 }
262
263 #define CENTERPRINT_MAX_LINES 30
264 string centerprint_messages[CENTERPRINT_MAX_LINES];
265 float centerprint_width[CENTERPRINT_MAX_LINES];
266 vector centerprint_start;
267 float centerprint_expire;
268 float centerprint_num;
269 float centerprint_offset_hint;
270 vector centerprint_fontsize;
271
272 void centerprint(string strMessage)
273 {
274         float i, j, n, hcount;
275         string s;
276
277         centerprint_fontsize = HUD_GetFontsize("scr_centersize");
278
279         centerprint_expire = min(centerprint_expire, time); // if any of the returns happens, this message will fade out
280
281         if(cvar("scr_centertime") <= 0)
282                 return;
283
284         if(strMessage == "")
285                 return;
286
287         // strip trailing newlines
288         j = strlen(strMessage) - 1;
289         while(substring(strMessage, j, 1) == "\n" && j >= 0)
290                 j = j - 1;
291         strMessage = substring(strMessage, 0, j + 1);
292
293         if(strMessage == "")
294                 return;
295
296         // strip leading newlines and remember them, they are a hint that the message should be lower on the screen
297         j = 0;
298         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
299                 j = j + 1;
300         strMessage = substring(strMessage, j, strlen(strMessage) - j);
301         centerprint_offset_hint = j;
302
303         if(strMessage == "")
304                 return;
305
306         // if we get here, we have a message. Initialize its height.
307         centerprint_num = 0;
308
309         n = tokenizebyseparator(strMessage, "\n");
310         i = hcount = 0;
311         for(j = 0; j < n; ++j)
312         {
313                 getWrappedLine_remaining = argv(j);
314                 while(getWrappedLine_remaining)
315                 {
316                         s = getWrappedLine(vid_conwidth * 0.75, centerprint_fontsize, stringwidth_colors);
317                         if(centerprint_messages[i])
318                                 strunzone(centerprint_messages[i]);
319                         centerprint_messages[i] = strzone(s);
320                         centerprint_width[i] = stringwidth(s, TRUE, centerprint_fontsize);
321                         ++i;
322
323                         // half height for empty lines looks better
324                         if(s == "")
325                                 hcount += 0.5;
326                         else
327                                 hcount += 1;
328
329                         if(i >= CENTERPRINT_MAX_LINES)
330                                 break;
331                 }
332         }
333
334         float h, havail;
335         h = centerprint_fontsize_y*hcount;
336
337         havail = vid_conheight;
338         if(cvar("con_chatpos") < 0)
339                 havail -= (-cvar("con_chatpos") + cvar("con_chat")) * cvar("con_chatsize"); // avoid overlapping chat
340         if(havail > vid_conheight - 70)
341                 havail = vid_conheight - 70; // avoid overlapping HUD
342
343         centerprint_start_x = 0;
344
345 #if 0
346         float forbiddenmin, forbiddenmax, allowedmin, allowedmax, preferred;
347
348         // here, the centerprint would cover the crosshair. REALLY BAD.
349         forbiddenmin = vid_conheight * 0.5 - h - 16;
350         forbiddenmax = vid_conheight * 0.5 + 16;
351
352         allowedmin = scoreboard_bottom;
353         allowedmax = havail - h;
354         preferred = (havail - h)/2;
355
356
357         // possible orderings (total: 4! / 4 = 6)
358         //  allowedmin allowedmax forbiddenmin forbiddenmax
359         //  forbiddenmin forbiddenmax allowedmin allowedmax
360         if(allowedmax < forbiddenmin || allowedmin > forbiddenmax)
361         {
362                 // forbidden doesn't matter in this case
363                 centerprint_start_y = bound(allowedmin, preferred, allowedmax);
364         }
365         //  allowedmin forbiddenmin allowedmax forbiddenmax
366         else if(allowedmin < forbiddenmin && allowedmax < forbiddenmax)
367         {
368                 centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
369         }
370         //  allowedmin forbiddenmin forbiddenmax allowedmax
371         else if(allowedmin < forbiddenmin)
372         {
373                 // make sure the forbidden zone is not covered
374                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
375                         centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
376                 else
377                         centerprint_start_y = bound(forbiddenmax, preferred, allowedmin);
378         }
379         //  forbiddenmin allowedmin allowedmax forbiddenmax
380         else if(allowedmax < forbiddenmax)
381         {
382                 // it's better to leave the allowed zone (overlap with scoreboard) than
383                 // to cover the forbidden zone (crosshair)
384                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
385                         centerprint_start_y = forbiddenmax;
386                 else
387                         centerprint_start_y = forbiddenmin;
388         }
389         //  forbiddenmin allowedmin forbiddenmax allowedmax
390         else
391         {
392                 centerprint_start_y = bound(forbiddenmax, preferred, allowedmax);
393         }
394 #else
395         centerprint_start_y =
396                 min(
397                         max(
398                                 max(scoreboard_bottom, vid_conheight * 0.5 + 16),
399                                 (havail - h)/2
400                         ),
401                         havail - h
402                 );
403 #endif
404
405         centerprint_num = i;
406         centerprint_expire = time + cvar("scr_centertime");
407 }
408
409 void HUD_DrawCenterPrint (void)
410 {
411         float i;
412         vector pos;
413         string ts;
414         float a;
415
416         //if(time > centerprint_expire)
417         //      return;
418
419         //a = bound(0, 1 - 2 * (time - centerprint_expire), 1);
420         a = bound(0, 1 - 4 * (time - centerprint_expire), 1);
421         //sz = 1.2 / (a + 0.2);
422
423         if(a <= 0)
424                 return;
425
426         pos = centerprint_start;
427         for (i=0; i<centerprint_num; i = i + 1)
428         {
429                 pos_x = (vid_conwidth - centerprint_width[i]) * 0.5;
430                 ts = centerprint_messages[i];
431                 if (ts != "")
432                 {
433                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
434                         drawcolorcodedstring(pos, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
435                         //  - '0 0.5 0' * (sz - 1) * centerprint_fontsize_x - '0.5 0 0' * (sz - 1) * centerprint_width[i] * centerprint_fontsize_y, centerprint_fontsize * sz
436                         pos_y = pos_y + centerprint_fontsize_y;
437                 }
438                 else
439                         // half height for empty lines looks better
440                         pos_y = pos_y + centerprint_fontsize_y * 0.5;
441         }
442 }
443
444 void drawstringright(vector position, string text, vector scale, vector rgb, float alpha, float flag)
445 {
446         position_x -= 2 / 3 * strlen(text) * scale_x;
447         drawstring(position, text, scale, rgb, alpha, flag);
448 }
449
450 void drawstringcenter(vector position, string text, vector scale, vector rgb, float alpha, float flag)
451 {
452         position_x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * scale_x);
453         drawstring(position, text, scale, rgb, alpha, flag);
454 }
455
456 // return the string of the given race place
457 string race_PlaceName(float pos) {
458         if(pos == 1)
459                 return "1st";
460         else if(pos == 2)
461                 return "2nd";
462         else if(pos == 3)
463                 return "3rd";
464         else
465                 return strcat(ftos(pos), "th");
466 }
467
468 // return the string of the onscreen race timer
469 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
470 {
471         string col;
472         string timestr;
473         string cpname;
474         string lapstr;
475         lapstr = "";
476
477         if(histime == 0) // goal hit
478         {
479                 if(mytime > 0)
480                 {
481                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
482                         col = "^1";
483                 }
484                 else if(mytime == 0)
485                 {
486                         timestr = "+0.0";
487                         col = "^3";
488                 }
489                 else
490                 {
491                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
492                         col = "^2";
493                 }
494
495                 if(lapdelta > 0)
496                 {
497                         lapstr = strcat(" (-", ftos(lapdelta), "L)");
498                         col = "^2";
499                 }
500                 else if(lapdelta < 0)
501                 {
502                         lapstr = strcat(" (+", ftos(-lapdelta), "L)");
503                         col = "^1";
504                 }
505         }
506         else if(histime > 0) // anticipation
507         {
508                 if(mytime >= histime)
509                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
510                 else
511                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
512                 col = "^3";
513         }
514         else
515                 col = "^7";
516
517         if(cp == 254)
518                 cpname = "Start line";
519         else if(cp == 255)
520                 cpname = "Finish line";
521         else if(cp)
522                 cpname = strcat("Intermediate ", ftos(cp));
523         else
524                 cpname = "Finish line";
525
526         if(histime < 0)
527                 return strcat(col, cpname);
528         else if(hisname == "")
529                 return strcat(col, cpname, " (", timestr, ")");
530         else
531                 return strcat(col, cpname, " (", timestr, " ", strcat(hisname, col, lapstr), ")");
532 }
533
534 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
535 float race_CheckName(string net_name) {
536         float i;
537         for (i=RANKINGS_CNT-1;i>=0;--i)
538                 if(grecordholder[i] == net_name)
539                         return i+1;
540         return 0;
541 }
542
543 /*
544 ==================
545 HUD panels
546 ==================
547 */
548
549 string HUD_Panel_GetName(float id)
550 {
551         switch(id) {
552                 case 0: return "weaponicons"; break;
553                 case 1: return "inventory"; break;
554                 case 2: return "powerups"; break;
555                 case 3: return "healtharmor"; break;
556                 case 4: return "notify"; break;
557                 case 5: return "timer"; break;
558                 case 6: return "radar"; break;
559                 case 7: return "score"; break;
560                 case 8: return "racetimer"; break;
561                 case 9: return "vote"; break;
562                 case 10: return "modicons"; break;
563                 case 11: return "pressedkeys"; break;
564                 default: return "";
565         }
566 }
567
568 // Save the config
569 void HUD_Panel_ExportCfg(string cfgname)
570 {
571         float fh;
572         fh = fopen(strcat("hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg"), FILE_WRITE);
573         if(fh >= 0)
574         {
575                 fputs(fh, strcat("seta hud_skin \"", cvar_string("hud_skin"), "\"", "\n"));
576                 fputs(fh, strcat("seta hud_bg \"", cvar_string("hud_bg"), "\"", "\n"));
577                 fputs(fh, strcat("seta hud_bg_color \"", cvar_string("hud_bg_color"), "\"", "\n"));
578                 fputs(fh, strcat("seta hud_bg_alpha ", ftos(cvar("hud_bg_alpha")), "\n"));
579                 fputs(fh, strcat("seta hud_bg_border ", ftos(cvar("hud_bg_border")), "\n"));
580                 fputs(fh, strcat("seta hud_fg_alpha ", ftos(cvar("hud_fg_alpha")), "\n"));
581                 fputs(fh, "\n");
582
583                 fputs(fh, strcat("seta hud_dock \"", cvar_string("hud_dock"), "\"", "\n"));
584                 fputs(fh, strcat("seta hud_dock_color \"", cvar_string("hud_dock_color"), "\"", "\n"));
585                 fputs(fh, strcat("seta hud_dock_alpha ", ftos(cvar("hud_dock_alpha")), "\n"));
586                 fputs(fh, "\n");
587
588                 fputs(fh, strcat("seta hud_progressbar_alpha ", ftos(cvar("hud_progressbar_alpha")), "\n"));
589                 fputs(fh, strcat("seta hud_progressbar_strength_color \"", cvar_string("hud_progressbar_strength_color"), "\"", "\n"));
590                 fputs(fh, strcat("seta hud_progressbar_shield_color \"", cvar_string("hud_progressbar_shield_color"), "\"", "\n"));
591                 fputs(fh, strcat("seta hud_progressbar_health_color \"", cvar_string("hud_progressbar_health_color"), "\"", "\n"));
592                 fputs(fh, strcat("seta hud_progressbar_armor_color \"", cvar_string("hud_progressbar_armor_color"), "\"", "\n"));
593                 fputs(fh, strcat("seta hud_progressbar_fuel_color \"", cvar_string("hud_progressbar_fuel_color"), "\"", "\n"));
594                 fputs(fh, strcat("seta hud_progressbar_nexball_color \"", cvar_string("hud_progressbar_nexball_color"), "\"", "\n"));
595                 fputs(fh, "\n");
596
597                 // common cvars for all panels
598                 float i;
599                 for (i = 0; i < panel_cnt; ++i)
600                 {
601                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), " ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i)))), "\n"));
602                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_pos \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_pos")), "\"", "\n"));
603                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_size \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_size")), "\"", "\n"));
604                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg")), "\"", "\n"));
605                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_color \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_color")), "\"", "\n"));
606                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_bg_alpha"))), "\n"));
607                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_border ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_bg_border"))), "\n"));
608                         switch(i) {
609                                 case 0:
610                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_accuracy_height ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_accuracy_height"))), "\n"));
611                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_accuracy_yellow ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_accuracy_yellow"))), "\n"));
612                                         break;
613                                 case 1:
614                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_onlycurrent ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_onlycurrent"))), "\n"));
615                                         break;
616                                 case 2:
617                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
618                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_mirror ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
619                                         break;
620                                 case 3:
621                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
622                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_mirror ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
623                                         break;
624                                 case 9:
625                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha"))), "\n"));
626                                         break;
627                         }
628                         fputs(fh, "\n");
629                 }
630
631                 print("^2Successfully exported to hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg! (Note: It's saved in data/data/)\n");
632         }
633         fclose(fh);
634 }
635
636 vector HUD_Panel_CheckLimitSize(float id, vector mySize)
637 {
638         switch(id) {
639                 case 0: 
640                         mySize_x = max(mySize_y * (1/10), mySize_x); // at least 1/10 * height
641                         mySize_y = max(mySize_x * (1/26), mySize_y); // at least 1/26 * width
642                         break;
643                 case 1: 
644                         mySize_x = max(mySize_y * 0.7, mySize_x); // at least 0.7 * height
645                         break;
646                 //case 2: 
647                 //      mySize_x = max(mySize_y * 1.5, mySize_x); // at least 1.5 * height
648                 //      break;
649                 //case 3: 
650                 //      mySize_x = max(mySize_y * 2, mySize_x); // at least 2 * height
651                 //      break;
652                 case 5: 
653                         mySize_y = (1/4.1) * mySize_x; // 1/4.1 * width, trial and error...
654                         break;
655                 case 7: 
656                         mySize_y = (1/4) * mySize_x; // 1/4 * width
657                         break;
658                 case 8: 
659                         mySize_y = (1/4) * mySize_x; // 1/4 * width
660                         break;
661                 case 9: 
662                         mySize_y = (1/4) * mySize_x; // 1/4 * width
663                         break;
664                 case 10: 
665                         mySize_y = (1/2) * mySize_x; // 1/2 * width
666                         break;
667                 case 11: 
668                         mySize_y = 0.5898 * mySize_x; // 0.5898 * width, reason: bg has weird dimensions...
669                         break;
670         }
671         return mySize;
672 }
673
674 // return active status of panel
675 float HUD_Panel_CheckActive(float id)
676 {
677         if (cvar_or(strcat("hud_", HUD_Panel_GetName(id)), 1))
678                 return 1;
679         return 0;
680 }
681
682 // return size of given panel
683 vector HUD_Panel_GetSize(float id)
684 {
685         vector mySize;
686         mySize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
687
688         mySize = eX * mySize_x * vid_conwidth + eY * mySize_y * vid_conheight;
689
690         return mySize;
691 }
692
693 // return pos of given panel
694 vector HUD_Panel_GetPos(float id)
695 {
696         vector pos;
697         pos = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_pos")));
698
699         pos = eX * pos_x * vid_conwidth + eY * pos_y * vid_conheight;
700
701         if (pos_x < 0)
702                 pos_x = vid_conwidth + pos_x;
703         if (pos_y < 0)
704                 pos_y = vid_conheight + pos_y;
705         return pos;
706 }
707
708 float HUD_Panel_GetBorder(float id)
709 {
710         float border;
711         border = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_border"));
712         if(!border)
713                 border = cvar("hud_bg_border");
714         return border;
715 }
716
717 // draw the background/borders
718 void HUD_Panel_DrawBg(float id, vector pos, vector mySize)
719 {
720         if(!hud_configure && cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg")) == "0")
721                 return;
722
723         string bg;
724         bg = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg"));
725         if(bg == "")
726                 bg = cvar_string("hud_bg");
727
728         if(bg != "0")
729         {
730                 float border;
731                 border = max(0.0000001, HUD_Panel_GetBorder(id)); // draw_BorderPicture does not like border = 0
732
733                 vector color;
734                 if(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color")) != "")
735                         color = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color")));
736                 else
737                         color = stov(cvar_string("hud_bg_color"));
738
739                 float alpha;
740                 alpha = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_alpha"));
741                 if(!alpha)
742                         alpha = cvar("hud_bg_alpha");
743                 if(hud_configure)
744                         alpha = max(cvar("hud_configure_bg_minalpha"), alpha);
745
746                 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));
747         }
748 }
749
750 vector HUD_Panel_GetProgressBarColor(string item)
751 {
752         return stov(cvar_string(strcat("hud_progressbar_", item, "_color")));
753 }
754
755 float resizeCorner; // 1 = topleft, 2 = topright, 3 = bottomleft, 4 = bottomright
756 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
757 vector HUD_Panel_CheckResize(float id, vector myPos, vector mySize)
758 {
759         float i;
760
761         vector myTarget;
762         myTarget = mySize;
763
764         vector targPos;
765         vector targSize;
766         vector myCenter;
767         vector targCenter;
768
769         for (i = 0; i < panel_cnt; ++i) {
770                 if(i == id || !HUD_Panel_CheckActive(i))
771                         continue;
772
773                 targPos = HUD_Panel_GetPos(i);
774                 targSize = HUD_Panel_GetSize(i);
775
776                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
777                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(id);
778
779                 if(myPos_y + mySize_y < targPos_y)
780                         continue;
781                 if(myPos_y > targPos_y + targSize_y)
782                         continue;
783
784                 if(myPos_x + mySize_x < targPos_x)
785                         continue;
786                 if(myPos_x > targPos_x + targSize_x)
787                         continue;
788
789                 // OK, there IS a collision.
790
791                 myCenter_x = myPos_x + 0.5 * mySize_x;
792                 myCenter_y = myPos_y + 0.5 * mySize_y;
793
794                 targCenter_x = targPos_x + 0.5 * targSize_x;
795                 targCenter_y = targPos_y + 0.5 * targSize_y;
796
797                 float k, y;
798                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y && resizeCorner != 1) // top left (of target panel)
799                 {
800                         if(myPos_x + mySize_x - targPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
801                                 myTarget_x = targPos_x - myPos_x;
802                         else // push it upwards
803                                 myTarget_y = targPos_y - myPos_y;
804                 }
805                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y && resizeCorner != 2) // top right
806                 {
807                         if(targPos_x + targSize_x - myPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
808                                 myTarget_x = targPos_x + targSize_x;
809                         else // push it upwards
810                                 myTarget_y = targPos_y - myPos_y;
811                 }
812                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
813                 {
814                         if(myPos_x + mySize_x - targPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
815                                 myTarget_x = targPos_x - myPos_x;
816                         else // push it downwards
817                                 myTarget_y = targPos_y + targSize_y;
818                 }
819                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
820                 {
821                         if(targPos_x + targSize_x - myPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
822                                 myTarget_x = targPos_x + targSize_x;
823                         else // push it downwards
824                                 myTarget_y = targPos_y + targSize_y;
825                 }
826         }
827
828         return myTarget;
829 }
830
831 // TODO: checkResize
832 float HUD_Panel_SetSize(float id, vector mySize)
833 {
834         float didntresize;
835
836         vector oldSize;
837         oldSize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
838
839         vector myPos;
840         myPos = HUD_Panel_GetPos(id);
841
842         // check for collisions
843         if(cvar("hud_configure_checkcollisions"))
844                 mySize = HUD_Panel_CheckResize(id, HUD_Panel_GetPos(id), mySize);
845
846         mySize_x = bound(0.025 * vid_conwidth, mySize_x, vid_conwidth - myPos_x);
847         mySize_y = bound(0.025 * vid_conheight, mySize_y, vid_conheight - myPos_y);
848
849         // cap against panel's own limits
850         mySize = HUD_Panel_CheckLimitSize(id, mySize);
851
852         //mySize_x = bound(0.025 * vid_conwidth, mySize_x, vid_conwidth);
853         //mySize_y = bound(0.025 * vid_conheight, mySize_y, vid_conheight);
854
855         if(cvar("hud_configure_grid"))
856         {
857                 mySize_x = floor(mySize_x/cvar("hud_configure_grid_x") + 0.5) * cvar("hud_configure_grid_x");
858                 mySize_y = floor(mySize_y/cvar("hud_configure_grid_y") + 0.5) * cvar("hud_configure_grid_y");
859         }
860
861         // TODO: is this needed?
862         // this is to check if (and how) SetPos should be called
863         if(mySize_x == oldSize_x && mySize_y == oldSize_y)
864                 didntresize = 3; // didnt resize either
865         else if(mySize_x == oldSize_x && mySize_y != oldSize_y)
866                 didntresize = 2; // resized Y
867         else if(mySize_y == oldSize_y && mySize_x != oldSize_x)
868                 didntresize = 1; // resized X
869
870         string s;
871         s = strcat(ftos(mySize_x/vid_conwidth), " ", ftos(mySize_y/vid_conheight));
872         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_size"), s);
873         return didntresize;
874 }
875
876 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
877 vector HUD_Panel_CheckMove(float id, vector myPos, vector mySize)
878 {
879         float i;
880
881         vector myTarget;
882         myTarget = myPos;
883
884         vector targPos;
885         vector targSize;
886         vector myCenter;
887         vector targCenter;
888
889         for (i = 0; i < panel_cnt; ++i) {
890                 if(i == id || !HUD_Panel_CheckActive(i))
891                         continue;
892
893                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
894                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(id);
895
896                 if(myPos_y + mySize_y < targPos_y)
897                         continue;
898                 if(myPos_y > targPos_y + targSize_y)
899                         continue;
900
901                 if(myPos_x + mySize_x < targPos_x)
902                         continue;
903                 if(myPos_x > targPos_x + targSize_x)
904                         continue;
905
906                 // OK, there IS a collision.
907
908                 myCenter_x = myPos_x + 0.5 * mySize_x;
909                 myCenter_y = myPos_y + 0.5 * mySize_y;
910
911                 targCenter_x = targPos_x + 0.5 * targSize_x;
912                 targCenter_y = targPos_y + 0.5 * targSize_y;
913
914                 float k, y;
915                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y) // top left (of the target panel)
916                 {
917                         if(myPos_x + mySize_x - targPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
918                                 myTarget_x = targPos_x - mySize_x;
919                         else // push it upwards
920                                 myTarget_y = targPos_y - mySize_y;
921                 }
922                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y) // top right
923                 {
924                         if(targPos_x + targSize_x - myPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
925                                 myTarget_x = targPos_x + targSize_x;
926                         else // push it upwards
927                                 myTarget_y = targPos_y - mySize_y;
928                 }
929                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
930                 {
931                         if(myPos_x + mySize_x - targPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
932                                 myTarget_x = targPos_x - mySize_x;
933                         else // push it downwards
934                                 myTarget_y = targPos_y + targSize_y;
935                 }
936                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
937                 {
938                         if(targPos_x + targSize_x - myPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
939                                 myTarget_x = targPos_x + targSize_x;
940                         else // push it downwards
941                                 myTarget_y = targPos_y + targSize_y;
942                 }
943         }
944
945         return myTarget;
946 }
947
948 void HUD_Panel_SetPos(float id, vector pos, float didntresize)
949 {
950         vector oldPos;
951         oldPos = HUD_Panel_GetPos(id);
952         if(didntresize == 2)
953                 pos_x = oldPos_x;
954         else if(didntresize == 1)
955                 pos_y = oldPos_y;
956         else if(didntresize == 3)
957                 return;
958
959         vector mySize;
960         mySize = HUD_Panel_GetSize(id);
961
962         if(cvar("hud_configure_checkcollisions"))
963                 pos = HUD_Panel_CheckMove(id, pos, mySize);
964
965         pos_x = bound(0, pos_x, vid_conwidth - mySize_x);
966         pos_y = bound(0, pos_y, vid_conheight - mySize_y);
967
968         if(cvar("hud_configure_grid"))
969         {
970                 pos_x = floor(pos_x/cvar("hud_configure_grid_x") + 0.5) * cvar("hud_configure_grid_x");
971                 pos_y = floor(pos_y/cvar("hud_configure_grid_y") + 0.5) * cvar("hud_configure_grid_y");
972         }
973
974         if (pos_x + 0.5 * mySize_x > 0.5 * vid_conwidth)
975                 pos_x = pos_x - vid_conwidth;
976         if (pos_y + 0.5 * mySize_y > 0.5 * vid_conheight)
977                 pos_y = pos_y - vid_conheight;
978
979         string s;
980         s = strcat(ftos(pos_x/vid_conwidth), " ", ftos(pos_y/vid_conheight));
981
982         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_pos"), s);
983 }
984
985 float mouseClicked;
986 float prevMouseClicked; // previous state
987 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
988 {
989         prevMouseClicked = mouseClicked;
990         if(nPrimary == K_MOUSE1)
991         {
992                 if(bInputType == 0) { // key pressed
993                         mouseClicked = 1;
994                         return true;
995                 }
996                 if(bInputType == 1) {// key released
997                         mouseClicked = 0;
998                         return true;
999                 }
1000         }
1001         return false;
1002 }
1003
1004 vector mousepos, mouseprevpos;
1005 vector panel_click_distance; // mouse cursor distance from the top left corner of the panel (saved only upon a click)
1006 vector panel_click_pos; // panel pos (saved only upon a click)
1007 vector panel_click_size; // panel size (saved only upon a click)
1008 float highlightedPanel;
1009 float highlightedAction; // 0 = nothing, 1 = move, 2 = resize
1010 void HUD_Panel_Mouse()
1011 {
1012         if(mouseClicked == 0) {
1013                 highlightedPanel = -1;
1014                 highlightedAction = 0;
1015         }
1016
1017         mousepos = mousepos + getmousepos();
1018
1019         mousepos_x = bound(0, mousepos_x, vid_conwidth);
1020         mousepos_y = bound(0, mousepos_y, vid_conheight);
1021
1022         drawpic(mousepos, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1023
1024         if(mouseClicked)
1025         {
1026                 float i, border;
1027                 vector panelPos;
1028                 vector panelSize;
1029
1030                 for(i = 0; i <= panel_cnt; ++i)
1031                 {
1032                         panelPos = HUD_Panel_GetPos(i);
1033                         panelSize = HUD_Panel_GetSize(i);
1034                         border = HUD_Panel_GetBorder(i);
1035                         if(prevMouseClicked == 0) {
1036                                 // move
1037                                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1038                                 {
1039                                         highlightedPanel = i;
1040                                         highlightedAction = 1;
1041                                 }
1042                                 // resize from topleft border
1043                                 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)
1044                                 {
1045                                         highlightedPanel = i;
1046                                         highlightedAction = 2;
1047                                         resizeCorner = 1;
1048                                 }
1049                                 // resize from topright border
1050                                 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)
1051                                 {
1052                                         highlightedPanel = i;
1053                                         highlightedAction = 2;
1054                                         resizeCorner = 2;
1055                                 }
1056                                 // resize from bottomleft border
1057                                 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)
1058                                 {
1059                                         highlightedPanel = i;
1060                                         highlightedAction = 2;
1061                                         resizeCorner = 3;
1062                                 }
1063                                 // resize from bottomright border
1064                                 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)
1065                                 {
1066                                         highlightedPanel = i;
1067                                         highlightedAction = 2;
1068                                         resizeCorner = 4;
1069                                 }
1070                         }
1071
1072                         if(highlightedPanel == i)
1073                         {
1074                                 if(prevMouseClicked == 0)
1075                                 {
1076                                         panel_click_distance = mousepos - panelPos;
1077                                         panel_click_pos = panelPos;
1078                                         panel_click_size = panelSize;
1079                                 }
1080
1081                                 if(highlightedAction == 1)
1082                                         HUD_Panel_SetPos(i, mousepos - panel_click_distance, 0);
1083                                 else if(highlightedAction == 2)
1084                                 {
1085                                         float didntresize; // panel too big/too small, dont resize (also has to affect moving)
1086                                         if(resizeCorner == 1) {
1087                                                 didntresize = HUD_Panel_SetSize(i, panelSize + mouseprevpos - mousepos);
1088                                                 HUD_Panel_SetPos(i, mousepos - panel_click_distance, didntresize);
1089                                         }
1090                                         if(resizeCorner == 2) {
1091                                                 didntresize = HUD_Panel_SetSize(i, eY * panel_click_size_y + eX * (mousepos_x - panelPos_x - (panel_click_distance_x - panel_click_size_x))
1092                                                                 + eY * (panel_click_distance_y + (panel_click_pos_y - mousepos_y)));
1093                                                 HUD_Panel_SetPos(i, eX * panelPos_x + eY * (mousepos_y - panel_click_distance_y), didntresize);
1094                                         }
1095                                         if(resizeCorner == 3) {
1096                                                 didntresize = HUD_Panel_SetSize(i, panelSize + eX * (mouseprevpos_x - mousepos_x) + eY * (mousepos_y - mouseprevpos_y));
1097                                                 HUD_Panel_SetPos(i, eX * (mousepos_x - panel_click_distance_x) + eY * panelPos_y, didntresize);
1098                                         }
1099                                         if(resizeCorner == 4) {
1100                                                 HUD_Panel_SetSize(i, mousepos - panelPos - (panel_click_distance - panel_click_size));
1101                                         }
1102                                 }
1103                         }
1104                 }
1105         }
1106         mouseprevpos = mousepos;
1107         prevMouseClicked = mouseClicked;
1108 }
1109
1110 /*void HUD_DrawDockEdge(float id, vector p1, vector p2, float target)
1111 {
1112         vector pos;
1113         vector size;
1114
1115         pos =
1116 */ // maybe one day, since this will be quite complicated
1117
1118 // Weapon icons (#0)
1119 //
1120 float weaponspace[10];
1121 void HUD_WeaponIcons_Clear()
1122 {
1123         float idx;
1124         for(idx = 0; idx < 10; ++idx)
1125                 weaponspace[idx] = 0;
1126 }
1127
1128 entity weaponorder[WEP_MAXCOUNT];
1129
1130 void weaponorder_swap(float i, float j, entity pass)
1131 {
1132         entity h;
1133         h = weaponorder[i];
1134         weaponorder[i] = weaponorder[j];
1135         weaponorder[j] = h;
1136 }
1137
1138 float weaponorder_cmp(float i, float j, entity pass)
1139 {
1140         float d, ii, ij;
1141         d = mod(weaponorder[i].impulse + 9, 10) - mod(weaponorder[j].impulse + 9, 10);
1142         if(d)
1143                 return d;
1144         d = weaponorder[i].weapon - weaponorder[j].weapon;
1145         return d;
1146 }
1147
1148 void HUD_WeaponIcons()
1149 {
1150         float id = 0;
1151         float alpha, height, accuracybar_height, stat_weapons; // "constants"
1152         vector pos, mySize, mysize, mypos, accuracy_color;
1153         float i, weapid, fade, weapon_stats, weapon_hit, weapon_damage, weapon_cnt; // variables
1154
1155         pos = HUD_Panel_GetPos(id);
1156         mySize = HUD_Panel_GetSize(id);
1157         accuracybar_height = cvar_or("hud_weaponicons_accuracy_height", 3);
1158
1159         stat_weapons = getstati(STAT_WEAPONS);
1160         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1161         {
1162                 self = get_weaponinfo(i);
1163                 if(self.weapons && (self.impulse >= 0) && (stat_weapons & self.weapons) || hud_configure)
1164                 {
1165                         weaponorder[weapon_cnt] = self;
1166                         ++weapon_cnt;
1167                 }
1168         }
1169         heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
1170
1171         HUD_Panel_DrawBg(id, pos, mySize);
1172         float padding;
1173         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1174         if(padding)
1175         {
1176                 pos += '1 1 0' * padding;
1177                 mySize -= '2 2 0' * padding;
1178         }
1179
1180         // hits
1181         weapon_stats = getstati(STAT_DAMAGE_HITS);
1182         weapon_number = weapon_stats & 63;
1183         weapon_hits[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1184         // fired
1185         weapon_stats = getstati(STAT_DAMAGE_FIRED);
1186         weapon_number = weapon_stats & 63;
1187         weapon_fired[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1188
1189         if(cvar_or("hud_weaponicons_fade", 1))
1190         {
1191                 fade = 3.2 - 2 * (time - weapontime);
1192                 fade = bound(0.7, fade, 1);
1193         }
1194         else
1195                 fade = 1;
1196
1197         HUD_WeaponIcons_Clear();
1198
1199         float rows, columns;
1200         rows = mySize_y/mySize_x;
1201         rows = bound(1, floor((sqrt(4 * (2/1) * rows * WEP_COUNT + rows * rows) + rows + 0.5) / 2), WEP_COUNT);
1202         //                               ^^^ weapon icon aspect goes here
1203
1204         columns = ceil(WEP_COUNT/rows);
1205         float row, column;
1206         for(i = 0; i < weapon_cnt; ++i)
1207         {
1208                 self = weaponorder[i];
1209                 if((self.weapons && (self.impulse >= 0) && (stat_weapons & self.weapons)) || hud_configure)
1210                 {
1211                         weapid = self.impulse;
1212
1213                         alpha = (self.weapon == activeweapon) ? 1 : 0.6;
1214
1215                         weapon_hit = weapon_hits[self.weapon-WEP_FIRST];
1216                         weapon_damage = weapon_fired[self.weapon-WEP_FIRST];
1217
1218                         if(self.weapon == activeweapon)
1219                                 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_alpha_fg, DRAWFLAG_NORMAL);
1220                         drawpic(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), strcat("gfx/weapons/weapon", self.netname), eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows), '1 1 1', fade * hud_alpha_fg, DRAWFLAG_NORMAL);
1221
1222                         if(cvar_or("hud_weaponicons_number", 1))
1223                                 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_alpha_fg, DRAWFLAG_NORMAL);
1224
1225                         // draw the weapon accuracy on the HUD
1226                         if(hud_accuracy_hud && !(gametype == GAME_RACE || gametype == GAME_CTS))
1227                         {
1228                                 if(weapon_damage)
1229                                         weapon_stats = floor(100 * weapon_hit / weapon_damage);
1230
1231                                 accuracy_color = HUD_AccuracyColor(weapon_stats);
1232                                 if(weapon_damage)
1233                                         drawpic_skin(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows) - '2 0 0' + eY * (mySize_y/rows - accuracybar_height), "accuracy_bar.tga", eX * mySize_x*(1/columns) + eY * accuracybar_height, accuracy_color, hud_alpha_fg, DRAWFLAG_NORMAL);
1234                         }
1235                 }
1236
1237                 ++row;
1238                 if(row >= rows)
1239                 {
1240                         row = 0;
1241                         ++column;
1242                 }
1243         }
1244
1245 }
1246
1247 // Inventory (#1)
1248 //
1249 float GetAmmoStat(float i)
1250 {
1251         switch(i)
1252         {
1253                 case 0: return STAT_SHELLS;
1254                 case 1: return STAT_NAILS;
1255                 case 2: return STAT_ROCKETS;
1256                 case 3: return STAT_CELLS;
1257                 case 4: return STAT_FUEL;
1258                 default: return -1;
1259         }
1260 }
1261
1262 float GetAmmoItemCode(float i)
1263 {
1264         switch(i)
1265         {
1266                 case 0: return IT_SHELLS;
1267                 case 1: return IT_NAILS;
1268                 case 2: return IT_ROCKETS;
1269                 case 3: return IT_CELLS;
1270                 case 4: return IT_FUEL;
1271                 default: return -1;
1272         }
1273 }
1274
1275 string GetAmmoPicture(float i)
1276 {
1277         switch(i)
1278         {
1279                 case 0: return "ammo_shells";
1280                 case 1: return "ammo_bullets";
1281                 case 2: return "ammo_rocket";
1282                 case 3: return "ammo_cells";
1283                 case 4: return "ammo_fuel";
1284                 default: return "";
1285         }
1286 }
1287
1288 void HUD_Inventory()
1289 {
1290         float id = 1;
1291         float i;
1292         float stat_items;
1293
1294         vector pos, mySize, mysize, mypos;
1295         pos = HUD_Panel_GetPos(id);
1296         mySize = HUD_Panel_GetSize(id);
1297
1298         HUD_Panel_DrawBg(id, pos, mySize);
1299         float padding;
1300         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1301         if(padding)
1302         {
1303                 pos += '1 1 0' * padding;
1304                 mySize -= '2 2 0' * padding;
1305         }
1306
1307         // ammo
1308         stat_items = getstati(STAT_ITEMS);
1309         for (i = 0; i < 4; ++i) {
1310                 float a;
1311                 a = getstati(GetAmmoStat(i)); // how much ammo do we have of type i?
1312                 if(hud_configure)
1313                         a = 100;
1314
1315                 if(cvar("hud_inventory_onlycurrent")) {
1316                         if (stat_items & GetAmmoItemCode(i)) {
1317                                 drawpic_skin(pos, "ammo_current_bg", mySize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1318                                 drawpic_skin(pos + eY * 0.05 * mySize_y, GetAmmoPicture(i), '1 1 0' * 0.8 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1319                                 if(a < 10)
1320                                         HUD_DrawXNum(pos + 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_alpha_fg, DRAWFLAG_NORMAL);
1321                                 else
1322                                         HUD_DrawXNum(pos + 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_alpha_fg, DRAWFLAG_NORMAL);
1323                         }
1324                 } else {
1325                         if (a > 0) {
1326                                 if(mySize_x/mySize_y >= 10) { // arrange horizontally
1327                                         switch (i) {
1328                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1329                                                 case 1: mypos_x = pos_x + 0.25 * mySize_x;      mypos_y = pos_y;                        break; // bullets
1330                                                 case 2: mypos_x = pos_x + 0.5  * mySize_x;      mypos_y = pos_y;                        break; // rockets
1331                                                 case 3: mypos_x = pos_x + 0.75 * mySize_x;      mypos_y = pos_y;                        break; // cells
1332                                         }
1333                                         mysize_x = 0.25 * mySize_x;
1334                                         mysize_y = mySize_y;
1335                                 } else if(mySize_x/mySize_y >= 2.5) { // arrange in a 2x2 grid
1336                                         switch (i) {
1337                                                 case 0: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y + 0.5 * mySize_y;       break; // shells
1338                                                 case 1: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y;                        break; // bullets
1339                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5 * mySize_y;       break; // rockets
1340                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // cells
1341                                         }
1342                                         mysize_x = 0.5 * mySize_x;
1343                                         mysize_y = 0.5 * mySize_y;
1344                                 } else { // arrange vertically
1345                                         switch (i) {
1346                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1347                                                 case 1: mypos_x = pos_x;                        mypos_y = pos_y + 0.25 * mySize_y;      break; // bullets
1348                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5  * mySize_y;      break; // rockets
1349                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y + 0.75 * mySize_y;      break; // cells
1350                                         }
1351                                         mysize_x = mySize_x;
1352                                         mysize_y = 0.25 * mySize_y;
1353                                 }
1354
1355                                 if (stat_items & GetAmmoItemCode(i))
1356                                         drawpic_skin(mypos, "ammo_current_bg", mysize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1357                                 drawpic_skin(mypos + eY * 0.05 * mysize_y, GetAmmoPicture(i), '1 1 0' * 0.8 * mysize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1358                                 if (a < 10) {
1359                                         if(stat_items & GetAmmoItemCode(i))
1360                                                 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_alpha_fg, DRAWFLAG_NORMAL);
1361                                         else
1362                                                 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_alpha_fg * 0.7, DRAWFLAG_NORMAL);
1363                                 } else {
1364                                         if(stat_items & GetAmmoItemCode(i))
1365                                                 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_alpha_fg, DRAWFLAG_NORMAL);
1366                                         else
1367                                                 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_alpha_fg * 0.7, DRAWFLAG_NORMAL);
1368                                 }
1369                         }
1370                 }
1371         }
1372 }
1373
1374
1375 // Powerups (#2)
1376 //
1377 float shield_maxtime;
1378 float strength_maxtime;
1379 void HUD_Powerups() {
1380         float id = 2;
1381         float stat_items;
1382         stat_items = getstati(STAT_ITEMS);
1383
1384         if(!hud_configure)
1385         {
1386                 if not(stat_items & IT_STRENGTH)
1387                         if not(stat_items & IT_INVINCIBLE)
1388                                 return;
1389
1390                 if (getstati(STAT_HEALTH) <= 0)
1391                         return;
1392         }
1393
1394         vector pos, mySize;
1395         pos = HUD_Panel_GetPos(id);
1396         mySize = HUD_Panel_GetSize(id);
1397
1398         HUD_Panel_DrawBg(id, pos, mySize);
1399         float padding;
1400         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1401         if(padding)
1402         {
1403                 pos += '1 1 0' * padding;
1404                 mySize -= '2 2 0' * padding;
1405         }
1406
1407         float strength_time, shield_time;
1408
1409         strength_time = bound(0, ceil(getstatf(STAT_STRENGTH_FINISHED) - time), 99);
1410         shield_time = bound(0, ceil(getstatf(STAT_INVINCIBLE_FINISHED) - time), 99);
1411
1412         if(hud_configure)
1413         {
1414                 strength_time = 15;
1415                 shield_time = 27;
1416         }
1417
1418         float len;
1419
1420         vector barpos, barsize;
1421         vector picpos;
1422         vector numpos;
1423
1424         string leftname, rightname;
1425         float leftcnt, rightcnt;
1426         float leftalpha, rightalpha;
1427         if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) {
1428                 leftname = "strength";
1429                 leftcnt = strength_time;
1430
1431                 rightname = "shield";
1432                 rightcnt = shield_time;
1433         } else {
1434                 leftname = "shield";
1435                 leftcnt = shield_time;
1436
1437                 rightname = "strength";
1438                 rightcnt = strength_time;
1439         }
1440         leftalpha = bound(0, leftcnt, 1);
1441         rightalpha = bound(0, rightcnt, 1);
1442
1443         // TODO: tile statusbars?
1444         if (mySize_x/mySize_y > 4)
1445         {
1446                 if(leftcnt)
1447                 {
1448                         len = strlen(ftos(leftcnt));
1449
1450                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1451                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/30);
1452                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1453                                 picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
1454                                 numpos = picpos - eX * 2 * 0.5 * mySize_y + eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1455                         } else {
1456                                 barpos = pos;
1457                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1458                                 picpos = pos;
1459                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1460                         }
1461
1462                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1463                         drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1464                         HUD_DrawXNum(numpos, leftcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1465                 }
1466
1467                 if(rightcnt)
1468                 {
1469                         len = strlen(ftos(rightcnt));
1470
1471                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1472                                 barpos = pos + eX * 0.5 * mySize_x;
1473                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1474                                 picpos = pos + eX * 0.5 * mySize_x;
1475                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1476                         } else {
1477                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/30);
1478                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1479                                 picpos = pos + eX * mySize_x - eX * mySize_y;
1480                                 numpos = picpos - eX * mySize_y + eY * 0.25 * mySize_y;
1481                         }
1482
1483                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1484                         drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1485                         HUD_DrawXNum(numpos, rightcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1486                 }
1487         }
1488         else if (mySize_x/mySize_y > 1.5)
1489         {
1490                 if(leftcnt)
1491                 {
1492                         len = strlen(ftos(leftcnt));
1493
1494                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1495                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/30);
1496                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1497                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
1498                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1499                         } else {
1500                                 barpos = pos;
1501                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1502                                 picpos = pos;
1503                                 numpos = picpos + eX * 0.5 * mySize_y;
1504                         }
1505
1506                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1507                         drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1508                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1509                 }
1510
1511                 if(rightcnt)
1512                 {
1513                         len = strlen(ftos(rightcnt));
1514
1515                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1516                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1517                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1518                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
1519                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1520                         } else {
1521                                 barpos = pos + eY * 0.5 * mySize_y;
1522                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1523                                 picpos = pos + eY * 0.5 * mySize_y;
1524                                 numpos = picpos + eX * 0.5 * mySize_y;
1525                         }
1526
1527                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1528                         drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1529                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1530                 }
1531         }
1532         else
1533         {
1534                 if(leftcnt)
1535                 {
1536                         len = strlen(ftos(leftcnt));
1537
1538                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1539                                 barpos = pos;
1540                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1541                                 picpos = pos + eX * 0.05 * mySize_x;
1542                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
1543                         } else {
1544                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/30);
1545                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1546                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x;
1547                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x;
1548                         }
1549
1550                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1551                         drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1552                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1553                 }
1554
1555                 if(rightcnt)
1556                 {
1557                         len = strlen(ftos(rightcnt));
1558
1559                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1560                                 barpos = pos + eX * 0.5 * mySize_x;
1561                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1562                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1563                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1564                         } else {
1565                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/30) + eX * 0.5 * mySize_x;
1566                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1567                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x + eX * 0.5 * mySize_x;
1568                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
1569                         }
1570
1571                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1572                         drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1573                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1574                 }
1575         }
1576
1577         /* TODO: add expand from old code!
1578         //strength
1579         if (strength_time) {
1580                 dt = strength_time - time;
1581                 if(dt > 0)
1582                 {
1583                         if(dt < 5)
1584                         {
1585                                 drawpic_expanding_two(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1586                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1587                         }
1588                         else
1589                         {
1590                                 drawpic_skin(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
1591                         }
1592                         HUD_DrawXNum(pos - '40 -2 0', ceil(dt), 2, 0, countdown_fontsize, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1593                 }
1594                 else if(dt > -1)
1595                 {
1596                         drawpic_expanding(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1597                                 bound(0, -dt / 0.5, 1));
1598                 }
1599         }
1600
1601         //invincibility
1602         if (invincibility_time) {
1603                 dt = invincibility_time - time;
1604                 if(dt > 0)
1605                 {
1606                         if(dt < 5)
1607                         {
1608                                 drawpic_expanding_two(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1609                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1610                         }
1611                         else
1612                         {
1613                                 drawpic_skin(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
1614                         }
1615                         HUD_DrawXNum(pos - '40 -24 0', ceil(dt), 2, 0, countdown_fontsize, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1616                 }
1617                 else if(dt > -1)
1618                 {
1619                         drawpic_expanding(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1620                                 bound(0, -dt / 0.5, 1));
1621                 }
1622         }
1623         */
1624 }
1625
1626 // Health/armor (#3)
1627 //
1628 void HUD_HealthArmor(void)
1629 {
1630         float id = 3;
1631         vector pos, mySize;
1632         pos = HUD_Panel_GetPos(id);
1633         mySize = HUD_Panel_GetSize(id);
1634
1635         HUD_Panel_DrawBg(id, pos, mySize);
1636         float padding;
1637         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1638         if(padding)
1639         {
1640                 pos += '1 1 0' * padding;
1641                 mySize -= '2 2 0' * padding;
1642         }
1643
1644         float armor, health, x;
1645         armor = getstati(STAT_ARMOR);
1646         health = getstati(STAT_HEALTH);
1647
1648         float fuel;
1649         fuel = getstati(GetAmmoStat(4)); // how much fuel do we have?
1650
1651         if(hud_configure)
1652         {
1653                 armor = 150;
1654                 health = 100;
1655                 fuel = 70;
1656         }
1657
1658         if(health <= 0)
1659                 return;
1660
1661         float len;
1662
1663         // TODO!
1664         /*
1665         if(hud_hudselector == 2) // combined health and armor display
1666         {
1667                 vector v;
1668                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
1669
1670                 vector num_pos;
1671                 num_pos = - '96 28 0';
1672
1673                 x = floor(v_x + 1);
1674
1675                 if(v_z) // fully armored
1676                 {
1677                         // here, armorideal > armor
1678                         drawpic_skin(num_pos + '78 -4.5 0', "health", '32 32 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1679                         drawpic_skin(num_pos + '108 -4.5 0', "armor", '20 20 0', '1 1 1', hud_alpha_fg * armor / v_y, DRAWFLAG_NORMAL);
1680                 }
1681                 else
1682                 {
1683                         drawpic_skin(num_pos + '108 -4.5 0', "health", '20 20 0', '1 1 1', hud_alpha_fg * v_y / armor, DRAWFLAG_NORMAL);
1684                         drawpic_skin(num_pos + '78 -4.5 0', "armor", '32 32 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1685                 }
1686                 HUD_DrawXNum_Colored(num_pos, x, 3, 24, hud_alpha_fg); // draw the combined health and armor
1687         }
1688
1689         else
1690         {
1691         */
1692         vector barpos, barsize;
1693         vector picpos;
1694         vector numpos;
1695
1696         string leftname, rightname;
1697         float leftcnt, rightcnt;
1698         float leftactive, rightactive;
1699         float leftalpha, rightalpha;
1700         // TODO!
1701         if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) { // old style layout with armor left/top of health
1702                 leftname = "armor";
1703                 leftcnt = armor;
1704                 if(leftcnt)
1705                         leftactive = 1;
1706                 leftalpha = min((armor+10)/55, 1);
1707
1708                 rightname = "health";
1709                 rightcnt = health;
1710                 rightactive = 1;
1711                 rightalpha = 1;
1712         } else {
1713                 leftname = "health";
1714                 leftcnt = health;
1715                 leftactive = 1;
1716                 leftalpha = 1;
1717
1718                 rightname = "armor";
1719                 rightcnt = armor;
1720                 if(rightcnt)
1721                         rightactive = 1;
1722                 rightalpha = min((armor+10)/55, 1);
1723         }
1724
1725         // TODO: tile statusbars?
1726         if (mySize_x/mySize_y > 5)
1727         {
1728                 if(leftactive)
1729                 {
1730                         len = strlen(ftos(leftcnt));
1731
1732                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1733                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/200);
1734                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1735                                 picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
1736                                 numpos = picpos - eX * 3 * 0.5 * mySize_y + eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1737                         } else {
1738                                 barpos = pos;
1739                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1740                                 picpos = pos;
1741                                 numpos = picpos + eX * mySize_y + eY * 0.25 * mySize_y;
1742                         }
1743
1744                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1745                         drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1746                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1747                 }
1748
1749                 if(rightactive)
1750                 {
1751                         len = strlen(ftos(rightcnt));
1752
1753                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1754                                 barpos = pos + eX * 0.5 * mySize_x;
1755                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1756                                 picpos = pos + eX * 0.5 * mySize_x;
1757                                 numpos = picpos + eX * mySize_y - eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1758                         } else {
1759                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/200);
1760                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1761                                 picpos = pos + eX * mySize_x - eX * mySize_y;
1762                                 numpos = picpos - eX * 1.5 * mySize_y + eY * 0.25 * mySize_y;
1763                         }
1764
1765                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1766                         drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1767                         HUD_DrawXNum_Colored(numpos, rightcnt, 3, 0.5 * mySize_y, hud_alpha_fg);
1768                 }
1769
1770                 if(fuel)
1771                         drawpic_skin(pos, "statusbar", eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1772         }
1773         else if (mySize_x/mySize_y > 2)
1774         {
1775                 if(leftactive)
1776                 {
1777                         len = strlen(ftos(leftcnt));
1778
1779                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1780                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/200);
1781                                 barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
1782                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
1783                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1784                         } else {
1785                                 barpos = pos;
1786                                 barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
1787                                 picpos = pos;
1788                                 numpos = picpos + eX * 0.5 * mySize_y;
1789                         }
1790
1791                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1792                         drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1793                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1794                 }
1795
1796                 if(rightactive)
1797                 {
1798                         len = strlen(ftos(rightcnt));
1799
1800                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1801                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1802                                 barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1803                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
1804                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1805                         } else {
1806                                 barpos = pos + eY * 0.5 * mySize_y;
1807                                 barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1808                                 picpos = pos + eY * 0.5 * mySize_y;
1809                                 numpos = picpos + eX * 0.5 * mySize_y;
1810                         }
1811
1812                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1813                         drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1814                         HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1815                 }
1816
1817                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1818                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
1819                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
1820                 } else {
1821                         barpos = pos;
1822                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
1823                 }
1824                 if(fuel)
1825                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1826         }
1827         else
1828         {
1829                 if(leftactive)
1830                 {
1831                         len = strlen(ftos(leftcnt));
1832
1833                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1834                                 barpos = pos;
1835                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
1836                                 picpos = pos + eX * 0.05 * mySize_x;
1837                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
1838                         } else {
1839                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/200);
1840                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
1841                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x;
1842                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x;
1843                         }
1844
1845                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1846                         drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1847                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.166 * mySize_x, hud_alpha_fg);
1848                 }
1849
1850                 if(rightactive)
1851                 {
1852                         len = strlen(ftos(rightcnt));
1853
1854                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1855                                 barpos = pos + eX * 0.5 * mySize_x;
1856                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
1857                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1858                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1859                         } else {
1860                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/200) + eX * 0.5 * mySize_x;
1861                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
1862                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x + eX * 0.5 * mySize_x;
1863                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x + eX * 0.5 * mySize_x;
1864                         }
1865
1866                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1867                         drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1868                         HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.166 * mySize_x, hud_alpha_fg);
1869                 }
1870
1871                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1872                         barpos = pos;
1873                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
1874                 } else {
1875                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, fuel/100);
1876                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
1877                 }
1878                 if(fuel)
1879                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1880         }
1881 }
1882
1883 // ___TODO___ !!!
1884 // Notification area (#4)
1885 //
1886 void HUD_Notify (void)
1887 {
1888         float id = 4;
1889         vector pos, mySize;
1890         pos = HUD_Panel_GetPos(id);
1891         mySize = HUD_Panel_GetSize(id);
1892
1893         HUD_Panel_DrawBg(id, pos, mySize);
1894         float padding;
1895         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1896         if(padding)
1897         {
1898                 pos += '1 1 0' * padding;
1899                 mySize -= '2 2 0' * padding;
1900         }
1901
1902         string s;
1903         entity tm;
1904         if(spectatee_status && !intermission)
1905         {
1906                 drawfont = hud_bigfont;
1907                 if(spectatee_status == -1)
1908                         s = "^1Observing";
1909                 else
1910                         s = GetPlayerName(spectatee_status - 1);
1911                 // spectated player name between HUD and chat area, aligned to the left
1912                 pos_x = 0;
1913                 pos_y = - 50 - hud_fontsize_spec_y;
1914                 s = textShortenToWidth(s, vid_conwidth/2.5, hud_fontsize_spec, stringwidth_colors);
1915                 drawcolorcodedstring(pos, s, hud_fontsize_spec, hud_alpha_fg, DRAWFLAG_NORMAL);
1916                 drawfont = hud_font;
1917
1918                 // spectator text in the upper right corner
1919                 if(spectatee_status == -1)
1920                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 to spectate");
1921                 else
1922                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 for another player");
1923
1924                 if(spectatee_status == -1)
1925                         s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
1926                 else
1927                         s = strcat("^1Press ^3", getcommandkey("secondary fire", "+attack2"), "^1 to observe");
1928
1929                 s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
1930
1931                 if(gametype == GAME_ARENA)
1932                         s = "^1Wait for your turn to join";
1933                 else if(gametype == GAME_LMS)
1934                 {
1935                         entity sk;
1936                         sk = playerslots[player_localentnum - 1];
1937                         if(sk.(scores[ps_primary]) >= 666)
1938                                 s = "^1Match has already begun";
1939                         else if(sk.(scores[ps_primary]) > 0)
1940                                 s = "^1You have no more lives left";
1941                         else
1942                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1943                 }
1944                 else
1945                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1946
1947                 //show restart countdown:
1948                 if (time < getstatf(STAT_GAMESTARTTIME)) {
1949                         float countdown;
1950                         //we need to ceil, otherwise the countdown would be off by .5 when using round()
1951                         countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
1952                         s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
1953                 }
1954         }
1955         if(warmup_stage && !intermission)
1956         {
1957                 s = "^2Currently in ^1warmup^2 stage!";
1958         }
1959
1960         // move more important stuff more to the middle so its more visible
1961
1962         string blinkcolor;
1963         if(mod(time, 1) >= 0.5)
1964                 blinkcolor = "^1";
1965         else
1966                 blinkcolor = "^3";
1967
1968         if(ready_waiting && !intermission && !spectatee_status)
1969         {
1970                 if(ready_waiting_for_me)
1971                 {
1972                         if(warmup_stage)
1973                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
1974                         else
1975                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
1976                 }
1977                 else
1978                 {
1979                         if(warmup_stage)
1980                                 s = strcat("^2Waiting for others to ready up to end warmup...");
1981                         else
1982                                 s = strcat("^2Waiting for others to ready up...");
1983                 }
1984         }
1985         else if(warmup_stage && !intermission && !spectatee_status)
1986         {
1987                 s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
1988         }
1989
1990         if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
1991         {
1992                 float ts_min, ts_max;
1993                 tm = teams.sort_next;
1994                 if (tm)
1995                 {
1996                         for(; tm.sort_next; tm = tm.sort_next)
1997                         {
1998                                 if(!tm.team_size || tm.team == COLOR_SPECTATOR)
1999                                         continue;
2000                                 if(!ts_min) ts_min = tm.team_size;
2001                                 else ts_min = min(ts_min, tm.team_size);
2002                                 if(!ts_max) ts_max = tm.team_size;
2003                                 else ts_max = max(ts_max, tm.team_size);
2004                         }
2005                         if ((ts_max - ts_min) > 1)
2006                         {
2007                                 s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
2008                                 tm = GetTeam(myteam, false);
2009                                 if (tm)
2010                                 if (tm.team != COLOR_SPECTATOR)
2011                                 if (tm.team_size == ts_max)
2012                                         s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
2013
2014                         }
2015                 }
2016         }
2017 }
2018
2019 // Timer (#5)
2020 //
2021 void HUD_Timer()
2022 {
2023         float id = 5;
2024         vector pos, mySize;
2025         pos = HUD_Panel_GetPos(id);
2026         mySize = HUD_Panel_GetSize(id);
2027
2028         HUD_Panel_DrawBg(id, pos, mySize);
2029         float padding;
2030         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2031         if(padding)
2032         {
2033                 pos += '1 1 0' * padding;
2034                 mySize -= '2 2 0' * padding;
2035         }
2036
2037         float timelimit, elapsedTime, minutes, seconds, timeleft, minutesLeft, secondsLeft;
2038
2039         timelimit = getstatf(STAT_TIMELIMIT);
2040
2041         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
2042         timeleft = ceil(timeleft);
2043         minutesLeft = floor(timeleft / 60);
2044         secondsLeft = timeleft - minutesLeft*60;
2045
2046         vector timer_color;
2047         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
2048                 timer_color = '1 1 1'; //white
2049         else if(minutesLeft >= 1)
2050                 timer_color = '1 1 0'; //yellow
2051         else
2052                 timer_color = '1 0 0'; //red
2053
2054         if (cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
2055                 if (time < getstatf(STAT_GAMESTARTTIME)) {
2056                         //while restart is still active, show 00:00
2057                         minutes = seconds = 0;
2058                 } else {
2059                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
2060                         minutes = floor(elapsedTime / 60);
2061                         seconds = elapsedTime - minutes*60;
2062                 }
2063         } else {
2064                 minutes = minutesLeft;
2065                 seconds = secondsLeft;
2066         }
2067
2068         if(minutes > 999)
2069                 seconds = 99;
2070         minutes = min(minutes, 999);
2071         if(minutesLeft >= 1 || cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
2072                 HUD_DrawXNum(pos + eX * mySize_x - eX * 5.1 * mySize_y, minutes, 3, 0, mySize_y, timer_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2073                 drawpic_skin(pos + eX * mySize_x - eX * 2.57 * mySize_y, "num_colon", '1 1 0' * mySize_y, timer_color, hud_alpha_fg, DRAWFLAG_NORMAL);
2074         }
2075         HUD_DrawXNum(pos + eX * mySize_x - eX * 2 * mySize_y, seconds, -2, 0, mySize_y, timer_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2076 }
2077
2078 // Radar (#6)
2079 //
2080 void HUD_Radar(void)
2081 {
2082         float id = 6;
2083         vector pos, mySize;
2084         pos = HUD_Panel_GetPos(id);
2085         mySize = HUD_Panel_GetSize(id);
2086
2087         HUD_Panel_DrawBg(id, pos, mySize);
2088         float padding;
2089         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2090         if(padding)
2091         {
2092                 pos += '1 1 0' * padding;
2093                 mySize -= '2 2 0' * padding;
2094         }
2095
2096         local float color1, color2; // color already declared as a global in hud.qc
2097         local vector rgb;
2098         local entity tm;
2099         float scale2d, normalsize, bigsize;
2100         float f;
2101
2102         teamradar_origin2d = pos + 0.5 * mySize; // TODO: stupid compat, should be removed
2103         teamradar_size2d = mySize;
2104
2105         if(minimapname == "" && !ons_showmap)
2106                 return;
2107
2108         teamradar_loadcvars();
2109
2110         switch(cl_teamradar_zoommode)
2111         {
2112                 default:
2113                 case 0:
2114                         f = current_zoomfraction;
2115                         break;
2116                 case 1:
2117                         f = 1 - current_zoomfraction;
2118                         break;
2119                 case 2:
2120                         f = 0;
2121                         break;
2122                 case 3:
2123                         f = 1;
2124                         break;
2125         }
2126
2127         switch(cl_teamradar_rotation)
2128         {
2129                 case 0:
2130                         teamradar_angle = view_angles_y - 90;
2131                         break;
2132                 default:
2133                         teamradar_angle = 90 * cl_teamradar_rotation;
2134                         break;
2135         }
2136
2137         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
2138         teamradar_size2d = mySize;
2139
2140         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
2141
2142         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
2143         if(cl_teamradar_rotation == 0)
2144         {
2145                 // max-min distance must fit the radar in any rotation
2146                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
2147         }
2148         else
2149         {
2150                 vector c0, c1, c2, c3, span;
2151                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
2152                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
2153                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
2154                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
2155                 span = '0 0 0';
2156                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
2157                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
2158
2159                 // max-min distance must fit the radar in x=x, y=y
2160                 bigsize = min(
2161                         teamradar_size2d_x * scale2d / (1.05 * span_x),
2162                         teamradar_size2d_y * scale2d / (1.05 * span_y)
2163                 );
2164         }
2165
2166         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / cl_teamradar_scale;
2167         if(bigsize > normalsize)
2168                 normalsize = bigsize;
2169
2170         teamradar_size =
2171                   f * bigsize
2172                 + (1 - f) * normalsize;
2173         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
2174                   f * (mi_min + mi_max) * 0.5
2175                 + (1 - f) * view_origin);
2176
2177         color1 = GetPlayerColor(player_localentnum-1);
2178         rgb = GetTeamRGB(color1);
2179
2180         drawsetcliparea(
2181                 pos_x,
2182                 pos_y,
2183                 mySize_x,
2184                 mySize_y
2185         );
2186
2187         draw_teamradar_background(cl_teamradar_background_alpha, cl_teamradar_foreground_alpha);
2188
2189         if(ons_showmap)
2190         {
2191                 drawresetcliparea();
2192
2193                 vector frame_origin, frame_size;
2194                 frame_origin = frame_size = '0 0 0';
2195
2196                 frame_origin_x = pos_x - teamradar_size2d_x * 0.55859375; // matches the picture
2197                 frame_origin_y = pos_y - teamradar_size2d_y * 0.55859375; // matches the picture
2198                 frame_size_x = pos_x * 1.1171875; // matches the picture
2199                 frame_size_y = pos_y * 1.1171875; // matches the picture
2200                 drawpic_skin(frame_origin, "gfx/ons-frame.tga", frame_size, '1 1 1', hud_alpha_fg, 0);
2201                 drawpic_skin(frame_origin, "gfx/ons-frame-team.tga", frame_size, rgb, hud_alpha_fg, 0);
2202
2203                 drawsetcliparea(
2204                         pos_x - teamradar_size2d_x * 0.5,
2205                         pos_y - teamradar_size2d_y * 0.5,
2206                         teamradar_size2d_x,
2207                         teamradar_size2d_y
2208                 );
2209         }
2210
2211         for(tm = world; (tm = find(tm, classname, "radarlink")); )
2212                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
2213         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
2214                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, hud_alpha_fg);
2215         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
2216         {
2217                 color2 = GetPlayerColor(tm.sv_entnum);
2218                 //if(color == COLOR_SPECTATOR || color == color2)
2219                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
2220         }
2221         draw_teamradar_player(view_origin, view_angles, '1 1 1');
2222
2223         drawresetcliparea();
2224 };
2225
2226 // Score (#7)
2227 //
2228 void HUD_Score()
2229 {
2230         float id = 7;
2231         vector pos, mySize;
2232         pos = HUD_Panel_GetPos(id);
2233         mySize = HUD_Panel_GetSize(id);
2234
2235         HUD_Panel_DrawBg(id, pos, mySize);
2236         float padding;
2237         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2238         if(padding)
2239         {
2240                 pos += '1 1 0' * padding;
2241                 mySize -= '2 2 0' * padding;
2242         }
2243
2244         float score, distribution, leader;
2245         float score_len, distr_len;
2246         vector score_pos, secondary_score_pos, distribution_color;
2247         entity tm, pl, me;
2248         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
2249
2250         // TODO... this (race part) still uses constant coordinates :/
2251         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
2252                 /*pl = players.sort_next;
2253                 if(pl == me)
2254                         pl = pl.sort_next;
2255                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
2256                         if(pl.scores[ps_primary] == 0)
2257                                 pl = world;
2258
2259                 score = me.(scores[ps_primary]);
2260
2261                 float racemin, racesec, racemsec;
2262                 float distsec, distmsec, minusplus;
2263
2264                 racemin = floor(score/(60 * TIME_FACTOR));
2265                 racesec = floor((score - racemin*(60 * TIME_FACTOR))/TIME_FACTOR);
2266                 racemsec = score - racemin*60*TIME_FACTOR - racesec*TIME_FACTOR;
2267
2268                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
2269                         // distribution display
2270                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2271
2272                         if (distribution < TIME_FACTOR && distribution > -TIME_FACTOR)
2273                                 distmsec = fabs(distribution);
2274                         else {
2275                                 distsec = floor(fabs(distribution)/TIME_FACTOR);
2276                                 distmsec = fabs(distribution) - distsec*TIME_FACTOR;
2277                                 if (distribution < 0)
2278                                         distsec = -distsec;
2279                         }
2280
2281                         if (distribution <= 0) {
2282                                 distribution_color = eY;
2283                                 minusplus = 1; // minusplus 1: always prefix with minus sign
2284                         }
2285                         else {
2286                                 distribution_color = eX;
2287                                 minusplus = 2; // minusplus 1: always prefix with plus sign
2288                         }
2289                         HUD_DrawXNum(bottomright - '0 48 0' - '16 0 0' * TIME_DECIMALS, distmsec, -TIME_DECIMALS, 0, 16, distribution_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2290                         HUD_DrawXNum(bottomright - '68 48 0' - '16 0 0' * TIME_DECIMALS, distsec, 4, minusplus, 16, distribution_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2291                         drawpic_skin(bottomright - '10 48 0' - '16 0 0' * TIME_DECIMALS, "num_dot", '16 16 0', distribution_color, hud_alpha_fg, DRAWFLAG_ADDITIVE);
2292                 }
2293                 // race record display
2294                 if (distribution <= 0 || distribution == score) // draw the highlight background behind the timer if we have the lead
2295                         drawpic_skin(bottomright - '0 32 0' - '32 0 0' * (4 + TIME_DECIMALS), "highlight_4", '0 28 0' + '32 0 0' * (4 + TIME_DECIMALS), '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2296
2297                 HUD_DrawXNum(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0', racemsec, -TIME_DECIMALS, 0, 30, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2298                 HUD_DrawXNum(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0'  - '66 0 0', racesec, -2, 0, 30, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2299                 drawpic_skin(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '18 0 0', "num_dot", '30 30 0', '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
2300
2301                 HUD_DrawXNum(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '132 0 0', racemin, -2, 0, 30, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2302                 drawpic_skin(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '84 0 0', "num_colon", '30 30 0', '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
2303                 */
2304         } else if (!teamplay) { // non-teamgames
2305                 // me vector := [team/connected frags id]
2306                 pl = players.sort_next;
2307                 if(pl == me)
2308                         pl = pl.sort_next;
2309
2310                 if(hud_configure)
2311                         distribution = 42;
2312                 else if(pl)
2313                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2314                 else
2315                         distribution = 0;
2316
2317                 score = me.(scores[ps_primary]);
2318                 if(hud_configure)
2319                         score = 123;
2320
2321                 if(distribution >= 5) {
2322                         distribution_color = eY;
2323                         leader = 1;
2324                 } else if(distribution >= 0) {
2325                         distribution_color = '1 1 1';
2326                         leader = 1;
2327                 } else if(distribution >= -5)
2328                         distribution_color = '1 1 0';
2329                 else
2330                         distribution_color = eX;
2331
2332                 score_len = strlen(ftos(score));
2333                 distr_len = strlen(ftos(distribution));
2334
2335                 HUD_DrawXNum(pos + eX * mySize_x - eX * 3 * 0.33 * mySize_y, distribution, 3, 3, 0.33 * mySize_y, distribution_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2336                 if (leader)
2337                         drawpic_skin(pos + eX * mySize_x - eX * score_len * mySize_y - eX * 3 * 0.33 * mySize_y, strcat("highlight_", ftos(score_len)), eX * score_len * mySize_y + eY * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2338                 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_alpha_fg, DRAWFLAG_NORMAL);
2339         } else { // teamgames
2340                 float max_fragcount;
2341                 max_fragcount = -99;
2342
2343                 float teamnum;
2344                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2345                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !hud_configure)) // no players? don't display
2346                                 continue;
2347                         score = tm.(teamscores[ts_primary]);
2348                         if(hud_configure)
2349                                 score = 123;
2350                         leader = 0;
2351                         
2352                         score_len = strlen(ftos(score));
2353
2354                         if (score > max_fragcount)
2355                                 max_fragcount = score;
2356
2357                         if(tm.team == myteam) {
2358                                 if (max_fragcount == score)
2359                                         leader = 1;
2360                                 if (leader)
2361                                         drawpic_skin(pos + eX * mySize_x - eX * score_len * mySize_y - eX * 3 * 0.33 * mySize_y, strcat("highlight_", ftos(score_len)), eX * score_len * mySize_y + eY * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2362                                 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_alpha_fg, DRAWFLAG_NORMAL);
2363                         } else {
2364                                 if (max_fragcount == score)
2365                                         leader = 1;
2366                                 if (leader)
2367                                         drawpic_skin(pos + eX * mySize_x - eX * 0.33 * score_len * mySize_y + eY * 0.33 * mySize_y * teamnum, strcat("highlight_", ftos(score_len)), eX * 0.33 * score_len * mySize_y + eY * 0.33 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2368                                 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_alpha_fg, DRAWFLAG_NORMAL);
2369                                 teamnum += 1;
2370                         }
2371                 }
2372         }
2373 }
2374
2375 // Race timer (#8)
2376 //
2377 void HUD_RaceTimer (void) {
2378         float id = 8;
2379         vector pos, mySize;
2380         pos = HUD_Panel_GetPos(id);
2381         mySize = HUD_Panel_GetSize(id);
2382
2383         HUD_Panel_DrawBg(id, pos, mySize);
2384         float padding;
2385         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2386         if(padding)
2387         {
2388                 pos += '1 1 0' * padding;
2389                 mySize -= '2 2 0' * padding;
2390         }
2391
2392         drawfont = hud_bigfont;
2393         float a, t;
2394         string s, forcetime;
2395
2396         if(hud_configure)
2397         {
2398                 s = "0:13:37";
2399                 drawstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, FALSE, '0.75 0.75 0' * mySize_y), s, '0.75 0.75 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2400                 s = "^1Intermediate 1 (+15.42)";
2401                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.25 * mySize_y) + eY * 0.75 * mySize_y, s, '1 1 0' * 0.25 * mySize_y, hud_alpha_fg, DRAWFLAG_NORMAL);
2402         }
2403         else if(race_checkpointtime)
2404         {
2405                 a = bound(0, 2 - (time - race_checkpointtime), 1);
2406                 s = "";
2407                 forcetime = "";
2408                 if(a > 0) // just hit a checkpoint?
2409                 {
2410                         if(race_checkpoint != 254)
2411                         {
2412                                 if(race_time && race_previousbesttime)
2413                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
2414                                 else
2415                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
2416                                 if(race_time)
2417                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
2418                         }
2419                 }
2420                 else
2421                 {
2422                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
2423                         {
2424                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
2425                                 if(a > 0) // next one?
2426                                 {
2427                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
2428                                 }
2429                         }
2430                 }
2431
2432                 if(s != "" && a > 0)
2433                 {
2434                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2435                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2436                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.25 * mySize_y) + eY * 0.75 * mySize_y, s, '1 1 0' * 0.25 * mySize_y, hud_alpha_fg * a, DRAWFLAG_NORMAL);
2437                 }
2438
2439                 // ___ TODO!!!___
2440                 if(race_penaltytime)
2441                 {
2442                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
2443                         if(a > 0)
2444                         {
2445                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
2446                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2447                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2448                                 drawcolorcodedstring(pos - '0 32 0' - '0.5 0 0' * stringwidth(s, TRUE, '16 16 0'), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2449                         }
2450                 }
2451
2452                 if(forcetime != "")
2453                 {
2454                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
2455                         //drawstring_expanding(m - '16 0 0' * stringwidth(forcetime, FALSE), forcetime, '32 32 0', '1 1 1', hud_alpha_fg, 0, a);
2456                         drawstring_expanding(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(forcetime, FALSE, '1 1 0' * 0.75 * mySize_y), forcetime, '1 1 0' * 0.75 * mySize_y, '1 1 1', hud_alpha_fg, 0, a);
2457                 }
2458                 else
2459                         a = 1;
2460
2461                 if(race_laptime && race_checkpoint != 255)
2462                 {
2463                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
2464                         //drawstring(m - '16 0 0' * stringwidth(s, FALSE), s, '32 32 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2465                         drawstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, FALSE, '0.75 0.75 0' * mySize_y), s, '0.75 0.75 0' * mySize_y, '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2466                 }
2467         }
2468         else
2469         {
2470                 if(race_mycheckpointtime)
2471                 {
2472                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
2473                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
2474                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2475                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2476                         drawcolorcodedstring(pos - '0 16 0' - '0.5 0 0' * stringwidth(s, TRUE, '16 16 0'), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2477                 }
2478                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
2479                 {
2480                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
2481                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
2482                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2483                         //drawcolorcodedstring(m - '0 0 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2484                         drawcolorcodedstring(pos - '0 0 0' - '0.5 0 0' * stringwidth(s, TRUE, '16 16 0'), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2485                 }
2486
2487                 if(race_penaltytime && !race_penaltyaccumulator)
2488                 {
2489                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
2490                         a = bound(0, (1 + t - time), 1);
2491                         if(a > 0)
2492                         {
2493                                 if(time < t)
2494                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
2495                                 else
2496                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
2497                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2498                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2499                                 drawcolorcodedstring(pos - '0 32 0' - '0.5 0 0' * stringwidth(s, TRUE, '16 16 0'), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2500                         }
2501                 }
2502         }
2503
2504         drawfont = hud_font;
2505 }
2506
2507 // Vote window (#9)
2508 //
2509 float vote_yescount;
2510 float vote_nocount;
2511 float vote_needed;
2512 float vote_highlighted; // currently selected vote
2513
2514 float vote_active; // is there an active vote?
2515 float vote_prev; // previous state of vote_active to check for a change
2516 float vote_alpha;
2517 float vote_change; // "time" when vote_active changed
2518
2519 void HUD_VoteWindow(void) 
2520 {
2521         float id = 9;
2522         vector pos, mySize;
2523         pos = HUD_Panel_GetPos(id);
2524         mySize = HUD_Panel_GetSize(id);
2525
2526         string s;
2527         float a;
2528         if(vote_active != vote_prev) {
2529                 vote_change = time;
2530                 vote_prev = bound(0, vote_active, 1);
2531         }
2532
2533         if(vote_active)
2534                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
2535         else
2536                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
2537
2538         if(hud_configure)
2539         {
2540                 vote_alpha = 1;
2541                 vote_yescount = 3;
2542                 vote_nocount = 2;
2543                 vote_needed = 4;
2544         }
2545
2546         HUD_Panel_DrawBg(id, pos, mySize);
2547         float padding;
2548         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2549         if(padding)
2550         {
2551                 pos += '1 1 0' * padding;
2552                 mySize -= '2 2 0' * padding;
2553         }
2554
2555         if(vote_alpha) {
2556                 a = vote_alpha * bound(cvar_or("hud_vote_alreadyvoted_alpha", 0.75), 1 - vote_highlighted, 1);
2557
2558                 drawpic_skin(pos, "voteprogress_back", mySize, HUD_GetBgColor(), a * hud_alpha_bg, DRAWFLAG_NORMAL);
2559
2560                 s = "A vote has been called for: ";
2561                 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_alpha_fg, DRAWFLAG_NORMAL);
2562                 s = textShortenToWidth(vote_called_vote, mySize_x * 0.96, '10 0 0', stringwidth_colors);
2563                 if(hud_configure)
2564                         s = "Configure the HUD";
2565                 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_alpha_fg, DRAWFLAG_NORMAL);
2566
2567                 // print the yes/no counts
2568                 s = strcat("Yes: ", ftos(vote_yescount));
2569                 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_alpha_fg, DRAWFLAG_NORMAL);
2570                 s = strcat("No: ", ftos(vote_nocount));
2571                 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_alpha_fg, DRAWFLAG_NORMAL);
2572
2573                 // draw the progress bars
2574                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
2575                 drawpic_skin(pos, "voteprogress_prog", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2576
2577                 drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
2578                 drawpic_skin(pos, "voteprogress_prog", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2579
2580                 // draw the highlights
2581                 if(vote_highlighted == 1) {
2582                         drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
2583                         drawpic_skin(pos, "voteprogress_voted", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2584                 }
2585                 else if(vote_highlighted == 2) {
2586                         drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
2587                         drawpic_skin(pos, "voteprogress_voted", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2588                 }
2589
2590                 drawresetcliparea();
2591         }
2592         else if(!vote_active) {
2593                 vote_highlighted = 0;
2594         }
2595 }
2596
2597 // Mod icons panel (#10)
2598 //
2599
2600 // CTF HUD modicon section
2601 float redflag_prevframe, blueflag_prevframe; // status during previous frame
2602 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
2603 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
2604
2605 void HUD_Mod_CTF_Reset(void)
2606 {
2607         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
2608 }
2609
2610 void HUD_Mod_CTF(vector pos, vector mySize)
2611 {
2612         vector redflag_pos, blueflag_pos;
2613         float f; // every function should have that
2614
2615         float redflag, blueflag; // current status
2616         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
2617         float stat_items;
2618
2619         stat_items = getstati(STAT_ITEMS);
2620         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
2621         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
2622
2623         if(hud_configure)
2624         {
2625                 redflag = 1;
2626                 blueflag = 2;
2627         }
2628
2629         // when status CHANGES, set old status into prevstatus and current status into status
2630         if (redflag != redflag_prevframe)
2631         {
2632                 redflag_statuschange_time = time;
2633                 redflag_prevstatus = redflag_prevframe;
2634                 redflag_prevframe = redflag;
2635         }
2636
2637         if (blueflag != blueflag_prevframe)
2638         {
2639                 blueflag_statuschange_time = time;
2640                 blueflag_prevstatus = blueflag_prevframe;
2641                 blueflag_prevframe = blueflag;
2642         }
2643
2644         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
2645         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
2646
2647         float BLINK_FACTOR = 0.15;
2648         float BLINK_BASE = 0.85;
2649         // note:
2650         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
2651         // thus
2652         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
2653         // ensure RMS == 1
2654         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
2655
2656         string red_icon, red_icon_prevstatus;
2657         float red_alpha, red_alpha_prevstatus;
2658         red_alpha = red_alpha_prevstatus = 1;
2659         switch(redflag) {
2660                 case 1: red_icon = "flag_red_taken"; break;
2661                 case 2: red_icon = "flag_red_lost"; break;
2662                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2663                 default:
2664                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2665                                 red_icon = "flag_red_shielded";
2666                         else
2667                                 red_icon = string_null;
2668                         break;
2669         }
2670         switch(redflag_prevstatus) {
2671                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
2672                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
2673                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2674                 default:
2675                         if(redflag == 3)
2676                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
2677                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2678                                 red_icon_prevstatus = "flag_red_shielded";
2679                         else
2680                                 red_icon_prevstatus = string_null;
2681                         break;
2682         }
2683
2684         string blue_icon, blue_icon_prevstatus;
2685         float blue_alpha, blue_alpha_prevstatus;
2686         blue_alpha = blue_alpha_prevstatus = 1;
2687         switch(blueflag) {
2688                 case 1: blue_icon = "flag_blue_taken"; break;
2689                 case 2: blue_icon = "flag_blue_lost"; break;
2690                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2691                 default:
2692                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2693                                 blue_icon = "flag_blue_shielded";
2694                         else
2695                                 blue_icon = string_null;
2696                         break;
2697         }
2698         switch(blueflag_prevstatus) {
2699                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
2700                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
2701                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2702                 default:
2703                         if(blueflag == 3)
2704                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
2705                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2706                                 blue_icon_prevstatus = "flag_blue_shielded";
2707                         else
2708                                 blue_icon_prevstatus = string_null;
2709                         break;
2710         }
2711
2712         if (myteam == COLOR_TEAM1) { // always draw own flag on left
2713                 redflag_pos = pos;
2714                 blueflag_pos = pos + eX * mySize_y;
2715         } else {
2716                 blueflag_pos = pos;
2717                 redflag_pos = pos + eX * mySize_y;
2718         }
2719
2720         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
2721         if(red_icon_prevstatus && f < 1)
2722                 drawpic_skin_expanding(redflag_pos, red_icon_prevstatus, '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2723         if(red_icon)
2724                 drawpic_skin(redflag_pos, red_icon, '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg * red_alpha * f, DRAWFLAG_NORMAL);
2725
2726         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
2727         if(blue_icon_prevstatus && f < 1)
2728                 drawpic_skin_expanding(blueflag_pos, blue_icon_prevstatus, '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2729         if(blue_icon)
2730                 drawpic_skin(blueflag_pos, blue_icon, '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg * blue_alpha * f, DRAWFLAG_NORMAL);
2731 }
2732
2733 // Keyhunt HUD modicon section
2734 float kh_runheretime;
2735
2736 void HUD_Mod_KH_Reset(void)
2737 {
2738         kh_runheretime = 0;
2739 }
2740
2741 void HUD_Mod_KH(vector pos, vector mySize)
2742 {
2743         float kh_keys;
2744         float keyteam;
2745         float a, aa;
2746         vector p, pa, kh_size, kh_asize;
2747
2748         p_x = pos_x;
2749         p_y = pos_y + 0.25 * mySize_y;
2750
2751         kh_keys = getstati(STAT_KH_KEYS);
2752
2753         kh_size_x = mySize_x * 0.25;
2754         kh_size_y = 0.75 * mySize_y;
2755
2756         pa = p - eY * 0.25 * mySize_y;
2757
2758         kh_asize_x = mySize_x * 0.25;
2759         kh_asize_y = mySize_y * 0.25;
2760
2761         float i, key;
2762
2763         float keycount;
2764         keycount = 0;
2765         for(i = 0; i < 4; ++i)
2766         {
2767                 key = floor(kh_keys / pow(32, i)) & 31;
2768                 keyteam = key - 1;
2769                 if(keyteam == 30 && keycount <= 4)
2770                         keycount += 4;
2771                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
2772                         keycount += 1;
2773         }
2774         // this yields 8 exactly if "RUN HERE" shows
2775
2776         if(keycount == 8)
2777         {
2778                 if(!kh_runheretime)
2779                         kh_runheretime = time;
2780                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
2781         }
2782         else
2783                 kh_runheretime = 0;
2784
2785         for(i = 0; i < 4; ++i)
2786         {
2787                 key = floor(kh_keys / pow(32, i)) & 31;
2788                 keyteam = key - 1;
2789                 switch(keyteam)
2790                 {
2791                         case 30: // my key
2792                                 keyteam = myteam;
2793                                 a = 1;
2794                                 aa = 1;
2795                                 break;
2796                         case -1: // no key
2797                                 a = 0;
2798                                 aa = 0;
2799                                 break;
2800                         default: // owned or dropped
2801                                 a = 0.2;
2802                                 aa = 0.5;
2803                                 break;
2804                 }
2805                 a = a * hud_alpha_fg;
2806                 aa = aa * hud_alpha_fg;
2807                 if(a > 0)
2808                 {
2809                         switch(keyteam)
2810                         {
2811                                 case COLOR_TEAM1:
2812                                         drawpic_skin(pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2813                                         break;
2814                                 case COLOR_TEAM2:
2815                                         drawpic_skin(pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2816                                         break;
2817                                 case COLOR_TEAM3:
2818                                         drawpic_skin(pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2819                                         break;
2820                                 case COLOR_TEAM4:
2821                                         drawpic_skin(pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2822                                         break;
2823                                 default:
2824                                         break;
2825                         }
2826                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
2827                         {
2828                                 case 0:
2829                                         drawpic_skin(p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2830                                         break;
2831                                 case 1:
2832                                         drawpic_skin(p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2833                                         break;
2834                                 case 2:
2835                                         drawpic_skin(p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2836                                         break;
2837                                 case 3:
2838                                         drawpic_skin(p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2839                                         break;
2840                         }
2841                 }
2842                 p_x += 0.25 * mySize_x;
2843                 pa_x += 0.25 * mySize_x;
2844         }
2845 }
2846
2847 // Nexball HUD mod icon
2848 void HUD_Mod_NexBall(vector pos, vector mySize)
2849 {
2850         float stat_items, nb_pb_starttime, dt, p;
2851
2852         stat_items = getstati(STAT_ITEMS);
2853         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
2854
2855         //Manage the progress bar if any
2856         if (nb_pb_starttime > 0)
2857         {
2858                 vector s;
2859                 dt = mod(time - nb_pb_starttime, nb_pb_period);
2860                 // one period of positive triangle
2861                 p = 2 * dt / nb_pb_period;
2862                 if (p > 1)
2863                         p = 2 - p;
2864
2865                 //Draw the filling
2866                 drawpic_skin(pos, "statusbar", eX * p * mySize_x + eY * mySize_y, HUD_Panel_GetProgressBarColor("nexball"), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
2867         }
2868
2869         pos_x += 0.5 * mySize_x - 0.5 * mySize_y; //horizontal margin to the picture
2870
2871         if (stat_items & IT_KEY1)
2872                 drawpic_skin(pos, "nexball_carrying", '1 1 0' * mySize_y, '1 1 1', 1, DRAWFLAG_NORMAL);
2873 }
2874
2875 // Race/CTS HUD mod icons
2876 float crecordtime_prev; // last remembered crecordtime
2877 float crecordtime_change_time; // time when crecordtime last changed
2878 float srecordtime_prev; // last remembered srecordtime
2879 float srecordtime_change_time; // time when srecordtime last changed
2880
2881 float race_status_time;
2882 float race_status_prev;
2883 string race_status_name_prev;
2884 void HUD_Mod_Race(vector pos, vector mySize)
2885 {
2886         entity me;
2887         me = playerslots[player_localentnum - 1];
2888         float t, score;
2889         float f; // yet another function has this
2890         score = me.(scores[ps_primary]);
2891
2892         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
2893                 return; // no records in the actual race
2894
2895         drawfont = hud_bigfont;
2896
2897         // clientside personal record
2898         string rr;
2899         if(gametype == GAME_CTS)
2900                 rr = CTS_RECORD;
2901         else
2902                 rr = RACE_RECORD;
2903         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
2904
2905         if(score && (score < t || !t)) {
2906                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
2907                 if(cvar("cl_autodemo_delete_keeprecords"))
2908                 {
2909                         f = cvar("cl_autodemo_delete");
2910                         f &~= 1;
2911                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
2912                 }
2913         }
2914
2915         if(t != crecordtime_prev) {
2916                 crecordtime_prev = t;
2917                 crecordtime_change_time = time;
2918         }
2919         f = time - crecordtime_change_time;
2920
2921         if (f > 1) {
2922                 drawstring(pos, "Personal best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2923                 drawstring(pos + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t), '1 1 0' * 0.2 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2924         } else {
2925                 drawstring(pos, "Personal best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2926                 drawstring(pos + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t), '1 1 0' * 0.2 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2927                 drawstring_expanding(pos, "Personal best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2928                 drawstring_expanding(pos + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t), '1 1 0' * 0.2 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2929         }
2930
2931         // server record
2932         t = race_server_record;
2933         if(t != srecordtime_prev) {
2934                 srecordtime_prev = t;
2935                 srecordtime_change_time = time;
2936         }
2937         f = time - srecordtime_change_time;
2938
2939         if (f > 1) {
2940                 drawstring(pos + eY * 0.5 * mySize_y, "Server best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2941                 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_alpha_fg, DRAWFLAG_NORMAL);
2942         } else {
2943                 drawstring(pos + eY * 0.5 * mySize_y, "Server best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2944                 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_alpha_fg, DRAWFLAG_NORMAL);
2945                 drawstring_expanding(pos + eY * 0.5 * mySize_y, "Server best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2946                 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_alpha_fg, DRAWFLAG_NORMAL, f);
2947         }
2948
2949         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
2950                 race_status_time = time + 5;
2951                 race_status_prev = race_status;
2952                 if (race_status_name_prev)
2953                         strunzone(race_status_name_prev);
2954                 race_status_name_prev = strzone(race_status_name);
2955         }
2956
2957         pos_x += mySize_x/2;
2958         // race "awards"
2959         float a;
2960         a = bound(0, race_status_time - time, 1);
2961
2962         string s;
2963         s = textShortenToWidth(race_status_name, 120, '10 10 0', stringwidth_colors);
2964
2965         float rank;
2966         if(race_status > 0)
2967                 rank = race_CheckName(race_status_name);
2968         string rankname;
2969         rankname = race_PlaceName(rank);
2970
2971         if(race_status == 0)
2972                 drawpic_skin(pos, "race_newfail", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2973         else if(race_status == 1) {
2974                 drawpic_skin(pos, "race_newtime", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2975                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2976                 drawstring(pos + '40 20 0' - eX * stringwidth(rankname, TRUE, '7 0 0'), rankname, '14 14 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2977         } else if(race_status == 2) {
2978                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
2979                         drawpic_skin(pos, "race_newrankgreen", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2980                 else
2981                         drawpic_skin(pos, "race_newrankyellow", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2982                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2983                 drawstring(pos + '40 20 0' - eX * stringwidth(rankname, TRUE, '7 0 0'), rankname, '14 14 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2984         } else if(race_status == 3) {
2985                 drawpic_skin(pos, "race_newrecordserver", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2986                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2987                 drawstring(pos + '40 20 0' - eX * stringwidth(rankname, TRUE, '7 0 0'), rankname, '14 14 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2988         }
2989
2990         if (race_status_time - time <= 0) {
2991                 race_status_prev = -1;
2992                 race_status = -1;
2993                 if(race_status_name)
2994                         strunzone(race_status_name);
2995                 race_status_name = string_null;
2996                 if(race_status_name_prev)
2997                         strunzone(race_status_name_prev);
2998                 race_status_name_prev = string_null;
2999         }
3000         drawfont = hud_font;
3001 }
3002
3003 // TODO: idea: alpha fade in/out empty panels
3004 void HUD_ModIcons(void)
3005 {
3006         if (gametype != GAME_KEYHUNT && gametype != GAME_CTF && gametype != GAME_NEXBALL && gametype != GAME_CTS && gametype != GAME_RACE && !hud_configure)
3007                 return;
3008         float id = 10;
3009         vector pos, mySize;
3010         pos = HUD_Panel_GetPos(id);
3011         mySize = HUD_Panel_GetSize(id);
3012
3013         HUD_Panel_DrawBg(id, pos, mySize);
3014         float padding;
3015         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
3016         if(padding)
3017         {
3018                 pos += '1 1 0' * padding;
3019                 mySize -= '2 2 0' * padding;
3020         }
3021
3022         // TODO... well make them work in a panel etc
3023         if(gametype == GAME_KEYHUNT)
3024                 HUD_Mod_KH(pos, mySize);
3025         else if(gametype == GAME_CTF || hud_configure)
3026                 HUD_Mod_CTF(pos, mySize);
3027         else if(gametype == GAME_NEXBALL)
3028                 HUD_Mod_NexBall(pos, mySize);
3029         else if(gametype == GAME_CTS || gametype == GAME_RACE)
3030                 HUD_Mod_Race(pos, mySize);
3031 }
3032
3033 // Draw pressed keys (#11)
3034 //
3035 void HUD_DrawPressedKeys(void)
3036 {
3037         float id = 11;
3038         vector pos, mySize;
3039         pos = HUD_Panel_GetPos(id);
3040         mySize = HUD_Panel_GetSize(id);
3041
3042         HUD_Panel_DrawBg(id, pos, mySize);
3043         float padding;
3044         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
3045         if(padding)
3046         {
3047                 pos += '1 1 0' * padding;
3048                 mySize -= '2 2 0' * padding;
3049         }
3050
3051         float pressedkeys;
3052
3053         pressedkeys = getstatf(STAT_PRESSED_KEYS);
3054         drawpic_skin(pos, "key_bg.tga",           mySize, '1 1 1', 0.1 * hud_alpha_fg, DRAWFLAG_NORMAL);
3055         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_alpha_fg, DRAWFLAG_NORMAL);
3056         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_alpha_fg, DRAWFLAG_NORMAL);
3057         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_alpha_fg, DRAWFLAG_NORMAL);
3058         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_alpha_fg, DRAWFLAG_NORMAL);
3059         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_alpha_fg, DRAWFLAG_NORMAL);
3060         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_alpha_fg, DRAWFLAG_NORMAL);
3061 }
3062
3063 /*
3064 ==================
3065 Main HUD system
3066 ==================
3067 */
3068
3069 void HUD_ShowSpeed(void)
3070 {
3071         vector numsize;
3072         float pos, conversion_factor;
3073         string speed, zspeed, unit;
3074
3075         switch(cvar("cl_showspeed_unit"))
3076         {
3077                 default:
3078                 case 0:
3079                         unit = "";
3080                         conversion_factor = 1.0;
3081                         break;
3082                 case 1:
3083                         unit = " qu/s";
3084                         conversion_factor = 1.0;
3085                         break;
3086                 case 2:
3087                         unit = " m/s";
3088                         conversion_factor = 0.0254;
3089                         break;
3090                 case 3:
3091                         unit = " km/h";
3092                         conversion_factor = 0.0254 * 3.6;
3093                         break;
3094                 case 4:
3095                         unit = " mph";
3096                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
3097                         break;
3098                 case 5:
3099                         unit = " knots";
3100                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
3101                         break;
3102         }
3103
3104         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
3105
3106         numsize_x = numsize_y = cvar("cl_showspeed_size");
3107         pos = (vid_conheight - numsize_y) * cvar("cl_showspeed_position");
3108
3109         drawfont = hud_bigfont;
3110         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3111
3112         if (cvar("cl_showspeed_z") == 1) {
3113                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
3114                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3115         }
3116
3117         drawfont = hud_font;
3118 }
3119
3120 vector acc_prevspeed;
3121 float acc_prevtime;
3122 float acc_avg;
3123
3124 void HUD_ShowAcceleration(void)
3125 {
3126         float acceleration, sz, scale, alpha, f;
3127         vector pos, top, rgb;
3128         top_x = vid_conwidth/2;
3129         top_y = 0;
3130
3131         f = time - acc_prevtime;
3132         if(cvar("cl_showacceleration_z"))
3133                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
3134         else
3135                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
3136         acc_prevspeed = pmove_vel;
3137         acc_prevtime = time;
3138
3139         f = bound(0, f * 10, 1);
3140         acc_avg = acc_avg * (1 - f) + acceleration * f;
3141         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
3142
3143         pos = top - sz/2 * eY + (cvar("cl_showacceleration_position") * vid_conheight) * eY;
3144
3145         sz = cvar("cl_showacceleration_size");
3146         scale = cvar("cl_showacceleration_scale");
3147         alpha = cvar("cl_showacceleration_alpha");
3148         if (cvar("cl_showacceleration_color_custom"))
3149                 rgb = stov(cvar_string("cl_showacceleration_color"));
3150         else {
3151                 rgb = '1 1 1';
3152                 if (acceleration < 0) {
3153                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
3154                 } else if (acceleration > 0) {
3155                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
3156                 }
3157         }
3158
3159         if (acceleration > 0)
3160                 drawpic_skin(pos, "statusbar", acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3161         else if (acceleration < 0)
3162                 drawpic_skin(pos + acceleration * scale * '40 0 0', "statusbar", -acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3163 }
3164
3165 void HUD_Reset (void)
3166 {
3167         // reset gametype specific icons
3168         if(gametype == GAME_KEYHUNT)
3169                 HUD_Mod_KH_Reset();
3170         else if(gametype == GAME_CTF)
3171                 HUD_Mod_CTF_Reset();
3172 }
3173
3174 void HUD_Main (void)
3175 {
3176         hud_alpha_bg = cvar_or("hud_bg_alpha", 0.8) * (1 - cvar("_menu_alpha"));
3177         hud_border_thickness = bound(0, cvar("hud_border_thickness"), 5);
3178         hud_accuracy_border_thickness = bound(0, cvar_or("hud_accuracy_border_thickness", 1), 5);
3179         hud_color_bg_team = cvar("hud_color_bg_team");
3180
3181         hud_fontsize = HUD_GetFontsize("hud_fontsize");
3182         hud_fontsize_spec = HUD_GetFontsize("hud_fontsize_spec");
3183
3184         hud_configure = cvar("_hud_configure");
3185
3186         // Drawing stuff
3187         if(cvar("hud_dock"))
3188                 drawpic_skin('0 0 0', "dock", eX * vid_conwidth + eY * vid_conheight, stov(cvar_string("hud_dock_color")), cvar("hud_dock_alpha"), DRAWFLAG_NORMAL);
3189
3190         if(HUD_Panel_CheckActive(0))
3191                 HUD_WeaponIcons();
3192         if(HUD_Panel_CheckActive(1))
3193                 HUD_Inventory();
3194         if(HUD_Panel_CheckActive(2))
3195                 HUD_Powerups();
3196         if(HUD_Panel_CheckActive(3))
3197                 HUD_HealthArmor();
3198         if(HUD_Panel_CheckActive(4))
3199                 HUD_Notify();
3200         if(HUD_Panel_CheckActive(5))
3201                 HUD_Timer();
3202         // TODO hud'ify
3203         if(HUD_Panel_CheckActive(6))
3204                 if(ons_showmap || cvar_string("cl_teamradar") != "0" && (cvar("cl_teamradar") == 2 || teamplay))
3205                         HUD_Radar();
3206         if(HUD_Panel_CheckActive(7))
3207                 HUD_Score();
3208         if(HUD_Panel_CheckActive(8))
3209                 if(gametype == GAME_RACE || gametype == GAME_CTS || hud_configure)
3210                         HUD_RaceTimer();
3211         if(HUD_Panel_CheckActive(9))
3212                 HUD_VoteWindow();
3213         if(HUD_Panel_CheckActive(10))
3214                 HUD_ModIcons();
3215         // TODO hud'ify
3216         if(HUD_Panel_CheckActive(11))
3217                 if(spectatee_status > 0 || cvar("hud_pressedkeys") >= 2 || hud_configure)
3218                         HUD_DrawPressedKeys();
3219
3220         // TODO hud_'ify these
3221         if (cvar("cl_showspeed"))
3222                 HUD_ShowSpeed();
3223         if (cvar("cl_showacceleration"))
3224                 HUD_ShowAcceleration();
3225 }