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