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