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