]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
Merge remote branch 'origin/akari/freezetag-fixes'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qc
1 /*
2 ==================
3 Misc HUD functions
4 ==================
5 */
6
7 // a border picture is a texture containing nine parts:
8 //   1/4 width: left part
9 //   1/2 width: middle part (stretched)
10 //   1/4 width: right part
11 // divided into
12 //   1/4 height: top part
13 //   1/2 height: middle part (stretched)
14 //   1/4 height: bottom part
15 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
16 {
17     if (theBorderSize_x < 0 && theBorderSize_y < 0) // draw whole image as it is
18     {
19                 drawpic(theOrigin, pic, theSize, theColor, theAlpha, 0);
20                 return;
21     }
22         if (theBorderSize_x == 0 && theBorderSize_y == 0) // no border
23         {
24                 // draw only the central part
25                 drawsubpic(theOrigin, theSize, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
26                 return;
27         }
28
29         vector dX, dY;
30         vector width, height;
31         vector bW, bH;
32         //pic = draw_UseSkinFor(pic);
33         width = eX * theSize_x;
34         height = eY * theSize_y;
35         if(theSize_x <= theBorderSize_x * 2)
36         {
37                 // not wide enough... draw just left and right then
38                 bW = eX * (0.25 * theSize_x / (theBorderSize_x * 2));
39                 if(theSize_y <= theBorderSize_y * 2)
40                 {
41                         // not high enough... draw just corners
42                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
43                         drawsubpic(theOrigin,                 width * 0.5 + height * 0.5, pic, '0 0 0',           bW + bH, theColor, theAlpha, 0);
44                         drawsubpic(theOrigin + width   * 0.5, width * 0.5 + height * 0.5, pic, eX - bW,           bW + bH, theColor, theAlpha, 0);
45                         drawsubpic(theOrigin + height  * 0.5, width * 0.5 + height * 0.5, pic, eY - bH,           bW + bH, theColor, theAlpha, 0);
46                         drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
47                 }
48                 else
49                 {
50                         dY = theBorderSize_x * eY;
51                         drawsubpic(theOrigin,                             width * 0.5          +     dY, pic, '0 0    0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
52                         drawsubpic(theOrigin + width * 0.5,               width * 0.5          +     dY, pic, '0 0    0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
53                         drawsubpic(theOrigin                        + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0',           '0 0.5  0' + bW, theColor, theAlpha, 0);
54                         drawsubpic(theOrigin + width * 0.5          + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0' + eX - bW, '0 0.5  0' + bW, theColor, theAlpha, 0);
55                         drawsubpic(theOrigin               + height - dY, width * 0.5          +     dY, pic, '0 0.75 0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
56                         drawsubpic(theOrigin + width * 0.5 + height - dY, width * 0.5          +     dY, pic, '0 0.75 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
57                 }
58         }
59         else
60         {
61                 if(theSize_y <= theBorderSize_y * 2)
62                 {
63                         // not high enough... draw just top and bottom then
64                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
65                         dX = theBorderSize_x * eX;
66                         drawsubpic(theOrigin,                                         dX + height * 0.5, pic, '0    0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
67                         drawsubpic(theOrigin + dX,                        width - 2 * dX + height * 0.5, pic, '0.25 0 0',           '0.5  0 0' + bH, theColor, theAlpha, 0);
68                         drawsubpic(theOrigin + width - dX,                            dX + height * 0.5, pic, '0.75 0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
69                         drawsubpic(theOrigin              + height * 0.5,             dX + height * 0.5, pic, '0    0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
70                         drawsubpic(theOrigin + dX         + height * 0.5, width - 2 * dX + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5  0 0' + bH, theColor, theAlpha, 0);
71                         drawsubpic(theOrigin + width - dX + height * 0.5,             dX + height * 0.5, pic, '0.75 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
72                 }
73                 else
74                 {
75                         dX = theBorderSize_x * eX;
76                         dY = theBorderSize_x * eY;
77                         drawsubpic(theOrigin,                                        dX          +     dY, pic, '0    0    0', '0.25 0.25 0', theColor, theAlpha, 0);
78                         drawsubpic(theOrigin                  + dX,      width - 2 * dX          +     dY, pic, '0.25 0    0', '0.5  0.25 0', theColor, theAlpha, 0);
79                         drawsubpic(theOrigin          + width - dX,                  dX          +     dY, pic, '0.75 0    0', '0.25 0.25 0', theColor, theAlpha, 0);
80                         drawsubpic(theOrigin          + dY,                          dX + height - 2 * dY, pic, '0    0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
81                         drawsubpic(theOrigin          + dY         + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5  0.5  0', theColor, theAlpha, 0);
82                         drawsubpic(theOrigin          + dY + width - dX,             dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
83                         drawsubpic(theOrigin + height - dY,                          dX          +     dY, pic, '0    0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
84                         drawsubpic(theOrigin + height - dY         + dX, width - 2 * dX          +     dY, pic, '0.25 0.75 0', '0.5  0.25 0', theColor, theAlpha, 0);
85                         drawsubpic(theOrigin + height - dY + width - dX,             dX          +     dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
86                 }
87         }
88 }
89
90 vector HUD_Get_Num_Color (float x, float maxvalue)
91 {
92         float blinkingamt;
93         vector color;
94         if(x >= maxvalue) {
95                 color_x = sin(2*M_PI*time);
96                 color_y = 1;
97                 color_z = sin(2*M_PI*time);
98         }
99         else if(x > maxvalue * 0.75) {
100                 color_x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
101                 color_y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
102                 color_z = 0;
103         }
104         else if(x > maxvalue * 0.5) {
105                 color_x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
106                 color_y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
107                 color_z = 1 - (x-100)*0.02; // blue value between 1 -> 0
108         }
109         else if(x > maxvalue * 0.25) {
110                 color_x = 1;
111                 color_y = 1;
112                 color_z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
113         }
114         else if(x > maxvalue * 0.1) {
115                 color_x = 1;
116                 color_y = (x-20)*90/27/100; // green value between 0 -> 1
117                 color_z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
118         }
119         else {
120                 color_x = 1;
121                 color_y = 0;
122                 color_z = 0;
123         }
124
125         blinkingamt = (1 - x/maxvalue/0.25);
126         if(blinkingamt > 0)
127         {
128                 color_x = color_x - color_x * blinkingamt * sin(2*M_PI*time);
129                 color_y = color_y - color_y * blinkingamt * sin(2*M_PI*time);
130                 color_z = color_z - color_z * blinkingamt * sin(2*M_PI*time);
131         }
132         return color;
133 }
134
135 float stringwidth_colors(string s, vector theSize)
136 {
137         return stringwidth(s, TRUE, theSize);
138 }
139
140 float stringwidth_nocolors(string s, vector theSize)
141 {
142         return stringwidth(s, FALSE, theSize);
143 }
144
145 #define CENTERPRINT_MAX_LINES 30
146 string centerprint_messages[CENTERPRINT_MAX_LINES];
147 float centerprint_width[CENTERPRINT_MAX_LINES];
148 float centerprint_time;
149 float centerprint_expire;
150 float centerprint_num;
151 float centerprint_offset_hint;
152 vector centerprint_fontsize;
153
154 void centerprint(string strMessage)
155 {
156         float i, j, n, hcount;
157         string s;
158
159         centerprint_fontsize = HUD_GetFontsize("scr_centersize");
160
161         centerprint_expire = min(centerprint_expire, time); // if any of the returns happens, this message will fade out
162
163         if(autocvar_scr_centertime <= 0)
164                 return;
165
166         if(strMessage == "")
167                 return;
168
169         // strip trailing newlines
170         j = strlen(strMessage) - 1;
171         while(substring(strMessage, j, 1) == "\n" && j >= 0)
172                 j = j - 1;
173         strMessage = substring(strMessage, 0, j + 1);
174
175         if(strMessage == "")
176                 return;
177
178         // strip leading newlines and remember them, they are a hint that the message should be lower on the screen
179         j = 0;
180         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
181                 j = j + 1;
182         strMessage = substring(strMessage, j, strlen(strMessage) - j);
183         centerprint_offset_hint = j;
184
185         if(strMessage == "")
186                 return;
187
188         // if we get here, we have a message. Initialize its height.
189         centerprint_num = 0;
190
191         n = tokenizebyseparator(strMessage, "\n");
192         i = hcount = 0;
193         for(j = 0; j < n; ++j)
194         {
195                 getWrappedLine_remaining = argv(j);
196                 while(getWrappedLine_remaining)
197                 {
198                         s = getWrappedLine(vid_conwidth * 0.75, centerprint_fontsize, stringwidth_colors);
199                         if(centerprint_messages[i] != s) // don't fade the same message in, looks stupid
200                                 centerprint_time = time;
201                         if(centerprint_messages[i])
202                                 strunzone(centerprint_messages[i]);
203                         centerprint_messages[i] = strzone(s);
204                         centerprint_width[i] = stringwidth(s, TRUE, centerprint_fontsize);
205                         ++i;
206
207                         // half height for empty lines looks better
208                         if(s == "")
209                                 hcount += 0.5;
210                         else
211                                 hcount += 1;
212
213                         if(i >= CENTERPRINT_MAX_LINES)
214                                 break;
215                 }
216         }
217
218         float h, havail;
219         h = centerprint_fontsize_y*hcount;
220
221         havail = vid_conheight;
222         if(autocvar_con_chatpos < 0)
223                 havail -= (-autocvar_con_chatpos + autocvar_con_chat) * autocvar_con_chatsize; // avoid overlapping chat
224         if(havail > vid_conheight - 70)
225                 havail = vid_conheight - 70; // avoid overlapping HUD
226
227 #if 0
228         float forbiddenmin, forbiddenmax, allowedmin, allowedmax, preferred;
229
230         // here, the centerprint would cover the crosshair. REALLY BAD.
231         forbiddenmin = vid_conheight * 0.5 - h - 16;
232         forbiddenmax = vid_conheight * 0.5 + 16;
233
234         allowedmin = scoreboard_bottom;
235         allowedmax = havail - h;
236         preferred = (havail - h)/2;
237
238
239         // possible orderings (total: 4! / 4 = 6)
240         //  allowedmin allowedmax forbiddenmin forbiddenmax
241         //  forbiddenmin forbiddenmax allowedmin allowedmax
242         if(allowedmax < forbiddenmin || allowedmin > forbiddenmax)
243         {
244                 // forbidden doesn't matter in this case
245                 centerprint_start_y = bound(allowedmin, preferred, allowedmax);
246         }
247         //  allowedmin forbiddenmin allowedmax forbiddenmax
248         else if(allowedmin < forbiddenmin && allowedmax < forbiddenmax)
249         {
250                 centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
251         }
252         //  allowedmin forbiddenmin forbiddenmax allowedmax
253         else if(allowedmin < forbiddenmin)
254         {
255                 // make sure the forbidden zone is not covered
256                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
257                         centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
258                 else
259                         centerprint_start_y = bound(forbiddenmax, preferred, allowedmin);
260         }
261         //  forbiddenmin allowedmin allowedmax forbiddenmax
262         else if(allowedmax < forbiddenmax)
263         {
264                 // it's better to leave the allowed zone (overlap with scoreboard) than
265                 // to cover the forbidden zone (crosshair)
266                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
267                         centerprint_start_y = forbiddenmax;
268                 else
269                         centerprint_start_y = forbiddenmin;
270         }
271         //  forbiddenmin allowedmin forbiddenmax allowedmax
272         else
273         {
274                 centerprint_start_y = bound(forbiddenmax, preferred, allowedmax);
275         }
276 #else
277 #endif
278
279         centerprint_num = i;
280
281         centerprint_expire = time + autocvar_scr_centertime;
282 }
283
284 void HUD_DrawCenterPrint (void)
285 {
286         float i;
287         vector pos;
288         string ts;
289         float a, sz;
290
291         if(time - centerprint_time < 0.25)
292                 a = (time - centerprint_time) / 0.25;
293         else
294                 a = bound(0, 1 - 4 * (time - centerprint_expire), 1);
295
296         if(a <= 0)
297                 return;
298
299         sz = 0.8 + (a / 5);
300
301         if(centerprint_num * autocvar_scr_centersize > 24 && scoreboard_active) // 24 = height of Scoreboard text
302                 centerprint_start_y = scoreboard_bottom + centerprint_fontsize_y;
303
304         pos = centerprint_start;
305         for (i=0; i<centerprint_num; i = i + 1)
306         {
307                 ts = centerprint_messages[i];
308                 drawfontscale = sz * '1 1 0';
309                 pos_x = (vid_conwidth - stringwidth(ts, TRUE, centerprint_fontsize)) * 0.5;
310                 if (ts != "")
311                 {
312                         drawcolorcodedstring(pos + '0 1 0' * (1 - sz) * 0.5 *centerprint_fontsize_y, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
313                         pos_y = pos_y + centerprint_fontsize_y;
314                 }
315                 else
316                         // half height for empty lines looks better
317                         pos_y = pos_y + sz * centerprint_fontsize_y * 0.5;
318                 drawfontscale = '1 1 0';
319         }
320 }
321
322 void drawstringright(vector position, string text, vector scale, vector rgb, float alpha, float flag)
323 {
324         position_x -= 2 / 3 * strlen(text) * scale_x;
325         drawstring(position, text, scale, rgb, alpha, flag);
326 }
327
328 void drawstringcenter(vector position, string text, vector scale, vector rgb, float alpha, float flag)
329 {
330         position_x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * scale_x);
331         drawstring(position, text, scale, rgb, alpha, flag);
332 }
333
334 // return the string of the given race place
335 string race_PlaceName(float pos) {
336         if(pos == 1)
337                 return _("1st");
338         else if(pos == 2)
339                 return _("2nd");
340         else if(pos == 3)
341                 return _("3rd");
342         else
343                 return sprintf(_("%dth"), pos);
344 }
345
346 // return the string of the onscreen race timer
347 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
348 {
349         string col;
350         string timestr;
351         string cpname;
352         string lapstr;
353         lapstr = "";
354
355         if(histime == 0) // goal hit
356         {
357                 if(mytime > 0)
358                 {
359                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
360                         col = "^1";
361                 }
362                 else if(mytime == 0)
363                 {
364                         timestr = "+0.0";
365                         col = "^3";
366                 }
367                 else
368                 {
369                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
370                         col = "^2";
371                 }
372
373                 if(lapdelta > 0)
374                 {
375                         lapstr = sprintf(_(" (-%dL)"), lapdelta);
376                         col = "^2";
377                 }
378                 else if(lapdelta < 0)
379                 {
380                         lapstr = sprintf(_(" (+%dL)"), -lapdelta);
381                         col = "^1";
382                 }
383         }
384         else if(histime > 0) // anticipation
385         {
386                 if(mytime >= histime)
387                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
388                 else
389                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
390                 col = "^3";
391         }
392         else
393                 col = "^7";
394
395         if(cp == 254)
396                 cpname = _("Start line");
397         else if(cp == 255)
398                 cpname = _("Finish line");
399         else if(cp)
400                 cpname = sprintf(_("Intermediate %d"), cp);
401         else
402                 cpname = _("Finish line");
403
404         if(histime < 0)
405                 return strcat(col, cpname);
406         else if(hisname == "")
407                 return strcat(col, sprintf(_("%s (%s)"), cpname, timestr));
408         else
409                 return strcat(col, sprintf(_("%s (%s %s)"), cpname, timestr, strcat(hisname, col, lapstr)));
410 }
411
412 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
413 float race_CheckName(string net_name) {
414         float i;
415         for (i=RANKINGS_CNT-1;i>=0;--i)
416                 if(grecordholder[i] == net_name)
417                         return i+1;
418         return 0;
419 }
420
421 /*
422 ==================
423 HUD panels
424 ==================
425 */
426
427 #define HUD_Write(s) fputs(fh, s)
428 // q: quoted, n: not quoted
429 #define HUD_Write_Cvar_n(cvar) HUD_Write(strcat("seta ", cvar, " ", cvar_string(cvar), "\n"))
430 #define HUD_Write_Cvar_q(cvar) HUD_Write(strcat("seta ", cvar, " \"", cvar_string(cvar), "\"\n"))
431 #define HUD_Write_PanelCvar_n(cvar_suf) HUD_Write_Cvar_n(strcat("hud_panel_", panel_name, cvar_suf))
432 #define HUD_Write_PanelCvar_q(cvar_suf) HUD_Write_Cvar_q(strcat("hud_panel_", panel_name, cvar_suf))
433 // Save the config
434 void HUD_Panel_ExportCfg(string cfgname)
435 {
436         float fh;
437         string filename = strcat("hud_", autocvar_hud_skin, "_", cfgname, ".cfg");
438         fh = fopen(filename, FILE_WRITE);
439         if(fh >= 0)
440         {
441                 HUD_Write_Cvar_q("hud_skin");
442                 HUD_Write_Cvar_q("hud_panel_bg");
443                 HUD_Write_Cvar_q("hud_panel_bg_color");
444                 HUD_Write_Cvar_q("hud_panel_bg_color_team");
445                 HUD_Write_Cvar_q("hud_panel_bg_alpha");
446                 HUD_Write_Cvar_q("hud_panel_bg_border");
447                 HUD_Write_Cvar_q("hud_panel_bg_padding");
448                 HUD_Write_Cvar_q("hud_panel_fg_alpha");
449                 HUD_Write("\n");
450
451                 HUD_Write_Cvar_q("hud_dock");
452                 HUD_Write_Cvar_q("hud_dock_color");
453                 HUD_Write_Cvar_q("hud_dock_color_team");
454                 HUD_Write_Cvar_q("hud_dock_alpha");
455                 HUD_Write("\n");
456
457                 HUD_Write_Cvar_q("hud_progressbar_alpha");
458                 HUD_Write_Cvar_q("hud_progressbar_strength_color");
459                 HUD_Write_Cvar_q("hud_progressbar_shield_color");
460                 HUD_Write_Cvar_q("hud_progressbar_health_color");
461                 HUD_Write_Cvar_q("hud_progressbar_armor_color");
462                 HUD_Write_Cvar_q("hud_progressbar_fuel_color");
463                 HUD_Write_Cvar_q("hud_progressbar_nexball_color");
464                 HUD_Write("\n");
465
466                 HUD_Write_Cvar_q("_hud_panelorder");
467                 HUD_Write("\n");
468
469                 HUD_Write_Cvar_q("hud_configure_grid");
470                 HUD_Write_Cvar_q("hud_configure_grid_xsize");
471                 HUD_Write_Cvar_q("hud_configure_grid_ysize");
472                 HUD_Write("\n");
473
474                 HUD_Write_Cvar_q("scr_centerpos");
475                 HUD_Write("\n");
476
477                 // common cvars for all panels
478                 float i;
479                 for (i = 0; i < HUD_PANEL_NUM; ++i)
480                 {
481                         HUD_Panel_GetName(i);
482
483                         HUD_Write_PanelCvar_n("");
484                         HUD_Write_PanelCvar_q("_pos");
485                         HUD_Write_PanelCvar_q("_size");
486                         HUD_Write_PanelCvar_q("_bg");
487                         HUD_Write_PanelCvar_q("_bg_color");
488                         HUD_Write_PanelCvar_q("_bg_color_team");
489                         HUD_Write_PanelCvar_q("_bg_alpha");
490                         HUD_Write_PanelCvar_q("_bg_border");
491                         HUD_Write_PanelCvar_q("_bg_padding");
492                         switch(i) {
493                                 case HUD_PANEL_WEAPONS:
494                                         HUD_Write_PanelCvar_q("_complainbubble");
495                                         HUD_Write_PanelCvar_q("_complainbubble_padding");
496                                         HUD_Write_PanelCvar_q("_complainbubble_color_outofammo");
497                                         HUD_Write_PanelCvar_q("_complainbubble_color_donthave");
498                                         HUD_Write_PanelCvar_q("_complainbubble_color_unavailable");
499                                         HUD_Write_PanelCvar_q("_ammo_color");
500                                         HUD_Write_PanelCvar_q("_ammo_alpha");
501                                         HUD_Write_PanelCvar_q("_aspect");
502                                         HUD_Write_PanelCvar_q("_timeout");
503                                         HUD_Write_PanelCvar_q("_timeout_effect");
504                                         break;
505                                 case HUD_PANEL_AMMO:
506                                         HUD_Write_PanelCvar_q("_onlycurrent");
507                                         HUD_Write_PanelCvar_q("_iconalign");
508                                         HUD_Write_PanelCvar_q("_progressbar");
509                                         HUD_Write_PanelCvar_q("_progressbar_name");
510                                         HUD_Write_PanelCvar_q("_progressbar_xoffset");
511                                         HUD_Write_PanelCvar_q("_text");
512                                         break;
513                                 case HUD_PANEL_POWERUPS:
514                                         HUD_Write_PanelCvar_q("_flip");
515                                         HUD_Write_PanelCvar_q("_iconalign");
516                                         HUD_Write_PanelCvar_q("_baralign");
517                                         HUD_Write_PanelCvar_q("_progressbar");
518                                         HUD_Write_PanelCvar_q("_progressbar_strength");
519                                         HUD_Write_PanelCvar_q("_progressbar_shield");
520                                         HUD_Write_PanelCvar_q("_text");
521                                         break;
522                                 case HUD_PANEL_HEALTHARMOR:
523                                         HUD_Write_PanelCvar_q("_flip");
524                                         HUD_Write_PanelCvar_q("_iconalign");
525                                         HUD_Write_PanelCvar_q("_baralign");
526                                         HUD_Write_PanelCvar_q("_progressbar");
527                                         HUD_Write_PanelCvar_q("_progressbar_health");
528                                         HUD_Write_PanelCvar_q("_progressbar_armor");
529                                         HUD_Write_PanelCvar_q("_text");
530                                         break;
531                                 case HUD_PANEL_NOTIFY:
532                                         HUD_Write_PanelCvar_q("_flip");
533                                         HUD_Write_PanelCvar_q("_fontsize");
534                                         HUD_Write_PanelCvar_q("_print");
535                                         break;
536                                 case HUD_PANEL_RADAR:
537                                         HUD_Write_PanelCvar_q("_foreground_alpha");
538                                         break;
539                                 case HUD_PANEL_VOTE:
540                                         HUD_Write_PanelCvar_q("_alreadyvoted_alpha");
541                                         break;
542                                 case HUD_PANEL_PRESSEDKEYS:
543                                         HUD_Write_PanelCvar_q("_aspect");
544                                         break;
545                                 case HUD_PANEL_INFOMESSAGES:
546                                         HUD_Write_PanelCvar_q("_flip");
547                                         break;
548                         }
549                         HUD_Write("\n");
550                 }
551                 HUD_Write("menu_sync\n"); // force the menu to reread the cvars, so that the dialogs are updated
552
553                 print(sprintf(_("^2Successfully exported to %s! (Note: It's saved in data/data/)\n"), filename));
554                 fclose(fh);
555         }
556         else
557                 print(sprintf(_("^1Couldn't write to %s\n"), filename));
558 }
559
560 const float hlBorderSize = 4;
561 const string hlBorder = "gfx/hud/default/border_highlighted";
562 const string hlBorder2 = "gfx/hud/default/border_highlighted2";
563 void HUD_Panel_HlBorder(float myBorder, vector color, float alpha)
564 {
565         drawfill(panel_pos - '1 1 0' * myBorder, panel_size + '2 2 0' * myBorder, '0 0.5 1', .5 * alpha, DRAWFLAG_NORMAL);
566         drawpic_tiled(panel_pos - '1 1 0' * myBorder, hlBorder, '8 1 0' * hlBorderSize, eX * (panel_size_x + 2 * myBorder) + eY * hlBorderSize, color, alpha, DRAWFLAG_NORMAL);
567         drawpic_tiled(panel_pos - '1 1 0' * myBorder + eY * (panel_size_y + 2 * myBorder - hlBorderSize), hlBorder, '8 1 0' * hlBorderSize, eX * (panel_size_x + 2 * myBorder) + eY * hlBorderSize, color, alpha, DRAWFLAG_NORMAL);
568         drawpic_tiled(panel_pos - '1 1 0' * myBorder + eY * hlBorderSize, hlBorder2, '1 8 0' * hlBorderSize, eY * (panel_size_y + 2 * myBorder - 2 * hlBorderSize) + eX * hlBorderSize, color, alpha, DRAWFLAG_NORMAL);
569         drawpic_tiled(panel_pos - '1 1 0' * myBorder + eY * hlBorderSize + eX * (panel_size_x + 2 * myBorder - hlBorderSize), hlBorder2, '1 8 0' * hlBorderSize, eY * (panel_size_y + 2 * myBorder - 2 * hlBorderSize) + eX * hlBorderSize, color, alpha, DRAWFLAG_NORMAL);
570 }
571
572 // draw the background/borders
573 #define HUD_Panel_DrawBg(alpha)\
574 if(panel_bg != "0")\
575         draw_BorderPicture(panel_pos - '1 1 0' * panel_bg_border, panel_bg, panel_size + '1 1 0' * 2 * panel_bg_border, panel_bg_color, panel_bg_alpha * alpha, '1 1 0' * (panel_bg_border/BORDER_MULTIPLIER));\
576 if(highlightedPanel_prev == active_panel && autocvar__hud_configure)\
577 {\
578         HUD_Panel_HlBorder(panel_bg_border + 1.5 * hlBorderSize, '0 0.5 1', 0.25 * (1 - autocvar__menu_alpha) * alpha);\
579 } ENDS_WITH_CURLY_BRACE
580
581 void HUD_Panel_DrawProgressBar(vector pos, vector mySize, string pic, float vertical, float barflip, float x, vector color, float alpha, float drawflag)
582 {
583         if(!alpha || x == 0)
584                 return;
585
586     x = bound(0, x, 1);
587
588         if(vertical) {
589                 pic = strcat(hud_skin_path, "/", pic, "_vertical");
590                 if(precache_pic(pic) == "") {
591                         pic = "gfx/hud/default/statusbar_vertical";
592                 }
593
594         if(barflip)
595             drawsetcliparea(pos_x, pos_y + mySize_y * (1 - x), mySize_x, mySize_y * x);
596         else
597             drawsetcliparea(pos_x, pos_y, mySize_x, mySize_y * x);
598         } else {
599                 pic = strcat(hud_skin_path, "/", pic);
600                 if(precache_pic(pic) == "") {
601                         pic = "gfx/hud/default/statusbar";
602                 }
603
604         if(barflip)
605             drawsetcliparea(pos_x + mySize_x * (1 - x), pos_y, mySize_x * x, mySize_y);
606         else
607             drawsetcliparea(pos_x, pos_y, mySize_x * x, mySize_y);
608         }
609
610     drawpic(pos, pic, mySize, color, alpha, drawflag);
611     drawresetcliparea();
612 }
613
614 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float alpha, float drawflag)
615 {
616         if(!alpha)
617                 return;
618
619         string pic;
620         pic = strcat(hud_skin_path, "/num_leading");
621         if(precache_pic(pic) == "") {
622                 pic = "gfx/hud/default/num_leading";
623         }
624
625         drawsubpic(pos, eX * min(mySize_x * 0.5, mySize_y) + eY * mySize_y, pic, '0 0 0', '0.25 1 0', color, alpha, drawflag);
626         if(mySize_x/mySize_y > 2)
627                 drawsubpic(pos + eX * mySize_y, eX * (mySize_x - 2 * mySize_y) + eY * mySize_y, pic, '0.25 0 0', '0.5 1 0', color, alpha, drawflag);
628         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, alpha, drawflag);
629 }
630
631 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
632 vector HUD_Panel_CheckMove(vector myPos, vector mySize)
633 {
634         float i;
635
636         vector myTarget;
637         myTarget = myPos;
638
639         vector myCenter;
640         vector targCenter;
641         myCenter = '0 0 0'; // shut up fteqcc, there IS a reference
642         targCenter = '0 0 0'; // shut up fteqcc, there IS a reference
643
644         for (i = 0; i < HUD_PANEL_NUM; ++i) {
645                 if(i == highlightedPanel || !panel_enabled)
646                         continue;
647
648                 HUD_Panel_UpdatePosSizeForId(i);
649
650                 panel_pos -= '1 1 0' * panel_bg_border;
651                 panel_size += '2 2 0' * panel_bg_border;
652
653                 if(myPos_y + mySize_y < panel_pos_y)
654                         continue;
655                 if(myPos_y > panel_pos_y + panel_size_y)
656                         continue;
657
658                 if(myPos_x + mySize_x < panel_pos_x)
659                         continue;
660                 if(myPos_x > panel_pos_x + panel_size_x)
661                         continue;
662
663                 // OK, there IS a collision.
664
665                 myCenter_x = myPos_x + 0.5 * mySize_x;
666                 myCenter_y = myPos_y + 0.5 * mySize_y;
667
668                 targCenter_x = panel_pos_x + 0.5 * panel_size_x;
669                 targCenter_y = panel_pos_y + 0.5 * panel_size_y;
670
671                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y) // top left (of the target panel)
672                 {
673                         if(myPos_x + mySize_x - panel_pos_x < myPos_y + mySize_y - panel_pos_y) // push it to the side
674                                 myTarget_x = panel_pos_x - mySize_x;
675                         else // push it upwards
676                                 myTarget_y = panel_pos_y - mySize_y;
677                 }
678                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y) // top right
679                 {
680                         if(panel_pos_x + panel_size_x - myPos_x < myPos_y + mySize_y - panel_pos_y) // push it to the side
681                                 myTarget_x = panel_pos_x + panel_size_x;
682                         else // push it upwards
683                                 myTarget_y = panel_pos_y - mySize_y;
684                 }
685                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
686                 {
687                         if(myPos_x + mySize_x - panel_pos_x < panel_pos_y + panel_size_y - myPos_y) // push it to the side
688                                 myTarget_x = panel_pos_x - mySize_x;
689                         else // push it downwards
690                                 myTarget_y = panel_pos_y + panel_size_y;
691                 }
692                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
693                 {
694                         if(panel_pos_x + panel_size_x - myPos_x < panel_pos_y + panel_size_y - myPos_y) // push it to the side
695                                 myTarget_x = panel_pos_x + panel_size_x;
696                         else // push it downwards
697                                 myTarget_y = panel_pos_y + panel_size_y;
698                 }
699                 //if(cvar("hud_configure_checkcollisions_debug"))
700                         //drawfill(panel_pos, panel_size, '1 1 0', .3, DRAWFLAG_NORMAL);
701         }
702
703         return myTarget;
704 }
705
706 void HUD_Panel_SetPos(vector pos)
707 {
708         HUD_Panel_UpdatePosSizeForId(highlightedPanel);
709         vector mySize;
710         mySize = panel_size;
711
712         //if(cvar("hud_configure_checkcollisions_debug"))
713                 //drawfill(pos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
714
715         if(autocvar_hud_configure_grid)
716         {
717                 pos_x = floor((pos_x/vid_conwidth)/bound(0.005, autocvar_hud_configure_grid_xsize, 0.2) + 0.5) * bound(0.005, autocvar_hud_configure_grid_xsize, 0.2) * vid_conwidth;
718                 pos_y = floor((pos_y/vid_conheight)/bound(0.005, autocvar_hud_configure_grid_ysize, 0.2) + 0.5) * bound(0.005, autocvar_hud_configure_grid_ysize, 0.2) * vid_conheight;
719         }
720
721         if(hud_configure_checkcollisions)
722                 pos = HUD_Panel_CheckMove(pos, mySize);
723
724         pos_x = bound(0, pos_x, vid_conwidth - mySize_x);
725         pos_y = bound(0, pos_y, vid_conheight - mySize_y);
726
727         string s;
728         s = strcat(ftos(pos_x/vid_conwidth), " ", ftos(pos_y/vid_conheight));
729
730         HUD_Panel_GetName(highlightedPanel);
731         cvar_set(strcat("hud_panel_", panel_name, "_pos"), s);
732 }
733
734 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
735 vector HUD_Panel_CheckResize(vector mySize, vector resizeorigin) {
736         float i;
737
738         vector targEndPos;
739
740         float dist_x, dist_y;
741         float ratio;
742         ratio = mySize_x/mySize_y;
743
744         for (i = 0; i < HUD_PANEL_NUM; ++i) {
745                 if(i == highlightedPanel || !panel_enabled)
746                         continue;
747
748                 HUD_Panel_UpdatePosSizeForId(i);
749
750                 panel_pos -= '1 1 0' * panel_bg_border;
751                 panel_size += '2 2 0' * panel_bg_border;
752
753                 targEndPos = panel_pos + panel_size;
754
755                 // resizeorigin is WITHIN target panel, just abort any collision testing against that particular panel to produce expected behaviour!
756                 if(resizeorigin_x > panel_pos_x && resizeorigin_x < targEndPos_x && resizeorigin_y > panel_pos_y && resizeorigin_y < targEndPos_y)
757                         continue;
758
759                 if (resizeCorner == 1)
760                 {
761                         // check if this panel is on our way
762                         if (resizeorigin_x <= panel_pos_x)
763                                 continue;
764                         if (resizeorigin_y <= panel_pos_y)
765                                 continue;
766                         if (targEndPos_x <= resizeorigin_x - mySize_x)
767                                 continue;
768                         if (targEndPos_y <= resizeorigin_y - mySize_y)
769                                 continue;
770
771                         // there is a collision:
772                         // detect which side of the panel we are facing is actually limiting the resizing
773                         // (which side the resize direction finds for first) and reduce the size up to there
774                         //
775                         // dist is the distance between resizeorigin and the "analogous" point of the panel
776                         // in this case resizeorigin (bottom-right point) and the bottom-right point of the panel
777                         dist_x = resizeorigin_x - targEndPos_x;
778                         dist_y = resizeorigin_y - targEndPos_y;
779                         if (dist_y <= 0 || dist_x / dist_y > ratio)
780                                 mySize_x = min(mySize_x, dist_x);
781                         else
782                                 mySize_y = min(mySize_y, dist_y);
783                 }
784                 else if (resizeCorner == 2)
785                 {
786                         if (resizeorigin_x >= targEndPos_x)
787                                 continue;
788                         if (resizeorigin_y <= panel_pos_y)
789                                 continue;
790                         if (panel_pos_x >= resizeorigin_x + mySize_x)
791                                 continue;
792                         if (targEndPos_y <= resizeorigin_y - mySize_y)
793                                 continue;
794
795                         dist_x = panel_pos_x - resizeorigin_x;
796                         dist_y = resizeorigin_y - targEndPos_y;
797                         if (dist_y <= 0 || dist_x / dist_y > ratio)
798                                 mySize_x = min(mySize_x, dist_x);
799                         else
800                                 mySize_y = min(mySize_y, dist_y);
801                 }
802                 else if (resizeCorner == 3)
803                 {
804                         if (resizeorigin_x <= panel_pos_x)
805                                 continue;
806                         if (resizeorigin_y >= targEndPos_y)
807                                 continue;
808                         if (targEndPos_x <= resizeorigin_x - mySize_x)
809                                 continue;
810                         if (panel_pos_y >= resizeorigin_y + mySize_y)
811                                 continue;
812
813                         dist_x = resizeorigin_x - targEndPos_x;
814                         dist_y = panel_pos_y - resizeorigin_y;
815                         if (dist_y <= 0 || dist_x / dist_y > ratio)
816                                 mySize_x = min(mySize_x, dist_x);
817                         else
818                                 mySize_y = min(mySize_y, dist_y);
819                 }
820                 else if (resizeCorner == 4)
821                 {
822                         if (resizeorigin_x >= targEndPos_x)
823                                 continue;
824                         if (resizeorigin_y >= targEndPos_y)
825                                 continue;
826                         if (panel_pos_x >= resizeorigin_x + mySize_x)
827                                 continue;
828                         if (panel_pos_y >= resizeorigin_y + mySize_y)
829                                 continue;
830
831                         dist_x = panel_pos_x - resizeorigin_x;
832                         dist_y = panel_pos_y - resizeorigin_y;
833                         if (dist_y <= 0 || dist_x / dist_y > ratio)
834                                 mySize_x = min(mySize_x, dist_x);
835                         else
836                                 mySize_y = min(mySize_y, dist_y);
837                 }
838                 //if(cvar("hud_configure_checkcollisions_debug"))
839                         //drawfill(panel_pos, panel_size, '1 1 0', .3, DRAWFLAG_NORMAL);
840         }
841
842         return mySize;
843 }
844
845 void HUD_Panel_SetPosSize(vector mySize)
846 {
847         HUD_Panel_UpdatePosSizeForId(highlightedPanel);
848         vector resizeorigin;
849         resizeorigin = panel_click_resizeorigin;
850         vector myPos;
851
852         // minimum panel size cap
853         mySize_x = max(0.025 * vid_conwidth, mySize_x);
854         mySize_y = max(0.025 * vid_conheight, mySize_y);
855
856         if(highlightedPanel == HUD_PANEL_CHAT) // some panels have their own restrictions, like the chat panel (which actually only moves the engine chat print around). Looks bad if it's too small.
857         {
858                 mySize_x = max(17 * autocvar_con_chatsize, mySize_x);
859                 mySize_y = max(2 * autocvar_con_chatsize + 2 * panel_bg_padding, mySize_y);
860         }
861
862         // collision testing|
863         // -----------------+
864
865         // we need to know pos at this stage, but it might still change later if we hit a screen edge/other panel (?)
866         if(resizeCorner == 1) {
867                 myPos_x = resizeorigin_x - mySize_x;
868                 myPos_y = resizeorigin_y - mySize_y;
869         } else if(resizeCorner == 2) {
870                 myPos_x = resizeorigin_x;
871                 myPos_y = resizeorigin_y - mySize_y;
872         } else if(resizeCorner == 3) {
873                 myPos_x = resizeorigin_x - mySize_x;
874                 myPos_y = resizeorigin_y;
875         } else { // resizeCorner == 4
876                 myPos_x = resizeorigin_x;
877                 myPos_y = resizeorigin_y;
878         }
879
880         // left/top screen edges
881         if(myPos_x < 0)
882                 mySize_x = mySize_x + myPos_x;
883         if(myPos_y < 0)
884                 mySize_y = mySize_y + myPos_y;
885
886         // bottom/right screen edges
887         if(myPos_x + mySize_x > vid_conwidth)
888                 mySize_x = vid_conwidth - myPos_x;
889         if(myPos_y + mySize_y > vid_conheight)
890                 mySize_y = vid_conheight - myPos_y;
891
892         //if(cvar("hud_configure_checkcollisions_debug"))
893                 //drawfill(myPos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
894
895         // before checkresize, otherwise panel can be snapped partially inside another panel or panel aspect ratio can be broken
896         if(autocvar_hud_configure_grid)
897         {
898                 mySize_x = floor((mySize_x/vid_conwidth)/bound(0.005, autocvar_hud_configure_grid_xsize, 0.2) + 0.5) * bound(0.005, autocvar_hud_configure_grid_xsize, 0.2) * vid_conwidth;
899                 mySize_y = floor((mySize_y/vid_conheight)/bound(0.005, autocvar_hud_configure_grid_ysize, 0.2) + 0.5) * bound(0.005, autocvar_hud_configure_grid_ysize, 0.2) * vid_conheight;
900         }
901
902         if(hud_configure_checkcollisions)
903                 mySize = HUD_Panel_CheckResize(mySize, resizeorigin);
904
905         // minimum panel size cap, do this once more so we NEVER EVER EVER have a panel smaller than this, JUST IN CASE above code still makes the panel eg negative (impossible to resize back without changing cvars manually then)
906         mySize_x = max(0.025 * vid_conwidth, mySize_x);
907         mySize_y = max(0.025 * vid_conheight, mySize_y);
908
909         // do another pos check, as size might have changed by now
910         if(resizeCorner == 1) {
911                 myPos_x = resizeorigin_x - mySize_x;
912                 myPos_y = resizeorigin_y - mySize_y;
913         } else if(resizeCorner == 2) {
914                 myPos_x = resizeorigin_x;
915                 myPos_y = resizeorigin_y - mySize_y;
916         } else if(resizeCorner == 3) {
917                 myPos_x = resizeorigin_x - mySize_x;
918                 myPos_y = resizeorigin_y;
919         } else { // resizeCorner == 4
920                 myPos_x = resizeorigin_x;
921                 myPos_y = resizeorigin_y;
922         }
923
924         //if(cvar("hud_configure_checkcollisions_debug"))
925                 //drawfill(myPos, mySize, '0 1 0', .3, DRAWFLAG_NORMAL);
926
927         HUD_Panel_GetName(highlightedPanel);
928         string s;
929         s = strcat(ftos(mySize_x/vid_conwidth), " ", ftos(mySize_y/vid_conheight));
930         cvar_set(strcat("hud_panel_", panel_name, "_size"), s);
931
932         s = strcat(ftos(myPos_x/vid_conwidth), " ", ftos(myPos_y/vid_conheight));
933         cvar_set(strcat("hud_panel_", panel_name, "_pos"), s);
934 }
935
936 float mouseClicked;
937 float prevMouseClicked; // previous state
938 float prevMouseClickedTime; // time during previous mouse click, to check for doubleclicks
939 vector prevMouseClickedPos; // pos during previous mouse click, to check for doubleclicks
940
941 float pressed_key_time;
942 void HUD_Panel_Arrow_Action(float nPrimary)
943 {
944         if (highlightedPanel_prev == -1 || mouseClicked)
945                 return;
946
947         hud_configure_checkcollisions = (!(hudShiftState & S_CTRL) && autocvar_hud_configure_checkcollisions);
948
949         float step;
950         if(autocvar_hud_configure_grid)
951         {
952                 if (nPrimary == K_UPARROW || nPrimary == K_DOWNARROW)
953                 {
954                         if (hudShiftState & S_SHIFT)
955                                 step = bound(0.005, autocvar_hud_configure_grid_ysize, 0.2) * vid_conheight;
956                         else
957                                 step = 2 * bound(0.005, autocvar_hud_configure_grid_ysize, 0.2) * vid_conheight;
958                 }
959                 else
960                 {
961                         if (hudShiftState & S_SHIFT)
962                                 step = bound(0.005, autocvar_hud_configure_grid_xsize, 0.2) * vid_conwidth;
963                         else
964                                 step = 2 * bound(0.005, autocvar_hud_configure_grid_xsize, 0.2) * vid_conwidth;
965                 }
966         }
967         else
968         {
969                 if (nPrimary == K_UPARROW || nPrimary == K_DOWNARROW)
970                         step = vid_conheight;
971                 else
972                         step = vid_conwidth;
973                 if (hudShiftState & S_SHIFT)
974                         step = (step / 256); // more precision
975                 else
976                         step = (step / 64) * (1 + 2 * (time - pressed_key_time));
977         }
978
979         highlightedPanel = highlightedPanel_prev;
980
981         HUD_Panel_UpdatePosSizeForId(highlightedPanel);
982
983         vector prev_pos, prev_size;
984         prev_pos = panel_pos;
985         prev_size = panel_size;
986
987         if (hudShiftState & S_ALT) // resize
988         {
989                 highlightedAction = 1;
990                 if(nPrimary == K_UPARROW)
991                         resizeCorner = 1;
992                 else if(nPrimary == K_RIGHTARROW)
993                         resizeCorner = 2;
994                 else if(nPrimary == K_LEFTARROW)
995                         resizeCorner = 3;
996                 else // if(nPrimary == K_DOWNARROW)
997                         resizeCorner = 4;
998
999                 // ctrl+arrow reduces the size, instead of increasing it
1000                 // Note that ctrl disables collisions check too, but it's fine
1001                 // since we don't collide with anything reducing the size
1002                 if (hudShiftState & S_CTRL) {
1003                         step = -step;
1004                         resizeCorner = 5 - resizeCorner;
1005                 }
1006
1007                 vector mySize;
1008                 mySize = panel_size;
1009                 panel_click_resizeorigin = panel_pos;
1010                 if(resizeCorner == 1) {
1011                         panel_click_resizeorigin += mySize;
1012                         mySize_y += step;
1013                 } else if(resizeCorner == 2) {
1014                         panel_click_resizeorigin_y += mySize_y;
1015                         mySize_x += step;
1016                 } else if(resizeCorner == 3) {
1017                         panel_click_resizeorigin_x += mySize_x;
1018                         mySize_x += step;
1019                 } else { // resizeCorner == 4
1020                         mySize_y += step;
1021                 }
1022                 HUD_Panel_SetPosSize(mySize);
1023         }
1024         else // move
1025         {
1026                 highlightedAction = 2;
1027                 vector pos;
1028                 pos = panel_pos;
1029                 if(nPrimary == K_UPARROW)
1030                         pos_y -= step;
1031                 else if(nPrimary == K_DOWNARROW)
1032                         pos_y += step;
1033                 else if(nPrimary == K_LEFTARROW)
1034                         pos_x -= step;
1035                 else // if(nPrimary == K_RIGHTARROW)
1036                         pos_x += step;
1037
1038                 HUD_Panel_SetPos(pos);
1039         }
1040
1041         HUD_Panel_UpdatePosSizeForId(highlightedPanel);
1042
1043         if (prev_pos != panel_pos || prev_size != panel_size)
1044         {
1045                 // backup!
1046                 panel_pos_backup = prev_pos;
1047                 panel_size_backup = prev_size;
1048                 highlightedPanel_backup = highlightedPanel;
1049         }
1050 }
1051
1052 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
1053 {
1054         string s;
1055
1056         if(!autocvar__hud_configure)
1057                 return false;
1058
1059         // allow console bind to work
1060         string con_keys;
1061         float keys;
1062         con_keys = findkeysforcommand("toggleconsole");
1063         keys = tokenize(con_keys);
1064
1065         float hit_con_bind, i;
1066         for (i = 0; i < keys; ++i)
1067         {
1068                 if(nPrimary == stof(argv(i)))
1069                         hit_con_bind = 1;
1070         }
1071
1072         if(bInputType == 0) {
1073                 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
1074                 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
1075                 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
1076         }
1077         else if(bInputType == 1) {
1078                 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
1079                 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
1080                 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
1081         }
1082
1083         if(nPrimary == K_MOUSE1)
1084         {
1085                 if(bInputType == 0) { // key pressed
1086                         mouseClicked = 1;
1087                         return true;
1088                 }
1089                 else if(bInputType == 1) {// key released
1090                         mouseClicked = 0;
1091                         return true;
1092                 }
1093         }
1094         else if(nPrimary == K_ESCAPE)
1095         {
1096                 if (bInputType == 1)
1097                         return true;
1098                 menu_enabled = 1;
1099                 menu_enabled_time = time;
1100                 localcmd("menu_showhudexit\n");
1101         }
1102         else if(hudShiftState & S_CTRL)
1103         {
1104                 if (mouseClicked)
1105                         return true;
1106
1107                 if(nPrimary == K_SPACE) // enable/disable highlighted panel or dock
1108                 {
1109                         if (bInputType == 1)
1110                                 return true;
1111
1112                         if (highlightedPanel_prev != -1)
1113                                 cvar_set(strcat("hud_panel_", panel_name), ftos(!(panel_enabled)));
1114                         else
1115                                 cvar_set(strcat("hud_dock"), (autocvar_hud_dock == "") ? "dock" : "");
1116                 }
1117                 if(nPrimary == 'c') // copy highlighted panel size
1118                 {
1119                         if (bInputType == 1)
1120                                 return true;
1121
1122                         if (highlightedPanel_prev != -1)
1123                         {
1124                                 panel_size_copied = panel_size;
1125                                 highlightedPanel_copied = highlightedPanel_prev;
1126                         }
1127                 }
1128                 else if(nPrimary == 'v') // past copied size on the highlighted panel
1129                 {
1130                         if (bInputType == 1)
1131                                 return true;
1132
1133                         if (highlightedPanel_copied == -1 || highlightedPanel_prev == -1)
1134                                 return true;
1135
1136                         HUD_Panel_UpdatePosSizeForId(highlightedPanel_prev);
1137
1138                         // reduce size if it'd go beyond screen boundaries
1139                         vector tmp_size = panel_size_copied;
1140                         if (panel_pos_x + panel_size_copied_x > vid_conwidth)
1141                                 tmp_size_x = vid_conwidth - panel_pos_x;
1142                         if (panel_pos_y + panel_size_copied_y > vid_conheight)
1143                                 tmp_size_y = vid_conheight - panel_pos_y;
1144
1145                         if (panel_size == tmp_size)
1146                                 return true;
1147
1148                         // backup first!
1149                         panel_pos_backup = panel_pos;
1150                         panel_size_backup = panel_size;
1151                         highlightedPanel_backup = highlightedPanel_prev;
1152
1153                         s = strcat(ftos(tmp_size_x/vid_conwidth), " ", ftos(tmp_size_y/vid_conheight));
1154                         cvar_set(strcat("hud_panel_", panel_name, "_size"), s);
1155                 }
1156                 else if(nPrimary == 'z') // undo last action
1157                 {
1158                         if (bInputType == 1)
1159                                 return true;
1160                         //restore previous values
1161                         if (highlightedPanel_backup != -1)
1162                         {
1163                                 HUD_Panel_GetName(highlightedPanel_backup);
1164                                 s = strcat(ftos(panel_pos_backup_x/vid_conwidth), " ", ftos(panel_pos_backup_y/vid_conheight));
1165                                 cvar_set(strcat("hud_panel_", panel_name, "_pos"), s);
1166                                 s = strcat(ftos(panel_size_backup_x/vid_conwidth), " ", ftos(panel_size_backup_y/vid_conheight));
1167                                 cvar_set(strcat("hud_panel_", panel_name, "_size"), s);
1168                                 highlightedPanel_backup = -1;
1169                         }
1170                 }
1171         }
1172         else if(nPrimary == K_UPARROW || nPrimary == K_DOWNARROW || nPrimary == K_LEFTARROW || nPrimary == K_RIGHTARROW)
1173         {
1174                 if (bInputType == 1)
1175                 {
1176                         pressed_key_time = 0;
1177                         return true;
1178                 }
1179                 else if (pressed_key_time == 0)
1180                         pressed_key_time = time;
1181
1182                 HUD_Panel_Arrow_Action(nPrimary); //move or resize panel
1183         }
1184         else if(hit_con_bind)
1185                 return false;
1186
1187         return true; // Suppress ALL other input
1188 }
1189
1190 float HUD_Panel_HighlightCheck()
1191 {
1192         float i, j, border;
1193         vector panelPos;
1194         vector panelSize;
1195
1196         while(j <= HUD_PANEL_NUM)
1197         {
1198                 i = panel_order[j];
1199                 j += 1;
1200
1201                 HUD_Panel_UpdatePosSizeForId(i);
1202
1203                 panelPos = panel_pos;
1204                 panelSize = panel_size;
1205                 border = max(8, panel_bg_border); // FORCED border so a small border size doesn't mean you can't resize
1206
1207                 // move
1208                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1209                 {
1210                         return 1;
1211                 }
1212                 // resize from topleft border
1213                 else if(mousepos_x >= panelPos_x - border && mousepos_y >= panelPos_y - border && mousepos_x <= panelPos_x + 0.5 * panelSize_x && mousepos_y <= panelPos_y + 0.5 * panelSize_y)
1214                 {
1215                         return 2;
1216                 }
1217                 // resize from topright border
1218                 else if(mousepos_x >= panelPos_x + 0.5 * panelSize_x && mousepos_y >= panelPos_y - border && mousepos_x <= panelPos_x + panelSize_x + border && mousepos_y <= panelPos_y + 0.5 * panelSize_y)
1219                 {
1220                         return 3;
1221                 }
1222                 // resize from bottomleft border
1223                 else if(mousepos_x >= panelPos_x - border && mousepos_y >= panelPos_y + 0.5 * panelSize_y && mousepos_x <= panelPos_x + 0.5 * panelSize_x && mousepos_y <= panelPos_y + panelSize_y + border)
1224                 {
1225                         return 3;
1226                 }
1227                 // resize from bottomright border
1228                 else if(mousepos_x >= panelPos_x + 0.5 * panelSize_x && mousepos_y >= panelPos_y + 0.5 * panelSize_y && mousepos_x <= panelPos_x + panelSize_x + border && mousepos_y <= panelPos_y + panelSize_y + border)
1229                 {
1230                         return 2;
1231                 }
1232         }
1233         return 0;
1234 }
1235
1236 // move a panel to the beginning of the panel order array (which means it gets drawn last, on top of everything else)
1237 void HUD_Panel_FirstInDrawQ(float id)
1238 {
1239         float i;
1240         var float place = -1;
1241         // find out where in the array our current id is, save into place
1242         for(i = 0; i < HUD_PANEL_NUM; ++i)
1243         {
1244                 if(panel_order[i] == id)
1245                 {
1246                         place = i;
1247                         break;
1248                 }
1249         }
1250         // place last if we didn't find a place for it yet (probably new panel, or screwed up cvar)
1251         if(place == -1)
1252                 place = HUD_PANEL_NUM - 1;
1253
1254         // move all ids up by one step in the array until "place"
1255         for(i = place; i > 0; --i)
1256         {
1257                 panel_order[i] = panel_order[i-1];
1258         }
1259         // now save the new top id
1260         panel_order[0] = id;
1261         
1262         // let's save them into the cvar by some strcat trickery
1263         string s;
1264         for(i = 0; i < HUD_PANEL_NUM; ++i)
1265         {
1266                 s = strcat(s, ftos(panel_order[i]), " ");
1267         }
1268         cvar_set("_hud_panelorder", s);
1269         if(hud_panelorder_prev)
1270                 strunzone(hud_panelorder_prev);
1271         hud_panelorder_prev = strzone(autocvar__hud_panelorder); // prevent HUD_Main from doing useless update, we already updated here
1272 }
1273
1274 void HUD_Panel_Highlight()
1275 {
1276         float i, j, border;
1277         vector panelPos;
1278         vector panelSize;
1279
1280         while(j <= HUD_PANEL_NUM)
1281         {
1282                 i = panel_order[j];
1283                 j += 1;
1284
1285                 HUD_Panel_UpdatePosSizeForId(i);
1286
1287                 panelPos = panel_pos;
1288                 panelSize = panel_size;
1289                 border = max(8, panel_bg_border); // FORCED border so a small border size doesn't mean you can't resize
1290
1291                 // move
1292                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1293                 {
1294                         highlightedPanel = i;
1295                         HUD_Panel_FirstInDrawQ(i);
1296                         highlightedAction = 1;
1297                         panel_click_distance = mousepos - panelPos;
1298                         return;
1299                 }
1300                 // resize from topleft border
1301                 else if(mousepos_x >= panelPos_x - border && mousepos_y >= panelPos_y - border && mousepos_x <= panelPos_x + 0.5 * panelSize_x && mousepos_y <= panelPos_y + 0.5 * panelSize_y)
1302                 {
1303                         highlightedPanel = i;
1304                         HUD_Panel_FirstInDrawQ(i);
1305                         highlightedAction = 2;
1306                         resizeCorner = 1;
1307                         panel_click_distance = mousepos - panelPos;
1308                         panel_click_resizeorigin = panelPos + panelSize;
1309                         return;
1310                 }
1311                 // resize from topright border
1312                 else if(mousepos_x >= panelPos_x + 0.5 * panelSize_x && mousepos_y >= panelPos_y - border && mousepos_x <= panelPos_x + panelSize_x + border && mousepos_y <= panelPos_y + 0.5 * panelSize_y)
1313                 {
1314                         highlightedPanel = i;
1315                         HUD_Panel_FirstInDrawQ(i);
1316                         highlightedAction = 2;
1317                         resizeCorner = 2;
1318                         panel_click_distance_x = panelSize_x - mousepos_x + panelPos_x;
1319                         panel_click_distance_y = mousepos_y - panelPos_y;
1320                         panel_click_resizeorigin = panelPos + eY * panelSize_y;
1321                         return;
1322                 }
1323                 // resize from bottomleft border
1324                 else if(mousepos_x >= panelPos_x - border && mousepos_y >= panelPos_y + 0.5 * panelSize_y && mousepos_x <= panelPos_x + 0.5 * panelSize_x && mousepos_y <= panelPos_y + panelSize_y + border)
1325                 {
1326                         highlightedPanel = i;
1327                         HUD_Panel_FirstInDrawQ(i);
1328                         highlightedAction = 2;
1329                         resizeCorner = 3;
1330                         panel_click_distance_x = mousepos_x - panelPos_x;
1331                         panel_click_distance_y = panelSize_y - mousepos_y + panelPos_y;
1332                         panel_click_resizeorigin = panelPos + eX * panelSize_x;
1333                         return;
1334                 }
1335                 // resize from bottomright border
1336                 else if(mousepos_x >= panelPos_x + 0.5 * panelSize_x && mousepos_y >= panelPos_y + 0.5 * panelSize_y && mousepos_x <= panelPos_x + panelSize_x + border && mousepos_y <= panelPos_y + panelSize_y + border)
1337                 {
1338                         highlightedPanel = i;
1339                         HUD_Panel_FirstInDrawQ(i);
1340                         highlightedAction = 2;
1341                         resizeCorner = 4;
1342                         panel_click_distance = panelSize - mousepos + panelPos;
1343                         panel_click_resizeorigin = panelPos;
1344                         return;
1345                 }
1346                 else
1347                 {
1348                         highlightedPanel_prev = -1;
1349                 }
1350         }
1351 }
1352
1353 float highlightcheck;
1354 vector prev_pos, prev_size;
1355 void HUD_Panel_Mouse()
1356 {
1357         // TODO: needs better check... is there any float that contains the current state of the menu? _menu_alpha isn't apparently updated the frame the menu gets enabled
1358         if (autocvar__menu_alpha == 0 && time - menu_enabled_time > 0.5)
1359                 menu_enabled = 0;
1360
1361         /*
1362         print("menu_enabled: ", ftos(menu_enabled), "\n");
1363         print("Highlighted: ", ftos(highlightedPanel), "\n");
1364         print("Menu alpha: ", ftos(autocvar__menu_alpha), "\n");
1365         */
1366
1367         // instantly hide the editor cursor if we open the HUDExit dialog
1368         // as hud_fade_alpha doesn't decrease to 0 in this case
1369         // TODO: find a way to fade the cursor out even in this case
1370         if(menu_enabled == 1 || (menu_enabled == 2 && !hud_fade_alpha))
1371                 return;
1372
1373         if(mouseClicked == 0 && menu_enabled != 2 && highlightedPanel >= 0) { // don't reset these variables in menu_enabled mode 2!
1374                 highlightedPanel = -1;
1375                 highlightedAction = 0;
1376         }
1377         if(highlightedPanel != -1)
1378                 highlightedPanel_prev = highlightedPanel;
1379
1380         mousepos = mousepos + getmousepos() * autocvar_menu_mouse_speed;
1381
1382         mousepos_x = bound(0, mousepos_x, vid_conwidth);
1383         mousepos_y = bound(0, mousepos_y, vid_conheight);
1384
1385         if(mouseClicked)
1386         {
1387                 if(prevMouseClicked == 0)
1388                 {
1389                         HUD_Panel_Highlight(); // sets highlightedPanel, highlightedAction, panel_click_distance, panel_click_resizeorigin
1390                                                                         // and calls HUD_Panel_UpdatePosSizeForId() for the highlighted panel
1391                         prev_pos = panel_pos;
1392                         prev_size = panel_size;
1393                 }
1394                 else
1395                         HUD_Panel_UpdatePosSizeForId(highlightedPanel);
1396
1397                 if (prev_pos != panel_pos || prev_size != panel_size)
1398                 {
1399                         hud_configure_checkcollisions = (!(hudShiftState & S_CTRL) && autocvar_hud_configure_checkcollisions);
1400                         // backup!
1401                         panel_pos_backup = prev_pos;
1402                         panel_size_backup = prev_size;
1403                         highlightedPanel_backup = highlightedPanel;
1404                 }
1405                 else
1406                         // in case the clicked panel is inside another panel and we aren't
1407                         // moving it, avoid the immediate "fix" of its position/size
1408                         // (often unwanted and hateful) by disabling collisions check
1409                         hud_configure_checkcollisions = false;
1410
1411                 if(highlightedAction == 1)
1412                         HUD_Panel_SetPos(mousepos - panel_click_distance);
1413                 else if(highlightedAction == 2)
1414                 {
1415                         vector mySize;
1416                         if(resizeCorner == 1) {
1417                                 mySize_x = panel_click_resizeorigin_x - (mousepos_x - panel_click_distance_x);
1418                                 mySize_y = panel_click_resizeorigin_y - (mousepos_y - panel_click_distance_y);
1419                         } else if(resizeCorner == 2) {
1420                                 mySize_x = mousepos_x + panel_click_distance_x - panel_click_resizeorigin_x;
1421                                 mySize_y = panel_click_distance_y + panel_click_resizeorigin_y - mousepos_y;
1422                         } else if(resizeCorner == 3) {
1423                                 mySize_x = panel_click_resizeorigin_x + panel_click_distance_x - mousepos_x;
1424                                 mySize_y = mousepos_y + panel_click_distance_y - panel_click_resizeorigin_y;
1425                         } else { // resizeCorner == 4
1426                                 mySize_x = mousepos_x - (panel_click_resizeorigin_x - panel_click_distance_x);
1427                                 mySize_y = mousepos_y - (panel_click_resizeorigin_y - panel_click_distance_y);
1428                         }
1429                         HUD_Panel_SetPosSize(mySize);
1430                 }
1431
1432                 // doubleclick check
1433                 if(time - prevMouseClickedTime < 0.4 && prevMouseClicked == 0 && prevMouseClickedPos == mousepos && highlightedPanel >= 0)
1434                 {
1435                         mouseClicked = 0; // to prevent spam, I guess.
1436                         menu_enabled = 2;
1437                         menu_enabled_time = time;
1438                         HUD_Panel_GetName(highlightedPanel);
1439                         localcmd("menu_showhudoptions ", panel_name, "\n");
1440                         return;
1441                 }
1442                 if(prevMouseClicked == 0)
1443                 {
1444                         prevMouseClickedTime = time;
1445                         prevMouseClickedPos = mousepos;
1446                 }
1447         }
1448         else
1449         {
1450                 highlightcheck = HUD_Panel_HighlightCheck();
1451         }
1452         // draw cursor after performing move/resize to have the panel pos/size updated before highlightcheck
1453         vector cursorsize;
1454         cursorsize = '32 32 0';
1455
1456         if(highlightcheck == 0)
1457                 drawpic(mousepos, strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"), '32 32 0', '1 1 1', hud_fade_alpha, DRAWFLAG_NORMAL);
1458         else if(highlightcheck == 1)
1459                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", autocvar_menu_skin, "/cursor_move.tga"), '32 32 0', '1 1 1', hud_fade_alpha, DRAWFLAG_NORMAL);
1460         else if(highlightcheck == 2)
1461                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", autocvar_menu_skin, "/cursor_resize.tga"), '32 32 0', '1 1 1', hud_fade_alpha, DRAWFLAG_NORMAL);
1462         else
1463                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", autocvar_menu_skin, "/cursor_resize2.tga"), '32 32 0', '1 1 1', hud_fade_alpha, DRAWFLAG_NORMAL);
1464
1465         prevMouseClicked = mouseClicked;
1466 }
1467
1468 // Weapon icons (#0)
1469 //
1470 float weaponspace[10];
1471 #define HUD_Weapons_Clear()\
1472         float idx;\
1473         for(idx = 0; idx < 10; ++idx)\
1474                 weaponspace[idx] = 0
1475
1476 entity weaponorder[WEP_MAXCOUNT];
1477 void weaponorder_swap(float i, float j, entity pass)
1478 {
1479         entity h;
1480         h = weaponorder[i];
1481         weaponorder[i] = weaponorder[j];
1482         weaponorder[j] = h;
1483 }
1484
1485 string weaponorder_cmp_str;
1486 float weaponorder_cmp(float i, float j, entity pass)
1487 {
1488         float ai, aj;
1489         ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
1490         aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
1491         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)
1492 }
1493
1494 float GetAmmoStat(float i)
1495 {
1496         switch(i)
1497         {
1498                 case 0: return STAT_SHELLS;
1499                 case 1: return STAT_NAILS;
1500                 case 2: return STAT_ROCKETS;
1501                 case 3: return STAT_CELLS;
1502                 case 4: return STAT_FUEL;
1503                 default: return -1;
1504         }
1505 }
1506
1507 float GetAmmoTypeForWep(float i)
1508 {
1509         switch(i)
1510         {
1511                 case WEP_SHOTGUN: return 0;
1512                 case WEP_UZI: return 1;
1513                 case WEP_GRENADE_LAUNCHER: return 2;
1514                 case WEP_MINE_LAYER: return 2;
1515                 case WEP_ELECTRO: return 3;
1516                 case WEP_CRYLINK: return 3;
1517                 case WEP_HLAC: return 3;
1518                 case WEP_MINSTANEX: return 3;
1519                 case WEP_NEX: return 3;
1520                 case WEP_SNIPERRIFLE: return 1;
1521                 case WEP_HAGAR: return 2;
1522                 case WEP_ROCKET_LAUNCHER: return 2;
1523                 case WEP_SEEKER: return 2;
1524                 case WEP_FIREBALL: return 4;
1525                 case WEP_HOOK: return 3;
1526                 default: return -1;
1527         }
1528 }
1529
1530 void HUD_Weapons(void)
1531 {
1532         float f, screen_ar;
1533         float center_x, center_y;
1534
1535         if(!autocvar_hud_panel_weapons && !autocvar__hud_configure)
1536                 return;
1537
1538         float timeout = autocvar_hud_panel_weapons_timeout;
1539         float timeout_effect_length, timein_effect_length;
1540         if (autocvar_hud_panel_weapons_timeout_effect == 0)
1541         {
1542                 timeout_effect_length = 0;
1543                 timein_effect_length = 0;
1544         }
1545         else
1546         {
1547                 timeout_effect_length = 0.75;
1548                 timein_effect_length = 0.375;
1549         }
1550
1551         if (timeout && time >= weapontime + timeout + timeout_effect_length && !autocvar__hud_configure)
1552         {
1553                 weaponprevtime = time;
1554                 return;
1555         }
1556
1557         active_panel = HUD_PANEL_WEAPONS;
1558         HUD_Panel_UpdateCvars(weapons);
1559
1560         if (timeout && time >= weapontime + timeout && !autocvar__hud_configure)
1561         {
1562                 f = (time - (weapontime + timeout)) / timeout_effect_length;
1563                 if (autocvar_hud_panel_weapons_timeout_effect)
1564                 {
1565                         panel_bg_alpha *= (1 - f);
1566                         panel_fg_alpha *= (1 - f);
1567                 }
1568                 if (autocvar_hud_panel_weapons_timeout_effect == 1)
1569                 {
1570                         f *= f; // for a cooler movement
1571                         center_x = panel_pos_x + panel_size_x/2;
1572                         center_y = panel_pos_y + panel_size_y/2;
1573                         screen_ar = vid_conwidth/vid_conheight;
1574                         if (center_x/center_y < screen_ar) //bottom left
1575                         {
1576                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //bottom
1577                                         panel_pos_y += f * (vid_conheight - panel_pos_y);
1578                                 else //left
1579                                         panel_pos_x -= f * (panel_pos_x + panel_size_x);
1580                         }
1581                         else //top right
1582                         {
1583                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //right
1584                                         panel_pos_x += f * (vid_conwidth - panel_pos_x);
1585                                 else //top
1586                                         panel_pos_y -= f * (panel_pos_y + panel_size_y);
1587                         }
1588                 }
1589                 weaponprevtime = time - (1 - f) * timein_effect_length;
1590         }
1591         else if (timeout && time < weaponprevtime + timein_effect_length && !autocvar__hud_configure)
1592         {
1593                 f = (time - weaponprevtime) / timein_effect_length;
1594                 if (autocvar_hud_panel_weapons_timeout_effect)
1595                 {
1596                         panel_bg_alpha *= (f);
1597                         panel_fg_alpha *= (f);
1598                 }
1599                 if (autocvar_hud_panel_weapons_timeout_effect == 1)
1600                 {
1601                         f *= f; // for a cooler movement
1602                         f = 1 - f;
1603                         center_x = panel_pos_x + panel_size_x/2;
1604                         center_y = panel_pos_y + panel_size_y/2;
1605                         screen_ar = vid_conwidth/vid_conheight;
1606                         if (center_x/center_y < screen_ar) //bottom left
1607                         {
1608                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //bottom
1609                                         panel_pos_y += f * (vid_conheight - panel_pos_y);
1610                                 else //left
1611                                         panel_pos_x -= f * (panel_pos_x + panel_size_x);
1612                         }
1613                         else //top right
1614                         {
1615                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //right
1616                                         panel_pos_x += f * (vid_conwidth - panel_pos_x);
1617                                 else //top
1618                                         panel_pos_y -= f * (panel_pos_y + panel_size_y);
1619                         }
1620                 }
1621         }
1622
1623         float i, weapid, wpnalpha, weapon_cnt;
1624         weapon_cnt = 0;
1625         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1626         {
1627                 self = get_weaponinfo(i);
1628                 if(self.impulse >= 0)
1629                         ++weapon_cnt;
1630         }
1631
1632         // TODO make this configurable
1633         if(weaponorder_bypriority != autocvar_cl_weaponpriority || !weaponorder[0])
1634         {
1635                 if(weaponorder_bypriority)
1636                         strunzone(weaponorder_bypriority);
1637                 if(weaponorder_byimpulse)
1638                         strunzone(weaponorder_byimpulse);
1639
1640                 weaponorder_bypriority = strzone(autocvar_cl_weaponpriority);
1641                 weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(W_FixWeaponOrder_ForceComplete(W_NumberWeaponOrder(weaponorder_bypriority))));
1642                 weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
1643
1644                 weapon_cnt = 0;
1645                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1646                 {
1647                         self = get_weaponinfo(i);
1648                         if(self.impulse >= 0)
1649                         {
1650                                 weaponorder[weapon_cnt] = self;
1651                                 ++weapon_cnt;
1652                         }
1653                 }
1654                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
1655
1656                 weaponorder_cmp_str = string_null;
1657         }
1658
1659         HUD_Panel_DrawBg(1);
1660         if(panel_bg_padding)
1661         {
1662                 panel_pos += '1 1 0' * panel_bg_padding;
1663                 panel_size -= '2 2 0' * panel_bg_padding;
1664         }
1665
1666         if(autocvar_hud_panel_weapons_fade)
1667         {
1668                 wpnalpha = 3.2 - 2 * (time - weapontime);
1669                 wpnalpha = bound(0.7, wpnalpha, 1) * panel_fg_alpha;
1670         }
1671         else
1672                 wpnalpha = panel_fg_alpha;
1673
1674         HUD_Weapons_Clear();
1675
1676         float rows, columns;
1677         float aspect = autocvar_hud_panel_weapons_aspect;
1678         rows = panel_size_y/panel_size_x;
1679         rows = bound(1, floor((sqrt(4 * aspect * rows * WEP_COUNT + rows * rows) + rows + 0.5) / 2), WEP_COUNT);
1680
1681         columns = ceil(WEP_COUNT/rows);
1682         float row, column;
1683
1684         float a, type, fullammo;
1685         float when;
1686         when = autocvar_hud_panel_weapons_complainbubble_time;
1687         float fadetime;
1688         fadetime = autocvar_hud_panel_weapons_complainbubble_fadetime;
1689
1690         vector color;
1691         vector wpnpos;
1692         vector wpnsize;
1693         
1694         float fullammo_shells, fullammo_nails, fullammo_rockets, fullammo_cells, fullammo_fuel;
1695         vector ammo_color;
1696         float ammo_alpha;
1697         wpnsize = eX * panel_size_x*(1/columns) + eY * panel_size_y*(1/rows);
1698         float barsize_x, barsize_y, baroffset_x, baroffset_y;
1699         float show_ammo = autocvar_hud_panel_weapons_ammo;
1700         if (show_ammo)
1701         {
1702                 fullammo_shells = autocvar_hud_panel_weapons_ammo_full_shells;
1703                 fullammo_nails = autocvar_hud_panel_weapons_ammo_full_nails;
1704                 fullammo_rockets = autocvar_hud_panel_weapons_ammo_full_rockets;
1705                 fullammo_cells = autocvar_hud_panel_weapons_ammo_full_cells;
1706                 fullammo_fuel = autocvar_hud_panel_weapons_ammo_full_fuel;
1707                 ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
1708                 ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
1709
1710
1711                 if(wpnsize_x/wpnsize_y > aspect)
1712                 {
1713                         barsize_x = aspect * wpnsize_y;
1714                         barsize_y = wpnsize_y;
1715                         baroffset_x = (wpnsize_x - barsize_x) / 2;
1716                 }
1717                 else
1718                 {
1719                         barsize_y = 1/aspect * wpnsize_x;
1720                         barsize_x = wpnsize_x;
1721                         baroffset_y = (wpnsize_y - barsize_y) / 2;
1722                 }
1723         }
1724
1725         float show_accuracy;
1726         float weapon_stats;
1727         if(autocvar_hud_panel_weapons_accuracy && acc_levels)
1728         {
1729                 show_accuracy = true;
1730                 if (acc_col_x[0] == -1)
1731                         for (i = 0; i < acc_levels; ++i)
1732                                 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
1733         }
1734
1735         float weapons_st = getstati(STAT_WEAPONS);
1736         float label = autocvar_hud_panel_weapons_label;
1737
1738         for(i = 0; i < weapon_cnt; ++i)
1739         {
1740                 wpnpos = panel_pos + eX * column * wpnsize_x + eY * row * wpnsize_y;
1741
1742                 self = weaponorder[i];
1743                 weapid = self.impulse;
1744
1745                 // draw background behind currently selected weapon
1746                 if(self.weapon == activeweapon)
1747                         drawpic_aspect_skin(wpnpos, "weapon_current_bg", wpnsize, '1 1 1', wpnalpha, DRAWFLAG_NORMAL);
1748
1749                 // draw the weapon accuracy
1750                 if(show_accuracy)
1751                 {
1752                         weapon_stats = weapon_accuracy[self.weapon-WEP_FIRST];
1753                         if(weapon_stats >= 0)
1754                         {
1755                                 // find the max level lower than weapon_stats
1756                                 float j;
1757                                 j = acc_levels-1;
1758                                 while ( j && weapon_stats < acc_lev[j] )
1759                                         --j;
1760
1761                                 // inject color j+1 in color j, how much depending on how much weapon_stats is higher than level j
1762                                 float factor;
1763                                 factor = (weapon_stats - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
1764                                 color = acc_col[j];
1765                                 color = color + factor * (acc_col[j+1] - color);
1766
1767                                 drawpic_aspect_skin(wpnpos, "weapon_accuracy", wpnsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
1768                         }
1769                 }
1770
1771                 // draw the weapon icon
1772                 if((weapid >= 0) && (weapons_st & self.weapons))
1773                 {
1774                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '1 1 1', wpnalpha, DRAWFLAG_NORMAL);
1775
1776                         if(label == 1) // weapon number
1777                                 drawstring(wpnpos, ftos(weapid), '1 1 0' * 0.5 * wpnsize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1778                         else if(label == 2) // bind
1779                                 drawstring(wpnpos, getcommandkey(ftos(weapid), strcat("impulse ", ftos(weapid))), '1 1 0' * 0.5 * wpnsize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1780
1781                         // draw ammo status bar
1782                         if(show_ammo && self.weapon != WEP_TUBA && self.weapon != WEP_LASER && self.weapon != WEP_PORTO)
1783                         {
1784                                 a = 0;
1785                                 type = GetAmmoTypeForWep(self.weapon);
1786                                 if(type != -1)
1787                                         a = getstati(GetAmmoStat(type)); // how much ammo do we have?
1788
1789                                 if(a > 0)
1790                                 {
1791                                         switch(type) {
1792                                                 case 0: fullammo = fullammo_shells; break;
1793                                                 case 1: fullammo = fullammo_nails; break;
1794                                                 case 2: fullammo = fullammo_rockets; break;
1795                                                 case 3: fullammo = fullammo_cells; break;
1796                                                 case 4: fullammo = fullammo_fuel; break;
1797                                                 default: fullammo = 60;
1798                                         }
1799
1800                                         drawsetcliparea(
1801                                                 wpnpos_x + baroffset_x,
1802                                                 wpnpos_y + baroffset_y,
1803                                                 barsize_x * bound(0, a/fullammo, 1),
1804                                                 barsize_y);
1805                                         drawpic_aspect_skin(wpnpos, "weapon_ammo", wpnsize, ammo_color, ammo_alpha, DRAWFLAG_NORMAL);
1806                                         drawresetcliparea();
1807                                 }
1808                         }
1809                 }
1810
1811                 // draw a "ghost weapon icon" if you don't have the weapon
1812                 else
1813                 {
1814                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '0 0 0', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
1815                 }
1816
1817                 // draw the complain message
1818                 if(time - complain_weapon_time < when + fadetime && self.weapon == complain_weapon && autocvar_hud_panel_weapons_complainbubble)
1819                 {
1820                         if(fadetime)
1821                         {
1822                                 if(complain_weapon_time + when > time)
1823                                         a = 1;
1824                                 else
1825                                         a = bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1);
1826                         }
1827                         else
1828                         {
1829                                 if(complain_weapon_time + when > time)
1830                                         a = 1;
1831                                 else
1832                                         a = 0;
1833                         }
1834
1835                         string s;
1836                         if(complain_weapon_type == 0) {
1837                                 s = _("Out of ammo");
1838                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
1839                         }
1840                         else if(complain_weapon_type == 1) {
1841                                 s = _("Don't have");
1842                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
1843                         }
1844                         else {
1845                                 s = _("Unavailable");
1846                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
1847                         }
1848                         float padding = autocvar_hud_panel_weapons_complainbubble_padding;
1849                         drawpic_aspect_skin(wpnpos + '1 1 0' * padding, "weapon_complainbubble", wpnsize - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
1850                         drawstring_aspect(wpnpos + '1 1 0' * padding, s, wpnsize - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
1851                 }
1852
1853                 ++row;
1854                 if(row >= rows)
1855                 {
1856                         row = 0;
1857                         ++column;
1858                 }
1859         }
1860
1861 }
1862
1863 // Ammo (#1)
1864 //
1865 // TODO: macro
1866 float GetAmmoItemCode(float i)
1867 {
1868         switch(i)
1869         {
1870                 case 0: return IT_SHELLS;
1871                 case 1: return IT_NAILS;
1872                 case 2: return IT_ROCKETS;
1873                 case 3: return IT_CELLS;
1874                 case 4: return IT_FUEL;
1875                 default: return -1;
1876         }
1877 }
1878
1879 string GetAmmoPicture(float i)
1880 {
1881         switch(i)
1882         {
1883                 case 0: return "ammo_shells";
1884                 case 1: return "ammo_bullets";
1885                 case 2: return "ammo_rockets";
1886                 case 3: return "ammo_cells";
1887                 case 4: return "ammo_fuel";
1888                 default: return "";
1889         }
1890 }
1891
1892 void DrawAmmoItem(vector myPos, vector mySize, float itemcode, float currently_selected)
1893 {
1894         float a;
1895         if(autocvar__hud_configure)
1896         {
1897                 currently_selected = (itemcode == 2); //rockets always selected
1898                 a = 100;
1899         }
1900         else
1901                 a = getstati(GetAmmoStat(itemcode)); // how much ammo do we have of type itemcode?
1902
1903         vector color;
1904         if(a < 10)
1905                 color = '0.7 0 0';
1906         else
1907                 color = '1 1 1';
1908
1909         float alpha;
1910         if(currently_selected)
1911                 alpha = 1;
1912         else
1913                 alpha = 0.7;
1914
1915         vector picpos, numpos;
1916         if(autocvar_hud_panel_ammo_iconalign)
1917         {
1918                 numpos = myPos;
1919                 picpos = myPos + eX * 2 * mySize_y;
1920         }
1921         else
1922         {
1923                 numpos = myPos + eX * mySize_y;
1924                 picpos = myPos;
1925         }
1926
1927         if (currently_selected)
1928                 drawpic_aspect_skin(myPos, "ammo_current_bg", mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1929
1930     if(a > 0 && autocvar_hud_panel_ammo_progressbar)
1931         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, 0, 0, min(1, a/autocvar_hud_panel_ammo_maxammo), color, autocvar_hud_progressbar_alpha * panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1932
1933     if(autocvar_hud_panel_ammo_text)
1934     {
1935         if(a > 0)
1936             drawstring_aspect(numpos, ftos(a), eX * (2/3) * mySize_x + eY * mySize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1937         else // "ghost" ammo count
1938             drawstring_aspect(numpos, ftos(a), eX * (2/3) * mySize_x + eY * mySize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1939     }
1940         if(a > 0)
1941                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1942         else // "ghost" ammo icon
1943                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * mySize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1944 }
1945
1946 void HUD_Ammo(void)
1947 {
1948         if(!autocvar_hud_panel_ammo && !autocvar__hud_configure)
1949                 return;
1950
1951         active_panel = HUD_PANEL_AMMO;
1952         HUD_Panel_UpdateCvars(ammo);
1953         vector pos, mySize;
1954         pos = panel_pos;
1955         mySize = panel_size;
1956
1957         HUD_Panel_DrawBg(1);
1958         if(panel_bg_padding)
1959         {
1960                 pos += '1 1 0' * panel_bg_padding;
1961                 mySize -= '2 2 0' * panel_bg_padding;
1962         }
1963
1964         float rows, columns, row, column;
1965         vector ammo_size;
1966         if (autocvar_hud_panel_ammo_onlycurrent)
1967                 ammo_size = mySize;
1968         else
1969         {
1970                 rows = mySize_y/mySize_x;
1971                 rows = bound(1, floor((sqrt(4 * (3/1) * rows * AMMO_COUNT + rows * rows) + rows + 0.5) / 2), AMMO_COUNT);
1972                 //                               ^^^ ammo item aspect goes here
1973
1974                 columns = ceil(AMMO_COUNT/rows);
1975
1976                 ammo_size = eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows);
1977         }
1978
1979         vector offset;
1980         float newSize;
1981         if(ammo_size_x/ammo_size_y > 3)
1982         {
1983                 newSize = 3 * ammo_size_y;
1984                 offset_x = ammo_size_x - newSize;
1985                 pos_x += offset_x/2;
1986                 ammo_size_x = newSize;
1987         }
1988         else
1989         {
1990                 newSize = 1/3 * ammo_size_x;
1991                 offset_y = ammo_size_y - newSize;
1992                 pos_y += offset_y/2;
1993                 ammo_size_y = newSize;
1994         }
1995
1996         float i, stat_items, currently_selected;
1997         if (autocvar_hud_panel_ammo_onlycurrent)
1998         {
1999                 if(autocvar__hud_configure)
2000                 {
2001                         DrawAmmoItem(pos, ammo_size, 2, true); //show rockets
2002                         return;
2003                 }
2004                 stat_items = getstati(STAT_ITEMS);
2005                 for (i = 0; i < AMMO_COUNT; ++i) {
2006                         currently_selected = stat_items & GetAmmoItemCode(i);
2007                         if (currently_selected)
2008                         {
2009                                 DrawAmmoItem(pos, ammo_size, i, true);
2010                                 return;
2011                         }
2012                 }
2013                 return; // nothing to display
2014         }
2015
2016         stat_items = getstati(STAT_ITEMS);
2017         for (i = 0; i < AMMO_COUNT; ++i) {
2018                 currently_selected = stat_items & GetAmmoItemCode(i);
2019                 DrawAmmoItem(pos + eX * column * (ammo_size_x + offset_x) + eY * row * (ammo_size_y + offset_y), ammo_size, i, currently_selected);
2020                 ++row;
2021                 if(row >= rows)
2022                 {
2023                         row = 0;
2024                         column = column + 1;
2025                 }
2026         }
2027 }
2028
2029 void DrawNumIcon(float iconalign, vector myPos, vector mySize, float x, string icon, float left, vector color, float alpha)
2030 {
2031         vector newPos;
2032         float newSize_x, newSize_y;
2033         if(mySize_x/mySize_y > 3)
2034         {
2035                 newSize_x = 3 * mySize_y;
2036                 newSize_y = mySize_y;
2037
2038                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
2039                 newPos_y = myPos_y;
2040         }
2041         else
2042         {
2043                 newSize_y = 1/3 * mySize_x;
2044                 newSize_x = mySize_x;
2045
2046                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
2047                 newPos_x = myPos_x;
2048         }
2049
2050         vector picpos, numpos;
2051         if(left)
2052         {
2053                 if(iconalign == 1 || iconalign == 3) // right align
2054                 {
2055                         numpos = newPos;
2056                         picpos = newPos + eX * 2 * newSize_y;
2057                 }
2058                 else // left align
2059                 {
2060                         numpos = newPos + eX * newSize_y;
2061                         picpos = newPos;
2062                 }
2063         }
2064         else
2065         {
2066                 if(iconalign == 0 || iconalign == 3) // left align
2067                 {
2068                         numpos = newPos + eX * newSize_y;
2069                         picpos = newPos;
2070                 } 
2071                 else // right align
2072                 {
2073                         numpos = newPos;
2074                         picpos = newPos + eX * 2 * newSize_y;
2075                 }
2076         }
2077
2078         drawstring_aspect(numpos, ftos(x), eX * (2/3) * newSize_x + eY * newSize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2079         drawpic_aspect_skin(picpos, icon, '1 1 0' * newSize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2080 }
2081
2082 void DrawNumIcon_expanding(float iconalign, vector myPos, vector mySize, float x, string icon, float left, vector color, float fadelerp)
2083 {
2084         float sz;
2085         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
2086
2087         DrawNumIcon(iconalign, myPos + expandingbox_resize_centered_box_offset(sz, mySize, 1), mySize * sz, x, icon, left, color, (1 - fadelerp));
2088 }
2089
2090 // Powerups (#2)
2091 //
2092 void HUD_Powerups(void) {
2093         if(!autocvar_hud_panel_powerups && !autocvar__hud_configure)
2094                 return;
2095
2096         if(!autocvar__hud_configure)
2097         {
2098                 if not(getstati(STAT_ITEMS) & (IT_STRENGTH | IT_INVINCIBLE))
2099                         return;
2100
2101                 if (getstati(STAT_HEALTH) <= 0)
2102                         return;
2103         }
2104
2105         active_panel = HUD_PANEL_POWERUPS;
2106         HUD_Panel_UpdateCvars(powerups);
2107         vector pos, mySize;
2108         pos = panel_pos;
2109         mySize = panel_size;
2110
2111         float strength_time, shield_time;
2112         if(autocvar__hud_configure)
2113         {
2114                 strength_time = 15;
2115                 shield_time = 27;
2116         }
2117         else
2118         {
2119                 strength_time = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
2120                 shield_time = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
2121         }
2122
2123         HUD_Panel_DrawBg(bound(0, max(strength_time, shield_time), 1));
2124         if(panel_bg_padding)
2125         {
2126                 pos += '1 1 0' * panel_bg_padding;
2127                 mySize -= '2 2 0' * panel_bg_padding;
2128         }
2129
2130         vector barpos, barsize;
2131         vector picpos;
2132         vector numpos;
2133
2134         string leftname, rightname;
2135     string leftprogressname, rightprogressname;
2136         float leftcnt, rightcnt;
2137         float leftexact, rightexact;
2138         float flip = autocvar_hud_panel_powerups_flip;
2139         if (flip) {
2140                 leftname = "strength";
2141         leftprogressname = autocvar_hud_panel_powerups_progressbar_strength;
2142                 leftcnt = ceil(strength_time);
2143                 leftexact = strength_time;
2144
2145                 rightname = "shield";
2146         rightprogressname = autocvar_hud_panel_powerups_progressbar_shield;
2147                 rightcnt = ceil(shield_time);
2148                 rightexact = shield_time;
2149         } else {
2150                 leftname = "shield";
2151         leftprogressname = autocvar_hud_panel_powerups_progressbar_shield;
2152                 leftcnt = ceil(shield_time);
2153                 leftexact = shield_time;
2154
2155                 rightname = "strength";
2156         rightprogressname = autocvar_hud_panel_powerups_progressbar_strength;
2157                 rightcnt = ceil(strength_time);
2158                 rightexact = strength_time;
2159         }
2160
2161         float baralign = autocvar_hud_panel_powerups_baralign;
2162     float barflip;
2163         float iconalign = autocvar_hud_panel_powerups_iconalign;
2164         float progressbar = autocvar_hud_panel_powerups_progressbar;
2165         if (mySize_x/mySize_y > 4)
2166         {
2167         barsize = eX * 0.5 * mySize_x + eY * mySize_y;
2168                 if(leftcnt)
2169                 {
2170                         if(baralign == 1 || baralign == 3) { // right align
2171                 barpos = pos + eX * 0.5 * mySize_x;
2172                 barflip = 1;
2173                         } else { // left align
2174                 barpos = pos;
2175                 barflip = 0;
2176                         }
2177
2178                         if(progressbar)
2179                         {
2180                                 HUD_Panel_GetProgressBarColorForString(leftname);
2181                                 HUD_Panel_DrawProgressBar(barpos, barsize, leftprogressname, 0, barflip, min(1, leftcnt/30), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1), DRAWFLAG_NORMAL);
2182                         }
2183             if(autocvar_hud_panel_powerups_text)
2184             {
2185                 if(leftcnt > 1)
2186                     DrawNumIcon(iconalign, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, '1 1 1', 1);
2187                 if(leftcnt <= 5)
2188                     DrawNumIcon_expanding(iconalign, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, '1 1 1', bound(0, (leftcnt - leftexact) / 0.5, 1));
2189             }
2190                 }
2191
2192                 if(rightcnt)
2193                 {
2194                         if(baralign == 0 || baralign == 3) { // left align
2195                 barpos = pos;
2196                 barflip = 0;
2197                         } else { // right align
2198                 barpos = pos + eX * 0.5 * mySize_x;
2199                 barflip = 1;
2200                         }
2201
2202                         if(progressbar)
2203                         {
2204                                 HUD_Panel_GetProgressBarColorForString(rightname);
2205                                 HUD_Panel_DrawProgressBar(barpos, barsize, rightprogressname, 0, barflip, min(1, rightcnt/30), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1), DRAWFLAG_NORMAL);
2206                         }
2207             if(autocvar_hud_panel_powerups_text)
2208             {
2209                 if(rightcnt > 1)
2210                     DrawNumIcon(iconalign, pos + eX * 0.5 * mySize_x, eX * 0.5 * mySize_x + eY * mySize_y, rightcnt, rightname, 0, '1 1 1', 1);
2211                 if(rightcnt <= 5)
2212                     DrawNumIcon_expanding(iconalign, pos + eX * 0.5 * mySize_x, eX * 0.5 * mySize_x + eY * mySize_y, rightcnt, rightname, 0, '1 1 1', bound(0, (rightcnt - rightexact) / 0.5, 1));
2213             }
2214                 }
2215         }
2216         else if (mySize_x/mySize_y > 1.5)
2217         {
2218         barsize = eX * mySize_x + eY * 0.5 * mySize_y;
2219                 if(leftcnt)
2220                 {
2221             barpos = pos;
2222                         if(baralign == 1 || baralign == 3) { // right/down align
2223                 barflip = 1;
2224                         } else { // left/up align
2225                 barflip = 0;
2226                         }
2227
2228                         if(progressbar)
2229                         {
2230                                 HUD_Panel_GetProgressBarColorForString(leftname);
2231                                 HUD_Panel_DrawProgressBar(barpos, barsize, leftprogressname, 0, barflip, min(1, leftcnt/30), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1), DRAWFLAG_NORMAL);
2232                         }
2233             if(autocvar_hud_panel_powerups_text)
2234             {
2235                 if(leftcnt > 1)
2236                     DrawNumIcon(iconalign, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, '1 1 1', 1);
2237                 if(leftcnt <= 5)
2238                     DrawNumIcon_expanding(iconalign, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, '1 1 1', bound(0, (leftcnt - leftexact) / 0.5, 1));
2239             }
2240                 }
2241
2242                 if(rightcnt)
2243                 {
2244             barpos = pos + eY * 0.5 * mySize_y;
2245                         if(baralign == 0 || baralign == 3) { // left align
2246                 barflip = 0;
2247                         } else { // right align
2248                 barflip = 1;
2249                         }
2250
2251                         if(progressbar)
2252                         {
2253                                 HUD_Panel_GetProgressBarColorForString(rightname);
2254                                 HUD_Panel_DrawProgressBar(barpos, barsize, rightprogressname, 0, barflip, min(1, rightcnt/30), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1), DRAWFLAG_NORMAL);
2255                         }
2256             if(autocvar_hud_panel_powerups_text)
2257             {
2258                 if(rightcnt > 1)
2259                     DrawNumIcon(iconalign, pos + eY * 0.5 * mySize_y, eX * mySize_x + eY * 0.5 * mySize_y, rightcnt, rightname, 0, '1 1 1', 1);
2260                 if(rightcnt <= 5)
2261                     DrawNumIcon_expanding(iconalign, pos + eY * 0.5 * mySize_y, eX * mySize_x + eY * 0.5 * mySize_y, rightcnt, rightname, 0, '1 1 1', bound(0, (rightcnt - rightexact) / 0.5, 1));
2262             }
2263                 }
2264         }
2265         else
2266         {
2267         barsize = eX * 0.5 * mySize_x + eY * mySize_y;
2268                 if(leftcnt)
2269                 {
2270             barpos = pos;
2271                         if(baralign == 1 || baralign == 3) { // down align
2272                 barflip = 1;
2273                         } else { // up align
2274                 barflip = 0;
2275                         }
2276
2277                         if(iconalign == 1 || iconalign == 3) { // down align
2278                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
2279                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
2280                         } else { // up align
2281                                 picpos = pos + eX * 0.05 * mySize_x;
2282                                 numpos = pos + eY * 0.4 * mySize_x;
2283                         }
2284
2285                         if(progressbar)
2286                         {
2287                                 HUD_Panel_GetProgressBarColorForString(leftname);
2288                                 HUD_Panel_DrawProgressBar(barpos, barsize, leftprogressname, 1, barflip, min(1, leftcnt/30), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1), DRAWFLAG_NORMAL);
2289                         }
2290             if(autocvar_hud_panel_powerups_text)
2291             {
2292                 if(leftcnt <= 5)
2293                     drawpic_aspect_skin_expanding(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, bound(0, (leftcnt - leftexact) / 0.5, 1));
2294                 if(leftcnt > 1)
2295                     drawpic_aspect_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2296                 drawstring_aspect(numpos, ftos(leftcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2297             }
2298                 }
2299
2300                 if(rightcnt)
2301                 {
2302             barpos = pos + eX * 0.5 * mySize_x;
2303                         if(baralign == 0 || baralign == 3) { // down align
2304                 barflip = 1;
2305                         } else { // up align
2306                 barflip = 0;
2307                         }
2308
2309                         if(iconalign == 0 || iconalign == 3) { // up align
2310                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2311                                 numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2312                         } else { // down align
2313                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
2314                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
2315                         }
2316
2317                         if(progressbar)
2318                         {
2319                                 HUD_Panel_GetProgressBarColorForString(rightname);
2320                                 HUD_Panel_DrawProgressBar(barpos, barsize, rightprogressname, 1, barflip, min(1, rightcnt/30), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1), DRAWFLAG_NORMAL);
2321                         }
2322             if(autocvar_hud_panel_powerups_text)
2323             {
2324                 if(rightcnt <= 5)
2325                     drawpic_aspect_skin_expanding(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, bound(0, (rightcnt - rightexact) / 0.5, 1));
2326                 if(rightcnt > 1)
2327                     drawpic_aspect_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2328                 drawstring_aspect(numpos, ftos(rightcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2329             }
2330                 }
2331         }
2332 }
2333
2334 // Health/armor (#3)
2335 //
2336 void HUD_HealthArmor(void)
2337 {
2338         if(!autocvar_hud_panel_healtharmor && !autocvar__hud_configure)
2339                 return;
2340
2341         active_panel = HUD_PANEL_HEALTHARMOR;
2342         HUD_Panel_UpdateCvars(healtharmor);
2343         vector pos, mySize;
2344         pos = panel_pos;
2345         mySize = panel_size;
2346
2347         HUD_Panel_DrawBg(1);
2348         if(panel_bg_padding)
2349         {
2350                 pos += '1 1 0' * panel_bg_padding;
2351                 mySize -= '2 2 0' * panel_bg_padding;
2352         }
2353
2354         float armor, health, fuel;
2355         armor = getstati(STAT_ARMOR);
2356         health = getstati(STAT_HEALTH);
2357         fuel = getstati(STAT_FUEL);
2358
2359         if(autocvar__hud_configure)
2360         {
2361                 armor = 75;
2362                 health = 150;
2363                 fuel = 20;
2364         }
2365
2366         if(health <= 0)
2367                 return;
2368
2369         vector barpos, barsize;
2370         vector picpos;
2371         vector numpos;
2372
2373         float baralign = autocvar_hud_panel_healtharmor_baralign;
2374         float iconalign = autocvar_hud_panel_healtharmor_iconalign;
2375         float progressbar = autocvar_hud_panel_healtharmor_progressbar;
2376
2377     float maxhealth = autocvar_hud_panel_healtharmor_maxhealth;
2378     float maxarmor = autocvar_hud_panel_healtharmor_maxarmor;
2379         if(autocvar_hud_panel_healtharmor == 2) // combined health and armor display
2380         {
2381                 vector v;
2382                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
2383
2384                 float x;
2385                 x = floor(v_x + 1);
2386
2387         float maxtotal = maxhealth + maxarmor;
2388
2389         barpos = pos;
2390         barsize = mySize;
2391
2392                 string biggercount;
2393                 if(v_z) // NOT fully armored
2394                 {
2395                         biggercount = "health";
2396                         if(progressbar)
2397                         {
2398                                 HUD_Panel_GetProgressBarColor(health);
2399                                 HUD_Panel_DrawProgressBar(barpos, barsize, autocvar_hud_panel_healtharmor_progressbar_health, 0, mod(baralign, 2), x/maxtotal, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2400                         }
2401                         if(armor)
2402             if(autocvar_hud_panel_healtharmor_text)
2403                                 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);
2404                 }
2405                 else
2406                 {
2407                         biggercount = "armor";
2408                         if(progressbar)
2409                         {
2410                                 HUD_Panel_GetProgressBarColor(armor);
2411                                 HUD_Panel_DrawProgressBar(barpos, barsize, autocvar_hud_panel_healtharmor_progressbar_armor, 0, mod(baralign, 2), x/maxtotal, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2412                         }
2413                         if(health)
2414             if(autocvar_hud_panel_healtharmor_text)
2415                                 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);
2416                 }
2417         if(autocvar_hud_panel_healtharmor_text)
2418             DrawNumIcon(iconalign, pos, mySize, x, biggercount, 1, HUD_Get_Num_Color(x, maxtotal), 1);
2419
2420                 // fuel
2421                 if(fuel)
2422                 {
2423             barpos = pos;
2424             barsize = eX * mySize_x + eY * 0.2 * mySize_y;
2425                         HUD_Panel_GetProgressBarColor(fuel);
2426             HUD_Panel_DrawProgressBar(barpos, barsize, "progressbar", 0, mod(baralign, 2), min(1, fuel/100), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2427                 }
2428         }
2429         else
2430         {
2431                 string leftname, rightname;
2432         string leftprogressname, rightprogressname;
2433                 float leftcnt, rightcnt;
2434                 float leftmax, rightmax;
2435                 float leftactive, rightactive;
2436                 float leftalpha, rightalpha;
2437                 float flip = autocvar_hud_panel_healtharmor_flip;
2438         float barflip;
2439                 if (flip) { // old style layout with armor left/top of health
2440                         leftname = "armor";
2441             leftprogressname = autocvar_hud_panel_healtharmor_progressbar_armor;
2442                         leftcnt = armor;
2443                         if(leftcnt)
2444                                 leftactive = 1;
2445                         leftalpha = min((armor+10)/55, 1);
2446             leftmax = maxarmor;
2447
2448                         rightname = "health";
2449             rightprogressname = autocvar_hud_panel_healtharmor_progressbar_health;
2450                         rightcnt = health;
2451                         rightactive = 1;
2452                         rightalpha = 1;
2453             rightmax = maxhealth;
2454                 } else {
2455                         leftname = "health";
2456             leftprogressname = autocvar_hud_panel_healtharmor_progressbar_health;
2457                         leftcnt = health;
2458                         leftactive = 1;
2459                         leftalpha = 1;
2460             leftmax = maxhealth;
2461
2462                         rightname = "armor";
2463             rightprogressname = autocvar_hud_panel_healtharmor_progressbar_armor;
2464                         rightcnt = armor;
2465                         if(rightcnt)
2466                                 rightactive = 1;
2467                         rightalpha = min((armor+10)/55, 1);
2468             rightmax = maxarmor;
2469                 }
2470
2471                 if (mySize_x/mySize_y > 4)
2472                 {
2473             barsize = eX * 0.5 * mySize_x + eY * mySize_y;
2474                         if(leftactive)
2475                         {
2476                 barpos = pos;
2477                                 if(baralign == 1 || baralign == 3) { // right align
2478                     barflip = 1;
2479                                 } else { // left align
2480                     barflip = 0;
2481                                 }
2482
2483                                 if(progressbar)
2484                                 {
2485                                         HUD_Panel_GetProgressBarColorForString(leftname);
2486                     HUD_Panel_DrawProgressBar(barpos, barsize, leftprogressname, 0, barflip, min(1, leftcnt/leftmax), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2487                                 }
2488                 if(autocvar_hud_panel_healtharmor_text)
2489                     DrawNumIcon(iconalign, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, HUD_Get_Num_Color(leftcnt, leftmax), 1);
2490                         }
2491
2492                         if(rightactive)
2493                         {
2494                 barpos = pos + eX * 0.5 * mySize_x;
2495                                 if(baralign == 0 || baralign == 3) { // left align
2496                     barflip = 0;
2497                                 } else { // right align
2498                     barflip = 1;
2499                                 }
2500
2501                                 if(progressbar)
2502                                 {
2503                                         HUD_Panel_GetProgressBarColorForString(rightname);
2504                     HUD_Panel_DrawProgressBar(barpos, barsize, rightprogressname, 0, barflip, min(1, rightcnt/rightmax), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2505                                 }
2506                 if(autocvar_hud_panel_healtharmor_text)
2507                     DrawNumIcon(iconalign, pos + eX * 0.5 * mySize_x, eX * 0.5 * mySize_x + eY * mySize_y, rightcnt, rightname, 0, HUD_Get_Num_Color(rightcnt, rightmax), 1);
2508                         }
2509
2510                         if(fuel)
2511                         {
2512                 barpos = pos;
2513                 barsize = eX * mySize_x + eY * 0.2 * mySize_y;
2514                 HUD_Panel_GetProgressBarColor(fuel);
2515                 HUD_Panel_DrawProgressBar(barpos, barsize, "progressbar", 0, mod(baralign, 2), min(1, fuel/100), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2516                         }
2517                 }
2518                 else if (mySize_x/mySize_y > 1.5)
2519                 {
2520             barsize = eX * mySize_x + eY * 0.5 * mySize_y;
2521                         if(leftactive)
2522                         {
2523                 barpos = pos;
2524                                 if(baralign == 1 || baralign == 3) { // right align
2525                     barflip = 1;
2526                                 } else { // left align
2527                     barflip = 0;
2528                                 }
2529
2530                                 if(progressbar)
2531                                 {
2532                                         HUD_Panel_GetProgressBarColorForString(leftname);
2533                     HUD_Panel_DrawProgressBar(barpos, barsize, leftprogressname, 0, barflip, min(1, leftcnt/leftmax), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2534                                 }
2535                 if(autocvar_hud_panel_healtharmor_text)
2536                     DrawNumIcon(iconalign, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, HUD_Get_Num_Color(leftcnt, leftmax), 1);
2537                         }
2538
2539                         if(rightactive)
2540                         {
2541                 barpos = pos + eY * 0.5 * mySize_y;
2542                                 if(baralign == 0 || baralign == 3) { // left align
2543                     barflip = 0;
2544                                 } else { // right align
2545                     barflip = 1;
2546                                 }
2547
2548                                 if(progressbar)
2549                                 {
2550                                         HUD_Panel_GetProgressBarColorForString(rightname);
2551                     HUD_Panel_DrawProgressBar(barpos, barsize, rightprogressname, 0, barflip, min(1, rightcnt/rightmax), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2552                                 }
2553                 if(autocvar_hud_panel_healtharmor_text)
2554                     DrawNumIcon(iconalign, pos + eY * 0.5 * mySize_y, eX * mySize_x + eY * 0.5 * mySize_y, rightcnt, rightname, 0, HUD_Get_Num_Color(rightcnt, rightmax), 1);
2555                         }
2556
2557                         if(fuel)
2558                         {
2559                 barpos = pos;
2560                 barsize = eX * mySize_x + eY * 0.2 * mySize_y;
2561                 HUD_Panel_GetProgressBarColor(fuel);
2562                 HUD_Panel_DrawProgressBar(barpos, barsize, "progressbar", 0, mod(baralign, 2), min(1, fuel/100), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2563                         }
2564                 }
2565                 else
2566                 {
2567             barsize = eX * 0.5 * mySize_x + eY * mySize_y;
2568                         if(leftactive)
2569                         {
2570                 barpos = pos;
2571                                 if(baralign == 1 || baralign == 3) { // right align
2572                     barflip = 1;
2573                                 } else { // left align
2574                     barflip = 0;
2575                                 }
2576
2577                                 if(iconalign == 1 || iconalign == 3) { // down align
2578                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
2579                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
2580                                 } else { // up align
2581                                         picpos = pos + eX * 0.05 * mySize_x;
2582                                         numpos = pos + eY * 0.4 * mySize_x;
2583                                 }
2584
2585                                 if(progressbar)
2586                                 {
2587                                         HUD_Panel_GetProgressBarColorForString(leftname);
2588                     HUD_Panel_DrawProgressBar(barpos, barsize, leftprogressname, 1, barflip, min(1, leftcnt/leftmax), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2589                                 }
2590                 if(autocvar_hud_panel_healtharmor_text)
2591                 {
2592                     drawpic_aspect_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2593                     drawstring_aspect(numpos, ftos(leftcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, HUD_Get_Num_Color(leftcnt, leftmax), panel_fg_alpha, DRAWFLAG_NORMAL);
2594                 }
2595                         }
2596
2597                         if(rightactive)
2598                         {
2599                 barpos = pos + eX * 0.5 * mySize_x;
2600                                 if(baralign == 0 || baralign == 3) { // left align
2601                     barflip = 0;
2602                                 } else { // right align
2603                     barflip = 1;
2604                                 }
2605
2606                                 if(iconalign == 0 || iconalign == 3) { // up align
2607                                         picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2608                                         numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2609                                 } else { // down align
2610                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
2611                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
2612                                 }
2613
2614                                 if(progressbar)
2615                                 {
2616                                         HUD_Panel_GetProgressBarColorForString(rightname);
2617                     HUD_Panel_DrawProgressBar(barpos, barsize, rightprogressname, 1, barflip, min(1, rightcnt/rightmax), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2618                                 }
2619                 if(autocvar_hud_panel_healtharmor_text)
2620                 {
2621                     drawpic_aspect_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2622                     drawstring_aspect(numpos, ftos(rightcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, HUD_Get_Num_Color(rightcnt, rightmax), panel_fg_alpha, DRAWFLAG_NORMAL);
2623                 }
2624                         }
2625
2626                         if(fuel)
2627                         {
2628                 barpos = pos;
2629                 barsize = eX * 0.05 * mySize_x + eY * mySize_y;
2630                 HUD_Panel_GetProgressBarColor(fuel);
2631                 HUD_Panel_DrawProgressBar(barpos, barsize, "progressbar", 1, mod(baralign, 2), min(1, fuel/100), progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2632                         }
2633                 }
2634         }
2635 }
2636
2637 // Notification area (#4)
2638 //
2639
2640 string Weapon_SuicideMessage(float deathtype)
2641 {
2642         w_deathtype = deathtype;
2643         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_SUICIDEMESSAGE);
2644         return w_deathtypestring;
2645 }
2646
2647 string Weapon_KillMessage(float deathtype)
2648 {
2649         w_deathtype = deathtype;
2650         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_KILLMESSAGE);
2651         return w_deathtypestring;
2652 }
2653
2654 #define KN_MAX_ENTRIES 10
2655 float kn_index;
2656 float killnotify_times[KN_MAX_ENTRIES];
2657 float killnotify_deathtype[KN_MAX_ENTRIES];
2658 float killnotify_actiontype[KN_MAX_ENTRIES]; // 0 = "Y [used by] X", 1 = "X [did action to] Y"
2659 string killnotify_attackers[KN_MAX_ENTRIES];
2660 string killnotify_victims[KN_MAX_ENTRIES];
2661 void HUD_KillNotify_Push(string attacker, string victim, float actiontype, float wpn)
2662 {
2663         --kn_index;
2664         if (kn_index == -1)
2665                 kn_index = KN_MAX_ENTRIES-1;
2666         killnotify_times[kn_index] = time;
2667         killnotify_deathtype[kn_index] = wpn;
2668         killnotify_actiontype[kn_index] = actiontype;
2669         if(killnotify_attackers[kn_index])
2670                 strunzone(killnotify_attackers[kn_index]);
2671         killnotify_attackers[kn_index] = strzone(attacker);
2672         if(killnotify_victims[kn_index])
2673                 strunzone(killnotify_victims[kn_index]);
2674         killnotify_victims[kn_index] = strzone(victim);
2675 }
2676
2677 void HUD_KillNotify(string s1, string s2, string s3, float type, float msg) // s1 = attacker, s2 = victim
2678 {
2679         float w;
2680         float alsoprint, gentle;
2681         alsoprint = (autocvar_hud_panel_notify_print || !panel_enabled); // print message to console if: notify panel disabled, or cvar to do so enabled
2682         gentle = (autocvar_cl_gentle || autocvar_cl_gentle_messages);
2683         
2684         if ((msg == MSG_SUICIDE || msg == MSG_KILL || msg == MSG_KILL_ACTION) && gametype == GAME_CTS) // selfkill isn't interesting in CTS and only spams up the notify panel
2685                 return;
2686
2687         if(msg == MSG_SUICIDE) {
2688                 w = DEATH_WEAPONOF(type);
2689                 if(WEP_VALID(w)) {
2690                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2691                         if (alsoprint)
2692                                 print("^1", sprintf(Weapon_SuicideMessage(type), strcat(s1, "^1")), "\n");
2693                 } else if (type == DEATH_KILL) {
2694                         HUD_KillNotify_Push(s1, "", 0, DEATH_KILL);
2695                         if (alsoprint)
2696                                 print (sprintf(_("^1%s^1 couldn't take it anymore\n"), s1));
2697                 } else if (type == DEATH_ROT) {
2698                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2699                         if (alsoprint)
2700                                 print (sprintf(_("^1%s^1 died\n"), s1));
2701                 } else if (type == DEATH_NOAMMO) {
2702                         HUD_KillNotify_Push(s1, "", 0, DEATH_NOAMMO);
2703                         if (alsoprint)
2704                                 print (sprintf(_("^7%s^7 committed suicide. What's the point of living without ammo?\n"), s1));
2705                 } else if (type == DEATH_CAMP) {
2706                         HUD_KillNotify_Push(s1, "", 0, DEATH_CAMP);
2707                         if (alsoprint)
2708                                 print (sprintf(_("^1%s^1 thought they found a nice camping ground\n"), s1));
2709                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2710                         HUD_KillNotify_Push(s1, "", 0, type);
2711                         if (alsoprint)
2712                                 print (sprintf(_("^1%s^1 didn't become friends with the Lord of Teamplay\n"), s1));
2713                 } else if (type == DEATH_CHEAT) {
2714                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2715                         if (alsoprint)
2716                                 print (sprintf(_("^1%s^1 unfairly eliminated themself\n"), s1));
2717                 } else if (type == DEATH_FIRE) {
2718                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2719                         if (alsoprint)
2720                                 print (sprintf(_("^1%s^1 burned to death\n"), s1));
2721                 } else if (type != DEATH_TEAMCHANGE && type != DEATH_QUIET) {
2722                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2723                         if (alsoprint)
2724                                 print (sprintf(_("^1%s^1 couldn't resist the urge to self-destruct\n"), s1));
2725                 } 
2726                 
2727                 if (stof(s2) > 2) // killcount > 2
2728                         print (sprintf(_("^1%s^1 ended it all after a %d kill spree\n"), s1, stof(s2)));
2729         } else if(msg == MSG_KILL) {
2730                 w = DEATH_WEAPONOF(type);
2731                 if(WEP_VALID(w)) {
2732                         if((w == WEP_SNIPERRIFLE || w == WEP_MINSTANEX) && type & HITTYPE_HEADSHOT) // all headshot weapons go here
2733                                 HUD_KillNotify_Push(s1, s2, 1, DEATH_HEADSHOT);
2734                         else
2735                                 HUD_KillNotify_Push(s1, s2, 1, type);
2736
2737                         if (alsoprint)
2738                                 print("^1", sprintf(Weapon_KillMessage(type), strcat(s2, "^1"), strcat(s1, "^1")), "\n"); // default order: victim, killer
2739                 }
2740                 else if(type == KILL_TEAM_RED || type == KILL_TEAM_BLUE || type == KILL_TEAM_SPREE) {
2741                         HUD_KillNotify_Push(s1, s2, 1, type);
2742                         if(alsoprint)
2743                         {
2744                                 if(gentle) {
2745                                         print (sprintf(_("^1%s^1 took action against a team mate\n"), s1));
2746                                 } else {
2747                                         print (sprintf(_("^1%s^1 mows down a team mate\n"), s1));
2748                                 }
2749                         }
2750                         if (stof(s2) > 2 && type == KILL_TEAM_SPREE) {
2751                                 if(gentle)
2752                                         print (sprintf(_("^1%s^1 ended a %d scoring spree by going against a team mate\n"), s1, stof(s3)));
2753                                 else
2754                                         print (sprintf(_("^1%s^1 ended a %d kill spree by killing a team mate\n"), s1, stof(s3)));
2755                         }
2756                         else if (stof(s2) > 2) {
2757                                 if(gentle)
2758                                         print (sprintf(_("^1%s^1's %s scoring spree was ended by a team mate!\n"), s1, stof(s3)));
2759                                 else
2760                                         print (sprintf(_("^1%s^1's %s kill spree was ended by a team mate!\n"), s1, stof(s3)));
2761                         }
2762                 }
2763                 else if(type == KILL_FIRST_BLOOD)
2764                         print(sprintf(_("^1%s^1 drew first blood\n"), s1));
2765                 else if (type == DEATH_TELEFRAG) {
2766                         HUD_KillNotify_Push(s1, s2, 1, DEATH_TELEFRAG);
2767                         if(gentle)
2768                                 print (sprintf(_("^1%s^1 tried to occupy %s^1's teleport destination space\n"), s2, s1));
2769                         else
2770                                 print (sprintf(_("^1%s^1 was telefragged by %s\n"), s2, s1));
2771                 }
2772                 else if (type == DEATH_DROWN) {
2773                         HUD_KillNotify_Push(s1, s2, 1, DEATH_DROWN);
2774                         if(alsoprint)
2775                                 print (sprintf(_("^1%s^1 was drowned by %s\n"), s2, s1));
2776                 }
2777                 else if (type == DEATH_SLIME) {
2778                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SLIME);
2779                         if(alsoprint)
2780                                 print (sprintf(_("^1%s^1 was slimed by %s\n"), s2, s1));
2781                 }
2782                 else if (type == DEATH_LAVA) {
2783                         HUD_KillNotify_Push(s1, s2, 1, DEATH_LAVA);
2784                         if(alsoprint)
2785                                 print (sprintf(_("^1%s^1 was cooked by %s\n"), s2, s1));
2786                 }
2787                 else if (type == DEATH_FALL) {
2788                         HUD_KillNotify_Push(s1, s2, 1, DEATH_FALL);
2789                         if(alsoprint)
2790                                 print (sprintf(_("^1%s^1 was grounded by %s\n"), s2, s1));
2791                 }
2792                 else if (type == DEATH_SHOOTING_STAR) {
2793                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SHOOTING_STAR);
2794                         if(alsoprint)
2795                                 print (sprintf(_("^1%s^1 was shot into space by %s\n"), s2, s1));
2796                 }
2797                 else if (type == DEATH_SWAMP) {
2798                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2799                         if(alsoprint)
2800                                 print (sprintf(_("^1%s^1 was conserved by %s\n"), s2, s1));
2801                 }
2802                 else if (type == DEATH_HURTTRIGGER)
2803                 {
2804                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2805                         if(alsoprint)
2806                                 print(sprintf(_("^1%s^1 was thrown into a world of hurt by %s\n"), s2, s1));
2807                 } else if(type == DEATH_SBCRUSH) {
2808                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2809                         if(alsoprint)
2810                                 print (sprintf(_("^1%s^1 was crushed by %s\n"), s2, s1));
2811                 } else if(type == DEATH_SBMINIGUN) {
2812                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2813                         if(alsoprint)
2814                                 print (sprintf(_("^1%s^1 got shredded by %s\n"), s2, s1));
2815                 } else if(type == DEATH_SBROCKET) {
2816                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2817                         if(alsoprint)
2818                                 print (sprintf(_("^1%s^1 was blasted to bits by %s\n"), s2, s1));
2819                 } else if(type == DEATH_SBBLOWUP) {
2820                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2821                         if(alsoprint)
2822                                 print (sprintf(_("^1%s^1 got caught in the destruction of %s^1's vehicle\n"), s2, s1));
2823                 } else if(type == DEATH_WAKIGUN) {
2824                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2825                         if(alsoprint)
2826                                 print (sprintf(_("^1%s^1 was bolted down by %s\n"), s2, s1));
2827                 } else if(type == DEATH_WAKIROCKET) {
2828                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2829                         if(alsoprint)
2830                                 print (sprintf(_("^1%s^1 could find no shelter from %s^1's rockets\n"), s2, s1));
2831                 } else if(type == DEATH_WAKIBLOWUP) {
2832                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2833                         if(alsoprint)
2834                                 print (sprintf(_("^1%s^1 dies when %s^1's wakizashi dies.\n"), s2, s1));
2835                 } else if(type == DEATH_TURRET) {
2836                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2837                         if(alsoprint)
2838                                 print (sprintf(_("^1%s^1 was pushed into the line of fire by %s\n"), s2, s1));
2839                 } else if(type == DEATH_TOUCHEXPLODE) {
2840                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2841                         if(alsoprint)
2842                                 print (sprintf(_("^1%s^1 was pushed into an accident by %s\n"), s2, s1));
2843                 } else if(type == DEATH_CHEAT) {
2844                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2845                         if(alsoprint)
2846                                 print (sprintf(_("^1%s^1 was unfairly eliminated by %s\n"), s2, s1));
2847                 } else if (type == DEATH_FIRE) {
2848                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2849                         if(alsoprint)
2850                                 print (sprintf(_("^1%s^1 was burnt to death by %s\n"), s2, s1));
2851                 } else if (type == DEATH_CUSTOM) {
2852                         HUD_KillNotify_Push(s1, s2, 1, DEATH_CUSTOM);
2853                         if(alsoprint)
2854                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2855                 } else if (type == DEATH_HURTTRIGGER) {
2856                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2857                         if(alsoprint)
2858                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2859                 } else {
2860                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2861                         if(alsoprint)
2862                                 print (sprintf(_("^1%s^1 was fragged by %s\n"), s2, s1));
2863                 }
2864         } else if(msg == MSG_SPREE) {
2865                 if(type == KILL_END_SPREE) {
2866                         if(gentle)
2867                                 print (sprintf(_("^1%s^1's %s scoring spree was ended by %s\n"), s1, s2, s3));
2868                         else
2869                                 print (sprintf(_("^1%s^1's %s kill spree was ended by %s\n"), s1, s2, s3));
2870                 } else if(type == KILL_SPREE) {
2871                         if(gentle)
2872                                 print (sprintf(_("^1%s^1 made %s scores in a row\n"), s1, s2));
2873                         else
2874                                 print (sprintf(_("^1%s^1 has %s frags in a row\n"), s1, s2));
2875                 } else if(type == KILL_SPREE_3) {
2876                         if(gentle)
2877                                 print (sprintf(_("%s^7 made a ^1TRIPLE SCORE\n"), s1));
2878                         else
2879                                 print (sprintf(_("%s^7 made a ^1TRIPLE FRAG\n"), s1));
2880                 } else if(type == KILL_SPREE_5) {
2881                         if(gentle)
2882                                 print (sprintf(_("%s^7 unleashes ^1SCORING RAGE\n"), s1));
2883                         else
2884                                 print (sprintf(_("%s^7 unleashes ^1RAGE\n"), s1));
2885                 } else if(type == KILL_SPREE_10) {
2886                         if(gentle)
2887                                 print (sprintf(_("%s^7 made ^1TEN SCORES IN A ROW!\n"), s1));
2888                         else
2889                                 print (sprintf(_("%s^7 starts the ^1MASSACRE!\n"), s1));
2890                 } else if(type == KILL_SPREE_15) {
2891                         if(gentle)
2892                                 print (sprintf(_("%s^7 made ^1FIFTEEN SCORES IN A ROW!\n"), s1));
2893                         else
2894                                 print (sprintf(_("%s^7 executes ^1MAYHEM!\n"), s1));
2895                 } else if(type == KILL_SPREE_20) {
2896                         if(gentle)
2897                                 print (sprintf(_("%s^7 made ^1TWENTY SCORES IN A ROW!\n"), s1));
2898                         else
2899                                 print (sprintf(_("%s^7 is a ^1BERSERKER!\n"), s1));
2900                 } else if(type == KILL_SPREE_25) {
2901                         if(gentle)
2902                                 print (sprintf(_("%s^7 made ^1TWENTY FIVE SCORES IN A ROW!\n"), s1));
2903                         else
2904                                 print (sprintf(_("%s^7 inflicts ^1CARNAGE!\n"), s1));
2905                 } else if(type == KILL_SPREE_30) {
2906                         if(gentle)
2907                                 print (sprintf(_("%s^7 made ^1THIRTY SCORES IN A ROW!\n"), s1));
2908                         else
2909                                 print (sprintf(_("%s^7 unleashes ^1ARMAGEDDON!\n"), s1));
2910                 }
2911         } else if(msg == MSG_KILL_ACTION) { // wtf is this? isnt it basically the same as MSG_SUICIDE?
2912                 if (type == DEATH_DROWN) {
2913                         HUD_KillNotify_Push(s1, "", 0, DEATH_DROWN);
2914                         if(alsoprint)
2915                         {
2916                                 if(gentle)
2917                                         print (sprintf(_("^1%s^1 was in the water for too long\n"), s1));
2918                                 else
2919                                         print (sprintf(_("^1%s^1 drowned\n"), s1));
2920                         }
2921                 } else if (type == DEATH_SLIME) {
2922                         HUD_KillNotify_Push(s1, "", 0, DEATH_SLIME);
2923                         if(alsoprint)
2924                                 print (sprintf(_("^1%s^1 was slimed\n"), s1));
2925                 } else if (type == DEATH_LAVA) {
2926                         HUD_KillNotify_Push(s1, "", 0, DEATH_LAVA);
2927                         if(alsoprint)
2928                         {
2929                                 if(gentle)
2930                                         print (sprintf(_("^1%s^1 found a hot place\n"), s1));
2931                                 else
2932                                         print (sprintf(_("^1%s^1 turned into hot slag\n"), s1));
2933                         }
2934                 } else if (type == DEATH_FALL) {
2935                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2936                         if(alsoprint)
2937                         {
2938                                 if(gentle)
2939                                         print (sprintf(_("^1%s^1 tested gravity (and it worked)\n"), s1));
2940                                 else
2941                                         print (sprintf(_("^1%s^1 hit the ground with a crunch\n"), s1));
2942                         }
2943                 } else if (type == DEATH_SHOOTING_STAR) {
2944                         HUD_KillNotify_Push(s1, "", 0, DEATH_SHOOTING_STAR);
2945                         if(alsoprint)
2946                                 print (sprintf(_("^1%s^1 became a shooting star\n"), s1));
2947                 } else if (type == DEATH_SWAMP) {
2948                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2949                         if(alsoprint)
2950                         {
2951                                 if(gentle)
2952                                         print (sprintf(_("^1%s^1 discovered a swamp\n"), s1));
2953                                 else
2954                                         print (sprintf(_("^1%s^1 is now conserved for centuries to come\n"), s1));
2955                         }
2956                 } else if(type == DEATH_TURRET) {
2957                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2958                         if(alsoprint)
2959                                 print (sprintf(_("^1%s^1 was mowed down by a turret \n"), s1));
2960                 } else if (type == DEATH_CUSTOM) {
2961                         HUD_KillNotify_Push(s1, "", 0, DEATH_CUSTOM);
2962                         if(alsoprint)
2963                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
2964                 } else if (type == DEATH_HURTTRIGGER) {
2965                         HUD_KillNotify_Push(s1, "", 0, DEATH_HURTTRIGGER);
2966                         if(alsoprint)
2967                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
2968                 } else if(type == DEATH_TOUCHEXPLODE) {
2969                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2970                         if(alsoprint)
2971                                 print (sprintf(_("^1%s^1 died in an accident\n"), s1));
2972                 } else if(type == DEATH_CHEAT) {
2973                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2974                         if(alsoprint)
2975                                 print (sprintf(_("^1%s^1 was unfairly eliminated\n"), s1));
2976                 } else if(type == DEATH_FIRE) {
2977                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2978                         if(alsoprint)
2979                         {
2980                                 if(gentle)
2981                                         print (sprintf(_("^1%s^1 felt a little hot\n"), s1));
2982                                 else
2983                                         print (sprintf(_("^1%s^1 burnt to death\n"), s1));
2984                                 }
2985                 } else {
2986                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2987                         if(alsoprint)
2988                         {
2989                                 if(gentle)
2990                                         print (sprintf(_("^1%s^1 needs a restart\n"), s1));
2991                                 else
2992                                         print (sprintf(_("^1%s^1 died\n"), s1));
2993                         }
2994                 }
2995         } else if(msg == MSG_KILL_ACTION_SPREE) {
2996                 if(gentle)
2997                         print (sprintf(_("^1%s^1 needs a restart after a %d scoring spree\n"), s1, stof(s2)));
2998                 else
2999                         print (sprintf(_("^1%s^1 died with a %d kill spree\n"), s1, stof(s2)));
3000         } else if(msg == MSG_INFO) {
3001                 if(type == INFO_GOTFLAG) { // here, s2 is the flag name
3002                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
3003                         print(sprintf(_("%s^7 got the %s\n"), s1, s2));
3004                 } else if(type == INFO_LOSTFLAG) {
3005                         HUD_KillNotify_Push(s1, s2, 0, INFO_LOSTFLAG);
3006                         print(sprintf(_("%s^7 lost the %s\n"), s1, s2));
3007                 } else if(type == INFO_PICKUPFLAG) {
3008                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
3009                         print(sprintf(_("%s^7 picked up the %s\n"), s1, s2));
3010                 } else if(type == INFO_RETURNFLAG) {
3011                         HUD_KillNotify_Push(s1, s2, 0, INFO_RETURNFLAG);
3012                         print(sprintf(_("%s^7 returned the %s\n"), s1, s2));
3013                 } else if(type == INFO_CAPTUREFLAG) {
3014                         HUD_KillNotify_Push(s1, s2, 0, INFO_CAPTUREFLAG);
3015                         print(sprintf(_("%s^7 captured the %s%s\n"), s1, s2, s3));
3016                 }
3017         } else if(msg == MSG_RACE) {
3018                 if(type == RACE_SERVER_RECORD) {
3019                         HUD_KillNotify_Push(s1, s2, 1, RACE_SERVER_RECORD);
3020                 }
3021                 else if(type == RACE_NEW_RANK) {
3022                         HUD_KillNotify_Push(s1, s2, 1, RACE_NEW_RANK);
3023                 }
3024                 else if(type == RACE_NEW_TIME) {
3025                         HUD_KillNotify_Push(s1, s2, 1, RACE_NEW_TIME);
3026                 }
3027                 else if(type == RACE_FAIL) {
3028                         HUD_KillNotify_Push(s1, s2, 1, RACE_FAIL);
3029                 }
3030         } else if(msg == MSG_KA) {
3031                 if(type == KA_PICKUPBALL) {
3032                         HUD_KillNotify_Push(s1, s2, 0, KA_PICKUPBALL);
3033                         if(alsoprint)
3034                                 print (sprintf(_("%s^7 has picked up the ball!\n"), s1));
3035                 }
3036                 else if(type == KA_DROPBALL) {
3037                         HUD_KillNotify_Push(s1, s2, 0, KA_DROPBALL);
3038                         if(alsoprint)
3039                                 print(sprintf(_("%s^7 has dropped the ball!\n"), s1));
3040                 }
3041         }
3042 }
3043
3044 #define DAMAGE_CENTERPRINT_SPACER NEWLINES
3045
3046 void HUD_Centerprint(string s1, string s2, float type, float msg)
3047 {
3048         float gentle;
3049         gentle = (autocvar_cl_gentle || autocvar_cl_gentle_messages);
3050         if(msg == MSG_SUICIDE) {
3051                 if (type == DEATH_TEAMCHANGE) {
3052                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("You are now on: %s"), s1)));
3053                 } else if (type == DEATH_AUTOTEAMCHANGE) {
3054                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("You have been moved into a different team to improve team balance\nYou are now on: %s"), s1)));
3055                 } else if (type == DEATH_CAMP) {
3056                         if(gentle)
3057                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Reconsider your tactics, camper!")));
3058                         else
3059                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Die camper!")));
3060                 } else if (type == DEATH_NOAMMO) {
3061                         if(gentle)
3062                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You are reinserted into the game for running out of ammo...")));
3063                         else
3064                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You were killed for running out of ammo...")));
3065                 } else if (type == DEATH_ROT) {
3066                         if(gentle)
3067                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You need to preserve your health")));
3068                         else
3069                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You grew too old without taking your medicine")));
3070                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
3071                         if(gentle)
3072                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Don't go against team mates!")));
3073                         else
3074                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Don't shoot your team mates!")));
3075                 } else if (type == DEATH_QUIET) {
3076                         // do nothing
3077                 } else { // generic message
3078                         if(gentle)
3079                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You need to be more careful!")));
3080                         else
3081                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1You killed your own dumb self!")));
3082                 }
3083         } else if(msg == MSG_KILL) {
3084                 if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
3085                         if(gentle) {
3086                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1Moron! You went against %s, a team mate!"), s1)));
3087                         } else {
3088                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1Moron! You fragged %s, a team mate!"), s1)));
3089                         }
3090                 } else if (type == KILL_FIRST_BLOOD) {
3091                         if(gentle) {
3092                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1First score")));
3093                         } else {
3094                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1First blood")));
3095                         }
3096                 } else if (type == KILL_FIRST_VICTIM) {
3097                         if(gentle) {
3098                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1First casualty")));
3099                         } else {
3100                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1First victim")));
3101                         }
3102                 } else if (type == KILL_TYPEFRAG) { // s2 contains "advanced kill messages" such as ping, handicap...
3103                         if(gentle) {
3104                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You scored against ^7%s^1 who was typing!"), s1), s2));
3105                         } else {
3106                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You typefragged ^7%s"), s1), s2));
3107                         }
3108                 } else if (type == KILL_TYPEFRAGGED) {
3109                         if(gentle) {
3110                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You were scored against by ^7%s^1 while you were typing!"), s1), s2));
3111                         } else {
3112                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You were typefragged by ^7%s"), s1), s2));
3113                         }
3114                 } else if (type == KILL_FRAG) {
3115                         if(gentle) {
3116                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^4You scored against ^7%s"), s1), s2));
3117                         } else {
3118                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^4You fragged ^7%s"), s1), s2));
3119                         }
3120                 } else { // generic message
3121                         if(gentle) {
3122                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You were scored against by ^7%s"), s1), s2));
3123                         } else {
3124                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, sprintf(_("^1You were fragged by ^7%s"), s1), s2));
3125                         }
3126                 }
3127         } else if(msg == MSG_KILL_ACTION) {
3128                 // TODO: invent more centerprints here?
3129                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, _("^1Watch your step!")));
3130         }
3131 }
3132
3133 void HUD_Notify (void)
3134 {
3135         if(!autocvar_hud_panel_notify && !autocvar__hud_configure)
3136                 return;
3137
3138         active_panel = HUD_PANEL_NOTIFY;
3139         HUD_Panel_UpdateCvars(notify);
3140         vector pos, mySize;
3141         pos = panel_pos;
3142         mySize = panel_size;
3143
3144         HUD_Panel_DrawBg(1);
3145         if(panel_bg_padding)
3146         {
3147                 pos += '1 1 0' * panel_bg_padding;
3148                 mySize -= '2 2 0' * panel_bg_padding;
3149         }
3150
3151         float entries, height;
3152         entries = bound(1, floor(KN_MAX_ENTRIES * mySize_y/mySize_x), KN_MAX_ENTRIES);
3153         height = mySize_y/entries;
3154         
3155         vector fontsize;
3156         float fontheight = height * autocvar_hud_panel_notify_fontsize;
3157         fontsize = '0.5 0.5 0' * fontheight;
3158
3159         float a;
3160         float when;
3161         when = autocvar_hud_panel_notify_time;
3162         float fadetime;
3163         fadetime = autocvar_hud_panel_notify_fadetime;
3164
3165         string s;
3166
3167         vector pos_attacker, pos_victim;
3168         vector weap_pos;
3169         float width_attacker;
3170         string attacker, victim;
3171
3172         float i, j, w, step, limit;
3173         if(autocvar_hud_panel_notify_flip) //order items from the top down
3174         {
3175                 i = 0;
3176                 step = +1;
3177                 limit = entries;
3178         }
3179         else //order items from the bottom up
3180         {
3181                 i = entries - 1;
3182                 step = -1;
3183                 limit = -1;
3184         }
3185
3186         for(j = kn_index;  i != limit;  i += step, ++j)
3187         {
3188                 if(autocvar__hud_configure)
3189                 {
3190                         if (step == +1)
3191                                 a = i;
3192                         else // inverse order
3193                                 a = entries - 1 - i;
3194                         attacker = textShortenToWidth(sprintf(_("Player %d"), a+1), 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3195                         victim = textShortenToWidth(sprintf(_("Player %d"), a+2), 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3196                         s = strcat("weapon", get_weaponinfo(WEP_FIRST + mod(floor(a*2.4), WEP_LAST)).netname);
3197                         a = bound(0, (when - a) / 4, 1);
3198                         goto hud_config_notifyprint;
3199                 }
3200
3201                 if (j == KN_MAX_ENTRIES)
3202                         j = 0;
3203
3204                 if(killnotify_times[j] + when > time)
3205                         a = 1;
3206                 else if(fadetime)
3207                 {
3208                         a = bound(0, (killnotify_times[j] + when + fadetime - time) / fadetime, 1);
3209                         if(!a)
3210                         {
3211                                 break;
3212                         }
3213                 }
3214                 else
3215                 {
3216                         break;
3217                 }
3218
3219                 s = "";
3220
3221                 w = -1;
3222                 w = DEATH_WEAPONOF(killnotify_deathtype[j]);
3223
3224                 // TODO: maybe print in team colors?
3225                 //
3226                 // Y [used by] X
3227                 if(killnotify_actiontype[j] == 0) 
3228                 {
3229                         if(killnotify_deathtype[j] == DEATH_GENERIC)
3230                         {
3231                                 s = "notify_death";
3232                         }
3233                         else if(killnotify_deathtype[j] == DEATH_NOAMMO)
3234                         {
3235                                 s = "notify_outofammo";
3236                         }
3237                         else if(killnotify_deathtype[j] == DEATH_KILL)
3238                         {
3239                                 s = "notify_selfkill";
3240                         }
3241                         else if(killnotify_deathtype[j] == DEATH_CAMP)
3242                         {
3243                                 s = "notify_camping";
3244                         }
3245                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
3246                         {
3247                                 s = "notify_teamkill_red";
3248                         }
3249                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
3250                         {
3251                                 s = "notify_teamkill_blue";
3252                         }
3253                         else if(killnotify_deathtype[j] == DEATH_DROWN)
3254                         {
3255                                 s = "notify_water";
3256                         }
3257                         else if(killnotify_deathtype[j] == DEATH_SLIME)
3258                         {
3259                                 s = "notify_slime";
3260                         }
3261                         else if(killnotify_deathtype[j] == DEATH_LAVA)
3262                         {
3263                                 s = "notify_lava";
3264                         }
3265                         else if(killnotify_deathtype[j] == DEATH_FALL)
3266                         {
3267                                 s = "notify_fall";
3268                         }
3269                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
3270                         {
3271                                 s = "notify_shootingstar";
3272                         }
3273                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM)
3274                         {
3275                                 s = "notify_death";
3276                         }
3277                         else if(killnotify_deathtype[j] == INFO_GOTFLAG)
3278                         {
3279                                 if(killnotify_victims[j] == "^1RED^7 flag")
3280                                 {
3281                                         s = "notify_red_taken";
3282                                 }
3283                                 else
3284                                 {
3285                                         s = "notify_blue_taken";
3286                                 }
3287                         }
3288                         else if(killnotify_deathtype[j] == INFO_RETURNFLAG)
3289                         {
3290                                 if(killnotify_victims[j] == "^1RED^7 flag")
3291                                 {
3292                                         s = "notify_red_returned";
3293                                 }
3294                                 else
3295                                 {
3296                                         s = "notify_blue_returned";
3297                                 }
3298                         }
3299                         else if(killnotify_deathtype[j] == INFO_LOSTFLAG)
3300                         {
3301                                 if(killnotify_victims[j] == "^1RED^7 flag")
3302                                 {
3303                                         s = "notify_red_lost";
3304                                 }
3305                                 else
3306                                 {
3307                                         s = "notify_blue_lost";
3308                                 }
3309                         }
3310                         else if(killnotify_deathtype[j] == INFO_CAPTUREFLAG)
3311                         {
3312                                 if(killnotify_victims[j] == "^1RED^7 flag")
3313                                 {
3314                                         s = "notify_red_captured";
3315                                 }
3316                                 else
3317                                 {
3318                                         s = "notify_blue_captured";
3319                                 }
3320                         }
3321                         else if(killnotify_deathtype[j] == KA_DROPBALL)
3322                         {
3323                                 s = "notify_balldropped";
3324                         }
3325                         else if(killnotify_deathtype[j] == KA_PICKUPBALL)
3326                         {
3327                                 s = "notify_ballpickedup";
3328                         }
3329                         
3330                         attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3331                         pos_attacker = pos + eX * (0.52 * mySize_x + height) + eY * ((0.5 * fontsize_y + i * height) + (0.5 * (height - fontheight)));
3332                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
3333
3334                         if(s != "")
3335                         {
3336                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3337                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3338                         }
3339                 }
3340                 // X [did action to] Y
3341                 else
3342                 {
3343                         if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_LASER)
3344                         {
3345                                 s = "notify_melee_laser";
3346                         }
3347                         else if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_SHOTGUN)
3348                         {
3349                                 s = "notify_melee_shotgun";
3350                         }
3351                         else if(WEP_VALID(w))
3352                         {
3353                                 self = get_weaponinfo(w);
3354                                 s = strcat("weapon", self.netname);
3355                         }
3356                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
3357                         {
3358                                 s = "notify_teamkill_red";
3359                         }
3360                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
3361                         {
3362                                 s = "notify_teamkill_red";
3363                         }
3364                         else if(killnotify_deathtype[j] == DEATH_TELEFRAG)
3365                         {
3366                                 s = "notify_telefrag";
3367                         }
3368                         else if(killnotify_deathtype[j] == DEATH_DROWN)
3369                         {
3370                                 s = "notify_water";
3371                         }
3372                         else if(killnotify_deathtype[j] == DEATH_SLIME)
3373                         {
3374                                 s = "notify_slime";
3375                         }
3376                         else if(killnotify_deathtype[j] == DEATH_LAVA)
3377                         {
3378                                 s = "notify_lava";
3379                         }
3380                         else if(killnotify_deathtype[j] == DEATH_FALL)
3381                         {
3382                                 s = "notify_fall";
3383                         }
3384                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
3385                         {
3386                                 s = "notify_shootingstar";
3387                         }
3388                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM) // DEATH_CUSTOM is also void, right?
3389                         {
3390                                 s = "notify_void";
3391                         }
3392                         else if(killnotify_deathtype[j] == DEATH_HEADSHOT)
3393                         {
3394                                 s = "notify_headshot";
3395                         }
3396                         else if(killnotify_deathtype[j] == RACE_SERVER_RECORD)
3397                         {
3398                                 s = "race_newrecordserver";
3399                         }
3400                         else if(killnotify_deathtype[j] == RACE_NEW_RANK)
3401                         {
3402                                 s = "race_newrankyellow";
3403                         }
3404                         else if(killnotify_deathtype[j] == RACE_NEW_TIME)
3405                         {
3406                                 s = "race_newtime";
3407                         }
3408                         else if(killnotify_deathtype[j] == RACE_FAIL)
3409                         {
3410                                 s = "race_newfail";
3411                         }
3412
3413                         attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3414                         victim = textShortenToWidth(killnotify_victims[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3415 :hud_config_notifyprint
3416                         width_attacker = stringwidth(attacker, TRUE, fontsize);
3417                         pos_attacker = pos + eX * (0.48 * mySize_x - height - width_attacker) + eY * ((0.5 * fontsize_y + i * height) + (0.5 * (height - fontheight)));
3418                         pos_victim = pos + eX * (0.52 * mySize_x + height) + eY * ((0.5 * fontsize_y + i * height) + (0.5 * (height - fontheight)));
3419                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
3420
3421                         if(s != "")
3422                         {
3423                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3424                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3425                                 drawcolorcodedstring(pos_victim, victim, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3426                         }
3427                 }
3428         }
3429 }
3430
3431 // Timer (#5)
3432 //
3433 // TODO: macro
3434 string seconds_tostring(float sec)
3435 {
3436         float minutes;
3437         minutes = floor(sec / 60);
3438
3439         sec -= minutes * 60;
3440         return sprintf("%d:%02d", minutes, sec);
3441 }
3442
3443 void HUD_Timer(void)
3444 {
3445         if(!autocvar_hud_panel_timer && !autocvar__hud_configure)
3446                 return;
3447
3448         active_panel = HUD_PANEL_TIMER;
3449         HUD_Panel_UpdateCvars(timer);
3450         vector pos, mySize;
3451         pos = panel_pos;
3452         mySize = panel_size;
3453
3454         HUD_Panel_DrawBg(1);
3455         if(panel_bg_padding)
3456         {
3457                 pos += '1 1 0' * panel_bg_padding;
3458                 mySize -= '2 2 0' * panel_bg_padding;
3459         }
3460
3461         string timer;
3462         float timelimit, elapsedTime, timeleft, minutesLeft;
3463
3464         timelimit = getstatf(STAT_TIMELIMIT);
3465
3466         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
3467         timeleft = ceil(timeleft);
3468
3469         minutesLeft = floor(timeleft / 60);
3470
3471         vector timer_color;
3472         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
3473                 timer_color = '1 1 1'; //white
3474         else if(minutesLeft >= 1)
3475                 timer_color = '1 1 0'; //yellow
3476         else
3477                 timer_color = '1 0 0'; //red
3478
3479         if (autocvar_hud_panel_timer_increment || timelimit == 0 || warmup_stage) {
3480                 if (time < getstatf(STAT_GAMESTARTTIME)) {
3481                         //while restart is still active, show 00:00
3482                         timer = seconds_tostring(0);
3483                 } else {
3484                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
3485                         timer = seconds_tostring(elapsedTime);
3486                 }
3487         } else {
3488                 timer = seconds_tostring(timeleft);
3489         }
3490
3491         drawstring_aspect(pos, timer, mySize, timer_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3492 }
3493
3494 // Radar (#6)
3495 //
3496 void HUD_Radar(void)
3497 {
3498         if ((autocvar_hud_panel_radar == 0 || (autocvar_hud_panel_radar != 2 && !teamplay)) && !autocvar__hud_configure)
3499                 return;
3500
3501         active_panel = HUD_PANEL_RADAR;
3502         HUD_Panel_UpdateCvars(radar);
3503         vector pos, mySize;
3504         pos = panel_pos;
3505         mySize = panel_size;
3506
3507         HUD_Panel_DrawBg(1);
3508         if(panel_bg_padding)
3509         {
3510                 pos += '1 1 0' * panel_bg_padding;
3511                 mySize -= '2 2 0' * panel_bg_padding;
3512         }
3513
3514         local float color1, color2; // color already declared as a global in hud.qc
3515         local vector rgb;
3516         local entity tm;
3517         float scale2d, normalsize, bigsize;
3518         float f;
3519
3520         teamradar_origin2d = pos + 0.5 * mySize;
3521         teamradar_size2d = mySize;
3522
3523         if(minimapname == "")
3524                 return;
3525
3526         teamradar_loadcvars();
3527
3528         switch(hud_panel_radar_zoommode)
3529         {
3530                 default:
3531                 case 0:
3532                         f = current_zoomfraction;
3533                         break;
3534                 case 1:
3535                         f = 1 - current_zoomfraction;
3536                         break;
3537                 case 2:
3538                         f = 0;
3539                         break;
3540                 case 3:
3541                         f = 1;
3542                         break;
3543         }
3544
3545         switch(hud_panel_radar_rotation)
3546         {
3547                 case 0:
3548                         teamradar_angle = view_angles_y - 90;
3549                         break;
3550                 default:
3551                         teamradar_angle = 90 * hud_panel_radar_rotation;
3552                         break;
3553         }
3554
3555         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
3556         teamradar_size2d = mySize;
3557
3558         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
3559
3560         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
3561         if(hud_panel_radar_rotation == 0)
3562         {
3563                 // max-min distance must fit the radar in any rotation
3564                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_scale));
3565         }
3566         else
3567         {
3568                 vector c0, c1, c2, c3, span;
3569                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
3570                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
3571                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
3572                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
3573                 span = '0 0 0';
3574                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
3575                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
3576
3577                 // max-min distance must fit the radar in x=x, y=y
3578                 bigsize = min(
3579                         teamradar_size2d_x * scale2d / (1.05 * span_x),
3580                         teamradar_size2d_y * scale2d / (1.05 * span_y)
3581                 );
3582         }
3583
3584         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
3585         if(bigsize > normalsize)
3586                 normalsize = bigsize;
3587
3588         teamradar_size =
3589                   f * bigsize
3590                 + (1 - f) * normalsize;
3591         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
3592                   f * mi_center
3593                 + (1 - f) * view_origin);
3594
3595         color1 = GetPlayerColor(player_localentnum-1);
3596         rgb = GetTeamRGB(color1);
3597
3598         drawsetcliparea(
3599                 pos_x,
3600                 pos_y,
3601                 mySize_x,
3602                 mySize_y
3603         );
3604
3605         draw_teamradar_background(hud_panel_radar_foreground_alpha);
3606
3607         for(tm = world; (tm = find(tm, classname, "radarlink")); )
3608                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
3609         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
3610                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, panel_fg_alpha);
3611         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
3612         {
3613                 color2 = GetPlayerColor(tm.sv_entnum);
3614                 //if(color == COLOR_SPECTATOR || color == color2)
3615                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
3616         }
3617         draw_teamradar_player(view_origin, view_angles, '1 1 1');
3618
3619         drawresetcliparea();
3620 };
3621
3622 // Score (#7)
3623 //
3624 void HUD_Score(void)
3625 {
3626         if(!autocvar_hud_panel_score && !autocvar__hud_configure)
3627                 return;
3628
3629         active_panel = HUD_PANEL_SCORE;
3630         HUD_Panel_UpdateCvars(score);
3631         vector pos, mySize;
3632         pos = panel_pos;
3633         mySize = panel_size;
3634
3635         HUD_Panel_DrawBg(1);
3636         if(panel_bg_padding)
3637         {
3638                 pos += '1 1 0' * panel_bg_padding;
3639                 mySize -= '2 2 0' * panel_bg_padding;
3640         }
3641
3642         float score, distribution, leader;
3643         string sign;
3644         vector distribution_color;
3645         entity tm, pl, me;
3646         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
3647
3648         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
3649                 string timer, distrtimer;
3650
3651                 pl = players.sort_next;
3652                 if(pl == me)
3653                         pl = pl.sort_next;
3654                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
3655                         if(pl.scores[ps_primary] == 0)
3656                                 pl = world;
3657
3658                 score = me.(scores[ps_primary]);
3659                 timer = TIME_ENCODED_TOSTRING(score);
3660
3661                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
3662                         // distribution display
3663                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3664
3665                         distrtimer = ftos_decimals(fabs(distribution/pow(10, TIME_DECIMALS)), TIME_DECIMALS);
3666
3667                         if (distribution <= 0) {
3668                                 distribution_color = '0 1 0';
3669                                 sign = "-";
3670                         }
3671                         else {
3672                                 distribution_color = '1 0 0';
3673                                 sign = "+";
3674                         }
3675                         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);
3676                 }
3677                 // race record display
3678                 if (distribution <= 0)
3679                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3680                 drawstring_aspect(pos, timer, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3681         } else if (!teamplay) { // non-teamgames
3682                 // me vector := [team/connected frags id]
3683                 pl = players.sort_next;
3684                 if(pl == me)
3685                         pl = pl.sort_next;
3686
3687                 if(autocvar__hud_configure)
3688                         distribution = 42;
3689                 else if(pl)
3690                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3691                 else
3692                         distribution = 0;
3693
3694                 score = me.(scores[ps_primary]);
3695                 if(autocvar__hud_configure)
3696                         score = 123;
3697
3698                 if(distribution >= 5) {
3699                         distribution_color = eY;
3700                         leader = 1;
3701                 } else if(distribution >= 0) {
3702                         distribution_color = '1 1 1';
3703                         leader = 1;
3704                 } else if(distribution >= -5)
3705                         distribution_color = '1 1 0';
3706                 else
3707                         distribution_color = eX;
3708
3709                 drawstring_aspect(pos + eX * 0.75 * mySize_x, ftos(distribution), eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3710                 if (leader)
3711                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3712                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3713         } else { // teamgames
3714                 float max_fragcount;
3715                 max_fragcount = -99;
3716
3717                 float teamnum;
3718                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
3719                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !autocvar__hud_configure)) // no players? don't display
3720                                 continue;
3721                         score = tm.(teamscores[ts_primary]);
3722                         if(autocvar__hud_configure)
3723                                 score = 123;
3724                         leader = 0;
3725                         
3726                         if (score > max_fragcount)
3727                                 max_fragcount = score;
3728
3729                         if(tm.team == myteam) {
3730                                 if (max_fragcount == score)
3731                                         leader = 1;
3732                                 if (leader)
3733                                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3734                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
3735                         } else {
3736                                 if (max_fragcount == score)
3737                                         leader = 1;
3738                                 if (leader)
3739                                         HUD_Panel_DrawHighlight(pos + eX * 0.75 * mySize_x + eY * (1/3) * teamnum * mySize_y, eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3740                                 drawstring_aspect(pos + eX * 0.75 * mySize_x + eY * (1/3) * teamnum * mySize_y, ftos(score), eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
3741                                 teamnum += 1;
3742                         }
3743                 }
3744         }
3745 }
3746
3747 // Race timer (#8)
3748 //
3749 void HUD_RaceTimer (void) {
3750         if(!autocvar_hud_panel_racetimer && !(gametype == GAME_RACE || gametype == GAME_CTS) && !autocvar__hud_configure)
3751                 return;
3752
3753         active_panel = HUD_PANEL_RACETIMER;
3754         HUD_Panel_UpdateCvars(racetimer);
3755         vector pos, mySize;
3756         pos = panel_pos;
3757         mySize = panel_size;
3758
3759         HUD_Panel_DrawBg(1);
3760         if(panel_bg_padding)
3761         {
3762                 pos += '1 1 0' * panel_bg_padding;
3763                 mySize -= '2 2 0' * panel_bg_padding;
3764         }
3765
3766         // always force 4:1 aspect
3767         vector newSize;
3768         if(mySize_x/mySize_y > 4)
3769         {
3770                 newSize_x = 4 * mySize_y;
3771                 newSize_y = mySize_y;
3772
3773                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3774         }
3775         else
3776         {
3777                 newSize_y = 1/4 * mySize_x;
3778                 newSize_x = mySize_x;
3779
3780                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3781         }
3782         mySize = newSize;
3783
3784         float a, t;
3785         string s, forcetime;
3786
3787         if(autocvar__hud_configure)
3788         {
3789                 s = "0:13:37";
3790                 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);
3791                 s = _("^1Intermediate 1 (+15.42)");
3792                 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);
3793                 s = sprintf(_("^1PENALTY: %.1f (%s)"), 2, "missing a checkpoint");
3794                 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);
3795         }
3796         else if(race_checkpointtime)
3797         {
3798                 a = bound(0, 2 - (time - race_checkpointtime), 1);
3799                 s = "";
3800                 forcetime = "";
3801                 if(a > 0) // just hit a checkpoint?
3802                 {
3803                         if(race_checkpoint != 254)
3804                         {
3805                                 if(race_time && race_previousbesttime)
3806                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
3807                                 else
3808                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
3809                                 if(race_time)
3810                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
3811                         }
3812                 }
3813                 else
3814                 {
3815                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
3816                         {
3817                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
3818                                 if(a > 0) // next one?
3819                                 {
3820                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
3821                                 }
3822                         }
3823                 }
3824
3825                 if(s != "" && a > 0)
3826                 {
3827                         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);
3828                 }
3829
3830                 if(race_penaltytime)
3831                 {
3832                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
3833                         if(a > 0)
3834                         {
3835                                 s = sprintf(_("^1PENALTY: %.1f (%s)"), race_penaltytime * 0.1, race_penaltyreason);
3836                                 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);
3837                         }
3838                 }
3839
3840                 if(forcetime != "")
3841                 {
3842                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
3843                         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);
3844                 }
3845                 else
3846                         a = 1;
3847
3848                 if(race_laptime && race_checkpoint != 255)
3849                 {
3850                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
3851                         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);
3852                 }
3853         }
3854         else
3855         {
3856                 if(race_mycheckpointtime)
3857                 {
3858                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
3859                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
3860                         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);
3861                 }
3862                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
3863                 {
3864                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
3865                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
3866                         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);
3867                 }
3868
3869                 if(race_penaltytime && !race_penaltyaccumulator)
3870                 {
3871                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
3872                         a = bound(0, (1 + t - time), 1);
3873                         if(a > 0)
3874                         {
3875                                 if(time < t)
3876                                         s = sprintf(_("^1PENALTY: %.1f (%s)"), (t - time) * 0.1, race_penaltyreason);
3877                                 else
3878                                         s = sprintf(_("^2PENALTY: %.1f (%s)"), 0, race_penaltyreason);
3879                                 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);
3880                         }
3881                 }
3882         }
3883 }
3884
3885 // Vote window (#9)
3886 //
3887 float vote_yescount;
3888 float vote_nocount;
3889 float vote_needed;
3890 float vote_highlighted; // currently selected vote
3891
3892 float vote_active; // is there an active vote?
3893 float vote_prev; // previous state of vote_active to check for a change
3894 float vote_alpha;
3895 float vote_change; // "time" when vote_active changed
3896
3897 void HUD_VoteWindow(void) 
3898 {
3899     uid2name_dialog = 0;
3900         if(autocvar_cl_allow_uid2name == -1 && (gametype == GAME_CTS || gametype == GAME_RACE))
3901         {
3902                 vote_active = 1;
3903                 vote_called_vote = strzone(_("^2Name ^7instead of \"^1Unregistered player^7\" in stats"));
3904         uid2name_dialog = 1;
3905         }
3906
3907         if(!autocvar_hud_panel_vote && !autocvar__hud_configure)
3908                 return;
3909
3910         if(!autocvar__hud_configure)
3911         {
3912                 panel_fg_alpha = autocvar_hud_panel_fg_alpha;
3913                 panel_bg_alpha_str = autocvar_hud_panel_vote_bg_alpha;
3914
3915                 if(panel_bg_alpha_str == "") {
3916                         panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);
3917                 }
3918                 panel_bg_alpha = stof(panel_bg_alpha_str);
3919         }
3920
3921         string s;
3922         float a;
3923         if(vote_active != vote_prev) {
3924                 vote_change = time;
3925                 vote_prev = vote_active;
3926         }
3927
3928         if(vote_active || autocvar__hud_configure)
3929                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
3930         else
3931                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
3932
3933         if(autocvar__hud_configure)
3934         {
3935                 vote_yescount = 3;
3936                 vote_nocount = 2;
3937                 vote_needed = 4;
3938         }
3939
3940         if(!vote_alpha)
3941                 return;
3942
3943         active_panel = HUD_PANEL_VOTE;
3944         HUD_Panel_UpdateCvars(vote);
3945
3946         if(uid2name_dialog)
3947         {
3948                 panel_pos = eX * 0.3 * vid_conwidth + eY * 0.1 * vid_conheight;
3949                 panel_size = eX * 0.4 * vid_conwidth + eY * 0.3 * vid_conheight;
3950         }
3951
3952     // these must be below above block
3953         vector pos, mySize;
3954         pos = panel_pos;
3955         mySize = panel_size;
3956
3957         a = vote_alpha * bound(autocvar_hud_panel_vote_alreadyvoted_alpha, 1 - vote_highlighted, 1);
3958         HUD_Panel_DrawBg(a);
3959         a = panel_fg_alpha * a;
3960
3961         if(panel_bg_padding)
3962         {
3963                 pos += '1 1 0' * panel_bg_padding;
3964                 mySize -= '2 2 0' * panel_bg_padding;
3965         }
3966
3967         // always force 3:1 aspect
3968         vector newSize;
3969         if(mySize_x/mySize_y > 3)
3970         {
3971                 newSize_x = 3 * mySize_y;
3972                 newSize_y = mySize_y;
3973
3974                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3975         }
3976         else
3977         {
3978                 newSize_y = 1/3 * mySize_x;
3979                 newSize_x = mySize_x;
3980
3981                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3982         }
3983         mySize = newSize;
3984
3985         s = _("A vote has been called for:");
3986         if(uid2name_dialog)
3987                 s = _("Allow servers to store and display your name?");
3988         drawstring_aspect(pos, s, eX * mySize_x + eY * (2/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3989         s = textShortenToWidth(vote_called_vote, mySize_x, '1 1 0' * mySize_y * (1/8), stringwidth_colors);
3990         if(autocvar__hud_configure)
3991                 s = _("^1Configure the HUD");
3992         drawcolorcodedstring_aspect(pos + eY * (2/8) * mySize_y, s, eX * mySize_x + eY * (1.75/8) * mySize_y, a, DRAWFLAG_NORMAL);
3993
3994         // print the yes/no counts
3995     s = sprintf(_("Yes (%s): %d"), getcommandkey("vyes", "vyes"), vote_yescount);
3996         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);
3997     s = sprintf(_("No (%s): %d"), getcommandkey("vno", "vno"), vote_nocount);
3998         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);
3999
4000         // draw the progress bar backgrounds
4001         drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_back", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
4002
4003         // draw the highlights
4004         if(vote_highlighted == 1) {
4005                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
4006                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
4007         }
4008         else if(vote_highlighted == 2) {
4009                 drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
4010                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
4011         }
4012
4013         // draw the progress bars
4014         if(vote_yescount && vote_needed)
4015         {
4016                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
4017                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
4018         }
4019
4020         if(vote_nocount && vote_needed)
4021         {
4022                 drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
4023                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
4024         }
4025
4026         drawresetcliparea();
4027
4028         if(!vote_active) {
4029                 vote_highlighted = 0;
4030         }
4031 }
4032
4033 // Mod icons panel (#10)
4034 //
4035
4036 float mod_active; // is there any active mod icon?
4037
4038 // Clan Arena HUD modicons
4039 void HUD_Mod_CA(vector pos, vector mySize)
4040 {
4041         mod_active = 1; // CA should never hide the mod icons panel
4042         float redalive, bluealive;
4043         redalive = getstati(STAT_REDALIVE);
4044         bluealive = getstati(STAT_BLUEALIVE);
4045
4046         vector redpos, bluepos;
4047         if(mySize_x > mySize_y)
4048         {
4049                 redpos = pos;
4050                 bluepos = pos + eY * 0.5 * mySize_y;
4051                 drawpic_aspect_skin(redpos, "player_red.tga", 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4052                 drawstring_aspect(redpos + eX * 0.5 * mySize_x, ftos(redalive), 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4053                 drawpic_aspect_skin(bluepos, "player_blue.tga", 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4054                 drawstring_aspect(bluepos + eX * 0.5 * mySize_x, ftos(bluealive), 0.5 * mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4055         }
4056         else
4057         {
4058                 redpos = pos;
4059                 bluepos = pos + eY * 0.5 * mySize_y;
4060                 drawpic_aspect_skin(redpos, "player_red.tga", eX * mySize_x + eY * 0.3 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4061                 drawstring_aspect(redpos + eY * 0.3 * mySize_y, ftos(redalive), eX * mySize_x + eY * 0.2 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4062                 drawpic_aspect_skin(bluepos, "player_blue.tga", eX * mySize_x + eY * 0.3 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4063                 drawstring_aspect(bluepos + eY * 0.3 * mySize_y, ftos(bluealive), eX * mySize_x + eY * 0.2 * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4064         }
4065 }
4066
4067 // CTF HUD modicon section
4068 float redflag_prevframe, blueflag_prevframe; // status during previous frame
4069 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
4070 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
4071
4072 void HUD_Mod_CTF_Reset(void)
4073 {
4074         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
4075 }
4076
4077 void HUD_Mod_CTF(vector pos, vector mySize)
4078 {
4079         vector redflag_pos, blueflag_pos;
4080         vector flag_size;
4081         float f; // every function should have that
4082
4083         float redflag, blueflag; // current status
4084         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
4085         float stat_items;
4086
4087         stat_items = getstati(STAT_ITEMS);
4088         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
4089         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
4090         
4091         if(redflag || blueflag)
4092                 mod_active = 1;
4093         else
4094                 mod_active = 0;
4095
4096         if(autocvar__hud_configure)
4097         {
4098                 redflag = 1;
4099                 blueflag = 2;
4100         }
4101
4102         // when status CHANGES, set old status into prevstatus and current status into status
4103         if (redflag != redflag_prevframe)
4104         {
4105                 redflag_statuschange_time = time;
4106                 redflag_prevstatus = redflag_prevframe;
4107                 redflag_prevframe = redflag;
4108         }
4109
4110         if (blueflag != blueflag_prevframe)
4111         {
4112                 blueflag_statuschange_time = time;
4113                 blueflag_prevstatus = blueflag_prevframe;
4114                 blueflag_prevframe = blueflag;
4115         }
4116
4117         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
4118         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
4119
4120         float BLINK_FACTOR = 0.15;
4121         float BLINK_BASE = 0.85;
4122         // note:
4123         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
4124         // thus
4125         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
4126         // ensure RMS == 1
4127         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
4128
4129         string red_icon, red_icon_prevstatus;
4130         float red_alpha, red_alpha_prevstatus;
4131         red_alpha = red_alpha_prevstatus = 1;
4132         switch(redflag) {
4133                 case 1: red_icon = "flag_red_taken"; break;
4134                 case 2: red_icon = "flag_red_lost"; break;
4135                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4136                 default:
4137                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
4138                                 red_icon = "flag_red_shielded";
4139                         else
4140                                 red_icon = string_null;
4141                         break;
4142         }
4143         switch(redflag_prevstatus) {
4144                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
4145                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
4146                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4147                 default:
4148                         if(redflag == 3)
4149                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
4150                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
4151                                 red_icon_prevstatus = "flag_red_shielded";
4152                         else
4153                                 red_icon_prevstatus = string_null;
4154                         break;
4155         }
4156
4157         string blue_icon, blue_icon_prevstatus;
4158         float blue_alpha, blue_alpha_prevstatus;
4159         blue_alpha = blue_alpha_prevstatus = 1;
4160         switch(blueflag) {
4161                 case 1: blue_icon = "flag_blue_taken"; break;
4162                 case 2: blue_icon = "flag_blue_lost"; break;
4163                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4164                 default:
4165                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
4166                                 blue_icon = "flag_blue_shielded";
4167                         else
4168                                 blue_icon = string_null;
4169                         break;
4170         }
4171         switch(blueflag_prevstatus) {
4172                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
4173                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
4174                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
4175                 default:
4176                         if(blueflag == 3)
4177                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
4178                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
4179                                 blue_icon_prevstatus = "flag_blue_shielded";
4180                         else
4181                                 blue_icon_prevstatus = string_null;
4182                         break;
4183         }
4184
4185         if(mySize_x > mySize_y) {
4186                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
4187                         redflag_pos = pos;
4188                         blueflag_pos = pos + eX * 0.5 * mySize_x;
4189                 } else {
4190                         blueflag_pos = pos;
4191                         redflag_pos = pos + eX * 0.5 * mySize_x;
4192                 }
4193                 flag_size = eX * 0.5 * mySize_x + eY * mySize_y;
4194         } else {
4195                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
4196                         redflag_pos = pos;
4197                         blueflag_pos = pos + eY * 0.5 * mySize_y;
4198                 } else {
4199                         blueflag_pos = pos;
4200                         redflag_pos = pos + eY * 0.5 * mySize_y;
4201                 }
4202                 flag_size = eY * 0.5 * mySize_y + eX * mySize_x;
4203         }
4204
4205         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
4206         if(red_icon_prevstatus && f < 1)
4207                 drawpic_aspect_skin_expanding(redflag_pos, red_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
4208         if(red_icon)
4209                 drawpic_aspect_skin(redflag_pos, red_icon, flag_size, '1 1 1', panel_fg_alpha * red_alpha * f, DRAWFLAG_NORMAL);
4210
4211         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
4212         if(blue_icon_prevstatus && f < 1)
4213                 drawpic_aspect_skin_expanding(blueflag_pos, blue_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
4214         if(blue_icon)
4215                 drawpic_aspect_skin(blueflag_pos, blue_icon, flag_size, '1 1 1', panel_fg_alpha * blue_alpha * f, DRAWFLAG_NORMAL);
4216 }
4217
4218 // Keyhunt HUD modicon section
4219 float kh_runheretime;
4220
4221 void HUD_Mod_KH_Reset(void)
4222 {
4223         kh_runheretime = 0;
4224 }
4225
4226 void HUD_Mod_KH(vector pos, vector mySize)
4227 {
4228         mod_active = 1; // keyhunt should never hide the mod icons panel
4229         float kh_keys;
4230         float keyteam;
4231         float a, aa;
4232         vector p, pa, kh_size, kh_asize;
4233
4234         kh_keys = getstati(STAT_KH_KEYS);
4235
4236         p_x = pos_x;
4237         if(mySize_x > mySize_y)
4238         {
4239                 p_y = pos_y + 0.25 * mySize_y;
4240                 pa = p - eY * 0.25 * mySize_y;
4241
4242                 kh_size_x = mySize_x * 0.25;
4243                 kh_size_y = 0.75 * mySize_y;
4244                 kh_asize_x = mySize_x * 0.25;
4245                 kh_asize_y = mySize_y * 0.25;
4246         }
4247         else
4248         {
4249                 p_y = pos_y + 0.125 * mySize_y;
4250                 pa = p - eY * 0.125 * mySize_y;
4251
4252                 kh_size_x = mySize_x * 0.5;
4253                 kh_size_y = 0.375 * mySize_y;
4254                 kh_asize_x = mySize_x * 0.5;
4255                 kh_asize_y = mySize_y * 0.125;
4256         }
4257
4258         float i, key;
4259
4260         float keycount;
4261         keycount = 0;
4262         for(i = 0; i < 4; ++i)
4263         {
4264                 key = floor(kh_keys / pow(32, i)) & 31;
4265                 keyteam = key - 1;
4266                 if(keyteam == 30 && keycount <= 4)
4267                         keycount += 4;
4268                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
4269                         keycount += 1;
4270         }
4271
4272         // this yields 8 exactly if "RUN HERE" shows
4273
4274         if(keycount == 8)
4275         {
4276                 if(!kh_runheretime)
4277                         kh_runheretime = time;
4278                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
4279         }
4280         else
4281                 kh_runheretime = 0;
4282
4283         for(i = 0; i < 4; ++i)
4284         {
4285                 key = floor(kh_keys / pow(32, i)) & 31;
4286                 keyteam = key - 1;
4287                 switch(keyteam)
4288                 {
4289                         case 30: // my key
4290                                 keyteam = myteam;
4291                                 a = 1;
4292                                 aa = 1;
4293                                 break;
4294                         case -1: // no key
4295                                 a = 0;
4296                                 aa = 0;
4297                                 break;
4298                         default: // owned or dropped
4299                                 a = 0.2;
4300                                 aa = 0.5;
4301                                 break;
4302                 }
4303                 a = a * panel_fg_alpha;
4304                 aa = aa * panel_fg_alpha;
4305                 if(a > 0)
4306                 {
4307                         switch(keyteam)
4308                         {
4309                                 case COLOR_TEAM1:
4310                                         drawpic_aspect_skin(pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4311                                         break;
4312                                 case COLOR_TEAM2:
4313                                         drawpic_aspect_skin(pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4314                                         break;
4315                                 case COLOR_TEAM3:
4316                                         drawpic_aspect_skin(pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4317                                         break;
4318                                 case COLOR_TEAM4:
4319                                         drawpic_aspect_skin(pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4320                                         break;
4321                                 default:
4322                                         break;
4323                         }
4324                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
4325                         {
4326                                 case 0:
4327                                         drawpic_aspect_skin(p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4328                                         break;
4329                                 case 1:
4330                                         drawpic_aspect_skin(p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4331                                         break;
4332                                 case 2:
4333                                         drawpic_aspect_skin(p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4334                                         break;
4335                                 case 3:
4336                                         drawpic_aspect_skin(p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4337                                         break;
4338                         }
4339                 }
4340                 if(mySize_x > mySize_y)
4341                 {
4342                         p_x += 0.25 * mySize_x;
4343                         pa_x += 0.25 * mySize_x;
4344                 }
4345                 else
4346                 {
4347                         if(i == 1)
4348                         {
4349                                 p_y = pos_y + 0.625 * mySize_y;
4350                                 pa_y = pos_y + 0.5 * mySize_y;
4351                                 p_x = pos_x;
4352                                 pa_x = pos_x;
4353                         }
4354                         else
4355                         {
4356                                 p_x += 0.5 * mySize_x;
4357                                 pa_x += 0.5 * mySize_x;
4358                         }
4359                 }
4360         }
4361 }
4362
4363 // Keepaway HUD mod icon
4364 float kaball_prevstatus; // last remembered status
4365 float kaball_statuschange_time; // time when the status changed
4366
4367 // we don't need to reset for keepaway since it immediately 
4368 // autocorrects prevstatus as to if the player has the ball or not
4369
4370 void HUD_Mod_Keepaway(vector pos, vector mySize)
4371 {
4372         mod_active = 1; // keepaway should always show the mod HUD
4373         
4374         float BLINK_FACTOR = 0.15;
4375         float BLINK_BASE = 0.85;
4376         float BLINK_FREQ = 5; 
4377         float kaball_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
4378         
4379         float stat_items = getstati(STAT_ITEMS);
4380         float kaball = (stat_items/IT_KEY1) & 1;
4381         
4382         if(kaball != kaball_prevstatus)
4383         {
4384                 kaball_statuschange_time = time;
4385                 kaball_prevstatus = kaball;
4386         }
4387         
4388         vector kaball_pos, kaball_size;
4389         
4390         if(mySize_x > mySize_y) {
4391                 kaball_pos = pos + eX * 0.25 * mySize_x;
4392                 kaball_size = eX * 0.5 * mySize_x + eY * mySize_y;
4393         } else {
4394                 kaball_pos = pos + eY * 0.25 * mySize_y;
4395                 kaball_size = eY * 0.5 * mySize_y + eX * mySize_x;
4396         }
4397         
4398         float kaball_statuschange_elapsedtime = time - kaball_statuschange_time;
4399         float f = bound(0, kaball_statuschange_elapsedtime*2, 1);
4400         
4401         if(kaball_prevstatus && f < 1)
4402                 drawpic_aspect_skin_expanding(kaball_pos, "keepawayball_carrying", kaball_size, '1 1 1', panel_fg_alpha * kaball_alpha, DRAWFLAG_NORMAL, f);
4403         
4404         if(kaball)
4405                 drawpic_aspect_skin(pos, "keepawayball_carrying", eX * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha * kaball_alpha * f, DRAWFLAG_NORMAL);
4406 }
4407
4408
4409 // Nexball HUD mod icon
4410 void HUD_Mod_NexBall(vector pos, vector mySize)
4411 {
4412         float stat_items, nb_pb_starttime, dt, p;
4413
4414         stat_items = getstati(STAT_ITEMS);
4415         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
4416
4417         if (stat_items & IT_KEY1)
4418                 mod_active = 1;
4419         else
4420                 mod_active = 0;
4421
4422         //Manage the progress bar if any
4423         if (nb_pb_starttime > 0)
4424         {
4425                 dt = mod(time - nb_pb_starttime, nb_pb_period);
4426                 // one period of positive triangle
4427                 p = 2 * dt / nb_pb_period;
4428                 if (p > 1)
4429                         p = 2 - p;
4430
4431                 //Draw the filling
4432                 float vertical;
4433                 if(mySize_x > mySize_y)
4434                 {
4435                         vertical = 0;
4436                 }
4437                 else
4438                 {
4439                         vertical = 1;
4440                 }
4441                 HUD_Panel_GetProgressBarColor(nexball);
4442         HUD_Panel_DrawProgressBar(pos, mySize, "statusbar", vertical, 0, p, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4443         }
4444
4445         if (stat_items & IT_KEY1)
4446                 drawpic_aspect_skin(pos, "nexball_carrying", eX * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4447 }
4448
4449 // Race/CTS HUD mod icons
4450 float crecordtime_prev; // last remembered crecordtime
4451 float crecordtime_change_time; // time when crecordtime last changed
4452 float srecordtime_prev; // last remembered srecordtime
4453 float srecordtime_change_time; // time when srecordtime last changed
4454
4455 float race_status_time;
4456 float race_status_prev;
4457 string race_status_name_prev;
4458 void HUD_Mod_Race(vector pos, vector mySize)
4459 {
4460         mod_active = 1; // race should never hide the mod icons panel
4461         entity me;
4462         me = playerslots[player_localentnum - 1];
4463         float t, score;
4464         float f; // yet another function has this
4465         score = me.(scores[ps_primary]);
4466
4467         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
4468                 return; // no records in the actual race
4469
4470         // clientside personal record
4471         string rr;
4472         if(gametype == GAME_CTS)
4473                 rr = CTS_RECORD;
4474         else
4475                 rr = RACE_RECORD;
4476         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
4477
4478         if(score && (score < t || !t)) {
4479                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
4480                 if(autocvar_cl_autodemo_delete_keeprecords)
4481                 {
4482                         f = autocvar_cl_autodemo_delete;
4483                         f &~= 1;
4484                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
4485                 }
4486         }
4487
4488         if(t != crecordtime_prev) {
4489                 crecordtime_prev = t;
4490                 crecordtime_change_time = time;
4491         }
4492
4493         vector textPos, medalPos;
4494         float squareSize;
4495         if(mySize_x > mySize_y) {
4496                 // text on left side
4497                 squareSize = min(mySize_y, mySize_x/2);
4498                 textPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eY * 0.5 * (mySize_y - squareSize);
4499                 medalPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eX * 0.5 * mySize_x + eY * 0.5 * (mySize_y - squareSize);
4500         } else {
4501                 // text on top
4502                 squareSize = min(mySize_x, mySize_y/2);
4503                 textPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eX * 0.5 * (mySize_x - squareSize);
4504                 medalPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eY * 0.5 * mySize_y + eX * 0.5 * (mySize_x - squareSize);
4505         }
4506
4507         f = time - crecordtime_change_time;
4508
4509         if (f > 1) {
4510                 drawstring_aspect(textPos, _("Personal best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4511                 drawstring_aspect(textPos + eY * 0.25 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4512         } else {
4513                 drawstring_aspect(textPos, _("Personal best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4514                 drawstring_aspect(textPos + eY * 0.25 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4515                 drawstring_aspect_expanding(pos, _("Personal best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
4516                 drawstring_aspect_expanding(pos + eY * 0.25 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
4517         }
4518
4519         // server record
4520         t = race_server_record;
4521         if(t != srecordtime_prev) {
4522                 srecordtime_prev = t;
4523                 srecordtime_change_time = time;
4524         }
4525         f = time - srecordtime_change_time;
4526
4527         if (f > 1) {
4528                 drawstring_aspect(textPos + eY * 0.5 * squareSize, _("Server best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4529                 drawstring_aspect(textPos + eY * 0.75 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4530         } else {
4531                 drawstring_aspect(textPos + eY * 0.5 * squareSize, _("Server best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4532                 drawstring_aspect(textPos + eY * 0.75 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4533                 drawstring_aspect_expanding(textPos + eY * 0.5 * squareSize, _("Server best"), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
4534                 drawstring_aspect_expanding(textPos + eY * 0.75 * squareSize, TIME_ENCODED_TOSTRING(t), eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
4535         }
4536
4537         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
4538                 race_status_time = time + 5;
4539                 race_status_prev = race_status;
4540                 if (race_status_name_prev)
4541                         strunzone(race_status_name_prev);
4542                 race_status_name_prev = strzone(race_status_name);
4543         }
4544
4545         // race "awards"
4546         float a;
4547         a = bound(0, race_status_time - time, 1);
4548
4549         string s;
4550         s = textShortenToWidth(race_status_name, squareSize, '1 1 0' * 0.1 * squareSize, stringwidth_colors);
4551
4552         float rank;
4553         if(race_status > 0)
4554                 rank = race_CheckName(race_status_name);
4555         string rankname;
4556         rankname = race_PlaceName(rank);
4557
4558         vector namepos;
4559         namepos = medalPos + '0 0.8 0' * squareSize;
4560         vector rankpos;
4561         rankpos = medalPos + '0 0.15 0' * squareSize;
4562
4563         if(race_status == 0)
4564                 drawpic_aspect_skin(medalPos, "race_newfail", '1 1 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4565         else if(race_status == 1) {
4566                 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newtime", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4567                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4568                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4569         } else if(race_status == 2) {
4570                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
4571                         drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrankgreen", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4572                 else
4573                         drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrankyellow", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4574                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4575                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4576         } else if(race_status == 3) {
4577                 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrecordserver", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4578                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4579                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4580         }
4581
4582         if (race_status_time - time <= 0) {
4583                 race_status_prev = -1;
4584                 race_status = -1;
4585                 if(race_status_name)
4586                         strunzone(race_status_name);
4587                 race_status_name = string_null;
4588                 if(race_status_name_prev)
4589                         strunzone(race_status_name_prev);
4590                 race_status_name_prev = string_null;
4591         }
4592 }
4593
4594 float mod_prev; // previous state of mod_active to check for a change
4595 float mod_alpha;
4596 float mod_change; // "time" when mod_active changed
4597
4598 void HUD_ModIcons(void)
4599 {
4600         if(!autocvar_hud_panel_modicons && !autocvar__hud_configure)
4601                 return;
4602
4603         if (gametype != GAME_KEYHUNT && gametype != GAME_CTF && gametype != GAME_NEXBALL && gametype != GAME_CTS && gametype != GAME_RACE && gametype != GAME_CA && gametype != GAME_FREEZETAG && gametype != GAME_KEEPAWAY && !autocvar__hud_configure)
4604                 return;
4605
4606         active_panel = HUD_PANEL_MODICONS;
4607         HUD_Panel_UpdateCvars(modicons);
4608         vector pos, mySize;
4609         pos = panel_pos;
4610         mySize = panel_size;
4611
4612         if(mod_active != mod_prev) {
4613                 mod_change = time;
4614                 mod_prev = mod_active;
4615         }
4616
4617         if(mod_active || autocvar__hud_configure)
4618                 mod_alpha = bound(0, (time - mod_change) * 2, 1);
4619         else
4620                 mod_alpha = bound(0, 1 - (time - mod_change) * 2, 1);
4621
4622         if(mod_alpha)
4623                 HUD_Panel_DrawBg(mod_alpha);
4624
4625         if(panel_bg_padding)
4626         {
4627                 pos += '1 1 0' * panel_bg_padding;
4628                 mySize -= '2 2 0' * panel_bg_padding;
4629         }
4630
4631         // these MUST be ran in order to update mod_active
4632         if(gametype == GAME_KEYHUNT)
4633                 HUD_Mod_KH(pos, mySize);
4634         else if(gametype == GAME_CTF || autocvar__hud_configure)
4635                 HUD_Mod_CTF(pos, mySize); // forcealpha only needed for ctf icons, as only they are shown in config mode
4636         else if(gametype == GAME_NEXBALL)
4637                 HUD_Mod_NexBall(pos, mySize);
4638         else if(gametype == GAME_CTS || gametype == GAME_RACE)
4639                 HUD_Mod_Race(pos, mySize);
4640         else if(gametype == GAME_CA || gametype == GAME_FREEZETAG)
4641                 HUD_Mod_CA(pos, mySize);
4642         else if(gametype == GAME_KEEPAWAY)
4643                 HUD_Mod_Keepaway(pos, mySize);
4644 }
4645
4646 // Draw pressed keys (#11)
4647 //
4648 void HUD_DrawPressedKeys(void)
4649 {
4650         if(!autocvar_hud_panel_pressedkeys && !autocvar__hud_configure)
4651                 return;
4652
4653         if(!(spectatee_status > 0 || autocvar_hud_panel_pressedkeys >= 2 || autocvar__hud_configure))
4654                 return;
4655
4656         active_panel = HUD_PANEL_PRESSEDKEYS;
4657         HUD_Panel_UpdateCvars(pressedkeys);
4658         vector pos, mySize;
4659         pos = panel_pos;
4660         mySize = panel_size;
4661
4662         HUD_Panel_DrawBg(1);
4663         if(panel_bg_padding)
4664         {
4665                 pos += '1 1 0' * panel_bg_padding;
4666                 mySize -= '2 2 0' * panel_bg_padding;
4667         }
4668
4669         // force custom aspect
4670         float aspect = autocvar_hud_panel_pressedkeys_aspect;
4671         if(aspect)
4672         {
4673                 vector newSize;
4674                 if(mySize_x/mySize_y > aspect)
4675                 {
4676                         newSize_x = aspect * mySize_y;
4677                         newSize_y = mySize_y;
4678
4679                         pos_x = pos_x + (mySize_x - newSize_x) / 2;
4680                 }
4681                 else
4682                 {
4683                         newSize_y = 1/aspect * mySize_x;
4684                         newSize_x = mySize_x;
4685
4686                         pos_y = pos_y + (mySize_y - newSize_y) / 2;
4687                 }
4688                 mySize = newSize;
4689         }
4690
4691         vector keysize;
4692         keysize = eX * mySize_x * (1/3) + eY * mySize_y * 0.5;
4693         float pressedkeys;
4694
4695         pressedkeys = getstatf(STAT_PRESSED_KEYS);
4696         drawpic_aspect_skin(pos, ((pressedkeys & KEY_CROUCH) ? "key_crouch_inv.tga" : "key_crouch.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4697         drawpic_aspect_skin(pos + eX * mySize_x * (1/3), ((pressedkeys & KEY_FORWARD) ? "key_forward_inv.tga" : "key_forward.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4698         drawpic_aspect_skin(pos + eX * mySize_x * (2/3), ((pressedkeys & KEY_JUMP) ? "key_jump_inv.tga" : "key_jump.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4699         drawpic_aspect_skin(pos + eY * 0.5 * mySize_y, ((pressedkeys & KEY_LEFT) ? "key_left_inv.tga" : "key_left.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4700         drawpic_aspect_skin(pos + eY * 0.5 * mySize_y + eX * mySize_x * (1/3), ((pressedkeys & KEY_BACKWARD) ? "key_backward_inv.tga" : "key_backward.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4701         drawpic_aspect_skin(pos + eY * 0.5 * mySize_y + eX * mySize_x * (2/3), ((pressedkeys & KEY_RIGHT) ? "key_right_inv.tga" : "key_right.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4702 }
4703
4704 // Handle chat as a panel (#12)
4705 //
4706 void HUD_Chat(void)
4707 {
4708         if(!autocvar_hud_panel_chat && !autocvar__hud_configure)
4709         {
4710                 cvar_set("con_chatrect", "0");
4711                 return;
4712         }
4713
4714         active_panel = HUD_PANEL_CHAT;
4715         HUD_Panel_UpdateCvars(chat);
4716
4717         if(autocvar__con_chat_maximized && !autocvar__hud_configure) // draw at full screen height if maximized
4718         {
4719                 panel_pos_y = panel_bg_border;
4720                 panel_size_y = vid_conheight - panel_bg_border * 2;
4721                 if(panel_bg == "0") // force a border when maximized
4722                 {
4723                         if(precache_pic(panel_bg) == "") {
4724                                 panel_bg = strcat(hud_skin_path, "/border_default");
4725                                 if(precache_pic(panel_bg) == "") {
4726                                         panel_bg = "gfx/hud/default/border_default";
4727                                 }
4728                         }
4729                 }
4730                 panel_bg_alpha = max(0.75, panel_bg_alpha); // force an alpha of at least 0.75
4731         }
4732
4733         vector pos, mySize;
4734         pos = panel_pos;
4735         mySize = panel_size;
4736
4737         HUD_Panel_DrawBg(1);
4738
4739         if(panel_bg_padding)
4740         {
4741                 pos += '1 1 0' * panel_bg_padding;
4742                 mySize -= '2 2 0' * panel_bg_padding;
4743         }
4744
4745         cvar_set("con_chatrect", "1");
4746
4747         cvar_set("con_chatrect_x", ftos(pos_x/vid_conwidth));
4748         cvar_set("con_chatrect_y", ftos(pos_y/vid_conheight));
4749
4750         cvar_set("con_chatwidth", ftos(mySize_x/vid_conwidth));
4751         cvar_set("con_chat", ftos(floor(mySize_y/autocvar_con_chatsize - 0.5)));
4752
4753         if(autocvar__hud_configure)
4754         {
4755                 float chatsize;
4756                 chatsize = autocvar_con_chatsize;
4757                 cvar_set("con_chatrect_x", "9001"); // over 9000, we'll fake it instead for more control over alpha and such
4758                 float i, a;
4759                 for(i = 0; i < autocvar_con_chat; ++i)
4760                 {
4761                         if(i == autocvar_con_chat - 1)
4762                                 a = panel_fg_alpha;
4763                         else
4764                                 a = panel_fg_alpha * floor(((i + 1) * 7 + autocvar_con_chattime)/45);
4765                         drawcolorcodedstring(pos + eY * i * chatsize, textShortenToWidth(_("^3Player^7: This is the chat area."), mySize_x, '1 1 0' * chatsize, stringwidth_colors), '1 1 0' * chatsize, a, DRAWFLAG_NORMAL);
4766                 }
4767         }
4768 }
4769
4770 // Engine info panel (#13)
4771 //
4772 float prevfps;
4773 float prevfps_time;
4774 float framecounter;
4775
4776 float frametimeavg;
4777 float frametimeavg1; // 1 frame ago
4778 float frametimeavg2; // 2 frames ago
4779 void HUD_EngineInfo(void)
4780 {
4781         if(!autocvar_hud_panel_engineinfo && !autocvar__hud_configure)
4782                 return;
4783
4784         active_panel = HUD_PANEL_ENGINEINFO;
4785         HUD_Panel_UpdateCvars(engineinfo);
4786         vector pos, mySize;
4787         pos = panel_pos;
4788         mySize = panel_size;
4789
4790         HUD_Panel_DrawBg(1);
4791         if(panel_bg_padding)
4792         {
4793                 pos += '1 1 0' * panel_bg_padding;
4794                 mySize -= '2 2 0' * panel_bg_padding;
4795         }
4796
4797         float currentTime = gettime(GETTIME_REALTIME);
4798         if(cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage"))
4799         {
4800                 float currentframetime = currentTime - prevfps_time;
4801                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + currentframetime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
4802                 frametimeavg2 = frametimeavg1;
4803                 frametimeavg1 = frametimeavg;
4804                 
4805                 float weight;
4806                 weight = cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight");
4807                 if(currentframetime > 0.0001) // filter out insane values which sometimes seem to occur and throw off the average? If you are getting 10,000 fps or more, then you don't need a framerate counter.
4808                 {
4809                         if(fabs(prevfps - (1/frametimeavg)) > prevfps * cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage_instantupdate_change_threshold")) // if there was a big jump in fps, just force prevfps at current (1/currentframetime) to make big updates instant
4810                                 prevfps = (1/currentframetime);
4811                         prevfps = (1 - weight) * prevfps + weight * (1/frametimeavg); // framecounter just used so there's no need for a new variable, think of it as "frametime average"
4812                 }
4813                 prevfps_time = currentTime;
4814         }
4815         else
4816         {
4817                 framecounter += 1;
4818                 if(currentTime - prevfps_time > autocvar_hud_panel_engineinfo_framecounter_time)
4819                 {
4820                         prevfps = framecounter/(currentTime - prevfps_time);
4821                         framecounter = 0;
4822                         prevfps_time = currentTime;
4823                 }
4824         }
4825
4826         vector color;
4827         color = HUD_Get_Num_Color (prevfps, 100);
4828         drawstring_aspect(pos, sprintf(_("FPS: %.*f"), autocvar_hud_panel_engineinfo_framecounter_decimals, prevfps), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
4829 }
4830
4831 // Info messages panel (#14)
4832 //
4833 #define drawInfoMessage(s)\
4834         if(autocvar_hud_panel_infomessages_flip)\
4835                 o_x = pos_x + mySize_x - stringwidth(s, TRUE, fontsize);\
4836         drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);\
4837         o_y += fontsize_y;
4838 void HUD_InfoMessages(void)
4839 {
4840         if(!autocvar_hud_panel_infomessages && !autocvar__hud_configure)
4841                 return;
4842
4843         active_panel = HUD_PANEL_INFOMESSAGES;
4844         HUD_Panel_UpdateCvars(infomessages);
4845         vector pos, mySize;
4846         pos = panel_pos;
4847         mySize = panel_size;
4848
4849         HUD_Panel_DrawBg(1);
4850         if(panel_bg_padding)
4851         {
4852                 pos += '1 1 0' * panel_bg_padding;
4853                 mySize -= '2 2 0' * panel_bg_padding;
4854         }
4855
4856         // always force 5:1 aspect
4857         vector newSize;
4858         if(mySize_x/mySize_y > 5)
4859         {
4860                 newSize_x = 5 * mySize_y;
4861                 newSize_y = mySize_y;
4862
4863                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
4864         }
4865         else
4866         {
4867                 newSize_y = 1/5 * mySize_x;
4868                 newSize_x = mySize_x;
4869
4870                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
4871         }
4872
4873         mySize = newSize;
4874         entity tm;
4875         vector o;
4876         o = pos;
4877
4878         vector fontsize;
4879         fontsize = '0.20 0.20 0' * mySize_y;
4880         
4881         float a;
4882         if(spectatee_status != 0)
4883                 a = 1;
4884         else
4885                 a = panel_fg_alpha;
4886
4887         string s;
4888         if(!autocvar__hud_configure)
4889         {
4890                 if(spectatee_status && !intermission)
4891                 {
4892                         if(spectatee_status == -1)
4893                                 s = _("^1Observing");
4894                         else
4895                                 s = sprintf(_("^1Spectating: ^7%s"), GetPlayerName(spectatee_status - 1));
4896                         drawInfoMessage(s)
4897
4898                         if(spectatee_status == -1)
4899                                 s = sprintf(_("^1Press ^3%s^1 to spectate"), getcommandkey("primary fire", "+fire"));
4900                         else
4901                                 s = sprintf(_("^1Press ^3%s^1 for another player"), getcommandkey("primary fire", "+fire"));
4902                         drawInfoMessage(s)
4903
4904                         if(spectatee_status == -1)
4905                                 s = sprintf(_("^1Use ^3%s^1 or ^3%s^1 to change the speed"), getcommandkey("next weapon", "weapnext"), getcommandkey("previous weapon", "weapprev"));
4906                         else
4907                                 s = sprintf(_("^1Press ^3%s^1 to observe"), getcommandkey("secondary fire", "+fire2"));
4908                         drawInfoMessage(s)
4909
4910                         s = sprintf(_("^1Press ^3%s^1 for gamemode info"), getcommandkey("server info", "+show_info"));
4911                         drawInfoMessage(s)
4912
4913                         if(gametype == GAME_ARENA)
4914                                 s = _("^1Wait for your turn to join");
4915                         else if(gametype == GAME_LMS)
4916                         {
4917                                 entity sk;
4918                                 sk = playerslots[player_localentnum - 1];
4919                                 if(sk.(scores[ps_primary]) >= 666)
4920                                         s = _("^1Match has already begun");
4921                                 else if(sk.(scores[ps_primary]) > 0)
4922                                         s = _("^1You have no more lives left");
4923                                 else
4924                                         s = sprintf(_("^1Press ^3%s^1 to join"), getcommandkey("jump", "+jump"));
4925                         }
4926                         else
4927                                 s = sprintf(_("^1Press ^3%s^1 to join"), getcommandkey("jump", "+jump"));
4928                         drawInfoMessage(s)
4929
4930                         //show restart countdown:
4931                         if (time < getstatf(STAT_GAMESTARTTIME)) {
4932                                 float countdown;
4933                                 //we need to ceil, otherwise the countdown would be off by .5 when using round()
4934                                 countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
4935                                 s = sprintf(_("^1Game starts in ^3%d^1 seconds"), countdown);
4936                                 drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);
4937                                 o_y += fontsize_y;
4938                         }
4939                 }
4940                 if(warmup_stage && !intermission)
4941                 {
4942                         s = _("^2Currently in ^1warmup^2 stage!");
4943                         drawInfoMessage(s)
4944                 }
4945
4946                 string blinkcolor;
4947                 if(mod(time, 1) >= 0.5)
4948                         blinkcolor = "^1";
4949                 else
4950                         blinkcolor = "^3";
4951
4952                 if(ready_waiting && !intermission && !spectatee_status)
4953                 {
4954                         if(ready_waiting_for_me)
4955                         {
4956                                 if(warmup_stage)
4957                                         s = sprintf(_("%sPress ^3%s%s to end warmup"), blinkcolor, getcommandkey("ready", "ready"), blinkcolor);
4958                                 else
4959                                         s = sprintf(_("%sPress ^3%s%s once you are ready"), blinkcolor, getcommandkey("ready", "ready"), blinkcolor);
4960                         }
4961                         else
4962                         {
4963                                 if(warmup_stage)
4964                                         s = _("^2Waiting for others to ready up to end warmup...");
4965                                 else
4966                                         s = _("^2Waiting for others to ready up...");
4967                         }
4968                         drawInfoMessage(s)
4969                 }
4970                 else if(warmup_stage && !intermission && !spectatee_status)
4971                 {
4972                         s = sprintf(_("^2Press ^3%s^2 to end warmup"), getcommandkey("ready", "ready"));
4973                         drawInfoMessage(s)
4974                 }
4975
4976                 if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
4977                 {
4978                         float ts_min, ts_max;
4979                         tm = teams.sort_next;
4980                         if (tm)
4981                         {
4982                                 for(; tm.sort_next; tm = tm.sort_next)
4983                                 {
4984                                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
4985                                                 continue;
4986                                         if(!ts_min) ts_min = tm.team_size;
4987                                         else ts_min = min(ts_min, tm.team_size);
4988                                         if(!ts_max) ts_max = tm.team_size;
4989                                         else ts_max = max(ts_max, tm.team_size);
4990                                 }
4991                                 if ((ts_max - ts_min) > 1)
4992                                 {
4993                                         s = strcat(blinkcolor, _("Teamnumbers are unbalanced!"));
4994                                         tm = GetTeam(myteam, false);
4995                                         if (tm)
4996                                         if (tm.team != COLOR_SPECTATOR)
4997                                         if (tm.team_size == ts_max)
4998                                                 s = strcat(s, sprintf(_(" Press ^3%s%s to adjust"), getcommandkey("team menu", "menu_showteamselect"), blinkcolor));
4999                                         drawInfoMessage(s)
5000                                 }
5001                         }
5002                 }
5003         }
5004         else 
5005         {
5006                 s = _("^7Press ^3ESC ^7to show HUD options.");
5007                 drawInfoMessage(s)
5008                 s = _("^3Doubleclick ^7a panel for panel-specific options.");
5009                 drawInfoMessage(s)
5010                 s = _("^3CTRL ^7to disable collision testing, ^3SHIFT ^7and");
5011                 drawInfoMessage(s)
5012                 s = _("^3ALT ^7+ ^3ARROW KEYS ^7for fine adjustments.");
5013                 drawInfoMessage(s)
5014         }
5015 }
5016
5017 /*
5018 ==================
5019 Main HUD system
5020 ==================
5021 */
5022
5023 void HUD_ShowSpeed(void)
5024 {
5025         vector numsize;
5026         float pos, conversion_factor;
5027         string speed, zspeed, unit;
5028
5029         switch(autocvar_cl_showspeed_unit)
5030         {
5031                 default:
5032                 case 0:
5033                         unit = "";
5034                         conversion_factor = 1.0;
5035                         break;
5036                 case 1:
5037                         unit = _(" qu/s");
5038                         conversion_factor = 1.0;
5039                         break;
5040                 case 2:
5041                         unit = _(" m/s");
5042                         conversion_factor = 0.0254;
5043                         break;
5044                 case 3:
5045                         unit = _(" km/h");
5046                         conversion_factor = 0.0254 * 3.6;
5047                         break;
5048                 case 4:
5049                         unit = _(" mph");
5050                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
5051                         break;
5052                 case 5:
5053                         unit = _(" knots");
5054                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
5055                         break;
5056         }
5057
5058         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
5059
5060         numsize_x = numsize_y = autocvar_cl_showspeed_size;
5061         pos = (vid_conheight - numsize_y) * autocvar_cl_showspeed_position;
5062
5063         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
5064
5065         if (autocvar_cl_showspeed_z == 1) {
5066                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
5067                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
5068         }
5069 }
5070
5071 vector acc_prevspeed;
5072 float acc_prevtime;
5073 float acc_avg;
5074
5075 void HUD_ShowAcceleration(void)
5076 {
5077         float acceleration, sz, scale, alpha, f;
5078         vector pos, top, rgb;
5079         top_x = vid_conwidth/2;
5080         top_y = 0;
5081
5082         f = time - acc_prevtime;
5083         if(autocvar_cl_showacceleration_z)
5084                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
5085         else
5086                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
5087         acc_prevspeed = pmove_vel;
5088         acc_prevtime = time;
5089
5090         f = bound(0, f * 10, 1);
5091         acc_avg = acc_avg * (1 - f) + acceleration * f;
5092         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
5093         if (acceleration == 0)
5094                 return;
5095
5096         pos = top - sz/2 * eY + (autocvar_cl_showacceleration_position * vid_conheight) * eY;
5097
5098         sz = autocvar_cl_showacceleration_size;
5099         scale = autocvar_cl_showacceleration_scale;
5100         alpha = autocvar_cl_showacceleration_alpha;
5101         if (autocvar_cl_showacceleration_color_custom)
5102                 rgb = stov(autocvar_cl_showacceleration_color);
5103         else {
5104                 if (acceleration < 0)
5105                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
5106                 else
5107                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
5108         }
5109
5110         if (acceleration > 0)
5111         HUD_Panel_DrawProgressBar(pos, eX * (vid_conwidth - pos_x) + eY * sz, "accelbar", 0, 0, acceleration * scale, rgb, alpha * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
5112         else
5113         HUD_Panel_DrawProgressBar(eY * pos_y, eX * pos_x + eY * sz, "accelbar", 0, 1, -acceleration * scale, rgb, alpha * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
5114 }
5115
5116 void HUD_Reset (void)
5117 {
5118         // reset gametype specific icons
5119         if(gametype == GAME_KEYHUNT)
5120                 HUD_Mod_KH_Reset();
5121         else if(gametype == GAME_CTF)
5122                 HUD_Mod_CTF_Reset();
5123 }
5124
5125 #define HUD_DrawPanel(id)\
5126 switch (id) {\
5127         case (HUD_PANEL_RADAR):\
5128                 HUD_Radar(); break;\
5129         case (HUD_PANEL_WEAPONS):\
5130                 HUD_Weapons(); break;\
5131         case (HUD_PANEL_AMMO):\
5132                 HUD_Ammo(); break;\
5133         case (HUD_PANEL_POWERUPS):\
5134                 HUD_Powerups(); break;\
5135         case (HUD_PANEL_HEALTHARMOR):\
5136                 HUD_HealthArmor(); break;\
5137         case (HUD_PANEL_NOTIFY):\
5138                 HUD_Notify(); break;\
5139         case (HUD_PANEL_TIMER):\
5140                 HUD_Timer(); break;\
5141         case (HUD_PANEL_SCORE):\
5142                 HUD_Score(); break;\
5143         case (HUD_PANEL_RACETIMER):\
5144                 HUD_RaceTimer(); break;\
5145         case (HUD_PANEL_VOTE):\
5146                 HUD_VoteWindow(); break;\
5147         case (HUD_PANEL_MODICONS):\
5148                 HUD_ModIcons(); break;\
5149         case (HUD_PANEL_PRESSEDKEYS):\
5150                 HUD_DrawPressedKeys(); break;\
5151         case (HUD_PANEL_CHAT):\
5152                 HUD_Chat(); break;\
5153         case (HUD_PANEL_ENGINEINFO):\
5154                 HUD_EngineInfo(); break;\
5155         case (HUD_PANEL_INFOMESSAGES):\
5156                  HUD_InfoMessages(); break;\
5157 } ENDS_WITH_CURLY_BRACE
5158
5159 void HUD_Main (void)
5160 {
5161         float i;
5162         // global hud alpha fade
5163         if(menu_enabled == 1)
5164                 hud_fade_alpha = 1;
5165         else
5166                 hud_fade_alpha = (1 - autocvar__menu_alpha);
5167
5168         if(scoreboard_fade_alpha)
5169                 hud_fade_alpha = (1 - scoreboard_fade_alpha);
5170
5171         if(intermission == 2) // no hud during mapvote
5172                 hud_fade_alpha = 0;
5173         else if(autocvar__menu_alpha == 0 && scoreboard_fade_alpha == 0)
5174                 hud_fade_alpha = 1;
5175
5176         hud_fontsize = HUD_GetFontsize("hud_fontsize");
5177
5178         if(!autocvar__hud_configure && !hud_fade_alpha)
5179                 return;
5180
5181         // Drawing stuff
5182         if (hud_skin_path != autocvar_hud_skin)
5183         {
5184                 if (hud_skin_path)
5185                         strunzone(hud_skin_path);
5186                 hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
5187         }
5188
5189         // HUD configure visible grid
5190         if(autocvar__hud_configure && autocvar_hud_configure_grid && autocvar_hud_configure_grid_alpha)
5191         {
5192                 // x-axis
5193                 for(i = 0; i < 1/bound(0.005, autocvar_hud_configure_grid_xsize, 0.2); ++i)
5194                 {
5195                         drawfill(eX * i * vid_conwidth * bound(0.005, autocvar_hud_configure_grid_xsize, 0.2), eX + eY * vid_conheight, '0.5 0.5 0.5', autocvar_hud_configure_grid_alpha, DRAWFLAG_NORMAL);
5196                 }
5197                 // y-axis
5198                 for(i = 0; i < 1/bound(0.005, autocvar_hud_configure_grid_ysize, 0.2); ++i)
5199                 {
5200                         drawfill(eY * i * vid_conheight * bound(0.005, autocvar_hud_configure_grid_ysize, 0.2), eY + eX * vid_conwidth, '0.5 0.5 0.5', autocvar_hud_configure_grid_alpha, DRAWFLAG_NORMAL);
5201                 }
5202         }
5203
5204     current_player = (spectatee_status > 0) ? spectatee_status : player_localentnum;
5205
5206         // draw the dock
5207         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
5208         {
5209                 float f;
5210                 vector color;
5211                 float hud_dock_color_team = autocvar_hud_dock_color_team;
5212                 if((teamplay) && hud_dock_color_team) {
5213                         f = stof(getplayerkey(current_player - 1, "colors"));
5214                         color = colormapPaletteColor(mod(f, 16), 1) * hud_dock_color_team;
5215                 }
5216                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
5217                         color = '1 0 0' * hud_dock_color_team;
5218                 }
5219                 else
5220                 {
5221                         string hud_dock_color = autocvar_hud_dock_color;
5222                         if(hud_dock_color == "shirt") {
5223                                 f = stof(getplayerkey(current_player - 1, "colors"));
5224                                 color = colormapPaletteColor(floor(f / 16), 0);
5225                         }
5226                         else if(hud_dock_color == "pants") {
5227                                 f = stof(getplayerkey(current_player - 1, "colors"));
5228                                 color = colormapPaletteColor(mod(f, 16), 1);
5229                         }
5230                         else
5231                                 color = stov(hud_dock_color);
5232                 }
5233
5234                 string pic;
5235                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
5236                 if(precache_pic(pic) == "") {
5237                         pic = strcat(hud_skin_path, "/dock_medium");
5238                         if(precache_pic(pic) == "") {
5239                                 pic = "gfx/hud/default/dock_medium";
5240                         }
5241                 }
5242                 drawpic('0 0 0', pic, eX * vid_conwidth + eY * vid_conheight, color, autocvar_hud_dock_alpha * hud_fade_alpha, DRAWFLAG_NORMAL); // no aspect ratio forcing on dock...
5243         }
5244
5245         // cache the panel order into the panel_order array
5246         if(autocvar__hud_panelorder != hud_panelorder_prev) {
5247                 if(hud_panelorder_prev)
5248                         strunzone(hud_panelorder_prev);
5249                 hud_panelorder_prev = strzone(autocvar__hud_panelorder);
5250                 tokenize_console(autocvar__hud_panelorder);
5251                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
5252                         panel_order[i] = stof(argv(i));
5253                 }
5254         }
5255         // draw panels in order specified by panel_order array
5256         for(i = HUD_PANEL_NUM - 1; i >= 0; --i) {
5257                 if(i != HUD_PANEL_CHAT || !autocvar__con_chat_maximized) // don't draw maximized chat panel twice!
5258                         HUD_DrawPanel(panel_order[i]);
5259         }
5260
5261         // draw chat panel on top if it is maximized
5262         if(autocvar__con_chat_maximized)
5263                 HUD_Chat(); // HUD_DrawPanel(HUD_PANEL_CHAT);
5264
5265         // TODO hud_'ify these
5266         if (autocvar_cl_showspeed)
5267                 HUD_ShowSpeed();
5268         if (autocvar_cl_showacceleration)
5269                 HUD_ShowAcceleration();
5270
5271         if (autocvar__hud_configure && spectatee_status && hud_configure_prev == -1) // try to join if we are in hud_configure mode, but still spectating, and in the first frame (in order to get rid of motd when launching a server via the menu "HUD Setup" button)
5272                 localcmd("cmd selectteam auto; cmd join\n");
5273
5274         hud_configure_prev = autocvar__hud_configure;
5275
5276         if (!autocvar__hud_configure) // hud config mode disabled, enable normal alpha stuff again
5277                 if (menu_enabled)
5278                         menu_enabled = 0;
5279 }