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