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