]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
remove the timer icon, it's often only in the way. designer can place it on their...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qc
1 /*
2 ==================
3 Misc HUD functions
4 ==================
5 */
6
7 // a border picture is a texture containing nine parts:
8 //   1/4 width: left part
9 //   1/2 width: middle part (stretched)
10 //   1/4 width: right part
11 // divided into
12 //   1/4 height: top part
13 //   1/2 height: middle part (stretched)
14 //   1/4 height: bottom part
15 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
16 {
17         vector dX, dY;
18         vector width, height;
19         vector bW, bH;
20         //pic = draw_UseSkinFor(pic);
21         width = eX * theSize_x;
22         height = eY * theSize_y;
23         if(theSize_x <= theBorderSize_x * 2)
24         {
25                 // not wide enough... draw just left and right then
26                 bW = eX * (0.25 * theSize_x / (theBorderSize_x * 2));
27                 if(theSize_y <= theBorderSize_y * 2)
28                 {
29                         // not high enough... draw just corners
30                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
31                         drawsubpic(theOrigin,                 width * 0.5 + height * 0.5, pic, '0 0 0',           bW + bH, theColor, theAlpha, 0);
32                         drawsubpic(theOrigin + width   * 0.5, width * 0.5 + height * 0.5, pic, eX - bW,           bW + bH, theColor, theAlpha, 0);
33                         drawsubpic(theOrigin + height  * 0.5, width * 0.5 + height * 0.5, pic, eY - bH,           bW + bH, theColor, theAlpha, 0);
34                         drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
35                 }
36                 else
37                 {
38                         dY = theBorderSize_x * eY;
39                         drawsubpic(theOrigin,                             width * 0.5          +     dY, pic, '0 0    0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
40                         drawsubpic(theOrigin + width * 0.5,               width * 0.5          +     dY, pic, '0 0    0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
41                         drawsubpic(theOrigin                        + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0',           '0 0.5  0' + bW, theColor, theAlpha, 0);
42                         drawsubpic(theOrigin + width * 0.5          + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0' + eX - bW, '0 0.5  0' + bW, theColor, theAlpha, 0);
43                         drawsubpic(theOrigin               + height - dY, width * 0.5          +     dY, pic, '0 0.75 0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
44                         drawsubpic(theOrigin + width * 0.5 + height - dY, width * 0.5          +     dY, pic, '0 0.75 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
45                 }
46         }
47         else
48         {
49                 if(theSize_y <= theBorderSize_y * 2)
50                 {
51                         // not high enough... draw just top and bottom then
52                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
53                         dX = theBorderSize_x * eX;
54                         drawsubpic(theOrigin,                                         dX + height * 0.5, pic, '0    0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
55                         drawsubpic(theOrigin + dX,                        width - 2 * dX + height * 0.5, pic, '0.25 0 0',           '0.5  0 0' + bH, theColor, theAlpha, 0);
56                         drawsubpic(theOrigin + width - dX,                            dX + height * 0.5, pic, '0.75 0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
57                         drawsubpic(theOrigin              + height * 0.5,             dX + height * 0.5, pic, '0    0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
58                         drawsubpic(theOrigin + dX         + height * 0.5, width - 2 * dX + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5  0 0' + bH, theColor, theAlpha, 0);
59                         drawsubpic(theOrigin + width - dX + height * 0.5,             dX + height * 0.5, pic, '0.75 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
60                 }
61                 else
62                 {
63                         dX = theBorderSize_x * eX;
64                         dY = theBorderSize_x * eY;
65                         drawsubpic(theOrigin,                                        dX          +     dY, pic, '0    0    0', '0.25 0.25 0', theColor, theAlpha, 0);
66                         drawsubpic(theOrigin                  + dX,      width - 2 * dX          +     dY, pic, '0.25 0    0', '0.5  0.25 0', theColor, theAlpha, 0);
67                         drawsubpic(theOrigin          + width - dX,                  dX          +     dY, pic, '0.75 0    0', '0.25 0.25 0', theColor, theAlpha, 0);
68                         drawsubpic(theOrigin          + dY,                          dX + height - 2 * dY, pic, '0    0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
69                         drawsubpic(theOrigin          + dY         + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5  0.5  0', theColor, theAlpha, 0);
70                         drawsubpic(theOrigin          + dY + width - dX,             dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
71                         drawsubpic(theOrigin + height - dY,                          dX          +     dY, pic, '0    0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
72                         drawsubpic(theOrigin + height - dY         + dX, width - 2 * dX          +     dY, pic, '0.25 0.75 0', '0.5  0.25 0', theColor, theAlpha, 0);
73                         drawsubpic(theOrigin + height - dY + width - dX,             dX          +     dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
74                 }
75         }
76 }
77
78 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
79 void drawpic_skin(vector pos, string pic, vector sz, vector color, float alpha, float drawflag) {
80         drawpic(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, color, alpha, drawflag);
81 }
82
83 // return HUD background color
84 vector HUD_GetBgColor()
85 {
86         vector color;
87         if (teamplay)
88                 GetTeamRGB(myteam) * hud_color_bg_team;
89         else {
90                 // allow custom HUD colors in non-teamgames
91                 color_x = cvar("hud_color_bg_r");
92                 color_y = cvar("hud_color_bg_g");
93                 color_z = cvar("hud_color_bg_b");
94         }
95         return color;
96 }
97
98 // return accuracy text color
99 vector HUD_AccuracyColor(float accuracy)
100 {
101         vector rgb;
102         float yellow_accuracy = cvar("hud_weaponicons_accuracy_yellow"); // value at which this function returns yellow
103         if(accuracy >= 100) {
104                 rgb_x = 0;
105                 rgb_y = 1;
106         }
107         else if(accuracy > yellow_accuracy) {
108                 rgb_x = 1 - (accuracy-yellow_accuracy)/(100-yellow_accuracy); // red value between 1 -> 0
109                 rgb_y = 1;
110         }
111         else {
112                 rgb_x = 1;
113                 rgb_y = accuracy/yellow_accuracy; // green value between 0 -> 1
114         }
115         rgb_z = 0;
116         return rgb;
117 }
118
119 // draw number in the XSCALE font
120 void HUD_DrawXNum (vector pos, float num, float digits, float showsign, float lettersize, vector rgb, float highlighted, float stroke, float alpha, float dflags)
121 {
122         float l, i;
123         string str, tmp, l_length;
124         float minus, plus;
125         vector vsize, num_color;
126
127         vsize_x = vsize_y = lettersize;
128         vsize_z = 0;
129
130         // showsign 1: always prefix with minus sign (useful in race distribution display)
131         // showsign 2: always prefix with plus sign (useful in race distribution display)
132         // showsign 3: prefix with minus sign if negative, plus sign if positive (useful in score distribution display)
133
134         if((showsign == 2 && num >= 0) || (num > 0 && showsign == 3))
135         {
136                 plus = true;
137                 pos_x -= lettersize;
138         } else
139                 plus = false;
140
141         if(num < 0 || (num < 0 && showsign == 3) || (showsign == 1 && num <= 0))
142         {
143                 minus = true;
144                 num = -num;
145                 pos_x -= lettersize;
146         } else
147                 minus = false;
148
149         if(digits < 0)
150         {
151                 tmp = ftos(num);
152                 digits = -digits;
153                 str = strcat(substring("0000000000", 0, digits - strlen(tmp)), tmp);
154         } else
155                 str = ftos(num);
156
157         l = strlen(str);
158         l_length = ftos(l);
159
160         if(l > digits)
161         {
162                 str = substring(str, l-digits, 999);
163                 l = strlen(str);
164         } else if(l < digits)
165                 pos_x += (digits-l) * lettersize;
166
167         if (highlighted == 1) {
168                 vector hl_size;
169                 hl_size_x = vsize_x * l + vsize_x * 0.2;
170                 hl_size_y = vsize_y * 1.1;
171                 hl_size_z = 0;
172                 if(minus)
173                         hl_size_x = hl_size_x + vsize_x;
174
175                 vector hl_pos;
176                 hl_pos_x = pos_x - lettersize/10;
177                 hl_pos_y = pos_y - lettersize/20;
178                 hl_pos_z = 0;
179
180                 drawpic_skin(hl_pos, strcat("highlight_", l_length), hl_size, '1 1 1', alpha, dflags);
181         }
182
183         if (stroke == 1)
184                 num_color = '1 1 1';
185         else
186                 num_color = rgb;
187
188         if(minus)
189         {
190                 if (stroke == 1)
191                         drawpic_skin(pos, "num_minus_stroke", vsize, rgb, alpha, dflags);
192                 drawpic_skin(pos, "num_minus", vsize, num_color, alpha, dflags);
193                 pos_x += lettersize;
194         } else if(plus)
195         {
196                 if (stroke == 1)
197                         drawpic_skin(pos, "num_plus_stroke", vsize, rgb, alpha, dflags);
198                 drawpic_skin(pos, "num_plus", vsize, num_color, alpha, dflags);
199                 pos_x += lettersize;
200         }
201
202         for(i = 0; i < l; ++i)
203         {
204                 tmp = substring(str, i, 1);
205                 if (stroke == 1)
206                         drawpic_skin(pos, strcat("num_", tmp, "_stroke"), vsize, rgb, alpha, dflags);
207                 drawpic_skin(pos, strcat("num_", tmp), vsize, num_color, alpha, dflags);
208                 pos_x += lettersize;
209         }
210 }
211
212 // color the number differently based on how big it is (used in the health/armor panel)
213 void HUD_DrawXNum_Colored (vector pos, float x, float digits, float lettersize, float alpha)
214 {
215         vector color;
216         if(x > 200) {
217                 color_x = 0;
218                 color_y = 1;
219                 color_z = 0;
220         }
221         else if(x > 150) {
222                 color_x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
223                 color_y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
224                 color_z = 0;
225         }
226         else if(x > 100) {
227                 color_x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
228                 color_y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
229                 color_z = 1 - (x-100)*0.02; // blue value between 1 -> 0
230         }
231         else if(x > 50) {
232                 color_x = 1;
233                 color_y = 1;
234                 color_z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
235         }
236         else if(x > 20) {
237                 color_x = 1;
238                 color_y = (x-20)*90/27/100; // green value between 0 -> 1
239                 color_z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
240         }
241         else {
242                 color_x = 1;
243                 color_y = 0;
244                 color_z = 0;
245         }
246         HUD_DrawXNum(pos, x, digits, 0, lettersize, color, 0, 0, alpha, DRAWFLAG_NORMAL);
247 }
248
249 float stringwidth_colors(string s, vector theSize)
250 {
251         return stringwidth(s, TRUE, theSize);
252 }
253
254 float stringwidth_nocolors(string s, vector theSize)
255 {
256         return stringwidth(s, FALSE, theSize);
257 }
258
259 #define CENTERPRINT_MAX_LINES 30
260 string centerprint_messages[CENTERPRINT_MAX_LINES];
261 float centerprint_width[CENTERPRINT_MAX_LINES];
262 vector centerprint_start;
263 float centerprint_expire;
264 float centerprint_num;
265 float centerprint_offset_hint;
266 vector centerprint_fontsize;
267
268 void centerprint(string strMessage)
269 {
270         float i, j, n, hcount;
271         string s;
272
273         centerprint_fontsize = HUD_GetFontsize("scr_centersize");
274
275         centerprint_expire = min(centerprint_expire, time); // if any of the returns happens, this message will fade out
276
277         if(cvar("scr_centertime") <= 0)
278                 return;
279
280         if(strMessage == "")
281                 return;
282
283         // strip trailing newlines
284         j = strlen(strMessage) - 1;
285         while(substring(strMessage, j, 1) == "\n" && j >= 0)
286                 j = j - 1;
287         strMessage = substring(strMessage, 0, j + 1);
288
289         if(strMessage == "")
290                 return;
291
292         // strip leading newlines and remember them, they are a hint that the message should be lower on the screen
293         j = 0;
294         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
295                 j = j + 1;
296         strMessage = substring(strMessage, j, strlen(strMessage) - j);
297         centerprint_offset_hint = j;
298
299         if(strMessage == "")
300                 return;
301
302         // if we get here, we have a message. Initialize its height.
303         centerprint_num = 0;
304
305         n = tokenizebyseparator(strMessage, "\n");
306         i = hcount = 0;
307         for(j = 0; j < n; ++j)
308         {
309                 getWrappedLine_remaining = argv(j);
310                 while(getWrappedLine_remaining)
311                 {
312                         s = getWrappedLine(vid_conwidth * 0.75, centerprint_fontsize, stringwidth_colors);
313                         if(centerprint_messages[i])
314                                 strunzone(centerprint_messages[i]);
315                         centerprint_messages[i] = strzone(s);
316                         centerprint_width[i] = stringwidth(s, TRUE, centerprint_fontsize);
317                         ++i;
318
319                         // half height for empty lines looks better
320                         if(s == "")
321                                 hcount += 0.5;
322                         else
323                                 hcount += 1;
324
325                         if(i >= CENTERPRINT_MAX_LINES)
326                                 break;
327                 }
328         }
329
330         float h, havail;
331         h = centerprint_fontsize_y*hcount;
332
333         havail = vid_conheight;
334         if(cvar("con_chatpos") < 0)
335                 havail -= (-cvar("con_chatpos") + cvar("con_chat")) * cvar("con_chatsize"); // avoid overlapping chat
336         if(havail > vid_conheight - 70)
337                 havail = vid_conheight - 70; // avoid overlapping HUD
338
339         centerprint_start_x = 0;
340
341 #if 0
342         float forbiddenmin, forbiddenmax, allowedmin, allowedmax, preferred;
343
344         // here, the centerprint would cover the crosshair. REALLY BAD.
345         forbiddenmin = vid_conheight * 0.5 - h - 16;
346         forbiddenmax = vid_conheight * 0.5 + 16;
347
348         allowedmin = scoreboard_bottom;
349         allowedmax = havail - h;
350         preferred = (havail - h)/2;
351
352
353         // possible orderings (total: 4! / 4 = 6)
354         //  allowedmin allowedmax forbiddenmin forbiddenmax
355         //  forbiddenmin forbiddenmax allowedmin allowedmax
356         if(allowedmax < forbiddenmin || allowedmin > forbiddenmax)
357         {
358                 // forbidden doesn't matter in this case
359                 centerprint_start_y = bound(allowedmin, preferred, allowedmax);
360         }
361         //  allowedmin forbiddenmin allowedmax forbiddenmax
362         else if(allowedmin < forbiddenmin && allowedmax < forbiddenmax)
363         {
364                 centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
365         }
366         //  allowedmin forbiddenmin forbiddenmax allowedmax
367         else if(allowedmin < forbiddenmin)
368         {
369                 // make sure the forbidden zone is not covered
370                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
371                         centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
372                 else
373                         centerprint_start_y = bound(forbiddenmax, preferred, allowedmin);
374         }
375         //  forbiddenmin allowedmin allowedmax forbiddenmax
376         else if(allowedmax < forbiddenmax)
377         {
378                 // it's better to leave the allowed zone (overlap with scoreboard) than
379                 // to cover the forbidden zone (crosshair)
380                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
381                         centerprint_start_y = forbiddenmax;
382                 else
383                         centerprint_start_y = forbiddenmin;
384         }
385         //  forbiddenmin allowedmin forbiddenmax allowedmax
386         else
387         {
388                 centerprint_start_y = bound(forbiddenmax, preferred, allowedmax);
389         }
390 #else
391         centerprint_start_y =
392                 min(
393                         max(
394                                 max(scoreboard_bottom, vid_conheight * 0.5 + 16),
395                                 (havail - h)/2
396                         ),
397                         havail - h
398                 );
399 #endif
400
401         centerprint_num = i;
402         centerprint_expire = time + cvar("scr_centertime");
403 }
404
405 void HUD_DrawCenterPrint (void)
406 {
407         float i;
408         vector pos;
409         string ts;
410         float a;
411
412         //if(time > centerprint_expire)
413         //      return;
414
415         //a = bound(0, 1 - 2 * (time - centerprint_expire), 1);
416         a = bound(0, 1 - 4 * (time - centerprint_expire), 1);
417         //sz = 1.2 / (a + 0.2);
418
419         if(a <= 0)
420                 return;
421
422         pos = centerprint_start;
423         for (i=0; i<centerprint_num; i = i + 1)
424         {
425                 pos_x = (vid_conwidth - centerprint_width[i]) * 0.5;
426                 ts = centerprint_messages[i];
427                 if (ts != "")
428                 {
429                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
430                         drawcolorcodedstring(pos, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
431                         //  - '0 0.5 0' * (sz - 1) * centerprint_fontsize_x - '0.5 0 0' * (sz - 1) * centerprint_width[i] * centerprint_fontsize_y, centerprint_fontsize * sz
432                         pos_y = pos_y + centerprint_fontsize_y;
433                 }
434                 else
435                         // half height for empty lines looks better
436                         pos_y = pos_y + centerprint_fontsize_y * 0.5;
437         }
438 }
439
440 void drawstringright(vector position, string text, vector scale, vector rgb, float alpha, float flag)
441 {
442         position_x -= 2 / 3 * strlen(text) * scale_x;
443         drawstring(position, text, scale, rgb, alpha, flag);
444 }
445
446 void drawstringcenter(vector position, string text, vector scale, vector rgb, float alpha, float flag)
447 {
448         position_x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * scale_x);
449         drawstring(position, text, scale, rgb, alpha, flag);
450 }
451
452 // return the string of the given race place
453 string race_PlaceName(float pos) {
454         if(pos == 1)
455                 return "1st";
456         else if(pos == 2)
457                 return "2nd";
458         else if(pos == 3)
459                 return "3rd";
460         else
461                 return strcat(ftos(pos), "th");
462 }
463
464 // return the string of the onscreen race timer
465 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
466 {
467         string col;
468         string timestr;
469         string cpname;
470         string lapstr;
471         lapstr = "";
472
473         if(histime == 0) // goal hit
474         {
475                 if(mytime > 0)
476                 {
477                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
478                         col = "^1";
479                 }
480                 else if(mytime == 0)
481                 {
482                         timestr = "+0.0";
483                         col = "^3";
484                 }
485                 else
486                 {
487                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
488                         col = "^2";
489                 }
490
491                 if(lapdelta > 0)
492                 {
493                         lapstr = strcat(" (-", ftos(lapdelta), "L)");
494                         col = "^2";
495                 }
496                 else if(lapdelta < 0)
497                 {
498                         lapstr = strcat(" (+", ftos(-lapdelta), "L)");
499                         col = "^1";
500                 }
501         }
502         else if(histime > 0) // anticipation
503         {
504                 if(mytime >= histime)
505                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
506                 else
507                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
508                 col = "^3";
509         }
510         else
511                 col = "^7";
512
513         if(cp == 254)
514                 cpname = "Start line";
515         else if(cp == 255)
516                 cpname = "Finish line";
517         else if(cp)
518                 cpname = strcat("Intermediate ", ftos(cp));
519         else
520                 cpname = "Finish line";
521
522         if(histime < 0)
523                 return strcat(col, cpname);
524         else if(hisname == "")
525                 return strcat(col, cpname, " (", timestr, ")");
526         else
527                 return strcat(col, cpname, " (", timestr, " ", strcat(hisname, col, lapstr), ")");
528 }
529
530 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
531 float race_CheckName(string net_name) {
532         float i;
533         for (i=RANKINGS_CNT-1;i>=0;--i)
534                 if(grecordholder[i] == net_name)
535                         return i+1;
536         return 0;
537 }
538
539 /*
540 ==================
541 HUD panels
542 ==================
543 */
544
545 string HUD_Panel_GetName(float id)
546 {
547         switch(id) {
548                 case 0: return "weaponicons"; break;
549                 case 1: return "inventory"; break;
550                 case 2: return "powerups"; break;
551                 case 3: return "healtharmor"; break;
552                 case 4: return "notify"; break;
553                 case 5: return "timer"; break;
554                 case 6: return "radar"; break;
555                 case 7: return "score"; break;
556                 case 8: return "racetimer"; break;
557                 case 9: return "vote"; break;
558                 case 10: return "pressedkeys"; break;
559                 default: return "";
560         }
561 }
562
563 // Save the config
564 void HUD_Panel_ExportCfg(string cfgname)
565 {
566         float fh;
567         fh = fopen(strcat("hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg"), FILE_WRITE);
568         if(fh >= 0)
569         {
570                 fputs(fh, strcat("seta hud_skin \"", cvar_string("hud_skin"), "\"", "\n"));
571                 fputs(fh, strcat("seta hud_bg \"", cvar_string("hud_bg"), "\"", "\n"));
572                 fputs(fh, strcat("seta hud_bg_color \"", cvar_string("hud_bg_color"), "\"", "\n"));
573                 fputs(fh, strcat("seta hud_bg_alpha ", ftos(cvar("hud_bg_alpha")), "\n"));
574                 fputs(fh, strcat("seta hud_bg_border ", ftos(cvar("hud_bg_border")), "\n"));
575                 fputs(fh, strcat("seta hud_fg_alpha ", ftos(cvar("hud_fg_alpha")), "\n"));
576                 fputs(fh, "\n");
577
578                 fputs(fh, strcat("seta hud_dock \"", cvar_string("hud_dock"), "\"", "\n"));
579                 fputs(fh, strcat("seta hud_dock_color \"", cvar_string("hud_dock_color"), "\"", "\n"));
580                 fputs(fh, strcat("seta hud_dock_alpha ", ftos(cvar("hud_dock_alpha")), "\n"));
581                 fputs(fh, "\n");
582
583                 fputs(fh, strcat("seta hud_progressbar_alpha ", ftos(cvar("hud_progressbar_alpha")), "\n"));
584                 fputs(fh, strcat("seta hud_progressbar_strength_color \"", cvar_string("hud_progressbar_strength_color"), "\"", "\n"));
585                 fputs(fh, strcat("seta hud_progressbar_shield_color \"", cvar_string("hud_progressbar_shield_color"), "\"", "\n"));
586                 fputs(fh, strcat("seta hud_progressbar_health_color \"", cvar_string("hud_progressbar_health_color"), "\"", "\n"));
587                 fputs(fh, strcat("seta hud_progressbar_armor_color \"", cvar_string("hud_progressbar_armor_color"), "\"", "\n"));
588                 fputs(fh, strcat("seta hud_progressbar_fuel_color \"", cvar_string("hud_progressbar_fuel_color"), "\"", "\n"));
589                 fputs(fh, "\n");
590
591                 // common cvars for all panels
592                 float i;
593                 for (i = 0; i < panel_cnt; ++i)
594                 {
595                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), " ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i)))), "\n"));
596                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_pos \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_pos")), "\"", "\n"));
597                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_size \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_size")), "\"", "\n"));
598                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg")), "\"", "\n"));
599                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_color \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_color")), "\"", "\n"));
600                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_bg_alpha"))), "\n"));
601                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_border ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_bg_border"))), "\n"));
602                         switch(i) {
603                                 case 0:
604                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_accuracy_height ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_accuracy_height"))), "\n"));
605                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_accuracy_yellow ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_accuracy_yellow"))), "\n"));
606                                         break;
607                                 case 1:
608                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_onlycurrent ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_onlycurrent"))), "\n"));
609                                         break;
610                                 case 2:
611                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
612                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_mirror ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
613                                         break;
614                                 case 3:
615                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
616                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_mirror ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
617                                         break;
618                                 case 9:
619                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha"))), "\n"));
620                                         break;
621                         }
622                         fputs(fh, "\n");
623                 }
624
625                 print("^2Successfully exported to hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg! (Note: It's saved in data/data/)\n");
626         }
627         fclose(fh);
628 }
629
630 vector HUD_Panel_CheckLimitSize(float id, vector mySize)
631 {
632         switch(id) {
633                 case 0: 
634                         mySize_x = max(mySize_y * (1/10), mySize_x); // at least 1/10 * height
635                         mySize_y = max(mySize_x * (1/26), mySize_y); // at least 1/26 * width
636                         break;
637                 case 1: 
638                         mySize_x = max(mySize_y * 0.7, mySize_x); // at least 0.7 * height
639                         break;
640                 //case 2: 
641                 //      mySize_x = max(mySize_y * 1.5, mySize_x); // at least 1.5 * height
642                 //      break;
643                 //case 3: 
644                 //      mySize_x = max(mySize_y * 2, mySize_x); // at least 2 * height
645                 //      break;
646                 case 5: 
647                         mySize_y = (1/4.1) * mySize_x; // 1/4.1 * width, trial and error...
648                         break;
649                 case 7: 
650                         mySize_y = (1/4) * mySize_x; // 1/4 * width
651                         break;
652                 case 8: 
653                         mySize_y = (1/4) * mySize_x; // 1/4 * width
654                         break;
655                 case 9: 
656                         mySize_y = (1/4) * mySize_x; // 1/4 * width
657                         break;
658                 case 10: 
659                         mySize_y = 0.5898 * mySize_x; // 0.5898 * width, reason: bg has weird dimensions...
660                         break;
661         }
662         return mySize;
663 }
664
665 // return active status of panel
666 float HUD_Panel_CheckActive(float id)
667 {
668         if (cvar_or(strcat("hud_", HUD_Panel_GetName(id)), 1))
669                 return 1;
670         return 0;
671 }
672
673 // return size of given panel
674 vector HUD_Panel_GetSize(float id)
675 {
676         vector mySize;
677         mySize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
678
679         mySize = eX * mySize_x * vid_conwidth + eY * mySize_y * vid_conheight;
680
681         return mySize;
682 }
683
684 // return pos of given panel
685 vector HUD_Panel_GetPos(float id)
686 {
687         vector pos;
688         pos = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_pos")));
689
690         pos = eX * pos_x * vid_conwidth + eY * pos_y * vid_conheight;
691
692         if (pos_x < 0)
693                 pos_x = vid_conwidth + pos_x;
694         if (pos_y < 0)
695                 pos_y = vid_conheight + pos_y;
696         return pos;
697 }
698
699 float HUD_Panel_GetBorder(float id)
700 {
701         float border;
702         border = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_border"));
703         if(!border)
704                 border = cvar("hud_bg_border");
705         return border;
706 }
707
708 // draw the background/borders
709 void HUD_Panel_DrawBg(float id, vector pos, vector mySize)
710 {
711         if(!hud_configure && cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg")) == "0")
712                 return;
713
714         string bg;
715         bg = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg"));
716         if(bg == "")
717                 bg = cvar_string("hud_bg");
718
719         if(bg != "0")
720         {
721                 float border;
722                 border = HUD_Panel_GetBorder(id);
723
724                 vector color;
725                 if(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color")) != "")
726                         color = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color")));
727                 else
728                         color = stov(cvar_string("hud_bg_color"));
729
730                 float alpha;
731                 alpha = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_alpha"));
732                 if(!alpha)
733                         alpha = cvar("hud_bg_alpha");
734                 if(hud_configure)
735                         alpha = max(cvar("hud_configure_bg_minalpha"), alpha);
736
737                 draw_BorderPicture(pos - '1 1 0' * border, strcat("gfx/hud/", cvar_string("hud_skin"), "/", bg), mySize + '1 1 0' * 2 * border, color, alpha, '1 1 0' * (border/BORDER_MULTIPLIER));
738         }
739 }
740
741 vector HUD_Panel_GetProgressBarColor(string item)
742 {
743         return stov(cvar_string(strcat("hud_progressbar_", item, "_color")));
744 }
745
746 float resizeCorner; // 1 = topleft, 2 = topright, 3 = bottomleft, 4 = bottomright
747 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
748 vector HUD_Panel_CheckResize(float id, vector myPos, vector mySize)
749 {
750         float i;
751
752         vector myTarget;
753         myTarget = mySize;
754
755         vector targPos;
756         vector targSize;
757         vector myCenter;
758         vector targCenter;
759
760         for (i = 0; i < panel_cnt; ++i) {
761                 if(i == id || !HUD_Panel_CheckActive(i))
762                         continue;
763
764                 targPos = HUD_Panel_GetPos(i);
765                 targSize = HUD_Panel_GetSize(i);
766
767                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
768                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(id);
769
770                 if(myPos_y + mySize_y < targPos_y)
771                         continue;
772                 if(myPos_y > targPos_y + targSize_y)
773                         continue;
774
775                 if(myPos_x + mySize_x < targPos_x)
776                         continue;
777                 if(myPos_x > targPos_x + targSize_x)
778                         continue;
779
780                 // OK, there IS a collision.
781
782                 myCenter_x = myPos_x + 0.5 * mySize_x;
783                 myCenter_y = myPos_y + 0.5 * mySize_y;
784
785                 targCenter_x = targPos_x + 0.5 * targSize_x;
786                 targCenter_y = targPos_y + 0.5 * targSize_y;
787
788                 float k, y;
789                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y && resizeCorner != 1) // top left (of target panel)
790                 {
791                         if(myPos_x + mySize_x - targPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
792                                 myTarget_x = targPos_x - myPos_x;
793                         else // push it upwards
794                                 myTarget_y = targPos_y - myPos_y;
795                 }
796                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y && resizeCorner != 2) // top right
797                 {
798                         if(targPos_x + targSize_x - myPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
799                                 myTarget_x = targPos_x + targSize_x;
800                         else // push it upwards
801                                 myTarget_y = targPos_y - myPos_y;
802                 }
803                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
804                 {
805                         if(myPos_x + mySize_x - targPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
806                                 myTarget_x = targPos_x - myPos_x;
807                         else // push it downwards
808                                 myTarget_y = targPos_y + targSize_y;
809                 }
810                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
811                 {
812                         if(targPos_x + targSize_x - myPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
813                                 myTarget_x = targPos_x + targSize_x;
814                         else // push it downwards
815                                 myTarget_y = targPos_y + targSize_y;
816                 }
817         }
818
819         return myTarget;
820 }
821
822 // TODO: checkResize
823 float HUD_Panel_SetSize(float id, vector mySize)
824 {
825         float didntresize;
826
827         vector oldSize;
828         oldSize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
829
830         vector myPos;
831         myPos = HUD_Panel_GetPos(id);
832
833         // check for collisions
834         if(cvar("hud_configure_checkcollisions"))
835                 mySize = HUD_Panel_CheckResize(id, HUD_Panel_GetPos(id), mySize);
836
837         mySize_x = bound(0.025 * vid_conwidth, mySize_x, vid_conwidth - myPos_x);
838         mySize_y = bound(0.025 * vid_conheight, mySize_y, vid_conheight - myPos_y);
839
840         // cap against panel's own limits
841         mySize = HUD_Panel_CheckLimitSize(id, mySize);
842
843         //mySize_x = bound(0.025 * vid_conwidth, mySize_x, vid_conwidth);
844         //mySize_y = bound(0.025 * vid_conheight, mySize_y, vid_conheight);
845
846         // TODO: is this needed?
847         // this is to check if (and how) SetPos should be called
848         if(mySize_x == oldSize_x && mySize_y == oldSize_y)
849                 didntresize = 3; // didnt resize either
850         else if(mySize_x == oldSize_x && mySize_y != oldSize_y)
851                 didntresize = 2; // resized Y
852         else if(mySize_y == oldSize_y && mySize_x != oldSize_x)
853                 didntresize = 1; // resized X
854
855         string s;
856         s = strcat(ftos(mySize_x/vid_conwidth), " ", ftos(mySize_y/vid_conheight));
857         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_size"), s);
858         return didntresize;
859 }
860
861 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
862 vector HUD_Panel_CheckMove(float id, vector myPos, vector mySize)
863 {
864         float i;
865
866         vector myTarget;
867         myTarget = myPos;
868
869         vector targPos;
870         vector targSize;
871         vector myCenter;
872         vector targCenter;
873
874         for (i = 0; i < panel_cnt; ++i) {
875                 if(i == id || !HUD_Panel_CheckActive(i))
876                         continue;
877
878                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
879                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(id);
880
881                 if(myPos_y + mySize_y < targPos_y)
882                         continue;
883                 if(myPos_y > targPos_y + targSize_y)
884                         continue;
885
886                 if(myPos_x + mySize_x < targPos_x)
887                         continue;
888                 if(myPos_x > targPos_x + targSize_x)
889                         continue;
890
891                 // OK, there IS a collision.
892
893                 myCenter_x = myPos_x + 0.5 * mySize_x;
894                 myCenter_y = myPos_y + 0.5 * mySize_y;
895
896                 targCenter_x = targPos_x + 0.5 * targSize_x;
897                 targCenter_y = targPos_y + 0.5 * targSize_y;
898
899                 float k, y;
900                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y) // top left (of the target panel)
901                 {
902                         if(myPos_x + mySize_x - targPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
903                                 myTarget_x = targPos_x - mySize_x;
904                         else // push it upwards
905                                 myTarget_y = targPos_y - mySize_y;
906                 }
907                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y) // top right
908                 {
909                         if(targPos_x + targSize_x - myPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
910                                 myTarget_x = targPos_x + targSize_x;
911                         else // push it upwards
912                                 myTarget_y = targPos_y - mySize_y;
913                 }
914                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
915                 {
916                         if(myPos_x + mySize_x - targPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
917                                 myTarget_x = targPos_x - mySize_x;
918                         else // push it downwards
919                                 myTarget_y = targPos_y + targSize_y;
920                 }
921                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
922                 {
923                         if(targPos_x + targSize_x - myPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
924                                 myTarget_x = targPos_x + targSize_x;
925                         else // push it downwards
926                                 myTarget_y = targPos_y + targSize_y;
927                 }
928         }
929
930         return myTarget;
931 }
932
933 void HUD_Panel_SetPos(float id, vector pos, float didntresize)
934 {
935         vector oldPos;
936         oldPos = HUD_Panel_GetPos(id);
937         if(didntresize == 2)
938                 pos_x = oldPos_x;
939         else if(didntresize == 1)
940                 pos_y = oldPos_y;
941         else if(didntresize == 3)
942                 return;
943
944         vector mySize;
945         mySize = HUD_Panel_GetSize(id);
946
947         if(cvar("hud_configure_checkcollisions"))
948                 pos = HUD_Panel_CheckMove(id, pos, mySize);
949
950         pos_x = bound(0, pos_x, vid_conwidth - mySize_x);
951         pos_y = bound(0, pos_y, vid_conheight - mySize_y);
952
953         if (pos_x + 0.5 * mySize_x > 0.5 * vid_conwidth)
954                 pos_x = pos_x - vid_conwidth;
955         if (pos_y + 0.5 * mySize_y > 0.5 * vid_conheight)
956                 pos_y = pos_y - vid_conheight;
957
958         string s;
959         s = strcat(ftos(pos_x/vid_conwidth), " ", ftos(pos_y/vid_conheight));
960
961         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_pos"), s);
962 }
963
964 float mouseClicked;
965 float prevMouseClicked; // previous state
966 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
967 {
968         prevMouseClicked = mouseClicked;
969         if(nPrimary == K_MOUSE1)
970         {
971                 if(bInputType == 0) { // key pressed
972                         mouseClicked = 1;
973                         return true;
974                 }
975                 if(bInputType == 1) {// key released
976                         mouseClicked = 0;
977                         return true;
978                 }
979         }
980         return false;
981 }
982
983 vector mousepos, mouseprevpos;
984 vector panel_click_distance; // mouse cursor distance from the top left corner of the panel (saved only upon a click)
985 vector panel_click_pos; // panel pos (saved only upon a click)
986 vector panel_click_size; // panel size (saved only upon a click)
987 float highlightedPanel;
988 float highlightedAction; // 0 = nothing, 1 = move, 2 = resize
989 void HUD_Panel_Mouse()
990 {
991         if(mouseClicked == 0) {
992                 highlightedPanel = -1;
993                 highlightedAction = 0;
994         }
995
996         mousepos = mousepos + getmousepos();
997
998         mousepos_x = bound(0, mousepos_x, vid_conwidth);
999         mousepos_y = bound(0, mousepos_y, vid_conheight);
1000
1001         drawpic(mousepos, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1002
1003         if(mouseClicked)
1004         {
1005                 float i, border;
1006                 vector panelPos;
1007                 vector panelSize;
1008
1009                 for(i = 0; i <= panel_cnt; ++i)
1010                 {
1011                         panelPos = HUD_Panel_GetPos(i);
1012                         panelSize = HUD_Panel_GetSize(i);
1013                         border = HUD_Panel_GetBorder(i);
1014                         if(prevMouseClicked == 0) {
1015                                 // move
1016                                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1017                                 {
1018                                         highlightedPanel = i;
1019                                         highlightedAction = 1;
1020                                 }
1021                                 // resize from topleft border
1022                                 else if(mousepos_x >= panelPos_x - border && mousepos_y >= panelPos_y - border && mousepos_x <= panelPos_x + 0.5 * panelSize_x && mousepos_y <= panelPos_y + 0.5 * panelSize_y)
1023                                 {
1024                                         highlightedPanel = i;
1025                                         highlightedAction = 2;
1026                                         resizeCorner = 1;
1027                                 }
1028                                 // resize from topright border
1029                                 else if(mousepos_x >= panelPos_x + 0.5 * panelSize_x && mousepos_y >= panelPos_y - border && mousepos_x <= panelPos_x + panelSize_x + border && mousepos_y <= panelPos_y + 0.5 * panelSize_y)
1030                                 {
1031                                         highlightedPanel = i;
1032                                         highlightedAction = 2;
1033                                         resizeCorner = 2;
1034                                 }
1035                                 // resize from bottomleft border
1036                                 else if(mousepos_x >= panelPos_x - border && mousepos_y >= panelPos_y + 0.5 * panelSize_y && mousepos_x <= panelPos_x + 0.5 * panelSize_x && mousepos_y <= panelPos_y + panelSize_y + border)
1037                                 {
1038                                         highlightedPanel = i;
1039                                         highlightedAction = 2;
1040                                         resizeCorner = 3;
1041                                 }
1042                                 // resize from bottomright border
1043                                 else if(mousepos_x >= panelPos_x + 0.5 * panelSize_x && mousepos_y >= panelPos_y + 0.5 * panelSize_y && mousepos_x <= panelPos_x + panelSize_x + border && mousepos_y <= panelPos_y + panelSize_y + border)
1044                                 {
1045                                         highlightedPanel = i;
1046                                         highlightedAction = 2;
1047                                         resizeCorner = 4;
1048                                 }
1049                         }
1050
1051                         if(highlightedPanel == i)
1052                         {
1053                                 if(prevMouseClicked == 0)
1054                                 {
1055                                         panel_click_distance = mousepos - panelPos;
1056                                         panel_click_pos = panelPos;
1057                                         panel_click_size = panelSize;
1058                                 }
1059
1060                                 if(highlightedAction == 1)
1061                                         HUD_Panel_SetPos(i, mousepos - panel_click_distance, 0);
1062                                 else if(highlightedAction == 2)
1063                                 {
1064                                         float didntresize; // panel too big/too small, dont resize (also has to affect moving)
1065                                         if(resizeCorner == 1) {
1066                                                 didntresize = HUD_Panel_SetSize(i, panelSize + mouseprevpos - mousepos);
1067                                                 HUD_Panel_SetPos(i, mousepos - panel_click_distance, didntresize);
1068                                         }
1069                                         if(resizeCorner == 2) {
1070                                                 didntresize = HUD_Panel_SetSize(i, eY * panel_click_size_y + eX * (mousepos_x - panelPos_x - (panel_click_distance_x - panel_click_size_x))
1071                                                                 + eY * (panel_click_distance_y + (panel_click_pos_y - mousepos_y)));
1072                                                 HUD_Panel_SetPos(i, eX * panelPos_x + eY * (mousepos_y - panel_click_distance_y), didntresize);
1073                                         }
1074                                         if(resizeCorner == 3) {
1075                                                 didntresize = HUD_Panel_SetSize(i, panelSize + eX * (mouseprevpos_x - mousepos_x) + eY * (mousepos_y - mouseprevpos_y));
1076                                                 HUD_Panel_SetPos(i, eX * (mousepos_x - panel_click_distance_x) + eY * panelPos_y, didntresize);
1077                                         }
1078                                         if(resizeCorner == 4) {
1079                                                 HUD_Panel_SetSize(i, mousepos - panelPos - (panel_click_distance - panel_click_size));
1080                                         }
1081                                 }
1082                         }
1083                 }
1084         }
1085         mouseprevpos = mousepos;
1086         prevMouseClicked = mouseClicked;
1087 }
1088
1089 /*void HUD_DrawDockEdge(float id, vector p1, vector p2, float target)
1090 {
1091         vector pos;
1092         vector size;
1093
1094         pos =
1095 */ // maybe one day, since this will be quite complicated
1096
1097 // Weapon icons (#0)
1098 //
1099
1100 float weaponspace[10];
1101 void HUD_WeaponIcons_Clear()
1102 {
1103         float idx;
1104         for(idx = 0; idx < 10; ++idx)
1105                 weaponspace[idx] = 0;
1106 }
1107
1108 entity weaponorder[WEP_MAXCOUNT];
1109
1110 void weaponorder_swap(float i, float j, entity pass)
1111 {
1112         entity h;
1113         h = weaponorder[i];
1114         weaponorder[i] = weaponorder[j];
1115         weaponorder[j] = h;
1116 }
1117
1118 float weaponorder_cmp(float i, float j, entity pass)
1119 {
1120         float d, ii, ij;
1121         d = mod(weaponorder[i].impulse + 9, 10) - mod(weaponorder[j].impulse + 9, 10);
1122         if(d)
1123                 return d;
1124         d = weaponorder[i].weapon - weaponorder[j].weapon;
1125         return d;
1126 }
1127
1128 void HUD_WeaponIcons()
1129 {
1130         float 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(minutes > 999)
2000                 seconds = 99;
2001         minutes = min(minutes, 999);
2002         if(minutesLeft >= 1 || cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
2003                 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);
2004                 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);
2005         }
2006         HUD_DrawXNum(pos + eX * mySize_x - eX * 2 * mySize_y, seconds, -2, 0, mySize_y, timer_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2007 }
2008
2009 // Radar (#6)
2010 //
2011 void HUD_Radar(void)
2012 {
2013         vector pos, mySize;
2014         pos = HUD_Panel_GetPos(6);
2015         mySize = HUD_Panel_GetSize(6);
2016
2017         HUD_Panel_DrawBg(6, pos, mySize);
2018
2019         local float color1, color2; // color already declared as a global in hud.qc
2020         local vector rgb;
2021         local entity tm;
2022         float scale2d, normalsize, bigsize;
2023         float f;
2024
2025         teamradar_origin2d = pos + 0.5 * mySize; // TODO: stupid compat, should be removed
2026         teamradar_size2d = mySize;
2027
2028         if(minimapname == "" && !ons_showmap)
2029                 return;
2030
2031         teamradar_loadcvars();
2032
2033         switch(cl_teamradar_zoommode)
2034         {
2035                 default:
2036                 case 0:
2037                         f = current_zoomfraction;
2038                         break;
2039                 case 1:
2040                         f = 1 - current_zoomfraction;
2041                         break;
2042                 case 2:
2043                         f = 0;
2044                         break;
2045                 case 3:
2046                         f = 1;
2047                         break;
2048         }
2049
2050         switch(cl_teamradar_rotation)
2051         {
2052                 case 0:
2053                         teamradar_angle = view_angles_y - 90;
2054                         break;
2055                 default:
2056                         teamradar_angle = 90 * cl_teamradar_rotation;
2057                         break;
2058         }
2059
2060         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
2061         teamradar_size2d = mySize;
2062
2063         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
2064
2065         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
2066         if(cl_teamradar_rotation == 0)
2067         {
2068                 // max-min distance must fit the radar in any rotation
2069                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
2070         }
2071         else
2072         {
2073                 vector c0, c1, c2, c3, span;
2074                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
2075                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
2076                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
2077                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
2078                 span = '0 0 0';
2079                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
2080                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
2081
2082                 // max-min distance must fit the radar in x=x, y=y
2083                 bigsize = min(
2084                         teamradar_size2d_x * scale2d / (1.05 * span_x),
2085                         teamradar_size2d_y * scale2d / (1.05 * span_y)
2086                 );
2087         }
2088
2089         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / cl_teamradar_scale;
2090         if(bigsize > normalsize)
2091                 normalsize = bigsize;
2092
2093         teamradar_size =
2094                   f * bigsize
2095                 + (1 - f) * normalsize;
2096         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
2097                   f * (mi_min + mi_max) * 0.5
2098                 + (1 - f) * view_origin);
2099
2100         color1 = GetPlayerColor(player_localentnum-1);
2101         rgb = GetTeamRGB(color1);
2102
2103         drawsetcliparea(
2104                 pos_x,
2105                 pos_y,
2106                 mySize_x,
2107                 mySize_y
2108         );
2109
2110         draw_teamradar_background(cl_teamradar_background_alpha, cl_teamradar_foreground_alpha);
2111
2112         if(ons_showmap)
2113         {
2114                 drawresetcliparea();
2115
2116                 vector frame_origin, frame_size;
2117                 frame_origin = frame_size = '0 0 0';
2118
2119                 frame_origin_x = pos_x - teamradar_size2d_x * 0.55859375; // matches the picture
2120                 frame_origin_y = pos_y - teamradar_size2d_y * 0.55859375; // matches the picture
2121                 frame_size_x = pos_x * 1.1171875; // matches the picture
2122                 frame_size_y = pos_y * 1.1171875; // matches the picture
2123                 drawpic_skin(frame_origin, "gfx/ons-frame.tga", frame_size, '1 1 1', hud_alpha_fg, 0);
2124                 drawpic_skin(frame_origin, "gfx/ons-frame-team.tga", frame_size, rgb, hud_alpha_fg, 0);
2125
2126                 drawsetcliparea(
2127                         pos_x - teamradar_size2d_x * 0.5,
2128                         pos_y - teamradar_size2d_y * 0.5,
2129                         teamradar_size2d_x,
2130                         teamradar_size2d_y
2131                 );
2132         }
2133
2134         for(tm = world; (tm = find(tm, classname, "radarlink")); )
2135                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
2136         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
2137                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, hud_alpha_fg);
2138         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
2139         {
2140                 color2 = GetPlayerColor(tm.sv_entnum);
2141                 //if(color == COLOR_SPECTATOR || color == color2)
2142                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
2143         }
2144         draw_teamradar_player(view_origin, view_angles, '1 1 1');
2145
2146         drawresetcliparea();
2147 };
2148
2149 // Score (#7)
2150 //
2151 void HUD_Score()
2152 {
2153         vector pos, mySize;
2154         pos = HUD_Panel_GetPos(7);
2155         mySize = HUD_Panel_GetSize(7);
2156
2157         HUD_Panel_DrawBg(7, pos, mySize);
2158
2159         float score, distribution, leader;
2160         float score_len, distr_len;
2161         vector score_pos, secondary_score_pos, distribution_color;
2162         entity tm, pl, me;
2163         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
2164
2165         if (!teamplay) { // non-teamgames
2166                 // me vector := [team/connected frags id]
2167                 pl = players.sort_next;
2168                 if(pl == me)
2169                         pl = pl.sort_next;
2170
2171                 if(hud_configure)
2172                         distribution = 42;
2173                 else if(pl)
2174                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2175                 else
2176                         distribution = 0;
2177
2178                 score = me.(scores[ps_primary]);
2179                 if(hud_configure)
2180                         score = 123;
2181
2182                 if(distribution >= 5) {
2183                         distribution_color = eY;
2184                         leader = 1;
2185                 } else if(distribution >= 0) {
2186                         distribution_color = '1 1 1';
2187                         leader = 1;
2188                 } else if(distribution >= -5)
2189                         distribution_color = '1 1 0';
2190                 else
2191                         distribution_color = eX;
2192
2193                 score_len = strlen(ftos(score));
2194                 distr_len = strlen(ftos(distribution));
2195
2196                 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);
2197                 if (leader)
2198                         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);
2199                 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);
2200         } else { // teamgames
2201                 float max_fragcount;
2202                 max_fragcount = -99;
2203
2204                 float teamnum;
2205                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2206                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !hud_configure)) // no players? don't display
2207                                 continue;
2208                         score = tm.(teamscores[ts_primary]);
2209                         if(hud_configure)
2210                                 score = 123;
2211                         leader = 0;
2212                         
2213                         score_len = strlen(ftos(score));
2214
2215                         if (score > max_fragcount)
2216                                 max_fragcount = score;
2217
2218                         if(tm.team == myteam) {
2219                                 if (max_fragcount == score)
2220                                         leader = 1;
2221                                 if (leader)
2222                                         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);
2223                                 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);
2224                         } else {
2225                                 if (max_fragcount == score)
2226                                         leader = 1;
2227                                 if (leader)
2228                                         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);
2229                                 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);
2230                                 teamnum += 1;
2231                         }
2232                 }
2233         }
2234 }
2235
2236 // Race timer (#8)
2237 //
2238 void HUD_RaceTimer (void) {
2239         vector pos, mySize;
2240         pos = HUD_Panel_GetPos(8);
2241         mySize = HUD_Panel_GetSize(8);
2242
2243         HUD_Panel_DrawBg(8, pos, mySize);
2244
2245         drawfont = hud_bigfont;
2246         float a, t;
2247         string s, forcetime;
2248
2249         if(hud_configure)
2250         {
2251                 s = "0:13:37";
2252                 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);
2253                 s = "^1Intermediate 1 (+15.42)";
2254                 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);
2255         }
2256         else if(race_checkpointtime)
2257         {
2258                 a = bound(0, 2 - (time - race_checkpointtime), 1);
2259                 s = "";
2260                 forcetime = "";
2261                 if(a > 0) // just hit a checkpoint?
2262                 {
2263                         if(race_checkpoint != 254)
2264                         {
2265                                 if(race_time && race_previousbesttime)
2266                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
2267                                 else
2268                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
2269                                 if(race_time)
2270                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
2271                         }
2272                 }
2273                 else
2274                 {
2275                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
2276                         {
2277                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
2278                                 if(a > 0) // next one?
2279                                 {
2280                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
2281                                 }
2282                         }
2283                 }
2284
2285                 if(s != "" && a > 0)
2286                 {
2287                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2288                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2289                         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);
2290                 }
2291
2292                 if(race_penaltytime)
2293                 {
2294                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
2295                         if(a > 0)
2296                         {
2297                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
2298                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2299                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2300                                 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);
2301                         }
2302                 }
2303
2304                 if(forcetime != "")
2305                 {
2306                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
2307                         //drawstring_expanding(m - '16 0 0' * stringwidth(forcetime, FALSE), forcetime, '32 32 0', '1 1 1', hud_alpha_fg, 0, a);
2308                         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);
2309                 }
2310                 else
2311                         a = 1;
2312
2313                 if(race_laptime && race_checkpoint != 255)
2314                 {
2315                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
2316                         //drawstring(m - '16 0 0' * stringwidth(s, FALSE), s, '32 32 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2317                         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);
2318                 }
2319         }
2320         else
2321         {
2322                 if(race_mycheckpointtime)
2323                 {
2324                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
2325                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
2326                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2327                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2328                         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);
2329                 }
2330                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
2331                 {
2332                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
2333                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
2334                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2335                         //drawcolorcodedstring(m - '0 0 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2336                         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);
2337                 }
2338
2339                 if(race_penaltytime && !race_penaltyaccumulator)
2340                 {
2341                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
2342                         a = bound(0, (1 + t - time), 1);
2343                         if(a > 0)
2344                         {
2345                                 if(time < t)
2346                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
2347                                 else
2348                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
2349                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2350                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2351                                 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);
2352                         }
2353                 }
2354         }
2355
2356         drawfont = hud_font;
2357 }
2358
2359 // Vote window (#9)
2360 float vote_yescount;
2361 float vote_nocount;
2362 float vote_needed;
2363 float vote_highlighted; // currently selected vote
2364
2365 float vote_active; // is there an active vote?
2366 float vote_prev; // previous state of vote_active to check for a change
2367 float vote_alpha;
2368 float vote_change; // "time" when vote_active changed
2369
2370 void HUD_VoteWindow(void) 
2371 {
2372         vector pos, mySize;
2373         pos = HUD_Panel_GetPos(9);
2374         mySize = HUD_Panel_GetSize(9);
2375
2376         string s;
2377         float a;
2378         if(vote_active != vote_prev) {
2379                 vote_change = time;
2380                 vote_prev = bound(0, vote_active, 1);
2381         }
2382
2383         if(vote_active)
2384                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
2385         else
2386                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
2387
2388         if(hud_configure)
2389         {
2390                 vote_alpha = 1;
2391                 vote_yescount = 3;
2392                 vote_nocount = 2;
2393                 vote_needed = 4;
2394         }
2395
2396         HUD_Panel_DrawBg(9, pos, mySize);
2397
2398         if(vote_alpha) {
2399                 a = vote_alpha * bound(cvar_or("hud_vote_alreadyvoted_alpha", 0.75), 1 - vote_highlighted, 1);
2400
2401                 drawpic_skin(pos, "voteprogress_back", mySize, HUD_GetBgColor(), a * hud_alpha_bg, DRAWFLAG_NORMAL);
2402
2403                 s = "A vote has been called for: ";
2404                 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);
2405                 s = textShortenToWidth(vote_called_vote, mySize_x * 0.96, '10 0 0', stringwidth_colors);
2406                 if(hud_configure)
2407                         s = "Configure the HUD";
2408                 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);
2409
2410                 // print the yes/no counts
2411                 s = strcat("Yes: ", ftos(vote_yescount));
2412                 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);
2413                 s = strcat("No: ", ftos(vote_nocount));
2414                 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);
2415
2416                 // draw the progress bars
2417                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
2418                 drawpic_skin(pos, "voteprogress_prog", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2419
2420                 drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
2421                 drawpic_skin(pos, "voteprogress_prog", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2422
2423                 // draw the highlights
2424                 if(vote_highlighted == 1) {
2425                         drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
2426                         drawpic_skin(pos, "voteprogress_voted", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2427                 }
2428                 else if(vote_highlighted == 2) {
2429                         drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
2430                         drawpic_skin(pos, "voteprogress_voted", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2431                 }
2432
2433                 drawresetcliparea();
2434         }
2435         else if(!vote_active) {
2436                 vote_highlighted = 0;
2437         }
2438 }
2439
2440 // Awards system
2441 float race_status_time;
2442 float race_status_prev;
2443 string race_status_name_prev;
2444 void HUD_DrawRaceStatus(vector pos)
2445 {
2446         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
2447                 race_status_time = time + 5;
2448                 race_status_prev = race_status;
2449                 if (race_status_name_prev)
2450                         strunzone(race_status_name_prev);
2451                 race_status_name_prev = strzone(race_status_name);
2452         }
2453
2454         float a;
2455         a = bound(0, race_status_time - time, 1);
2456
2457         string s;
2458         s = textShortenToWidth(race_status_name, 120, '10 10 0', stringwidth_colors);
2459
2460         float rank;
2461         if(race_status > 0)
2462                 rank = race_CheckName(race_status_name);
2463         string rankname;
2464         rankname = race_PlaceName(rank);
2465
2466         if(race_status == 0)
2467                 drawpic_skin(pos, "race_newfail", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2468         else if(race_status == 1) {
2469                 drawpic_skin(pos, "race_newtime", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2470                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2471                 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);
2472         } else if(race_status == 2) {
2473                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
2474                         drawpic_skin(pos, "race_newrankgreen", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2475                 else
2476                         drawpic_skin(pos, "race_newrankyellow", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2477                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2478                 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);
2479         } else if(race_status == 3) {
2480                 drawpic_skin(pos, "race_newrecordserver", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2481                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2482                 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);
2483         }
2484
2485         if (race_status_time - time <= 0) {
2486                 race_status_prev = -1;
2487                 race_status = -1;
2488                 if(race_status_name)
2489                         strunzone(race_status_name);
2490                 race_status_name = string_null;
2491                 if(race_status_name_prev)
2492                         strunzone(race_status_name_prev);
2493                 race_status_name_prev = string_null;
2494         }
2495 }
2496
2497 // CTF HUD modicon section
2498 float redflag_prevframe, blueflag_prevframe; // status during previous frame
2499 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
2500 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
2501
2502 void CSQC_ctf_hudreset(void)
2503 {
2504         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
2505 }
2506
2507 void CSQC_ctf_hud(void)
2508 {
2509         vector bottomleft, redflag_pos, blueflag_pos, sz;
2510         float f; // every function should have that
2511         bottomleft_y = vid_conheight;
2512         bottomleft_z = 0;
2513
2514         float redflag, blueflag; // current status
2515         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
2516         float stat_items;
2517
2518         stat_items = getstati(STAT_ITEMS);
2519         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
2520         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
2521
2522         // when status CHANGES, set old status into prevstatus and current status into status
2523         if (redflag != redflag_prevframe)
2524         {
2525                 redflag_statuschange_time = time;
2526                 redflag_prevstatus = redflag_prevframe;
2527                 redflag_prevframe = redflag;
2528         }
2529
2530         if (blueflag != blueflag_prevframe)
2531         {
2532                 blueflag_statuschange_time = time;
2533                 blueflag_prevstatus = blueflag_prevframe;
2534                 blueflag_prevframe = blueflag;
2535         }
2536
2537         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
2538         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
2539
2540         float BLINK_FACTOR = 0.15;
2541         float BLINK_BASE = 0.85;
2542         // note:
2543         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
2544         // thus
2545         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
2546         // ensure RMS == 1
2547         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
2548
2549         string red_icon, red_icon_prevstatus;
2550         float red_alpha, red_alpha_prevstatus;
2551         red_alpha = red_alpha_prevstatus = 1;
2552         switch(redflag) {
2553                 case 1: red_icon = "flag_red_taken"; break;
2554                 case 2: red_icon = "flag_red_lost"; break;
2555                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2556                 default:
2557                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2558                                 red_icon = "flag_red_shielded";
2559                         else
2560                                 red_icon = string_null;
2561                         break;
2562         }
2563         switch(redflag_prevstatus) {
2564                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
2565                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
2566                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2567                 default:
2568                         if(redflag == 3)
2569                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
2570                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2571                                 red_icon_prevstatus = "flag_red_shielded";
2572                         else
2573                                 red_icon_prevstatus = string_null;
2574                         break;
2575         }
2576
2577         string blue_icon, blue_icon_prevstatus;
2578         float blue_alpha, blue_alpha_prevstatus;
2579         blue_alpha = blue_alpha_prevstatus = 1;
2580         switch(blueflag) {
2581                 case 1: blue_icon = "flag_blue_taken"; break;
2582                 case 2: blue_icon = "flag_blue_lost"; break;
2583                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2584                 default:
2585                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2586                                 blue_icon = "flag_blue_shielded";
2587                         else
2588                                 blue_icon = string_null;
2589                         break;
2590         }
2591         switch(blueflag_prevstatus) {
2592                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
2593                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
2594                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2595                 default:
2596                         if(blueflag == 3)
2597                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
2598                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2599                                 blue_icon_prevstatus = "flag_blue_shielded";
2600                         else
2601                                 blue_icon_prevstatus = string_null;
2602                         break;
2603         }
2604
2605         if (myteam == COLOR_TEAM1) { // always draw own flag on left
2606                 redflag_pos = bottomleft - '-4 50 0';
2607                 blueflag_pos = bottomleft - '-62 50 0';
2608         } else {
2609                 blueflag_pos = bottomleft - '-4 50 0';
2610                 redflag_pos = bottomleft - '-62 50 0';
2611         }
2612
2613         sz = '52 52 0';
2614
2615         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
2616         if(red_icon_prevstatus && f < 1)
2617                 drawpic_expanding(redflag_pos, red_icon_prevstatus, sz, '1 1 1', hud_alpha_fg * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2618         if(red_icon)
2619                 drawpic_skin(redflag_pos, red_icon, sz, '1 1 1', hud_alpha_fg * red_alpha * f, DRAWFLAG_NORMAL);
2620
2621         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
2622         if(blue_icon_prevstatus && f < 1)
2623                 drawpic_expanding(blueflag_pos, blue_icon_prevstatus, sz, '1 1 1', hud_alpha_fg * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2624         if(blue_icon)
2625                 drawpic_skin(blueflag_pos, blue_icon, sz, '1 1 1', hud_alpha_fg * blue_alpha * f, DRAWFLAG_NORMAL);
2626 }
2627
2628 /*void HUD_Mod_Race (void) {
2629         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
2630                 pl = players.sort_next;
2631                 if(pl == me)
2632                         pl = pl.sort_next;
2633                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
2634                         if(pl.scores[ps_primary] == 0)
2635                                 pl = world;
2636
2637                 score = me.(scores[ps_primary]);
2638
2639                 float racemin, racesec, racemsec;
2640                 float distsec, distmsec, minusplus;
2641
2642                 racemin = floor(score/(60 * TIME_FACTOR));
2643                 racesec = floor((score - racemin*(60 * TIME_FACTOR))/TIME_FACTOR);
2644                 racemsec = score - racemin*60*TIME_FACTOR - racesec*TIME_FACTOR;
2645
2646                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
2647                         // distribution display
2648                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2649
2650                         if (distribution < TIME_FACTOR && distribution > -TIME_FACTOR)
2651                                 distmsec = fabs(distribution);
2652                         else {
2653                                 distsec = floor(fabs(distribution)/TIME_FACTOR);
2654                                 distmsec = fabs(distribution) - distsec*TIME_FACTOR;
2655                                 if (distribution < 0)
2656                                         distsec = -distsec;
2657                         }
2658
2659                         if (distribution <= 0) {
2660                                 distribution_color = eY;
2661                                 minusplus = 1; // minusplus 1: always prefix with minus sign
2662                         }
2663                         else {
2664                                 distribution_color = eX;
2665                                 minusplus = 2; // minusplus 1: always prefix with plus sign
2666                         }
2667                         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);
2668                         HUD_DrawXNum(bottomright - '68 48 0' - '16 0 0' * TIME_DECIMALS, distsec, 4, minusplus, 16, distribution_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2669                         drawpic_skin(bottomright - '10 48 0' - '16 0 0' * TIME_DECIMALS, "num_dot", '16 16 0', distribution_color, hud_alpha_fg, DRAWFLAG_ADDITIVE);
2670                 }
2671                 // race record display
2672                 if (distribution <= 0 || distribution == score) // draw the highlight background behind the timer if we have the lead
2673                         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);
2674
2675                 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);
2676                 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);
2677                 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);
2678
2679                 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);
2680                 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);
2681         }
2682 }*/
2683
2684 // Keyhunt HUD modicon section
2685 float kh_runheretime;
2686
2687 void CSQC_kh_hudreset(void)
2688 {
2689         kh_runheretime = 0;
2690 }
2691
2692 void CSQC_kh_hud(void)
2693 {
2694         float kh_keys;
2695         float keyteam;
2696         float a, aa;
2697         vector p, pa, kh_size, kh_asize;
2698
2699         p_x = 6;
2700         p_y = vid_conheight - 34 - 3;
2701         p_z = 0;
2702
2703         kh_keys = getstati(STAT_KH_KEYS);
2704
2705         kh_size = '19 34 0';
2706         kh_asize = '19 10 0';
2707         pa = p + '0 -10 0';
2708
2709         float i, key;
2710
2711         float keycount;
2712         keycount = 0;
2713         for(i = 0; i < 4; ++i)
2714         {
2715                 key = floor(kh_keys / pow(32, i)) & 31;
2716                 keyteam = key - 1;
2717                 if(keyteam == 30 && keycount <= 4)
2718                         keycount += 4;
2719                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
2720                         keycount += 1;
2721         }
2722         // this yields 8 exactly if "RUN HERE" shows
2723
2724         if(keycount == 8)
2725         {
2726                 if(!kh_runheretime)
2727                         kh_runheretime = time;
2728                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
2729         }
2730         else
2731                 kh_runheretime = 0;
2732
2733         for(i = 0; i < 4; ++i)
2734         {
2735                 key = floor(kh_keys / pow(32, i)) & 31;
2736                 keyteam = key - 1;
2737                 switch(keyteam)
2738                 {
2739                         case 30: // my key
2740                                 keyteam = myteam;
2741                                 a = 1;
2742                                 aa = 1;
2743                                 break;
2744                         case -1: // no key
2745                                 a = 0;
2746                                 aa = 0;
2747                                 break;
2748                         default: // owned or dropped
2749                                 a = 0.2;
2750                                 aa = 0.5;
2751                                 break;
2752                 }
2753                 a = a * hud_alpha_fg;
2754                 aa = aa * hud_alpha_fg;
2755                 if(a > 0)
2756                 {
2757                         switch(keyteam)
2758                         {
2759                                 case COLOR_TEAM1:
2760                                         drawpic (pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2761                                         break;
2762                                 case COLOR_TEAM2:
2763                                         drawpic (pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2764                                         break;
2765                                 case COLOR_TEAM3:
2766                                         drawpic (pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2767                                         break;
2768                                 case COLOR_TEAM4:
2769                                         drawpic (pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2770                                         break;
2771                                 default:
2772                                         break;
2773                         }
2774                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
2775                         {
2776                                 case 0:
2777                                         drawpic (p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2778                                         break;
2779                                 case 1:
2780                                         drawpic (p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2781                                         break;
2782                                 case 2:
2783                                         drawpic (p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2784                                         break;
2785                                 case 3:
2786                                         drawpic (p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2787                                         break;
2788                         }
2789                 }
2790                 p_x += 24;
2791                 pa_x += 24;
2792         }
2793 }
2794
2795 // Nexball HUD modicon section
2796 #define NBPB_SIZE '96 38 0'
2797 #define NBPB_BT 2                   //thickness
2798 #define NBPB_BRGB '1 1 1'
2799 #define NBPB_BALPH 1                //alpha
2800 #define NBPB_BFLAG DRAWFLAG_NORMAL
2801 #define NBPB_IALPH 0.4
2802 #define NBPB_IFLAG DRAWFLAG_NORMAL
2803 #define NBPB_IRGB '0.7 0.1 0'
2804
2805 void CSQC_nb_hud(void)
2806 {
2807         float stat_items, nb_pb_starttime, dt, p;
2808         vector pos;
2809
2810         stat_items = getstati(STAT_ITEMS);
2811         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
2812
2813         pos_x = 4;
2814         pos_y = vid_conheight - 42;
2815         pos_z = 0;
2816
2817         //Manage the progress bar if any
2818         if (nb_pb_starttime > 0)
2819         {
2820                 vector s;
2821                 dt = mod(time - nb_pb_starttime, nb_pb_period);
2822                 // one period of positive triangle
2823                 p = 2 * dt / nb_pb_period;
2824                 if (p > 1)
2825                         p = 2 - p;
2826
2827                 s = NBPB_SIZE;
2828                 //Draw the filling
2829                 drawfill(pos, p * s_x * eX + s_y * eY, NBPB_IRGB, NBPB_IALPH, NBPB_IFLAG);
2830
2831                 //Draw the box
2832                 s = NBPB_SIZE;
2833                 drawline(NBPB_BT, pos    , pos + eX * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2834                 drawline(NBPB_BT, pos    , pos + eY * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2835                 drawline(NBPB_BT, pos + s, pos + eX * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2836                 drawline(NBPB_BT, pos + s, pos + eY * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2837         }
2838
2839         pos_x += 12; //horizontal margin to the picture
2840         pos_y += 2; //vertical margin to the picture
2841
2842         if (stat_items & IT_KEY1)
2843                 drawpic_skin(pos, "nexball_carrying", '80 34 0', '1 1 1', 1, DRAWFLAG_NORMAL);
2844 }
2845
2846 // Race/CTS HUD modicon section
2847 float crecordtime_prev; // last remembered crecordtime
2848 float crecordtime_change_time; // time when crecordtime last changed
2849 float srecordtime_prev; // last remembered srecordtime
2850 float srecordtime_change_time; // time when srecordtime last changed
2851 void CSQC_race_hud(void)
2852 {
2853         entity me;
2854         me = playerslots[player_localentnum - 1];
2855         float t, score;
2856         float f; // yet another function has this
2857         score = me.(scores[ps_primary]);
2858
2859         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
2860                 return; // no records in the actual race
2861
2862         drawfont = hud_bigfont;
2863         vector pos;
2864         pos_x = 2;
2865         pos_y = vid_conheight - 48;
2866
2867         // clientside personal record
2868         string rr;
2869         if(gametype == GAME_CTS)
2870                 rr = CTS_RECORD;
2871         else
2872                 rr = RACE_RECORD;
2873         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
2874
2875         if(score && (score < t || !t)) {
2876                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
2877                 if(cvar("cl_autodemo_delete_keeprecords"))
2878                 {
2879                         f = cvar("cl_autodemo_delete");
2880                         f &~= 1;
2881                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
2882                 }
2883         }
2884
2885         if(t != crecordtime_prev) {
2886                 crecordtime_prev = t;
2887                 crecordtime_change_time = time;
2888         }
2889         f = time - crecordtime_change_time;
2890
2891         if (f > 1) {
2892                 drawstring(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2893                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2894         } else {
2895                 drawstring(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2896                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2897                 drawstring_expanding(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2898                 drawstring_expanding(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2899         }
2900
2901         // server record
2902         pos_y += 26;
2903         t = race_server_record;
2904         if(t != srecordtime_prev) {
2905                 srecordtime_prev = t;
2906                 srecordtime_change_time = time;
2907         }
2908         f = time - srecordtime_change_time;
2909
2910         if (f > 1) {
2911                 drawstring(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2912                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2913         } else {
2914                 drawstring(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2915                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2916                 drawstring_expanding(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2917                 drawstring_expanding(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2918         }
2919         drawfont = hud_font;
2920 }
2921
2922 /*
2923 ==================
2924 Main HUD system
2925 ==================
2926 */
2927
2928 void HUD_DrawPressedKeys(void)
2929 {
2930         vector pos, mySize;
2931         pos = HUD_Panel_GetPos(10);
2932         mySize = HUD_Panel_GetSize(10);
2933
2934         HUD_Panel_DrawBg(10, pos, mySize);
2935
2936         float pressedkeys;
2937
2938         pressedkeys = getstatf(STAT_PRESSED_KEYS);
2939         drawpic_skin(pos, "key_bg.tga",           mySize, '1 1 1', 0.1 * hud_alpha_fg, DRAWFLAG_NORMAL);
2940         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);
2941         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);
2942         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);
2943         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);
2944         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);
2945         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);
2946 }
2947
2948 void HUD_ShowSpeed(void)
2949 {
2950         vector numsize;
2951         float pos, conversion_factor;
2952         string speed, zspeed, unit;
2953
2954         switch(cvar("cl_showspeed_unit"))
2955         {
2956                 default:
2957                 case 0:
2958                         unit = "";
2959                         conversion_factor = 1.0;
2960                         break;
2961                 case 1:
2962                         unit = " qu/s";
2963                         conversion_factor = 1.0;
2964                         break;
2965                 case 2:
2966                         unit = " m/s";
2967                         conversion_factor = 0.0254;
2968                         break;
2969                 case 3:
2970                         unit = " km/h";
2971                         conversion_factor = 0.0254 * 3.6;
2972                         break;
2973                 case 4:
2974                         unit = " mph";
2975                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
2976                         break;
2977                 case 5:
2978                         unit = " knots";
2979                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
2980                         break;
2981         }
2982
2983         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
2984
2985         numsize_x = numsize_y = cvar("cl_showspeed_size");
2986         pos = (vid_conheight - numsize_y) * cvar("cl_showspeed_position");
2987
2988         drawfont = hud_bigfont;
2989         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2990
2991         if (cvar("cl_showspeed_z") == 1) {
2992                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
2993                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2994         }
2995
2996         drawfont = hud_font;
2997 }
2998
2999 vector acc_prevspeed;
3000 float acc_prevtime;
3001 float acc_avg;
3002
3003 void HUD_ShowAcceleration(void)
3004 {
3005         float acceleration, sz, scale, alpha, f;
3006         vector pos, top, rgb;
3007         top_x = vid_conwidth/2;
3008         top_y = 0;
3009
3010         f = time - acc_prevtime;
3011         if(cvar("cl_showacceleration_z"))
3012                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
3013         else
3014                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
3015         acc_prevspeed = pmove_vel;
3016         acc_prevtime = time;
3017
3018         f = bound(0, f * 10, 1);
3019         acc_avg = acc_avg * (1 - f) + acceleration * f;
3020         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
3021
3022         pos = top - sz/2 * eY + (cvar("cl_showacceleration_position") * vid_conheight) * eY;
3023
3024         sz = cvar("cl_showacceleration_size");
3025         scale = cvar("cl_showacceleration_scale");
3026         alpha = cvar("cl_showacceleration_alpha");
3027         if (cvar("cl_showacceleration_color_custom"))
3028                 rgb = stov(cvar_string("cl_showacceleration_color"));
3029         else {
3030                 rgb = '1 1 1';
3031                 if (acceleration < 0) {
3032                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
3033                 } else if (acceleration > 0) {
3034                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
3035                 }
3036         }
3037
3038         if (acceleration > 0)
3039                 drawpic_skin(pos, "statusbar", acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3040         else if (acceleration < 0)
3041                 drawpic_skin(pos + acceleration * scale * '40 0 0', "statusbar", -acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3042 }
3043
3044 void HUD_Reset (void)
3045 {
3046         // reset gametype specific icons
3047         if(gametype == GAME_KEYHUNT)
3048                 CSQC_kh_hudreset();
3049         else if(gametype == GAME_CTF)
3050                 CSQC_ctf_hudreset();
3051 }
3052
3053 void HUD_Main (void)
3054 {
3055         hud_alpha_bg = cvar_or("hud_bg_alpha", 0.8) * (1 - cvar("_menu_alpha"));
3056         hud_border_thickness = bound(0, cvar("hud_border_thickness"), 5);
3057         hud_accuracy_border_thickness = bound(0, cvar_or("hud_accuracy_border_thickness", 1), 5);
3058         hud_color_bg_team = cvar("hud_color_bg_team");
3059
3060         hud_fontsize = HUD_GetFontsize("hud_fontsize");
3061         hud_fontsize_spec = HUD_GetFontsize("hud_fontsize_spec");
3062
3063         hud_configure = cvar("_hud_configure");
3064
3065         // Drawing stuff
3066         if(cvar("hud_dock"))
3067                 drawpic_skin('0 0 0', "dock", eX * vid_conwidth + eY * vid_conheight, stov(cvar_string("hud_dock_color")), cvar("hud_dock_alpha"), DRAWFLAG_NORMAL);
3068
3069         if(HUD_Panel_CheckActive(0))
3070                 HUD_WeaponIcons();
3071         if(HUD_Panel_CheckActive(1))
3072                 HUD_Inventory();
3073         if(HUD_Panel_CheckActive(2))
3074                 HUD_Powerups();
3075         if(HUD_Panel_CheckActive(3))
3076                 HUD_HealthArmor();
3077         if(HUD_Panel_CheckActive(4))
3078                 HUD_Notify();
3079         if(HUD_Panel_CheckActive(5))
3080                 HUD_Timer();
3081         // TODO hud'ify
3082         if(HUD_Panel_CheckActive(6))
3083                 if(ons_showmap || cvar_string("cl_teamradar") != "0" && (cvar("cl_teamradar") == 2 || teamplay))
3084                         HUD_Radar();
3085         if(HUD_Panel_CheckActive(7))
3086                 HUD_Score();
3087         if(HUD_Panel_CheckActive(8))
3088                 if(gametype == GAME_RACE || gametype == GAME_CTS || hud_configure)
3089                         HUD_RaceTimer();
3090         if(HUD_Panel_CheckActive(9))
3091                 HUD_VoteWindow();
3092         // TODO hud'ify
3093         if(HUD_Panel_CheckActive(10))
3094                 if(spectatee_status > 0 || cvar("cl_showpressedkeys") >= 2 || hud_configure)
3095                         HUD_DrawPressedKeys();
3096
3097         // TODO hud_'ify these
3098         if (cvar("cl_showspeed"))
3099                 HUD_ShowSpeed();
3100         if (cvar("cl_showacceleration"))
3101                 HUD_ShowAcceleration();
3102
3103         // TODO... well make them work in a panel etc
3104         if(gametype == GAME_KEYHUNT)
3105                 CSQC_kh_hud();
3106         else if(gametype == GAME_CTF)
3107                 CSQC_ctf_hud();
3108         else if(gametype == GAME_NEXBALL)
3109                 CSQC_nb_hud();
3110         else if(gametype == GAME_CTS || gametype == GAME_RACE)
3111                 CSQC_race_hud();
3112         return;
3113 }