]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
more preparations for modicon panel
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qc
1 /*
2 ==================
3 Misc HUD functions
4 ==================
5 */
6
7 // a border picture is a texture containing nine parts:
8 //   1/4 width: left part
9 //   1/2 width: middle part (stretched)
10 //   1/4 width: right part
11 // divided into
12 //   1/4 height: top part
13 //   1/2 height: middle part (stretched)
14 //   1/4 height: bottom part
15 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
16 {
17         vector dX, dY;
18         vector width, height;
19         vector bW, bH;
20         //pic = draw_UseSkinFor(pic);
21         width = eX * theSize_x;
22         height = eY * theSize_y;
23         if(theSize_x <= theBorderSize_x * 2)
24         {
25                 // not wide enough... draw just left and right then
26                 bW = eX * (0.25 * theSize_x / (theBorderSize_x * 2));
27                 if(theSize_y <= theBorderSize_y * 2)
28                 {
29                         // not high enough... draw just corners
30                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
31                         drawsubpic(theOrigin,                 width * 0.5 + height * 0.5, pic, '0 0 0',           bW + bH, theColor, theAlpha, 0);
32                         drawsubpic(theOrigin + width   * 0.5, width * 0.5 + height * 0.5, pic, eX - bW,           bW + bH, theColor, theAlpha, 0);
33                         drawsubpic(theOrigin + height  * 0.5, width * 0.5 + height * 0.5, pic, eY - bH,           bW + bH, theColor, theAlpha, 0);
34                         drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
35                 }
36                 else
37                 {
38                         dY = theBorderSize_x * eY;
39                         drawsubpic(theOrigin,                             width * 0.5          +     dY, pic, '0 0    0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
40                         drawsubpic(theOrigin + width * 0.5,               width * 0.5          +     dY, pic, '0 0    0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
41                         drawsubpic(theOrigin                        + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0',           '0 0.5  0' + bW, theColor, theAlpha, 0);
42                         drawsubpic(theOrigin + width * 0.5          + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0' + eX - bW, '0 0.5  0' + bW, theColor, theAlpha, 0);
43                         drawsubpic(theOrigin               + height - dY, width * 0.5          +     dY, pic, '0 0.75 0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
44                         drawsubpic(theOrigin + width * 0.5 + height - dY, width * 0.5          +     dY, pic, '0 0.75 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
45                 }
46         }
47         else
48         {
49                 if(theSize_y <= theBorderSize_y * 2)
50                 {
51                         // not high enough... draw just top and bottom then
52                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
53                         dX = theBorderSize_x * eX;
54                         drawsubpic(theOrigin,                                         dX + height * 0.5, pic, '0    0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
55                         drawsubpic(theOrigin + dX,                        width - 2 * dX + height * 0.5, pic, '0.25 0 0',           '0.5  0 0' + bH, theColor, theAlpha, 0);
56                         drawsubpic(theOrigin + width - dX,                            dX + height * 0.5, pic, '0.75 0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
57                         drawsubpic(theOrigin              + height * 0.5,             dX + height * 0.5, pic, '0    0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
58                         drawsubpic(theOrigin + dX         + height * 0.5, width - 2 * dX + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5  0 0' + bH, theColor, theAlpha, 0);
59                         drawsubpic(theOrigin + width - dX + height * 0.5,             dX + height * 0.5, pic, '0.75 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
60                 }
61                 else
62                 {
63                         dX = theBorderSize_x * eX;
64                         dY = theBorderSize_x * eY;
65                         drawsubpic(theOrigin,                                        dX          +     dY, pic, '0    0    0', '0.25 0.25 0', theColor, theAlpha, 0);
66                         drawsubpic(theOrigin                  + dX,      width - 2 * dX          +     dY, pic, '0.25 0    0', '0.5  0.25 0', theColor, theAlpha, 0);
67                         drawsubpic(theOrigin          + width - dX,                  dX          +     dY, pic, '0.75 0    0', '0.25 0.25 0', theColor, theAlpha, 0);
68                         drawsubpic(theOrigin          + dY,                          dX + height - 2 * dY, pic, '0    0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
69                         drawsubpic(theOrigin          + dY         + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5  0.5  0', theColor, theAlpha, 0);
70                         drawsubpic(theOrigin          + dY + width - dX,             dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
71                         drawsubpic(theOrigin + height - dY,                          dX          +     dY, pic, '0    0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
72                         drawsubpic(theOrigin + height - dY         + dX, width - 2 * dX          +     dY, pic, '0.25 0.75 0', '0.5  0.25 0', theColor, theAlpha, 0);
73                         drawsubpic(theOrigin + height - dY + width - dX,             dX          +     dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
74                 }
75         }
76 }
77
78 // 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 = (1/2) * mySize_x; // 1/2 * width
661                         break;
662                 case 11: 
663                         mySize_y = 0.5898 * mySize_x; // 0.5898 * width, reason: bg has weird dimensions...
664                         break;
665         }
666         return mySize;
667 }
668
669 // return active status of panel
670 float HUD_Panel_CheckActive(float id)
671 {
672         if (cvar_or(strcat("hud_", HUD_Panel_GetName(id)), 1))
673                 return 1;
674         return 0;
675 }
676
677 // return size of given panel
678 vector HUD_Panel_GetSize(float id)
679 {
680         vector mySize;
681         mySize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
682
683         mySize = eX * mySize_x * vid_conwidth + eY * mySize_y * vid_conheight;
684
685         return mySize;
686 }
687
688 // return pos of given panel
689 vector HUD_Panel_GetPos(float id)
690 {
691         vector pos;
692         pos = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_pos")));
693
694         pos = eX * pos_x * vid_conwidth + eY * pos_y * vid_conheight;
695
696         if (pos_x < 0)
697                 pos_x = vid_conwidth + pos_x;
698         if (pos_y < 0)
699                 pos_y = vid_conheight + pos_y;
700         return pos;
701 }
702
703 float HUD_Panel_GetBorder(float id)
704 {
705         float border;
706         border = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_border"));
707         if(!border)
708                 border = cvar("hud_bg_border");
709         return border;
710 }
711
712 // draw the background/borders
713 void HUD_Panel_DrawBg(float id, vector pos, vector mySize)
714 {
715         if(!hud_configure && cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg")) == "0")
716                 return;
717
718         string bg;
719         bg = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg"));
720         if(bg == "")
721                 bg = cvar_string("hud_bg");
722
723         if(bg != "0")
724         {
725                 float border;
726                 border = max(0.0000001, HUD_Panel_GetBorder(id)); // draw_BorderPicture does not like border = 0
727
728                 vector color;
729                 if(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color")) != "")
730                         color = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color")));
731                 else
732                         color = stov(cvar_string("hud_bg_color"));
733
734                 float alpha;
735                 alpha = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_alpha"));
736                 if(!alpha)
737                         alpha = cvar("hud_bg_alpha");
738                 if(hud_configure)
739                         alpha = max(cvar("hud_configure_bg_minalpha"), alpha);
740
741                 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));
742         }
743 }
744
745 vector HUD_Panel_GetProgressBarColor(string item)
746 {
747         return stov(cvar_string(strcat("hud_progressbar_", item, "_color")));
748 }
749
750 float resizeCorner; // 1 = topleft, 2 = topright, 3 = bottomleft, 4 = bottomright
751 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
752 vector HUD_Panel_CheckResize(float id, vector myPos, vector mySize)
753 {
754         float i;
755
756         vector myTarget;
757         myTarget = mySize;
758
759         vector targPos;
760         vector targSize;
761         vector myCenter;
762         vector targCenter;
763
764         for (i = 0; i < panel_cnt; ++i) {
765                 if(i == id || !HUD_Panel_CheckActive(i))
766                         continue;
767
768                 targPos = HUD_Panel_GetPos(i);
769                 targSize = HUD_Panel_GetSize(i);
770
771                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
772                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(id);
773
774                 if(myPos_y + mySize_y < targPos_y)
775                         continue;
776                 if(myPos_y > targPos_y + targSize_y)
777                         continue;
778
779                 if(myPos_x + mySize_x < targPos_x)
780                         continue;
781                 if(myPos_x > targPos_x + targSize_x)
782                         continue;
783
784                 // OK, there IS a collision.
785
786                 myCenter_x = myPos_x + 0.5 * mySize_x;
787                 myCenter_y = myPos_y + 0.5 * mySize_y;
788
789                 targCenter_x = targPos_x + 0.5 * targSize_x;
790                 targCenter_y = targPos_y + 0.5 * targSize_y;
791
792                 float k, y;
793                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y && resizeCorner != 1) // top left (of target panel)
794                 {
795                         if(myPos_x + mySize_x - targPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
796                                 myTarget_x = targPos_x - myPos_x;
797                         else // push it upwards
798                                 myTarget_y = targPos_y - myPos_y;
799                 }
800                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y && resizeCorner != 2) // top right
801                 {
802                         if(targPos_x + targSize_x - myPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
803                                 myTarget_x = targPos_x + targSize_x;
804                         else // push it upwards
805                                 myTarget_y = targPos_y - myPos_y;
806                 }
807                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
808                 {
809                         if(myPos_x + mySize_x - targPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
810                                 myTarget_x = targPos_x - myPos_x;
811                         else // push it downwards
812                                 myTarget_y = targPos_y + targSize_y;
813                 }
814                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
815                 {
816                         if(targPos_x + targSize_x - myPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
817                                 myTarget_x = targPos_x + targSize_x;
818                         else // push it downwards
819                                 myTarget_y = targPos_y + targSize_y;
820                 }
821         }
822
823         return myTarget;
824 }
825
826 // TODO: checkResize
827 float HUD_Panel_SetSize(float id, vector mySize)
828 {
829         float didntresize;
830
831         vector oldSize;
832         oldSize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
833
834         vector myPos;
835         myPos = HUD_Panel_GetPos(id);
836
837         // check for collisions
838         if(cvar("hud_configure_checkcollisions"))
839                 mySize = HUD_Panel_CheckResize(id, HUD_Panel_GetPos(id), mySize);
840
841         mySize_x = bound(0.025 * vid_conwidth, mySize_x, vid_conwidth - myPos_x);
842         mySize_y = bound(0.025 * vid_conheight, mySize_y, vid_conheight - myPos_y);
843
844         // cap against panel's own limits
845         mySize = HUD_Panel_CheckLimitSize(id, mySize);
846
847         //mySize_x = bound(0.025 * vid_conwidth, mySize_x, vid_conwidth);
848         //mySize_y = bound(0.025 * vid_conheight, mySize_y, vid_conheight);
849
850         // TODO: is this needed?
851         // this is to check if (and how) SetPos should be called
852         if(mySize_x == oldSize_x && mySize_y == oldSize_y)
853                 didntresize = 3; // didnt resize either
854         else if(mySize_x == oldSize_x && mySize_y != oldSize_y)
855                 didntresize = 2; // resized Y
856         else if(mySize_y == oldSize_y && mySize_x != oldSize_x)
857                 didntresize = 1; // resized X
858
859         string s;
860         s = strcat(ftos(mySize_x/vid_conwidth), " ", ftos(mySize_y/vid_conheight));
861         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_size"), s);
862         return didntresize;
863 }
864
865 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
866 vector HUD_Panel_CheckMove(float id, vector myPos, vector mySize)
867 {
868         float i;
869
870         vector myTarget;
871         myTarget = myPos;
872
873         vector targPos;
874         vector targSize;
875         vector myCenter;
876         vector targCenter;
877
878         for (i = 0; i < panel_cnt; ++i) {
879                 if(i == id || !HUD_Panel_CheckActive(i))
880                         continue;
881
882                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
883                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(id);
884
885                 if(myPos_y + mySize_y < targPos_y)
886                         continue;
887                 if(myPos_y > targPos_y + targSize_y)
888                         continue;
889
890                 if(myPos_x + mySize_x < targPos_x)
891                         continue;
892                 if(myPos_x > targPos_x + targSize_x)
893                         continue;
894
895                 // OK, there IS a collision.
896
897                 myCenter_x = myPos_x + 0.5 * mySize_x;
898                 myCenter_y = myPos_y + 0.5 * mySize_y;
899
900                 targCenter_x = targPos_x + 0.5 * targSize_x;
901                 targCenter_y = targPos_y + 0.5 * targSize_y;
902
903                 float k, y;
904                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y) // top left (of the target panel)
905                 {
906                         if(myPos_x + mySize_x - targPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
907                                 myTarget_x = targPos_x - mySize_x;
908                         else // push it upwards
909                                 myTarget_y = targPos_y - mySize_y;
910                 }
911                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y) // top right
912                 {
913                         if(targPos_x + targSize_x - myPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
914                                 myTarget_x = targPos_x + targSize_x;
915                         else // push it upwards
916                                 myTarget_y = targPos_y - mySize_y;
917                 }
918                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
919                 {
920                         if(myPos_x + mySize_x - targPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
921                                 myTarget_x = targPos_x - mySize_x;
922                         else // push it downwards
923                                 myTarget_y = targPos_y + targSize_y;
924                 }
925                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
926                 {
927                         if(targPos_x + targSize_x - myPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
928                                 myTarget_x = targPos_x + targSize_x;
929                         else // push it downwards
930                                 myTarget_y = targPos_y + targSize_y;
931                 }
932         }
933
934         return myTarget;
935 }
936
937 void HUD_Panel_SetPos(float id, vector pos, float didntresize)
938 {
939         vector oldPos;
940         oldPos = HUD_Panel_GetPos(id);
941         if(didntresize == 2)
942                 pos_x = oldPos_x;
943         else if(didntresize == 1)
944                 pos_y = oldPos_y;
945         else if(didntresize == 3)
946                 return;
947
948         vector mySize;
949         mySize = HUD_Panel_GetSize(id);
950
951         if(cvar("hud_configure_checkcollisions"))
952                 pos = HUD_Panel_CheckMove(id, pos, mySize);
953
954         pos_x = bound(0, pos_x, vid_conwidth - mySize_x);
955         pos_y = bound(0, pos_y, vid_conheight - mySize_y);
956
957         if (pos_x + 0.5 * mySize_x > 0.5 * vid_conwidth)
958                 pos_x = pos_x - vid_conwidth;
959         if (pos_y + 0.5 * mySize_y > 0.5 * vid_conheight)
960                 pos_y = pos_y - vid_conheight;
961
962         string s;
963         s = strcat(ftos(pos_x/vid_conwidth), " ", ftos(pos_y/vid_conheight));
964
965         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_pos"), s);
966 }
967
968 float mouseClicked;
969 float prevMouseClicked; // previous state
970 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
971 {
972         prevMouseClicked = mouseClicked;
973         if(nPrimary == K_MOUSE1)
974         {
975                 if(bInputType == 0) { // key pressed
976                         mouseClicked = 1;
977                         return true;
978                 }
979                 if(bInputType == 1) {// key released
980                         mouseClicked = 0;
981                         return true;
982                 }
983         }
984         return false;
985 }
986
987 vector mousepos, mouseprevpos;
988 vector panel_click_distance; // mouse cursor distance from the top left corner of the panel (saved only upon a click)
989 vector panel_click_pos; // panel pos (saved only upon a click)
990 vector panel_click_size; // panel size (saved only upon a click)
991 float highlightedPanel;
992 float highlightedAction; // 0 = nothing, 1 = move, 2 = resize
993 void HUD_Panel_Mouse()
994 {
995         if(mouseClicked == 0) {
996                 highlightedPanel = -1;
997                 highlightedAction = 0;
998         }
999
1000         mousepos = mousepos + getmousepos();
1001
1002         mousepos_x = bound(0, mousepos_x, vid_conwidth);
1003         mousepos_y = bound(0, mousepos_y, vid_conheight);
1004
1005         drawpic(mousepos, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1006
1007         if(mouseClicked)
1008         {
1009                 float i, border;
1010                 vector panelPos;
1011                 vector panelSize;
1012
1013                 for(i = 0; i <= panel_cnt; ++i)
1014                 {
1015                         panelPos = HUD_Panel_GetPos(i);
1016                         panelSize = HUD_Panel_GetSize(i);
1017                         border = HUD_Panel_GetBorder(i);
1018                         if(prevMouseClicked == 0) {
1019                                 // move
1020                                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1021                                 {
1022                                         highlightedPanel = i;
1023                                         highlightedAction = 1;
1024                                 }
1025                                 // resize from topleft border
1026                                 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)
1027                                 {
1028                                         highlightedPanel = i;
1029                                         highlightedAction = 2;
1030                                         resizeCorner = 1;
1031                                 }
1032                                 // resize from topright border
1033                                 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)
1034                                 {
1035                                         highlightedPanel = i;
1036                                         highlightedAction = 2;
1037                                         resizeCorner = 2;
1038                                 }
1039                                 // resize from bottomleft border
1040                                 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)
1041                                 {
1042                                         highlightedPanel = i;
1043                                         highlightedAction = 2;
1044                                         resizeCorner = 3;
1045                                 }
1046                                 // resize from bottomright border
1047                                 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)
1048                                 {
1049                                         highlightedPanel = i;
1050                                         highlightedAction = 2;
1051                                         resizeCorner = 4;
1052                                 }
1053                         }
1054
1055                         if(highlightedPanel == i)
1056                         {
1057                                 if(prevMouseClicked == 0)
1058                                 {
1059                                         panel_click_distance = mousepos - panelPos;
1060                                         panel_click_pos = panelPos;
1061                                         panel_click_size = panelSize;
1062                                 }
1063
1064                                 if(highlightedAction == 1)
1065                                         HUD_Panel_SetPos(i, mousepos - panel_click_distance, 0);
1066                                 else if(highlightedAction == 2)
1067                                 {
1068                                         float didntresize; // panel too big/too small, dont resize (also has to affect moving)
1069                                         if(resizeCorner == 1) {
1070                                                 didntresize = HUD_Panel_SetSize(i, panelSize + mouseprevpos - mousepos);
1071                                                 HUD_Panel_SetPos(i, mousepos - panel_click_distance, didntresize);
1072                                         }
1073                                         if(resizeCorner == 2) {
1074                                                 didntresize = HUD_Panel_SetSize(i, eY * panel_click_size_y + eX * (mousepos_x - panelPos_x - (panel_click_distance_x - panel_click_size_x))
1075                                                                 + eY * (panel_click_distance_y + (panel_click_pos_y - mousepos_y)));
1076                                                 HUD_Panel_SetPos(i, eX * panelPos_x + eY * (mousepos_y - panel_click_distance_y), didntresize);
1077                                         }
1078                                         if(resizeCorner == 3) {
1079                                                 didntresize = HUD_Panel_SetSize(i, panelSize + eX * (mouseprevpos_x - mousepos_x) + eY * (mousepos_y - mouseprevpos_y));
1080                                                 HUD_Panel_SetPos(i, eX * (mousepos_x - panel_click_distance_x) + eY * panelPos_y, didntresize);
1081                                         }
1082                                         if(resizeCorner == 4) {
1083                                                 HUD_Panel_SetSize(i, mousepos - panelPos - (panel_click_distance - panel_click_size));
1084                                         }
1085                                 }
1086                         }
1087                 }
1088         }
1089         mouseprevpos = mousepos;
1090         prevMouseClicked = mouseClicked;
1091 }
1092
1093 /*void HUD_DrawDockEdge(float id, vector p1, vector p2, float target)
1094 {
1095         vector pos;
1096         vector size;
1097
1098         pos =
1099 */ // maybe one day, since this will be quite complicated
1100
1101 // Weapon icons (#0)
1102 //
1103 float weaponspace[10];
1104 void HUD_WeaponIcons_Clear()
1105 {
1106         float idx;
1107         for(idx = 0; idx < 10; ++idx)
1108                 weaponspace[idx] = 0;
1109 }
1110
1111 entity weaponorder[WEP_MAXCOUNT];
1112
1113 void weaponorder_swap(float i, float j, entity pass)
1114 {
1115         entity h;
1116         h = weaponorder[i];
1117         weaponorder[i] = weaponorder[j];
1118         weaponorder[j] = h;
1119 }
1120
1121 float weaponorder_cmp(float i, float j, entity pass)
1122 {
1123         float d, ii, ij;
1124         d = mod(weaponorder[i].impulse + 9, 10) - mod(weaponorder[j].impulse + 9, 10);
1125         if(d)
1126                 return d;
1127         d = weaponorder[i].weapon - weaponorder[j].weapon;
1128         return d;
1129 }
1130
1131 void HUD_WeaponIcons()
1132 {
1133         float id = 0;
1134         float alpha, height, accuracybar_height, stat_weapons; // "constants"
1135         vector pos, mySize, mysize, mypos, accuracy_color;
1136         float i, weapid, fade, weapon_stats, weapon_hit, weapon_damage, weapon_cnt; // variables
1137
1138         pos = HUD_Panel_GetPos(id);
1139         mySize = HUD_Panel_GetSize(id);
1140         accuracybar_height = cvar_or("hud_weaponicons_accuracy_height", 3);
1141
1142         stat_weapons = getstati(STAT_WEAPONS);
1143         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1144         {
1145                 self = get_weaponinfo(i);
1146                 if(self.weapons && (self.impulse >= 0) && (stat_weapons & self.weapons) || hud_configure)
1147                 {
1148                         weaponorder[weapon_cnt] = self;
1149                         ++weapon_cnt;
1150                 }
1151         }
1152         heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
1153
1154         HUD_Panel_DrawBg(id, pos, mySize);
1155         float padding;
1156         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1157         if(padding)
1158         {
1159                 pos += '1 1 0' * padding;
1160                 mySize -= '2 2 0' * padding;
1161         }
1162
1163         // hits
1164         weapon_stats = getstati(STAT_DAMAGE_HITS);
1165         weapon_number = weapon_stats & 63;
1166         weapon_hits[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1167         // fired
1168         weapon_stats = getstati(STAT_DAMAGE_FIRED);
1169         weapon_number = weapon_stats & 63;
1170         weapon_fired[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1171
1172         if(cvar_or("hud_weaponicons_fade", 1))
1173         {
1174                 fade = 3.2 - 2 * (time - weapontime);
1175                 fade = bound(0.7, fade, 1);
1176         }
1177         else
1178                 fade = 1;
1179
1180         HUD_WeaponIcons_Clear();
1181
1182         float rows, columns;
1183         rows = mySize_y/mySize_x;
1184         rows = bound(1, floor((sqrt(4 * (2/1) * rows * WEP_COUNT + rows * rows) + rows + 0.5) / 2), WEP_COUNT);
1185         //                               ^^^ weapon icon aspect goes here
1186
1187         columns = ceil(WEP_COUNT/rows);
1188         float row, column;
1189         for(i = 0; i < weapon_cnt; ++i)
1190         {
1191                 self = weaponorder[i];
1192                 if((self.weapons && (self.impulse >= 0) && (stat_weapons & self.weapons)) || hud_configure)
1193                 {
1194                         weapid = self.impulse;
1195
1196                         alpha = (self.weapon == activeweapon) ? 1 : 0.6;
1197
1198                         weapon_hit = weapon_hits[self.weapon-WEP_FIRST];
1199                         weapon_damage = weapon_fired[self.weapon-WEP_FIRST];
1200
1201                         if(self.weapon == activeweapon)
1202                                 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);
1203                         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);
1204
1205                         if(cvar_or("hud_weaponicons_number", 1))
1206                                 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);
1207
1208                         // draw the weapon accuracy on the HUD
1209                         if(hud_accuracy_hud && !(gametype == GAME_RACE || gametype == GAME_CTS))
1210                         {
1211                                 if(weapon_damage)
1212                                         weapon_stats = floor(100 * weapon_hit / weapon_damage);
1213
1214                                 accuracy_color = HUD_AccuracyColor(weapon_stats);
1215                                 if(weapon_damage)
1216                                         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);
1217                         }
1218                 }
1219
1220                 ++row;
1221                 if(row >= rows)
1222                 {
1223                         row = 0;
1224                         ++column;
1225                 }
1226         }
1227
1228 }
1229
1230 // Inventory (#1)
1231 //
1232 float GetAmmoStat(float i)
1233 {
1234         switch(i)
1235         {
1236                 case 0: return STAT_SHELLS;
1237                 case 1: return STAT_NAILS;
1238                 case 2: return STAT_ROCKETS;
1239                 case 3: return STAT_CELLS;
1240                 case 4: return STAT_FUEL;
1241                 default: return -1;
1242         }
1243 }
1244
1245 float GetAmmoItemCode(float i)
1246 {
1247         switch(i)
1248         {
1249                 case 0: return IT_SHELLS;
1250                 case 1: return IT_NAILS;
1251                 case 2: return IT_ROCKETS;
1252                 case 3: return IT_CELLS;
1253                 case 4: return IT_FUEL;
1254                 default: return -1;
1255         }
1256 }
1257
1258 string GetAmmoPicture(float i)
1259 {
1260         switch(i)
1261         {
1262                 case 0: return "ammo_shells";
1263                 case 1: return "ammo_bullets";
1264                 case 2: return "ammo_rocket";
1265                 case 3: return "ammo_cells";
1266                 case 4: return "ammo_fuel";
1267                 default: return "";
1268         }
1269 }
1270
1271 void HUD_Inventory()
1272 {
1273         float id = 1;
1274         float i;
1275         float stat_items;
1276
1277         vector pos, mySize, mysize, mypos;
1278         pos = HUD_Panel_GetPos(id);
1279         mySize = HUD_Panel_GetSize(id);
1280
1281         HUD_Panel_DrawBg(id, pos, mySize);
1282         float padding;
1283         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1284         if(padding)
1285         {
1286                 pos += '1 1 0' * padding;
1287                 mySize -= '2 2 0' * padding;
1288         }
1289
1290         // ammo
1291         stat_items = getstati(STAT_ITEMS);
1292         for (i = 0; i < 4; ++i) {
1293                 float a;
1294                 a = getstati(GetAmmoStat(i)); // how much ammo do we have of type i?
1295                 if(hud_configure)
1296                         a = 100;
1297
1298                 if(cvar("hud_inventory_onlycurrent")) {
1299                         if (stat_items & GetAmmoItemCode(i)) {
1300                                 drawpic_skin(pos, "ammo_current_bg", mySize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1301                                 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);
1302                                 if(a < 10)
1303                                         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);
1304                                 else
1305                                         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);
1306                         }
1307                 } else {
1308                         if (a > 0) {
1309                                 if(mySize_x/mySize_y >= 10) { // arrange horizontally
1310                                         switch (i) {
1311                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1312                                                 case 1: mypos_x = pos_x + 0.25 * mySize_x;      mypos_y = pos_y;                        break; // bullets
1313                                                 case 2: mypos_x = pos_x + 0.5  * mySize_x;      mypos_y = pos_y;                        break; // rockets
1314                                                 case 3: mypos_x = pos_x + 0.75 * mySize_x;      mypos_y = pos_y;                        break; // cells
1315                                         }
1316                                         mysize_x = 0.25 * mySize_x;
1317                                         mysize_y = mySize_y;
1318                                 } else if(mySize_x/mySize_y >= 2.5) { // arrange in a 2x2 grid
1319                                         switch (i) {
1320                                                 case 0: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y + 0.5 * mySize_y;       break; // shells
1321                                                 case 1: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y;                        break; // bullets
1322                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5 * mySize_y;       break; // rockets
1323                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // cells
1324                                         }
1325                                         mysize_x = 0.5 * mySize_x;
1326                                         mysize_y = 0.5 * mySize_y;
1327                                 } else { // arrange vertically
1328                                         switch (i) {
1329                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1330                                                 case 1: mypos_x = pos_x;                        mypos_y = pos_y + 0.25 * mySize_y;      break; // bullets
1331                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5  * mySize_y;      break; // rockets
1332                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y + 0.75 * mySize_y;      break; // cells
1333                                         }
1334                                         mysize_x = mySize_x;
1335                                         mysize_y = 0.25 * mySize_y;
1336                                 }
1337
1338                                 if (stat_items & GetAmmoItemCode(i))
1339                                         drawpic_skin(mypos, "ammo_current_bg", mysize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1340                                 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);
1341                                 if (a < 10) {
1342                                         if(stat_items & GetAmmoItemCode(i))
1343                                                 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);
1344                                         else
1345                                                 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);
1346                                 } else {
1347                                         if(stat_items & GetAmmoItemCode(i))
1348                                                 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);
1349                                         else
1350                                                 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);
1351                                 }
1352                         }
1353                 }
1354         }
1355 }
1356
1357
1358 // Powerups (#2)
1359 //
1360 float shield_maxtime;
1361 float strength_maxtime;
1362 void HUD_Powerups() {
1363         float id = 2;
1364         float stat_items;
1365         stat_items = getstati(STAT_ITEMS);
1366
1367         if(!hud_configure)
1368         {
1369                 if not(stat_items & IT_STRENGTH)
1370                         if not(stat_items & IT_INVINCIBLE)
1371                                 return;
1372
1373                 if (getstati(STAT_HEALTH) <= 0)
1374                         return;
1375         }
1376
1377         vector pos, mySize;
1378         pos = HUD_Panel_GetPos(id);
1379         mySize = HUD_Panel_GetSize(id);
1380
1381         HUD_Panel_DrawBg(id, pos, mySize);
1382         float padding;
1383         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1384         if(padding)
1385         {
1386                 pos += '1 1 0' * padding;
1387                 mySize -= '2 2 0' * padding;
1388         }
1389
1390         float strength_time, shield_time;
1391
1392         strength_time = bound(0, ceil(getstatf(STAT_STRENGTH_FINISHED) - time), 99);
1393         shield_time = bound(0, ceil(getstatf(STAT_INVINCIBLE_FINISHED) - time), 99);
1394
1395         if(hud_configure)
1396         {
1397                 strength_time = 15;
1398                 shield_time = 27;
1399         }
1400
1401         float len;
1402
1403         vector barpos, barsize;
1404         vector picpos;
1405         vector numpos;
1406
1407         string leftname, rightname;
1408         float leftcnt, rightcnt;
1409         float leftalpha, rightalpha;
1410         if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) {
1411                 leftname = "strength";
1412                 leftcnt = strength_time;
1413
1414                 rightname = "shield";
1415                 rightcnt = shield_time;
1416         } else {
1417                 leftname = "shield";
1418                 leftcnt = shield_time;
1419
1420                 rightname = "strength";
1421                 rightcnt = strength_time;
1422         }
1423         leftalpha = bound(0, leftcnt, 1);
1424         rightalpha = bound(0, rightcnt, 1);
1425
1426         // TODO: tile statusbars?
1427         if (mySize_x/mySize_y > 4)
1428         {
1429                 if(leftcnt)
1430                 {
1431                         len = strlen(ftos(leftcnt));
1432
1433                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1434                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/30);
1435                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1436                                 picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
1437                                 numpos = picpos - eX * 2 * 0.5 * mySize_y + eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1438                         } else {
1439                                 barpos = pos;
1440                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1441                                 picpos = pos;
1442                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1443                         }
1444
1445                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1446                         drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1447                         HUD_DrawXNum(numpos, leftcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1448                 }
1449
1450                 if(rightcnt)
1451                 {
1452                         len = strlen(ftos(rightcnt));
1453
1454                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1455                                 barpos = pos + eX * 0.5 * mySize_x;
1456                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1457                                 picpos = pos + eX * 0.5 * mySize_x;
1458                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1459                         } else {
1460                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/30);
1461                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1462                                 picpos = pos + eX * mySize_x - eX * mySize_y;
1463                                 numpos = picpos - eX * mySize_y + eY * 0.25 * mySize_y;
1464                         }
1465
1466                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1467                         drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1468                         HUD_DrawXNum(numpos, rightcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1469                 }
1470         }
1471         else if (mySize_x/mySize_y > 1.5)
1472         {
1473                 if(leftcnt)
1474                 {
1475                         len = strlen(ftos(leftcnt));
1476
1477                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1478                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/30);
1479                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1480                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
1481                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1482                         } else {
1483                                 barpos = pos;
1484                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1485                                 picpos = pos;
1486                                 numpos = picpos + eX * 0.5 * mySize_y;
1487                         }
1488
1489                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1490                         drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1491                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1492                 }
1493
1494                 if(rightcnt)
1495                 {
1496                         len = strlen(ftos(rightcnt));
1497
1498                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1499                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1500                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1501                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
1502                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1503                         } else {
1504                                 barpos = pos + eY * 0.5 * mySize_y;
1505                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1506                                 picpos = pos + eY * 0.5 * mySize_y;
1507                                 numpos = picpos + eX * 0.5 * mySize_y;
1508                         }
1509
1510                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1511                         drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1512                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1513                 }
1514         }
1515         else
1516         {
1517                 if(leftcnt)
1518                 {
1519                         len = strlen(ftos(leftcnt));
1520
1521                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1522                                 barpos = pos;
1523                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1524                                 picpos = pos + eX * 0.05 * mySize_x;
1525                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
1526                         } else {
1527                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/30);
1528                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1529                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x;
1530                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x;
1531                         }
1532
1533                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1534                         drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1535                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1536                 }
1537
1538                 if(rightcnt)
1539                 {
1540                         len = strlen(ftos(rightcnt));
1541
1542                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1543                                 barpos = pos + eX * 0.5 * mySize_x;
1544                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1545                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1546                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1547                         } else {
1548                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/30) + eX * 0.5 * mySize_x;
1549                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1550                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x + eX * 0.5 * mySize_x;
1551                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
1552                         }
1553
1554                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1555                         drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1556                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1557                 }
1558         }
1559
1560         /* TODO: add expand from old code!
1561         //strength
1562         if (strength_time) {
1563                 dt = strength_time - time;
1564                 if(dt > 0)
1565                 {
1566                         if(dt < 5)
1567                         {
1568                                 drawpic_expanding_two(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1569                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1570                         }
1571                         else
1572                         {
1573                                 drawpic_skin(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
1574                         }
1575                         HUD_DrawXNum(pos - '40 -2 0', ceil(dt), 2, 0, countdown_fontsize, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1576                 }
1577                 else if(dt > -1)
1578                 {
1579                         drawpic_expanding(pos, "str", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1580                                 bound(0, -dt / 0.5, 1));
1581                 }
1582         }
1583
1584         //invincibility
1585         if (invincibility_time) {
1586                 dt = invincibility_time - time;
1587                 if(dt > 0)
1588                 {
1589                         if(dt < 5)
1590                         {
1591                                 drawpic_expanding_two(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1592                                         bound(0, (ceil(dt) - dt) / 0.5, 1));
1593                         }
1594                         else
1595                         {
1596                                 drawpic_skin(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE);
1597                         }
1598                         HUD_DrawXNum(pos - '40 -24 0', ceil(dt), 2, 0, countdown_fontsize, '1 1 1', 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
1599                 }
1600                 else if(dt > -1)
1601                 {
1602                         drawpic_expanding(pos - '0 -22 0', "invinc", '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg, DRAWFLAG_ADDITIVE,
1603                                 bound(0, -dt / 0.5, 1));
1604                 }
1605         }
1606         */
1607 }
1608
1609 // Health/armor (#3)
1610 //
1611 void HUD_HealthArmor(void)
1612 {
1613         float id = 3;
1614         vector pos, mySize;
1615         pos = HUD_Panel_GetPos(id);
1616         mySize = HUD_Panel_GetSize(id);
1617
1618         HUD_Panel_DrawBg(id, pos, mySize);
1619         float padding;
1620         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1621         if(padding)
1622         {
1623                 pos += '1 1 0' * padding;
1624                 mySize -= '2 2 0' * padding;
1625         }
1626
1627         float armor, health, x;
1628         armor = getstati(STAT_ARMOR);
1629         health = getstati(STAT_HEALTH);
1630
1631         float fuel;
1632         fuel = getstati(GetAmmoStat(4)); // how much fuel do we have?
1633
1634         if(hud_configure)
1635         {
1636                 armor = 150;
1637                 health = 100;
1638                 fuel = 70;
1639         }
1640
1641         if(health <= 0)
1642                 return;
1643
1644         float len;
1645
1646         // TODO!
1647         /*
1648         if(hud_hudselector == 2) // combined health and armor display
1649         {
1650                 vector v;
1651                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
1652
1653                 vector num_pos;
1654                 num_pos = - '96 28 0';
1655
1656                 x = floor(v_x + 1);
1657
1658                 if(v_z) // fully armored
1659                 {
1660                         // here, armorideal > armor
1661                         drawpic_skin(num_pos + '78 -4.5 0', "health", '32 32 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1662                         drawpic_skin(num_pos + '108 -4.5 0', "armor", '20 20 0', '1 1 1', hud_alpha_fg * armor / v_y, DRAWFLAG_NORMAL);
1663                 }
1664                 else
1665                 {
1666                         drawpic_skin(num_pos + '108 -4.5 0', "health", '20 20 0', '1 1 1', hud_alpha_fg * v_y / armor, DRAWFLAG_NORMAL);
1667                         drawpic_skin(num_pos + '78 -4.5 0', "armor", '32 32 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
1668                 }
1669                 HUD_DrawXNum_Colored(num_pos, x, 3, 24, hud_alpha_fg); // draw the combined health and armor
1670         }
1671
1672         else
1673         {
1674         */
1675         vector barpos, barsize;
1676         vector picpos;
1677         vector numpos;
1678
1679         string leftname, rightname;
1680         float leftcnt, rightcnt;
1681         float leftactive, rightactive;
1682         float leftalpha, rightalpha;
1683         // TODO!
1684         if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) { // old style layout with armor left/top of health
1685                 leftname = "armor";
1686                 leftcnt = armor;
1687                 if(leftcnt)
1688                         leftactive = 1;
1689                 leftalpha = min((armor+10)/55, 1);
1690
1691                 rightname = "health";
1692                 rightcnt = health;
1693                 rightactive = 1;
1694                 rightalpha = 1;
1695         } else {
1696                 leftname = "health";
1697                 leftcnt = health;
1698                 leftactive = 1;
1699                 leftalpha = 1;
1700
1701                 rightname = "armor";
1702                 rightcnt = armor;
1703                 if(rightcnt)
1704                         rightactive = 1;
1705                 rightalpha = min((armor+10)/55, 1);
1706         }
1707
1708         // TODO: tile statusbars?
1709         if (mySize_x/mySize_y > 5)
1710         {
1711                 if(leftactive)
1712                 {
1713                         len = strlen(ftos(leftcnt));
1714
1715                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1716                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/200);
1717                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1718                                 picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
1719                                 numpos = picpos - eX * 3 * 0.5 * mySize_y + eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1720                         } else {
1721                                 barpos = pos;
1722                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1723                                 picpos = pos;
1724                                 numpos = picpos + eX * mySize_y + eY * 0.25 * mySize_y;
1725                         }
1726
1727                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1728                         drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1729                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1730                 }
1731
1732                 if(rightactive)
1733                 {
1734                         len = strlen(ftos(rightcnt));
1735
1736                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1737                                 barpos = pos + eX * 0.5 * mySize_x;
1738                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1739                                 picpos = pos + eX * 0.5 * mySize_x;
1740                                 numpos = picpos + eX * mySize_y - eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1741                         } else {
1742                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/200);
1743                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1744                                 picpos = pos + eX * mySize_x - eX * mySize_y;
1745                                 numpos = picpos - eX * 1.5 * mySize_y + eY * 0.25 * mySize_y;
1746                         }
1747
1748                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1749                         drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1750                         HUD_DrawXNum_Colored(numpos, rightcnt, 3, 0.5 * mySize_y, hud_alpha_fg);
1751                 }
1752
1753                 if(fuel)
1754                         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);
1755         }
1756         else if (mySize_x/mySize_y > 2)
1757         {
1758                 if(leftactive)
1759                 {
1760                         len = strlen(ftos(leftcnt));
1761
1762                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1763                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/200);
1764                                 barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
1765                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
1766                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1767                         } else {
1768                                 barpos = pos;
1769                                 barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
1770                                 picpos = pos;
1771                                 numpos = picpos + eX * 0.5 * mySize_y;
1772                         }
1773
1774                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1775                         drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1776                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1777                 }
1778
1779                 if(rightactive)
1780                 {
1781                         len = strlen(ftos(rightcnt));
1782
1783                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1784                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1785                                 barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1786                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
1787                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1788                         } else {
1789                                 barpos = pos + eY * 0.5 * mySize_y;
1790                                 barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
1791                                 picpos = pos + eY * 0.5 * mySize_y;
1792                                 numpos = picpos + eX * 0.5 * mySize_y;
1793                         }
1794
1795                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1796                         drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1797                         HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.5 * mySize_y, hud_alpha_fg);
1798                 }
1799
1800                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1801                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
1802                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
1803                 } else {
1804                         barpos = pos;
1805                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
1806                 }
1807                 if(fuel)
1808                         drawpic_skin(barpos, "statusbar", barsize, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1809         }
1810         else
1811         {
1812                 if(leftactive)
1813                 {
1814                         len = strlen(ftos(leftcnt));
1815
1816                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1817                                 barpos = pos;
1818                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
1819                                 picpos = pos + eX * 0.05 * mySize_x;
1820                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
1821                         } else {
1822                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/200);
1823                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
1824                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x;
1825                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x;
1826                         }
1827
1828                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1829                         drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1830                         HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.166 * mySize_x, hud_alpha_fg);
1831                 }
1832
1833                 if(rightactive)
1834                 {
1835                         len = strlen(ftos(rightcnt));
1836
1837                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1838                                 barpos = pos + eX * 0.5 * mySize_x;
1839                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
1840                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1841                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1842                         } else {
1843                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/200) + eX * 0.5 * mySize_x;
1844                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
1845                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x + eX * 0.5 * mySize_x;
1846                                 numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x + eX * 0.5 * mySize_x;
1847                         }
1848
1849                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha"), DRAWFLAG_NORMAL);
1850                         drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * hud_alpha_fg, DRAWFLAG_NORMAL);
1851                         HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.166 * mySize_x, hud_alpha_fg);
1852                 }
1853
1854                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1855                         barpos = pos;
1856                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
1857                 } else {
1858                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, fuel/100);
1859                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
1860                 }
1861                 if(fuel)
1862                         drawpic_skin(barpos, "statusbar_vertical", barsize, HUD_Panel_GetProgressBarColor("fuel"), hud_alpha_fg * 0.8, DRAWFLAG_NORMAL);
1863         }
1864 }
1865
1866 // ___TODO___ !!!
1867 // Notification area (#4)
1868 //
1869 void HUD_Notify (void)
1870 {
1871         float id = 4;
1872         vector pos, mySize;
1873         pos = HUD_Panel_GetPos(id);
1874         mySize = HUD_Panel_GetSize(id);
1875
1876         HUD_Panel_DrawBg(id, pos, mySize);
1877         float padding;
1878         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
1879         if(padding)
1880         {
1881                 pos += '1 1 0' * padding;
1882                 mySize -= '2 2 0' * padding;
1883         }
1884
1885         string s;
1886         entity tm;
1887         if(spectatee_status && !intermission)
1888         {
1889                 drawfont = hud_bigfont;
1890                 if(spectatee_status == -1)
1891                         s = "^1Observing";
1892                 else
1893                         s = GetPlayerName(spectatee_status - 1);
1894                 // spectated player name between HUD and chat area, aligned to the left
1895                 pos_x = 0;
1896                 pos_y = - 50 - hud_fontsize_spec_y;
1897                 s = textShortenToWidth(s, vid_conwidth/2.5, hud_fontsize_spec, stringwidth_colors);
1898                 drawcolorcodedstring(pos, s, hud_fontsize_spec, hud_alpha_fg, DRAWFLAG_NORMAL);
1899                 drawfont = hud_font;
1900
1901                 // spectator text in the upper right corner
1902                 if(spectatee_status == -1)
1903                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 to spectate");
1904                 else
1905                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 for another player");
1906
1907                 if(spectatee_status == -1)
1908                         s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
1909                 else
1910                         s = strcat("^1Press ^3", getcommandkey("secondary fire", "+attack2"), "^1 to observe");
1911
1912                 s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
1913
1914                 if(gametype == GAME_ARENA)
1915                         s = "^1Wait for your turn to join";
1916                 else if(gametype == GAME_LMS)
1917                 {
1918                         entity sk;
1919                         sk = playerslots[player_localentnum - 1];
1920                         if(sk.(scores[ps_primary]) >= 666)
1921                                 s = "^1Match has already begun";
1922                         else if(sk.(scores[ps_primary]) > 0)
1923                                 s = "^1You have no more lives left";
1924                         else
1925                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1926                 }
1927                 else
1928                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
1929
1930                 //show restart countdown:
1931                 if (time < getstatf(STAT_GAMESTARTTIME)) {
1932                         float countdown;
1933                         //we need to ceil, otherwise the countdown would be off by .5 when using round()
1934                         countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
1935                         s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
1936                 }
1937         }
1938         if(warmup_stage && !intermission)
1939         {
1940                 s = "^2Currently in ^1warmup^2 stage!";
1941         }
1942
1943         // move more important stuff more to the middle so its more visible
1944
1945         string blinkcolor;
1946         if(mod(time, 1) >= 0.5)
1947                 blinkcolor = "^1";
1948         else
1949                 blinkcolor = "^3";
1950
1951         if(ready_waiting && !intermission && !spectatee_status)
1952         {
1953                 if(ready_waiting_for_me)
1954                 {
1955                         if(warmup_stage)
1956                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
1957                         else
1958                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
1959                 }
1960                 else
1961                 {
1962                         if(warmup_stage)
1963                                 s = strcat("^2Waiting for others to ready up to end warmup...");
1964                         else
1965                                 s = strcat("^2Waiting for others to ready up...");
1966                 }
1967         }
1968         else if(warmup_stage && !intermission && !spectatee_status)
1969         {
1970                 s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
1971         }
1972
1973         if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
1974         {
1975                 float ts_min, ts_max;
1976                 tm = teams.sort_next;
1977                 if (tm)
1978                 {
1979                         for(; tm.sort_next; tm = tm.sort_next)
1980                         {
1981                                 if(!tm.team_size || tm.team == COLOR_SPECTATOR)
1982                                         continue;
1983                                 if(!ts_min) ts_min = tm.team_size;
1984                                 else ts_min = min(ts_min, tm.team_size);
1985                                 if(!ts_max) ts_max = tm.team_size;
1986                                 else ts_max = max(ts_max, tm.team_size);
1987                         }
1988                         if ((ts_max - ts_min) > 1)
1989                         {
1990                                 s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
1991                                 tm = GetTeam(myteam, false);
1992                                 if (tm)
1993                                 if (tm.team != COLOR_SPECTATOR)
1994                                 if (tm.team_size == ts_max)
1995                                         s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
1996
1997                         }
1998                 }
1999         }
2000 }
2001
2002 // Timer (#5)
2003 //
2004 void HUD_Timer()
2005 {
2006         float id = 5;
2007         vector pos, mySize;
2008         pos = HUD_Panel_GetPos(id);
2009         mySize = HUD_Panel_GetSize(id);
2010
2011         HUD_Panel_DrawBg(id, pos, mySize);
2012         float padding;
2013         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2014         if(padding)
2015         {
2016                 pos += '1 1 0' * padding;
2017                 mySize -= '2 2 0' * padding;
2018         }
2019
2020         float timelimit, elapsedTime, minutes, seconds, timeleft, minutesLeft, secondsLeft;
2021
2022         timelimit = getstatf(STAT_TIMELIMIT);
2023
2024         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
2025         timeleft = ceil(timeleft);
2026         minutesLeft = floor(timeleft / 60);
2027         secondsLeft = timeleft - minutesLeft*60;
2028
2029         vector timer_color;
2030         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
2031                 timer_color = '1 1 1'; //white
2032         else if(minutesLeft >= 1)
2033                 timer_color = '1 1 0'; //yellow
2034         else
2035                 timer_color = '1 0 0'; //red
2036
2037         if (cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
2038                 if (time < getstatf(STAT_GAMESTARTTIME)) {
2039                         //while restart is still active, show 00:00
2040                         minutes = seconds = 0;
2041                 } else {
2042                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
2043                         minutes = floor(elapsedTime / 60);
2044                         seconds = elapsedTime - minutes*60;
2045                 }
2046         } else {
2047                 minutes = minutesLeft;
2048                 seconds = secondsLeft;
2049         }
2050
2051         if(minutes > 999)
2052                 seconds = 99;
2053         minutes = min(minutes, 999);
2054         if(minutesLeft >= 1 || cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
2055                 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);
2056                 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);
2057         }
2058         HUD_DrawXNum(pos + eX * mySize_x - eX * 2 * mySize_y, seconds, -2, 0, mySize_y, timer_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2059 }
2060
2061 // Radar (#6)
2062 //
2063 void HUD_Radar(void)
2064 {
2065         float id = 6;
2066         vector pos, mySize;
2067         pos = HUD_Panel_GetPos(id);
2068         mySize = HUD_Panel_GetSize(id);
2069
2070         HUD_Panel_DrawBg(id, pos, mySize);
2071         float padding;
2072         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2073         if(padding)
2074         {
2075                 pos += '1 1 0' * padding;
2076                 mySize -= '2 2 0' * padding;
2077         }
2078
2079         local float color1, color2; // color already declared as a global in hud.qc
2080         local vector rgb;
2081         local entity tm;
2082         float scale2d, normalsize, bigsize;
2083         float f;
2084
2085         teamradar_origin2d = pos + 0.5 * mySize; // TODO: stupid compat, should be removed
2086         teamradar_size2d = mySize;
2087
2088         if(minimapname == "" && !ons_showmap)
2089                 return;
2090
2091         teamradar_loadcvars();
2092
2093         switch(cl_teamradar_zoommode)
2094         {
2095                 default:
2096                 case 0:
2097                         f = current_zoomfraction;
2098                         break;
2099                 case 1:
2100                         f = 1 - current_zoomfraction;
2101                         break;
2102                 case 2:
2103                         f = 0;
2104                         break;
2105                 case 3:
2106                         f = 1;
2107                         break;
2108         }
2109
2110         switch(cl_teamradar_rotation)
2111         {
2112                 case 0:
2113                         teamradar_angle = view_angles_y - 90;
2114                         break;
2115                 default:
2116                         teamradar_angle = 90 * cl_teamradar_rotation;
2117                         break;
2118         }
2119
2120         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
2121         teamradar_size2d = mySize;
2122
2123         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
2124
2125         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
2126         if(cl_teamradar_rotation == 0)
2127         {
2128                 // max-min distance must fit the radar in any rotation
2129                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
2130         }
2131         else
2132         {
2133                 vector c0, c1, c2, c3, span;
2134                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
2135                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
2136                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
2137                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
2138                 span = '0 0 0';
2139                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
2140                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
2141
2142                 // max-min distance must fit the radar in x=x, y=y
2143                 bigsize = min(
2144                         teamradar_size2d_x * scale2d / (1.05 * span_x),
2145                         teamradar_size2d_y * scale2d / (1.05 * span_y)
2146                 );
2147         }
2148
2149         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / cl_teamradar_scale;
2150         if(bigsize > normalsize)
2151                 normalsize = bigsize;
2152
2153         teamradar_size =
2154                   f * bigsize
2155                 + (1 - f) * normalsize;
2156         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
2157                   f * (mi_min + mi_max) * 0.5
2158                 + (1 - f) * view_origin);
2159
2160         color1 = GetPlayerColor(player_localentnum-1);
2161         rgb = GetTeamRGB(color1);
2162
2163         drawsetcliparea(
2164                 pos_x,
2165                 pos_y,
2166                 mySize_x,
2167                 mySize_y
2168         );
2169
2170         draw_teamradar_background(cl_teamradar_background_alpha, cl_teamradar_foreground_alpha);
2171
2172         if(ons_showmap)
2173         {
2174                 drawresetcliparea();
2175
2176                 vector frame_origin, frame_size;
2177                 frame_origin = frame_size = '0 0 0';
2178
2179                 frame_origin_x = pos_x - teamradar_size2d_x * 0.55859375; // matches the picture
2180                 frame_origin_y = pos_y - teamradar_size2d_y * 0.55859375; // matches the picture
2181                 frame_size_x = pos_x * 1.1171875; // matches the picture
2182                 frame_size_y = pos_y * 1.1171875; // matches the picture
2183                 drawpic_skin(frame_origin, "gfx/ons-frame.tga", frame_size, '1 1 1', hud_alpha_fg, 0);
2184                 drawpic_skin(frame_origin, "gfx/ons-frame-team.tga", frame_size, rgb, hud_alpha_fg, 0);
2185
2186                 drawsetcliparea(
2187                         pos_x - teamradar_size2d_x * 0.5,
2188                         pos_y - teamradar_size2d_y * 0.5,
2189                         teamradar_size2d_x,
2190                         teamradar_size2d_y
2191                 );
2192         }
2193
2194         for(tm = world; (tm = find(tm, classname, "radarlink")); )
2195                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
2196         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
2197                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, hud_alpha_fg);
2198         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
2199         {
2200                 color2 = GetPlayerColor(tm.sv_entnum);
2201                 //if(color == COLOR_SPECTATOR || color == color2)
2202                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
2203         }
2204         draw_teamradar_player(view_origin, view_angles, '1 1 1');
2205
2206         drawresetcliparea();
2207 };
2208
2209 todo merge!
2210 void HUD_Mod_Race (void) {
2211         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
2212                 pl = players.sort_next;
2213                 if(pl == me)
2214                         pl = pl.sort_next;
2215                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
2216                         if(pl.scores[ps_primary] == 0)
2217                                 pl = world;
2218
2219                 score = me.(scores[ps_primary]);
2220
2221                 float racemin, racesec, racemsec;
2222                 float distsec, distmsec, minusplus;
2223
2224                 racemin = floor(score/(60 * TIME_FACTOR));
2225                 racesec = floor((score - racemin*(60 * TIME_FACTOR))/TIME_FACTOR);
2226                 racemsec = score - racemin*60*TIME_FACTOR - racesec*TIME_FACTOR;
2227
2228                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
2229                         // distribution display
2230                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2231
2232                         if (distribution < TIME_FACTOR && distribution > -TIME_FACTOR)
2233                                 distmsec = fabs(distribution);
2234                         else {
2235                                 distsec = floor(fabs(distribution)/TIME_FACTOR);
2236                                 distmsec = fabs(distribution) - distsec*TIME_FACTOR;
2237                                 if (distribution < 0)
2238                                         distsec = -distsec;
2239                         }
2240
2241                         if (distribution <= 0) {
2242                                 distribution_color = eY;
2243                                 minusplus = 1; // minusplus 1: always prefix with minus sign
2244                         }
2245                         else {
2246                                 distribution_color = eX;
2247                                 minusplus = 2; // minusplus 1: always prefix with plus sign
2248                         }
2249                         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);
2250                         HUD_DrawXNum(bottomright - '68 48 0' - '16 0 0' * TIME_DECIMALS, distsec, 4, minusplus, 16, distribution_color, 0, 0, hud_alpha_fg, DRAWFLAG_NORMAL);
2251                         drawpic_skin(bottomright - '10 48 0' - '16 0 0' * TIME_DECIMALS, "num_dot", '16 16 0', distribution_color, hud_alpha_fg, DRAWFLAG_ADDITIVE);
2252                 }
2253                 // race record display
2254                 if (distribution <= 0 || distribution == score) // draw the highlight background behind the timer if we have the lead
2255                         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);
2256
2257                 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);
2258                 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);
2259                 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);
2260
2261                 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);
2262                 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);
2263         }
2264 }
2265
2266 // Score (#7)
2267 //
2268 void HUD_Score()
2269 {
2270         float id = 7;
2271         vector pos, mySize;
2272         pos = HUD_Panel_GetPos(id);
2273         mySize = HUD_Panel_GetSize(id);
2274
2275         HUD_Panel_DrawBg(id, pos, mySize);
2276         float padding;
2277         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2278         if(padding)
2279         {
2280                 pos += '1 1 0' * padding;
2281                 mySize -= '2 2 0' * padding;
2282         }
2283
2284         float score, distribution, leader;
2285         float score_len, distr_len;
2286         vector score_pos, secondary_score_pos, distribution_color;
2287         entity tm, pl, me;
2288         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
2289
2290         if (!teamplay) { // non-teamgames
2291                 // me vector := [team/connected frags id]
2292                 pl = players.sort_next;
2293                 if(pl == me)
2294                         pl = pl.sort_next;
2295
2296                 if(hud_configure)
2297                         distribution = 42;
2298                 else if(pl)
2299                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2300                 else
2301                         distribution = 0;
2302
2303                 score = me.(scores[ps_primary]);
2304                 if(hud_configure)
2305                         score = 123;
2306
2307                 if(distribution >= 5) {
2308                         distribution_color = eY;
2309                         leader = 1;
2310                 } else if(distribution >= 0) {
2311                         distribution_color = '1 1 1';
2312                         leader = 1;
2313                 } else if(distribution >= -5)
2314                         distribution_color = '1 1 0';
2315                 else
2316                         distribution_color = eX;
2317
2318                 score_len = strlen(ftos(score));
2319                 distr_len = strlen(ftos(distribution));
2320
2321                 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);
2322                 if (leader)
2323                         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);
2324                 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);
2325         } else { // teamgames
2326                 float max_fragcount;
2327                 max_fragcount = -99;
2328
2329                 float teamnum;
2330                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2331                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !hud_configure)) // no players? don't display
2332                                 continue;
2333                         score = tm.(teamscores[ts_primary]);
2334                         if(hud_configure)
2335                                 score = 123;
2336                         leader = 0;
2337                         
2338                         score_len = strlen(ftos(score));
2339
2340                         if (score > max_fragcount)
2341                                 max_fragcount = score;
2342
2343                         if(tm.team == myteam) {
2344                                 if (max_fragcount == score)
2345                                         leader = 1;
2346                                 if (leader)
2347                                         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);
2348                                 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);
2349                         } else {
2350                                 if (max_fragcount == score)
2351                                         leader = 1;
2352                                 if (leader)
2353                                         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);
2354                                 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);
2355                                 teamnum += 1;
2356                         }
2357                 }
2358         }
2359 }
2360
2361 // Race timer (#8)
2362 //
2363 void HUD_RaceTimer (void) {
2364         float id = 8;
2365         vector pos, mySize;
2366         pos = HUD_Panel_GetPos(id);
2367         mySize = HUD_Panel_GetSize(id);
2368
2369         HUD_Panel_DrawBg(id, pos, mySize);
2370         float padding;
2371         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2372         if(padding)
2373         {
2374                 pos += '1 1 0' * padding;
2375                 mySize -= '2 2 0' * padding;
2376         }
2377
2378         drawfont = hud_bigfont;
2379         float a, t;
2380         string s, forcetime;
2381
2382         if(hud_configure)
2383         {
2384                 s = "0:13:37";
2385                 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);
2386                 s = "^1Intermediate 1 (+15.42)";
2387                 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);
2388         }
2389         else if(race_checkpointtime)
2390         {
2391                 a = bound(0, 2 - (time - race_checkpointtime), 1);
2392                 s = "";
2393                 forcetime = "";
2394                 if(a > 0) // just hit a checkpoint?
2395                 {
2396                         if(race_checkpoint != 254)
2397                         {
2398                                 if(race_time && race_previousbesttime)
2399                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
2400                                 else
2401                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
2402                                 if(race_time)
2403                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
2404                         }
2405                 }
2406                 else
2407                 {
2408                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
2409                         {
2410                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
2411                                 if(a > 0) // next one?
2412                                 {
2413                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
2414                                 }
2415                         }
2416                 }
2417
2418                 if(s != "" && a > 0)
2419                 {
2420                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2421                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2422                         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);
2423                 }
2424
2425                 // ___ TODO!!!___
2426                 if(race_penaltytime)
2427                 {
2428                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
2429                         if(a > 0)
2430                         {
2431                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
2432                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2433                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2434                                 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);
2435                         }
2436                 }
2437
2438                 if(forcetime != "")
2439                 {
2440                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
2441                         //drawstring_expanding(m - '16 0 0' * stringwidth(forcetime, FALSE), forcetime, '32 32 0', '1 1 1', hud_alpha_fg, 0, a);
2442                         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);
2443                 }
2444                 else
2445                         a = 1;
2446
2447                 if(race_laptime && race_checkpoint != 255)
2448                 {
2449                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
2450                         //drawstring(m - '16 0 0' * stringwidth(s, FALSE), s, '32 32 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2451                         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);
2452                 }
2453         }
2454         else
2455         {
2456                 if(race_mycheckpointtime)
2457                 {
2458                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
2459                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
2460                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2461                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2462                         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);
2463                 }
2464                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
2465                 {
2466                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
2467                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
2468                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2469                         //drawcolorcodedstring(m - '0 0 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2470                         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);
2471                 }
2472
2473                 if(race_penaltytime && !race_penaltyaccumulator)
2474                 {
2475                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
2476                         a = bound(0, (1 + t - time), 1);
2477                         if(a > 0)
2478                         {
2479                                 if(time < t)
2480                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
2481                                 else
2482                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
2483                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
2484                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2485                                 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);
2486                         }
2487                 }
2488         }
2489
2490         drawfont = hud_font;
2491 }
2492
2493 // Vote window (#9)
2494 //
2495 float vote_yescount;
2496 float vote_nocount;
2497 float vote_needed;
2498 float vote_highlighted; // currently selected vote
2499
2500 float vote_active; // is there an active vote?
2501 float vote_prev; // previous state of vote_active to check for a change
2502 float vote_alpha;
2503 float vote_change; // "time" when vote_active changed
2504
2505 void HUD_VoteWindow(void) 
2506 {
2507         float id = 9;
2508         vector pos, mySize;
2509         pos = HUD_Panel_GetPos(id);
2510         mySize = HUD_Panel_GetSize(id);
2511
2512         string s;
2513         float a;
2514         if(vote_active != vote_prev) {
2515                 vote_change = time;
2516                 vote_prev = bound(0, vote_active, 1);
2517         }
2518
2519         if(vote_active)
2520                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
2521         else
2522                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
2523
2524         if(hud_configure)
2525         {
2526                 vote_alpha = 1;
2527                 vote_yescount = 3;
2528                 vote_nocount = 2;
2529                 vote_needed = 4;
2530         }
2531
2532         HUD_Panel_DrawBg(id, pos, mySize);
2533         float padding;
2534         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
2535         if(padding)
2536         {
2537                 pos += '1 1 0' * padding;
2538                 mySize -= '2 2 0' * padding;
2539         }
2540
2541         if(vote_alpha) {
2542                 a = vote_alpha * bound(cvar_or("hud_vote_alreadyvoted_alpha", 0.75), 1 - vote_highlighted, 1);
2543
2544                 drawpic_skin(pos, "voteprogress_back", mySize, HUD_GetBgColor(), a * hud_alpha_bg, DRAWFLAG_NORMAL);
2545
2546                 s = "A vote has been called for: ";
2547                 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);
2548                 s = textShortenToWidth(vote_called_vote, mySize_x * 0.96, '10 0 0', stringwidth_colors);
2549                 if(hud_configure)
2550                         s = "Configure the HUD";
2551                 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);
2552
2553                 // print the yes/no counts
2554                 s = strcat("Yes: ", ftos(vote_yescount));
2555                 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);
2556                 s = strcat("No: ", ftos(vote_nocount));
2557                 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);
2558
2559                 // draw the progress bars
2560                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
2561                 drawpic_skin(pos, "voteprogress_prog", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2562
2563                 drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
2564                 drawpic_skin(pos, "voteprogress_prog", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2565
2566                 // draw the highlights
2567                 if(vote_highlighted == 1) {
2568                         drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
2569                         drawpic_skin(pos, "voteprogress_voted", mySize, eY, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2570                 }
2571                 else if(vote_highlighted == 2) {
2572                         drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
2573                         drawpic_skin(pos, "voteprogress_voted", mySize, eX, a * hud_alpha_fg, DRAWFLAG_NORMAL);
2574                 }
2575
2576                 drawresetcliparea();
2577         }
2578         else if(!vote_active) {
2579                 vote_highlighted = 0;
2580         }
2581 }
2582
2583 // Mod icons panel (#10)
2584 //
2585
2586 // CTF HUD modicon section
2587 float redflag_prevframe, blueflag_prevframe; // status during previous frame
2588 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
2589 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
2590
2591 void HUD_Mod_CTF_Reset(void)
2592 {
2593         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
2594 }
2595
2596 void HUD_Mod_CTF(vector pos, vector mySize)
2597 {
2598         vector redflag_pos, blueflag_pos;
2599         float f; // every function should have that
2600
2601         float redflag, blueflag; // current status
2602         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
2603         float stat_items;
2604
2605         stat_items = getstati(STAT_ITEMS);
2606         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
2607         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
2608
2609         // when status CHANGES, set old status into prevstatus and current status into status
2610         if (redflag != redflag_prevframe)
2611         {
2612                 redflag_statuschange_time = time;
2613                 redflag_prevstatus = redflag_prevframe;
2614                 redflag_prevframe = redflag;
2615         }
2616
2617         if (blueflag != blueflag_prevframe)
2618         {
2619                 blueflag_statuschange_time = time;
2620                 blueflag_prevstatus = blueflag_prevframe;
2621                 blueflag_prevframe = blueflag;
2622         }
2623
2624         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
2625         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
2626
2627         float BLINK_FACTOR = 0.15;
2628         float BLINK_BASE = 0.85;
2629         // note:
2630         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
2631         // thus
2632         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
2633         // ensure RMS == 1
2634         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
2635
2636         string red_icon, red_icon_prevstatus;
2637         float red_alpha, red_alpha_prevstatus;
2638         red_alpha = red_alpha_prevstatus = 1;
2639         switch(redflag) {
2640                 case 1: red_icon = "flag_red_taken"; break;
2641                 case 2: red_icon = "flag_red_lost"; break;
2642                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2643                 default:
2644                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2645                                 red_icon = "flag_red_shielded";
2646                         else
2647                                 red_icon = string_null;
2648                         break;
2649         }
2650         switch(redflag_prevstatus) {
2651                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
2652                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
2653                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2654                 default:
2655                         if(redflag == 3)
2656                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
2657                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
2658                                 red_icon_prevstatus = "flag_red_shielded";
2659                         else
2660                                 red_icon_prevstatus = string_null;
2661                         break;
2662         }
2663
2664         string blue_icon, blue_icon_prevstatus;
2665         float blue_alpha, blue_alpha_prevstatus;
2666         blue_alpha = blue_alpha_prevstatus = 1;
2667         switch(blueflag) {
2668                 case 1: blue_icon = "flag_blue_taken"; break;
2669                 case 2: blue_icon = "flag_blue_lost"; break;
2670                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2671                 default:
2672                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2673                                 blue_icon = "flag_blue_shielded";
2674                         else
2675                                 blue_icon = string_null;
2676                         break;
2677         }
2678         switch(blueflag_prevstatus) {
2679                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
2680                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
2681                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2682                 default:
2683                         if(blueflag == 3)
2684                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
2685                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
2686                                 blue_icon_prevstatus = "flag_blue_shielded";
2687                         else
2688                                 blue_icon_prevstatus = string_null;
2689                         break;
2690         }
2691
2692         if (myteam == COLOR_TEAM1) { // always draw own flag on left
2693                 redflag_pos = pos;
2694                 blueflag_pos = pos + eX * mySize_y;
2695         } else {
2696                 blueflag_pos = pos;
2697                 redflag_pos = pos + eX * mySize_y;
2698         }
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, '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2703         if(red_icon)
2704                 drawpic_skin(redflag_pos, red_icon, '1 1 0' * mySize_y, '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, '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
2709         if(blue_icon)
2710                 drawpic_skin(blueflag_pos, blue_icon, '1 1 0' * mySize_y, '1 1 1', hud_alpha_fg * blue_alpha * f, DRAWFLAG_NORMAL);
2711 }
2712
2713 // Keyhunt HUD modicon section
2714 float kh_runheretime;
2715
2716 void HUD_Mod_KH_Reset(void)
2717 {
2718         kh_runheretime = 0;
2719 }
2720
2721 void HUD_Mod_KH(void)
2722 {
2723         float kh_keys;
2724         float keyteam;
2725         float a, aa;
2726         vector p, pa, kh_size, kh_asize;
2727
2728         p_x = 6;
2729         p_y = vid_conheight - 34 - 3;
2730         p_z = 0;
2731
2732         kh_keys = getstati(STAT_KH_KEYS);
2733
2734         kh_size = '19 34 0';
2735         kh_asize = '19 10 0';
2736         pa = p + '0 -10 0';
2737
2738         float i, key;
2739
2740         float keycount;
2741         keycount = 0;
2742         for(i = 0; i < 4; ++i)
2743         {
2744                 key = floor(kh_keys / pow(32, i)) & 31;
2745                 keyteam = key - 1;
2746                 if(keyteam == 30 && keycount <= 4)
2747                         keycount += 4;
2748                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
2749                         keycount += 1;
2750         }
2751         // this yields 8 exactly if "RUN HERE" shows
2752
2753         if(keycount == 8)
2754         {
2755                 if(!kh_runheretime)
2756                         kh_runheretime = time;
2757                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
2758         }
2759         else
2760                 kh_runheretime = 0;
2761
2762         for(i = 0; i < 4; ++i)
2763         {
2764                 key = floor(kh_keys / pow(32, i)) & 31;
2765                 keyteam = key - 1;
2766                 switch(keyteam)
2767                 {
2768                         case 30: // my key
2769                                 keyteam = myteam;
2770                                 a = 1;
2771                                 aa = 1;
2772                                 break;
2773                         case -1: // no key
2774                                 a = 0;
2775                                 aa = 0;
2776                                 break;
2777                         default: // owned or dropped
2778                                 a = 0.2;
2779                                 aa = 0.5;
2780                                 break;
2781                 }
2782                 a = a * hud_alpha_fg;
2783                 aa = aa * hud_alpha_fg;
2784                 if(a > 0)
2785                 {
2786                         switch(keyteam)
2787                         {
2788                                 case COLOR_TEAM1:
2789                                         drawpic (pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2790                                         break;
2791                                 case COLOR_TEAM2:
2792                                         drawpic (pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2793                                         break;
2794                                 case COLOR_TEAM3:
2795                                         drawpic (pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2796                                         break;
2797                                 case COLOR_TEAM4:
2798                                         drawpic (pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
2799                                         break;
2800                                 default:
2801                                         break;
2802                         }
2803                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
2804                         {
2805                                 case 0:
2806                                         drawpic (p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2807                                         break;
2808                                 case 1:
2809                                         drawpic (p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2810                                         break;
2811                                 case 2:
2812                                         drawpic (p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2813                                         break;
2814                                 case 3:
2815                                         drawpic (p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
2816                                         break;
2817                         }
2818                 }
2819                 p_x += 24;
2820                 pa_x += 24;
2821         }
2822 }
2823
2824 // Nexball HUD modicon section
2825 #define NBPB_SIZE '96 38 0'
2826 #define NBPB_BT 2                   //thickness
2827 #define NBPB_BRGB '1 1 1'
2828 #define NBPB_BALPH 1                //alpha
2829 #define NBPB_BFLAG DRAWFLAG_NORMAL
2830 #define NBPB_IALPH 0.4
2831 #define NBPB_IFLAG DRAWFLAG_NORMAL
2832 #define NBPB_IRGB '0.7 0.1 0'
2833
2834 void HUD_Mod_NexBall(void)
2835 {
2836         float stat_items, nb_pb_starttime, dt, p;
2837         vector pos;
2838
2839         stat_items = getstati(STAT_ITEMS);
2840         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
2841
2842         pos_x = 4;
2843         pos_y = vid_conheight - 42;
2844         pos_z = 0;
2845
2846         //Manage the progress bar if any
2847         if (nb_pb_starttime > 0)
2848         {
2849                 vector s;
2850                 dt = mod(time - nb_pb_starttime, nb_pb_period);
2851                 // one period of positive triangle
2852                 p = 2 * dt / nb_pb_period;
2853                 if (p > 1)
2854                         p = 2 - p;
2855
2856                 s = NBPB_SIZE;
2857                 //Draw the filling
2858                 drawfill(pos, p * s_x * eX + s_y * eY, NBPB_IRGB, NBPB_IALPH, NBPB_IFLAG);
2859
2860                 //Draw the box
2861                 s = NBPB_SIZE;
2862                 drawline(NBPB_BT, pos    , pos + eX * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2863                 drawline(NBPB_BT, pos    , pos + eY * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2864                 drawline(NBPB_BT, pos + s, pos + eX * s_x, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2865                 drawline(NBPB_BT, pos + s, pos + eY * s_y, NBPB_BRGB, NBPB_BALPH, NBPB_BFLAG);
2866         }
2867
2868         pos_x += 12; //horizontal margin to the picture
2869         pos_y += 2; //vertical margin to the picture
2870
2871         if (stat_items & IT_KEY1)
2872                 drawpic_skin(pos, "nexball_carrying", '80 34 0', '1 1 1', 1, DRAWFLAG_NORMAL);
2873 }
2874
2875 float race_status_time;
2876 float race_status_prev;
2877 string race_status_name_prev;
2878 void HUD_DrawRaceStatus(vector pos)
2879 {
2880         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
2881                 race_status_time = time + 5;
2882                 race_status_prev = race_status;
2883                 if (race_status_name_prev)
2884                         strunzone(race_status_name_prev);
2885                 race_status_name_prev = strzone(race_status_name);
2886         }
2887
2888         float a;
2889         a = bound(0, race_status_time - time, 1);
2890
2891         string s;
2892         s = textShortenToWidth(race_status_name, 120, '10 10 0', stringwidth_colors);
2893
2894         float rank;
2895         if(race_status > 0)
2896                 rank = race_CheckName(race_status_name);
2897         string rankname;
2898         rankname = race_PlaceName(rank);
2899
2900         if(race_status == 0)
2901                 drawpic_skin(pos, "race_newfail", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2902         else if(race_status == 1) {
2903                 drawpic_skin(pos, "race_newtime", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2904                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2905                 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);
2906         } else if(race_status == 2) {
2907                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
2908                         drawpic_skin(pos, "race_newrankgreen", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2909                 else
2910                         drawpic_skin(pos, "race_newrankyellow", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2911                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2912                 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);
2913         } else if(race_status == 3) {
2914                 drawpic_skin(pos, "race_newrecordserver", '80 80 0', '1 1 1', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2915                 drawcolorcodedstring(pos + '40 80 0' - eX * stringwidth(s, TRUE, '5 0 0'), s, '10 10 0', hud_alpha_fg * a, DRAWFLAG_NORMAL);
2916                 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);
2917         }
2918
2919         if (race_status_time - time <= 0) {
2920                 race_status_prev = -1;
2921                 race_status = -1;
2922                 if(race_status_name)
2923                         strunzone(race_status_name);
2924                 race_status_name = string_null;
2925                 if(race_status_name_prev)
2926                         strunzone(race_status_name_prev);
2927                 race_status_name_prev = string_null;
2928         }
2929 }
2930
2931 foo
2932 // merge with above
2933 // Race/CTS HUD modicon section
2934 float crecordtime_prev; // last remembered crecordtime
2935 float crecordtime_change_time; // time when crecordtime last changed
2936 float srecordtime_prev; // last remembered srecordtime
2937 float srecordtime_change_time; // time when srecordtime last changed
2938 void CSQC_race_hud(void)
2939 {
2940         entity me;
2941         me = playerslots[player_localentnum - 1];
2942         float t, score;
2943         float f; // yet another function has this
2944         score = me.(scores[ps_primary]);
2945
2946         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
2947                 return; // no records in the actual race
2948
2949         drawfont = hud_bigfont;
2950         vector pos;
2951         pos_x = 2;
2952         pos_y = vid_conheight - 48;
2953
2954         // clientside personal record
2955         string rr;
2956         if(gametype == GAME_CTS)
2957                 rr = CTS_RECORD;
2958         else
2959                 rr = RACE_RECORD;
2960         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
2961
2962         if(score && (score < t || !t)) {
2963                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
2964                 if(cvar("cl_autodemo_delete_keeprecords"))
2965                 {
2966                         f = cvar("cl_autodemo_delete");
2967                         f &~= 1;
2968                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
2969                 }
2970         }
2971
2972         if(t != crecordtime_prev) {
2973                 crecordtime_prev = t;
2974                 crecordtime_change_time = time;
2975         }
2976         f = time - crecordtime_change_time;
2977
2978         if (f > 1) {
2979                 drawstring(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2980                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2981         } else {
2982                 drawstring(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2983                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2984                 drawstring_expanding(pos, "Personal best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2985                 drawstring_expanding(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
2986         }
2987
2988         // server record
2989         pos_y += 26;
2990         t = race_server_record;
2991         if(t != srecordtime_prev) {
2992                 srecordtime_prev = t;
2993                 srecordtime_change_time = time;
2994         }
2995         f = time - srecordtime_change_time;
2996
2997         if (f > 1) {
2998                 drawstring(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
2999                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3000         } else {
3001                 drawstring(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3002                 drawstring(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3003                 drawstring_expanding(pos, "Server best ", '10 10 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
3004                 drawstring_expanding(pos + '0 10 0', TIME_ENCODED_TOSTRING(t),'14 14 0', '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL, f);
3005         }
3006         drawfont = hud_font;
3007 }
3008
3009 void HUD_ModIcons(void)
3010 {
3011         float id = 10;
3012         vector pos, mySize;
3013         pos = HUD_Panel_GetPos(id);
3014         mySize = HUD_Panel_GetSize(id);
3015
3016         HUD_Panel_DrawBg(id, pos, mySize);
3017         float padding;
3018         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
3019         if(padding)
3020         {
3021                 pos += '1 1 0' * padding;
3022                 mySize -= '2 2 0' * padding;
3023         }
3024
3025         // TODO... well make them work in a panel etc
3026         if(gametype == GAME_KEYHUNT)
3027                 HUD_Mod_KH();
3028         else if(gametype == GAME_CTF)
3029                 HUD_Mod_CTF(pos, mySize);
3030         else if(gametype == GAME_NEXBALL)
3031                 HUD_Mod_NexBall();
3032         else if(gametype == GAME_CTS || gametype == GAME_RACE)
3033                 HUD_Mod_Race();
3034 }
3035
3036 // Draw pressed keys (#11)
3037 //
3038 void HUD_DrawPressedKeys(void)
3039 {
3040         float id = 11;
3041         vector pos, mySize;
3042         pos = HUD_Panel_GetPos(id);
3043         mySize = HUD_Panel_GetSize(id);
3044
3045         HUD_Panel_DrawBg(id, pos, mySize);
3046         float padding;
3047         padding = cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
3048         if(padding)
3049         {
3050                 pos += '1 1 0' * padding;
3051                 mySize -= '2 2 0' * padding;
3052         }
3053
3054         float pressedkeys;
3055
3056         pressedkeys = getstatf(STAT_PRESSED_KEYS);
3057         drawpic_skin(pos, "key_bg.tga",           mySize, '1 1 1', 0.1 * hud_alpha_fg, DRAWFLAG_NORMAL);
3058         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);
3059         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);
3060         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);
3061         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);
3062         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);
3063         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);
3064 }
3065
3066 /*
3067 ==================
3068 Main HUD system
3069 ==================
3070 */
3071
3072 void HUD_ShowSpeed(void)
3073 {
3074         vector numsize;
3075         float pos, conversion_factor;
3076         string speed, zspeed, unit;
3077
3078         switch(cvar("cl_showspeed_unit"))
3079         {
3080                 default:
3081                 case 0:
3082                         unit = "";
3083                         conversion_factor = 1.0;
3084                         break;
3085                 case 1:
3086                         unit = " qu/s";
3087                         conversion_factor = 1.0;
3088                         break;
3089                 case 2:
3090                         unit = " m/s";
3091                         conversion_factor = 0.0254;
3092                         break;
3093                 case 3:
3094                         unit = " km/h";
3095                         conversion_factor = 0.0254 * 3.6;
3096                         break;
3097                 case 4:
3098                         unit = " mph";
3099                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
3100                         break;
3101                 case 5:
3102                         unit = " knots";
3103                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
3104                         break;
3105         }
3106
3107         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
3108
3109         numsize_x = numsize_y = cvar("cl_showspeed_size");
3110         pos = (vid_conheight - numsize_y) * cvar("cl_showspeed_position");
3111
3112         drawfont = hud_bigfont;
3113         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3114
3115         if (cvar("cl_showspeed_z") == 1) {
3116                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
3117                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', hud_alpha_fg, DRAWFLAG_NORMAL);
3118         }
3119
3120         drawfont = hud_font;
3121 }
3122
3123 vector acc_prevspeed;
3124 float acc_prevtime;
3125 float acc_avg;
3126
3127 void HUD_ShowAcceleration(void)
3128 {
3129         float acceleration, sz, scale, alpha, f;
3130         vector pos, top, rgb;
3131         top_x = vid_conwidth/2;
3132         top_y = 0;
3133
3134         f = time - acc_prevtime;
3135         if(cvar("cl_showacceleration_z"))
3136                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
3137         else
3138                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
3139         acc_prevspeed = pmove_vel;
3140         acc_prevtime = time;
3141
3142         f = bound(0, f * 10, 1);
3143         acc_avg = acc_avg * (1 - f) + acceleration * f;
3144         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
3145
3146         pos = top - sz/2 * eY + (cvar("cl_showacceleration_position") * vid_conheight) * eY;
3147
3148         sz = cvar("cl_showacceleration_size");
3149         scale = cvar("cl_showacceleration_scale");
3150         alpha = cvar("cl_showacceleration_alpha");
3151         if (cvar("cl_showacceleration_color_custom"))
3152                 rgb = stov(cvar_string("cl_showacceleration_color"));
3153         else {
3154                 rgb = '1 1 1';
3155                 if (acceleration < 0) {
3156                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
3157                 } else if (acceleration > 0) {
3158                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
3159                 }
3160         }
3161
3162         if (acceleration > 0)
3163                 drawpic_skin(pos, "statusbar", acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3164         else if (acceleration < 0)
3165                 drawpic_skin(pos + acceleration * scale * '40 0 0', "statusbar", -acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_alpha_fg, DRAWFLAG_NORMAL);
3166 }
3167
3168 void HUD_Reset (void)
3169 {
3170         // reset gametype specific icons
3171         if(gametype == GAME_KEYHUNT)
3172                 HUD_Mod_KH_Reset();
3173         else if(gametype == GAME_CTF)
3174                 HUD_Mod_CTF_Reset();
3175 }
3176
3177 void HUD_Main (void)
3178 {
3179         hud_alpha_bg = cvar_or("hud_bg_alpha", 0.8) * (1 - cvar("_menu_alpha"));
3180         hud_border_thickness = bound(0, cvar("hud_border_thickness"), 5);
3181         hud_accuracy_border_thickness = bound(0, cvar_or("hud_accuracy_border_thickness", 1), 5);
3182         hud_color_bg_team = cvar("hud_color_bg_team");
3183
3184         hud_fontsize = HUD_GetFontsize("hud_fontsize");
3185         hud_fontsize_spec = HUD_GetFontsize("hud_fontsize_spec");
3186
3187         hud_configure = cvar("_hud_configure");
3188
3189         // Drawing stuff
3190         if(cvar("hud_dock"))
3191                 drawpic_skin('0 0 0', "dock", eX * vid_conwidth + eY * vid_conheight, stov(cvar_string("hud_dock_color")), cvar("hud_dock_alpha"), DRAWFLAG_NORMAL);
3192
3193         if(HUD_Panel_CheckActive(0))
3194                 HUD_WeaponIcons();
3195         if(HUD_Panel_CheckActive(1))
3196                 HUD_Inventory();
3197         if(HUD_Panel_CheckActive(2))
3198                 HUD_Powerups();
3199         if(HUD_Panel_CheckActive(3))
3200                 HUD_HealthArmor();
3201         if(HUD_Panel_CheckActive(4))
3202                 HUD_Notify();
3203         if(HUD_Panel_CheckActive(5))
3204                 HUD_Timer();
3205         // TODO hud'ify
3206         if(HUD_Panel_CheckActive(6))
3207                 if(ons_showmap || cvar_string("cl_teamradar") != "0" && (cvar("cl_teamradar") == 2 || teamplay))
3208                         HUD_Radar();
3209         if(HUD_Panel_CheckActive(7))
3210                 HUD_Score();
3211         if(HUD_Panel_CheckActive(8))
3212                 if(gametype == GAME_RACE || gametype == GAME_CTS || hud_configure)
3213                         HUD_RaceTimer();
3214         if(HUD_Panel_CheckActive(9))
3215                 HUD_VoteWindow();
3216         if(HUD_Panel_CheckActive(9))
3217                 HUD_ModIcons();
3218         // TODO hud'ify
3219         if(HUD_Panel_CheckActive(11))
3220                 if(spectatee_status > 0 || cvar("cl_showpressedkeys") >= 2 || hud_configure)
3221                         HUD_DrawPressedKeys();
3222
3223         // TODO hud_'ify these
3224         if (cvar("cl_showspeed"))
3225                 HUD_ShowSpeed();
3226         if (cvar("cl_showacceleration"))
3227                 HUD_ShowAcceleration();
3228 }