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