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