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