]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
HUD_Write* macros to make the HUD exporter code easier to read and edit
[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         fh = fopen(strcat("hud_", autocvar_hud_skin, "_", cfgname, ".cfg"), FILE_WRITE);
426         if(fh >= 0)
427         {
428                 HUD_Write_Cvar_q("hud_skin");
429                 HUD_Write_Cvar_q("hud_panel_bg");
430                 HUD_Write_Cvar_q("hud_panel_bg_color");
431                 HUD_Write_Cvar_q("hud_panel_bg_color_team");
432                 HUD_Write_Cvar_q("hud_panel_bg_alpha");
433                 HUD_Write_Cvar_q("hud_panel_bg_border");
434                 HUD_Write_Cvar_q("hud_panel_bg_padding");
435                 HUD_Write_Cvar_q("hud_panel_fg_alpha");
436                 HUD_Write("\n");
437
438                 HUD_Write_Cvar_q("hud_dock");
439                 HUD_Write_Cvar_q("hud_dock_color");
440                 HUD_Write_Cvar_q("hud_dock_color_team");
441                 HUD_Write_Cvar_q("hud_dock_alpha");
442                 HUD_Write("\n");
443
444                 HUD_Write_Cvar_q("hud_progressbar_alpha");
445                 HUD_Write_Cvar_q("hud_progressbar_strength_color");
446                 HUD_Write_Cvar_q("hud_progressbar_shield_color");
447                 HUD_Write_Cvar_q("hud_progressbar_health_color");
448                 HUD_Write_Cvar_q("hud_progressbar_armor_color");
449                 HUD_Write_Cvar_q("hud_progressbar_fuel_color");
450                 HUD_Write_Cvar_q("hud_progressbar_nexball_color");
451                 HUD_Write("\n");
452
453                 HUD_Write_Cvar_q("_hud_panelorder");
454                 HUD_Write("\n");
455
456                 HUD_Write_Cvar_q("hud_configure_grid");
457                 HUD_Write_Cvar_q("hud_configure_grid_xsize");
458                 HUD_Write_Cvar_q("hud_configure_grid_ysize");
459                 HUD_Write("\n");
460
461                 HUD_Write_Cvar_q("scr_centerpos");
462                 HUD_Write("\n");
463
464                 // common cvars for all panels
465                 float i;
466                 for (i = 0; i < HUD_PANEL_NUM; ++i)
467                 {
468                         HUD_Panel_GetName(i)
469
470                         HUD_Write_PanelCvar_n("");
471                         HUD_Write_PanelCvar_q("_pos");
472                         HUD_Write_PanelCvar_q("_size");
473                         HUD_Write_PanelCvar_q("_bg");
474                         HUD_Write_PanelCvar_q("_bg_color");
475                         HUD_Write_PanelCvar_q("_bg_color_team");
476                         HUD_Write_PanelCvar_q("_bg_alpha");
477                         HUD_Write_PanelCvar_q("_bg_border");
478                         HUD_Write_PanelCvar_q("_bg_padding");
479                         switch(i) {
480                                 case HUD_PANEL_WEAPONS:
481                                         HUD_Write_PanelCvar_q("_complainbubble");
482                                         HUD_Write_PanelCvar_q("_complainbubble_padding");
483                                         HUD_Write_PanelCvar_q("_complainbubble_color_outofammo");
484                                         HUD_Write_PanelCvar_q("_complainbubble_color_donthave");
485                                         HUD_Write_PanelCvar_q("_complainbubble_color_unavailable");
486                                         HUD_Write_PanelCvar_q("_ammo_color");
487                                         HUD_Write_PanelCvar_q("_ammo_alpha");
488                                         HUD_Write_PanelCvar_q("_aspect");
489                                         break;
490                                 case HUD_PANEL_AMMO:
491                                         HUD_Write_PanelCvar_q("_onlycurrent");
492                                         HUD_Write_PanelCvar_q("_iconalign");
493                                         break;
494                                 case HUD_PANEL_POWERUPS:
495                                         HUD_Write_PanelCvar_q("_flip");
496                                         HUD_Write_PanelCvar_q("_iconalign");
497                                         HUD_Write_PanelCvar_q("_baralign");
498                                         HUD_Write_PanelCvar_q("_progressbar");
499                                         break;
500                                 case HUD_PANEL_HEALTHARMOR:
501                                         HUD_Write_PanelCvar_q("_flip");
502                                         HUD_Write_PanelCvar_q("_iconalign");
503                                         HUD_Write_PanelCvar_q("_baralign");
504                                         HUD_Write_PanelCvar_q("_progressbar");
505                                         break;
506                                 case HUD_PANEL_NOTIFY:
507                                         HUD_Write_PanelCvar_q("_flip");
508                                         HUD_Write_PanelCvar_q("_print");
509                                         break;
510                                 case HUD_PANEL_RADAR:
511                                         HUD_Write_PanelCvar_q("_foreground_alpha");
512                                         break;
513                                 case HUD_PANEL_VOTE:
514                                         HUD_Write_PanelCvar_q("_alreadyvoted_alpha");
515                                         break;
516                                 case HUD_PANEL_PRESSEDKEYS:
517                                         HUD_Write_PanelCvar_q("_aspect");
518                                         break;
519                                 case HUD_PANEL_INFOMESSAGES:
520                                         HUD_Write_PanelCvar_q("_flip");
521                                         break;
522                         }
523                         HUD_Write("\n");
524                 }
525                 HUD_Write("menu_sync\n"); // force the menu to reread the cvars, so that the dialogs are updated
526
527                 print("^2Successfully exported to hud_", autocvar_hud_skin, "_", cfgname, ".cfg! (Note: It's saved in data/data/)\n");
528         }
529         fclose(fh);
530 }
531
532 const float hlBorderSize = 4;
533 const string hlBorder = "gfx/hud/default/border_highlighted";
534 const string hlBorder2 = "gfx/hud/default/border_highlighted2";
535 void HUD_Panel_HlBorder(float myBorder, vector color, float alpha)
536 {
537         drawfill(panel_pos - '1 1 0' * myBorder, panel_size + '2 2 0' * myBorder, '0 0.5 1', .5 * alpha, DRAWFLAG_NORMAL);
538         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);
539         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);
540         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);
541         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);
542 }
543
544 // draw the background/borders
545 #define HUD_Panel_DrawBg(alpha)\
546 if(panel_bg != "0")\
547         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));\
548 if(highlightedPanel_prev == active_panel && autocvar__hud_configure)\
549         HUD_Panel_HlBorder(panel_bg_border + 1.5 * hlBorderSize, '0 0.5 1', 0.25 * (1 - autocvar__menu_alpha) * alpha);
550
551 void HUD_Panel_DrawProgressBar(vector pos, float vertical, vector mySize, vector color, float alpha, float drawflag)
552 {
553         if(!alpha)
554                 return;
555
556         string pic;
557         if(vertical) {
558                 pic = strcat(hud_skin_path, "/statusbar_vertical");
559                 if(precache_pic(pic) == "") {
560                         pic = "gfx/hud/default/statusbar_vertical";
561                 }
562                 drawsubpic(pos, eY * min(mySize_y * 0.5, mySize_x) + eX * mySize_x, pic, '0 0 0', '1 0.25 0', color, alpha, drawflag);
563                 if(mySize_y/mySize_x > 2)
564                         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);
565                 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);
566         } else {
567                 pic = strcat(hud_skin_path, "/statusbar");
568                 if(precache_pic(pic) == "") {
569                         pic = "gfx/hud/default/statusbar";
570                 }
571                 drawsubpic(pos, eX * min(mySize_x * 0.5, mySize_y) + eY * mySize_y, pic, '0 0 0', '0.25 1 0', color, alpha, drawflag);
572                 if(mySize_x/mySize_y > 2)
573                         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);
574                 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);
575         }
576 }
577
578 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float alpha, float drawflag)
579 {
580         if(!alpha)
581                 return;
582
583         string pic;
584         pic = strcat(hud_skin_path, "/num_leading");
585         if(precache_pic(pic) == "") {
586                 pic = "gfx/hud/default/num_leading";
587         }
588
589         drawsubpic(pos, eX * min(mySize_x * 0.5, mySize_y) + eY * mySize_y, pic, '0 0 0', '0.25 1 0', color, alpha, drawflag);
590         if(mySize_x/mySize_y > 2)
591                 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);
592         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);
593 }
594
595 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
596 vector HUD_Panel_CheckMove(vector myPos, vector mySize)
597 {
598         float i;
599
600         vector myTarget;
601         myTarget = myPos;
602
603         vector targPos;
604         vector targSize;
605         vector myCenter;
606         vector targCenter;
607         myCenter = '0 0 0'; // shut up fteqcc, there IS a reference
608         targCenter = '0 0 0'; // shut up fteqcc, there IS a reference
609
610         for (i = 0; i < HUD_PANEL_NUM; ++i) {
611                 if(i == highlightedPanel || !panel_enabled)
612                         continue;
613
614                 HUD_Panel_UpdatePosSizeForId(i)
615
616                 panel_pos -= '1 1 0' * panel_bg_border;
617                 panel_size += '2 2 0' * panel_bg_border;
618
619                 if(myPos_y + mySize_y < panel_pos_y)
620                         continue;
621                 if(myPos_y > panel_pos_y + panel_size_y)
622                         continue;
623
624                 if(myPos_x + mySize_x < panel_pos_x)
625                         continue;
626                 if(myPos_x > panel_pos_x + panel_size_x)
627                         continue;
628
629                 // OK, there IS a collision.
630
631                 myCenter_x = myPos_x + 0.5 * mySize_x;
632                 myCenter_y = myPos_y + 0.5 * mySize_y;
633
634                 targCenter_x = panel_pos_x + 0.5 * panel_size_x;
635                 targCenter_y = panel_pos_y + 0.5 * panel_size_y;
636
637                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y) // top left (of the target panel)
638                 {
639                         if(myPos_x + mySize_x - panel_pos_x < myPos_y + mySize_y - panel_pos_y) // push it to the side
640                                 myTarget_x = panel_pos_x - mySize_x;
641                         else // push it upwards
642                                 myTarget_y = panel_pos_y - mySize_y;
643                 }
644                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y) // top right
645                 {
646                         if(panel_pos_x + panel_size_x - myPos_x < myPos_y + mySize_y - panel_pos_y) // push it to the side
647                                 myTarget_x = panel_pos_x + panel_size_x;
648                         else // push it upwards
649                                 myTarget_y = panel_pos_y - mySize_y;
650                 }
651                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
652                 {
653                         if(myPos_x + mySize_x - panel_pos_x < panel_pos_y + panel_size_y - myPos_y) // push it to the side
654                                 myTarget_x = panel_pos_x - mySize_x;
655                         else // push it downwards
656                                 myTarget_y = panel_pos_y + panel_size_y;
657                 }
658                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
659                 {
660                         if(panel_pos_x + panel_size_x - myPos_x < panel_pos_y + panel_size_y - myPos_y) // push it to the side
661                                 myTarget_x = panel_pos_x + panel_size_x;
662                         else // push it downwards
663                                 myTarget_y = panel_pos_y + panel_size_y;
664                 }
665                 if(cvar("hud_configure_checkcollisions_debug"))
666                         drawfill(panel_pos, panel_size, '1 1 0', .3, DRAWFLAG_NORMAL);
667         }
668
669         return myTarget;
670 }
671
672 void HUD_Panel_SetPos(vector pos)
673 {
674         HUD_Panel_UpdatePosSizeForId(highlightedPanel)
675         vector mySize;
676         mySize = panel_size;
677
678         if(cvar("hud_configure_checkcollisions_debug"))
679                 drawfill(pos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
680
681         if(autocvar_hud_configure_grid)
682         {
683                 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;
684                 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;
685         }
686
687         if(hud_configure_checkcollisions)
688                 pos = HUD_Panel_CheckMove(pos, mySize);
689
690         pos_x = bound(0, pos_x, vid_conwidth - mySize_x);
691         pos_y = bound(0, pos_y, vid_conheight - mySize_y);
692
693         string s;
694         s = strcat(ftos(pos_x/vid_conwidth), " ", ftos(pos_y/vid_conheight));
695
696         HUD_Panel_GetName(highlightedPanel);
697         cvar_set(strcat("hud_panel_", panel_name, "_pos"), s);
698 }
699
700 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
701 vector HUD_Panel_CheckResize(vector mySize, vector resizeorigin) {
702         float i;
703
704         float targBorder;
705         vector targPos;
706         vector targSize;
707         vector targEndPos;
708
709         vector dist;
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         if(!autocvar__hud_configure)
1024                 return false;
1025
1026         // allow console bind to work
1027         string con_keys;
1028         float keys;
1029         con_keys = findkeysforcommand("toggleconsole");
1030         keys = tokenize(con_keys);
1031
1032         float hit_con_bind, i;
1033         for (i = 0; i < keys; ++i)
1034         {
1035                 if(nPrimary == stof(argv(i)))
1036                         hit_con_bind = 1;
1037         }
1038
1039         if(bInputType == 0) {
1040                 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
1041                 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
1042                 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
1043         }
1044         else if(bInputType == 1) {
1045                 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
1046                 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
1047                 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
1048         }
1049
1050         if(nPrimary == K_MOUSE1)
1051         {
1052                 if(bInputType == 0) { // key pressed
1053                         mouseClicked = 1;
1054                         return true;
1055                 }
1056                 else if(bInputType == 1) {// key released
1057                         mouseClicked = 0;
1058                         return true;
1059                 }
1060         }
1061         else if(nPrimary == K_ESCAPE)
1062         {
1063                 if (bInputType == 1)
1064                         return true;
1065                 menu_enabled = 1;
1066                 menu_enabled_time = time;
1067                 localcmd("menu_showhudexit\n");
1068         }
1069         else if(hudShiftState & S_CTRL)
1070         {
1071                 if (mouseClicked)
1072                         return true;
1073
1074                 if(nPrimary == K_SPACE) // enable/disable highlighted panel or dock
1075                 {
1076                         if (bInputType == 1)
1077                                 return true;
1078
1079                         if (highlightedPanel_prev != -1)
1080                                 cvar_set(strcat("hud_panel_", panel_name), ftos(!(panel_enabled)));
1081                         else
1082                                 cvar_set(strcat("hud_dock"), (autocvar_hud_dock == "") ? "dock" : "");
1083                 }
1084                 if(nPrimary == 'c') // copy highlighted panel size
1085                 {
1086                         if (bInputType == 1)
1087                                 return true;
1088
1089                         if (highlightedPanel_prev != -1)
1090                         {
1091                                 panel_size_copied = panel_size;
1092                                 highlightedPanel_copied = highlightedPanel_prev;
1093                         }
1094                 }
1095                 else if(nPrimary == 'v') // past copied size on the highlighted panel
1096                 {
1097                         if (bInputType == 1)
1098                                 return true;
1099
1100                         if (highlightedPanel_copied == -1 || highlightedPanel_prev == -1)
1101                                 return true;
1102
1103                         HUD_Panel_UpdatePosSizeForId(highlightedPanel_prev)
1104
1105                         // reduce size if it'd go beyond screen boundaries
1106                         vector tmp_size = panel_size_copied;
1107                         if (panel_pos_x + panel_size_copied_x > vid_conwidth)
1108                                 tmp_size_x = vid_conwidth - panel_pos_x;
1109                         if (panel_pos_y + panel_size_copied_y > vid_conheight)
1110                                 tmp_size_y = vid_conheight - panel_pos_y;
1111
1112                         if (panel_size == tmp_size)
1113                                 return true;
1114
1115                         // backup first!
1116                         panel_pos_backup = panel_pos;
1117                         panel_size_backup = panel_size;
1118                         highlightedPanel_backup = highlightedPanel_prev;
1119
1120                         string s;
1121                         s = strcat(ftos(tmp_size_x/vid_conwidth), " ", ftos(tmp_size_y/vid_conheight));
1122                         cvar_set(strcat("hud_panel_", panel_name, "_size"), s);
1123                 }
1124                 else if(nPrimary == 'z') // undo last action
1125                 {
1126                         if (bInputType == 1)
1127                                 return true;
1128                         //restore previous values
1129                         if (highlightedPanel_backup != -1)
1130                         {
1131                                 HUD_Panel_GetName(highlightedPanel_backup)
1132                                 string s;
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) && autocvar_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         string cursor;
1423         vector cursorsize;
1424         cursorsize = '32 32 0';
1425
1426         if(highlightcheck == 0)
1427                 drawpic(mousepos, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor.tga"), '32 32 0', '1 1 1', hud_fade_alpha, DRAWFLAG_NORMAL);
1428         else if(highlightcheck == 1)
1429                 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);
1430         else if(highlightcheck == 2)
1431                 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);
1432         else
1433                 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);
1434
1435         prevMouseClicked = mouseClicked;
1436 }
1437
1438 // Weapon icons (#0)
1439 //
1440 float weaponspace[10];
1441 #define HUD_Weapons_Clear()\
1442         float idx;\
1443         for(idx = 0; idx < 10; ++idx)\
1444                 weaponspace[idx] = 0;
1445
1446 entity weaponorder[WEP_MAXCOUNT];
1447 void weaponorder_swap(float i, float j, entity pass)
1448 {
1449         entity h;
1450         h = weaponorder[i];
1451         weaponorder[i] = weaponorder[j];
1452         weaponorder[j] = h;
1453 }
1454
1455 string weaponorder_cmp_str_save;
1456 string weaponorder_cmp_str;
1457 float weaponorder_cmp(float i, float j, entity pass)
1458 {
1459         float ai, aj;
1460         ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
1461         aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
1462         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)
1463 }
1464
1465 float GetAmmoStat(float i)
1466 {
1467         switch(i)
1468         {
1469                 case 0: return STAT_SHELLS;
1470                 case 1: return STAT_NAILS;
1471                 case 2: return STAT_ROCKETS;
1472                 case 3: return STAT_CELLS;
1473                 case 4: return STAT_FUEL;
1474                 default: return -1;
1475         }
1476 }
1477
1478 float GetAmmoTypeForWep(float i)
1479 {
1480         switch(i)
1481         {
1482                 case WEP_SHOTGUN: return 0;
1483                 case WEP_UZI: return 1;
1484                 case WEP_CAMPINGRIFLE: return 1;
1485                 case WEP_GRENADE_LAUNCHER: return 2;
1486                 case WEP_MINE_LAYER: return 2;
1487                 case WEP_ELECTRO: return 3;
1488                 case WEP_CRYLINK: return 3;
1489                 case WEP_HLAC: return 3;
1490                 case WEP_MINSTANEX: return 3;
1491                 case WEP_NEX: return 3;
1492                 case WEP_HAGAR: return 2;
1493                 case WEP_ROCKET_LAUNCHER: return 2;
1494                 case WEP_SEEKER: return 2;
1495                 case WEP_FIREBALL: return 4;
1496                 case WEP_HOOK: return 3;
1497                 default: return -1;
1498         }
1499 }
1500
1501 void HUD_Weapons(void)
1502 {
1503         if(!autocvar_hud_panel_weapons && !autocvar__hud_configure)
1504                 return;
1505
1506         float timeout = cvar("hud_panel_weapons_timeout");
1507         float timeout_effect_length, timein_effect_length;
1508         if (cvar("hud_panel_weapons_timeout_effect") == 0)
1509         {
1510                 timeout_effect_length = 0;
1511                 timein_effect_length = 0;
1512         }
1513         else
1514         {
1515                 timeout_effect_length = 0.75;
1516                 timein_effect_length = 0.375;
1517         }
1518
1519         if (timeout && time >= weapontime + timeout + timeout_effect_length && !autocvar__hud_configure)
1520         {
1521                 weaponprevtime = time;
1522                 return;
1523         }
1524
1525         active_panel = HUD_PANEL_WEAPONS;
1526         HUD_Panel_UpdateCvars(weapons);
1527
1528         if (timeout && time >= weapontime + timeout && !autocvar__hud_configure)
1529         {
1530                 float f = (time - (weapontime + timeout)) / timeout_effect_length;
1531                 if (cvar("hud_panel_weapons_timeout_effect"))
1532                 {
1533                         panel_bg_alpha *= (1 - f);
1534                         panel_fg_alpha *= (1 - f);
1535                 }
1536                 if (cvar("hud_panel_weapons_timeout_effect") == 1)
1537                 {
1538                         f *= f; // for a cooler movement
1539                         vector center;
1540                         center_x = panel_pos_x + panel_size_x/2;
1541                         center_y = panel_pos_y + panel_size_y/2;
1542                         float screen_ar = vid_conwidth/vid_conheight;
1543                         if (center_x/center_y < screen_ar) //bottom left
1544                         {
1545                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //bottom
1546                                         panel_pos_y += f * (vid_conheight - panel_pos_y);
1547                                 else //left
1548                                         panel_pos_x -= f * (panel_pos_x + panel_size_x);
1549                         }
1550                         else //top right
1551                         {
1552                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //right
1553                                         panel_pos_x += f * (vid_conwidth - panel_pos_x);
1554                                 else //top
1555                                         panel_pos_y -= f * (panel_pos_y + panel_size_y);
1556                         }
1557                 }
1558                 weaponprevtime = time - (1 - f) * timein_effect_length;
1559         }
1560         else if (timeout && time < weaponprevtime + timein_effect_length && !autocvar__hud_configure)
1561         {
1562                 float f = (time - weaponprevtime) / timein_effect_length;
1563                 if (cvar("hud_panel_weapons_timeout_effect"))
1564                 {
1565                         panel_bg_alpha *= (f);
1566                         panel_fg_alpha *= (f);
1567                 }
1568                 if (cvar("hud_panel_weapons_timeout_effect") == 1)
1569                 {
1570                         f *= f; // for a cooler movement
1571                         f = 1 - f;
1572                         vector center;
1573                         center_x = panel_pos_x + panel_size_x/2;
1574                         center_y = panel_pos_y + panel_size_y/2;
1575                         float screen_ar = vid_conwidth/vid_conheight;
1576                         if (center_x/center_y < screen_ar) //bottom left
1577                         {
1578                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //bottom
1579                                         panel_pos_y += f * (vid_conheight - panel_pos_y);
1580                                 else //left
1581                                         panel_pos_x -= f * (panel_pos_x + panel_size_x);
1582                         }
1583                         else //top right
1584                         {
1585                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //right
1586                                         panel_pos_x += f * (vid_conwidth - panel_pos_x);
1587                                 else //top
1588                                         panel_pos_y -= f * (panel_pos_y + panel_size_y);
1589                         }
1590                 }
1591         }
1592
1593         float i, weapid, fade, weapon_stats, weapon_number, weapon_cnt;
1594         weapon_cnt = 0;
1595         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1596         {
1597                 self = get_weaponinfo(i);
1598                 if(self.impulse >= 0)
1599                         ++weapon_cnt;
1600         }
1601
1602         // TODO make this configurable
1603         weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
1604
1605         if(weaponorder_cmp_str != weaponorder_cmp_str_save)
1606         {
1607                 if(weaponorder_cmp_str_save)
1608                         strunzone(weaponorder_cmp_str_save);
1609                 weaponorder_cmp_str_save = strzone(weaponorder_cmp_str);
1610                 weapon_cnt = 0;
1611                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1612                 {
1613                         self = get_weaponinfo(i);
1614                         if(self.impulse >= 0)
1615                         {
1616                                 weaponorder[weapon_cnt] = self;
1617                                 ++weapon_cnt;
1618                         }
1619                 }
1620                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
1621         }
1622
1623         HUD_Panel_DrawBg(1);
1624         if(panel_bg_padding)
1625         {
1626                 panel_pos += '1 1 0' * panel_bg_padding;
1627                 panel_size -= '2 2 0' * panel_bg_padding;
1628         }
1629
1630         // hits
1631         weapon_stats = getstati(STAT_DAMAGE_HITS);
1632         weapon_number = weapon_stats & 63;
1633         weapon_hits[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1634         // fired
1635         weapon_stats = getstati(STAT_DAMAGE_FIRED);
1636         weapon_number = weapon_stats & 63;
1637         weapon_fired[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1638
1639         if(cvar_or("hud_panel_weapons_fade", 1))
1640         {
1641                 fade = 3.2 - 2 * (time - weapontime);
1642                 fade = bound(0.7, fade, 1);
1643         }
1644         else
1645                 fade = 1;
1646
1647         HUD_Weapons_Clear();
1648
1649         float rows, columns;
1650         rows = panel_size_y/panel_size_x;
1651         rows = bound(1, floor((sqrt(4 * autocvar_hud_panel_weapons_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 = autocvar_hud_panel_weapons_complainbubble_time;
1659         float fadetime;
1660         fadetime = autocvar_hud_panel_weapons_complainbubble_fadetime;
1661
1662         vector color;
1663         vector wpnpos;
1664         vector wpnsize;
1665
1666         float show_accuracy;
1667         if(autocvar_hud_panel_weapons_accuracy && acc_levels)
1668                 show_accuracy = true;
1669
1670         for(i = 0; i < weapon_cnt; ++i)
1671         {
1672                 wpnpos = panel_pos + eX * column * panel_size_x*(1/columns) + eY * row * panel_size_y*(1/rows);
1673                 wpnsize = eX * panel_size_x*(1/columns) + eY * panel_size_y*(1/rows);
1674
1675                 self = weaponorder[i];
1676                 weapid = self.impulse;
1677
1678                 // draw background behind currently selected weapon
1679                 if(self.weapon == activeweapon)
1680                         drawpic_aspect_skin(wpnpos, "weapon_current_bg", wpnsize, '1 1 1', fade * panel_fg_alpha, DRAWFLAG_NORMAL);
1681
1682                 // draw the weapon accuracy
1683                 if(show_accuracy)
1684                 {
1685                         float weapon_hit, weapon_damage;
1686                         weapon_damage = weapon_fired[self.weapon-WEP_FIRST];
1687                         if(weapon_damage)
1688                         {
1689                                 weapon_hit = weapon_hits[self.weapon-WEP_FIRST];
1690                                 weapon_stats = floor(100 * weapon_hit / weapon_damage);
1691                         }
1692
1693                         // find the max level lower than weapon_stats
1694                         float j;
1695                         j = acc_levels-1;
1696                         while ( j && weapon_stats < acc_lev[j] )
1697                                 --j;
1698
1699                         // inject color j+1 in color j, how much depending on how much weapon_stats is higher than level j
1700                         float factor;
1701                         factor = (weapon_stats - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
1702                         color = acc_color(j);
1703                         color = color + factor * (acc_color(j+1) - color);
1704
1705                         if(weapon_damage)
1706                                 drawpic_aspect_skin(wpnpos, "weapon_accuracy", wpnsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
1707                 }
1708
1709                 // draw the weapon icon
1710                 if((self.impulse >= 0) && (getstati(STAT_WEAPONS) & self.weapons))
1711                 {
1712                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '1 1 1', fade * panel_fg_alpha, DRAWFLAG_NORMAL);
1713
1714                         if(autocvar_hud_panel_weapons_label == 1) // weapon number
1715                                 drawstring(wpnpos, ftos(weapid), '1 1 0' * 0.5 * panel_size_y*(1/rows), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1716                         else if(autocvar_hud_panel_weapons_label == 2) // bind
1717                                 drawstring(wpnpos, getcommandkey(ftos(weapid), strcat("impulse ", ftos(weapid))), '1 1 0' * 0.5 * panel_size_y*(1/rows), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1718
1719                         // draw ammo status bar
1720                         if(autocvar_hud_panel_weapons_ammo && weapid != WEP_TUBA && weapid != WEP_LASER && weapid != WEP_PORTO)
1721                         {
1722                                 a = 0;
1723                                 type = GetAmmoTypeForWep(weapid);
1724                                 if(type != -1)
1725                                         a = getstati(GetAmmoStat(type)); // how much ammo do we have?
1726
1727                                 if(a > 0)
1728                                 {
1729                                         switch(type) {
1730                                                 case 0: fullammo = autocvar_hud_panel_weapons_ammo_full_shells; break;
1731                                                 case 1: fullammo = autocvar_hud_panel_weapons_ammo_full_nails; break;
1732                                                 case 2: fullammo = autocvar_hud_panel_weapons_ammo_full_rockets; break;
1733                                                 case 3: fullammo = autocvar_hud_panel_weapons_ammo_full_cells; break;
1734                                                 case 4: fullammo = autocvar_hud_panel_weapons_ammo_full_fuel; break;
1735                                                 default: fullammo = 60;
1736                                         }
1737
1738                                         vector barsize;
1739                                         vector barpos;
1740                                         if(wpnsize_x/wpnsize_y > autocvar_hud_panel_weapons_aspect)
1741                                         {
1742                                                 barsize_x = autocvar_hud_panel_weapons_aspect * wpnsize_y;
1743                                                 barsize_y = wpnsize_y;
1744
1745                                                 barpos_x = wpnpos_x + (wpnsize_x - barsize_x) / 2;
1746                                                 barpos_y = wpnpos_y;
1747                                         }
1748                                         else
1749                                         {
1750                                                 barsize_y = 1/autocvar_hud_panel_weapons_aspect * wpnsize_x;
1751                                                 barsize_x = wpnsize_x;
1752
1753                                                 barpos_y = wpnpos_y + (wpnsize_y - barsize_y) / 2;
1754                                                 barpos_x = wpnpos_x;
1755                                         }
1756
1757                                         drawsetcliparea(
1758                                                 barpos_x,
1759                                                 barpos_y,
1760                                                 barsize_x * bound(0, a/fullammo, 1),
1761                                                 barsize_y);
1762                                         drawpic_aspect_skin(wpnpos, "weapon_ammo", wpnsize, autocvar_hud_panel_weapons_ammo_color, panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha, DRAWFLAG_NORMAL);
1763                                         drawresetcliparea();
1764                                 }
1765                         }
1766                 }
1767
1768                 // draw a "ghost weapon icon" if you don't have the weapon
1769                 else
1770                 {
1771                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '0 0 0', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
1772                 }
1773
1774                 // draw the complain message
1775                 if(time - complain_weapon_time < when + fadetime && self.weapon == complain_weapon && autocvar_hud_panel_weapons_complainbubble)
1776                 {
1777                         if(fadetime)
1778                         {
1779                                 if(complain_weapon_time + when > time)
1780                                         a = 1;
1781                                 else
1782                                         a = bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1);
1783                         }
1784                         else
1785                         {
1786                                 if(complain_weapon_time + when > time)
1787                                         a = 1;
1788                                 else
1789                                         a = 0;
1790                         }
1791
1792                         string s;
1793                         if(complain_weapon_type == 0) {
1794                                 s = "Out of ammo";
1795                                 color = autocvar_hud_panel_weapons_complainbubble_color_outofammo;
1796                         }
1797                         else if(complain_weapon_type == 1) {
1798                                 s = "Don't have";
1799                                 color = autocvar_hud_panel_weapons_complainbubble_color_donthave;
1800                         }
1801                         else {
1802                                 s = "Unavailable";
1803                                 color = autocvar_hud_panel_weapons_complainbubble_color_unavailable;
1804                         }
1805                         drawpic_aspect_skin(wpnpos + '1 1 0' * autocvar_hud_panel_weapons_complainbubble_padding, "weapon_complainbubble", wpnsize - '2 2 0' * autocvar_hud_panel_weapons_complainbubble_padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
1806                         drawstring_aspect(wpnpos + '1 1 0' * autocvar_hud_panel_weapons_complainbubble_padding, s, wpnsize - '2 2 0' * autocvar_hud_panel_weapons_complainbubble_padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
1807                 }
1808
1809                 ++row;
1810                 if(row >= rows)
1811                 {
1812                         row = 0;
1813                         ++column;
1814                 }
1815         }
1816
1817 }
1818
1819 // Ammo (#1)
1820 //
1821 // TODO: macro
1822 float GetAmmoItemCode(float i)
1823 {
1824         switch(i)
1825         {
1826                 case 0: return IT_SHELLS;
1827                 case 1: return IT_NAILS;
1828                 case 2: return IT_ROCKETS;
1829                 case 3: return IT_CELLS;
1830                 case 4: return IT_FUEL;
1831                 default: return -1;
1832         }
1833 }
1834
1835 string GetAmmoPicture(float i)
1836 {
1837         switch(i)
1838         {
1839                 case 0: return "ammo_shells";
1840                 case 1: return "ammo_bullets";
1841                 case 2: return "ammo_rockets";
1842                 case 3: return "ammo_cells";
1843                 case 4: return "ammo_fuel";
1844                 default: return "";
1845         }
1846 }
1847
1848 void DrawAmmoItem(vector myPos, vector mySize, float itemcode, float currently_selected)
1849 {
1850         float a;
1851         a = getstati(GetAmmoStat(itemcode)); // how much ammo do we have of type itemcode?
1852         if(autocvar__hud_configure)
1853                 a = 100;
1854
1855         vector color;
1856         if(a < 10)
1857                 color = '0.7 0 0';
1858         else
1859                 color = '1 1 1';
1860
1861         float alpha;
1862         if(currently_selected)
1863                 alpha = 1;
1864         else
1865                 alpha = 0.7;
1866
1867         vector newSize, newPos;
1868         if(mySize_x/mySize_y > 3)
1869         {
1870                 newSize_x = 3 * mySize_y;
1871                 newSize_y = mySize_y;
1872
1873                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
1874                 newPos_y = myPos_y;
1875         }
1876         else
1877         {
1878                 newSize_y = 1/3 * mySize_x;
1879                 newSize_x = mySize_x;
1880
1881                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
1882                 newPos_x = myPos_x;
1883         }
1884
1885         vector picpos, numpos;
1886         if(autocvar_hud_panel_ammo_iconalign)
1887         {
1888                 numpos = newPos;
1889                 picpos = newPos + eX * 2 * newSize_y;
1890         }
1891         else
1892         {
1893                 numpos = newPos + eX * newSize_y;
1894                 picpos = newPos;
1895         }
1896
1897         if (currently_selected)
1898                 drawpic_aspect_skin(newPos, "ammo_current_bg", newSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1899
1900         drawfont = hud_bigfont;
1901         if(a > 0)
1902                 drawstring_aspect(numpos, ftos(a), eX * (2/3) * newSize_x + eY * newSize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1903         else // "ghost" ammo count
1904                 drawstring_aspect(numpos, ftos(a), eX * (2/3) * newSize_x + eY * newSize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1905         drawfont = hud_font;
1906         if(a > 0)
1907                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * newSize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1908         else // "ghost" ammo icon
1909                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * newSize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1910 }
1911
1912 void HUD_Ammo(void)
1913 {
1914         if(!autocvar_hud_panel_ammo && !autocvar__hud_configure)
1915                 return;
1916
1917         active_panel = HUD_PANEL_AMMO;
1918         HUD_Panel_UpdateCvars(ammo);
1919         float i, currently_selected;
1920
1921         vector pos, mySize;
1922         pos = panel_pos;
1923         mySize = panel_size;
1924
1925         HUD_Panel_DrawBg(1);
1926         if(panel_bg_padding)
1927         {
1928                 pos += '1 1 0' * panel_bg_padding;
1929                 mySize -= '2 2 0' * panel_bg_padding;
1930         }
1931
1932         float rows, columns;
1933         rows = mySize_y/mySize_x;
1934         rows = bound(1, floor((sqrt(4 * (3/1) * rows * AMMO_COUNT + rows * rows) + rows + 0.5) / 2), AMMO_COUNT);
1935         //                               ^^^ ammo item aspect goes here
1936
1937         columns = ceil(AMMO_COUNT/rows);
1938
1939         float row, column;
1940         // ammo
1941         for (i = 0; i < AMMO_COUNT; ++i) {
1942                 currently_selected = getstati(STAT_ITEMS) & GetAmmoItemCode(i);
1943                 if(autocvar_hud_panel_ammo_onlycurrent) {
1944                         if(autocvar__hud_configure)
1945                                 i = 2;
1946                         if (currently_selected || autocvar__hud_configure)
1947                         {
1948                                 DrawAmmoItem(pos, mySize, i, currently_selected);
1949                                 break;
1950                         }
1951                 } else {
1952                         DrawAmmoItem(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows), i, currently_selected);
1953                         ++row;
1954                         if(row >= rows)
1955                         {
1956                                 row = 0;
1957                                 column = column + 1;
1958                         }
1959                 }
1960         }
1961 }
1962
1963 void DrawNumIcon(float iconalign, vector myPos, vector mySize, float x, string icon, float left, vector color, float alpha)
1964 {
1965         vector newSize, newPos;
1966         if(mySize_x/mySize_y > 3)
1967         {
1968                 newSize_x = 3 * mySize_y;
1969                 newSize_y = mySize_y;
1970
1971                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
1972                 newPos_y = myPos_y;
1973         }
1974         else
1975         {
1976                 newSize_y = 1/3 * mySize_x;
1977                 newSize_x = mySize_x;
1978
1979                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
1980                 newPos_x = myPos_x;
1981         }
1982
1983         vector picpos, numpos;
1984         if(left)
1985         {
1986                 if(iconalign == 1 || iconalign == 3) // right align
1987                 {
1988                         numpos = newPos;
1989                         picpos = newPos + eX * 2 * newSize_y;
1990                 }
1991                 else // left align
1992                 {
1993                         numpos = newPos + eX * newSize_y;
1994                         picpos = newPos;
1995                 }
1996         }
1997         else
1998         {
1999                 if(iconalign == 0 || iconalign == 3) // left align
2000                 {
2001                         numpos = newPos + eX * newSize_y;
2002                         picpos = newPos;
2003                 } 
2004                 else // right align
2005                 {
2006                         numpos = newPos;
2007                         picpos = newPos + eX * 2 * newSize_y;
2008                 }
2009         }
2010
2011         drawfont = hud_bigfont;
2012         drawstring_aspect(numpos, ftos(x), eX * (2/3) * newSize_x + eY * newSize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2013         drawfont = hud_font;
2014         drawpic_aspect_skin(picpos, icon, '1 1 0' * newSize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2015 }
2016
2017 void DrawNumIcon_expanding(float iconalign, vector myPos, vector mySize, float x, string icon, float left, vector color, float fadelerp)
2018 {
2019         float sz;
2020         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
2021
2022         DrawNumIcon(iconalign, myPos + expandingbox_resize_centered_box_offset(sz, mySize, 1), mySize * sz, x, icon, left, color, (1 - fadelerp));
2023 }
2024
2025 // Powerups (#2)
2026 //
2027 void HUD_Powerups(void) {
2028         if(!autocvar_hud_panel_powerups && !autocvar__hud_configure)
2029                 return;
2030
2031         active_panel = HUD_PANEL_POWERUPS;
2032         HUD_Panel_UpdateCvars(powerups);
2033         float stat_items;
2034         stat_items = getstati(STAT_ITEMS);
2035
2036         if(!autocvar__hud_configure)
2037         {
2038                 if not(stat_items & IT_STRENGTH)
2039                         if not(stat_items & IT_INVINCIBLE)
2040                                 return;
2041
2042                 if (getstati(STAT_HEALTH) <= 0)
2043                         return;
2044         }
2045
2046         vector pos, mySize;
2047         pos = panel_pos;
2048         mySize = panel_size;
2049
2050         float strength_time, shield_time;
2051         if(autocvar__hud_configure)
2052         {
2053                 strength_time = 15;
2054                 shield_time = 27;
2055         }
2056         else
2057         {
2058                 strength_time = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
2059                 shield_time = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
2060         }
2061
2062         HUD_Panel_DrawBg(bound(0, max(strength_time, shield_time), 1));
2063         if(panel_bg_padding)
2064         {
2065                 pos += '1 1 0' * panel_bg_padding;
2066                 mySize -= '2 2 0' * panel_bg_padding;
2067         }
2068
2069         vector barpos, barsize;
2070         vector picpos;
2071         vector numpos;
2072
2073         string leftname, rightname;
2074         float leftcnt, rightcnt;
2075         float leftexact, rightexact;
2076         if (autocvar_hud_panel_powerups_flip) {
2077                 leftname = "strength";
2078                 leftcnt = ceil(strength_time);
2079                 leftexact = strength_time;
2080
2081                 rightname = "shield";
2082                 rightcnt = ceil(shield_time);
2083                 rightexact = shield_time;
2084         } else {
2085                 leftname = "shield";
2086                 leftcnt = ceil(shield_time);
2087                 leftexact = shield_time;
2088
2089                 rightname = "strength";
2090                 rightcnt = ceil(strength_time);
2091                 rightexact = strength_time;
2092         }
2093
2094         drawfont = hud_bigfont;
2095         if (mySize_x/mySize_y > 4)
2096         {
2097                 if(leftcnt)
2098                 {
2099                         if(autocvar_hud_panel_powerups_baralign == 1 || autocvar_hud_panel_powerups_baralign == 3) { // right align
2100                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/30);
2101                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
2102                         } else { // left align
2103                                 barpos = pos;
2104                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
2105                         }
2106
2107                         if(autocvar_hud_panel_powerups_progressbar)
2108                         {
2109                                 HUD_Panel_GetProgressBarColor(leftname)
2110                                 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);
2111                         }
2112                         if(leftcnt > 1)
2113                                 DrawNumIcon(autocvar_hud_panel_powerups_iconalign, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, '1 1 1', 1);
2114                         if(leftcnt <= 5)
2115                                 DrawNumIcon_expanding(autocvar_hud_panel_powerups_iconalign, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, '1 1 1', bound(0, (leftcnt - leftexact) / 0.5, 1));
2116                 }
2117
2118                 if(rightcnt)
2119                 {
2120                         if(autocvar_hud_panel_powerups_baralign == 0 || autocvar_hud_panel_powerups_baralign == 3) { // left align
2121                                 barpos = pos + eX * 0.5 * mySize_x;
2122                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
2123                         } else { // right align
2124                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/30);
2125                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
2126                         }
2127
2128                         if(autocvar_hud_panel_powerups_progressbar)
2129                         {
2130                                 HUD_Panel_GetProgressBarColor(rightname)
2131                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1) * panel_fg_alpha, DRAWFLAG_NORMAL);
2132                         }
2133                         if(rightcnt > 1)
2134                                 DrawNumIcon(autocvar_hud_panel_powerups_iconalign, pos + eX * 0.5 * mySize_x, eX * 0.5 * mySize_x + eY * mySize_y, rightcnt, rightname, 0, '1 1 1', 1);
2135                         if(rightcnt <= 5)
2136                                 DrawNumIcon_expanding(autocvar_hud_panel_powerups_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));
2137                 }
2138         }
2139         else if (mySize_x/mySize_y > 1.5)
2140         {
2141                 if(leftcnt)
2142                 {
2143                         if(autocvar_hud_panel_powerups_baralign == 1 || autocvar_hud_panel_powerups_baralign == 3) { // right align
2144                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/30);
2145                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
2146                         } else { // left align
2147                                 barpos = pos;
2148                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
2149                         }
2150
2151                         if(autocvar_hud_panel_powerups_progressbar)
2152                         {
2153                                 HUD_Panel_GetProgressBarColor(leftname)
2154                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1) * panel_fg_alpha, DRAWFLAG_NORMAL);
2155                         }
2156                         if(leftcnt > 1)
2157                                 DrawNumIcon(autocvar_hud_panel_powerups_iconalign, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, '1 1 1', 1);
2158                         if(leftcnt <= 5)
2159                                 DrawNumIcon_expanding(autocvar_hud_panel_powerups_iconalign, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, '1 1 1', bound(0, (leftcnt - leftexact) / 0.5, 1));
2160                 }
2161
2162                 if(rightcnt)
2163                 {
2164                         if(autocvar_hud_panel_powerups_baralign == 0 || autocvar_hud_panel_powerups_baralign == 3) { // left align
2165                                 barpos = pos + eY * 0.5 * mySize_y;
2166                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
2167                         } else { // right align
2168                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
2169                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
2170                         }
2171
2172                         if(autocvar_hud_panel_powerups_progressbar)
2173                         {
2174                                 HUD_Panel_GetProgressBarColor(rightname)
2175                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1) * panel_fg_alpha, DRAWFLAG_NORMAL);
2176                         }
2177                         if(rightcnt > 1)
2178                                 DrawNumIcon(autocvar_hud_panel_powerups_iconalign, pos + eY * 0.5 * mySize_y, eX * mySize_x + eY * 0.5 * mySize_y, rightcnt, rightname, 0, '1 1 1', 1);
2179                         if(rightcnt <= 5)
2180                                 DrawNumIcon_expanding(autocvar_hud_panel_powerups_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));
2181                 }
2182         }
2183         else
2184         {
2185                 if(leftcnt)
2186                 {
2187                         if(autocvar_hud_panel_powerups_baralign == 1 || autocvar_hud_panel_powerups_baralign == 3) { // down align
2188                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/30);
2189                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
2190                         } else { // up align
2191                                 barpos = pos;
2192                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
2193                         }
2194
2195                         if(autocvar_hud_panel_powerups_iconalign == 1 || autocvar_hud_panel_powerups_iconalign == 3) { // down align
2196                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
2197                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
2198                         } else { // up align
2199                                 picpos = pos + eX * 0.05 * mySize_x;
2200                                 numpos = pos + eY * 0.4 * mySize_x;
2201                         }
2202
2203                         if(autocvar_hud_panel_powerups_progressbar)
2204                         {
2205                                 HUD_Panel_GetProgressBarColor(leftname)
2206                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1) * panel_fg_alpha, DRAWFLAG_NORMAL);
2207                         }
2208                         if(leftcnt <= 5)
2209                                 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));
2210                         if(leftcnt > 1)
2211                                 drawpic_aspect_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2212                         drawstring_aspect(numpos, ftos(leftcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2213                 }
2214
2215                 if(rightcnt)
2216                 {
2217                         if(autocvar_hud_panel_powerups_baralign == 0 || autocvar_hud_panel_powerups_baralign == 3) { // up align
2218                                 barpos = pos + eX * 0.5 * mySize_x;
2219                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
2220                         } else { // down align
2221                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/30) + eX * 0.5 * mySize_x;
2222                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
2223                         }
2224
2225                         if(autocvar_hud_panel_powerups_iconalign == 0 || autocvar_hud_panel_powerups_iconalign == 3) { // up align
2226                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2227                                 numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2228                         } else { // down align
2229                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
2230                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
2231                         }
2232
2233                         if(autocvar_hud_panel_powerups_progressbar)
2234                         {
2235                                 HUD_Panel_GetProgressBarColor(rightname)
2236                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * bound(0, max(strength_time, shield_time), 1) * panel_fg_alpha, DRAWFLAG_NORMAL);
2237                         }
2238                         if(rightcnt <= 5)
2239                                 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));
2240                         if(rightcnt > 1)
2241                                 drawpic_aspect_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2242                         drawstring_aspect(numpos, ftos(rightcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2243                 }
2244         }
2245         drawfont = hud_font;
2246 }
2247
2248 // Health/armor (#3)
2249 //
2250 void HUD_HealthArmor(void)
2251 {
2252         if(!autocvar_hud_panel_healtharmor && !autocvar__hud_configure)
2253                 return;
2254
2255         active_panel = HUD_PANEL_HEALTHARMOR;
2256         HUD_Panel_UpdateCvars(healtharmor);
2257         vector pos, mySize;
2258         pos = panel_pos;
2259         mySize = panel_size;
2260
2261         HUD_Panel_DrawBg(1);
2262         if(panel_bg_padding)
2263         {
2264                 pos += '1 1 0' * panel_bg_padding;
2265                 mySize -= '2 2 0' * panel_bg_padding;
2266         }
2267
2268         float armor, health, fuel;
2269         armor = getstati(STAT_ARMOR);
2270         health = getstati(STAT_HEALTH);
2271         fuel = getstati(STAT_FUEL);
2272
2273         if(autocvar__hud_configure)
2274         {
2275                 armor = 150;
2276                 health = 100;
2277                 fuel = 70;
2278         }
2279
2280         if(health <= 0)
2281                 return;
2282
2283         vector barpos, barsize;
2284         vector picpos;
2285         vector numpos;
2286
2287         drawfont = hud_bigfont;
2288         if(autocvar_hud_panel_healtharmor == 2) // combined health and armor display
2289         {
2290                 vector v;
2291                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
2292
2293                 float x;
2294                 x = floor(v_x + 1);
2295
2296                 if(autocvar_hud_panel_healtharmor_baralign == 1 || autocvar_hud_panel_healtharmor_baralign == 3) { // right align
2297                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, x/400);
2298                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
2299                 } else { // left align
2300                         barpos = pos;
2301                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
2302                 }
2303
2304                 string biggercount;
2305                 if(v_z) // NOT fully armored
2306                 {
2307                         biggercount = "health";
2308                         if(autocvar_hud_panel_healtharmor_progressbar)
2309                         {
2310                                 HUD_Panel_GetProgressBarColor("health")
2311                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2312                         }
2313                         if(armor)
2314                                 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);
2315                 }
2316                 else
2317                 {
2318                         biggercount = "armor";
2319                         if(autocvar_hud_panel_healtharmor_progressbar)
2320                         {
2321                                 HUD_Panel_GetProgressBarColor("armor")
2322                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2323                         }
2324                         if(health)
2325                                 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);
2326                 }
2327                 DrawNumIcon(autocvar_hud_panel_healtharmor_iconalign, pos, mySize, x, biggercount, 1, HUD_Get_Num_Color(x, 2 * 200), 1);
2328
2329                 // fuel
2330                 if(fuel)
2331                 {
2332                         if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2333                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2334                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2335                         } else {
2336                                 barpos = pos;
2337                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2338                         }
2339                         HUD_Panel_GetProgressBarColor("fuel")
2340                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2341                 }
2342         }
2343         else
2344         {
2345                 string leftname, rightname;
2346                 float leftcnt, rightcnt;
2347                 float leftactive, rightactive;
2348                 float leftalpha, rightalpha;
2349                 if (autocvar_hud_panel_healtharmor_flip) { // old style layout with armor left/top of health
2350                         leftname = "armor";
2351                         leftcnt = armor;
2352                         if(leftcnt)
2353                                 leftactive = 1;
2354                         leftalpha = min((armor+10)/55, 1);
2355
2356                         rightname = "health";
2357                         rightcnt = health;
2358                         rightactive = 1;
2359                         rightalpha = 1;
2360                 } else {
2361                         leftname = "health";
2362                         leftcnt = health;
2363                         leftactive = 1;
2364                         leftalpha = 1;
2365
2366                         rightname = "armor";
2367                         rightcnt = armor;
2368                         if(rightcnt)
2369                                 rightactive = 1;
2370                         rightalpha = min((armor+10)/55, 1);
2371                 }
2372
2373                 if (mySize_x/mySize_y > 4)
2374                 {
2375                         if(leftactive)
2376                         {
2377                                 if(autocvar_hud_panel_healtharmor_baralign == 1 || autocvar_hud_panel_healtharmor_baralign == 3) { // right align
2378                                         barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/200);
2379                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
2380                                 } else { // left align
2381                                         barpos = pos;
2382                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
2383                                 }
2384
2385                                 if(autocvar_hud_panel_healtharmor_progressbar)
2386                                 {
2387                                         HUD_Panel_GetProgressBarColor(leftname)
2388                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2389                                 }
2390                                 DrawNumIcon(autocvar_hud_panel_healtharmor_iconalign, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, HUD_Get_Num_Color(leftcnt, 200), 1);
2391                         }
2392
2393                         if(rightactive)
2394                         {
2395                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2396                                         barpos = pos + eX * 0.5 * mySize_x;
2397                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
2398                                 } else { // right align
2399                                         barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/200);
2400                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
2401                                 }
2402
2403                                 if(autocvar_hud_panel_healtharmor_progressbar)
2404                                 {
2405                                         HUD_Panel_GetProgressBarColor(rightname)
2406                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2407                                 }
2408                                 DrawNumIcon(autocvar_hud_panel_healtharmor_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);
2409                         }
2410
2411                         if(fuel)
2412                         {
2413                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2414                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2415                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2416                                 } else {
2417                                         barpos = pos;
2418                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2419                                 }
2420                                 HUD_Panel_GetProgressBarColor("fuel")
2421                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2422                         }
2423                 }
2424                 else if (mySize_x/mySize_y > 1.5)
2425                 {
2426                         if(leftactive)
2427                         {
2428                                 if(autocvar_hud_panel_healtharmor_baralign == 1 || autocvar_hud_panel_healtharmor_baralign == 3) { // right align
2429                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/200);
2430                                         barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
2431                                 } else { // left align
2432                                         barpos = pos;
2433                                         barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
2434                                 }
2435
2436                                 if(autocvar_hud_panel_healtharmor_progressbar)
2437                                 {
2438                                         HUD_Panel_GetProgressBarColor(leftname)
2439                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2440                                 }
2441                                 DrawNumIcon(autocvar_hud_panel_healtharmor_iconalign, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, HUD_Get_Num_Color(leftcnt, 200), 1);
2442                         }
2443
2444                         if(rightactive)
2445                         {
2446                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2447                                         barpos = pos + eY * 0.5 * mySize_y;
2448                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2449                                 } else { // right align
2450                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2451                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2452                                 }
2453
2454                                 if(autocvar_hud_panel_healtharmor_progressbar)
2455                                 {
2456                                         HUD_Panel_GetProgressBarColor(rightname)
2457                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2458                                 }
2459                                 DrawNumIcon(autocvar_hud_panel_healtharmor_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);
2460                         }
2461
2462                         if(fuel)
2463                         {
2464                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2465                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2466                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2467                                 } else {
2468                                         barpos = pos;
2469                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2470                                 }
2471                                 HUD_Panel_GetProgressBarColor("fuel")
2472                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2473                         }
2474                 }
2475                 else
2476                 {
2477                         if(leftactive)
2478                         {
2479                                 if(autocvar_hud_panel_healtharmor_baralign == 1 || autocvar_hud_panel_healtharmor_baralign == 3) { // down align
2480                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/200);
2481                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2482                                 } else { // up align
2483                                         barpos = pos;
2484                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2485                                 }
2486
2487                                 if(autocvar_hud_panel_healtharmor_iconalign == 1 || autocvar_hud_panel_healtharmor_iconalign == 3) { // down align
2488                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
2489                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
2490                                 } else { // up align
2491                                         picpos = pos + eX * 0.05 * mySize_x;
2492                                         numpos = pos + eY * 0.4 * mySize_x;
2493                                 }
2494
2495                                 if(autocvar_hud_panel_healtharmor_progressbar)
2496                                 {
2497                                         HUD_Panel_GetProgressBarColor(leftname)
2498                                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2499                                 }
2500                                 drawpic_aspect_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2501                                 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);
2502                         }
2503
2504                         if(rightactive)
2505                         {
2506                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // up align
2507                                         barpos = pos + eX * 0.5 * mySize_x;
2508                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2509                                 } else { // down align
2510                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/200) + eX * 0.5 * mySize_x;
2511                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2512                                 }
2513
2514                                 if(autocvar_hud_panel_healtharmor_iconalign == 0 || autocvar_hud_panel_healtharmor_iconalign == 3) { // up align
2515                                         picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2516                                         numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2517                                 } else { // down align
2518                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
2519                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
2520                                 }
2521
2522                                 if(autocvar_hud_panel_healtharmor_progressbar)
2523                                 {
2524                                         HUD_Panel_GetProgressBarColor(rightname)
2525                                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2526                                 }
2527                                 drawpic_aspect_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2528                                 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);
2529                         }
2530
2531                         if(fuel)
2532                         {
2533                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2534                                         barpos = pos;
2535                                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2536                                 } else {
2537                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, fuel/100);
2538                                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2539                                 }
2540                                 HUD_Panel_GetProgressBarColor("fuel")
2541                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2542                         }
2543                 }
2544         }
2545         drawfont = hud_font;
2546 }
2547
2548 // Notification area (#4)
2549 //
2550
2551 string Weapon_SuicideMessage(float deathtype)
2552 {
2553         w_deathtype = deathtype;
2554         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_SUICIDEMESSAGE);
2555         return w_deathtypestring;
2556 }
2557
2558 string Weapon_KillMessage(float deathtype)
2559 {
2560         w_deathtype = deathtype;
2561         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_KILLMESSAGE);
2562         return w_deathtypestring;
2563 }
2564
2565 float killnotify_times[10];
2566 float killnotify_deathtype[10];
2567 float killnotify_actiontype[10]; // 0 = "Y [used by] X", 1 = "X [did action to] Y"
2568 string killnotify_attackers[10];
2569 string killnotify_victims[10];
2570 void HUD_KillNotify_Push(string attacker, string victim, float actiontype, float wpn)
2571 {
2572         float i;
2573         for (i = 9; i > 0; --i) {
2574                 killnotify_times[i] = killnotify_times[i-1];
2575                 killnotify_deathtype[i] = killnotify_deathtype[i-1];
2576                 killnotify_actiontype[i] = killnotify_actiontype[i-1];
2577                 if(killnotify_attackers[i])
2578                         strunzone(killnotify_attackers[i]);
2579                 killnotify_attackers[i] = strzone(killnotify_attackers[i-1]);
2580                 if(killnotify_victims[i])
2581                         strunzone(killnotify_victims[i]);
2582                 killnotify_victims[i] = strzone(killnotify_victims[i-1]);
2583         }
2584         killnotify_times[0] = time;
2585         killnotify_deathtype[0] = wpn;
2586         killnotify_actiontype[0] = actiontype;
2587         if(killnotify_attackers[0])
2588                 strunzone(killnotify_attackers[0]);
2589         killnotify_attackers[0] = strzone(attacker);
2590         if(killnotify_victims[0])
2591                 strunzone(killnotify_victims[0]);
2592         killnotify_victims[0] = strzone(victim);
2593 }
2594
2595 void HUD_KillNotify(string s1, string s2, string s3, float type, float msg) // s1 = attacker, s2 = victim
2596 {
2597         float w;
2598         float alsoprint, gentle;
2599         alsoprint = (autocvar_hud_panel_notify_print || !panel_enabled); // print message to console if: notify panel disabled, or cvar to do so enabled
2600         gentle = (autocvar_cl_gentle || autocvar_cl_gentle_messages);
2601         
2602         if(msg == MSG_SUICIDE) {
2603                 w = DEATH_WEAPONOF(type);
2604                 if(WEP_VALID(w)) {
2605                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2606                         if (alsoprint)
2607                                 print("^1", sprintf(Weapon_SuicideMessage(type), strcat(s1, "^1")), "\n");
2608                 } else if (type == DEATH_KILL) {
2609                         HUD_KillNotify_Push(s1, "", 0, DEATH_KILL);
2610                         if (alsoprint)
2611                                 print ("^1",s1, "^1 couldn't take it anymore\n");
2612                 } else if (type == DEATH_ROT) {
2613                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2614                         if (alsoprint)
2615                                 print ("^1",s1, "^1 died\n");
2616                 } else if (type == DEATH_NOAMMO) {
2617                         HUD_KillNotify_Push(s1, "", 0, DEATH_NOAMMO);
2618                         if (alsoprint)
2619                                 print ("^7",s1, "^7 committed suicide. What's the point of living without ammo?\n");
2620                 } else if (type == DEATH_CAMP) {
2621                         HUD_KillNotify_Push(s1, "", 0, DEATH_CAMP);
2622                         if (alsoprint)
2623                                 print ("^1",s1, "^1 thought they found a nice camping ground\n");
2624                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2625                         HUD_KillNotify_Push(s1, "", 0, type);
2626                         if (alsoprint)
2627                                 print ("^1",s1, "^1 didn't become friends with the Lord of Teamplay\n");
2628                 } else if (type == DEATH_CHEAT) {
2629                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2630                         if (alsoprint)
2631                                 print ("^1",s1, "^1 unfairly eliminated themself\n");
2632                 } else if (type == DEATH_FIRE) {
2633                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2634                         if (alsoprint)
2635                                 print ("^1",s1, "^1 burned to death\n");
2636                 } else if (type != DEATH_TEAMCHANGE && type != DEATH_QUIET) {
2637                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2638                         if (alsoprint)
2639                                 print ("^1",s1, "^1 couldn't resist the urge to self-destruct\n");
2640                 } 
2641                 
2642                 if (stof(s2) > 2) // killcount > 2
2643                         print ("^1",s1,"^1 ended it all after a ",s2," kill spree\n");
2644         } else if(msg == MSG_KILL) {
2645                 w = DEATH_WEAPONOF(type);
2646                 if(WEP_VALID(w)) {
2647                         HUD_KillNotify_Push(s1, s2, 1, type);
2648                         if (alsoprint)
2649                                 print("^1", sprintf(Weapon_KillMessage(type), strcat(s2, "^1"), strcat(s1, "^1")), "\n"); // default order: victim, killer
2650                 }
2651                 else if(type == KILL_TEAM_RED || type == KILL_TEAM_BLUE || type == KILL_TEAM_SPREE) {
2652                         HUD_KillNotify_Push(s1, s2, 1, type);
2653                         if(alsoprint)
2654                         {
2655                                 if(gentle) {
2656                                         print ("^1", s1, "^1 took action against a team mate\n");
2657                                 } else {
2658                                         print ("^1", s1, "^1 mows down a team mate\n");
2659                                 }
2660                         }
2661                         if (stof(s2) > 2 && type == KILL_TEAM_SPREE) {
2662                                 if(gentle)
2663                                         print ("^1",s1,"^1 ended a ",s3," scoring spree by going against a team mate\n");
2664                                 else
2665                                         print ("^1",s1,"^1 ended a ",s3," kill spree by killing a team mate\n");
2666                         }
2667                         else if (stof(s2) > 2) {
2668                                 if(gentle)
2669                                         print ("^1",s1,"'s ^1",s3," scoring spree was ended by a team mate!\n");
2670                                 else
2671                                         print ("^1",s1,"'s ^1",s3," kill spree was ended by a team mate!\n");
2672                         }
2673                 }
2674                 else if(type == KILL_FIRST_BLOOD)
2675                         print("^1",s1, "^1 drew first blood", "\n");
2676                 else if (type == DEATH_TELEFRAG) {
2677                         HUD_KillNotify_Push(s1, s2, 1, DEATH_TELEFRAG);
2678                         if(gentle)
2679                                 print ("^1",s2, "^1 tried to occupy ", s1, "^1's teleport destination space\n");
2680                         else
2681                                 print ("^1",s2, "^1 was telefragged by ", s1, "\n");
2682                 }
2683                 else if (type == DEATH_DROWN) {
2684                         HUD_KillNotify_Push(s1, s2, 1, DEATH_DROWN);
2685                         if(alsoprint)
2686                                 print ("^1",s2, "^1 was drowned by ", s1, "\n");
2687                 }
2688                 else if (type == DEATH_SLIME) {
2689                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SLIME);
2690                         if(alsoprint)
2691                                 print ("^1",s2, "^1 was slimed by ", s1, "\n");
2692                 }
2693                 else if (type == DEATH_LAVA) {
2694                         HUD_KillNotify_Push(s1, s2, 1, DEATH_LAVA);
2695                         if(alsoprint)
2696                                 print ("^1",s2, "^1 was cooked by ", s1, "\n");
2697                 }
2698                 else if (type == DEATH_FALL) {
2699                         HUD_KillNotify_Push(s1, s2, 1, DEATH_FALL);
2700                         if(alsoprint)
2701                                 print ("^1",s2, "^1 was grounded by ", s1, "\n");
2702                 }
2703                 else if (type == DEATH_SHOOTING_STAR) {
2704                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SHOOTING_STAR);
2705                         if(alsoprint)
2706                                 print ("^1",s2, "^1 was shot into space by ", s1, "\n");
2707                 }
2708                 else if (type == DEATH_SWAMP) {
2709                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2710                         if(alsoprint)
2711                                 print ("^1",s2, "^1 was conserved by ", s1, "\n");
2712                 }
2713                 else if (type == DEATH_HURTTRIGGER)
2714                 {
2715                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2716                         if(alsoprint)
2717                                 print("^1",s2, "^1 was thrown into a world of hurt by ", s1, "\n");
2718                 } else if(type == DEATH_SBCRUSH) {
2719                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2720                         if(alsoprint)
2721                                 print ("^1",s2, "^1 was crushed by ^1", s1, "\n");
2722                 } else if(type == DEATH_SBMINIGUN) {
2723                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2724                         if(alsoprint)
2725                                 print ("^1",s2, "^1 got shredded by ^1", s1, "\n");
2726                 } else if(type == DEATH_SBROCKET) {
2727                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2728                         if(alsoprint)
2729                                 print ("^1",s2, "^1 was blased to bits by ^1", s1, "\n");
2730                 } else if(type == DEATH_SBBLOWUP) {
2731                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2732                         if(alsoprint)
2733                                 print ("^1",s2, "^1 got caught in the destruction of ^1", s1, "'s vehicle\n");
2734                 } else if(type == DEATH_WAKIGUN) {
2735                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2736                         if(alsoprint)
2737                                 print ("^1",s2, "^1 was bolted down by ^1", s1, "\n");
2738                 } else if(type == DEATH_WAKIROCKET) {
2739                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2740                         if(alsoprint)
2741                                 print ("^1",s2, "^1 could find no shelter from ^1", s1, "'s rockets\n");
2742                 } else if(type == DEATH_WAKIBLOWUP) {
2743                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2744                         if(alsoprint)
2745                                 print ("^1",s2, "^1 dies when ^1", s1, "'s wakizashi dies.\n");
2746                 } else if(type == DEATH_TURRET) {
2747                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2748                         if(alsoprint)
2749                                 print ("^1",s2, "^1 was pushed into the line of fire by ^1", s1, "\n");
2750                 } else if(type == DEATH_TOUCHEXPLODE) {
2751                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2752                         if(alsoprint)
2753                                 print ("^1",s2, "^1 was pushed into an accident by ^1", s1, "\n");
2754                 } else if(type == DEATH_CHEAT) {
2755                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2756                         if(alsoprint)
2757                                 print ("^1",s2, "^1 was unfairly eliminated by ^1", s1, "\n");
2758                 } else if (type == DEATH_FIRE) {
2759                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2760                         if(alsoprint)
2761                                 print ("^1",s2, "^1 was burnt to death by ^1", s1, "\n");
2762                 } else if (type == DEATH_CUSTOM) {
2763                         HUD_KillNotify_Push(s1, s2, 1, DEATH_CUSTOM);
2764                         if(alsoprint)
2765                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2766                 } else if (type == DEATH_HURTTRIGGER) {
2767                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2768                         if(alsoprint)
2769                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2770                 } else {
2771                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2772                         if(alsoprint)
2773                                 print ("^1",s2, "^1 was fragged by ", s1, "\n");
2774                 }
2775         } else if(msg == MSG_SPREE) {
2776                 if(type == KILL_END_SPREE) {
2777                         if(gentle)
2778                                 print ("^1",s1,"'s ^1", s2, " scoring spree was ended by ", s3, "\n");
2779                         else
2780                                 print ("^1",s1,"'s ^1", s2, " kill spree was ended by ", s3, "\n");
2781                 } else if(type == KILL_SPREE) {
2782                         if(gentle)
2783                                 print ("^1",s1,"^1 made ",s2," scores in a row\n");
2784                         else
2785                                 print ("^1",s1,"^1 has ",s2," frags in a row\n");
2786                 } else if(type == KILL_SPREE_3) {
2787                         if(gentle)
2788                                 print (s1,"^7 made a ^1TRIPLE SCORE\n");
2789                         else
2790                                 print (s1,"^7 made a ^1TRIPLE FRAG\n");
2791                 } else if(type == KILL_SPREE_5) {
2792                         if(gentle)
2793                                 print (s1,"^7 unleashes ^1SCORING RAGE\n");
2794                         else
2795                                 print (s1,"^7 unleashes ^1RAGE\n");
2796                 } else if(type == KILL_SPREE_10) {
2797                         if(gentle)
2798                                 print (s1,"^7 made ^1TEN SCORES IN A ROW!\n");
2799                         else
2800                                 print (s1,"^7 starts the ^1MASSACRE!\n");
2801                 } else if(type == KILL_SPREE_15) {
2802                         if(gentle)
2803                                 print (s1,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
2804                         else
2805                                 print (s1,"^7 executes ^1MAYHEM!\n");
2806                 } else if(type == KILL_SPREE_20) {
2807                         if(gentle)
2808                                 print (s1,"^7 made ^1TWENTY SCORES IN A ROW!\n");
2809                         else
2810                                 print (s1,"^7 is a ^1BERSERKER!\n");
2811                 } else if(type == KILL_SPREE_25) {
2812                         if(gentle)
2813                                 print (s1,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
2814                         else
2815                                 print (s1,"^7 inflicts ^1CARNAGE!\n");
2816                 } else if(type == KILL_SPREE_30) {
2817                         if(gentle)
2818                                 print (s1,"^7 made ^1THIRTY SCORES IN A ROW!\n");
2819                         else
2820                                 print (s1,"^7 unleashes ^1ARMAGEDDON!\n");
2821                 }
2822         } else if(msg == MSG_KILL_ACTION) { // wtf is this? isnt it basically the same as MSG_SUICIDE?
2823                 if (type == DEATH_DROWN) {
2824                         HUD_KillNotify_Push(s1, "", 0, DEATH_DROWN);
2825                         if(alsoprint)
2826                         {
2827                                 if(gentle)
2828                                         print ("^1",s1, "^1 was in the water for too long\n");
2829                                 else
2830                                         print ("^1",s1, "^1 drowned\n");
2831                         }
2832                 } else if (type == DEATH_SLIME) {
2833                         HUD_KillNotify_Push(s1, "", 0, DEATH_SLIME);
2834                         if(alsoprint)
2835                                 print ("^1",s1, "^1 was slimed\n");
2836                 } else if (type == DEATH_LAVA) {
2837                         HUD_KillNotify_Push(s1, "", 0, DEATH_LAVA);
2838                         if(alsoprint)
2839                         {
2840                                 if(gentle)
2841                                         print ("^1",s1, "^1 found a hot place\n");
2842                                 else
2843                                         print ("^1",s1, "^1 turned into hot slag\n");
2844                         }
2845                 } else if (type == DEATH_FALL) {
2846                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2847                         if(alsoprint)
2848                         {
2849                                 if(gentle)
2850                                         print ("^1",s1, "^1 tested gravity (and it worked)\n");
2851                                 else
2852                                         print ("^1",s1, "^1 hit the ground with a crunch\n");
2853                         }
2854                 } else if (type == DEATH_SHOOTING_STAR) {
2855                         HUD_KillNotify_Push(s1, "", 0, DEATH_SHOOTING_STAR);
2856                         if(alsoprint)
2857                                 print ("^1",s1, "^1 became a shooting star\n");
2858                 } else if (type == DEATH_SWAMP) {
2859                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2860                         if(alsoprint)
2861                         {
2862                                 if(gentle)
2863                                         print ("^1",s1, "^1 discovered a swamp\n");
2864                                 else
2865                                         print ("^1",s1, "^1 is now conserved for centuries to come\n");
2866                         }
2867                 } else if(type == DEATH_TURRET) {
2868                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2869                         if(alsoprint)
2870                                 print ("^1",s1, "^1 was mowed down by a turret \n");
2871                 } else if (type == DEATH_CUSTOM) {
2872                         HUD_KillNotify_Push(s1, "", 0, DEATH_CUSTOM);
2873                         if(alsoprint)
2874                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
2875                 } else if (type == DEATH_HURTTRIGGER) {
2876                         HUD_KillNotify_Push(s1, "", 0, DEATH_HURTTRIGGER);
2877                         if(alsoprint)
2878                                 print("^1", sprintf(s2, strcat(s1, "^1")), "\n");
2879                 } else if(type == DEATH_TOUCHEXPLODE) {
2880                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2881                         if(alsoprint)
2882                                 print ("^1",s1, "^1 died in an accident\n");
2883                 } else if(type == DEATH_CHEAT) {
2884                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2885                         if(alsoprint)
2886                                 print ("^1",s1, "^1 was unfairly eliminated\n");
2887                 } else if(type == DEATH_FIRE) {
2888                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2889                         if(alsoprint)
2890                         {
2891                                 if(gentle)
2892                                         print ("^1",s1, "^1 felt a little hot\n");
2893                                 else
2894                                         print ("^1",s1, "^1 burnt to death\n");
2895                                 }
2896                 } else {
2897                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2898                         if(alsoprint)
2899                         {
2900                                 if(gentle)
2901                                         print ("^1",s1, "^1 needs a restart\n");
2902                                 else
2903                                         print ("^1",s1, "^1 died\n");
2904                         }
2905                 }
2906         } else if(msg == MSG_KILL_ACTION_SPREE) {
2907                 if(gentle)
2908                         print ("^1",s1,"^1 needs a restart after a ",s2," scoring spree\n");
2909                 else
2910                         print ("^1",s1,"^1 died with a ",s2," kill spree\n");
2911         } else if(msg == MSG_INFO) {
2912                 if(type == INFO_GOTFLAG) { // here, s2 is the flag name
2913                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2914                         print(s1, "^7 got the ", s2, "\n");
2915                 } else if(type == INFO_LOSTFLAG) {
2916                         HUD_KillNotify_Push(s1, s2, 0, INFO_LOSTFLAG);
2917                         print(s1, "^7 lost the ", s2, "\n");
2918                 } else if(type == INFO_PICKUPFLAG) {
2919                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2920                         print(s1, "^7 picked up the ", s2, "\n");
2921                 } else if(type == INFO_RETURNFLAG) {
2922                         HUD_KillNotify_Push(s1, s2, 0, INFO_RETURNFLAG);
2923                         print(s1, "^7 returned the ", s2, "\n");
2924                 } else if(type == INFO_CAPTUREFLAG) {
2925                         HUD_KillNotify_Push(s1, s2, 0, INFO_CAPTUREFLAG);
2926                         print(s1, "^7 captured the ", s2, s3, "\n");
2927                 }
2928         }
2929 }
2930
2931 #define DAMAGE_CENTERPRINT_SPACER NEWLINES
2932
2933 void HUD_Centerprint(string s1, string s2, float type, float msg)
2934 {
2935         float gentle;
2936         gentle = (autocvar_cl_gentle || autocvar_cl_gentle_messages);
2937         if(msg == MSG_SUICIDE) {
2938                 if (type == DEATH_TEAMCHANGE) {
2939                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You are now on: ", s1));
2940                 } else if (type == DEATH_AUTOTEAMCHANGE) {
2941                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You have been moved into a different team to improve team balance\nYou are now on: ", s1));
2942                 } else if (type == DEATH_CAMP) {
2943                         if(gentle)
2944                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Reconsider your tactics, camper!"));
2945                         else
2946                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Die camper!"));
2947                 } else if (type == DEATH_NOAMMO) {
2948                         if(gentle)
2949                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You are reinserted into the game for running out of ammo..."));
2950                         else
2951                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were killed for running out of ammo..."));
2952                 } else if (type == DEATH_ROT) {
2953                         if(gentle)
2954                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to preserve your health"));
2955                         else
2956                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You grew too old without taking your medicine"));
2957                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2958                         if(gentle)
2959                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't go against team mates!"));
2960                         else
2961                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't shoot your team mates!"));
2962                 } else if (type == DEATH_QUIET) {
2963                         // do nothing
2964                 } else { // generic message
2965                         if(gentle)
2966                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to be more careful!"));
2967                         else
2968                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You killed your own dumb self!"));
2969                 }
2970         } else if(msg == MSG_KILL) {
2971                 if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2972                         if(gentle) {
2973                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You went against", s1, ",a team mate!"));
2974                         } else {
2975                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You fragged ", s1, ", a team mate!"));
2976                         }
2977                 } else if (type == KILL_FIRST_BLOOD) {
2978                         if(gentle) {
2979                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First score"));
2980                         } else {
2981                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First blood"));
2982                         }
2983                 } else if (type == KILL_FIRST_VICTIM) {
2984                         if(gentle) {
2985                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First casualty"));
2986                         } else {
2987                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First victim"));
2988                         }
2989                 } else if (type == KILL_TYPEFRAG) { // s2 contains "advanced kill messages" such as ping, handicap...
2990                         if(gentle) {
2991                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You scored against ^7", s1, "^1 who was typing!", s2));
2992                         } else {
2993                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You typefragged ^7", s1, s2));
2994                         }
2995                 } else if (type == KILL_TYPEFRAGGED) {
2996                         if(gentle) {
2997                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, "^1 while you were typing!", s2));
2998                         } else {
2999                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were typefragged by ^7", s1, s2));
3000                         }
3001                 } else if (type == KILL_FRAG) {
3002                         if(gentle) {
3003                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You scored against ^7", s1, s2));
3004                         } else {
3005                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You fragged ^7", s1, s2));
3006                         }
3007                 } else { // generic message
3008                         if(gentle) {
3009                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, s2));
3010                         } else {
3011                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were fragged by ^7", s1, s2));
3012                         }
3013                 }
3014         } else if(msg == MSG_KILL_ACTION) {
3015                 // TODO: invent more centerprints here?
3016                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
3017         }
3018 }
3019
3020 void HUD_Notify (void)
3021 {
3022         if(!autocvar_hud_panel_notify && !autocvar__hud_configure)
3023                 return;
3024
3025         active_panel = HUD_PANEL_NOTIFY;
3026         HUD_Panel_UpdateCvars(notify);
3027         vector pos, mySize;
3028         pos = panel_pos;
3029         mySize = panel_size;
3030
3031         HUD_Panel_DrawBg(1);
3032         if(panel_bg_padding)
3033         {
3034                 pos += '1 1 0' * panel_bg_padding;
3035                 mySize -= '2 2 0' * panel_bg_padding;
3036         }
3037
3038         float entries, height;
3039         entries = bound(1, floor(10 * mySize_y/mySize_x), 10);
3040         height = mySize_y/entries;
3041         
3042         vector fontsize;
3043         fontsize = '0.5 0.5 0' * height;
3044
3045         float a;
3046         float when;
3047         when = autocvar_hud_panel_notify_time;
3048         float fadetime;
3049         fadetime = autocvar_hud_panel_notify_fadetime;
3050
3051         string s;
3052
3053         vector pos_attacker, pos_victim;
3054         vector weap_pos;
3055         float width_attacker;
3056         string attacker, victim;
3057
3058         float i, j, w;
3059         for(j = 0; j < entries; ++j)
3060         {
3061                 s = "";
3062                 if(autocvar_hud_panel_notify_flip)
3063                         i = j;
3064                 else // rather nasty hack for ordering items from the bottom up
3065                         i = entries - j - 1;
3066
3067                 if(fadetime)
3068                 {
3069                         if(killnotify_times[j] + when > time)
3070                                 a = 1;
3071                         else
3072                                 a = bound(0, (killnotify_times[j] + when + fadetime - time) / fadetime, 1);
3073                 }
3074                 else
3075                 {
3076                         if(killnotify_times[j] + when > time)
3077                                 a = 1;
3078                         else
3079                                 a = 0;
3080                 }
3081
3082                 w = -1;
3083                 w = DEATH_WEAPONOF(killnotify_deathtype[j]);
3084
3085                 // TODO: maybe print in team colors?
3086                 //
3087                 // Y [used by] X
3088                 if(killnotify_actiontype[j] == 0 && !autocvar__hud_configure) 
3089                 {
3090                         attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3091                         pos_attacker = pos + eX * (0.52 * mySize_x + height) + eY * (0.5 * fontsize_y + i * height);
3092                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
3093
3094                         if(killnotify_deathtype[j] == DEATH_GENERIC)
3095                         {
3096                                 s = "notify_death";
3097                         }
3098                         else if(killnotify_deathtype[j] == DEATH_NOAMMO)
3099                         {
3100                                 s = "notify_outofammo";
3101                         }
3102                         else if(killnotify_deathtype[j] == DEATH_KILL)
3103                         {
3104                                 s = "notify_selfkill";
3105                         }
3106                         else if(killnotify_deathtype[j] == DEATH_CAMP)
3107                         {
3108                                 s = "notify_camping";
3109                         }
3110                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
3111                         {
3112                                 s = "notify_teamkill_red";
3113                         }
3114                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
3115                         {
3116                                 s = "notify_teamkill_blue";
3117                         }
3118                         else if(killnotify_deathtype[j] == DEATH_DROWN)
3119                         {
3120                                 s = "notify_water";
3121                         }
3122                         else if(killnotify_deathtype[j] == DEATH_SLIME)
3123                         {
3124                                 s = "notify_slime";
3125                         }
3126                         else if(killnotify_deathtype[j] == DEATH_LAVA)
3127                         {
3128                                 s = "notify_lava";
3129                         }
3130                         else if(killnotify_deathtype[j] == DEATH_FALL)
3131                         {
3132                                 s = "notify_fall";
3133                         }
3134                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
3135                         {
3136                                 s = "notify_shootingstar";
3137                         }
3138                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM)
3139                         {
3140                                 s = "notify_death";
3141                         }
3142                         else if(killnotify_deathtype[j] == INFO_GOTFLAG)
3143                         {
3144                                 if(killnotify_victims[j] == "^1RED^7 flag")
3145                                 {
3146                                         s = "notify_red_taken";
3147                                 }
3148                                 else
3149                                 {
3150                                         s = "notify_blue_taken";
3151                                 }
3152                         }
3153                         else if(killnotify_deathtype[j] == INFO_RETURNFLAG)
3154                         {
3155                                 if(killnotify_victims[j] == "^1RED^7 flag")
3156                                 {
3157                                         s = "notify_red_returned";
3158                                 }
3159                                 else
3160                                 {
3161                                         s = "notify_blue_returned";
3162                                 }
3163                         }
3164                         else if(killnotify_deathtype[j] == INFO_LOSTFLAG)
3165                         {
3166                                 if(killnotify_victims[j] == "^1RED^7 flag")
3167                                 {
3168                                         s = "notify_red_lost";
3169                                 }
3170                                 else
3171                                 {
3172                                         s = "notify_blue_lost";
3173                                 }
3174                         }
3175                         else if(killnotify_deathtype[j] == INFO_CAPTUREFLAG)
3176                         {
3177                                 if(killnotify_victims[j] == "^1RED^7 flag")
3178                                 {
3179                                         s = "notify_red_captured";
3180                                 }
3181                                 else
3182                                 {
3183                                         s = "notify_blue_captured";
3184                                 }
3185                         }
3186                         if(s != "" && a)
3187                         {
3188                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3189                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3190                         }
3191                 }
3192                 // X [did action to] Y
3193                 else
3194                 {
3195                         if(autocvar__hud_configure)
3196                         {
3197                                 attacker = textShortenToWidth("Player1", 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3198                                 victim = textShortenToWidth("Player2", 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3199                                 a = bound(0, (when - j) / 4, 1);
3200                         }
3201                         else
3202                         {
3203                                 attacker = textShortenToWidth(killnotify_attackers[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3204                                 victim = textShortenToWidth(killnotify_victims[j], 0.48 * mySize_x - height, fontsize, stringwidth_colors);
3205                         }
3206                         width_attacker = stringwidth(attacker, TRUE, fontsize);
3207                         pos_attacker = pos + eX * (0.48 * mySize_x - height - width_attacker) + eY * (0.5 * fontsize_y + i * height);
3208                         pos_victim = pos + eX * (0.52 * mySize_x + height) + eY * (0.5 * fontsize_y + i * height);
3209                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
3210
3211                         if(autocvar__hud_configure) // example actions for config mode
3212                         {
3213                                 s = "weaponelectro";
3214                         }
3215                         else if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_LASER)
3216                         {
3217                                 s = "notify_melee_laser";
3218                         }
3219                         else if(killnotify_deathtype[j] & HITTYPE_SECONDARY && w == WEP_SHOTGUN)
3220                         {
3221                                 s = "notify_melee_shotgun";
3222                         }
3223                         else if(WEP_VALID(w))
3224                         {
3225                                 self = get_weaponinfo(w);
3226                                 s = strcat("weapon", self.netname);
3227                         }
3228                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
3229                         {
3230                                 s = "notify_teamkill_red";
3231                         }
3232                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
3233                         {
3234                                 s = "notify_teamkill_red";
3235                         }
3236                         else if(killnotify_deathtype[j] == DEATH_TELEFRAG)
3237                         {
3238                                 s = "notify_telefrag";
3239                         }
3240                         else if(killnotify_deathtype[j] == DEATH_DROWN)
3241                         {
3242                                 s = "notify_water";
3243                         }
3244                         else if(killnotify_deathtype[j] == DEATH_SLIME)
3245                         {
3246                                 s = "notify_slime";
3247                         }
3248                         else if(killnotify_deathtype[j] == DEATH_LAVA)
3249                         {
3250                                 s = "notify_lava";
3251                         }
3252                         else if(killnotify_deathtype[j] == DEATH_FALL)
3253                         {
3254                                 s = "notify_fall";
3255                         }
3256                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
3257                         {
3258                                 s = "notify_shootingstar";
3259                         }
3260                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM) // DEATH_CUSTOM is also void, right?
3261                         {
3262                                 s = "notify_void";
3263                         }
3264                         if(s != "" && a)
3265                         {
3266                                 drawpic_aspect_skin(weap_pos, s, '2 1 0' * height, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
3267                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3268                                 drawcolorcodedstring(pos_victim, victim, fontsize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
3269                         }
3270                 }
3271         }
3272 }
3273
3274 // Timer (#5)
3275 //
3276 // TODO: macro
3277 string seconds_tostring(float sec)
3278 {
3279         float minutes;
3280         minutes = floor(sec / 60);
3281
3282         sec -= minutes * 60;
3283
3284         string s;
3285         s = ftos(100 + sec);
3286
3287         return strcat(ftos(minutes), ":", substring(s, 1, 3));
3288 }
3289
3290 void HUD_Timer(void)
3291 {
3292         if(!autocvar_hud_panel_timer && !autocvar__hud_configure)
3293                 return;
3294
3295         active_panel = HUD_PANEL_TIMER;
3296         HUD_Panel_UpdateCvars(timer);
3297         vector pos, mySize;
3298         pos = panel_pos;
3299         mySize = panel_size;
3300
3301         HUD_Panel_DrawBg(1);
3302         if(panel_bg_padding)
3303         {
3304                 pos += '1 1 0' * panel_bg_padding;
3305                 mySize -= '2 2 0' * panel_bg_padding;
3306         }
3307
3308         string timer;
3309         float timelimit, elapsedTime, timeleft, minutesLeft;
3310
3311         timelimit = getstatf(STAT_TIMELIMIT);
3312
3313         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
3314         timeleft = ceil(timeleft);
3315
3316         minutesLeft = floor(timeleft / 60);
3317
3318         vector timer_color;
3319         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
3320                 timer_color = '1 1 1'; //white
3321         else if(minutesLeft >= 1)
3322                 timer_color = '1 1 0'; //yellow
3323         else
3324                 timer_color = '1 0 0'; //red
3325
3326         if (autocvar_hud_panel_timer_increment || timelimit == 0 || warmup_stage) {
3327                 if (time < getstatf(STAT_GAMESTARTTIME)) {
3328                         //while restart is still active, show 00:00
3329                         timer = seconds_tostring(0);
3330                 } else {
3331                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
3332                         timer = seconds_tostring(elapsedTime);
3333                 }
3334         } else {
3335                 timer = seconds_tostring(timeleft);
3336         }
3337
3338         drawfont = hud_bigfont;
3339         drawstring_aspect(pos, timer, mySize, timer_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3340         drawfont = hud_font;
3341 }
3342
3343 // Radar (#6)
3344 //
3345 void HUD_Radar(void)
3346 {
3347         if ((autocvar_hud_panel_radar == 0 || (autocvar_hud_panel_radar != 2 && !teamplay)) && !autocvar__hud_configure)
3348                 return;
3349
3350         active_panel = HUD_PANEL_RADAR;
3351         HUD_Panel_UpdateCvars(radar);
3352         vector pos, mySize;
3353         pos = panel_pos;
3354         mySize = panel_size;
3355
3356         HUD_Panel_DrawBg(1);
3357         if(panel_bg_padding)
3358         {
3359                 pos += '1 1 0' * panel_bg_padding;
3360                 mySize -= '2 2 0' * panel_bg_padding;
3361         }
3362
3363         local float color1, color2; // color already declared as a global in hud.qc
3364         local vector rgb;
3365         local entity tm;
3366         float scale2d, normalsize, bigsize;
3367         float f;
3368
3369         teamradar_origin2d = pos + 0.5 * mySize;
3370         teamradar_size2d = mySize;
3371
3372         if(minimapname == "")
3373                 return;
3374
3375         teamradar_loadcvars();
3376
3377         switch(hud_panel_radar_zoommode)
3378         {
3379                 default:
3380                 case 0:
3381                         f = current_zoomfraction;
3382                         break;
3383                 case 1:
3384                         f = 1 - current_zoomfraction;
3385                         break;
3386                 case 2:
3387                         f = 0;
3388                         break;
3389                 case 3:
3390                         f = 1;
3391                         break;
3392         }
3393
3394         switch(hud_panel_radar_rotation)
3395         {
3396                 case 0:
3397                         teamradar_angle = view_angles_y - 90;
3398                         break;
3399                 default:
3400                         teamradar_angle = 90 * hud_panel_radar_rotation;
3401                         break;
3402         }
3403
3404         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
3405         teamradar_size2d = mySize;
3406
3407         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
3408
3409         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
3410         if(hud_panel_radar_rotation == 0)
3411         {
3412                 // max-min distance must fit the radar in any rotation
3413                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
3414         }
3415         else
3416         {
3417                 vector c0, c1, c2, c3, span;
3418                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
3419                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
3420                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
3421                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
3422                 span = '0 0 0';
3423                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
3424                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
3425
3426                 // max-min distance must fit the radar in x=x, y=y
3427                 bigsize = min(
3428                         teamradar_size2d_x * scale2d / (1.05 * span_x),
3429                         teamradar_size2d_y * scale2d / (1.05 * span_y)
3430                 );
3431         }
3432
3433         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
3434         if(bigsize > normalsize)
3435                 normalsize = bigsize;
3436
3437         teamradar_size =
3438                   f * bigsize
3439                 + (1 - f) * normalsize;
3440         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
3441                   f * (mi_min + mi_max) * 0.5
3442                 + (1 - f) * view_origin);
3443
3444         color1 = GetPlayerColor(player_localentnum-1);
3445         rgb = GetTeamRGB(color1);
3446
3447         drawsetcliparea(
3448                 pos_x,
3449                 pos_y,
3450                 mySize_x,
3451                 mySize_y
3452         );
3453
3454         draw_teamradar_background(hud_panel_radar_foreground_alpha);
3455
3456         for(tm = world; (tm = find(tm, classname, "radarlink")); )
3457                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
3458         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
3459                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, panel_fg_alpha);
3460         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
3461         {
3462                 color2 = GetPlayerColor(tm.sv_entnum);
3463                 //if(color == COLOR_SPECTATOR || color == color2)
3464                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
3465         }
3466         draw_teamradar_player(view_origin, view_angles, '1 1 1');
3467
3468         drawresetcliparea();
3469 };
3470
3471 // Score (#7)
3472 //
3473 void HUD_Score(void)
3474 {
3475         if(!autocvar_hud_panel_score && !autocvar__hud_configure)
3476                 return;
3477
3478         active_panel = HUD_PANEL_SCORE;
3479         HUD_Panel_UpdateCvars(score);
3480         vector pos, mySize;
3481         pos = panel_pos;
3482         mySize = panel_size;
3483
3484         HUD_Panel_DrawBg(1);
3485         if(panel_bg_padding)
3486         {
3487                 pos += '1 1 0' * panel_bg_padding;
3488                 mySize -= '2 2 0' * panel_bg_padding;
3489         }
3490
3491         float score, distribution, leader;
3492         vector distribution_color;
3493         entity tm, pl, me;
3494         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
3495
3496         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
3497                 string timer, distrtimer;
3498
3499                 pl = players.sort_next;
3500                 if(pl == me)
3501                         pl = pl.sort_next;
3502                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
3503                         if(pl.scores[ps_primary] == 0)
3504                                 pl = world;
3505
3506                 score = me.(scores[ps_primary]);
3507                 timer = TIME_ENCODED_TOSTRING(score);
3508
3509                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
3510                         // distribution display
3511                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3512
3513                         distrtimer = ftos(distribution/pow(10, TIME_DECIMALS));
3514
3515                         if (distribution <= 0) {
3516                                 distribution_color = '0 1 0';
3517                         }
3518                         else {
3519                                 distribution_color = '1 0 0';
3520                         }
3521                         drawstring_aspect(pos + eX * 0.75 * mySize_x, distrtimer, eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3522                 }
3523                 // race record display
3524                 if (distribution <= 0)
3525                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3526                 drawfont = hud_bigfont;
3527                 drawstring_aspect(pos, timer, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3528                 drawfont = hud_font;
3529         } else if (!teamplay) { // non-teamgames
3530                 // me vector := [team/connected frags id]
3531                 pl = players.sort_next;
3532                 if(pl == me)
3533                         pl = pl.sort_next;
3534
3535                 if(autocvar__hud_configure)
3536                         distribution = 42;
3537                 else if(pl)
3538                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3539                 else
3540                         distribution = 0;
3541
3542                 score = me.(scores[ps_primary]);
3543                 if(autocvar__hud_configure)
3544                         score = 123;
3545
3546                 if(distribution >= 5) {
3547                         distribution_color = eY;
3548                         leader = 1;
3549                 } else if(distribution >= 0) {
3550                         distribution_color = '1 1 1';
3551                         leader = 1;
3552                 } else if(distribution >= -5)
3553                         distribution_color = '1 1 0';
3554                 else
3555                         distribution_color = eX;
3556
3557                 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);
3558                 if (leader)
3559                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3560                 drawfont = hud_bigfont;
3561                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
3562                 drawfont = hud_font;
3563         } else { // teamgames
3564                 float max_fragcount;
3565                 max_fragcount = -99;
3566
3567                 float teamnum;
3568                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
3569                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !autocvar__hud_configure)) // no players? don't display
3570                                 continue;
3571                         score = tm.(teamscores[ts_primary]);
3572                         if(autocvar__hud_configure)
3573                                 score = 123;
3574                         leader = 0;
3575                         
3576                         if (score > max_fragcount)
3577                                 max_fragcount = score;
3578
3579                         if(tm.team == myteam) {
3580                                 if (max_fragcount == score)
3581                                         leader = 1;
3582                                 if (leader)
3583                                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3584                                 drawfont = hud_bigfont;
3585                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
3586                                 drawfont = hud_font;
3587                         } else {
3588                                 if (max_fragcount == score)
3589                                         leader = 1;
3590                                 if (leader)
3591                                         HUD_Panel_DrawHighlight(pos + eX * 0.75 * mySize_x + eY * (1/3) * teamnum * mySize_y, eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
3592                                 drawstring_aspect(pos + eX * 0.75 * mySize_x + eY * (1/3) * teamnum * mySize_y, ftos(score), eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, GetTeamRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
3593                                 teamnum += 1;
3594                         }
3595                 }
3596         }
3597 }
3598
3599 // Race timer (#8)
3600 //
3601 void HUD_RaceTimer (void) {
3602         if(!autocvar_hud_panel_racetimer && !(gametype == GAME_RACE || gametype == GAME_CTS) && !autocvar__hud_configure)
3603                 return;
3604
3605         active_panel = HUD_PANEL_RACETIMER;
3606         HUD_Panel_UpdateCvars(racetimer);
3607         vector pos, mySize;
3608         pos = panel_pos;
3609         mySize = panel_size;
3610
3611         HUD_Panel_DrawBg(1);
3612         if(panel_bg_padding)
3613         {
3614                 pos += '1 1 0' * panel_bg_padding;
3615                 mySize -= '2 2 0' * panel_bg_padding;
3616         }
3617
3618         // always force 4:1 aspect
3619         vector newSize;
3620         if(mySize_x/mySize_y > 4)
3621         {
3622                 newSize_x = 4 * mySize_y;
3623                 newSize_y = mySize_y;
3624
3625                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3626         }
3627         else
3628         {
3629                 newSize_y = 1/4 * mySize_x;
3630                 newSize_x = mySize_x;
3631
3632                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3633         }
3634         mySize = newSize;
3635
3636         drawfont = hud_bigfont;
3637         float a, t;
3638         string s, forcetime;
3639
3640         if(autocvar__hud_configure)
3641         {
3642                 s = "0:13:37";
3643                 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);
3644                 s = "^1Intermediate 1 (+15.42)";
3645                 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);
3646                 s = strcat("^1PENALTY: ", ftos_decimals(20 * 0.1, 1), " (missing a checkpoint)");
3647                 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);
3648         }
3649         else if(race_checkpointtime)
3650         {
3651                 a = bound(0, 2 - (time - race_checkpointtime), 1);
3652                 s = "";
3653                 forcetime = "";
3654                 if(a > 0) // just hit a checkpoint?
3655                 {
3656                         if(race_checkpoint != 254)
3657                         {
3658                                 if(race_time && race_previousbesttime)
3659                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
3660                                 else
3661                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
3662                                 if(race_time)
3663                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
3664                         }
3665                 }
3666                 else
3667                 {
3668                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
3669                         {
3670                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
3671                                 if(a > 0) // next one?
3672                                 {
3673                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
3674                                 }
3675                         }
3676                 }
3677
3678                 if(s != "" && a > 0)
3679                 {
3680                         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);
3681                 }
3682
3683                 if(race_penaltytime)
3684                 {
3685                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
3686                         if(a > 0)
3687                         {
3688                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
3689                                 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);
3690                         }
3691                 }
3692
3693                 if(forcetime != "")
3694                 {
3695                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
3696                         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);
3697                 }
3698                 else
3699                         a = 1;
3700
3701                 if(race_laptime && race_checkpoint != 255)
3702                 {
3703                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
3704                         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);
3705                 }
3706         }
3707         else
3708         {
3709                 if(race_mycheckpointtime)
3710                 {
3711                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
3712                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
3713                         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);
3714                 }
3715                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
3716                 {
3717                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
3718                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
3719                         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);
3720                 }
3721
3722                 if(race_penaltytime && !race_penaltyaccumulator)
3723                 {
3724                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
3725                         a = bound(0, (1 + t - time), 1);
3726                         if(a > 0)
3727                         {
3728                                 if(time < t)
3729                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
3730                                 else
3731                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
3732                                 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);
3733                         }
3734                 }
3735         }
3736
3737         drawfont = hud_font;
3738 }
3739
3740 // Vote window (#9)
3741 //
3742 float vote_yescount;
3743 float vote_nocount;
3744 float vote_needed;
3745 float vote_highlighted; // currently selected vote
3746
3747 float vote_active; // is there an active vote?
3748 float vote_prev; // previous state of vote_active to check for a change
3749 float vote_alpha;
3750 float vote_change; // "time" when vote_active changed
3751
3752 void HUD_VoteWindow(void) 
3753 {
3754         if(!autocvar_hud_panel_vote && !autocvar__hud_configure)
3755                 return;
3756
3757         active_panel = HUD_PANEL_VOTE;
3758         HUD_Panel_UpdateCvars(vote);
3759         vector pos, mySize;
3760         pos = panel_pos;
3761         mySize = panel_size;
3762
3763         if(!autocvar__hud_configure)
3764         {
3765                 panel_fg_alpha = autocvar_hud_panel_fg_alpha;
3766                 panel_bg_alpha_str = autocvar_hud_panel_vote_bg_alpha;
3767
3768                 if(panel_bg_alpha_str == "") {
3769                         panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);
3770                 }
3771                 panel_bg_alpha = stof(panel_bg_alpha_str);
3772         }
3773
3774         string s;
3775         float a;
3776         if(vote_active != vote_prev) {
3777                 vote_change = time;
3778                 vote_prev = vote_active;
3779         }
3780
3781         if(vote_active || autocvar__hud_configure)
3782                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
3783         else
3784                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
3785
3786         if(autocvar__hud_configure)
3787         {
3788                 vote_yescount = 3;
3789                 vote_nocount = 2;
3790                 vote_needed = 4;
3791         }
3792
3793         if(!vote_alpha)
3794                 return;
3795
3796         a = panel_bg_alpha * vote_alpha * bound(autocvar_hud_panel_vote_alreadyvoted_alpha, 1 - vote_highlighted, 1);
3797         HUD_Panel_DrawBg(a);
3798         a = panel_fg_alpha * vote_alpha * bound(autocvar_hud_panel_vote_alreadyvoted_alpha, 1 - vote_highlighted, 1);
3799         if(panel_bg_padding)
3800         {
3801                 pos += '1 1 0' * panel_bg_padding;
3802                 mySize -= '2 2 0' * panel_bg_padding;
3803         }
3804
3805         // always force 3:1 aspect
3806         vector newSize;
3807         if(mySize_x/mySize_y > 3)
3808         {
3809                 newSize_x = 3 * mySize_y;
3810                 newSize_y = mySize_y;
3811
3812                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3813         }
3814         else
3815         {
3816                 newSize_y = 1/3 * mySize_x;
3817                 newSize_x = mySize_x;
3818
3819                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3820         }
3821         mySize = newSize;
3822
3823         s = "A vote has been called for:";
3824         drawstring_aspect(pos, s, eX * mySize_x + eY * (2/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3825         s = textShortenToWidth(vote_called_vote, mySize_x, '1 1 0' * mySize_y * (1.75/8), stringwidth_colors);
3826         if(autocvar__hud_configure)
3827                 s = "^1Configure the HUD";
3828         drawcolorcodedstring_aspect(pos + eY * (2/8) * mySize_y, s, eX * mySize_x + eY * (1.75/8) * mySize_y, a, DRAWFLAG_NORMAL);
3829
3830         // print the yes/no counts
3831         s = strcat("Yes (", getcommandkey("vyes", "vyes"), "): ", ftos(vote_yescount));
3832         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);
3833         s = strcat("No (", getcommandkey("vno", "vno"), "): ", ftos(vote_nocount));
3834         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);
3835
3836         // draw the progress bar backgrounds
3837         drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_back", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3838
3839         // draw the highlights
3840         if(vote_highlighted == 1) {
3841                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
3842                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3843         }
3844         else if(vote_highlighted == 2) {
3845                 drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
3846                 drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_voted", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3847         }
3848
3849         // draw the progress bars
3850         drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
3851         drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3852
3853         drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
3854         drawpic_skin(pos + eY * (5/8) * mySize_y, "voteprogress_prog", eX * mySize_x + eY * (3/8) * mySize_y, '1 1 1', a, DRAWFLAG_NORMAL);
3855
3856         drawresetcliparea();
3857
3858         if(!vote_active) {
3859                 vote_highlighted = 0;
3860         }
3861 }
3862
3863 // Mod icons panel (#10)
3864 //
3865
3866 float mod_active; // is there any active mod icon?
3867
3868 // CTF HUD modicon section
3869 float redflag_prevframe, blueflag_prevframe; // status during previous frame
3870 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
3871 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
3872
3873 void HUD_Mod_CTF_Reset(void)
3874 {
3875         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
3876 }
3877
3878 void HUD_Mod_CTF(vector pos, vector mySize)
3879 {
3880         vector redflag_pos, blueflag_pos;
3881         vector flag_size;
3882         float f; // every function should have that
3883
3884         float redflag, blueflag; // current status
3885         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
3886         float stat_items;
3887
3888         stat_items = getstati(STAT_ITEMS);
3889         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
3890         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
3891         
3892         if(redflag || blueflag)
3893                 mod_active = 1;
3894         else
3895                 mod_active = 0;
3896
3897         if(autocvar__hud_configure)
3898         {
3899                 redflag = 1;
3900                 blueflag = 2;
3901         }
3902
3903         // when status CHANGES, set old status into prevstatus and current status into status
3904         if (redflag != redflag_prevframe)
3905         {
3906                 redflag_statuschange_time = time;
3907                 redflag_prevstatus = redflag_prevframe;
3908                 redflag_prevframe = redflag;
3909         }
3910
3911         if (blueflag != blueflag_prevframe)
3912         {
3913                 blueflag_statuschange_time = time;
3914                 blueflag_prevstatus = blueflag_prevframe;
3915                 blueflag_prevframe = blueflag;
3916         }
3917
3918         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
3919         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
3920
3921         float BLINK_FACTOR = 0.15;
3922         float BLINK_BASE = 0.85;
3923         // note:
3924         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
3925         // thus
3926         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
3927         // ensure RMS == 1
3928         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
3929
3930         string red_icon, red_icon_prevstatus;
3931         float red_alpha, red_alpha_prevstatus;
3932         red_alpha = red_alpha_prevstatus = 1;
3933         switch(redflag) {
3934                 case 1: red_icon = "flag_red_taken"; break;
3935                 case 2: red_icon = "flag_red_lost"; break;
3936                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3937                 default:
3938                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
3939                                 red_icon = "flag_red_shielded";
3940                         else
3941                                 red_icon = string_null;
3942                         break;
3943         }
3944         switch(redflag_prevstatus) {
3945                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
3946                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
3947                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3948                 default:
3949                         if(redflag == 3)
3950                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
3951                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
3952                                 red_icon_prevstatus = "flag_red_shielded";
3953                         else
3954                                 red_icon_prevstatus = string_null;
3955                         break;
3956         }
3957
3958         string blue_icon, blue_icon_prevstatus;
3959         float blue_alpha, blue_alpha_prevstatus;
3960         blue_alpha = blue_alpha_prevstatus = 1;
3961         switch(blueflag) {
3962                 case 1: blue_icon = "flag_blue_taken"; break;
3963                 case 2: blue_icon = "flag_blue_lost"; break;
3964                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3965                 default:
3966                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
3967                                 blue_icon = "flag_blue_shielded";
3968                         else
3969                                 blue_icon = string_null;
3970                         break;
3971         }
3972         switch(blueflag_prevstatus) {
3973                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
3974                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
3975                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3976                 default:
3977                         if(blueflag == 3)
3978                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
3979                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
3980                                 blue_icon_prevstatus = "flag_blue_shielded";
3981                         else
3982                                 blue_icon_prevstatus = string_null;
3983                         break;
3984         }
3985
3986         if(mySize_x > mySize_y) {
3987                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
3988                         redflag_pos = pos;
3989                         blueflag_pos = pos + eX * 0.5 * mySize_x;
3990                 } else {
3991                         blueflag_pos = pos;
3992                         redflag_pos = pos + eX * 0.5 * mySize_x;
3993                 }
3994                 flag_size = eX * 0.5 * mySize_x + eY * mySize_y;
3995         } else {
3996                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
3997                         redflag_pos = pos;
3998                         blueflag_pos = pos + eY * 0.5 * mySize_y;
3999                 } else {
4000                         blueflag_pos = pos;
4001                         redflag_pos = pos + eY * 0.5 * mySize_y;
4002                 }
4003                 flag_size = eY * 0.5 * mySize_y + eX * mySize_x;
4004         }
4005
4006         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
4007         if(red_icon_prevstatus && f < 1)
4008                 drawpic_aspect_skin_expanding(redflag_pos, red_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
4009         if(red_icon)
4010                 drawpic_aspect_skin(redflag_pos, red_icon, flag_size, '1 1 1', panel_fg_alpha * red_alpha * f, DRAWFLAG_NORMAL);
4011
4012         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
4013         if(blue_icon_prevstatus && f < 1)
4014                 drawpic_aspect_skin_expanding(blueflag_pos, blue_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
4015         if(blue_icon)
4016                 drawpic_aspect_skin(blueflag_pos, blue_icon, flag_size, '1 1 1', panel_fg_alpha * blue_alpha * f, DRAWFLAG_NORMAL);
4017 }
4018
4019 // Keyhunt HUD modicon section
4020 float kh_runheretime;
4021
4022 void HUD_Mod_KH_Reset(void)
4023 {
4024         kh_runheretime = 0;
4025 }
4026
4027 void HUD_Mod_KH(vector pos, vector mySize)
4028 {
4029         mod_active = 1; // keyhunt should never hide the mod icons panel
4030         float kh_keys;
4031         float keyteam;
4032         float a, aa;
4033         vector p, pa, kh_size, kh_asize;
4034
4035         kh_keys = getstati(STAT_KH_KEYS);
4036
4037         p_x = pos_x;
4038         if(mySize_x > mySize_y)
4039         {
4040                 p_y = pos_y + 0.25 * mySize_y;
4041                 pa = p - eY * 0.25 * mySize_y;
4042
4043                 kh_size_x = mySize_x * 0.25;
4044                 kh_size_y = 0.75 * mySize_y;
4045                 kh_asize_x = mySize_x * 0.25;
4046                 kh_asize_y = mySize_y * 0.25;
4047         }
4048         else
4049         {
4050                 p_y = pos_y + 0.125 * mySize_y;
4051                 pa = p - eY * 0.125 * mySize_y;
4052
4053                 kh_size_x = mySize_x * 0.5;
4054                 kh_size_y = 0.375 * mySize_y;
4055                 kh_asize_x = mySize_x * 0.5;
4056                 kh_asize_y = mySize_y * 0.125;
4057         }
4058
4059         float i, key;
4060
4061         float keycount;
4062         keycount = 0;
4063         for(i = 0; i < 4; ++i)
4064         {
4065                 key = floor(kh_keys / pow(32, i)) & 31;
4066                 keyteam = key - 1;
4067                 if(keyteam == 30 && keycount <= 4)
4068                         keycount += 4;
4069                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
4070                         keycount += 1;
4071         }
4072
4073         // this yields 8 exactly if "RUN HERE" shows
4074
4075         if(keycount == 8)
4076         {
4077                 if(!kh_runheretime)
4078                         kh_runheretime = time;
4079                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
4080         }
4081         else
4082                 kh_runheretime = 0;
4083
4084         for(i = 0; i < 4; ++i)
4085         {
4086                 key = floor(kh_keys / pow(32, i)) & 31;
4087                 keyteam = key - 1;
4088                 switch(keyteam)
4089                 {
4090                         case 30: // my key
4091                                 keyteam = myteam;
4092                                 a = 1;
4093                                 aa = 1;
4094                                 break;
4095                         case -1: // no key
4096                                 a = 0;
4097                                 aa = 0;
4098                                 break;
4099                         default: // owned or dropped
4100                                 a = 0.2;
4101                                 aa = 0.5;
4102                                 break;
4103                 }
4104                 a = a * panel_fg_alpha;
4105                 aa = aa * panel_fg_alpha;
4106                 if(a > 0)
4107                 {
4108                         switch(keyteam)
4109                         {
4110                                 case COLOR_TEAM1:
4111                                         drawpic_aspect_skin(pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4112                                         break;
4113                                 case COLOR_TEAM2:
4114                                         drawpic_aspect_skin(pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4115                                         break;
4116                                 case COLOR_TEAM3:
4117                                         drawpic_aspect_skin(pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4118                                         break;
4119                                 case COLOR_TEAM4:
4120                                         drawpic_aspect_skin(pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
4121                                         break;
4122                                 default:
4123                                         break;
4124                         }
4125                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
4126                         {
4127                                 case 0:
4128                                         drawpic_aspect_skin(p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4129                                         break;
4130                                 case 1:
4131                                         drawpic_aspect_skin(p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4132                                         break;
4133                                 case 2:
4134                                         drawpic_aspect_skin(p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4135                                         break;
4136                                 case 3:
4137                                         drawpic_aspect_skin(p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
4138                                         break;
4139                         }
4140                 }
4141                 if(mySize_x > mySize_y)
4142                 {
4143                         p_x += 0.25 * mySize_x;
4144                         pa_x += 0.25 * mySize_x;
4145                 }
4146                 else
4147                 {
4148                         if(i == 1)
4149                         {
4150                                 p_y = pos_y + 0.625 * mySize_y;
4151                                 pa_y = pos_y + 0.5 * mySize_y;
4152                                 p_x = pos_x;
4153                                 pa_x = pos_x;
4154                         }
4155                         else
4156                         {
4157                                 p_x += 0.5 * mySize_x;
4158                                 pa_x += 0.5 * mySize_x;
4159                         }
4160                 }
4161         }
4162 }
4163
4164 // Nexball HUD mod icon
4165 void HUD_Mod_NexBall(vector pos, vector mySize)
4166 {
4167         float stat_items, nb_pb_starttime, dt, p;
4168
4169         stat_items = getstati(STAT_ITEMS);
4170         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
4171
4172         if (stat_items & IT_KEY1)
4173                 mod_active = 1;
4174         else
4175                 mod_active = 0;
4176
4177         //Manage the progress bar if any
4178         if (nb_pb_starttime > 0)
4179         {
4180                 dt = mod(time - nb_pb_starttime, nb_pb_period);
4181                 // one period of positive triangle
4182                 p = 2 * dt / nb_pb_period;
4183                 if (p > 1)
4184                         p = 2 - p;
4185
4186                 //Draw the filling
4187                 vector barsize;
4188                 float vertical;
4189                 if(mySize_x > mySize_y)
4190                 {
4191                         barsize = eX * p * mySize_x + eY * mySize_y;
4192                         vertical = 0;
4193                 }
4194                 else
4195                 {
4196                         barsize = eX * mySize_x + eY * p * mySize_y;
4197                         vertical = 1;
4198                 }
4199                 HUD_Panel_GetProgressBarColor("nexball")
4200                 HUD_Panel_DrawProgressBar(pos, vertical, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
4201         }
4202
4203         if (stat_items & IT_KEY1)
4204                 drawpic_aspect_skin(pos, "nexball_carrying", eX * mySize_x + eY * mySize_y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4205 }
4206
4207 // Race/CTS HUD mod icons
4208 float crecordtime_prev; // last remembered crecordtime
4209 float crecordtime_change_time; // time when crecordtime last changed
4210 float srecordtime_prev; // last remembered srecordtime
4211 float srecordtime_change_time; // time when srecordtime last changed
4212
4213 float race_status_time;
4214 float race_status_prev;
4215 string race_status_name_prev;
4216 void HUD_Mod_Race(vector pos, vector mySize)
4217 {
4218         mod_active = 1; // race should never hide the mod icons panel
4219         entity me;
4220         me = playerslots[player_localentnum - 1];
4221         float t, score;
4222         float f; // yet another function has this
4223         score = me.(scores[ps_primary]);
4224
4225         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
4226                 return; // no records in the actual race
4227
4228         drawfont = hud_bigfont;
4229
4230         // clientside personal record
4231         string rr;
4232         if(gametype == GAME_CTS)
4233                 rr = CTS_RECORD;
4234         else
4235                 rr = RACE_RECORD;
4236         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
4237
4238         if(score && (score < t || !t)) {
4239                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
4240                 if(cvar("cl_autodemo_delete_keeprecords"))
4241                 {
4242                         f = cvar("cl_autodemo_delete");
4243                         f &~= 1;
4244                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
4245                 }
4246         }
4247
4248         if(t != crecordtime_prev) {
4249                 crecordtime_prev = t;
4250                 crecordtime_change_time = time;
4251         }
4252
4253         vector textPos, medalPos;
4254         float squareSize;
4255         if(mySize_x > mySize_y) {
4256                 // text on left side
4257                 squareSize = min(mySize_y, mySize_x/2);
4258                 textPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eY * 0.5 * (mySize_y - squareSize);
4259                 medalPos = pos + eX * 0.5 * max(0, mySize_x/2 - squareSize) + eX * 0.5 * mySize_x + eY * 0.5 * (mySize_y - squareSize);
4260         } else {
4261                 // text on top
4262                 squareSize = min(mySize_x, mySize_y/2);
4263                 textPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eX * 0.5 * (mySize_x - squareSize);;
4264                 medalPos = pos + eY * 0.5 * max(0, mySize_y/2 - squareSize) + eY * 0.5 * mySize_y + eX * 0.5 * (mySize_x - squareSize);
4265         }
4266
4267         f = time - crecordtime_change_time;
4268
4269         if (f > 1) {
4270                 drawstring_aspect(textPos, "Personal best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4271                 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);
4272         } else {
4273                 drawstring_aspect(textPos, "Personal best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4274                 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);
4275                 drawstring_aspect_expanding(pos, "Personal best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
4276                 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);
4277         }
4278
4279         // server record
4280         t = race_server_record;
4281         if(t != srecordtime_prev) {
4282                 srecordtime_prev = t;
4283                 srecordtime_change_time = time;
4284         }
4285         f = time - srecordtime_change_time;
4286
4287         if (f > 1) {
4288                 drawstring_aspect(textPos + eY * 0.5 * squareSize, "Server best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4289                 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);
4290         } else {
4291                 drawstring_aspect(textPos + eY * 0.5 * squareSize, "Server best", eX * squareSize + eY * 0.25 * squareSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4292                 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);
4293                 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);
4294                 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);
4295         }
4296
4297         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
4298                 race_status_time = time + 5;
4299                 race_status_prev = race_status;
4300                 if (race_status_name_prev)
4301                         strunzone(race_status_name_prev);
4302                 race_status_name_prev = strzone(race_status_name);
4303         }
4304
4305         // race "awards"
4306         float a;
4307         a = bound(0, race_status_time - time, 1);
4308
4309         string s;
4310         s = textShortenToWidth(race_status_name, squareSize, '1 1 0' * 0.1 * squareSize, stringwidth_colors);
4311
4312         float rank;
4313         if(race_status > 0)
4314                 rank = race_CheckName(race_status_name);
4315         string rankname;
4316         rankname = race_PlaceName(rank);
4317
4318         vector namepos;
4319         namepos = medalPos + '0 0.8 0' * squareSize;
4320         vector rankpos;
4321         rankpos = medalPos + '0 0.15 0' * squareSize;
4322
4323         if(race_status == 0)
4324                 drawpic_aspect_skin(medalPos, "race_newfail", '1 1 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4325         else if(race_status == 1) {
4326                 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);
4327                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4328                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4329         } else if(race_status == 2) {
4330                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
4331                         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);
4332                 else
4333                         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);
4334                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4335                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4336         } else if(race_status == 3) {
4337                 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);
4338                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
4339                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
4340         }
4341
4342         if (race_status_time - time <= 0) {
4343                 race_status_prev = -1;
4344                 race_status = -1;
4345                 if(race_status_name)
4346                         strunzone(race_status_name);
4347                 race_status_name = string_null;
4348                 if(race_status_name_prev)
4349                         strunzone(race_status_name_prev);
4350                 race_status_name_prev = string_null;
4351         }
4352         drawfont = hud_font;
4353 }
4354
4355 float mod_prev; // previous state of mod_active to check for a change
4356 float mod_alpha;
4357 float mod_change; // "time" when mod_active changed
4358
4359 void HUD_ModIcons(void)
4360 {
4361         if(!autocvar_hud_panel_modicons && !autocvar__hud_configure)
4362                 return;
4363
4364         if (gametype != GAME_KEYHUNT && gametype != GAME_CTF && gametype != GAME_NEXBALL && gametype != GAME_CTS && gametype != GAME_RACE && !autocvar__hud_configure)
4365                 return;
4366
4367         active_panel = HUD_PANEL_MODICONS;
4368         HUD_Panel_UpdateCvars(modicons);
4369         vector pos, mySize;
4370         pos = panel_pos;
4371         mySize = panel_size;
4372
4373         if(mod_active != mod_prev) {
4374                 mod_change = time;
4375                 mod_prev = mod_active;
4376         }
4377
4378         if(mod_active || autocvar__hud_configure)
4379                 mod_alpha = bound(0, (time - mod_change) * 2, 1);
4380         else
4381                 mod_alpha = bound(0, 1 - (time - mod_change) * 2, 1);
4382
4383         if(mod_alpha)
4384                 HUD_Panel_DrawBg(mod_alpha);
4385
4386         if(panel_bg_padding)
4387         {
4388                 pos += '1 1 0' * panel_bg_padding;
4389                 mySize -= '2 2 0' * panel_bg_padding;
4390         }
4391
4392         // these MUST be ran in order to update mod_active
4393         if(gametype == GAME_KEYHUNT)
4394                 HUD_Mod_KH(pos, mySize);
4395         else if(gametype == GAME_CTF || autocvar__hud_configure)
4396                 HUD_Mod_CTF(pos, mySize); // forcealpha only needed for ctf icons, as only they are shown in config mode
4397         else if(gametype == GAME_NEXBALL)
4398                 HUD_Mod_NexBall(pos, mySize);
4399         else if(gametype == GAME_CTS || gametype == GAME_RACE)
4400                 HUD_Mod_Race(pos, mySize);
4401 }
4402
4403 // Draw pressed keys (#11)
4404 //
4405 void HUD_DrawPressedKeys(void)
4406 {
4407         if(!autocvar_hud_panel_pressedkeys && !autocvar__hud_configure)
4408                 return;
4409
4410         if(!(spectatee_status > 0 || autocvar_hud_panel_pressedkeys >= 2 || autocvar__hud_configure))
4411                 return;
4412
4413         active_panel = HUD_PANEL_PRESSEDKEYS;
4414         HUD_Panel_UpdateCvars(pressedkeys);
4415         vector pos, mySize;
4416         pos = panel_pos;
4417         mySize = panel_size;
4418
4419         HUD_Panel_DrawBg(1);
4420         if(panel_bg_padding)
4421         {
4422                 pos += '1 1 0' * panel_bg_padding;
4423                 mySize -= '2 2 0' * panel_bg_padding;
4424         }
4425
4426         // force custom aspect
4427         if(autocvar_hud_panel_pressedkeys_aspect)
4428         {
4429                 vector newSize;
4430                 if(mySize_x/mySize_y > autocvar_hud_panel_pressedkeys_aspect)
4431                 {
4432                         newSize_x = autocvar_hud_panel_pressedkeys_aspect * mySize_y;
4433                         newSize_y = mySize_y;
4434
4435                         pos_x = pos_x + (mySize_x - newSize_x) / 2;
4436                 }
4437                 else
4438                 {
4439                         newSize_y = 1/autocvar_hud_panel_pressedkeys_aspect * mySize_x;
4440                         newSize_x = mySize_x;
4441
4442                         pos_y = pos_y + (mySize_y - newSize_y) / 2;
4443                 }
4444                 mySize = newSize;
4445         }
4446
4447         vector keysize;
4448         keysize = eX * mySize_x * (1/3) + eY * mySize_y * 0.5;
4449         float pressedkeys;
4450
4451         pressedkeys = getstatf(STAT_PRESSED_KEYS);
4452         drawpic_aspect_skin(pos, ((pressedkeys & KEY_CROUCH) ? "key_crouch_inv.tga" : "key_crouch.tga"), keysize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
4453         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);
4454         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);
4455         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);
4456         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);
4457         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);
4458 }
4459
4460 // Handle chat as a panel (#12)
4461 //
4462 void HUD_Chat(void)
4463 {
4464         if(!autocvar_hud_panel_chat && !autocvar__hud_configure)
4465         {
4466                 cvar_set("con_chatrect", "0");
4467                 return;
4468         }
4469
4470         active_panel = HUD_PANEL_CHAT;
4471         HUD_Panel_UpdateCvars(chat);
4472
4473         if(autocvar__con_chat_maximized && !autocvar__hud_configure) // draw at full screen height if maximized
4474         {
4475                 panel_pos_y = panel_bg_border;
4476                 panel_size_y = vid_conheight - panel_bg_border * 2;
4477                 if(panel_bg == "0") // force a border when maximized
4478                 {
4479                         if(precache_pic(panel_bg) == "") {
4480                                 panel_bg = strcat(hud_skin_path, "/border_default");
4481                                 if(precache_pic(panel_bg) == "") {
4482                                         panel_bg = "gfx/hud/default/border_default";
4483                                 }
4484                         }
4485                 }
4486                 panel_bg_alpha = max(0.75, panel_bg_alpha); // force an alpha of at least 0.75
4487         }
4488
4489         vector pos, mySize;
4490         pos = panel_pos;
4491         mySize = panel_size;
4492
4493         HUD_Panel_DrawBg(1);
4494
4495         if(panel_bg_padding)
4496         {
4497                 pos += '1 1 0' * panel_bg_padding;
4498                 mySize -= '2 2 0' * panel_bg_padding;
4499         }
4500
4501         cvar_set("con_chatrect", "1");
4502
4503         cvar_set("con_chatrect_x", ftos(pos_x/vid_conwidth));
4504         cvar_set("con_chatrect_y", ftos(pos_y/vid_conheight));
4505
4506         cvar_set("con_chatwidth", ftos(mySize_x/vid_conwidth));
4507         cvar_set("con_chat", ftos(floor(mySize_y/cvar("con_chatsize") - 0.5)));
4508
4509         if(autocvar__hud_configure)
4510         {
4511                 float chatsize;
4512                 chatsize = cvar("con_chatsize");
4513                 cvar_set("con_chatrect_x", "9001"); // over 9000, we'll fake it instead for more control over alpha and such
4514                 float i, a;
4515                 for(i = 0; i < cvar("con_chat"); ++i)
4516                 {
4517                         if(i == cvar("con_chat") - 1)
4518                                 a = panel_fg_alpha;
4519                         else
4520                                 a = panel_fg_alpha * floor(((i + 1) * 7 + cvar("con_chattime"))/45);
4521                         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);
4522                 }
4523         }
4524 }
4525
4526 // Engine info panel (#13)
4527 //
4528 float prevfps;
4529 float prevfps_time;
4530 float framecounter;
4531
4532 float frametimeavg;
4533 float frametimeavg1; // 1 frame ago
4534 float frametimeavg2; // 2 frames ago
4535 void HUD_EngineInfo(void)
4536 {
4537         if(!autocvar_hud_panel_engineinfo && !autocvar__hud_configure)
4538                 return;
4539
4540         active_panel = HUD_PANEL_ENGINEINFO;
4541         HUD_Panel_UpdateCvars(engineinfo);
4542         vector pos, mySize;
4543         pos = panel_pos;
4544         mySize = panel_size;
4545
4546         HUD_Panel_DrawBg(1);
4547         if(panel_bg_padding)
4548         {
4549                 pos += '1 1 0' * panel_bg_padding;
4550                 mySize -= '2 2 0' * panel_bg_padding;
4551         }
4552
4553         float currentTime = gettime(GETTIME_REALTIME);
4554         if(cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage"))
4555         {
4556                 float currentframetime = currentTime - prevfps_time;
4557                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + currentframetime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
4558                 frametimeavg2 = frametimeavg1;
4559                 frametimeavg1 = frametimeavg;
4560                 
4561                 float weight;
4562                 weight = cvar("hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight");
4563                 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.
4564                 {
4565                         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
4566                                 prevfps = (1/currentframetime);
4567                         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"
4568                 }
4569                 prevfps_time = currentTime;
4570         }
4571         else
4572         {
4573                 framecounter += 1;
4574                 if(currentTime - prevfps_time > cvar("hud_panel_engineinfo_framecounter_time"))
4575                 {
4576                         prevfps = framecounter/(currentTime - prevfps_time);
4577                         framecounter = 0;
4578                         prevfps_time = currentTime;
4579                 }
4580         }
4581
4582         vector color;
4583         color = HUD_Get_Num_Color (prevfps, 100);
4584         drawfont = hud_bigfont;
4585         drawstring_aspect(pos, strcat("FPS: ", ftos_decimals(prevfps, cvar("hud_panel_engineinfo_framecounter_decimals"))), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
4586         drawfont = hud_font;
4587 }
4588
4589 // Info messages panel (#14)
4590 //
4591 #define drawInfoMessage(s)\
4592         if(autocvar_hud_panel_infomessages_flip)\
4593                 o_x = pos_x + mySize_x - stringwidth(s, TRUE, fontsize);\
4594         drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);\
4595         o_y += fontsize_y;
4596 void HUD_InfoMessages(void)
4597 {
4598         if(!autocvar_hud_panel_infomessages && !autocvar__hud_configure)
4599                 return;
4600
4601         active_panel = HUD_PANEL_INFOMESSAGES;
4602         HUD_Panel_UpdateCvars(infomessages);
4603         vector pos, mySize;
4604         pos = panel_pos;
4605         mySize = panel_size;
4606
4607         HUD_Panel_DrawBg(1);
4608         if(panel_bg_padding)
4609         {
4610                 pos += '1 1 0' * panel_bg_padding;
4611                 mySize -= '2 2 0' * panel_bg_padding;
4612         }
4613
4614         // always force 5:1 aspect
4615         vector newSize;
4616         if(mySize_x/mySize_y > 5)
4617         {
4618                 newSize_x = 5 * mySize_y;
4619                 newSize_y = mySize_y;
4620
4621                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
4622         }
4623         else
4624         {
4625                 newSize_y = 1/5 * mySize_x;
4626                 newSize_x = mySize_x;
4627
4628                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
4629         }
4630
4631         mySize = newSize;
4632         entity tm;
4633         vector o;
4634         o = pos;
4635
4636         vector fontsize;
4637         fontsize = '0.20 0.20 0' * mySize_y;
4638         
4639         float a;
4640         if(spectatee_status != 0)
4641                 a = 1;
4642         else
4643                 a = panel_fg_alpha;
4644
4645         string s;
4646         if(!autocvar__hud_configure)
4647         {
4648                 if(spectatee_status && !intermission)
4649                 {
4650                         if(spectatee_status == -1)
4651                                 s = "^1Observing";
4652                         else
4653                                 s = strcat("^1Spectating: ^7", GetPlayerName(spectatee_status - 1));
4654                         drawInfoMessage(s)
4655
4656                         if(spectatee_status == -1)
4657                                 s = strcat("^1Press ^3", getcommandkey("primary fire", "+fire"), "^1 to spectate");
4658                         else
4659                                 s = strcat("^1Press ^3", getcommandkey("primary fire", "+fire"), "^1 for another player");
4660                         drawInfoMessage(s)
4661
4662                         if(spectatee_status == -1)
4663                                 s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
4664                         else
4665                                 s = strcat("^1Press ^3", getcommandkey("secondary fire", "+fire2"), "^1 to observe");
4666                         drawInfoMessage(s)
4667
4668                         s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
4669                         drawInfoMessage(s)
4670
4671                         if(gametype == GAME_ARENA)
4672                                 s = "^1Wait for your turn to join";
4673                         else if(gametype == GAME_LMS)
4674                         {
4675                                 entity sk;
4676                                 sk = playerslots[player_localentnum - 1];
4677                                 if(sk.(scores[ps_primary]) >= 666)
4678                                         s = "^1Match has already begun";
4679                                 else if(sk.(scores[ps_primary]) > 0)
4680                                         s = "^1You have no more lives left";
4681                                 else
4682                                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
4683                         }
4684                         else
4685                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
4686                         drawInfoMessage(s)
4687
4688                         //show restart countdown:
4689                         if (time < getstatf(STAT_GAMESTARTTIME)) {
4690                                 float countdown;
4691                                 //we need to ceil, otherwise the countdown would be off by .5 when using round()
4692                                 countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
4693                                 s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
4694                                 drawcolorcodedstring(o, s, fontsize, a, DRAWFLAG_NORMAL);
4695                                 o_y += fontsize_y;
4696                         }
4697                 }
4698                 if(warmup_stage && !intermission)
4699                 {
4700                         s = "^2Currently in ^1warmup^2 stage!";
4701                         drawInfoMessage(s)
4702                 }
4703
4704                 string blinkcolor;
4705                 if(mod(time, 1) >= 0.5)
4706                         blinkcolor = "^1";
4707                 else
4708                         blinkcolor = "^3";
4709
4710                 if(ready_waiting && !intermission && !spectatee_status)
4711                 {
4712                         if(ready_waiting_for_me)
4713                         {
4714                                 if(warmup_stage)
4715                                         s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
4716                                 else
4717                                         s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
4718                         }
4719                         else
4720                         {
4721                                 if(warmup_stage)
4722                                         s = strcat("^2Waiting for others to ready up to end warmup...");
4723                                 else
4724                                         s = strcat("^2Waiting for others to ready up...");
4725                         }
4726                         drawInfoMessage(s)
4727                 }
4728                 else if(warmup_stage && !intermission && !spectatee_status)
4729                 {
4730                         s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
4731                         drawInfoMessage(s)
4732                 }
4733
4734                 if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
4735                 {
4736                         float ts_min, ts_max;
4737                         tm = teams.sort_next;
4738                         if (tm)
4739                         {
4740                                 for(; tm.sort_next; tm = tm.sort_next)
4741                                 {
4742                                         if(!tm.team_size || tm.team == COLOR_SPECTATOR)
4743                                                 continue;
4744                                         if(!ts_min) ts_min = tm.team_size;
4745                                         else ts_min = min(ts_min, tm.team_size);
4746                                         if(!ts_max) ts_max = tm.team_size;
4747                                         else ts_max = max(ts_max, tm.team_size);
4748                                 }
4749                                 if ((ts_max - ts_min) > 1)
4750                                 {
4751                                         s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
4752                                         tm = GetTeam(myteam, false);
4753                                         if (tm)
4754                                         if (tm.team != COLOR_SPECTATOR)
4755                                         if (tm.team_size == ts_max)
4756                                                 s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
4757                                         drawInfoMessage(s)
4758                                 }
4759                         }
4760                 }
4761         }
4762         else 
4763         {
4764                 s = "^7Press ^3ESC ^7to show HUD options.";
4765                 drawInfoMessage(s)
4766                 s = "^3Doubleclick ^7a panel for panel-specific options.";
4767                 drawInfoMessage(s)
4768                 s = "^3CTRL ^7to disable collision testing, ^3SHIFT ^7and";
4769                 drawInfoMessage(s)
4770                 s = "^3ALT ^7+ ^3ARROW KEYS ^7for fine adjustments.";
4771                 drawInfoMessage(s)
4772         }
4773 }
4774
4775 /*
4776 ==================
4777 Main HUD system
4778 ==================
4779 */
4780
4781 void HUD_ShowSpeed(void)
4782 {
4783         vector numsize;
4784         float pos, conversion_factor;
4785         string speed, zspeed, unit;
4786
4787         switch(cvar("cl_showspeed_unit"))
4788         {
4789                 default:
4790                 case 0:
4791                         unit = "";
4792                         conversion_factor = 1.0;
4793                         break;
4794                 case 1:
4795                         unit = " qu/s";
4796                         conversion_factor = 1.0;
4797                         break;
4798                 case 2:
4799                         unit = " m/s";
4800                         conversion_factor = 0.0254;
4801                         break;
4802                 case 3:
4803                         unit = " km/h";
4804                         conversion_factor = 0.0254 * 3.6;
4805                         break;
4806                 case 4:
4807                         unit = " mph";
4808                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
4809                         break;
4810                 case 5:
4811                         unit = " knots";
4812                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
4813                         break;
4814         }
4815
4816         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
4817
4818         numsize_x = numsize_y = cvar("cl_showspeed_size");
4819         pos = (vid_conheight - numsize_y) * cvar("cl_showspeed_position");
4820
4821         drawfont = hud_bigfont;
4822         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
4823
4824         if (cvar("cl_showspeed_z") == 1) {
4825                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
4826                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
4827         }
4828
4829         drawfont = hud_font;
4830 }
4831
4832 vector acc_prevspeed;
4833 float acc_prevtime;
4834 float acc_avg;
4835
4836 void HUD_ShowAcceleration(void)
4837 {
4838         float acceleration, sz, scale, alpha, f;
4839         vector pos, top, rgb;
4840         top_x = vid_conwidth/2;
4841         top_y = 0;
4842
4843         f = time - acc_prevtime;
4844         if(cvar("cl_showacceleration_z"))
4845                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
4846         else
4847                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
4848         acc_prevspeed = pmove_vel;
4849         acc_prevtime = time;
4850
4851         f = bound(0, f * 10, 1);
4852         acc_avg = acc_avg * (1 - f) + acceleration * f;
4853         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
4854         if (acceleration == 0)
4855                 return;
4856
4857         pos = top - sz/2 * eY + (cvar("cl_showacceleration_position") * vid_conheight) * eY;
4858
4859         sz = cvar("cl_showacceleration_size");
4860         scale = cvar("cl_showacceleration_scale");
4861         alpha = cvar("cl_showacceleration_alpha");
4862         if (cvar("cl_showacceleration_color_custom"))
4863                 rgb = stov(cvar_string("cl_showacceleration_color"));
4864         else {
4865                 if (acceleration < 0)
4866                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
4867                 else
4868                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
4869         }
4870
4871         if (acceleration > 0)
4872                 HUD_Panel_DrawProgressBar(pos, 0, acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * autocvar_hud_panel_fg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
4873         else
4874                 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);
4875 }
4876
4877 void HUD_Reset (void)
4878 {
4879         // reset gametype specific icons
4880         if(gametype == GAME_KEYHUNT)
4881                 HUD_Mod_KH_Reset();
4882         else if(gametype == GAME_CTF)
4883                 HUD_Mod_CTF_Reset();
4884 }
4885
4886 #define HUD_DrawPanel(id)\
4887 switch (id) {\
4888         case (HUD_PANEL_RADAR):\
4889                 HUD_Radar(); break;\
4890         case (HUD_PANEL_WEAPONS):\
4891                 HUD_Weapons(); break;\
4892         case (HUD_PANEL_AMMO):\
4893                 HUD_Ammo(); break;\
4894         case (HUD_PANEL_POWERUPS):\
4895                 HUD_Powerups(); break;\
4896         case (HUD_PANEL_HEALTHARMOR):\
4897                 HUD_HealthArmor(); break;\
4898         case (HUD_PANEL_NOTIFY):\
4899                 HUD_Notify(); break;\
4900         case (HUD_PANEL_TIMER):\
4901                 HUD_Timer(); break;\
4902         case (HUD_PANEL_SCORE):\
4903                 HUD_Score(); break;\
4904         case (HUD_PANEL_RACETIMER):\
4905                 HUD_RaceTimer(); break;\
4906         case (HUD_PANEL_VOTE):\
4907                 HUD_VoteWindow(); break;\
4908         case (HUD_PANEL_MODICONS):\
4909                 HUD_ModIcons(); break;\
4910         case (HUD_PANEL_PRESSEDKEYS):\
4911                 HUD_DrawPressedKeys(); break;\
4912         case (HUD_PANEL_CHAT):\
4913                 HUD_Chat(); break;\
4914         case (HUD_PANEL_ENGINEINFO):\
4915                 HUD_EngineInfo(); break;\
4916         case (HUD_PANEL_INFOMESSAGES):\
4917                  HUD_InfoMessages(); break;\
4918 }
4919
4920 void HUD_Main (void)
4921 {
4922         hud_skin_path = strcat("gfx/hud/", autocvar_hud_skin);
4923
4924         // global hud alpha fade
4925         if(menu_enabled == 1)
4926                 hud_fade_alpha = 1;
4927         else
4928                 hud_fade_alpha = (1 - autocvar__menu_alpha);
4929
4930         if(scoreboard_fade_alpha)
4931                 hud_fade_alpha = (1 - scoreboard_fade_alpha);
4932
4933         if(intermission == 2) // no hud during mapvote
4934                 hud_fade_alpha = 0;
4935         else if(autocvar__menu_alpha == 0 && scoreboard_fade_alpha == 0)
4936                 hud_fade_alpha = 1;
4937
4938         hud_fontsize = HUD_GetFontsize("hud_fontsize");
4939
4940         if(!autocvar__hud_configure && !hud_fade_alpha)
4941                 return;
4942
4943         // Drawing stuff
4944
4945         // HUD configure visible grid
4946         if(autocvar__hud_configure && autocvar_hud_configure_grid && autocvar_hud_configure_grid_alpha)
4947         {
4948                 float i;
4949                 // x-axis
4950                 for(i = 0; i < 1/bound(0.005, autocvar_hud_configure_grid_xsize, 0.2); ++i)
4951                 {
4952                         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);
4953                 }
4954                 // y-axis
4955                 for(i = 0; i < 1/bound(0.005, autocvar_hud_configure_grid_ysize, 0.2); ++i)
4956                 {
4957                         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);
4958                 }
4959         }
4960
4961         // draw the dock
4962         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
4963         {
4964                 float f;
4965                 vector color;
4966                 if((teamplay) && autocvar_hud_dock_color_team) {
4967                         f = stof(getplayerkey(player_localentnum - 1, "colors"));
4968                         color = colormapPaletteColor(mod(f, 16), 1) * autocvar_hud_dock_color_team;
4969                 }
4970                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && autocvar_hud_dock_color_team) {
4971                         color = '1 0 0' * autocvar_hud_dock_color_team;
4972                 }
4973                 else if(autocvar_hud_dock_color == "shirt") {
4974                         f = stof(getplayerkey(player_localentnum - 1, "colors"));
4975                         color = colormapPaletteColor(floor(f / 16), 0);
4976                 }
4977                 else if(autocvar_hud_dock_color == "pants") {
4978                         f = stof(getplayerkey(player_localentnum - 1, "colors"));
4979                         color = colormapPaletteColor(mod(f, 16), 1);
4980                 }
4981                 else
4982                         color = stov(autocvar_hud_dock_color);
4983
4984                 string pic;
4985                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
4986                 if(precache_pic(pic) == "") {
4987                         pic = strcat(hud_skin_path, "/dock_medium");
4988                         if(precache_pic(pic) == "") {
4989                                 pic = "gfx/hud/default/dock_medium";
4990                         }
4991                 }
4992                 drawpic('0 0 0', pic, eX * vid_conwidth + eY * vid_conheight, color, autocvar_hud_dock_alpha * hud_fade_alpha, DRAWFLAG_NORMAL); // no aspect ratio forcing on dock...
4993         }
4994
4995         // cache the panel order into the panel_order array
4996         if(autocvar__hud_panelorder != hud_panelorder_prev) {
4997                 if(hud_panelorder_prev)
4998                         strunzone(hud_panelorder_prev);
4999                 hud_panelorder_prev = strzone(autocvar__hud_panelorder);
5000                 tokenize_console(autocvar__hud_panelorder);
5001                 for(i = 0; i < HUD_PANEL_NUM; ++i) {
5002                         panel_order[i] = stof(argv(i));
5003                 }
5004         }
5005         // draw panels in order specified by panel_order array
5006         for(i = HUD_PANEL_NUM - 1; i >= 0; --i) {
5007                 if(i != HUD_PANEL_CHAT || !autocvar__con_chat_maximized) // don't draw maximized chat panel twice!
5008                         HUD_DrawPanel(panel_order[i]);
5009         }
5010
5011         // draw chat panel on top if it is maximized
5012         if(autocvar__con_chat_maximized)
5013                 HUD_DrawPanel(HUD_PANEL_CHAT);
5014
5015         // TODO hud_'ify these
5016         if (cvar("cl_showspeed"))
5017                 HUD_ShowSpeed();
5018         if (cvar("cl_showacceleration"))
5019                 HUD_ShowAcceleration();
5020
5021         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)
5022                 localcmd("cmd selectteam auto; cmd join\n");
5023
5024         hud_configure_prev = autocvar__hud_configure;
5025
5026         if (!autocvar__hud_configure) // hud config mode disabled, enable normal alpha stuff again
5027                 if (menu_enabled)
5028                         menu_enabled = 0;
5029 }