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