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