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