Merge remote branch 'origin/fruitiex/fruitbalance'
[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 && HUD_WouldDrawScoreboard()) // 24 = height of Scoreboard text
288         {
289                 centerprint_start_y = scoreboard_bottom + centerprint_fontsize_y;
290         }
291         pos = centerprint_start;
292         for (i=0; i<centerprint_num; i = i + 1)
293         {
294                 ts = centerprint_messages[i];
295                 drawfontscale = sz * '1 1 0';
296                 drawfont = hud_bigfont;
297                 pos_x = (vid_conwidth - stringwidth(ts, TRUE, centerprint_fontsize)) * 0.5;
298                 if (ts != "")
299                 {
300                         drawcolorcodedstring(pos + '0 1 0' * (1 - sz) * 0.5 *centerprint_fontsize_y, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
301                         pos_y = pos_y + centerprint_fontsize_y;
302                 }
303                 else
304                         // half height for empty lines looks better
305                         pos_y = pos_y + sz * centerprint_fontsize_y * 0.5;
306                 drawfontscale = '1 1 0';
307                 drawfont = hud_font;
308         }
309 }
310
311 void drawstringright(vector position, string text, vector scale, vector rgb, float alpha, float flag)
312 {
313         position_x -= 2 / 3 * strlen(text) * scale_x;
314         drawstring(position, text, scale, rgb, alpha, flag);
315 }
316
317 void drawstringcenter(vector position, string text, vector scale, vector rgb, float alpha, float flag)
318 {
319         position_x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * scale_x);
320         drawstring(position, text, scale, rgb, alpha, flag);
321 }
322
323 // return the string of the given race place
324 string race_PlaceName(float pos) {
325         if(pos == 1)
326                 return "1st";
327         else if(pos == 2)
328                 return "2nd";
329         else if(pos == 3)
330                 return "3rd";
331         else
332                 return strcat(ftos(pos), "th");
333 }
334
335 // return the string of the onscreen race timer
336 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
337 {
338         string col;
339         string timestr;
340         string cpname;
341         string lapstr;
342         lapstr = "";
343
344         if(histime == 0) // goal hit
345         {
346                 if(mytime > 0)
347                 {
348                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
349                         col = "^1";
350                 }
351                 else if(mytime == 0)
352                 {
353                         timestr = "+0.0";
354                         col = "^3";
355                 }
356                 else
357                 {
358                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
359                         col = "^2";
360                 }
361
362                 if(lapdelta > 0)
363                 {
364                         lapstr = strcat(" (-", ftos(lapdelta), "L)");
365                         col = "^2";
366                 }
367                 else if(lapdelta < 0)
368                 {
369                         lapstr = strcat(" (+", ftos(-lapdelta), "L)");
370                         col = "^1";
371                 }
372         }
373         else if(histime > 0) // anticipation
374         {
375                 if(mytime >= histime)
376                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
377                 else
378                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
379                 col = "^3";
380         }
381         else
382                 col = "^7";
383
384         if(cp == 254)
385                 cpname = "Start line";
386         else if(cp == 255)
387                 cpname = "Finish line";
388         else if(cp)
389                 cpname = strcat("Intermediate ", ftos(cp));
390         else
391                 cpname = "Finish line";
392
393         if(histime < 0)
394                 return strcat(col, cpname);
395         else if(hisname == "")
396                 return strcat(col, cpname, " (", timestr, ")");
397         else
398                 return strcat(col, cpname, " (", timestr, " ", strcat(hisname, col, lapstr), ")");
399 }
400
401 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
402 float race_CheckName(string net_name) {
403         float i;
404         for (i=RANKINGS_CNT-1;i>=0;--i)
405                 if(grecordholder[i] == net_name)
406                         return i+1;
407         return 0;
408 }
409
410 /*
411 ==================
412 HUD panels
413 ==================
414 */
415
416 // Save the config
417 void HUD_Panel_ExportCfg(string cfgname)
418 {
419         float fh;
420         fh = fopen(strcat("hud_", autocvar_hud_skin, "_", cfgname, ".cfg"), FILE_WRITE);
421         if(fh >= 0)
422         {
423                 fputs(fh, strcat("seta hud_skin \"", cvar_string("hud_skin"), "\"", "\n"));
424                 fputs(fh, strcat("seta hud_panel_bg \"", cvar_string("hud_panel_bg"), "\"", "\n"));
425                 fputs(fh, strcat("seta hud_panel_bg_color \"", cvar_string("hud_panel_bg_color"), "\"", "\n"));
426                 fputs(fh, strcat("seta hud_panel_bg_color_team \"", cvar_string("hud_panel_bg_color_team"), "\"", "\n"));
427                 fputs(fh, strcat("seta hud_panel_bg_alpha \"", cvar_string("hud_panel_bg_alpha"), "\"", "\n"));
428                 fputs(fh, strcat("seta hud_panel_bg_border \"", cvar_string("hud_panel_bg_border"), "\"", "\n"));
429                 fputs(fh, strcat("seta hud_panel_bg_padding \"", cvar_string("hud_panel_bg_padding"), "\"", "\n"));
430                 fputs(fh, strcat("seta hud_panel_fg_alpha \"", cvar_string("hud_panel_fg_alpha"), "\"", "\n"));
431                 fputs(fh, "\n");
432
433                 fputs(fh, strcat("seta hud_dock \"", cvar_string("hud_dock"), "\"", "\n"));
434                 fputs(fh, strcat("seta hud_dock_color \"", cvar_string("hud_dock_color"), "\"", "\n"));
435                 fputs(fh, strcat("seta hud_dock_color_team \"", cvar_string("hud_dock_color_team"), "\"", "\n"));
436                 fputs(fh, strcat("seta hud_dock_alpha \"", cvar_string("hud_dock_alpha"), "\"", "\n"));
437                 fputs(fh, "\n");
438
439                 fputs(fh, strcat("seta hud_progressbar_alpha \"", cvar_string("hud_progressbar_alpha"), "\"", "\n"));
440                 fputs(fh, strcat("seta hud_progressbar_strength_color \"", cvar_string("hud_progressbar_strength_color"), "\"", "\n"));
441                 fputs(fh, strcat("seta hud_progressbar_shield_color \"", cvar_string("hud_progressbar_shield_color"), "\"", "\n"));
442                 fputs(fh, strcat("seta hud_progressbar_health_color \"", cvar_string("hud_progressbar_health_color"), "\"", "\n"));
443                 fputs(fh, strcat("seta hud_progressbar_armor_color \"", cvar_string("hud_progressbar_armor_color"), "\"", "\n"));
444                 fputs(fh, strcat("seta hud_progressbar_fuel_color \"", cvar_string("hud_progressbar_fuel_color"), "\"", "\n"));
445                 fputs(fh, strcat("seta hud_progressbar_nexball_color \"", cvar_string("hud_progressbar_nexball_color"), "\"", "\n"));
446                 fputs(fh, "\n");
447
448                 fputs(fh, strcat("seta _hud_panelorder \"", cvar_string("_hud_panelorder"), "\"", "\n"));
449                 fputs(fh, "\n");
450
451                 fputs(fh, strcat("seta hud_configure_grid \"", cvar_string("hud_configure_grid"), "\"", "\n"));
452                 fputs(fh, strcat("seta hud_configure_grid_xsize \"", cvar_string("hud_configure_grid_xsize"), "\"", "\n"));
453                 fputs(fh, strcat("seta hud_configure_grid_ysize \"", cvar_string("hud_configure_grid_ysize"), "\"", "\n"));
454                 fputs(fh, "\n");
455
456                 fputs(fh, strcat("seta scr_centerpos \"", cvar_string("scr_centerpos"), "\"", "\n"));
457                 fputs(fh, "\n");
458
459                 // common cvars for all panels
460                 float i;
461                 for (i = 0; i < HUD_PANEL_NUM; ++i)
462                 {
463                         HUD_Panel_GetName(i)
464
465                         fputs(fh, strcat("seta hud_panel_", panel_name, " ", cvar_string(strcat("hud_panel_", panel_name)), "\n"));
466                         fputs(fh, strcat("seta hud_panel_", panel_name, "_pos \"", cvar_string(strcat("hud_panel_", panel_name, "_pos")), "\"", "\n"));
467                         fputs(fh, strcat("seta hud_panel_", panel_name, "_size \"", cvar_string(strcat("hud_panel_", panel_name, "_size")), "\"", "\n"));
468                         fputs(fh, strcat("seta hud_panel_", panel_name, "_bg \"", cvar_string(strcat("hud_panel_", panel_name, "_bg")), "\"", "\n"));
469                         fputs(fh, strcat("seta hud_panel_", panel_name, "_bg_color \"", cvar_string(strcat("hud_panel_", panel_name, "_bg_color")), "\"", "\n"));
470                         fputs(fh, strcat("seta hud_panel_", panel_name, "_bg_color_team \"", cvar_string(strcat("hud_panel_", panel_name, "_bg_color_team")), "\"", "\n"));
471                         fputs(fh, strcat("seta hud_panel_", panel_name, "_bg_alpha \"", cvar_string(strcat("hud_panel_", panel_name, "_bg_alpha")), "\"", "\n"));
472                         fputs(fh, strcat("seta hud_panel_", panel_name, "_bg_border \"", cvar_string(strcat("hud_panel_", panel_name, "_bg_border")), "\"", "\n"));
473                         fputs(fh, strcat("seta hud_panel_", panel_name, "_bg_padding \"", cvar_string(strcat("hud_panel_", panel_name, "_bg_padding")), "\"", "\n"));
474                         switch(i) {
475                                 case HUD_PANEL_WEAPONS:
476                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_complainbubble \"", cvar_string(strcat("hud_panel_", panel_name, "_complainbubble")), "\"", "\n"));
477                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_complainbubble_padding \"", cvar_string(strcat("hud_panel_", panel_name, "_complainbubble_padding")), "\"", "\n"));
478                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_complainbubble_color_outofammo \"", cvar_string(strcat("hud_panel_", panel_name, "_complainbubble_color_outofammo")), "\"", "\n"));
479                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_complainbubble_color_donthave \"", cvar_string(strcat("hud_panel_", panel_name, "_complainbubble_color_donthave")), "\"", "\n"));
480                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_complainbubble_color_unavailable \"", cvar_string(strcat("hud_panel_", panel_name, "_complainbubble_color_unavailable")), "\"", "\n"));
481                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_ammo_color \"", cvar_string(strcat("hud_panel_", panel_name, "_ammo_color")), "\"", "\n"));
482                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_ammo_alpha \"", cvar_string(strcat("hud_panel_", panel_name, "_ammo_alpha")), "\"", "\n"));
483                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_aspect \"", cvar_string(strcat("hud_panel_", panel_name, "_aspect")), "\"", "\n"));
484                                         break;
485                                 case HUD_PANEL_AMMO:
486                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_onlycurrent \"", cvar_string(strcat("hud_panel_", panel_name, "_onlycurrent")), "\"", "\n"));
487                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_iconalign \"", cvar_string(strcat("hud_panel_", panel_name, "_iconalign")), "\"", "\n"));
488                                         break;
489                                 case HUD_PANEL_POWERUPS:
490                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_flip \"", cvar_string(strcat("hud_panel_", panel_name, "_flip")), "\"", "\n"));
491                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_iconalign \"", cvar_string(strcat("hud_panel_", panel_name, "_iconalign")), "\"", "\n"));
492                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_baralign \"", cvar_string(strcat("hud_panel_", panel_name, "_baralign")), "\"", "\n"));
493                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_progressbar \"", cvar_string(strcat("hud_panel_", panel_name, "_progressbar")), "\"", "\n"));
494                                         break;
495                                 case HUD_PANEL_HEALTHARMOR:
496                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_flip \"", cvar_string(strcat("hud_panel_", panel_name, "_flip")), "\n"));
497                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_iconalign \"", cvar_string(strcat("hud_panel_", panel_name, "_iconalign")), "\"", "\n"));
498                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_baralign \"", cvar_string(strcat("hud_panel_", panel_name, "_baralign")), "\"", "\n"));
499                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_progressbar \"", cvar_string(strcat("hud_panel_", panel_name, "_progressbar")), "\"", "\n"));
500                                         break;
501                                 case HUD_PANEL_NOTIFY:
502                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_flip \"", cvar_string(strcat("hud_panel_", panel_name, "_flip")), "\"", "\n"));
503                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_print \"", cvar_string(strcat("hud_panel_", panel_name, "_print")), "\"", "\n"));
504                                         break;
505                                 case HUD_PANEL_RADAR:
506                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_foreground_alpha \"", cvar_string(strcat("hud_panel_", panel_name, "_foreground_alpha")), "\"", "\n"));
507                                         break;
508                                 case HUD_PANEL_VOTE:
509                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_alreadyvoted_alpha \"", cvar_string(strcat("hud_panel_", panel_name, "_alreadyvoted_alpha")), "\"", "\n"));
510                                         break;
511                                 case HUD_PANEL_PRESSEDKEYS:
512                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_aspect \"", cvar_string(strcat("hud_panel_", panel_name, "_aspect")), "\"", "\n"));
513                                         break;
514                                 case HUD_PANEL_INFOMESSAGES:
515                                         fputs(fh, strcat("seta hud_panel_", panel_name, "_flip \"", cvar_string(strcat("hud_panel_", panel_name, "_flip")), "\"", "\n"));
516                                         break;
517                         }
518                         fputs(fh, "\n");
519                 }
520                 fputs(fh, strcat("menu_sync", "\n")); // force the menu to reread the cvars, so that the dialogs are updated
521
522                 print("^2Successfully exported to hud_", autocvar_hud_skin, "_", cfgname, ".cfg! (Note: It's saved in data/data/)\n");
523         }
524         fclose(fh);
525 }
526
527 const float hlBorderSize = 4;
528 const string hlBorder = "gfx/hud/default/border_highlighted";
529 const string hlBorder2 = "gfx/hud/default/border_highlighted2";
530 void HUD_Panel_HlBorder(float myBorder, vector color, float alpha)
531 {
532         drawfill(panel_pos - '1 1 0' * myBorder, panel_size + '2 2 0' * myBorder, '0 0.5 1', .5 * alpha, DRAWFLAG_NORMAL);
533         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);
534         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);
535         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);
536         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);
537 }
538
539 // draw the background/borders
540 #define HUD_Panel_DrawBg(alpha)\
541 if(panel_bg != "0")\
542         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));\
543 if(highlightedPanel_prev == active_panel && autocvar__hud_configure)\
544         HUD_Panel_HlBorder(panel_bg_border + 1.5 * hlBorderSize, '0 0.5 1', 0.25 * (1 - autocvar__menu_alpha) * alpha);
545
546 void HUD_Panel_DrawProgressBar(vector pos, float vertical, vector mySize, vector color, float alpha, float drawflag)
547 {
548         if(!alpha)
549                 return;
550
551         string pic;
552         if(vertical) {
553                 pic = strcat(hud_skin_path, "/statusbar_vertical");
554                 if(precache_pic(pic) == "") {
555                         pic = "gfx/hud/default/statusbar_vertical";
556                 }
557                 drawsubpic(pos, eY * min(mySize_y * 0.5, mySize_x) + eX * mySize_x, pic, '0 0 0', '1 0.25 0', color, alpha, drawflag);
558                 if(mySize_y/mySize_x > 2)
559                         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);
560                 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);
561         } else {
562                 pic = strcat(hud_skin_path, "/statusbar");
563                 if(precache_pic(pic) == "") {
564                         pic = "gfx/hud/default/statusbar";
565                 }
566                 drawsubpic(pos, eX * min(mySize_x * 0.5, mySize_y) + eY * mySize_y, pic, '0 0 0', '0.25 1 0', color, alpha, drawflag);
567                 if(mySize_x/mySize_y > 2)
568                         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);
569                 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);
570         }
571 }
572
573 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float alpha, float drawflag)
574 {
575         if(!alpha)
576                 return;
577
578         string pic;
579         pic = strcat(hud_skin_path, "/num_leading");
580         if(precache_pic(pic) == "") {
581                 pic = "gfx/hud/default/num_leading";
582         }
583
584         drawsubpic(pos, eX * min(mySize_x * 0.5, mySize_y) + eY * mySize_y, pic, '0 0 0', '0.25 1 0', color, alpha, drawflag);
585         if(mySize_x/mySize_y > 2)
586                 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);
587         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);
588 }
589
590 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
591 vector HUD_Panel_CheckMove(vector myPos, vector mySize)
592 {
593         float i;
594
595         vector myTarget;
596         myTarget = myPos;
597
598         vector targPos;
599         vector targSize;
600         vector myCenter;
601         vector targCenter;
602         myCenter = '0 0 0'; // shut up fteqcc, there IS a reference
603         targCenter = '0 0 0'; // shut up fteqcc, there IS a reference
604
605         for (i = 0; i < HUD_PANEL_NUM; ++i) {
606                 if(i == highlightedPanel || !panel_enabled)
607                         continue;
608
609                 HUD_Panel_UpdatePosSizeForId(i)
610
611                 panel_pos -= '1 1 0' * panel_bg_border;
612                 panel_size += '2 2 0' * panel_bg_border;
613
614                 if(myPos_y + mySize_y < panel_pos_y)
615                         continue;
616                 if(myPos_y > panel_pos_y + panel_size_y)
617                         continue;
618
619                 if(myPos_x + mySize_x < panel_pos_x)
620                         continue;
621                 if(myPos_x > panel_pos_x + panel_size_x)
622                         continue;
623
624                 // OK, there IS a collision.
625
626                 myCenter_x = myPos_x + 0.5 * mySize_x;
627                 myCenter_y = myPos_y + 0.5 * mySize_y;
628
629                 targCenter_x = panel_pos_x + 0.5 * panel_size_x;
630                 targCenter_y = panel_pos_y + 0.5 * panel_size_y;
631
632                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y) // top left (of the target panel)
633                 {
634                         if(myPos_x + mySize_x - panel_pos_x < myPos_y + mySize_y - panel_pos_y) // push it to the side
635                                 myTarget_x = panel_pos_x - mySize_x;
636                         else // push it upwards
637                                 myTarget_y = panel_pos_y - mySize_y;
638                 }
639                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y) // top right
640                 {
641                         if(panel_pos_x + panel_size_x - myPos_x < myPos_y + mySize_y - panel_pos_y) // push it to the side
642                                 myTarget_x = panel_pos_x + panel_size_x;
643                         else // push it upwards
644                                 myTarget_y = panel_pos_y - mySize_y;
645                 }
646                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
647                 {
648                         if(myPos_x + mySize_x - panel_pos_x < panel_pos_y + panel_size_y - myPos_y) // push it to the side
649                                 myTarget_x = panel_pos_x - mySize_x;
650                         else // push it downwards
651                                 myTarget_y = panel_pos_y + panel_size_y;
652                 }
653                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
654                 {
655                         if(panel_pos_x + panel_size_x - myPos_x < panel_pos_y + panel_size_y - myPos_y) // push it to the side
656                                 myTarget_x = panel_pos_x + panel_size_x;
657                         else // push it downwards
658                                 myTarget_y = panel_pos_y + panel_size_y;
659                 }
660                 if(cvar("hud_configure_checkcollisions_debug"))
661                         drawfill(panel_pos, panel_size, '1 1 0', .3, DRAWFLAG_NORMAL);
662         }
663
664         return myTarget;
665 }
666
667 void HUD_Panel_SetPos(vector pos)
668 {
669         HUD_Panel_UpdatePosSizeForId(highlightedPanel)
670         vector mySize;
671         mySize = panel_size;
672
673         if(cvar("hud_configure_checkcollisions_debug"))
674                 drawfill(pos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
675
676         if(autocvar_hud_configure_grid)
677         {
678                 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;
679                 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;
680         }
681
682         if(hud_configure_checkcollisions)
683                 pos = HUD_Panel_CheckMove(pos, mySize);
684
685         pos_x = bound(0, pos_x, vid_conwidth - mySize_x);
686         pos_y = bound(0, pos_y, vid_conheight - mySize_y);
687
688         string s;
689         s = strcat(ftos(pos_x/vid_conwidth), " ", ftos(pos_y/vid_conheight));
690
691         HUD_Panel_GetName(highlightedPanel);
692         cvar_set(strcat("hud_panel_", panel_name, "_pos"), s);
693 }
694
695 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
696 vector HUD_Panel_CheckResize(vector mySize, vector resizeorigin) {
697         float i;
698
699         float targBorder;
700         vector targPos;
701         vector targSize;
702         vector targEndPos;
703
704         vector dist;
705         float ratio;
706         ratio = mySize_x/mySize_y;
707
708         for (i = 0; i < HUD_PANEL_NUM; ++i) {
709                 if(i == highlightedPanel || !panel_enabled)
710                         continue;
711
712                 HUD_Panel_UpdatePosSizeForId(i)
713
714                 panel_pos -= '1 1 0' * panel_bg_border;
715                 panel_size += '2 2 0' * panel_bg_border;
716
717                 targEndPos = panel_pos + panel_size;
718
719                 // resizeorigin is WITHIN target panel, just abort any collision testing against that particular panel to produce expected behaviour!
720                 if(resizeorigin_x > panel_pos_x && resizeorigin_x < targEndPos_x && resizeorigin_y > panel_pos_y && resizeorigin_y < targEndPos_y)
721                         continue;
722
723                 if (resizeCorner == 1)
724                 {
725                         // check if this panel is on our way
726                         if (resizeorigin_x <= panel_pos_x)
727                                 continue;
728                         if (resizeorigin_y <= panel_pos_y)
729                                 continue;
730                         if (targEndPos_x <= resizeorigin_x - mySize_x)
731                                 continue;
732                         if (targEndPos_y <= resizeorigin_y - mySize_y)
733                                 continue;
734
735                         // there is a collision:
736                         // detect which side of the panel we are facing is actually limiting the resizing
737                         // (which side the resize direction finds for first) and reduce the size up to there
738                         //
739                         // dist is the distance between resizeorigin and the "analogous" point of the panel
740                         // in this case resizeorigin (bottom-right point) and the bottom-right point of the panel
741                         dist_x = resizeorigin_x - targEndPos_x;
742                         dist_y = resizeorigin_y - targEndPos_y;
743                         if (dist_y <= 0 || dist_x / dist_y > ratio)
744                                 mySize_x = min(mySize_x, dist_x);
745                         else
746                                 mySize_y = min(mySize_y, dist_y);
747                 }
748                 else if (resizeCorner == 2)
749                 {
750                         if (resizeorigin_x >= targEndPos_x)
751                                 continue;
752                         if (resizeorigin_y <= panel_pos_y)
753                                 continue;
754                         if (panel_pos_x >= resizeorigin_x + mySize_x)
755                                 continue;
756                         if (targEndPos_y <= resizeorigin_y - mySize_y)
757                                 continue;
758
759                         dist_x = panel_pos_x - resizeorigin_x;
760                         dist_y = resizeorigin_y - targEndPos_y;
761                         if (dist_y <= 0 || dist_x / dist_y > ratio)
762                                 mySize_x = min(mySize_x, dist_x);
763                         else
764                                 mySize_y = min(mySize_y, dist_y);
765                 }
766                 else if (resizeCorner == 3)
767                 {
768                         if (resizeorigin_x <= panel_pos_x)
769                                 continue;
770                         if (resizeorigin_y >= targEndPos_y)
771                                 continue;
772                         if (targEndPos_x <= resizeorigin_x - mySize_x)
773                                 continue;
774                         if (panel_pos_y >= resizeorigin_y + mySize_y)
775                                 continue;
776
777                         dist_x = resizeorigin_x - targEndPos_x;
778                         dist_y = panel_pos_y - resizeorigin_y;
779                         if (dist_y <= 0 || dist_x / dist_y > ratio)
780                                 mySize_x = min(mySize_x, dist_x);
781                         else
782                                 mySize_y = min(mySize_y, dist_y);
783                 }
784                 else if (resizeCorner == 4)
785                 {
786                         if (resizeorigin_x >= targEndPos_x)
787                                 continue;
788                         if (resizeorigin_y >= targEndPos_y)
789                                 continue;
790                         if (panel_pos_x >= resizeorigin_x + mySize_x)
791                                 continue;
792                         if (panel_pos_y >= resizeorigin_y + mySize_y)
793                                 continue;
794
795                         dist_x = panel_pos_x - resizeorigin_x;
796                         dist_y = panel_pos_y - resizeorigin_y;
797                         if (dist_y <= 0 || dist_x / dist_y > ratio)
798                                 mySize_x = min(mySize_x, dist_x);
799                         else
800                                 mySize_y = min(mySize_y, dist_y);
801                 }
802                 if(cvar("hud_configure_checkcollisions_debug"))
803                         drawfill(panel_pos, panel_size, '1 1 0', .3, DRAWFLAG_NORMAL);
804         }
805
806         return mySize;
807 }
808
809 void HUD_Panel_SetPosSize(vector mySize)
810 {
811         HUD_Panel_UpdatePosSizeForId(highlightedPanel)
812         vector resizeorigin;
813         resizeorigin = panel_click_resizeorigin;
814         vector myPos;
815
816         // minimum panel size cap
817         mySize_x = max(0.025 * vid_conwidth, mySize_x);
818         mySize_y = max(0.025 * vid_conheight, mySize_y);
819
820         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.
821         {
822                 mySize_x = max(17 * cvar("con_chatsize"), mySize_x);
823                 mySize_y = max(2 * cvar("con_chatsize") + 2 * panel_bg_padding, mySize_y);
824         }
825
826         // collision testing|
827         // -----------------+
828
829         // we need to know pos at this stage, but it might still change later if we hit a screen edge/other panel (?)
830         if(resizeCorner == 1) {
831                 myPos_x = resizeorigin_x - mySize_x;
832                 myPos_y = resizeorigin_y - mySize_y;
833         } else if(resizeCorner == 2) {
834                 myPos_x = resizeorigin_x;
835                 myPos_y = resizeorigin_y - mySize_y;
836         } else if(resizeCorner == 3) {
837                 myPos_x = resizeorigin_x - mySize_x;
838                 myPos_y = resizeorigin_y;
839         } else { // resizeCorner == 4
840                 myPos_x = resizeorigin_x;
841                 myPos_y = resizeorigin_y;
842         }
843
844         // left/top screen edges
845         if(myPos_x < 0)
846                 mySize_x = mySize_x + myPos_x;
847         if(myPos_y < 0)
848                 mySize_y = mySize_y + myPos_y;
849
850         // bottom/right screen edges
851         if(myPos_x + mySize_x > vid_conwidth)
852                 mySize_x = vid_conwidth - myPos_x;
853         if(myPos_y + mySize_y > vid_conheight)
854                 mySize_y = vid_conheight - myPos_y;
855
856         if(cvar("hud_configure_checkcollisions_debug"))
857                 drawfill(myPos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
858
859         // before checkresize, otherwise panel can be snapped partially inside another panel or panel aspect ratio can be broken
860         if(autocvar_hud_configure_grid)
861         {
862                 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;
863                 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;
864         }
865
866         if(hud_configure_checkcollisions)
867                 mySize = HUD_Panel_CheckResize(mySize, resizeorigin);
868
869         // 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)
870         mySize_x = max(0.025 * vid_conwidth, mySize_x);
871         mySize_y = max(0.025 * vid_conheight, mySize_y);
872
873         // do another pos check, as size might have changed by now
874         if(resizeCorner == 1) {
875                 myPos_x = resizeorigin_x - mySize_x;
876                 myPos_y = resizeorigin_y - mySize_y;
877         } else if(resizeCorner == 2) {
878                 myPos_x = resizeorigin_x;
879                 myPos_y = resizeorigin_y - mySize_y;
880         } else if(resizeCorner == 3) {
881                 myPos_x = resizeorigin_x - mySize_x;
882                 myPos_y = resizeorigin_y;
883         } else { // resizeCorner == 4
884                 myPos_x = resizeorigin_x;
885                 myPos_y = resizeorigin_y;
886         }
887
888         if(cvar("hud_configure_checkcollisions_debug"))
889                 drawfill(myPos, mySize, '0 1 0', .3, DRAWFLAG_NORMAL);
890
891         HUD_Panel_GetName(highlightedPanel);
892         string s;
893         s = strcat(ftos(mySize_x/vid_conwidth), " ", ftos(mySize_y/vid_conheight));
894         cvar_set(strcat("hud_panel_", panel_name, "_size"), s);
895
896         s = strcat(ftos(myPos_x/vid_conwidth), " ", ftos(myPos_y/vid_conheight));
897         cvar_set(strcat("hud_panel_", panel_name, "_pos"), s);
898 }
899
900 float mouseClicked;
901 float prevMouseClicked; // previous state
902 float prevMouseClickedTime; // time during previous mouse click, to check for doubleclicks
903 vector prevMouseClickedPos; // pos during previous mouse click, to check for doubleclicks
904
905 float menu_enabled;
906 float menu_enabled_time;
907 float pressed_key_time;
908 void HUD_Panel_Arrow_Action(float nPrimary)
909 {
910         if (highlightedPanel_prev == -1 || mouseClicked)
911                 return;
912
913         hud_configure_checkcollisions = (!(hudShiftState & S_CTRL) && cvar("hud_configure_checkcollisions"));
914
915         float step;
916         if(autocvar_hud_configure_grid)
917         {
918                 if (nPrimary == K_UPARROW || nPrimary == K_DOWNARROW)
919                 {
920                         if (hudShiftState & S_SHIFT)
921                                 step = bound(0.005, autocvar_hud_configure_grid_ysize, 0.2) * vid_conheight;
922                         else
923                                 step = 2 * bound(0.005, autocvar_hud_configure_grid_ysize, 0.2) * vid_conheight;
924                 }
925                 else
926                 {
927                         if (hudShiftState & S_SHIFT)
928                                 step = bound(0.005, autocvar_hud_configure_grid_xsize, 0.2) * vid_conwidth;
929                         else
930                                 step = 2 * bound(0.005, autocvar_hud_configure_grid_xsize, 0.2) * vid_conwidth;
931                 }
932         }
933         else
934         {
935                 if (nPrimary == K_UPARROW || nPrimary == K_DOWNARROW)
936                         step = vid_conheight;
937                 else
938                         step = vid_conwidth;
939                 if (hudShiftState & S_SHIFT)
940                         step = (step / 256); // more precision
941                 else
942                         step = (step / 64) * (1 + 2 * (time - pressed_key_time));
943         }
944
945         highlightedPanel = highlightedPanel_prev;
946
947         HUD_Panel_UpdatePosSizeForId(highlightedPanel)
948
949         vector prev_pos, prev_size;
950         prev_pos = panel_pos;
951         prev_size = panel_size;
952
953         if (hudShiftState & S_ALT) // resize
954         {
955                 highlightedAction = 1;
956                 if(nPrimary == K_UPARROW)
957                         resizeCorner = 1;
958                 else if(nPrimary == K_RIGHTARROW)
959                         resizeCorner = 2;
960                 else if(nPrimary == K_LEFTARROW)
961                         resizeCorner = 3;
962                 else // if(nPrimary == K_DOWNARROW)
963                         resizeCorner = 4;
964
965                 // ctrl+arrow reduces the size, instead of increasing it
966                 // Note that ctrl disables collisions check too, but it's fine
967                 // since we don't collide with anything reducing the size
968                 if (hudShiftState & S_CTRL) {
969                         step = -step;
970                         resizeCorner = 5 - resizeCorner;
971                 }
972
973                 vector mySize;
974                 mySize = panel_size;
975                 panel_click_resizeorigin = panel_pos;
976                 if(resizeCorner == 1) {
977                         panel_click_resizeorigin += mySize;
978                         mySize_y += step;
979                 } else if(resizeCorner == 2) {
980                         panel_click_resizeorigin_y += mySize_y;
981                         mySize_x += step;
982                 } else if(resizeCorner == 3) {
983                         panel_click_resizeorigin_x += mySize_x;
984                         mySize_x += step;
985                 } else { // resizeCorner == 4
986                         mySize_y += step;
987                 }
988                 HUD_Panel_SetPosSize(mySize);
989         }
990         else // move
991         {
992                 highlightedAction = 2;
993                 vector pos;
994                 pos = panel_pos;
995                 if(nPrimary == K_UPARROW)
996                         pos_y -= step;
997                 else if(nPrimary == K_DOWNARROW)
998                         pos_y += step;
999                 else if(nPrimary == K_LEFTARROW)
1000                         pos_x -= step;
1001                 else // if(nPrimary == K_RIGHTARROW)
1002                         pos_x += step;
1003
1004                 HUD_Panel_SetPos(pos);
1005         }
1006
1007         HUD_Panel_UpdatePosSizeForId(highlightedPanel)
1008
1009         if (prev_pos != panel_pos || prev_size != panel_size)
1010         {
1011                 // backup!
1012                 panel_pos_backup = prev_pos;
1013                 panel_size_backup = prev_size;
1014                 highlightedPanel_backup = highlightedPanel;
1015         }
1016 }
1017
1018 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
1019 {
1020         if(!autocvar__hud_configure)
1021                 return false;
1022
1023         // allow console bind to work
1024         string con_keys;
1025         float keys;
1026         con_keys = findkeysforcommand("toggleconsole");
1027         keys = tokenize(con_keys);
1028
1029         float hit_con_bind, i;
1030         for (i = 0; i < keys; ++i)
1031         {
1032                 if(nPrimary == stof(argv(i)))
1033                         hit_con_bind = 1;
1034         }
1035
1036         if(bInputType == 0) {
1037                 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
1038                 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
1039                 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
1040         }
1041         else if(bInputType == 1) {
1042                 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
1043                 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
1044                 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
1045         }
1046
1047         if(nPrimary == K_MOUSE1)
1048         {
1049                 if(bInputType == 0) { // key pressed
1050                         mouseClicked = 1;
1051                         return true;
1052                 }
1053                 else if(bInputType == 1) {// key released
1054                         mouseClicked = 0;
1055                         return true;
1056                 }
1057         }
1058         else if(nPrimary == K_ESCAPE)
1059         {
1060                 if (bInputType == 1)
1061                         return true;
1062                 disable_menu_alphacheck = 1;
1063                 menu_enabled = 1;
1064                 menu_enabled_time = time;
1065                 localcmd("menu_showhudexit\n");
1066         }
1067         else if(hudShiftState & S_CTRL)
1068         {
1069                 if (mouseClicked)
1070                         return true;
1071
1072                 if(nPrimary == K_SPACE) // enable/disable highlighted panel or dock
1073                 {
1074                         if (bInputType == 1)
1075                                 return true;
1076
1077                         if (highlightedPanel_prev != -1)
1078                                 cvar_set(strcat("hud_panel_", panel_name), ftos(!(panel_enabled)));
1079                         else
1080                                 cvar_set(strcat("hud_dock"), (autocvar_hud_dock == "") ? "dock" : "");
1081                 }
1082                 if(nPrimary == 'c') // copy highlighted panel size
1083                 {
1084                         if (bInputType == 1)
1085                                 return true;
1086
1087                         if (highlightedPanel_prev != -1)
1088                         {
1089                                 panel_size_copied = panel_size;
1090                                 highlightedPanel_copied = highlightedPanel_prev;
1091                         }
1092                 }
1093                 else if(nPrimary == 'v') // past copied size on the highlighted panel
1094                 {
1095                         if (bInputType == 1)
1096                                 return true;
1097
1098                         if (highlightedPanel_copied == -1 || highlightedPanel_prev == -1)
1099                                 return true;
1100
1101                         HUD_Panel_UpdatePosSizeForId(highlightedPanel_prev)
1102
1103                         // reduce size if it'd go beyond screen boundaries
1104                         vector tmp_size = panel_size_copied;
1105                         if (panel_pos_x + panel_size_copied_x > vid_conwidth)
1106                                 tmp_size_x = vid_conwidth - panel_pos_x;
1107                         if (panel_pos_y + panel_size_copied_y > vid_conheight)
1108                                 tmp_size_y = vid_conheight - panel_pos_y;
1109
1110                         if (panel_size == tmp_size)
1111                                 return true;
1112
1113                         // backup first!
1114                         panel_pos_backup = panel_pos;
1115                         panel_size_backup = panel_size;
1116                         highlightedPanel_backup = highlightedPanel_prev;
1117
1118                         string s;
1119                         s = strcat(ftos(tmp_size_x/vid_conwidth), " ", ftos(tmp_size_y/vid_conheight));
1120                         cvar_set(strcat("hud_panel_", panel_name, "_size"), s);
1121                 }
1122                 else if(nPrimary == 'z') // undo last action
1123                 {
1124                         if (bInputType == 1)
1125                                 return true;
1126                         //restore previous values
1127                         if (highlightedPanel_backup != -1)
1128                         {
1129                                 HUD_Panel_GetName(highlightedPanel_backup)
1130                                 string s;
1131                                 s = strcat(ftos(panel_pos_backup_x/vid_conwidth), " ", ftos(panel_pos_backup_y/vid_conheight));
1132                                 cvar_set(strcat("hud_panel_", panel_name, "_pos"), s);
1133                                 s = strcat(ftos(panel_size_backup_x/vid_conwidth), " ", ftos(panel_size_backup_y/vid_conheight));
1134                                 cvar_set(strcat("hud_panel_", panel_name, "_size"), s);
1135                                 highlightedPanel_backup = -1;
1136                         }
1137                 }
1138         }
1139         else if(nPrimary == K_UPARROW || nPrimary == K_DOWNARROW || nPrimary == K_LEFTARROW || nPrimary == K_RIGHTARROW)
1140         {
1141                 if (bInputType == 1)
1142                 {
1143                         pressed_key_time = 0;
1144                         return true;
1145                 }
1146                 else if (pressed_key_time == 0)
1147                         pressed_key_time = time;
1148
1149                 HUD_Panel_Arrow_Action(nPrimary); //move or resize panel
1150         }
1151         else if(hit_con_bind)
1152                 return false;
1153
1154         return true; // Suppress ALL other input
1155 }
1156
1157 float HUD_Panel_HighlightCheck()
1158 {
1159         float i, j, border;
1160         vector panelPos;
1161         vector panelSize;
1162
1163         while(j <= HUD_PANEL_NUM)
1164         {
1165                 i = panel_order[j];
1166                 j += 1;
1167
1168                 HUD_Panel_UpdatePosSizeForId(i)
1169
1170                 panelPos = panel_pos;
1171                 panelSize = panel_size;
1172                 border = max(8, panel_bg_border); // FORCED border so a small border size doesn't mean you can't resize
1173
1174                 // move
1175                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1176                 {
1177                         return 1;
1178                 }
1179                 // resize from topleft border
1180                 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)
1181                 {
1182                         return 2;
1183                 }
1184                 // resize from topright border
1185                 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)
1186                 {
1187                         return 3;
1188                 }
1189                 // resize from bottomleft border
1190                 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)
1191                 {
1192                         return 3;
1193                 }
1194                 // resize from bottomright border
1195                 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)
1196                 {
1197                         return 2;
1198                 }
1199         }
1200         return 0;
1201 }
1202
1203 // move a panel to the beginning of the panel order array (which means it gets drawn last, on top of everything else)
1204 void HUD_Panel_FirstInDrawQ(float id)
1205 {
1206         float i;
1207         var float place = -1;
1208         // find out where in the array our current id is, save into place
1209         for(i = 0; i < HUD_PANEL_NUM; ++i)
1210         {
1211                 if(panel_order[i] == id)
1212                 {
1213                         place = i;
1214                         break;
1215                 }
1216         }
1217         // place last if we didn't find a place for it yet (probably new panel, or screwed up cvar)
1218         if(place == -1)
1219                 place = HUD_PANEL_NUM - 1;
1220
1221         // move all ids up by one step in the array until "place"
1222         for(i = place; i > 0; --i)
1223         {
1224                 panel_order[i] = panel_order[i-1];
1225         }
1226         // now save the new top id
1227         panel_order[0] = id;
1228         
1229         // let's save them into the cvar by some strcat trickery
1230         string s;
1231         for(i = 0; i < HUD_PANEL_NUM; ++i)
1232         {
1233                 s = strcat(s, ftos(panel_order[i]), " ");
1234         }
1235         cvar_set("_hud_panelorder", s);
1236         if(hud_panelorder_prev)
1237                 strunzone(hud_panelorder_prev);
1238         hud_panelorder_prev = strzone(autocvar__hud_panelorder); // prevent HUD_Main from doing useless update, we already updated here
1239 }
1240
1241 void HUD_Panel_Highlight()
1242 {
1243         float i, j, border;
1244         vector panelPos;
1245         vector panelSize;
1246
1247         while(j <= HUD_PANEL_NUM)
1248         {
1249                 i = panel_order[j];
1250                 j += 1;
1251
1252                 HUD_Panel_UpdatePosSizeForId(i)
1253
1254                 panelPos = panel_pos;
1255                 panelSize = panel_size;
1256                 border = max(8, panel_bg_border); // FORCED border so a small border size doesn't mean you can't resize
1257
1258                 // move
1259                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1260                 {
1261                         highlightedPanel = i;
1262                         HUD_Panel_FirstInDrawQ(i);
1263                         highlightedAction = 1;
1264                         panel_click_distance = mousepos - panelPos;
1265                         return;
1266                 }
1267                 // resize from topleft border
1268                 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)
1269                 {
1270                         highlightedPanel = i;
1271                         HUD_Panel_FirstInDrawQ(i);
1272                         highlightedAction = 2;
1273                         resizeCorner = 1;
1274                         panel_click_distance = mousepos - panelPos;
1275                         panel_click_resizeorigin = panelPos + panelSize;
1276                         return;
1277                 }
1278                 // resize from topright border
1279                 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)
1280                 {
1281                         highlightedPanel = i;
1282                         HUD_Panel_FirstInDrawQ(i);
1283                         highlightedAction = 2;
1284                         resizeCorner = 2;
1285                         panel_click_distance_x = panelSize_x - mousepos_x + panelPos_x;
1286                         panel_click_distance_y = mousepos_y - panelPos_y;
1287                         panel_click_resizeorigin = panelPos + eY * panelSize_y;
1288                         return;
1289                 }
1290                 // resize from bottomleft border
1291                 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)
1292                 {
1293                         highlightedPanel = i;
1294                         HUD_Panel_FirstInDrawQ(i);
1295                         highlightedAction = 2;
1296                         resizeCorner = 3;
1297                         panel_click_distance_x = mousepos_x - panelPos_x;
1298                         panel_click_distance_y = panelSize_y - mousepos_y + panelPos_y;
1299                         panel_click_resizeorigin = panelPos + eX * panelSize_x;
1300                         return;
1301                 }
1302                 // resize from bottomright border
1303                 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)
1304                 {
1305                         highlightedPanel = i;
1306                         HUD_Panel_FirstInDrawQ(i);
1307                         highlightedAction = 2;
1308                         resizeCorner = 4;
1309                         panel_click_distance = panelSize - mousepos + panelPos;
1310                         panel_click_resizeorigin = panelPos;
1311                         return;
1312                 }
1313                 else
1314                 {
1315                         highlightedPanel_prev = -1;
1316                 }
1317         }
1318 }
1319
1320 float highlightcheck;
1321 vector prev_pos, prev_size;
1322 void HUD_Panel_Mouse()
1323 {
1324         // 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
1325         if (menu_enabled == 0) // menu dialog closed, enable normal alpha stuff again
1326                 disable_menu_alphacheck = 0;
1327         if (autocvar__menu_alpha == 0 && time - menu_enabled_time > 0.5)
1328                 menu_enabled = 0;
1329
1330         /*
1331         print("Disable menu_alphacheck: ", ftos(disable_menu_alphacheck), "\n");
1332         print("Highlighted: ", ftos(highlightedPanel), "\n");
1333         print("Menu alpha: ", cvar_string("_menu_alpha"), "\n");
1334         */
1335
1336         if(mouseClicked == 0 && disable_menu_alphacheck != 2 && highlightedPanel >= 0) { // don't reset these variables in disable_menu_alphacheck mode 2!
1337                 highlightedPanel = -1;
1338                 highlightedAction = 0;
1339         }
1340         if(highlightedPanel != -1)
1341                 highlightedPanel_prev = highlightedPanel;
1342
1343         mousepos = mousepos + getmousepos() * cvar("menu_mouse_speed");
1344
1345         mousepos_x = bound(0, mousepos_x, vid_conwidth);
1346         mousepos_y = bound(0, mousepos_y, vid_conheight);
1347
1348         if(mouseClicked)
1349         {
1350                 if(prevMouseClicked == 0)
1351                 {
1352                         HUD_Panel_Highlight(); // sets highlightedPanel, highlightedAction, panel_click_distance, panel_click_resizeorigin
1353                                                                         // and calls HUD_Panel_UpdatePosSizeForId() for the highlighted panel
1354                         prev_pos = panel_pos;
1355                         prev_size = panel_size;
1356                 }
1357                 else
1358                         HUD_Panel_UpdatePosSizeForId(highlightedPanel)
1359
1360                 if (prev_pos != panel_pos || prev_size != panel_size)
1361                 {
1362                         hud_configure_checkcollisions = (!(hudShiftState & S_CTRL) && autocvar_hud_configure_checkcollisions);
1363                         // backup!
1364                         panel_pos_backup = prev_pos;
1365                         panel_size_backup = prev_size;
1366                         highlightedPanel_backup = highlightedPanel;
1367                 }
1368                 else
1369                         // in case the clicked panel is inside another panel and we aren't
1370                         // moving it, avoid the immediate "fix" of its position/size
1371                         // (often unwanted and hateful) by disabling collisions check
1372                         hud_configure_checkcollisions = false;
1373
1374                 if(highlightedAction == 1)
1375                         HUD_Panel_SetPos(mousepos - panel_click_distance);
1376                 else if(highlightedAction == 2)
1377                 {
1378                         vector mySize;
1379                         if(resizeCorner == 1) {
1380                                 mySize_x = panel_click_resizeorigin_x - (mousepos_x - panel_click_distance_x);
1381                                 mySize_y = panel_click_resizeorigin_y - (mousepos_y - panel_click_distance_y);
1382                         } else if(resizeCorner == 2) {
1383                                 mySize_x = mousepos_x + panel_click_distance_x - panel_click_resizeorigin_x;
1384                                 mySize_y = panel_click_distance_y + panel_click_resizeorigin_y - mousepos_y;
1385                         } else if(resizeCorner == 3) {
1386                                 mySize_x = panel_click_resizeorigin_x + panel_click_distance_x - mousepos_x;
1387                                 mySize_y = mousepos_y + panel_click_distance_y - panel_click_resizeorigin_y;
1388                         } else { // resizeCorner == 4
1389                                 mySize_x = mousepos_x - (panel_click_resizeorigin_x - panel_click_distance_x);
1390                                 mySize_y = mousepos_y - (panel_click_resizeorigin_y - panel_click_distance_y);
1391                         }
1392                         HUD_Panel_SetPosSize(mySize);
1393                 }
1394
1395                 // doubleclick check
1396                 if(time - prevMouseClickedTime < 0.4 && prevMouseClicked == 0 && prevMouseClickedPos == mousepos && highlightedPanel >= 0)
1397                 {
1398                         mouseClicked = 0; // to prevent spam, I guess.
1399                         disable_menu_alphacheck = 2;
1400                         menu_enabled = 1;
1401                         menu_enabled_time = time;
1402                         HUD_Panel_GetName(highlightedPanel)
1403                         localcmd("menu_showhudoptions ", panel_name, "\n");
1404                         return;
1405                 }
1406                 if(prevMouseClicked == 0)
1407                 {
1408                         prevMouseClickedTime = time;
1409                         prevMouseClickedPos = mousepos;
1410                 }
1411         }
1412         else
1413         {
1414                 highlightcheck = HUD_Panel_HighlightCheck();
1415         }
1416         // draw cursor after performing move/resize to have the panel pos/size updated before highlightcheck
1417         string cursor;
1418         vector cursorsize;
1419         cursorsize = '32 32 0';
1420
1421         if(highlightcheck == 0)
1422                 drawpic(mousepos, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1423         else if(highlightcheck == 1)
1424                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor_move.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1425         else if(highlightcheck == 2)
1426                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor_resize.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1427         else
1428                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor_resize2.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1429
1430         prevMouseClicked = mouseClicked;
1431 }
1432
1433 // Weapon icons (#0)
1434 //
1435 float weaponspace[10];
1436 #define HUD_Weapons_Clear()\
1437         float idx;\
1438         for(idx = 0; idx < 10; ++idx)\
1439                 weaponspace[idx] = 0;
1440
1441 entity weaponorder[WEP_MAXCOUNT];
1442 void weaponorder_swap(float i, float j, entity pass)
1443 {
1444         entity h;
1445         h = weaponorder[i];
1446         weaponorder[i] = weaponorder[j];
1447         weaponorder[j] = h;
1448 }
1449
1450 string weaponorder_cmp_str_save;
1451 string weaponorder_cmp_str;
1452 float weaponorder_cmp(float i, float j, entity pass)
1453 {
1454         float ai, aj;
1455         ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
1456         aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
1457         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)
1458 }
1459
1460 float GetAmmoStat(float i)
1461 {
1462         switch(i)
1463         {
1464                 case 0: return STAT_SHELLS;
1465                 case 1: return STAT_NAILS;
1466                 case 2: return STAT_ROCKETS;
1467                 case 3: return STAT_CELLS;
1468                 case 4: return STAT_FUEL;
1469                 default: return -1;
1470         }
1471 }
1472
1473 float GetAmmoTypeForWep(float i)
1474 {
1475         switch(i)
1476         {
1477                 case WEP_SHOTGUN: return 0;
1478                 case WEP_UZI: return 1;
1479                 case WEP_CAMPINGRIFLE: return 1;
1480                 case WEP_GRENADE_LAUNCHER: return 2;
1481                 case WEP_MINE_LAYER: return 2;
1482                 case WEP_ELECTRO: return 3;
1483                 case WEP_CRYLINK: return 3;
1484                 case WEP_HLAC: return 3;
1485                 case WEP_MINSTANEX: return 3;
1486                 case WEP_NEX: return 3;
1487                 case WEP_HAGAR: return 2;
1488                 case WEP_ROCKET_LAUNCHER: return 2;
1489                 case WEP_SEEKER: return 2;
1490                 case WEP_FIREBALL: return 4;
1491                 case WEP_HOOK: return 3;
1492                 default: return -1;
1493         }
1494 }
1495
1496 void HUD_Weapons(void)
1497 {
1498         if(!autocvar_hud_panel_weapons && !autocvar__hud_configure)
1499                 return;
1500
1501         float timeout = cvar("hud_panel_weapons_timeout");
1502         float timeout_effect_length, timein_effect_length;
1503         if (cvar("hud_panel_weapons_timeout_effect") == 0)
1504         {
1505                 timeout_effect_length = 0;
1506                 timein_effect_length = 0;
1507         }
1508         else
1509         {
1510                 timeout_effect_length = 0.75;
1511                 timein_effect_length = 0.375;
1512         }
1513
1514         if (timeout && time >= weapontime + timeout + timeout_effect_length && !autocvar__hud_configure)
1515         {
1516                 weaponprevtime = time;
1517                 return;
1518         }
1519
1520         active_panel = HUD_PANEL_WEAPONS;
1521         HUD_Panel_UpdateCvars(weapons);
1522
1523         if (timeout && time >= weapontime + timeout && !autocvar__hud_configure)
1524         {
1525                 float f = (time - (weapontime + timeout)) / timeout_effect_length;
1526                 if (cvar("hud_panel_weapons_timeout_effect"))
1527                 {
1528                         panel_bg_alpha *= (1 - f);
1529                         panel_fg_alpha *= (1 - f);
1530                 }
1531                 if (cvar("hud_panel_weapons_timeout_effect") == 1)
1532                 {
1533                         f *= f; // for a cooler movement
1534                         vector center;
1535                         center_x = panel_pos_x + panel_size_x/2;
1536                         center_y = panel_pos_y + panel_size_y/2;
1537                         float screen_ar = vid_conwidth/vid_conheight;
1538                         if (center_x/center_y < screen_ar) //bottom left
1539                         {
1540                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //bottom
1541                                         panel_pos_y += f * (vid_conheight - panel_pos_y);
1542                                 else //left
1543                                         panel_pos_x -= f * (panel_pos_x + panel_size_x);
1544                         }
1545                         else //top right
1546                         {
1547                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //right
1548                                         panel_pos_x += f * (vid_conwidth - panel_pos_x);
1549                                 else //top
1550                                         panel_pos_y -= f * (panel_pos_y + panel_size_y);
1551                         }
1552                 }
1553                 weaponprevtime = time - (1 - f) * timein_effect_length;
1554         }
1555         else if (timeout && time < weaponprevtime + timein_effect_length && !autocvar__hud_configure)
1556         {
1557                 float f = (time - weaponprevtime) / timein_effect_length;
1558                 if (cvar("hud_panel_weapons_timeout_effect"))
1559                 {
1560                         panel_bg_alpha *= (f);
1561                         panel_fg_alpha *= (f);
1562                 }
1563                 if (cvar("hud_panel_weapons_timeout_effect") == 1)
1564                 {
1565                         f *= f; // for a cooler movement
1566                         f = 1 - f;
1567                         vector center;
1568                         center_x = panel_pos_x + panel_size_x/2;
1569                         center_y = panel_pos_y + panel_size_y/2;
1570                         float screen_ar = vid_conwidth/vid_conheight;
1571                         if (center_x/center_y < screen_ar) //bottom left
1572                         {
1573                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //bottom
1574                                         panel_pos_y += f * (vid_conheight - panel_pos_y);
1575                                 else //left
1576                                         panel_pos_x -= f * (panel_pos_x + panel_size_x);
1577                         }
1578                         else //top right
1579                         {
1580                                 if ((vid_conwidth - center_x)/center_y < screen_ar) //right
1581                                         panel_pos_x += f * (vid_conwidth - panel_pos_x);
1582                                 else //top
1583                                         panel_pos_y -= f * (panel_pos_y + panel_size_y);
1584                         }
1585                 }
1586         }
1587
1588         float i, weapid, fade, weapon_stats, weapon_number, weapon_cnt;
1589         weapon_cnt = 0;
1590         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1591         {
1592                 self = get_weaponinfo(i);
1593                 if(self.impulse >= 0)
1594                         ++weapon_cnt;
1595         }
1596
1597         // TODO make this configurable
1598         weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
1599
1600         if(weaponorder_cmp_str != weaponorder_cmp_str_save)
1601         {
1602                 if(weaponorder_cmp_str_save)
1603                         strunzone(weaponorder_cmp_str_save);
1604                 weaponorder_cmp_str_save = strzone(weaponorder_cmp_str);
1605                 weapon_cnt = 0;
1606                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1607                 {
1608                         self = get_weaponinfo(i);
1609                         if(self.impulse >= 0)
1610                         {
1611                                 weaponorder[weapon_cnt] = self;
1612                                 ++weapon_cnt;
1613                         }
1614                 }
1615                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
1616         }
1617
1618         HUD_Panel_DrawBg(1);
1619         if(panel_bg_padding)
1620         {
1621                 panel_pos += '1 1 0' * panel_bg_padding;
1622                 panel_size -= '2 2 0' * panel_bg_padding;
1623         }
1624
1625         // hits
1626         weapon_stats = getstati(STAT_DAMAGE_HITS);
1627         weapon_number = weapon_stats & 63;
1628         weapon_hits[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1629         // fired
1630         weapon_stats = getstati(STAT_DAMAGE_FIRED);
1631         weapon_number = weapon_stats & 63;
1632         weapon_fired[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1633
1634         if(cvar_or("hud_panel_weapons_fade", 1))
1635         {
1636                 fade = 3.2 - 2 * (time - weapontime);
1637                 fade = bound(0.7, fade, 1);
1638         }
1639         else
1640                 fade = 1;
1641
1642         HUD_Weapons_Clear();
1643
1644         float rows, columns;
1645         rows = panel_size_y/panel_size_x;
1646         rows = bound(1, floor((sqrt(4 * autocvar_hud_panel_weapons_aspect * rows * WEP_COUNT + rows * rows) + rows + 0.5) / 2), WEP_COUNT);
1647
1648         columns = ceil(WEP_COUNT/rows);
1649         float row, column;
1650
1651         float a, type, fullammo;
1652         float when;
1653         when = autocvar_hud_panel_weapons_complainbubble_time;
1654         float fadetime;
1655         fadetime = autocvar_hud_panel_weapons_complainbubble_fadetime;
1656
1657         vector color;
1658         vector wpnpos;
1659         vector wpnsize;
1660
1661         for(i = 0; i < weapon_cnt; ++i)
1662         {
1663                 wpnpos = panel_pos + eX * column * panel_size_x*(1/columns) + eY * row * panel_size_y*(1/rows);
1664                 wpnsize = eX * panel_size_x*(1/columns) + eY * panel_size_y*(1/rows);
1665
1666                 self = weaponorder[i];
1667                 weapid = self.impulse;
1668
1669                 // draw background behind currently selected weapon
1670                 if(self.weapon == activeweapon)
1671                         drawpic_aspect_skin(wpnpos, "weapon_current_bg", wpnsize, '1 1 1', fade * panel_fg_alpha, DRAWFLAG_NORMAL);
1672
1673                 // draw the weapon accuracy
1674                 if(acc_levels)
1675                 {
1676                         float weapon_hit, weapon_damage;
1677                         weapon_damage = weapon_fired[self.weapon-WEP_FIRST];
1678                         if(weapon_damage)
1679                         {
1680                                 weapon_hit = weapon_hits[self.weapon-WEP_FIRST];
1681                                 weapon_stats = floor(100 * weapon_hit / weapon_damage);
1682                         }
1683
1684                         // find the max level lower than weapon_stats
1685                         float j;
1686                         j = acc_levels-1;
1687                         while ( j && weapon_stats < acc_lev[j] )
1688                                 --j;
1689
1690                         // inject color j+1 in color j, how much depending on how much weapon_stats is higher than level j
1691                         float factor;
1692                         factor = (weapon_stats - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
1693                         color = acc_color(j);
1694                         color = color + factor * (acc_color(j+1) - color);
1695
1696                         if(weapon_damage)
1697                                 drawpic_aspect_skin(wpnpos, "weapon_accuracy", wpnsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
1698                 }
1699
1700                 // draw the weapon icon
1701                 if((self.impulse >= 0) && (getstati(STAT_WEAPONS) & self.weapons))
1702                 {
1703                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '1 1 1', fade * panel_fg_alpha, DRAWFLAG_NORMAL);
1704
1705                         if(autocvar_hud_panel_weapons_label == 1) // weapon number
1706                                 drawstring(wpnpos, ftos(weapid), '1 1 0' * 0.5 * panel_size_y*(1/rows), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1707                         else if(autocvar_hud_panel_weapons_label == 2) // bind
1708                                 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);
1709
1710                         // draw ammo status bar
1711                         if(autocvar_hud_panel_weapons_ammo && weapid != WEP_TUBA && weapid != WEP_LASER && weapid != WEP_PORTO)
1712                         {
1713                                 a = 0;
1714                                 type = GetAmmoTypeForWep(weapid);
1715                                 if(type != -1)
1716                                         a = getstati(GetAmmoStat(type)); // how much ammo do we have?
1717
1718                                 if(a > 0)
1719                                 {
1720                                         switch(type) {
1721                                                 case 0: fullammo = autocvar_hud_panel_weapons_ammo_full_shells; break;
1722                                                 case 1: fullammo = autocvar_hud_panel_weapons_ammo_full_nails; break;
1723                                                 case 2: fullammo = autocvar_hud_panel_weapons_ammo_full_rockets; break;
1724                                                 case 3: fullammo = autocvar_hud_panel_weapons_ammo_full_cells; break;
1725                                                 case 4: fullammo = autocvar_hud_panel_weapons_ammo_full_fuel; break;
1726                                                 default: fullammo = 60;
1727                                         }
1728
1729                                         vector barsize;
1730                                         vector barpos;
1731                                         if(wpnsize_x/wpnsize_y > autocvar_hud_panel_weapons_aspect)
1732                                         {
1733                                                 barsize_x = autocvar_hud_panel_weapons_aspect * wpnsize_y;
1734                                                 barsize_y = wpnsize_y;
1735
1736                                                 barpos_x = wpnpos_x + (wpnsize_x - barsize_x) / 2;
1737                                                 barpos_y = wpnpos_y;
1738                                         }
1739                                         else
1740                                         {
1741                                                 barsize_y = 1/autocvar_hud_panel_weapons_aspect * wpnsize_x;
1742                                                 barsize_x = wpnsize_x;
1743
1744                                                 barpos_y = wpnpos_y + (wpnsize_y - barsize_y) / 2;
1745                                                 barpos_x = wpnpos_x;
1746                                         }
1747
1748                                         drawsetcliparea(
1749                                                 barpos_x,
1750                                                 barpos_y,
1751                                                 barsize_x * bound(0, a/fullammo, 1),
1752                                                 barsize_y);
1753                                         drawpic_aspect_skin(wpnpos, "weapon_ammo", wpnsize, autocvar_hud_panel_weapons_ammo_color, panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha, DRAWFLAG_NORMAL);
1754                                         drawresetcliparea();
1755                                 }
1756                         }
1757                 }
1758
1759                 // draw a "ghost weapon icon" if you don't have the weapon
1760                 else
1761                 {
1762                         drawpic_aspect_skin(wpnpos, strcat("weapon", self.netname), wpnsize, '0 0 0', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
1763                 }
1764
1765                 // draw the complain message
1766                 if(time - complain_weapon_time < when + fadetime && self.weapon == complain_weapon && autocvar_hud_panel_weapons_complainbubble)
1767                 {
1768                         if(fadetime)
1769                         {
1770                                 if(complain_weapon_time + when > time)
1771                                         a = 1;
1772                                 else
1773                                         a = bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1);
1774                         }
1775                         else
1776                         {
1777                                 if(complain_weapon_time + when > time)
1778                                         a = 1;
1779                                 else
1780                                         a = 0;
1781                         }
1782
1783                         string s;
1784                         if(complain_weapon_type == 0) {
1785                                 s = "Out of ammo";
1786                                 color = autocvar_hud_panel_weapons_complainbubble_color_outofammo;
1787                         }
1788                         else if(complain_weapon_type == 1) {
1789                                 s = "Don't have";
1790                                 color = autocvar_hud_panel_weapons_complainbubble_color_donthave;
1791                         }
1792                         else {
1793                                 s = "Unavailable";
1794                                 color = autocvar_hud_panel_weapons_complainbubble_color_unavailable;
1795                         }
1796                         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);
1797                         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);
1798                 }
1799
1800                 ++row;
1801                 if(row >= rows)
1802                 {
1803                         row = 0;
1804                         ++column;
1805                 }
1806         }
1807
1808 }
1809
1810 // Ammo (#1)
1811 //
1812 // TODO: macro
1813 float GetAmmoItemCode(float i)
1814 {
1815         switch(i)
1816         {
1817                 case 0: return IT_SHELLS;
1818                 case 1: return IT_NAILS;
1819                 case 2: return IT_ROCKETS;
1820                 case 3: return IT_CELLS;
1821                 case 4: return IT_FUEL;
1822                 default: return -1;
1823         }
1824 }
1825
1826 string GetAmmoPicture(float i)
1827 {
1828         switch(i)
1829         {
1830                 case 0: return "ammo_shells";
1831                 case 1: return "ammo_bullets";
1832                 case 2: return "ammo_rockets";
1833                 case 3: return "ammo_cells";
1834                 case 4: return "ammo_fuel";
1835                 default: return "";
1836         }
1837 }
1838
1839 void DrawAmmoItem(vector myPos, vector mySize, float itemcode, float currently_selected)
1840 {
1841         float a;
1842         a = getstati(GetAmmoStat(itemcode)); // how much ammo do we have of type itemcode?
1843         if(autocvar__hud_configure)
1844                 a = 100;
1845
1846         vector color;
1847         if(a < 10)
1848                 color = '0.7 0 0';
1849         else
1850                 color = '1 1 1';
1851
1852         float alpha;
1853         if(currently_selected)
1854                 alpha = 1;
1855         else
1856                 alpha = 0.7;
1857
1858         vector newSize, newPos;
1859         if(mySize_x/mySize_y > 3)
1860         {
1861                 newSize_x = 3 * mySize_y;
1862                 newSize_y = mySize_y;
1863
1864                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
1865                 newPos_y = myPos_y;
1866         }
1867         else
1868         {
1869                 newSize_y = 1/3 * mySize_x;
1870                 newSize_x = mySize_x;
1871
1872                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
1873                 newPos_x = myPos_x;
1874         }
1875
1876         vector picpos, numpos;
1877         if(autocvar_hud_panel_ammo_iconalign)
1878         {
1879                 numpos = newPos;
1880                 picpos = newPos + eX * 2 * newSize_y;
1881         }
1882         else
1883         {
1884                 numpos = newPos + eX * newSize_y;
1885                 picpos = newPos;
1886         }
1887
1888         if (currently_selected)
1889                 drawpic_aspect_skin(newPos, "ammo_current_bg", newSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1890
1891         drawfont = hud_bigfont;
1892         if(a > 0)
1893                 drawstring_aspect(numpos, ftos(a), eX * (2/3) * newSize_x + eY * newSize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1894         else // "ghost" ammo count
1895                 drawstring_aspect(numpos, ftos(a), eX * (2/3) * newSize_x + eY * newSize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1896         drawfont = hud_font;
1897         if(a > 0)
1898                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * newSize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
1899         else // "ghost" ammo icon
1900                 drawpic_aspect_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * newSize_y, '0 0 0', panel_fg_alpha * alpha * 0.5, DRAWFLAG_NORMAL);
1901 }
1902
1903 void HUD_Ammo(void)
1904 {
1905         if(!autocvar_hud_panel_ammo && !autocvar__hud_configure)
1906                 return;
1907
1908         active_panel = HUD_PANEL_AMMO;
1909         HUD_Panel_UpdateCvars(ammo);
1910         float i, currently_selected;
1911
1912         vector pos, mySize;
1913         pos = panel_pos;
1914         mySize = panel_size;
1915
1916         HUD_Panel_DrawBg(1);
1917         if(panel_bg_padding)
1918         {
1919                 pos += '1 1 0' * panel_bg_padding;
1920                 mySize -= '2 2 0' * panel_bg_padding;
1921         }
1922
1923         float rows, columns;
1924         rows = mySize_y/mySize_x;
1925         rows = bound(1, floor((sqrt(4 * (3/1) * rows * AMMO_COUNT + rows * rows) + rows + 0.5) / 2), AMMO_COUNT);
1926         //                               ^^^ ammo item aspect goes here
1927
1928         columns = ceil(AMMO_COUNT/rows);
1929
1930         float row, column;
1931         // ammo
1932         for (i = 0; i < AMMO_COUNT; ++i) {
1933                 currently_selected = getstati(STAT_ITEMS) & GetAmmoItemCode(i);
1934                 if(autocvar_hud_panel_ammo_onlycurrent) {
1935                         if(autocvar__hud_configure)
1936                                 i = 2;
1937                         if (currently_selected || autocvar__hud_configure)
1938                         {
1939                                 DrawAmmoItem(pos, mySize, i, currently_selected);
1940                                 break;
1941                         }
1942                 } else {
1943                         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);
1944                         ++row;
1945                         if(row >= rows)
1946                         {
1947                                 row = 0;
1948                                 column = column + 1;
1949                         }
1950                 }
1951         }
1952 }
1953
1954 void DrawNumIcon(float iconalign, vector myPos, vector mySize, float x, string icon, float left, vector color, float alpha)
1955 {
1956         vector newSize, newPos;
1957         if(mySize_x/mySize_y > 3)
1958         {
1959                 newSize_x = 3 * mySize_y;
1960                 newSize_y = mySize_y;
1961
1962                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
1963                 newPos_y = myPos_y;
1964         }
1965         else
1966         {
1967                 newSize_y = 1/3 * mySize_x;
1968                 newSize_x = mySize_x;
1969
1970                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
1971                 newPos_x = myPos_x;
1972         }
1973
1974         vector picpos, numpos;
1975         if(left)
1976         {
1977                 if(iconalign == 1 || iconalign == 3) // right align
1978                 {
1979                         numpos = newPos;
1980                         picpos = newPos + eX * 2 * newSize_y;
1981                 }
1982                 else // left align
1983                 {
1984                         numpos = newPos + eX * newSize_y;
1985                         picpos = newPos;
1986                 }
1987         }
1988         else
1989         {
1990                 if(iconalign == 0 || iconalign == 3) // left align
1991                 {
1992                         numpos = newPos + eX * newSize_y;
1993                         picpos = newPos;
1994                 } 
1995                 else // right align
1996                 {
1997                         numpos = newPos;
1998                         picpos = newPos + eX * 2 * newSize_y;
1999                 }
2000         }
2001
2002         drawfont = hud_bigfont;
2003         drawstring_aspect(numpos, ftos(x), eX * (2/3) * newSize_x + eY * newSize_y, color, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2004         drawfont = hud_font;
2005         drawpic_aspect_skin(picpos, icon, '1 1 0' * newSize_y, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
2006 }
2007
2008 void DrawNumIcon_expanding(float iconalign, vector myPos, vector mySize, float x, string icon, float left, vector color, float fadelerp)
2009 {
2010         float sz;
2011         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
2012
2013         DrawNumIcon(iconalign, myPos + expandingbox_resize_centered_box_offset(sz, mySize, 1), mySize * sz, x, icon, left, color, (1 - fadelerp));
2014 }
2015
2016 // Powerups (#2)
2017 //
2018 void HUD_Powerups(void) {
2019         if(!autocvar_hud_panel_powerups && !autocvar__hud_configure)
2020                 return;
2021
2022         active_panel = HUD_PANEL_POWERUPS;
2023         HUD_Panel_UpdateCvars(powerups);
2024         float stat_items;
2025         stat_items = getstati(STAT_ITEMS);
2026
2027         if(!autocvar__hud_configure)
2028         {
2029                 if not(stat_items & IT_STRENGTH)
2030                         if not(stat_items & IT_INVINCIBLE)
2031                                 return;
2032
2033                 if (getstati(STAT_HEALTH) <= 0)
2034                         return;
2035         }
2036
2037         vector pos, mySize;
2038         pos = panel_pos;
2039         mySize = panel_size;
2040
2041         float strength_time, shield_time;
2042         if(autocvar__hud_configure)
2043         {
2044                 strength_time = 15;
2045                 shield_time = 27;
2046         }
2047         else
2048         {
2049                 strength_time = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
2050                 shield_time = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
2051         }
2052
2053         HUD_Panel_DrawBg(bound(0, max(strength_time, shield_time), 1));
2054         if(panel_bg_padding)
2055         {
2056                 pos += '1 1 0' * panel_bg_padding;
2057                 mySize -= '2 2 0' * panel_bg_padding;
2058         }
2059
2060         vector barpos, barsize;
2061         vector picpos;
2062         vector numpos;
2063
2064         string leftname, rightname;
2065         float leftcnt, rightcnt;
2066         float leftexact, rightexact;
2067         if (autocvar_hud_panel_powerups_flip) {
2068                 leftname = "strength";
2069                 leftcnt = ceil(strength_time);
2070                 leftexact = strength_time;
2071
2072                 rightname = "shield";
2073                 rightcnt = ceil(shield_time);
2074                 rightexact = shield_time;
2075         } else {
2076                 leftname = "shield";
2077                 leftcnt = ceil(shield_time);
2078                 leftexact = shield_time;
2079
2080                 rightname = "strength";
2081                 rightcnt = ceil(strength_time);
2082                 rightexact = strength_time;
2083         }
2084
2085         drawfont = hud_bigfont;
2086         if (mySize_x/mySize_y > 4)
2087         {
2088                 if(leftcnt)
2089                 {
2090                         if(autocvar_hud_panel_powerups_baralign == 1 || autocvar_hud_panel_powerups_baralign == 3) { // right align
2091                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/30);
2092                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
2093                         } else { // left align
2094                                 barpos = pos;
2095                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
2096                         }
2097
2098                         if(autocvar_hud_panel_powerups_progressbar)
2099                         {
2100                                 HUD_Panel_GetProgressBarColor(leftname)
2101                                 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);
2102                         }
2103                         if(leftcnt > 1)
2104                                 DrawNumIcon(autocvar_hud_panel_powerups_iconalign, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, '1 1 1', 1);
2105                         if(leftcnt <= 5)
2106                                 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));
2107                 }
2108
2109                 if(rightcnt)
2110                 {
2111                         if(autocvar_hud_panel_powerups_baralign == 0 || autocvar_hud_panel_powerups_baralign == 3) { // left align
2112                                 barpos = pos + eX * 0.5 * mySize_x;
2113                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
2114                         } else { // right align
2115                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/30);
2116                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
2117                         }
2118
2119                         if(autocvar_hud_panel_powerups_progressbar)
2120                         {
2121                                 HUD_Panel_GetProgressBarColor(rightname)
2122                                 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);
2123                         }
2124                         if(rightcnt > 1)
2125                                 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);
2126                         if(rightcnt <= 5)
2127                                 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));
2128                 }
2129         }
2130         else if (mySize_x/mySize_y > 1.5)
2131         {
2132                 if(leftcnt)
2133                 {
2134                         if(autocvar_hud_panel_powerups_baralign == 1 || autocvar_hud_panel_powerups_baralign == 3) { // right align
2135                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/30);
2136                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
2137                         } else { // left align
2138                                 barpos = pos;
2139                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
2140                         }
2141
2142                         if(autocvar_hud_panel_powerups_progressbar)
2143                         {
2144                                 HUD_Panel_GetProgressBarColor(leftname)
2145                                 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);
2146                         }
2147                         if(leftcnt > 1)
2148                                 DrawNumIcon(autocvar_hud_panel_powerups_iconalign, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, '1 1 1', 1);
2149                         if(leftcnt <= 5)
2150                                 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));
2151                 }
2152
2153                 if(rightcnt)
2154                 {
2155                         if(autocvar_hud_panel_powerups_baralign == 0 || autocvar_hud_panel_powerups_baralign == 3) { // left align
2156                                 barpos = pos + eY * 0.5 * mySize_y;
2157                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
2158                         } else { // right align
2159                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
2160                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
2161                         }
2162
2163                         if(autocvar_hud_panel_powerups_progressbar)
2164                         {
2165                                 HUD_Panel_GetProgressBarColor(rightname)
2166                                 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);
2167                         }
2168                         if(rightcnt > 1)
2169                                 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);
2170                         if(rightcnt <= 5)
2171                                 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));
2172                 }
2173         }
2174         else
2175         {
2176                 if(leftcnt)
2177                 {
2178                         if(autocvar_hud_panel_powerups_baralign == 1 || autocvar_hud_panel_powerups_baralign == 3) { // down align
2179                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/30);
2180                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
2181                         } else { // up align
2182                                 barpos = pos;
2183                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
2184                         }
2185
2186                         if(autocvar_hud_panel_powerups_iconalign == 1 || autocvar_hud_panel_powerups_iconalign == 3) { // down align
2187                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
2188                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
2189                         } else { // up align
2190                                 picpos = pos + eX * 0.05 * mySize_x;
2191                                 numpos = pos + eY * 0.4 * mySize_x;
2192                         }
2193
2194                         if(autocvar_hud_panel_powerups_progressbar)
2195                         {
2196                                 HUD_Panel_GetProgressBarColor(leftname)
2197                                 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);
2198                         }
2199                         if(leftcnt <= 5)
2200                                 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));
2201                         if(leftcnt > 1)
2202                                 drawpic_aspect_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2203                         drawstring_aspect(numpos, ftos(leftcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2204                 }
2205
2206                 if(rightcnt)
2207                 {
2208                         if(autocvar_hud_panel_powerups_baralign == 0 || autocvar_hud_panel_powerups_baralign == 3) { // up align
2209                                 barpos = pos + eX * 0.5 * mySize_x;
2210                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
2211                         } else { // down align
2212                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/30) + eX * 0.5 * mySize_x;
2213                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
2214                         }
2215
2216                         if(autocvar_hud_panel_powerups_iconalign == 0 || autocvar_hud_panel_powerups_iconalign == 3) { // up align
2217                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2218                                 numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2219                         } else { // down align
2220                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
2221                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
2222                         }
2223
2224                         if(autocvar_hud_panel_powerups_progressbar)
2225                         {
2226                                 HUD_Panel_GetProgressBarColor(rightname)
2227                                 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);
2228                         }
2229                         if(rightcnt <= 5)
2230                                 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));
2231                         if(rightcnt > 1)
2232                                 drawpic_aspect_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2233                         drawstring_aspect(numpos, ftos(rightcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
2234                 }
2235         }
2236         drawfont = hud_font;
2237 }
2238
2239 // Health/armor (#3)
2240 //
2241 void HUD_HealthArmor(void)
2242 {
2243         if(!autocvar_hud_panel_healtharmor && !autocvar__hud_configure)
2244                 return;
2245
2246         active_panel = HUD_PANEL_HEALTHARMOR;
2247         HUD_Panel_UpdateCvars(healtharmor);
2248         vector pos, mySize;
2249         pos = panel_pos;
2250         mySize = panel_size;
2251
2252         HUD_Panel_DrawBg(1);
2253         if(panel_bg_padding)
2254         {
2255                 pos += '1 1 0' * panel_bg_padding;
2256                 mySize -= '2 2 0' * panel_bg_padding;
2257         }
2258
2259         float armor, health, fuel;
2260         armor = getstati(STAT_ARMOR);
2261         health = getstati(STAT_HEALTH);
2262         fuel = getstati(STAT_FUEL);
2263
2264         if(autocvar__hud_configure)
2265         {
2266                 armor = 150;
2267                 health = 100;
2268                 fuel = 70;
2269         }
2270
2271         if(health <= 0)
2272                 return;
2273
2274         vector barpos, barsize;
2275         vector picpos;
2276         vector numpos;
2277
2278         drawfont = hud_bigfont;
2279         if(autocvar_hud_panel_healtharmor == 2) // combined health and armor display
2280         {
2281                 vector v;
2282                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
2283
2284                 float x;
2285                 x = floor(v_x + 1);
2286
2287                 if(autocvar_hud_panel_healtharmor_baralign == 1 || autocvar_hud_panel_healtharmor_baralign == 3) { // right align
2288                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, x/400);
2289                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
2290                 } else { // left align
2291                         barpos = pos;
2292                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
2293                 }
2294
2295                 string biggercount;
2296                 if(v_z) // NOT fully armored
2297                 {
2298                         biggercount = "health";
2299                         if(autocvar_hud_panel_healtharmor_progressbar)
2300                         {
2301                                 HUD_Panel_GetProgressBarColor("health")
2302                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2303                         }
2304                         if(armor)
2305                                 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);
2306                 }
2307                 else
2308                 {
2309                         biggercount = "armor";
2310                         if(autocvar_hud_panel_healtharmor_progressbar)
2311                         {
2312                                 HUD_Panel_GetProgressBarColor("armor")
2313                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2314                         }
2315                         if(health)
2316                                 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);
2317                 }
2318                 DrawNumIcon(autocvar_hud_panel_healtharmor_iconalign, pos, mySize, x, biggercount, 1, HUD_Get_Num_Color(x, 2 * 200), 1);
2319
2320                 // fuel
2321                 if(fuel)
2322                 {
2323                         if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2324                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2325                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2326                         } else {
2327                                 barpos = pos;
2328                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2329                         }
2330                         HUD_Panel_GetProgressBarColor("fuel")
2331                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2332                 }
2333         }
2334         else
2335         {
2336                 string leftname, rightname;
2337                 float leftcnt, rightcnt;
2338                 float leftactive, rightactive;
2339                 float leftalpha, rightalpha;
2340                 if (autocvar_hud_panel_healtharmor_flip) { // old style layout with armor left/top of health
2341                         leftname = "armor";
2342                         leftcnt = armor;
2343                         if(leftcnt)
2344                                 leftactive = 1;
2345                         leftalpha = min((armor+10)/55, 1);
2346
2347                         rightname = "health";
2348                         rightcnt = health;
2349                         rightactive = 1;
2350                         rightalpha = 1;
2351                 } else {
2352                         leftname = "health";
2353                         leftcnt = health;
2354                         leftactive = 1;
2355                         leftalpha = 1;
2356
2357                         rightname = "armor";
2358                         rightcnt = armor;
2359                         if(rightcnt)
2360                                 rightactive = 1;
2361                         rightalpha = min((armor+10)/55, 1);
2362                 }
2363
2364                 if (mySize_x/mySize_y > 4)
2365                 {
2366                         if(leftactive)
2367                         {
2368                                 if(autocvar_hud_panel_healtharmor_baralign == 1 || autocvar_hud_panel_healtharmor_baralign == 3) { // right align
2369                                         barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/200);
2370                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
2371                                 } else { // left align
2372                                         barpos = pos;
2373                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
2374                                 }
2375
2376                                 if(autocvar_hud_panel_healtharmor_progressbar)
2377                                 {
2378                                         HUD_Panel_GetProgressBarColor(leftname)
2379                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2380                                 }
2381                                 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);
2382                         }
2383
2384                         if(rightactive)
2385                         {
2386                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2387                                         barpos = pos + eX * 0.5 * mySize_x;
2388                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
2389                                 } else { // right align
2390                                         barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/200);
2391                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
2392                                 }
2393
2394                                 if(autocvar_hud_panel_healtharmor_progressbar)
2395                                 {
2396                                         HUD_Panel_GetProgressBarColor(rightname)
2397                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2398                                 }
2399                                 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);
2400                         }
2401
2402                         if(fuel)
2403                         {
2404                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2405                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2406                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2407                                 } else {
2408                                         barpos = pos;
2409                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2410                                 }
2411                                 HUD_Panel_GetProgressBarColor("fuel")
2412                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2413                         }
2414                 }
2415                 else if (mySize_x/mySize_y > 1.5)
2416                 {
2417                         if(leftactive)
2418                         {
2419                                 if(autocvar_hud_panel_healtharmor_baralign == 1 || autocvar_hud_panel_healtharmor_baralign == 3) { // right align
2420                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/200);
2421                                         barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
2422                                 } else { // left align
2423                                         barpos = pos;
2424                                         barsize = eX * mySize_x * min(1, leftcnt/200) + eY * 0.5 * mySize_y;
2425                                 }
2426
2427                                 if(autocvar_hud_panel_healtharmor_progressbar)
2428                                 {
2429                                         HUD_Panel_GetProgressBarColor(leftname)
2430                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2431                                 }
2432                                 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);
2433                         }
2434
2435                         if(rightactive)
2436                         {
2437                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2438                                         barpos = pos + eY * 0.5 * mySize_y;
2439                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2440                                 } else { // right align
2441                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2442                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2443                                 }
2444
2445                                 if(autocvar_hud_panel_healtharmor_progressbar)
2446                                 {
2447                                         HUD_Panel_GetProgressBarColor(rightname)
2448                                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2449                                 }
2450                                 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);
2451                         }
2452
2453                         if(fuel)
2454                         {
2455                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2456                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2457                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2458                                 } else {
2459                                         barpos = pos;
2460                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2461                                 }
2462                                 HUD_Panel_GetProgressBarColor("fuel")
2463                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2464                         }
2465                 }
2466                 else
2467                 {
2468                         if(leftactive)
2469                         {
2470                                 if(autocvar_hud_panel_healtharmor_baralign == 1 || autocvar_hud_panel_healtharmor_baralign == 3) { // down align
2471                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/200);
2472                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2473                                 } else { // up align
2474                                         barpos = pos;
2475                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2476                                 }
2477
2478                                 if(autocvar_hud_panel_healtharmor_iconalign == 1 || autocvar_hud_panel_healtharmor_iconalign == 3) { // down align
2479                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
2480                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
2481                                 } else { // up align
2482                                         picpos = pos + eX * 0.05 * mySize_x;
2483                                         numpos = pos + eY * 0.4 * mySize_x;
2484                                 }
2485
2486                                 if(autocvar_hud_panel_healtharmor_progressbar)
2487                                 {
2488                                         HUD_Panel_GetProgressBarColor(leftname)
2489                                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2490                                 }
2491                                 drawpic_aspect_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2492                                 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);
2493                         }
2494
2495                         if(rightactive)
2496                         {
2497                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // up align
2498                                         barpos = pos + eX * 0.5 * mySize_x;
2499                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2500                                 } else { // down align
2501                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/200) + eX * 0.5 * mySize_x;
2502                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2503                                 }
2504
2505                                 if(autocvar_hud_panel_healtharmor_iconalign == 0 || autocvar_hud_panel_healtharmor_iconalign == 3) { // up align
2506                                         picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2507                                         numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2508                                 } else { // down align
2509                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
2510                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
2511                                 }
2512
2513                                 if(autocvar_hud_panel_healtharmor_progressbar)
2514                                 {
2515                                         HUD_Panel_GetProgressBarColor(rightname)
2516                                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2517                                 }
2518                                 drawpic_aspect_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * panel_fg_alpha, DRAWFLAG_NORMAL);
2519                                 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);
2520                         }
2521
2522                         if(fuel)
2523                         {
2524                                 if(autocvar_hud_panel_healtharmor_baralign == 0 || autocvar_hud_panel_healtharmor_baralign == 3) { // left align
2525                                         barpos = pos;
2526                                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2527                                 } else {
2528                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, fuel/100);
2529                                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2530                                 }
2531                                 HUD_Panel_GetProgressBarColor("fuel")
2532                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, progressbar_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
2533                         }
2534                 }
2535         }
2536         drawfont = hud_font;
2537 }
2538
2539 // Notification area (#4)
2540 //
2541
2542 string Weapon_SuicideMessage(float deathtype)
2543 {
2544         w_deathtype = deathtype;
2545         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_SUICIDEMESSAGE);
2546         return w_deathtypestring;
2547 }
2548
2549 string Weapon_KillMessage(float deathtype)
2550 {
2551         w_deathtype = deathtype;
2552         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_KILLMESSAGE);
2553         return w_deathtypestring;
2554 }
2555
2556 float killnotify_times[10];
2557 float killnotify_deathtype[10];
2558 float killnotify_actiontype[10]; // 0 = "Y [used by] X", 1 = "X [did action to] Y"
2559 string killnotify_attackers[10];
2560 string killnotify_victims[10];
2561 void HUD_KillNotify_Push(string attacker, string victim, float actiontype, float wpn)
2562 {
2563         float i;
2564         for (i = 9; i > 0; --i) {
2565                 killnotify_times[i] = killnotify_times[i-1];
2566                 killnotify_deathtype[i] = killnotify_deathtype[i-1];
2567                 killnotify_actiontype[i] = killnotify_actiontype[i-1];
2568                 if(killnotify_attackers[i])
2569                         strunzone(killnotify_attackers[i]);
2570                 killnotify_attackers[i] = strzone(killnotify_attackers[i-1]);
2571                 if(killnotify_victims[i])
2572                         strunzone(killnotify_victims[i]);
2573                 killnotify_victims[i] = strzone(killnotify_victims[i-1]);
2574         }
2575         killnotify_times[0] = time;
2576         killnotify_deathtype[0] = wpn;
2577         killnotify_actiontype[0] = actiontype;
2578         if(killnotify_attackers[0])
2579                 strunzone(killnotify_attackers[0]);
2580         killnotify_attackers[0] = strzone(attacker);
2581         if(killnotify_victims[0])
2582                 strunzone(killnotify_victims[0]);
2583         killnotify_victims[0] = strzone(victim);
2584 }
2585
2586 void HUD_KillNotify(string s1, string s2, string s3, float type, float msg) // s1 = attacker, s2 = victim
2587 {
2588         float w;
2589         float alsoprint, gentle;
2590         alsoprint = (autocvar_hud_panel_notify_print || !panel_enabled); // print message to console if: notify panel disabled, or cvar to do so enabled
2591         gentle = (autocvar_cl_gentle || autocvar_cl_gentle_messages);
2592         
2593         if(msg == MSG_SUICIDE) {
2594                 w = DEATH_WEAPONOF(type);
2595                 if(WEP_VALID(w)) {
2596                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2597                         if (alsoprint)
2598                                 print("^1", sprintf(Weapon_SuicideMessage(type), strcat(s1, "^1")), "\n");
2599                 } else if (type == DEATH_KILL) {
2600                         HUD_KillNotify_Push(s1, "", 0, DEATH_KILL);
2601                         if (alsoprint)
2602                                 print ("^1",s1, "^1 couldn't take it anymore\n");
2603                 } else if (type == DEATH_ROT) {
2604                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2605                         if (alsoprint)
2606                                 print ("^1",s1, "^1 died\n");
2607                 } else if (type == DEATH_NOAMMO) {
2608                         HUD_KillNotify_Push(s1, "", 0, DEATH_NOAMMO);
2609                         if (alsoprint)
2610                                 print ("^7",s1, "^7 committed suicide. What's the point of living without ammo?\n");
2611                 } else if (type == DEATH_CAMP) {
2612                         HUD_KillNotify_Push(s1, "", 0, DEATH_CAMP);
2613                         if (alsoprint)
2614                                 print ("^1",s1, "^1 thought they found a nice camping ground\n");
2615                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2616                         HUD_KillNotify_Push(s1, "", 0, type);
2617                         if (alsoprint)
2618                                 print ("^1",s1, "^1 didn't become friends with the Lord of Teamplay\n");
2619                 } else if (type == DEATH_CHEAT) {
2620                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2621                         if (alsoprint)
2622                                 print ("^1",s1, "^1 unfairly eliminated themself\n");
2623                 } else if (type == DEATH_FIRE) {
2624                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2625                         if (alsoprint)
2626                                 print ("^1",s1, "^1 burned to death\n");
2627                 } else if (type != DEATH_TEAMCHANGE && type != DEATH_QUIET) {
2628                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2629                         if (alsoprint)
2630                                 print ("^1",s1, "^1 couldn't resist the urge to self-destruct\n");
2631                 } 
2632                 
2633                 if (stof(s2) > 2) // killcount > 2
2634                         print ("^1",s1,"^1 ended it all after a ",s2," kill spree\n");
2635         } else if(msg == MSG_KILL) {
2636                 w = DEATH_WEAPONOF(type);
2637                 if(WEP_VALID(w)) {
2638                         HUD_KillNotify_Push(s1, s2, 1, type);
2639                         if (alsoprint)
2640                                 print("^1", sprintf(Weapon_KillMessage(type), strcat(s2, "^1"), strcat(s1, "^1")), "\n"); // default order: victim, killer
2641                 }
2642                 else if(type == KILL_TEAM_RED || type == KILL_TEAM_BLUE || type == KILL_TEAM_SPREE) {
2643                         HUD_KillNotify_Push(s1, s2, 1, type);
2644                         if(alsoprint)
2645                         {
2646                                 if(gentle) {
2647                                         print ("^1", s1, "^1 took action against a team mate\n");
2648                                 } else {
2649                                         print ("^1", s1, "^1 mows down a team mate\n");
2650                                 }
2651                         }
2652                         if (stof(s2) > 2 && type == KILL_TEAM_SPREE) {
2653                                 if(gentle)
2654                                         print ("^1",s1,"^1 ended a ",s3," scoring spree by going against a team mate\n");
2655                                 else
2656                                         print ("^1",s1,"^1 ended a ",s3," kill spree by killing a team mate\n");
2657                         }
2658                         else if (stof(s2) > 2) {
2659                                 if(gentle)
2660                                         print ("^1",s1,"'s ^1",s3," scoring spree was ended by a team mate!\n");
2661                                 else
2662                                         print ("^1",s1,"'s ^1",s3," kill spree was ended by a team mate!\n");
2663                         }
2664                 }
2665                 else if(type == KILL_FIRST_BLOOD)
2666                         print("^1",s1, "^1 drew first blood", "\n");
2667                 else if (type == DEATH_TELEFRAG) {
2668                         HUD_KillNotify_Push(s1, s2, 1, DEATH_TELEFRAG);
2669                         if(gentle)
2670                                 print ("^1",s2, "^1 tried to occupy ", s1, "^1's teleport destination space\n");
2671                         else
2672                                 print ("^1",s2, "^1 was telefragged by ", s1, "\n");
2673                 }
2674                 else if (type == DEATH_DROWN) {
2675                         HUD_KillNotify_Push(s1, s2, 1, DEATH_DROWN);
2676                         if(alsoprint)
2677                                 print ("^1",s2, "^1 was drowned by ", s1, "\n");
2678                 }
2679                 else if (type == DEATH_SLIME) {
2680                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SLIME);
2681                         if(alsoprint)
2682                                 print ("^1",s2, "^1 was slimed by ", s1, "\n");
2683                 }
2684                 else if (type == DEATH_LAVA) {
2685                         HUD_KillNotify_Push(s1, s2, 1, DEATH_LAVA);
2686                         if(alsoprint)
2687                                 print ("^1",s2, "^1 was cooked by ", s1, "\n");
2688                 }
2689                 else if (type == DEATH_FALL) {
2690                         HUD_KillNotify_Push(s1, s2, 1, DEATH_FALL);
2691                         if(alsoprint)
2692                                 print ("^1",s2, "^1 was grounded by ", s1, "\n");
2693                 }
2694                 else if (type == DEATH_SHOOTING_STAR) {
2695                         HUD_KillNotify_Push(s1, s2, 1, DEATH_SHOOTING_STAR);
2696                         if(alsoprint)
2697                                 print ("^1",s2, "^1 was shot into space by ", s1, "\n");
2698                 }
2699                 else if (type == DEATH_SWAMP) {
2700                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2701                         if(alsoprint)
2702                                 print ("^1",s2, "^1 was conserved by ", s1, "\n");
2703                 }
2704                 else if (type == DEATH_HURTTRIGGER)
2705                 {
2706                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2707                         if(alsoprint)
2708                                 print("^1",s2, "^1 was thrown into a world of hurt by ", s1, "\n");
2709                 } else if(type == DEATH_SBCRUSH) {
2710                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2711                         if(alsoprint)
2712                                 print ("^1",s2, "^1 was crushed by ^1", s1, "\n");
2713                 } else if(type == DEATH_SBMINIGUN) {
2714                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2715                         if(alsoprint)
2716                                 print ("^1",s2, "^1 got shredded by ^1", s1, "\n");
2717                 } else if(type == DEATH_SBROCKET) {
2718                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2719                         if(alsoprint)
2720                                 print ("^1",s2, "^1 was blased to bits by ^1", s1, "\n");
2721                 } else if(type == DEATH_SBBLOWUP) {
2722                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2723                         if(alsoprint)
2724                                 print ("^1",s2, "^1 got caught in the destruction of ^1", s1, "'s vehicle\n");
2725                 } else if(type == DEATH_WAKIGUN) {
2726                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2727                         if(alsoprint)
2728                                 print ("^1",s2, "^1 was bolted down by ^1", s1, "\n");
2729                 } else if(type == DEATH_WAKIROCKET) {
2730                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2731                         if(alsoprint)
2732                                 print ("^1",s2, "^1 could find no shelter from ^1", s1, "'s rockets\n");
2733                 } else if(type == DEATH_WAKIBLOWUP) {
2734                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2735                         if(alsoprint)
2736                                 print ("^1",s2, "^1 dies when ^1", s1, "'s wakizashi dies.\n");
2737                 } else if(type == DEATH_TURRET) {
2738                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2739                         if(alsoprint)
2740                                 print ("^1",s2, "^1 was pushed into the line of fire by ^1", s1, "\n");
2741                 } else if(type == DEATH_TOUCHEXPLODE) {
2742                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2743                         if(alsoprint)
2744                                 print ("^1",s2, "^1 was pushed into an accident by ^1", s1, "\n");
2745                 } else if(type == DEATH_CHEAT) {
2746                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2747                         if(alsoprint)
2748                                 print ("^1",s2, "^1 was unfairly eliminated by ^1", s1, "\n");
2749                 } else if (type == DEATH_FIRE) {
2750                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2751                         if(alsoprint)
2752                                 print ("^1",s2, "^1 was burnt to death by ^1", s1, "\n");
2753                 } else if (type == DEATH_CUSTOM) {
2754                         HUD_KillNotify_Push(s1, s2, 1, DEATH_CUSTOM);
2755                         if(alsoprint)
2756                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2757                 } else if (type == DEATH_HURTTRIGGER) {
2758                         HUD_KillNotify_Push(s1, s2, 1, DEATH_HURTTRIGGER);
2759                         if(alsoprint)
2760                                 print("^1", sprintf(s3, strcat(s2, "^1"), strcat(s1, "^1")), "\n");
2761                 } else {
2762                         HUD_KillNotify_Push(s1, s2, 1, DEATH_GENERIC);
2763                         if(alsoprint)
2764                                 print ("^1",s2, "^1 was fragged by ", s1, "\n");
2765                 }
2766         } else if(msg == MSG_SPREE) {
2767                 if(type == KILL_END_SPREE) {
2768                         if(gentle)
2769                                 print ("^1",s1,"'s ^1", s2, " scoring spree was ended by ", s3, "\n");
2770                         else
2771                                 print ("^1",s1,"'s ^1", s2, " kill spree was ended by ", s3, "\n");
2772    &nb