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