]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
same thing for powerups panel
[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_x = max(mySize_y * 2, mySize_x); // at least 2 * height
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 alpha, height, accuracybar_height, stat_weapons; // "constants"
1131         vector pos, mySize, mysize, mypos, accuracy_color;
1132         float i, id, fade, weapon_stats, weapon_hit, weapon_damage, weapon_cnt; // variables
1133
1134         pos = HUD_Panel_GetPos(0);
1135         mySize = HUD_Panel_GetSize(0);
1136         accuracybar_height = cvar_or("hud_weaponicons_accuracy_height", 3);
1137
1138         stat_weapons = getstati(STAT_WEAPONS);
1139         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1140         {
1141                 self = get_weaponinfo(i);
1142                 if(self.weapons && (self.impulse >= 0) && (stat_weapons & self.weapons) || hud_configure)
1143                 {
1144                         weaponorder[weapon_cnt] = self;
1145                         ++weapon_cnt;
1146                 }
1147         }
1148         heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
1149
1150         HUD_Panel_DrawBg(0, pos, mySize);
1151
1152         // hits
1153         weapon_stats = getstati(STAT_DAMAGE_HITS);
1154         weapon_number = weapon_stats & 63;
1155         weapon_hits[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1156         // fired
1157         weapon_stats = getstati(STAT_DAMAGE_FIRED);
1158         weapon_number = weapon_stats & 63;
1159         weapon_fired[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1160
1161         if(cvar_or("hud_weaponicons_fade", 1))
1162         {
1163                 fade = 3.2 - 2 * (time - weapontime);
1164                 fade = bound(0.7, fade, 1);
1165         }
1166         else
1167                 fade = 1;
1168
1169         HUD_WeaponIcons_Clear();
1170
1171         float rows, columns;
1172         rows = mySize_y/mySize_x;
1173         rows = bound(1, floor((sqrt(4 * (2/1) * rows * WEP_COUNT + rows * rows) + rows + 0.5) / 2), WEP_COUNT);
1174         //                               ^^^ weapon icon aspect goes here
1175
1176         columns = ceil(WEP_COUNT/rows);
1177         float row, column;
1178         for(i = 0; i < weapon_cnt; ++i)
1179         {
1180                 self = weaponorder[i];
1181                 if((self.weapons && (self.impulse >= 0) && (stat_weapons & self.weapons)) || hud_configure)
1182                 {
1183                         id = self.impulse;
1184
1185                         alpha = (self.weapon == activeweapon) ? 1 : 0.6;
1186
1187                         weapon_hit = weapon_hits[self.weapon-WEP_FIRST];
1188                         weapon_damage = weapon_fired[self.weapon-WEP_FIRST];
1189
1190                         if(self.weapon == activeweapon)
1191                                 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);
1192                         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);
1193
1194                         if(cvar_or("hud_weaponicons_number", 1))
1195                                 drawstring(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), ftos(id), '1 1 0' * 0.5 * mySize_y*(1/rows), '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1196
1197                         // draw the weapon accuracy on the HUD
1198                         if(hud_accuracy_hud && !(gametype == GAME_RACE || gametype == GAME_CTS))
1199                         {
1200                                 if(weapon_damage)
1201                                         weapon_stats = floor(100 * weapon_hit / weapon_damage);
1202
1203                                 accuracy_color = HUD_AccuracyColor(weapon_stats);
1204                                 if(weapon_damage)
1205                                         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);
1206                         }
1207                 }
1208
1209                 ++row;
1210                 if(row >= rows)
1211                 {
1212                         row = 0;
1213                         ++column;
1214                 }
1215         }
1216
1217 }
1218
1219 // Inventory (#1)
1220 //
1221
1222 float GetAmmoStat(float i)
1223 {
1224         switch(i)
1225         {
1226                 case 0: return STAT_SHELLS;
1227                 case 1: return STAT_NAILS;
1228                 case 2: return STAT_ROCKETS;
1229                 case 3: return STAT_CELLS;
1230                 case 4: return STAT_FUEL;
1231                 default: return -1;
1232         }
1233 }
1234
1235 float GetAmmoItemCode(float i)
1236 {
1237         switch(i)
1238         {
1239                 case 0: return IT_SHELLS;
1240                 case 1: return IT_NAILS;
1241                 case 2: return IT_ROCKETS;
1242                 case 3: return IT_CELLS;
1243                 case 4: return IT_FUEL;
1244                 default: return -1;
1245         }
1246 }
1247
1248 string GetAmmoPicture(float i)
1249 {
1250         switch(i)
1251         {
1252                 case 0: return "ammo_shells";
1253                 case 1: return "ammo_bullets";
1254                 case 2: return "ammo_rocket";
1255                 case 3: return "ammo_cells";
1256                 case 4: return "ammo_fuel";
1257                 default: return "";
1258         }
1259 }
1260
1261 void HUD_Inventory()
1262 {
1263         float i;
1264         float stat_items;
1265
1266         vector pos, mySize, mysize, mypos;
1267         pos = HUD_Panel_GetPos(1);
1268         mySize = HUD_Panel_GetSize(1);
1269
1270         HUD_Panel_DrawBg(1, pos, mySize);
1271
1272         // ammo
1273         stat_items = getstati(STAT_ITEMS);
1274         for (i = 0; i < 4; ++i) {
1275                 float a;
1276                 a = getstati(GetAmmoStat(i)); // how much ammo do we have of type i?
1277                 if(hud_configure)
1278                         a = 100;
1279
1280                 if(cvar("hud_inventory_onlycurrent")) {
1281                         if (stat_items & GetAmmoItemCode(i)) {
1282                                 drawpic_skin(pos, "ammo_current_bg", mySize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1283                                 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);
1284                                 if(a < 10)
1285                                         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);
1286                                 else
1287                                         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);
1288                         }
1289                 } else {
1290                         if (a > 0) {
1291                                 if(mySize_x/mySize_y >= 10) { // arrange horizontally
1292                                         switch (i) {
1293                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1294                                                 case 1: mypos_x = pos_x + 0.25 * mySize_x;      mypos_y = pos_y;                        break; // bullets
1295                                                 case 2: mypos_x = pos_x + 0.5  * mySize_x;      mypos_y = pos_y;                        break; // rockets
1296                                                 case 3: mypos_x = pos_x + 0.75 * mySize_x;      mypos_y = pos_y;                        break; // cells
1297                                         }
1298                                         mysize_x = 0.25 * mySize_x;
1299                                         mysize_y = mySize_y;
1300                                 } else if(mySize_x/mySize_y >= 2.5) { // arrange in a 2x2 grid
1301                                         switch (i) {
1302                                                 case 0: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y + 0.5 * mySize_y;       break; // shells
1303                                                 case 1: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y;                        break; // bullets
1304                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5 * mySize_y;       break; // rockets
1305                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // cells
1306                                         }
1307                                         mysize_x = 0.5 * mySize_x;
1308                                         mysize_y = 0.5 * mySize_y;
1309                                 } else { // arrange vertically
1310                                         switch (i) {
1311                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1312                                                 case 1: mypos_x = pos_x;                        mypos_y = pos_y + 0.25 * mySize_y;      break; // bullets
1313                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5  * mySize_y;      break; // rockets
1314                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y + 0.75 * mySize_y;      break; // cells
1315                                         }
1316                                         mysize_x = mySize_x;
1317                                         mysize_y = 0.25 * mySize_y;
1318                                 }
1319
1320                                 if (stat_items & GetAmmoItemCode(i))
1321                                         drawpic_skin(mypos, "ammo_current_bg", mysize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1322                                 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);
1323                                 if (a < 10) {
1324                                         if(stat_items & GetAmmoItemCode(i))
1325                                                 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);
1326                                         else
1327                                                 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);
1328                                 } else {
1329                                         if(stat_items & GetAmmoItemCode(i))
1330                                                 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);
1331                                         else
1332                                                 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);
1333                                 }
1334                         }
1335                 }
1336         }
1337 }
1338
1339
1340 // Powerups (#2)
1341 float shield_maxtime;
1342 float strength_maxtime;
1343 void HUD_Powerups() {
1344         float stat_items;
1345         stat_items = getstati(STAT_ITEMS);
1346
1347         if(!hud_configure)
1348         {
1349                 if not(stat_items & IT_STRENGTH)
1350                         if not(stat_items & IT_INVINCIBLE)
1351                                 return;
1352
1353                 if (getstati(STAT_HEALTH) <= 0)
1354                         return;
1355         }
1356
1357         vector pos, mySize;
1358         pos = HUD_Panel_GetPos(2);
1359         mySize = HUD_Panel_GetSize(2);
1360
1361         HUD_Panel_DrawBg(2, pos, mySize);
1362
1363         float strength_time, shield_time;
1364
1365         strength_time = bound(0, ceil(getstatf(STAT_STRENGTH_FINISHED) - time), 99);
1366         shield_time = bound(0, ceil(getstatf(STAT_INVINCIBLE_FINISHED) - time), 99);
1367
1368         if(hud_configure)
1369         {
1370                 strength_time = 15;
1371                 shield_time = 27;
1372         }
1373
1374         float len;
1375
1376         vector barpos, barsize;
1377         vector picpos;
1378         vector numpos;
1379
1380         string leftname, rightname;
1381         float leftcnt, rightcnt;
1382         float leftalpha, rightalpha;
1383         if (cvar(strcat("hud_", HUD_Panel_GetName(2), "_flip"))) {
1384                 leftname = "strength";
1385                 leftcnt = strength_time;
1386
1387                 rightname = "shield";
1388                 rightcnt = shield_time;
1389         } else {
1390                 leftname = "shield";
1391                 leftcnt = shield_time;
1392
1393                 rightname = "strength";
1394                 rightcnt = strength_time;
1395         }
1396         leftalpha = bound(0, leftcnt, 1);
1397         rightalpha = bound(0, rightcnt, 1);
1398
1399         // TODO: tile statusbars?
1400         if (mySize_x/mySize_y > 4)
1401         {
1402                 if(leftcnt)
1403                 {
1404                         len = strlen(ftos(leftcnt));
1405
1406                         if(cvar(strcat("hud_", HUD_Panel_GetName(2), "_mirror"))) {
1407                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/30);
1408                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1409                                 picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
1410                                 numpos = picpos - eX * 2 * 0.5 * mySize_y + eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1411                         } else {
1412                                 barpos = pos;
1413                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1414                                 picpos = pos;
1415                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1416                         }
1417
1418                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1419                         drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1420                         HUD_DrawXNum(numpos, leftcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1421                 }
1422
1423                 if(rightcnt)
1424                 {
1425                         len = strlen(ftos(rightcnt));
1426
1427                         if(cvar(strcat("hud_", HUD_Panel_GetName(2), "_mirror"))) {
1428                                 barpos = pos + eX * 0.5 * mySize_x;
1429                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1430                                 picpos = pos + eX * 0.5 * mySize_x;
1431                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1432                         } else {
1433                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/30);
1434                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1435                                 picpos = pos + eX * mySize_x - eX * mySize_y;
1436                                 numpos = picpos - eX * mySize_y + eY * 0.25 * mySize_y;
1437                         }
1438
1439                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1440                         drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1441                         HUD_DrawXNum(numpos, rightcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1442                 }
1443         }
1444         else if (mySize_x/mySize_y > 1.5)
1445         {
1446                 if(leftcnt)
1447                 {
1448                         len = strlen(ftos(leftcnt));
1449
1450                         if(cvar(strcat("hud_", HUD_Panel_GetName(2), "_mirror"))) {
1451                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/30);
1452                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1453                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
1454                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1455                         } else {
1456                                 barpos = pos;
1457                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1458                                 picpos = pos;
1459                                 numpos = picpos + eX * 0.5 * mySize_y;
1460                         }
1461
1462                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1463                         drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1464                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1465                 }
1466
1467                 if(rightcnt)
1468                 {
1469                         len = strlen(ftos(rightcnt));
1470
1471                         if(cvar(strcat("hud_", HUD_Panel_GetName(2), "_mirror"))) {
1472                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1473                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1474                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
1475                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1476                         } else {
1477                                 barpos = pos + eY * 0.5 * mySize_y;
1478                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1479                                 picpos = pos + eY * 0.5 * mySize_y;
1480                                 numpos = picpos + eX * 0.5 * mySize_y;
1481                         }
1482
1483                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1484                         drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1485                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1486                 }
1487         }
1488         else
1489         {
1490                 if(leftcnt)
1491                 {
1492                         len = strlen(ftos(leftcnt));
1493
1494                         if(cvar(strcat("hud_", HUD_Panel_GetName(2), "_mirror"))) {
1495                                 barpos = pos;
1496                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1497                                 picpos = pos + eX * 0.05 * mySize_x;
1498                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
1499                         } else {
1500                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/30);
1501                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1502                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x;
1503                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x;
1504                         }
1505
1506                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1507                         drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1508                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1509                 }
1510
1511                 if(rightcnt)
1512                 {
1513                         len = strlen(ftos(rightcnt));
1514
1515                         if(cvar(strcat("hud_", HUD_Panel_GetName(2), "_mirror"))) {
1516                                 barpos = pos + eX * 0.5 * mySize_x;
1517                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1518                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1519                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1520                         } else {
1521                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/30) + eX * 0.5 * mySize_x;
1522                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1523                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x + eX * 0.5 * mySize_x;
1524                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
1525                         }
1526
1527                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1528                         drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1529                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1530                 }
1531         }
1532
1533         /* TODO: add expand from old code!
1534         //strength
1535         if (strength_time) {
1536                 dt = strength_time - time;
1537                 if(dt > 0)
1538                 {
1539                         if(dt < 5)
1540                         {
1541                                 drawpic_expanding_two(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1542                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1543                         }
1544                         else
1545                         {
1546                                 drawpic_skin(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
1547                         }
1548                         HUD_DrawXNum(pos - '40 -2 0', ceil(dt), 2, 0, countdown_fontsize, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1549                 }
1550                 else if(dt > -1)
1551                 {
1552                         drawpic_expanding(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1553                                 bound(0, -dt / 0.5, 1));
1554                 }
1555         }
1556
1557         //invincibility
1558         if (invincibility_time) {
1559                 dt = invincibility_time - time;
1560                 if(dt > 0)
1561                 {
1562                         if(dt < 5)
1563                         {
1564                                 drawpic_expanding_two(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1565                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1566                         }
1567                         else
1568                         {
1569                                 drawpic_skin(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
1570                         }
1571                         HUD_DrawXNum(pos - '40 -24 0', ceil(dt), 2, 0, countdown_fontsize, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1572                 }
1573                 else if(dt > -1)
1574                 {
1575                         drawpic_expanding(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1576                                 bound(0, -dt / 0.5, 1));
1577                 }
1578         }
1579         */
1580 }
1581
1582 // Health/armor (#3)
1583 //
1584 void HUD_HealthArmor(void)
1585 {
1586         vector pos, mySize;
1587         pos = HUD_Panel_GetPos(3);
1588         mySize = HUD_Panel_GetSize(3);
1589
1590         HUD_Panel_DrawBg(3, pos, mySize);
1591
1592         float armor, health, x;
1593         armor = getstati(STAT_ARMOR);
1594         health = getstati(STAT_HEALTH);
1595
1596         float fuel;
1597         fuel = getstati(GetAmmoStat(4)); // how much fuel do we have?
1598
1599         if(hud_configure)
1600         {
1601                 armor = 150;
1602                 health = 100;
1603                 fuel = 70;
1604         }
1605
1606         if(health <= 0)
1607                 return;
1608
1609         float len;
1610
1611         // TODO!
1612         /*
1613         if(hud_hudselector == 2) // combined health and armor display
1614         {
1615                 vector v;
1616                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
1617
1618                 vector num_pos;
1619                 num_pos = - '96 28 0';
1620
1621                 x = floor(v_x + 1);
1622
1623                 if(v_z) // fully armored
1624                 {
1625                         // here, armorideal > armor
1626                         drawpic_skin(num_pos + '78 -4.5 0', "health", '32 32 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1627                         drawpic_skin(num_pos + '108 -4.5 0', "armor", '20 20 0', '1 1 1', hud_alpha_fg * armor / v_y, DRAWFLAG_NORMAL);
1628                 }
1629                 else
1630                 {
1631                         drawpic_skin(num_pos + '108 -4.5 0', "health", '20 20 0', '1 1 1', hud_alpha_fg * v_y / armor, DRAWFLAG_NORMAL);
1632                         drawpic_skin(num_pos + '78 -4.5 0', "armor", '32 32 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1633                 }
1634                 HUD_DrawXNum_Colored(num_pos, x, 3, 24, hud_alpha_fg); // draw the combined health and armor
1635         }
1636
1637         else
1638         {
1639         */
1640         vector barpos, barsize;
1641         vector picpos;
1642         vector numpos;
1643
1644         string leftname, rightname;
1645         float leftcnt, rightcnt;
1646         float leftactive, rightactive;
1647         float leftalpha, rightalpha;
1648         // TODO!
1649         if (cvar(strcat("hud_", HUD_Panel_GetName(3), "_flip"))) { // old style layout with armor left/top of health
1650                 leftname = "armor";
1651                 leftcnt = armor;
1652                 if(leftcnt)
1653                         leftactive = 1;
1654                 leftalpha = min((armor+10)/55, 1);
1655
1656                 rightname = "health";
1657                 rightcnt = health;
1658                 rightactive = 1;
1659                 rightalpha = 1;
1660         } else {
1661                 leftname = "health";
1662                 leftcnt = health;
1663                 leftactive = 1;
1664                 leftalpha = 1;
1665
1666                 rightname = "armor";
1667                 rightcnt = armor;
1668                 if(rightcnt)
1669                         rightactive = 1;
1670                 rightalpha = min((armor+10)/55, 1);
1671         }
1672
1673         // TODO: tile statusbars?
1674         if (mySize_x/mySize_y > 5)
1675         {
1676                 if(leftactive)
1677                 {
1678                         len = strlen(ftos(leftcnt));
1679
1680                         if(cvar(strcat("hud_", HUD_Panel_GetName(3), "_mirror"))) {
1681                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/200);
1682                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1683                                 picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
1684                                 numpos = picpos - eX * 3 * 0.5 * mySize_y + eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1685                         } else {
1686                                 barpos = pos;
1687                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1688                                 picpos = pos;
1689                                 numpos = picpos + eX * mySize_y + eY * 0.25 * mySize_y;
1690                         }
1691
1692                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1693                         drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1694                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1695                 }
1696
1697                 if(rightactive)
1698                 {
1699                         len = strlen(ftos(rightcnt));
1700
1701                         if(cvar(strcat("hud_", HUD_Panel_GetName(3), "_mirror"))) {
1702                                 barpos = pos + eX * 0.5 * mySize_x;
1703                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1704                                 picpos = pos + eX * 0.5 * mySize_x;
1705                                 numpos = picpos + eX * mySize_y - eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1706                         } else {
1707                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/200);
1708                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1709                                 picpos = pos + eX * mySize_x - eX * mySize_y;
1710                                 numpos = picpos - eX * 1.5 * mySize_y + eY * 0.25 * mySize_y;
1711                         }
1712
1713                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1714                         drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1715                         HUD_DrawXNum_Colored(numpos, rightcnt, 3, 0.5 * mySize_y, hud_alpha_fg);
1716                 }
1717
1718                 if(fuel)
1719                         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);
1720         }
1721         else if (mySize_x/mySize_y > 2)
1722         {
1723                 if(leftactive)
1724                 {
1725                         len = strlen(ftos(leftcnt));
1726
1727                         if(cvar(strcat("hud_", HUD_Panel_GetName(3), "_mirror"))) {
1728                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/200);
1729                                 barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
1730                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
1731                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1732                         } else {
1733                                 barpos = pos;
1734                                 barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
1735                                 picpos = pos;
1736                                 numpos = picpos + eX * 0.5 * mySize_y;
1737                         }
1738
1739                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1740                         drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1741                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1742                 }
1743
1744                 if(rightactive)
1745                 {
1746                         len = strlen(ftos(rightcnt));
1747
1748                         if(cvar(strcat("hud_", HUD_Panel_GetName(3), "_mirror"))) {
1749                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1750                                 barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1751                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
1752                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1753                         } else {
1754                                 barpos = pos + eY * 0.5 * mySize_y;
1755                                 barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1756                                 picpos = pos + eY * 0.5 * mySize_y;
1757                                 numpos = picpos + eX * 0.5 * mySize_y;
1758                         }
1759
1760                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1761                         drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1762                         HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1763                 }
1764
1765                 if(cvar(strcat("hud_", HUD_Panel_GetName(3), "_mirror"))) {
1766                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
1767                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
1768                 } else {
1769                         barpos = pos;
1770                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
1771                 }
1772                 if(fuel)
1773                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1774         }
1775         else
1776         {
1777                 if(leftactive)
1778                 {
1779                         len = strlen(ftos(leftcnt));
1780
1781                         if(cvar(strcat("hud_", HUD_Panel_GetName(3), "_mirror"))) {
1782                                 barpos = pos;
1783                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
1784                                 picpos = pos + eX * 0.05 * mySize_x;
1785                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
1786                         } else {
1787                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/200);
1788                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
1789                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x;
1790                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x;
1791                         }
1792
1793                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1794                         drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1795                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.166 * mySize_x, hud_alpha_fg);
1796                 }
1797
1798                 if(rightactive)
1799                 {
1800                         len = strlen(ftos(rightcnt));
1801
1802                         if(cvar(strcat("hud_", HUD_Panel_GetName(3), "_mirror"))) {
1803                                 barpos = pos + eX * 0.5 * mySize_x;
1804                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
1805                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1806                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1807                         } else {
1808                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/200) + eX * 0.5 * mySize_x;
1809                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
1810                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x + eX * 0.5 * mySize_x;
1811                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x + eX * 0.5 * mySize_x;
1812                         }
1813
1814                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1815                         drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1816                         HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.166 * mySize_x, hud_alpha_fg);
1817                 }
1818
1819                 if(cvar(strcat("hud_", HUD_Panel_GetName(3), "_mirror"))) {
1820                         barpos = pos;
1821                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
1822                 } else {
1823                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, fuel/100);
1824                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
1825                 }
1826                 if(fuel)
1827                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1828         }
1829 }
1830
1831 // ___TODO___ !!!
1832 // Notification area (#4)
1833 void HUD_Notify (void)
1834 {
1835         vector pos, mySize;
1836         pos = HUD_Panel_GetPos(4);
1837         mySize = HUD_Panel_GetSize(4);
1838
1839         HUD_Panel_DrawBg(4, pos, mySize);
1840
1841         string s;
1842         entity tm;
1843         if(spectatee_status && !intermission)
1844         {
1845                 drawfont = hud_bigfont;
1846                 if(spectatee_status == -1)
1847                         s = "^1Observing";
1848                 else
1849                         s = GetPlayerName(spectatee_status - 1);
1850                 // spectated player name between HUD and chat area, aligned to the left
1851                 pos_x = 0;
1852                 pos_y = - 50 - hud_fontsize_spec_y;
1853                 s = textShortenToWidth(s, vid_conwidth/2.5, hud_fontsize_spec, stringwidth_colors);
1854                 drawcolorcodedstring(pos, s, hud_fontsize_spec, hud_alpha_fg, DRAWFLAG_NORMAL);
1855                 drawfont = hud_font;
1856
1857                 // spectator text in the upper right corner
1858                 if(spectatee_status == -1)
1859                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 to spectate");
1860                 else
1861                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 for another player");
1862
1863                 if(spectatee_status == -1)
1864                         s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
1865                 else
1866                         s = strcat("^1Press ^3", getcommandkey("secondary fire", "+attack2"), "^1 to observe");
1867
1868                 s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
1869
1870                 if(gametype == GAME_ARENA)
1871                         s = "^1Wait for your turn to join";
1872                 else if(gametype == GAME_LMS)
1873                 {
1874                         entity sk;
1875                         sk = playerslots[player_localentnum - 1];
1876                         if(sk.(scores[ps_primary]) >= 666)
1877                                 s = "^1Match has already begun";
1878                         else if(sk.(scores[ps_primary]) > 0)
1879                                 s = "^1You have no more lives left";
1880                         else
1881                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1882                 }
1883                 else
1884                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1885
1886                 //show restart countdown:
1887                 if (time < getstatf(STAT_GAMESTARTTIME)) {
1888                         float countdown;
1889                         //we need to ceil, otherwise the countdown would be off by .5 when using round()
1890                         countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
1891                         s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
1892                 }
1893         }
1894         if(warmup_stage && !intermission)
1895         {
1896                 s = "^2Currently in ^1warmup^2 stage!";
1897         }
1898
1899         // move more important stuff more to the middle so its more visible
1900
1901         string blinkcolor;
1902         if(mod(time, 1) >= 0.5)
1903                 blinkcolor = "^1";
1904         else
1905                 blinkcolor = "^3";
1906
1907         if(ready_waiting && !intermission && !spectatee_status)
1908         {
1909                 if(ready_waiting_for_me)
1910                 {
1911                         if(warmup_stage)
1912                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
1913                         else
1914                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
1915                 }
1916                 else
1917                 {
1918                         if(warmup_stage)
1919                                 s = strcat("^2Waiting for others to ready up to end warmup...");
1920                         else
1921                                 s = strcat("^2Waiting for others to ready up...");
1922                 }
1923         }
1924         else if(warmup_stage && !intermission && !spectatee_status)
1925         {
1926                 s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
1927         }
1928
1929         if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
1930         {
1931                 float ts_min, ts_max;
1932                 tm = teams.sort_next;
1933                 if (tm)
1934                 {
1935                         for(; tm.sort_next; tm = tm.sort_next)
1936                         {
1937                                 if(!tm.team_size || tm.team == COLOR_SPECTATOR)
1938                                         continue;
1939                                 if(!ts_min) ts_min = tm.team_size;
1940                                 else ts_min = min(ts_min, tm.team_size);
1941                                 if(!ts_max) ts_max = tm.team_size;
1942                                 else ts_max = max(ts_max, tm.team_size);
1943                         }
1944                         if ((ts_max - ts_min) > 1)
1945                         {
1946                                 s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
1947                                 tm = GetTeam(myteam, false);
1948                                 if (tm)
1949                                 if (tm.team != COLOR_SPECTATOR)
1950                                 if (tm.team_size == ts_max)
1951                                         s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
1952
1953                         }
1954                 }
1955         }
1956 }
1957
1958 // Timer (#5)
1959 //
1960 void HUD_Timer()
1961 {
1962         vector pos, mySize;
1963         pos = HUD_Panel_GetPos(5);
1964         mySize = HUD_Panel_GetSize(5);
1965
1966         HUD_Panel_DrawBg(5, pos, mySize);
1967
1968         float timelimit, elapsedTime, minutes, seconds, timeleft, minutesLeft, secondsLeft;
1969
1970         timelimit = getstatf(STAT_TIMELIMIT);
1971
1972         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
1973         timeleft = ceil(timeleft);
1974         minutesLeft = floor(timeleft / 60);
1975         secondsLeft = timeleft - minutesLeft*60;
1976
1977         vector timer_color;
1978         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
1979                 timer_color = '1 1 1'; //white
1980         else if(minutesLeft >= 1)
1981                 timer_color = '1 1 0'; //yellow
1982         else
1983                 timer_color = '1 0 0'; //red
1984
1985         if (cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
1986                 if (time < getstatf(STAT_GAMESTARTTIME)) {
1987                         //while restart is still active, show 00:00
1988                         minutes = seconds = 0;
1989                 } else {
1990                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
1991                         minutes = floor(elapsedTime / 60);
1992                         seconds = elapsedTime - minutes*60;
1993                 }
1994         } else {
1995                 minutes = minutesLeft;
1996                 seconds = secondsLeft;
1997         }
1998
1999         if(mySize_x/mySize_y > 5.1)
2000         {
2001                 if(minutes > 999)
2002                         seconds = 99;
2003                 minutes = min(minutes, 999);
2004                 if(minutesLeft >= 1 || cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
2005                         if(minutes < 100)
2006                                 drawpic_skin(pos + eX * mySize_x - eX * 5.1 * mySize_y, "timer", '1 1 0' * mySize_y, timer_color, hud_alpha_fg, DRAWFLAG_NORMAL);
2007                         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);
2008                         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);
2009                 }
2010                 HUD_DrawXNum(pos + eX * mySize_x - eX * 2 * mySize_y, seconds, -2, 0, mySize_y, timer_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2011         }
2012         else
2013         {
2014                 if(minutes > 99)
2015                         seconds = 99;
2016                 minutes = min(minutes, 99);
2017                 if(minutesLeft >= 1 || cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
2018                         if(minutes < 100)
2019                                 drawpic_skin(pos + eX * 0.5 * mySize_x - eX * 0.5 * 0.5 * mySize_y, "timer", '0.5 0.5 0' * mySize_y, timer_color, hud_alpha_fg, DRAWFLAG_NORMAL);
2020                         HUD_DrawXNum(pos + eX * 0.5 * mySize_x - eX * mySize_y + eY * 0.5 * mySize_y, minutes, -2, 0, 0.5 * mySize_y, timer_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2021                         drawpic_skin(pos + eX * 0.5 * mySize_x - eX * 0.5 * 0.5 * mySize_y + eY * 0.5 * mySize_y, "num_colon", '0.5 0.5 0' * mySize_y, timer_color, hud_alpha_fg, DRAWFLAG_NORMAL);
2022                 }
2023                 HUD_DrawXNum(pos + eX * 0.51 * mySize_x + eY * 0.5 * mySize_y, seconds, -2, 0, 0.5 * mySize_y, timer_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2024         }
2025 }
2026
2027 // Radar (#6)
2028 //
2029 void HUD_Radar(void)
2030 {
2031         vector pos, mySize;
2032         pos = HUD_Panel_GetPos(6);
2033         mySize = HUD_Panel_GetSize(6);
2034
2035         HUD_Panel_DrawBg(6, pos, mySize);
2036
2037         local float color1, color2; // color already declared as a global in hud.qc
2038         local vector rgb;
2039         local entity tm;
2040         float scale2d, normalsize, bigsize;
2041         float f;
2042
2043         teamradar_origin2d = pos + 0.5 * mySize; // TODO: stupid compat, should be removed
2044         teamradar_size2d = mySize;
2045
2046         if(minimapname == "" && !ons_showmap)
2047                 return;
2048
2049         teamradar_loadcvars();
2050
2051         switch(cl_teamradar_zoommode)
2052         {
2053                 default:
2054                 case 0:
2055                         f = current_zoomfraction;
2056                         break;
2057                 case 1:
2058                         f = 1 - current_zoomfraction;
2059                         break;
2060                 case 2:
2061                         f = 0;
2062                         break;
2063                 case 3:
2064                         f = 1;
2065                         break;
2066         }
2067
2068         switch(cl_teamradar_rotation)
2069         {
2070                 case 0:
2071                         teamradar_angle = view_angles_y - 90;
2072                         break;
2073                 default:
2074                         teamradar_angle = 90 * cl_teamradar_rotation;
2075                         break;
2076         }
2077
2078         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
2079         teamradar_size2d = mySize;
2080
2081         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
2082
2083         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
2084         if(cl_teamradar_rotation == 0)
2085         {
2086                 // max-min distance must fit the radar in any rotation
2087                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
2088         }
2089         else
2090         {
2091                 vector c0, c1, c2, c3, span;
2092                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
2093                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
2094                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
2095                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
2096                 span = '0 0 0';
2097                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
2098                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
2099
2100                 // max-min distance must fit the radar in x=x, y=y
2101                 bigsize = min(
2102                         teamradar_size2d_x * scale2d / (1.05 * span_x),
2103                         teamradar_size2d_y * scale2d / (1.05 * span_y)
2104                 );
2105         }
2106
2107         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / cl_teamradar_scale;
2108         if(bigsize > normalsize)
2109                 normalsize = bigsize;
2110
2111         teamradar_size =
2112                   f * bigsize
2113                 + (1 - f) * normalsize;
2114         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
2115                   f * (mi_min + mi_max) * 0.5
2116                 + (1 - f) * view_origin);
2117
2118         color1 = GetPlayerColor(player_localentnum-1);
2119         rgb = GetTeamRGB(color1);
2120
2121         drawsetcliparea(
2122                 pos_x,
2123                 pos_y,
2124                 mySize_x,
2125                 mySize_y
2126         );
2127
2128         draw_teamradar_background(cl_teamradar_background_alpha, cl_teamradar_foreground_alpha);
2129
2130         if(ons_showmap)
2131         {
2132                 drawresetcliparea();
2133
2134                 vector frame_origin, frame_size;
2135                 frame_origin = frame_size = '0 0 0';
2136
2137                 frame_origin_x = pos_x - teamradar_size2d_x * 0.55859375; // matches the picture
2138                 frame_origin_y = pos_y - teamradar_size2d_y * 0.55859375; // matches the picture
2139                 frame_size_x = pos_x * 1.1171875; // matches the picture
2140                 frame_size_y = pos_y * 1.1171875; // matches the picture
2141                 drawpic_skin(frame_origin, "gfx/ons-frame.tga", frame_size, '1 1 1', hud_alpha_fg, 0);
2142                 drawpic_skin(frame_origin, "gfx/ons-frame-team.tga", frame_size, rgb, hud_alpha_fg, 0);
2143
2144                 drawsetcliparea(
2145                         pos_x - teamradar_size2d_x * 0.5,
2146                         pos_y - teamradar_size2d_y * 0.5,
2147                         teamradar_size2d_x,
2148                         teamradar_size2d_y
2149                 );
2150         }
2151
2152         for(tm = world; (tm = find(tm, classname, "radarlink")); )
2153                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
2154         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
2155                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, hud_alpha_fg);
2156         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
2157         {
2158                 color2 = GetPlayerColor(tm.sv_entnum);
2159                 //if(color == COLOR_SPECTATOR || color == color2)
2160                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
2161         }
2162         draw_teamradar_player(view_origin, view_angles, '1 1 1');
2163
2164         drawresetcliparea();
2165 };
2166
2167 // Score (#7)
2168 //
2169 void HUD_Score()
2170 {
2171         vector pos, mySize;
2172         pos = HUD_Panel_GetPos(7);
2173         mySize = HUD_Panel_GetSize(7);
2174
2175         HUD_Panel_DrawBg(7, pos, mySize);
2176
2177         float score, distribution, leader;
2178         float score_len, distr_len;
2179         vector score_pos, secondary_score_pos, distribution_color;
2180         entity tm, pl, me;
2181         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
2182
2183         if (!teamplay) { // non-teamgames
2184                 // me vector := [team/connected frags id]
2185                 pl = players.sort_next;
2186                 if(pl == me)
2187                         pl = pl.sort_next;
2188
2189                 if(hud_configure)
2190                         distribution = 42;
2191                 else if(pl)
2192                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2193                 else
2194                         distribution = 0;
2195
2196                 score = me.(scores[ps_primary]);
2197                 if(hud_configure)
2198                         score = 123;
2199
2200                 if(distribution >= 5) {
2201                         distribution_color = eY;
2202                         leader = 1;
2203                 } else if(distribution >= 0) {
2204                         distribution_color = '1 1 1';
2205                         leader = 1;
2206                 } else if(distribution >= -5)
2207                         distribution_color = '1 1 0';
2208                 else
2209                         distribution_color = eX;
2210
2211                 score_len = strlen(ftos(score));
2212                 distr_len = strlen(ftos(distribution));
2213
2214                 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);
2215                 if (leader)
2216                         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);
2217                 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);
2218         } else { // teamgames
2219                 float max_fragcount;
2220                 max_fragcount = -99;
2221
2222                 float teamnum;
2223                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2224                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !hud_configure)) // no players? don't display
2225                                 continue;
2226                         score = tm.(teamscores[ts_primary]);
2227                         if(hud_configure)
2228                                 score = 123;
2229                         leader = 0;
2230                         
2231                         score_len = strlen(ftos(score));
2232
2233                         if (score > max_fragcount)
2234                                 max_fragcount = score;
2235
2236                         if(tm.team == myteam) {
2237                                 if (max_fragcount == score)
2238                                         leader = 1;
2239                                 if (leader)
2240                                         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);
2241                                 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);
2242                         } else {
2243                                 if (max_fragcount == score)
2244                                         leader = 1;
2245                                 if (leader)
2246                                         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);
2247                                 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);
2248                                 teamnum += 1;
2249                         }
2250                 }
2251         }
2252 }
2253
2254 // Race timer (#8)
2255 //
2256 void HUD_RaceTimer (void) {
2257         vector pos, mySize;
2258         pos = HUD_Panel_GetPos(8);
2259         mySize = HUD_Panel_GetSize(8);
2260
2261         HUD_Panel_DrawBg(8, pos, mySize);
2262
2263         drawfont = hud_bigfont;
2264         float a, t;
2265         string s, forcetime;
2266
2267         if(hud_configure)
2268         {
2269                 s = "0:13:37";
2270                 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);
2271                 s = "^1Intermediate 1 (+15.42)";
2272                 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);
2273         }
2274         else if(race_checkpointtime)
2275         {
2276                 a = bound(0, 2 - (time - race_checkpointtime), 1);
2277                 s = "";
2278                 forcetime = "";
2279                 if(a > 0) // just hit a checkpoint?
2280                 {
2281                         if(race_checkpoint != 254)
2282                         {
2283                                 if(race_time && race_previousbesttime)
2284                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
2285                                 else
2286                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
2287                                 if(race_time)
2288                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
2289                         }
2290                 }
2291                 else
2292                 {
2293                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
2294                         {
2295                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
2296                                 if(a > 0) // next one?
2297                                 {
2298                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
2299                                 }
2300                         }
2301                 }
2302
2303                 if(s != "" && a > 0)
2304                 {
2305                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2306                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2307                         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);
2308                 }
2309
2310                 if(race_penaltytime)
2311                 {
2312                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
2313                         if(a > 0)
2314                         {
2315                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
2316                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2317                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2318                                 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);
2319                         }
2320                 }
2321
2322                 if(forcetime != "")
2323                 {
2324                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
2325                         //drawstring_expanding(m - '16 0 0' * stringwidth(forcetime, FALSE), forcetime, '32 32 0', '1 1 1', hud_alpha_fg, 0, a);
2326                         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);
2327                 }
2328                 else
2329                         a = 1;
2330
2331                 if(race_laptime && race_checkpoint != 255)
2332                 {
2333                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
2334                         //drawstring(m - '16 0 0' * stringwidth(s, FALSE), s, '32 32 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2335                         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);
2336                 }
2337         }
2338         else
2339         {
2340                 if(race_mycheckpointtime)
2341                 {
2342                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
2343                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
2344                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2345                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2346                         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);
2347                 }
2348                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
2349                 {
2350                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
2351                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
2352                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2353                         //drawcolorcodedstring(m - '0 0 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2354                         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);
2355                 }
2356
2357                 if(race_penaltytime && !race_penaltyaccumulator)
2358                 {
2359                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
2360                         a = bound(0, (1 + t - time), 1);
2361                         if(a > 0)
2362                         {
2363                                 if(time < t)
2364                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
2365                                 else
2366                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
2367                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2368                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2369                                 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);
2370                         }
2371                 }
2372         }
2373
2374         drawfont = hud_font;
2375 }
2376
2377 // Vote window (#9)
2378 float vote_yescount;
2379 float vote_nocount;
2380 float vote_needed;
2381 float vote_highlighted; // currently selected vote
2382
2383 float vote_active; // is there an active vote?
2384 float vote_prev; // previous state of vote_active to check for a change
2385 float vote_alpha;
2386 float vote_change; // "time" when vote_active changed
2387
2388 void HUD_VoteWindow(void) 
2389 {
2390         vector pos, mySize;
2391         pos = HUD_Panel_GetPos(9);
2392         mySize = HUD_Panel_GetSize(9);
2393
2394         string s;
2395         float a;
2396         if(vote_active != vote_prev) {
2397                 vote_change = time;
2398                 vote_prev = bound(0, vote_active, 1);
2399         }
2400
2401         if(vote_active)
2402                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
2403         else
2404                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
2405
2406         if(hud_configure)
2407         {
2408                 vote_alpha = 1;
2409                 vote_yescount = 3;
2410                 vote_nocount = 2;
2411                 vote_needed = 4;
2412         }
2413
2414         HUD_Panel_DrawBg(9, pos, mySize);
2415
2416         if(vote_alpha) {
2417                 a = vote_alpha * bound(cvar_or("hud_vote_alreadyvoted_alpha", 0.75), 1 - vote_highlighted, 1);
2418
2419                 drawpic_skin(pos, "voteprogress_back", mySize, HUD_GetBgColor(), a * hud_alpha_bg, DRAWFLAG_NORMAL);
2420
2421                 s = "A vote has been called for: ";
2422                 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);
2423                 s = textShortenToWidth(vote_called_vote, mySize_x * 0.96, '10 0 0', stringwidth_colors);
2424                 if(hud_configure)
2425                         s = "Configure the HUD";
2426                 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);
2427
2428                 // print the yes/no counts
2429                 s = strcat("Yes: ", ftos(vote_yescount));
2430                 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);
2431                 s = strcat("No: ", ftos(vote_nocount));
2432                 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);
2433
2434                 // draw the progress bars
2435                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
2436                 drawpic_skin(pos, "voteprogress_prog", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2437
2438                 drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
2439                 drawpic_skin(pos, "voteprogress_prog", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2440
2441                 // draw the highlights
2442                 if(vote_highlighted == 1) {
2443                         drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
2444                         drawpic_skin(pos, "voteprogress_voted", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2445                 }
2446                 else if(vote_highlighted == 2) {
2447                         drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
2448                         drawpic_skin(pos, "voteprogress_voted", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2449                 }
2450
2451                 drawresetcliparea();
2452         }
2453         else if(!vote_active) {
2454                 vote_highlighted = 0;
2455         }
2456 }
2457
2458 // Awards system
2459 float race_status_time;
2460 float race_status_prev;
2461 string race_status_name_prev;
2462 void HUD_DrawRaceStatus(vector pos)
2463 {
2464         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
2465                 race_status_time = time + 5;
2466                 race_status_prev = race_status;
2467                 if (race_status_name_prev)
2468                         strunzone(race_status_name_prev);
2469                 race_status_name_prev = strzone(race_status_name);
2470         }
2471
2472         float a;
2473         a = bound(0, race_status_time - time, 1);
2474
2475         string s;
2476         s = textShortenToWidth(race_status_name, 120, '10 10 0', stringwidth_colors);
2477
2478         float rank;
2479         if(race_status > 0)
2480                 rank = race_CheckName(race_status_name);
2481         string rankname;
2482         rankname = race_PlaceName(rank);
2483
2484         if(race_status == 0)
2485                 drawpic_skin(pos, "race_newfail", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2486         else if(race_status == 1) {
2487                 drawpic_skin(pos, "race_newtime", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2488                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2489                 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);
2490         } else if(race_status == 2) {
2491                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
2492                         drawpic_skin(pos, "race_newrankgreen", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2493                 else
2494                         drawpic_skin(pos, "race_newrankyellow", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2495                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2496                 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);
2497         } else if(race_status == 3) {
2498                 drawpic_skin(pos, "race_newrecordserver", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2499                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2500                 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);
2501         }
2502
2503         if (race_status_time - time <= 0) {
2504                 race_status_prev = -1;
2505                 race_status = -1;
2506                 if(race_status_name)
2507                         strunzone(race_status_name);
2508                 race_status_name = string_null;
2509                 if(race_status_name_prev)
2510                         strunzone(race_status_name_prev);
2511                 race_status_name_prev = string_null;
2512         }
2513 }
2514
2515 // CTF HUD modicon section
2516 float redflag_prevframe, blueflag_prevframe; // status during previous frame
2517 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
2518 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
2519
2520 void CSQC_ctf_hudreset(void)
2521 {
2522         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
2523 }
2524
2525 void CSQC_ctf_hud(void)
2526 {
2527         vector bottomleft, redflag_pos, blueflag_pos, sz;
2528         float f; // every function should have that
2529         bottomleft_y = vid_conheight;
2530         bottomleft_z = 0;
2531
2532         float redflag, blueflag; // current status
2533         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
2534         float stat_items;
2535
2536         stat_items = getstati(STAT_ITEMS);
2537         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
2538         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
2539
2540         // when status CHANGES, set old status into prevstatus and current status into status
2541         if (redflag != redflag_prevframe)
2542         {
2543                 redflag_statuschange_time = time;
2544                 redflag_prevstatus = redflag_prevframe;
2545                 redflag_prevframe = redflag;
2546         }
2547
2548         if (blueflag != blueflag_prevframe)
2549         {
2550                 blueflag_statuschange_time = time;
2551                 blueflag_prevstatus = blueflag_prevframe;
2552                 blueflag_prevframe = blueflag;
2553         }
2554
2555         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
2556         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
2557
2558         float BLINK_FACTOR = 0.15;
2559         float BLINK_BASE = 0.85;
2560         // note:
2561         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
2562         // thus
2563         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
2564         // ensure RMS == 1
2565         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
2566
2567         string red_icon, red_icon_prevstatus;
2568         float red_alpha, red_alpha_prevstatus;
2569         red_alpha = red_alpha_prevstatus = 1;
2570         switch(redflag) {
2571                 case 1: red_icon = "flag_red_taken"; break;
2572                 case 2: red_icon = "flag_red_lost"; break;
2573                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2574                 default:
2575                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2576                                 red_icon = "flag_red_shielded";
2577                         else
2578                                 red_icon = string_null;
2579                         break;
2580         }
2581         switch(redflag_prevstatus) {
2582                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
2583                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
2584                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2585                 default:
2586                         if(redflag == 3)
2587                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
2588                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2589                                 red_icon_prevstatus = "flag_red_shielded";
2590                         else
2591                                 red_icon_prevstatus = string_null;
2592                         break;
2593         }
2594
2595         string blue_icon, blue_icon_prevstatus;
2596         float blue_alpha, blue_alpha_prevstatus;
2597         blue_alpha = blue_alpha_prevstatus = 1;
2598         switch(blueflag) {
2599                 case 1: blue_icon = "flag_blue_taken"; break;
2600                 case 2: blue_icon = "flag_blue_lost"; break;
2601                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2602                 default:
2603                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2604                                 blue_icon = "flag_blue_shielded";
2605                         else
2606                                 blue_icon = string_null;
2607                         break;
2608         }
2609         switch(blueflag_prevstatus) {
2610                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
2611                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
2612                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2613                 default:
2614                         if(blueflag == 3)
2615                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
2616                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2617                                 blue_icon_prevstatus = "flag_blue_shielded";
2618                         else
2619                                 blue_icon_prevstatus = string_null;
2620                         break;
2621         }
2622
2623         if (myteam == COLOR_TEAM1) { // always draw own flag on left
2624                 redflag_pos = bottomleft - '-4 50 0';
2625                 blueflag_pos = bottomleft - '-62 50 0';
2626         } else {
2627                 blueflag_pos = bottomleft - '-4 50 0';
2628                 redflag_pos = bottomleft - '-62 50 0';
2629         }
2630
2631         sz = '52 52 0';
2632
2633         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
2634         if(red_icon_prevstatus && f < 1)
2635                 drawpic_expanding(redflag_pos, red_icon_prevstatus, sz, '1 1 1', hud_alpha_fg * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2636         if(red_icon)
2637                 drawpic_skin(redflag_pos, red_icon, sz, '1 1 1', hud_alpha_fg * red_alpha * f, DRAWFLAG_NORMAL);
2638
2639         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
2640         if(blue_icon_prevstatus && f < 1)
2641                 drawpic_expanding(blueflag_pos, blue_icon_prevstatus, sz, '1 1 1', hud_alpha_fg * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2642         if(blue_icon)
2643                 drawpic_skin(blueflag_pos, blue_icon, sz, '1 1 1', hud_alpha_fg * blue_alpha * f, DRAWFLAG_NORMAL);
2644 }
2645
2646 /*void HUD_Mod_Race (void) {
2647         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
2648                 pl = players.sort_next;
2649                 if(pl == me)
2650                         pl = pl.sort_next;
2651                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
2652                         if(pl.scores[ps_primary] == 0)
2653                                 pl = world;
2654
2655                 score = me.(scores[ps_primary]);
2656
2657                 float racemin, racesec, racemsec;
2658                 float distsec, distmsec, minusplus;
2659
2660                 racemin = floor(score/(60 * TIME_FACTOR));
2661                 racesec = floor((score - racemin*(60 * TIME_FACTOR))/TIME_FACTOR);
2662                 racemsec = score - racemin*60*TIME_FACTOR - racesec*TIME_FACTOR;
2663
2664                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
2665                         // distribution display
2666                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2667
2668                         if (distribution < TIME_FACTOR && distribution > -TIME_FACTOR)
2669                                 distmsec = fabs(distribution);
2670                         else {
2671                                 distsec = floor(fabs(distribution)/TIME_FACTOR);
2672                                 distmsec = fabs(distribution) - distsec*TIME_FACTOR;
2673                                 if (distribution < 0)
2674                                         distsec = -distsec;
2675                         }
2676
2677                         if (distribution <= 0) {
2678                                 distribution_color = eY;
2679                                 minusplus = 1; // minusplus 1: always prefix with minus sign
2680                         }
2681                         else {
2682                                 distribution_color = eX;
2683                                 minusplus = 2; // minusplus 1: always prefix with plus sign
2684                         }
2685                         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);
2686                         HUD_DrawXNum(bottomright - '68 48 0' - '16 0 0' * TIME_DECIMALS, distsec, 4, minusplus, 16, distribution_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2687                         drawpic_skin(bottomright - '10 48 0' - '16 0 0' * TIME_DECIMALS, "num_dot", '16 16 0', distribution_color, hud_alpha_fg, DRAWFLAG_ADDITIVE);
2688                 }
2689                 // race record display
2690                 if (distribution <= 0 || distribution == score) // draw the highlight background behind the timer if we have the lead
2691                         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);
2692
2693                 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);
2694                 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);
2695                 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);
2696
2697                 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);
2698                 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);
2699         }
2700 }*/
2701
2702 // Keyhunt HUD modicon section
2703 float kh_runheretime;
2704
2705 void CSQC_kh_hudreset(void)
2706 {
2707         kh_runheretime = 0;
2708 }
2709
2710 void CSQC_kh_hud(void)
2711 {
2712         float kh_keys;
2713         float keyteam;
2714         float a, aa;
2715         vector p, pa, kh_size, kh_asize;
2716
2717         p_x = 6;
2718         p_y = vid_conheight - 34 - 3;
2719         p_z = 0;
2720
2721         kh_keys = getstati(STAT_KH_KEYS);
2722
2723         kh_size = '19 34 0';
2724         kh_asize = '19 10 0';
2725         pa = p + '0 -10 0';
2726
2727         float i, key;
2728
2729         float keycount;
2730         keycount = 0;
2731         for(i = 0; i < 4; ++i)
2732         {
2733                 key = floor(kh_keys / pow(32, i)) & 31;
2734                 keyteam = key - 1;
2735                 if(keyteam == 30 && keycount <= 4)
2736                         keycount += 4;
2737                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
2738                         keycount += 1;
2739         }
2740         // this yields 8 exactly if "RUN HERE" shows
2741
2742         if(keycount == 8)
2743         {
2744                 if(!kh_runheretime)
2745                         kh_runheretime = time;
2746                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
2747         }
2748         else
2749                 kh_runheretime = 0;
2750
2751         for(i = 0; i < 4; ++i)
2752         {
2753                 key = floor(kh_keys / pow(32, i)) & 31;
2754                 keyteam = key - 1;
2755                 switch(keyteam)
2756                 {
2757                         case 30: // my key
2758                                 keyteam = myteam;
2759                                 a = 1;
2760                                 aa = 1;
2761                                 break;
2762                         case -1: // no key
2763                                 a = 0;
2764                                 aa = 0;
2765                                 break;
2766                         default: // owned or dropped
2767                                 a = 0.2;
2768                                 aa = 0.5;
2769                                 break;
2770                 }
2771                 a = a * hud_alpha_fg;
2772                 aa = aa * hud_alpha_fg;
2773                 if(a > 0)
2774                 {
2775                         switch(keyteam)
2776                         {
2777                                 case COLOR_TEAM1:
2778                                         drawpic (pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2779                                         break;
2780                                 case COLOR_TEAM2:
2781                                         drawpic (pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2782                                         break;
2783                                 case COLOR_TEAM3:
2784                                         drawpic (pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2785                                         break;
2786                                 case COLOR_TEAM4:
2787                                         drawpic (pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2788                                         break;
2789                                 default:
2790                                         break;
2791                         }
2792                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
2793                         {
2794                                 case 0:
2795                                         drawpic (p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2796                                         break;
2797                                 case 1:
2798                                         drawpic (p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2799                                         break;
2800                                 case 2:
2801                                         drawpic (p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2802                                         break;
2803                                 case 3:
2804                                         drawpic (p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2805                                         break;
2806                         }
2807                 }
2808                 p_x += 24;
2809                 pa_x += 24;
2810         }
2811 }
2812
2813 // Nexball HUD modicon section
2814 #define NBPB_SIZE '96 38 0'
2815 #define NBPB_BT 2                   //thickness
2816 #define NBPB_BRGB '1 1 1'
2817 #define NBPB_BALPH 1                //alpha
2818 #define NBPB_BFLAG DRAWFLAG_NORMAL
2819 #define NBPB_IALPH 0.4
2820 #define NBPB_IFLAG DRAWFLAG_NORMAL
2821 #define NBPB_IRGB '0.7 0.1 0'
2822
2823 void CSQC_nb_hud(void)
2824 {
2825         float stat_items, nb_pb_starttime, dt, p;
2826         vector pos;
2827
2828         stat_items = getstati(STAT_ITEMS);
2829         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
2830
2831         pos_x = 4;
2832         pos_y = vid_conheight - 42;
2833         pos_z = 0;
2834
2835         //Manage the progress bar if any
2836         if (nb_pb_starttime > 0)
2837         {
2838                 vector s;
2839                 dt = mod(time - nb_pb_starttime, nb_pb_period);
2840                 // one period of positive triangle
2841                 p = 2 * dt / nb_pb_period;
2842                 if (p > 1)
2843                         p = 2 - p;
2844
2845                 s = NBPB_SIZE;
2846                 //Draw the filling
2847                 drawfill(pos, p * s_x * eX + s_y * eY, NBPB_IRGB, NBPB_IALPH, NBPB_IFLAG);
2848
2849                 //Draw the box
2850                 s = NBPB_SIZE;
2851                 drawline(NBPB_BT, pos    , pos + eX * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2852                 drawline(NBPB_BT, pos    , pos + eY * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2853                 drawline(NBPB_BT, pos + s, pos + eX * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2854                 drawline(NBPB_BT, pos + s, pos + eY * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2855         }
2856
2857         pos_x += 12; //horizontal margin to the picture
2858         pos_y += 2; //vertical margin to the picture
2859
2860         if (stat_items & IT_KEY1)
2861                 drawpic_skin(pos, "nexball_carrying", '80 34 0', '1 1 1', 1, DRAWFLAG_NORMAL);
2862 }
2863
2864 // Race/CTS HUD modicon section
2865 float crecordtime_prev; // last remembered crecordtime
2866 float crecordtime_change_time; // time when crecordtime last changed
2867 float srecordtime_prev; // last remembered srecordtime
2868 float srecordtime_change_time; // time when srecordtime last changed
2869 void CSQC_race_hud(void)
2870 {
2871         entity me;
2872         me = playerslots[player_localentnum - 1];
2873         float t, score;
2874         float f; // yet another function has this
2875         score = me.(scores[ps_primary]);
2876
2877         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
2878                 return; // no records in the actual race
2879
2880         drawfont = hud_bigfont;
2881         vector pos;
2882         pos_x = 2;
2883         pos_y = vid_conheight - 48;
2884
2885         // clientside personal record
2886         string rr;
2887         if(gametype == GAME_CTS)
2888                 rr = CTS_RECORD;
2889         else
2890                 rr = RACE_RECORD;
2891         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
2892
2893         if(score && (score < t || !t)) {
2894                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
2895                 if(cvar("cl_autodemo_delete_keeprecords"))
2896                 {
2897                         f = cvar("cl_autodemo_delete");
2898                         f &~= 1;
2899                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
2900                 }
2901         }
2902
2903         if(t != crecordtime_prev) {
2904                 crecordtime_prev = t;
2905                 crecordtime_change_time = time;
2906         }
2907         f = time - crecordtime_change_time;
2908
2909         if (f > 1) {
2910                 drawstring(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2911                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2912         } else {
2913                 drawstring(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2914                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2915                 drawstring_expanding(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2916                 drawstring_expanding(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2917         }
2918
2919         // server record
2920         pos_y += 26;
2921         t = race_server_record;
2922         if(t != srecordtime_prev) {
2923                 srecordtime_prev = t;
2924                 srecordtime_change_time = time;
2925         }
2926         f = time - srecordtime_change_time;
2927
2928         if (f > 1) {
2929                 drawstring(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2930                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2931         } else {
2932                 drawstring(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2933                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2934                 drawstring_expanding(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2935                 drawstring_expanding(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2936         }
2937         drawfont = hud_font;
2938 }
2939
2940 /*
2941 ==================
2942 Main HUD system
2943 ==================
2944 */
2945
2946 void HUD_DrawPressedKeys(void)
2947 {
2948         vector pos, mySize;
2949         pos = HUD_Panel_GetPos(10);
2950         mySize = HUD_Panel_GetSize(10);
2951
2952         HUD_Panel_DrawBg(10, pos, mySize);
2953
2954         float pressedkeys;
2955
2956         pressedkeys = getstatf(STAT_PRESSED_KEYS);
2957         drawpic_skin(pos, "key_bg.tga",           mySize, '1 1 1', 0.1 * hud_alpha_fg, DRAWFLAG_NORMAL);
2958         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);
2959         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);
2960         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);
2961         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);
2962         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);
2963         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);
2964 }
2965
2966 void HUD_ShowSpeed(void)
2967 {
2968         vector numsize;
2969         float pos, conversion_factor;
2970         string speed, zspeed, unit;
2971
2972         switch(cvar("cl_showspeed_unit"))
2973         {
2974                 default:
2975                 case 0:
2976                         unit = "";
2977                         conversion_factor = 1.0;
2978                         break;
2979                 case 1:
2980                         unit = " qu/s";
2981                         conversion_factor = 1.0;
2982                         break;
2983                 case 2:
2984                         unit = " m/s";
2985                         conversion_factor = 0.0254;
2986                         break;
2987                 case 3:
2988                         unit = " km/h";
2989                         conversion_factor = 0.0254 * 3.6;
2990                         break;
2991                 case 4:
2992                         unit = " mph";
2993                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
2994                         break;
2995                 case 5:
2996                         unit = " knots";
2997                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
2998                         break;
2999         }
3000
3001         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
3002
3003         numsize_x = numsize_y = cvar("cl_showspeed_size");
3004         pos = (vid_conheight - numsize_y) * cvar("cl_showspeed_position");
3005
3006         drawfont = hud_bigfont;
3007         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3008
3009         if (cvar("cl_showspeed_z") == 1) {
3010                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
3011                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3012         }
3013
3014         drawfont = hud_font;
3015 }
3016
3017 vector acc_prevspeed;
3018 float acc_prevtime;
3019 float acc_avg;
3020
3021 void HUD_ShowAcceleration(void)
3022 {
3023         float acceleration, sz, scale, alpha, f;
3024         vector pos, top, rgb;
3025         top_x = vid_conwidth/2;
3026         top_y = 0;
3027
3028         f = time - acc_prevtime;
3029         if(cvar("cl_showacceleration_z"))
3030                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
3031         else
3032                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
3033         acc_prevspeed = pmove_vel;
3034         acc_prevtime = time;
3035
3036         f = bound(0, f * 10, 1);
3037         acc_avg = acc_avg * (1 - f) + acceleration * f;
3038         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
3039
3040         pos = top - sz/2 * eY + (cvar("cl_showacceleration_position") * vid_conheight) * eY;
3041
3042         sz = cvar("cl_showacceleration_size");
3043         scale = cvar("cl_showacceleration_scale");
3044         alpha = cvar("cl_showacceleration_alpha");
3045         if (cvar("cl_showacceleration_color_custom"))
3046                 rgb = stov(cvar_string("cl_showacceleration_color"));
3047         else {
3048                 rgb = '1 1 1';
3049                 if (acceleration < 0) {
3050                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
3051                 } else if (acceleration > 0) {
3052                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
3053                 }
3054         }
3055
3056         if (acceleration > 0)
3057                 drawpic_skin(pos, "statusbar", acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3058         else if (acceleration < 0)
3059                 drawpic_skin(pos + acceleration * scale * '40 0 0', "statusbar", -acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3060 }
3061
3062 void HUD_Reset (void)
3063 {
3064         // reset gametype specific icons
3065         if(gametype == GAME_KEYHUNT)
3066                 CSQC_kh_hudreset();
3067         else if(gametype == GAME_CTF)
3068                 CSQC_ctf_hudreset();
3069 }
3070
3071 void HUD_Main (void)
3072 {
3073         hud_alpha_bg = cvar_or("hud_bg_alpha", 0.8) * (1 - cvar("_menu_alpha"));
3074         hud_border_thickness = bound(0, cvar("hud_border_thickness"), 5);
3075         hud_accuracy_border_thickness = bound(0, cvar_or("hud_accuracy_border_thickness", 1), 5);
3076         hud_color_bg_team = cvar("hud_color_bg_team");
3077
3078         hud_fontsize = HUD_GetFontsize("hud_fontsize");
3079         hud_fontsize_spec = HUD_GetFontsize("hud_fontsize_spec");
3080
3081         hud_configure = cvar("_hud_configure");
3082
3083         // Drawing stuff
3084         if(cvar("hud_dock"))
3085                 drawpic_skin('0 0 0', "dock", eX * vid_conwidth + eY * vid_conheight, stov(cvar_string("hud_dock_color")), cvar("hud_dock_alpha"), DRAWFLAG_NORMAL);
3086
3087         if(HUD_Panel_CheckActive(0))
3088                 HUD_WeaponIcons();
3089         if(HUD_Panel_CheckActive(1))
3090                 HUD_Inventory();
3091         if(HUD_Panel_CheckActive(2))
3092                 HUD_Powerups();
3093         if(HUD_Panel_CheckActive(3))
3094                 HUD_HealthArmor();
3095         if(HUD_Panel_CheckActive(4))
3096                 HUD_Notify();
3097         if(HUD_Panel_CheckActive(5))
3098                 HUD_Timer();
3099         // TODO hud'ify
3100         if(HUD_Panel_CheckActive(6))
3101                 if(ons_showmap || cvar_string("cl_teamradar") != "0" && (cvar("cl_teamradar") == 2 || teamplay))
3102                         HUD_Radar();
3103         if(HUD_Panel_CheckActive(7))
3104                 HUD_Score();
3105         if(HUD_Panel_CheckActive(8))
3106                 if(gametype == GAME_RACE || gametype == GAME_CTS || hud_configure)
3107                         HUD_RaceTimer();
3108         if(HUD_Panel_CheckActive(9))
3109                 HUD_VoteWindow();
3110         // TODO hud'ify
3111         if(HUD_Panel_CheckActive(10))
3112                 if(spectatee_status > 0 || cvar("cl_showpressedkeys") >= 2 || hud_configure)
3113                         HUD_DrawPressedKeys();
3114
3115         // TODO hud_'ify these
3116         if (cvar("cl_showspeed"))
3117                 HUD_ShowSpeed();
3118         if (cvar("cl_showacceleration"))
3119                 HUD_ShowAcceleration();
3120
3121         // TODO... well make them work in a panel etc
3122         if(gametype == GAME_KEYHUNT)
3123                 CSQC_kh_hud();
3124         else if(gametype == GAME_CTF)
3125                 CSQC_ctf_hud();
3126         else if(gametype == GAME_NEXBALL)
3127                 CSQC_nb_hud();
3128         else if(gametype == GAME_CTS || gametype == GAME_RACE)
3129                 CSQC_race_hud();
3130         return;
3131 }