Merge remote-tracking branch 'origin/develop'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qc
1 #include "scoreboard.qh"
2 #include "teamradar.qh"
3 #include "../common/buffs.qh"
4 #include "../common/counting.qh"
5 #include "../common/mapinfo.qh"
6 #include "../common/nades.qh"
7 #include "../server/t_items.qh"
8
9 /*
10 ==================
11 Misc HUD functions
12 ==================
13 */
14
15 // a border picture is a texture containing nine parts:
16 //   1/4 width: left part
17 //   1/2 width: middle part (stretched)
18 //   1/4 width: right part
19 // divided into
20 //   1/4 height: top part
21 //   1/2 height: middle part (stretched)
22 //   1/4 height: bottom part
23 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
24 {
25     if (theBorderSize.x < 0 && theBorderSize.y < 0) // draw whole image as it is
26     {
27                 drawpic(theOrigin, pic, theSize, theColor, theAlpha, 0);
28                 return;
29     }
30         if (theBorderSize.x == 0 && theBorderSize.y == 0) // no border
31         {
32                 // draw only the central part
33                 drawsubpic(theOrigin, theSize, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
34                 return;
35         }
36
37         vector dX, dY;
38         vector width, height;
39         vector bW, bH;
40         //pic = draw_UseSkinFor(pic);
41         width = eX * theSize.x;
42         height = eY * theSize.y;
43         if(theSize.x <= theBorderSize.x * 2)
44         {
45                 // not wide enough... draw just left and right then
46                 bW = eX * (0.25 * theSize.x / (theBorderSize.x * 2));
47                 if(theSize.y <= theBorderSize.y * 2)
48                 {
49                         // not high enough... draw just corners
50                         bH = eY * (0.25 * theSize.y / (theBorderSize.y * 2));
51                         drawsubpic(theOrigin,                 width * 0.5 + height * 0.5, pic, '0 0 0',           bW + bH, theColor, theAlpha, 0);
52                         drawsubpic(theOrigin + width   * 0.5, width * 0.5 + height * 0.5, pic, eX - bW,           bW + bH, theColor, theAlpha, 0);
53                         drawsubpic(theOrigin + height  * 0.5, width * 0.5 + height * 0.5, pic, eY - bH,           bW + bH, theColor, theAlpha, 0);
54                         drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
55                 }
56                 else
57                 {
58                         dY = theBorderSize.x * eY;
59                         drawsubpic(theOrigin,                             width * 0.5          +     dY, pic, '0 0    0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
60                         drawsubpic(theOrigin + width * 0.5,               width * 0.5          +     dY, pic, '0 0    0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
61                         drawsubpic(theOrigin                        + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0',           '0 0.5  0' + bW, theColor, theAlpha, 0);
62                         drawsubpic(theOrigin + width * 0.5          + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0' + eX - bW, '0 0.5  0' + bW, theColor, theAlpha, 0);
63                         drawsubpic(theOrigin               + height - dY, width * 0.5          +     dY, pic, '0 0.75 0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
64                         drawsubpic(theOrigin + width * 0.5 + height - dY, width * 0.5          +     dY, pic, '0 0.75 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
65                 }
66         }
67         else
68         {
69                 if(theSize.y <= theBorderSize.y * 2)
70                 {
71                         // not high enough... draw just top and bottom then
72                         bH = eY * (0.25 * theSize.y / (theBorderSize.y * 2));
73                         dX = theBorderSize.x * eX;
74                         drawsubpic(theOrigin,                                         dX + height * 0.5, pic, '0    0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
75                         drawsubpic(theOrigin + dX,                        width - 2 * dX + height * 0.5, pic, '0.25 0 0',           '0.5  0 0' + bH, theColor, theAlpha, 0);
76                         drawsubpic(theOrigin + width - dX,                            dX + height * 0.5, pic, '0.75 0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
77                         drawsubpic(theOrigin              + height * 0.5,             dX + height * 0.5, pic, '0    0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
78                         drawsubpic(theOrigin + dX         + height * 0.5, width - 2 * dX + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5  0 0' + bH, theColor, theAlpha, 0);
79                         drawsubpic(theOrigin + width - dX + height * 0.5,             dX + height * 0.5, pic, '0.75 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
80                 }
81                 else
82                 {
83                         dX = theBorderSize.x * eX;
84                         dY = theBorderSize.x * eY;
85                         drawsubpic(theOrigin,                                        dX          +     dY, pic, '0    0    0', '0.25 0.25 0', theColor, theAlpha, 0);
86                         drawsubpic(theOrigin                  + dX,      width - 2 * dX          +     dY, pic, '0.25 0    0', '0.5  0.25 0', theColor, theAlpha, 0);
87                         drawsubpic(theOrigin          + width - dX,                  dX          +     dY, pic, '0.75 0    0', '0.25 0.25 0', theColor, theAlpha, 0);
88                         drawsubpic(theOrigin          + dY,                          dX + height - 2 * dY, pic, '0    0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
89                         drawsubpic(theOrigin          + dY         + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5  0.5  0', theColor, theAlpha, 0);
90                         drawsubpic(theOrigin          + dY + width - dX,             dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
91                         drawsubpic(theOrigin + height - dY,                          dX          +     dY, pic, '0    0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
92                         drawsubpic(theOrigin + height - dY         + dX, width - 2 * dX          +     dY, pic, '0.25 0.75 0', '0.5  0.25 0', theColor, theAlpha, 0);
93                         drawsubpic(theOrigin + height - dY + width - dX,             dX          +     dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
94                 }
95         }
96 }
97
98 vector HUD_Get_Num_Color (float x, float maxvalue)
99 {
100         float blinkingamt;
101         vector color;
102         if(x >= maxvalue) {
103                 color.x = sin(2*M_PI*time);
104                 color.y = 1;
105                 color.z = sin(2*M_PI*time);
106         }
107         else if(x > maxvalue * 0.75) {
108                 color.x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
109                 color.y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
110                 color.z = 0;
111         }
112         else if(x > maxvalue * 0.5) {
113                 color.x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
114                 color.y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
115                 color.z = 1 - (x-100)*0.02; // blue value between 1 -> 0
116         }
117         else if(x > maxvalue * 0.25) {
118                 color.x = 1;
119                 color.y = 1;
120                 color.z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
121         }
122         else if(x > maxvalue * 0.1) {
123                 color.x = 1;
124                 color.y = (x-20)*90/27/100; // green value between 0 -> 1
125                 color.z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
126         }
127         else {
128                 color.x = 1;
129                 color.y = 0;
130                 color.z = 0;
131         }
132
133         blinkingamt = (1 - x/maxvalue/0.25);
134         if(blinkingamt > 0)
135         {
136                 color.x = color.x - color.x * blinkingamt * sin(2*M_PI*time);
137                 color.y = color.y - color.y * blinkingamt * sin(2*M_PI*time);
138                 color.z = color.z - color.z * blinkingamt * sin(2*M_PI*time);
139         }
140         return color;
141 }
142
143 float stringwidth_colors(string s, vector theSize)
144 {
145         return stringwidth(s, true, theSize);
146 }
147
148 float stringwidth_nocolors(string s, vector theSize)
149 {
150         return stringwidth(s, false, theSize);
151 }
152
153 void drawstringright(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag)
154 {
155         position.x -= 2 / 3 * strlen(text) * theScale.x;
156         drawstring(position, text, theScale, rgb, theAlpha, flag);
157 }
158
159 void drawstringcenter(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag)
160 {
161         position.x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * theScale.x);
162         drawstring(position, text, theScale, rgb, theAlpha, flag);
163 }
164
165 // return the string of the onscreen race timer
166 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
167 {
168         string col;
169         string timestr;
170         string cpname;
171         string lapstr;
172         lapstr = "";
173
174         if(histime == 0) // goal hit
175         {
176                 if(mytime > 0)
177                 {
178                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
179                         col = "^1";
180                 }
181                 else if(mytime == 0)
182                 {
183                         timestr = "+0.0";
184                         col = "^3";
185                 }
186                 else
187                 {
188                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
189                         col = "^2";
190                 }
191
192                 if(lapdelta > 0)
193                 {
194                         lapstr = sprintf(_(" (-%dL)"), lapdelta);
195                         col = "^2";
196                 }
197                 else if(lapdelta < 0)
198                 {
199                         lapstr = sprintf(_(" (+%dL)"), -lapdelta);
200                         col = "^1";
201                 }
202         }
203         else if(histime > 0) // anticipation
204         {
205                 if(mytime >= histime)
206                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
207                 else
208                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
209                 col = "^3";
210         }
211         else
212         {
213                 col = "^7";
214                 timestr = "";
215         }
216
217         if(cp == 254)
218                 cpname = _("Start line");
219         else if(cp == 255)
220                 cpname = _("Finish line");
221         else if(cp)
222                 cpname = sprintf(_("Intermediate %d"), cp);
223         else
224                 cpname = _("Finish line");
225
226         if(histime < 0)
227                 return strcat(col, cpname);
228         else if(hisname == "")
229                 return strcat(col, sprintf("%s (%s)", cpname, timestr));
230         else
231                 return strcat(col, sprintf("%s (%s %s)", cpname, timestr, strcat(hisname, col, lapstr)));
232 }
233
234 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
235 float race_CheckName(string net_name) {
236         float i;
237         for (i=RANKINGS_CNT-1;i>=0;--i)
238                 if(grecordholder[i] == net_name)
239                         return i+1;
240         return 0;
241 }
242
243 float GetPlayerColorForce(int i)
244 {
245         if(!teamplay)
246                 return 0;
247         else
248                 return stof(getplayerkeyvalue(i, "colors")) & 15;
249 }
250
251 float GetPlayerColor(int i)
252 {
253         if(!playerslots[i].gotscores) // unconnected
254                 return NUM_SPECTATOR;
255         else if(stof(getplayerkeyvalue(i, "frags")) == FRAGS_SPECTATOR)
256                 return NUM_SPECTATOR;
257         else
258                 return GetPlayerColorForce(i);
259 }
260
261 string GetPlayerName(int i)
262 {
263         return ColorTranslateRGB(getplayerkeyvalue(i, "name"));
264 }
265
266
267 /*
268 ==================
269 HUD panels
270 ==================
271 */
272
273 // draw the background/borders
274 #define HUD_Panel_DrawBg(theAlpha) do {                                                                                                                                                         \
275         if(panel.current_panel_bg != "0" && panel.current_panel_bg != "")                                                                                               \
276                 draw_BorderPicture(panel_pos - '1 1 0' * panel_bg_border, panel.current_panel_bg, panel_size + '1 1 0' * 2 * panel_bg_border, panel_bg_color, panel_bg_alpha * theAlpha, '1 1 0' * (panel_bg_border/BORDER_MULTIPLIER));\
277 } while(0)
278
279 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
280 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, float vertical, float baralign, vector theColor, float theAlpha, float drawflag)
281 {
282         if(!length_ratio || !theAlpha)
283                 return;
284         if(length_ratio > 1)
285                 length_ratio = 1;
286         if (baralign == 3)
287         {
288                 if(length_ratio < -1)
289                         length_ratio = -1;
290         }
291         else if(length_ratio < 0)
292                 return;
293
294         vector square;
295         vector width, height;
296         if(vertical) {
297                 pic = strcat(hud_skin_path, "/", pic, "_vertical");
298                 if(precache_pic(pic) == "") {
299                         pic = "gfx/hud/default/progressbar_vertical";
300                 }
301
302         if (baralign == 1) // bottom align
303                         theOrigin.y += (1 - length_ratio) * theSize.y;
304         else if (baralign == 2) // center align
305             theOrigin.y += 0.5 * (1 - length_ratio) * theSize.y;
306         else if (baralign == 3) // center align, positive values down, negative up
307                 {
308                         theSize.y *= 0.5;
309                         if (length_ratio > 0)
310                                 theOrigin.y += theSize.y;
311                         else
312                         {
313                                 theOrigin.y += (1 + length_ratio) * theSize.y;
314                                 length_ratio = -length_ratio;
315                         }
316                 }
317                 theSize.y *= length_ratio;
318
319                 vector bH;
320                 width = eX * theSize.x;
321                 height = eY * theSize.y;
322                 if(theSize.y <= theSize.x * 2)
323                 {
324                         // button not high enough
325                         // draw just upper and lower part then
326                         square = eY * theSize.y * 0.5;
327                         bH = eY * (0.25 * theSize.y / (theSize.x * 2));
328                         drawsubpic(theOrigin,          square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, drawflag);
329                         drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, drawflag);
330                 }
331                 else
332                 {
333                         square = eY * theSize.x;
334                         drawsubpic(theOrigin,                   width   +     square, pic, '0 0    0', '1 0.25 0', theColor, theAlpha, drawflag);
335                         drawsubpic(theOrigin +          square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5  0', theColor, theAlpha, drawflag);
336                         drawsubpic(theOrigin + height - square, width   +     square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, drawflag);
337                 }
338         } else {
339                 pic = strcat(hud_skin_path, "/", pic);
340                 if(precache_pic(pic) == "") {
341                         pic = "gfx/hud/default/progressbar";
342                 }
343
344                 if (baralign == 1) // right align
345                         theOrigin.x += (1 - length_ratio) * theSize.x;
346         else if (baralign == 2) // center align
347             theOrigin.x += 0.5 * (1 - length_ratio) * theSize.x;
348         else if (baralign == 3) // center align, positive values on the right, negative on the left
349                 {
350                         theSize.x *= 0.5;
351                         if (length_ratio > 0)
352                                 theOrigin.x += theSize.x;
353                         else
354                         {
355                                 theOrigin.x += (1 + length_ratio) * theSize.x;
356                                 length_ratio = -length_ratio;
357                         }
358                 }
359                 theSize.x *= length_ratio;
360
361                 vector bW;
362                 width = eX * theSize.x;
363                 height = eY * theSize.y;
364                 if(theSize.x <= theSize.y * 2)
365                 {
366                         // button not wide enough
367                         // draw just left and right part then
368                         square = eX * theSize.x * 0.5;
369                         bW = eX * (0.25 * theSize.x / (theSize.y * 2));
370                         drawsubpic(theOrigin,          square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, drawflag);
371                         drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, drawflag);
372                 }
373                 else
374                 {
375                         square = eX * theSize.y;
376                         drawsubpic(theOrigin,                  height  +     square, pic, '0    0 0', '0.25 1 0', theColor, theAlpha, drawflag);
377                         drawsubpic(theOrigin +         square, theSize - 2 * square, pic, '0.25 0 0', '0.5  1 0', theColor, theAlpha, drawflag);
378                         drawsubpic(theOrigin + width - square, height  +     square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
379                 }
380         }
381 }
382
383 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, float drawflag)
384 {
385         if(!theAlpha)
386                 return;
387
388         string pic;
389         pic = strcat(hud_skin_path, "/num_leading");
390         if(precache_pic(pic) == "") {
391                 pic = "gfx/hud/default/num_leading";
392         }
393
394         drawsubpic(pos, eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0 0 0', '0.25 1 0', color, theAlpha, drawflag);
395         if(mySize.x/mySize.y > 2)
396                 drawsubpic(pos + eX * mySize.y, eX * (mySize.x - 2 * mySize.y) + eY * mySize.y, pic, '0.25 0 0', '0.5 1 0', color, theAlpha, drawflag);
397         drawsubpic(pos + eX * mySize.x - eX * min(mySize.x * 0.5, mySize.y), eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0.75 0 0', '0.25 1 0', color, theAlpha, drawflag);
398 }
399
400 // Weapon icons (#0)
401 //
402 entity weaponorder[WEP_MAXCOUNT];
403 void weaponorder_swap(int i, int j, entity pass)
404 {
405         entity h = weaponorder[i];
406         weaponorder[i] = weaponorder[j];
407         weaponorder[j] = h;
408 }
409
410 string weaponorder_cmp_str;
411 float weaponorder_cmp(float i, float j, entity pass)
412 {
413         float ai, aj;
414         ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
415         aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
416         return aj - ai; // the string is in REVERSE order (higher prio at the right is what we want, but higher prio first is the string)
417 }
418
419 void HUD_Weapons(void)
420 {
421         // declarations
422         WepSet weapons_stat = WepSet_GetFromStat();
423         float i, f, a;
424         float screen_ar;
425         vector center = '0 0 0';
426         float weapon_count, weapon_id;
427         float row, column, rows = 0, columns = 0;
428         float aspect = autocvar_hud_panel_weapons_aspect;
429
430         float panel_weapon_accuracy;
431
432         float timeout = autocvar_hud_panel_weapons_timeout;
433         float timein_effect_length = autocvar_hud_panel_weapons_timeout_speed_in; //? 0.375 : 0);
434         float timeout_effect_length = autocvar_hud_panel_weapons_timeout_speed_out; //? 0.75 : 0);
435
436         float ammo_full;
437         vector barsize = '0 0 0', baroffset = '0 0 0';
438         vector ammo_color = '1 0 1';
439         float ammo_alpha = 1;
440
441         float when = max(1, autocvar_hud_panel_weapons_complainbubble_time);
442         float fadetime = max(0, autocvar_hud_panel_weapons_complainbubble_fadetime);
443
444         vector weapon_pos, weapon_size = '0 0 0';
445         vector color;
446
447         // check to see if we want to continue
448         if(hud != HUD_NORMAL) { return; }
449
450         if(!autocvar__hud_configure)
451         {
452                 if((!autocvar_hud_panel_weapons) || (spectatee_status == -1))
453                         return;
454                 if(timeout && time >= weapontime + timeout + timeout_effect_length)
455                 if(autocvar_hud_panel_weapons_timeout_effect == 3 || (autocvar_hud_panel_weapons_timeout_effect == 1 && !(autocvar_hud_panel_weapons_timeout_fadebgmin + autocvar_hud_panel_weapons_timeout_fadefgmin)))
456                 {
457                         weaponprevtime = time;
458                         return;
459                 }
460         }
461
462         // update generic hud functions
463         HUD_Panel_UpdateCvars();
464
465         draw_beginBoldFont();
466
467         // figure out weapon order (how the weapons are sorted) // TODO make this configurable
468         if(weaponorder_bypriority != autocvar_cl_weaponpriority || !weaponorder[0])
469         {
470                 int weapon_cnt;
471                 if(weaponorder_bypriority)
472                         strunzone(weaponorder_bypriority);
473                 if(weaponorder_byimpulse)
474                         strunzone(weaponorder_byimpulse);
475
476                 weaponorder_bypriority = strzone(autocvar_cl_weaponpriority);
477                 weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(W_FixWeaponOrder_ForceComplete(W_NumberWeaponOrder(weaponorder_bypriority))));
478                 weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
479
480                 weapon_cnt = 0;
481                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
482                 {
483                         self = get_weaponinfo(i);
484                         if(self.impulse >= 0)
485                         {
486                                 weaponorder[weapon_cnt] = self;
487                                 ++weapon_cnt;
488                         }
489                 }
490                 for(i = weapon_cnt; i < WEP_MAXCOUNT; ++i)
491                         weaponorder[i] = world;
492                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
493
494                 weaponorder_cmp_str = string_null;
495         }
496
497         if(!autocvar_hud_panel_weapons_complainbubble || autocvar__hud_configure || time - complain_weapon_time >= when + fadetime)
498                 complain_weapon = 0;
499
500         // determine which weapons are going to be shown
501         if (autocvar_hud_panel_weapons_onlyowned)
502         {
503                 if(autocvar__hud_configure)
504                 {
505                         if (!weapons_stat)
506                                 for(i = WEP_FIRST; i <= WEP_LAST; i += floor((WEP_LAST-WEP_FIRST)/5))
507                                         weapons_stat |= WepSet_FromWeapon(i);
508
509                         if(menu_enabled != 2)
510                                 HUD_Panel_DrawBg(1); // also draw the bg of the entire panel
511                 }
512
513                 // do we own this weapon?
514                 weapon_count = 0;
515                 for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
516                         if(weapons_stat & WepSet_FromWeapon(weaponorder[i].weapon))
517                                 ++weapon_count;
518
519                 // add it anyway if weaponcomplain is shown
520                 if(complain_weapon)
521                         ++weapon_count;
522
523                 // might as well commit suicide now, no reason to live ;)
524                 if (weapon_count == 0)
525                 {
526                         draw_endBoldFont();
527                         return;
528                 }
529
530                 vector max_panel_size = panel_size - '2 2 0' * panel_bg_padding;
531
532                 // calculate distribution and size of table cells
533                 if(max_panel_size.x > max_panel_size.y)
534                 {
535                         while(weapon_count > columns * rows)
536                         {
537                                 ++rows;
538                                 columns = ceil(max_panel_size.x / (max_panel_size.y / rows * aspect));
539                         }
540
541                         weapon_size.x = max_panel_size.x / columns;
542                         weapon_size.y = max_panel_size.y / rows;
543                         columns = ceil(weapon_count / rows);
544                 }
545                 else
546                 {
547                         while(weapon_count > columns * rows)
548                         {
549                                 ++columns;
550                                 rows = ceil(max_panel_size.y / (max_panel_size.x / columns / aspect));
551                         }
552
553                         weapon_size.x = max_panel_size.x / columns;
554                         weapon_size.y = max_panel_size.y / rows;
555                         rows = ceil(weapon_count / columns);
556                 }
557
558                 // reduce size of the panel
559                 panel_size.x = columns * weapon_size.x;
560                 panel_size.y = rows * weapon_size.y;
561                 panel_pos.x += (max_panel_size.x - panel_size.x) / 2;
562                 panel_pos.y += (max_panel_size.y - panel_size.y) / 2;
563
564                 panel_size += '2 2 0' * panel_bg_padding;
565         }
566         else
567                 weapon_count = WEP_COUNT;
568
569         // animation for fading in/out the panel respectively when not in use
570         if(!autocvar__hud_configure)
571         {
572                 if (timeout && time >= weapontime + timeout) // apply timeout effect if needed
573                 {
574                         f = bound(0, (time - (weapontime + timeout)) / timeout_effect_length, 1);
575
576                         // fade the panel alpha
577                         if(autocvar_hud_panel_weapons_timeout_effect == 1)
578                         {
579                                 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * f + (1 - f));
580                                 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * f + (1 - f));
581                         }
582                         else if(autocvar_hud_panel_weapons_timeout_effect == 3)
583                         {
584                                 panel_bg_alpha *= (1 - f);
585                                 panel_fg_alpha *= (1 - f);
586                         }
587
588                         // move the panel off the screen
589                         if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
590                         {
591                                 f *= f; // for a cooler movement
592                                 center.x = panel_pos.x + panel_size.x/2;
593                                 center.y = panel_pos.y + panel_size.y/2;
594                                 screen_ar = vid_conwidth/vid_conheight;
595                                 if (center.x/center.y < screen_ar) //bottom left
596                                 {
597                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
598                                                 panel_pos.y += f * (vid_conheight - panel_pos.y);
599                                         else //left
600                                                 panel_pos.x -= f * (panel_pos.x + panel_size.x);
601                                 }
602                                 else //top right
603                                 {
604                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //right
605                                                 panel_pos.x += f * (vid_conwidth - panel_pos.x);
606                                         else //top
607                                                 panel_pos.y -= f * (panel_pos.y + panel_size.y);
608                                 }
609                                 if(f == 1)
610                                         center.x = -1; // mark the panel as off screen
611                         }
612                         weaponprevtime = time - (1 - f) * timein_effect_length;
613                 }
614                 else if (timeout && time < weaponprevtime + timein_effect_length) // apply timein effect if needed
615                 {
616                         f = bound(0, (time - weaponprevtime) / timein_effect_length, 1);
617
618                         // fade the panel alpha
619                         if(autocvar_hud_panel_weapons_timeout_effect == 1)
620                         {
621                                 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * (1 - f) + f);
622                                 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * (1 - f) + f);
623                         }
624                         else if(autocvar_hud_panel_weapons_timeout_effect == 3)
625                         {
626                                 panel_bg_alpha *= (f);
627                                 panel_fg_alpha *= (f);
628                         }
629
630                         // move the panel back on screen
631                         if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
632                         {
633                                 f *= f; // for a cooler movement
634                                 f = 1 - f;
635                                 center.x = panel_pos.x + panel_size.x/2;
636                                 center.y = panel_pos.y + panel_size.y/2;
637                                 screen_ar = vid_conwidth/vid_conheight;
638                                 if (center.x/center.y < screen_ar) //bottom left
639                                 {
640                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
641                                                 panel_pos.y += f * (vid_conheight - panel_pos.y);
642                                         else //left
643                                                 panel_pos.x -= f * (panel_pos.x + panel_size.x);
644                                 }
645                                 else //top right
646                                 {
647                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //right
648                                                 panel_pos.x += f * (vid_conwidth - panel_pos.x);
649                                         else //top
650                                                 panel_pos.y -= f * (panel_pos.y + panel_size.y);
651                                 }
652                         }
653                 }
654         }
655
656         // draw the background, then change the virtual size of it to better fit other items inside
657         HUD_Panel_DrawBg(1);
658
659         if(center.x == -1)
660         {
661                 draw_endBoldFont();
662                 return;
663         }
664
665         if(panel_bg_padding)
666         {
667                 panel_pos += '1 1 0' * panel_bg_padding;
668                 panel_size -= '2 2 0' * panel_bg_padding;
669         }
670
671         // after the sizing and animations are done, update the other values
672
673         if(!rows) // if rows is > 0 onlyowned code has already updated these vars
674         {
675                 rows = panel_size.y/panel_size.x;
676                 rows = bound(1, floor((sqrt(4 * aspect * rows * weapon_count + rows * rows) + rows + 0.5) / 2), weapon_count);
677                 columns = ceil(weapon_count/rows);
678                 weapon_size = eX * panel_size.x*(1/columns) + eY * panel_size.y*(1/rows);
679         }
680
681         // calculate position/size for visual bar displaying ammount of ammo status
682         if (autocvar_hud_panel_weapons_ammo)
683         {
684                 ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
685                 ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
686
687                 if(weapon_size.x/weapon_size.y > aspect)
688                 {
689                         barsize.x = aspect * weapon_size.y;
690                         barsize.y = weapon_size.y;
691                         baroffset.x = (weapon_size.x - barsize.x) / 2;
692                 }
693                 else
694                 {
695                         barsize.y = 1/aspect * weapon_size.x;
696                         barsize.x = weapon_size.x;
697                         baroffset.y = (weapon_size.y - barsize.y) / 2;
698                 }
699         }
700         if(autocvar_hud_panel_weapons_accuracy)
701                 Accuracy_LoadColors();
702
703         // draw items
704         row = column = 0;
705         vector label_size = '1 1 0' * min(weapon_size.x, weapon_size.y) * bound(0, autocvar_hud_panel_weapons_label_scale, 1);
706         for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
707         {
708                 // retrieve information about the current weapon to be drawn
709                 self = weaponorder[i];
710                 weapon_id = self.impulse;
711
712                 // skip if this weapon doesn't exist
713                 if(!self || weapon_id < 0) { continue; }
714
715                 // skip this weapon if we don't own it (and onlyowned is enabled)-- or if weapons_complainbubble is showing for this weapon
716                 if(autocvar_hud_panel_weapons_onlyowned)
717                 if (!((weapons_stat & WepSet_FromWeapon(self.weapon)) || (self.weapon == complain_weapon)))
718                         continue;
719
720                 // figure out the drawing position of weapon
721                 weapon_pos = (panel_pos
722                         + eX * column * weapon_size.x
723                         + eY * row * weapon_size.y);
724
725                 // draw background behind currently selected weapon
726                 if(self.weapon == switchweapon)
727                         drawpic_aspect_skin(weapon_pos, "weapon_current_bg", weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
728
729                 // draw the weapon accuracy
730                 if(autocvar_hud_panel_weapons_accuracy)
731                 {
732                         panel_weapon_accuracy = weapon_accuracy[self.weapon-WEP_FIRST];
733                         if(panel_weapon_accuracy >= 0)
734                         {
735                                 color = Accuracy_GetColor(panel_weapon_accuracy);
736                                 drawpic_aspect_skin(weapon_pos, "weapon_accuracy", weapon_size, color, panel_fg_alpha, DRAWFLAG_NORMAL);
737                         }
738                 }
739
740                 // drawing all the weapon items
741                 if(weapons_stat & WepSet_FromWeapon(self.weapon))
742                 {
743                         // draw the weapon image
744                         drawpic_aspect_skin(weapon_pos, self.model2, weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
745
746                         // draw weapon label string
747                         switch(autocvar_hud_panel_weapons_label)
748                         {
749                                 case 1: // weapon number
750                                         drawstring(weapon_pos, ftos(weapon_id), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
751                                         break;
752
753                                 case 2: // bind
754                                         drawstring(weapon_pos, getcommandkey(ftos(weapon_id), strcat("weapon_group_", ftos(weapon_id))), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
755                                         break;
756
757                                 case 3: // weapon name
758                                         drawstring(weapon_pos, strtolower(self.message), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
759                                         break;
760
761                                 default: // nothing
762                                         break;
763                         }
764
765                         // draw ammo status bar
766                         if(autocvar_hud_panel_weapons_ammo && (self.ammo_field != ammo_none))
767                         {
768                                 a = getstati(GetAmmoStat(self.ammo_field)); // how much ammo do we have?
769
770                                 if(a > 0)
771                                 {
772                                         switch(self.ammo_field)
773                                         {
774                                                 case ammo_shells:  ammo_full = autocvar_hud_panel_weapons_ammo_full_shells;  break;
775                                                 case ammo_nails:   ammo_full = autocvar_hud_panel_weapons_ammo_full_nails;   break;
776                                                 case ammo_rockets: ammo_full = autocvar_hud_panel_weapons_ammo_full_rockets; break;
777                                                 case ammo_cells:   ammo_full = autocvar_hud_panel_weapons_ammo_full_cells;   break;
778                                                 case ammo_plasma:  ammo_full = autocvar_hud_panel_weapons_ammo_full_plasma;  break;
779                                                 case ammo_fuel:    ammo_full = autocvar_hud_panel_weapons_ammo_full_fuel;    break;
780                                                 default: ammo_full = 60;
781                                         }
782
783                                         drawsetcliparea(
784                                                 weapon_pos.x + baroffset.x,
785                                                 weapon_pos.y + baroffset.y,
786                                                 barsize.x * bound(0, a/ammo_full, 1),
787                                                 barsize.y
788                                         );
789
790                                         drawpic_aspect_skin(
791                                                 weapon_pos,
792                                                 "weapon_ammo",
793                                                 weapon_size,
794                                                 ammo_color,
795                                                 ammo_alpha,
796                                                 DRAWFLAG_NORMAL
797                                         );
798
799                                         drawresetcliparea();
800                                 }
801                         }
802                 }
803                 else // draw a "ghost weapon icon" if you don't have the weapon
804                 {
805                         drawpic_aspect_skin(weapon_pos, self.model2, weapon_size, '0 0 0', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
806                 }
807
808                 // draw the complain message
809                 if(self.weapon == complain_weapon)
810                 {
811                         if(fadetime)
812                                 a = ((complain_weapon_time + when > time) ? 1 : bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1));
813                         else
814                                 a = ((complain_weapon_time + when > time) ? 1 : 0);
815
816                         string s;
817                         if(complain_weapon_type == 0) {
818                                 s = _("Out of ammo");
819                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
820                         }
821                         else if(complain_weapon_type == 1) {
822                                 s = _("Don't have");
823                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
824                         }
825                         else {
826                                 s = _("Unavailable");
827                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
828                         }
829                         float padding = autocvar_hud_panel_weapons_complainbubble_padding;
830                         drawpic_aspect_skin(weapon_pos + '1 1 0' * padding, "weapon_complainbubble", weapon_size - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
831                         drawstring_aspect(weapon_pos + '1 1 0' * padding, s, weapon_size - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
832                 }
833
834                 // continue with new position for the next weapon
835                 ++row;
836                 if(row >= rows)
837                 {
838                         row = 0;
839                         ++column;
840                 }
841         }
842
843         draw_endBoldFont();
844 }
845
846 // Ammo (#1)
847 void DrawNadeScoreBar(vector myPos, vector mySize, vector color)
848 {
849
850         HUD_Panel_DrawProgressBar(
851                 myPos + eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x,
852                 mySize - eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x,
853                 autocvar_hud_panel_ammo_progressbar_name,
854                 getstatf(STAT_NADE_BONUS_SCORE), 0, 0, color,
855                 autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
856
857 }
858
859 void DrawAmmoNades(vector myPos, vector mySize, float draw_expanding, float expand_time)
860 {
861         float theAlpha = 1, a, b;
862         vector nade_color, picpos, numpos;
863
864         nade_color = Nade_Color(getstati(STAT_NADE_BONUS_TYPE));
865
866         a = getstatf(STAT_NADE_BONUS);
867         b = getstatf(STAT_NADE_BONUS_SCORE);
868
869         if(autocvar_hud_panel_ammo_iconalign)
870         {
871                 numpos = myPos;
872                 picpos = myPos + eX * 2 * mySize.y;
873         }
874         else
875         {
876                 numpos = myPos + eX * mySize.y;
877                 picpos = myPos;
878         }
879
880         DrawNadeScoreBar(myPos, mySize, nade_color);
881
882         if(b > 0 || a > 0)
883         {
884                 if(autocvar_hud_panel_ammo_text)
885                         drawstring_aspect(numpos, ftos(a), eX * (2/3) * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
886
887                 if(draw_expanding)
888                         drawpic_aspect_skin_expanding(picpos, "nade_nbg", '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, expand_time);
889
890                 drawpic_aspect_skin(picpos, "nade_bg" , '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
891                 drawpic_aspect_skin(picpos, "nade_nbg" , '1 1 0' * mySize.y, nade_color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
892         }
893 }
894
895 void DrawAmmoItem(vector myPos, vector mySize, .float ammoType, float isCurrent, float isInfinite)
896 {
897         if(ammoType == ammo_none)
898                 return;
899
900         // Initialize variables
901
902         float ammo;
903         if(autocvar__hud_configure)
904         {
905                 isCurrent = (ammoType == ammo_rockets); // Rockets always current
906                 ammo = 60;
907         }
908         else
909                 ammo = getstati(GetAmmoStat(ammoType));
910
911         if(!isCurrent)
912         {
913                 float scale = bound(0, autocvar_hud_panel_ammo_noncurrent_scale, 1);
914                 myPos = myPos + (mySize - mySize * scale) * 0.5;
915                 mySize = mySize * scale;
916         }
917
918         vector iconPos, textPos;
919         if(autocvar_hud_panel_ammo_iconalign)
920         {
921                 iconPos = myPos + eX * 2 * mySize.y;
922                 textPos = myPos;
923         }
924         else
925         {
926                 iconPos = myPos;
927                 textPos = myPos + eX * mySize.y;
928         }
929
930         float isShadowed = (ammo <= 0 && !isCurrent && !isInfinite);
931
932         vector iconColor = isShadowed ? '0 0 0' : '1 1 1';
933         vector textColor;
934         if(isInfinite)
935                 textColor = '0.2 0.95 0';
936         else if(isShadowed)
937                 textColor = '0 0 0';
938         else if(ammo < 10)
939                 textColor = '0.8 0.04 0';
940         else
941                 textColor = '1 1 1';
942
943         float alpha;
944         if(isCurrent)
945                 alpha = panel_fg_alpha;
946         else if(isShadowed)
947                 alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_ammo_noncurrent_alpha, 1) * 0.5;
948         else
949                 alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_ammo_noncurrent_alpha, 1);
950
951         string text = isInfinite ? "\xE2\x88\x9E" : ftos(ammo); // Use infinity symbol (U+221E)
952
953         // Draw item
954
955         if(isCurrent)
956                 drawpic_aspect_skin(myPos, "ammo_current_bg", mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
957
958         if(ammo > 0 && autocvar_hud_panel_ammo_progressbar)
959                 HUD_Panel_DrawProgressBar(myPos + eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x, mySize - eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x, autocvar_hud_panel_ammo_progressbar_name, ammo/autocvar_hud_panel_ammo_maxammo, 0, 0, textColor, autocvar_hud_progressbar_alpha * alpha, DRAWFLAG_NORMAL);
960
961         if(autocvar_hud_panel_ammo_text)
962                 drawstring_aspect(textPos, text, eX * (2/3) * mySize.x + eY * mySize.y, textColor, alpha, DRAWFLAG_NORMAL);
963
964         drawpic_aspect_skin(iconPos, GetAmmoPicture(ammoType), '1 1 0' * mySize.y, iconColor, alpha, DRAWFLAG_NORMAL);
965 }
966
967 float nade_prevstatus;
968 float nade_prevframe;
969 float nade_statuschange_time;
970 void HUD_Ammo(void)
971 {
972         if(hud != HUD_NORMAL) return;
973         if(!autocvar__hud_configure)
974         {
975                 if(!autocvar_hud_panel_ammo) return;
976                 if(spectatee_status == -1) return;
977         }
978
979         HUD_Panel_UpdateCvars();
980
981         draw_beginBoldFont();
982
983         vector pos, mySize;
984         pos = panel_pos;
985         mySize = panel_size;
986
987         HUD_Panel_DrawBg(1);
988         if(panel_bg_padding)
989         {
990                 pos += '1 1 0' * panel_bg_padding;
991                 mySize -= '2 2 0' * panel_bg_padding;
992         }
993
994         float rows = 0, columns, row, column;
995         float nade_cnt = getstatf(STAT_NADE_BONUS), nade_score = getstatf(STAT_NADE_BONUS_SCORE);
996         float draw_nades = (nade_cnt > 0 || nade_score > 0), nade_statuschange_elapsedtime;
997         float total_ammo_count;
998
999         vector ammo_size;
1000         if (autocvar_hud_panel_ammo_onlycurrent)
1001                 total_ammo_count = 1;
1002         else
1003                 total_ammo_count = AMMO_COUNT;
1004
1005         if(draw_nades)
1006         {
1007                 ++total_ammo_count;
1008                 if (nade_cnt != nade_prevframe)
1009                 {
1010                         nade_statuschange_time = time;
1011                         nade_prevstatus = nade_prevframe;
1012                         nade_prevframe = nade_cnt;
1013                 }
1014         }
1015         else
1016                 nade_prevstatus = nade_prevframe = nade_statuschange_time = 0;
1017
1018         rows = mySize.y/mySize.x;
1019         rows = bound(1, floor((sqrt(4 * (3/1) * rows * (total_ammo_count) + rows * rows) + rows + 0.5) / 2), (total_ammo_count));
1020         //                               ^^^ ammo item aspect goes here
1021
1022         columns = ceil((total_ammo_count)/rows);
1023
1024         ammo_size = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
1025
1026
1027         vector offset = '0 0 0'; // fteqcc sucks
1028         float newSize;
1029         if(ammo_size.x/ammo_size.y > 3)
1030         {
1031                 newSize = 3 * ammo_size.y;
1032                 offset.x = ammo_size.x - newSize;
1033                 pos.x += offset.x/2;
1034                 ammo_size.x = newSize;
1035         }
1036         else
1037         {
1038                 newSize = 1/3 * ammo_size.x;
1039                 offset.y = ammo_size.y - newSize;
1040                 pos.y += offset.y/2;
1041                 ammo_size.y = newSize;
1042         }
1043
1044         float i;
1045         float infinite_ammo = (getstati(STAT_ITEMS, 0, 24) & IT_UNLIMITED_WEAPON_AMMO);
1046         row = column = 0;
1047         if(autocvar_hud_panel_ammo_onlycurrent)
1048         {
1049                 if(autocvar__hud_configure)
1050                 {
1051                         DrawAmmoItem(pos, ammo_size, ammo_rockets, true, false);
1052                 }
1053                 else
1054                 {
1055                         DrawAmmoItem(
1056                                 pos,
1057                                 ammo_size,
1058                                 (get_weaponinfo(switchweapon)).ammo_field,
1059                                 true,
1060                                 infinite_ammo
1061                         );
1062                 }
1063
1064                 ++row;
1065                 if(row >= rows)
1066                 {
1067                         row = 0;
1068                         column = column + 1;
1069                 }
1070         }
1071         else
1072         {
1073                 .float ammotype;
1074                 row = column = 0;
1075                 for(i = 0; i < AMMO_COUNT; ++i)
1076                 {
1077                         ammotype = GetAmmoFieldFromNum(i);
1078                         DrawAmmoItem(
1079                                 pos + eX * column * (ammo_size.x + offset.x) + eY * row * (ammo_size.y + offset.y),
1080                                 ammo_size,
1081                                 ammotype,
1082                                 ((get_weaponinfo(switchweapon)).ammo_field == ammotype),
1083                                 infinite_ammo
1084                         );
1085
1086                         ++row;
1087                         if(row >= rows)
1088                         {
1089                                 row = 0;
1090                                 column = column + 1;
1091                         }
1092                 }
1093         }
1094
1095         if (draw_nades)
1096         {
1097                 nade_statuschange_elapsedtime = time - nade_statuschange_time;
1098
1099                 float f = bound(0, nade_statuschange_elapsedtime*2, 1);
1100
1101                 DrawAmmoNades(pos + eX * column * (ammo_size.x + offset.x) + eY * row * (ammo_size.y + offset.y), ammo_size, nade_prevstatus < nade_cnt && nade_cnt != 0 && f < 1, f);
1102         }
1103
1104         draw_endBoldFont();
1105 }
1106
1107 void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, float vertical, float icon_right_align, vector color, float theAlpha, float fadelerp)
1108 {
1109         vector newPos = '0 0 0', newSize = '0 0 0';
1110         vector picpos, numpos;
1111
1112         if (vertical)
1113         {
1114                 if(mySize.y/mySize.x > 2)
1115                 {
1116                         newSize.y = 2 * mySize.x;
1117                         newSize.x = mySize.x;
1118
1119                         newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
1120                         newPos.x = myPos.x;
1121                 }
1122                 else
1123                 {
1124                         newSize.x = 1/2 * mySize.y;
1125                         newSize.y = mySize.y;
1126
1127                         newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
1128                         newPos.y = myPos.y;
1129                 }
1130
1131                 if(icon_right_align)
1132                 {
1133                         numpos = newPos;
1134                         picpos = newPos + eY * newSize.x;
1135                 }
1136                 else
1137                 {
1138                         picpos = newPos;
1139                         numpos = newPos + eY * newSize.x;
1140                 }
1141
1142                 newSize.y /= 2;
1143                 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
1144                 // make number smaller than icon, it looks better
1145                 // reduce only y to draw numbers with different number of digits with the same y size
1146                 numpos.y += newSize.y * ((1 - 0.7) / 2);
1147                 newSize.y *= 0.7;
1148                 drawstring_aspect(numpos, ftos(x), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
1149                 return;
1150         }
1151
1152         if(mySize.x/mySize.y > 3)
1153         {
1154                 newSize.x = 3 * mySize.y;
1155                 newSize.y = mySize.y;
1156
1157                 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
1158                 newPos.y = myPos.y;
1159         }
1160         else
1161         {
1162                 newSize.y = 1/3 * mySize.x;
1163                 newSize.x = mySize.x;
1164
1165                 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
1166                 newPos.x = myPos.x;
1167         }
1168
1169         if(icon_right_align) // right align
1170         {
1171                 numpos = newPos;
1172                 picpos = newPos + eX * 2 * newSize.y;
1173         }
1174         else // left align
1175         {
1176                 numpos = newPos + eX * newSize.y;
1177                 picpos = newPos;
1178         }
1179
1180         // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
1181         // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
1182         drawstring_aspect_expanding(numpos, ftos(x), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
1183         drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
1184 }
1185
1186 void DrawNumIcon(vector myPos, vector mySize, float x, string icon, float vertical, float icon_right_align, vector color, float theAlpha)
1187 {
1188         DrawNumIcon_expanding(myPos, mySize, x, icon, vertical, icon_right_align, color, theAlpha, 0);
1189 }
1190
1191 // Powerups (#2)
1192 //
1193 void HUD_Powerups(void)
1194 {
1195         float strength_time, shield_time, superweapons_time;
1196         if(!autocvar__hud_configure)
1197         {
1198                 if(!autocvar_hud_panel_powerups) return;
1199                 if(spectatee_status == -1) return;
1200                 if(!(getstati(STAT_ITEMS, 0, 24) & (IT_STRENGTH | IT_INVINCIBLE | IT_SUPERWEAPON))) return;
1201                 if (getstati(STAT_HEALTH) <= 0) return;
1202
1203                 strength_time = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
1204                 shield_time = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
1205                 superweapons_time = bound(0, getstatf(STAT_SUPERWEAPONS_FINISHED) - time, 99);
1206
1207                 if (getstati(STAT_ITEMS, 0, 24) & IT_UNLIMITED_SUPERWEAPONS)
1208                         superweapons_time = 99; // force max
1209
1210                 // prevent stuff to show up on mismatch that will be fixed next frame
1211                 if (!(getstati(STAT_ITEMS, 0, 24) & IT_SUPERWEAPON))
1212                         superweapons_time = 0;
1213         }
1214         else
1215         {
1216                 strength_time = 15;
1217                 shield_time = 27;
1218                 superweapons_time = 13;
1219         }
1220
1221         HUD_Panel_UpdateCvars();
1222
1223         draw_beginBoldFont();
1224
1225         vector pos, mySize;
1226         pos = panel_pos;
1227         mySize = panel_size;
1228
1229         HUD_Panel_DrawBg(bound(0, max(strength_time, shield_time, superweapons_time), 1));
1230         if(panel_bg_padding)
1231         {
1232                 pos += '1 1 0' * panel_bg_padding;
1233                 mySize -= '2 2 0' * panel_bg_padding;
1234         }
1235
1236         float panel_ar = mySize.x/mySize.y;
1237         float is_vertical = (panel_ar < 1);
1238         vector shield_offset = '0 0 0', strength_offset = '0 0 0', superweapons_offset = '0 0 0';
1239
1240         float superweapons_is = -1;
1241
1242         if(superweapons_time)
1243         {
1244                 if(strength_time)
1245                 {
1246                         if(shield_time)
1247                                 superweapons_is = 0;
1248                         else
1249                                 superweapons_is = 2;
1250                 }
1251                 else
1252                 {
1253                         if(shield_time)
1254                                 superweapons_is = 1;
1255                         else
1256                                 superweapons_is = 2;
1257                 }
1258         }
1259
1260         // FIXME handle superweapons here
1261         if(superweapons_is == 0)
1262         {
1263                 if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
1264                 {
1265                         mySize.x *= (1.0 / 3.0);
1266                         superweapons_offset.x = mySize.x;
1267                         if (autocvar_hud_panel_powerups_flip)
1268                                 shield_offset.x = 2*mySize.x;
1269                         else
1270                                 strength_offset.x = 2*mySize.x;
1271                 }
1272                 else
1273                 {
1274                         mySize.y *= (1.0 / 3.0);
1275                         superweapons_offset.y = mySize.y;
1276                         if (autocvar_hud_panel_powerups_flip)
1277                                 shield_offset.y = 2*mySize.y;
1278                         else
1279                                 strength_offset.y = 2*mySize.y;
1280                 }
1281         }
1282         else
1283         {
1284                 if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
1285                 {
1286                         mySize.x *= 0.5;
1287                         if (autocvar_hud_panel_powerups_flip)
1288                                 shield_offset.x = mySize.x;
1289                         else
1290                                 strength_offset.x = mySize.x;
1291                 }
1292                 else
1293                 {
1294                         mySize.y *= 0.5;
1295                         if (autocvar_hud_panel_powerups_flip)
1296                                 shield_offset.y = mySize.y;
1297                         else
1298                                 strength_offset.y = mySize.y;
1299                 }
1300         }
1301
1302         float shield_baralign, strength_baralign, superweapons_baralign;
1303         float shield_iconalign, strength_iconalign, superweapons_iconalign;
1304
1305         if (autocvar_hud_panel_powerups_flip)
1306         {
1307                 strength_baralign = (autocvar_hud_panel_powerups_baralign == 2 || autocvar_hud_panel_powerups_baralign == 1);
1308                 shield_baralign = (autocvar_hud_panel_powerups_baralign == 3 || autocvar_hud_panel_powerups_baralign == 1);
1309                 strength_iconalign = (autocvar_hud_panel_powerups_iconalign == 2 || autocvar_hud_panel_powerups_iconalign == 1);
1310                 shield_iconalign = (autocvar_hud_panel_powerups_iconalign == 3 || autocvar_hud_panel_powerups_iconalign == 1);
1311         }
1312         else
1313         {
1314                 shield_baralign = (autocvar_hud_panel_powerups_baralign == 2 || autocvar_hud_panel_powerups_baralign == 1);
1315                 strength_baralign = (autocvar_hud_panel_powerups_baralign == 3 || autocvar_hud_panel_powerups_baralign == 1);
1316                 shield_iconalign = (autocvar_hud_panel_powerups_iconalign == 2 || autocvar_hud_panel_powerups_iconalign == 1);
1317                 strength_iconalign = (autocvar_hud_panel_powerups_iconalign == 3 || autocvar_hud_panel_powerups_iconalign == 1);
1318         }
1319
1320         if(superweapons_is == 0)
1321         {
1322                 superweapons_iconalign = strength_iconalign;
1323                 superweapons_baralign = 2;
1324         }
1325         else if(superweapons_is == 1)
1326         {
1327                 superweapons_offset = strength_offset;
1328                 superweapons_iconalign = strength_iconalign;
1329                 superweapons_baralign = strength_baralign;
1330         }
1331         else // if(superweapons_is == 2)
1332         {
1333                 superweapons_offset = shield_offset;
1334                 superweapons_iconalign = shield_iconalign;
1335                 superweapons_baralign = shield_baralign;
1336         }
1337
1338         if(shield_time)
1339         {
1340                 const float maxshield = 30;
1341                 float shield = ceil(shield_time);
1342                 if(autocvar_hud_panel_powerups_progressbar)
1343                         HUD_Panel_DrawProgressBar(pos + shield_offset, mySize, autocvar_hud_panel_powerups_progressbar_shield, shield/maxshield, is_vertical, shield_baralign, autocvar_hud_progressbar_shield_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1344                 if(autocvar_hud_panel_powerups_text)
1345                 {
1346                         if(shield > 1)
1347                                 DrawNumIcon(pos + shield_offset, mySize, shield, "shield", is_vertical, shield_iconalign, '1 1 1', 1);
1348                         if(shield <= 5)
1349                                 DrawNumIcon_expanding(pos + shield_offset, mySize, shield, "shield", is_vertical, shield_iconalign, '1 1 1', 1, bound(0, (shield - shield_time) / 0.5, 1));
1350                 }
1351         }
1352
1353         if(strength_time)
1354         {
1355                 const float maxstrength = 30;
1356                 float strength = ceil(strength_time);
1357                 if(autocvar_hud_panel_powerups_progressbar)
1358                         HUD_Panel_DrawProgressBar(pos + strength_offset, mySize, autocvar_hud_panel_powerups_progressbar_strength, strength/maxstrength, is_vertical, strength_baralign, autocvar_hud_progressbar_strength_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1359                 if(autocvar_hud_panel_powerups_text)
1360                 {
1361                         if(strength > 1)
1362                                 DrawNumIcon(pos + strength_offset, mySize, strength, "strength", is_vertical, strength_iconalign, '1 1 1', 1);
1363                         if(strength <= 5)
1364                                 DrawNumIcon_expanding(pos + strength_offset, mySize, strength, "strength", is_vertical, strength_iconalign, '1 1 1', 1, bound(0, (strength - strength_time) / 0.5, 1));
1365                 }
1366         }
1367
1368         if(superweapons_time)
1369         {
1370                 const float maxsuperweapons = 30;
1371                 float superweapons = ceil(superweapons_time);
1372                 if(autocvar_hud_panel_powerups_progressbar)
1373                         HUD_Panel_DrawProgressBar(pos + superweapons_offset, mySize, autocvar_hud_panel_powerups_progressbar_superweapons, superweapons/maxsuperweapons, is_vertical, superweapons_baralign, autocvar_hud_progressbar_superweapons_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1374                 if(autocvar_hud_panel_powerups_text)
1375                 {
1376                         if(superweapons > 1)
1377                                 DrawNumIcon(pos + superweapons_offset, mySize, superweapons, "superweapons", is_vertical, superweapons_iconalign, '1 1 1', 1);
1378                         if(superweapons <= 5)
1379                                 DrawNumIcon_expanding(pos + superweapons_offset, mySize, superweapons, "superweapons", is_vertical, superweapons_iconalign, '1 1 1', 1, bound(0, (superweapons - superweapons_time) / 0.5, 1));
1380                 }
1381         }
1382
1383         draw_endBoldFont();
1384 }
1385
1386 // Health/armor (#3)
1387 //
1388
1389 // prev_* vars contain the health/armor at the previous FRAME
1390 // set to -1 when player is dead or was not playing
1391 float prev_health, prev_armor;
1392 float health_damagetime, armor_damagetime;
1393 float health_beforedamage, armor_beforedamage;
1394 // old_p_* vars keep track of previous values when smoothing value changes of the progressbar
1395 float old_p_health, old_p_armor;
1396 float old_p_healthtime, old_p_armortime;
1397 // prev_p_* vars contain the health/armor progressbar value at the previous FRAME
1398 // set to -1 to forcedly stop effects when we switch spectated player (e.g. from playerX: 70h to playerY: 50h)
1399 float prev_p_health, prev_p_armor;
1400
1401 void HUD_HealthArmor(void)
1402 {
1403         float armor, health, fuel;
1404         if(!autocvar__hud_configure)
1405         {
1406                 if(!autocvar_hud_panel_healtharmor) return;
1407                 if(hud != HUD_NORMAL) return;
1408                 if(spectatee_status == -1) return;
1409
1410                 health = getstati(STAT_HEALTH);
1411                 if(health <= 0)
1412                 {
1413                         prev_health = -1;
1414                         return;
1415                 }
1416                 armor = getstati(STAT_ARMOR);
1417
1418                 // code to check for spectatee_status changes is in Ent_ClientData()
1419                 // prev_p_health and prev_health can be set to -1 there
1420
1421                 if (prev_p_health == -1)
1422                 {
1423                         // no effect
1424                         health_beforedamage = 0;
1425                         armor_beforedamage = 0;
1426                         health_damagetime = 0;
1427                         armor_damagetime = 0;
1428                         prev_health = health;
1429                         prev_armor = armor;
1430                         old_p_health = health;
1431                         old_p_armor = armor;
1432                         prev_p_health = health;
1433                         prev_p_armor = armor;
1434                 }
1435                 else if (prev_health == -1)
1436                 {
1437                         //start the load effect
1438                         health_damagetime = 0;
1439                         armor_damagetime = 0;
1440                         prev_health = 0;
1441                         prev_armor = 0;
1442                 }
1443                 fuel = getstati(STAT_FUEL);
1444         }
1445         else
1446         {
1447                 health = 150;
1448                 armor = 75;
1449                 fuel = 20;
1450         }
1451
1452         HUD_Panel_UpdateCvars();
1453         vector pos, mySize;
1454         pos = panel_pos;
1455         mySize = panel_size;
1456
1457         HUD_Panel_DrawBg(1);
1458         if(panel_bg_padding)
1459         {
1460                 pos += '1 1 0' * panel_bg_padding;
1461                 mySize -= '2 2 0' * panel_bg_padding;
1462         }
1463
1464         float baralign = autocvar_hud_panel_healtharmor_baralign;
1465         float iconalign = autocvar_hud_panel_healtharmor_iconalign;
1466
1467     float maxhealth = autocvar_hud_panel_healtharmor_maxhealth;
1468     float maxarmor = autocvar_hud_panel_healtharmor_maxarmor;
1469         if(autocvar_hud_panel_healtharmor == 2) // combined health and armor display
1470         {
1471                 vector v;
1472                 v = healtharmor_maxdamage(health, armor, armorblockpercent, DEATH_WEAPON);
1473
1474                 float x;
1475                 x = floor(v.x + 1);
1476
1477         float maxtotal = maxhealth + maxarmor;
1478                 string biggercount;
1479                 if(v.z) // NOT fully armored
1480                 {
1481                         biggercount = "health";
1482                         if(autocvar_hud_panel_healtharmor_progressbar)
1483                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_health, x/maxtotal, 0, (baralign == 1 || baralign == 2), autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1484                         if(armor)
1485             if(autocvar_hud_panel_healtharmor_text)
1486                                 drawpic_aspect_skin(pos + eX * mySize.x - eX * 0.5 * mySize.y, "armor", '0.5 0.5 0' * mySize.y, '1 1 1', panel_fg_alpha * armor / health, DRAWFLAG_NORMAL);
1487                 }
1488                 else
1489                 {
1490                         biggercount = "armor";
1491                         if(autocvar_hud_panel_healtharmor_progressbar)
1492                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, x/maxtotal, 0, (baralign == 1 || baralign == 2), autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1493                         if(health)
1494             if(autocvar_hud_panel_healtharmor_text)
1495                                 drawpic_aspect_skin(pos + eX * mySize.x - eX * 0.5 * mySize.y, "health", '0.5 0.5 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1496                 }
1497         if(autocvar_hud_panel_healtharmor_text)
1498                         DrawNumIcon(pos, mySize, x, biggercount, 0, iconalign, HUD_Get_Num_Color(x, maxtotal), 1);
1499
1500                 if(fuel)
1501                         HUD_Panel_DrawProgressBar(pos, eX * mySize.x + eY * 0.2 * mySize.y, "progressbar", fuel/100, 0, (baralign == 1 || baralign == 3), autocvar_hud_progressbar_fuel_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
1502         }
1503         else
1504         {
1505                 float panel_ar = mySize.x/mySize.y;
1506                 float is_vertical = (panel_ar < 1);
1507                 vector health_offset = '0 0 0', armor_offset = '0 0 0';
1508                 if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
1509                 {
1510                         mySize.x *= 0.5;
1511                         if (autocvar_hud_panel_healtharmor_flip)
1512                                 health_offset.x = mySize.x;
1513                         else
1514                                 armor_offset.x = mySize.x;
1515                 }
1516                 else
1517                 {
1518                         mySize.y *= 0.5;
1519                         if (autocvar_hud_panel_healtharmor_flip)
1520                                 health_offset.y = mySize.y;
1521                         else
1522                                 armor_offset.y = mySize.y;
1523                 }
1524
1525                 float health_baralign, armor_baralign, fuel_baralign;
1526                 float health_iconalign, armor_iconalign;
1527                 if (autocvar_hud_panel_healtharmor_flip)
1528                 {
1529                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
1530                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
1531                         fuel_baralign = health_baralign;
1532                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
1533                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
1534                 }
1535                 else
1536                 {
1537                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
1538                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
1539                         fuel_baralign = armor_baralign;
1540                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
1541                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
1542                 }
1543
1544                 //if(health)
1545                 {
1546                         if(autocvar_hud_panel_healtharmor_progressbar)
1547                         {
1548                                 float p_health, pain_health_alpha;
1549                                 p_health = health;
1550                                 pain_health_alpha = 1;
1551                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
1552                                 {
1553                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
1554                                         {
1555                                                 if (fabs(prev_health - health) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
1556                                                 {
1557                                                         if (time - old_p_healthtime < 1)
1558                                                                 old_p_health = prev_p_health;
1559                                                         else
1560                                                                 old_p_health = prev_health;
1561                                                         old_p_healthtime = time;
1562                                                 }
1563                                                 if (time - old_p_healthtime < 1)
1564                                                 {
1565                                                         p_health += (old_p_health - health) * (1 - (time - old_p_healthtime));
1566                                                         prev_p_health = p_health;
1567                                                 }
1568                                         }
1569                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
1570                                         {
1571                                                 if (prev_health - health >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
1572                                                 {
1573                                                         if (time - health_damagetime >= 1)
1574                                                                 health_beforedamage = prev_health;
1575                                                         health_damagetime = time;
1576                                                 }
1577                                                 if (time - health_damagetime < 1)
1578                                                 {
1579                                                         float health_damagealpha = 1 - (time - health_damagetime)*(time - health_damagetime);
1580                                                         HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, health_beforedamage/maxhealth, is_vertical, health_baralign, autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * health_damagealpha, DRAWFLAG_NORMAL);
1581                                                 }
1582                                         }
1583                                         prev_health = health;
1584
1585                                         if (health <= autocvar_hud_panel_healtharmor_progressbar_gfx_lowhealth)
1586                                         {
1587                                                 float BLINK_FACTOR = 0.15;
1588                                                 float BLINK_BASE = 0.85;
1589                                                 float BLINK_FREQ = 9;
1590                                                 pain_health_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
1591                                         }
1592                                 }
1593                                 HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, p_health/maxhealth, is_vertical, health_baralign, autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * pain_health_alpha, DRAWFLAG_NORMAL);
1594                         }
1595                         if(autocvar_hud_panel_healtharmor_text)
1596                                 DrawNumIcon(pos + health_offset, mySize, health, "health", is_vertical, health_iconalign, HUD_Get_Num_Color(health, maxhealth), 1);
1597                 }
1598
1599                 if(armor)
1600                 {
1601                         if(autocvar_hud_panel_healtharmor_progressbar)
1602                         {
1603                                 float p_armor;
1604                                 p_armor = armor;
1605                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
1606                                 {
1607                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
1608                                         {
1609                                                 if (fabs(prev_armor - armor) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
1610                                                 {
1611                                                         if (time - old_p_armortime < 1)
1612                                                                 old_p_armor = prev_p_armor;
1613                                                         else
1614                                                                 old_p_armor = prev_armor;
1615                                                         old_p_armortime = time;
1616                                                 }
1617                                                 if (time - old_p_armortime < 1)
1618                                                 {
1619                                                         p_armor += (old_p_armor - armor) * (1 - (time - old_p_armortime));
1620                                                         prev_p_armor = p_armor;
1621                                                 }
1622                                         }
1623                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
1624                                         {
1625                                                 if (prev_armor - armor >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
1626                                                 {
1627                                                         if (time - armor_damagetime >= 1)
1628                                                                 armor_beforedamage = prev_armor;
1629                                                         armor_damagetime = time;
1630                                                 }
1631                                                 if (time - armor_damagetime < 1)
1632                                                 {
1633                                                         float armor_damagealpha = 1 - (time - armor_damagetime)*(time - armor_damagetime);
1634                                                         HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, armor_beforedamage/maxarmor, is_vertical, armor_baralign, autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * armor_damagealpha, DRAWFLAG_NORMAL);
1635                                                 }
1636                                         }
1637                                         prev_armor = armor;
1638                                 }
1639                                 HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, p_armor/maxarmor, is_vertical, armor_baralign, autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1640                         }
1641                         if(autocvar_hud_panel_healtharmor_text)
1642                                 DrawNumIcon(pos + armor_offset, mySize, armor, "armor", is_vertical, armor_iconalign, HUD_Get_Num_Color(armor, maxarmor), 1);
1643                 }
1644
1645                 if(fuel)
1646                 {
1647                         if (is_vertical)
1648                                 mySize.x *= 0.2 / 2; //if vertical always halve x to not cover too much numbers with 3 digits
1649                         else
1650                                 mySize.y *= 0.2;
1651                         if (panel_ar >= 4)
1652                                 mySize.x *= 2; //restore full panel size
1653                         else if (panel_ar < 1/4)
1654                                 mySize.y *= 2; //restore full panel size
1655                         HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", fuel/100, is_vertical, fuel_baralign, autocvar_hud_progressbar_fuel_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
1656                 }
1657         }
1658 }
1659
1660 // Notification area (#4)
1661 //
1662
1663 void HUD_Notify_Push(string icon, string attacker, string victim)
1664 {
1665         if (icon == "")
1666                 return;
1667
1668         ++notify_count;
1669         --notify_index;
1670
1671         if (notify_index == -1)
1672                 notify_index = NOTIFY_MAX_ENTRIES-1;
1673
1674         // Free old strings
1675         if (notify_attackers[notify_index])
1676                 strunzone(notify_attackers[notify_index]);
1677
1678         if (notify_victims[notify_index])
1679                 strunzone(notify_victims[notify_index]);
1680
1681         if (notify_icons[notify_index])
1682                 strunzone(notify_icons[notify_index]);
1683
1684         // Allocate new strings
1685         if (victim != "")
1686         {
1687                 notify_attackers[notify_index] = strzone(attacker);
1688                 notify_victims[notify_index] = strzone(victim);
1689         }
1690         else
1691         {
1692                 // In case of a notification without a victim, the attacker
1693                 // is displayed on the victim's side. Instead of special
1694                 // treatment later on, we can simply switch them here.
1695                 notify_attackers[notify_index] = string_null;
1696                 notify_victims[notify_index] = strzone(attacker);
1697         }
1698
1699         notify_icons[notify_index] = strzone(icon);
1700         notify_times[notify_index] = time;
1701 }
1702
1703 void HUD_Notify(void)
1704 {
1705         if (!autocvar__hud_configure)
1706                 if (!autocvar_hud_panel_notify)
1707                         return;
1708
1709         HUD_Panel_UpdateCvars();
1710         HUD_Panel_DrawBg(1);
1711
1712         if (!autocvar__hud_configure)
1713                 if (notify_count == 0)
1714                         return;
1715
1716         vector pos, size;
1717         pos  = panel_pos;
1718         size = panel_size;
1719
1720         if (panel_bg_padding)
1721         {
1722                 pos  += '1 1 0' * panel_bg_padding;
1723                 size -= '2 2 0' * panel_bg_padding;
1724         }
1725
1726         float fade_start = max(0, autocvar_hud_panel_notify_time);
1727         float fade_time = max(0, autocvar_hud_panel_notify_fadetime);
1728         float icon_aspect = max(1, autocvar_hud_panel_notify_icon_aspect);
1729
1730         float entry_count = bound(1, floor(NOTIFY_MAX_ENTRIES * size.y / size.x), NOTIFY_MAX_ENTRIES);
1731         float entry_height = size.y / entry_count;
1732
1733         float panel_width_half = size.x * 0.5;
1734         float icon_width_half = entry_height * icon_aspect / 2;
1735         float name_maxwidth = panel_width_half - icon_width_half - size.x * NOTIFY_ICON_MARGIN;
1736
1737         vector font_size = '0.5 0.5 0' * entry_height * autocvar_hud_panel_notify_fontsize;
1738         vector icon_size = (eX * icon_aspect + eY) * entry_height;
1739         vector icon_left = eX * (panel_width_half - icon_width_half);
1740         vector attacker_right = eX * name_maxwidth;
1741         vector victim_left = eX * (size.x - name_maxwidth);
1742
1743         vector attacker_pos, victim_pos, icon_pos;
1744         string attacker, victim, icon;
1745         float i, j, count, step, limit, alpha;
1746
1747         if (autocvar_hud_panel_notify_flip)
1748         {
1749                 // Order items from the top down
1750                 i = 0;
1751                 step = +1;
1752                 limit = entry_count;
1753         }
1754         else
1755         {
1756                 // Order items from the bottom up
1757                 i = entry_count - 1;
1758                 step = -1;
1759                 limit = -1;
1760         }
1761
1762         for (j = notify_index, count = 0; i != limit; i += step, ++j, ++count)
1763         {
1764                 if(autocvar__hud_configure)
1765                 {
1766                         attacker = sprintf(_("Player %d"), count + 1);
1767                         victim = sprintf(_("Player %d"), count + 2);
1768                         icon = get_weaponinfo(min(WEP_FIRST + count * 2, WEP_LAST)).model2;
1769                         alpha = bound(0, 1.2 - count / entry_count, 1);
1770                 }
1771                 else
1772                 {
1773                         if (j == NOTIFY_MAX_ENTRIES)
1774                                 j = 0;
1775
1776                         if (notify_times[j] + fade_start > time)
1777                                 alpha = 1;
1778                         else if (fade_time != 0)
1779                         {
1780                                 alpha = bound(0, (notify_times[j] + fade_start + fade_time - time) / fade_time, 1);
1781                                 if (alpha == 0)
1782                                         break;
1783                         }
1784                         else
1785                                 break;
1786
1787                         attacker = notify_attackers[j];
1788                         victim = notify_victims[j];
1789                         icon = notify_icons[j];
1790                 }
1791
1792                 if (icon != "" && victim != "")
1793                 {
1794                         vector name_top = eY * (i * entry_height + 0.5 * (entry_height - font_size.y));
1795
1796                         icon_pos = pos + icon_left + eY * i * entry_height;
1797                         drawpic_aspect_skin(icon_pos, icon, icon_size, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1798
1799                         victim = textShortenToWidth(victim, name_maxwidth, font_size, stringwidth_colors);
1800                         victim_pos = pos + victim_left + name_top;
1801                         drawcolorcodedstring(victim_pos, victim, font_size, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1802
1803                         if (attacker != "")
1804                         {
1805                                 attacker = textShortenToWidth(attacker, name_maxwidth, font_size, stringwidth_colors);
1806                                 attacker_pos = pos + attacker_right - eX * stringwidth(attacker, true, font_size) + name_top;
1807                                 drawcolorcodedstring(attacker_pos, attacker, font_size, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1808                         }
1809                 }
1810         }
1811
1812         notify_count = count;
1813 }
1814
1815 // Timer (#5)
1816 //
1817 // TODO: macro
1818 string seconds_tostring(float sec)
1819 {
1820         float minutes;
1821         minutes = floor(sec / 60);
1822
1823         sec -= minutes * 60;
1824         return sprintf("%d:%02d", minutes, sec);
1825 }
1826
1827 void HUD_Timer(void)
1828 {
1829         if(!autocvar__hud_configure)
1830         {
1831                 if(!autocvar_hud_panel_timer) return;
1832         }
1833
1834         HUD_Panel_UpdateCvars();
1835
1836         draw_beginBoldFont();
1837
1838         vector pos, mySize;
1839         pos = panel_pos;
1840         mySize = panel_size;
1841
1842         HUD_Panel_DrawBg(1);
1843         if(panel_bg_padding)
1844         {
1845                 pos += '1 1 0' * panel_bg_padding;
1846                 mySize -= '2 2 0' * panel_bg_padding;
1847         }
1848
1849         string timer;
1850         float timelimit, elapsedTime, timeleft, minutesLeft;
1851
1852         timelimit = getstatf(STAT_TIMELIMIT);
1853
1854         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
1855         timeleft = ceil(timeleft);
1856
1857         minutesLeft = floor(timeleft / 60);
1858
1859         vector timer_color;
1860         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
1861                 timer_color = '1 1 1'; //white
1862         else if(minutesLeft >= 1)
1863                 timer_color = '1 1 0'; //yellow
1864         else
1865                 timer_color = '1 0 0'; //red
1866
1867         if (autocvar_hud_panel_timer_increment || timelimit == 0 || warmup_stage) {
1868                 if (time < getstatf(STAT_GAMESTARTTIME)) {
1869                         //while restart is still active, show 00:00
1870                         timer = seconds_tostring(0);
1871                 } else {
1872                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
1873                         timer = seconds_tostring(elapsedTime);
1874                 }
1875         } else {
1876                 timer = seconds_tostring(timeleft);
1877         }
1878
1879         drawstring_aspect(pos, timer, mySize, timer_color, panel_fg_alpha, DRAWFLAG_NORMAL);
1880
1881         draw_endBoldFont();
1882 }
1883
1884 // Radar (#6)
1885 //
1886 void HUD_Radar(void)
1887 {
1888         if (!autocvar__hud_configure)
1889         {
1890                 if (hud_panel_radar_maximized)
1891                 {
1892                         if (!hud_draw_maximized) return;
1893                 }
1894                 else
1895                 {
1896                         if (autocvar_hud_panel_radar == 0) return;
1897                         if (autocvar_hud_panel_radar != 2 && !teamplay) return;
1898                         if(radar_panel_modified)
1899                         {
1900                                 panel.update_time = time; // forces reload of panel attributes
1901                                 radar_panel_modified = false;
1902                         }
1903                 }
1904         }
1905
1906         HUD_Panel_UpdateCvars();
1907
1908         float f = 0;
1909
1910         if (hud_panel_radar_maximized && !autocvar__hud_configure)
1911         {
1912                 panel_size = autocvar_hud_panel_radar_maximized_size;
1913                 panel_size.x = bound(0.2, panel_size.x, 1) * vid_conwidth;
1914                 panel_size.y = bound(0.2, panel_size.y, 1) * vid_conheight;
1915                 panel_pos.x = (vid_conwidth - panel_size.x) / 2;
1916                 panel_pos.y = (vid_conheight - panel_size.y) / 2;
1917
1918                 string panel_bg;
1919                 panel_bg = strcat(hud_skin_path, "/border_default"); // always use the default border when maximized
1920                 if(precache_pic(panel_bg) == "")
1921                         panel_bg = "gfx/hud/default/border_default"; // fallback
1922                 if(!radar_panel_modified && panel_bg != panel.current_panel_bg)
1923                         radar_panel_modified = true;
1924                 if(panel.current_panel_bg)
1925                         strunzone(panel.current_panel_bg);
1926                 panel.current_panel_bg = strzone(panel_bg);
1927
1928                 switch(hud_panel_radar_maximized_zoommode)
1929                 {
1930                         default:
1931                         case 0:
1932                                 f = current_zoomfraction;
1933                                 break;
1934                         case 1:
1935                                 f = 1 - current_zoomfraction;
1936                                 break;
1937                         case 2:
1938                                 f = 0;
1939                                 break;
1940                         case 3:
1941                                 f = 1;
1942                                 break;
1943                 }
1944
1945                 switch(hud_panel_radar_maximized_rotation)
1946                 {
1947                         case 0:
1948                                 teamradar_angle = view_angles.y - 90;
1949                                 break;
1950                         default:
1951                                 teamradar_angle = 90 * hud_panel_radar_maximized_rotation;
1952                                 break;
1953                 }
1954         }
1955         if (!hud_panel_radar_maximized && !autocvar__hud_configure)
1956         {
1957                 switch(hud_panel_radar_zoommode)
1958                 {
1959                         default:
1960                         case 0:
1961                                 f = current_zoomfraction;
1962                                 break;
1963                         case 1:
1964                                 f = 1 - current_zoomfraction;
1965                                 break;
1966                         case 2:
1967                                 f = 0;
1968                                 break;
1969                         case 3:
1970                                 f = 1;
1971                                 break;
1972                 }
1973
1974                 switch(hud_panel_radar_rotation)
1975                 {
1976                         case 0:
1977                                 teamradar_angle = view_angles.y - 90;
1978                                 break;
1979                         default:
1980                                 teamradar_angle = 90 * hud_panel_radar_rotation;
1981                                 break;
1982                 }
1983         }
1984
1985         vector pos, mySize;
1986         pos = panel_pos;
1987         mySize = panel_size;
1988
1989         HUD_Panel_DrawBg(1);
1990         if(panel_bg_padding)
1991         {
1992                 pos += '1 1 0' * panel_bg_padding;
1993                 mySize -= '2 2 0' * panel_bg_padding;
1994         }
1995
1996         float color2;
1997         entity tm;
1998         float scale2d, normalsize, bigsize;
1999
2000         teamradar_origin2d = pos + 0.5 * mySize;
2001         teamradar_size2d = mySize;
2002
2003         if(minimapname == "")
2004                 return;
2005
2006         teamradar_loadcvars();
2007
2008         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
2009         teamradar_size2d = mySize;
2010
2011         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
2012
2013         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
2014         if((hud_panel_radar_rotation == 0 && !hud_panel_radar_maximized) || (hud_panel_radar_maximized_rotation == 0 && hud_panel_radar_maximized))
2015         {
2016                 // max-min distance must fit the radar in any rotation
2017                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_scale));
2018         }
2019         else
2020         {
2021                 vector c0, c1, c2, c3, span;
2022                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
2023                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
2024                 c2 = rotate('1 0 0' * mi_min.x + '0 1 0' * mi_max.y, teamradar_angle * DEG2RAD);
2025                 c3 = rotate('1 0 0' * mi_max.x + '0 1 0' * mi_min.y, teamradar_angle * DEG2RAD);
2026                 span = '0 0 0';
2027                 span.x = max(c0_x, c1_x, c2_x, c3_x) - min(c0_x, c1_x, c2_x, c3_x);
2028                 span.y = max(c0_y, c1_y, c2_y, c3_y) - min(c0_y, c1_y, c2_y, c3_y);
2029
2030                 // max-min distance must fit the radar in x=x, y=y
2031                 bigsize = min(
2032                         teamradar_size2d.x * scale2d / (1.05 * span.x),
2033                         teamradar_size2d.y * scale2d / (1.05 * span.y)
2034                 );
2035         }
2036
2037         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
2038         if(bigsize > normalsize)
2039                 normalsize = bigsize;
2040
2041         teamradar_size =
2042                   f * bigsize
2043                 + (1 - f) * normalsize;
2044         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
2045                   f * mi_center
2046                 + (1 - f) * view_origin);
2047
2048         drawsetcliparea(
2049                 pos.x,
2050                 pos.y,
2051                 mySize.x,
2052                 mySize.y
2053         );
2054
2055         draw_teamradar_background(hud_panel_radar_foreground_alpha);
2056
2057         for(tm = world; (tm = find(tm, classname, "radarlink")); )
2058                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
2059         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
2060                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, panel_fg_alpha);
2061         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
2062         {
2063                 color2 = GetPlayerColor(tm.sv_entnum);
2064                 //if(color == NUM_SPECTATOR || color == color2)
2065                         draw_teamradar_player(tm.origin, tm.angles, Team_ColorRGB(color2));
2066         }
2067         draw_teamradar_player(view_origin, view_angles, '1 1 1');
2068
2069         drawresetcliparea();
2070 }
2071
2072 // Score (#7)
2073 //
2074 void HUD_UpdatePlayerTeams();
2075 void HUD_Score_Rankings(vector pos, vector mySize, entity me)
2076 {
2077         float score;
2078         entity tm = world, pl;
2079         float SCOREPANEL_MAX_ENTRIES = 6;
2080         float SCOREPANEL_ASPECTRATIO = 2;
2081         float entries = bound(1, floor(SCOREPANEL_MAX_ENTRIES * mySize.y/mySize.x * SCOREPANEL_ASPECTRATIO), SCOREPANEL_MAX_ENTRIES);
2082         vector fontsize = '1 1 0' * (mySize.y/entries);
2083
2084         vector rgb, score_color;
2085         rgb = '1 1 1';
2086         score_color = '1 1 1';
2087
2088         float name_size = mySize.x*0.75;
2089         float spacing_size = mySize.x*0.04;
2090         const float highlight_alpha = 0.2;
2091         float i = 0, me_printed = 0, first_pl = 0;
2092         string s;
2093         if (autocvar__hud_configure)
2094         {
2095                 float players_per_team = 0;
2096                 if (team_count)
2097                 {
2098                         // show team scores in the first line
2099                         float score_size = mySize.x / team_count;
2100                         players_per_team = max(2, ceil((entries - 1) / team_count));
2101                         for(i=0; i<team_count; ++i) {
2102                                 if (i == floor((entries - 2) / players_per_team) || (entries == 1 && i == 0))
2103                                         HUD_Panel_DrawHighlight(pos + eX * score_size * i, eX * score_size + eY * fontsize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2104                                 drawstring_aspect(pos + eX * score_size * i, ftos(175 - 23*i), eX * score_size + eY * fontsize.y, Team_ColorRGB(ColorByTeam(i)) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2105                         }
2106                         first_pl = 1;
2107                         pos.y += fontsize.y;
2108                 }
2109                 score = 10 + SCOREPANEL_MAX_ENTRIES * 3;
2110                 for (i=first_pl; i<entries; ++i)
2111                 {
2112                         //simulate my score is lower than all displayed players,
2113                         //so that I don't appear at all showing pure rankings.
2114                         //This is to better show the difference between the 2 ranking views
2115                         if (i == entries-1 && autocvar_hud_panel_score_rankings == 1)
2116                         {
2117                                 rgb = '1 1 0';
2118                                 drawfill(pos, eX * mySize.x + eY * fontsize.y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2119                                 s = GetPlayerName(player_localnum);
2120                                 score = 7;
2121                         }
2122                         else
2123                         {
2124                                 s = sprintf(_("Player %d"), i + 1 - first_pl);
2125                                 score -= 3;
2126                         }
2127
2128                         if (team_count)
2129                                 score_color = Team_ColorRGB(ColorByTeam(floor((i - first_pl) / players_per_team))) * 0.8;
2130                         s = textShortenToWidth(s, name_size, fontsize, stringwidth_colors);
2131                         drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, true, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
2132                         drawstring(pos + eX * (name_size + spacing_size), ftos(score), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2133                         pos.y += fontsize.y;
2134                 }
2135                 return;
2136         }
2137
2138         if (!scoreboard_fade_alpha) // the scoreboard too calls HUD_UpdatePlayerTeams
2139                 HUD_UpdatePlayerTeams();
2140         if (team_count)
2141         {
2142                 // show team scores in the first line
2143                 float score_size = mySize.x / team_count;
2144                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2145                         if(tm.team == NUM_SPECTATOR)
2146                                 continue;
2147                         if (tm.team == myteam)
2148                                 drawfill(pos + eX * score_size * i, eX * score_size + eY * fontsize.y, '1 1 1', highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2149                         drawstring_aspect(pos + eX * score_size * i, ftos(tm.(teamscores[ts_primary])), eX * score_size + eY * fontsize.y, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2150                         ++i;
2151                 }
2152                 first_pl = 1;
2153                 pos.y += fontsize.y;
2154                 tm = teams.sort_next;
2155         }
2156         i = first_pl;
2157
2158         do
2159         for (pl = players.sort_next; pl && i<entries; pl = pl.sort_next)
2160         {
2161                 if ((team_count && pl.team != tm.team) || pl.team == NUM_SPECTATOR)
2162                         continue;
2163
2164                 if (i == entries-1 && !me_printed && pl != me)
2165                 if (autocvar_hud_panel_score_rankings == 1 && spectatee_status != -1)
2166                 {
2167                         for (pl = me.sort_next; pl; pl = pl.sort_next)
2168                                 if (pl.team != NUM_SPECTATOR)
2169                                         break;
2170
2171                         if (pl)
2172                                 rgb = '1 1 0'; //not last but not among the leading players: yellow
2173                         else
2174                                 rgb = '1 0 0'; //last: red
2175                         pl = me;
2176                 }
2177
2178                 if (pl == me)
2179                 {
2180                         if (i == first_pl)
2181                                 rgb = '0 1 0'; //first: green
2182                         me_printed = 1;
2183                         drawfill(pos, eX * mySize.x + eY * fontsize.y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2184                 }
2185                 if (team_count)
2186                         score_color = Team_ColorRGB(pl.team) * 0.8;
2187                 s = textShortenToWidth(GetPlayerName(pl.sv_entnum), name_size, fontsize, stringwidth_colors);
2188                 drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, true, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
2189                 drawstring(pos + eX * (name_size + spacing_size), ftos(pl.(scores[ps_primary])), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2190                 pos.y += fontsize.y;
2191                 ++i;
2192         }
2193         while (i<entries && team_count && (tm = tm.sort_next) && (tm.team != NUM_SPECTATOR || (tm = tm.sort_next)));
2194 }
2195
2196 void HUD_Score(void)
2197 {
2198         if(!autocvar__hud_configure)
2199         {
2200                 if(!autocvar_hud_panel_score) return;
2201                 if(spectatee_status == -1 && (gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS)) return;
2202         }
2203
2204         HUD_Panel_UpdateCvars();
2205         vector pos, mySize;
2206         pos = panel_pos;
2207         mySize = panel_size;
2208
2209         HUD_Panel_DrawBg(1);
2210         if(panel_bg_padding)
2211         {
2212                 pos += '1 1 0' * panel_bg_padding;
2213                 mySize -= '2 2 0' * panel_bg_padding;
2214         }
2215
2216         float score, distribution = 0;
2217         string sign;
2218         vector distribution_color;
2219         entity tm, pl, me;
2220
2221         me = playerslots[player_localentnum - 1];
2222
2223         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
2224                 string timer, distrtimer;
2225
2226                 pl = players.sort_next;
2227                 if(pl == me)
2228                         pl = pl.sort_next;
2229                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
2230                         if(pl.scores[ps_primary] == 0)
2231                                 pl = world;
2232
2233                 score = me.(scores[ps_primary]);
2234                 timer = TIME_ENCODED_TOSTRING(score);
2235
2236                 draw_beginBoldFont();
2237                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
2238                         // distribution display
2239                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2240
2241                         distrtimer = ftos_decimals(fabs(distribution/pow(10, TIME_DECIMALS)), TIME_DECIMALS);
2242
2243                         if (distribution <= 0) {
2244                                 distribution_color = '0 1 0';
2245                                 sign = "-";
2246                         }
2247                         else {
2248                                 distribution_color = '1 0 0';
2249                                 sign = "+";
2250                         }
2251                         drawstring_aspect(pos + eX * 0.75 * mySize.x, strcat(sign, distrtimer), eX * 0.25 * mySize.x + eY * (1/3) * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2252                 }
2253                 // race record display
2254                 if (distribution <= 0)
2255                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2256                 drawstring_aspect(pos, timer, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2257                 draw_endBoldFont();
2258         } else if (!teamplay) { // non-teamgames
2259                 if ((spectatee_status == -1 && !autocvar__hud_configure) || autocvar_hud_panel_score_rankings)
2260                 {
2261                         HUD_Score_Rankings(pos, mySize, me);
2262                         return;
2263                 }
2264                 // me vector := [team/connected frags id]
2265                 pl = players.sort_next;
2266                 if(pl == me)
2267                         pl = pl.sort_next;
2268
2269                 if(autocvar__hud_configure)
2270                         distribution = 42;
2271                 else if(pl)
2272                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2273                 else
2274                         distribution = 0;
2275
2276                 score = me.(scores[ps_primary]);
2277                 if(autocvar__hud_configure)
2278                         score = 123;
2279
2280                 if(distribution >= 5)
2281                         distribution_color = eY;
2282                 else if(distribution >= 0)
2283                         distribution_color = '1 1 1';
2284                 else if(distribution >= -5)
2285                         distribution_color = '1 1 0';
2286                 else
2287                         distribution_color = eX;
2288
2289                 string distribution_str;
2290                 distribution_str = ftos(distribution);
2291                 draw_beginBoldFont();
2292                 if (distribution >= 0)
2293                 {
2294                         if (distribution > 0)
2295                                 distribution_str = strcat("+", distribution_str);
2296                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2297                 }
2298                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize.x + eY * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2299                 drawstring_aspect(pos + eX * 0.75 * mySize.x, distribution_str, eX * 0.25 * mySize.x + eY * (1/3) * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2300                 draw_endBoldFont();
2301         } else { // teamgames
2302                 float row, column, rows = 0, columns = 0;
2303                 vector offset = '0 0 0';
2304                 vector score_pos, score_size; //for scores other than myteam
2305                 if(autocvar_hud_panel_score_rankings)
2306                 {
2307                         HUD_Score_Rankings(pos, mySize, me);
2308                         return;
2309                 }
2310                 if(spectatee_status == -1)
2311                 {
2312                         rows = mySize.y/mySize.x;
2313                         rows = bound(1, floor((sqrt(4 * (3/1) * rows * team_count + rows * rows) + rows + 0.5) / 2), team_count);
2314                         //                               ^^^ ammo item aspect goes here
2315
2316                         columns = ceil(team_count/rows);
2317
2318                         score_size = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
2319
2320                         float newSize;
2321                         if(score_size.x/score_size.y > 3)
2322                         {
2323                                 newSize = 3 * score_size.y;
2324                                 offset.x = score_size.x - newSize;
2325                                 pos.x += offset.x/2;
2326                                 score_size.x = newSize;
2327                         }
2328                         else
2329                         {
2330                                 newSize = 1/3 * score_size.x;
2331                                 offset.y = score_size.y - newSize;
2332                                 pos.y += offset.y/2;
2333                                 score_size.y = newSize;
2334                         }
2335                 }
2336                 else
2337                         score_size = eX * mySize.x*(1/4) + eY * mySize.y*(1/3);
2338
2339                 float max_fragcount;
2340                 max_fragcount = -99;
2341                 draw_beginBoldFont();
2342                 row = column = 0;
2343                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2344                         if(tm.team == NUM_SPECTATOR)
2345                                 continue;
2346                         score = tm.(teamscores[ts_primary]);
2347                         if(autocvar__hud_configure)
2348                                 score = 123;
2349
2350                         if (score > max_fragcount)
2351                                 max_fragcount = score;
2352
2353                         if (spectatee_status == -1)
2354                         {
2355                                 score_pos = pos + eX * column * (score_size.x + offset.x) + eY * row * (score_size.y + offset.y);
2356                                 if (max_fragcount == score)
2357                                         HUD_Panel_DrawHighlight(score_pos, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2358                                 drawstring_aspect(score_pos, ftos(score), score_size, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2359                                 ++row;
2360                                 if(row >= rows)
2361                                 {
2362                                         row = 0;
2363                                         ++column;
2364                                 }
2365                         }
2366                         else if(tm.team == myteam) {
2367                                 if (max_fragcount == score)
2368                                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2369                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize.x + eY * mySize.y, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2370                         } else {
2371                                 if (max_fragcount == score)
2372                                         HUD_Panel_DrawHighlight(pos + eX * 0.75 * mySize.x + eY * (1/3) * rows * mySize.y, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2373                                 drawstring_aspect(pos + eX * 0.75 * mySize.x + eY * (1/3) * rows * mySize.y, ftos(score), score_size, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2374                                 ++rows;
2375                         }
2376                 }
2377                 draw_endBoldFont();
2378         }
2379 }
2380
2381 // Race timer (#8)
2382 //
2383 void HUD_RaceTimer (void)
2384 {
2385         if(!autocvar__hud_configure)
2386         {
2387                 if(!autocvar_hud_panel_racetimer) return;
2388                 if(!(gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS)) return;
2389                 if(spectatee_status == -1) return;
2390         }
2391
2392         HUD_Panel_UpdateCvars();
2393
2394         draw_beginBoldFont();
2395
2396         vector pos, mySize;
2397         pos = panel_pos;
2398         mySize = panel_size;
2399
2400         HUD_Panel_DrawBg(1);
2401         if(panel_bg_padding)
2402         {
2403                 pos += '1 1 0' * panel_bg_padding;
2404                 mySize -= '2 2 0' * panel_bg_padding;
2405         }
2406
2407         // always force 4:1 aspect
2408         vector newSize = '0 0 0';
2409         if(mySize.x/mySize.y > 4)
2410         {
2411                 newSize.x = 4 * mySize.y;
2412                 newSize.y = mySize.y;
2413
2414                 pos.x = pos.x + (mySize.x - newSize.x) / 2;
2415         }
2416         else
2417         {
2418                 newSize.y = 1/4 * mySize.x;
2419                 newSize.x = mySize.x;
2420
2421                 pos.y = pos.y + (mySize.y - newSize.y) / 2;
2422         }
2423         mySize = newSize;
2424
2425         float a, t;
2426         string s, forcetime;
2427
2428         if(autocvar__hud_configure)
2429         {
2430                 s = "0:13:37";
2431                 drawstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, false, '0.60 0.60 0' * mySize.y), s, '0.60 0.60 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2432                 s = _("^1Intermediate 1 (+15.42)");
2433                 drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.20 * mySize.y) + eY * 0.60 * mySize.y, s, '1 1 0' * 0.20 * mySize.y, panel_fg_alpha, DRAWFLAG_NORMAL);
2434                 s = sprintf(_("^1PENALTY: %.1f (%s)"), 2, "missing a checkpoint");
2435                 drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.20 * mySize.y) + eY * 0.80 * mySize.y, s, '1 1 0' * 0.20 * mySize.y, panel_fg_alpha, DRAWFLAG_NORMAL);
2436         }
2437         else if(race_checkpointtime)
2438         {
2439                 a = bound(0, 2 - (time - race_checkpointtime), 1);
2440                 s = "";
2441                 forcetime = "";
2442                 if(a > 0) // just hit a checkpoint?
2443                 {
2444                         if(race_checkpoint != 254)
2445                         {
2446                                 if(race_time && race_previousbesttime)
2447                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
2448                                 else
2449                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
2450                                 if(race_time)
2451                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
2452                         }
2453                 }
2454                 else
2455                 {
2456                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
2457                         {
2458                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
2459                                 if(a > 0) // next one?
2460                                 {
2461                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
2462                                 }
2463                         }
2464                 }
2465
2466                 if(s != "" && a > 0)
2467                 {
2468                         drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.6 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2469                 }
2470
2471                 if(race_penaltytime)
2472                 {
2473                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
2474                         if(a > 0)
2475                         {
2476                                 s = sprintf(_("^1PENALTY: %.1f (%s)"), race_penaltytime * 0.1, race_penaltyreason);
2477                                 drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.8 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2478                         }
2479                 }
2480
2481                 if(forcetime != "")
2482                 {
2483                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
2484                         drawstring_expanding(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(forcetime, false, '1 1 0' * 0.6 * mySize.y), forcetime, '1 1 0' * 0.6 * mySize.y, '1 1 1', panel_fg_alpha, 0, a);
2485                 }
2486                 else
2487                         a = 1;
2488
2489                 if(race_laptime && race_checkpoint != 255)
2490                 {
2491                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
2492                         drawstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, false, '0.6 0.6 0' * mySize.y), s, '0.6 0.6 0' * mySize.y, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
2493                 }
2494         }
2495         else
2496         {
2497                 if(race_mycheckpointtime)
2498                 {
2499                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
2500                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -(race_mycheckpointenemy == ""), race_mycheckpointlapsdelta, race_mycheckpointenemy);
2501                         drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.6 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2502                 }
2503                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
2504                 {
2505                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
2506                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -(race_othercheckpointenemy == ""), race_othercheckpointlapsdelta, race_othercheckpointenemy);
2507                         drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.6 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2508                 }
2509
2510                 if(race_penaltytime && !race_penaltyaccumulator)
2511                 {
2512                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
2513                         a = bound(0, (1 + t - time), 1);
2514                         if(a > 0)
2515                         {
2516                                 if(time < t)
2517                                         s = sprintf(_("^1PENALTY: %.1f (%s)"), (t - time) * 0.1, race_penaltyreason);
2518                                 else
2519                                         s = sprintf(_("^2PENALTY: %.1f (%s)"), 0, race_penaltyreason);
2520                                 drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.6 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2521                         }
2522                 }
2523         }
2524
2525         draw_endBoldFont();
2526 }
2527
2528 // Vote window (#9)
2529 //
2530 float vote_yescount;
2531 float vote_nocount;
2532 float vote_needed;
2533 float vote_highlighted; // currently selected vote
2534
2535 float vote_active; // is there an active vote?
2536 float vote_prev; // previous state of vote_active to check for a change
2537 float vote_alpha;
2538 float vote_change; // "time" when vote_active changed
2539
2540 void HUD_Vote(void)
2541 {
2542         if(autocvar_cl_allow_uid2name == -1 && (gametype == MAPINFO_TYPE_CTS || gametype == MAPINFO_TYPE_RACE || (serverflags & SERVERFLAG_PLAYERSTATS)))
2543         {
2544                 vote_active = 1;
2545                 if (autocvar__hud_configure)
2546                 {
2547                         vote_yescount = 0;
2548                         vote_nocount = 0;
2549                         print(_("^1You must answer before entering hud configure mode\n"));
2550                         cvar_set("_hud_configure", "0");
2551                 }
2552                 if(vote_called_vote)
2553                         strunzone(vote_called_vote);
2554                 vote_called_vote = strzone(_("^2Name ^7instead of \"^1Anonymous player^7\" in stats"));
2555                 uid2name_dialog = 1;
2556         }
2557
2558         if(!autocvar__hud_configure)
2559         {
2560                 if(!autocvar_hud_panel_vote) return;
2561
2562                 panel_fg_alpha = autocvar_hud_panel_fg_alpha;
2563                 panel_bg_alpha_str = autocvar_hud_panel_vote_bg_alpha;
2564
2565                 if(panel_bg_alpha_str == "") {
2566                         panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);
2567                 }
2568                 panel_bg_alpha = stof(panel_bg_alpha_str);
2569         }
2570         else
2571         {
2572                 vote_yescount = 3;
2573                 vote_nocount = 2;
2574                 vote_needed = 4;
2575         }
2576
2577         string s;
2578         float a;
2579         if(vote_active != vote_prev) {
2580                 vote_change = time;
2581                 vote_prev = vote_active;
2582         }
2583
2584         if(vote_active || autocvar__hud_configure)
2585                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
2586         else
2587                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
2588
2589         if(!vote_alpha)
2590                 return;
2591
2592         HUD_Panel_UpdateCvars();
2593
2594         if(uid2name_dialog)
2595         {
2596                 panel_pos = eX * 0.3 * vid_conwidth + eY * 0.1 * vid_conheight;
2597                 panel_size = eX * 0.4 * vid_conwidth + eY * 0.3 * vid_conheight;
2598         }
2599
2600     // these must be below above block
2601         vector pos, mySize;
2602         pos = panel_pos;
2603         mySize = panel_size;
2604
2605         a = vote_alpha * (vote_highlighted ? autocvar_hud_panel_vote_alreadyvoted_alpha : 1);
2606         HUD_Panel_DrawBg(a);
2607         a = panel_fg_alpha * a;
2608
2609         if(panel_bg_padding)
2610         {
2611                 pos += '1 1 0' * panel_bg_padding;
2612                 mySize -= '2 2 0' * panel_bg_padding;
2613         }
2614
2615         // always force 3:1 aspect
2616         vector newSize = '0 0 0';
2617         if(mySize.x/mySize.y > 3)
2618         {
2619                 newSize.x = 3 * mySize.y;
2620                 newSize.y = mySize.y;
2621
2622                 pos.x = pos.x + (mySize.x - newSize.x) / 2;
2623         }
2624         else
2625         {
2626                 newSize.y = 1/3 * mySize.x;
2627                 newSize.x = mySize.x;
2628
2629                 pos.y = pos.y + (mySize.y - newSize.y) / 2;
2630         }
2631         mySize = newSize;
2632
2633         s = _("A vote has been called for:");
2634         if(uid2name_dialog)
2635                 s = _("Allow servers to store and display your name?");
2636         drawstring_aspect(pos, s, eX * mySize.x + eY * (2/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2637         s = textShortenToWidth(vote_called_vote, mySize.x, '1 1 0' * mySize.y * (1/8), stringwidth_colors);
2638         if(autocvar__hud_configure)
2639                 s = _("^1Configure the HUD");
2640         drawcolorcodedstring_aspect(pos + eY * (2/8) * mySize.y, s, eX * mySize.x + eY * (1.75/8) * mySize.y, a, DRAWFLAG_NORMAL);
2641
2642         // print the yes/no counts
2643     s = sprintf(_("Yes (%s): %d"), getcommandkey("vyes", "vyes"), vote_yescount);
2644         drawstring_aspect(pos + eY * (4/8) * mySize.y, s, eX * 0.5 * mySize.x + eY * (1.5/8) * mySize.y, '0 1 0', a, DRAWFLAG_NORMAL);
2645     s = sprintf(_("No (%s): %d"), getcommandkey("vno", "vno"), vote_nocount);
2646         drawstring_aspect(pos + eX * 0.5 * mySize.x + eY * (4/8) * mySize.y, s, eX * 0.5 * mySize.x + eY * (1.5/8) * mySize.y, '1 0 0', a, DRAWFLAG_NORMAL);
2647
2648         // draw the progress bar backgrounds
2649         drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_back", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2650
2651         // draw the highlights
2652         if(vote_highlighted == 1) {
2653                 drawsetcliparea(pos.x, pos.y, mySize.x * 0.5, mySize.y);
2654                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_voted", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2655         }
2656         else if(vote_highlighted == -1) {
2657                 drawsetcliparea(pos.x + 0.5 * mySize.x, pos.y, mySize.x * 0.5, mySize.y);
2658                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_voted", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2659         }
2660
2661         // draw the progress bars
2662         if(vote_yescount && vote_needed)
2663         {
2664                 drawsetcliparea(pos.x, pos.y, mySize.x * 0.5 * (vote_yescount/vote_needed), mySize.y);
2665                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_prog", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2666         }
2667
2668         if(vote_nocount && vote_needed)
2669         {
2670                 drawsetcliparea(pos.x + mySize.x - mySize.x * 0.5 * (vote_nocount/vote_needed), pos.y, mySize.x * 0.5, mySize.y);
2671                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_prog", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2672         }
2673
2674         drawresetcliparea();
2675 }
2676
2677 // Mod icons panel (#10)
2678 //
2679
2680 float mod_active; // is there any active mod icon?
2681
2682 void DrawCAItem(vector myPos, vector mySize, float aspect_ratio, int layout, float i)
2683 {
2684         float stat = -1;
2685         string pic = "";
2686         vector color = '0 0 0';
2687         switch(i)
2688         {
2689                 case 0:
2690                         stat = getstati(STAT_REDALIVE);
2691                         pic = "player_red.tga";
2692                         color = '1 0 0';
2693                         break;
2694                 case 1:
2695                         stat = getstati(STAT_BLUEALIVE);
2696                         pic = "player_blue.tga";
2697                         color = '0 0 1';
2698                         break;
2699                 case 2:
2700                         stat = getstati(STAT_YELLOWALIVE);
2701                         pic = "player_yellow.tga";
2702                         color = '1 1 0';
2703                         break;
2704                 default:
2705                 case 3:
2706                         stat = getstati(STAT_PINKALIVE);
2707                         pic = "player_pink.tga";
2708                         color = '1 0 1';
2709                         break;
2710         }
2711
2712         if(mySize.x/mySize.y > aspect_ratio)
2713         {
2714                 i = aspect_ratio * mySize.y;
2715                 myPos.x = myPos.x + (mySize.x - i) / 2;
2716                 mySize.x = i;
2717         }
2718         else
2719         {
2720                 i = 1/aspect_ratio * mySize.x;
2721                 myPos.y = myPos.y + (mySize.y - i) / 2;
2722                 mySize.y = i;
2723         }
2724
2725         if(layout)
2726         {
2727                 drawpic_aspect_skin(myPos, pic, eX * 0.7 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2728                 drawstring_aspect(myPos + eX * 0.7 * mySize.x, ftos(stat), eX * 0.3 * mySize.x + eY * mySize.y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
2729         }
2730         else
2731                 drawstring_aspect(myPos, ftos(stat), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
2732 }
2733
2734 // Clan Arena and Freeze Tag HUD modicons
2735 void HUD_Mod_CA(vector myPos, vector mySize)
2736 {
2737         mod_active = 1; // required in each mod function that always shows something
2738
2739         int layout;
2740         if(gametype == MAPINFO_TYPE_CA)
2741                 layout = autocvar_hud_panel_modicons_ca_layout;
2742         else //if(gametype == MAPINFO_TYPE_FREEZETAG)
2743                 layout = autocvar_hud_panel_modicons_freezetag_layout;
2744         float rows, columns, aspect_ratio;
2745         rows = mySize.y/mySize.x;
2746         aspect_ratio = (layout) ? 2 : 1;
2747         rows = bound(1, floor((sqrt((4 * aspect_ratio * team_count + rows) * rows) + rows + 0.5) / 2), team_count);
2748         columns = ceil(team_count/rows);
2749
2750         int i;
2751         float row = 0, column = 0;
2752         vector pos, itemSize;
2753         itemSize = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
2754         for(i=0; i<team_count; ++i)
2755         {
2756                 pos = myPos + eX * column * itemSize.x + eY * row * itemSize.y;
2757
2758                 DrawCAItem(pos, itemSize, aspect_ratio, layout, i);
2759
2760                 ++row;
2761                 if(row >= rows)
2762                 {
2763                         row = 0;
2764                         ++column;
2765                 }
2766         }
2767 }
2768
2769 // CTF HUD modicon section
2770 float redflag_prevframe, blueflag_prevframe; // status during previous frame
2771 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
2772 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
2773
2774 void HUD_Mod_CTF_Reset(void)
2775 {
2776         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
2777 }
2778
2779 void HUD_Mod_CTF(vector pos, vector mySize)
2780 {
2781         vector redflag_pos, blueflag_pos;
2782         vector flag_size;
2783         float f; // every function should have that
2784
2785         float redflag, blueflag; // current status
2786         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
2787         float stat_items;
2788
2789         stat_items = getstati(STAT_ITEMS, 0, 24);
2790         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
2791         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
2792
2793         if(redflag || blueflag)
2794                 mod_active = 1;
2795         else
2796                 mod_active = 0;
2797
2798         if(autocvar__hud_configure)
2799         {
2800                 redflag = 1;
2801                 blueflag = 2;
2802         }
2803
2804         // when status CHANGES, set old status into prevstatus and current status into status
2805         if (redflag != redflag_prevframe)
2806         {
2807                 redflag_statuschange_time = time;
2808                 redflag_prevstatus = redflag_prevframe;
2809                 redflag_prevframe = redflag;
2810         }
2811
2812         if (blueflag != blueflag_prevframe)
2813         {
2814                 blueflag_statuschange_time = time;
2815                 blueflag_prevstatus = blueflag_prevframe;
2816                 blueflag_prevframe = blueflag;
2817         }
2818
2819         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
2820         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
2821
2822         float BLINK_FACTOR = 0.15;
2823         float BLINK_BASE = 0.85;
2824         // note:
2825         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
2826         // thus
2827         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
2828         // ensure RMS == 1
2829         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
2830
2831         string red_icon, red_icon_prevstatus;
2832         float red_alpha, red_alpha_prevstatus;
2833         red_alpha = red_alpha_prevstatus = 1;
2834         switch(redflag) {
2835                 case 1: red_icon = "flag_red_taken"; break;
2836                 case 2: red_icon = "flag_red_lost"; break;
2837                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2838                 default:
2839                         if((stat_items & IT_CTF_SHIELDED) && (myteam == NUM_TEAM_2))
2840                                 red_icon = "flag_red_shielded";
2841                         else
2842                                 red_icon = string_null;
2843                         break;
2844         }
2845         switch(redflag_prevstatus) {
2846                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
2847                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
2848                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2849                 default:
2850                         if(redflag == 3)
2851                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
2852                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == NUM_TEAM_2))
2853                                 red_icon_prevstatus = "flag_red_shielded";
2854                         else
2855                                 red_icon_prevstatus = string_null;
2856                         break;
2857         }
2858
2859         string blue_icon, blue_icon_prevstatus;
2860         float blue_alpha, blue_alpha_prevstatus;
2861         blue_alpha = blue_alpha_prevstatus = 1;
2862         switch(blueflag) {
2863                 case 1: blue_icon = "flag_blue_taken"; break;
2864                 case 2: blue_icon = "flag_blue_lost"; break;
2865                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2866                 default:
2867                         if((stat_items & IT_CTF_SHIELDED) && (myteam == NUM_TEAM_1))
2868                                 blue_icon = "flag_blue_shielded";
2869                         else
2870                                 blue_icon = string_null;
2871                         break;
2872         }
2873         switch(blueflag_prevstatus) {
2874                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
2875                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
2876                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
2877                 default:
2878                         if(blueflag == 3)