]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
no aspect ratio forcing on dock
[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 // drawpic wrapper to draw an image as large as possible with preserved aspect ratio into a box
79 void drawpic_aspect(vector pos, string pic, vector sz, vector color, float alpha, float drawflag) {
80         vector imgsize;
81         imgsize = drawgetimagesize(pic);
82         float imgaspect;
83         imgaspect = imgsize_x/imgsize_y;
84
85         vector oldsz;
86         oldsz = sz;
87         float aspect;
88         aspect = sz_x/sz_y;
89
90         if(aspect > imgaspect) {
91                 sz_x = sz_y * imgaspect;
92                 drawpic(pos + eX * (oldsz_x - sz_x) * 0.5, pic, sz, color, alpha, drawflag);
93         } else {
94                 sz_y = sz_x / imgaspect; 
95                 drawpic(pos + eY * (oldsz_y - sz_y) * 0.5, pic, sz, color, alpha, drawflag);
96         }
97 }
98
99 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
100 void drawpic_skin(vector pos, string pic, vector sz, vector color, float alpha, float drawflag) {
101         drawpic_aspect(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, color, alpha, drawflag);
102 }
103
104 void drawpic_skin_expanding(vector pos, string pic, vector sz, vector rgb, float alpha, float flag, float fadelerp) {
105         drawpic_expanding(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, rgb, alpha, flag, fadelerp);
106 }
107
108 void drawpic_skin_expanding_two(vector pos, string pic, vector sz, vector rgb, float alpha, float flag, float fadelerp) {
109         drawpic_expanding_two(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, rgb, alpha, flag, fadelerp);
110 }
111
112 // return HUD background color
113 vector HUD_GetBgColor()
114 {
115         vector color;
116         if (teamplay)
117                 GetTeamRGB(myteam) * hud_color_bg_team;
118         else {
119                 // allow custom HUD colors in non-teamgames
120                 color_x = cvar("hud_color_bg_r");
121                 color_y = cvar("hud_color_bg_g");
122                 color_z = cvar("hud_color_bg_b");
123         }
124         return color;
125 }
126
127 // return accuracy text color
128 vector HUD_AccuracyColor(float accuracy)
129 {
130         vector rgb;
131         float yellow_accuracy = cvar("hud_weaponicons_accuracy_yellow"); // value at which this function returns yellow
132         if(accuracy >= 100) {
133                 rgb_x = 0;
134                 rgb_y = 1;
135         }
136         else if(accuracy > yellow_accuracy) {
137                 rgb_x = 1 - (accuracy-yellow_accuracy)/(100-yellow_accuracy); // red value between 1 -> 0
138                 rgb_y = 1;
139         }
140         else {
141                 rgb_x = 1;
142                 rgb_y = accuracy/yellow_accuracy; // green value between 0 -> 1
143         }
144         rgb_z = 0;
145         return rgb;
146 }
147
148 // draw number in the XSCALE font
149 void HUD_DrawXNum (vector pos, float num, float digits, float showsign, float lettersize, vector rgb, float highlighted, float stroke, float alpha, float dflags)
150 {
151         float l, i;
152         string str, tmp, l_length;
153         float minus, plus;
154         vector vsize, num_color;
155
156         vsize_x = vsize_y = lettersize;
157         vsize_z = 0;
158
159         // showsign 1: always prefix with minus sign (useful in race distribution display)
160         // showsign 2: always prefix with plus sign (useful in race distribution display)
161         // showsign 3: prefix with minus sign if negative, plus sign if positive (useful in score distribution display)
162
163         if((showsign == 2 && num >= 0) || (num > 0 && showsign == 3))
164         {
165                 plus = true;
166                 pos_x -= lettersize;
167         } else
168                 plus = false;
169
170         if(num < 0 || (num < 0 && showsign == 3) || (showsign == 1 && num <= 0))
171         {
172                 minus = true;
173                 num = -num;
174                 pos_x -= lettersize;
175         } else
176                 minus = false;
177
178         if(digits < 0)
179         {
180                 tmp = ftos(num);
181                 digits = -digits;
182                 str = strcat(substring("0000000000", 0, digits - strlen(tmp)), tmp);
183         } else
184                 str = ftos(num);
185
186         l = strlen(str);
187         l_length = ftos(l);
188
189         if(l > digits)
190         {
191                 str = substring(str, l-digits, 999);
192                 l = strlen(str);
193         } else if(l < digits)
194                 pos_x += (digits-l) * lettersize;
195
196         if (highlighted == 1) {
197                 vector hl_size;
198                 hl_size_x = vsize_x * l + vsize_x * 0.2;
199                 hl_size_y = vsize_y * 1.1;
200                 hl_size_z = 0;
201                 if(minus)
202                         hl_size_x = hl_size_x + vsize_x;
203
204                 vector hl_pos;
205                 hl_pos_x = pos_x - lettersize/10;
206                 hl_pos_y = pos_y - lettersize/20;
207                 hl_pos_z = 0;
208
209                 drawpic_skin(hl_pos, strcat("num_leading_", l_length), hl_size, '1 1 1', alpha, dflags);
210         }
211
212         if (stroke == 1)
213                 num_color = '1 1 1';
214         else
215                 num_color = rgb;
216
217         if(minus)
218         {
219                 if (stroke == 1)
220                         drawpic_skin(pos, "num_minus_stroke", vsize, rgb, alpha, dflags);
221                 drawpic_skin(pos, "num_minus", vsize, num_color, alpha, dflags);
222                 pos_x += lettersize;
223         } else if(plus)
224         {
225                 if (stroke == 1)
226                         drawpic_skin(pos, "num_plus_stroke", vsize, rgb, alpha, dflags);
227                 drawpic_skin(pos, "num_plus", vsize, num_color, alpha, dflags);
228                 pos_x += lettersize;
229         }
230
231         for(i = 0; i < l; ++i)
232         {
233                 tmp = substring(str, i, 1);
234                 if (stroke == 1)
235                         drawpic_skin(pos, strcat("num_", tmp, "_stroke"), vsize, rgb, alpha, dflags);
236                 drawpic_skin(pos, strcat("num_", tmp), vsize, num_color, alpha, dflags);
237                 pos_x += lettersize;
238         }
239 }
240
241 vector HUD_Get_Num_Color (float x, float maxvalue)
242 {
243         vector color;
244         if(x > maxvalue) {
245                 color_x = 0;
246                 color_y = 1;
247                 color_z = 0;
248         }
249         else if(x > maxvalue * 0.75) {
250                 color_x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
251                 color_y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
252                 color_z = 0;
253         }
254         else if(x > maxvalue * 0.5) {
255                 color_x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
256                 color_y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
257                 color_z = 1 - (x-100)*0.02; // blue value between 1 -> 0
258         }
259         else if(x > maxvalue * 0.25) {
260                 color_x = 1;
261                 color_y = 1;
262                 color_z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
263         }
264         else if(x > maxvalue * 0.1) {
265                 color_x = 1;
266                 color_y = (x-20)*90/27/100; // green value between 0 -> 1
267                 color_z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
268         }
269         else {
270                 color_x = 1;
271                 color_y = 0;
272                 color_z = 0;
273         }
274         return color;
275 }
276 // color the number differently based on how big it is (used in the health/armor panel)
277 void HUD_DrawXNum_Colored (vector pos, float x, float digits, float lettersize, float alpha)
278 {
279         vector color;
280         color = HUD_Get_Num_Color (x, 200);
281         HUD_DrawXNum(pos, x, digits, 0, lettersize, color, 0, 0, alpha, DRAWFLAG_NORMAL);
282 }
283
284 float stringwidth_colors(string s, vector theSize)
285 {
286         return stringwidth(s, TRUE, theSize);
287 }
288
289 float stringwidth_nocolors(string s, vector theSize)
290 {
291         return stringwidth(s, FALSE, theSize);
292 }
293
294 #define CENTERPRINT_MAX_LINES 30
295 string centerprint_messages[CENTERPRINT_MAX_LINES];
296 float centerprint_width[CENTERPRINT_MAX_LINES];
297 vector centerprint_start;
298 float centerprint_expire;
299 float centerprint_num;
300 float centerprint_offset_hint;
301 vector centerprint_fontsize;
302
303 void centerprint(string strMessage)
304 {
305         float i, j, n, hcount;
306         string s;
307
308         centerprint_fontsize = HUD_GetFontsize("scr_centersize");
309
310         centerprint_expire = min(centerprint_expire, time); // if any of the returns happens, this message will fade out
311
312         if(cvar("scr_centertime") <= 0)
313                 return;
314
315         if(strMessage == "")
316                 return;
317
318         // strip trailing newlines
319         j = strlen(strMessage) - 1;
320         while(substring(strMessage, j, 1) == "\n" && j >= 0)
321                 j = j - 1;
322         strMessage = substring(strMessage, 0, j + 1);
323
324         if(strMessage == "")
325                 return;
326
327         // strip leading newlines and remember them, they are a hint that the message should be lower on the screen
328         j = 0;
329         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
330                 j = j + 1;
331         strMessage = substring(strMessage, j, strlen(strMessage) - j);
332         centerprint_offset_hint = j;
333
334         if(strMessage == "")
335                 return;
336
337         // if we get here, we have a message. Initialize its height.
338         centerprint_num = 0;
339
340         n = tokenizebyseparator(strMessage, "\n");
341         i = hcount = 0;
342         for(j = 0; j < n; ++j)
343         {
344                 getWrappedLine_remaining = argv(j);
345                 while(getWrappedLine_remaining)
346                 {
347                         s = getWrappedLine(vid_conwidth * 0.75, centerprint_fontsize, stringwidth_colors);
348                         if(centerprint_messages[i])
349                                 strunzone(centerprint_messages[i]);
350                         centerprint_messages[i] = strzone(s);
351                         centerprint_width[i] = stringwidth(s, TRUE, centerprint_fontsize);
352                         ++i;
353
354                         // half height for empty lines looks better
355                         if(s == "")
356                                 hcount += 0.5;
357                         else
358                                 hcount += 1;
359
360                         if(i >= CENTERPRINT_MAX_LINES)
361                                 break;
362                 }
363         }
364
365         float h, havail;
366         h = centerprint_fontsize_y*hcount;
367
368         havail = vid_conheight;
369         if(cvar("con_chatpos") < 0)
370                 havail -= (-cvar("con_chatpos") + cvar("con_chat")) * cvar("con_chatsize"); // avoid overlapping chat
371         if(havail > vid_conheight - 70)
372                 havail = vid_conheight - 70; // avoid overlapping HUD
373
374         centerprint_start_x = 0;
375
376 #if 0
377         float forbiddenmin, forbiddenmax, allowedmin, allowedmax, preferred;
378
379         // here, the centerprint would cover the crosshair. REALLY BAD.
380         forbiddenmin = vid_conheight * 0.5 - h - 16;
381         forbiddenmax = vid_conheight * 0.5 + 16;
382
383         allowedmin = scoreboard_bottom;
384         allowedmax = havail - h;
385         preferred = (havail - h)/2;
386
387
388         // possible orderings (total: 4! / 4 = 6)
389         //  allowedmin allowedmax forbiddenmin forbiddenmax
390         //  forbiddenmin forbiddenmax allowedmin allowedmax
391         if(allowedmax < forbiddenmin || allowedmin > forbiddenmax)
392         {
393                 // forbidden doesn't matter in this case
394                 centerprint_start_y = bound(allowedmin, preferred, allowedmax);
395         }
396         //  allowedmin forbiddenmin allowedmax forbiddenmax
397         else if(allowedmin < forbiddenmin && allowedmax < forbiddenmax)
398         {
399                 centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
400         }
401         //  allowedmin forbiddenmin forbiddenmax allowedmax
402         else if(allowedmin < forbiddenmin)
403         {
404                 // make sure the forbidden zone is not covered
405                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
406                         centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
407                 else
408                         centerprint_start_y = bound(forbiddenmax, preferred, allowedmin);
409         }
410         //  forbiddenmin allowedmin allowedmax forbiddenmax
411         else if(allowedmax < forbiddenmax)
412         {
413                 // it's better to leave the allowed zone (overlap with scoreboard) than
414                 // to cover the forbidden zone (crosshair)
415                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
416                         centerprint_start_y = forbiddenmax;
417                 else
418                         centerprint_start_y = forbiddenmin;
419         }
420         //  forbiddenmin allowedmin forbiddenmax allowedmax
421         else
422         {
423                 centerprint_start_y = bound(forbiddenmax, preferred, allowedmax);
424         }
425 #else
426         centerprint_start_y =
427                 min(
428                         max(
429                                 max(scoreboard_bottom, vid_conheight * 0.5 + 16),
430                                 (havail - h)/2
431                         ),
432                         havail - h
433                 );
434 #endif
435
436         centerprint_num = i;
437         centerprint_expire = time + cvar("scr_centertime");
438 }
439
440 void HUD_DrawCenterPrint (void)
441 {
442         float i;
443         vector pos;
444         string ts;
445         float a;
446
447         //if(time > centerprint_expire)
448         //      return;
449
450         //a = bound(0, 1 - 2 * (time - centerprint_expire), 1);
451         a = bound(0, 1 - 4 * (time - centerprint_expire), 1);
452         //sz = 1.2 / (a + 0.2);
453
454         if(a <= 0)
455                 return;
456
457         pos = centerprint_start;
458         for (i=0; i<centerprint_num; i = i + 1)
459         {
460                 pos_x = (vid_conwidth - centerprint_width[i]) * 0.5;
461                 ts = centerprint_messages[i];
462                 if (ts != "")
463                 {
464                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
465                         drawcolorcodedstring(pos, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
466                         //  - '0 0.5 0' * (sz - 1) * centerprint_fontsize_x - '0.5 0 0' * (sz - 1) * centerprint_width[i] * centerprint_fontsize_y, centerprint_fontsize * sz
467                         pos_y = pos_y + centerprint_fontsize_y;
468                 }
469                 else
470                         // half height for empty lines looks better
471                         pos_y = pos_y + centerprint_fontsize_y * 0.5;
472         }
473 }
474
475 void drawstringright(vector position, string text, vector scale, vector rgb, float alpha, float flag)
476 {
477         position_x -= 2 / 3 * strlen(text) * scale_x;
478         drawstring(position, text, scale, rgb, alpha, flag);
479 }
480
481 void drawstringcenter(vector position, string text, vector scale, vector rgb, float alpha, float flag)
482 {
483         position_x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * scale_x);
484         drawstring(position, text, scale, rgb, alpha, flag);
485 }
486
487 // return the string of the given race place
488 string race_PlaceName(float pos) {
489         if(pos == 1)
490                 return "1st";
491         else if(pos == 2)
492                 return "2nd";
493         else if(pos == 3)
494                 return "3rd";
495         else
496                 return strcat(ftos(pos), "th");
497 }
498
499 // return the string of the onscreen race timer
500 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
501 {
502         string col;
503         string timestr;
504         string cpname;
505         string lapstr;
506         lapstr = "";
507
508         if(histime == 0) // goal hit
509         {
510                 if(mytime > 0)
511                 {
512                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
513                         col = "^1";
514                 }
515                 else if(mytime == 0)
516                 {
517                         timestr = "+0.0";
518                         col = "^3";
519                 }
520                 else
521                 {
522                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
523                         col = "^2";
524                 }
525
526                 if(lapdelta > 0)
527                 {
528                         lapstr = strcat(" (-", ftos(lapdelta), "L)");
529                         col = "^2";
530                 }
531                 else if(lapdelta < 0)
532                 {
533                         lapstr = strcat(" (+", ftos(-lapdelta), "L)");
534                         col = "^1";
535                 }
536         }
537         else if(histime > 0) // anticipation
538         {
539                 if(mytime >= histime)
540                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
541                 else
542                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
543                 col = "^3";
544         }
545         else
546                 col = "^7";
547
548         if(cp == 254)
549                 cpname = "Start line";
550         else if(cp == 255)
551                 cpname = "Finish line";
552         else if(cp)
553                 cpname = strcat("Intermediate ", ftos(cp));
554         else
555                 cpname = "Finish line";
556
557         if(histime < 0)
558                 return strcat(col, cpname);
559         else if(hisname == "")
560                 return strcat(col, cpname, " (", timestr, ")");
561         else
562                 return strcat(col, cpname, " (", timestr, " ", strcat(hisname, col, lapstr), ")");
563 }
564
565 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
566 float race_CheckName(string net_name) {
567         float i;
568         for (i=RANKINGS_CNT-1;i>=0;--i)
569                 if(grecordholder[i] == net_name)
570                         return i+1;
571         return 0;
572 }
573
574 /*
575 ==================
576 HUD panels
577 ==================
578 */
579
580 // Save the config
581 void HUD_Panel_ExportCfg(string cfgname)
582 {
583         float fh;
584         fh = fopen(strcat("hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg"), FILE_WRITE);
585         if(fh >= 0)
586         {
587                 fputs(fh, strcat("seta hud_skin \"", cvar_string("hud_skin"), "\"", "\n"));
588                 fputs(fh, strcat("seta hud_bg \"", cvar_string("hud_bg"), "\"", "\n"));
589                 fputs(fh, strcat("seta hud_bg_color \"", cvar_string("hud_bg_color"), "\"", "\n"));
590                 fputs(fh, strcat("seta hud_bg_color_team \"", cvar_string("hud_bg_color_team"), "\"", "\n"));
591                 fputs(fh, strcat("seta hud_bg_alpha \"", cvar_string("hud_bg_alpha"), "\"", "\n"));
592                 fputs(fh, strcat("seta hud_bg_border \"", cvar_string("hud_bg_border"), "\"", "\n"));
593                 fputs(fh, strcat("seta hud_bg_padding \"", cvar_string("hud_bg_padding"), "\"", "\n"));
594                 fputs(fh, strcat("seta hud_fg_alpha \"", cvar_string("hud_fg_alpha"), "\"", "\n"));
595                 fputs(fh, "\n");
596
597                 fputs(fh, strcat("seta hud_dock \"", cvar_string("hud_dock"), "\"", "\n"));
598                 fputs(fh, strcat("seta hud_dock_color \"", cvar_string("hud_dock_color"), "\"", "\n"));
599                 fputs(fh, strcat("seta hud_dock_color_team \"", cvar_string("hud_dock_color_team"), "\"", "\n"));
600                 fputs(fh, strcat("seta hud_dock_alpha \"", ftos(cvar("hud_dock_alpha")), "\"", "\n"));
601                 fputs(fh, "\n");
602
603                 fputs(fh, strcat("seta hud_progressbar_alpha ", ftos(cvar("hud_progressbar_alpha")), "\n"));
604                 fputs(fh, strcat("seta hud_progressbar_strength_color \"", cvar_string("hud_progressbar_strength_color"), "\"", "\n"));
605                 fputs(fh, strcat("seta hud_progressbar_shield_color \"", cvar_string("hud_progressbar_shield_color"), "\"", "\n"));
606                 fputs(fh, strcat("seta hud_progressbar_health_color \"", cvar_string("hud_progressbar_health_color"), "\"", "\n"));
607                 fputs(fh, strcat("seta hud_progressbar_armor_color \"", cvar_string("hud_progressbar_armor_color"), "\"", "\n"));
608                 fputs(fh, strcat("seta hud_progressbar_fuel_color \"", cvar_string("hud_progressbar_fuel_color"), "\"", "\n"));
609                 fputs(fh, strcat("seta hud_progressbar_nexball_color \"", cvar_string("hud_progressbar_nexball_color"), "\"", "\n"));
610                 fputs(fh, "\n");
611
612                 // common cvars for all panels
613                 float i;
614                 for (i = 0; i < HUD_PANEL_NUM; ++i)
615                 {
616                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), " ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i)))), "\n"));
617                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_pos \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_pos")), "\"", "\n"));
618                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_size \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_size")), "\"", "\n"));
619                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg")), "\"", "\n"));
620                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_color \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_color")), "\"", "\n"));
621                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_color_team \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_color_team")), "\"", "\n"));
622                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_alpha \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_alpha")), "\"", "\n"));
623                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_border \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_border")), "\"", "\n"));
624                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_padding \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_padding")), "\"", "\n"));
625                         switch(i) {
626                                 case 0:
627                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_accuracy_yellow ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_accuracy_yellow"))), "\n"));
628                                         break;
629                                 case 1:
630                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_onlycurrent ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_onlycurrent"))), "\n"));
631                                         break;
632                                 case 2:
633                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
634                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_mirror ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
635                                         break;
636                                 case 3:
637                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
638                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_mirror ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
639                                         break;
640                                 case 4:
641                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
642                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_info_top ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_info_top"))), "\n"));
643                                         break;
644                                 case 6:
645                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_foreground_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_foreground_alpha"))), "\n"));
646                                 case 9:
647                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha"))), "\n"));
648                                         break;
649                         }
650                         fputs(fh, "\n");
651                 }
652
653                 print("^2Successfully exported to hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg! (Note: It's saved in data/data/)\n");
654         }
655         fclose(fh);
656 }
657
658 // return active status of panel
659 float HUD_Panel_CheckActive(float id)
660 {
661         if (cvar_or(strcat("hud_", HUD_Panel_GetName(id)), 1))
662                 return 1;
663         return 0;
664 }
665
666 // return size of given panel
667 vector HUD_Panel_GetSize(float id)
668 {
669         vector mySize;
670         mySize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
671
672         mySize = eX * mySize_x * vid_conwidth + eY * mySize_y * vid_conheight;
673
674         if(disable_menu_alphacheck == 2 && id == highlightedPanel)
675         {
676                 vector menu_enable_maxsize, menu_enable_size;
677                 menu_enable_maxsize = '0 0 0'; // shut up FTEQCC
678                 menu_enable_maxsize_x = 0.3 * vid_conwidth;
679                 menu_enable_maxsize_y = 0.18 * vid_conheight;
680                 if(mySize_x > mySize_y)
681                 {
682                         if(mySize_y > menu_enable_maxsize_y)
683                         {
684                                 menu_enable_size_y = menu_enable_maxsize_y;
685                                 menu_enable_size_x = mySize_x * (menu_enable_maxsize_y/mySize_y);
686                                 mySize = (1 - cvar("_menu_alpha")) * mySize + (cvar("_menu_alpha")) * menu_enable_size;
687                         }
688                 }
689                 else
690                 {
691                         if(mySize_x > menu_enable_maxsize_x)
692                         {
693                                 menu_enable_size_x = menu_enable_maxsize_x;
694                                 menu_enable_size_y = mySize_y * (menu_enable_maxsize_x/mySize_x);
695                                 mySize = (1 - cvar("_menu_alpha")) * mySize + (cvar("_menu_alpha")) * menu_enable_size;
696                         }
697                 }
698         }
699         return mySize;
700 }
701
702 // return pos of given panel
703 vector HUD_Panel_GetPos(float id)
704 {
705         vector pos;
706         pos = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_pos")));
707
708         pos = eX * pos_x * vid_conwidth + eY * pos_y * vid_conheight;
709
710         if(disable_menu_alphacheck == 2 && id == highlightedPanel)
711         {
712                 vector mySize, menu_enable_panelpos;
713                 mySize = HUD_Panel_GetSize(id);
714                 if(mySize_x > mySize_y)
715                         menu_enable_panelpos = eX * 0.5 * vid_conwidth - eX * 0.5 * mySize_x + eY * 0.82 * vid_conheight;
716                 else
717                         menu_enable_panelpos = eY * 0.5 * vid_conheight - eY * 0.5 * mySize_y + eX * 0.7 * vid_conwidth;
718                 pos = (1 - cvar("_menu_alpha")) * pos + (cvar("_menu_alpha")) * menu_enable_panelpos;
719         }
720         return pos;
721 }
722
723 float HUD_Panel_GetBorder(float id)
724 {
725         string border;
726         border = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_border"));
727         if(border == "")
728                 border = cvar_string("hud_bg_border");
729         return stof(border);
730 }
731
732 vector HUD_Panel_GetColor(float id)
733 {
734         float f;
735         vector color_vec;
736         string color;
737
738         // fetch per-panel color
739         if(teamplay && cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color_team")) != "") {
740                 f = stof(getplayerkey(self.sv_entnum, "colors"));
741                 color = vtos(colormapPaletteColor(mod(f, 16), 1) * cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_color_team")));
742         }
743         else
744                 color = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color"));
745         color_vec = stov(color);
746
747         if(color == "") { // fetch default color
748                 color = cvar_string("hud_bg_color");
749                 color_vec = stov(color);
750                 if(teamplay && cvar(strcat("hud_bg_color_team"))) {
751                         f = stof(getplayerkey(self.sv_entnum, "colors"));
752                         color_vec = colormapPaletteColor(mod(f, 16), 1) * cvar("hud_bg_color_team");
753                 }
754                 else if(color == "shirt") {
755                         f = stof(getplayerkey(self.sv_entnum, "colors"));
756                         color_vec = colormapPaletteColor(floor(f / 16), 0);
757                 }
758                 else if(color == "pants") {
759                         f = stof(getplayerkey(self.sv_entnum, "colors"));
760                         color_vec = colormapPaletteColor(mod(f, 16), 1);
761                 }
762         }
763         else if(color == "shirt") {
764                 f = stof(getplayerkey(self.sv_entnum, "colors"));
765                 color_vec = colormapPaletteColor(floor(f / 16), 0);
766         }
767         else if(color == "pants") {
768                 f = stof(getplayerkey(self.sv_entnum, "colors"));
769                 color_vec = colormapPaletteColor(mod(f, 16), 1);
770         }
771         return color_vec;
772 }
773
774 vector HUD_Panel_Dock_GetColor(void)
775 {
776         float f;
777         vector color_vec;
778         string color;
779         color = cvar_string("hud_dock_color");
780         color_vec = stov(color);
781         if(teamplay && cvar(strcat("hud_dock_color_team"))) {
782                 f = stof(getplayerkey(self.sv_entnum, "colors"));
783                 color_vec = colormapPaletteColor(mod(f, 16), 1) * cvar("hud_dock_color_team");
784         }
785         else if(color == "shirt") {
786                 f = stof(getplayerkey(self.sv_entnum, "colors"));
787                 color_vec = colormapPaletteColor(floor(f / 16), 0);
788         }
789         else if(color == "pants") {
790                 f = stof(getplayerkey(self.sv_entnum, "colors"));
791                 color_vec = colormapPaletteColor(mod(f, 16), 1);
792         }
793         return color_vec;
794 }
795
796 float HUD_Panel_GetBgAlpha(float id)
797 {
798         string alpha;
799         alpha = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_alpha"));
800         if(alpha == "")
801                 alpha = cvar_string("hud_bg_alpha");
802
803         if(hud_configure && disable_menu_alphacheck == 2 && highlightedPanel == id) // do not set a minalpha cap when showing the config dialog for this panel
804                 alpha = ftos((1 - cvar("_menu_alpha")) * max(cvar("hud_configure_bg_minalpha"), stof(alpha)) + (cvar("_menu_alpha")) * stof(alpha));
805         else if(hud_configure)
806                 alpha = ftos(max(cvar("hud_configure_bg_minalpha"), stof(alpha)));
807
808         if(hud_configure && !cvar(strcat("hud_", HUD_Panel_GetName(id)))) // ALWAYS show disabled panels at 0.25 alpha when in config mode
809                 return 0.25;
810
811         if(disable_menu_alphacheck == 2 && highlightedPanel == id) // don't fade this panel when showing the panel-specific menu dialog
812                 return stof(alpha);
813         else
814                 return stof(alpha) * menu_fade_alpha;
815 }
816
817 float HUD_Panel_GetFgAlpha(float id)
818 {
819         float alpha;
820         alpha = hud_fg_alpha;
821
822         if(hud_configure && !cvar(strcat("hud_", HUD_Panel_GetName(id)))) // ALWAYS show disabled panels at 0.25 alpha when in config mode
823                 return 0.25;
824
825         if(disable_menu_alphacheck == 2 && highlightedPanel == id) // don't fade this panel when showing the panel-specific menu dialog
826                 return alpha;
827         else
828                 return alpha * menu_fade_alpha;
829 }
830
831 float HUD_Panel_GetPadding(float id)
832 {
833         string padding;
834         padding = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
835         if(padding == "")
836                 padding = cvar_string("hud_bg_padding");
837
838         vector mySize;
839         mySize = HUD_Panel_GetSize(id);
840         float smallestsize;
841         smallestsize = min(mySize_x, mySize_y);
842         return min(smallestsize/2 - 5, stof(padding));
843 }
844
845 // draw the background/borders
846 void HUD_Panel_DrawBg(float id, vector pos, vector mySize, float alpha)
847 {
848         if(!hud_configure && cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg")) == "0")
849                 return;
850
851         string bg;
852         bg = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg"));
853         if(bg == "")
854                 bg = cvar_string("hud_bg");
855
856         if(bg == "0" && hud_configure) {
857                 bg = "border"; // we probably want to see a background in config mode at all times...
858         }
859
860         if(bg != "0")
861         {
862                 float border;
863                 border = max(0.0000001, HUD_Panel_GetBorder(id)); // draw_BorderPicture does not like border = 0
864
865                 vector color;
866                 color = HUD_Panel_GetColor(id);
867
868                 if(alpha)
869                         alpha = HUD_Panel_GetBgAlpha(id) * alpha; // allow panels to fade in/out by passing an alpha value
870                 else
871                         alpha = HUD_Panel_GetBgAlpha(id);
872
873                 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));
874         }
875 }
876
877 void HUD_Panel_DrawProgressBar(vector pos, float vertical, vector mySize, vector color, float alpha, float drawflag)
878 {
879 //float drawsubpic(vector position, vector size, string pic, vector srcPosition, vector srcSize, vector rgb, float alpha, float flag) = #328;
880         string pic;
881         pic = strcat("gfx/hud/", cvar_string("hud_skin"), "/");
882         if(vertical) {
883                 drawsubpic(pos, eY * min(mySize_y * 0.5, mySize_x) + eX * mySize_x, strcat(pic, "statusbar_vertical"), '0 0 0', '1 0.25 0', color, alpha, drawflag);
884                 if(mySize_y/mySize_x > 2)
885                         drawsubpic(pos + eY * mySize_x, eY * (mySize_y - 2 * mySize_x) + eX * mySize_x, strcat(pic, "statusbar_vertical"), '0 0.25 0', '1 0.5 0', color, alpha, drawflag);
886                 drawsubpic(pos + eY * mySize_y - eY * min(mySize_y * 0.5, mySize_x), eY * min(mySize_y * 0.5, mySize_x) + eX * mySize_x, strcat(pic, "statusbar_vertical"), '0 0.75 0', '1 0.25 0', color, alpha, drawflag);
887         } else {
888                 drawsubpic(pos, eX * min(mySize_x * 0.5, mySize_y) + eY * mySize_y, strcat(pic, "statusbar"), '0 0 0', '0.25 1 0', color, alpha, drawflag);
889                 if(mySize_x/mySize_y > 2)
890                         drawsubpic(pos + eX * mySize_y, eX * (mySize_x - 2 * mySize_y) + eY * mySize_y, strcat(pic, "statusbar"), '0.25 0 0', '0.5 1 0', color, alpha, drawflag);
891                 drawsubpic(pos + eX * mySize_x - eX * min(mySize_x * 0.5, mySize_y), eX * min(mySize_x * 0.5, mySize_y) + eY * mySize_y, strcat(pic, "statusbar"), '0.75 0 0', '0.25 1 0', color, alpha, drawflag);
892         }
893 }
894
895 vector HUD_Panel_GetProgressBarColor(string item)
896 {
897         return stov(cvar_string(strcat("hud_progressbar_", item, "_color")));
898 }
899
900 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
901 vector HUD_Panel_CheckMove(float id, vector myPos, vector mySize)
902 {
903         float i;
904
905         vector myTarget;
906         myTarget = myPos;
907
908         vector targPos;
909         vector targSize;
910         vector myCenter;
911         vector targCenter;
912         myCenter = '0 0 0'; // shut up fteqcc, there IS a reference
913         targCenter = '0 0 0'; // shut up fteqcc, there IS a reference
914
915         for (i = 0; i < HUD_PANEL_NUM; ++i) {
916                 if(i == id || !HUD_Panel_CheckActive(i))
917                         continue;
918
919                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
920                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(id);
921
922                 if(myPos_y + mySize_y < targPos_y)
923                         continue;
924                 if(myPos_y > targPos_y + targSize_y)
925                         continue;
926
927                 if(myPos_x + mySize_x < targPos_x)
928                         continue;
929                 if(myPos_x > targPos_x + targSize_x)
930                         continue;
931
932                 // OK, there IS a collision.
933
934                 myCenter_x = myPos_x + 0.5 * mySize_x;
935                 myCenter_y = myPos_y + 0.5 * mySize_y;
936
937                 targCenter_x = targPos_x + 0.5 * targSize_x;
938                 targCenter_y = targPos_y + 0.5 * targSize_y;
939
940                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y) // top left (of the target panel)
941                 {
942                         if(myPos_x + mySize_x - targPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
943                                 myTarget_x = targPos_x - mySize_x;
944                         else // push it upwards
945                                 myTarget_y = targPos_y - mySize_y;
946                 }
947                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y) // top right
948                 {
949                         if(targPos_x + targSize_x - myPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
950                                 myTarget_x = targPos_x + targSize_x;
951                         else // push it upwards
952                                 myTarget_y = targPos_y - mySize_y;
953                 }
954                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
955                 {
956                         if(myPos_x + mySize_x - targPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
957                                 myTarget_x = targPos_x - mySize_x;
958                         else // push it downwards
959                                 myTarget_y = targPos_y + targSize_y;
960                 }
961                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
962                 {
963                         if(targPos_x + targSize_x - myPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
964                                 myTarget_x = targPos_x + targSize_x;
965                         else // push it downwards
966                                 myTarget_y = targPos_y + targSize_y;
967                 }
968         }
969
970         return myTarget;
971 }
972
973 void HUD_Panel_SetPos(float id, vector pos)
974 {
975         vector oldPos;
976         oldPos = HUD_Panel_GetPos(id);
977
978         vector mySize;
979         mySize = HUD_Panel_GetSize(id);
980
981         if(cvar("hud_configure_checkcollisions"))
982                 pos = HUD_Panel_CheckMove(id, pos, mySize);
983
984         pos_x = bound(0, pos_x, vid_conwidth - mySize_x);
985         pos_y = bound(0, pos_y, vid_conheight - mySize_y);
986
987         if(cvar("hud_configure_grid"))
988         {
989                 pos_x = floor((pos_x/vid_conwidth)/bound(0.005, cvar("hud_configure_grid_x"), 0.2) + 0.5) * cvar("hud_configure_grid_x") * vid_conwidth;
990                 pos_y = floor((pos_y/vid_conheight)/bound(0.005, cvar("hud_configure_grid_y"), 0.2) + 0.5) * cvar("hud_configure_grid_y") * vid_conheight;
991         }
992
993         string s;
994         s = strcat(ftos(pos_x/vid_conwidth), " ", ftos(pos_y/vid_conheight));
995
996         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_pos"), s);
997 }
998
999 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
1000 vector HUD_Panel_CheckResize(float id, vector mySize, vector resizeorigin) {
1001         float i;
1002
1003         float targBorder;
1004         vector targPos;
1005         vector targSize;
1006         vector targEndPos;
1007         vector dist;
1008
1009         for (i = 0; i < HUD_PANEL_NUM; ++i) {
1010                 if(i == id || !HUD_Panel_CheckActive(i))
1011                         continue;
1012
1013                 targBorder = HUD_Panel_GetBorder(i);
1014                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * targBorder;
1015                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * targBorder;
1016                 targEndPos = targPos + targSize;
1017
1018                 // resizeorigin is WITHIN target panel, just abort any collision testing against that particular panel to produce expected behaviour!
1019                 if(resizeorigin_x > targPos_x && resizeorigin_x < targPos_x + targSize_x && resizeorigin_y > targPos_y && resizeorigin_y < targPos_y + targSize_y)
1020                         continue;
1021
1022                 if (resizeCorner == 1)
1023                 {
1024                         // check if this panel is on our way
1025                         if (resizeorigin_x <= targPos_x)
1026                                 continue;
1027                         if (resizeorigin_y <= targPos_y)
1028                                 continue;
1029                         if (targEndPos_x <= resizeorigin_x - mySize_x)
1030                                 continue;
1031                         if (targEndPos_y <= resizeorigin_y - mySize_y)
1032                                 continue;
1033
1034                         // there is a collision:
1035                         // detect which side of the panel we are facing is actually limiting the resizing
1036                         // (which side the resize direction finds for first) and reduce the size up to there
1037                         //
1038                         // dist is the distance between resizeorigin and the "analogous" point of the panel
1039                         // in this case resizeorigin (bottom-right point) and the bottom-right point of the panel
1040                         dist_x = resizeorigin_x - targEndPos_x;
1041                         dist_y = resizeorigin_y - targEndPos_y;
1042                         if (dist_y < 0)
1043                                 mySize_x = min(mySize_x, dist_x);
1044                         else
1045                                 mySize_y = min(mySize_y, dist_y);
1046                 }
1047                 else if (resizeCorner == 2)
1048                 {
1049                         if (resizeorigin_x >= targEndPos_x)
1050                                 continue;
1051                         if (resizeorigin_y <= targPos_y)
1052                                 continue;
1053                         if (targPos_x >= resizeorigin_x + mySize_x)
1054                                 continue;
1055                         if (targEndPos_y <= resizeorigin_y - mySize_y)
1056                                 continue;
1057
1058                         dist_x = targPos_x - resizeorigin_x;
1059                         dist_y = resizeorigin_y - targEndPos_y;
1060                         if (dist_y < 0)
1061                                 mySize_x = min(mySize_x, dist_x);
1062                         else
1063                                 mySize_y = min(mySize_y, dist_y);
1064                 }
1065                 else if (resizeCorner == 3)
1066                 {
1067                         if (resizeorigin_x <= targPos_x)
1068                                 continue;
1069                         if (resizeorigin_y >= targEndPos_y)
1070                                 continue;
1071                         if (targEndPos_x <= resizeorigin_x - mySize_x)
1072                                 continue;
1073                         if (targPos_y >= resizeorigin_y + mySize_y)
1074                                 continue;
1075
1076                         dist_x = resizeorigin_x - targEndPos_x;
1077                         dist_y = targPos_y - resizeorigin_y;
1078                         if (dist_y < 0)
1079                                 mySize_x = min(mySize_x, dist_x);
1080                         else
1081                                 mySize_y = min(mySize_y, dist_y);
1082                 }
1083                 else if (resizeCorner == 4)
1084                 {
1085                         if (resizeorigin_x >= targEndPos_x)
1086                                 continue;
1087                         if (resizeorigin_y >= targEndPos_y)
1088                                 continue;
1089                         if (targPos_x >= resizeorigin_x + mySize_x)
1090                                 continue;
1091                         if (targPos_y >= resizeorigin_y + mySize_y)
1092                                 continue;
1093
1094                         dist_x = targPos_x - resizeorigin_x;
1095                         dist_y = targPos_y - resizeorigin_y;
1096                         if (dist_y < 0)
1097                                 mySize_x = min(mySize_x, dist_x);
1098                         else
1099                                 mySize_y = min(mySize_y, dist_y);
1100                 }
1101                 if(cvar("hud_configure_checkcollisions_debug"))
1102                         drawfill(targPos + '1 1 0' * targBorder, targSize - '2 2 0' * targBorder, '1 1 0', .3, DRAWFLAG_NORMAL);
1103         }
1104
1105         return mySize;
1106 }
1107
1108 void HUD_Panel_SetPosSize(float id)
1109 {
1110         vector resizeorigin;
1111         resizeorigin = panel_click_resizeorigin;
1112         vector mySize, myPos;
1113
1114         if(resizeCorner == 1) {
1115                 mySize_x = resizeorigin_x - (mousepos_x - panel_click_distance_x);
1116                 mySize_y = resizeorigin_y - (mousepos_y - panel_click_distance_y);
1117         } else if(resizeCorner == 2) {          
1118                 mySize_x = mousepos_x + panel_click_distance_x - resizeorigin_x;
1119                 mySize_y = panel_click_distance_y + resizeorigin_y - mousepos_y;
1120         } else if(resizeCorner == 3) {
1121                 mySize_x = resizeorigin_x + panel_click_distance_x - mousepos_x;
1122                 mySize_y = mousepos_y + panel_click_distance_y - resizeorigin_y;
1123         } else { // resizeCorner == 4
1124                 mySize_x = mousepos_x - (resizeorigin_x - panel_click_distance_x);
1125                 mySize_y = mousepos_y - (resizeorigin_y - panel_click_distance_y);
1126         }
1127
1128         // minimum panel size cap
1129         mySize_x = max(0.025 * vid_conwidth, mySize_x);
1130         mySize_y = max(0.025 * vid_conheight, mySize_y);
1131
1132         if(id == 12) // some panels have their own restrictions, like the chat panel (which actually only moves the engine chat print around). Looks bad if it's too small.
1133         {
1134                 mySize_x = max(17 * cvar("con_chatsize"), mySize_x);
1135                 mySize_y = max(2 * cvar("con_chatsize") + 2 * HUD_Panel_GetPadding(id), mySize_y);
1136         }
1137
1138         // collision testing|
1139         // -----------------+
1140
1141         // we need to know pos at this stage, but it might still change later if we hit a screen edge/other panel (?)
1142         if(resizeCorner == 1) {
1143                 myPos_x = resizeorigin_x - mySize_x;
1144                 myPos_y = resizeorigin_y - mySize_y;
1145         } else if(resizeCorner == 2) {
1146                 myPos_x = resizeorigin_x;
1147                 myPos_y = resizeorigin_y - mySize_y;
1148         } else if(resizeCorner == 3) {
1149                 myPos_x = resizeorigin_x - mySize_x;
1150                 myPos_y = resizeorigin_y;
1151         } else { // resizeCorner == 4
1152                 myPos_x = resizeorigin_x;
1153                 myPos_y = resizeorigin_y;
1154         }
1155
1156         // left/top screen edges
1157         if(myPos_x < 0)
1158                 mySize_x = mySize_x + myPos_x;
1159         if(myPos_y < 0)
1160                 mySize_y = mySize_y + myPos_y;
1161
1162         // bottom/right screen edges
1163         if(myPos_x + mySize_x > vid_conwidth)
1164                 mySize_x = vid_conwidth - myPos_x;
1165         if(myPos_y + mySize_y > vid_conheight)
1166                 mySize_y = vid_conheight - myPos_y;
1167
1168         if(cvar("hud_configure_checkcollisions_debug"))
1169                 drawfill(myPos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
1170
1171         // before checkresize, otherwise panel can be snapped partially inside another panel or panel aspect ratio can be broken
1172         if(cvar("hud_configure_grid"))
1173         {
1174                 mySize_x = floor((mySize_x/vid_conwidth)/bound(0.005, cvar("hud_configure_grid_x"), 0.2) + 0.5) * cvar("hud_configure_grid_x") * vid_conwidth;
1175                 mySize_y = floor((mySize_y/vid_conheight)/bound(0.005, cvar("hud_configure_grid_y"), 0.2) + 0.5) * cvar("hud_configure_grid_y") * vid_conheight;
1176         }
1177
1178         if(cvar("hud_configure_checkcollisions"))
1179         {
1180                 mySize = HUD_Panel_CheckResize(id, mySize, resizeorigin);
1181
1182                 // Make sure once more that we DON'T cross the screen edges
1183                 // left/top screen edges
1184                 if(myPos_x < 0)
1185                         mySize_x = mySize_x + myPos_x;
1186                 if(myPos_y < 0)
1187                         mySize_y = mySize_y + myPos_y;
1188
1189                 // bottom/right screen edges
1190                 if(myPos_x + mySize_x > vid_conwidth)
1191                         mySize_x = vid_conwidth - myPos_x;
1192                 if(myPos_y + mySize_y > vid_conheight)
1193                         mySize_y = vid_conheight - myPos_y;
1194         }
1195
1196         // minimum panel size cap, do this once more so we NEVER EVER EVER have a panel smaller than this, JUST IN CASE above code still makes the panel eg negative (impossible to resize back without changing cvars manually then)
1197         mySize_x = max(0.025 * vid_conwidth, mySize_x);
1198         mySize_y = max(0.025 * vid_conheight, mySize_y);
1199
1200         // do another pos check, as size might have changed by now
1201         if(resizeCorner == 1) {
1202                 myPos_x = resizeorigin_x - mySize_x;
1203                 myPos_y = resizeorigin_y - mySize_y;
1204         } else if(resizeCorner == 2) {
1205                 myPos_x = resizeorigin_x;
1206                 myPos_y = resizeorigin_y - mySize_y;
1207         } else if(resizeCorner == 3) {
1208                 myPos_x = resizeorigin_x - mySize_x;
1209                 myPos_y = resizeorigin_y;
1210         } else { // resizeCorner == 4
1211                 myPos_x = resizeorigin_x;
1212                 myPos_y = resizeorigin_y;
1213         }
1214
1215         if(cvar("hud_configure_checkcollisions_debug"))
1216         if(cvar("hud_configure_checkcollisions"))
1217                 drawfill(myPos, mySize, '0 1 0', .3, DRAWFLAG_NORMAL);
1218
1219         string s;
1220         s = strcat(ftos(mySize_x/vid_conwidth), " ", ftos(mySize_y/vid_conheight));
1221         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_size"), s);
1222
1223         s = strcat(ftos(myPos_x/vid_conwidth), " ", ftos(myPos_y/vid_conheight));
1224         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_pos"), s);
1225 }
1226
1227 float mouseClicked;
1228 float prevMouseClicked; // previous state
1229 float prevMouseClickedTime; // time during previous mouse click, to check for doubleclicks
1230 vector prevMouseClickedPos; // pos during previous mouse click, to check for doubleclicks
1231
1232 float menu_enabled;
1233 float menu_enabled_time;
1234 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
1235 {
1236         if(!hud_configure)
1237                 return false;
1238
1239         // allow console bind to work
1240         string con_keys;
1241         float keys;
1242         con_keys = findkeysforcommand("toggleconsole");
1243         keys = tokenize(con_keys);
1244
1245         float hit_con_bind, i;
1246         for (i = 0; i < keys; ++i)
1247         {
1248                 if(nPrimary == stof(argv(i)))
1249                         hit_con_bind = 1;
1250         }
1251
1252         if(nPrimary == K_MOUSE1)
1253         {
1254                 if(bInputType == 0) { // key pressed
1255                         mouseClicked = 1;
1256                         return true;
1257                 }
1258                 else if(bInputType == 1) {// key released
1259                         mouseClicked = 0;
1260                         return true;
1261                 }
1262         }
1263         else if(nPrimary == K_ESCAPE)
1264         {
1265                 disable_menu_alphacheck = 1;
1266                 menu_enabled = 1;
1267                 menu_enabled_time = time;
1268                 localcmd("menu_showhudexit\n");
1269         }
1270
1271         else if(hit_con_bind)
1272                 return false;
1273
1274         return true; // Suppress ALL other input
1275 }
1276
1277 void HUD_Panel_Mouse()
1278 {
1279         // TODO: needs better check... is there any float that contains the current state of the menu? _menu_alpha isn't apparently updated the frame the menu gets enabled
1280         if (menu_enabled == 0) // menu dialog closed, enable normal alpha stuff again
1281                 disable_menu_alphacheck = 0;
1282         if (cvar("_menu_alpha") == 0 && time - menu_enabled_time > 0.5)
1283                 menu_enabled = 0;
1284
1285         /*
1286         print("Disable menu_alphacheck: ", ftos(disable_menu_alphacheck), "\n");
1287         print("Highlighted: ", ftos(highlightedPanel), "\n");
1288         print("Menu alpha: ", cvar_string("_menu_alpha"), "\n");
1289         */
1290
1291         if(mouseClicked == 0 && disable_menu_alphacheck != 2) { // don't reset these variables in disable_menu_alphacheck mode 2!
1292                 highlightedPanel = -1;
1293                 highlightedAction = 0;
1294         }
1295
1296         mousepos = mousepos + getmousepos();
1297
1298         mousepos_x = bound(0, mousepos_x, vid_conwidth);
1299         mousepos_y = bound(0, mousepos_y, vid_conheight);
1300
1301         drawpic(mousepos, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1302
1303         if(mouseClicked)
1304         {
1305                 float i, border;
1306                 vector panelPos;
1307                 vector panelSize;
1308
1309                 for(i = 0; i < HUD_PANEL_NUM; ++i)
1310                 {
1311                         panelPos = HUD_Panel_GetPos(i);
1312                         panelSize = HUD_Panel_GetSize(i);
1313                         border = HUD_Panel_GetBorder(i);
1314                         if(prevMouseClicked == 0) {
1315                                 // move
1316                                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1317                                 {
1318                                         highlightedPanel = i;
1319                                         highlightedAction = 1;
1320                                 }
1321                                 // resize from topleft border
1322                                 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)
1323                                 {
1324                                         highlightedPanel = i;
1325                                         highlightedAction = 2;
1326                                         resizeCorner = 1;
1327                                 }
1328                                 // resize from topright border
1329                                 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)
1330                                 {
1331                                         highlightedPanel = i;
1332                                         highlightedAction = 2;
1333                                         resizeCorner = 2;
1334                                 }
1335                                 // resize from bottomleft border
1336                                 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)
1337                                 {
1338                                         highlightedPanel = i;
1339                                         highlightedAction = 2;
1340                                         resizeCorner = 3;
1341                                 }
1342                                 // resize from bottomright border
1343                                 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)
1344                                 {
1345                                         highlightedPanel = i;
1346                                         highlightedAction = 2;
1347                                         resizeCorner = 4;
1348                                 }
1349                         }
1350
1351                         if(highlightedPanel == i)
1352                         {
1353                                 if(prevMouseClicked == 0)
1354                                 {
1355                                         if(highlightedAction == 1)
1356                                                 panel_click_distance = mousepos - panelPos;
1357                                         else if(highlightedAction == 2)
1358                                         {
1359                                                 if(resizeCorner == 1) {
1360                                                         panel_click_distance = mousepos - panelPos;
1361                                                         panel_click_resizeorigin = panelPos + panelSize;
1362                                                 } else if(resizeCorner == 2) {
1363                                                         panel_click_distance_x = panelSize_x - mousepos_x + panelPos_x;
1364                                                         panel_click_distance_y = mousepos_y - panelPos_y;
1365                                                         panel_click_resizeorigin = panelPos + eY * panelSize_y;
1366                                                 } else if(resizeCorner == 3) {
1367                                                         panel_click_distance_x = mousepos_x - panelPos_x;
1368                                                         panel_click_distance_y = panelSize_y - mousepos_y + panelPos_y;
1369                                                         panel_click_resizeorigin = panelPos + eX * panelSize_x;
1370                                                 } else if(resizeCorner == 4) {
1371                                                         panel_click_distance = panelSize - mousepos + panelPos;
1372                                                         panel_click_resizeorigin = panelPos;
1373                                                 }
1374                                         }       
1375                                 }
1376
1377                                 if(cvar("hud_configure_checkcollisions_debug"))
1378                                         drawfill(panelPos, panelSize, '1 0 0', .3, DRAWFLAG_NORMAL);
1379
1380                                 if(highlightedAction == 1)
1381                                         HUD_Panel_SetPos(i, mousepos - panel_click_distance);
1382                                 else if(highlightedAction == 2)
1383                                         HUD_Panel_SetPosSize(i);
1384                         }
1385                 }
1386
1387                 // doubleclick check
1388                 if(time - prevMouseClickedTime < 0.4 && prevMouseClicked == 0 && prevMouseClickedPos == mousepos && highlightedPanel >= 0)
1389                 {
1390                         mouseClicked = 0; // to prevent spam, I guess.
1391                         disable_menu_alphacheck = 2;
1392                         menu_enabled = 1;
1393                         menu_enabled_time = time;
1394                         localcmd("menu_showhudoptions ", ftos(highlightedPanel), "\n");
1395                         return;
1396                 }
1397                 if(prevMouseClicked == 0)
1398                 {
1399                         prevMouseClickedTime = time;
1400                         prevMouseClickedPos = mousepos;
1401                 }
1402         }
1403         prevMouseClicked = mouseClicked;
1404 }
1405
1406 // Weapon icons (#0)
1407 //
1408 float weaponspace[10];
1409 void HUD_WeaponIcons_Clear()
1410 {
1411         float idx;
1412         for(idx = 0; idx < 10; ++idx)
1413                 weaponspace[idx] = 0;
1414 }
1415
1416 entity weaponorder[WEP_MAXCOUNT];
1417
1418 void weaponorder_swap(float i, float j, entity pass)
1419 {
1420         entity h;
1421         h = weaponorder[i];
1422         weaponorder[i] = weaponorder[j];
1423         weaponorder[j] = h;
1424 }
1425
1426 string weaponorder_cmp_str;
1427 float weaponorder_cmp(float i, float j, entity pass)
1428 {
1429         float ai, aj;
1430         ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
1431         aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
1432         return aj - ai; // the string is in REVERSE order (higher prio at the right is what we want, but higher prio first is the string)
1433 }
1434
1435 void HUD_WeaponIcons(void)
1436 {
1437         float id = HUD_PANEL_WEAPONICONS;
1438         float alpha, stat_weapons; // "constants"
1439         vector pos, mySize, accuracy_color;
1440         float i, weapid, fade, weapon_stats, weapon_hit, weapon_damage, weapon_cnt; // variables
1441
1442         pos = HUD_Panel_GetPos(id);
1443         mySize = HUD_Panel_GetSize(id);
1444
1445         stat_weapons = getstati(STAT_WEAPONS);
1446         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1447         {
1448                 self = get_weaponinfo(i);
1449                 if(self.impulse >= 0)
1450                 {
1451                         weaponorder[weapon_cnt] = self;
1452                         ++weapon_cnt;
1453                 }
1454         }
1455
1456         // TODO make this configurable
1457         weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
1458         heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
1459
1460         HUD_Panel_DrawBg(id, pos, mySize, 0);
1461         float padding;
1462         padding = HUD_Panel_GetPadding(id);
1463         if(padding)
1464         {
1465                 pos += '1 1 0' * padding;
1466                 mySize -= '2 2 0' * padding;
1467         }
1468
1469         // hits
1470         weapon_stats = getstati(STAT_DAMAGE_HITS);
1471         weapon_number = weapon_stats & 63;
1472         weapon_hits[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1473         // fired
1474         weapon_stats = getstati(STAT_DAMAGE_FIRED);
1475         weapon_number = weapon_stats & 63;
1476         weapon_fired[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1477
1478         if(cvar_or("hud_weaponicons_fade", 1))
1479         {
1480                 fade = 3.2 - 2 * (time - weapontime);
1481                 fade = bound(0.7, fade, 1);
1482         }
1483         else
1484                 fade = 1;
1485
1486         HUD_WeaponIcons_Clear();
1487
1488         float rows, columns;
1489         rows = mySize_y/mySize_x;
1490         rows = bound(1, floor((sqrt(4 * (2/1) * rows * WEP_COUNT + rows * rows) + rows + 0.5) / 2), WEP_COUNT);
1491         //                               ^^^ weapon icon aspect goes here
1492
1493         columns = ceil(WEP_COUNT/rows);
1494         float row, column;
1495         for(i = 0; i < weapon_cnt; ++i)
1496         {
1497                 self = weaponorder[i];
1498                 weapid = self.impulse;
1499
1500                 alpha = (self.weapon == activeweapon) ? 1 : 0.6;
1501
1502                 weapon_hit = weapon_hits[self.weapon-WEP_FIRST];
1503                 weapon_damage = weapon_fired[self.weapon-WEP_FIRST];
1504
1505                 // draw background behind currently selected weapon
1506                 if(self.weapon == activeweapon)
1507                         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1508
1509                 // draw the weapon accuracy on the HUD
1510                 if(hud_accuracy_hud && !(gametype == GAME_RACE || gametype == GAME_CTS))
1511                 {
1512                         if(weapon_damage)
1513                                 weapon_stats = floor(100 * weapon_hit / weapon_damage);
1514
1515                         accuracy_color = HUD_AccuracyColor(weapon_stats);
1516                         if(weapon_damage)
1517                                 drawpic_skin(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), "weapon_accuracy", eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows), accuracy_color, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1518                 }
1519
1520                 // draw the weapon icon
1521                 if((self.impulse >= 0) && (stat_weapons & self.weapons))
1522                 {
1523                         drawpic_skin(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), strcat("weapon", self.netname), eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows), '1 1 1', fade * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1524
1525                         if(cvar_or("hud_weaponicons_number", 1))
1526                                 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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1527                 }
1528                 else
1529                 {
1530                         drawpic_skin(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), strcat("weapon", self.netname), eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows), '0 0 0', HUD_Panel_GetFgAlpha(id) * 0.5, DRAWFLAG_NORMAL);
1531                 }
1532
1533                 ++row;
1534                 if(row >= rows)
1535                 {
1536                         row = 0;
1537                         ++column;
1538                 }
1539         }
1540
1541 }
1542
1543 // Inventory (#1)
1544 //
1545 float GetAmmoStat(float i)
1546 {
1547         switch(i)
1548         {
1549                 case 0: return STAT_SHELLS;
1550                 case 1: return STAT_NAILS;
1551                 case 2: return STAT_ROCKETS;
1552                 case 3: return STAT_CELLS;
1553                 case 4: return STAT_FUEL;
1554                 default: return -1;
1555         }
1556 }
1557
1558 float GetAmmoItemCode(float i)
1559 {
1560         switch(i)
1561         {
1562                 case 0: return IT_SHELLS;
1563                 case 1: return IT_NAILS;
1564                 case 2: return IT_ROCKETS;
1565                 case 3: return IT_CELLS;
1566                 case 4: return IT_FUEL;
1567                 default: return -1;
1568         }
1569 }
1570
1571 string GetAmmoPicture(float i)
1572 {
1573         switch(i)
1574         {
1575                 case 0: return "ammo_shells";
1576                 case 1: return "ammo_bullets";
1577                 case 2: return "ammo_rockets";
1578                 case 3: return "ammo_cells";
1579                 case 4: return "ammo_fuel";
1580                 default: return "";
1581         }
1582 }
1583
1584 void HUD_Inventory(void)
1585 {
1586         float id = HUD_PANEL_INVENTORY;
1587         float i;
1588         float stat_items;
1589
1590         vector pos, mySize, mysize, mypos;
1591         pos = HUD_Panel_GetPos(id);
1592         mySize = HUD_Panel_GetSize(id);
1593
1594         HUD_Panel_DrawBg(id, pos, mySize, 0);
1595         float padding;
1596         padding = HUD_Panel_GetPadding(id);
1597         if(padding)
1598         {
1599                 pos += '1 1 0' * padding;
1600                 mySize -= '2 2 0' * padding;
1601         }
1602
1603         // ammo
1604         stat_items = getstati(STAT_ITEMS);
1605         for (i = 0; i < 4; ++i) {
1606                 float a;
1607                 a = getstati(GetAmmoStat(i)); // how much ammo do we have of type i?
1608                 if(hud_configure)
1609                         a = 100;
1610
1611                 if(cvar("hud_inventory_onlycurrent")) {
1612                         if(hud_configure)
1613                                 i = 2;
1614                         if (stat_items & GetAmmoItemCode(i) || hud_configure) {
1615                                 drawpic_skin(pos, GetAmmoPicture(i), '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1616                                 if(a < 10)
1617                                         HUD_DrawXNum(pos + eX * mySize_y + eY * 0.25 * mySize_y, a, strlen(ftos(a)), 0, 0.5 * mySize_y, '0.7 0 0', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1618                                 else
1619                                         HUD_DrawXNum(pos + eX * mySize_y + eY * 0.25 * mySize_y, a, strlen(ftos(a)), 0, 0.5 * mySize_y, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1620                         }
1621                         if(hud_configure)
1622                                 break;
1623                 } else {
1624                         if (a > 0) {
1625                                 if(mySize_x/mySize_y >= 10) { // arrange horizontally
1626                                         switch (i) {
1627                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1628                                                 case 1: mypos_x = pos_x + 0.25 * mySize_x;      mypos_y = pos_y;                        break; // bullets
1629                                                 case 2: mypos_x = pos_x + 0.5  * mySize_x;      mypos_y = pos_y;                        break; // rockets
1630                                                 case 3: mypos_x = pos_x + 0.75 * mySize_x;      mypos_y = pos_y;                        break; // cells
1631                                         }
1632                                         mysize_x = 0.25 * mySize_x;
1633                                         mysize_y = mySize_y;
1634                                 } else if(mySize_x/mySize_y >= 2.5) { // arrange in a 2x2 grid
1635                                         switch (i) {
1636                                                 case 0: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y + 0.5 * mySize_y;       break; // shells
1637                                                 case 1: mypos_x = pos_x + 0.5 * mySize_x;       mypos_y = pos_y;                        break; // bullets
1638                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5 * mySize_y;       break; // rockets
1639                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // cells
1640                                         }
1641                                         mysize_x = 0.5 * mySize_x;
1642                                         mysize_y = 0.5 * mySize_y;
1643                                 } else { // arrange vertically
1644                                         switch (i) {
1645                                                 case 0: mypos_x = pos_x;                        mypos_y = pos_y;                        break; // shells
1646                                                 case 1: mypos_x = pos_x;                        mypos_y = pos_y + 0.25 * mySize_y;      break; // bullets
1647                                                 case 2: mypos_x = pos_x;                        mypos_y = pos_y + 0.5  * mySize_y;      break; // rockets
1648                                                 case 3: mypos_x = pos_x;                        mypos_y = pos_y + 0.75 * mySize_y;      break; // cells
1649                                         }
1650                                         mysize_x = mySize_x;
1651                                         mysize_y = 0.25 * mySize_y;
1652                                 }
1653
1654                                 if (stat_items & GetAmmoItemCode(i))
1655                                         drawpic_skin(mypos, "ammo_current_bg", mysize, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1656                                 drawpic_skin(mypos + eY * 0.05 * mysize_y, GetAmmoPicture(i), '1 1 0' * 0.8 * mysize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1657                                 if (a < 10) {
1658                                         if(stat_items & GetAmmoItemCode(i))
1659                                                 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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1660                                         else
1661                                                 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_Panel_GetFgAlpha(id) * 0.7, DRAWFLAG_NORMAL);
1662                                 } else {
1663                                         if(stat_items & GetAmmoItemCode(i))
1664                                                 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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1665                                         else
1666                                                 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_Panel_GetFgAlpha(id) * 0.7, DRAWFLAG_NORMAL);
1667                                 }
1668                         }
1669                 }
1670         }
1671 }
1672
1673
1674 // Powerups (#2)
1675 //
1676 void HUD_Powerups(void) {
1677         float id = HUD_PANEL_POWERUPS;
1678         float stat_items;
1679         stat_items = getstati(STAT_ITEMS);
1680
1681         if(!hud_configure)
1682         {
1683                 if not(stat_items & IT_STRENGTH)
1684                         if not(stat_items & IT_INVINCIBLE)
1685                                 return;
1686
1687                 if (getstati(STAT_HEALTH) <= 0)
1688                         return;
1689         }
1690
1691         vector pos, mySize;
1692         pos = HUD_Panel_GetPos(id);
1693         mySize = HUD_Panel_GetSize(id);
1694
1695         HUD_Panel_DrawBg(id, pos, mySize, 0);
1696         float padding;
1697         padding = HUD_Panel_GetPadding(id);
1698         if(padding)
1699         {
1700                 pos += '1 1 0' * padding;
1701                 mySize -= '2 2 0' * padding;
1702         }
1703
1704         float strength_time, shield_time;
1705
1706         strength_time = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
1707         shield_time = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
1708
1709         if(hud_configure)
1710         {
1711                 strength_time = 15;
1712                 shield_time = 27;
1713         }
1714
1715         float len;
1716
1717         vector barpos, barsize;
1718         vector picpos;
1719         vector numpos;
1720
1721         string leftname, rightname;
1722         float leftcnt, rightcnt;
1723         float leftexact, rightexact;
1724         float leftalpha, rightalpha;
1725         if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) {
1726                 leftname = "strength";
1727                 leftcnt = ceil(strength_time);
1728                 leftexact = strength_time;
1729
1730                 rightname = "shield";
1731                 rightcnt = ceil(shield_time);
1732                 rightexact = shield_time;
1733         } else {
1734                 leftname = "shield";
1735                 leftcnt = ceil(shield_time);
1736                 leftexact = shield_time;
1737
1738                 rightname = "strength";
1739                 rightcnt = ceil(strength_time);
1740                 rightexact = strength_time;
1741         }
1742         leftalpha = bound(0, leftexact, 1);
1743         rightalpha = bound(0, rightexact, 1);
1744
1745         if (mySize_x/mySize_y > 4)
1746         {
1747                 if(leftcnt)
1748                 {
1749                         len = strlen(ftos(leftcnt));
1750
1751                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1752                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/30);
1753                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1754                                 picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
1755                                 numpos = picpos - eX * 2 * 0.5 * mySize_y + eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1756                         } else {
1757                                 barpos = pos;
1758                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1759                                 picpos = pos;
1760                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1761                         }
1762
1763                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1764                         if(leftcnt <= 5)
1765                                 drawpic_skin_expanding_two(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE, bound(0, (leftcnt - leftexact) / 0.5, 1));
1766                         else
1767                                 drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1768                         HUD_DrawXNum(numpos, leftcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1769                 }
1770
1771                 if(rightcnt)
1772                 {
1773                         len = strlen(ftos(rightcnt));
1774
1775                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1776                                 barpos = pos + eX * 0.5 * mySize_x;
1777                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1778                                 picpos = pos + eX * 0.5 * mySize_x;
1779                                 numpos = picpos + eX * mySize_y - eX * (2-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
1780                         } else {
1781                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/30);
1782                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1783                                 picpos = pos + eX * mySize_x - eX * mySize_y;
1784                                 numpos = picpos - eX * mySize_y + eY * 0.25 * mySize_y;
1785                         }
1786
1787                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1788                         if(rightcnt <= 5)
1789                                 drawpic_skin_expanding_two(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE, bound(0, (rightcnt - rightexact) / 0.5, 1));
1790                         else
1791                                 drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1792                         HUD_DrawXNum(numpos, rightcnt, 2, 0, 0.5 * mySize_y, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1793                 }
1794         }
1795         else if (mySize_x/mySize_y > 1.5)
1796         {
1797                 if(leftcnt)
1798                 {
1799                         len = strlen(ftos(leftcnt));
1800
1801                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1802                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/30);
1803                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1804                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
1805                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1806                         } else {
1807                                 barpos = pos;
1808                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1809                                 picpos = pos;
1810                                 numpos = picpos + eX * 0.5 * mySize_y;
1811                         }
1812
1813                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1814                         if(leftcnt <= 5)
1815                                 drawpic_skin_expanding_two(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE, bound(0, (leftcnt - leftexact) / 0.5, 1));
1816                         else
1817                                 drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1818                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1819                 }
1820
1821                 if(rightcnt)
1822                 {
1823                         len = strlen(ftos(rightcnt));
1824
1825                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1826                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1827                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1828                                 picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
1829                                 numpos = picpos - eX * len * 0.5 * mySize_y;
1830                         } else {
1831                                 barpos = pos + eY * 0.5 * mySize_y;
1832                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1833                                 picpos = pos + eY * 0.5 * mySize_y;
1834                                 numpos = picpos + eX * 0.5 * mySize_y;
1835                         }
1836
1837                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1838                         if(rightcnt <= 5)
1839                                 drawpic_skin_expanding_two(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE, bound(0, (rightcnt - rightexact) / 0.5, 1));
1840                         else
1841                                 drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1842                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.5 * mySize_y, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1843                 }
1844         }
1845         else
1846         {
1847                 if(leftcnt)
1848                 {
1849                         len = strlen(ftos(leftcnt));
1850
1851                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1852                                 barpos = pos;
1853                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1854                                 picpos = pos + eX * 0.05 * mySize_x;
1855                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
1856                         } else {
1857                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/30);
1858                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1859                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x;
1860                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x;
1861                         }
1862
1863                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1864                         if(leftcnt <= 5)
1865                                 drawpic_skin_expanding_two(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE, bound(0, (leftcnt - leftexact) / 0.5, 1));
1866                         else
1867                                 drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1868                         HUD_DrawXNum(numpos, leftcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1869                 }
1870
1871                 if(rightcnt)
1872                 {
1873                         len = strlen(ftos(rightcnt));
1874
1875                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1876                                 barpos = pos + eX * 0.5 * mySize_x;
1877                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1878                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1879                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1880                         } else {
1881                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/30) + eX * 0.5 * mySize_x;
1882                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1883                                 picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.65 * mySize_x + eX * 0.5 * mySize_x;
1884                                 numpos = pos + eX * ((2-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
1885                         }
1886
1887                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1888                         if(rightcnt <= 5)
1889                                 drawpic_skin_expanding_two(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE, bound(0, (rightcnt - rightexact) / 0.5, 1));
1890                         else
1891                                 drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1892                         HUD_DrawXNum(numpos, rightcnt, len, 0, 0.25 * mySize_x, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1893                 }
1894         }
1895 }
1896
1897 // Health/armor (#3)
1898 //
1899 void HUD_HealthArmor(void)
1900 {
1901         float id = HUD_PANEL_HEALTHARMOR;
1902         vector pos, mySize;
1903         pos = HUD_Panel_GetPos(id);
1904         mySize = HUD_Panel_GetSize(id);
1905
1906         HUD_Panel_DrawBg(id, pos, mySize, 0);
1907         float padding;
1908         padding = HUD_Panel_GetPadding(id);
1909         if(padding)
1910         {
1911                 pos += '1 1 0' * padding;
1912                 mySize -= '2 2 0' * padding;
1913         }
1914
1915         float armor, health;
1916         armor = getstati(STAT_ARMOR);
1917         health = getstati(STAT_HEALTH);
1918
1919         float fuel;
1920         fuel = getstati(GetAmmoStat(4)); // how much fuel do we have?
1921
1922         if(hud_configure)
1923         {
1924                 armor = 150;
1925                 health = 100;
1926                 fuel = 70;
1927         }
1928
1929         if(health <= 0)
1930                 return;
1931
1932         float len;
1933         vector barpos, barsize;
1934         vector picpos;
1935         vector numpos;
1936
1937         if(cvar("hud_healtharmor") == 2) // combined health and armor display
1938         {
1939                 vector v;
1940                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
1941
1942                 float x;
1943                 x = floor(v_x + 1);
1944
1945                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1946                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, x/400);
1947                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
1948                         picpos = pos;
1949                         numpos = picpos + eX * 1.5 * mySize_y;
1950                 } else {
1951                         barpos = pos;
1952                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
1953                         picpos = pos + eX * 3 * mySize_y;
1954                         numpos = pos;
1955                 }
1956
1957                 if(v_z) // NOT fully armored
1958                 {
1959                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("health"), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1960                         drawpic_skin(picpos, "health", '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1961                         if(armor)
1962                                 drawpic_skin(picpos + eX * mySize_y, "armor", '0.5 0.5 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id) * armor / health, DRAWFLAG_NORMAL);
1963                 }
1964                 else
1965                 {
1966                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("armor"), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1967                         drawpic_skin(picpos + eX * mySize_y, "health", '0.5 0.5 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id) * health / armor, DRAWFLAG_NORMAL);
1968                         if(armor)
1969                                 drawpic_skin(picpos, "armor", '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1970                 }
1971                 HUD_DrawXNum_Colored(numpos, x, 3, mySize_y, HUD_Panel_GetFgAlpha(id)); // draw the combined health and armor
1972
1973                 // fuel
1974                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
1975                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
1976                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
1977                 } else {
1978                         barpos = pos;
1979                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
1980                 }
1981                 if(fuel)
1982                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("fuel"), HUD_Panel_GetFgAlpha(id) * 0.8, DRAWFLAG_NORMAL);
1983         }
1984
1985         else
1986         {
1987                 string leftname, rightname;
1988                 float leftcnt, rightcnt;
1989                 float leftactive, rightactive;
1990                 float leftalpha, rightalpha;
1991                 if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) { // old style layout with armor left/top of health
1992                         leftname = "armor";
1993                         leftcnt = armor;
1994                         if(leftcnt)
1995                                 leftactive = 1;
1996                         leftalpha = min((armor+10)/55, 1);
1997
1998                         rightname = "health";
1999                         rightcnt = health;
2000                         rightactive = 1;
2001                         rightalpha = 1;
2002                 } else {
2003                         leftname = "health";
2004                         leftcnt = health;
2005                         leftactive = 1;
2006                         leftalpha = 1;
2007
2008                         rightname = "armor";
2009                         rightcnt = armor;
2010                         if(rightcnt)
2011                                 rightactive = 1;
2012                         rightalpha = min((armor+10)/55, 1);
2013                 }
2014
2015                 if (mySize_x/mySize_y > 5)
2016                 {
2017                         if(leftactive)
2018                         {
2019                                 len = strlen(ftos(leftcnt));
2020
2021                                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
2022                                         barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/200);
2023                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
2024                                         picpos = pos + eX * 0.5 * mySize_x - eX * mySize_y;
2025                                         numpos = picpos - eX * 3 * 0.5 * mySize_y + eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
2026                                 } else {
2027                                         barpos = pos;
2028                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
2029                                         picpos = pos;
2030                                         numpos = picpos + eX * mySize_y + eY * 0.25 * mySize_y;
2031                                 }
2032
2033                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2034                                 drawpic_skin(picpos, leftname, '1 1 0' * mySize_y, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2035                                 HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, HUD_Panel_GetFgAlpha(id));
2036                         }
2037
2038                         if(rightactive)
2039                         {
2040                                 len = strlen(ftos(rightcnt));
2041
2042                                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
2043                                         barpos = pos + eX * 0.5 * mySize_x;
2044                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
2045                                         picpos = pos + eX * 0.5 * mySize_x;
2046                                         numpos = picpos + eX * mySize_y - eX * (3-len) * 0.5 * mySize_y + eY * 0.25 * mySize_y;
2047                                 } else {
2048                                         barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/200);
2049                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
2050                                         picpos = pos + eX * mySize_x - eX * mySize_y;
2051                                         numpos = picpos - eX * 1.5 * mySize_y + eY * 0.25 * mySize_y;
2052                                 }
2053
2054                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2055                                 drawpic_skin(picpos, rightname, '1 1 0' * mySize_y, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2056                                 HUD_DrawXNum_Colored(numpos, rightcnt, 3, 0.5 * mySize_y, HUD_Panel_GetFgAlpha(id));
2057                         }
2058
2059                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
2060                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2061                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2062                         } else {
2063                                 barpos = pos;
2064                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2065                         }
2066                         if(fuel)
2067                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("fuel"), HUD_Panel_GetFgAlpha(id) * 0.8, DRAWFLAG_NORMAL);
2068                 }
2069                 else if (mySize_x/mySize_y > 2)
2070                 {
2071                         if(leftactive)
2072                         {
2073                                 len = strlen(ftos(leftcnt));
2074
2075                                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
2076                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/200);
2077                                         barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
2078                                         picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y;
2079                                         numpos = picpos - eX * len * 0.5 * mySize_y;
2080                                 } else {
2081                                         barpos = pos;
2082                                         barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
2083                                         picpos = pos;
2084                                         numpos = picpos + eX * 0.5 * mySize_y;
2085                                 }
2086
2087                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2088                                 drawpic_skin(picpos, leftname, '0.5 0.5 0' * mySize_y, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2089                                 HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.5 * mySize_y, HUD_Panel_GetFgAlpha(id));
2090                         }
2091
2092                         if(rightactive)
2093                         {
2094                                 len = strlen(ftos(rightcnt));
2095
2096                                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
2097                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2098                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2099                                         picpos = pos + eX * mySize_x - eX * 0.5 * mySize_y + eY * 0.5 * mySize_y;
2100                                         numpos = picpos - eX * len * 0.5 * mySize_y;
2101                                 } else {
2102                                         barpos = pos + eY * 0.5 * mySize_y;
2103                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2104                                         picpos = pos + eY * 0.5 * mySize_y;
2105                                         numpos = picpos + eX * 0.5 * mySize_y;
2106                                 }
2107
2108                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2109                                 drawpic_skin(picpos, rightname, '0.5 0.5 0' * mySize_y, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2110                                 HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.5 * mySize_y, HUD_Panel_GetFgAlpha(id));
2111                         }
2112
2113                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
2114                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2115                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2116                         } else {
2117                                 barpos = pos;
2118                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2119                         }
2120                         if(fuel)
2121                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("fuel"), HUD_Panel_GetFgAlpha(id) * 0.8, DRAWFLAG_NORMAL);
2122                 }
2123                 else
2124                 {
2125                         if(leftactive)
2126                         {
2127                                 len = strlen(ftos(leftcnt));
2128
2129                                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
2130                                         barpos = pos;
2131                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2132                                         picpos = pos + eX * 0.05 * mySize_x;
2133                                         numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x;
2134                                 } else {
2135                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/200);
2136                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2137                                         picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x;
2138                                         numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x;
2139                                 }
2140
2141                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2142                                 drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2143                                 HUD_DrawXNum_Colored(numpos, leftcnt, len, 0.166 * mySize_x, HUD_Panel_GetFgAlpha(id));
2144                         }
2145
2146                         if(rightactive)
2147                         {
2148                                 len = strlen(ftos(rightcnt));
2149
2150                                 if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
2151                                         barpos = pos + eX * 0.5 * mySize_x;
2152                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2153                                         picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2154                                         numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2155                                 } else {
2156                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/200) + eX * 0.5 * mySize_x;
2157                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2158                                         picpos = pos + eX * 0.05 * mySize_x + eY * mySize_y - eY * 0.566 * mySize_x + eX * 0.5 * mySize_x;
2159                                         numpos = pos + eX * ((3-len)/2) * 0.25 * mySize_x + eY * mySize_y - eY * 0.166 * mySize_x + eX * 0.5 * mySize_x;
2160                                 }
2161
2162                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2163                                 drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2164                                 HUD_DrawXNum_Colored(numpos, rightcnt, len, 0.166 * mySize_x, HUD_Panel_GetFgAlpha(id));
2165                         }
2166
2167                         if(cvar(strcat("hud_", HUD_Panel_GetName(id), "_mirror"))) {
2168                                 barpos = pos;
2169                                 barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2170                         } else {
2171                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, fuel/100);
2172                                 barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2173                         }
2174                         if(fuel)
2175                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor("fuel"), HUD_Panel_GetFgAlpha(id) * 0.8, DRAWFLAG_NORMAL);
2176                 }
2177         }
2178 }
2179
2180 // ___TODO___ !!!
2181 // Notification area (#4)
2182 //
2183
2184 string Weapon_SuicideMessage(float deathtype)
2185 {
2186         w_deathtype = deathtype;
2187         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_SUICIDEMESSAGE);
2188         return w_deathtypestring;
2189 }
2190
2191 string Weapon_KillMessage(float deathtype)
2192 {
2193         w_deathtype = deathtype;
2194         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_KILLMESSAGE);
2195         return w_deathtypestring;
2196 }
2197
2198 float killnotify_times[10];
2199 float killnotify_deathtype[10];
2200 float killnotify_actiontype[10]; // 0 = "Y [used by] X", 1 = "X [did action to] Y"
2201 string killnotify_attackers[10];
2202 string killnotify_victims[10];
2203 void HUD_KillNotify_Push(string attacker, string victim, float actiontype, float wpn)
2204 {
2205         float i;
2206         for (i = 9; i > 0; --i) {
2207                 killnotify_times[i] = killnotify_times[i-1];
2208                 killnotify_deathtype[i] = killnotify_deathtype[i-1];
2209                 killnotify_actiontype[i] = killnotify_actiontype[i-1];
2210                 if(killnotify_attackers[i])
2211                         strunzone(killnotify_attackers[i]);
2212                 killnotify_attackers[i] = strzone(killnotify_attackers[i-1]);
2213                 if(killnotify_victims[i])
2214                         strunzone(killnotify_victims[i]);
2215                 killnotify_victims[i] = strzone(killnotify_victims[i-1]);
2216         }
2217         killnotify_times[0] = time;
2218         killnotify_deathtype[0] = wpn;
2219         killnotify_actiontype[0] = actiontype;
2220         if(killnotify_attackers[0])
2221                 strunzone(killnotify_attackers[0]);
2222         killnotify_attackers[0] = strzone(attacker);
2223         if(killnotify_victims[0])
2224                 strunzone(killnotify_victims[0]);
2225         killnotify_victims[0] = strzone(victim);
2226 }
2227
2228 void HUD_KillNotify(string s1, string s2, string s3, float type, float msg)
2229 {
2230         float w;
2231         float alsoprint;
2232         alsoprint = (cvar("hud_notify_print") || !HUD_Panel_CheckActive(4)); // print message to console if: notify panel disabled, or cvar to do so enabled
2233         
2234         if(msg == MSG_SUICIDE) {
2235                 // TODO: cl_gentle
2236                 w = DEATH_WEAPONOF(type);
2237                 if(WEP_VALID(w)) {
2238                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2239                         if (alsoprint)
2240                                 print("^1", s1, "^1 ", Weapon_SuicideMessage(type), "\n");
2241                 } else if (type == DEATH_KILL) {
2242                         HUD_KillNotify_Push(s1, "", 0, DEATH_KILL);
2243                         if (alsoprint)
2244                                 print ("^1",s1, "^1 couldn't take it anymore\n");
2245                 } else if (type == DEATH_ROT) {
2246                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2247                         if (alsoprint)
2248                                 print ("^1",s1, "^1 died\n");
2249                 } else if (type == DEATH_NOAMMO) {
2250                         HUD_KillNotify_Push(s1, "", 0, DEATH_NOAMMO);
2251                         if (alsoprint)
2252                                 print ("^7",s1, "^7 committed suicide. What's the point of living without ammo?\n");
2253                 } else if (type == DEATH_CAMP) {
2254                         HUD_KillNotify_Push(s1, "", 0, DEATH_CAMP);
2255                         if (alsoprint)
2256                                 print ("^1",s1, "^1 thought they found a nice camping ground\n");
2257                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2258                         HUD_KillNotify_Push(s1, "", 0, type);
2259                         if (alsoprint)
2260                                 print ("^1",s1, "^1 didn't become friends with the Lord of Teamplay\n");
2261                 } else if (type == DEATH_CHEAT) {
2262                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2263                         if (alsoprint)
2264                                 print ("^1",s1, "^1 unfairly eliminated themself\n");
2265                 } else if (type == DEATH_FIRE) {
2266                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2267                         if (alsoprint)
2268                                 print ("^1",s1, "^1 burned to death\n");
2269                 } else if (type != DEATH_TEAMCHANGE && type != DEATH_QUIET) {
2270                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2271                         if (alsoprint)
2272                                 print ("^1",s1, "^1 couldn't resist the urge to self-destruct\n");
2273                 } 
2274                 
2275                 if (stof(s2) > 2) // killcount > 2
2276                         print ("^1",s1,"^1 ended it all after a ",s2," kill spree\n");
2277         } else if(msg == MSG_KILL) {
2278                 w = DEATH_WEAPONOF(type);
2279                 if(WEP_VALID(w)) {
2280                         HUD_KillNotify_Push(s2, s1, 1, w);
2281                         if (alsoprint)
2282                                 print("^1", s1, "^1 ", Weapon_KillMessage(type), "\n");
2283                 }
2284                 else if(type == KILL_TEAM_RED || type == KILL_TEAM_BLUE || type == KILL_TEAM_SPREE) {
2285                         HUD_KillNotify_Push(s1, s2, 1, type);
2286                         if(alsoprint)
2287                         {
2288                                 if(cvar("cl_gentle")) {
2289                                         print ("^1", s1, "^1 took action against a team mate\n");
2290                                 } else {
2291                                         print ("^1", s1, "^1 mows down a team mate\n");
2292                                 }
2293                         }
2294                         if (stof(s2) > 2 && type == KILL_TEAM_SPREE) {
2295                                 if(cvar("cl_gentle"))
2296                                         print ("^1",s1,"^1 ended a ",s3," scoring spree by going against a team mate\n");
2297                                 else
2298                                         print ("^1",s1,"^1 ended a ",s3," kill spree by killing a team mate\n");
2299                         }
2300                         else if (stof(s2) > 2) {
2301                                 if(cvar("cl_gentle"))
2302                                         print ("^1",s1,"'s ^1",s3," scoring spree was ended by a team mate!\n");
2303                                 else
2304                                         print ("^1",s1,"'s ^1",s3," kill spree was ended by a team mate!\n");
2305                         }
2306                 }
2307                 else if(type == KILL_FIRST_BLOOD)
2308                         print("^1",s1, "^1 drew first blood", "\n");
2309                 // TODO: icon!
2310                 else if (type == DEATH_TELEFRAG)
2311                         print ("^1",s1, "^1 was telefragged by ", s2, "\n");
2312                 else if (type == DEATH_DROWN) {
2313                         HUD_KillNotify_Push(s2, s1, 1, DEATH_DROWN);
2314                         if(alsoprint)
2315                                 print ("^1",s1, "^1 was drowned by ", s2, "\n");
2316                 }
2317                 else if (type == DEATH_SLIME) {
2318                         HUD_KillNotify_Push(s2, s1, 1, DEATH_SLIME);
2319                         if(alsoprint)
2320                                 print ("^1",s1, "^1 was slimed by ", s2, "\n");
2321                 }
2322                 else if (type == DEATH_LAVA) {
2323                         HUD_KillNotify_Push(s2, s1, 1, DEATH_LAVA);
2324                         if(alsoprint)
2325                                 print ("^1",s1, "^1 was cooked by ", s2, "\n");
2326                 }
2327                 else if (type == DEATH_FALL) {
2328                         HUD_KillNotify_Push(s2, s1, 1, DEATH_FALL);
2329                         if(alsoprint)
2330                                 print ("^1",s1, "^1 was grounded by ", s2, "\n");
2331                 }
2332                 else if (type == DEATH_SHOOTING_STAR) {
2333                         HUD_KillNotify_Push(s2, s1, 1, DEATH_SHOOTING_STAR);
2334                         if(alsoprint)
2335                                 print ("^1",s1, "^1 was shot into space by ", s2, "\n");
2336                 }
2337                 else if (type == DEATH_SWAMP) {
2338                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2339                         if(alsoprint)
2340                                 print ("^1",s1, "^1 was conserved by ", s2, "\n");
2341                 }
2342                 else if (type == DEATH_HURTTRIGGER)
2343                 {
2344                         HUD_KillNotify_Push(s2, s1, 1, DEATH_HURTTRIGGER);
2345                         if(alsoprint)
2346                                 print("^1",s1, "^1 was thrown into a world of hurt by ", s2, "\n");
2347                 } else if(type == DEATH_SBCRUSH) {
2348                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2349                         if(alsoprint)
2350                                 print ("^1",s1, "^1 was crushed by ^1", s2, "\n");
2351                 } else if(type == DEATH_SBMINIGUN) {
2352                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2353                         if(alsoprint)
2354                                 print ("^1",s1, "^1 got shredded by ^1", s2, "\n");
2355                 } else if(type == DEATH_SBROCKET) {
2356                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2357                         if(alsoprint)
2358                                 print ("^1",s1, "^1 was blased to bits by ^1", s2, "\n");
2359                 } else if(type == DEATH_SBBLOWUP) {
2360                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2361                         if(alsoprint)
2362                                 print ("^1",s1, "^1 got caught in the destruction of ^1", s2, "'s vehicle\n");
2363                 } else if(type == DEATH_WAKIGUN) {
2364                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2365                         if(alsoprint)
2366                                 print ("^1",s1, "^1 was bolted down by ^1", s2, "\n");
2367                 } else if(type == DEATH_WAKIROCKET) {
2368                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2369                         if(alsoprint)
2370                                 print ("^1",s1, "^1 could find no shelter from ^1", s2, "'s rockets\n");
2371                 } else if(type == DEATH_WAKIBLOWUP) {
2372                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2373                         if(alsoprint)
2374                                 print ("^1",s1, "^1 dies when ^1", s2, "'s wakizashi dies.\n");
2375                 } else if(type == DEATH_TURRET) {
2376                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2377                         if(alsoprint)
2378                                 print ("^1",s1, "^1 was pushed into the line of fire by ^1", s2, "\n");
2379                 } else if(type == DEATH_TOUCHEXPLODE) {
2380                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2381                         if(alsoprint)
2382                                 print ("^1",s1, "^1 was pushed into an accident by ^1", s2, "\n");
2383                 } else if(type == DEATH_CHEAT) {
2384                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2385                         if(alsoprint)
2386                                 print ("^1",s1, "^1 was unfairly eliminated by ^1", s2, "\n");
2387                 } else if (type == DEATH_FIRE) {
2388                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2389                         if(alsoprint)
2390                                 print ("^1",s1, "^1 was burnt to death by ^1", s2, "\n");
2391                 } else if (type == DEATH_CUSTOM) {
2392                         HUD_KillNotify_Push(s2, s1, 1, DEATH_CUSTOM);
2393                         if(alsoprint)
2394                                 print ("^1",s1, "^1 ", s2, "\n");
2395                 } else {
2396                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2397                         if(alsoprint)
2398                                 print ("^1",s1, "^1 was fragged by ", s2, "\n");
2399                 }
2400         } else if(msg == MSG_SPREE) {
2401                 if(type == KILL_END_SPREE) {
2402                         if(cvar("cl_gentle"))
2403                                 print ("^1",s1,"'s ^1", s2, " scoring spree was ended by ", s3, "\n");
2404                         else
2405                                 print ("^1",s1,"'s ^1", s2, " kill spree was ended by ", s3, "\n");
2406                 } else if(type == KILL_SPREE) {
2407                         if(cvar("cl_gentle"))
2408                                 print ("^1",s1,"^1 made ",s2," scores in a row\n");
2409                         else
2410                                 print ("^1",s1,"^1 has ",s2," frags in a row\n");
2411                 } else if(type == KILL_SPREE_3) {
2412                         if(cvar("cl_gentle"))
2413                                 print (s1,"^7 made a ^1TRIPLE SCORE\n");
2414                         else
2415                                 print (s1,"^7 made a ^1TRIPLE FRAG\n");
2416                 } else if(type == KILL_SPREE_5) {
2417                         if(cvar("cl_gentle"))
2418                                 print (s1,"^7 unleashes ^1SCORING RAGE\n");
2419                         else
2420                                 print (s1,"^7 unleashes ^1RAGE\n");
2421                 } else if(type == KILL_SPREE_10) {
2422                         if(cvar("cl_gentle"))
2423                                 print (s1,"^7 made ^1TEN SCORES IN A ROW!\n");
2424                         else
2425                                 print (s1,"^7 starts the ^1MASSACRE!\n");
2426                 } else if(type == KILL_SPREE_15) {
2427                         if(cvar("cl_gentle"))
2428                                 print (s1,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
2429                         else
2430                                 print (s1,"^7 executes ^1MAYHEM!\n");
2431                 } else if(type == KILL_SPREE_20) {
2432                         if(cvar("cl_gentle"))
2433                                 print (s1,"^7 made ^1TWENTY SCORES IN A ROW!\n");
2434                         else
2435                                 print (s1,"^7 is a ^1BERSERKER!\n");
2436                 } else if(type == KILL_SPREE_25) {
2437                         if(cvar("cl_gentle"))
2438                                 print (s1,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
2439                         else
2440                                 print (s1,"^7 inflicts ^1CARNAGE!\n");
2441                 } else if(type == KILL_SPREE_30) {
2442                         if(cvar("cl_gentle"))
2443                                 print (s1,"^7 made ^1THIRTY SCORES IN A ROW!\n");
2444                         else
2445                                 print (s1,"^7 unleashes ^1ARMAGEDDON!\n");
2446                 }
2447         } else if(msg == MSG_KILL_ACTION) { // wtf is this? isnt it basically the same as MSG_SUICIDE?
2448                 if (type == DEATH_DROWN) {
2449                         HUD_KillNotify_Push(s1, "", 0, DEATH_DROWN);
2450                         if(alsoprint)
2451                         {
2452                                 if(cvar("cl_gentle"))
2453                                         print ("^1",s1, "^1 was in the water for too long\n");
2454                                 else
2455                                         print ("^1",s1, "^1 drowned\n");
2456                         }
2457                 } else if (type == DEATH_SLIME) {
2458                         HUD_KillNotify_Push(s1, "", 0, DEATH_SLIME);
2459                         if(alsoprint)
2460                                 print ("^1",s1, "^1 was slimed\n");
2461                 } else if (type == DEATH_LAVA) {
2462                         HUD_KillNotify_Push(s1, "", 0, DEATH_LAVA);
2463                         if(alsoprint)
2464                         {
2465                                 if(cvar("cl_gentle"))
2466                                         print ("^1",s1, "^1 found a hot place\n");
2467                                 else
2468                                         print ("^1",s1, "^1 turned into hot slag\n");
2469                         }
2470                 } else if (type == DEATH_FALL) {
2471                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2472                         if(alsoprint)
2473                         {
2474                                 if(cvar("cl_gentle"))
2475                                         print ("^1",s1, "^1 tested gravity (and it worked)\n");
2476                                 else
2477                                         print ("^1",s1, "^1 hit the ground with a crunch\n");
2478                         }
2479                 } else if (type == DEATH_SHOOTING_STAR) {
2480                         HUD_KillNotify_Push(s1, "", 0, DEATH_SHOOTING_STAR);
2481                         if(alsoprint)
2482                                 print ("^1",s1, "^1 became a shooting star\n");
2483                 } else if (type == DEATH_SWAMP) {
2484                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2485                         if(alsoprint)
2486                         {
2487                                 if(cvar("cl_gentle"))
2488                                         print ("^1",s1, "^1 discovered a swamp\n");
2489                                 else
2490                                         print ("^1",s1, "^1 is now conserved for centuries to come\n");
2491                         }
2492                 } else if(type == DEATH_TURRET) {
2493                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2494                         if(alsoprint)
2495                                 print ("^1",s1, "^1 was mowed down by a turret \n");
2496                 } else if (type == DEATH_CUSTOM) {
2497                         HUD_KillNotify_Push(s1, "", 0, DEATH_CUSTOM);
2498                         if(alsoprint)
2499                                 print ("^1",s1, "^1 ", s2, "\n");
2500                 } else if (type == DEATH_HURTTRIGGER) {
2501                         HUD_KillNotify_Push(s1, "", 0, DEATH_HURTTRIGGER);
2502                         if(alsoprint)
2503                                 print ("^1",s1, "^1 was in the wrong place\n");
2504                 } else if(type == DEATH_TOUCHEXPLODE) {
2505                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2506                         if(alsoprint)
2507                                 print ("^1",s1, "^1 died in an accident\n");
2508                 } else if(type == DEATH_CHEAT) {
2509                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2510                         if(alsoprint)
2511                                 print ("^1",s1, "^1 was unfairly eliminated\n");
2512                 } else if(type == DEATH_FIRE) {
2513                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2514                         if(alsoprint)
2515                         {
2516                                 if(cvar("cl_gentle"))
2517                                         print ("^1",s1, "^1 felt a little hot\n");
2518                                 else
2519                                         print ("^1",s1, "^1 burnt to death\n");
2520                                 }
2521                 } else {
2522                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2523                         if(alsoprint)
2524                         {
2525                                 if(cvar("cl_gentle"))
2526                                         print ("^1",s1, "^1 needs a restart\n");
2527                                 else
2528                                         print ("^1",s1, "^1 died\n");
2529                         }
2530                 }
2531         } else if(msg == MSG_KILL_ACTION_SPREE) {
2532                 if(cvar("cl_gentle"))
2533                         print ("^1",s1,"^1 needs a restart after a ",s2," scoring spree\n");
2534                 else
2535                         print ("^1",s1,"^1 died with a ",s2," kill spree\n");
2536         } else if(msg == MSG_INFO) {
2537                 if(type == INFO_GOTFLAG) { // here, s2 is the flag name
2538                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2539                         print(s1, "^7 got the ", s2, "\n");
2540                 } else if(type == INFO_LOSTFLAG) {
2541                         HUD_KillNotify_Push(s1, s2, 0, INFO_LOSTFLAG);
2542                         print(s1, "^7 lost the ", s2, "\n");
2543                 } else if(type == INFO_PICKUPFLAG) {
2544                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2545                         print(s1, "^7 picked up the ", s2, "\n");
2546                 } else if(type == INFO_RETURNFLAG) {
2547                         HUD_KillNotify_Push(s1, s2, 0, INFO_RETURNFLAG);
2548                         print(s1, "^7 returned the ", s2, "\n");
2549                 }
2550         }
2551 }
2552
2553 #define DAMAGE_CENTERPRINT_SPACER NEWLINES
2554
2555 void HUD_Centerprint(string s1, string s2, float type, float msg)
2556 {
2557         if(msg == MSG_SUICIDE) {
2558                 if (type == DEATH_TEAMCHANGE) {
2559                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You are now on: ", s1));
2560                 } else if (type == DEATH_AUTOTEAMCHANGE) {
2561                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You have been moved into a different team to improve team balance\nYou are now on: ", s1));
2562                 } else if (type == DEATH_CAMP) {
2563                         if(cvar("cl_gentle"))
2564                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Reconsider your tactics, camper!"));
2565                         else
2566                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Die camper!"));
2567                 } else if (type == DEATH_NOAMMO) {
2568                         if(cvar("cl_gentle"))
2569                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You are reinserted into the game for running out of ammo..."));
2570                         else
2571                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were killed for running out of ammo..."));
2572                 } else if (type == DEATH_ROT) {
2573                         if(cvar("cl_gentle"))
2574                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to preserve your health"));
2575                         else
2576                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You grew too old without taking your medicine"));
2577                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2578                         if(cvar("cl_gentle"))
2579                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't go against team mates!"));
2580                         else
2581                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't shoot your team mates!"));
2582                 } else if (type == DEATH_QUIET) {
2583                         // do nothing
2584                 } else if (type == DEATH_KILL) {
2585                         if(cvar("cl_gentle"))
2586                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to be more careful!"));
2587                         else
2588                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You killed your own dumb self!"));
2589                 }
2590         } else if(msg == MSG_KILL) {
2591                 if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2592                         if(cvar("cl_gentle")) {
2593                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You went against", s1, ",a team mate!"));
2594                         } else {
2595                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You fragged ", s1, ", a team mate!"));
2596                         }
2597                 } else if (type == KILL_FIRST_BLOOD) {
2598                         if(cvar("cl_gentle")) {
2599                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First score"));
2600                         } else {
2601                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First blood"));
2602                         }
2603                 } else if (type == KILL_FIRST_VICTIM) {
2604                         if(cvar("cl_gentle")) {
2605                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First casualty"));
2606                         } else {
2607                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First victim"));
2608                         }
2609                 } else if (type == KILL_TYPEFRAG) { // s2 contains "advanced kill messages" such as ping, handicap...
2610                         if(cvar("cl_gentle")) {
2611                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You scored against ^7", s1, "^1 who was typing!", s2));
2612                         } else {
2613                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You typefragged ^7", s1, s2));
2614                         }
2615                 } else if (type == KILL_TYPEFRAGGED) {
2616                         if(cvar("cl_gentle")) {
2617                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, "^1 while you were typing!", s2));
2618                         } else {
2619                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were typefragged by ^7", s1, s2));
2620                         }
2621                 } else if (type == KILL_FRAG) {
2622                         if(cvar("cl_gentle")) {
2623                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You scored against ^7", s1, s2));
2624                         } else {
2625                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You fragged ^7", s1, s2));
2626                         }
2627                 } else if (type == KILL_FRAGGED) {
2628                         if(cvar("cl_gentle")) {
2629                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, s2));
2630                         } else {
2631                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were fragged by ^7", s1, s2));
2632                         }
2633                 }
2634         } else if(msg == MSG_KILL_ACTION) {
2635                 // TODO: invent more centerprints here?
2636                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
2637         }
2638 }
2639
2640 void HUD_Notify (void)
2641 {
2642         float id = HUD_PANEL_NOTIFY;
2643         vector pos, mySize;
2644         pos = HUD_Panel_GetPos(id);
2645         mySize = HUD_Panel_GetSize(id);
2646
2647         HUD_Panel_DrawBg(id, pos, mySize, 0);
2648         float padding;
2649         padding = HUD_Panel_GetPadding(id);
2650         if(padding)
2651         {
2652                 pos += '1 1 0' * padding;
2653                 mySize -= '2 2 0' * padding;
2654         }
2655
2656         float entries, height;
2657         entries = bound(1, floor(14 * mySize_y/mySize_x), 12);
2658         height = mySize_y/entries;
2659         entries -= 2; // top/bottom two lines reserved for info messaged, such as spec instructions
2660         
2661         vector fontsize;
2662         fontsize = '0.5 0.5 0' * height;
2663
2664         float a;
2665         float when;
2666         when = cvar("hud_notify_time");
2667         float fadetime;
2668         fadetime = cvar("hud_notify_fadetime");
2669
2670         string s;
2671
2672         vector pos_attacker, pos_victim;
2673         vector weap_pos;
2674         float width_attacker, width_victim;
2675         string attacker, victim;
2676
2677         float i, j;
2678         for(j = 0; j < entries; ++j)
2679         {
2680                 if(cvar("hud_notify_flip"))
2681                         i = j;
2682                 else // rather nasty hack for ordering items from the bottom up
2683                         i = entries - j - 1;
2684                 if(cvar("hud_notify_info_top"))
2685                         i += 2; // top/bottom two lines reserved for info messaged, such as spec instructions
2686
2687                 if(fadetime)
2688                 {
2689                         if(killnotify_times[j] + when > time)
2690                                 a = 1;
2691                         else
2692                                 a = bound(0, (killnotify_times[j] + when + fadetime - time) / fadetime, 1);
2693                 }
2694                 else
2695                 {
2696                         if(killnotify_times[j] + when > time)
2697                                 a = 1;
2698                         else
2699                                 a = 0;
2700                 }
2701
2702                 // TODO: maybe print in team colors?
2703                 // TODO: maybe this could be cleaned up somehow...
2704                 // TODO: less copypaste code below
2705                 //
2706                 // Y [used by] X
2707                 if(killnotify_actiontype[j] == 0 && !hud_configure) 
2708                 {
2709                         attacker = textShortenToWidth(killnotify_attackers[j], mySize_x - 2 * height, fontsize, stringwidth_colors);
2710
2711                         width_attacker = stringwidth(attacker, TRUE, fontsize);
2712                         pos_attacker = pos + eX * 0.5 * (mySize_x - width_attacker + 2 * height) + eY * 0.5 * fontsize_y + eY * i * height;
2713
2714                         weap_pos = pos + eX * 0.5 * (mySize_x - width_attacker) - eX * height + eY * i * height;
2715                         if(killnotify_deathtype[j] == DEATH_GENERIC)
2716                         {
2717                                 drawpic_skin(weap_pos, "notify_death", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2718                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2719                         }
2720                         else if(killnotify_deathtype[j] == DEATH_NOAMMO)
2721                         {
2722                                 drawpic_skin(weap_pos, "notify_outofammo", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2723                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2724                         }
2725                         else if(killnotify_deathtype[j] == DEATH_KILL)
2726                         {
2727                                 drawpic_skin(weap_pos, "notify_selfkill", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2728                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2729                         }
2730                         else if(killnotify_deathtype[j] == DEATH_CAMP)
2731                         {
2732                                 drawpic_skin(weap_pos, "notify_camping", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2733                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2734                         }
2735                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
2736                         {
2737                                 drawpic_skin(weap_pos, "notify_teamkill", '2 1 0' * height, '1 0 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2738                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2739                         }
2740                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
2741                         {
2742                                 drawpic_skin(weap_pos, "notify_teamkill", '2 1 0' * height, '0 0 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2743                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2744                         }
2745                         else if(killnotify_deathtype[j] == DEATH_DROWN)
2746                         {
2747                                 drawpic_skin(weap_pos, "notify_water", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2748                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2749                         }
2750                         else if(killnotify_deathtype[j] == DEATH_SLIME)
2751                         {
2752                                 drawpic_skin(weap_pos, "notify_slime", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2753                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2754                         }
2755                         else if(killnotify_deathtype[j] == DEATH_LAVA)
2756                         {
2757                                 drawpic_skin(weap_pos, "notify_lava", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2758                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2759                         }
2760                         else if(killnotify_deathtype[j] == DEATH_FALL)
2761                         {
2762                                 drawpic_skin(weap_pos, "notify_fall", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2763                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2764                         }
2765                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
2766                         {
2767                                 drawpic_skin(weap_pos, "notify_shootingstar", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2768                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2769                         }
2770                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM)
2771                         {
2772                                 drawpic_skin(weap_pos, "notify_void", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2773                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2774                         }
2775                         else if(killnotify_deathtype[j] == INFO_GOTFLAG)
2776                         {
2777                                 if(killnotify_victims[j] == "^1RED^7 flag")
2778                                         s = "red";
2779                                 else
2780                                         s = "blue";
2781                                 drawpic_skin(weap_pos, strcat("flag_", s, "_carrying"), '1 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2782                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2783                         }
2784                         else if(killnotify_deathtype[j] == INFO_RETURNFLAG)
2785                         {
2786                                 if(killnotify_victims[j] == "^1RED^7 flag")
2787                                         s = "red";
2788                                 else
2789                                         s = "blue";
2790                                 drawpic_skin(weap_pos, strcat("flag_", s, "_taken"), '1 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2791                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2792                         }
2793                         else if(killnotify_deathtype[j] == INFO_LOSTFLAG)
2794                         {
2795                                 if(killnotify_victims[j] == "^1RED^7 flag")
2796                                         s = "red";
2797                                 else
2798                                         s = "blue";
2799                                 drawpic_skin(weap_pos, strcat("flag_", s, "_lost"), '1 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2800                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2801                         }
2802                 }
2803                 // X [did action to] Y
2804                 else
2805                 {
2806                         if(hud_configure)
2807                         {
2808                                 attacker = textShortenToWidth("Player1", 0.5 * mySize_x - height, fontsize, stringwidth_colors);
2809                                 victim = textShortenToWidth("Player2", 0.5 * mySize_x - height, fontsize, stringwidth_colors);
2810                                 a = bound(0, (when - j) / 4, 1);
2811                         }
2812                         else
2813                         {
2814                                 attacker = textShortenToWidth(killnotify_attackers[j], 0.5 * mySize_x - height, fontsize, stringwidth_colors);
2815                                 victim = textShortenToWidth(killnotify_victims[j], 0.5 * mySize_x - height, fontsize, stringwidth_colors);
2816                         }
2817                         width_attacker = stringwidth(attacker, TRUE, fontsize);
2818                         width_victim = stringwidth(victim, TRUE, fontsize);
2819                         pos_attacker = pos + eX * 0.5 * ((0.5 * mySize_x - height) - width_attacker) + eY * 0.5 * fontsize_y + eY * i * height;
2820                         pos_victim = pos + eX * 0.5 * ((0.5 * mySize_x - height) - width_victim) + eY * 0.5 * fontsize_y + eX * 0.5 * mySize_x + eX * height + eY * i * height;
2821                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
2822
2823                         if(hud_configure) // example actions for config mode
2824                         {
2825                                 drawpic_skin(weap_pos, strcat("weapon", "electro"), '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2826                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2827                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2828                         }
2829                         else if(WEP_VALID(killnotify_deathtype[j]))
2830                         {
2831                                 self = get_weaponinfo(killnotify_deathtype[j]);
2832                                 drawpic_skin(weap_pos, strcat("weapon", self.netname), '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2833                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2834                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2835                         }
2836                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
2837                         {
2838                                 drawpic_skin(weap_pos, "notify_teamkill", '2 1 0' * height, '1 0 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2839                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2840                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2841                         }
2842                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
2843                         {
2844                                 drawpic_skin(weap_pos, "notify_teamkill", '2 1 0' * height, '0 0 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2845                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2846                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2847                         }
2848                         else if(killnotify_deathtype[j] == DEATH_DROWN)
2849                         {
2850                                 drawpic_skin(weap_pos, "notify_water", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2851                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2852                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2853                         }
2854                         else if(killnotify_deathtype[j] == DEATH_SLIME)
2855                         {
2856                                 drawpic_skin(weap_pos, "notify_slime", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2857                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2858                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2859                         }
2860                         else if(killnotify_deathtype[j] == DEATH_LAVA)
2861                         {
2862                                 drawpic_skin(weap_pos, "notify_lava", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2863                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2864                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2865                         }
2866                         else if(killnotify_deathtype[j] == DEATH_FALL)
2867                         {
2868                                 drawpic_skin(weap_pos, "notify_fall", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2869                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2870                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2871                         }
2872                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
2873                         {
2874                                 drawpic_skin(weap_pos, "notify_shootingstar", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2875                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2876                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2877                         }
2878                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM) // DEATH_CUSTOM is also void, right?
2879                         {
2880                                 drawpic_skin(weap_pos, "notify_void", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2881                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2882                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2883                         }
2884                 }
2885
2886         }
2887
2888         // Info messages
2889         //
2890         entity tm;
2891         vector o;
2892         o = pos;
2893         if(cvar("hud_notify_info_top"))
2894                 o = pos + eY;
2895         else
2896                 o = pos + eY * mySize_y - eY * 2 * height;
2897         
2898         if(spectatee_status && !intermission)
2899         {
2900                 //drawfont = hud_bigfont;
2901                 if(spectatee_status == -1)
2902                         s = "^1Observing";
2903                 else
2904                         s = GetPlayerName(spectatee_status - 1);
2905
2906                 //s = textShortenToWidth(s, mySize_y, 0.5 * height, stringwidth_colors);
2907                 //drawcolorcodedstring(pos + eY * 0.25 * height, s, 0.5 * height, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2908                 //drawfont = hud_font;
2909
2910                 // spectator text in the upper right corner
2911                 if(spectatee_status == -1)
2912                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 to spectate");
2913                 else
2914                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 for another player");
2915                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2916                 o += eY * fontsize_y;
2917
2918                 if(spectatee_status == -1)
2919                         s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
2920                 else
2921                         s = strcat("^1Press ^3", getcommandkey("secondary fire", "+attack2"), "^1 to observe");
2922                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2923                 o += eY * fontsize_y;
2924
2925                 s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
2926                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2927                 o += eY * fontsize_y;
2928
2929                 if(gametype == GAME_ARENA)
2930                         s = "^1Wait for your turn to join";
2931                 else if(gametype == GAME_LMS)
2932                 {
2933                         entity sk;
2934                         sk = playerslots[player_localentnum - 1];
2935                         if(sk.(scores[ps_primary]) >= 666)
2936                                 s = "^1Match has already begun";
2937                         else if(sk.(scores[ps_primary]) > 0)
2938                                 s = "^1You have no more lives left";
2939                         else
2940                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
2941                 }
2942                 else
2943                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
2944                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2945                 o += eY * fontsize_y;
2946
2947                 //show restart countdown:
2948                 if (time < getstatf(STAT_GAMESTARTTIME)) {
2949                         float countdown;
2950                         //we need to ceil, otherwise the countdown would be off by .5 when using round()
2951                         countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
2952                         s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
2953                         drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2954                         o += eY * fontsize_y;
2955                 }
2956         }
2957         if(warmup_stage && !intermission)
2958         {
2959                 s = "^2Currently in ^1warmup^2 stage!";
2960                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2961                 o += eY * fontsize_y;
2962         }
2963
2964         string blinkcolor;
2965         if(mod(time, 1) >= 0.5)
2966                 blinkcolor = "^1";
2967         else
2968                 blinkcolor = "^3";
2969
2970         if(ready_waiting && !intermission && !spectatee_status)
2971         {
2972                 if(ready_waiting_for_me)
2973                 {
2974                         if(warmup_stage)
2975                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
2976                         else
2977                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
2978                 }
2979                 else
2980                 {
2981                         if(warmup_stage)
2982                                 s = strcat("^2Waiting for others to ready up to end warmup...");
2983                         else
2984                                 s = strcat("^2Waiting for others to ready up...");
2985                 }
2986                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2987                 o += eY * fontsize_y;
2988         }
2989         else if(warmup_stage && !intermission && !spectatee_status)
2990         {
2991                 s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
2992                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2993                 o += eY * fontsize_y;
2994         }
2995
2996         if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
2997         {
2998                 float ts_min, ts_max;
2999                 tm = teams.sort_next;
3000                 if (tm)
3001                 {
3002                         for(; tm.sort_next; tm = tm.sort_next)
3003                         {
3004                                 if(!tm.team_size || tm.team == COLOR_SPECTATOR)
3005                                         continue;
3006                                 if(!ts_min) ts_min = tm.team_size;
3007                                 else ts_min = min(ts_min, tm.team_size);
3008                                 if(!ts_max) ts_max = tm.team_size;
3009                                 else ts_max = max(ts_max, tm.team_size);
3010                         }
3011                         if ((ts_max - ts_min) > 1)
3012                         {
3013                                 s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
3014                                 tm = GetTeam(myteam, false);
3015                                 if (tm)
3016                                 if (tm.team != COLOR_SPECTATOR)
3017                                 if (tm.team_size == ts_max)
3018                                         s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
3019
3020                                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3021                                 o += eY * fontsize_y;
3022                         }
3023                 }
3024         }
3025         if(hud_configure)
3026         {
3027                 s = "^7Press ^3ESC ^7to show HUD options.";
3028                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3029                 o += eY * fontsize_y;
3030                 s = "^3Doubleclick a panel for panel-specific options.";
3031                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3032                 o += eY * fontsize_y;
3033         }
3034 }
3035
3036 // Timer (#5)
3037 //
3038 void HUD_Timer(void)
3039 {
3040         float id = HUD_PANEL_TIMER;
3041         vector pos, mySize;
3042         pos = HUD_Panel_GetPos(id);
3043         mySize = HUD_Panel_GetSize(id);
3044
3045         HUD_Panel_DrawBg(id, pos, mySize, 0);
3046         float padding;
3047         padding = HUD_Panel_GetPadding(id);
3048         if(padding)
3049         {
3050                 pos += '1 1 0' * padding;
3051                 mySize -= '2 2 0' * padding;
3052         }
3053
3054         float timelimit, elapsedTime, minutes, seconds, timeleft, minutesLeft, secondsLeft;
3055
3056         timelimit = getstatf(STAT_TIMELIMIT);
3057
3058         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
3059         timeleft = ceil(timeleft);
3060         minutesLeft = floor(timeleft / 60);
3061         secondsLeft = timeleft - minutesLeft*60;
3062
3063         vector timer_color;
3064         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
3065                 timer_color = '1 1 1'; //white
3066         else if(minutesLeft >= 1)
3067                 timer_color = '1 1 0'; //yellow
3068         else
3069                 timer_color = '1 0 0'; //red
3070
3071         if (cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
3072                 if (time < getstatf(STAT_GAMESTARTTIME)) {
3073                         //while restart is still active, show 00:00
3074                         minutes = seconds = 0;
3075                 } else {
3076                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
3077                         minutes = floor(elapsedTime / 60);
3078                         seconds = elapsedTime - minutes*60;
3079                 }
3080         } else {
3081                 minutes = minutesLeft;
3082                 seconds = secondsLeft;
3083         }
3084
3085         if(minutes > 999)
3086                 seconds = 99;
3087         minutes = min(minutes, 999);
3088         if(minutesLeft >= 1 || cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
3089                 HUD_DrawXNum(pos + eX * mySize_x - eX * 5.1 * mySize_y, minutes, 3, 0, mySize_y, timer_color, 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3090                 drawpic_skin(pos + eX * mySize_x - eX * 2.57 * mySize_y, "num_colon", '1 1 0' * mySize_y, timer_color, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3091         }
3092         HUD_DrawXNum(pos + eX * mySize_x - eX * 2 * mySize_y, seconds, -2, 0, mySize_y, timer_color, 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3093 }
3094
3095 // Radar (#6)
3096 //
3097 void HUD_Radar(void)
3098 {
3099         float id = HUD_PANEL_RADAR;
3100         vector pos, mySize;
3101         pos = HUD_Panel_GetPos(id);
3102         mySize = HUD_Panel_GetSize(id);
3103
3104         HUD_Panel_DrawBg(id, pos, mySize, 0);
3105         float padding;
3106         padding = HUD_Panel_GetPadding(id);
3107         if(padding)
3108         {
3109                 pos += '1 1 0' * padding;
3110                 mySize -= '2 2 0' * padding;
3111         }
3112
3113         local float color1, color2; // color already declared as a global in hud.qc
3114         local vector rgb;
3115         local entity tm;
3116         float scale2d, normalsize, bigsize;
3117         float f;
3118
3119         teamradar_origin2d = pos + 0.5 * mySize;
3120         teamradar_size2d = mySize;
3121
3122         if(minimapname == "")
3123                 return;
3124
3125         teamradar_loadcvars();
3126
3127         switch(hud_radar_zoommode)
3128         {
3129                 default:
3130                 case 0:
3131                         f = current_zoomfraction;
3132                         break;
3133                 case 1:
3134                         f = 1 - current_zoomfraction;
3135                         break;
3136                 case 2:
3137                         f = 0;
3138                         break;
3139                 case 3:
3140                         f = 1;
3141                         break;
3142         }
3143
3144         switch(hud_radar_rotation)
3145         {
3146                 case 0:
3147                         teamradar_angle = view_angles_y - 90;
3148                         break;
3149                 default:
3150                         teamradar_angle = 90 * hud_radar_rotation;
3151                         break;
3152         }
3153
3154         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
3155         teamradar_size2d = mySize;
3156
3157         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
3158
3159         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
3160         if(hud_radar_rotation == 0)
3161         {
3162                 // max-min distance must fit the radar in any rotation
3163                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
3164         }
3165         else
3166         {
3167                 vector c0, c1, c2, c3, span;
3168                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
3169                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
3170                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
3171                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
3172                 span = '0 0 0';
3173                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
3174                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
3175
3176                 // max-min distance must fit the radar in x=x, y=y
3177                 bigsize = min(
3178                         teamradar_size2d_x * scale2d / (1.05 * span_x),
3179                         teamradar_size2d_y * scale2d / (1.05 * span_y)
3180                 );
3181         }
3182
3183         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_radar_scale;
3184         if(bigsize > normalsize)
3185                 normalsize = bigsize;
3186
3187         teamradar_size =
3188                   f * bigsize
3189                 + (1 - f) * normalsize;
3190         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
3191                   f * (mi_min + mi_max) * 0.5
3192                 + (1 - f) * view_origin);
3193
3194         color1 = GetPlayerColor(player_localentnum-1);
3195         rgb = GetTeamRGB(color1);
3196
3197         drawsetcliparea(
3198                 pos_x,
3199                 pos_y,
3200                 mySize_x,
3201                 mySize_y
3202         );
3203
3204         draw_teamradar_background(hud_radar_background_alpha, hud_radar_foreground_alpha);
3205
3206         for(tm = world; (tm = find(tm, classname, "radarlink")); )
3207                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
3208         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
3209                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, HUD_Panel_GetFgAlpha(id));
3210         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
3211         {
3212                 color2 = GetPlayerColor(tm.sv_entnum);
3213                 //if(color == COLOR_SPECTATOR || color == color2)
3214                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
3215         }
3216         draw_teamradar_player(view_origin, view_angles, '1 1 1');
3217
3218         drawresetcliparea();
3219 };
3220
3221 // Score (#7)
3222 //
3223 void HUD_Score(void)
3224 {
3225         float id = HUD_PANEL_SCORE;
3226         vector pos, mySize;
3227         pos = HUD_Panel_GetPos(id);
3228         mySize = HUD_Panel_GetSize(id);
3229
3230         HUD_Panel_DrawBg(id, pos, mySize, 0);
3231         float padding;
3232         padding = HUD_Panel_GetPadding(id);
3233         if(padding)
3234         {
3235                 pos += '1 1 0' * padding;
3236                 mySize -= '2 2 0' * padding;
3237         }
3238
3239         float score, distribution, leader;
3240         float score_len, distr_len;
3241         vector distribution_color;
3242         entity tm, pl, me;
3243         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
3244
3245         // TODO... this (race part) still uses constant coordinates :/
3246         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
3247                 /*pl = players.sort_next;
3248                 if(pl == me)
3249                         pl = pl.sort_next;
3250                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
3251                         if(pl.scores[ps_primary] == 0)
3252                                 pl = world;
3253
3254                 score = me.(scores[ps_primary]);
3255
3256                 float racemin, racesec, racemsec;
3257                 float distsec, distmsec, minusplus;
3258
3259                 racemin = floor(score/(60 * TIME_FACTOR));
3260                 racesec = floor((score - racemin*(60 * TIME_FACTOR))/TIME_FACTOR);
3261                 racemsec = score - racemin*60*TIME_FACTOR - racesec*TIME_FACTOR;
3262
3263                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
3264                         // distribution display
3265                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3266
3267                         if (distribution < TIME_FACTOR && distribution > -TIME_FACTOR)
3268                                 distmsec = fabs(distribution);
3269                         else {
3270                                 distsec = floor(fabs(distribution)/TIME_FACTOR);
3271                                 distmsec = fabs(distribution) - distsec*TIME_FACTOR;
3272                                 if (distribution < 0)
3273                                         distsec = -distsec;
3274                         }
3275
3276                         if (distribution <= 0) {
3277                                 distribution_color = eY;
3278                                 minusplus = 1; // minusplus 1: always prefix with minus sign
3279                         }
3280                         else {
3281                                 distribution_color = eX;
3282                                 minusplus = 2; // minusplus 1: always prefix with plus sign
3283                         }
3284                         HUD_DrawXNum(bottomright - '0 48 0' - '16 0 0' * TIME_DECIMALS, distmsec, -TIME_DECIMALS, 0, 16, distribution_color, 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3285                         HUD_DrawXNum(bottomright - '68 48 0' - '16 0 0' * TIME_DECIMALS, distsec, 4, minusplus, 16, distribution_color, 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3286                         drawpic_skin(bottomright - '10 48 0' - '16 0 0' * TIME_DECIMALS, "num_dot", '16 16 0', distribution_color, HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE);
3287                 }
3288                 // race record display
3289                 if (distribution <= 0 || distribution == score) // draw the highlight background behind the timer if we have the lead
3290                         drawpic_skin(bottomright - '0 32 0' - '32 0 0' * (4 + TIME_DECIMALS), "num_leading_4", '0 28 0' + '32 0 0' * (4 + TIME_DECIMALS), '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3291
3292                 HUD_DrawXNum(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0', racemsec, -TIME_DECIMALS, 0, 30, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3293                 HUD_DrawXNum(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0'  - '66 0 0', racesec, -2, 0, 30, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3294                 drawpic_skin(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '18 0 0', "num_dot", '30 30 0', '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE);
3295
3296                 HUD_DrawXNum(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '132 0 0', racemin, -2, 0, 30, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3297                 drawpic_skin(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '84 0 0', "num_colon", '30 30 0', '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE);
3298                 */
3299         } else if (!teamplay) { // non-teamgames
3300                 // me vector := [team/connected frags id]
3301                 pl = players.sort_next;
3302                 if(pl == me)
3303                         pl = pl.sort_next;
3304
3305                 if(hud_configure)
3306                         distribution = 42;
3307                 else if(pl)
3308                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3309                 else
3310                         distribution = 0;
3311
3312                 score = me.(scores[ps_primary]);
3313                 if(hud_configure)
3314                         score = 123;
3315
3316                 if(distribution >= 5) {
3317                         distribution_color = eY;
3318                         leader = 1;
3319                 } else if(distribution >= 0) {
3320                         distribution_color = '1 1 1';
3321                         leader = 1;
3322                 } else if(distribution >= -5)
3323                         distribution_color = '1 1 0';
3324                 else
3325                         distribution_color = eX;
3326
3327                 score_len = strlen(ftos(score));
3328                 distr_len = strlen(ftos(distribution));
3329
3330                 HUD_DrawXNum(pos + eX * mySize_x - eX * 3 * 0.33 * mySize_y, distribution, 3, 3, 0.33 * mySize_y, distribution_color, 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3331                 if (leader)
3332                         drawpic_skin(pos + eX * mySize_x - eX * score_len * mySize_y - eX * 3 * 0.33 * mySize_y, strcat("num_leading_", ftos(score_len)), eX * score_len * mySize_y + eY * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3333                 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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3334         } else { // teamgames
3335                 float max_fragcount;
3336                 max_fragcount = -99;
3337
3338                 float teamnum;
3339                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
3340                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !hud_configure)) // no players? don't display
3341                                 continue;
3342                         score = tm.(teamscores[ts_primary]);
3343                         if(hud_configure)
3344                                 score = 123;
3345                         leader = 0;
3346                         
3347                         score_len = strlen(ftos(score));
3348
3349                         if (score > max_fragcount)
3350                                 max_fragcount = score;
3351
3352                         if(tm.team == myteam) {
3353                                 if (max_fragcount == score)
3354                                         leader = 1;
3355                                 if (leader)
3356                                         drawpic_skin(pos + eX * mySize_x - eX * score_len * mySize_y - eX * 3 * 0.33 * mySize_y, strcat("num_leading_", ftos(score_len)), eX * score_len * mySize_y + eY * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3357                                 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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3358                         } else {
3359                                 if (max_fragcount == score)
3360                                         leader = 1;
3361                                 if (leader)
3362                                         drawpic_skin(pos + eX * mySize_x - eX * 0.33 * score_len * mySize_y + eY * 0.33 * mySize_y * teamnum, strcat("num_leading_", ftos(score_len)), eX * 0.33 * score_len * mySize_y + eY * 0.33 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3363                                 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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3364                                 teamnum += 1;
3365                         }
3366                 }
3367         }
3368 }
3369
3370 // Race timer (#8)
3371 //
3372 void HUD_RaceTimer (void) {
3373         float id = HUD_PANEL_RACETIMER;
3374         vector pos, mySize;
3375         pos = HUD_Panel_GetPos(id);
3376         mySize = HUD_Panel_GetSize(id);
3377
3378         HUD_Panel_DrawBg(id, pos, mySize, 0);
3379         float padding;
3380         padding = HUD_Panel_GetPadding(id);
3381         if(padding)
3382         {
3383                 pos += '1 1 0' * padding;
3384                 mySize -= '2 2 0' * padding;
3385         }
3386
3387         drawfont = hud_bigfont;
3388         float a, t;
3389         string s, forcetime;
3390
3391         if(hud_configure)
3392         {
3393                 s = "0:13:37";
3394                 drawstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, FALSE, '0.60 0.60 0' * mySize_y), s, '0.60 0.60 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3395                 s = "^1Intermediate 1 (+15.42)";
3396                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.20 * mySize_y) + eY * 0.60 * mySize_y, s, '1 1 0' * 0.20 * mySize_y, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3397                 s = strcat("^1PENALTY: ", ftos_decimals(20 * 0.1, 1), " (missing a checkpoint)");
3398                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.20 * mySize_y) + eY * 0.80 * mySize_y, s, '1 1 0' * 0.20 * mySize_y, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3399         }
3400         else if(race_checkpointtime)
3401         {
3402                 a = bound(0, 2 - (time - race_checkpointtime), 1);
3403                 s = "";
3404                 forcetime = "";
3405                 if(a > 0) // just hit a checkpoint?
3406                 {
3407                         if(race_checkpoint != 254)
3408                         {
3409                                 if(race_time && race_previousbesttime)
3410                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
3411                                 else
3412                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
3413                                 if(race_time)
3414                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
3415                         }
3416                 }
3417                 else
3418                 {
3419                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
3420                         {
3421                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
3422                                 if(a > 0) // next one?
3423                                 {
3424                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
3425                                 }
3426                         }
3427                 }
3428
3429                 if(s != "" && a > 0)
3430                 {
3431                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3432                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3433                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3434                 }
3435
3436                 if(race_penaltytime)
3437                 {
3438                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
3439                         if(a > 0)
3440                         {
3441                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
3442                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3443                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3444                                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.8 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3445                         }
3446                 }
3447
3448                 if(forcetime != "")
3449                 {
3450                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
3451                         //drawstring_expanding(m - '16 0 0' * stringwidth(forcetime, FALSE), forcetime, '32 32 0', '1 1 1', HUD_Panel_GetFgAlpha(id), 0, a);
3452                         drawstring_expanding(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(forcetime, FALSE, '1 1 0' * 0.6 * mySize_y), forcetime, '1 1 0' * 0.6 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), 0, a);
3453                 }
3454                 else
3455                         a = 1;
3456
3457                 if(race_laptime && race_checkpoint != 255)
3458                 {
3459                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
3460                         //drawstring(m - '16 0 0' * stringwidth(s, FALSE), s, '32 32 0', '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3461                         drawstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, FALSE, '0.6 0.6 0' * mySize_y), s, '0.6 0.6 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3462                 }
3463         }
3464         else
3465         {
3466                 if(race_mycheckpointtime)
3467                 {
3468                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
3469                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
3470                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3471                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3472                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3473                 }
3474                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
3475                 {
3476                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
3477                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
3478                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3479                         //drawcolorcodedstring(m - '0 0 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3480                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3481                 }
3482
3483                 if(race_penaltytime && !race_penaltyaccumulator)
3484                 {
3485                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
3486                         a = bound(0, (1 + t - time), 1);
3487                         if(a > 0)
3488                         {
3489                                 if(time < t)
3490                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
3491                                 else
3492                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
3493                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3494                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3495                                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3496                         }
3497                 }
3498         }
3499
3500         drawfont = hud_font;
3501 }
3502
3503 // Vote window (#9)
3504 //
3505 float vote_yescount;
3506 float vote_nocount;
3507 float vote_needed;
3508 float vote_highlighted; // currently selected vote
3509
3510 float vote_active; // is there an active vote?
3511 float vote_prev; // previous state of vote_active to check for a change
3512 float vote_alpha;
3513 float vote_change; // "time" when vote_active changed
3514
3515 void HUD_VoteWindow(void) 
3516 {
3517         float id = HUD_PANEL_VOTE;
3518         vector pos, mySize;
3519         pos = HUD_Panel_GetPos(id);
3520         mySize = HUD_Panel_GetSize(id);
3521
3522         string s;
3523         float a;
3524         if(vote_active != vote_prev) {
3525                 vote_change = time;
3526                 vote_prev = vote_active;
3527         }
3528
3529         if(vote_active || hud_configure)
3530                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
3531         else
3532                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
3533
3534         if(hud_configure)
3535         {
3536                 vote_yescount = 3;
3537                 vote_nocount = 2;
3538                 vote_needed = 4;
3539         }
3540
3541         if(!vote_alpha)
3542                 return;
3543
3544         a = vote_alpha * bound(cvar_or("hud_vote_alreadyvoted_alpha", 0.75), 1 - vote_highlighted, 1);
3545
3546         HUD_Panel_DrawBg(id, pos, mySize, a);
3547         float padding;
3548         padding = HUD_Panel_GetPadding(id);
3549         if(padding)
3550         {
3551                 pos += '1 1 0' * padding;
3552                 mySize -= '2 2 0' * padding;
3553         }
3554
3555         drawpic_skin(pos, "voteprogress_back", mySize, '1 1 1', a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3556
3557         s = "A vote has been called for: ";
3558         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3559         s = textShortenToWidth(vote_called_vote, mySize_x * 0.96, '1 1 0' * mySize_y*(1/5), stringwidth_colors); // TODO: broken?
3560         if(hud_configure)
3561                 s = "Configure the HUD";
3562         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3563
3564         // print the yes/no counts
3565         s = strcat("Yes: ", ftos(vote_yescount));
3566         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3567         s = strcat("No: ", ftos(vote_nocount));
3568         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3569
3570         // draw the progress bars
3571         drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
3572         drawpic_skin(pos, "voteprogress_prog", mySize, eY, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3573
3574         drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
3575         drawpic_skin(pos, "voteprogress_prog", mySize, eX, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3576
3577         // draw the highlights
3578         if(vote_highlighted == 1) {
3579                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
3580                 drawpic_skin(pos, "voteprogress_voted", mySize, eY, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3581         }
3582         else if(vote_highlighted == 2) {
3583                 drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
3584                 drawpic_skin(pos, "voteprogress_voted", mySize, eX, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3585         }
3586
3587         drawresetcliparea();
3588
3589         if(!vote_active) {
3590                 vote_highlighted = 0;
3591         }
3592 }
3593
3594 // Mod icons panel (#10)
3595 //
3596
3597 float mod_active; // is there any active mod icon?
3598
3599 // CTF HUD modicon section
3600 float redflag_prevframe, blueflag_prevframe; // status during previous frame
3601 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
3602 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
3603
3604 void HUD_Mod_CTF_Reset(void)
3605 {
3606         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
3607 }
3608
3609 void HUD_Mod_CTF(vector pos, vector mySize)
3610 {
3611         vector redflag_pos, blueflag_pos;
3612         float f; // every function should have that
3613
3614         float redflag, blueflag; // current status
3615         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
3616         float stat_items;
3617
3618         stat_items = getstati(STAT_ITEMS);
3619         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
3620         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
3621         
3622         if(redflag || blueflag)
3623                 mod_active = 1;
3624         else
3625                 mod_active = 0;
3626
3627         if(hud_configure)
3628         {
3629                 redflag = 1;
3630                 blueflag = 2;
3631         }
3632
3633         // when status CHANGES, set old status into prevstatus and current status into status
3634         if (redflag != redflag_prevframe)
3635         {
3636                 redflag_statuschange_time = time;
3637                 redflag_prevstatus = redflag_prevframe;
3638                 redflag_prevframe = redflag;
3639         }
3640
3641         if (blueflag != blueflag_prevframe)
3642         {
3643                 blueflag_statuschange_time = time;
3644                 blueflag_prevstatus = blueflag_prevframe;
3645                 blueflag_prevframe = blueflag;
3646         }
3647
3648         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
3649         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
3650
3651         float BLINK_FACTOR = 0.15;
3652         float BLINK_BASE = 0.85;
3653         // note:
3654         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
3655         // thus
3656         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
3657         // ensure RMS == 1
3658         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
3659
3660         string red_icon, red_icon_prevstatus;
3661         float red_alpha, red_alpha_prevstatus;
3662         red_alpha = red_alpha_prevstatus = 1;
3663         switch(redflag) {
3664                 case 1: red_icon = "flag_red_taken"; break;
3665                 case 2: red_icon = "flag_red_lost"; break;
3666                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3667                 default:
3668                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
3669                                 red_icon = "flag_red_shielded";
3670                         else
3671                                 red_icon = string_null;
3672                         break;
3673         }
3674         switch(redflag_prevstatus) {
3675                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
3676                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
3677                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3678                 default:
3679                         if(redflag == 3)
3680                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
3681                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
3682                                 red_icon_prevstatus = "flag_red_shielded";
3683                         else
3684                                 red_icon_prevstatus = string_null;
3685                         break;
3686         }
3687
3688         string blue_icon, blue_icon_prevstatus;
3689         float blue_alpha, blue_alpha_prevstatus;
3690         blue_alpha = blue_alpha_prevstatus = 1;
3691         switch(blueflag) {
3692                 case 1: blue_icon = "flag_blue_taken"; break;
3693                 case 2: blue_icon = "flag_blue_lost"; break;
3694                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3695                 default:
3696                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
3697                                 blue_icon = "flag_blue_shielded";
3698                         else
3699                                 blue_icon = string_null;
3700                         break;
3701         }
3702         switch(blueflag_prevstatus) {
3703                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
3704                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
3705                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3706                 default:
3707                         if(blueflag == 3)
3708                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
3709                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
3710                                 blue_icon_prevstatus = "flag_blue_shielded";
3711                         else
3712                                 blue_icon_prevstatus = string_null;
3713                         break;
3714         }
3715
3716         if (myteam == COLOR_TEAM1) { // always draw own flag on left
3717                 redflag_pos = pos;
3718                 blueflag_pos = pos + eX * mySize_y;
3719         } else {
3720                 blueflag_pos = pos;
3721                 redflag_pos = pos + eX * mySize_y;
3722         }
3723
3724         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
3725         if(red_icon_prevstatus && f < 1)
3726                 drawpic_skin_expanding(redflag_pos, red_icon_prevstatus, '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
3727         if(red_icon)
3728                 drawpic_skin(redflag_pos, red_icon, '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * red_alpha * f, DRAWFLAG_NORMAL);
3729
3730         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
3731         if(blue_icon_prevstatus && f < 1)
3732                 drawpic_skin_expanding(blueflag_pos, blue_icon_prevstatus, '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
3733         if(blue_icon)
3734                 drawpic_skin(blueflag_pos, blue_icon, '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * blue_alpha * f, DRAWFLAG_NORMAL);
3735 }
3736
3737 // Keyhunt HUD modicon section
3738 float kh_runheretime;
3739
3740 void HUD_Mod_KH_Reset(void)
3741 {
3742         kh_runheretime = 0;
3743 }
3744
3745 void HUD_Mod_KH(vector pos, vector mySize)
3746 {
3747         mod_active = 1; // keyhunt should never hide the mod icons panel
3748         float kh_keys;
3749         float keyteam;
3750         float a, aa;
3751         vector p, pa, kh_size, kh_asize;
3752
3753         p_x = pos_x;
3754         p_y = pos_y + 0.25 * mySize_y;
3755
3756         kh_keys = getstati(STAT_KH_KEYS);
3757
3758         kh_size_x = mySize_x * 0.25;
3759         kh_size_y = 0.75 * mySize_y;
3760
3761         pa = p - eY * 0.25 * mySize_y;
3762
3763         kh_asize_x = mySize_x * 0.25;
3764         kh_asize_y = mySize_y * 0.25;
3765
3766         float i, key;
3767
3768         float keycount;
3769         keycount = 0;
3770         for(i = 0; i < 4; ++i)
3771         {
3772                 key = floor(kh_keys / pow(32, i)) & 31;
3773                 keyteam = key - 1;
3774                 if(keyteam == 30 && keycount <= 4)
3775                         keycount += 4;
3776                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
3777                         keycount += 1;
3778         }
3779
3780         // this yields 8 exactly if "RUN HERE" shows
3781
3782         if(keycount == 8)
3783         {
3784                 if(!kh_runheretime)
3785                         kh_runheretime = time;
3786                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
3787         }
3788         else
3789                 kh_runheretime = 0;
3790
3791         for(i = 0; i < 4; ++i)
3792         {
3793                 key = floor(kh_keys / pow(32, i)) & 31;
3794                 keyteam = key - 1;
3795                 switch(keyteam)
3796                 {
3797                         case 30: // my key
3798                                 keyteam = myteam;
3799                                 a = 1;
3800                                 aa = 1;
3801                                 break;
3802                         case -1: // no key
3803                                 a = 0;
3804                                 aa = 0;
3805                                 break;
3806                         default: // owned or dropped
3807                                 a = 0.2;
3808                                 aa = 0.5;
3809                                 break;
3810                 }
3811                 a = a * HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS);
3812                 aa = aa * HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS);
3813                 if(a > 0)
3814                 {
3815                         switch(keyteam)
3816                         {
3817                                 case COLOR_TEAM1:
3818                                         drawpic_skin(pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3819                                         break;
3820                                 case COLOR_TEAM2:
3821                                         drawpic_skin(pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3822                                         break;
3823                                 case COLOR_TEAM3:
3824                                         drawpic_skin(pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3825                                         break;
3826                                 case COLOR_TEAM4:
3827                                         drawpic_skin(pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3828                                         break;
3829                                 default:
3830                                         break;
3831                         }
3832                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
3833                         {
3834                                 case 0:
3835                                         drawpic_skin(p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3836                                         break;
3837                                 case 1:
3838                                         drawpic_skin(p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3839                                         break;
3840                                 case 2:
3841                                         drawpic_skin(p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3842                                         break;
3843                                 case 3:
3844                                         drawpic_skin(p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3845                                         break;
3846                         }
3847                 }
3848                 p_x += 0.25 * mySize_x;
3849                 pa_x += 0.25 * mySize_x;
3850         }
3851 }
3852
3853 // Nexball HUD mod icon
3854 void HUD_Mod_NexBall(vector pos, vector mySize)
3855 {
3856         float stat_items, nb_pb_starttime, dt, p;
3857
3858         stat_items = getstati(STAT_ITEMS);
3859         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
3860
3861         if (stat_items & IT_KEY1)
3862                 mod_active = 1;
3863         else
3864                 mod_active = 0;
3865
3866         //Manage the progress bar if any
3867         if (nb_pb_starttime > 0)
3868         {
3869                 dt = mod(time - nb_pb_starttime, nb_pb_period);
3870                 // one period of positive triangle
3871                 p = 2 * dt / nb_pb_period;
3872                 if (p > 1)
3873                         p = 2 - p;
3874
3875                 //Draw the filling
3876                 HUD_Panel_DrawProgressBar(pos, 0, eX * p * mySize_x + eY * mySize_y, HUD_Panel_GetProgressBarColor("nexball"), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3877         }
3878
3879         pos_x += 0.5 * mySize_x - 0.5 * mySize_y; //horizontal margin to the picture
3880
3881         if (stat_items & IT_KEY1)
3882                 drawpic_skin(pos, "nexball_carrying", '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3883 }
3884
3885 // Race/CTS HUD mod icons
3886 float crecordtime_prev; // last remembered crecordtime
3887 float crecordtime_change_time; // time when crecordtime last changed
3888 float srecordtime_prev; // last remembered srecordtime
3889 float srecordtime_change_time; // time when srecordtime last changed
3890
3891 float race_status_time;
3892 float race_status_prev;
3893 string race_status_name_prev;
3894 void HUD_Mod_Race(vector pos, vector mySize)
3895 {
3896         mod_active = 1; // race should never hide the mod icons panel
3897         entity me;
3898         me = playerslots[player_localentnum - 1];
3899         float t, score;
3900         float f; // yet another function has this
3901         score = me.(scores[ps_primary]);
3902
3903         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
3904                 return; // no records in the actual race
3905
3906         drawfont = hud_bigfont;
3907
3908         // clientside personal record
3909         string rr;
3910         if(gametype == GAME_CTS)
3911                 rr = CTS_RECORD;
3912         else
3913                 rr = RACE_RECORD;
3914         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
3915
3916         if(score && (score < t || !t)) {
3917                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
3918                 if(cvar("cl_autodemo_delete_keeprecords"))
3919                 {
3920                         f = cvar("cl_autodemo_delete");
3921                         f &~= 1;
3922                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
3923                 }
3924         }
3925
3926         if(t != crecordtime_prev) {
3927                 crecordtime_prev = t;
3928                 crecordtime_change_time = time;
3929         }
3930         f = time - crecordtime_change_time;
3931
3932         if (f > 1) {
3933                 drawstring(pos, "Personal best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3934                 drawstring(pos + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t), '1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3935         } else {
3936                 drawstring(pos, "Personal best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3937                 drawstring(pos + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t), '1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3938                 drawstring_expanding(pos, "Personal best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL, f);
3939                 drawstring_expanding(pos + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t), '1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL, f);
3940         }
3941
3942         // server record
3943         t = race_server_record;
3944         if(t != srecordtime_prev) {
3945                 srecordtime_prev = t;
3946                 srecordtime_change_time = time;
3947         }
3948         f = time - srecordtime_change_time;
3949
3950         if (f > 1) {
3951                 drawstring(pos + eY * 0.5 * mySize_y, "Server best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3952                 drawstring(pos + eY * 0.5 * mySize_y + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t),'1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3953         } else {
3954                 drawstring(pos + eY * 0.5 * mySize_y, "Server best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3955                 drawstring(pos + eY * 0.5 * mySize_y + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t),'1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3956                 drawstring_expanding(pos + eY * 0.5 * mySize_y, "Server best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL, f);
3957                 drawstring_expanding(pos + eY * 0.5 * mySize_y + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t),'1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL, f);
3958         }
3959
3960         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
3961                 race_status_time = time + 5;
3962                 race_status_prev = race_status;
3963                 if (race_status_name_prev)
3964                         strunzone(race_status_name_prev);
3965                 race_status_name_prev = strzone(race_status_name);
3966         }
3967
3968         pos_x += mySize_x/2;
3969         // race "awards"
3970         float a;
3971         a = bound(0, race_status_time - time, 1);
3972
3973         string s;
3974         s = textShortenToWidth(race_status_name, mySize_y, '1 1 0' * 0.1 * mySize_y, stringwidth_colors);
3975
3976         float rank;
3977         if(race_status > 0)
3978                 rank = race_CheckName(race_status_name);
3979         string rankname;
3980         rankname = race_PlaceName(rank);
3981
3982         vector namepos;
3983         namepos = pos + '0.5 0.9 0' * mySize_y - eX * stringwidth(s, TRUE, '1 1 0' * 0.1 * mySize_y);
3984         vector rankpos;
3985         rankpos = pos + '0.5 0.25 0' * mySize_y - eX * stringwidth(rankname, TRUE, '1 1 0' * 0.15 * mySize_y);
3986
3987         if(race_status == 0)
3988                 drawpic_skin(pos, "race_newfail", '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
3989         else if(race_status == 1) {
3990                 drawpic_skin(pos, "race_newtime", '1 1 0' * 0.9 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
3991                 drawcolorcodedstring(namepos, s, '1 1 0' * 0.1 * mySize_y, HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
3992                 drawstring(rankpos, rankname, '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
3993         } else if(race_status == 2) {
3994                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
3995                         drawpic_skin(pos, "race_newrankgreen", '1 1 0' * 0.9 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
3996                 else
3997                         drawpic_skin(pos, "race_newrankyellow", '1 1 0' * 0.9 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
3998                 drawcolorcodedstring(namepos, s, '1 1 0' * 0.1 * mySize_y, HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
3999                 drawstring(rankpos, rankname, '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4000         } else if(race_status == 3) {
4001                 drawpic_skin(pos, "race_newrecordserver", '1 1 0' * 0.9 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4002                 drawcolorcodedstring(namepos, s, '1 1 0' * 0.1 * mySize_y, HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4003                 drawstring(rankpos, rankname, '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4004         }
4005
4006         if (race_status_time - time <= 0) {
4007                 race_status_prev = -1;
4008                 race_status = -1;
4009                 if(race_status_name)
4010                         strunzone(race_status_name);
4011                 race_status_name = string_null;
4012                 if(race_status_name_prev)
4013                         strunzone(race_status_name_prev);
4014                 race_status_name_prev = string_null;
4015         }
4016         drawfont = hud_font;
4017 }
4018
4019 float mod_prev; // previous state of mod_active to check for a change
4020 float mod_alpha;
4021 float mod_change; // "time" when mod_active changed
4022
4023 void HUD_ModIcons(void)
4024 {
4025         if (gametype != GAME_KEYHUNT && gametype != GAME_CTF && gametype != GAME_NEXBALL && gametype != GAME_CTS && gametype != GAME_RACE && !hud_configure)
4026                 return;
4027
4028         float id = HUD_PANEL_MODICONS;
4029         vector pos, mySize;
4030         pos = HUD_Panel_GetPos(id);
4031         mySize = HUD_Panel_GetSize(id);
4032
4033         if(mod_active != mod_prev) {
4034                 mod_change = time;
4035                 mod_prev = mod_active;
4036         }
4037
4038         if(mod_active || hud_configure)
4039                 mod_alpha = bound(0, (time - mod_change) * 2, 1);
4040         else
4041                 mod_alpha = bound(0, 1 - (time - mod_change) * 2, 1);
4042
4043         if(mod_alpha)
4044                 HUD_Panel_DrawBg(id, pos, mySize, mod_alpha);
4045
4046         float padding;
4047         padding = HUD_Panel_GetPadding(id);
4048         if(padding)
4049         {
4050                 pos += '1 1 0' * padding;
4051                 mySize -= '2 2 0' * padding;
4052         }
4053
4054         // these MUST be ran in order to update mod_active
4055         if(gametype == GAME_KEYHUNT)
4056                 HUD_Mod_KH(pos, mySize);
4057         else if(gametype == GAME_CTF || hud_configure)
4058                 HUD_Mod_CTF(pos, mySize); // forcealpha only needed for ctf icons, as only they are shown in config mode
4059         else if(gametype == GAME_NEXBALL)
4060                 HUD_Mod_NexBall(pos, mySize);
4061         else if(gametype == GAME_CTS || gametype == GAME_RACE)
4062                 HUD_Mod_Race(pos, mySize);
4063 }
4064
4065 // Draw pressed keys (#11)
4066 //
4067 void HUD_DrawPressedKeys(void)
4068 {
4069         float id = HUD_PANEL_PRESSEDKEYS;
4070         vector pos, mySize;
4071         pos = HUD_Panel_GetPos(id);
4072         mySize = HUD_Panel_GetSize(id);
4073
4074         HUD_Panel_DrawBg(id, pos, mySize, 0);
4075         float padding;
4076         padding = HUD_Panel_GetPadding(id);
4077         if(padding)
4078         {
4079                 pos += '1 1 0' * padding;
4080                 mySize -= '2 2 0' * padding;
4081         }
4082
4083         float pressedkeys;
4084
4085         pressedkeys = getstatf(STAT_PRESSED_KEYS);
4086         drawpic_skin(pos, "key_bg.tga",           mySize, '1 1 1', 0.1 * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4087         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4088         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4089         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4090         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4091         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4092         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_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4093 }
4094
4095 // Handle chat as a panel (#12)
4096 //
4097 void HUD_Chat(void)
4098 {
4099         float id = HUD_PANEL_CHAT;
4100         vector pos, mySize;
4101         pos = HUD_Panel_GetPos(id);
4102         mySize = HUD_Panel_GetSize(id);
4103
4104         HUD_Panel_DrawBg(id, pos, mySize, 0);
4105         float padding;
4106         padding = HUD_Panel_GetPadding(id);
4107         if(padding)
4108         {
4109                 pos += '1 1 0' * padding;
4110                 mySize -= '2 2 0' * padding;
4111         }
4112
4113         cvar_set("con_chatrect", "1");
4114
4115         cvar_set("con_chatrect_x", ftos(pos_x/vid_conwidth));
4116         cvar_set("con_chatrect_y", ftos(pos_y/vid_conheight));
4117
4118         cvar_set("con_chatwidth", ftos(mySize_x/vid_conwidth));
4119         cvar_set("con_chat", ftos(floor(mySize_y/cvar("con_chatsize") - 0.5)));
4120
4121         if(hud_configure)
4122         {
4123                 float chatsize;
4124                 chatsize = cvar("con_chatsize");
4125                 cvar_set("con_chatrect_x", "9001"); // over 9000, we'll fake it instead for more control over alpha and such
4126                 float i, a;
4127                 for(i = 0; i < cvar("con_chat"); ++i)
4128                 {
4129                         if(i == cvar("con_chat") - 1)
4130                                 a = HUD_Panel_GetFgAlpha(id);
4131                         else
4132                                 a = HUD_Panel_GetFgAlpha(id) * floor(((i + 1) * 7 + cvar("con_chattime"))/45);
4133                         drawcolorcodedstring(pos + eY * i * chatsize, textShortenToWidth("^3Player^7: This is the chat area.", mySize_x, '1 1 0' * chatsize, stringwidth_colors), '1 1 0' * chatsize, a, DRAWFLAG_NORMAL);
4134                 }
4135         }
4136 }
4137
4138 // Engine info panel (#13)
4139 //
4140 float prevfps;
4141 float prevfps_time;
4142 float framecounter;
4143
4144 float frametimeavg;
4145 float frametimeavg1; // 1 frame ago
4146 float frametimeavg2; // 2 frames ago
4147 void HUD_EngineInfo(void)
4148 {
4149         float id = HUD_PANEL_ENGINEINFO;
4150         vector pos, mySize;
4151         pos = HUD_Panel_GetPos(id);
4152         mySize = HUD_Panel_GetSize(id);
4153
4154         HUD_Panel_DrawBg(id, pos, mySize, 0);
4155         float padding;
4156         padding = HUD_Panel_GetPadding(id);
4157         if(padding)
4158         {
4159                 pos += '1 1 0' * padding;
4160                 mySize -= '2 2 0' * padding;
4161         }
4162
4163         if(cvar("hud_engineinfo_framecounter_exponentialmovingaverage"))
4164         {
4165                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + frametime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
4166                 frametimeavg2 = frametimeavg1;
4167                 frametimeavg1 = frametimeavg;
4168                 
4169                 float weight;
4170                 weight = cvar("hud_engineinfo_framecounter_exponentialmovingaverage_new_weight");
4171                 if(frametime > 0.0001) // filter out insane values which sometimes seem to occur and throw off the average? If you are getting 10,000 fps or more, then you don't need a framerate counter.
4172                 {
4173                         if(fabs(prevfps - (1/frametimeavg)) > prevfps * cvar("hud_engineinfo_framecounter_exponentialmovingaverage_instantupdate_change_threshold")) // if there was a big jump in fps, just force prevfps at current (1/frametime) to make big updates instant
4174                                 prevfps = (1/frametime);
4175                         prevfps = (1 - weight) * prevfps + weight * (1/frametimeavg); // framecounter just used so there's no need for a new variable, think of it as "frametime average"
4176                 }
4177         }
4178         else
4179         {
4180                 framecounter += 1;
4181                 if(time - prevfps_time > cvar("hud_engineinfo_framecounter_time"))
4182                 {
4183                         prevfps = framecounter/cvar("hud_engineinfo_framecounter_time");
4184                         framecounter = 0;
4185                         prevfps_time = time;
4186                 }
4187         }
4188
4189         vector color;
4190         color = HUD_Get_Num_Color (prevfps, 100);
4191         drawstring(pos, strcat("FPS: ", ftos_decimals(prevfps, cvar("hud_engineinfo_framecounter_decimals"))), '1 1 0' * mySize_y, color, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4192 }
4193 /*
4194 ==================
4195 Main HUD system
4196 ==================
4197 */
4198
4199 void HUD_ShowSpeed(void)
4200 {
4201         vector numsize;
4202         float pos, conversion_factor;
4203         string speed, zspeed, unit;
4204
4205         switch(cvar("cl_showspeed_unit"))
4206         {
4207                 default:
4208                 case 0:
4209                         unit = "";
4210                         conversion_factor = 1.0;
4211                         break;
4212                 case 1:
4213                         unit = " qu/s";
4214                         conversion_factor = 1.0;
4215                         break;
4216                 case 2:
4217                         unit = " m/s";
4218                         conversion_factor = 0.0254;
4219                         break;
4220                 case 3:
4221                         unit = " km/h";
4222                         conversion_factor = 0.0254 * 3.6;
4223                         break;
4224                 case 4:
4225                         unit = " mph";
4226                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
4227                         break;
4228                 case 5:
4229                         unit = " knots";
4230                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
4231                         break;
4232         }
4233
4234         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
4235
4236         numsize_x = numsize_y = cvar("cl_showspeed_size");
4237         pos = (vid_conheight - numsize_y) * cvar("cl_showspeed_position");
4238
4239         drawfont = hud_bigfont;
4240         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', hud_fg_alpha, DRAWFLAG_NORMAL);
4241
4242         if (cvar("cl_showspeed_z") == 1) {
4243                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
4244                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', hud_fg_alpha, DRAWFLAG_NORMAL);
4245         }
4246
4247         drawfont = hud_font;
4248 }
4249
4250 vector acc_prevspeed;
4251 float acc_prevtime;
4252 float acc_avg;
4253
4254 void HUD_ShowAcceleration(void)
4255 {
4256         float acceleration, sz, scale, alpha, f;
4257         vector pos, top, rgb;
4258         top_x = vid_conwidth/2;
4259         top_y = 0;
4260
4261         f = time - acc_prevtime;
4262         if(cvar("cl_showacceleration_z"))
4263                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
4264         else
4265                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
4266         acc_prevspeed = pmove_vel;
4267         acc_prevtime = time;
4268
4269         f = bound(0, f * 10, 1);
4270         acc_avg = acc_avg * (1 - f) + acceleration * f;
4271         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
4272
4273         pos = top - sz/2 * eY + (cvar("cl_showacceleration_position") * vid_conheight) * eY;
4274
4275         sz = cvar("cl_showacceleration_size");
4276         scale = cvar("cl_showacceleration_scale");
4277         alpha = cvar("cl_showacceleration_alpha");
4278         if (cvar("cl_showacceleration_color_custom"))
4279                 rgb = stov(cvar_string("cl_showacceleration_color"));
4280         else {
4281                 rgb = '1 1 1';
4282                 if (acceleration < 0) {
4283                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
4284                 } else if (acceleration > 0) {
4285                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
4286                 }
4287         }
4288
4289         if (acceleration > 0)
4290                 HUD_Panel_DrawProgressBar(pos, 0, acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_fg_alpha, DRAWFLAG_NORMAL);
4291         else if (acceleration < 0)
4292                 HUD_Panel_DrawProgressBar(pos + acceleration * scale * '40 0 0', 0, -acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_fg_alpha, DRAWFLAG_NORMAL);
4293 }
4294
4295 void HUD_Reset (void)
4296 {
4297         // reset gametype specific icons
4298         if(gametype == GAME_KEYHUNT)
4299                 HUD_Mod_KH_Reset();
4300         else if(gametype == GAME_CTF)
4301                 HUD_Mod_CTF_Reset();
4302 }
4303
4304 void HUD_Main (void)
4305 {
4306         if(disable_menu_alphacheck == 1)
4307                 menu_fade_alpha = 1;
4308         else
4309                 menu_fade_alpha = (1 - cvar("_menu_alpha"));
4310         hud_fg_alpha = cvar("hud_fg_alpha");
4311
4312         hud_border_thickness = bound(0, cvar("hud_border_thickness"), 5);
4313         hud_accuracy_border_thickness = bound(0, cvar_or("hud_accuracy_border_thickness", 1), 5);
4314         hud_color_bg_team = cvar("hud_color_bg_team");
4315
4316         hud_fontsize = HUD_GetFontsize("hud_fontsize");
4317         hud_fontsize_spec = HUD_GetFontsize("hud_fontsize_spec");
4318
4319         hud_configure = cvar("_hud_configure");
4320
4321         // Drawing stuff
4322
4323         // HUD configure visible grid
4324         if(hud_configure && cvar("hud_configure_grid") && cvar("hud_configure_grid_alpha"))
4325         {
4326                 float i;
4327                 // x-axis
4328                 for(i = 0; i < 1/bound(0.005, bound(0.005, cvar("hud_configure_grid_x"), 0.2), 0.2); ++i)
4329                 {
4330                         drawfill(eX * i * vid_conwidth * bound(0.005, bound(0.005, cvar("hud_configure_grid_x"), 0.2), 0.2), eX + eY * vid_conheight, '0.5 0.5 0.5', cvar("hud_configure_grid_alpha"), DRAWFLAG_NORMAL);
4331                 }
4332                 // y-axis
4333                 for(i = 0; i < 1/bound(0.005, bound(0.005, cvar("hud_configure_grid_y"), 0.2), 0.2); ++i)
4334                 {
4335                         drawfill(eY * i * vid_conheight * bound(0.005, bound(0.005, cvar("hud_configure_grid_y"), 0.2), 0.2), eY + eX * vid_conwidth, '0.5 0.5 0.5', cvar("hud_configure_grid_alpha"), DRAWFLAG_NORMAL);
4336                 }
4337         }
4338
4339         if(cvar_string("hud_dock") != "")
4340                 drawpic('0 0 0', strcat("gfx/hud/", cvar_string("hud_skin"), "/", cvar_string("hud_dock")), eX * vid_conwidth + eY * vid_conheight, HUD_Panel_Dock_GetColor(), cvar("hud_dock_alpha") * menu_fade_alpha, DRAWFLAG_NORMAL); // no aspect ratio forcing on dock...
4341
4342         if(HUD_Panel_CheckActive(HUD_PANEL_RADAR) || hud_configure)
4343                 if(cvar_string("hud_radar") != "0" && (cvar("hud_radar") == 2 || teamplay))
4344                         HUD_Radar();
4345         if(HUD_Panel_CheckActive(HUD_PANEL_WEAPONICONS) || hud_configure)
4346                 HUD_WeaponIcons();
4347         if(HUD_Panel_CheckActive(HUD_PANEL_INVENTORY) || hud_configure)
4348                 HUD_Inventory();
4349         if(HUD_Panel_CheckActive(HUD_PANEL_POWERUPS) || hud_configure)
4350                 HUD_Powerups();
4351         if(HUD_Panel_CheckActive(HUD_PANEL_HEALTHARMOR) || hud_configure)
4352                 HUD_HealthArmor();
4353         if(HUD_Panel_CheckActive(HUD_PANEL_NOTIFY) || hud_configure)
4354                 HUD_Notify();
4355         if(HUD_Panel_CheckActive(HUD_PANEL_TIMER) || hud_configure)
4356                 HUD_Timer();
4357         if(HUD_Panel_CheckActive(HUD_PANEL_SCORE) || hud_configure)
4358                 HUD_Score();
4359         if(HUD_Panel_CheckActive(HUD_PANEL_RACETIMER) || hud_configure)
4360                 if(gametype == GAME_RACE || gametype == GAME_CTS || hud_configure)
4361                         HUD_RaceTimer();
4362         if(HUD_Panel_CheckActive(HUD_PANEL_VOTE) || hud_configure)
4363                 HUD_VoteWindow();
4364         if(HUD_Panel_CheckActive(HUD_PANEL_MODICONS) || hud_configure)
4365                 HUD_ModIcons();
4366         // TODO hud'ify
4367         if(HUD_Panel_CheckActive(HUD_PANEL_PRESSEDKEYS) || hud_configure)
4368                 if(spectatee_status > 0 || cvar("hud_pressedkeys") >= 2 || hud_configure)
4369                         HUD_DrawPressedKeys();
4370         if(HUD_Panel_CheckActive(HUD_PANEL_CHAT) || hud_configure)
4371                 HUD_Chat();
4372         else
4373                 cvar_set("con_chatrect", "0");
4374         if(HUD_Panel_CheckActive(HUD_PANEL_ENGINEINFO) || hud_configure)
4375                 HUD_EngineInfo();
4376
4377         // TODO hud_'ify these
4378         if (cvar("cl_showspeed"))
4379                 HUD_ShowSpeed();
4380         if (cvar("cl_showacceleration"))
4381                 HUD_ShowAcceleration();
4382
4383         if (hud_configure && spectatee_status) // try to join if we are in hud_configure mode, but still spectating (in order to get rid of motd and such)
4384                 localcmd("cmd selectteam auto; cmd join\n");
4385
4386         if (!hud_configure) // hud config mode disabled, enable normal alpha stuff again
4387                 disable_menu_alphacheck = 0;
4388 }