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