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