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