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