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