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