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