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