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