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