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