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