Nades: rename registry globals
[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 "../server/mutators/gamemode_ctf.qh"
20
21 #include "../common/stats.qh"
22
23 #include "../csqcmodellib/cl_player.qh"
24
25
26 /*
27 ==================
28 Misc HUD functions
29 ==================
30 */
31
32 vector HUD_Get_Num_Color (float x, float maxvalue)
33 {
34         float blinkingamt;
35         vector color;
36         if(x >= maxvalue) {
37                 color.x = sin(2*M_PI*time);
38                 color.y = 1;
39                 color.z = sin(2*M_PI*time);
40         }
41         else if(x > maxvalue * 0.75) {
42                 color.x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
43                 color.y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
44                 color.z = 0;
45         }
46         else if(x > maxvalue * 0.5) {
47                 color.x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
48                 color.y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
49                 color.z = 1 - (x-100)*0.02; // blue value between 1 -> 0
50         }
51         else if(x > maxvalue * 0.25) {
52                 color.x = 1;
53                 color.y = 1;
54                 color.z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
55         }
56         else if(x > maxvalue * 0.1) {
57                 color.x = 1;
58                 color.y = (x-20)*90/27/100; // green value between 0 -> 1
59                 color.z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
60         }
61         else {
62                 color.x = 1;
63                 color.y = 0;
64                 color.z = 0;
65         }
66
67         blinkingamt = (1 - x/maxvalue/0.25);
68         if(blinkingamt > 0)
69         {
70                 color.x = color.x - color.x * blinkingamt * sin(2*M_PI*time);
71                 color.y = color.y - color.y * blinkingamt * sin(2*M_PI*time);
72                 color.z = color.z - color.z * blinkingamt * sin(2*M_PI*time);
73         }
74         return color;
75 }
76
77 float HUD_GetRowCount(int item_count, vector size, float item_aspect)
78 {
79         float aspect = size_y / size_x;
80         return bound(1, floor((sqrt(4 * item_aspect * aspect * item_count + aspect * aspect) + aspect + 0.5) / 2), item_count);
81 }
82
83 vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspect)
84 {
85         float columns, rows;
86         float ratio, best_ratio = 0;
87         float best_columns = 1, best_rows = 1;
88         bool vertical = (psize.x / psize.y >= item_aspect);
89         if(vertical)
90         {
91                 psize = eX * psize.y + eY * psize.x;
92                 item_aspect = 1 / item_aspect;
93         }
94
95         rows = ceil(sqrt(item_count));
96         columns = ceil(item_count/rows);
97         while(columns >= 1)
98         {
99                 ratio = (psize.x/columns) / (psize.y/rows);
100                 if(ratio > item_aspect)
101                         ratio = item_aspect * item_aspect / ratio;
102
103                 if(ratio <= best_ratio)
104                         break; // ratio starts decreasing by now, skip next configurations
105
106                 best_columns = columns;
107                 best_rows = rows;
108                 best_ratio = ratio;
109
110                 if(columns == 1)
111                         break;
112
113                 --columns;
114                 rows = ceil(item_count/columns);
115         }
116
117         if(vertical)
118                 return eX * best_rows + eY * best_columns;
119         else
120                 return eX * best_columns + eY * best_rows;
121 }
122
123 // return the string of the onscreen race timer
124 string MakeRaceString(int cp, float mytime, float theirtime, float lapdelta, string theirname)
125 {
126         string col;
127         string timestr;
128         string cpname;
129         string lapstr;
130         lapstr = "";
131
132         if(theirtime == 0) // goal hit
133         {
134                 if(mytime > 0)
135                 {
136                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
137                         col = "^1";
138                 }
139                 else if(mytime == 0)
140                 {
141                         timestr = "+0.0";
142                         col = "^3";
143                 }
144                 else
145                 {
146                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
147                         col = "^2";
148                 }
149
150                 if(lapdelta > 0)
151                 {
152                         lapstr = sprintf(_(" (-%dL)"), lapdelta);
153                         col = "^2";
154                 }
155                 else if(lapdelta < 0)
156                 {
157                         lapstr = sprintf(_(" (+%dL)"), -lapdelta);
158                         col = "^1";
159                 }
160         }
161         else if(theirtime > 0) // anticipation
162         {
163                 if(mytime >= theirtime)
164                         timestr = strcat("+", ftos_decimals(mytime - theirtime, TIME_DECIMALS));
165                 else
166                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(theirtime));
167                 col = "^3";
168         }
169         else
170         {
171                 col = "^7";
172                 timestr = "";
173         }
174
175         if(cp == 254)
176                 cpname = _("Start line");
177         else if(cp == 255)
178                 cpname = _("Finish line");
179         else if(cp)
180                 cpname = sprintf(_("Intermediate %d"), cp);
181         else
182                 cpname = _("Finish line");
183
184         if(theirtime < 0)
185                 return strcat(col, cpname);
186         else if(theirname == "")
187                 return strcat(col, sprintf("%s (%s)", cpname, timestr));
188         else
189                 return strcat(col, sprintf("%s (%s %s)", cpname, timestr, strcat(theirname, col, lapstr)));
190 }
191
192 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
193 int race_CheckName(string net_name)
194 {
195         int i;
196         for (i=RANKINGS_CNT-1;i>=0;--i)
197                 if(grecordholder[i] == net_name)
198                         return i+1;
199         return 0;
200 }
201
202 /*
203 ==================
204 HUD panels
205 ==================
206 */
207
208 // draw the background/borders
209 #define HUD_Panel_DrawBg(theAlpha) do {                                                                                                                                                         \
210         if(panel.current_panel_bg != "0" && panel.current_panel_bg != "")                                                                                               \
211                 draw_BorderPicture(panel_pos - '1 1 0' * panel_bg_border, panel.current_panel_bg, panel_size + '1 1 0' * 2 * panel_bg_border, panel_bg_color, panel_bg_alpha * theAlpha, '1 1 0' * (panel_bg_border/BORDER_MULTIPLIER));\
212 } while(0)
213
214 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
215 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
216 {
217         if(!length_ratio || !theAlpha)
218                 return;
219         if(length_ratio > 1)
220                 length_ratio = 1;
221         if (baralign == 3)
222         {
223                 if(length_ratio < -1)
224                         length_ratio = -1;
225         }
226         else if(length_ratio < 0)
227                 return;
228
229         vector square;
230         vector width, height;
231         if(vertical) {
232                 pic = strcat(hud_skin_path, "/", pic, "_vertical");
233                 if(precache_pic(pic) == "") {
234                         pic = "gfx/hud/default/progressbar_vertical";
235                 }
236
237         if (baralign == 1) // bottom align
238                         theOrigin.y += (1 - length_ratio) * theSize.y;
239         else if (baralign == 2) // center align
240             theOrigin.y += 0.5 * (1 - length_ratio) * theSize.y;
241         else if (baralign == 3) // center align, positive values down, negative up
242                 {
243                         theSize.y *= 0.5;
244                         if (length_ratio > 0)
245                                 theOrigin.y += theSize.y;
246                         else
247                         {
248                                 theOrigin.y += (1 + length_ratio) * theSize.y;
249                                 length_ratio = -length_ratio;
250                         }
251                 }
252                 theSize.y *= length_ratio;
253
254                 vector bH;
255                 width = eX * theSize.x;
256                 height = eY * theSize.y;
257                 if(theSize.y <= theSize.x * 2)
258                 {
259                         // button not high enough
260                         // draw just upper and lower part then
261                         square = eY * theSize.y * 0.5;
262                         bH = eY * (0.25 * theSize.y / (theSize.x * 2));
263                         drawsubpic(theOrigin,          square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, drawflag);
264                         drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, drawflag);
265                 }
266                 else
267                 {
268                         square = eY * theSize.x;
269                         drawsubpic(theOrigin,                   width   +     square, pic, '0 0    0', '1 0.25 0', theColor, theAlpha, drawflag);
270                         drawsubpic(theOrigin +          square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5  0', theColor, theAlpha, drawflag);
271                         drawsubpic(theOrigin + height - square, width   +     square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, drawflag);
272                 }
273         } else {
274                 pic = strcat(hud_skin_path, "/", pic);
275                 if(precache_pic(pic) == "") {
276                         pic = "gfx/hud/default/progressbar";
277                 }
278
279                 if (baralign == 1) // right align
280                         theOrigin.x += (1 - length_ratio) * theSize.x;
281         else if (baralign == 2) // center align
282             theOrigin.x += 0.5 * (1 - length_ratio) * theSize.x;
283         else if (baralign == 3) // center align, positive values on the right, negative on the left
284                 {
285                         theSize.x *= 0.5;
286                         if (length_ratio > 0)
287                                 theOrigin.x += theSize.x;
288                         else
289                         {
290                                 theOrigin.x += (1 + length_ratio) * theSize.x;
291                                 length_ratio = -length_ratio;
292                         }
293                 }
294                 theSize.x *= length_ratio;
295
296                 vector bW;
297                 width = eX * theSize.x;
298                 height = eY * theSize.y;
299                 if(theSize.x <= theSize.y * 2)
300                 {
301                         // button not wide enough
302                         // draw just left and right part then
303                         square = eX * theSize.x * 0.5;
304                         bW = eX * (0.25 * theSize.x / (theSize.y * 2));
305                         drawsubpic(theOrigin,          square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, drawflag);
306                         drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, drawflag);
307                 }
308                 else
309                 {
310                         square = eX * theSize.y;
311                         drawsubpic(theOrigin,                  height  +     square, pic, '0    0 0', '0.25 1 0', theColor, theAlpha, drawflag);
312                         drawsubpic(theOrigin +         square, theSize - 2 * square, pic, '0.25 0 0', '0.5  1 0', theColor, theAlpha, drawflag);
313                         drawsubpic(theOrigin + width - square, height  +     square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
314                 }
315         }
316 }
317
318 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag)
319 {
320         if(!theAlpha)
321                 return;
322
323         string pic;
324         pic = strcat(hud_skin_path, "/num_leading");
325         if(precache_pic(pic) == "") {
326                 pic = "gfx/hud/default/num_leading";
327         }
328
329         drawsubpic(pos, eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0 0 0', '0.25 1 0', color, theAlpha, drawflag);
330         if(mySize.x/mySize.y > 2)
331                 drawsubpic(pos + eX * mySize.y, eX * (mySize.x - 2 * mySize.y) + eY * mySize.y, pic, '0.25 0 0', '0.5 1 0', color, theAlpha, drawflag);
332         drawsubpic(pos + eX * mySize.x - eX * min(mySize.x * 0.5, mySize.y), eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0.75 0 0', '0.25 1 0', color, theAlpha, drawflag);
333 }
334
335 // Weapon icons (#0)
336 //
337 entity weaponorder[Weapons_MAX];
338 void weaponorder_swap(int i, int j, entity pass)
339 {
340         entity h = weaponorder[i];
341         weaponorder[i] = weaponorder[j];
342         weaponorder[j] = h;
343 }
344
345 string weaponorder_cmp_str;
346 int weaponorder_cmp(int i, int j, entity pass)
347 {
348         int ai, aj;
349         ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
350         aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
351         return aj - ai; // the string is in REVERSE order (higher prio at the right is what we want, but higher prio first is the string)
352 }
353
354 void HUD_Weapons(void)
355 {SELFPARAM();
356         // declarations
357         WepSet weapons_stat = WepSet_GetFromStat();
358         int i;
359         float f, a;
360         float screen_ar;
361         vector center = '0 0 0';
362         int weapon_count, weapon_id;
363         int row, column, rows = 0, columns = 0;
364         bool vertical_order = true;
365         float aspect = autocvar_hud_panel_weapons_aspect;
366
367         float timeout = autocvar_hud_panel_weapons_timeout;
368         float timein_effect_length = autocvar_hud_panel_weapons_timeout_speed_in; //? 0.375 : 0);
369         float timeout_effect_length = autocvar_hud_panel_weapons_timeout_speed_out; //? 0.75 : 0);
370
371         vector barsize = '0 0 0', baroffset = '0 0 0';
372         vector ammo_color = '1 0 1';
373         float ammo_alpha = 1;
374
375         float when = max(1, autocvar_hud_panel_weapons_complainbubble_time);
376         float fadetime = max(0, autocvar_hud_panel_weapons_complainbubble_fadetime);
377
378         vector weapon_pos, weapon_size = '0 0 0';
379         vector color;
380
381         // check to see if we want to continue
382         if(intermission == 2) return;
383         if(hud != HUD_NORMAL) return;
384
385         if(!autocvar__hud_configure)
386         {
387                 if((!autocvar_hud_panel_weapons) || (spectatee_status == -1))
388                         return;
389                 if(timeout && time >= weapontime + timeout + timeout_effect_length)
390                 if(autocvar_hud_panel_weapons_timeout_effect == 3 || (autocvar_hud_panel_weapons_timeout_effect == 1 && !(autocvar_hud_panel_weapons_timeout_fadebgmin + autocvar_hud_panel_weapons_timeout_fadefgmin)))
391                 {
392                         weaponprevtime = time;
393                         return;
394                 }
395         }
396
397         // update generic hud functions
398         HUD_Panel_UpdateCvars();
399
400         // figure out weapon order (how the weapons are sorted) // TODO make this configurable
401         if(weaponorder_bypriority != autocvar_cl_weaponpriority || !weaponorder[0])
402         {
403                 int weapon_cnt;
404                 if(weaponorder_bypriority)
405                         strunzone(weaponorder_bypriority);
406                 if(weaponorder_byimpulse)
407                         strunzone(weaponorder_byimpulse);
408
409                 weaponorder_bypriority = strzone(autocvar_cl_weaponpriority);
410                 weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(W_FixWeaponOrder_ForceComplete(W_NumberWeaponOrder(weaponorder_bypriority))));
411                 weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
412
413                 weapon_cnt = 0;
414                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
415                 {
416                         setself(get_weaponinfo(i));
417                         if(self.impulse >= 0)
418                         {
419                                 weaponorder[weapon_cnt] = self;
420                                 ++weapon_cnt;
421                         }
422                 }
423                 for(i = weapon_cnt; i < Weapons_MAX; ++i)
424                         weaponorder[i] = world;
425                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
426
427                 weaponorder_cmp_str = string_null;
428         }
429
430         if(!autocvar_hud_panel_weapons_complainbubble || autocvar__hud_configure || time - complain_weapon_time >= when + fadetime)
431                 complain_weapon = 0;
432
433         if(autocvar__hud_configure)
434         {
435                 if(!weapons_stat)
436                         for(i = WEP_FIRST; i <= WEP_LAST; i += floor((WEP_LAST-WEP_FIRST)/5))
437                                 weapons_stat |= WepSet_FromWeapon(i);
438
439                 #if 0
440                 /// debug code
441                 if(cvar("wep_add"))
442                 {
443                         weapons_stat = '0 0 0';
444                         float countw = 1 + floor((floor(time * cvar("wep_add"))) % (Weapons_COUNT - 1));
445                         for(i = WEP_FIRST; i <= countw; ++i)
446                                 weapons_stat |= WepSet_FromWeapon(i);
447                 }
448                 #endif
449         }
450
451         // determine which weapons are going to be shown
452         if (autocvar_hud_panel_weapons_onlyowned)
453         {
454                 if(autocvar__hud_configure)
455                 {
456                         if(menu_enabled != 2)
457                                 HUD_Panel_DrawBg(1); // also draw the bg of the entire panel
458                 }
459
460                 // do we own this weapon?
461                 weapon_count = 0;
462                 for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
463                         if((weapons_stat & WepSet_FromWeapon(weaponorder[i].weapon)) || (weaponorder[i].weapon == complain_weapon))
464                                 ++weapon_count;
465
466
467                 // might as well commit suicide now, no reason to live ;)
468                 if (weapon_count == 0)
469                         return;
470
471                 vector old_panel_size = panel_size;
472                 vector padded_panel_size = panel_size - '2 2 0' * panel_bg_padding;
473
474                 // get the all-weapons layout
475                 int nHidden = 0;
476                 WepSet weapons_stat = WepSet_GetFromStat();
477                 for (int i = WEP_FIRST; i <= WEP_LAST; ++i) {
478                         WepSet weapons_wep = WepSet_FromWeapon(i);
479                         if (weapons_stat & weapons_wep) continue;
480                         Weapon w = get_weaponinfo(i);
481                         if (w.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1;
482                 }
483                 vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1) - nHidden, padded_panel_size, aspect);
484                 columns = table_size.x;
485                 rows = table_size.y;
486                 weapon_size.x = padded_panel_size.x / columns;
487                 weapon_size.y = padded_panel_size.y / rows;
488
489                 // NOTE: although weapons should aways look the same even if onlyowned is enabled,
490                 // we enlarge them a bit when possible to better match the desired aspect ratio
491                 if(padded_panel_size.x / padded_panel_size.y < aspect)
492                 {
493                         // maximum number of rows that allows to display items with the desired aspect ratio
494                         int max_rows = floor(padded_panel_size.y / (weapon_size.x / aspect));
495                         columns = min(columns, ceil(weapon_count / max_rows));
496                         rows = ceil(weapon_count / columns);
497                         weapon_size.y = min(padded_panel_size.y / rows, weapon_size.x / aspect);
498                         weapon_size.x = min(padded_panel_size.x / columns, aspect * weapon_size.y);
499                         vertical_order = false;
500                 }
501                 else
502                 {
503                         int max_columns = floor(padded_panel_size.x / (weapon_size.y * aspect));
504                         rows = min(rows, ceil(weapon_count / max_columns));
505                         columns = ceil(weapon_count / rows);
506                         weapon_size.x = min(padded_panel_size.x / columns, aspect * weapon_size.y);
507                         weapon_size.y = min(padded_panel_size.y / rows, weapon_size.x / aspect);
508                         vertical_order = true;
509                 }
510
511                 // reduce size of the panel
512                 panel_size.x = columns * weapon_size.x;
513                 panel_size.y = rows * weapon_size.y;
514                 panel_size += '2 2 0' * panel_bg_padding;
515
516                 // center the resized panel, or snap it to the screen edge when close enough
517                 if(panel_pos.x > vid_conwidth * 0.001)
518                 {
519                         if(panel_pos.x + old_panel_size.x > vid_conwidth * 0.999)
520                                 panel_pos.x += old_panel_size.x - panel_size.x;
521                         else
522                                 panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
523                 }
524                 else if(old_panel_size.x > vid_conwidth * 0.999)
525                         panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
526
527                 if(panel_pos.y > vid_conheight * 0.001)
528                 {
529                         if(panel_pos.y + old_panel_size.y > vid_conheight * 0.999)
530                                 panel_pos.y += old_panel_size.y - panel_size.y;
531                         else
532                                 panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
533                 }
534                 else if(old_panel_size.y > vid_conheight * 0.999)
535                         panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
536         }
537         else
538                 weapon_count = (Weapons_COUNT - 1);
539
540         // animation for fading in/out the panel respectively when not in use
541         if(!autocvar__hud_configure)
542         {
543                 if (timeout && time >= weapontime + timeout) // apply timeout effect if needed
544                 {
545                         f = bound(0, (time - (weapontime + timeout)) / timeout_effect_length, 1);
546
547                         // fade the panel alpha
548                         if(autocvar_hud_panel_weapons_timeout_effect == 1)
549                         {
550                                 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * f + (1 - f));
551                                 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * f + (1 - f));
552                         }
553                         else if(autocvar_hud_panel_weapons_timeout_effect == 3)
554                         {
555                                 panel_bg_alpha *= (1 - f);
556                                 panel_fg_alpha *= (1 - f);
557                         }
558
559                         // move the panel off the screen
560                         if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
561                         {
562                                 f *= f; // for a cooler movement
563                                 center.x = panel_pos.x + panel_size.x/2;
564                                 center.y = panel_pos.y + panel_size.y/2;
565                                 screen_ar = vid_conwidth/vid_conheight;
566                                 if (center.x/center.y < screen_ar) //bottom left
567                                 {
568                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
569                                                 panel_pos.y += f * (vid_conheight - panel_pos.y);
570                                         else //left
571                                                 panel_pos.x -= f * (panel_pos.x + panel_size.x);
572                                 }
573                                 else //top right
574                                 {
575                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //right
576                                                 panel_pos.x += f * (vid_conwidth - panel_pos.x);
577                                         else //top
578                                                 panel_pos.y -= f * (panel_pos.y + panel_size.y);
579                                 }
580                                 if(f == 1)
581                                         center.x = -1; // mark the panel as off screen
582                         }
583                         weaponprevtime = time - (1 - f) * timein_effect_length;
584                 }
585                 else if (timeout && time < weaponprevtime + timein_effect_length) // apply timein effect if needed
586                 {
587                         f = bound(0, (time - weaponprevtime) / timein_effect_length, 1);
588
589                         // fade the panel alpha
590                         if(autocvar_hud_panel_weapons_timeout_effect == 1)
591                         {
592                                 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * (1 - f) + f);
593                                 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * (1 - f) + f);
594                         }
595                         else if(autocvar_hud_panel_weapons_timeout_effect == 3)
596                         {
597                                 panel_bg_alpha *= (f);
598                                 panel_fg_alpha *= (f);
599                         }
600
601                         // move the panel back on screen
602                         if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
603                         {
604                                 f *= f; // for a cooler movement
605                                 f = 1 - f;
606                                 center.x = panel_pos.x + panel_size.x/2;
607                                 center.y = panel_pos.y + panel_size.y/2;
608                                 screen_ar = vid_conwidth/vid_conheight;
609                                 if (center.x/center.y < screen_ar) //bottom left
610                                 {
611                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
612                                                 panel_pos.y += f * (vid_conheight - panel_pos.y);
613                                         else //left
614                                                 panel_pos.x -= f * (panel_pos.x + panel_size.x);
615                                 }
616                                 else //top right
617                                 {
618                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //right
619                                                 panel_pos.x += f * (vid_conwidth - panel_pos.x);
620                                         else //top
621                                                 panel_pos.y -= f * (panel_pos.y + panel_size.y);
622                                 }
623                         }
624                 }
625         }
626
627         // draw the background, then change the virtual size of it to better fit other items inside
628         HUD_Panel_DrawBg(1);
629
630         if(center.x == -1)
631                 return;
632
633         if(panel_bg_padding)
634         {
635                 panel_pos += '1 1 0' * panel_bg_padding;
636                 panel_size -= '2 2 0' * panel_bg_padding;
637         }
638
639         // after the sizing and animations are done, update the other values
640
641         if(!rows) // if rows is > 0 onlyowned code has already updated these vars
642         {
643                 vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1), panel_size, aspect);
644                 columns = table_size.x;
645                 rows = table_size.y;
646                 weapon_size.x = panel_size.x / columns;
647                 weapon_size.y = panel_size.y / rows;
648                 vertical_order = (panel_size.x / panel_size.y >= aspect);
649         }
650
651         // calculate position/size for visual bar displaying ammount of ammo status
652         if (autocvar_hud_panel_weapons_ammo)
653         {
654                 ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
655                 ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
656
657                 if(weapon_size.x/weapon_size.y > aspect)
658                 {
659                         barsize.x = aspect * weapon_size.y;
660                         barsize.y = weapon_size.y;
661                         baroffset.x = (weapon_size.x - barsize.x) / 2;
662                 }
663                 else
664                 {
665                         barsize.y = 1/aspect * weapon_size.x;
666                         barsize.x = weapon_size.x;
667                         baroffset.y = (weapon_size.y - barsize.y) / 2;
668                 }
669         }
670         if(autocvar_hud_panel_weapons_accuracy)
671                 Accuracy_LoadColors();
672
673         // draw items
674         row = column = 0;
675         vector label_size = '1 1 0' * min(weapon_size.x, weapon_size.y) * bound(0, autocvar_hud_panel_weapons_label_scale, 1);
676         vector noncurrent_pos = '0 0 0';
677         vector noncurrent_size = weapon_size * bound(0, autocvar_hud_panel_weapons_noncurrent_scale, 1);
678         float noncurrent_alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_weapons_noncurrent_alpha, 1);
679         bool isCurrent;
680
681         for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
682         {
683                 // retrieve information about the current weapon to be drawn
684                 setself(weaponorder[i]);
685                 weapon_id = self.impulse;
686                 isCurrent = (self.weapon == switchweapon);
687
688                 // skip if this weapon doesn't exist
689                 if(!self || weapon_id < 0) { continue; }
690
691                 // skip this weapon if we don't own it (and onlyowned is enabled)-- or if weapons_complainbubble is showing for this weapon
692                 if(autocvar_hud_panel_weapons_onlyowned)
693                 if (!((weapons_stat & WepSet_FromWeapon(self.weapon)) || (self.weapon == complain_weapon)))
694                         continue;
695
696                 // figure out the drawing position of weapon
697                 weapon_pos = (panel_pos + eX * column * weapon_size.x + eY * row * weapon_size.y);
698                 noncurrent_pos.x = weapon_pos.x + (weapon_size.x - noncurrent_size.x) / 2;
699                 noncurrent_pos.y = weapon_pos.y + (weapon_size.y - noncurrent_size.y) / 2;
700
701                 // draw background behind currently selected weapon
702                 if(isCurrent)
703                         drawpic_aspect_skin(weapon_pos, "weapon_current_bg", weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
704
705                 // draw the weapon accuracy
706                 if(autocvar_hud_panel_weapons_accuracy)
707                 {
708                         float panel_weapon_accuracy = weapon_accuracy[self.weapon-WEP_FIRST];
709                         if(panel_weapon_accuracy >= 0)
710                         {
711                                 color = Accuracy_GetColor(panel_weapon_accuracy);
712                                 drawpic_aspect_skin(weapon_pos, "weapon_accuracy", weapon_size, color, panel_fg_alpha, DRAWFLAG_NORMAL);
713                         }
714                 }
715
716                 // drawing all the weapon items
717                 if(weapons_stat & WepSet_FromWeapon(self.weapon))
718                 {
719                         // draw the weapon image
720                         if(isCurrent)
721                                 drawpic_aspect_skin(weapon_pos, self.model2, weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
722                         else
723                                 drawpic_aspect_skin(noncurrent_pos, self.model2, noncurrent_size, '1 1 1', noncurrent_alpha, DRAWFLAG_NORMAL);
724
725                         // draw weapon label string
726                         switch(autocvar_hud_panel_weapons_label)
727                         {
728                                 case 1: // weapon number
729                                         drawstring(weapon_pos, ftos(weapon_id), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
730                                         break;
731
732                                 case 2: // bind
733                                         drawstring(weapon_pos, getcommandkey(ftos(weapon_id), strcat("weapon_group_", ftos(weapon_id))), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
734                                         break;
735
736                                 case 3: // weapon name
737                                         drawstring(weapon_pos, strtolower(self.message), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
738                                         break;
739
740                                 default: // nothing
741                                         break;
742                         }
743
744                         // draw ammo status bar
745                         if(autocvar_hud_panel_weapons_ammo && (self.ammo_field != ammo_none))
746                         {
747                                 float ammo_full;
748                                 a = getstati(GetAmmoStat(self.ammo_field)); // how much ammo do we have?
749
750                                 if(a > 0)
751                                 {
752                                         switch(self.ammo_field)
753                                         {
754                                                 case ammo_shells:  ammo_full = autocvar_hud_panel_weapons_ammo_full_shells;  break;
755                                                 case ammo_nails:   ammo_full = autocvar_hud_panel_weapons_ammo_full_nails;   break;
756                                                 case ammo_rockets: ammo_full = autocvar_hud_panel_weapons_ammo_full_rockets; break;
757                                                 case ammo_cells:   ammo_full = autocvar_hud_panel_weapons_ammo_full_cells;   break;
758                                                 case ammo_plasma:  ammo_full = autocvar_hud_panel_weapons_ammo_full_plasma;  break;
759                                                 case ammo_fuel:    ammo_full = autocvar_hud_panel_weapons_ammo_full_fuel;    break;
760                                                 default: ammo_full = 60;
761                                         }
762
763                                         drawsetcliparea(
764                                                 weapon_pos.x + baroffset.x,
765                                                 weapon_pos.y + baroffset.y,
766                                                 barsize.x * bound(0, a/ammo_full, 1),
767                                                 barsize.y
768                                         );
769
770                                         drawpic_aspect_skin(
771                                                 weapon_pos,
772                                                 "weapon_ammo",
773                                                 weapon_size,
774                                                 ammo_color,
775                                                 ammo_alpha,
776                                                 DRAWFLAG_NORMAL
777                                         );
778
779                                         drawresetcliparea();
780                                 }
781                         }
782                 }
783                 else // draw a "ghost weapon icon" if you don't have the weapon
784                 {
785                         drawpic_aspect_skin(noncurrent_pos, self.model2, noncurrent_size, '0.2 0.2 0.2', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
786                 }
787
788                 // draw the complain message
789                 if(self.weapon == complain_weapon)
790                 {
791                         if(fadetime)
792                                 a = ((complain_weapon_time + when > time) ? 1 : bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1));
793                         else
794                                 a = ((complain_weapon_time + when > time) ? 1 : 0);
795
796                         string s;
797                         if(complain_weapon_type == 0) {
798                                 s = _("Out of ammo");
799                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
800                         }
801                         else if(complain_weapon_type == 1) {
802                                 s = _("Don't have");
803                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
804                         }
805                         else {
806                                 s = _("Unavailable");
807                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
808                         }
809                         float padding = autocvar_hud_panel_weapons_complainbubble_padding;
810                         drawpic_aspect_skin(weapon_pos + '1 1 0' * padding, "weapon_complainbubble", weapon_size - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
811                         drawstring_aspect(weapon_pos + '1 1 0' * padding, s, weapon_size - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
812                 }
813
814                 #if 0
815                 /// debug code
816                 if(!autocvar_hud_panel_weapons_onlyowned)
817                 {
818                         drawfill(weapon_pos + '1 1 0', weapon_size - '2 2 0', '1 1 1', panel_fg_alpha * 0.2, DRAWFLAG_NORMAL);
819                         drawstring(weapon_pos, ftos(i + 1), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
820                 }
821                 #endif
822
823                 // continue with new position for the next weapon
824                 if(vertical_order)
825                 {
826                         ++column;
827                         if(column >= columns)
828                         {
829                                 column = 0;
830                                 ++row;
831                         }
832                 }
833                 else
834                 {
835                         ++row;
836                         if(row >= rows)
837                         {
838                                 row = 0;
839                                 ++column;
840                         }
841                 }
842         }
843 }
844
845 // Ammo (#1)
846 void DrawNadeProgressBar(vector myPos, vector mySize, float progress, vector color)
847 {
848         HUD_Panel_DrawProgressBar(
849                 myPos + eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x,
850                 mySize - eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x,
851                 autocvar_hud_panel_ammo_progressbar_name,
852                 progress, 0, 0, color,
853                 autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
854 }
855
856 void DrawAmmoNades(vector myPos, vector mySize, bool draw_expanding, float expand_time)
857 {
858         float bonusNades    = getstatf(STAT_NADE_BONUS);
859         float bonusProgress = getstatf(STAT_NADE_BONUS_SCORE);
860         float bonusType     = getstati(STAT_NADE_BONUS_TYPE);
861         vector nadeColor    = Nades[bonusType].m_color;
862         string nadeIcon     = Nades[bonusType].m_icon;
863
864         vector iconPos, textPos;
865
866         if(autocvar_hud_panel_ammo_iconalign)
867         {
868                 iconPos = myPos + eX * 2 * mySize.y;
869                 textPos = myPos;
870         }
871         else
872         {
873                 iconPos = myPos;
874                 textPos = myPos + eX * mySize.y;
875         }
876
877         if(bonusNades > 0 || bonusProgress > 0)
878         {
879                 DrawNadeProgressBar(myPos, mySize, bonusProgress, nadeColor);
880
881                 if(autocvar_hud_panel_ammo_text)
882                         drawstring_aspect(textPos, ftos(bonusNades), eX * (2/3) * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
883
884                 if(draw_expanding)
885                         drawpic_aspect_skin_expanding(iconPos, nadeIcon, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, expand_time);
886
887                 drawpic_aspect_skin(iconPos, nadeIcon, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
888         }
889 }
890
891 void DrawAmmoItem(vector myPos, vector mySize, .int ammoType, bool isCurrent, bool isInfinite)
892 {
893         if(ammoType == ammo_none)
894                 return;
895
896         // Initialize variables
897
898         int ammo;
899         if(autocvar__hud_configure)
900         {
901                 isCurrent = (ammoType == ammo_rockets); // Rockets always current
902                 ammo = 60;
903         }
904         else
905                 ammo = getstati(GetAmmoStat(ammoType));
906
907         if(!isCurrent)
908         {
909                 float scale = bound(0, autocvar_hud_panel_ammo_noncurrent_scale, 1);
910                 myPos = myPos + (mySize - mySize * scale) * 0.5;
911                 mySize = mySize * scale;
912         }
913
914         vector iconPos, textPos;
915         if(autocvar_hud_panel_ammo_iconalign)
916         {
917                 iconPos = myPos + eX * 2 * mySize.y;
918                 textPos = myPos;
919         }
920         else
921         {
922                 iconPos = myPos;
923                 textPos = myPos + eX * mySize.y;
924         }
925
926         bool isShadowed = (ammo <= 0 && !isCurrent && !isInfinite);
927
928         vector iconColor = isShadowed ? '0 0 0' : '1 1 1';
929         vector textColor;
930         if(isInfinite)
931                 textColor = '0.2 0.95 0';
932         else if(isShadowed)
933                 textColor = '0 0 0';
934         else if(ammo < 10)
935                 textColor = '0.8 0.04 0';
936         else
937                 textColor = '1 1 1';
938
939         float alpha;
940         if(isCurrent)
941                 alpha = panel_fg_alpha;
942         else if(isShadowed)
943                 alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_ammo_noncurrent_alpha, 1) * 0.5;
944         else
945                 alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_ammo_noncurrent_alpha, 1);
946
947         string text = isInfinite ? "\xE2\x88\x9E" : ftos(ammo); // Use infinity symbol (U+221E)
948
949         // Draw item
950
951         if(isCurrent)
952                 drawpic_aspect_skin(myPos, "ammo_current_bg", mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
953
954         if(ammo > 0 && autocvar_hud_panel_ammo_progressbar)
955                 HUD_Panel_DrawProgressBar(myPos + eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x, mySize - eX * autocvar_hud_panel_ammo_progressbar_xoffset * mySize.x, autocvar_hud_panel_ammo_progressbar_name, ammo/autocvar_hud_panel_ammo_maxammo, 0, 0, textColor, autocvar_hud_progressbar_alpha * alpha, DRAWFLAG_NORMAL);
956
957         if(autocvar_hud_panel_ammo_text)
958                 drawstring_aspect(textPos, text, eX * (2/3) * mySize.x + eY * mySize.y, textColor, alpha, DRAWFLAG_NORMAL);
959
960         drawpic_aspect_skin(iconPos, GetAmmoPicture(ammoType), '1 1 0' * mySize.y, iconColor, alpha, DRAWFLAG_NORMAL);
961 }
962
963 int nade_prevstatus;
964 int nade_prevframe;
965 float nade_statuschange_time;
966 void HUD_Ammo(void)
967 {
968         if(intermission == 2) return;
969         if(hud != HUD_NORMAL) return;
970         if(!autocvar__hud_configure)
971         {
972                 if(!autocvar_hud_panel_ammo) return;
973                 if(spectatee_status == -1) return;
974         }
975
976         HUD_Panel_UpdateCvars();
977
978         draw_beginBoldFont();
979
980         vector pos, mySize;
981         pos = panel_pos;
982         mySize = panel_size;
983
984         HUD_Panel_DrawBg(1);
985         if(panel_bg_padding)
986         {
987                 pos += '1 1 0' * panel_bg_padding;
988                 mySize -= '2 2 0' * panel_bg_padding;
989         }
990
991         int rows = 0, columns, row, column;
992         float nade_cnt = getstatf(STAT_NADE_BONUS), nade_score = getstatf(STAT_NADE_BONUS_SCORE);
993         bool draw_nades = (nade_cnt > 0 || nade_score > 0);
994         float nade_statuschange_elapsedtime;
995         int total_ammo_count;
996
997         vector ammo_size;
998         if (autocvar_hud_panel_ammo_onlycurrent)
999                 total_ammo_count = 1;
1000         else
1001                 total_ammo_count = AMMO_COUNT;
1002
1003         if(draw_nades)
1004         {
1005                 ++total_ammo_count;
1006                 if (nade_cnt != nade_prevframe)
1007                 {
1008                         nade_statuschange_time = time;
1009                         nade_prevstatus = nade_prevframe;
1010                         nade_prevframe = nade_cnt;
1011                 }
1012         }
1013         else
1014                 nade_prevstatus = nade_prevframe = nade_statuschange_time = 0;
1015
1016         rows = HUD_GetRowCount(total_ammo_count, mySize, 3);
1017         columns = ceil((total_ammo_count)/rows);
1018         ammo_size = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
1019
1020         vector offset = '0 0 0'; // fteqcc sucks
1021         float newSize;
1022         if(ammo_size.x/ammo_size.y > 3)
1023         {
1024                 newSize = 3 * ammo_size.y;
1025                 offset.x = ammo_size.x - newSize;
1026                 pos.x += offset.x/2;
1027                 ammo_size.x = newSize;
1028         }
1029         else
1030         {
1031                 newSize = 1/3 * ammo_size.x;
1032                 offset.y = ammo_size.y - newSize;
1033                 pos.y += offset.y/2;
1034                 ammo_size.y = newSize;
1035         }
1036
1037         int i;
1038         bool infinite_ammo = (getstati(STAT_ITEMS, 0, 24) & IT_UNLIMITED_WEAPON_AMMO);
1039         row = column = 0;
1040         if(autocvar_hud_panel_ammo_onlycurrent)
1041         {
1042                 if(autocvar__hud_configure)
1043                 {
1044                         DrawAmmoItem(pos, ammo_size, ammo_rockets, true, false);
1045                 }
1046                 else
1047                 {
1048                         DrawAmmoItem(
1049                                 pos,
1050                                 ammo_size,
1051                                 (get_weaponinfo(switchweapon)).ammo_field,
1052                                 true,
1053                                 infinite_ammo
1054                         );
1055                 }
1056
1057                 ++row;
1058                 if(row >= rows)
1059                 {
1060                         row = 0;
1061                         column = column + 1;
1062                 }
1063         }
1064         else
1065         {
1066                 .int ammotype;
1067                 row = column = 0;
1068                 for(i = 0; i < AMMO_COUNT; ++i)
1069                 {
1070                         ammotype = GetAmmoFieldFromNum(i);
1071                         DrawAmmoItem(
1072                                 pos + eX * column * (ammo_size.x + offset.x) + eY * row * (ammo_size.y + offset.y),
1073                                 ammo_size,
1074                                 ammotype,
1075                                 ((get_weaponinfo(switchweapon)).ammo_field == ammotype),
1076                                 infinite_ammo
1077                         );
1078
1079                         ++row;
1080                         if(row >= rows)
1081                         {
1082                                 row = 0;
1083                                 column = column + 1;
1084                         }
1085                 }
1086         }
1087
1088         if (draw_nades)
1089         {
1090                 nade_statuschange_elapsedtime = time - nade_statuschange_time;
1091
1092                 float f = bound(0, nade_statuschange_elapsedtime*2, 1);
1093
1094                 DrawAmmoNades(pos + eX * column * (ammo_size.x + offset.x) + eY * row * (ammo_size.y + offset.y), ammo_size, nade_prevstatus < nade_cnt && nade_cnt != 0 && f < 1, f);
1095         }
1096
1097         draw_endBoldFont();
1098 }
1099
1100 void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, bool vertical, bool icon_right_align, vector color, float theAlpha, float fadelerp)
1101 {
1102         vector newPos = '0 0 0', newSize = '0 0 0';
1103         vector picpos, numpos;
1104
1105         if (vertical)
1106         {
1107                 if(mySize.y/mySize.x > 2)
1108                 {
1109                         newSize.y = 2 * mySize.x;
1110                         newSize.x = mySize.x;
1111
1112                         newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
1113                         newPos.x = myPos.x;
1114                 }
1115                 else
1116                 {
1117                         newSize.x = 1/2 * mySize.y;
1118                         newSize.y = mySize.y;
1119
1120                         newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
1121                         newPos.y = myPos.y;
1122                 }
1123
1124                 if(icon_right_align)
1125                 {
1126                         numpos = newPos;
1127                         picpos = newPos + eY * newSize.x;
1128                 }
1129                 else
1130                 {
1131                         picpos = newPos;
1132                         numpos = newPos + eY * newSize.x;
1133                 }
1134
1135                 newSize.y /= 2;
1136                 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
1137                 // make number smaller than icon, it looks better
1138                 // reduce only y to draw numbers with different number of digits with the same y size
1139                 numpos.y += newSize.y * ((1 - 0.7) / 2);
1140                 newSize.y *= 0.7;
1141                 drawstring_aspect(numpos, ftos(x), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
1142                 return;
1143         }
1144
1145         if(mySize.x/mySize.y > 3)
1146         {
1147                 newSize.x = 3 * mySize.y;
1148                 newSize.y = mySize.y;
1149
1150                 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
1151                 newPos.y = myPos.y;
1152         }
1153         else
1154         {
1155                 newSize.y = 1/3 * mySize.x;
1156                 newSize.x = mySize.x;
1157
1158                 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
1159                 newPos.x = myPos.x;
1160         }
1161
1162         if(icon_right_align) // right align
1163         {
1164                 numpos = newPos;
1165                 picpos = newPos + eX * 2 * newSize.y;
1166         }
1167         else // left align
1168         {
1169                 numpos = newPos + eX * newSize.y;
1170                 picpos = newPos;
1171         }
1172
1173         // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
1174         // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
1175         drawstring_aspect_expanding(numpos, ftos(x), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
1176         drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
1177 }
1178
1179 void DrawNumIcon(vector myPos, vector mySize, float x, string icon, bool vertical, bool icon_right_align, vector color, float theAlpha)
1180 {
1181         DrawNumIcon_expanding(myPos, mySize, x, icon, vertical, icon_right_align, color, theAlpha, 0);
1182 }
1183
1184 // Powerups (#2)
1185 //
1186
1187 // Powerup item fields (reusing existing fields)
1188 .string message;  // Human readable name
1189 .string netname;  // Icon name
1190 .vector colormod; // Color
1191 .float count;     // Time left
1192 .float lifetime;  // Maximum time
1193
1194 entity powerupItems;
1195 int powerupItemsCount;
1196
1197 void resetPowerupItems()
1198 {
1199         entity item;
1200         for(item = powerupItems; item; item = item.chain)
1201                 item.count = 0;
1202
1203         powerupItemsCount = 0;
1204 }
1205
1206 void addPowerupItem(string name, string icon, vector color, float currentTime, float lifeTime)
1207 {
1208         if(!powerupItems)
1209                 powerupItems = spawn();
1210
1211         entity item;
1212         for(item = powerupItems; item.count; item = item.chain)
1213                 if(!item.chain)
1214                         item.chain = spawn();
1215
1216         item.message  = name;
1217         item.netname  = icon;
1218         item.colormod = color;
1219         item.count    = currentTime;
1220         item.lifetime = lifeTime;
1221
1222         ++powerupItemsCount;
1223 }
1224
1225 int getPowerupItemAlign(int align, int column, int row, int columns, int rows, bool isVertical)
1226 {
1227         if(align < 2)
1228                 return align;
1229
1230         bool isTop    =  isVertical && rows > 1 && row == 0;
1231         bool isBottom =  isVertical && rows > 1 && row == rows-1;
1232         bool isLeft   = !isVertical && columns > 1 && column == 0;
1233         bool isRight  = !isVertical && columns > 1 && column == columns-1;
1234
1235         if(isTop    || isLeft)  return (align == 2) ? 1 : 0;
1236         if(isBottom || isRight) return (align == 2) ? 0 : 1;
1237
1238         return 2;
1239 }
1240
1241 void HUD_Powerups()
1242 {
1243         if(intermission == 2) return;
1244
1245         int allItems = getstati(STAT_ITEMS, 0, 24);
1246         int allBuffs = getstati(STAT_BUFFS, 0, 24);
1247         int strengthTime, shieldTime, superTime;
1248
1249         // Initialize items
1250         if(!autocvar__hud_configure)
1251         {
1252                 if(!autocvar_hud_panel_powerups) return;
1253                 if(spectatee_status == -1) return;
1254                 if(getstati(STAT_HEALTH) <= 0) return;
1255                 if(!(allItems & (ITEM_Strength.m_itemid | ITEM_Shield.m_itemid | IT_SUPERWEAPON)) && !allBuffs) return;
1256
1257                 strengthTime = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
1258                 shieldTime = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
1259                 superTime = bound(0, getstatf(STAT_SUPERWEAPONS_FINISHED) - time, 99);
1260
1261                 if(allItems & IT_UNLIMITED_SUPERWEAPONS)
1262                         superTime = 99;
1263
1264                 // Prevent stuff to show up on mismatch that will be fixed next frame
1265                 if(!(allItems & IT_SUPERWEAPON))
1266                         superTime = 0;
1267         }
1268         else
1269         {
1270                 strengthTime = 15;
1271                 shieldTime = 27;
1272                 superTime = 13;
1273                 allBuffs = 0;
1274         }
1275
1276         // Add items to linked list
1277         resetPowerupItems();
1278
1279         if(strengthTime)
1280                 addPowerupItem("Strength", "strength", autocvar_hud_progressbar_strength_color, strengthTime, 30);
1281         if(shieldTime)
1282                 addPowerupItem("Shield", "shield", autocvar_hud_progressbar_shield_color, shieldTime, 30);
1283         if(superTime)
1284                 addPowerupItem("Superweapons", "superweapons", autocvar_hud_progressbar_superweapons_color, superTime, 30);
1285
1286         FOREACH(Buffs, it.m_itemid & allBuffs, LAMBDA(
1287                 addPowerupItem(it.m_prettyName, strcat("buff_", it.m_name), it.m_color, bound(0, getstatf(STAT_BUFF_TIME) - time, 99), 60);
1288         ));
1289
1290         if(!powerupItemsCount)
1291                 return;
1292
1293         // Draw panel background
1294         HUD_Panel_UpdateCvars();
1295         HUD_Panel_DrawBg(1);
1296
1297         // Set drawing area
1298         vector pos = panel_pos;
1299         vector size = panel_size;
1300         bool isVertical = size.y > size.x;
1301
1302         if(panel_bg_padding)
1303         {
1304                 pos += '1 1 0' * panel_bg_padding;
1305                 size -= '2 2 0' * panel_bg_padding;
1306         }
1307
1308         // Find best partitioning of the drawing area
1309         const float DESIRED_ASPECT = 6;
1310         float aspect = 0, a;
1311         int columns = 0, c;
1312         int rows = 0, r;
1313         int i = 1;
1314
1315         do
1316         {
1317                 c = floor(powerupItemsCount / i);
1318                 r = ceil(powerupItemsCount / c);
1319                 a = isVertical ? (size.y/r) / (size.x/c) : (size.x/c) / (size.y/r);
1320
1321                 if(i == 1 || fabs(DESIRED_ASPECT - a) < fabs(DESIRED_ASPECT - aspect))
1322                 {
1323                         aspect = a;
1324                         columns = c;
1325                         rows = r;
1326                 }
1327         }
1328         while(++i <= powerupItemsCount);
1329
1330         // Prevent single items from getting too wide
1331         if(powerupItemsCount == 1 && aspect > DESIRED_ASPECT)
1332         {
1333                 if(isVertical)
1334                 {
1335                         size.y *= 0.5;
1336                         pos.y += size.y * 0.5;
1337                 }
1338                 else
1339                 {
1340                         size.x *= 0.5;
1341                         pos.x += size.x * 0.5;
1342                 }
1343         }
1344
1345         // Draw items from linked list
1346         vector itemPos = pos;
1347         vector itemSize = eX * (size.x / columns) + eY * (size.y / rows);
1348         vector textColor = '1 1 1';
1349
1350         int fullSeconds = 0;
1351         int align = 0;
1352         int column = 0;
1353         int row = 0;
1354
1355         draw_beginBoldFont();
1356         for(entity item = powerupItems; item.count; item = item.chain)
1357         {
1358                 itemPos = eX * (pos.x + column * itemSize.x) + eY * (pos.y + row * itemSize.y);
1359
1360                 // Draw progressbar
1361                 if(autocvar_hud_panel_powerups_progressbar)
1362                 {
1363                         align = getPowerupItemAlign(autocvar_hud_panel_powerups_baralign, column, row, columns, rows, isVertical);
1364                         HUD_Panel_DrawProgressBar(itemPos, itemSize, "progressbar", item.count / item.lifetime, isVertical, align, item.colormod, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1365                 }
1366
1367                 // Draw icon and text
1368                 if(autocvar_hud_panel_powerups_text)
1369                 {
1370                         align = getPowerupItemAlign(autocvar_hud_panel_powerups_iconalign, column, row, columns, rows, isVertical);
1371                         fullSeconds = ceil(item.count);
1372                         textColor = '0.6 0.6 0.6' + (item.colormod * 0.4);
1373
1374                         if(item.count > 1)
1375                                 DrawNumIcon(itemPos, itemSize, fullSeconds, item.netname, isVertical, align, textColor, panel_fg_alpha);
1376                         if(item.count <= 5)
1377                                 DrawNumIcon_expanding(itemPos, itemSize, fullSeconds, item.netname, isVertical, align, textColor, panel_fg_alpha, bound(0, (fullSeconds - item.count) / 0.5, 1));
1378                 }
1379
1380                 // Determine next section
1381                 if(isVertical)
1382                 {
1383                         if(++column >= columns)
1384                         {
1385                                 column = 0;
1386                                 ++row;
1387                         }
1388                 }
1389                 else
1390                 {
1391                         if(++row >= rows)
1392                         {
1393                                 row = 0;
1394                                 ++column;
1395                         }
1396                 }
1397         }
1398         draw_endBoldFont();
1399 }
1400
1401 // Health/armor (#3)
1402 //
1403
1404
1405 void HUD_HealthArmor(void)
1406 {
1407         int armor, health, fuel;
1408         if(intermission == 2) return;
1409         if(!autocvar__hud_configure)
1410         {
1411                 if(!autocvar_hud_panel_healtharmor) return;
1412                 if(hud != HUD_NORMAL) return;
1413                 if(spectatee_status == -1) return;
1414
1415                 health = getstati(STAT_HEALTH);
1416                 if(health <= 0)
1417                 {
1418                         prev_health = -1;
1419                         return;
1420                 }
1421                 armor = getstati(STAT_ARMOR);
1422
1423                 // code to check for spectatee_status changes is in Ent_ClientData()
1424                 // prev_p_health and prev_health can be set to -1 there
1425
1426                 if (prev_p_health == -1)
1427                 {
1428                         // no effect
1429                         health_beforedamage = 0;
1430                         armor_beforedamage = 0;
1431                         health_damagetime = 0;
1432                         armor_damagetime = 0;
1433                         prev_health = health;
1434                         prev_armor = armor;
1435                         old_p_health = health;
1436                         old_p_armor = armor;
1437                         prev_p_health = health;
1438                         prev_p_armor = armor;
1439                 }
1440                 else if (prev_health == -1)
1441                 {
1442                         //start the load effect
1443                         health_damagetime = 0;
1444                         armor_damagetime = 0;
1445                         prev_health = 0;
1446                         prev_armor = 0;
1447                 }
1448                 fuel = getstati(STAT_FUEL);
1449         }
1450         else
1451         {
1452                 health = 150;
1453                 armor = 75;
1454                 fuel = 20;
1455         }
1456
1457         HUD_Panel_UpdateCvars();
1458
1459         draw_beginBoldFont();
1460
1461         vector pos, mySize;
1462         pos = panel_pos;
1463         mySize = panel_size;
1464
1465         HUD_Panel_DrawBg(1);
1466         if(panel_bg_padding)
1467         {
1468                 pos += '1 1 0' * panel_bg_padding;
1469                 mySize -= '2 2 0' * panel_bg_padding;
1470         }
1471
1472         int baralign = autocvar_hud_panel_healtharmor_baralign;
1473         int iconalign = autocvar_hud_panel_healtharmor_iconalign;
1474
1475     int maxhealth = autocvar_hud_panel_healtharmor_maxhealth;
1476     int maxarmor = autocvar_hud_panel_healtharmor_maxarmor;
1477         if(autocvar_hud_panel_healtharmor == 2) // combined health and armor display
1478         {
1479                 vector v;
1480                 v = healtharmor_maxdamage(health, armor, armorblockpercent, DEATH_WEAPON);
1481
1482                 float x;
1483                 x = floor(v.x + 1);
1484
1485         float maxtotal = maxhealth + maxarmor;
1486                 string biggercount;
1487                 if(v.z) // NOT fully armored
1488                 {
1489                         biggercount = "health";
1490                         if(autocvar_hud_panel_healtharmor_progressbar)
1491                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_health, x/maxtotal, 0, (baralign == 1 || baralign == 2), autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1492                         if(armor)
1493             if(autocvar_hud_panel_healtharmor_text)
1494                                 drawpic_aspect_skin(pos + eX * mySize.x - eX * 0.5 * mySize.y, "armor", '0.5 0.5 0' * mySize.y, '1 1 1', panel_fg_alpha * armor / health, DRAWFLAG_NORMAL);
1495                 }
1496                 else
1497                 {
1498                         biggercount = "armor";
1499                         if(autocvar_hud_panel_healtharmor_progressbar)
1500                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, x/maxtotal, 0, (baralign == 1 || baralign == 2), autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1501                         if(health)
1502             if(autocvar_hud_panel_healtharmor_text)
1503                                 drawpic_aspect_skin(pos + eX * mySize.x - eX * 0.5 * mySize.y, "health", '0.5 0.5 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1504                 }
1505         if(autocvar_hud_panel_healtharmor_text)
1506                         DrawNumIcon(pos, mySize, x, biggercount, 0, iconalign, HUD_Get_Num_Color(x, maxtotal), 1);
1507
1508                 if(fuel)
1509                         HUD_Panel_DrawProgressBar(pos, eX * mySize.x + eY * 0.2 * mySize.y, "progressbar", fuel/100, 0, (baralign == 1 || baralign == 3), autocvar_hud_progressbar_fuel_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
1510         }
1511         else
1512         {
1513                 float panel_ar = mySize.x/mySize.y;
1514                 bool is_vertical = (panel_ar < 1);
1515                 vector health_offset = '0 0 0', armor_offset = '0 0 0';
1516                 if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
1517                 {
1518                         mySize.x *= 0.5;
1519                         if (autocvar_hud_panel_healtharmor_flip)
1520                                 health_offset.x = mySize.x;
1521                         else
1522                                 armor_offset.x = mySize.x;
1523                 }
1524                 else
1525                 {
1526                         mySize.y *= 0.5;
1527                         if (autocvar_hud_panel_healtharmor_flip)
1528                                 health_offset.y = mySize.y;
1529                         else
1530                                 armor_offset.y = mySize.y;
1531                 }
1532
1533                 bool health_baralign, armor_baralign, fuel_baralign;
1534                 bool health_iconalign, armor_iconalign;
1535                 if (autocvar_hud_panel_healtharmor_flip)
1536                 {
1537                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
1538                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
1539                         fuel_baralign = health_baralign;
1540                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
1541                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
1542                 }
1543                 else
1544                 {
1545                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
1546                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
1547                         fuel_baralign = armor_baralign;
1548                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
1549                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
1550                 }
1551
1552                 //if(health)
1553                 {
1554                         if(autocvar_hud_panel_healtharmor_progressbar)
1555                         {
1556                                 float p_health, pain_health_alpha;
1557                                 p_health = health;
1558                                 pain_health_alpha = 1;
1559                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
1560                                 {
1561                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
1562                                         {
1563                                                 if (fabs(prev_health - health) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
1564                                                 {
1565                                                         if (time - old_p_healthtime < 1)
1566                                                                 old_p_health = prev_p_health;
1567                                                         else
1568                                                                 old_p_health = prev_health;
1569                                                         old_p_healthtime = time;
1570                                                 }
1571                                                 if (time - old_p_healthtime < 1)
1572                                                 {
1573                                                         p_health += (old_p_health - health) * (1 - (time - old_p_healthtime));
1574                                                         prev_p_health = p_health;
1575                                                 }
1576                                         }
1577                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
1578                                         {
1579                                                 if (prev_health - health >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
1580                                                 {
1581                                                         if (time - health_damagetime >= 1)
1582                                                                 health_beforedamage = prev_health;
1583                                                         health_damagetime = time;
1584                                                 }
1585                                                 if (time - health_damagetime < 1)
1586                                                 {
1587                                                         float health_damagealpha = 1 - (time - health_damagetime)*(time - health_damagetime);
1588                                                         HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, health_beforedamage/maxhealth, is_vertical, health_baralign, autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * health_damagealpha, DRAWFLAG_NORMAL);
1589                                                 }
1590                                         }
1591                                         prev_health = health;
1592
1593                                         if (health <= autocvar_hud_panel_healtharmor_progressbar_gfx_lowhealth)
1594                                         {
1595                                                 float BLINK_FACTOR = 0.15;
1596                                                 float BLINK_BASE = 0.85;
1597                                                 float BLINK_FREQ = 9;
1598                                                 pain_health_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
1599                                         }
1600                                 }
1601                                 HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, p_health/maxhealth, is_vertical, health_baralign, autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * pain_health_alpha, DRAWFLAG_NORMAL);
1602                         }
1603                         if(autocvar_hud_panel_healtharmor_text)
1604                                 DrawNumIcon(pos + health_offset, mySize, health, "health", is_vertical, health_iconalign, HUD_Get_Num_Color(health, maxhealth), 1);
1605                 }
1606
1607                 if(armor)
1608                 {
1609                         if(autocvar_hud_panel_healtharmor_progressbar)
1610                         {
1611                                 float p_armor;
1612                                 p_armor = armor;
1613                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
1614                                 {
1615                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
1616                                         {
1617                                                 if (fabs(prev_armor - armor) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
1618                                                 {
1619                                                         if (time - old_p_armortime < 1)
1620                                                                 old_p_armor = prev_p_armor;
1621                                                         else
1622                                                                 old_p_armor = prev_armor;
1623                                                         old_p_armortime = time;
1624                                                 }
1625                                                 if (time - old_p_armortime < 1)
1626                                                 {
1627                                                         p_armor += (old_p_armor - armor) * (1 - (time - old_p_armortime));
1628                                                         prev_p_armor = p_armor;
1629                                                 }
1630                                         }
1631                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
1632                                         {
1633                                                 if (prev_armor - armor >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
1634                                                 {
1635                                                         if (time - armor_damagetime >= 1)
1636                                                                 armor_beforedamage = prev_armor;
1637                                                         armor_damagetime = time;
1638                                                 }
1639                                                 if (time - armor_damagetime < 1)
1640                                                 {
1641                                                         float armor_damagealpha = 1 - (time - armor_damagetime)*(time - armor_damagetime);
1642                                                         HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, armor_beforedamage/maxarmor, is_vertical, armor_baralign, autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * armor_damagealpha, DRAWFLAG_NORMAL);
1643                                                 }
1644                                         }
1645                                         prev_armor = armor;
1646                                 }
1647                                 HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, p_armor/maxarmor, is_vertical, armor_baralign, autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1648                         }
1649                         if(autocvar_hud_panel_healtharmor_text)
1650                                 DrawNumIcon(pos + armor_offset, mySize, armor, "armor", is_vertical, armor_iconalign, HUD_Get_Num_Color(armor, maxarmor), 1);
1651                 }
1652
1653                 if(fuel)
1654                 {
1655                         if (is_vertical)
1656                                 mySize.x *= 0.2 / 2; //if vertical always halve x to not cover too much numbers with 3 digits
1657                         else
1658                                 mySize.y *= 0.2;
1659                         if (panel_ar >= 4)
1660                                 mySize.x *= 2; //restore full panel size
1661                         else if (panel_ar < 1/4)
1662                                 mySize.y *= 2; //restore full panel size
1663                         HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", fuel/100, is_vertical, fuel_baralign, autocvar_hud_progressbar_fuel_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
1664                 }
1665         }
1666
1667         draw_endBoldFont();
1668 }
1669
1670 // Notification area (#4)
1671 //
1672
1673 void HUD_Notify_Push(string icon, string attacker, string victim)
1674 {
1675         if (icon == "")
1676                 return;
1677
1678         ++notify_count;
1679         --notify_index;
1680
1681         if (notify_index == -1)
1682                 notify_index = NOTIFY_MAX_ENTRIES-1;
1683
1684         // Free old strings
1685         if (notify_attackers[notify_index])
1686                 strunzone(notify_attackers[notify_index]);
1687
1688         if (notify_victims[notify_index])
1689                 strunzone(notify_victims[notify_index]);
1690
1691         if (notify_icons[notify_index])
1692                 strunzone(notify_icons[notify_index]);
1693
1694         // Allocate new strings
1695         if (victim != "")
1696         {
1697                 notify_attackers[notify_index] = strzone(attacker);
1698                 notify_victims[notify_index] = strzone(victim);
1699         }
1700         else
1701         {
1702                 // In case of a notification without a victim, the attacker
1703                 // is displayed on the victim's side. Instead of special
1704                 // treatment later on, we can simply switch them here.
1705                 notify_attackers[notify_index] = string_null;
1706                 notify_victims[notify_index] = strzone(attacker);
1707         }
1708
1709         notify_icons[notify_index] = strzone(icon);
1710         notify_times[notify_index] = time;
1711 }
1712
1713 void HUD_Notify(void)
1714 {
1715         if(intermission == 2) return;
1716         if (!autocvar__hud_configure)
1717                 if (!autocvar_hud_panel_notify)
1718                         return;
1719
1720         HUD_Panel_UpdateCvars();
1721         HUD_Panel_DrawBg(1);
1722
1723         if (!autocvar__hud_configure)
1724                 if (notify_count == 0)
1725                         return;
1726
1727         vector pos, size;
1728         pos  = panel_pos;
1729         size = panel_size;
1730
1731         if (panel_bg_padding)
1732         {
1733                 pos  += '1 1 0' * panel_bg_padding;
1734                 size -= '2 2 0' * panel_bg_padding;
1735         }
1736
1737         float fade_start = max(0, autocvar_hud_panel_notify_time);
1738         float fade_time = max(0, autocvar_hud_panel_notify_fadetime);
1739         float icon_aspect = max(1, autocvar_hud_panel_notify_icon_aspect);
1740
1741         int entry_count = bound(1, floor(NOTIFY_MAX_ENTRIES * size.y / size.x), NOTIFY_MAX_ENTRIES);
1742         float entry_height = size.y / entry_count;
1743
1744         float panel_width_half = size.x * 0.5;
1745         float icon_width_half = entry_height * icon_aspect / 2;
1746         float name_maxwidth = panel_width_half - icon_width_half - size.x * NOTIFY_ICON_MARGIN;
1747
1748         vector font_size = '0.5 0.5 0' * entry_height * autocvar_hud_panel_notify_fontsize;
1749         vector icon_size = (eX * icon_aspect + eY) * entry_height;
1750         vector icon_left = eX * (panel_width_half - icon_width_half);
1751         vector attacker_right = eX * name_maxwidth;
1752         vector victim_left = eX * (size.x - name_maxwidth);
1753
1754         vector attacker_pos, victim_pos, icon_pos;
1755         string attacker, victim, icon;
1756         int i, j, count, step, limit;
1757         float alpha;
1758
1759         if (autocvar_hud_panel_notify_flip)
1760         {
1761                 // Order items from the top down
1762                 i = 0;
1763                 step = +1;
1764                 limit = entry_count;
1765         }
1766         else
1767         {
1768                 // Order items from the bottom up
1769                 i = entry_count - 1;
1770                 step = -1;
1771                 limit = -1;
1772         }
1773
1774         for (j = notify_index, count = 0; i != limit; i += step, ++j, ++count)
1775         {
1776                 if(autocvar__hud_configure)
1777                 {
1778                         attacker = sprintf(_("Player %d"), count + 1);
1779                         victim = sprintf(_("Player %d"), count + 2);
1780                         icon = get_weaponinfo(min(WEP_FIRST + count * 2, WEP_LAST)).model2;
1781                         alpha = bound(0, 1.2 - count / entry_count, 1);
1782                 }
1783                 else
1784                 {
1785                         if (j == NOTIFY_MAX_ENTRIES)
1786                                 j = 0;
1787
1788                         if (notify_times[j] + fade_start > time)
1789                                 alpha = 1;
1790                         else if (fade_time != 0)
1791                         {
1792                                 alpha = bound(0, (notify_times[j] + fade_start + fade_time - time) / fade_time, 1);
1793                                 if (alpha == 0)
1794                                         break;
1795                         }
1796                         else
1797                                 break;
1798
1799                         attacker = notify_attackers[j];
1800                         victim = notify_victims[j];
1801                         icon = notify_icons[j];
1802                 }
1803
1804                 if (icon != "" && victim != "")
1805                 {
1806                         vector name_top = eY * (i * entry_height + 0.5 * (entry_height - font_size.y));
1807
1808                         icon_pos = pos + icon_left + eY * i * entry_height;
1809                         drawpic_aspect_skin(icon_pos, icon, icon_size, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1810
1811                         victim = textShortenToWidth(victim, name_maxwidth, font_size, stringwidth_colors);
1812                         victim_pos = pos + victim_left + name_top;
1813                         drawcolorcodedstring(victim_pos, victim, font_size, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1814
1815                         if (attacker != "")
1816                         {
1817                                 attacker = textShortenToWidth(attacker, name_maxwidth, font_size, stringwidth_colors);
1818                                 attacker_pos = pos + attacker_right - eX * stringwidth(attacker, true, font_size) + name_top;
1819                                 drawcolorcodedstring(attacker_pos, attacker, font_size, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1820                         }
1821                 }
1822         }
1823
1824         notify_count = count;
1825 }
1826
1827 void HUD_Timer(void)
1828 {
1829         if(intermission == 2) return;
1830         if(!autocvar__hud_configure)
1831         {
1832                 if(!autocvar_hud_panel_timer) return;
1833         }
1834
1835         HUD_Panel_UpdateCvars();
1836
1837         draw_beginBoldFont();
1838
1839         vector pos, mySize;
1840         pos = panel_pos;
1841         mySize = panel_size;
1842
1843         HUD_Panel_DrawBg(1);
1844         if(panel_bg_padding)
1845         {
1846                 pos += '1 1 0' * panel_bg_padding;
1847                 mySize -= '2 2 0' * panel_bg_padding;
1848         }
1849
1850         string timer;
1851         float timelimit, elapsedTime, timeleft, minutesLeft;
1852
1853         timelimit = getstatf(STAT_TIMELIMIT);
1854
1855         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
1856         timeleft = ceil(timeleft);
1857
1858         minutesLeft = floor(timeleft / 60);
1859
1860         vector timer_color;
1861         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
1862                 timer_color = '1 1 1'; //white
1863         else if(minutesLeft >= 1)
1864                 timer_color = '1 1 0'; //yellow
1865         else
1866                 timer_color = '1 0 0'; //red
1867
1868         if (autocvar_hud_panel_timer_increment || timelimit == 0 || warmup_stage) {
1869                 if (time < getstatf(STAT_GAMESTARTTIME)) {
1870                         //while restart is still active, show 00:00
1871                         timer = seconds_tostring(0);
1872                 } else {
1873                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
1874                         timer = seconds_tostring(elapsedTime);
1875                 }
1876         } else {
1877                 timer = seconds_tostring(timeleft);
1878         }
1879
1880         drawstring_aspect(pos, timer, mySize, timer_color, panel_fg_alpha, DRAWFLAG_NORMAL);
1881
1882         draw_endBoldFont();
1883 }
1884
1885 // Radar (#6)
1886 //
1887
1888 float HUD_Radar_Clickable()
1889 {
1890         return hud_panel_radar_mouse && !hud_panel_radar_temp_hidden;
1891 }
1892
1893 void HUD_Radar_Show_Maximized(bool doshow,float clickable)
1894 {
1895         hud_panel_radar_maximized = doshow;
1896         hud_panel_radar_temp_hidden = 0;
1897
1898         if ( doshow )
1899         {
1900                 if (clickable)
1901                 {
1902                         if(autocvar_hud_cursormode)
1903                                 setcursormode(1);
1904                         hud_panel_radar_mouse = 1;
1905                 }
1906         }
1907         else if ( hud_panel_radar_mouse )
1908         {
1909                 hud_panel_radar_mouse = 0;
1910                 mouseClicked = 0;
1911                 if(autocvar_hud_cursormode)
1912                 if(!mv_active)
1913                         setcursormode(0);
1914         }
1915 }
1916 void HUD_Radar_Hide_Maximized()
1917 {
1918         HUD_Radar_Show_Maximized(false,false);
1919 }
1920
1921
1922 float HUD_Radar_InputEvent(float bInputType, float nPrimary, float nSecondary)
1923 {
1924         if(!hud_panel_radar_maximized || !hud_panel_radar_mouse ||
1925                 autocvar__hud_configure || mv_active)
1926                 return false;
1927
1928         if(bInputType == 3)
1929         {
1930                 mousepos_x = nPrimary;
1931                 mousepos_y = nSecondary;
1932                 return true;
1933         }
1934
1935         if(nPrimary == K_MOUSE1)
1936         {
1937                 if(bInputType == 0) // key pressed
1938                         mouseClicked |= S_MOUSE1;
1939                 else if(bInputType == 1) // key released
1940                         mouseClicked -= (mouseClicked & S_MOUSE1);
1941         }
1942         else if(nPrimary == K_MOUSE2)
1943         {
1944                 if(bInputType == 0) // key pressed
1945                         mouseClicked |= S_MOUSE2;
1946                 else if(bInputType == 1) // key released
1947                         mouseClicked -= (mouseClicked & S_MOUSE2);
1948         }
1949         else if ( nPrimary == K_ESCAPE && bInputType == 0 )
1950         {
1951                 HUD_Radar_Hide_Maximized();
1952         }
1953         else
1954         {
1955                 // allow console/use binds to work without hiding the map
1956                 string con_keys;
1957                 float keys;
1958                 float i;
1959                 con_keys = strcat(findkeysforcommand("toggleconsole", 0)," ",findkeysforcommand("+use", 0)) ;
1960                 keys = tokenize(con_keys); // findkeysforcommand returns data for this
1961                 for (i = 0; i < keys; ++i)
1962                 {
1963                         if(nPrimary == stof(argv(i)))
1964                                 return false;
1965                 }
1966
1967                 if ( getstati(STAT_HEALTH) <= 0 )
1968                 {
1969                         // Show scoreboard
1970                         if ( bInputType < 2 )
1971                         {
1972                                 con_keys = findkeysforcommand("+showscores", 0);
1973                                 keys = tokenize(con_keys);
1974                                 for (i = 0; i < keys; ++i)
1975                                 {
1976                                         if ( nPrimary == stof(argv(i)) )
1977                                         {
1978                                                 hud_panel_radar_temp_hidden = bInputType == 0;
1979                                                 return false;
1980                                         }
1981                                 }
1982                         }
1983                 }
1984                 else if ( bInputType == 0 )
1985                         HUD_Radar_Hide_Maximized();
1986
1987                 return false;
1988         }
1989
1990         return true;
1991 }
1992
1993 void HUD_Radar_Mouse()
1994 {
1995         if ( !hud_panel_radar_mouse ) return;
1996         if(mv_active) return;
1997
1998         if ( intermission )
1999         {
2000                 HUD_Radar_Hide_Maximized();
2001                 return;
2002         }
2003
2004         if(mouseClicked & S_MOUSE2)
2005         {
2006                 HUD_Radar_Hide_Maximized();
2007                 return;
2008         }
2009
2010         if(!autocvar_hud_cursormode)
2011         {
2012                 mousepos = mousepos + getmousepos() * autocvar_menu_mouse_speed;
2013
2014                 mousepos_x = bound(0, mousepos_x, vid_conwidth);
2015                 mousepos_y = bound(0, mousepos_y, vid_conheight);
2016         }
2017
2018         HUD_Panel_UpdateCvars();
2019
2020
2021         panel_size = autocvar_hud_panel_radar_maximized_size;
2022         panel_size_x = bound(0.2, panel_size_x, 1) * vid_conwidth;
2023         panel_size_y = bound(0.2, panel_size_y, 1) * vid_conheight;
2024         panel_pos_x = (vid_conwidth - panel_size_x) / 2;
2025         panel_pos_y = (vid_conheight - panel_size_y) / 2;
2026
2027         if(mouseClicked & S_MOUSE1)
2028         {
2029                 // click outside
2030                 if ( mousepos_x < panel_pos_x || mousepos_x > panel_pos_x + panel_size_x ||
2031                          mousepos_y < panel_pos_y || mousepos_y > panel_pos_y + panel_size_y )
2032                 {
2033                         HUD_Radar_Hide_Maximized();
2034                         return;
2035                 }
2036                 vector pos = teamradar_texcoord_to_3dcoord(teamradar_2dcoord_to_texcoord(mousepos),view_origin_z);
2037                 localcmd(sprintf("cmd ons_spawn %f %f %f",pos_x,pos_y,pos_z));
2038
2039                 HUD_Radar_Hide_Maximized();
2040                 return;
2041         }
2042
2043
2044         const vector cursor_size = '32 32 0';
2045         drawpic(mousepos-'8 4 0', strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"), cursor_size, '1 1 1', 0.8, DRAWFLAG_NORMAL);
2046 }
2047
2048 void HUD_Radar(void)
2049 {
2050         if(intermission == 2) return;
2051         if (!autocvar__hud_configure)
2052         {
2053                 if (hud_panel_radar_maximized)
2054                 {
2055                         if (!hud_draw_maximized) return;
2056                 }
2057                 else
2058                 {
2059                         if (autocvar_hud_panel_radar == 0) return;
2060                         if (autocvar_hud_panel_radar != 2 && !teamplay) return;
2061                         if(radar_panel_modified)
2062                         {
2063                                 panel.update_time = time; // forces reload of panel attributes
2064                                 radar_panel_modified = false;
2065                         }
2066                 }
2067         }
2068
2069         if ( hud_panel_radar_temp_hidden )
2070                 return;
2071
2072         HUD_Panel_UpdateCvars();
2073
2074         float f = 0;
2075
2076         if (hud_panel_radar_maximized && !autocvar__hud_configure)
2077         {
2078                 panel_size = autocvar_hud_panel_radar_maximized_size;
2079                 panel_size.x = bound(0.2, panel_size.x, 1) * vid_conwidth;
2080                 panel_size.y = bound(0.2, panel_size.y, 1) * vid_conheight;
2081                 panel_pos.x = (vid_conwidth - panel_size.x) / 2;
2082                 panel_pos.y = (vid_conheight - panel_size.y) / 2;
2083
2084                 string panel_bg;
2085                 panel_bg = strcat(hud_skin_path, "/border_default"); // always use the default border when maximized
2086                 if(precache_pic(panel_bg) == "")
2087                         panel_bg = "gfx/hud/default/border_default"; // fallback
2088                 if(!radar_panel_modified && panel_bg != panel.current_panel_bg)
2089                         radar_panel_modified = true;
2090                 if(panel.current_panel_bg)
2091                         strunzone(panel.current_panel_bg);
2092                 panel.current_panel_bg = strzone(panel_bg);
2093
2094                 switch(hud_panel_radar_maximized_zoommode)
2095                 {
2096                         default:
2097                         case 0:
2098                                 f = current_zoomfraction;
2099                                 break;
2100                         case 1:
2101                                 f = 1 - current_zoomfraction;
2102                                 break;
2103                         case 2:
2104                                 f = 0;
2105                                 break;
2106                         case 3:
2107                                 f = 1;
2108                                 break;
2109                 }
2110
2111                 switch(hud_panel_radar_maximized_rotation)
2112                 {
2113                         case 0:
2114                                 teamradar_angle = view_angles.y - 90;
2115                                 break;
2116                         default:
2117                                 teamradar_angle = 90 * hud_panel_radar_maximized_rotation;
2118                                 break;
2119                 }
2120         }
2121         if (!hud_panel_radar_maximized && !autocvar__hud_configure)
2122         {
2123                 switch(hud_panel_radar_zoommode)
2124                 {
2125                         default:
2126                         case 0:
2127                                 f = current_zoomfraction;
2128                                 break;
2129                         case 1:
2130                                 f = 1 - current_zoomfraction;
2131                                 break;
2132                         case 2:
2133                                 f = 0;
2134                                 break;
2135                         case 3:
2136                                 f = 1;
2137                                 break;
2138                 }
2139
2140                 switch(hud_panel_radar_rotation)
2141                 {
2142                         case 0:
2143                                 teamradar_angle = view_angles.y - 90;
2144                                 break;
2145                         default:
2146                                 teamradar_angle = 90 * hud_panel_radar_rotation;
2147                                 break;
2148                 }
2149         }
2150
2151         vector pos, mySize;
2152         pos = panel_pos;
2153         mySize = panel_size;
2154
2155         HUD_Panel_DrawBg(1);
2156         if(panel_bg_padding)
2157         {
2158                 pos += '1 1 0' * panel_bg_padding;
2159                 mySize -= '2 2 0' * panel_bg_padding;
2160         }
2161
2162         int color2;
2163         entity tm;
2164         float scale2d, normalsize, bigsize;
2165
2166         teamradar_origin2d = pos + 0.5 * mySize;
2167         teamradar_size2d = mySize;
2168
2169         if(minimapname == "")
2170                 return;
2171
2172         teamradar_loadcvars();
2173
2174         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
2175         teamradar_size2d = mySize;
2176
2177         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
2178
2179         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
2180         if((hud_panel_radar_rotation == 0 && !hud_panel_radar_maximized) || (hud_panel_radar_maximized_rotation == 0 && hud_panel_radar_maximized))
2181         {
2182                 // max-min distance must fit the radar in any rotation
2183                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_scale));
2184         }
2185         else
2186         {
2187                 vector c0, c1, c2, c3, span;
2188                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
2189                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
2190                 c2 = rotate('1 0 0' * mi_min.x + '0 1 0' * mi_max.y, teamradar_angle * DEG2RAD);
2191                 c3 = rotate('1 0 0' * mi_max.x + '0 1 0' * mi_min.y, teamradar_angle * DEG2RAD);
2192                 span = '0 0 0';
2193                 span.x = max(c0_x, c1_x, c2_x, c3_x) - min(c0_x, c1_x, c2_x, c3_x);
2194                 span.y = max(c0_y, c1_y, c2_y, c3_y) - min(c0_y, c1_y, c2_y, c3_y);
2195
2196                 // max-min distance must fit the radar in x=x, y=y
2197                 bigsize = min(
2198                         teamradar_size2d.x * scale2d / (1.05 * span.x),
2199                         teamradar_size2d.y * scale2d / (1.05 * span.y)
2200                 );
2201         }
2202
2203         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
2204         if(bigsize > normalsize)
2205                 normalsize = bigsize;
2206
2207         teamradar_size =
2208                   f * bigsize
2209                 + (1 - f) * normalsize;
2210         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
2211                   f * mi_center
2212                 + (1 - f) * view_origin);
2213
2214         drawsetcliparea(
2215                 pos.x,
2216                 pos.y,
2217                 mySize.x,
2218                 mySize.y
2219         );
2220
2221         draw_teamradar_background(hud_panel_radar_foreground_alpha);
2222
2223         for(tm = world; (tm = find(tm, classname, "radarlink")); )
2224                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
2225
2226         vector coord;
2227         vector brightcolor;
2228         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
2229         {
2230                 if ( hud_panel_radar_mouse )
2231                 if ( tm.health > 0 )
2232                 if ( tm.team == myteam+1 )
2233                 {
2234                         coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(tm.origin));
2235                         if ( vlen(mousepos-coord) < 8 )
2236                         {
2237                                 brightcolor_x = min(1,tm.teamradar_color_x*1.5);
2238                                 brightcolor_y = min(1,tm.teamradar_color_y*1.5);
2239                                 brightcolor_z = min(1,tm.teamradar_color_z*1.5);
2240                                 drawpic(coord - '8 8 0', "gfx/teamradar_icon_glow", '16 16 0', brightcolor, panel_fg_alpha, 0);
2241                         }
2242                 }
2243
2244                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, panel_fg_alpha);
2245         }
2246         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
2247         {
2248                 color2 = GetPlayerColor(tm.sv_entnum);
2249                 //if(color == NUM_SPECTATOR || color == color2)
2250                         draw_teamradar_player(tm.origin, tm.angles, Team_ColorRGB(color2));
2251         }
2252         draw_teamradar_player(view_origin, view_angles, '1 1 1');
2253
2254         drawresetcliparea();
2255
2256         if ( hud_panel_radar_mouse )
2257         {
2258                 string message = "Click to select teleport destination";
2259
2260                 if ( getstati(STAT_HEALTH) <= 0 )
2261                 {
2262                         message = "Click to select spawn location";
2263                 }
2264
2265                 drawcolorcodedstring(pos + '0.5 0 0' * (mySize_x - stringwidth(message, true, hud_fontsize)) - '0 1 0' * hud_fontsize_y * 2,
2266                                                          message, hud_fontsize, hud_panel_radar_foreground_alpha, DRAWFLAG_NORMAL);
2267
2268                 hud_panel_radar_bottom = pos_y + mySize_y + hud_fontsize_y;
2269         }
2270 }
2271
2272 // Score (#7)
2273 //
2274 void HUD_UpdatePlayerTeams();
2275 void HUD_Score_Rankings(vector pos, vector mySize, entity me)
2276 {
2277         float score;
2278         entity tm = world, pl;
2279         int SCOREPANEL_MAX_ENTRIES = 6;
2280         float SCOREPANEL_ASPECTRATIO = 2;
2281         int entries = bound(1, floor(SCOREPANEL_MAX_ENTRIES * mySize.y/mySize.x * SCOREPANEL_ASPECTRATIO), SCOREPANEL_MAX_ENTRIES);
2282         vector fontsize = '1 1 0' * (mySize.y/entries);
2283
2284         vector rgb, score_color;
2285         rgb = '1 1 1';
2286         score_color = '1 1 1';
2287
2288         float name_size = mySize.x*0.75;
2289         float spacing_size = mySize.x*0.04;
2290         const float highlight_alpha = 0.2;
2291         int i = 0, first_pl = 0;
2292         bool me_printed = false;
2293         string s;
2294         if (autocvar__hud_configure)
2295         {
2296                 float players_per_team = 0;
2297                 if (team_count)
2298                 {
2299                         // show team scores in the first line
2300                         float score_size = mySize.x / team_count;
2301                         players_per_team = max(2, ceil((entries - 1) / team_count));
2302                         for(i=0; i<team_count; ++i) {
2303                                 if (i == floor((entries - 2) / players_per_team) || (entries == 1 && i == 0))
2304                                         HUD_Panel_DrawHighlight(pos + eX * score_size * i, eX * score_size + eY * fontsize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2305                                 drawstring_aspect(pos + eX * score_size * i, ftos(175 - 23*i), eX * score_size + eY * fontsize.y, Team_ColorRGB(ColorByTeam(i)) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2306                         }
2307                         first_pl = 1;
2308                         pos.y += fontsize.y;
2309                 }
2310                 score = 10 + SCOREPANEL_MAX_ENTRIES * 3;
2311                 for (i=first_pl; i<entries; ++i)
2312                 {
2313                         //simulate my score is lower than all displayed players,
2314                         //so that I don't appear at all showing pure rankings.
2315                         //This is to better show the difference between the 2 ranking views
2316                         if (i == entries-1 && autocvar_hud_panel_score_rankings == 1)
2317                         {
2318                                 rgb = '1 1 0';
2319                                 drawfill(pos, eX * mySize.x + eY * fontsize.y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2320                                 s = GetPlayerName(player_localnum);
2321                                 score = 7;
2322                         }
2323                         else
2324                         {
2325                                 s = sprintf(_("Player %d"), i + 1 - first_pl);
2326                                 score -= 3;
2327                         }
2328
2329                         if (team_count)
2330                                 score_color = Team_ColorRGB(ColorByTeam(floor((i - first_pl) / players_per_team))) * 0.8;
2331                         s = textShortenToWidth(s, name_size, fontsize, stringwidth_colors);
2332                         drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, true, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
2333                         drawstring(pos + eX * (name_size + spacing_size), ftos(score), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2334                         pos.y += fontsize.y;
2335                 }
2336                 return;
2337         }
2338
2339         if (!scoreboard_fade_alpha) // the scoreboard too calls HUD_UpdatePlayerTeams
2340                 HUD_UpdatePlayerTeams();
2341         if (team_count)
2342         {
2343                 // show team scores in the first line
2344                 float score_size = mySize.x / team_count;
2345                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2346                         if(tm.team == NUM_SPECTATOR)
2347                                 continue;
2348                         if (tm.team == myteam)
2349                                 drawfill(pos + eX * score_size * i, eX * score_size + eY * fontsize.y, '1 1 1', highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2350                         drawstring_aspect(pos + eX * score_size * i, ftos(tm.(teamscores[ts_primary])), eX * score_size + eY * fontsize.y, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2351                         ++i;
2352                 }
2353                 first_pl = 1;
2354                 pos.y += fontsize.y;
2355                 tm = teams.sort_next;
2356         }
2357         i = first_pl;
2358
2359         do
2360         for (pl = players.sort_next; pl && i<entries; pl = pl.sort_next)
2361         {
2362                 if ((team_count && pl.team != tm.team) || pl.team == NUM_SPECTATOR)
2363                         continue;
2364
2365                 if (i == entries-1 && !me_printed && pl != me)
2366                 if (autocvar_hud_panel_score_rankings == 1 && spectatee_status != -1)
2367                 {
2368                         for (pl = me.sort_next; pl; pl = pl.sort_next)
2369                                 if (pl.team != NUM_SPECTATOR)
2370                                         break;
2371
2372                         if (pl)
2373                                 rgb = '1 1 0'; //not last but not among the leading players: yellow
2374                         else
2375                                 rgb = '1 0 0'; //last: red
2376                         pl = me;
2377                 }
2378
2379                 if (pl == me)
2380                 {
2381                         if (i == first_pl)
2382                                 rgb = '0 1 0'; //first: green
2383                         me_printed = true;
2384                         drawfill(pos, eX * mySize.x + eY * fontsize.y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2385                 }
2386                 if (team_count)
2387                         score_color = Team_ColorRGB(pl.team) * 0.8;
2388                 s = textShortenToWidth(GetPlayerName(pl.sv_entnum), name_size, fontsize, stringwidth_colors);
2389                 drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, true, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
2390                 drawstring(pos + eX * (name_size + spacing_size), ftos(pl.(scores[ps_primary])), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2391                 pos.y += fontsize.y;
2392                 ++i;
2393         }
2394         while (i<entries && team_count && (tm = tm.sort_next) && (tm.team != NUM_SPECTATOR || (tm = tm.sort_next)));
2395 }
2396
2397 void HUD_Score(void)
2398 {
2399         if(intermission == 2) return;
2400         if(!autocvar__hud_configure)
2401         {
2402                 if(!autocvar_hud_panel_score) return;
2403                 if(spectatee_status == -1 && (gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS)) return;
2404         }
2405
2406         HUD_Panel_UpdateCvars();
2407         vector pos, mySize;
2408         pos = panel_pos;
2409         mySize = panel_size;
2410
2411         HUD_Panel_DrawBg(1);
2412         if(panel_bg_padding)
2413         {
2414                 pos += '1 1 0' * panel_bg_padding;
2415                 mySize -= '2 2 0' * panel_bg_padding;
2416         }
2417
2418         float score, distribution = 0;
2419         string sign;
2420         vector distribution_color;
2421         entity tm, pl, me;
2422
2423         me = playerslots[current_player];
2424
2425         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
2426                 string timer, distrtimer;
2427
2428                 pl = players.sort_next;
2429                 if(pl == me)
2430                         pl = pl.sort_next;
2431                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
2432                         if(pl.scores[ps_primary] == 0)
2433                                 pl = world;
2434
2435                 score = me.(scores[ps_primary]);
2436                 timer = TIME_ENCODED_TOSTRING(score);
2437
2438                 draw_beginBoldFont();
2439                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
2440                         // distribution display
2441                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2442
2443                         distrtimer = ftos_decimals(fabs(distribution/pow(10, TIME_DECIMALS)), TIME_DECIMALS);
2444
2445                         if (distribution <= 0) {
2446                                 distribution_color = '0 1 0';
2447                                 sign = "-";
2448                         }
2449                         else {
2450                                 distribution_color = '1 0 0';
2451                                 sign = "+";
2452                         }
2453                         drawstring_aspect(pos + eX * 0.75 * mySize.x, strcat(sign, distrtimer), eX * 0.25 * mySize.x + eY * (1/3) * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2454                 }
2455                 // race record display
2456                 if (distribution <= 0)
2457                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2458                 drawstring_aspect(pos, timer, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2459                 draw_endBoldFont();
2460         } else if (!teamplay) { // non-teamgames
2461                 if ((spectatee_status == -1 && !autocvar__hud_configure) || autocvar_hud_panel_score_rankings)
2462                 {
2463                         HUD_Score_Rankings(pos, mySize, me);
2464                         return;
2465                 }
2466                 // me vector := [team/connected frags id]
2467                 pl = players.sort_next;
2468                 if(pl == me)
2469                         pl = pl.sort_next;
2470
2471                 if(autocvar__hud_configure)
2472                         distribution = 42;
2473                 else if(pl)
2474                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
2475                 else
2476                         distribution = 0;
2477
2478                 score = me.(scores[ps_primary]);
2479                 if(autocvar__hud_configure)
2480                         score = 123;
2481
2482                 if(distribution >= 5)
2483                         distribution_color = eY;
2484                 else if(distribution >= 0)
2485                         distribution_color = '1 1 1';
2486                 else if(distribution >= -5)
2487                         distribution_color = '1 1 0';
2488                 else
2489                         distribution_color = eX;
2490
2491                 string distribution_str;
2492                 distribution_str = ftos(distribution);
2493                 draw_beginBoldFont();
2494                 if (distribution >= 0)
2495                 {
2496                         if (distribution > 0)
2497                                 distribution_str = strcat("+", distribution_str);
2498                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2499                 }
2500                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize.x + eY * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2501                 drawstring_aspect(pos + eX * 0.75 * mySize.x, distribution_str, eX * 0.25 * mySize.x + eY * (1/3) * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
2502                 draw_endBoldFont();
2503         } else { // teamgames
2504                 float row, column, rows = 0, columns = 0;
2505                 vector offset = '0 0 0';
2506                 vector score_pos, score_size; //for scores other than myteam
2507                 if(autocvar_hud_panel_score_rankings)
2508                 {
2509                         HUD_Score_Rankings(pos, mySize, me);
2510                         return;
2511                 }
2512                 if(spectatee_status == -1)
2513                 {
2514                         rows = HUD_GetRowCount(team_count, mySize, 3);
2515                         columns = ceil(team_count/rows);
2516                         score_size = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
2517
2518                         float newSize;
2519                         if(score_size.x/score_size.y > 3)
2520                         {
2521                                 newSize = 3 * score_size.y;
2522                                 offset.x = score_size.x - newSize;
2523                                 pos.x += offset.x/2;
2524                                 score_size.x = newSize;
2525                         }
2526                         else
2527                         {
2528                                 newSize = 1/3 * score_size.x;
2529                                 offset.y = score_size.y - newSize;
2530                                 pos.y += offset.y/2;
2531                                 score_size.y = newSize;
2532                         }
2533                 }
2534                 else
2535                         score_size = eX * mySize.x*(1/4) + eY * mySize.y*(1/3);
2536
2537                 float max_fragcount;
2538                 max_fragcount = -99;
2539                 draw_beginBoldFont();
2540                 row = column = 0;
2541                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
2542                         if(tm.team == NUM_SPECTATOR)
2543                                 continue;
2544                         score = tm.(teamscores[ts_primary]);
2545                         if(autocvar__hud_configure)
2546                                 score = 123;
2547
2548                         if (score > max_fragcount)
2549                                 max_fragcount = score;
2550
2551                         if (spectatee_status == -1)
2552                         {
2553                                 score_pos = pos + eX * column * (score_size.x + offset.x) + eY * row * (score_size.y + offset.y);
2554                                 if (max_fragcount == score)
2555                                         HUD_Panel_DrawHighlight(score_pos, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2556                                 drawstring_aspect(score_pos, ftos(score), score_size, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2557                                 ++row;
2558                                 if(row >= rows)
2559                                 {
2560                                         row = 0;
2561                                         ++column;
2562                                 }
2563                         }
2564                         else if(tm.team == myteam) {
2565                                 if (max_fragcount == score)
2566                                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2567                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize.x + eY * mySize.y, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2568                         } else {
2569                                 if (max_fragcount == score)
2570                                         HUD_Panel_DrawHighlight(pos + eX * 0.75 * mySize.x + eY * (1/3) * rows * mySize.y, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2571                                 drawstring_aspect(pos + eX * 0.75 * mySize.x + eY * (1/3) * rows * mySize.y, ftos(score), score_size, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
2572                                 ++rows;
2573                         }
2574                 }
2575                 draw_endBoldFont();
2576         }
2577 }
2578
2579 // Race timer (#8)
2580 //
2581 void HUD_RaceTimer (void)
2582 {
2583         if(intermission == 2) return;
2584         if(!autocvar__hud_configure)
2585         {
2586                 if(!autocvar_hud_panel_racetimer) return;
2587                 if(!(gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS)) return;
2588                 if(spectatee_status == -1) return;
2589         }
2590
2591         HUD_Panel_UpdateCvars();
2592
2593         vector pos, mySize;
2594         pos = panel_pos;
2595         mySize = panel_size;
2596
2597         HUD_Panel_DrawBg(1);
2598         if(panel_bg_padding)
2599         {
2600                 pos += '1 1 0' * panel_bg_padding;
2601                 mySize -= '2 2 0' * panel_bg_padding;
2602         }
2603
2604         // always force 4:1 aspect
2605         vector newSize = '0 0 0';
2606         if(mySize.x/mySize.y > 4)
2607         {
2608                 newSize.x = 4 * mySize.y;
2609                 newSize.y = mySize.y;
2610
2611                 pos.x = pos.x + (mySize.x - newSize.x) / 2;
2612         }
2613         else
2614         {
2615                 newSize.y = 1/4 * mySize.x;
2616                 newSize.x = mySize.x;
2617
2618                 pos.y = pos.y + (mySize.y - newSize.y) / 2;
2619         }
2620         mySize = newSize;
2621
2622         float a, t;
2623         string s, forcetime;
2624
2625         if(autocvar__hud_configure)
2626         {
2627                 s = "0:13:37";
2628                 draw_beginBoldFont();
2629                 drawstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, false, '0.60 0.60 0' * mySize.y), s, '0.60 0.60 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2630                 draw_endBoldFont();
2631                 s = _("^1Intermediate 1 (+15.42)");
2632                 drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.20 * mySize.y) + eY * 0.60 * mySize.y, s, '1 1 0' * 0.20 * mySize.y, panel_fg_alpha, DRAWFLAG_NORMAL);
2633                 s = sprintf(_("^1PENALTY: %.1f (%s)"), 2, "missing a checkpoint");
2634                 drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.20 * mySize.y) + eY * 0.80 * mySize.y, s, '1 1 0' * 0.20 * mySize.y, panel_fg_alpha, DRAWFLAG_NORMAL);
2635         }
2636         else if(race_checkpointtime)
2637         {
2638                 a = bound(0, 2 - (time - race_checkpointtime), 1);
2639                 s = "";
2640                 forcetime = "";
2641                 if(a > 0) // just hit a checkpoint?
2642                 {
2643                         if(race_checkpoint != 254)
2644                         {
2645                                 if(race_time && race_previousbesttime)
2646                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
2647                                 else
2648                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
2649                                 if(race_time)
2650                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
2651                         }
2652                 }
2653                 else
2654                 {
2655                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
2656                         {
2657                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
2658                                 if(a > 0) // next one?
2659                                 {
2660                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
2661                                 }
2662                         }
2663                 }
2664
2665                 if(s != "" && a > 0)
2666                 {
2667                         drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.6 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2668                 }
2669
2670                 if(race_penaltytime)
2671                 {
2672                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
2673                         if(a > 0)
2674                         {
2675                                 s = sprintf(_("^1PENALTY: %.1f (%s)"), race_penaltytime * 0.1, race_penaltyreason);
2676                                 drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.8 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2677                         }
2678                 }
2679
2680                 draw_beginBoldFont();
2681
2682                 if(forcetime != "")
2683                 {
2684                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
2685                         drawstring_expanding(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(forcetime, false, '1 1 0' * 0.6 * mySize.y), forcetime, '1 1 0' * 0.6 * mySize.y, '1 1 1', panel_fg_alpha, 0, a);
2686                 }
2687                 else
2688                         a = 1;
2689
2690                 if(race_laptime && race_checkpoint != 255)
2691                 {
2692                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
2693                         drawstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, false, '0.6 0.6 0' * mySize.y), s, '0.6 0.6 0' * mySize.y, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
2694                 }
2695
2696                 draw_endBoldFont();
2697         }
2698         else
2699         {
2700                 if(race_mycheckpointtime)
2701                 {
2702                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
2703                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -(race_mycheckpointenemy == ""), race_mycheckpointlapsdelta, race_mycheckpointenemy);
2704                         drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.6 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2705                 }
2706                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
2707                 {
2708                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
2709                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -(race_othercheckpointenemy == ""), race_othercheckpointlapsdelta, race_othercheckpointenemy);
2710                         drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.6 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2711                 }
2712
2713                 if(race_penaltytime && !race_penaltyaccumulator)
2714                 {
2715                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
2716                         a = bound(0, (1 + t - time), 1);
2717                         if(a > 0)
2718                         {
2719                                 if(time < t)
2720                                         s = sprintf(_("^1PENALTY: %.1f (%s)"), (t - time) * 0.1, race_penaltyreason);
2721                                 else
2722                                         s = sprintf(_("^2PENALTY: %.1f (%s)"), 0, race_penaltyreason);
2723                                 drawcolorcodedstring(pos + eX * 0.5 * mySize.x - '0.5 0 0' * stringwidth(s, true, '1 1 0' * 0.2 * mySize.y) + eY * 0.6 * mySize.y, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
2724                         }
2725                 }
2726         }
2727 }
2728
2729 // Vote window (#9)
2730 //
2731
2732 void HUD_Vote(void)
2733 {
2734         if(intermission == 2) return;
2735         if(autocvar_cl_allow_uid2name == -1 && (gametype == MAPINFO_TYPE_CTS || gametype == MAPINFO_TYPE_RACE || (serverflags & SERVERFLAG_PLAYERSTATS)))
2736         {
2737                 vote_active = 1;
2738                 if (autocvar__hud_configure)
2739                 {
2740                         vote_yescount = 0;
2741                         vote_nocount = 0;
2742                         LOG_INFO(_("^1You must answer before entering hud configure mode\n"));
2743                         cvar_set("_hud_configure", "0");
2744                 }
2745                 if(vote_called_vote)
2746                         strunzone(vote_called_vote);
2747                 vote_called_vote = strzone(_("^2Name ^7instead of \"^1Anonymous player^7\" in stats"));
2748                 uid2name_dialog = 1;
2749         }
2750
2751         if(!autocvar__hud_configure)
2752         {
2753                 if(!autocvar_hud_panel_vote) return;
2754
2755                 panel_fg_alpha = autocvar_hud_panel_fg_alpha;
2756                 panel_bg_alpha_str = autocvar_hud_panel_vote_bg_alpha;
2757
2758                 if(panel_bg_alpha_str == "") {
2759                         panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);
2760                 }
2761                 panel_bg_alpha = stof(panel_bg_alpha_str);
2762         }
2763         else
2764         {
2765                 vote_yescount = 3;
2766                 vote_nocount = 2;
2767                 vote_needed = 4;
2768         }
2769
2770         string s;
2771         float a;
2772         if(vote_active != vote_prev) {
2773                 vote_change = time;
2774                 vote_prev = vote_active;
2775         }
2776
2777         if(vote_active || autocvar__hud_configure)
2778                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
2779         else
2780                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
2781
2782         if(!vote_alpha)
2783                 return;
2784
2785         HUD_Panel_UpdateCvars();
2786
2787         if(uid2name_dialog)
2788         {
2789                 panel_pos = eX * 0.3 * vid_conwidth + eY * 0.1 * vid_conheight;
2790                 panel_size = eX * 0.4 * vid_conwidth + eY * 0.3 * vid_conheight;
2791         }
2792
2793     // these must be below above block
2794         vector pos, mySize;
2795         pos = panel_pos;
2796         mySize = panel_size;
2797
2798         a = vote_alpha * (vote_highlighted ? autocvar_hud_panel_vote_alreadyvoted_alpha : 1);
2799         HUD_Panel_DrawBg(a);
2800         a = panel_fg_alpha * a;
2801
2802         if(panel_bg_padding)
2803         {
2804                 pos += '1 1 0' * panel_bg_padding;
2805                 mySize -= '2 2 0' * panel_bg_padding;
2806         }
2807
2808         // always force 3:1 aspect
2809         vector newSize = '0 0 0';
2810         if(mySize.x/mySize.y > 3)
2811         {
2812                 newSize.x = 3 * mySize.y;
2813                 newSize.y = mySize.y;
2814
2815                 pos.x = pos.x + (mySize.x - newSize.x) / 2;
2816         }
2817         else
2818         {
2819                 newSize.y = 1/3 * mySize.x;
2820                 newSize.x = mySize.x;
2821
2822                 pos.y = pos.y + (mySize.y - newSize.y) / 2;
2823         }
2824         mySize = newSize;
2825
2826         s = _("A vote has been called for:");
2827         if(uid2name_dialog)
2828                 s = _("Allow servers to store and display your name?");
2829         drawstring_aspect(pos, s, eX * mySize.x + eY * (2/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2830         s = textShortenToWidth(vote_called_vote, mySize.x, '1 1 0' * mySize.y * (1/8), stringwidth_colors);
2831         if(autocvar__hud_configure)
2832                 s = _("^1Configure the HUD");
2833         drawcolorcodedstring_aspect(pos + eY * (2/8) * mySize.y, s, eX * mySize.x + eY * (1.75/8) * mySize.y, a, DRAWFLAG_NORMAL);
2834
2835         // print the yes/no counts
2836     s = sprintf(_("Yes (%s): %d"), getcommandkey("vyes", "vyes"), vote_yescount);
2837         drawstring_aspect(pos + eY * (4/8) * mySize.y, s, eX * 0.5 * mySize.x + eY * (1.5/8) * mySize.y, '0 1 0', a, DRAWFLAG_NORMAL);
2838     s = sprintf(_("No (%s): %d"), getcommandkey("vno", "vno"), vote_nocount);
2839         drawstring_aspect(pos + eX * 0.5 * mySize.x + eY * (4/8) * mySize.y, s, eX * 0.5 * mySize.x + eY * (1.5/8) * mySize.y, '1 0 0', a, DRAWFLAG_NORMAL);
2840
2841         // draw the progress bar backgrounds
2842         drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_back", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2843
2844         // draw the highlights
2845         if(vote_highlighted == 1) {
2846                 drawsetcliparea(pos.x, pos.y, mySize.x * 0.5, mySize.y);
2847                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_voted", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2848         }
2849         else if(vote_highlighted == -1) {
2850                 drawsetcliparea(pos.x + 0.5 * mySize.x, pos.y, mySize.x * 0.5, mySize.y);
2851                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_voted", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2852         }
2853
2854         // draw the progress bars
2855         if(vote_yescount && vote_needed)
2856         {
2857                 drawsetcliparea(pos.x, pos.y, mySize.x * 0.5 * (vote_yescount/vote_needed), mySize.y);
2858                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_prog", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2859         }
2860
2861         if(vote_nocount && vote_needed)
2862         {
2863                 drawsetcliparea(pos.x + mySize.x - mySize.x * 0.5 * (vote_nocount/vote_needed), pos.y, mySize.x * 0.5, mySize.y);
2864                 drawpic_skin(pos + eY * (5/8) * mySize.y, "voteprogress_prog", eX * mySize.x + eY * (3/8) * mySize.y, '1 1 1', a, DRAWFLAG_NORMAL);
2865         }
2866
2867         drawresetcliparea();
2868 }
2869
2870 // Mod icons panel (#10)
2871 //
2872
2873 bool mod_active; // is there any active mod icon?
2874
2875 void DrawCAItem(vector myPos, vector mySize, float aspect_ratio, int layout, int i)
2876 {
2877         int stat = -1;
2878         string pic = "";
2879         vector color = '0 0 0';
2880         switch(i)
2881         {
2882                 case 0:
2883                         stat = getstati(STAT_REDALIVE);
2884                         pic = "player_red.tga";
2885                         color = '1 0 0';
2886                         break;
2887                 case 1:
2888                         stat = getstati(STAT_BLUEALIVE);
2889                         pic = "player_blue.tga";
2890                         color = '0 0 1';
2891                         break;
2892                 case 2:
2893                         stat = getstati(STAT_YELLOWALIVE);
2894                         pic = "player_yellow.tga";
2895                         color = '1 1 0';
2896                         break;
2897                 default:
2898                 case 3:
2899                         stat = getstati(STAT_PINKALIVE);
2900                         pic = "player_pink.tga";
2901                         color = '1 0 1';
2902                         break;
2903         }
2904
2905         if(mySize.x/mySize.y > aspect_ratio)
2906         {
2907                 i = aspect_ratio * mySize.y;
2908                 myPos.x = myPos.x + (mySize.x - i) / 2;
2909                 mySize.x = i;
2910         }
2911         else
2912         {
2913                 i = 1/aspect_ratio * mySize.x;
2914                 myPos.y = myPos.y + (mySize.y - i) / 2;
2915                 mySize.y = i;
2916   &nbs