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