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