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