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