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