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