]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
Add translation helpers to /lib
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qc
1 #include "hud.qh"
2 #include "_all.qh"
3
4 #include "hud_config.qh"
5 #include "mapvoting.qh"
6 #include "scoreboard.qh"
7 #include "sortlist.qh"
8 #include "teamradar.qh"
9 #include "t_items.qh"
10
11 #include "../dpdefs/keycodes.qh"
12
13 #include "../common/buffs.qh"
14 #include "../common/constants.qh"
15 #include "../common/deathtypes.qh"
16 #include "../common/items/all.qc"
17 #include "../common/mapinfo.qh"
18 #include "../common/nades.qh"
19
20 #include "../server/mutators/gamemode_ctf.qh"
21
22 #include "../common/stats.qh"
23
24 #include "../csqcmodellib/cl_player.qh"
25
26 #include "../warpzonelib/mathlib.qh"
27
28 /*
29 ==================
30 Misc HUD functions
31 ==================
32 */
33
34 // a border picture is a texture containing nine parts:
35 //   1/4 width: left part
36 //   1/2 width: middle part (stretched)
37 //   1/4 width: right part
38 // divided into
39 //   1/4 height: top part
40 //   1/2 height: middle part (stretched)
41 //   1/4 height: bottom part
42 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
43 {
44     if (theBorderSize.x < 0 && theBorderSize.y < 0) // draw whole image as it is
45     {
46                 drawpic(theOrigin, pic, theSize, theColor, theAlpha, 0);
47                 return;
48     }
49         if (theBorderSize.x == 0 && theBorderSize.y == 0) // no border
50         {
51                 // draw only the central part
52                 drawsubpic(theOrigin, theSize, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
53                 return;
54         }
55
56         vector dX, dY;
57         vector width, height;
58         vector bW, bH;
59         //pic = draw_UseSkinFor(pic);
60         width = eX * theSize.x;
61         height = eY * theSize.y;
62         if(theSize.x <= theBorderSize.x * 2)
63         {
64                 // not wide enough... draw just left and right then
65                 bW = eX * (0.25 * theSize.x / (theBorderSize.x * 2));
66                 if(theSize.y <= theBorderSize.y * 2)
67                 {
68                         // not high enough... draw just corners
69                         bH = eY * (0.25 * theSize.y / (theBorderSize.y * 2));
70                         drawsubpic(theOrigin,                 width * 0.5 + height * 0.5, pic, '0 0 0',           bW + bH, theColor, theAlpha, 0);
71                         drawsubpic(theOrigin + width   * 0.5, width * 0.5 + height * 0.5, pic, eX - bW,           bW + bH, theColor, theAlpha, 0);
72                         drawsubpic(theOrigin + height  * 0.5, width * 0.5 + height * 0.5, pic, eY - bH,           bW + bH, theColor, theAlpha, 0);
73                         drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
74                 }
75                 else
76                 {
77                         dY = theBorderSize.x * eY;
78                         drawsubpic(theOrigin,                             width * 0.5          +     dY, pic, '0 0    0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
79                         drawsubpic(theOrigin + width * 0.5,               width * 0.5          +     dY, pic, '0 0    0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
80                         drawsubpic(theOrigin                        + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0',           '0 0.5  0' + bW, theColor, theAlpha, 0);
81                         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);
82                         drawsubpic(theOrigin               + height - dY, width * 0.5          +     dY, pic, '0 0.75 0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
83                         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);
84                 }
85         }
86         else
87         {
88                 if(theSize.y <= theBorderSize.y * 2)
89                 {
90                         // not high enough... draw just top and bottom then
91                         bH = eY * (0.25 * theSize.y / (theBorderSize.y * 2));
92                         dX = theBorderSize.x * eX;
93                         drawsubpic(theOrigin,                                         dX + height * 0.5, pic, '0    0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
94                         drawsubpic(theOrigin + dX,                        width - 2 * dX + height * 0.5, pic, '0.25 0 0',           '0.5  0 0' + bH, theColor, theAlpha, 0);
95                         drawsubpic(theOrigin + width - dX,                            dX + height * 0.5, pic, '0.75 0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
96                         drawsubpic(theOrigin              + height * 0.5,             dX + height * 0.5, pic, '0    0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
97                         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);
98                         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);
99                 }
100                 else
101                 {
102                         dX = theBorderSize.x * eX;
103                         dY = theBorderSize.x * eY;
104                         drawsubpic(theOrigin,                                        dX          +     dY, pic, '0    0    0', '0.25 0.25 0', theColor, theAlpha, 0);
105                         drawsubpic(theOrigin                  + dX,      width - 2 * dX          +     dY, pic, '0.25 0    0', '0.5  0.25 0', theColor, theAlpha, 0);
106                         drawsubpic(theOrigin          + width - dX,                  dX          +     dY, pic, '0.75 0    0', '0.25 0.25 0', theColor, theAlpha, 0);
107                         drawsubpic(theOrigin          + dY,                          dX + height - 2 * dY, pic, '0    0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
108                         drawsubpic(theOrigin          + dY         + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5  0.5  0', theColor, theAlpha, 0);
109                         drawsubpic(theOrigin          + dY + width - dX,             dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
110                         drawsubpic(theOrigin + height - dY,                          dX          +     dY, pic, '0    0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
111                         drawsubpic(theOrigin + height - dY         + dX, width - 2 * dX          +     dY, pic, '0.25 0.75 0', '0.5  0.25 0', theColor, theAlpha, 0);
112                         drawsubpic(theOrigin + height - dY + width - dX,             dX          +     dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
113                 }
114         }
115 }
116
117 vector HUD_Get_Num_Color (float x, float maxvalue)
118 {
119         float blinkingamt;
120         vector color;
121         if(x >= maxvalue) {
122                 color.x = sin(2*M_PI*time);
123                 color.y = 1;
124                 color.z = sin(2*M_PI*time);
125         }
126         else if(x > maxvalue * 0.75) {
127                 color.x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
128                 color.y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
129                 color.z = 0;
130         }
131         else if(x > maxvalue * 0.5) {
132                 color.x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
133                 color.y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
134                 color.z = 1 - (x-100)*0.02; // blue value between 1 -> 0
135         }
136         else if(x > maxvalue * 0.25) {
137                 color.x = 1;
138                 color.y = 1;
139                 color.z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
140         }
141         else if(x > maxvalue * 0.1) {
142                 color.x = 1;
143                 color.y = (x-20)*90/27/100; // green value between 0 -> 1
144                 color.z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
145         }
146         else {
147                 color.x = 1;
148                 color.y = 0;
149                 color.z = 0;
150         }
151
152         blinkingamt = (1 - x/maxvalue/0.25);
153         if(blinkingamt > 0)
154         {
155                 color.x = color.x - color.x * blinkingamt * sin(2*M_PI*time);
156                 color.y = color.y - color.y * blinkingamt * sin(2*M_PI*time);
157                 color.z = color.z - color.z * blinkingamt * sin(2*M_PI*time);
158         }
159         return color;
160 }
161
162 float HUD_GetRowCount(int item_count, vector size, float item_aspect)
163 {
164         float aspect = size_y / size_x;
165         return bound(1, floor((sqrt(4 * item_aspect * aspect * item_count + aspect * aspect) + aspect + 0.5) / 2), item_count);
166 }
167
168 vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspect)
169 {
170         float columns, rows;
171         float ratio, best_ratio = 0;
172         float best_columns = 1, best_rows = 1;
173         bool vertical = (psize.x / psize.y >= item_aspect);
174         if(vertical)
175         {
176                 psize = eX * psize.y + eY * psize.x;
177                 item_aspect = 1 / item_aspect;
178         }
179
180         rows = ceil(sqrt(item_count));
181         columns = ceil(item_count/rows);
182         while(columns >= 1)
183         {
184                 ratio = (psize.x/columns) / (psize.y/rows);
185                 if(ratio > item_aspect)
186                         ratio = item_aspect * item_aspect / ratio;
187
188                 if(ratio <= best_ratio)
189                         break; // ratio starts decreasing by now, skip next configurations
190
191                 best_columns = columns;
192                 best_rows = rows;
193                 best_ratio = ratio;
194
195                 if(columns == 1)
196                         break;
197
198                 --columns;
199                 rows = ceil(item_count/columns);
200         }
201
202         if(vertical)
203                 return eX * best_rows + eY * best_columns;
204         else
205                 return eX * best_columns + eY * best_rows;
206 }
207
208 float stringwidth_colors(string s, vector theSize)
209 {
210         return stringwidth(s, true, theSize);
211 }
212
213 float stringwidth_nocolors(string s, vector theSize)
214 {
215         return stringwidth(s, false, theSize);
216 }
217
218 void drawstringright(vector position, string text, vector theScale, vector rgb, float theAlpha, int flag)
219 {
220         position.x -= 2 / 3 * strlen(text) * theScale.x;
221         drawstring(position, text, theScale, rgb, theAlpha, flag);
222 }
223
224 void drawstringcenter(vector position, string text, vector theScale, vector rgb, float theAlpha, int flag)
225 {
226         position.x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * theScale.x);
227         drawstring(position, text, theScale, rgb, theAlpha, flag);
228 }
229
230 // return the string of the onscreen race timer
231 string MakeRaceString(int cp, float mytime, float theirtime, float lapdelta, string theirname)
232 {
233         string col;
234         string timestr;
235         string cpname;
236         string lapstr;
237         lapstr = "";
238
239         if(theirtime == 0) // goal hit
240         {
241                 if(mytime > 0)
242                 {
243                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
244                         col = "^1";
245                 }
246                 else if(mytime == 0)
247                 {
248                         timestr = "+0.0";
249                         col = "^3";
250                 }
251                 else
252                 {
253                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
254                         col = "^2";
255                 }
256
257                 if(lapdelta > 0)
258                 {
259                         lapstr = sprintf(_(" (-%dL)"), lapdelta);
260                         col = "^2";
261                 }
262                 else if(lapdelta < 0)
263                 {
264                         lapstr = sprintf(_(" (+%dL)"), -lapdelta);
265                         col = "^1";
266                 }
267         }
268         else if(theirtime > 0) // anticipation
269         {
270                 if(mytime >= theirtime)
271                         timestr = strcat("+", ftos_decimals(mytime - theirtime, TIME_DECIMALS));
272                 else
273                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(theirtime));
274                 col = "^3";
275         }
276         else
277         {
278                 col = "^7";
279                 timestr = "";
280         }
281
282         if(cp == 254)
283                 cpname = _("Start line");
284         else if(cp == 255)
285                 cpname = _("Finish line");
286         else if(cp)
287                 cpname = sprintf(_("Intermediate %d"), cp);
288         else
289                 cpname = _("Finish line");
290
291         if(theirtime < 0)
292                 return strcat(col, cpname);
293         else if(theirname == "")
294                 return strcat(col, sprintf("%s (%s)", cpname, timestr));
295         else
296                 return strcat(col, sprintf("%s (%s %s)", cpname, timestr, strcat(theirname, col, lapstr)));
297 }
298
299 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
300 int race_CheckName(string net_name)
301 {
302         int i;
303         for (i=RANKINGS_CNT-1;i>=0;--i)
304                 if(grecordholder[i] == net_name)
305                         return i+1;
306         return 0;
307 }
308
309 int GetPlayerColorForce(int i)
310 {
311         if(!teamplay)
312                 return 0;
313         else
314                 return stof(getplayerkeyvalue(i, "colors")) & 15;
315 }
316
317 int GetPlayerColor(int i)
318 {
319         if(!playerslots[i].gotscores) // unconnected
320                 return NUM_SPECTATOR;
321         else if(stof(getplayerkeyvalue(i, "frags")) == FRAGS_SPECTATOR)
322                 return NUM_SPECTATOR;
323         else
324                 return GetPlayerColorForce(i);
325 }
326
327 string GetPlayerName(int i)
328 {
329         return ColorTranslateRGB(getplayerkeyvalue(i, "name"));
330 }
331
332
333 /*
334 ==================
335 HUD panels
336 ==================
337 */
338
339 // draw the background/borders
340 #define HUD_Panel_DrawBg(theAlpha) do {                                                                                                                                                         \
341         if(panel.current_panel_bg != "0" && panel.current_panel_bg != "")                                                                                               \
342                 draw_BorderPicture(panel_pos - '1 1 0' * panel_bg_border, panel.current_panel_bg, panel_size + '1 1 0' * 2 * panel_bg_border, panel_bg_color, panel_bg_alpha * theAlpha, '1 1 0' * (panel_bg_border/BORDER_MULTIPLIER));\
343 } while(0)
344
345 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
346 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
347 {
348         if(!length_ratio || !theAlpha)
349                 return;
350         if(length_ratio > 1)
351                 length_ratio = 1;
352         if (baralign == 3)
353         {
354                 if(length_ratio < -1)
355                         length_ratio = -1;
356         }
357         else if(length_ratio < 0)
358                 return;
359
360         vector square;
361         vector width, height;
362         if(vertical) {
363                 pic = strcat(hud_skin_path, "/", pic, "_vertical");
364                 if(precache_pic(pic) == "") {
365                         pic = "gfx/hud/default/progressbar_vertical";
366                 }
367
368         if (baralign == 1) // bottom align
369                         theOrigin.y += (1 - length_ratio) * theSize.y;
370         else if (baralign == 2) // center align
371             theOrigin.y += 0.5 * (1 - length_ratio) * theSize.y;
372         else if (baralign == 3) // center align, positive values down, negative up
373                 {
374                         theSize.y *= 0.5;
375                         if (length_ratio > 0)
376                                 theOrigin.y += theSize.y;
377                         else
378                         {
379                                 theOrigin.y += (1 + length_ratio) * theSize.y;
380                                 length_ratio = -length_ratio;
381                         }
382                 }
383                 theSize.y *= length_ratio;
384
385                 vector bH;
386                 width = eX * theSize.x;
387                 height = eY * theSize.y;
388                 if(theSize.y <= theSize.x * 2)
389                 {
390                         // button not high enough
391                         // draw just upper and lower part then
392                         square = eY * theSize.y * 0.5;
393                         bH = eY * (0.25 * theSize.y / (theSize.x * 2));
394                         drawsubpic(theOrigin,          square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, drawflag);
395                         drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, drawflag);
396                 }
397                 else
398                 {
399                         square = eY * theSize.x;
400                         drawsubpic(theOrigin,                   width   +     square, pic, '0 0    0', '1 0.25 0', theColor, theAlpha, drawflag);
401                         drawsubpic(theOrigin +          square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5  0', theColor, theAlpha, drawflag);
402                         drawsubpic(theOrigin + height - square, width   +     square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, drawflag);
403                 }
404         } else {
405                 pic = strcat(hud_skin_path, "/", pic);
406                 if(precache_pic(pic) == "") {
407                         pic = "gfx/hud/default/progressbar";
408                 }
409
410                 if (baralign == 1) // right align
411                         theOrigin.x += (1 - length_ratio) * theSize.x;
412         else if (baralign == 2) // center align
413             theOrigin.x += 0.5 * (1 - length_ratio) * theSize.x;
414         else if (baralign == 3) // center align, positive values on the right, negative on the left
415                 {
416                         theSize.x *= 0.5;
417                         if (length_ratio > 0)
418                                 theOrigin.x += theSize.x;
419                         else
420                         {
421                                 theOrigin.x += (1 + length_ratio) * theSize.x;
422                                 length_ratio = -length_ratio;
423                         }
424                 }
425                 theSize.x *= length_ratio;
426
427                 vector bW;
428                 width = eX * theSize.x;
429                 height = eY * theSize.y;
430                 if(theSize.x <= theSize.y * 2)
431                 {
432                         // button not wide enough
433                         // draw just left and right part then
434                         square = eX * theSize.x * 0.5;
435                         bW = eX * (0.25 * theSize.x / (theSize.y * 2));
436                         drawsubpic(theOrigin,          square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, drawflag);
437                         drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, drawflag);
438                 }
439                 else
440                 {
441                         square = eX * theSize.y;
442                         drawsubpic(theOrigin,                  height  +     square, pic, '0    0 0', '0.25 1 0', theColor, theAlpha, drawflag);
443                         drawsubpic(theOrigin +         square, theSize - 2 * square, pic, '0.25 0 0', '0.5  1 0', theColor, theAlpha, drawflag);
444                         drawsubpic(theOrigin + width - square, height  +     square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
445                 }
446         }
447 }
448
449 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag)
450 {
451         if(!theAlpha)
452                 return;
453
454         string pic;
455         pic = strcat(hud_skin_path, "/num_leading");
456         if(precache_pic(pic) == "") {
457                 pic = "gfx/hud/default/num_leading";
458         }
459
460         drawsubpic(pos, eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0 0 0', '0.25 1 0', color, theAlpha, drawflag);
461         if(mySize.x/mySize.y > 2)
462                 drawsubpic(pos + eX * mySize.y, eX * (mySize.x - 2 * mySize.y) + eY * mySize.y, pic, '0.25 0 0', '0.5 1 0', color, theAlpha, drawflag);
463         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, theAlpha, drawflag);
464 }
465
466 // Weapon icons (#0)
467 //
468 entity weaponorder[WEP_MAXCOUNT];
469 void weaponorder_swap(int i, int j, entity pass)
470 {
471         entity h = weaponorder[i];
472         weaponorder[i] = weaponorder[j];
473         weaponorder[j] = h;
474 }
475
476 string weaponorder_cmp_str;
477 int weaponorder_cmp(int i, int j, entity pass)
478 {
479         int ai, aj;
480         ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
481         aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
482         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)
483 }
484
485 void HUD_Weapons(void)
486 {
487         // declarations
488         WepSet weapons_stat = WepSet_GetFromStat();
489         int i;
490         float f, a;
491         float screen_ar;
492         vector center = '0 0 0';
493         int weapon_count, weapon_id;
494         int row, column, rows = 0, columns = 0;
495         bool vertical_order = true;
496         float aspect = autocvar_hud_panel_weapons_aspect;
497
498         float timeout = autocvar_hud_panel_weapons_timeout;
499         float timein_effect_length = autocvar_hud_panel_weapons_timeout_speed_in; //? 0.375 : 0);
500         float timeout_effect_length = autocvar_hud_panel_weapons_timeout_speed_out; //? 0.75 : 0);
501
502         vector barsize = '0 0 0', baroffset = '0 0 0';
503         vector ammo_color = '1 0 1';
504         float ammo_alpha = 1;
505
506         float when = max(1, autocvar_hud_panel_weapons_complainbubble_time);
507         float fadetime = max(0, autocvar_hud_panel_weapons_complainbubble_fadetime);
508
509         vector weapon_pos, weapon_size = '0 0 0';
510         vector color;
511
512         // check to see if we want to continue
513         if(intermission == 2) return;
514         if(hud != HUD_NORMAL) return;
515
516         if(!autocvar__hud_configure)
517         {
518                 if((!autocvar_hud_panel_weapons) || (spectatee_status == -1))
519                         return;
520                 if(timeout && time >= weapontime + timeout + timeout_effect_length)
521                 if(autocvar_hud_panel_weapons_timeout_effect == 3 || (autocvar_hud_panel_weapons_timeout_effect == 1 && !(autocvar_hud_panel_weapons_timeout_fadebgmin + autocvar_hud_panel_weapons_timeout_fadefgmin)))
522                 {
523                         weaponprevtime = time;
524                         return;
525                 }
526         }
527
528         // update generic hud functions
529         HUD_Panel_UpdateCvars();
530
531         // figure out weapon order (how the weapons are sorted) // TODO make this configurable
532         if(weaponorder_bypriority != autocvar_cl_weaponpriority || !weaponorder[0])
533         {
534                 int weapon_cnt;
535                 if(weaponorder_bypriority)
536                         strunzone(weaponorder_bypriority);
537                 if(weaponorder_byimpulse)
538                         strunzone(weaponorder_byimpulse);
539
540                 weaponorder_bypriority = strzone(autocvar_cl_weaponpriority);
541                 weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(W_FixWeaponOrder_ForceComplete(W_NumberWeaponOrder(weaponorder_bypriority))));
542                 weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
543
544                 weapon_cnt = 0;
545                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
546                 {
547                         self = get_weaponinfo(i);
548                         if(self.impulse >= 0)
549                         {
550                                 weaponorder[weapon_cnt] = self;
551                                 ++weapon_cnt;
552                         }
553                 }
554                 for(i = weapon_cnt; i < WEP_MAXCOUNT; ++i)
555                         weaponorder[i] = world;
556                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
557
558                 weaponorder_cmp_str = string_null;
559         }
560
561         if(!autocvar_hud_panel_weapons_complainbubble || autocvar__hud_configure || time - complain_weapon_time >= when + fadetime)
562                 complain_weapon = 0;
563
564         if(autocvar__hud_configure)
565         {
566                 if(!weapons_stat)
567                         for(i = WEP_FIRST; i <= WEP_LAST; i += floor((WEP_LAST-WEP_FIRST)/5))
568                                 weapons_stat |= WepSet_FromWeapon(i);
569
570                 #if 0
571                 /// debug code
572                 if(cvar("wep_add"))
573                 {
574                         weapons_stat = '0 0 0';
575                         float countw = 1 + floor((floor(time * cvar("wep_add"))) % WEP_COUNT);
576                         for(i = WEP_FIRST; i <= countw; ++i)
577                                 weapons_stat |= WepSet_FromWeapon(i);
578                 }
579                 #endif
580         }
581
582         // determine which weapons are going to be shown
583         if (autocvar_hud_panel_weapons_onlyowned)
584         {
585                 if(autocvar__hud_configure)
586                 {
587                         if(menu_enabled != 2)
588                                 HUD_Panel_DrawBg(1); // also draw the bg of the entire panel
589                 }
590
591                 // do we own this weapon?
592                 weapon_count = 0;
593                 for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
594                         if((weapons_stat & WepSet_FromWeapon(weaponorder[i].weapon)) || (weaponorder[i].weapon == complain_weapon))
595                                 ++weapon_count;
596
597
598                 // might as well commit suicide now, no reason to live ;)
599                 if (weapon_count == 0)
600                         return;
601
602                 vector old_panel_size = panel_size;
603                 vector padded_panel_size = panel_size - '2 2 0' * panel_bg_padding;
604
605                 // get the all-weapons layout
606                 vector table_size = HUD_GetTableSize_BestItemAR(WEP_COUNT, padded_panel_size, aspect);
607                 columns = table_size.x;
608                 rows = table_size.y;
609                 weapon_size.x = padded_panel_size.x / columns;
610                 weapon_size.y = padded_panel_size.y / rows;
611
612                 // NOTE: although weapons should aways look the same even if onlyowned is enabled,
613                 // we enlarge them a bit when possible to better match the desired aspect ratio
614                 if(padded_panel_size.x / padded_panel_size.y < aspect)
615                 {
616                         // maximum number of rows that allows to display items with the desired aspect ratio
617                         int max_rows = floor(padded_panel_size.y / (weapon_size.x / aspect));
618                         columns = min(columns, ceil(weapon_count / max_rows));
619                         rows = ceil(weapon_count / columns);
620                         weapon_size.y = min(padded_panel_size.y / rows, weapon_size.x / aspect);
621                         weapon_size.x = min(padded_panel_size.x / columns, aspect * weapon_size.y);
622                         vertical_order = false;
623                 }
624                 else
625                 {
626                         int max_columns = floor(padded_panel_size.x / (weapon_size.y * aspect));
627                         rows = min(rows, ceil(weapon_count / max_columns));
628                         columns = ceil(weapon_count / rows);
629                         weapon_size.x = min(padded_panel_size.x / columns, aspect * weapon_size.y);
630                         weapon_size.y = min(padded_panel_size.y / rows, weapon_size.x / aspect);
631                         vertical_order = true;
632                 }
633
634                 // reduce size of the panel
635                 panel_size.x = columns * weapon_size.x;
636                 panel_size.y = rows * weapon_size.y;
637                 panel_size += '2 2 0' * panel_bg_padding;
638
639                 // center the resized panel, or snap it to the screen edge when close enough
640                 if(panel_pos.x > vid_conwidth * 0.001)
641                 {
642                         if(panel_pos.x + old_panel_size.x > vid_conwidth * 0.999)
643                                 panel_pos.x += old_panel_size.x - panel_size.x;
644                         else
645                                 panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
646                 }
647                 else if(old_panel_size.x > vid_conwidth * 0.999)
648                         panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
649
650                 if(panel_pos.y > vid_conheight * 0.001)
651                 {
652                         if(panel_pos.y + old_panel_size.y > vid_conheight * 0.999)
653                                 panel_pos.y += old_panel_size.y - panel_size.y;
654                         else
655                                 panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
656                 }
657                 else if(old_panel_size.y > vid_conheight * 0.999)
658                         panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
659         }
660         else
661                 weapon_count = WEP_COUNT;
662
663         // animation for fading in/out the panel respectively when not in use
664         if(!autocvar__hud_configure)
665         {
666                 if (timeout && time >= weapontime + timeout) // apply timeout effect if needed
667                 {
668                         f = bound(0, (time - (weapontime + timeout)) / timeout_effect_length, 1);
669
670                         // fade the panel alpha
671                         if(autocvar_hud_panel_weapons_timeout_effect == 1)
672                         {
673                                 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * f + (1 - f));
674                                 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * f + (1 - f));
675                         }
676                         else if(autocvar_hud_panel_weapons_timeout_effect == 3)
677                         {
678                                 panel_bg_alpha *= (1 - f);
679                                 panel_fg_alpha *= (1 - f);
680                         }
681
682                         // move the panel off the screen
683                         if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
684                         {
685                                 f *= f; // for a cooler movement
686                                 center.x = panel_pos.x + panel_size.x/2;
687                                 center.y = panel_pos.y + panel_size.y/2;
688                                 screen_ar = vid_conwidth/vid_conheight;
689                                 if (center.x/center.y < screen_ar) //bottom left
690                                 {
691                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
692                                                 panel_pos.y += f * (vid_conheight - panel_pos.y);
693                                         else //left
694                                                 panel_pos.x -= f * (panel_pos.x + panel_size.x);
695                                 }
696                                 else //top right
697                                 {
698                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //right
699                                                 panel_pos.x += f * (vid_conwidth - panel_pos.x);
700                                         else //top
701                                                 panel_pos.y -= f * (panel_pos.y + panel_size.y);
702                                 }
703                                 if(f == 1)
704                                         center.x = -1; // mark the panel as off screen
705                         }
706                         weaponprevtime = time - (1 - f) * timein_effect_length;
707                 }
708                 else if (timeout && time < weaponprevtime + timein_effect_length) // apply timein effect if needed
709                 {
710                         f = bound(0, (time - weaponprevtime) / timein_effect_length, 1);
711
712                         // fade the panel alpha
713                         if(autocvar_hud_panel_weapons_timeout_effect == 1)
714                         {
715                                 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * (1 - f) + f);
716                                 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * (1 - f) + f);
717                         }
718                         else if(autocvar_hud_panel_weapons_timeout_effect == 3)
719                         {
720                                 panel_bg_alpha *= (f);
721                                 panel_fg_alpha *= (f);
722                         }
723
724                         // move the panel back on screen
725                         if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
726                         {
727                                 f *= f; // for a cooler movement
728                                 f = 1 - f;
729                                 center.x = panel_pos.x + panel_size.x/2;
730                                 center.y = panel_pos.y + panel_size.y/2;
731                                 screen_ar = vid_conwidth/vid_conheight;
732                                 if (center.x/center.y < screen_ar) //bottom left
733                                 {
734                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
735                                                 panel_pos.y += f * (vid_conheight - panel_pos.y);
736                                         else //left
737                                                 panel_pos.x -= f * (panel_pos.x + panel_size.x);
738                                 }
739                                 else //top right
740                                 {
741                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //right
742                                                 panel_pos.x += f * (vid_conwidth - panel_pos.x);
743                                         else //top
744                                                 panel_pos.y -= f * (panel_pos.y + panel_size.y);
745                                 }
746                         }
747                 }
748         }
749
750         // draw the background, then change the virtual size of it to better fit other items inside
751         HUD_Panel_DrawBg(1);
752
753         if(center.x == -1)
754                 return;
755
756         if(panel_bg_padding)
757         {
758                 panel_pos += '1 1 0' * panel_bg_padding;
759                 panel_size -= '2 2 0' * panel_bg_padding;
760         }
761
762         // after the sizing and animations are done, update the other values
763
764         if(!rows) // if rows is > 0 onlyowned code has already updated these vars
765         {
766                 vector table_size = HUD_GetTableSize_BestItemAR(WEP_COUNT, panel_size, aspect);
767                 columns = table_size.x;
768                 rows = table_size.y;
769                 weapon_size.x = panel_size.x / columns;
770                 weapon_size.y = panel_size.y / rows;
771                 vertical_order = (panel_size.x / panel_size.y >= aspect);
772         }
773
774         // calculate position/size for visual bar displaying ammount of ammo status
775         if (autocvar_hud_panel_weapons_ammo)
776         {
777                 ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
778                 ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
779
780                 if(weapon_size.x/weapon_size.y > aspect)
781                 {
782                         barsize.x = aspect * weapon_size.y;
783                         barsize.y = weapon_size.y;
784                         baroffset.x = (weapon_size.x - barsize.x) / 2;
785                 }
786                 else
787                 {
788                         barsize.y = 1/aspect * weapon_size.x;
789                         barsize.x = weapon_size.x;
790                         baroffset.y = (weapon_size.y - barsize.y) / 2;
791                 }
792         }
793         if(autocvar_hud_panel_weapons_accuracy)
794                 Accuracy_LoadColors();
795
796         // draw items
797         row = column = 0;
798         vector label_size = '1 1 0' * min(weapon_size.x, weapon_size.y) * bound(0, autocvar_hud_panel_weapons_label_scale, 1);
799         vector noncurrent_pos = '0 0 0';
800         vector noncurrent_size = weapon_size * bound(0, autocvar_hud_panel_weapons_noncurrent_scale, 1);
801         float noncurrent_alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_weapons_noncurrent_alpha, 1);
802         bool isCurrent;
803
804         for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
805         {
806                 // retrieve information about the current weapon to be drawn
807                 self = weaponorder[i];
808                 weapon_id = self.impulse;
809                 isCurrent = (self.weapon == switchweapon);
810
811                 // skip if this weapon doesn't exist
812                 if(!self || weapon_id < 0) { continue; }
813
814                 // skip this weapon if we don't own it (and onlyowned is enabled)-- or if weapons_complainbubble is showing for this weapon
815                 if(autocvar_hud_panel_weapons_onlyowned)
816                 if (!((weapons_stat & WepSet_FromWeapon(self.weapon)) || (self.weapon == complain_weapon)))
817                         continue;
818
819                 // figure out the drawing position of weapon
820                 weapon_pos = (panel_pos + eX * column * weapon_size.x + eY * row * weapon_size.y);
821                 noncurrent_pos.x = weapon_pos.x + (weapon_size.x - noncurrent_size.x) / 2;
822                 noncurrent_pos.y = weapon_pos.y + (weapon_size.y - noncurrent_size.y) / 2;
823
824                 // draw background behind currently selected weapon
825                 if(isCurrent)
826                         drawpic_aspect_skin(weapon_pos, "weapon_current_bg", weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
827
828                 // draw the weapon accuracy
829                 if(autocvar_hud_panel_weapons_accuracy)
830                 {
831                         float panel_weapon_accuracy = weapon_accuracy[self.weapon-WEP_FIRST];
832                         if(panel_weapon_accuracy >= 0)
833                         {
834                                 color = Accuracy_GetColor(panel_weapon_accuracy);
835                                 drawpic_aspect_skin(weapon_pos, "weapon_accuracy", weapon_size, color, panel_fg_alpha, DRAWFLAG_NORMAL);
836                         }
837                 }
838
839                 // drawing all the weapon items
840                 if(weapons_stat & WepSet_FromWeapon(self.weapon))
841                 {
842                         // draw the weapon image
843                         if(isCurrent)
844                                 drawpic_aspect_skin(weapon_pos, self.model2, weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
845                         else
846                                 drawpic_aspect_skin(noncurrent_pos, self.model2, noncurrent_size, '1 1 1', noncurrent_alpha, DRAWFLAG_NORMAL);
847
848                         // draw weapon label string
849                         switch(autocvar_hud_panel_weapons_label)
850                         {
851                                 case 1: // weapon number
852                                         drawstring(weapon_pos, ftos(weapon_id), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
853                                         break;
854
855                                 case 2: // bind
856                                         drawstring(weapon_pos, getcommandkey(ftos(weapon_id), strcat("weapon_group_", ftos(weapon_id))), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
857                                         break;
858
859                                 case 3: // weapon name
860                                         drawstring(weapon_pos, strtolower(self.message), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
861                                         break;
862
863                                 default: // nothing
864                                         break;
865                         }
866
867                         // draw ammo status bar
868                         if(autocvar_hud_panel_weapons_ammo && (self.ammo_field != ammo_none))
869                         {
870                                 float ammo_full;
871                                 a = getstati(GetAmmoStat(self.ammo_field)); // how much ammo do we have?
872
873                                 if(a > 0)
874                                 {
875                                         switch(self.ammo_field)
876                                         {
877                                                 case ammo_shells:  ammo_full = autocvar_hud_panel_weapons_ammo_full_shells;  break;
878                                                 case ammo_nails:   ammo_full = autocvar_hud_panel_weapons_ammo_full_nails;   break;
879                                                 case ammo_rockets: ammo_full = autocvar_hud_panel_weapons_ammo_full_rockets; break;
880                                                 case ammo_cells:   ammo_full = autocvar_hud_panel_weapons_ammo_full_cells;   break;
881                                                 case ammo_plasma:  ammo_full = autocvar_hud_panel_weapons_ammo_full_plasma;  break;
882                                                 case ammo_fuel:    ammo_full = autocvar_hud_panel_weapons_ammo_full_fuel;    break;
883                                                 default: ammo_full = 60;
884                                         }
885
886                                         drawsetcliparea(
887                                                 weapon_pos.x + baroffset.x,
888                                                 weapon_pos.y + baroffset.y,
889                                                 barsize.x * bound(0, a/ammo_full, 1),
890                                                 barsize.y
891                                         );
892
893                                         drawpic_aspect_skin(
894                                                 weapon_pos,
895                                                 "weapon_ammo",
896                                                 weapon_size,
897                                                 ammo_color,
898                                                 ammo_alpha,
899                                                 DRAWFLAG_NORMAL
900                                         );
901
902                                         drawresetcliparea();
903                                 }
904                         }
905                 }
906                 else // draw a "ghost weapon icon" if you don't have the weapon
907                 {
908                         drawpic_aspect_skin(noncurrent_pos, self.model2, noncurrent_size, '0.2 0.2 0.2', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
909                 }
910
911                 // draw the complain message
912                 if(self.weapon == complain_weapon)
913                 {
914                         if(fadetime)
915                                 a = ((complain_weapon_time + when > time) ? 1 : bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1));
916                         else
917                                 a = ((complain_weapon_time + when > time) ? 1 : 0);
918
919                         string s;
920                         if(complain_weapon_type == 0) {
921                                 s = _("Out of ammo");
922                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
923                         }
924                         else if(complain_weapon_type == 1) {
925                                 s = _("Don't have");
926                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
927                         }
928                         else {
929                                 s = _("Unavailable");
930                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
931                         }
932                         float padding = autocvar_hud_panel_weapons_complainbubble_padding;
933                         drawpic_aspect_skin(weapon_pos + '1 1 0' * padding, "weapon_complainbubble", weapon_size - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
934                         drawstring_aspect(weapon_pos + '1 1 0' * padding, s, weapon_size - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
935                 }
936
937                 #if 0
938                 /// debug code
939                 if(!autocvar_hud_panel_weapons_onlyowned)
940                 {
941                         drawfill(weapon_pos + '1 1 0', weapon_size - '2 2 0', '1 1 1', panel_fg_alpha * 0.2, DRAWFLAG_NORMAL);
942                         drawstring(weapon_pos, ftos(i + 1), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
943                 }
944                 #endif
945
946                 // continue with new position for the next weapon
947                 if(vertical_order)
948                 {
949                         ++column;
950                         if(column >= columns)
951                         {
952                                 column = 0;
953                                 ++row;
954                         }
955                 }
956                 else
957                 {
958                         ++row;
959                         if(row >= rows)
960                         {
961                                 row = 0;
962                                 ++column;
963                         }
964                 }
965         }
966 }
967
968 // Ammo (#1)
969 void DrawNadeProgressBar(vector myPos, vector mySize, float progress, vector color)
970 {
971         HUD_Panel_DrawProgressBar(
972                 myPos + eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x,
973                 mySize - eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x,
974                 autocvar_hud_panel_ammo_progressbar_name,
975                 progress, 0, 0, color,
976                 autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
977 }
978
979 void DrawAmmoNades(vector myPos, vector mySize, bool draw_expanding, float expand_time)
980 {
981         float bonusNades    = getstatf(STAT_NADE_BONUS);
982         float bonusProgress = getstatf(STAT_NADE_BONUS_SCORE);
983         float bonusType     = getstati(STAT_NADE_BONUS_TYPE);
984         vector nadeColor    = NADES[bonusType].m_color;
985         string nadeIcon     = NADES[bonusType].m_icon;
986
987         vector iconPos, textPos;
988
989         if(autocvar_hud_panel_ammo_iconalign)
990         {
991                 iconPos = myPos + eX * 2 * mySize.y;
992                 textPos = myPos;
993         }
994         else
995         {
996                 iconPos = myPos;
997                 textPos = myPos + eX * mySize.y;
998         }
999
1000         if(bonusNades > 0 || bonusProgress > 0)
1001         {
1002                 DrawNadeProgressBar(myPos, mySize, bonusProgress, nadeColor);
1003
1004                 if(autocvar_hud_panel_ammo_text)
1005                         drawstring_aspect(textPos, ftos(bonusNades), eX * (2/3) * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1006
1007                 if(draw_expanding)
1008                         drawpic_aspect_skin_expanding(iconPos, nadeIcon, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, expand_time);
1009
1010                 drawpic_aspect_skin(iconPos, nadeIcon, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1011         }
1012 }
1013
1014 void DrawAmmoItem(vector myPos, vector mySize, .int ammoType, bool isCurrent, bool isInfinite)
1015 {
1016         if(ammoType == ammo_none)
1017                 return;
1018
1019         // Initialize variables
1020
1021         int ammo;
1022         if(autocvar__hud_configure)
1023         {
1024                 isCurrent = (ammoType == ammo_rockets); // Rockets always current
1025                 ammo = 60;
1026         }
1027         else
1028                 ammo = getstati(GetAmmoStat(ammoType));
1029
1030         if(!isCurrent)
1031         {
1032                 float scale = bound(0, autocvar_hud_panel_ammo_noncurrent_scale, 1);
1033                 myPos = myPos + (mySize - mySize * scale) * 0.5;
1034                 mySize = mySize * scale;
1035         }
1036
1037         vector iconPos, textPos;
1038         if(autocvar_hud_panel_ammo_iconalign)
1039         {
1040                 iconPos = myPos + eX * 2 * mySize.y;
1041                 textPos = myPos;
1042         }
1043         else
1044         {
1045                 iconPos = myPos;
1046                 textPos = myPos + eX * mySize.y;
1047         }
1048
1049         bool isShadowed = (ammo <= 0 && !isCurrent && !isInfinite);
1050
1051         vector iconColor = isShadowed ? '0 0 0' : '1 1 1';
1052         vector textColor;
1053         if(isInfinite)
1054                 textColor = '0.2 0.95 0';
1055         else if(isShadowed)
1056                 textColor = '0 0 0';
1057         else if(ammo < 10)
1058                 textColor = '0.8 0.04 0';
1059         else
1060                 textColor = '1 1 1';
1061
1062         float alpha;
1063         if(isCurrent)
1064                 alpha = panel_fg_alpha;
1065         else if(isShadowed)
1066                 alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_ammo_noncurrent_alpha, 1) * 0.5;
1067         else
1068                 alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_ammo_noncurrent_alpha, 1);
1069
1070         string text = isInfinite ? "\xE2\x88\x9E" : ftos(ammo); // Use infinity symbol (U+221E)
1071
1072         // Draw item
1073
1074         if(isCurrent)
1075                 drawpic_aspect_skin(myPos, "ammo_current_bg", mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1076
1077         if(ammo > 0 && autocvar_hud_panel_ammo_progressbar)
1078                 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, ammo/autocvar_hud_panel_ammo_maxammo, 0, 0, textColor, autocvar_hud_progressbar_alpha * alpha, DRAWFLAG_NORMAL);
1079
1080         if(autocvar_hud_panel_ammo_text)
1081                 drawstring_aspect(textPos, text, eX * (2/3) * mySize.x + eY * mySize.y, textColor, alpha, DRAWFLAG_NORMAL);
1082
1083         drawpic_aspect_skin(iconPos, GetAmmoPicture(ammoType), '1 1 0' * mySize.y, iconColor, alpha, DRAWFLAG_NORMAL);
1084 }
1085
1086 int nade_prevstatus;
1087 int nade_prevframe;
1088 float nade_statuschange_time;
1089 void HUD_Ammo(void)
1090 {
1091         if(intermission == 2) return;
1092         if(hud != HUD_NORMAL) return;
1093         if(!autocvar__hud_configure)
1094         {
1095                 if(!autocvar_hud_panel_ammo) return;
1096                 if(spectatee_status == -1) return;
1097         }
1098
1099         HUD_Panel_UpdateCvars();
1100
1101         draw_beginBoldFont();
1102
1103         vector pos, mySize;
1104         pos = panel_pos;
1105         mySize = panel_size;
1106
1107         HUD_Panel_DrawBg(1);
1108         if(panel_bg_padding)
1109         {
1110                 pos += '1 1 0' * panel_bg_padding;
1111                 mySize -= '2 2 0' * panel_bg_padding;
1112         }
1113
1114         int rows = 0, columns, row, column;
1115         float nade_cnt = getstatf(STAT_NADE_BONUS), nade_score = getstatf(STAT_NADE_BONUS_SCORE);
1116         bool draw_nades = (nade_cnt > 0 || nade_score > 0);
1117         float nade_statuschange_elapsedtime;
1118         int total_ammo_count;
1119
1120         vector ammo_size;
1121         if (autocvar_hud_panel_ammo_onlycurrent)
1122                 total_ammo_count = 1;
1123         else
1124                 total_ammo_count = AMMO_COUNT;
1125
1126         if(draw_nades)
1127         {
1128                 ++total_ammo_count;
1129                 if (nade_cnt != nade_prevframe)
1130                 {
1131                         nade_statuschange_time = time;
1132                         nade_prevstatus = nade_prevframe;
1133                         nade_prevframe = nade_cnt;
1134                 }
1135         }
1136         else
1137                 nade_prevstatus = nade_prevframe = nade_statuschange_time = 0;
1138
1139         rows = HUD_GetRowCount(total_ammo_count, mySize, 3);
1140         columns = ceil((total_ammo_count)/rows);
1141         ammo_size = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
1142
1143         vector offset = '0 0 0'; // fteqcc sucks
1144         float newSize;
1145         if(ammo_size.x/ammo_size.y > 3)
1146         {
1147                 newSize = 3 * ammo_size.y;
1148                 offset.x = ammo_size.x - newSize;
1149                 pos.x += offset.x/2;
1150                 ammo_size.x = newSize;
1151         }
1152         else
1153         {
1154                 newSize = 1/3 * ammo_size.x;
1155                 offset.y = ammo_size.y - newSize;
1156                 pos.y += offset.y/2;
1157                 ammo_size.y = newSize;
1158         }
1159
1160         int i;
1161         bool infinite_ammo = (getstati(STAT_ITEMS, 0, 24) & IT_UNLIMITED_WEAPON_AMMO);
1162         row = column = 0;
1163         if(autocvar_hud_panel_ammo_onlycurrent)
1164         {
1165                 if(autocvar__hud_configure)
1166                 {
1167                         DrawAmmoItem(pos, ammo_size, ammo_rockets, true, false);
1168                 }
1169                 else
1170                 {
1171                         DrawAmmoItem(
1172                                 pos,
1173                                 ammo_size,
1174                                 (get_weaponinfo(switchweapon)).ammo_field,
1175                                 true,
1176                                 infinite_ammo
1177                         );
1178                 }
1179
1180                 ++row;
1181                 if(row >= rows)
1182                 {
1183                         row = 0;
1184                         column = column + 1;
1185                 }
1186         }
1187         else
1188         {
1189                 .int ammotype;
1190                 row = column = 0;
1191                 for(i = 0; i < AMMO_COUNT; ++i)
1192                 {
1193                         ammotype = GetAmmoFieldFromNum(i);
1194                         DrawAmmoItem(
1195                                 pos + eX * column * (ammo_size.x + offset.x) + eY * row * (ammo_size.y + offset.y),
1196                                 ammo_size,
1197                                 ammotype,
1198                                 ((get_weaponinfo(switchweapon)).ammo_field == ammotype),
1199                                 infinite_ammo
1200                         );
1201
1202                         ++row;
1203                         if(row >= rows)
1204                         {
1205                                 row = 0;
1206                                 column = column + 1;
1207                         }
1208                 }
1209         }
1210
1211         if (draw_nades)
1212         {
1213                 nade_statuschange_elapsedtime = time - nade_statuschange_time;
1214
1215                 float f = bound(0, nade_statuschange_elapsedtime*2, 1);
1216
1217                 DrawAmmoNades(pos + eX * column * (ammo_size.x + offset.x) + eY * row * (ammo_size.y + offset.y), ammo_size, nade_prevstatus < nade_cnt && nade_cnt != 0 && f < 1, f);
1218         }
1219
1220         draw_endBoldFont();
1221 }
1222
1223 void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, bool vertical, bool icon_right_align, vector color, float theAlpha, float fadelerp)
1224 {
1225         vector newPos = '0 0 0', newSize = '0 0 0';
1226         vector picpos, numpos;
1227
1228         if (vertical)
1229         {
1230                 if(mySize.y/mySize.x > 2)
1231                 {
1232                         newSize.y = 2 * mySize.x;
1233                         newSize.x = mySize.x;
1234
1235                         newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
1236                         newPos.x = myPos.x;
1237                 }
1238                 else
1239                 {
1240                         newSize.x = 1/2 * mySize.y;
1241                         newSize.y = mySize.y;
1242
1243                         newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
1244                         newPos.y = myPos.y;
1245                 }
1246
1247                 if(icon_right_align)
1248                 {
1249                         numpos = newPos;
1250                         picpos = newPos + eY * newSize.x;
1251                 }
1252                 else
1253                 {
1254                         picpos = newPos;
1255                         numpos = newPos + eY * newSize.x;
1256                 }
1257
1258                 newSize.y /= 2;
1259                 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
1260                 // make number smaller than icon, it looks better
1261                 // reduce only y to draw numbers with different number of digits with the same y size
1262                 numpos.y += newSize.y * ((1 - 0.7) / 2);
1263                 newSize.y *= 0.7;
1264                 drawstring_aspect(numpos, ftos(x), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
1265                 return;
1266         }
1267
1268         if(mySize.x/mySize.y > 3)
1269         {
1270                 newSize.x = 3 * mySize.y;
1271                 newSize.y = mySize.y;
1272
1273                 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
1274                 newPos.y = myPos.y;
1275         }
1276         else
1277         {
1278                 newSize.y = 1/3 * mySize.x;
1279                 newSize.x = mySize.x;
1280
1281                 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
1282                 newPos.x = myPos.x;
1283         }
1284
1285         if(icon_right_align) // right align
1286         {
1287                 numpos = newPos;
1288                 picpos = newPos + eX * 2 * newSize.y;
1289         }
1290         else // left align
1291         {
1292                 numpos = newPos + eX * newSize.y;
1293                 picpos = newPos;
1294         }
1295
1296         // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
1297         // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
1298         drawstring_aspect_expanding(numpos, ftos(x), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
1299         drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
1300 }
1301
1302 void DrawNumIcon(vector myPos, vector mySize, float x, string icon, bool vertical, bool icon_right_align, vector color, float theAlpha)
1303 {
1304         DrawNumIcon_expanding(myPos, mySize, x, icon, vertical, icon_right_align, color, theAlpha, 0);
1305 }
1306
1307 // Powerups (#2)
1308 //
1309
1310 // Powerup item fields (reusing existing fields)
1311 .string message;  // Human readable name
1312 .string netname;  // Icon name
1313 .vector colormod; // Color
1314 .float count;     // Time left
1315 .float lifetime;  // Maximum time
1316
1317 entity powerupItems;
1318 int powerupItemsCount;
1319
1320 void resetPowerupItems()
1321 {
1322         entity item;
1323         for(item = powerupItems; item; item = item.chain)
1324                 item.count = 0;
1325
1326         powerupItemsCount = 0;
1327 }
1328
1329 void addPowerupItem(string name, string icon, vector color, float currentTime, float lifeTime)
1330 {
1331         if(!powerupItems)
1332                 powerupItems = spawn();
1333
1334         entity item;
1335         for(item = powerupItems; item.count; item = item.chain)
1336                 if(!item.chain)
1337                         item.chain = spawn();
1338
1339         item.message  = name;
1340         item.netname  = icon;
1341         item.colormod = color;
1342         item.count    = currentTime;
1343         item.lifetime = lifeTime;
1344
1345         ++powerupItemsCount;
1346 }
1347
1348 int getPowerupItemAlign(int align, int column, int row, int columns, int rows, bool isVertical)
1349 {
1350         if(align < 2)
1351                 return align;
1352
1353         bool isTop    =  isVertical && rows > 1 && row == 0;
1354         bool isBottom =  isVertical && rows > 1 && row == rows-1;
1355         bool isLeft   = !isVertical && columns > 1 && column == 0;
1356         bool isRight  = !isVertical && columns > 1 && column == columns-1;
1357
1358         if(isTop    || isLeft)  return (align == 2) ? 1 : 0;
1359         if(isBottom || isRight) return (align == 2) ? 0 : 1;
1360
1361         return 2;
1362 }
1363
1364 void HUD_Powerups()
1365 {
1366         if(intermission == 2) return;
1367
1368         int allItems = getstati(STAT_ITEMS, 0, 24);
1369         int allBuffs = getstati(STAT_BUFFS, 0, 24);
1370         int strengthTime, shieldTime, superTime;
1371
1372         // Initialize items
1373         if(!autocvar__hud_configure)
1374         {
1375                 if(!autocvar_hud_panel_powerups) return;
1376                 if(spectatee_status == -1) return;
1377                 if(getstati(STAT_HEALTH) <= 0) return;
1378                 if(!(allItems & (ITEM_Strength.m_itemid | ITEM_Shield.m_itemid | IT_SUPERWEAPON)) && !allBuffs) return;
1379
1380                 strengthTime = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
1381                 shieldTime = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
1382                 superTime = bound(0, getstatf(STAT_SUPERWEAPONS_FINISHED) - time, 99);
1383
1384                 if(allItems & IT_UNLIMITED_SUPERWEAPONS)
1385                         superTime = 99;
1386
1387                 // Prevent stuff to show up on mismatch that will be fixed next frame
1388                 if(!(allItems & IT_SUPERWEAPON))
1389                         superTime = 0;
1390         }
1391         else
1392         {
1393                 strengthTime = 15;
1394                 shieldTime = 27;
1395                 superTime = 13;
1396                 allBuffs = 0;
1397         }
1398
1399         // Add items to linked list
1400         resetPowerupItems();
1401
1402         if(strengthTime)
1403                 addPowerupItem("Strength", "strength", autocvar_hud_progressbar_strength_color, strengthTime, 30);
1404         if(shieldTime)
1405                 addPowerupItem("Shield", "shield", autocvar_hud_progressbar_shield_color, shieldTime, 30);
1406         if(superTime)
1407                 addPowerupItem("Superweapons", "superweapons", autocvar_hud_progressbar_superweapons_color, superTime, 30);
1408
1409         FOREACH(BUFFS, it.m_itemid & allBuffs, LAMBDA(
1410                 addPowerupItem(it.m_prettyName, strcat("buff_", it.m_name), it.m_color, bound(0, getstatf(STAT_BUFF_TIME) - time, 99), 60);
1411         ));
1412
1413         if(!powerupItemsCount)
1414                 return;
1415
1416         // Draw panel background
1417         HUD_Panel_UpdateCvars();
1418         HUD_Panel_DrawBg(1);
1419
1420         // Set drawing area
1421         vector pos = panel_pos;
1422         vector size = panel_size;
1423         bool isVertical = size.y > size.x;
1424
1425         if(panel_bg_padding)
1426         {
1427                 pos += '1 1 0' * panel_bg_padding;
1428                 size -= '2 2 0' * panel_bg_padding;
1429         }
1430
1431         // Find best partitioning of the drawing area
1432         const float DESIRED_ASPECT = 6;
1433         float aspect = 0, a;
1434         int columns = 0, c;
1435         int rows = 0, r;
1436         int i = 1;
1437
1438         do
1439         {
1440                 c = floor(powerupItemsCount / i);
1441                 r = ceil(powerupItemsCount / c);
1442                 a = isVertical ? (size.y/r) / (size.x/c) : (size.x/c) / (size.y/r);
1443
1444                 if(i == 1 || fabs(DESIRED_ASPECT - a) < fabs(DESIRED_ASPECT - aspect))
1445                 {
1446                         aspect = a;
1447                         columns = c;
1448                         rows = r;
1449                 }
1450         }
1451         while(++i <= powerupItemsCount);
1452
1453         // Prevent single items from getting too wide
1454         if(powerupItemsCount == 1 && aspect > DESIRED_ASPECT)
1455         {
1456                 if(isVertical)
1457                 {
1458                         size.y *= 0.5;
1459                         pos.y += size.y * 0.5;
1460                 }
1461                 else
1462                 {
1463                         size.x *= 0.5;
1464                         pos.x += size.x * 0.5;
1465                 }
1466         }
1467
1468         // Draw items from linked list
1469         vector itemPos = pos;
1470         vector itemSize = eX * (size.x / columns) + eY * (size.y / rows);
1471         vector textColor = '1 1 1';
1472
1473         int fullSeconds = 0;
1474         int align = 0;
1475         int column = 0;
1476         int row = 0;
1477
1478         draw_beginBoldFont();
1479         for(entity item = powerupItems; item.count; item = item.chain)
1480         {
1481                 itemPos = eX * (pos.x + column * itemSize.x) + eY * (pos.y + row * itemSize.y);
1482
1483                 // Draw progressbar
1484                 if(autocvar_hud_panel_powerups_progressbar)
1485                 {
1486                         align = getPowerupItemAlign(autocvar_hud_panel_powerups_baralign, column, row, columns, rows, isVertical);
1487                         HUD_Panel_DrawProgressBar(itemPos, itemSize, "progressbar", item.count / item.lifetime, isVertical, align, item.colormod, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1488                 }
1489
1490                 // Draw icon and text
1491                 if(autocvar_hud_panel_powerups_text)
1492                 {
1493                         align = getPowerupItemAlign(autocvar_hud_panel_powerups_iconalign, column, row, columns, rows, isVertical);
1494                         fullSeconds = ceil(item.count);
1495                         textColor = '0.6 0.6 0.6' + (item.colormod * 0.4);
1496
1497                         if(item.count > 1)
1498                                 DrawNumIcon(itemPos, itemSize, fullSeconds, item.netname, isVertical, align, textColor, panel_fg_alpha);
1499                         if(item.count <= 5)
1500                                 DrawNumIcon_expanding(itemPos, itemSize, fullSeconds, item.netname, isVertical, align, textColor, panel_fg_alpha, bound(0, (fullSeconds - item.count) / 0.5, 1));
1501                 }
1502
1503                 // Determine next section
1504                 if(isVertical)
1505                 {
1506                         if(++column >= columns)
1507                         {
1508                                 column = 0;
1509                                 ++row;
1510                         }
1511                 }
1512                 else
1513                 {
1514                         if(++row >= rows)
1515                         {
1516                                 row = 0;
1517                                 ++column;
1518                         }
1519                 }
1520         }
1521         draw_endBoldFont();
1522 }
1523
1524 // Health/armor (#3)
1525 //
1526
1527
1528 void HUD_HealthArmor(void)
1529 {
1530         int armor, health, fuel;
1531         if(intermission == 2) return;
1532         if(!autocvar__hud_configure)
1533         {
1534                 if(!autocvar_hud_panel_healtharmor) return;
1535                 if(hud != HUD_NORMAL) return;
1536                 if(spectatee_status == -1) return;
1537
1538                 health = getstati(STAT_HEALTH);
1539                 if(health <= 0)
1540                 {
1541                         prev_health = -1;
1542                         return;
1543                 }
1544                 armor = getstati(STAT_ARMOR);
1545
1546                 // code to check for spectatee_status changes is in Ent_ClientData()
1547                 // prev_p_health and prev_health can be set to -1 there
1548
1549                 if (prev_p_health == -1)
1550                 {
1551                         // no effect
1552                         health_beforedamage = 0;
1553                         armor_beforedamage = 0;
1554                         health_damagetime = 0;
1555                         armor_damagetime = 0;
1556                         prev_health = health;
1557                         prev_armor = armor;
1558                         old_p_health = health;
1559                         old_p_armor = armor;
1560                         prev_p_health = health;
1561                         prev_p_armor = armor;
1562                 }
1563                 else if (prev_health == -1)
1564                 {
1565                         //start the load effect
1566                         health_damagetime = 0;
1567                         armor_damagetime = 0;
1568                         prev_health = 0;
1569                         prev_armor = 0;
1570                 }
1571                 fuel = getstati(STAT_FUEL);
1572         }
1573         else
1574         {
1575                 health = 150;
1576                 armor = 75;
1577                 fuel = 20;
1578         }
1579
1580         HUD_Panel_UpdateCvars();
1581
1582         draw_beginBoldFont();
1583
1584         vector pos, mySize;
1585         pos = panel_pos;
1586         mySize = panel_size;
1587
1588         HUD_Panel_DrawBg(1);
1589         if(panel_bg_padding)
1590         {
1591                 pos += '1 1 0' * panel_bg_padding;
1592                 mySize -= '2 2 0' * panel_bg_padding;
1593         }
1594
1595         int baralign = autocvar_hud_panel_healtharmor_baralign;
1596         int iconalign = autocvar_hud_panel_healtharmor_iconalign;
1597
1598     int maxhealth = autocvar_hud_panel_healtharmor_maxhealth;
1599     int maxarmor = autocvar_hud_panel_healtharmor_maxarmor;
1600         if(autocvar_hud_panel_healtharmor == 2) // combined health and armor display
1601         {
1602                 vector v;
1603                 v = healtharmor_maxdamage(health, armor, armorblockpercent, DEATH_WEAPON);
1604
1605                 float x;
1606                 x = floor(v.x + 1);
1607
1608         float maxtotal = maxhealth + maxarmor;
1609                 string biggercount;
1610                 if(v.z) // NOT fully armored
1611                 {
1612                         biggercount = "health";
1613                         if(autocvar_hud_panel_healtharmor_progressbar)
1614                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_health, x/maxtotal, 0, (baralign == 1 || baralign == 2), autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1615                         if(armor)
1616             if(autocvar_hud_panel_healtharmor_text)
1617                                 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);
1618                 }
1619                 else
1620                 {
1621                         biggercount = "armor";
1622                         if(autocvar_hud_panel_healtharmor_progressbar)
1623                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, x/maxtotal, 0, (baralign == 1 || baralign == 2), autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1624                         if(health)
1625             if(autocvar_hud_panel_healtharmor_text)
1626                                 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);
1627                 }
1628         if(autocvar_hud_panel_healtharmor_text)
1629                         DrawNumIcon(pos, mySize, x, biggercount, 0, iconalign, HUD_Get_Num_Color(x, maxtotal), 1);
1630
1631                 if(fuel)
1632                         HUD_Panel_DrawProgressBar(pos, eX * mySize.x + eY * 0.2 * mySize.y, "progressbar", fuel/100, 0, (baralign == 1 || baralign == 3), autocvar_hud_progressbar_fuel_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
1633         }
1634         else
1635         {
1636                 float panel_ar = mySize.x/mySize.y;
1637                 bool is_vertical = (panel_ar < 1);
1638                 vector health_offset = '0 0 0', armor_offset = '0 0 0';
1639                 if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
1640                 {
1641                         mySize.x *= 0.5;
1642                         if (autocvar_hud_panel_healtharmor_flip)
1643                                 health_offset.x = mySize.x;
1644                         else
1645                                 armor_offset.x = mySize.x;
1646                 }
1647                 else
1648                 {
1649                         mySize.y *= 0.5;
1650                         if (autocvar_hud_panel_healtharmor_flip)
1651                                 health_offset.y = mySize.y;
1652                         else
1653                                 armor_offset.y = mySize.y;
1654                 }
1655
1656                 bool health_baralign, armor_baralign, fuel_baralign;
1657                 bool health_iconalign, armor_iconalign;
1658                 if (autocvar_hud_panel_healtharmor_flip)
1659                 {
1660                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
1661                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
1662                         fuel_baralign = health_baralign;
1663                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
1664                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
1665                 }
1666                 else
1667                 {
1668                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
1669                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
1670                         fuel_baralign = armor_baralign;
1671                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
1672                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
1673                 }
1674
1675                 //if(health)
1676                 {
1677                         if(autocvar_hud_panel_healtharmor_progressbar)
1678                         {
1679                                 float p_health, pain_health_alpha;
1680                                 p_health = health;
1681                                 pain_health_alpha = 1;
1682                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
1683                                 {
1684                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
1685                                         {
1686                                                 if (fabs(prev_health - health) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
1687                                                 {
1688                                                         if (time - old_p_healthtime < 1)
1689                                                                 old_p_health = prev_p_health;
1690                                                         else
1691                                                                 old_p_health = prev_health;
1692                                                         old_p_healthtime = time;
1693                                                 }
1694                                                 if (time - old_p_healthtime < 1)
1695                                                 {
1696                                                         p_health += (old_p_health - health) * (1 - (time - old_p_healthtime));
1697                                                         prev_p_health = p_health;
1698                                                 }
1699                                         }
1700                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
1701                                         {
1702                                                 if (prev_health - health >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
1703                                                 {
1704                                                         if (time - health_damagetime >= 1)
1705                                                                 health_beforedamage = prev_health;
1706                                                         health_damagetime = time;
1707                                                 }
1708                                                 if (time - health_damagetime < 1)
1709                                                 {
1710                                                         float health_damagealpha = 1 - (time - health_damagetime)*(time - health_damagetime);
1711                                                         HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, health_beforedamage/maxhealth, is_vertical, health_baralign, autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * health_damagealpha, DRAWFLAG_NORMAL);
1712                                                 }
1713                                         }
1714                                         prev_health = health;
1715
1716                                         if (health <= autocvar_hud_panel_healtharmor_progressbar_gfx_lowhealth)
1717                                         {
1718                                                 float BLINK_FACTOR = 0.15;
1719                                                 float BLINK_BASE = 0.85;
1720                                                 float BLINK_FREQ = 9;
1721                                                 pain_health_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
1722                                         }
1723                                 }
1724                                 HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, p_health/maxhealth, is_vertical, health_baralign, autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * pain_health_alpha, DRAWFLAG_NORMAL);
1725                         }
1726                         if(autocvar_hud_panel_healtharmor_text)
1727                                 DrawNumIcon(pos + health_offset, mySize, health, "health", is_vertical, health_iconalign, HUD_Get_Num_Color(health, maxhealth), 1);
1728                 }
1729
1730                 if(armor)
1731                 {
1732                         if(autocvar_hud_panel_healtharmor_progressbar)
1733                         {
1734                                 float p_armor;
1735                                 p_armor = armor;
1736                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
1737                                 {
1738                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
1739                                         {
1740                                                 if (fabs(prev_armor - armor) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
1741                                                 {
1742                                                         if (time - old_p_armortime < 1)
1743                                                                 old_p_armor = prev_p_armor;
1744                                                         else
1745                                                                 old_p_armor = prev_armor;
1746                                                         old_p_armortime = time;
1747                                                 }
1748                                                 if (time - old_p_armortime < 1)
1749                                                 {
1750                                                         p_armor += (old_p_armor - armor) * (1 - (time - old_p_armortime));
1751                                                         prev_p_armor = p_armor;
1752                                                 }
1753                                         }
1754                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
1755                                         {
1756                                                 if (prev_armor - armor >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
1757                                                 {
1758                                                         if (time - armor_damagetime >= 1)
1759                                                                 armor_beforedamage = prev_armor;
1760                                                         armor_damagetime = time;
1761                                                 }
1762                                                 if (time - armor_damagetime < 1)
1763                                                 {
1764                                                         float armor_damagealpha = 1 - (time - armor_damagetime)*(time - armor_damagetime);
1765                                                         HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, armor_beforedamage/maxarmor, is_vertical, armor_baralign, autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * armor_damagealpha, DRAWFLAG_NORMAL);
1766                                                 }
1767                                         }
1768                                         prev_armor = armor;
1769                                 }
1770                                 HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, p_armor/maxarmor, is_vertical, armor_baralign, autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1771                         }
1772                         if(autocvar_hud_panel_healtharmor_text)
1773                                 DrawNumIcon(pos + armor_offset, mySize, armor, "armor", is_vertical, armor_iconalign, HUD_Get_Num_Color(armor, maxarmor), 1);
1774                 }
1775
1776                 if(fuel)
1777                 {
1778                         if (is_vertical)
1779                                 mySize.x *= 0.2 / 2; //if vertical always halve x to not cover too much numbers with 3 digits
1780                         else
1781                                 mySize.y *= 0.2;
1782                         if (panel_ar >= 4)
1783                                 mySize.x *= 2; //restore full panel size
1784                         else if (panel_ar < 1/4)
1785                                 mySize.y *= 2; //restore full panel size
1786                         HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", fuel/100, is_vertical, fuel_baralign, autocvar_hud_progressbar_fuel_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
1787                 }
1788         }
1789
1790         draw_endBoldFont();
1791 }
1792
1793 // Notification area (#4)
1794 //
1795
1796 void HUD_Notify_Push(string icon, string attacker, string victim)
1797 {
1798         if (icon == "")
1799                 return;
1800
1801         ++notify_count;
1802         --notify_index;
1803
1804         if (notify_index == -1)
1805                 notify_index = NOTIFY_MAX_ENTRIES-1;
1806
1807         // Free old strings
1808         if (notify_attackers[notify_index])
1809                 strunzone(notify_attackers[notify_index]);
1810
1811         if (notify_victims[notify_index])
1812                 strunzone(notify_victims[notify_index]);
1813
1814         if (notify_icons[notify_index])
1815                 strunzone(notify_icons[notify_index]);
1816
1817         // Allocate new strings
1818         if (victim != "")
1819         {
1820                 notify_attackers[notify_index] = strzone(attacker);
1821                 notify_victims[notify_index] = strzone(victim);
1822         }
1823         else
1824         {
1825                 // In case of a notification without a victim, the attacker
1826                 // is displayed on the victim's side. Instead of special
1827                 // treatment later on, we can simply switch them here.
1828                 notify_attackers[notify_index] = string_null;
1829                 notify_victims[notify_index] = strzone(attacker);
1830         }
1831
1832         notify_icons[notify_index] = strzone(icon);
1833         notify_times[notify_index] = time;
1834 }
1835
1836 void HUD_Notify(void)
1837 {
1838         if(intermission == 2) return;
1839         if (!autocvar__hud_configure)
1840                 if (!autocvar_hud_panel_notify)
1841                         return;
1842
1843         HUD_Panel_UpdateCvars();
1844         HUD_Panel_DrawBg(1);
1845
1846         if (!autocvar__hud_configure)
1847                 if (notify_count == 0)
1848                         return;
1849
1850         vector pos, size;
1851         pos  = panel_pos;
1852         size = panel_size;
1853
1854         if (panel_bg_padding)
1855         {
1856                 pos  += '1 1 0' * panel_bg_padding;
1857                 size -= '2 2 0' * panel_bg_padding;
1858         }
1859
1860         float fade_start = max(0, autocvar_hud_panel_notify_time);
1861         float fade_time = max(0, autocvar_hud_panel_notify_fadetime);
1862         float icon_aspect = max(1, autocvar_hud_panel_notify_icon_aspect);
1863
1864         int entry_count = bound(1, floor(NOTIFY_MAX_ENTRIES * size.y / size.x), NOTIFY_MAX_ENTRIES);
1865         float entry_height = size.y / entry_count;
1866
1867         float panel_width_half = size.x * 0.5;
1868         float icon_width_half = entry_height * icon_aspect / 2;
1869         float name_maxwidth = panel_width_half - icon_width_half - size.x * NOTIFY_ICON_MARGIN;
1870
1871         vector font_size = '0.5 0.5 0' * entry_height * autocvar_hud_panel_notify_fontsize;
1872         vector icon_size = (eX * icon_aspect + eY) * entry_height;
1873         vector icon_left = eX * (panel_width_half - icon_width_half);
1874         vector attacker_right = eX * name_maxwidth;
1875         vector victim_left = eX * (size.x - name_maxwidth);
1876
1877         vector attacker_pos, victim_pos, icon_pos;
1878         string attacker, victim, icon;
1879         int i, j, count, step, limit;
1880         float alpha;
1881
1882         if (autocvar_hud_panel_notify_flip)
1883         {
1884                 // Order items from the top down
1885                 i = 0;
1886                 step = +1;
1887                 limit = entry_count;
1888         }
1889         else
1890         {
1891                 // Order items from the bottom up
1892                 i = entry_count - 1;
1893                 step = -1;
1894                 limit = -1;
1895         }
1896
1897         for (j = notify_index, count = 0; i != limit; i += step, ++j, ++count)
1898         {
1899                 if(autocvar__hud_configure)
1900                 {
1901                         attacker = sprintf(_("Player %d"), count + 1);
1902                         victim = sprintf(_("Player %d"), count + 2);
1903                         icon = get_weaponinfo(min(WEP_FIRST + count * 2, WEP_LAST)).model2;
1904                         alpha = bound(0, 1.2 - count / entry_count, 1);
1905                 }
1906                 else
1907                 {
1908                         if (j == NOTIFY_MAX_ENTRIES)
1909                                 j = 0;
1910
1911                         if (notify_times[j] + fade_start > time)
1912                                 alpha = 1;
1913                         else if (fade_time != 0)
1914                         {
1915                                 alpha = bound(0, (notify_times[j] + fade_start + fade_time - time) / fade_time, 1);
1916                                 if (alpha == 0)
1917                                         break;
1918                         }
1919                         else
1920                                 break;
1921
1922                         attacker = notify_attackers[j];
1923                         victim = notify_victims[j];
1924                         icon = notify_icons[j];
1925                 }
1926
1927                 if (icon != "" && victim != "")
1928                 {
1929                         vector name_top = eY * (i * entry_height + 0.5 * (entry_height - font_size.y));
1930
1931                         icon_pos = pos + icon_left + eY * i * entry_height;
1932                         drawpic_aspect_skin(icon_pos, icon, icon_size, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1933
1934                         victim = textShortenToWidth(victim, name_maxwidth, font_size, stringwidth_colors);
1935                         victim_pos = pos + victim_left + name_top;
1936                         drawcolorcodedstring(victim_pos, victim, font_size, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1937
1938                         if (attacker != "")
1939                         {
1940                                 attacker = textShortenToWidth(attacker, name_maxwidth, font_size, stringwidth_colors);
1941                                 attacker_pos = pos + attacker_right - eX * stringwidth(attacker, true, font_size) + name_top;
1942                                 drawcolorcodedstring(attacker_pos, attacker, font_size, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1943                         }
1944                 }
1945         }
1946
1947         notify_count = count;
1948 }
1949
1950 // Timer (#5)
1951 //
1952 // TODO: macro
1953 string seconds_tostring(float sec)
1954 {
1955         float minutes;
1956         minutes = floor(sec / 60);
1957
1958         sec -= minutes * 60;
1959         return sprintf("%d:%02d", minutes, sec);
1960 }
1961
1962 void HUD_Timer(void)
1963 {
1964         if(intermission == 2) return;
1965         if(!autocvar__hud_configure)
1966         {
1967                 if(!autocvar_hud_panel_timer) return;
1968         }
1969
1970         HUD_Panel_UpdateCvars();
1971
1972         draw_beginBoldFont();
1973
1974         vector pos, mySize;
1975         pos = panel_pos;
1976         mySize = panel_size;
1977
1978         HUD_Panel_DrawBg(1);
1979         if(panel_bg_padding)
1980         {
1981                 pos += '1 1 0' * panel_bg_padding;
1982                 mySize -= '2 2 0' * panel_bg_padding;
1983         }
1984
1985         string timer;
1986         float timelimit, elapsedTime, timeleft, minutesLeft;
1987
1988         timelimit = getstatf(STAT_TIMELIMIT);
1989
1990         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
1991         timeleft = ceil(timeleft);
1992
1993         minutesLeft = floor(timeleft / 60);
1994
1995         vector timer_color;
1996         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
1997                 timer_color = '1 1 1'; //white
1998         else if(minutesLeft >= 1)
1999                 timer_color = '1 1 0'; //yellow
2000         else
2001                 timer_color = '1 0 0'; //red
2002
2003         if (autocvar_hud_panel_timer_increment || timelimit == 0 || warmup_stage) {
2004                 if (time < getstatf(STAT_GAMESTARTTIME)) {
2005                         //while restart is still active, show 00:00
2006                         timer = seconds_tostring(0);
2007                 } else {
2008                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
2009                         timer = seconds_tostring(elapsedTime);
2010                 }
2011         } else {
2012                 timer = seconds_tostring(timeleft);
2013         }
2014
2015         drawstring_aspect(pos, timer, mySize, timer_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2016
2017         draw_endBoldFont();
2018 }
2019
2020 // Radar (#6)
2021 //
2022
2023 float HUD_Radar_Clickable()
2024 {
2025         return hud_panel_radar_mouse && !hud_panel_radar_temp_hidden;
2026 }
2027
2028 void HUD_Radar_Show_Maximized(bool doshow,float clickable)
2029 {
2030         hud_panel_radar_maximized = doshow;
2031         hud_panel_radar_temp_hidden = 0;
2032
2033         if ( doshow )
2034         {
2035                 if (clickable)
2036                 {
2037                         if(autocvar_hud_cursormode)
2038                                 setcursormode(1);
2039                         hud_panel_radar_mouse = 1;
2040                 }
2041         }
2042         else if ( hud_panel_radar_mouse )
2043         {
2044                 hud_panel_radar_mouse = 0;
2045                 mouseClicked = 0;
2046                 if(autocvar_hud_cursormode)
2047                 if(!mv_active)
2048                         setcursormode(0);
2049         }
2050 }
2051 void HUD_Radar_Hide_Maximized()
2052 {
2053         HUD_Radar_Show_Maximized(false,false);
2054 }
2055
2056
2057 float HUD_Radar_InputEvent(float bInputType, float nPrimary, float nSecondary)
2058 {
2059         if(!hud_panel_radar_maximized || !hud_panel_radar_mouse ||
2060                 autocvar__hud_configure || mv_active)
2061                 return false;
2062
2063         if(bInputType == 3)
2064         {
2065                 mousepos_x = nPrimary;
2066                 mousepos_y = nSecondary;
2067                 return true;
2068         }
2069
2070         if(nPrimary == K_MOUSE1)
2071         {
2072                 if(bInputType == 0) // key pressed
2073                         mouseClicked |= S_MOUSE1;
2074                 else if(bInputType == 1) // key released
2075                         mouseClicked -= (mouseClicked & S_MOUSE1);
2076         }
2077         else if(nPrimary == K_MOUSE2)
2078         {
2079                 if(bInputType == 0) // key pressed
2080                         mouseClicked |= S_MOUSE2;
2081                 else if(bInputType == 1) // key released
2082                         mouseClicked -= (mouseClicked & S_MOUSE2);
2083         }
2084         else if ( nPrimary == K_ESCAPE && bInputType == 0 )
2085         {
2086                 HUD_Radar_Hide_Maximized();
2087         }
2088         else
2089         {
2090                 // allow console/use binds to work without hiding the map
2091                 string con_keys;
2092                 float keys;
2093                 float i;
2094                 con_keys = strcat(findkeysforcommand("toggleconsole", 0)," ",findkeysforcommand("+use", 0)) ;
2095                 keys = tokenize(con_keys); // findkeysforcommand returns data for this
2096                 for (i = 0; i < keys; ++i)
2097                 {
2098                         if(nPrimary == stof(argv(i)))
2099                                 return false;
2100                 }
2101
2102                 if ( getstati(STAT_HEALTH) <= 0 )
2103                 {
2104                         // Show scoreboard
2105                         if ( bInputType < 2 )
2106                         {
2107                                 con_keys = findkeysforcommand("+showscores", 0);
2108                                 keys = tokenize(con_keys);
2109                                 for (i = 0; i < keys; ++i)
2110                                 {
2111                                         if ( nPrimary == stof(argv(i)) )
2112                                         {
2113                                                 hud_panel_radar_temp_hidden = bInputType == 0;
2114                                                 return false;
2115                                         }
2116                                 }
2117                         }
2118                 }
2119                 else if ( bInputType == 0 )
2120                         HUD_Radar_Hide_Maximized();
2121
2122                 return false;
2123         }
2124
2125         return true;
2126 }
2127
2128 void HUD_Radar_Mouse()
2129 {
2130         if ( !hud_panel_radar_mouse ) return;
2131         if(mv_active) return;
2132
2133         if ( intermission )
2134         {
2135                 HUD_Radar_Hide_Maximized();
2136                 return;
2137         }
2138
2139         if(mouseClicked & S_MOUSE2)
2140         {
2141                 HUD_Radar_Hide_Maximized();
2142                 return;
2143         }
2144
2145         if(!autocvar_hud_cursormode)
2146         {
2147                 mousepos = mousepos + getmousepos() * autocvar_menu_mouse_speed;
2148
2149                 mousepos_x = bound(0, mousepos_x, vid_conwidth);
2150                 mousepos_y = bound(0, mousepos_y, vid_conheight);
2151         }
2152
2153         HUD_Panel_UpdateCvars();
2154
2155
2156         panel_size = autocvar_hud_panel_radar_maximized_size;
2157         panel_size_x = bound(0.2, panel_size_x, 1) * vid_conwidth;
2158         panel_size_y = bound(0.2, panel_size_y, 1) * vid_conheight;
2159         panel_pos_x = (vid_conwidth - panel_size_x) / 2;
2160         panel_pos_y = (vid_conheight - panel_size_y) / 2;
2161
2162         if(mouseClicked & S_MOUSE1)
2163         {
2164                 // click outside
2165                 if ( mousepos_x < panel_pos_x || mousepos_x > panel_pos_x + panel_size_x ||
2166                          mousepos_y < panel_pos_y || mousepos_y > panel_pos_y + panel_size_y )
2167                 {
2168                         HUD_Radar_Hide_Maximized();
2169                         return;
2170                 }
2171                 vector pos = teamradar_texcoord_to_3dcoord(teamradar_2dcoord_to_texcoord(mousepos),view_origin_z);
2172                 localcmd(sprintf("cmd ons_spawn %f %f %f",pos_x,pos_y,pos_z));
2173
2174                 HUD_Radar_Hide_Maximized();
2175                 return;
2176         }
2177
2178
2179         const vector cursor_size = '32 32 0';
2180         drawpic(mousepos-'8 4 0', strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"), cursor_size, '1 1 1', 0.8, DRAWFLAG_NORMAL);
2181 }
2182
2183 void HUD_Radar(void)
2184 {
2185         if(intermission == 2) return;
2186         if (!autocvar__hud_configure)
2187         {
2188                 if (hud_panel_radar_maximized)
2189                 {
2190                         if (!hud_draw_maximized) return;
2191                 }
2192                 else
2193                 {
2194                         if (autocvar_hud_panel_radar == 0) return;
2195                         if (autocvar_hud_panel_radar != 2 && !teamplay) return;
2196                         if(radar_panel_modified)
2197                         {
2198                                 panel.update_time = time; // forces reload of panel attributes
2199                                 radar_panel_modified = false;
2200                         }
2201                 }
2202         }
2203
2204         if ( hud_panel_radar_temp_hidden )
2205                 return;
2206
2207         HUD_Panel_UpdateCvars();
2208
2209         float f = 0;
2210
2211         if (hud_panel_radar_maximized && !autocvar__hud_configure)
2212         {
2213                 panel_size = autocvar_hud_panel_radar_maximized_size;
2214                 panel_size.x = bound(0.2, panel_size.x, 1) * vid_conwidth;
2215                 panel_size.y = bound(0.2, panel_size.y, 1) * vid_conheight;
2216                 panel_pos.x = (vid_conwidth - panel_size.x) / 2;
2217                 panel_pos.y = (vid_conheight - panel_size.y) / 2;
2218
2219                 string panel_bg;
2220                 panel_bg = strcat(hud_skin_path, "/border_default"); // always use the default border when maximized
2221                 if(precache_pic(panel_bg) == "")
2222                         panel_bg = "gfx/hud/default/border_default"; // fallback
2223                 if(!radar_panel_modified && panel_bg != panel.current_panel_bg)
2224                         radar_panel_modified = true;
2225                 if(panel.current_panel_bg)
2226                         strunzone(panel.current_panel_bg);
2227                 panel.current_panel_bg = strzone(panel_bg);
2228
2229                 switch(hud_panel_radar_maximized_zoommode)
2230                 {
2231                         default:
2232                         case 0:
2233                                 f = current_zoomfraction;
2234                                 break;
2235                         case 1:
2236                                 f = 1 - current_zoomfraction;
2237                                 break;
2238                         case 2:
2239                                 f = 0;
2240                                 break;
2241                         case 3:
2242                                 f = 1;
2243                                 break;
2244                 }
2245
2246                 switch(hud_panel_radar_maximized_rotation)
2247                 {
2248                         case 0:
2249                                 teamradar_angle = view_angles.y - 90;
2250                                 break;
2251                         default:
2252                                 teamradar_angle = 90 * hud_panel_radar_maximized_rotation;
2253                                 break;
2254                 }
2255         }
2256         if (!hud_panel_radar_maximized && !autocvar__hud_configure)
2257         {
2258                 switch(hud_panel_radar_zoommode)
2259                 {
2260                         default:
2261                         case 0:
2262                                 f = current_zoomfraction;
2263                                 break;
2264                         case 1:
2265                                 f = 1 - current_zoomfraction;
2266                                 break;
2267                         case 2:
2268                                 f = 0;
2269                                 break;
2270                         case 3:
2271                                 f = 1;
2272                                 break;
2273                 }
2274
2275                 switch(hud_panel_radar_rotation)
2276                 {
2277                         case 0:
2278                                 teamradar_angle = view_angles.y - 90;
2279                                 break;
2280                         default:
2281                                 teamradar_angle = 90 * hud_panel_radar_rotation;
2282                                 break;
2283                 }
2284         }
2285
2286         vector pos, mySize;
2287         pos = panel_pos;
2288         mySize = panel_size;
2289
2290         HUD_Panel_DrawBg(1);
2291         if(panel_bg_padding)
2292         {
2293                 pos += '1 1 0' * panel_bg_padding;
2294                 mySize -= '2 2 0' * panel_bg_padding;
2295         }
2296
2297         int color2;
2298         entity tm;
2299         float scale2d, normalsize, bigsize;
2300
2301         teamradar_origin2d = pos + 0.5 * mySize;
2302         teamradar_size2d = mySize;
2303
2304         if(minimapname == "")
2305                 return;
2306
2307         teamradar_loadcvars();
2308
2309         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
2310         teamradar_size2d = mySize;
2311
2312         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
2313
2314         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
2315         if((hud_panel_radar_rotation == 0 && !hud_panel_radar_maximized) || (hud_panel_radar_maximized_rotation == 0 && hud_panel_radar_maximized))
2316         {
2317                 // max-min distance must fit the radar in any rotation
2318                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_scale));
2319         }
2320         else
2321         {
2322                 vector c0, c1, c2, c3, span;
2323                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
2324                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
2325                 c2 = rotate('1 0 0' * mi_min.x + '0 1 0' * mi_max.y, teamradar_angle * DEG2RAD);
2326                 c3 = rotate('1 0 0' * mi_max.x + '0 1 0' * mi_min.y, teamradar_angle * DEG2RAD);
2327                 span = '0 0 0';
2328                 span.x = max(c0_x, c1_x, c2_x, c3_x) - min(c0_x, c1_x, c2_x, c3_x);
2329                 span.y = max(c0_y, c1_y, c2_y, c3_y) - min(c0_y, c1_y, c2_y, c3_y);
2330
2331                 // max-min distance must fit the radar in x=x, y=y
2332                 bigsize = min(
2333                         teamradar_size2d.x * scale2d / (1.05 * span.x),
2334                         teamradar_size2d.y * scale2d / (1.05 * span.y)
2335                 );
2336         }
2337
2338         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
2339         if(bigsize > normalsize)
2340                 normalsize = bigsize;
2341
2342         teamradar_size =
2343                   f * bigsize
2344                 + (1 - f) * normalsize;
2345         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
2346                   f * mi_center
2347                 + (1 - f) * view_origin);
2348
2349         drawsetcliparea(
2350                 pos.x,
2351                 pos.y,
2352                 mySize.x,
2353                 mySize.y
2354         );
2355
2356         draw_teamradar_background(hud_panel_radar_foreground_alpha);
2357
2358         for(tm = world; (tm = find(tm, classname, "radarlink")); )
2359                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
2360
2361         vector coord;
2362         vector brightcolor;
2363         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
2364         {
2365                 if ( hud_panel_radar_mouse )
2366                 if ( tm.health > 0 )
2367                 if ( tm.team == myteam+1 )
2368                 {
2369                         coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(tm.origin));
2370                         if ( vlen(mousepos-coord) < 8 )
2371                         {
2372                                 brightcolor_x = min(1,tm.teamradar_color_x*1.5);
2373                                 brightcolor_y = min(1,tm.teamradar_color_y*1.5);
2374                                 brightcolor_z = min(1,tm.teamradar_color_z*1.5);
2375                                 drawpic(coord - '8 8 0', "gfx/teamradar_icon_glow", '16 16 0', brightcolor, panel_fg_alpha, 0);
2376                         }
2377                 }
2378
2379                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, panel_fg_alpha);
2380         }
2381         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
2382         {
2383                 color2 = GetPlayerColor(tm.sv_entnum);
2384                 //if(color == NUM_SPECTATOR || color == color2)
2385                         draw_teamradar_player(tm.origin, tm.angles, Team_ColorRGB(color2));
2386         }
2387         draw_teamradar_player(view_origin, view_angles, '1 1 1');
2388
2389         drawresetcliparea();
2390
2391         if ( hud_panel_radar_mouse )
2392         {
2393                 string message = "Click to select teleport destination";
2394
2395                 if ( getstati(STAT_HEALTH) <= 0 )
2396                 {
2397                         message = "Click to select spawn location";
2398                 }
2399
2400                 drawcolorcodedstring(pos + '0.5 0 0' * (mySize_x - stringwidth(message, true, hud_fontsize)) - '0 1 0' * hud_fontsize_y * 2,
2401                                                          message, hud_fontsize, hud_panel_radar_foreground_alpha, DRAWFLAG_NORMAL);
2402
2403                 hud_panel_radar_bottom = pos_y + mySize_y + hud_fontsize_y;
2404         }
2405 }
2406
2407 // Score (#7)
2408 //
2409 void HUD_UpdatePlayerTeams();
2410 void HUD_Score_Rankings(vector pos, vector mySize, entity me)
2411 {
2412         float score;
2413         entity tm = world, pl;
2414         int SCOREPANEL_MAX_ENTRIES = 6;
2415         float SCOREPANEL_ASPECTRATIO = 2;
2416         int entries = bound(1, floor(SCOREPANEL_MAX_ENTRIES * mySize.y/mySize.x * SCOREPANEL_ASPECTRATIO), SCOREPANEL_MAX_ENTRIES);
2417         vector fontsize = '1 1 0' * (mySize.y/entries);
2418
2419         vector rgb, score_color;
2420         rgb = '1 1 1';
2421         score_color = '1 1 1';
2422
2423         float name_size = mySize.x*0.75;
2424         float spacing_size = mySize.x*0.04;
2425         const float highlight_alpha = 0.2;
2426         int i = 0, first_pl = 0;
2427         bool me_printed = false;
2428         string s;
2429         if (autocvar__hud_configure)
2430         {
2431                 float players_per_team = 0;
2432                 if (team_count)
2433                 {
2434                         // show team scores in the first line
2435                         float score_size = mySize.x / team_count;
2436                         players_per_team = max(2, ceil((entries - 1) / team_count));
2437                         for(i=0; i<team_count; ++i) {
2438                                 if (i == floor((entries - 2) / players_per_team) || (entries == 1 && i == 0))
2439                                         HUD_Panel_DrawHighlight(pos + eX * score_size * i, eX * score_size + eY * fontsize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2440                                 drawstring_aspect(pos + eX * score_size * i, ftos(175 - 23*i), eX * score_size + eY * fontsize.y, Team_ColorRGB(ColorByTeam(i)) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2441                         }
2442                         first_pl = 1;
2443                         pos.y += fontsize.y;
2444                 }
2445                 score = 10 + SCOREPANEL_MAX_ENTRIES * 3;
2446                 for (i=first_pl; i<entries; ++i)
2447                 {
2448                         //simulate my score is lower than all displayed players,
2449                         //so that I don't appear at all showing pure rankings.
2450                         //This is to better show the difference between the 2 ranking views
2451                         if (i == entries-1 && autocvar_hud_panel_score_rankings == 1)
2452                         {
2453                                 rgb = '1 1 0';
2454                                 drawfill(pos, eX * mySize.x + eY * fontsize.y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2455                                 s = GetPlayerName(player_localnum);
2456                                 score = 7;
2457                         }
2458                         else
2459                         {
2460                                 s = sprintf(_("Player %d"), i + 1 - first_pl);
2461                                 score -= 3;
2462                         }
2463
2464                         if (team_count)
2465                                 score_color = Team_ColorRGB(ColorByTeam(floor((i - first_pl) / players_per_team))) * 0.8;
2466                         s = textShortenToWidth(s, name_size, fontsize, stringwidth_colors);
2467                         drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, true, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
2468                         drawstring(pos + eX * (name_size + spacing_size), ftos(score), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2469                         pos.y += fontsize.y;
2470                 }
2471                 return;
2472         }
2473
2474         if (!scoreboard_fade_alpha) // the scoreboard too calls HUD_UpdatePlayerTeams
2475                 HUD_UpdatePlayerTeams();
2476         if (team_count)
2477         {
2478                 // show team scores in the first line
2479                 float score_size = mySize.x / team_count;
2480                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2481                         if(tm.team == NUM_SPECTATOR)
2482                                 continue;
2483                         if (tm.team == myteam)
2484                                 drawfill(pos + eX * score_size * i, eX * score_size + eY * fontsize.y, '1 1 1', highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2485                         drawstring_aspect(pos + eX * score_size * i, ftos(tm.(teamscores[ts_primary])), eX * score_size + eY * fontsize.y, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2486                         ++i;
2487                 }
2488                 first_pl = 1;
2489                 pos.y += fontsize.y;
2490                 tm = teams.sort_next;
2491         }
2492         i = first_pl;
2493
2494         do
2495         for (pl = players.sort_next; pl && i<entries; pl = pl.sort_next)
2496         {
2497                 if ((team_count && pl.team != tm.team) || pl.team == NUM_SPECTATOR)
2498                         continue;
2499
2500                 if (i == entries-1 && !me_printed && pl != me)
2501                 if (autocvar_hud_panel_score_rankings == 1 && spectatee_status != -1)
2502                 {
2503                         for (pl = me.sort_next; pl; pl = pl.sort_next)
2504                                 if (pl.team != NUM_SPECTATOR)
2505                                         break;
2506
2507                         if (pl)
2508                                 rgb = '1 1 0'; //not last but not among the leading players: yellow
2509                         else
2510                                 rgb = '1 0 0'; //last: red
2511                         pl = me;
2512                 }
2513
2514                 if (pl == me)
2515                 {
2516                         if (i == first_pl)
2517                                 rgb = '0 1 0'; //first: green
2518                         me_printed = true;
2519                         drawfill(pos, eX * mySize.x + eY * fontsize.y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2520                 }
2521                 if (team_count)
2522                         score_color = Team_ColorRGB(pl.team) * 0.8;
2523                 s = textShortenToWidth(GetPlayerName(pl.sv_entnum), name_size, fontsize, stringwidth_colors);
2524                 drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, true, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
2525                 drawstring(pos + eX * (name_size + spacing_size), ftos(pl.(scores[ps_primary])), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2526                 pos.y += fontsize.y;
2527                 ++i;
2528         }
2529         while (i<entries && team_count && (tm = tm.sort_next) && (tm.team != NUM_SPECTATOR || (tm = tm.sort_next)));
2530 }
2531
2532 void HUD_Score(void)
2533 {
2534         if(intermission == 2) return;
2535         if(!autocvar__hud_configure)
2536         {
2537                 if(!autocvar_hud_panel_score) return;
2538                 if(spectatee_status == -1 && (gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS)) return;
2539         }
2540
2541         HUD_Panel_UpdateCvars();
2542         vector pos, mySize;
2543         pos = panel_pos;
2544         mySize = panel_size;
2545
2546         HUD_Panel_DrawBg(1);
2547         if(panel_bg_padding)
2548         {
2549                 pos += '1 1 0' * panel_bg_padding;
2550                 mySize -= '2 2 0' * panel_bg_padding;
2551         }
2552
2553         float score, distribution = 0;
2554         string sign;
2555         vector distribution_color;
2556         entity tm, pl, me;
2557
2558         me = playerslots[current_player];
2559
2560         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
2561                 string timer, distrtimer;
2562
2563                 pl = players.sort_next;
2564                 if(pl == me)
2565                         pl = pl.sort_next;
2566                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
2567                         if(pl.scores[ps_primary] == 0)
2568                                 pl = world;
2569
2570                 score = me.(scores[ps_primary]);
2571                 timer = TIME_ENCODED_TOSTRING(score);
2572
2573                 draw_beginBoldFont();
2574                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
2575                         // distribution display
2576                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2577
2578                         distrtimer = ftos_decimals(fabs(distribution/pow(10, TIME_DECIMALS)), TIME_DECIMALS);
2579
2580                         if (distribution <= 0) {
2581                                 distribution_color = '0 1 0';
2582                                 sign = "-";
2583                         }
2584                         else {
2585                                 distribution_color = '1 0 0';
2586                                 sign = "+";
2587                         }
2588                         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);
2589                 }
2590                 // race record display
2591                 if (distribution <= 0)
2592                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2593                 drawstring_aspect(pos, timer, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2594                 draw_endBoldFont();
2595         } else if (!teamplay) { // non-teamgames
2596                 if ((spectatee_status == -1 && !autocvar__hud_configure) || autocvar_hud_panel_score_rankings)
2597                 {
2598                         HUD_Score_Rankings(pos, mySize, me);
2599                         return;
2600                 }
2601                 // me vector := [team/connected frags id]
2602                 pl = players.sort_next;
2603                 if(pl == me)
2604                         pl = pl.sort_next;
2605
2606                 if(autocvar__hud_configure)
2607                         distribution = 42;
2608                 else if(pl)
2609                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2610                 else
2611                         distribution = 0;
2612
2613                 score = me.(scores[ps_primary]);
2614                 if(autocvar__hud_configure)
2615                         score = 123;
2616
2617                 if(distribution >= 5)
2618                         distribution_color = eY;
2619                 else if(distribution >= 0)
2620                         distribution_color = '1 1 1';
2621                 else if(distribution >= -5)
2622                         distribution_color = '1 1 0';
2623                 else
2624                         distribution_color = eX;
2625
2626                 string distribution_str;
2627                 distribution_str = ftos(distribution);
2628                 draw_beginBoldFont();
2629                 if (distribution >= 0)
2630                 {
2631                         if (distribution > 0)
2632                                 distribution_str = strcat("+", distribution_str);
2633                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2634                 }
2635                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize.x + eY * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2636                 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);
2637                 draw_endBoldFont();
2638         } else { // teamgames
2639                 float row, column, rows = 0, columns = 0;
2640                 vector offset = '0 0 0';
2641                 vector score_pos, score_size; //for scores other than myteam
2642                 if(autocvar_hud_panel_score_rankings)
2643                 {
2644                         HUD_Score_Rankings(pos, mySize, me);
2645                         return;
2646                 }
2647                 if(spectatee_status == -1)
2648                 {
2649                         rows = HUD_GetRowCount(team_count, mySize, 3);
2650                         columns = ceil(team_count/rows);
2651                         score_size = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
2652
2653                         float newSize;
2654                         if(score_size.x/score_size.y > 3)
2655                         {
2656                                 newSize = 3 * score_size.y;
2657                                 offset.x = score_size.x - newSize;
2658                                 pos.x += offset.x/2;
2659                                 score_size.x = newSize;
2660                         }
2661                         else
2662                         {
2663                                 newSize = 1/3 * score_size.x;
2664                                 offset.y = score_size.y - newSize;
2665                                 pos.y += offset.y/2;
2666                                 score_size.y = newSize;
2667                         }
2668                 }
2669                 else
2670                         score_size = eX * mySize.x*(1/4) + eY * mySize.y*(1/3);
2671
2672                 float max_fragcount;
2673                 max_fragcount = -99;
2674                 draw_beginBoldFont();
2675                 row = column = 0;
2676                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2677                         if(tm.team == NUM_SPECTATOR)
2678                                 continue;
2679                         score = tm.(teamscores[ts_primary]);
2680                         if(autocvar__hud_configure)
2681                                 score = 123;
2682
2683                         if (score > max_fragcount)
2684                                 max_fragcount = score;
2685
2686                         if (spectatee_status == -1)
2687                         {
2688                                 score_pos = pos + eX * column * (score_size.x + offset.x) + eY * row * (score_size.y + offset.y);
2689                                 if (max_fragcount == score)
2690                                         HUD_Panel_DrawHighlight(score_pos, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2691                                 drawstring_aspect(score_pos, ftos(score), score_size, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2692                                 ++row;
2693                                 if(row >= rows)
2694                                 {
2695                                         row = 0;
2696                                         ++column;
2697                                 }
2698                         }
2699                         else if(tm.team == myteam) {
2700                                 if (max_fragcount == score)
2701                                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2702                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize.x + eY * mySize.y, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2703                         } else {
2704                                 if (max_fragcount == score)
2705                                         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);
2706                                 drawstring_aspect(pos + eX * 0.75 * mySize.x + eY * (1/3) * rows * mySize.y, ftos(score), score_size, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2707                                 ++rows;
2708                         }
2709                 }
2710                 draw_endBoldFont();
2711         }
2712 }
2713
2714 // Race timer (#8)
2715 //
2716 void HUD_RaceTimer (void)
2717 {
2718         if(intermission == 2) return;
2719         if(!autocvar__hud_configure)
2720         {
2721                 if(!autocvar_hud_panel_racetimer) return;
2722                 if(!(gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS)) return;
2723                 if(spectatee_status == -1) return;
2724         }
2725
2726         HUD_Panel_UpdateCvars();
2727
2728         vector pos, mySize;
2729         pos = panel_pos;
2730         mySize = panel_size;
2731
2732         HUD_Panel_DrawBg(1);
2733         if(panel_bg_padding)
2734         {
2735                 pos += '1 1 0' * panel_bg_padding;
2736                 mySize -= '2 2 0' * panel_bg_padding;
2737         }
2738
2739         // always force 4:1 aspect
2740         vector newSize = '0 0 0';
2741         if(mySize.x/mySize.y > 4)
2742         {
2743                 newSize.x = 4 * mySize.y;
2744                 newSize.y = mySize.y;
2745
2746                 pos.x = pos.x + (mySize.x - newSize.x) / 2;
2747         }
2748         else
2749         {
2750                 newSize.y = 1/4 * mySize.x;
2751                 newSize.x = mySize.x;
2752
2753                 pos.y = pos.y + (mySize.y - newSize.y) / 2;
2754         }
2755         mySize = newSize;
2756
2757         float a, t;
2758         string s, forcetime;
2759
2760         if(autocvar__hud_configure)
2761         {
2762                 s = "0:13:37";
2763                 draw_beginBoldFont();
2764                 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);
2765                 draw_endBoldFont();
2766                 s = _("^1Intermediate 1 (+15.42)");
2767                 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);
2768                 s = sprintf(_("^1PENALTY: %.1f (%s)"), 2, "missing a checkpoint");
2769                 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);
2770         }
2771         else if(race_checkpointtime)
2772         {
2773                 a = bound(0, 2 - (time - race_checkpointtime), 1);
2774                 s = "";
2775                 forcetime = "";
2776                 if(a > 0) // just hit a checkpoint?
2777                 {
2778                         if(race_checkpoint != 254)
2779                         {
2780                                 if(race_time && race_previousbesttime)
2781                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
2782                                 else
2783                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
2784                                 if(race_time)
2785                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
2786                         }
2787                 }
2788                 else
2789                 {
2790                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
2791                         {
2792                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
2793                                 if(a > 0) // next one?
2794                                 {
2795                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
2796                                 }
2797                         }
2798                 }
2799
2800                 if(s != "" && a > 0)
2801                 {
2802                         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);
2803                 }
2804
2805                 if(race_penaltytime)
2806                 {
2807                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
2808                         if(a > 0)
2809                         {
2810                                 s = sprintf(_("^1PENALTY: %.1f (%s)"), race_penaltytime * 0.1, race_penaltyreason);
2811                                 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);
2812                         }
2813                 }
2814
2815                 draw_beginBoldFont();
2816
2817                 if(forcetime != "")
2818                 {
2819                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
2820                         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);
2821                 }
2822                 else
2823                         a = 1;
2824
2825                 if(race_laptime && race_checkpoint != 255)
2826                 {
2827                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
2828                         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);
2829                 }
2830
2831                 draw_endBoldFont();
2832         }
2833         else
2834         {
2835                 if(race_mycheckpointtime)
2836                 {
2837                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
2838                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -(race_mycheckpointenemy == ""), race_mycheckpointlapsdelta, race_mycheckpointenemy);
2839                         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);
2840                 }
2841                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
2842                 {
2843                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
2844                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -(race_othercheckpointenemy == ""), race_othercheckpointlapsdelta, race_othercheckpointenemy);
2845                         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);
2846                 }
2847
2848                 if(race_penaltytime && !race_penaltyaccumulator)
2849                 {
2850                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
2851                         a = bound(0, (1 + t - time), 1);
2852                         if(a > 0)
2853                         {
2854                                 if(time < t)
2855                                         s = sprintf(_("^1PENALTY: %.1f (%s)"), (t - time) * 0.1, race_penaltyreason);
2856                                 else
2857                                         s = sprintf(_("^2PENALTY: %.1f (%s)"), 0, race_penaltyreason);
2858                                 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);
2859                         }
2860                 }
2861         }
2862 }
2863
2864 // Vote window (#9)
2865 //
2866
2867 void HUD_Vote(void)
2868 {
2869         if(intermission == 2) return;
2870         if(autocvar_cl_allow_uid2name == -1 && (gametype == MAPINFO_TYPE_CTS || gametype == MAPINFO_TYPE_RACE || (serverflags & SERVERFLAG_PLAYERSTATS)))
2871         {
2872                 vote_active = 1;
2873                 if (autocvar__hud_configure)
2874                 {
2875                         vote_yescount = 0;
2876                         vote_nocount = 0;
2877                         print(_("^1You must answer before entering hud configure mode\n"));
2878                         cvar_set("_hud_configure", "0");
2879                 }
2880                 if(vote_called_vote)
2881                         strunzone(vote_called_vote);
2882                 vote_called_vote = strzone(_("^2Name ^7instead of \"^1Anonymous player^7\" in stats"));
2883                 uid2name_dialog = 1;
2884         }
2885
2886         if(!autocvar__hud_configure)
2887         {
2888                 if(!autocvar_hud_panel_vote) return;
2889
2890                 panel_fg_alpha = autocvar_hud_panel_fg_alpha;
2891                 panel_bg_alpha_str = autocvar_hud_panel_vote_bg_alpha;
2892
2893                 if(panel_bg_alpha_str == "") {
2894                         panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);
2895                 }
2896                 panel_bg_alpha = stof(panel_bg_alpha_str);
2897         }
2898         else
2899         {
2900                 vote_yescount = 3;
2901                 vote_nocount = 2;
2902                 vote_needed = 4;
2903         }
2904
2905         string s;
2906         float a;
2907         if(vote_active != vote_prev) {
2908                 vote_change = time;
2909                 vote_prev = vote_active;
2910         }
2911
2912         if(vote_active || autocvar__hud_configure)
2913                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
2914         else
2915                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
2916
2917         if(!vote_alpha)
2918                 return;
2919
2920         HUD_Panel_UpdateCvars();
2921
2922         if(uid2name_dialog)
2923         {
2924                 panel_pos = eX * 0.3 * vid_conwidth + eY * 0.1 * vid_conheight;
2925                 panel_size = eX * 0.4 * vid_conwidth + eY * 0.3 * vid_conheight;
2926         }
2927
2928     // these must be below above block
2929         vector pos, mySize;
2930         pos = panel_pos;
2931         mySize = panel_size;
2932
2933         a = vote_alpha * (vote_highlighted ? autocvar_hud_panel_vote_alreadyvoted_alpha : 1);
2934         HUD_Panel_DrawBg(a);
2935         a = panel_fg_alpha * a;
2936
2937         if(panel_bg_padding)
2938         {
2939                 pos += '1 1 0' * panel_bg_padding;
2940                 mySize -= '2 2 0' * panel_bg_padding;
2941         }
2942
2943         // always force 3:1 aspect
2944         vector newSize = '0 0 0';
2945         if(mySize.x/mySize.y > 3)
2946         {
2947                 newSize.x = 3 * mySize.y;
2948                 newSize.y = mySize.y;
2949
2950                 pos.x = pos.x + (mySize.x - newSize.x) / 2;
2951         }
2952         else
2953         {
2954                 newSize.y = 1/3 * mySize.x;
2955                 newSize.x = mySize.x;
2956
2957                 pos.y = pos.y + (mySize.y - newSize.y) / 2;
2958         }
2959         mySize = newSize;
2960
2961         s = _("A vote has been called for:");
2962         if(uid2name_dialog)
2963                 s = _("Allow servers to store and display your name?");
2964         drawstring_aspect(pos, s, eX * mySize.x + eY * (2/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2965         s = textShortenToWidth(vote_called_vote, mySize.x, '1 1 0' * mySize.y * (1/8), stringwidth_colors);
2966         if(autocvar__hud_configure)
2967                 s = _("^1Configure the HUD");
2968         drawcolorcodedstring_aspect(pos + eY * (2/8) * mySize.y, s, eX * mySize.x + eY * (1.75/8) * mySize.y, a, DRAWFLAG_NORMAL);
2969
2970         // print the yes/no counts
2971     s = sprintf(_("Yes (%s): %d"), getcommandkey("vyes", "vyes"), vote_yescount);
2972         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);
2973     s = sprintf(_("No (%s): %d"), getcommandkey("vno", "vno"), vote_nocount);
2974         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);
2975
2976         // draw the progress bar backgrounds
2977         drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_back", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2978
2979         // draw the highlights
2980         if(vote_highlighted == 1) {
2981                 drawsetcliparea(pos.x, pos.y, mySize.x * 0.5, mySize.y);
2982                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_voted", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2983         }
2984         else if(vote_highlighted == -1) {
2985                 drawsetcliparea(pos.x + 0.5 * mySize.x, pos.y, mySize.x * 0.5, mySize.y);
2986                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_voted", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2987         }
2988
2989         // draw the progress bars
2990         if(vote_yescount && vote_needed)
2991         {
2992                 drawsetcliparea(pos.x, pos.y, mySize.x * 0.5 * (vote_yescount/vote_needed), 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         if(vote_nocount && vote_needed)
2997         {
2998                 drawsetcliparea(pos.x + mySize.x - mySize.x * 0.5 * (vote_nocount/vote_needed), pos.y, mySize.x * 0.5, mySize.y);
2999                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_prog", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
3000         }
3001
3002         drawresetcliparea();
3003 }
3004
3005 // Mod icons panel (#10)
3006 //
3007
3008 bool mod_active; // is there any active mod icon?
3009
3010 void DrawCAItem(vector myPos, vector mySize, float aspect_ratio, int layout, int i)
3011 {
3012         int stat = -1;
3013         string pic = "";
3014         vector color = '0 0 0';
3015         switch(i)
3016         {
3017                 case 0:
3018                         stat = getstati(STAT_REDALIVE);
3019                         pic = "player_red.tga";
3020                         color = '1 0 0';
3021                         break;
3022                 case 1:
3023                         stat = getstati(STAT_BLUEALIVE);
3024                         pic = "player_blue.tga";
3025                         color = '0 0 1';
3026                         break;
3027                 case 2:
3028                         stat = getstati(STAT_YELLOWALIVE);
3029                         pic = "player_yellow.tga";
3030                         color = '1 1 0';
3031                         break;
3032                 default:
3033                 case 3:
3034                         stat = getstati(STAT_PINKALIVE);
3035                         pic = "player_pink.tga";
3036                         color = '1 0 1';
3037                         break;
3038         }
3039
3040         if(mySize.x/mySize.y > aspect_ratio)
3041         {
3042                 i = aspect_ratio * mySize.y;
3043                 myPos.x = myPos.x + (mySize.x - i) / 2;
3044                 mySize.x = i;
3045         }
3046         else
3047         {
3048                 i = 1/aspect_ratio * mySize.x;
3049                 myPos.y = myPos.y + (mySize.y - i) / 2;
3050                 mySize.y = i;
3051         }
3052
3053         if(layout)
3054         {
3055                 drawpic_aspect_skin(myPos, pic, eX * 0.7 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3056                 drawstring_aspect(myPos + eX * 0.7 * mySize.x, ftos(stat), eX * 0.3 * mySize.x + eY * mySize.y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
3057         }
3058         else
3059                 drawstring_aspect(myPos, ftos(stat), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
3060 }
3061
3062 // Clan Arena and Freeze Tag HUD modicons
3063 void HUD_Mod_CA(vector myPos, vector mySize)
3064 {
3065         mod_active = 1; // required in each mod function that always shows something
3066
3067         int layout;
3068         if(gametype == MAPINFO_TYPE_CA)
3069                 layout = autocvar_hud_panel_modicons_ca_layout;
3070         else //if(gametype == MAPINFO_TYPE_FREEZETAG)
3071                 layout = autocvar_hud_panel_modicons_freezetag_layout;
3072         int rows, columns;
3073         float aspect_ratio;
3074         aspect_ratio = (layout) ? 2 : 1;
3075         rows = HUD_GetRowCount(team_count, mySize, aspect_ratio);
3076         columns = ceil(team_count/rows);
3077
3078         int i;
3079         float row = 0, column = 0;
3080         vector pos, itemSize;
3081         itemSize = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
3082         for(i=0; i<team_count; ++i)
3083         {
3084                 pos = myPos + eX * column * itemSize.x + eY * row * itemSize.y;
3085
3086                 DrawCAItem(pos, itemSize, aspect_ratio, layout, i);
3087
3088                 ++row;
3089                 if(row >= rows)
3090                 {
3091                         row = 0;
3092                         ++column;
3093                 }
3094         }
3095 }
3096
3097 // CTF HUD modicon section
3098 int redflag_prevframe, blueflag_prevframe, yellowflag_prevframe, pinkflag_prevframe, neutralflag_prevframe; // status during previous frame
3099 int redflag_prevstatus, blueflag_prevstatus, yellowflag_prevstatus, pinkflag_prevstatus, neutralflag_prevstatus; // last remembered status
3100 float redflag_statuschange_time, blueflag_statuschange_time, yellowflag_statuschange_time, pinkflag_statuschange_time, neutralflag_statuschange_time; // time when the status changed
3101
3102 void HUD_Mod_CTF_Reset(void)
3103 {
3104         redflag_prevstatus = blueflag_prevstatus = yellowflag_prevstatus = pinkflag_prevstatus = neutralflag_prevstatus = 0;
3105         redflag_prevframe = blueflag_prevframe = yellowflag_prevframe = pinkflag_prevframe = neutralflag_prevframe = 0;
3106         redflag_statuschange_time = blueflag_statuschange_time = yellowflag_statuschange_time = pinkflag_statuschange_time = neutralflag_statuschange_time = 0;
3107 }
3108
3109 void HUD_Mod_CTF(vector pos, vector mySize)
3110 {
3111         vector redflag_pos, blueflag_pos, yellowflag_pos, pinkflag_pos, neutralflag_pos;
3112         vector flag_size;
3113         float f; // every function should have that
3114
3115         int redflag, blueflag, yellowflag, pinkflag, neutralflag; // current status
3116         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime, yellowflag_statuschange_elapsedtime, pinkflag_statuschange_elapsedtime, neutralflag_statuschange_elapsedtime; // time since the status changed
3117         bool ctf_oneflag; // one-flag CTF mode enabled/disabled
3118         int stat_items = getstati(STAT_CTF_FLAGSTATUS, 0, 24);
3119         float fs, fs2, fs3, size1, size2;
3120         vector e1, e2;
3121
3122         redflag = (stat_items/CTF_RED_FLAG_TAKEN) & 3;
3123         blueflag = (stat_items/CTF_BLUE_FLAG_TAKEN) & 3;
3124         yellowflag = (stat_items/CTF_YELLOW_FLAG_TAKEN) & 3;
3125         pinkflag = (stat_items/CTF_PINK_FLAG_TAKEN) & 3;
3126         neutralflag = (stat_items/CTF_NEUTRAL_FLAG_TAKEN) & 3;
3127
3128         ctf_oneflag = (stat_items & CTF_FLAG_NEUTRAL);
3129
3130         mod_active = (redflag || blueflag || yellowflag || pinkflag || neutralflag);
3131
3132         if (autocvar__hud_configure) {
3133                 redflag = 1;
3134                 blueflag = 2;
3135                 if (team_count >= 3)
3136                         yellowflag = 2;
3137                 if (team_count >= 4)
3138                         pinkflag = 3;
3139                 ctf_oneflag = neutralflag = 0; // disable neutral flag in hud editor?
3140         }
3141
3142         // when status CHANGES, set old status into prevstatus and current status into status
3143         #define X(team) do {                                                                                                                    \
3144                 if (team##flag != team##flag_prevframe) {                                                                       \
3145                 team##flag_statuschange_time = time;                                                                    \
3146                 team##flag_prevstatus = team##flag_prevframe;                                                   \
3147                 team##flag_prevframe = team##flag;                                                                              \
3148         }                                                                                                                                                       \
3149         team##flag_statuschange_elapsedtime = time - team##flag_statuschange_time;      \
3150     } while (0)
3151         X(red);
3152         X(blue);
3153         X(yellow);
3154         X(pink);
3155         X(neutral);
3156         #undef X
3157
3158         const float BLINK_FACTOR = 0.15;
3159         const float BLINK_BASE = 0.85;
3160         // note:
3161         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
3162         // thus
3163         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
3164         // ensure RMS == 1
3165         const float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
3166
3167         #define X(team, cond) \
3168         string team##_icon, team##_icon_prevstatus; \
3169         int team##_alpha, team##_alpha_prevstatus; \
3170         team##_alpha = team##_alpha_prevstatus = 1; \
3171         do { \
3172                 switch (team##flag) { \
3173                         case 1: team##_icon = "flag_" #team "_taken"; break; \
3174                         case 2: team##_icon = "flag_" #team "_lost"; break; \
3175                         case 3: team##_icon = "flag_" #team "_carrying"; team##_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break; \
3176                         default: \
3177                                 if ((stat_items & CTF_SHIELDED) && (cond)) { \
3178                                         team##_icon = "flag_" #team "_shielded"; \
3179                                 } else { \
3180                                         team##_icon = string_null; \
3181                                 } \
3182                                 break; \
3183                 } \
3184                 switch (team##flag_prevstatus) { \
3185                         case 1: team##_icon_prevstatus = "flag_" #team "_taken"; break; \
3186                         case 2: team##_icon_prevstatus = "flag_" #team "_lost"; break; \
3187                         case 3: team##_icon_prevstatus = "flag_" #team "_carrying"; team##_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break; \
3188                         default: \
3189                                 if (team##flag == 3) { \
3190                                         team##_icon_prevstatus = "flag_" #team "_carrying"; /* make it more visible */\
3191                                 } else if((stat_items & CTF_SHIELDED) && (cond)) { \
3192                                         team##_icon_prevstatus = "flag_" #team "_shielded"; \
3193                                 } else { \
3194                                         team##_icon_prevstatus = string_null; \
3195                                 } \
3196                                 break; \
3197                 } \
3198         } while (0)
3199         X(red, myteam != NUM_TEAM_1);
3200         X(blue, myteam != NUM_TEAM_2);
3201         X(yellow, myteam != NUM_TEAM_3);
3202         X(pink, myteam != NUM_TEAM_4);
3203         X(neutral, true);
3204         #undef X
3205
3206         if (ctf_oneflag) {
3207                 // hacky, but these aren't needed
3208                 red_icon = red_icon_prevstatus = blue_icon = blue_icon_prevstatus = yellow_icon = yellow_icon_prevstatus = pink_icon = pink_icon_prevstatus = string_null;
3209                 fs = fs2 = fs3 = 1;
3210         } else switch (team_count) {
3211                 default:
3212                 case 2: fs = 0.5; fs2 = 0.5; fs3 = 0.5; break;
3213                 case 3: fs = 1; fs2 = 0.35; fs3 = 0.35; break;
3214                 case 4: fs = 0.75; fs2 = 0.25; fs3 = 0.5; break;
3215         }
3216
3217         if (mySize_x > mySize_y) {
3218                 size1 = mySize_x;
3219                 size2 = mySize_y;
3220                 e1 = eX;
3221                 e2 = eY;
3222         } else {
3223                 size1 = mySize_y;
3224                 size2 = mySize_x;
3225                 e1 = eY;
3226                 e2 = eX;
3227         }
3228
3229         switch (myteam) {
3230                 default:
3231                 case NUM_TEAM_1: {
3232                         redflag_pos = pos;
3233                         blueflag_pos = pos + eX * fs2 * size1;
3234                         yellowflag_pos = pos - eX * fs2 * size1;
3235                         pinkflag_pos = pos + eX * fs3 * size1;
3236                         break;
3237                 }
3238                 case NUM_TEAM_2: {
3239                         redflag_pos = pos + eX * fs2 * size1;
3240                         blueflag_pos = pos;
3241                         yellowflag_pos = pos - eX * fs2 * size1;
3242                         pinkflag_pos = pos + eX * fs3 * size1;
3243                         break;
3244                 }
3245                 case NUM_TEAM_3: {
3246                         redflag_pos = pos + eX * fs3 * size1;
3247                         blueflag_pos = pos - eX * fs2 * size1;
3248                         yellowflag_pos = pos;
3249                         pinkflag_pos = pos + eX * fs2 * size1;
3250                         break;
3251                 }
3252                 case NUM_TEAM_4: {
3253                         redflag_pos = pos - eX * fs2 * size1;
3254                         blueflag_pos = pos + eX * fs3 * size1;
3255                         yellowflag_pos = pos + eX * fs2 * size1;
3256                         pinkflag_pos = pos;
3257                         break;
3258                 }
3259         }
3260         neutralflag_pos = pos;
3261         flag_size = e1 * fs * size1 + e2 * size2;
3262
3263         #define X(team) do { \
3264                 f = bound(0, team##flag_statuschange_elapsedtime * 2, 1); \
3265                 if (team##_icon_prevstatus && f < 1) \
3266                         drawpic_aspect_skin_expanding(team##flag_pos, team##_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * team##_alpha_prevstatus, DRAWFLAG_NORMAL, f); \
3267                 if (team##_icon) \
3268                         drawpic_aspect_skin(team##flag_pos, team##_icon, flag_size, '1 1 1', panel_fg_alpha * team##_alpha * f, DRAWFLAG_NORMAL); \
3269         } while (0)
3270         X(red);
3271         X(blue);
3272         X(yellow);
3273         X(pink);
3274         X(neutral);
3275         #undef X
3276 }
3277
3278 // Keyhunt HUD modicon section
3279 vector KH_SLOTS[4];
3280
3281 void HUD_Mod_KH(vector pos, vector mySize)
3282 {
3283         mod_active = 1; // keyhunt should never hide the mod icons panel
3284
3285         // Read current state
3286
3287         int state = getstati(STAT_KH_KEYS);
3288         int i, key_state;
3289         int all_keys, team1_keys, team2_keys, team3_keys, team4_keys, dropped_keys, carrying_keys;
3290         all_keys = team1_keys = team2_keys = team3_keys = team4_keys = dropped_keys = carrying_keys = 0;
3291
3292         for(i = 0; i < 4; ++i)
3293         {
3294                 key_state = (bitshift(state, i * -5) & 31) - 1;
3295
3296                 if(key_state == -1)
3297                         continue;
3298
3299                 if(key_state == 30)
3300                 {
3301                         ++carrying_keys;
3302                         key_state = myteam;
3303                 }
3304
3305                 switch(key_state)
3306                 {
3307                         case NUM_TEAM_1: ++team1_keys; break;
3308                         case NUM_TEAM_2: ++team2_keys; break;
3309                         case NUM_TEAM_3: ++team3_keys; break;
3310                         case NUM_TEAM_4: ++team4_keys; break;
3311                         case 29: ++dropped_keys; break;
3312                 }
3313
3314                 ++all_keys;
3315         }
3316
3317         // Calculate slot measurements
3318
3319         vector slot_size;
3320
3321         if(all_keys == 4 && mySize.x * 0.5 < mySize.y && mySize.y * 0.5 < mySize.x)
3322         {
3323                 // Quadratic arrangement
3324                 slot_size = eX * mySize.x * 0.5 + eY * mySize.y * 0.5;
3325                 KH_SLOTS[0] = pos;
3326                 KH_SLOTS[1] = pos + eX * slot_size.x;
3327                 KH_SLOTS[2] = pos + eY * slot_size.y;
3328                 KH_SLOTS[3] = pos + eX * slot_size.x + eY * slot_size.y;
3329         }
3330         else
3331         {
3332                 if(mySize.x > mySize.y)
3333                 {
3334                         // Horizontal arrangement
3335                         slot_size = eX * mySize.x / all_keys + eY * mySize.y;
3336                         for(i = 0; i < all_keys; ++i)
3337                                 KH_SLOTS[i] = pos + eX * slot_size.x * i;
3338                 }
3339                 else
3340                 {
3341                         // Vertical arrangement
3342                         slot_size = eX * mySize.x + eY * mySize.y / all_keys;
3343                         for(i = 0; i < all_keys; ++i)
3344                                 KH_SLOTS[i] = pos + eY * slot_size.y * i;
3345                 }
3346         }
3347
3348         // Make icons blink in case of RUN HERE
3349
3350         float blink = 0.6 + sin(2*M_PI*time) / 2.5; // Oscillate between 0.2 and 1
3351         float alpha;
3352         alpha = 1;
3353
3354         if(carrying_keys)
3355                 switch(myteam)
3356                 {
3357                         case NUM_TEAM_1: if(team1_keys == all_keys) alpha = blink; break;
3358                         case NUM_TEAM_2: if(team2_keys == all_keys) alpha = blink; break;
3359                         case NUM_TEAM_3: if(team3_keys == all_keys) alpha = blink; break;
3360                         case NUM_TEAM_4: if(team4_keys == all_keys) alpha = blink; break;
3361                 }
3362
3363         // Draw icons
3364
3365         i = 0;
3366
3367         while(team1_keys--)
3368                 if(myteam == NUM_TEAM_1 && carrying_keys)
3369                 {
3370                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_red_carrying", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
3371                         --carrying_keys;
3372                 }
3373                 else
3374                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_red_taken", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
3375
3376         while(team2_keys--)
3377                 if(myteam == NUM_TEAM_2 && carrying_keys)
3378                 {
3379                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_blue_carrying", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
3380                         --carrying_keys;
3381                 }
3382                 else
3383                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_blue_taken", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
3384
3385         while(team3_keys--)
3386                 if(myteam == NUM_TEAM_3 && carrying_keys)
3387                 {
3388                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_yellow_carrying", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
3389                         --carrying_keys;
3390                 }
3391                 else
3392                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_yellow_taken", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
3393
3394         while(team4_keys--)
3395                 if(myteam == NUM_TEAM_4 && carrying_keys)
3396                 {
3397                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_pink_carrying", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
3398                         --carrying_keys;
3399                 }
3400                 else
3401                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_pink_taken", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
3402
3403         while(dropped_keys--)
3404                 drawpic_aspect_skin(KH_SLOTS[i++], "kh_dropped", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
3405 }
3406
3407 // Keepaway HUD mod icon
3408 int kaball_prevstatus; // last remembered status
3409 float kaball_statuschange_time; // time when the status changed
3410
3411 // we don't need to reset for keepaway since it immediately
3412 // autocorrects prevstatus as to if the player has the ball or not
3413
3414 void HUD_Mod_Keepaway(vector pos, vector mySize)
3415 {
3416         mod_active = 1; // keepaway should always show the mod HUD
3417
3418         float BLINK_FACTOR = 0.15;
3419         float BLINK_BASE = 0.85;
3420         float BLINK_FREQ = 5;
3421         float kaball_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
3422
3423         int stat_items = getstati(STAT_ITEMS, 0, 24);
3424         int kaball = (stat_items/IT_KEY1) & 1;
3425
3426         if(kaball != kaball_prevstatus)
3427         {
3428                 kaball_statuschange_time = time;
3429                 kaball_prevstatus = kaball;
3430         }
3431
3432         vector kaball_pos, kaball_size;
3433
3434         if(mySize.x > mySize.y) {
3435                 kaball_pos = pos + eX * 0.25 * mySize.x;
3436                 kaball_size = eX * 0.5 * mySize.x + eY * mySize.y;
3437         } else {
3438                 kaball_pos = pos + eY * 0.25 * mySize.y;
3439                 kaball_size = eY * 0.5 * mySize.y + eX * mySize.x;
3440         }
3441
3442         float kaball_statuschange_elapsedtime = time - kaball_statuschange_time;
3443         float f = bound(0, kaball_statuschange_elapsedtime*2, 1);
3444
3445         if(kaball_prevstatus && f < 1)
3446                 drawpic_aspect_skin_expanding(kaball_pos, "keepawayball_carrying", kaball_size, '1 1 1', panel_fg_alpha * kaball_alpha, DRAWFLAG_NORMAL, f);
3447
3448         if(kaball)
3449                 drawpic_aspect_skin(pos, "keepawayball_carrying", eX * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha * kaball_alpha * f, DRAWFLAG_NORMAL);
3450 }
3451
3452
3453 // Nexball HUD mod icon
3454 void HUD_Mod_NexBall(vector pos, vector mySize)
3455 {
3456         float nb_pb_starttime, dt, p;
3457         int stat_items;
3458
3459         stat_items = getstati(STAT_ITEMS, 0, 24);
3460         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
3461
3462         if (stat_items & IT_KEY1)
3463                 mod_active = 1;
3464         else
3465                 mod_active = 0;
3466
3467         //Manage the progress bar if any
3468         if (nb_pb_starttime > 0)
3469         {
3470                 dt = (time - nb_pb_starttime) % nb_pb_period;
3471                 // one period of positive triangle
3472                 p = 2 * dt / nb_pb_period;
3473                 if (p > 1)
3474                         p = 2 - p;
3475
3476                 HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", p, (mySize.x <= mySize.y), 0, autocvar_hud_progressbar_nexball_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
3477         }
3478
3479         if (stat_items & IT_KEY1)
3480                 drawpic_aspect_skin(pos, "nexball_carrying", eX * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3481 }
3482
3483 // Race/CTS HUD mod icons
3484 float crecordtime_prev; // last remembered crecordtime
3485 float crecordtime_change_time; // time when crecordtime last changed
3486 float srecordtime_prev; // last remembered srecordtime
3487 float srecordtime_change_time; // time when srecordtime last changed
3488
3489 float race_status_time;
3490 int race_status_prev;
3491 string race_status_name_prev;
3492 void HUD_Mod_Race(vector pos, vector mySize)
3493 {
3494         mod_active = 1; // race should never hide the mod icons panel
3495         entity me;
3496         me = playerslots[player_localnum];
3497         float t, score;
3498         float f; // yet another function has this
3499         score = me.(scores[ps_primary]);
3500
3501         if(!(scores_flags[ps_primary] & SFL_TIME) || teamplay) // race/cts record display on HUD
3502                 return; // no records in the actual race
3503
3504         // clientside personal record
3505         string rr;
3506         if(gametype == MAPINFO_TYPE_CTS)
3507                 rr = CTS_RECORD;
3508         else
3509                 rr = RACE_RECORD;
3510         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
3511
3512         if(score && (score < t || !t)) {
3513                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
3514                 if(autocvar_cl_autodemo_delete_keeprecords)
3515                 {
3516                         f = autocvar_cl_autodemo_delete;
3517                         f &= ~1;
3518                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
3519                 }
3520         }
3521
3522         if(t != crecordtime_prev) {
3523                 crecordtime_prev = t;
3524                 crecordtime_change_time = time;
3525         }
3526
3527         vector textPos, medalPos;
3528         float squareSize;
3529         if(mySize.x > mySize.y) {
3530                 // text on left side
3531                 squareSize = min(mySize.y, mySize.x/2);
3532                 textPos = pos + eX * 0.5 * max(0, mySize.x/2 - squareSize) + eY * 0.5 * (mySize.y - squareSize);
3533                 medalPos = pos + eX * 0.5 * max(0, mySize.x/2 - squareSize) + eX * 0.5 * mySize.x + eY * 0.5 * (mySize.y - squareSize);
3534         } else {
3535                 // text on top
3536                 squareSize = min(mySize.x, mySize.y/2);
3537                 textPos = pos + eY * 0.5 * max(0, mySize.y/2 - squareSize) + eX * 0.5 * (mySize.x - squareSize);
3538                 medalPos = pos + eY * 0.5 * max(0, mySize.y/2 - squareSize) + eY * 0.5 * mySize.y + eX * 0.5 * (mySize.x - squareSize);
3539         }
3540
3541         f = time - crecordtime_change_time;
3542
3543         if (f > 1) {
3544                 drawstring_aspect(textPos, _("Personal best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3545                 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);
3546         } else {
3547                 drawstring_aspect(textPos, _("Personal best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3548                 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);
3549                 drawstring_aspect_expanding(pos, _("Personal best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
3550                 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);
3551         }
3552
3553         // server record
3554         t = race_server_record;
3555         if(t != srecordtime_prev) {
3556                 srecordtime_prev = t;
3557                 srecordtime_change_time = time;
3558         }
3559         f = time - srecordtime_change_time;
3560
3561         if (f > 1) {
3562                 drawstring_aspect(textPos + eY * 0.5 * squareSize, _("Server best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3563                 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);
3564         } else {
3565                 drawstring_aspect(textPos + eY * 0.5 * squareSize, _("Server best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3566                 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);
3567                 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);
3568                 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);
3569         }
3570
3571         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
3572                 race_status_time = time + 5;
3573                 race_status_prev = race_status;
3574                 if (race_status_name_prev)
3575                         strunzone(race_status_name_prev);
3576                 race_status_name_prev = strzone(race_status_name);
3577         }
3578
3579         // race "awards"
3580         float a;
3581         a = bound(0, race_status_time - time, 1);
3582
3583         string s;
3584         s = textShortenToWidth(race_status_name, squareSize, '1 1 0' * 0.1 * squareSize, stringwidth_colors);
3585
3586         float rank;
3587         if(race_status > 0)
3588                 rank = race_CheckName(race_status_name);
3589         else
3590                 rank = 0;
3591         string rankname;
3592         rankname = count_ordinal(rank);
3593
3594         vector namepos;
3595         namepos = medalPos + '0 0.8 0' * squareSize;
3596         vector rankpos;
3597         rankpos = medalPos + '0 0.15 0' * squareSize;
3598
3599         if(race_status == 0)
3600                 drawpic_aspect_skin(medalPos, "race_newfail", '1 1 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3601         else if(race_status == 1) {
3602                 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);
3603                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3604                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3605         } else if(race_status == 2) {
3606                 if(race_status_name == GetPlayerName(player_localnum) || !race_myrank || race_myrank < rank)
3607                         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);
3608                 else
3609                         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);
3610                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3611                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3612         } else if(race_status == 3) {
3613                 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);
3614                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3615                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3616         }
3617
3618         if (race_status_time - time <= 0) {
3619                 race_status_prev = -1;
3620                 race_status = -1;
3621                 if(race_status_name)
3622                         strunzone(race_status_name);
3623                 race_status_name = string_null;
3624                 if(race_status_name_prev)
3625                         strunzone(race_status_name_prev);
3626                 race_status_name_prev = string_null;
3627         }
3628 }
3629
3630 void DrawDomItem(vector myPos, vector mySize, float aspect_ratio, int layout, int i)
3631 {
3632         float stat = -1;
3633         string pic = "";
3634         vector color = '0 0 0';
3635         switch(i)
3636         {
3637                 case 0:
3638                         stat = getstatf(STAT_DOM_PPS_RED);
3639                         pic = "dom_icon_red";
3640                         color = '1 0 0';
3641                         break;
3642                 case 1:
3643                         stat = getstatf(STAT_DOM_PPS_BLUE);
3644                         pic = "dom_icon_blue";
3645                         color = '0 0 1';
3646                         break;
3647                 case 2:
3648                         stat = getstatf(STAT_DOM_PPS_YELLOW);
3649                         pic = "dom_icon_yellow";
3650                         color = '1 1 0';
3651                         break;
3652                 default:
3653                 case 3:
3654                         stat = getstatf(STAT_DOM_PPS_PINK);
3655                         pic = "dom_icon_pink";
3656                         color = '1 0 1';
3657                         break;
3658         }
3659         float pps_ratio = stat / getstatf(STAT_DOM_TOTAL_PPS);
3660
3661         if(mySize.x/mySize.y > aspect_ratio)
3662         {
3663                 i = aspect_ratio * mySize.y;
3664                 myPos.x = myPos.x + (mySize.x - i) / 2;
3665                 mySize.x = i;
3666         }
3667         else
3668         {
3669                 i = 1/aspect_ratio * mySize.x;
3670                 myPos.y = myPos.y + (mySize.y - i) / 2;
3671                 mySize.y = i;
3672         }
3673
3674         if (layout) // show text too
3675         {
3676                 //draw the text
3677                 color *= 0.5 + pps_ratio * (1 - 0.5); // half saturated color at min, full saturated at max
3678                 if (layout == 2) // average pps
3679                         drawstring_aspect(myPos + eX * mySize.y, ftos_decimals(stat, 2), eX * (2/3) * mySize.x + eY * mySize.y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
3680                 else // percentage of average pps
3681                         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);
3682         }
3683
3684         //draw the icon
3685         drawpic_aspect_skin(myPos, pic, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3686         if (stat > 0)
3687         {
3688                 drawsetcliparea(myPos.x, myPos.y + mySize.y * (1 - pps_ratio), mySize.y, mySize.y * pps_ratio);
3689                 drawpic_aspect_skin(myPos, strcat(pic, "-highlighted"), '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3690                 drawresetcliparea();
3691         }
3692 }
3693
3694 void HUD_Mod_Dom(vector myPos, vector mySize)
3695 {
3696         mod_active = 1; // required in each mod function that always shows something
3697
3698         int layout = autocvar_hud_panel_modicons_dom_layout;
3699         int rows, columns;
3700         float aspect_ratio;
3701         aspect_ratio = (layout) ? 3 : 1;
3702         rows = HUD_GetRowCount(team_count, mySize, aspect_ratio);
3703         columns = ceil(team_count/rows);
3704
3705         int i;
3706         float row = 0, column = 0;
3707         vector pos, itemSize;
3708         itemSize = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
3709         for(i=0; i<team_count; ++i)
3710         {
3711                 pos = myPos + eX * column * itemSize.x + eY * row * itemSize.y;
3712
3713                 DrawDomItem(pos, itemSize, aspect_ratio, layout, i);
3714
3715                 ++row;
3716                 if(row >= rows)
3717                 {
3718                         row = 0;
3719                         ++column;
3720                 }
3721         }
3722 }
3723
3724 void HUD_ModIcons_SetFunc()
3725 {
3726         switch(gametype)
3727         {
3728                 case MAPINFO_TYPE_KEYHUNT:              HUD_ModIcons_GameType = HUD_Mod_KH; break;
3729                 case MAPINFO_TYPE_CTF:                  HUD_ModIcons_GameType = HUD_Mod_CTF; break;
3730                 case MAPINFO_TYPE_NEXBALL:              HUD_ModIcons_GameType = HUD_Mod_NexBall; break;
3731                 case MAPINFO_TYPE_CTS:
3732                 case MAPINFO_TYPE_RACE:         HUD_ModIcons_GameType = HUD_Mod_Race; break;
3733                 case MAPINFO_TYPE_CA:
3734                 case MAPINFO_TYPE_FREEZETAG:    HUD_ModIcons_GameType = HUD_Mod_CA; break;
3735                 case MAPINFO_TYPE_DOMINATION:   HUD_ModIcons_GameType = HUD_Mod_Dom; break;
3736                 case MAPINFO_TYPE_KEEPAWAY:     HUD_ModIcons_GameType = HUD_Mod_Keepaway; break;
3737         }
3738 }
3739
3740 int mod_prev; // previous state of mod_active to check for a change
3741 float mod_alpha;
3742 float mod_change; // "time" when mod_active changed
3743
3744 void HUD_ModIcons(void)
3745 {
3746         if(intermission == 2) return;
3747         if(!autocvar__hud_configure)
3748         {
3749                 if(!autocvar_hud_panel_modicons) return;
3750                 if(!HUD_ModIcons_GameType) return;
3751         }
3752
3753         HUD_Panel_UpdateCvars();
3754
3755         draw_beginBoldFont();
3756
3757         if(mod_active != mod_prev) {
3758                 mod_change = time;
3759                 mod_prev = mod_active;
3760         }
3761
3762         if(mod_active || autocvar__hud_configure)
3763                 mod_alpha = bound(0, (time - mod_change) * 2, 1);
3764         else
3765                 mod_alpha = bound(0, 1 - (time - mod_change) * 2, 1);
3766
3767         if(mod_alpha)
3768                 HUD_Panel_DrawBg(mod_alpha);
3769
3770         if(panel_bg_padding)
3771         {
3772                 panel_pos += '1 1 0' * panel_bg_padding;
3773                 panel_size -= '2 2 0' * panel_bg_padding;
3774         }
3775
3776         if(autocvar__hud_configure)
3777                 HUD_Mod_CTF(panel_pos, panel_size);
3778         else
3779                 HUD_ModIcons_GameType(panel_pos, panel_size);
3780
3781         draw_endBoldFont();
3782 }
3783
3784 // Draw pressed keys (#11)
3785 //
3786 void HUD_PressedKeys(void)
3787 {
3788         if(intermission == 2) return;
3789         if(!autocvar__hud_configure)
3790         {
3791                 if(!autocvar_hud_panel_pressedkeys) return;
3792                 if(spectatee_status <= 0 && autocvar_hud_panel_pressedkeys < 2) return;
3793         }
3794
3795         HUD_Panel_UpdateCvars();
3796         vector pos, mySize;
3797         pos = panel_pos;
3798         mySize = panel_size;
3799
3800         HUD_Panel_DrawBg(1);
3801         if(panel_bg_padding)
3802         {
3803                 pos += '1 1 0' * panel_bg_padding;
3804                 mySize -= '2 2 0' * panel_bg_padding;
3805         }
3806
3807         // force custom aspect
3808         float aspect = autocvar_hud_panel_pressedkeys_aspect;
3809         if(aspect)
3810         {
3811                 vector newSize = '0 0 0';
3812                 if(mySize.x/mySize.y > aspect)
3813                 {
3814                         newSize.x = aspect * mySize.y;
3815                         newSize.y = mySize.y;
3816
3817                         pos.x = pos.x + (mySize.x - newSize.x) / 2;
3818                 }
3819                 else
3820                 {
3821                         newSize.y = 1/aspect * mySize.x;
3822                         newSize.x = mySize.x;
3823
3824                         pos.y = pos.y + (mySize.y - newSize.y) / 2;
3825                 }
3826                 mySize = newSize;
3827         }
3828
3829         vector keysize;
3830         keysize = eX * mySize.x * (1/3.0) + eY * mySize.y * (1/(3.0 - !autocvar_hud_panel_pressedkeys_attack));
3831         float pressedkeys;
3832         pressedkeys = getstatf(STAT_PRESSED_KEYS);
3833
3834         if(autocvar_hud_panel_pressedkeys_attack)
3835         {
3836                 drawpic_aspect_skin(pos + eX * keysize.x * 0.5, ((pressedkeys & KEY_ATCK) ? "key_atck_inv.tga" : "key_atck.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3837                 drawpic_aspect_skin(pos + eX * keysize.x * 1.5, ((pressedkeys & KEY_ATCK2) ? "key_atck_inv.tga" : "key_atck.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3838                 pos.y += keysize.y;
3839         }
3840
3841         drawpic_aspect_skin(pos, ((pressedkeys & KEY_CROUCH) ? "key_crouch_inv.tga" : "key_crouch.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3842         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);
3843         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);
3844         pos.y += keysize.y;
3845         drawpic_aspect_skin(pos, ((pressedkeys & KEY_LEFT) ? "key_left_inv.tga" : "key_left.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3846         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);
3847         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);
3848 }
3849
3850 // Handle chat as a panel (#12)
3851 //
3852 void HUD_Chat(void)
3853 {
3854         if(!autocvar__hud_configure)
3855         {
3856                 if (!autocvar_hud_panel_chat)
3857                 {
3858                         if (!autocvar_con_chatrect)
3859                                 cvar_set("con_chatrect", "0");
3860                         return;
3861                 }
3862                 if(autocvar__con_chat_maximized)
3863                 {
3864                         if(!hud_draw_maximized) return;
3865                 }
3866                 else if(chat_panel_modified)
3867                 {
3868                         panel.update_time = time; // forces reload of panel attributes
3869                         chat_panel_modified = false;
3870                 }
3871         }
3872
3873         HUD_Panel_UpdateCvars();
3874
3875         if(intermission == 2)
3876         {
3877                 // reserve some more space to the mapvote panel
3878                 // by resizing and moving chat panel to the bottom
3879                 panel_size.y = min(panel_size.y, vid_conheight * 0.2);
3880                 panel_pos.y = vid_conheight - panel_size.y - panel_bg_border * 2;
3881                 chat_posy = panel_pos.y;
3882                 chat_sizey = panel_size.y;
3883         }
3884         if(autocvar__con_chat_maximized && !autocvar__hud_configure) // draw at full screen height if maximized
3885         {
3886                 panel_pos.y = panel_bg_border;
3887                 panel_size.y = vid_conheight - panel_bg_border * 2;
3888                 if(panel.current_panel_bg == "0") // force a border when maximized
3889                 {
3890                         string panel_bg;
3891                         panel_bg = strcat(hud_skin_path, "/border_default");
3892                         if(precache_pic(panel_bg) == "")
3893                                 panel_bg = "gfx/hud/default/border_default";
3894                         if(panel.current_panel_bg)
3895                                 strunzone(panel.current_panel_bg);
3896                         panel.current_panel_bg = strzone(panel_bg);
3897                         chat_panel_modified = true;
3898                 }
3899                 panel_bg_alpha = max(0.75, panel_bg_alpha); // force an theAlpha of at least 0.75
3900         }
3901
3902         vector pos, mySize;
3903         pos = panel_pos;
3904         mySize = panel_size;
3905
3906         HUD_Panel_DrawBg(1);
3907
3908         if(panel_bg_padding)
3909         {
3910                 pos += '1 1 0' * panel_bg_padding;
3911                 mySize -= '2 2 0' * panel_bg_padding;
3912         }
3913
3914         if (!autocvar_con_chatrect)
3915                 cvar_set("con_chatrect", "1");
3916
3917         cvar_set("con_chatrect_x", ftos(pos.x/vid_conwidth));
3918         cvar_set("con_chatrect_y", ftos(pos.y/vid_conheight));
3919
3920         cvar_set("con_chatwidth", ftos(mySize.x/vid_conwidth));
3921         cvar_set("con_chat", ftos(floor(mySize.y/autocvar_con_chatsize - 0.5)));
3922
3923         if(autocvar__hud_configure)
3924         {
3925                 vector chatsize;
3926                 chatsize = '1 1 0' * autocvar_con_chatsize;
3927                 cvar_set("con_chatrect_x", "9001"); // over 9000, we'll fake it instead for more control over theAlpha and such
3928                 float i, a;
3929                 for(i = 0; i < autocvar_con_chat; ++i)
3930                 {
3931                         if(i == autocvar_con_chat - 1)
3932                                 a = panel_fg_alpha;
3933                         else
3934                                 a = panel_fg_alpha * floor(((i + 1) * 7 + autocvar_con_chattime)/45);
3935                         drawcolorcodedstring(pos, textShortenToWidth(_("^3Player^7: This is the chat area."), mySize.x, chatsize, stringwidth_colors), chatsize, a, DRAWFLAG_NORMAL);
3936                         pos.y += chatsize.y;
3937                 }
3938         }
3939 }
3940
3941 // Engine info panel (#13)
3942 //
3943 float prevfps;
3944 float prevfps_time;
3945 int framecounter;
3946
3947 float frametimeavg;
3948 float frametimeavg1; // 1 frame ago
3949 float frametimeavg2; // 2 frames ago
3950 void HUD_EngineInfo(void)
3951 {
3952         //if(intermission == 2) return;
3953         if(!autocvar__hud_configure)
3954         {
3955                 if(!autocvar_hud_panel_engineinfo) return;
3956         }
3957
3958         HUD_Panel_UpdateCvars();
3959         vector pos, mySize;
3960         pos = panel_pos;
3961         mySize = panel_size;
3962
3963         HUD_Panel_DrawBg(1);
3964         if(panel_bg_padding)
3965         {
3966                 pos += '1 1 0' * panel_bg_padding;
3967                 mySize -= '2 2 0' * panel_bg_padding;
3968         }
3969
3970         float currentTime = gettime(GETTIME_REALTIME);
3971         if(cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage"))
3972         {
3973                 float currentframetime = currentTime - prevfps_time;
3974                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + currentframetime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
3975                 frametimeavg2 = frametimeavg1;
3976                 frametimeavg1 = frametimeavg;
3977
3978                 float weight;
3979                 weight = cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight");
3980                 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.
3981                 {
3982                         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
3983                                 prevfps = (1/currentframetime);
3984                         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"
3985                 }
3986                 prevfps_time = currentTime;
3987         }
3988         else
3989         {
3990                 framecounter += 1;
3991                 if(currentTime - prevfps_time > autocvar_hud_panel_engineinfo_framecounter_time)
3992                 {
3993                         prevfps = framecounter/(currentTime - prevfps_time);
3994                         framecounter = 0;
3995                         prevfps_time = currentTime;
3996                 }
3997         }
3998
3999         vector color;
4000         color = HUD_Get_Num_Color (prevfps, 100);
4001         drawstring_aspect(pos, sprintf(_("FPS: %.*f"), autocvar_hud_panel_engineinfo_framecounter_decimals, prevfps), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
4002 }
4003
4004 // Info messages panel (#14)
4005 //
4006 #define drawInfoMessage(s) do {                                                                                                                                                                         \
4007         if(autocvar_hud_panel_infomessages_flip)                                                                                                                                                \
4008                 o.x = pos.x + mySize.x - stringwidth(s, true, fontsize);                                                                                                        \
4009         drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);                                                                                                               \
4010         o.y += fontsize.y;                                                                                                                                                                                              \
4011 } while(0)
4012 void HUD_InfoMessages(void)
4013 {
4014         if(intermission == 2) return;
4015         if(!autocvar__hud_configure)
4016         {
4017                 if(!autocvar_hud_panel_infomessages) return;
4018         }
4019
4020         HUD_Panel_UpdateCvars();
4021         vector pos, mySize;
4022         pos = panel_pos;
4023         mySize = panel_size;
4024
4025         HUD_Panel_DrawBg(1);
4026         if(panel_bg_padding)
4027         {
4028                 pos += '1 1 0' * panel_bg_padding;
4029                 mySize -= '2 2 0' * panel_bg_padding;
4030         }
4031
4032         // always force 5:1 aspect
4033         vector newSize = '0 0 0';
4034         if(mySize.x/mySize.y > 5)
4035         {
4036                 newSize.x = 5 * mySize.y;
4037                 newSize.y = mySize.y;
4038
4039                 pos.x = pos.x + (mySize.x - newSize.x) / 2;
4040         }
4041         else
4042         {
4043                 newSize.y = 1/5 * mySize.x;
4044                 newSize.x = mySize.x;
4045
4046                 pos.y = pos.y + (mySize.y - newSize.y) / 2;
4047         }
4048
4049         mySize = newSize;
4050         entity tm;
4051         vector o;
4052         o = pos;
4053
4054         vector fontsize;
4055         fontsize = '0.20 0.20 0' * mySize.y;
4056
4057         float a;
4058         a = panel_fg_alpha;
4059
4060         string s;
4061         if(!autocvar__hud_configure)
4062         {
4063                 if(spectatee_status && !intermission)
4064                 {
4065                         a = 1;
4066                         if(spectatee_status == -1)
4067                                 s = _("^1Observing");
4068                         else
4069                                 s = sprintf(_("^1Spectating: ^7%s"), GetPlayerName(current_player));
4070                         drawInfoMessage(s);
4071
4072                         if(spectatee_status == -1)
4073                                 s = sprintf(_("^1Press ^3%s^1 to spectate"), getcommandkey("primary fire", "+fire"));
4074                         else
4075                                 s = sprintf(_("^1Press ^3%s^1 or ^3%s^1 for next or previous player"), getcommandkey("next weapon", "weapnext"), getcommandkey("previous weapon", "weapprev"));
4076                         drawInfoMessage(s);
4077
4078                         if(spectatee_status == -1)
4079                                 s = sprintf(_("^1Use ^3%s^1 or ^3%s^1 to change the speed"), getcommandkey("next weapon", "weapnext"), getcommandkey("previous weapon", "weapprev"));
4080                         else
4081                                 s = sprintf(_("^1Press ^3%s^1 to observe"), getcommandkey("secondary fire", "+fire2"));
4082                         drawInfoMessage(s);
4083
4084                         s = sprintf(_("^1Press ^3%s^1 for gamemode info"), getcommandkey("server info", "+show_info"));
4085                         drawInfoMessage(s);
4086
4087                         if(gametype == MAPINFO_TYPE_LMS)
4088                         {
4089                                 entity sk;
4090                                 sk = playerslots[player_localnum];
4091                                 if(sk.(scores[ps_primary]) >= 666)
4092                                         s = _("^1Match has already begun");
4093                                 else if(sk.(scores[ps_primary]) > 0)
4094                                         s = _("^1You have no more lives left");
4095                                 else
4096                                         s = sprintf(_("^1Press ^3%s^1 to join"), getcommandkey("jump", "+jump"));
4097                         }
4098                         else
4099                                 s = sprintf(_("^1Press ^3%s^1 to join"), getcommandkey("jump", "+jump"));
4100                         drawInfoMessage(s);
4101
4102                         //show restart countdown:
4103                         if (time < getstatf(STAT_GAMESTARTTIME)) {
4104                                 float countdown;
4105                                 //we need to ceil, otherwise the countdown would be off by .5 when using round()
4106                                 countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
4107                                 s = sprintf(_("^1Game starts in ^3%d^1 seconds"), countdown);
4108                                 drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);
4109                                 o.y += fontsize.y;
4110                         }
4111                 }
4112                 if(warmup_stage && !intermission)
4113                 {
4114                         s = _("^2Currently in ^1warmup^2 stage!");
4115                         drawInfoMessage(s);
4116                 }
4117
4118                 string blinkcolor;
4119                 if(time % 1 >= 0.5)
4120                         blinkcolor = "^1";
4121                 else
4122                         blinkcolor = "^3";
4123
4124                 if(ready_waiting && !intermission && !spectatee_status)
4125                 {
4126                         if(ready_waiting_for_me)
4127                         {
4128                                 if(warmup_stage)
4129                                         s = sprintf(_("%sPress ^3%s%s to end warmup"), blinkcolor, getcommandkey("ready", "ready"), blinkcolor);
4130                                 else
4131                                         s = sprintf(_("%sPress ^3%s%s once you are ready"), blinkcolor, getcommandkey("ready", "ready"), blinkcolor);
4132                         }
4133                         else
4134                         {
4135                                 if(warmup_stage)
4136                                         s = _("^2Waiting for others to ready up to end warmup...");
4137                                 else
4138                                         s = _("^2Waiting for others to ready up...");
4139                         }
4140                         drawInfoMessage(s);
4141                 }
4142                 else if(warmup_stage && !intermission && !spectatee_status)
4143                 {
4144                         s = sprintf(_("^2Press ^3%s^2 to end warmup"), getcommandkey("ready", "ready"));
4145                         drawInfoMessage(s);
4146                 }
4147
4148                 if(teamplay && !intermission && !spectatee_status && gametype != MAPINFO_TYPE_CA && teamnagger)
4149                 {
4150                         float ts_min = 0, ts_max = 0;
4151                         tm = teams.sort_next;
4152                         if (tm)
4153                         {
4154                                 for (; tm.sort_next; tm = tm.sort_next)
4155                                 {
4156                                         if(!tm.team_size || tm.team == NUM_SPECTATOR)
4157                                                 continue;
4158                                         if(!ts_min) ts_min = tm.team_size;
4159                                         else ts_min = min(ts_min, tm.team_size);
4160                                         if(!ts_max) ts_max = tm.team_size;
4161                                         else ts_max = max(ts_max, tm.team_size);
4162                                 }
4163                                 if ((ts_max - ts_min) > 1)
4164                                 {
4165                                         s = strcat(blinkcolor, _("Teamnumbers are unbalanced!"));
4166                                         tm = GetTeam(myteam, false);
4167                                         if (tm)
4168                                         if (tm.team != NUM_SPECTATOR)
4169                                         if (tm.team_size == ts_max)
4170                                                 s = strcat(s, sprintf(_(" Press ^3%s%s to adjust"), getcommandkey("team menu", "menu_showteamselect"), blinkcolor));
4171                                         drawInfoMessage(s);
4172                                 }
4173                         }
4174                 }
4175         }
4176         else
4177         {
4178                 s = _("^7Press ^3ESC ^7to show HUD options.");
4179                 drawInfoMessage(s);
4180                 s = _("^3Doubleclick ^7a panel for panel-specific options.");
4181                 drawInfoMessage(s);
4182                 s = _("^3CTRL ^7to disable collision testing, ^3SHIFT ^7and");
4183                 drawInfoMessage(s);
4184                 s = _("^3ALT ^7+ ^3ARROW KEYS ^7for fine adjustments.");
4185                 drawInfoMessage(s);
4186         }
4187 }
4188
4189 // Physics panel (#15)
4190 //
4191 vector acc_prevspeed;
4192 float acc_prevtime, acc_avg, top_speed, top_speed_time;
4193 float physics_update_time, discrete_speed, discrete_acceleration;
4194 void HUD_Physics(void)
4195 {
4196         if(intermission == 2) return;
4197         if(!autocvar__hud_configure)
4198         {
4199                 if(!autocvar_hud_panel_physics) return;
4200                 if(spectatee_status == -1 && (autocvar_hud_panel_physics == 1 || autocvar_hud_panel_physics == 3)) return;
4201                 if(autocvar_hud_panel_physics == 3 && !(gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS)) return;
4202         }
4203
4204         HUD_Panel_UpdateCvars();
4205
4206         draw_beginBoldFont();
4207
4208         HUD_Panel_DrawBg(1);
4209         if(panel_bg_padding)
4210         {
4211                 panel_pos += '1 1 0' * panel_bg_padding;
4212                 panel_size -= '2 2 0' * panel_bg_padding;
4213         }
4214
4215         float acceleration_progressbar_scale = 0;
4216         if(autocvar_hud_panel_physics_progressbar && autocvar_hud_panel_physics_acceleration_progressbar_scale > 1)
4217                 acceleration_progressbar_scale = autocvar_hud_panel_physics_acceleration_progressbar_scale;
4218
4219         float text_scale;
4220         if (autocvar_hud_panel_physics_text_scale <= 0)
4221                 text_scale = 1;
4222         else
4223                 text_scale = min(autocvar_hud_panel_physics_text_scale, 1);
4224
4225         //compute speed
4226         float speed, conversion_factor;
4227         string unit;
4228
4229         switch(autocvar_hud_panel_physics_speed_unit)
4230         {
4231                 default:
4232                 case 1:
4233                         unit = _(" qu/s");
4234                         conversion_factor = 1.0;
4235                         break;
4236                 case 2:
4237                         unit = _(" m/s");
4238                         conversion_factor = 0.0254;
4239                         break;
4240                 case 3:
4241                         unit = _(" km/h");
4242                         conversion_factor = 0.0254 * 3.6;
4243                         break;
4244                 case 4:
4245                         unit = _(" mph");
4246                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
4247                         break;
4248                 case 5:
4249                         unit = _(" knots");
4250                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
4251                         break;
4252         }
4253
4254         vector vel = (csqcplayer ? csqcplayer.velocity : pmove_vel);
4255
4256         float max_speed = floor( autocvar_hud_panel_physics_speed_max * conversion_factor + 0.5 );
4257         if (autocvar__hud_configure)
4258                 speed = floor( max_speed * 0.65 + 0.5 );
4259         else if(autocvar_hud_panel_physics_speed_vertical)
4260                 speed = floor( vlen(vel) * conversion_factor + 0.5 );
4261         else
4262                 speed = floor( vlen(vel - vel.z * '0 0 1') * conversion_factor + 0.5 );
4263
4264         //compute acceleration
4265         float acceleration, f;
4266         if (autocvar__hud_configure)
4267                 acceleration = autocvar_hud_panel_physics_acceleration_max * 0.3;
4268         else
4269         {
4270                 // 1 m/s = 0.0254 qu/s; 1 g = 9.80665 m/s^2
4271                 f = time - acc_prevtime;
4272                 if(autocvar_hud_panel_physics_acceleration_vertical)
4273                         acceleration = (vlen(vel) - vlen(acc_prevspeed));
4274                 else
4275                         acceleration = (vlen(vel - '0 0 1' * vel.z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed.z));
4276
4277                 acceleration = acceleration * (1 / max(0.0001, f)) * (0.0254 / 9.80665);
4278
4279                 acc_prevspeed = vel;
4280                 acc_prevtime = time;
4281
4282                 if(autocvar_hud_panel_physics_acceleration_movingaverage)
4283                 {
4284                         f = bound(0, f * 10, 1);
4285                         acc_avg = acc_avg * (1 - f) + acceleration * f;
4286                         acceleration = acc_avg;
4287                 }
4288         }
4289
4290         int acc_decimals = 2;
4291         if(time > physics_update_time)
4292         {
4293                 // workaround for ftos_decimals returning a negative 0
4294                 if(discrete_acceleration > -1 / pow(10, acc_decimals) && discrete_acceleration < 0)
4295                         discrete_acceleration = 0;
4296                 discrete_acceleration = acceleration;
4297                 discrete_speed = speed;
4298                 physics_update_time += autocvar_hud_panel_physics_update_interval;
4299         }
4300
4301         //compute layout
4302         float panel_ar = panel_size.x/panel_size.y;
4303         vector speed_offset = '0 0 0', acceleration_offset = '0 0 0';
4304         if (panel_ar >= 5 && !acceleration_progressbar_scale)
4305         {
4306                 panel_size.x *= 0.5;
4307                 if (autocvar_hud_panel_physics_flip)
4308                         speed_offset.x = panel_size.x;
4309                 else
4310                         acceleration_offset.x = panel_size.x;
4311         }
4312         else
4313         {
4314                 panel_size.y *= 0.5;
4315                 if (autocvar_hud_panel_physics_flip)
4316                         speed_offset.y = panel_size.y;
4317                 else
4318                         acceleration_offset.y = panel_size.y;
4319         }
4320         int speed_baralign, acceleration_baralign;
4321         if (autocvar_hud_panel_physics_baralign == 1)
4322                 acceleration_baralign = speed_baralign = 1;
4323     else if(autocvar_hud_panel_physics_baralign == 4)
4324                 acceleration_baralign = speed_baralign = 2;
4325         else if (autocvar_hud_panel_physics_flip)
4326         {
4327                 acceleration_baralign = (autocvar_hud_panel_physics_baralign == 2);
4328                 speed_baralign = (autocvar_hud_panel_physics_baralign == 3);
4329         }
4330         else
4331         {
4332                 speed_baralign = (autocvar_hud_panel_physics_baralign == 2);
4333                 acceleration_baralign = (autocvar_hud_panel_physics_baralign == 3);
4334         }
4335         if (autocvar_hud_panel_physics_acceleration_progressbar_mode == 0)
4336                 acceleration_baralign = 3; //override hud_panel_physics_baralign value for acceleration
4337
4338         //draw speed
4339         if(speed)
4340         if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 2)
4341                 HUD_Panel_DrawProgressBar(panel_pos + speed_offset, panel_size, "progressbar", speed/max_speed, 0, speed_baralign, autocvar_hud_progressbar_speed_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4342         vector tmp_offset = '0 0 0', tmp_size = '0 0 0';
4343         if (autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 2)
4344         {
4345                 tmp_size.x = panel_size.x * 0.75;
4346                 tmp_size.y = panel_size.y * text_scale;
4347                 if (speed_baralign)
4348                         tmp_offset.x = panel_size.x - tmp_size.x;
4349                 //else
4350                         //tmp_offset_x = 0;
4351                 tmp_offset.y = (panel_size.y - tmp_size.y) / 2;
4352                 drawstring_aspect(panel_pos + speed_offset + tmp_offset, ftos(discrete_speed), tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4353
4354                 //draw speed unit
4355                 if (speed_baralign)
4356                         tmp_offset.x = 0;
4357                 else
4358                         tmp_offset.x = tmp_size.x;
4359                 if (autocvar_hud_panel_physics_speed_unit_show)
4360                 {
4361                         //tmp_offset_y = 0;
4362                         tmp_size.x = panel_size.x * (1 - 0.75);
4363                         tmp_size.y = panel_size.y * 0.4 * text_scale;
4364                         tmp_offset.y = (panel_size.y * 0.4 - tmp_size.y) / 2;
4365                         drawstring_aspect(panel_pos + speed_offset + tmp_offset, unit, tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4366                 }
4367         }
4368
4369         //compute and draw top speed
4370         if (autocvar_hud_panel_physics_topspeed)
4371         if (autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 2)
4372         {
4373                 if (autocvar__hud_configure)
4374                 {
4375                         top_speed = floor( max_speed * 0.75 + 0.5 );
4376                         f = 1;
4377                 }
4378                 else
4379                 {
4380                         if (speed >= top_speed)
4381                         {
4382                                 top_speed = speed;
4383                                 top_speed_time = time;
4384                         }
4385                         if (top_speed != 0)
4386                         {
4387                                 f = max(1, autocvar_hud_panel_physics_topspeed_time);
4388                                 // divide by f to make it start from 1
4389                                 f = cos( ((time - top_speed_time) / f) * PI/2 );
4390                         }
4391             else //hide top speed 0, it would be stupid
4392                                 f = 0;
4393                 }
4394                 if (f > 0)
4395                 {
4396                         //top speed progressbar peak
4397                         if(speed < top_speed)
4398                         if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 2)
4399                         {
4400                                 float peak_offsetX;
4401                                 vector peak_size = '0 0 0';
4402                                 if (speed_baralign == 0)
4403                                         peak_offsetX = min(top_speed, max_speed)/max_speed * panel_size.x;
4404                 else if (speed_baralign == 1)
4405                                         peak_offsetX = (1 - min(top_speed, max_speed)/max_speed) * panel_size.x;
4406                 else // if (speed_baralign == 2)
4407                     peak_offsetX = min(top_speed, max_speed)/max_speed * panel_size.x * 0.5;
4408                                 peak_size.x = floor(panel_size.x * 0.01 + 1.5);
4409                 peak_size.y = panel_size.y;
4410                 if (speed_baralign == 2) // draw two peaks, on both sides
4411                 {
4412                     drawfill(panel_pos + speed_offset + eX * (0.5 * panel_size.x + peak_offsetX - peak_size.x), peak_size, autocvar_hud_progressbar_speed_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4413                     drawfill(panel_pos + speed_offset + eX * (0.5 * panel_size.x - peak_offsetX + peak_size.x), peak_size, autocvar_hud_progressbar_speed_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4414                 }
4415                 else
4416                     drawfill(panel_pos + speed_offset + eX * (peak_offsetX - peak_size.x), peak_size, autocvar_hud_progressbar_speed_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4417                         }
4418
4419                         //top speed
4420                         tmp_offset.y = panel_size.y * 0.4;
4421                         tmp_size.x = panel_size.x * (1 - 0.75);
4422                         tmp_size.y = (panel_size.y - tmp_offset.y) * text_scale;
4423                         tmp_offset.y += (panel_size.y - tmp_offset.y - tmp_size.y) / 2;
4424                         drawstring_aspect(panel_pos + speed_offset + tmp_offset, ftos(top_speed), tmp_size, '1 0 0', f * panel_fg_alpha, DRAWFLAG_NORMAL);
4425                 }
4426                 else
4427                         top_speed = 0;
4428         }
4429
4430         //draw acceleration
4431         if(acceleration)
4432         if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 3)
4433         {
4434                 vector progressbar_color;
4435                 if(acceleration < 0)
4436                         progressbar_color = autocvar_hud_progressbar_acceleration_neg_color;
4437                 else
4438                         progressbar_color = autocvar_hud_progressbar_acceleration_color;
4439
4440                 f = acceleration/autocvar_hud_panel_physics_acceleration_max;
4441                 if (autocvar_hud_panel_physics_acceleration_progressbar_nonlinear)
4442                         f = (f >= 0 ? sqrt(f) : -sqrt(-f));
4443
4444                 if (acceleration_progressbar_scale) // allow progressbar to go out of panel bounds
4445                 {
4446                         tmp_size = acceleration_progressbar_scale * panel_size.x * eX + panel_size.y * eY;
4447
4448                         if (acceleration_baralign == 1)
4449                                 tmp_offset.x = panel_size.x - tmp_size.x;
4450                         else if (acceleration_baralign == 2 || acceleration_baralign == 3)
4451                                 tmp_offset.x = (panel_size.x - tmp_size.x) / 2;
4452                         else
4453                                 tmp_offset.x = 0;
4454                         tmp_offset.y = 0;
4455                 }
4456                 else
4457                 {
4458                         tmp_size = panel_size;
4459                         tmp_offset = '0 0 0';
4460                 }
4461
4462                 HUD_Panel_DrawProgressBar(panel_pos + acceleration_offset + tmp_offset, tmp_size, "accelbar", f, 0, acceleration_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4463         }
4464
4465         if(autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 3)
4466         {
4467                 tmp_size.x = panel_size.x;
4468                 tmp_size.y = panel_size.y * text_scale;
4469                 tmp_offset.x = 0;
4470                 tmp_offset.y = (panel_size.y - tmp_size.y) / 2;
4471
4472                 drawstring_aspect(panel_pos + acceleration_offset + tmp_offset, strcat(ftos_decimals(discrete_acceleration, acc_decimals), "g"), tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4473         }
4474
4475         draw_endBoldFont();
4476 }
4477
4478 // CenterPrint (#16)
4479 //
4480 const int CENTERPRINT_MAX_MSGS = 10;
4481 const int CENTERPRINT_MAX_ENTRIES = 50;
4482 const float CENTERPRINT_SPACING = 0.7;
4483 int cpm_index;
4484 string centerprint_messages[CENTERPRINT_MAX_MSGS];
4485 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
4486 float centerprint_time[CENTERPRINT_MAX_MSGS];
4487 float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
4488 int centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
4489 bool centerprint_showing;
4490
4491 void centerprint_generic(int new_id, string strMessage, float duration, int countdown_num)
4492 {
4493         //printf("centerprint_generic(%d, '%s^7', %d, %d);\n", new_id, strMessage, duration, countdown_num);
4494         int i, j;
4495
4496         if(strMessage == "" && new_id == 0)
4497                 return;
4498
4499         // strip trailing newlines
4500         j = strlen(strMessage) - 1;
4501         while(substring(strMessage, j, 1) == "\n" && j >= 0)
4502                 --j;
4503         if (j < strlen(strMessage) - 1)
4504                 strMessage = substring(strMessage, 0, j + 1);
4505
4506         if(strMessage == "" && new_id == 0)
4507                 return;
4508
4509         // strip leading newlines
4510         j = 0;
4511         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
4512                 ++j;
4513         if (j > 0)
4514                 strMessage = substring(strMessage, j, strlen(strMessage) - j);
4515
4516         if(strMessage == "" && new_id == 0)
4517                 return;
4518
4519         if (!centerprint_showing)
4520                 centerprint_showing = true;
4521
4522         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
4523         {
4524                 if (j == CENTERPRINT_MAX_MSGS)
4525                         j = 0;
4526                 if (new_id && new_id == centerprint_msgID[j])
4527                 {
4528                         if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
4529                         {
4530                                 // fade out the current msg (duration and countdown_num are ignored)
4531                                 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
4532                                 if (centerprint_expire_time[j] > time + min(5, autocvar_hud_panel_centerprint_fade_out) || centerprint_expire_time[j] < time)
4533                                         centerprint_expire_time[j] = time + min(5, autocvar_hud_panel_centerprint_fade_out);
4534                                 return;
4535                         }
4536                         break; // found a msg with the same id, at position j
4537                 }
4538         }
4539
4540         if (i == CENTERPRINT_MAX_MSGS)
4541         {
4542                 // a msg with the same id was not found, add the msg at the next position
4543                 --cpm_index;
4544                 if (cpm_index == -1)
4545                         cpm_index = CENTERPRINT_MAX_MSGS - 1;
4546                 j = cpm_index;
4547         }
4548         if(centerprint_messages[j])
4549                 strunzone(centerprint_messages[j]);
4550         centerprint_messages[j] = strzone(strMessage);
4551         centerprint_msgID[j] = new_id;
4552         if (duration < 0)
4553         {
4554                 centerprint_time[j] = -1;
4555                 centerprint_expire_time[j] = time;
4556         }
4557         else
4558         {
4559                 if(duration == 0)
4560                         duration = max(1, autocvar_hud_panel_centerprint_time);
4561                 centerprint_time[j] = duration;
4562                 centerprint_expire_time[j] = time + duration;
4563         }
4564         centerprint_countdown_num[j] = countdown_num;
4565 }
4566
4567 void centerprint_hud(string strMessage)
4568 {
4569         centerprint_generic(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
4570 }
4571
4572 void reset_centerprint_messages(void)
4573 {
4574         int i;
4575         for (i=0; i<CENTERPRINT_MAX_MSGS; ++i)
4576         {
4577                 centerprint_expire_time[i] = 0;
4578                 centerprint_time[i] = 1;
4579                 centerprint_msgID[i] = 0;
4580                 if(centerprint_messages[i])
4581                         strunzone(centerprint_messages[i]);
4582                 centerprint_messages[i] = string_null;
4583         }
4584 }
4585 float hud_configure_cp_generation_time;
4586 void HUD_CenterPrint (void)
4587 {
4588         if(intermission == 2) return;
4589         if(!autocvar__hud_configure)
4590         {
4591                 if(!autocvar_hud_panel_centerprint) return;
4592
4593                 if(hud_configure_prev)
4594                         reset_centerprint_messages();
4595         }
4596         else
4597         {
4598                 if(!hud_configure_prev)
4599                         reset_centerprint_messages();
4600                 if (time > hud_configure_cp_generation_time)
4601                 {
4602                         if(HUD_PANEL(CENTERPRINT) == highlightedPanel)
4603                         {
4604                                 float r;
4605                                 r = random();
4606                                 if (r > 0.8)
4607                                         centerprint_generic(floor(r*1000), strcat(sprintf("^3Countdown message at time %s", seconds_tostring(time)), ", seconds left: ^COUNT"), 1, 10);
4608                                 else if (r > 0.55)
4609                                         centerprint_generic(0, sprintf("^1Multiline message at time %s that\n^1lasts longer than normal", seconds_tostring(time)), 20, 0);
4610                                 else
4611                                         centerprint_hud(sprintf("Message at time %s", seconds_tostring(time)));
4612                                 hud_configure_cp_generation_time = time + 1 + random()*4;
4613                         }
4614                         else
4615                         {
4616                                 centerprint_generic(0, sprintf("Centerprint message", seconds_tostring(time)), 10, 0);
4617                                 hud_configure_cp_generation_time = time + 10 - random()*3;
4618                         }
4619                 }
4620         }
4621
4622         // this panel fades only when the menu does
4623         float hud_fade_alpha_save = 0;
4624         if(scoreboard_fade_alpha)
4625         {
4626                 hud_fade_alpha_save = hud_fade_alpha;
4627                 hud_fade_alpha = 1 - autocvar__menu_alpha;
4628         }
4629         HUD_Panel_UpdateCvars();
4630
4631         if ( HUD_Radar_Clickable() )
4632         {
4633                 if (hud_panel_radar_bottom >= 0.96 * vid_conheight)
4634                         return;
4635
4636                 panel_pos = eY * hud_panel_radar_bottom + eX * 0.5 * (vid_conwidth - panel_size_x);
4637                 panel_size_y = min(panel_size_y, vid_conheight - hud_panel_radar_bottom);
4638         }
4639         else if(scoreboard_fade_alpha)
4640         {
4641                 hud_fade_alpha = hud_fade_alpha_save;
4642
4643                 // move the panel below the scoreboard
4644                 if (scoreboard_bottom >= 0.96 * vid_conheight)
4645                         return;
4646                 vector target_pos;
4647
4648                 target_pos = eY * scoreboard_bottom + eX * 0.5 * (vid_conwidth - panel_size.x);
4649
4650                 if(target_pos.y > panel_pos.y)
4651                 {
4652                         panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
4653                         panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
4654                 }
4655         }
4656
4657         HUD_Panel_DrawBg(1);
4658
4659         if (!centerprint_showing)
4660                 return;
4661
4662         if(panel_bg_padding)
4663         {
4664                 panel_pos += '1 1 0' * panel_bg_padding;
4665                 panel_size -= '2 2 0' * panel_bg_padding;
4666         }
4667
4668         int entries;
4669         float height;
4670         vector fontsize;
4671         // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
4672         // height = panel_size_y/entries;
4673         // fontsize = '1 1 0' * height;
4674         height = vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale;
4675         fontsize = '1 1 0' * height;
4676         entries = bound(1, floor(panel_size.y/height), CENTERPRINT_MAX_ENTRIES);
4677
4678         int i, j, k, n, g;
4679         float a, sz, align, current_msg_posY = 0, msg_size;
4680         vector pos;
4681         string ts;
4682         bool all_messages_expired = true;
4683
4684         pos = panel_pos;
4685         if (autocvar_hud_panel_centerprint_flip)
4686                 pos.y += panel_size.y;
4687         align = bound(0, autocvar_hud_panel_centerprint_align, 1);
4688         for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
4689         {
4690                 if (j == CENTERPRINT_MAX_MSGS)
4691                         j = 0;
4692                 if (centerprint_expire_time[j] <= time)
4693                 {
4694                         if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
4695                         {
4696                                 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
4697                                 if (centerprint_countdown_num[j] == 0)
4698                                         continue;
4699                                 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
4700                         }
4701                         else if(centerprint_time[j] != -1)
4702                                 continue;
4703                 }
4704
4705                 all_messages_expired = false;
4706
4707                 // fade the centerprint_hud in/out
4708                 if(centerprint_time[j] < 0)  // Expired but forced. Expire time is the fade-in time.
4709                         a = (time - centerprint_expire_time[j]) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
4710                 else if(centerprint_expire_time[j] - autocvar_hud_panel_centerprint_fade_out > time)  // Regularily printed. Not fading out yet.
4711                         a = (time - (centerprint_expire_time[j] - centerprint_time[j])) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
4712                 else // Expiring soon, so fade it out.
4713                         a = (centerprint_expire_time[j] - time) / max(0.0001, autocvar_hud_panel_centerprint_fade_out);
4714
4715                 // while counting down show it anyway in order to hold the current message position
4716                 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0)  // Guaranteed invisible - don't show.
4717                         continue;
4718                 if (a > 1)
4719                         a = 1;
4720
4721                 // set the size from fading in/out before subsequent fading
4722                 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
4723
4724                 // also fade it based on positioning
4725                 if(autocvar_hud_panel_centerprint_fade_subsequent)
4726                 {
4727                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1); // pass one: all messages after the first have half theAlpha
4728                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1); // pass two: after that, gradually lower theAlpha even more for each message
4729                 }
4730                 a *= panel_fg_alpha;
4731
4732                 // finally set the size based on the new theAlpha from subsequent fading
4733                 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
4734                 drawfontscale = sz * '1 1 0';
4735
4736                 if (centerprint_countdown_num[j])
4737                         n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
4738                 else
4739                         n = tokenizebyseparator(centerprint_messages[j], "\n");
4740
4741                 if (autocvar_hud_panel_centerprint_flip)
4742                 {
4743                         // check if the message can be entirely shown
4744                         for(k = 0; k < n; ++k)
4745                         {
4746                                 getWrappedLine_remaining = argv(k);
4747                                 while(getWrappedLine_remaining)
4748                                 {
4749                                         ts = getWrappedLine(panel_size.x * sz, fontsize, stringwidth_colors);
4750                                         if (ts != "")
4751                                                 pos.y -= fontsize.y;
4752                                         else
4753                                                 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
4754                                 }
4755                         }
4756                         current_msg_posY = pos.y; // save starting pos (first line) of the current message
4757                 }
4758
4759                 msg_size = pos.y;
4760                 for(k = 0; k < n; ++k)
4761                 {
4762                         getWrappedLine_remaining = argv(k);
4763                         while(getWrappedLine_remaining)
4764                         {
4765                                 ts = getWrappedLine(panel_size.x * sz, fontsize, stringwidth_colors);
4766                                 if (ts != "")
4767                                 {
4768                                         if (align)
4769                                                 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize)) * align;
4770                                         if (a > 0.5/255.0)  // Otherwise guaranteed invisible - don't show. This is checked a second time after some multiplications with other factors were done so temporary changes of these cannot cause flicker.
4771                                                 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
4772                                         pos.y += fontsize.y;
4773                                 }
4774                                 else
4775                                         pos.y += fontsize.y * CENTERPRINT_SPACING/2;
4776                         }
4777                 }
4778
4779                 ++g; // move next position number up
4780
4781                 msg_size = pos.y - msg_size;
4782                 if (autocvar_hud_panel_centerprint_flip)
4783                 {
4784                         pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
4785                         if (a < 1 && centerprint_msgID[j] == 0) // messages with id can be replaced just after they are faded out, so never move over them the next messages
4786                                 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
4787
4788                         if (pos.y < panel_pos.y) // check if the next message can be shown
4789                         {
4790                                 drawfontscale = '1 1 0';
4791                                 return;
4792                         }
4793                 }
4794                 else
4795                 {
4796                         pos.y += CENTERPRINT_SPACING * fontsize.y;
4797                         if (a < 1 && centerprint_msgID[j] == 0) // messages with id can be replaced just after they are faded out, so never move over them the next messages
4798                                 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
4799
4800                         if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
4801                         {
4802                                 drawfontscale = '1 1 0';
4803                                 return;
4804                         }
4805                 }
4806         }
4807         drawfontscale = '1 1 0';
4808         if (all_messages_expired)
4809         {
4810                 centerprint_showing = false;
4811                 reset_centerprint_messages();
4812         }
4813 }
4814
4815
4816 // Minigame
4817 //
4818 #include "../common/minigames/cl_minigames_hud.qc"
4819
4820 /*
4821 ==================
4822 Main HUD system
4823 ==================
4824 */
4825
4826 bool HUD_Panel_CheckFlags(int showflags)
4827 {
4828         if ( HUD_Minigame_Showpanels() )
4829                 return showflags & PANEL_SHOW_MINIGAME;
4830         return showflags & PANEL_SHOW_MAINGAME;
4831 }
4832
4833 void HUD_Panel_Draw(entity panent)
4834 {
4835         panel = panent;
4836         if ( HUD_Panel_CheckFlags(panel.panel_showflags) )
4837                 panel.panel_draw();
4838 }
4839
4840 void HUD_Reset (void)
4841 {
4842         // reset gametype specific icons
4843         if(gametype == MAPINFO_TYPE_CTF)
4844                 HUD_Mod_CTF_Reset();
4845 }
4846
4847 void HUD_Main (void)
4848 {
4849         int i;
4850         // global hud theAlpha fade
4851         if(menu_enabled == 1)
4852                 hud_fade_alpha = 1;
4853         else
4854                 hud_fade_alpha = (1 - autocvar__menu_alpha);
4855
4856         if(scoreboard_fade_alpha)
4857                 hud_fade_alpha = (1 - scoreboard_fade_alpha);
4858
4859         HUD_Configure_Frame();
4860
4861         // panels that we want to be active together with the scoreboard
4862         // they must fade only when the menu does
4863         if(scoreboard_fade_alpha == 1)
4864         {
4865                 HUD_Panel_Draw(HUD_PANEL(CENTERPRINT));
4866                 return;
4867         }
4868
4869         if(!autocvar__hud_configure && !hud_fade_alpha)
4870         {
4871                 hud_fade_alpha = 1;
4872                 HUD_Panel_Draw(HUD_PANEL(VOTE));
4873                 hud_fade_alpha = 0;
4874                 return;
4875         }
4876
4877         // Drawing stuff
4878         if (hud_skin_prev != autocvar_hud_skin)
4879         {
4880                 if (hud_skin_path)
4881                         strunzone(hud_skin_path);
4882                 hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
4883                 if (hud_skin_prev)
4884                         strunzone(hud_skin_prev);
4885                 hud_skin_prev = strzone(autocvar_hud_skin);
4886         }
4887
4888         // draw the dock
4889         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
4890         {
4891                 int f;
4892                 vector color;
4893                 float hud_dock_color_team = autocvar_hud_dock_color_team;
4894                 if((teamplay) && hud_dock_color_team) {
4895                         if(autocvar__hud_configure && myteam == NUM_SPECTATOR)
4896                                 color = '1 0 0' * hud_dock_color_team;
4897                         else
4898                                 color = myteamcolors * hud_dock_color_team;
4899                 }
4900                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
4901                         color = '1 0 0' * hud_dock_color_team;
4902                 }
4903                 else
4904                 {
4905                         string hud_dock_color = autocvar_hud_dock_color;
4906                         if(hud_dock_color == "shirt") {
4907                                 f = stof(getplayerkeyvalue(current_player, "colors"));
4908                                 color = colormapPaletteColor(floor(f / 16), 0);
4909                         }
4910                         else if(hud_dock_color == "pants") {
4911                                 f = stof(getplayerkeyvalue(current_player, "colors"));
4912                                 color = colormapPaletteColor(f % 16, 1);
4913                         }
4914                         else
4915                                 color = stov(hud_dock_color);
4916                 }
4917
4918                 string pic;
4919                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
4920                 if(precache_pic(pic) == "") {
4921                         pic = strcat(hud_skin_path, "/dock_medium");
4922                         if(precache_pic(pic) == "") {
4923                                 pic = "gfx/hud/default/dock_medium";
4924                         }
4925                 }
4926                 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...
4927         }
4928
4929         // cache the panel order into the panel_order array
4930         if(autocvar__hud_panelorder != hud_panelorder_prev) {
4931                 for(i = 0; i < HUD_PANEL_NUM; ++i)
4932                         panel_order[i] = -1;
4933                 string s = "";
4934                 int p_num;
4935                 bool warning = false;
4936                 int argc = tokenize_console(autocvar__hud_panelorder);
4937                 if (argc > HUD_PANEL_NUM)
4938                         warning = true;
4939                 //first detect wrong/missing panel numbers
4940                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
4941                         p_num = stoi(argv(i));
4942                         if (p_num >= 0 && p_num < HUD_PANEL_NUM) { //correct panel number?
4943                                 if (panel_order[p_num] == -1) //found for the first time?
4944                                         s = strcat(s, ftos(p_num), " ");
4945                                 panel_order[p_num] = 1; //mark as found
4946                         }
4947                         else
4948                                 warning = true;
4949                 }
4950                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
4951                         if (panel_order[i] == -1) {
4952                                 warning = true;
4953                                 s = strcat(s, ftos(i), " "); //add missing panel number
4954                         }
4955                 }
4956                 if (warning)
4957                         dprint("Automatically fixed wrong/missing panel numbers in _hud_panelorder\n");
4958
4959                 cvar_set("_hud_panelorder", s);
4960                 if(hud_panelorder_prev)
4961                         strunzone(hud_panelorder_prev);
4962                 hud_panelorder_prev = strzone(s);
4963
4964                 //now properly set panel_order
4965                 tokenize_console(s);
4966                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
4967                         panel_order[i] = stof(argv(i));
4968                 }
4969         }
4970
4971         hud_draw_maximized = 0;
4972         // draw panels in the order specified by panel_order array
4973         for(i = HUD_PANEL_NUM - 1; i >= 0; --i)
4974                 HUD_Panel_Draw(hud_panel[panel_order[i]]);
4975
4976         hud_draw_maximized = 1; // panels that may be maximized must check this var
4977         // draw maximized panels on top
4978         if(hud_panel_radar_maximized)
4979                 HUD_Panel_Draw(HUD_PANEL(RADAR));
4980         if(autocvar__con_chat_maximized)
4981                 HUD_Panel_Draw(HUD_PANEL(CHAT));
4982
4983         HUD_Configure_PostDraw();
4984
4985         hud_configure_prev = autocvar__hud_configure;
4986 }