]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
04360a03bd10a087867f3e20f1126bb32b6f3b66
[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 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
79 void drawpic_skin(vector pos, string pic, vector sz, vector color, float alpha, float drawflag) {
80         drawpic(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, color, alpha, drawflag);
81 }
82
83 // return HUD background color
84 vector HUD_GetBgColor()
85 {
86         vector color;
87         if (teamplay)
88                 GetTeamRGB(myteam) * hud_color_bg_team;
89         else {
90                 // allow custom HUD colors in non-teamgames
91                 color_x = cvar("hud_color_bg_r");
92                 color_y = cvar("hud_color_bg_g");
93                 color_z = cvar("hud_color_bg_b");
94         }
95         return color;
96 }
97
98 // return accuracy text color
99 vector HUD_AccuracyColor(float accuracy)
100 {
101         vector rgb;
102         float yellow_accuracy = cvar("hud_weaponicons_accuracy_yellow"); // value at which this function returns yellow
103         if(accuracy >= 100) {
104                 rgb_x = 0;
105                 rgb_y = 1;
106         }
107         else if(accuracy > yellow_accuracy) {
108                 rgb_x = 1 - (accuracy-yellow_accuracy)/(100-yellow_accuracy); // red value between 1 -> 0
109                 rgb_y = 1;
110         }
111         else {
112                 rgb_x = 1;
113                 rgb_y = accuracy/yellow_accuracy; // green value between 0 -> 1
114         }
115         rgb_z = 0;
116         return rgb;
117 }
118
119 // draw number in the XSCALE font
120 void HUD_DrawXNum (vector pos, float num, float digits, float showsign, float lettersize, vector rgb, float highlighted, float stroke, float alpha, float dflags)
121 {
122         float l, i;
123         string str, tmp, l_length;
124         float minus, plus;
125         vector vsize, num_color;
126
127         vsize_x = vsize_y = lettersize;
128         vsize_z = 0;
129
130         // showsign 1: always prefix with minus sign (useful in race distribution display)
131         // showsign 2: always prefix with plus sign (useful in race distribution display)
132         // showsign 3: prefix with minus sign if negative, plus sign if positive (useful in score distribution display)
133
134         if((showsign == 2 && num >= 0) || (num > 0 && showsign == 3))
135         {
136                 plus = true;
137                 pos_x -= lettersize;
138         } else
139                 plus = false;
140
141         if(num < 0 || (num < 0 && showsign == 3) || (showsign == 1 && num <= 0))
142         {
143                 minus = true;
144                 num = -num;
145                 pos_x -= lettersize;
146         } else
147                 minus = false;
148
149         if(digits < 0)
150         {
151                 tmp = ftos(num);
152                 digits = -digits;
153                 str = strcat(substring("0000000000", 0, digits - strlen(tmp)), tmp);
154         } else
155                 str = ftos(num);
156
157         l = strlen(str);
158         l_length = ftos(l);
159
160         if(l > digits)
161         {
162                 str = substring(str, l-digits, 999);
163                 l = strlen(str);
164         } else if(l < digits)
165                 pos_x += (digits-l) * lettersize;
166
167         if (highlighted == 1) {
168                 vector hl_size;
169                 hl_size_x = vsize_x * l + vsize_x * 0.2;
170                 hl_size_y = vsize_y * 1.1;
171                 hl_size_z = 0;
172                 if(minus)
173                         hl_size_x = hl_size_x + vsize_x;
174
175                 vector hl_pos;
176                 hl_pos_x = pos_x - lettersize/10;
177                 hl_pos_y = pos_y - lettersize/20;
178                 hl_pos_z = 0;
179
180                 drawpic_skin(hl_pos, strcat("highlight_", l_length), hl_size, '1 1 1', alpha, dflags);
181         }
182
183         if (stroke == 1)
184                 num_color = '1 1 1';
185         else
186                 num_color = rgb;
187
188         if(minus)
189         {
190                 if (stroke == 1)
191                         drawpic_skin(pos, "num_minus_stroke", vsize, rgb, alpha, dflags);
192                 drawpic_skin(pos, "num_minus", vsize, num_color, alpha, dflags);
193                 pos_x += lettersize;
194         } else if(plus)
195         {
196                 if (stroke == 1)
197                         drawpic_skin(pos, "num_plus_stroke", vsize, rgb, alpha, dflags);
198                 drawpic_skin(pos, "num_plus", vsize, num_color, alpha, dflags);
199                 pos_x += lettersize;
200         }
201
202         for(i = 0; i < l; ++i)
203         {
204                 tmp = substring(str, i, 1);
205                 if (stroke == 1)
206                         drawpic_skin(pos, strcat("num_", tmp, "_stroke"), vsize, rgb, alpha, dflags);
207                 drawpic_skin(pos, strcat("num_", tmp), vsize, num_color, alpha, dflags);
208                 pos_x += lettersize;
209         }
210 }
211
212 // color the number differently based on how big it is (used in the health/armor panel)
213 void HUD_DrawXNum_Colored (vector pos, float x, float digits, float lettersize, float alpha)
214 {
215         vector color;
216         if(x > 200) {
217                 color_x = 0;
218                 color_y = 1;
219                 color_z = 0;
220         }
221         else if(x > 150) {
222                 color_x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
223                 color_y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
224                 color_z = 0;
225         }
226         else if(x > 100) {
227                 color_x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
228                 color_y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
229                 color_z = 1 - (x-100)*0.02; // blue value between 1 -> 0
230         }
231         else if(x > 50) {
232                 color_x = 1;
233                 color_y = 1;
234                 color_z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
235         }
236         else if(x > 20) {
237                 color_x = 1;
238                 color_y = (x-20)*90/27/100; // green value between 0 -> 1
239                 color_z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
240         }
241         else {
242                 color_x = 1;
243                 color_y = 0;
244                 color_z = 0;
245         }
246         HUD_DrawXNum(pos, x, digits, 0, lettersize, color, 0, 0, alpha, DRAWFLAG_NORMAL);
247 }
248
249 float stringwidth_colors(string s, vector theSize)
250 {
251         return stringwidth(s, TRUE, theSize);
252 }
253
254 float stringwidth_nocolors(string s, vector theSize)
255 {
256         return stringwidth(s, FALSE, theSize);
257 }
258
259 #define CENTERPRINT_MAX_LINES 30
260 string centerprint_messages[CENTERPRINT_MAX_LINES];
261 float centerprint_width[CENTERPRINT_MAX_LINES];
262 vector centerprint_start;
263 float centerprint_expire;
264 float centerprint_num;
265 float centerprint_offset_hint;
266 vector centerprint_fontsize;
267
268 void centerprint(string strMessage)
269 {
270         float i, j, n, hcount;
271         string s;
272
273         centerprint_fontsize = HUD_GetFontsize("scr_centersize");
274
275         centerprint_expire = min(centerprint_expire, time); // if any of the returns happens, this message will fade out
276
277         if(cvar("scr_centertime") <= 0)
278                 return;
279
280         if(strMessage == "")
281                 return;
282
283         // strip trailing newlines
284         j = strlen(strMessage) - 1;
285         while(substring(strMessage, j, 1) == "\n" && j >= 0)
286                 j = j - 1;
287         strMessage = substring(strMessage, 0, j + 1);
288
289         if(strMessage == "")
290                 return;
291
292         // strip leading newlines and remember them, they are a hint that the message should be lower on the screen
293         j = 0;
294         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
295                 j = j + 1;
296         strMessage = substring(strMessage, j, strlen(strMessage) - j);
297         centerprint_offset_hint = j;
298
299         if(strMessage == "")
300                 return;
301
302         // if we get here, we have a message. Initialize its height.
303         centerprint_num = 0;
304
305         n = tokenizebyseparator(strMessage, "\n");
306         i = hcount = 0;
307         for(j = 0; j < n; ++j)
308         {
309                 getWrappedLine_remaining = argv(j);
310                 while(getWrappedLine_remaining)
311                 {
312                         s = getWrappedLine(vid_conwidth * 0.75, centerprint_fontsize, stringwidth_colors);
313                         if(centerprint_messages[i])
314                                 strunzone(centerprint_messages[i]);
315                         centerprint_messages[i] = strzone(s);
316                         centerprint_width[i] = stringwidth(s, TRUE, centerprint_fontsize);
317                         ++i;
318
319                         // half height for empty lines looks better
320                         if(s == "")
321                                 hcount += 0.5;
322                         else
323                                 hcount += 1;
324
325                         if(i >= CENTERPRINT_MAX_LINES)
326                                 break;
327                 }
328         }
329
330         float h, havail;
331         h = centerprint_fontsize_y*hcount;
332
333         havail = vid_conheight;
334         if(cvar("con_chatpos") < 0)
335                 havail -= (-cvar("con_chatpos") + cvar("con_chat")) * cvar("con_chatsize"); // avoid overlapping chat
336         if(havail > vid_conheight - 70)
337                 havail = vid_conheight - 70; // avoid overlapping HUD
338
339         centerprint_start_x = 0;
340
341 #if 0
342         float forbiddenmin, forbiddenmax, allowedmin, allowedmax, preferred;
343
344         // here, the centerprint would cover the crosshair. REALLY BAD.
345         forbiddenmin = vid_conheight * 0.5 - h - 16;
346         forbiddenmax = vid_conheight * 0.5 + 16;
347
348         allowedmin = scoreboard_bottom;
349         allowedmax = havail - h;
350         preferred = (havail - h)/2;
351
352
353         // possible orderings (total: 4! / 4 = 6)
354         //  allowedmin allowedmax forbiddenmin forbiddenmax
355         //  forbiddenmin forbiddenmax allowedmin allowedmax
356         if(allowedmax < forbiddenmin || allowedmin > forbiddenmax)
357         {
358                 // forbidden doesn't matter in this case
359                 centerprint_start_y = bound(allowedmin, preferred, allowedmax);
360         }
361         //  allowedmin forbiddenmin allowedmax forbiddenmax
362         else if(allowedmin < forbiddenmin && allowedmax < forbiddenmax)
363         {
364                 centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
365         }
366         //  allowedmin forbiddenmin forbiddenmax allowedmax
367         else if(allowedmin < forbiddenmin)
368         {
369                 // make sure the forbidden zone is not covered
370                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
371                         centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
372                 else
373                         centerprint_start_y = bound(forbiddenmax, preferred, allowedmin);
374         }
375         //  forbiddenmin allowedmin allowedmax forbiddenmax
376         else if(allowedmax < forbiddenmax)
377         {
378                 // it's better to leave the allowed zone (overlap with scoreboard) than
379                 // to cover the forbidden zone (crosshair)
380                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
381                         centerprint_start_y = forbiddenmax;
382                 else
383                         centerprint_start_y = forbiddenmin;
384         }
385         //  forbiddenmin allowedmin forbiddenmax allowedmax
386         else
387         {
388                 centerprint_start_y = bound(forbiddenmax, preferred, allowedmax);
389         }
390 #else
391         centerprint_start_y =
392                 min(
393                         max(
394                                 max(scoreboard_bottom, vid_conheight * 0.5 + 16),
395                                 (havail - h)/2
396                         ),
397                         havail - h
398                 );
399 #endif
400
401         centerprint_num = i;
402         centerprint_expire = time + cvar("scr_centertime");
403 }
404
405 void HUD_DrawCenterPrint (void)
406 {
407         float i;
408         vector pos;
409         string ts;
410         float a;
411
412         //if(time > centerprint_expire)
413         //      return;
414
415         //a = bound(0, 1 - 2 * (time - centerprint_expire), 1);
416         a = bound(0, 1 - 4 * (time - centerprint_expire), 1);
417         //sz = 1.2 / (a + 0.2);
418
419         if(a <= 0)
420                 return;
421
422         pos = centerprint_start;
423         for (i=0; i<centerprint_num; i = i + 1)
424         {
425                 pos_x = (vid_conwidth - centerprint_width[i]) * 0.5;
426                 ts = centerprint_messages[i];
427                 if (ts != "")
428                 {
429                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
430                         drawcolorcodedstring(pos, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
431                         //  - '0 0.5 0' * (sz - 1) * centerprint_fontsize_x - '0.5 0 0' * (sz - 1) * centerprint_width[i] * centerprint_fontsize_y, centerprint_fontsize * sz
432                         pos_y = pos_y + centerprint_fontsize_y;
433                 }
434                 else
435                         // half height for empty lines looks better
436                         pos_y = pos_y + centerprint_fontsize_y * 0.5;
437         }
438 }
439
440 void drawstringright(vector position, string text, vector scale, vector rgb, float alpha, float flag)
441 {
442         position_x -= 2 / 3 * strlen(text) * scale_x;
443         drawstring(position, text, scale, rgb, alpha, flag);
444 }
445
446 void drawstringcenter(vector position, string text, vector scale, vector rgb, float alpha, float flag)
447 {
448         position_x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * scale_x);
449         drawstring(position, text, scale, rgb, alpha, flag);
450 }
451
452 // return the string of the given race place
453 string race_PlaceName(float pos) {
454         if(pos == 1)
455                 return "1st";
456         else if(pos == 2)
457                 return "2nd";
458         else if(pos == 3)
459                 return "3rd";
460         else
461                 return strcat(ftos(pos), "th");
462 }
463
464 // return the string of the onscreen race timer
465 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
466 {
467         string col;
468         string timestr;
469         string cpname;
470         string lapstr;
471         lapstr = "";
472
473         if(histime == 0) // goal hit
474         {
475                 if(mytime > 0)
476                 {
477                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
478                         col = "^1";
479                 }
480                 else if(mytime == 0)
481                 {
482                         timestr = "+0.0";
483                         col = "^3";
484                 }
485                 else
486                 {
487                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
488                         col = "^2";
489                 }
490
491                 if(lapdelta > 0)
492                 {
493                         lapstr = strcat(" (-", ftos(lapdelta), "L)");
494                         col = "^2";
495                 }
496                 else if(lapdelta < 0)
497                 {
498                         lapstr = strcat(" (+", ftos(-lapdelta), "L)");
499                         col = "^1";
500                 }
501         }
502         else if(histime > 0) // anticipation
503         {
504                 if(mytime >= histime)
505                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
506                 else
507                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
508                 col = "^3";
509         }
510         else
511                 col = "^7";
512
513         if(cp == 254)
514                 cpname = "Start line";
515         else if(cp == 255)
516                 cpname = "Finish line";
517         else if(cp)
518                 cpname = strcat("Intermediate ", ftos(cp));
519         else
520                 cpname = "Finish line";
521
522         if(histime < 0)
523                 return strcat(col, cpname);
524         else if(hisname == "")
525                 return strcat(col, cpname, " (", timestr, ")");
526         else
527                 return strcat(col, cpname, " (", timestr, " ", strcat(hisname, col, lapstr), ")");
528 }
529
530 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
531 float race_CheckName(string net_name) {
532         float i;
533         for (i=RANKINGS_CNT-1;i>=0;--i)
534                 if(grecordholder[i] == net_name)
535                         return i+1;
536         return 0;
537 }
538
539 /*
540 ==================
541 HUD panels
542 ==================
543 */
544
545 string HUD_Panel_GetName(float id)
546 {
547         switch(id) {
548                 case 0: return "weaponicons"; break;
549                 case 1: return "inventory"; break;
550                 case 2: return "powerups"; break;
551                 case 3: return "healtharmor"; break;
552                 case 4: return "notify"; break;
553                 case 5: return "timer"; break;
554                 case 6: return "radar"; break;
555                 case 7: return "score"; break;
556                 case 8: return "racetimer"; break;
557                 case 9: return "vote"; break;
558                 case 10: return "modicons"; break;
559                 case 11: return "pressedkeys"; break;
560                 default: return "";
561         }
562 }
563
564 // Save the config
565 void HUD_Panel_ExportCfg(string cfgname)
566 {
567         float fh;
568         fh = fopen(strcat("hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg"), FILE_WRITE);
569         if(fh >= 0)
570         {
571                 fputs(fh, strcat("seta hud_skin \"", cvar_string("hud_skin"), "\"", "\n"));
572                 fputs(fh, strcat("seta hud_bg \"", cvar_string("hud_bg"), "\"", "\n"));
573                 fputs(fh, strcat("seta hud_bg_color \"", cvar_string("hud_bg_color"), "\"", "\n"));
574                 fputs(fh, strcat("seta hud_bg_alpha ", ftos(cvar("hud_bg_alpha")), "\n"));
575                 fputs(fh, strcat("seta hud_bg_border ", ftos(cvar("hud_bg_border")), "\n"));
576                 fputs(fh, strcat("seta hud_fg_alpha ", ftos(cvar("hud_fg_alpha")), "\n"));
577                 fputs(fh, "\n");
578
579                 fputs(fh, strcat("seta hud_dock \"", cvar_string("hud_dock"), "\"", "\n"));
580                 fputs(fh, strcat("seta hud_dock_color \"", cvar_string("hud_dock_color"), "\"", "\n"));
581                 fputs(fh, strcat("seta hud_dock_alpha ", ftos(cvar("hud_dock_alpha")), "\n"));
582                 fputs(fh, "\n");
583
584                 fputs(fh, strcat("seta hud_progressbar_alpha ", ftos(cvar("hud_progressbar_alpha")), "\n"));
585                 fputs(fh, strcat("seta hud_progressbar_strength_color \"", cvar_string("hud_progressbar_strength_color"), "\"", "\n"));
586                 fputs(fh, strcat("seta hud_progressbar_shield_color \"", cvar_string("hud_progressbar_shield_color"), "\"", "\n"));
587                 fputs(fh, strcat("seta hud_progressbar_health_color \"", cvar_string("hud_progressbar_health_color"), "\"", "\n"));
588                 fputs(fh, strcat("seta hud_progressbar_armor_color \"", cvar_string("hud_progressbar_armor_color"), "\"", "\n"));
589                 fputs(fh, strcat("seta hud_progressbar_fuel_color \"", cvar_string("hud_progressbar_fuel_color"), "\"", "\n"));
590                 fputs(fh, "\n");
591
592                 // common cvars for all panels
593                 float i;
594                 for (i = 0; i < panel_cnt; ++i)
595                 {
596                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), " ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i)))), "\n"));
597                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_pos \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_pos")), "\"", "\n"));
598                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_size \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_size")), "\"", "\n"));
599                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg")), "\"", "\n"));
600                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_color \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_color")), "\"", "\n"));
601                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_bg_alpha"))), "\n"));
602                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_border ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_bg_border"))), "\n"));
603                         switch(i) {
604                                 case 0:
605                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_accuracy_height ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_accuracy_height"))), "\n"));
606                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_accuracy_yellow ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_accuracy_yellow"))), "\n"));
607                                         break;
608                                 case 1:
609                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_onlycurrent ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_onlycurrent"))), "\n"));
610                                         break;
611                                 case 2:
612                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
613                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_mirror ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
614                                         break;
615                                 case 3:
616                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
617                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_mirror ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
618                                         break;
619                                 case 9:
620                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha"))), "\n"));
621                                         break;
622                         }
623                         fputs(fh, "\n");
624                 }
625
626                 print("^2Successfully exported to hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg! (Note: It's saved in data/data/)\n");
627         }
628         fclose(fh);
629 }
630
631 vector HUD_Panel_CheckLimitSize(float id, vector mySize)
632 {
633         switch(id) {
634                 case 0: 
635                         mySize_x = max(mySize_y * (1/10), mySize_x); // at least 1/10 * height
636                         mySize_y = max(mySize_x * (1/26), mySize_y); // at least 1/26 * width
637                         break;
638                 case 1: 
639                         mySize_x = max(mySize_y * 0.7, mySize_x); // at least 0.7 * height
640                         break;
641                 //case 2: 
642                 //      mySize_x = max(mySize_y * 1.5, mySize_x); // at least 1.5 * height
643                 //      break;
644                 //case 3: 
645                 //      mySize_x = max(mySize_y * 2, mySize_x); // at least 2 * height
646                 //      break;
647                 case 5: 
648                         mySize_y = (1/4.1) * mySize_x; // 1/4.1 * width, trial and error...
649                         break;
650                 case 7: 
651                         mySize_y = (1/4) * mySize_x; // 1/4 * width
652                         break;
653                 case 8: 
654                         mySize_y = (1/4) * mySize_x; // 1/4 * width
655                         break;
656                 case 9: 
657                         mySize_y = (1/4) * mySize_x; // 1/4 * width
658                         break;
659                 case 10: 
660                         mySize_y = 0.5898 * mySize_x; // 0.5898 * width, reason: bg has weird dimensions...
661                         break;
662         }
663         return mySize;
664 }
665
666 // return active status of panel
667 float HUD_Panel_CheckActive(float id)
668 {
669         if (cvar_or(strcat("hud_", HUD_Panel_GetName(id)), 1))
670                 return 1;
671         return 0;
672 }
673
674 // return size of given panel
675 vector HUD_Panel_GetSize(float id)
676 {
677         vector mySize;
678         mySize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
679
680         mySize = eX * mySize_x * vid_conwidth + eY * mySize_y * vid_conheight;
681
682         return mySize;
683 }
684
685 // return pos of given panel
686 vector HUD_Panel_GetPos(float id)
687 {
688         vector pos;
689         pos = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_pos")));
690
691         pos = eX * pos_x * vid_conwidth + eY * pos_y * vid_conheight;
692
693         if (pos_x < 0)
694                 pos_x = vid_conwidth + pos_x;
695         if (pos_y < 0)
696                 pos_y = vid_conheight + pos_y;
697         return pos;
698 }
699
700 float HUD_Panel_GetBorder(float id)
701 {
702         float border;
703         border = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_border"));
704         if(!border)
705                 border = cvar("hud_bg_border");
706         return border;
707 }
708
709 // draw the background/borders
710 void HUD_Panel_DrawBg(float id, vector pos, vector mySize)
711 {
712         if(!hud_configure && cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg")) == "0")
713                 return;
714
715         string bg;
716         bg = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg"));
717         if(bg == "")
718                 bg = cvar_string("hud_bg");
719
720         if(bg != "0")
721         {
722                 float border;
723                 border = max(0.0000001, HUD_Panel_GetBorder(id)); // draw_BorderPicture does not like border = 0
724
725                 vector color;
726                 if(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color")) != "")
727                         color = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color")));
728                 else
729                         color = stov(cvar_string("hud_bg_color"));
730
731                 float alpha;
732                 alpha = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_alpha"));
733                 if(!alpha)
734                         alpha = cvar("hud_bg_alpha");
735                 if(hud_configure)
736                         alpha = max(cvar("hud_configure_bg_minalpha"), alpha);
737
738                 draw_BorderPicture(pos - '1 1 0' * border, strcat("gfx/hud/", cvar_string("hud_skin"), "/", bg), mySize + '1 1 0' * 2 * border, color, alpha, '1 1 0' * (border/BORDER_MULTIPLIER));
739         }
740 }
741
742 vector HUD_Panel_GetProgressBarColor(string item)
743 {
744         return stov(cvar_string(strcat("hud_progressbar_", item, "_color")));
745 }
746
747 float resizeCorner; // 1 = topleft, 2 = topright, 3 = bottomleft, 4 = bottomright
748 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
749 vector HUD_Panel_CheckResize(float id, vector myPos, vector mySize)
750 {
751         float i;
752
753         vector myTarget;
754         myTarget = mySize;
755
756         vector targPos;
757         vector targSize;
758         vector myCenter;
759         vector targCenter;
760
761         for (i = 0; i < panel_cnt; ++i) {
762                 if(i == id || !HUD_Panel_CheckActive(i))
763                         continue;
764
765                 targPos = HUD_Panel_GetPos(i);
766                 targSize = HUD_Panel_GetSize(i);
767
768                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
769                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(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 && resizeCorner != 1) // top left (of 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 - myPos_x;
794                         else // push it upwards
795                                 myTarget_y = targPos_y - myPos_y;
796                 }
797                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y && resizeCorner != 2) // 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 - myPos_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 - myPos_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 // TODO: checkResize
824 float HUD_Panel_SetSize(float id, vector mySize)
825 {
826         float didntresize;
827
828         vector oldSize;
829         oldSize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
830
831         vector myPos;
832         myPos = HUD_Panel_GetPos(id);
833
834         // check for collisions
835         if(cvar("hud_configure_checkcollisions"))
836                 mySize = HUD_Panel_CheckResize(id, HUD_Panel_GetPos(id), mySize);
837
838         mySize_x = bound(0.025 * vid_conwidth, mySize_x, vid_conwidth - myPos_x);
839         mySize_y = bound(0.025 * vid_conheight, mySize_y, vid_conheight - myPos_y);
840
841         // cap against panel's own limits
842         mySize = HUD_Panel_CheckLimitSize(id, mySize);
843
844         //mySize_x = bound(0.025 * vid_conwidth, mySize_x, vid_conwidth);
845         //mySize_y = bound(0.025 * vid_conheight, mySize_y, vid_conheight);
846
847         // TODO: is this needed?
848         // this is to check if (and how) SetPos should be called
849         if(mySize_x == oldSize_x && mySize_y == oldSize_y)
850                 didntresize = 3; // didnt resize either
851         else if(mySize_x == oldSize_x && mySize_y != oldSize_y)
852                 didntresize = 2; // resized Y
853         else if(mySize_y == oldSize_y && mySize_x != oldSize_x)
854                 didntresize = 1; // resized X
855
856         string s;
857         s = strcat(ftos(mySize_x/vid_conwidth), " ", ftos(mySize_y/vid_conheight));
858         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_size"), s);
859         return didntresize;
860 }
861
862 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
863 vector HUD_Panel_CheckMove(float id, vector myPos, vector mySize)
864 {
865         float i;
866
867         vector myTarget;
868         myTarget = myPos;
869
870         vector targPos;
871         vector targSize;
872         vector myCenter;
873         vector targCenter;
874
875         for (i = 0; i < panel_cnt; ++i) {
876                 if(i == id || !HUD_Panel_CheckActive(i))
877                         continue;
878
879                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
880                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(id);
881
882                 if(myPos_y + mySize_y < targPos_y)
883                         continue;
884                 if(myPos_y > targPos_y + targSize_y)
885                         continue;
886
887                 if(myPos_x + mySize_x < targPos_x)
888                         continue;
889                 if(myPos_x > targPos_x + targSize_x)
890                         continue;
891
892                 // OK, there IS a collision.
893
894                 myCenter_x = myPos_x + 0.5 * mySize_x;
895                 myCenter_y = myPos_y + 0.5 * mySize_y;
896
897                 targCenter_x = targPos_x + 0.5 * targSize_x;
898                 targCenter_y = targPos_y + 0.5 * targSize_y;
899
900                 float k, y;
901                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y) // top left (of the target panel)
902                 {
903                         if(myPos_x + mySize_x - targPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
904                                 myTarget_x = targPos_x - mySize_x;
905                         else // push it upwards
906                                 myTarget_y = targPos_y - mySize_y;
907                 }
908                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y) // top right
909                 {
910                         if(targPos_x + targSize_x - myPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
911                                 myTarget_x = targPos_x + targSize_x;
912                         else // push it upwards
913                                 myTarget_y = targPos_y - mySize_y;
914                 }
915                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
916                 {
917                         if(myPos_x + mySize_x - targPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
918                                 myTarget_x = targPos_x - mySize_x;
919                         else // push it downwards
920                                 myTarget_y = targPos_y + targSize_y;
921                 }
922                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
923                 {
924                         if(targPos_x + targSize_x - myPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
925                                 myTarget_x = targPos_x + targSize_x;
926                         else // push it downwards
927                                 myTarget_y = targPos_y + targSize_y;
928                 }
929         }
930
931         return myTarget;
932 }
933
934 void HUD_Panel_SetPos(float id, vector pos, float didntresize)
935 {
936         vector oldPos;
937         oldPos = HUD_Panel_GetPos(id);
938         if(didntresize == 2)
939                 pos_x = oldPos_x;
940         else if(didntresize == 1)
941                 pos_y = oldPos_y;
942         else if(didntresize == 3)
943                 return;
944
945         vector mySize;
946         mySize = HUD_Panel_GetSize(id);
947
948         if(cvar("hud_configure_checkcollisions"))
949                 pos = HUD_Panel_CheckMove(id, pos, mySize);
950
951         pos_x = bound(0, pos_x, vid_conwidth - mySize_x);
952         pos_y = bound(0, pos_y, vid_conheight - mySize_y);
953
954         if (pos_x + 0.5 * mySize_x > 0.5 * vid_conwidth)
955                 pos_x = pos_x - vid_conwidth;
956         if (pos_y + 0.5 * mySize_y > 0.5 * vid_conheight)
957                 pos_y = pos_y - vid_conheight;
958
959         string s;
960         s = strcat(ftos(pos_x/vid_conwidth), " ", ftos(pos_y/vid_conheight));
961
962         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_pos"), s);
963 }
964
965 float mouseClicked;
966 float prevMouseClicked; // previous state
967 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
968 {
969         prevMouseClicked = mouseClicked;
970         if(nPrimary == K_MOUSE1)
971         {
972                 if(bInputType == 0) { // key pressed
973                         mouseClicked = 1;
974                         return true;
975                 }
976                 if(bInputType == 1) {// key released
977                         mouseClicked = 0;
978                         return true;
979                 }
980         }
981         return false;
982 }
983
984 vector mousepos, mouseprevpos;
985 vector panel_click_distance; // mouse cursor distance from the top left corner of the panel (saved only upon a click)
986 vector panel_click_pos; // panel pos (saved only upon a click)
987 vector panel_click_size; // panel size (saved only upon a click)
988 float highlightedPanel;
989 float highlightedAction; // 0 = nothing, 1 = move, 2 = resize
990 void HUD_Panel_Mouse()
991 {
992         if(mouseClicked == 0) {
993                 highlightedPanel = -1;
994                 highlightedAction = 0;
995         }
996
997         mousepos = mousepos + getmousepos();
998
999         mousepos_x = bound(0, mousepos_x, vid_conwidth);
1000         mousepos_y = bound(0, mousepos_y, vid_conheight);
1001
1002         drawpic(mousepos, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1003
1004         if(mouseClicked)
1005         {
1006                 float i, border;
1007                 vector panelPos;
1008                 vector panelSize;
1009
1010                 for(i = 0; i <= panel_cnt; ++i)
1011                 {
1012                         panelPos = HUD_Panel_GetPos(i);
1013                         panelSize = HUD_Panel_GetSize(i);
1014                         border = HUD_Panel_GetBorder(i);
1015                         if(prevMouseClicked == 0) {
1016                                 // move
1017                                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1018                                 {
1019                                         highlightedPanel = i;
1020                                         highlightedAction = 1;
1021                                 }
1022                                 // resize from topleft border
1023                                 else if(mousepos_x >= panelPos_x - border && mousepos_y >= panelPos_y - border && mousepos_x <= panelPos_x + 0.5 * panelSize_x && mousepos_y <= panelPos_y + 0.5 * panelSize_y)
1024                                 {
1025                                         highlightedPanel = i;
1026                                         highlightedAction = 2;
1027                                         resizeCorner = 1;
1028                                 }
1029                                 // resize from topright border
1030                                 else if(mousepos_x >= panelPos_x + 0.5 * panelSize_x && mousepos_y >= panelPos_y - border && mousepos_x <= panelPos_x + panelSize_x + border && mousepos_y <= panelPos_y + 0.5 * panelSize_y)
1031                                 {
1032                                         highlightedPanel = i;
1033                                         highlightedAction = 2;
1034                                         resizeCorner = 2;
1035                                 }
1036                                 // resize from bottomleft border
1037                                 else if(mousepos_x >= panelPos_x - border && mousepos_y >= panelPos_y + 0.5 * panelSize_y && mousepos_x <= panelPos_x + 0.5 * panelSize_x && mousepos_y <= panelPos_y + panelSize_y + border)
1038                                 {
1039                                         highlightedPanel = i;
1040                                         highlightedAction = 2;
1041                                         resizeCorner = 3;
1042                                 }
1043                                 // resize from bottomright border
1044                                 else if(mousepos_x >= panelPos_x + 0.5 * panelSize_x && mousepos_y >= panelPos_y + 0.5 * panelSize_y && mousepos_x <= panelPos_x + panelSize_x + border && mousepos_y <= panelPos_y + panelSize_y + border)
1045                                 {
1046                                         highlightedPanel = i;
1047                                         highlightedAction = 2;
1048                                         resizeCorner = 4;
1049                                 }
1050                         }
1051
1052                         if(highlightedPanel == i)
1053                         {
1054                                 if(prevMouseClicked == 0)
1055                                 {
1056                                         panel_click_distance = mousepos - panelPos;
1057                                         panel_click_pos = panelPos;
1058                                         panel_click_size = panelSize;
1059                                 }
1060
1061                                 if(highlightedAction == 1)
1062                                         HUD_Panel_SetPos(i, mousepos - panel_click_distance, 0);
1063                                 else if(highlightedAction == 2)
1064                                 {
1065                                         float didntresize; // panel too big/too small, dont resize (also has to affect moving)
1066                                         if(resizeCorner == 1) {
1067                                                 didntresize = HUD_Panel_SetSize(i, panelSize + mouseprevpos - mousepos);
1068                                                 HUD_Panel_SetPos(i, mousepos - panel_click_distance, didntresize);
1069                                         }
1070                                         if(resizeCorner == 2) {
1071                                                 didntresize = HUD_Panel_SetSize(i, eY * panel_click_size_y + eX * (mousepos_x - panelPos_x - (panel_click_distance_x - panel_click_size_x))
1072                                                                 + eY * (panel_click_distance_y + (panel_click_pos_y - mousepos_y)));
1073                                                 HUD_Panel_SetPos(i, eX * panelPos_x + eY * (mousepos_y - panel_click_distance_y), didntresize);
1074                                         }
1075                                         if(resizeCorner == 3) {
1076                                                 didntresize = HUD_Panel_SetSize(i, panelSize + eX * (mouseprevpos_x - mousepos_x) + eY * (mousepos_y - mouseprevpos_y));
1077                                                 HUD_Panel_SetPos(i, eX * (mousepos_x - panel_click_distance_x) + eY * panelPos_y, didntresize);
1078                                         }
1079                                         if(resizeCorner == 4) {
1080                                                 HUD_Panel_SetSize(i, mousepos - panelPos - (panel_click_distance - panel_click_size));
1081                                         }
1082                                 }
1083                         }
1084                 }
1085         }
1086         mouseprevpos = mousepos;
1087         prevMouseClicked = mouseClicked;
1088 }
1089
1090 /*void HUD_DrawDockEdge(float id, vector p1, vector p2, float target)
1091 {
1092         vector pos;
1093         vector size;
1094
1095         pos =
1096 */ // maybe one day, since this will be quite complicated
1097
1098 // Weapon icons (#0)
1099 //
1100 float weaponspace[10];
1101 void HUD_WeaponIcons_Clear()
1102 {
1103         float idx;
1104         for(idx = 0; idx < 10; ++idx)
1105                 weaponspace[idx] = 0;
1106 }
1107
1108 entity weaponorder[WEP_MAXCOUNT];
1109
1110 void weaponorder_swap(float i, float j, entity pass)
1111 {
1112         entity h;
1113         h = weaponorder[i];
1114         weaponorder[i] = weaponorder[j];
1115         weaponorder[j] = h;
1116 }
1117
1118 float weaponorder_cmp(float i, float j, entity pass)
1119 {
1120         float d, ii, ij;
1121         d = mod(weaponorder[i].impulse + 9, 10) - mod(weaponorder[j].impulse + 9, 10);
1122         if(d)
1123                 return d;
1124         d = weaponorder[i].weapon - weaponorder[j].weapon;
1125         return d;
1126 }
1127
1128 void HUD_WeaponIcons()
1129 {
1130         float id = 0;
1131         float alpha, height, accuracybar_height, stat_weapons; // "constants"
1132         vector pos, mySize, mysize, mypos, accuracy_color;
1133         float i, weapid, fade, weapon_stats, weapon_hit, weapon_damage, weapon_cnt; // variables
1134
1135         pos = HUD_Panel_GetPos(id);
1136         mySize = HUD_Panel_GetSize(id);
1137         accuracybar_height = cvar_or("hud_weaponicons_accuracy_height", 3);
1138
1139         stat_weapons = getstati(STAT_WEAPONS);
1140         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1141         {
1142                 self = get_weaponinfo(i);
1143                 if(self.weapons && (self.impulse >= 0) && (stat_weapons & self.weapons) || hud_configure)
1144                 {
1145                         weaponorder[weapon_cnt] = self;
1146                         ++weapon_cnt;
1147                 }
1148         }
1149         heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
1150
1151         HUD_Panel_DrawBg(id, pos, mySize);
1152         float padding;
1153         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1154         if(padding)
1155         {
1156                 pos += '1 1 0' * padding;
1157                 mySize -= '2 2 0' * padding;
1158         }
1159
1160         // hits
1161         weapon_stats = getstati(STAT_DAMAGE_HITS);
1162         weapon_number = weapon_stats & 63;
1163         weapon_hits[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1164         // fired
1165         weapon_stats = getstati(STAT_DAMAGE_FIRED);
1166         weapon_number = weapon_stats & 63;
1167         weapon_fired[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1168
1169         if(cvar_or("hud_weaponicons_fade", 1))
1170         {
1171                 fade = 3.2 - 2 * (time - weapontime);
1172                 fade = bound(0.7, fade, 1);
1173         }
1174         else
1175                 fade = 1;
1176
1177         HUD_WeaponIcons_Clear();
1178
1179         float rows, columns;
1180         rows = mySize_y/mySize_x;
1181         rows = bound(1, floor((sqrt(4 * (2/1) * rows * WEP_COUNT + rows * rows) + rows + 0.5) / 2), WEP_COUNT);
1182         //                               ^^^ weapon icon aspect goes here
1183
1184         columns = ceil(WEP_COUNT/rows);
1185         float row, column;
1186         for(i = 0; i < weapon_cnt; ++i)
1187         {
1188                 self = weaponorder[i];
1189                 if((self.weapons && (self.impulse >= 0) && (stat_weapons & self.weapons)) || hud_configure)
1190                 {
1191                         weapid = self.impulse;
1192
1193                         alpha = (self.weapon == activeweapon) ? 1 : 0.6;
1194
1195                         weapon_hit = weapon_hits[self.weapon-WEP_FIRST];
1196                         weapon_damage = weapon_fired[self.weapon-WEP_FIRST];
1197
1198                         if(self.weapon == activeweapon)
1199                                 drawpic_skin(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), "weapon_current_bg", eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows), '1 1 1', fade * hud_alpha_fg, DRAWFLAG_NORMAL);
1200                         drawpic(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), strcat("gfx/weapons/weapon", self.netname), eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows), '1 1 1', fade * hud_alpha_fg, DRAWFLAG_NORMAL);
1201
1202                         if(cvar_or("hud_weaponicons_number", 1))
1203                                 drawstring(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), ftos(weapid), '1 1 0' * 0.5 * mySize_y*(1/rows), '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1204
1205                         // draw the weapon accuracy on the HUD
1206                         if(hud_accuracy_hud && !(gametype == GAME_RACE || gametype == GAME_CTS))
1207                         {
1208                                 if(weapon_damage)
1209                                         weapon_stats = floor(100 * weapon_hit / weapon_damage);
1210
1211                                 accuracy_color = HUD_AccuracyColor(weapon_stats);
1212                                 if(weapon_damage)
1213                                         drawpic_skin(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows) - '2 0 0' + eY * (mySize_y/rows - accuracybar_height), "accuracy_bar.tga", eX * mySize_x*(1/columns) + eY * accuracybar_height, accuracy_color, hud_alpha_fg, DRAWFLAG_NORMAL);
1214                         }
1215                 }
1216
1217                 ++row;
1218                 if(row >= rows)
1219                 {
1220                         row = 0;
1221                         ++column;
1222                 }
1223         }
1224
1225 }
1226
1227 // Inventory (#1)
1228 //
1229 float GetAmmoStat(float i)
1230 {
1231         switch(i)
1232         {
1233                 case 0: return STAT_SHELLS;
1234                 case 1: return STAT_NAILS;
1235                 case 2: return STAT_ROCKETS;
1236                 case 3: return STAT_CELLS;
1237                 case 4: return STAT_FUEL;
1238                 default: return -1;
1239         }
1240 }
1241
1242 float GetAmmoItemCode(float i)
1243 {
1244         switch(i)
1245         {
1246                 case 0: return IT_SHELLS;
1247                 case 1: return IT_NAILS;
1248                 case 2: return IT_ROCKETS;
1249                 case 3: return IT_CELLS;
1250                 case 4: return IT_FUEL;
1251                 default: return -1;
1252         }
1253 }
1254
1255 string GetAmmoPicture(float i)
1256 {
1257         switch(i)
1258         {
1259                 case 0: return "ammo_shells";
1260                 case 1: return "ammo_bullets";
1261                 case 2: return "ammo_rocket";
1262                 case 3: return "ammo_cells";
1263                 case 4: return "ammo_fuel";
1264                 default: return "";
1265         }
1266 }
1267
1268 void HUD_Inventory()
1269 {
1270         float id = 1;
1271         float i;
1272         float stat_items;
1273
1274         vector pos, mySize, mysize, mypos;
1275         pos = HUD_Panel_GetPos(id);
1276         mySize = HUD_Panel_GetSize(id);
1277
1278         HUD_Panel_DrawBg(id, pos, mySize);
1279         float padding;
1280         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1281         if(padding)
1282         {
1283                 pos += '1 1 0' * padding;
1284                 mySize -= '2 2 0' * padding;
1285         }
1286
1287         // ammo
1288         stat_items = getstati(STAT_ITEMS);
1289         for (i = 0; i < 4; ++i) {
1290                 float a;
1291                 a = getstati(GetAmmoStat(i)); // how much ammo do we have of type i?
1292                 if(hud_configure)
1293                         a = 100;
1294
1295                 if(cvar("hud_inventory_onlycurrent")) {
1296                         if (stat_items & GetAmmoItemCode(i)) {
1297                                 drawpic_skin(pos, "ammo_current_bg", mySize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1298                                 drawpic_skin(pos + eY * 0.05 * mySize_y, GetAmmoPicture(i), '1 1 0' * 0.8 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1299                                 if(a < 10)
1300                                         HUD_DrawXNum(pos + eX * 0.8 * mySize_y + eY * 0.25 * mySize_y, a, strlen(ftos(a)), 0, 0.5 * mySize_y, '0.7 0 0', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1301                                 else
1302                                         HUD_DrawXNum(pos + eX * 0.8 * mySize_y + eY * 0.25 * mySize_y, a, strlen(ftos(a)), 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1303                         }
1304                 } else {
1305                         if (a > 0) {
1306                                 if(mySize_x/mySize_y >= 10) { // arrange horizontally
1307                                         switch (i) {
1308                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1309                                                 case 1: mypos_x = pos_x + 0.25 * mySize_x;      mypos_y = pos_y;                        break; // bullets
1310                                                 case 2: mypos_x = pos_x + 0.5  * mySize_x;      mypos_y = pos_y;                        break; // rockets
1311                                                 case 3: mypos_x = pos_x + 0.75 * mySize_x;      mypos_y = pos_y;                        break; // cells
1312                                         }
1313                                         mysize_x = 0.25 * mySize_x;
1314                                         mysize_y = mySize_y;
1315                                 } else if(mySize_x/mySize_y >= 2.5) { // arrange in a 2x2 grid
1316                                         switch (i) {
1317                                                 case 0: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y + 0.5 * mySize_y;       break; // shells
1318                                                 case 1: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y;                        break; // bullets
1319                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5 * mySize_y;       break; // rockets
1320                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // cells
1321                                         }
1322                                         mysize_x = 0.5 * mySize_x;
1323                                         mysize_y = 0.5 * mySize_y;
1324                                 } else { // arrange vertically
1325                                         switch (i) {
1326                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1327                                                 case 1: mypos_x = pos_x;                        mypos_y = pos_y + 0.25 * mySize_y;      break; // bullets
1328                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5  * mySize_y;      break; // rockets
1329                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y + 0.75 * mySize_y;      break; // cells
1330                                         }
1331                                         mysize_x = mySize_x;
1332                                         mysize_y = 0.25 * mySize_y;
1333                                 }
1334
1335                                 if (stat_items & GetAmmoItemCode(i))
1336                                         drawpic_skin(mypos, "ammo_current_bg", mysize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1337                                 drawpic_skin(mypos + eY * 0.05 * mysize_y, GetAmmoPicture(i), '1 1 0' * 0.8 * mysize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1338                                 if (a < 10) {
1339                                         if(stat_items & GetAmmoItemCode(i))
1340                                                 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);
1341                                         else
1342                                                 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);
1343                                 } else {
1344                                         if(stat_items & GetAmmoItemCode(i))
1345                                                 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);
1346                                         else
1347                                                 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);
1348                                 }
1349                         }
1350                 }
1351         }
1352 }
1353
1354
1355 // Powerups (#2)
1356 //
1357 float shield_maxtime;
1358 float strength_maxtime;
1359 void HUD_Powerups() {
1360         float id = 2;
1361         float stat_items;
1362         stat_items = getstati(STAT_ITEMS);
1363
1364         if(!hud_configure)
1365         {
1366                 if not(stat_items & IT_STRENGTH)
1367                         if not(stat_items & IT_INVINCIBLE)
1368                                 return;
1369
1370                 if (getstati(STAT_HEALTH) <= 0)
1371                         return;
1372         }
1373
1374         vector pos, mySize;
1375         pos = HUD_Panel_GetPos(id);
1376         mySize = HUD_Panel_GetSize(id);
1377
1378         HUD_Panel_DrawBg(id, pos, mySize);
1379         float padding;
1380         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1381         if(padding)
1382         {
1383                 pos += '1 1 0' * padding;
1384                 mySize -= '2 2 0' * padding;
1385         }
1386
1387         float strength_time, shield_time;
1388
1389         strength_time = bound(0, ceil(getstatf(STAT_STRENGTH_FINISHED) - time), 99);
1390         shield_time = bound(0, ceil(getstatf(STAT_INVINCIBLE_FINISHED) - time), 99);
1391
1392         if(hud_configure)
1393         {
1394                 strength_time = 15;
1395                 shield_time = 27;
1396         }
1397
1398         float len;
1399
1400         vector barpos, barsize;
1401         vector picpos;
1402         vector numpos;
1403
1404         string leftname, rightname;
1405         float leftcnt, rightcnt;
1406         float leftalpha, rightalpha;
1407         if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) {
1408                 leftname = "strength";
1409                 leftcnt = strength_time;
1410
1411                 rightname = "shield";
1412                 rightcnt = shield_time;
1413         } else {
1414                 leftname = "shield";
1415                 leftcnt = shield_time;
1416
1417                 rightname = "strength";
1418                 rightcnt = strength_time;
1419         }
1420         leftalpha = bound(0, leftcnt, 1);
1421         rightalpha = bound(0, rightcnt, 1);
1422
1423         // TODO: tile statusbars?
1424         if (mySize_x/mySize_y > 4)
1425         {
1426                 if(leftcnt)
1427                 {
1428                         len = strlen(ftos(leftcnt));
1429
1430                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1431                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/30);
1432                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1433                                 picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
1434                                 numpos = picpos - eX * 2 * 0.5 * mySize_y + eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1435                         } else {
1436                                 barpos = pos;
1437                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1438                                 picpos = pos;
1439                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1440                         }
1441
1442                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1443                         drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1444                         HUD_DrawXNum(numpos, leftcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1445                 }
1446
1447                 if(rightcnt)
1448                 {
1449                         len = strlen(ftos(rightcnt));
1450
1451                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1452                                 barpos = pos + eX * 0.5 * mySize_x;
1453                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1454                                 picpos = pos + eX * 0.5 * mySize_x;
1455                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1456                         } else {
1457                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/30);
1458                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1459                                 picpos = pos + eX * mySize_x - eX * mySize_y;
1460                                 numpos = picpos - eX * mySize_y + eY * 0.25 * mySize_y;
1461                         }
1462
1463                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1464                         drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1465                         HUD_DrawXNum(numpos, rightcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1466                 }
1467         }
1468         else if (mySize_x/mySize_y > 1.5)
1469         {
1470                 if(leftcnt)
1471                 {
1472                         len = strlen(ftos(leftcnt));
1473
1474                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1475                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/30);
1476                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1477                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
1478                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1479                         } else {
1480                                 barpos = pos;
1481                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1482                                 picpos = pos;
1483                                 numpos = picpos + eX * 0.5 * mySize_y;
1484                         }
1485
1486                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1487                         drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1488                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1489                 }
1490
1491                 if(rightcnt)
1492                 {
1493                         len = strlen(ftos(rightcnt));
1494
1495                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1496                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1497                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1498                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
1499                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1500                         } else {
1501                                 barpos = pos + eY * 0.5 * mySize_y;
1502                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1503                                 picpos = pos + eY * 0.5 * mySize_y;
1504                                 numpos = picpos + eX * 0.5 * mySize_y;
1505                         }
1506
1507                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1508                         drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1509                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1510                 }
1511         }
1512         else
1513         {
1514                 if(leftcnt)
1515                 {
1516                         len = strlen(ftos(leftcnt));
1517
1518                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1519                                 barpos = pos;
1520                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1521                                 picpos = pos + eX * 0.05 * mySize_x;
1522                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
1523                         } else {
1524                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/30);
1525                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1526                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x;
1527                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x;
1528                         }
1529
1530                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1531                         drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1532                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1533                 }
1534
1535                 if(rightcnt)
1536                 {
1537                         len = strlen(ftos(rightcnt));
1538
1539                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1540                                 barpos = pos + eX * 0.5 * mySize_x;
1541                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1542                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1543                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1544                         } else {
1545                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/30) + eX * 0.5 * mySize_x;
1546                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1547                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x + eX * 0.5 * mySize_x;
1548                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
1549                         }
1550
1551                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1552                         drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1553                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1554                 }
1555         }
1556
1557         /* TODO: add expand from old code!
1558         //strength
1559         if (strength_time) {
1560                 dt = strength_time - time;
1561                 if(dt > 0)
1562                 {
1563                         if(dt < 5)
1564                         {
1565                                 drawpic_expanding_two(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1566                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1567                         }
1568                         else
1569                         {
1570                                 drawpic_skin(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
1571                         }
1572                         HUD_DrawXNum(pos - '40 -2 0', ceil(dt), 2, 0, countdown_fontsize, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1573                 }
1574                 else if(dt > -1)
1575                 {
1576                         drawpic_expanding(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1577                                 bound(0, -dt / 0.5, 1));
1578                 }
1579         }
1580
1581         //invincibility
1582         if (invincibility_time) {
1583                 dt = invincibility_time - time;
1584                 if(dt > 0)
1585                 {
1586                         if(dt < 5)
1587                         {
1588                                 drawpic_expanding_two(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1589                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1590                         }
1591                         else
1592                         {
1593                                 drawpic_skin(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
1594                         }
1595                         HUD_DrawXNum(pos - '40 -24 0', ceil(dt), 2, 0, countdown_fontsize, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1596                 }
1597                 else if(dt > -1)
1598                 {
1599                         drawpic_expanding(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1600                                 bound(0, -dt / 0.5, 1));
1601                 }
1602         }
1603         */
1604 }
1605
1606 // Health/armor (#3)
1607 //
1608 void HUD_HealthArmor(void)
1609 {
1610         float id = 3;
1611         vector pos, mySize;
1612         pos = HUD_Panel_GetPos(id);
1613         mySize = HUD_Panel_GetSize(id);
1614
1615         HUD_Panel_DrawBg(id, pos, mySize);
1616         float padding;
1617         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1618         if(padding)
1619         {
1620                 pos += '1 1 0' * padding;
1621                 mySize -= '2 2 0' * padding;
1622         }
1623
1624         float armor, health, x;
1625         armor = getstati(STAT_ARMOR);
1626         health = getstati(STAT_HEALTH);
1627
1628         float fuel;
1629         fuel = getstati(GetAmmoStat(4)); // how much fuel do we have?
1630
1631         if(hud_configure)
1632         {
1633                 armor = 150;
1634                 health = 100;
1635                 fuel = 70;
1636         }
1637
1638         if(health <= 0)
1639                 return;
1640
1641         float len;
1642
1643         // TODO!
1644         /*
1645         if(hud_hudselector == 2) // combined health and armor display
1646         {
1647                 vector v;
1648                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
1649
1650                 vector num_pos;
1651                 num_pos = - '96 28 0';
1652
1653                 x = floor(v_x + 1);
1654
1655                 if(v_z) // fully armored
1656                 {
1657                         // here, armorideal > armor
1658                         drawpic_skin(num_pos + '78 -4.5 0', "health", '32 32 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1659                         drawpic_skin(num_pos + '108 -4.5 0', "armor", '20 20 0', '1 1 1', hud_alpha_fg * armor / v_y, DRAWFLAG_NORMAL);
1660                 }
1661                 else
1662                 {
1663                         drawpic_skin(num_pos + '108 -4.5 0', "health", '20 20 0', '1 1 1', hud_alpha_fg * v_y / armor, DRAWFLAG_NORMAL);
1664                         drawpic_skin(num_pos + '78 -4.5 0', "armor", '32 32 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1665                 }
1666                 HUD_DrawXNum_Colored(num_pos, x, 3, 24, hud_alpha_fg); // draw the combined health and armor
1667         }
1668
1669         else
1670         {
1671         */
1672         vector barpos, barsize;
1673         vector picpos;
1674         vector numpos;
1675
1676         string leftname, rightname;
1677         float leftcnt, rightcnt;
1678         float leftactive, rightactive;
1679         float leftalpha, rightalpha;
1680         // TODO!
1681         if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) { // old style layout with armor left/top of health
1682                 leftname = "armor";
1683                 leftcnt = armor;
1684                 if(leftcnt)
1685                         leftactive = 1;
1686                 leftalpha = min((armor+10)/55, 1);
1687
1688                 rightname = "health";
1689                 rightcnt = health;
1690                 rightactive = 1;
1691                 rightalpha = 1;
1692         } else {
1693                 leftname = "health";
1694                 leftcnt = health;
1695                 leftactive = 1;
1696                 leftalpha = 1;
1697
1698                 rightname = "armor";
1699                 rightcnt = armor;
1700                 if(rightcnt)
1701                         rightactive = 1;
1702                 rightalpha = min((armor+10)/55, 1);
1703         }
1704
1705         // TODO: tile statusbars?
1706         if (mySize_x/mySize_y > 5)
1707         {
1708                 if(leftactive)
1709                 {
1710                         len = strlen(ftos(leftcnt));
1711
1712                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1713                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/200);
1714                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1715                                 picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
1716                                 numpos = picpos - eX * 3 * 0.5 * mySize_y + eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1717                         } else {
1718                                 barpos = pos;
1719                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1720                                 picpos = pos;
1721                                 numpos = picpos + eX * mySize_y + eY * 0.25 * mySize_y;
1722                         }
1723
1724                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1725                         drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1726                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1727                 }
1728
1729                 if(rightactive)
1730                 {
1731                         len = strlen(ftos(rightcnt));
1732
1733                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1734                                 barpos = pos + eX * 0.5 * mySize_x;
1735                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1736                                 picpos = pos + eX * 0.5 * mySize_x;
1737                                 numpos = picpos + eX * mySize_y - eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1738                         } else {
1739                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/200);
1740                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1741                                 picpos = pos + eX * mySize_x - eX * mySize_y;
1742                                 numpos = picpos - eX * 1.5 * mySize_y + eY * 0.25 * mySize_y;
1743                         }
1744
1745                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1746                         drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1747                         HUD_DrawXNum_Colored(numpos, rightcnt, 3, 0.5 * mySize_y, hud_alpha_fg);
1748                 }
1749
1750                 if(fuel)
1751                         drawpic_skin(pos, "statusbar", eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1752         }
1753         else if (mySize_x/mySize_y > 2)
1754         {
1755                 if(leftactive)
1756                 {
1757                         len = strlen(ftos(leftcnt));
1758
1759                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1760                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/200);
1761                                 barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
1762                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
1763                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1764                         } else {
1765                                 barpos = pos;
1766                                 barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
1767                                 picpos = pos;
1768                                 numpos = picpos + eX * 0.5 * mySize_y;
1769                         }
1770
1771                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1772                         drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1773                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1774                 }
1775
1776                 if(rightactive)
1777                 {
1778                         len = strlen(ftos(rightcnt));
1779
1780                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1781                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1782                                 barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1783                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
1784                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1785                         } else {
1786                                 barpos = pos + eY * 0.5 * mySize_y;
1787                                 barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1788                                 picpos = pos + eY * 0.5 * mySize_y;
1789                                 numpos = picpos + eX * 0.5 * mySize_y;
1790                         }
1791
1792                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1793                         drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1794                         HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1795                 }
1796
1797                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1798                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
1799                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
1800                 } else {
1801                         barpos = pos;
1802                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
1803                 }
1804                 if(fuel)
1805                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1806         }
1807         else
1808         {
1809                 if(leftactive)
1810                 {
1811                         len = strlen(ftos(leftcnt));
1812
1813                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1814                                 barpos = pos;
1815                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
1816                                 picpos = pos + eX * 0.05 * mySize_x;
1817                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
1818                         } else {
1819                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/200);
1820                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
1821                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x;
1822                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x;
1823                         }
1824
1825                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1826                         drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1827                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.166 * mySize_x, hud_alpha_fg);
1828                 }
1829
1830                 if(rightactive)
1831                 {
1832                         len = strlen(ftos(rightcnt));
1833
1834                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1835                                 barpos = pos + eX * 0.5 * mySize_x;
1836                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
1837                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1838                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1839                         } else {
1840                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/200) + eX * 0.5 * mySize_x;
1841                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
1842                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x + eX * 0.5 * mySize_x;
1843                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x + eX * 0.5 * mySize_x;
1844                         }
1845
1846                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1847                         drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1848                         HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.166 * mySize_x, hud_alpha_fg);
1849                 }
1850
1851                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1852                         barpos = pos;
1853                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
1854                 } else {
1855                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, fuel/100);
1856                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
1857                 }
1858                 if(fuel)
1859                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1860         }
1861 }
1862
1863 // ___TODO___ !!!
1864 // Notification area (#4)
1865 //
1866 void HUD_Notify (void)
1867 {
1868         float id = 4;
1869         vector pos, mySize;
1870         pos = HUD_Panel_GetPos(id);
1871         mySize = HUD_Panel_GetSize(id);
1872
1873         HUD_Panel_DrawBg(id, pos, mySize);
1874         float padding;
1875         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1876         if(padding)
1877         {
1878                 pos += '1 1 0' * padding;
1879                 mySize -= '2 2 0' * padding;
1880         }
1881
1882         string s;
1883         entity tm;
1884         if(spectatee_status && !intermission)
1885         {
1886                 drawfont = hud_bigfont;
1887                 if(spectatee_status == -1)
1888                         s = "^1Observing";
1889                 else
1890                         s = GetPlayerName(spectatee_status - 1);
1891                 // spectated player name between HUD and chat area, aligned to the left
1892                 pos_x = 0;
1893                 pos_y = - 50 - hud_fontsize_spec_y;
1894                 s = textShortenToWidth(s, vid_conwidth/2.5, hud_fontsize_spec, stringwidth_colors);
1895                 drawcolorcodedstring(pos, s, hud_fontsize_spec, hud_alpha_fg, DRAWFLAG_NORMAL);
1896                 drawfont = hud_font;
1897
1898                 // spectator text in the upper right corner
1899                 if(spectatee_status == -1)
1900                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 to spectate");
1901                 else
1902                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 for another player");
1903
1904                 if(spectatee_status == -1)
1905                         s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
1906                 else
1907                         s = strcat("^1Press ^3", getcommandkey("secondary fire", "+attack2"), "^1 to observe");
1908
1909                 s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
1910
1911                 if(gametype == GAME_ARENA)
1912                         s = "^1Wait for your turn to join";
1913                 else if(gametype == GAME_LMS)
1914                 {
1915                         entity sk;
1916                         sk = playerslots[player_localentnum - 1];
1917                         if(sk.(scores[ps_primary]) >= 666)
1918                                 s = "^1Match has already begun";
1919                         else if(sk.(scores[ps_primary]) > 0)
1920                                 s = "^1You have no more lives left";
1921                         else
1922                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1923                 }
1924                 else
1925                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1926
1927                 //show restart countdown:
1928                 if (time < getstatf(STAT_GAMESTARTTIME)) {
1929                         float countdown;
1930                         //we need to ceil, otherwise the countdown would be off by .5 when using round()
1931                         countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
1932                         s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
1933                 }
1934         }
1935         if(warmup_stage && !intermission)
1936         {
1937                 s = "^2Currently in ^1warmup^2 stage!";
1938         }
1939
1940         // move more important stuff more to the middle so its more visible
1941
1942         string blinkcolor;
1943         if(mod(time, 1) >= 0.5)
1944                 blinkcolor = "^1";
1945         else
1946                 blinkcolor = "^3";
1947
1948         if(ready_waiting && !intermission && !spectatee_status)
1949         {
1950                 if(ready_waiting_for_me)
1951                 {
1952                         if(warmup_stage)
1953                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
1954                         else
1955                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
1956                 }
1957                 else
1958                 {
1959                         if(warmup_stage)
1960                                 s = strcat("^2Waiting for others to ready up to end warmup...");
1961                         else
1962                                 s = strcat("^2Waiting for others to ready up...");
1963                 }
1964         }
1965         else if(warmup_stage && !intermission && !spectatee_status)
1966         {
1967                 s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
1968         }
1969
1970         if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
1971         {
1972                 float ts_min, ts_max;
1973                 tm = teams.sort_next;
1974                 if (tm)
1975                 {
1976                         for(; tm.sort_next; tm = tm.sort_next)
1977                         {
1978                                 if(!tm.team_size || tm.team == COLOR_SPECTATOR)
1979                                         continue;
1980                                 if(!ts_min) ts_min = tm.team_size;
1981                                 else ts_min = min(ts_min, tm.team_size);
1982                                 if(!ts_max) ts_max = tm.team_size;
1983                                 else ts_max = max(ts_max, tm.team_size);
1984                         }
1985                         if ((ts_max - ts_min) > 1)
1986                         {
1987                                 s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
1988                                 tm = GetTeam(myteam, false);
1989                                 if (tm)
1990                                 if (tm.team != COLOR_SPECTATOR)
1991                                 if (tm.team_size == ts_max)
1992                                         s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
1993
1994                         }
1995                 }
1996         }
1997 }
1998
1999 // Timer (#5)
2000 //
2001 void HUD_Timer()
2002 {
2003         float id = 5;
2004         vector pos, mySize;
2005         pos = HUD_Panel_GetPos(id);
2006         mySize = HUD_Panel_GetSize(id);
2007
2008         HUD_Panel_DrawBg(id, pos, mySize);
2009         float padding;
2010         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2011         if(padding)
2012         {
2013                 pos += '1 1 0' * padding;
2014                 mySize -= '2 2 0' * padding;
2015         }
2016
2017         float timelimit, elapsedTime, minutes, seconds, timeleft, minutesLeft, secondsLeft;
2018
2019         timelimit = getstatf(STAT_TIMELIMIT);
2020
2021         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
2022         timeleft = ceil(timeleft);
2023         minutesLeft = floor(timeleft / 60);
2024         secondsLeft = timeleft - minutesLeft*60;
2025
2026         vector timer_color;
2027         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
2028                 timer_color = '1 1 1'; //white
2029         else if(minutesLeft >= 1)
2030                 timer_color = '1 1 0'; //yellow
2031         else
2032                 timer_color = '1 0 0'; //red
2033
2034         if (cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
2035                 if (time < getstatf(STAT_GAMESTARTTIME)) {
2036                         //while restart is still active, show 00:00
2037                         minutes = seconds = 0;
2038                 } else {
2039                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
2040                         minutes = floor(elapsedTime / 60);
2041                         seconds = elapsedTime - minutes*60;
2042                 }
2043         } else {
2044                 minutes = minutesLeft;
2045                 seconds = secondsLeft;
2046         }
2047
2048         if(minutes > 999)
2049                 seconds = 99;
2050         minutes = min(minutes, 999);
2051         if(minutesLeft >= 1 || cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
2052                 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);
2053                 drawpic_skin(pos + eX * mySize_x - eX * 2.57 * mySize_y, "num_colon", '1 1 0' * mySize_y, timer_color, hud_alpha_fg, DRAWFLAG_NORMAL);
2054         }
2055         HUD_DrawXNum(pos + eX * mySize_x - eX * 2 * mySize_y, seconds, -2, 0, mySize_y, timer_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2056 }
2057
2058 // Radar (#6)
2059 //
2060 void HUD_Radar(void)
2061 {
2062         float id = 6;
2063         vector pos, mySize;
2064         pos = HUD_Panel_GetPos(id);
2065         mySize = HUD_Panel_GetSize(id);
2066
2067         HUD_Panel_DrawBg(id, pos, mySize);
2068         float padding;
2069         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2070         if(padding)
2071         {
2072                 pos += '1 1 0' * padding;
2073                 mySize -= '2 2 0' * padding;
2074         }
2075
2076         local float color1, color2; // color already declared as a global in hud.qc
2077         local vector rgb;
2078         local entity tm;
2079         float scale2d, normalsize, bigsize;
2080         float f;
2081
2082         teamradar_origin2d = pos + 0.5 * mySize; // TODO: stupid compat, should be removed
2083         teamradar_size2d = mySize;
2084
2085         if(minimapname == "" && !ons_showmap)
2086                 return;
2087
2088         teamradar_loadcvars();
2089
2090         switch(cl_teamradar_zoommode)
2091         {
2092                 default:
2093                 case 0:
2094                         f = current_zoomfraction;
2095                         break;
2096                 case 1:
2097                         f = 1 - current_zoomfraction;
2098                         break;
2099                 case 2:
2100                         f = 0;
2101                         break;
2102                 case 3:
2103                         f = 1;
2104                         break;
2105         }
2106
2107         switch(cl_teamradar_rotation)
2108         {
2109                 case 0:
2110                         teamradar_angle = view_angles_y - 90;
2111                         break;
2112                 default:
2113                         teamradar_angle = 90 * cl_teamradar_rotation;
2114                         break;
2115         }
2116
2117         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
2118         teamradar_size2d = mySize;
2119
2120         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
2121
2122         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
2123         if(cl_teamradar_rotation == 0)
2124         {
2125                 // max-min distance must fit the radar in any rotation
2126                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
2127         }
2128         else
2129         {
2130                 vector c0, c1, c2, c3, span;
2131                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
2132                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
2133                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
2134                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
2135                 span = '0 0 0';
2136                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
2137                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
2138
2139                 // max-min distance must fit the radar in x=x, y=y
2140                 bigsize = min(
2141                         teamradar_size2d_x * scale2d / (1.05 * span_x),
2142                         teamradar_size2d_y * scale2d / (1.05 * span_y)
2143                 );
2144         }
2145
2146         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / cl_teamradar_scale;
2147         if(bigsize > normalsize)
2148                 normalsize = bigsize;
2149
2150         teamradar_size =
2151                   f * bigsize
2152                 + (1 - f) * normalsize;
2153         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
2154                   f * (mi_min + mi_max) * 0.5
2155                 + (1 - f) * view_origin);
2156
2157         color1 = GetPlayerColor(player_localentnum-1);
2158         rgb = GetTeamRGB(color1);
2159
2160         drawsetcliparea(
2161                 pos_x,
2162                 pos_y,
2163                 mySize_x,
2164                 mySize_y
2165         );
2166
2167         draw_teamradar_background(cl_teamradar_background_alpha, cl_teamradar_foreground_alpha);
2168
2169         if(ons_showmap)
2170         {
2171                 drawresetcliparea();
2172
2173                 vector frame_origin, frame_size;
2174                 frame_origin = frame_size = '0 0 0';
2175
2176                 frame_origin_x = pos_x - teamradar_size2d_x * 0.55859375; // matches the picture
2177                 frame_origin_y = pos_y - teamradar_size2d_y * 0.55859375; // matches the picture
2178                 frame_size_x = pos_x * 1.1171875; // matches the picture
2179                 frame_size_y = pos_y * 1.1171875; // matches the picture
2180                 drawpic_skin(frame_origin, "gfx/ons-frame.tga", frame_size, '1 1 1', hud_alpha_fg, 0);
2181                 drawpic_skin(frame_origin, "gfx/ons-frame-team.tga", frame_size, rgb, hud_alpha_fg, 0);
2182
2183                 drawsetcliparea(
2184                         pos_x - teamradar_size2d_x * 0.5,
2185                         pos_y - teamradar_size2d_y * 0.5,
2186                         teamradar_size2d_x,
2187                         teamradar_size2d_y
2188                 );
2189         }
2190
2191         for(tm = world; (tm = find(tm, classname, "radarlink")); )
2192                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
2193         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
2194                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, hud_alpha_fg);
2195         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
2196         {
2197                 color2 = GetPlayerColor(tm.sv_entnum);
2198                 //if(color == COLOR_SPECTATOR || color == color2)
2199                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
2200         }
2201         draw_teamradar_player(view_origin, view_angles, '1 1 1');
2202
2203         drawresetcliparea();
2204 };
2205
2206 // Score (#7)
2207 //
2208 void HUD_Score()
2209 {
2210         float id = 7;
2211         vector pos, mySize;
2212         pos = HUD_Panel_GetPos(id);
2213         mySize = HUD_Panel_GetSize(id);
2214
2215         HUD_Panel_DrawBg(id, pos, mySize);
2216         float padding;
2217         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2218         if(padding)
2219         {
2220                 pos += '1 1 0' * padding;
2221                 mySize -= '2 2 0' * padding;
2222         }
2223
2224         float score, distribution, leader;
2225         float score_len, distr_len;
2226         vector score_pos, secondary_score_pos, distribution_color;
2227         entity tm, pl, me;
2228         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
2229
2230         if (!teamplay) { // non-teamgames
2231                 // me vector := [team/connected frags id]
2232                 pl = players.sort_next;
2233                 if(pl == me)
2234                         pl = pl.sort_next;
2235
2236                 if(hud_configure)
2237                         distribution = 42;
2238                 else if(pl)
2239                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2240                 else
2241                         distribution = 0;
2242
2243                 score = me.(scores[ps_primary]);
2244                 if(hud_configure)
2245                         score = 123;
2246
2247                 if(distribution >= 5) {
2248                         distribution_color = eY;
2249                         leader = 1;
2250                 } else if(distribution >= 0) {
2251                         distribution_color = '1 1 1';
2252                         leader = 1;
2253                 } else if(distribution >= -5)
2254                         distribution_color = '1 1 0';
2255                 else
2256                         distribution_color = eX;
2257
2258                 score_len = strlen(ftos(score));
2259                 distr_len = strlen(ftos(distribution));
2260
2261                 HUD_DrawXNum(pos + eX * mySize_x - eX * 3 * 0.33 * mySize_y, distribution, 3, 3, 0.33 * mySize_y, distribution_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2262                 if (leader)
2263                         drawpic_skin(pos + eX * mySize_x - eX * score_len * mySize_y - eX * 3 * 0.33 * mySize_y, strcat("highlight_", ftos(score_len)), eX * score_len * mySize_y + eY * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2264                 HUD_DrawXNum(pos + eX * mySize_x - eX * 3 * mySize_y - eX * 3 * 0.33 * mySize_y, score, 3, 0, mySize_y, distribution_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2265         } else { // teamgames
2266                 float max_fragcount;
2267                 max_fragcount = -99;
2268
2269                 float teamnum;
2270                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2271                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !hud_configure)) // no players? don't display
2272                                 continue;
2273                         score = tm.(teamscores[ts_primary]);
2274                         if(hud_configure)
2275                                 score = 123;
2276                         leader = 0;
2277                         
2278                         score_len = strlen(ftos(score));
2279
2280                         if (score > max_fragcount)
2281                                 max_fragcount = score;
2282
2283                         if(tm.team == myteam) {
2284                                 if (max_fragcount == score)
2285                                         leader = 1;
2286                                 if (leader)
2287                                         drawpic_skin(pos + eX * mySize_x - eX * score_len * mySize_y - eX * 3 * 0.33 * mySize_y, strcat("highlight_", ftos(score_len)), eX * score_len * mySize_y + eY * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2288                                 HUD_DrawXNum(pos + eX * mySize_x - eX * 3 * mySize_y - eX * 3 * 0.33 * mySize_y, score, 3, 0, mySize_y, GetTeamRGB(tm.team) * 0.8, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2289                         } else {
2290                                 if (max_fragcount == score)
2291                                         leader = 1;
2292                                 if (leader)
2293                                         drawpic_skin(pos + eX * mySize_x - eX * 0.33 * score_len * mySize_y + eY * 0.33 * mySize_y * teamnum, strcat("highlight_", ftos(score_len)), eX * 0.33 * score_len * mySize_y + eY * 0.33 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2294                                 HUD_DrawXNum(pos + eX * mySize_x - eX * 3 * 0.33 * mySize_y + eY * 0.33 * mySize_y * teamnum, score, 3, 0, 0.33 * mySize_y, GetTeamRGB(tm.team) * 0.8, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2295                                 teamnum += 1;
2296                         }
2297                 }
2298         }
2299 }
2300
2301 // Race timer (#8)
2302 //
2303 void HUD_RaceTimer (void) {
2304         float id = 8;
2305         vector pos, mySize;
2306         pos = HUD_Panel_GetPos(id);
2307         mySize = HUD_Panel_GetSize(id);
2308
2309         HUD_Panel_DrawBg(id, pos, mySize);
2310         float padding;
2311         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2312         if(padding)
2313         {
2314                 pos += '1 1 0' * padding;
2315                 mySize -= '2 2 0' * padding;
2316         }
2317
2318         drawfont = hud_bigfont;
2319         float a, t;
2320         string s, forcetime;
2321
2322         if(hud_configure)
2323         {
2324                 s = "0:13:37";
2325                 drawstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, FALSE, '0.75 0.75 0' * mySize_y), s, '0.75 0.75 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2326                 s = "^1Intermediate 1 (+15.42)";
2327                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.25 * mySize_y) + eY * 0.75 * mySize_y, s, '1 1 0' * 0.25 * mySize_y, hud_alpha_fg, DRAWFLAG_NORMAL);
2328         }
2329         else if(race_checkpointtime)
2330         {
2331                 a = bound(0, 2 - (time - race_checkpointtime), 1);
2332                 s = "";
2333                 forcetime = "";
2334                 if(a > 0) // just hit a checkpoint?
2335                 {
2336                         if(race_checkpoint != 254)
2337                         {
2338                                 if(race_time && race_previousbesttime)
2339                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
2340                                 else
2341                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
2342                                 if(race_time)
2343                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
2344                         }
2345                 }
2346                 else
2347                 {
2348                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
2349                         {
2350                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
2351                                 if(a > 0) // next one?
2352                                 {
2353                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
2354                                 }
2355                         }
2356                 }
2357
2358                 if(s != "" && a > 0)
2359                 {
2360                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2361                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2362                         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);
2363                 }
2364
2365                 // ___ TODO!!!___
2366                 if(race_penaltytime)
2367                 {
2368                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
2369                         if(a > 0)
2370                         {
2371                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
2372                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2373                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2374                                 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);
2375                         }
2376                 }
2377
2378                 if(forcetime != "")
2379                 {
2380                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
2381                         //drawstring_expanding(m - '16 0 0' * stringwidth(forcetime, FALSE), forcetime, '32 32 0', '1 1 1', hud_alpha_fg, 0, a);
2382                         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);
2383                 }
2384                 else
2385                         a = 1;
2386
2387                 if(race_laptime && race_checkpoint != 255)
2388                 {
2389                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
2390                         //drawstring(m - '16 0 0' * stringwidth(s, FALSE), s, '32 32 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2391                         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);
2392                 }
2393         }
2394         else
2395         {
2396                 if(race_mycheckpointtime)
2397                 {
2398                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
2399                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
2400                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2401                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2402                         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);
2403                 }
2404                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
2405                 {
2406                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
2407                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
2408                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2409                         //drawcolorcodedstring(m - '0 0 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2410                         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);
2411                 }
2412
2413                 if(race_penaltytime && !race_penaltyaccumulator)
2414                 {
2415                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
2416                         a = bound(0, (1 + t - time), 1);
2417                         if(a > 0)
2418                         {
2419                                 if(time < t)
2420                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
2421                                 else
2422                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
2423                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2424                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2425                                 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);
2426                         }
2427                 }
2428         }
2429
2430         drawfont = hud_font;
2431 }
2432
2433 // Vote window (#9)
2434 //
2435 float vote_yescount;
2436 float vote_nocount;
2437 float vote_needed;
2438 float vote_highlighted; // currently selected vote
2439
2440 float vote_active; // is there an active vote?
2441 float vote_prev; // previous state of vote_active to check for a change
2442 float vote_alpha;
2443 float vote_change; // "time" when vote_active changed
2444
2445 void HUD_VoteWindow(void) 
2446 {
2447         float id = 9;
2448         vector pos, mySize;
2449         pos = HUD_Panel_GetPos(id);
2450         mySize = HUD_Panel_GetSize(id);
2451
2452         string s;
2453         float a;
2454         if(vote_active != vote_prev) {
2455                 vote_change = time;
2456                 vote_prev = bound(0, vote_active, 1);
2457         }
2458
2459         if(vote_active)
2460                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
2461         else
2462                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
2463
2464         if(hud_configure)
2465         {
2466                 vote_alpha = 1;
2467                 vote_yescount = 3;
2468                 vote_nocount = 2;
2469                 vote_needed = 4;
2470         }
2471
2472         HUD_Panel_DrawBg(id, pos, mySize);
2473         float padding;
2474         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2475         if(padding)
2476         {
2477                 pos += '1 1 0' * padding;
2478                 mySize -= '2 2 0' * padding;
2479         }
2480
2481         if(vote_alpha) {
2482                 a = vote_alpha * bound(cvar_or("hud_vote_alreadyvoted_alpha", 0.75), 1 - vote_highlighted, 1);
2483
2484                 drawpic_skin(pos, "voteprogress_back", mySize, HUD_GetBgColor(), a * hud_alpha_bg, DRAWFLAG_NORMAL);
2485
2486                 s = "A vote has been called for: ";
2487                 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);
2488                 s = textShortenToWidth(vote_called_vote, mySize_x * 0.96, '10 0 0', stringwidth_colors);
2489                 if(hud_configure)
2490                         s = "Configure the HUD";
2491                 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);
2492
2493                 // print the yes/no counts
2494                 s = strcat("Yes: ", ftos(vote_yescount));
2495                 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);
2496                 s = strcat("No: ", ftos(vote_nocount));
2497                 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);
2498
2499                 // draw the progress bars
2500                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
2501                 drawpic_skin(pos, "voteprogress_prog", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2502
2503                 drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
2504                 drawpic_skin(pos, "voteprogress_prog", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2505
2506                 // draw the highlights
2507                 if(vote_highlighted == 1) {
2508                         drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
2509                         drawpic_skin(pos, "voteprogress_voted", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2510                 }
2511                 else if(vote_highlighted == 2) {
2512                         drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
2513                         drawpic_skin(pos, "voteprogress_voted", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2514                 }
2515
2516                 drawresetcliparea();
2517         }
2518         else if(!vote_active) {
2519                 vote_highlighted = 0;
2520         }
2521 }
2522
2523 // Mod icons/awards panel (#10)
2524 //
2525
2526 float race_status_time;
2527 float race_status_prev;
2528 string race_status_name_prev;
2529 void HUD_DrawRaceStatus(vector pos)
2530 {
2531         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
2532                 race_status_time = time + 5;
2533                 race_status_prev = race_status;
2534                 if (race_status_name_prev)
2535                         strunzone(race_status_name_prev);
2536                 race_status_name_prev = strzone(race_status_name);
2537         }
2538
2539         float a;
2540         a = bound(0, race_status_time - time, 1);
2541
2542         string s;
2543         s = textShortenToWidth(race_status_name, 120, '10 10 0', stringwidth_colors);
2544
2545         float rank;
2546         if(race_status > 0)
2547                 rank = race_CheckName(race_status_name);
2548         string rankname;
2549         rankname = race_PlaceName(rank);
2550
2551         if(race_status == 0)
2552                 drawpic_skin(pos, "race_newfail", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2553         else if(race_status == 1) {
2554                 drawpic_skin(pos, "race_newtime", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2555                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2556                 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);
2557         } else if(race_status == 2) {
2558                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
2559                         drawpic_skin(pos, "race_newrankgreen", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2560                 else
2561                         drawpic_skin(pos, "race_newrankyellow", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2562                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2563                 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);
2564         } else if(race_status == 3) {
2565                 drawpic_skin(pos, "race_newrecordserver", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2566                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2567                 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);
2568         }
2569
2570         if (race_status_time - time <= 0) {
2571                 race_status_prev = -1;
2572                 race_status = -1;
2573                 if(race_status_name)
2574                         strunzone(race_status_name);
2575                 race_status_name = string_null;
2576                 if(race_status_name_prev)
2577                         strunzone(race_status_name_prev);
2578                 race_status_name_prev = string_null;
2579         }
2580 }
2581
2582 // CTF HUD modicon section
2583 float redflag_prevframe, blueflag_prevframe; // status during previous frame
2584 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
2585 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
2586
2587 void CSQC_ctf_hudreset(void)
2588 {
2589         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
2590 }
2591
2592 void CSQC_ctf_hud(void)
2593 {
2594         vector bottomleft, redflag_pos, blueflag_pos, sz;
2595         float f; // every function should have that
2596         bottomleft_y = vid_conheight;
2597         bottomleft_z = 0;
2598
2599         float redflag, blueflag; // current status
2600         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
2601         float stat_items;
2602
2603         stat_items = getstati(STAT_ITEMS);
2604         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
2605         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
2606
2607         // when status CHANGES, set old status into prevstatus and current status into status
2608         if (redflag != redflag_prevframe)
2609         {
2610                 redflag_statuschange_time = time;
2611                 redflag_prevstatus = redflag_prevframe;
2612                 redflag_prevframe = redflag;
2613         }
2614
2615         if (blueflag != blueflag_prevframe)
2616         {
2617                 blueflag_statuschange_time = time;
2618                 blueflag_prevstatus = blueflag_prevframe;
2619                 blueflag_prevframe = blueflag;
2620         }
2621
2622         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
2623         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
2624
2625         float BLINK_FACTOR = 0.15;
2626         float BLINK_BASE = 0.85;
2627         // note:
2628         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
2629         // thus
2630         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
2631         // ensure RMS == 1
2632         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
2633
2634         string red_icon, red_icon_prevstatus;
2635         float red_alpha, red_alpha_prevstatus;
2636         red_alpha = red_alpha_prevstatus = 1;
2637         switch(redflag) {
2638                 case 1: red_icon = "flag_red_taken"; break;
2639                 case 2: red_icon = "flag_red_lost"; break;
2640                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2641                 default:
2642                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2643                                 red_icon = "flag_red_shielded";
2644                         else
2645                                 red_icon = string_null;
2646                         break;
2647         }
2648         switch(redflag_prevstatus) {
2649                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
2650                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
2651                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2652                 default:
2653                         if(redflag == 3)
2654                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
2655                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2656                                 red_icon_prevstatus = "flag_red_shielded";
2657                         else
2658                                 red_icon_prevstatus = string_null;
2659                         break;
2660         }
2661
2662         string blue_icon, blue_icon_prevstatus;
2663         float blue_alpha, blue_alpha_prevstatus;
2664         blue_alpha = blue_alpha_prevstatus = 1;
2665         switch(blueflag) {
2666                 case 1: blue_icon = "flag_blue_taken"; break;
2667                 case 2: blue_icon = "flag_blue_lost"; break;
2668                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2669                 default:
2670                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2671                                 blue_icon = "flag_blue_shielded";
2672                         else
2673                                 blue_icon = string_null;
2674                         break;
2675         }
2676         switch(blueflag_prevstatus) {
2677                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
2678                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
2679                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2680                 default:
2681                         if(blueflag == 3)
2682                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
2683                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2684                                 blue_icon_prevstatus = "flag_blue_shielded";
2685                         else
2686                                 blue_icon_prevstatus = string_null;
2687                         break;
2688         }
2689
2690         if (myteam == COLOR_TEAM1) { // always draw own flag on left
2691                 redflag_pos = bottomleft - '-4 50 0';
2692                 blueflag_pos = bottomleft - '-62 50 0';
2693         } else {
2694                 blueflag_pos = bottomleft - '-4 50 0';
2695                 redflag_pos = bottomleft - '-62 50 0';
2696         }
2697
2698         sz = '52 52 0';
2699
2700         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
2701         if(red_icon_prevstatus && f < 1)
2702                 drawpic_expanding(redflag_pos, red_icon_prevstatus, sz, '1 1 1', hud_alpha_fg * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2703         if(red_icon)
2704                 drawpic_skin(redflag_pos, red_icon, sz, '1 1 1', hud_alpha_fg * red_alpha * f, DRAWFLAG_NORMAL);
2705
2706         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
2707         if(blue_icon_prevstatus && f < 1)
2708                 drawpic_expanding(blueflag_pos, blue_icon_prevstatus, sz, '1 1 1', hud_alpha_fg * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2709         if(blue_icon)
2710                 drawpic_skin(blueflag_pos, blue_icon, sz, '1 1 1', hud_alpha_fg * blue_alpha * f, DRAWFLAG_NORMAL);
2711 }
2712
2713 /*void HUD_Mod_Race (void) {
2714         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
2715                 pl = players.sort_next;
2716                 if(pl == me)
2717                         pl = pl.sort_next;
2718                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
2719                         if(pl.scores[ps_primary] == 0)
2720                                 pl = world;
2721
2722                 score = me.(scores[ps_primary]);
2723
2724                 float racemin, racesec, racemsec;
2725                 float distsec, distmsec, minusplus;
2726
2727                 racemin = floor(score/(60 * TIME_FACTOR));
2728                 racesec = floor((score - racemin*(60 * TIME_FACTOR))/TIME_FACTOR);
2729                 racemsec = score - racemin*60*TIME_FACTOR - racesec*TIME_FACTOR;
2730
2731                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
2732                         // distribution display
2733                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2734
2735                         if (distribution < TIME_FACTOR && distribution > -TIME_FACTOR)
2736                                 distmsec = fabs(distribution);
2737                         else {
2738                                 distsec = floor(fabs(distribution)/TIME_FACTOR);
2739                                 distmsec = fabs(distribution) - distsec*TIME_FACTOR;
2740                                 if (distribution < 0)
2741                                         distsec = -distsec;
2742                         }
2743
2744                         if (distribution <= 0) {
2745                                 distribution_color = eY;
2746                                 minusplus = 1; // minusplus 1: always prefix with minus sign
2747                         }
2748                         else {
2749                                 distribution_color = eX;
2750                                 minusplus = 2; // minusplus 1: always prefix with plus sign
2751                         }
2752                         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);
2753                         HUD_DrawXNum(bottomright - '68 48 0' - '16 0 0' * TIME_DECIMALS, distsec, 4, minusplus, 16, distribution_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2754                         drawpic_skin(bottomright - '10 48 0' - '16 0 0' * TIME_DECIMALS, "num_dot", '16 16 0', distribution_color, hud_alpha_fg, DRAWFLAG_ADDITIVE);
2755                 }
2756                 // race record display
2757                 if (distribution <= 0 || distribution == score) // draw the highlight background behind the timer if we have the lead
2758                         drawpic_skin(bottomright - '0 32 0' - '32 0 0' * (4 + TIME_DECIMALS), "highlight_4", '0 28 0' + '32 0 0' * (4 + TIME_DECIMALS), '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2759
2760                 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);
2761                 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);
2762                 drawpic_skin(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '18 0 0', "num_dot", '30 30 0', '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
2763
2764                 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);
2765                 drawpic_skin(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '84 0 0', "num_colon", '30 30 0', '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
2766         }
2767 }*/
2768
2769 // Keyhunt HUD modicon section
2770 float kh_runheretime;
2771
2772 void CSQC_kh_hudreset(void)
2773 {
2774         kh_runheretime = 0;
2775 }
2776
2777 void CSQC_kh_hud(void)
2778 {
2779         float kh_keys;
2780         float keyteam;
2781         float a, aa;
2782         vector p, pa, kh_size, kh_asize;
2783
2784         p_x = 6;
2785         p_y = vid_conheight - 34 - 3;
2786         p_z = 0;
2787
2788         kh_keys = getstati(STAT_KH_KEYS);
2789
2790         kh_size = '19 34 0';
2791         kh_asize = '19 10 0';
2792         pa = p + '0 -10 0';
2793
2794         float i, key;
2795
2796         float keycount;
2797         keycount = 0;
2798         for(i = 0; i < 4; ++i)
2799         {
2800                 key = floor(kh_keys / pow(32, i)) & 31;
2801                 keyteam = key - 1;
2802                 if(keyteam == 30 && keycount <= 4)
2803                         keycount += 4;
2804                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
2805                         keycount += 1;
2806         }
2807         // this yields 8 exactly if "RUN HERE" shows
2808
2809         if(keycount == 8)
2810         {
2811                 if(!kh_runheretime)
2812                         kh_runheretime = time;
2813                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
2814         }
2815         else
2816                 kh_runheretime = 0;
2817
2818         for(i = 0; i < 4; ++i)
2819         {
2820                 key = floor(kh_keys / pow(32, i)) & 31;
2821                 keyteam = key - 1;
2822                 switch(keyteam)
2823                 {
2824                         case 30: // my key
2825                                 keyteam = myteam;
2826                                 a = 1;
2827                                 aa = 1;
2828                                 break;
2829                         case -1: // no key
2830                                 a = 0;
2831                                 aa = 0;
2832                                 break;
2833                         default: // owned or dropped
2834                                 a = 0.2;
2835                                 aa = 0.5;
2836                                 break;
2837                 }
2838                 a = a * hud_alpha_fg;
2839                 aa = aa * hud_alpha_fg;
2840                 if(a > 0)
2841                 {
2842                         switch(keyteam)
2843                         {
2844                                 case COLOR_TEAM1:
2845                                         drawpic (pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2846                                         break;
2847                                 case COLOR_TEAM2:
2848                                         drawpic (pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2849                                         break;
2850                                 case COLOR_TEAM3:
2851                                         drawpic (pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2852                                         break;
2853                                 case COLOR_TEAM4:
2854                                         drawpic (pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2855                                         break;
2856                                 default:
2857                                         break;
2858                         }
2859                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
2860                         {
2861                                 case 0:
2862                                         drawpic (p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2863                                         break;
2864                                 case 1:
2865                                         drawpic (p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2866                                         break;
2867                                 case 2:
2868                                         drawpic (p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2869                                         break;
2870                                 case 3:
2871                                         drawpic (p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2872                                         break;
2873                         }
2874                 }
2875                 p_x += 24;
2876                 pa_x += 24;
2877         }
2878 }
2879
2880 // Nexball HUD modicon section
2881 #define NBPB_SIZE '96 38 0'
2882 #define NBPB_BT 2                   //thickness
2883 #define NBPB_BRGB '1 1 1'
2884 #define NBPB_BALPH 1                //alpha
2885 #define NBPB_BFLAG DRAWFLAG_NORMAL
2886 #define NBPB_IALPH 0.4
2887 #define NBPB_IFLAG DRAWFLAG_NORMAL
2888 #define NBPB_IRGB '0.7 0.1 0'
2889
2890 void CSQC_nb_hud(void)
2891 {
2892         float stat_items, nb_pb_starttime, dt, p;
2893         vector pos;
2894
2895         stat_items = getstati(STAT_ITEMS);
2896         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
2897
2898         pos_x = 4;
2899         pos_y = vid_conheight - 42;
2900         pos_z = 0;
2901
2902         //Manage the progress bar if any
2903         if (nb_pb_starttime > 0)
2904         {
2905                 vector s;
2906                 dt = mod(time - nb_pb_starttime, nb_pb_period);
2907                 // one period of positive triangle
2908                 p = 2 * dt / nb_pb_period;
2909                 if (p > 1)
2910                         p = 2 - p;
2911
2912                 s = NBPB_SIZE;
2913                 //Draw the filling
2914                 drawfill(pos, p * s_x * eX + s_y * eY, NBPB_IRGB, NBPB_IALPH, NBPB_IFLAG);
2915
2916                 //Draw the box
2917                 s = NBPB_SIZE;
2918                 drawline(NBPB_BT, pos    , pos + eX * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2919                 drawline(NBPB_BT, pos    , pos + eY * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2920                 drawline(NBPB_BT, pos + s, pos + eX * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2921                 drawline(NBPB_BT, pos + s, pos + eY * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2922         }
2923
2924         pos_x += 12; //horizontal margin to the picture
2925         pos_y += 2; //vertical margin to the picture
2926
2927         if (stat_items & IT_KEY1)
2928                 drawpic_skin(pos, "nexball_carrying", '80 34 0', '1 1 1', 1, DRAWFLAG_NORMAL);
2929 }
2930
2931 // Race/CTS HUD modicon section
2932 float crecordtime_prev; // last remembered crecordtime
2933 float crecordtime_change_time; // time when crecordtime last changed
2934 float srecordtime_prev; // last remembered srecordtime
2935 float srecordtime_change_time; // time when srecordtime last changed
2936 void CSQC_race_hud(void)
2937 {
2938         entity me;
2939         me = playerslots[player_localentnum - 1];
2940         float t, score;
2941         float f; // yet another function has this
2942         score = me.(scores[ps_primary]);
2943
2944         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
2945                 return; // no records in the actual race
2946
2947         drawfont = hud_bigfont;
2948         vector pos;
2949         pos_x = 2;
2950         pos_y = vid_conheight - 48;
2951
2952         // clientside personal record
2953         string rr;
2954         if(gametype == GAME_CTS)
2955                 rr = CTS_RECORD;
2956         else
2957                 rr = RACE_RECORD;
2958         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
2959
2960         if(score && (score < t || !t)) {
2961                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
2962                 if(cvar("cl_autodemo_delete_keeprecords"))
2963                 {
2964                         f = cvar("cl_autodemo_delete");
2965                         f &~= 1;
2966                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
2967                 }
2968         }
2969
2970         if(t != crecordtime_prev) {
2971                 crecordtime_prev = t;
2972                 crecordtime_change_time = time;
2973         }
2974         f = time - crecordtime_change_time;
2975
2976         if (f > 1) {
2977                 drawstring(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2978                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2979         } else {
2980                 drawstring(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2981                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2982                 drawstring_expanding(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2983                 drawstring_expanding(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2984         }
2985
2986         // server record
2987         pos_y += 26;
2988         t = race_server_record;
2989         if(t != srecordtime_prev) {
2990                 srecordtime_prev = t;
2991                 srecordtime_change_time = time;
2992         }
2993         f = time - srecordtime_change_time;
2994
2995         if (f > 1) {
2996                 drawstring(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2997                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2998         } else {
2999                 drawstring(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3000                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3001                 drawstring_expanding(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
3002                 drawstring_expanding(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
3003         }
3004         drawfont = hud_font;
3005 }
3006
3007 void HUD_ModIcons(void)
3008 {
3009         float id = 10;
3010         vector pos, mySize;
3011         pos = HUD_Panel_GetPos(id);
3012         mySize = HUD_Panel_GetSize(id);
3013
3014         HUD_Panel_DrawBg(id, pos, mySize);
3015         float padding;
3016         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
3017         if(padding)
3018         {
3019                 pos += '1 1 0' * padding;
3020                 mySize -= '2 2 0' * padding;
3021         }
3022 }
3023
3024 // Draw pressed keys (#11)
3025 //
3026 void HUD_DrawPressedKeys(void)
3027 {
3028         float id = 11;
3029         vector pos, mySize;
3030         pos = HUD_Panel_GetPos(id);
3031         mySize = HUD_Panel_GetSize(id);
3032
3033         HUD_Panel_DrawBg(id, pos, mySize);
3034         float padding;
3035         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
3036         if(padding)
3037         {
3038                 pos += '1 1 0' * padding;
3039                 mySize -= '2 2 0' * padding;
3040         }
3041
3042         float pressedkeys;
3043
3044         pressedkeys = getstatf(STAT_PRESSED_KEYS);
3045         drawpic_skin(pos, "key_bg.tga",           mySize, '1 1 1', 0.1 * hud_alpha_fg, DRAWFLAG_NORMAL);
3046         drawpic_skin(pos + eX * mySize_x - eX * 0.22 * mySize_x +       eY * 0.195 * mySize_y, ((pressedkeys & KEY_CROUCH) ? "key_crouch_inv.tga" : "key_crouch.tga"),        '1 1 0' * (1/3) * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3047         drawpic_skin(pos + eX * 0.5 * mySize_x - eX * 0.23 * mySize_y + eY * 0.040 * mySize_y, ((pressedkeys & KEY_FORWARD) ? "key_forward_inv.tga" : "key_forward.tga"),     '1 1 0' * 0.46 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3048         drawpic_skin(pos + eX * 0.023 * mySize_x +                      eY * 0.195 * mySize_y, ((pressedkeys & KEY_JUMP) ? "key_jump_inv.tga" : "key_jump.tga"),              '1 1 0' * (1/3) * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3049         drawpic_skin(pos + eX * 0.1 * mySize_x +                        eY * 0.486 * mySize_y, ((pressedkeys & KEY_LEFT) ? "key_left_inv.tga" : "key_left.tga"),              '1 1 0' * 0.46 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3050         drawpic_skin(pos + eX * 0.5 * mySize_x - eX * 0.23 * mySize_y + eY * 0.486 * mySize_y, ((pressedkeys & KEY_BACKWARD) ? "key_backward_inv.tga" : "key_backward.tga"),  '1 1 0' * 0.46 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3051         drawpic_skin(pos + eX * mySize_x - eX * 0.372 * mySize_x +      eY * 0.486 * mySize_y, ((pressedkeys & KEY_RIGHT) ? "key_right_inv.tga" : "key_right.tga"),           '1 1 0' * 0.46 * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3052 }
3053
3054 /*
3055 ==================
3056 Main HUD system
3057 ==================
3058 */
3059
3060 void HUD_ShowSpeed(void)
3061 {
3062         vector numsize;
3063         float pos, conversion_factor;
3064         string speed, zspeed, unit;
3065
3066         switch(cvar("cl_showspeed_unit"))
3067         {
3068                 default:
3069                 case 0:
3070                         unit = "";
3071                         conversion_factor = 1.0;
3072                         break;
3073                 case 1:
3074                         unit = " qu/s";
3075                         conversion_factor = 1.0;
3076                         break;
3077                 case 2:
3078                         unit = " m/s";
3079                         conversion_factor = 0.0254;
3080                         break;
3081                 case 3:
3082                         unit = " km/h";
3083                         conversion_factor = 0.0254 * 3.6;
3084                         break;
3085                 case 4:
3086                         unit = " mph";
3087                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
3088                         break;
3089                 case 5:
3090                         unit = " knots";
3091                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
3092                         break;
3093         }
3094
3095         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
3096
3097         numsize_x = numsize_y = cvar("cl_showspeed_size");
3098         pos = (vid_conheight - numsize_y) * cvar("cl_showspeed_position");
3099
3100         drawfont = hud_bigfont;
3101         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3102
3103         if (cvar("cl_showspeed_z") == 1) {
3104                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
3105                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3106         }
3107
3108         drawfont = hud_font;
3109 }
3110
3111 vector acc_prevspeed;
3112 float acc_prevtime;
3113 float acc_avg;
3114
3115 void HUD_ShowAcceleration(void)
3116 {
3117         float acceleration, sz, scale, alpha, f;
3118         vector pos, top, rgb;
3119         top_x = vid_conwidth/2;
3120         top_y = 0;
3121
3122         f = time - acc_prevtime;
3123         if(cvar("cl_showacceleration_z"))
3124                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
3125         else
3126                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
3127         acc_prevspeed = pmove_vel;
3128         acc_prevtime = time;
3129
3130         f = bound(0, f * 10, 1);
3131         acc_avg = acc_avg * (1 - f) + acceleration * f;
3132         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
3133
3134         pos = top - sz/2 * eY + (cvar("cl_showacceleration_position") * vid_conheight) * eY;
3135
3136         sz = cvar("cl_showacceleration_size");
3137         scale = cvar("cl_showacceleration_scale");
3138         alpha = cvar("cl_showacceleration_alpha");
3139         if (cvar("cl_showacceleration_color_custom"))
3140                 rgb = stov(cvar_string("cl_showacceleration_color"));
3141         else {
3142                 rgb = '1 1 1';
3143                 if (acceleration < 0) {
3144                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
3145                 } else if (acceleration > 0) {
3146                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
3147                 }
3148         }
3149
3150         if (acceleration > 0)
3151                 drawpic_skin(pos, "statusbar", acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3152         else if (acceleration < 0)
3153                 drawpic_skin(pos + acceleration * scale * '40 0 0', "statusbar", -acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3154 }
3155
3156 void HUD_Reset (void)
3157 {
3158         // reset gametype specific icons
3159         if(gametype == GAME_KEYHUNT)
3160                 CSQC_kh_hudreset();
3161         else if(gametype == GAME_CTF)
3162                 CSQC_ctf_hudreset();
3163 }
3164
3165 void HUD_Main (void)
3166 {
3167         hud_alpha_bg = cvar_or("hud_bg_alpha", 0.8) * (1 - cvar("_menu_alpha"));
3168         hud_border_thickness = bound(0, cvar("hud_border_thickness"), 5);
3169         hud_accuracy_border_thickness = bound(0, cvar_or("hud_accuracy_border_thickness", 1), 5);
3170         hud_color_bg_team = cvar("hud_color_bg_team");
3171
3172         hud_fontsize = HUD_GetFontsize("hud_fontsize");
3173         hud_fontsize_spec = HUD_GetFontsize("hud_fontsize_spec");
3174
3175         hud_configure = cvar("_hud_configure");
3176
3177         // Drawing stuff
3178         if(cvar("hud_dock"))
3179                 drawpic_skin('0 0 0', "dock", eX * vid_conwidth + eY * vid_conheight, stov(cvar_string("hud_dock_color")), cvar("hud_dock_alpha"), DRAWFLAG_NORMAL);
3180
3181         if(HUD_Panel_CheckActive(0))
3182                 HUD_WeaponIcons();
3183         if(HUD_Panel_CheckActive(1))
3184                 HUD_Inventory();
3185         if(HUD_Panel_CheckActive(2))
3186                 HUD_Powerups();
3187         if(HUD_Panel_CheckActive(3))
3188                 HUD_HealthArmor();
3189         if(HUD_Panel_CheckActive(4))
3190                 HUD_Notify();
3191         if(HUD_Panel_CheckActive(5))
3192                 HUD_Timer();
3193         // TODO hud'ify
3194         if(HUD_Panel_CheckActive(6))
3195                 if(ons_showmap || cvar_string("cl_teamradar") != "0" && (cvar("cl_teamradar") == 2 || teamplay))
3196                         HUD_Radar();
3197         if(HUD_Panel_CheckActive(7))
3198                 HUD_Score();
3199         if(HUD_Panel_CheckActive(8))
3200                 if(gametype == GAME_RACE || gametype == GAME_CTS || hud_configure)
3201                         HUD_RaceTimer();
3202         if(HUD_Panel_CheckActive(9))
3203                 HUD_VoteWindow();
3204         // TODO hud'ify
3205         if(HUD_Panel_CheckActive(10))
3206                 if(spectatee_status > 0 || cvar("cl_showpressedkeys") >= 2 || hud_configure)
3207                         HUD_DrawPressedKeys();
3208
3209         // TODO hud_'ify these
3210         if (cvar("cl_showspeed"))
3211                 HUD_ShowSpeed();
3212         if (cvar("cl_showacceleration"))
3213                 HUD_ShowAcceleration();
3214
3215         // TODO... well make them work in a panel etc
3216         if(gametype == GAME_KEYHUNT)
3217                 CSQC_kh_hud();
3218         else if(gametype == GAME_CTF)
3219                 CSQC_ctf_hud();
3220         else if(gametype == GAME_NEXBALL)
3221                 CSQC_nb_hud();
3222         else if(gametype == GAME_CTS || gametype == GAME_RACE)
3223                 CSQC_race_hud();
3224         return;
3225 }