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