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