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