]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qc
add new cvars to ExportCfg, do some cleanups and initial work on aspect forcing on...
[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         vector dX, dY;
18         vector width, height;
19         vector bW, bH;
20         //pic = draw_UseSkinFor(pic);
21         width = eX * theSize_x;
22         height = eY * theSize_y;
23         if(theSize_x <= theBorderSize_x * 2)
24         {
25                 // not wide enough... draw just left and right then
26                 bW = eX * (0.25 * theSize_x / (theBorderSize_x * 2));
27                 if(theSize_y <= theBorderSize_y * 2)
28                 {
29                         // not high enough... draw just corners
30                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
31                         drawsubpic(theOrigin,                 width * 0.5 + height * 0.5, pic, '0 0 0',           bW + bH, theColor, theAlpha, 0);
32                         drawsubpic(theOrigin + width   * 0.5, width * 0.5 + height * 0.5, pic, eX - bW,           bW + bH, theColor, theAlpha, 0);
33                         drawsubpic(theOrigin + height  * 0.5, width * 0.5 + height * 0.5, pic, eY - bH,           bW + bH, theColor, theAlpha, 0);
34                         drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
35                 }
36                 else
37                 {
38                         dY = theBorderSize_x * eY;
39                         drawsubpic(theOrigin,                             width * 0.5          +     dY, pic, '0 0    0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
40                         drawsubpic(theOrigin + width * 0.5,               width * 0.5          +     dY, pic, '0 0    0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
41                         drawsubpic(theOrigin                        + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0',           '0 0.5  0' + bW, theColor, theAlpha, 0);
42                         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);
43                         drawsubpic(theOrigin               + height - dY, width * 0.5          +     dY, pic, '0 0.75 0',           '0 0.25 0' + bW, theColor, theAlpha, 0);
44                         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);
45                 }
46         }
47         else
48         {
49                 if(theSize_y <= theBorderSize_y * 2)
50                 {
51                         // not high enough... draw just top and bottom then
52                         bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
53                         dX = theBorderSize_x * eX;
54                         drawsubpic(theOrigin,                                         dX + height * 0.5, pic, '0    0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
55                         drawsubpic(theOrigin + dX,                        width - 2 * dX + height * 0.5, pic, '0.25 0 0',           '0.5  0 0' + bH, theColor, theAlpha, 0);
56                         drawsubpic(theOrigin + width - dX,                            dX + height * 0.5, pic, '0.75 0 0',           '0.25 0 0' + bH, theColor, theAlpha, 0);
57                         drawsubpic(theOrigin              + height * 0.5,             dX + height * 0.5, pic, '0    0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
58                         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);
59                         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);
60                 }
61                 else
62                 {
63                         dX = theBorderSize_x * eX;
64                         dY = theBorderSize_x * eY;
65                         drawsubpic(theOrigin,                                        dX          +     dY, pic, '0    0    0', '0.25 0.25 0', theColor, theAlpha, 0);
66                         drawsubpic(theOrigin                  + dX,      width - 2 * dX          +     dY, pic, '0.25 0    0', '0.5  0.25 0', theColor, theAlpha, 0);
67                         drawsubpic(theOrigin          + width - dX,                  dX          +     dY, pic, '0.75 0    0', '0.25 0.25 0', theColor, theAlpha, 0);
68                         drawsubpic(theOrigin          + dY,                          dX + height - 2 * dY, pic, '0    0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
69                         drawsubpic(theOrigin          + dY         + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5  0.5  0', theColor, theAlpha, 0);
70                         drawsubpic(theOrigin          + dY + width - dX,             dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5  0', theColor, theAlpha, 0);
71                         drawsubpic(theOrigin + height - dY,                          dX          +     dY, pic, '0    0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
72                         drawsubpic(theOrigin + height - dY         + dX, width - 2 * dX          +     dY, pic, '0.25 0.75 0', '0.5  0.25 0', theColor, theAlpha, 0);
73                         drawsubpic(theOrigin + height - dY + width - dX,             dX          +     dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
74                 }
75         }
76 }
77
78 // drawpic wrapper to draw an image as large as possible with preserved aspect ratio into a box
79 void drawpic_aspect(vector pos, string pic, vector sz, vector color, float alpha, float drawflag) {
80         vector imgsize;
81         imgsize = drawgetimagesize(pic);
82         float imgaspect;
83         imgaspect = imgsize_x/imgsize_y;
84
85         vector oldsz;
86         oldsz = sz;
87         float aspect;
88         aspect = sz_x/sz_y;
89
90         if(aspect > imgaspect) {
91                 sz_x = sz_y * imgaspect;
92                 drawpic(pos + eX * (oldsz_x - sz_x) * 0.5, pic, sz, color, alpha, drawflag);
93         } else {
94                 sz_y = sz_x / imgaspect; 
95                 drawpic(pos + eY * (oldsz_y - sz_y) * 0.5, pic, sz, color, alpha, drawflag);
96         }
97 }
98
99 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
100 // TODO: make a default skin, and fallback to these!
101 void drawpic_skin(vector pos, string pic, vector sz, vector color, float alpha, float drawflag) {
102         drawpic_aspect(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, color, alpha, drawflag);
103 }
104
105 // TODO: aspect!
106 void drawpic_skin_expanding(vector pos, string pic, vector sz, vector rgb, float alpha, float flag, float fadelerp) {
107         return;
108         //drawpic_aspect_expanding(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, rgb, alpha, flag, fadelerp);
109 }
110
111 void drawpic_skin_expanding_two(vector pos, string pic, vector sz, vector rgb, float alpha, float flag, float fadelerp) {
112         return;
113         //drawpic_aspect_expanding_two(pos, strcat("gfx/hud/", cvar_string("hud_skin"), "/", pic), sz, rgb, alpha, flag, fadelerp);
114 }
115
116 // return HUD background color
117 vector HUD_GetBgColor()
118 {
119         vector color;
120         if (teamplay)
121                 GetTeamRGB(myteam) * hud_color_bg_team;
122         else {
123                 // allow custom HUD colors in non-teamgames
124                 color_x = cvar("hud_color_bg_r");
125                 color_y = cvar("hud_color_bg_g");
126                 color_z = cvar("hud_color_bg_b");
127         }
128         return color;
129 }
130
131 // return accuracy text color
132 vector HUD_AccuracyColor(float accuracy)
133 {
134         vector rgb;
135         float yellow_accuracy = cvar("hud_weaponicons_accuracy_yellow"); // value at which this function returns yellow
136         if(accuracy >= 100) {
137                 rgb_x = 0;
138                 rgb_y = 1;
139         }
140         else if(accuracy > yellow_accuracy) {
141                 rgb_x = 1 - (accuracy-yellow_accuracy)/(100-yellow_accuracy); // red value between 1 -> 0
142                 rgb_y = 1;
143         }
144         else {
145                 rgb_x = 1;
146                 rgb_y = accuracy/yellow_accuracy; // green value between 0 -> 1
147         }
148         rgb_z = 0;
149         return rgb;
150 }
151
152 vector HUD_Get_Num_Color (float x, float maxvalue)
153 {
154         vector color;
155         if(x > maxvalue) {
156                 color_x = 0;
157                 color_y = 1;
158                 color_z = 0;
159         }
160         else if(x > maxvalue * 0.75) {
161                 color_x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
162                 color_y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
163                 color_z = 0;
164         }
165         else if(x > maxvalue * 0.5) {
166                 color_x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
167                 color_y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
168                 color_z = 1 - (x-100)*0.02; // blue value between 1 -> 0
169         }
170         else if(x > maxvalue * 0.25) {
171                 color_x = 1;
172                 color_y = 1;
173                 color_z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
174         }
175         else if(x > maxvalue * 0.1) {
176                 color_x = 1;
177                 color_y = (x-20)*90/27/100; // green value between 0 -> 1
178                 color_z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
179         }
180         else {
181                 color_x = 1;
182                 color_y = 0;
183                 color_z = 0;
184         }
185         return color;
186 }
187
188 float stringwidth_colors(string s, vector theSize)
189 {
190         return stringwidth(s, TRUE, theSize);
191 }
192
193 float stringwidth_nocolors(string s, vector theSize)
194 {
195         return stringwidth(s, FALSE, theSize);
196 }
197
198 #define CENTERPRINT_MAX_LINES 30
199 string centerprint_messages[CENTERPRINT_MAX_LINES];
200 float centerprint_width[CENTERPRINT_MAX_LINES];
201 vector centerprint_start;
202 float centerprint_expire;
203 float centerprint_num;
204 float centerprint_offset_hint;
205 vector centerprint_fontsize;
206
207 void centerprint(string strMessage)
208 {
209         float i, j, n, hcount;
210         string s;
211
212         centerprint_fontsize = HUD_GetFontsize("scr_centersize");
213
214         centerprint_expire = min(centerprint_expire, time); // if any of the returns happens, this message will fade out
215
216         if(cvar("scr_centertime") <= 0)
217                 return;
218
219         if(strMessage == "")
220                 return;
221
222         // strip trailing newlines
223         j = strlen(strMessage) - 1;
224         while(substring(strMessage, j, 1) == "\n" && j >= 0)
225                 j = j - 1;
226         strMessage = substring(strMessage, 0, j + 1);
227
228         if(strMessage == "")
229                 return;
230
231         // strip leading newlines and remember them, they are a hint that the message should be lower on the screen
232         j = 0;
233         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
234                 j = j + 1;
235         strMessage = substring(strMessage, j, strlen(strMessage) - j);
236         centerprint_offset_hint = j;
237
238         if(strMessage == "")
239                 return;
240
241         // if we get here, we have a message. Initialize its height.
242         centerprint_num = 0;
243
244         n = tokenizebyseparator(strMessage, "\n");
245         i = hcount = 0;
246         for(j = 0; j < n; ++j)
247         {
248                 getWrappedLine_remaining = argv(j);
249                 while(getWrappedLine_remaining)
250                 {
251                         s = getWrappedLine(vid_conwidth * 0.75, centerprint_fontsize, stringwidth_colors);
252                         if(centerprint_messages[i])
253                                 strunzone(centerprint_messages[i]);
254                         centerprint_messages[i] = strzone(s);
255                         centerprint_width[i] = stringwidth(s, TRUE, centerprint_fontsize);
256                         ++i;
257
258                         // half height for empty lines looks better
259                         if(s == "")
260                                 hcount += 0.5;
261                         else
262                                 hcount += 1;
263
264                         if(i >= CENTERPRINT_MAX_LINES)
265                                 break;
266                 }
267         }
268
269         float h, havail;
270         h = centerprint_fontsize_y*hcount;
271
272         havail = vid_conheight;
273         if(cvar("con_chatpos") < 0)
274                 havail -= (-cvar("con_chatpos") + cvar("con_chat")) * cvar("con_chatsize"); // avoid overlapping chat
275         if(havail > vid_conheight - 70)
276                 havail = vid_conheight - 70; // avoid overlapping HUD
277
278         centerprint_start_x = 0;
279
280 #if 0
281         float forbiddenmin, forbiddenmax, allowedmin, allowedmax, preferred;
282
283         // here, the centerprint would cover the crosshair. REALLY BAD.
284         forbiddenmin = vid_conheight * 0.5 - h - 16;
285         forbiddenmax = vid_conheight * 0.5 + 16;
286
287         allowedmin = scoreboard_bottom;
288         allowedmax = havail - h;
289         preferred = (havail - h)/2;
290
291
292         // possible orderings (total: 4! / 4 = 6)
293         //  allowedmin allowedmax forbiddenmin forbiddenmax
294         //  forbiddenmin forbiddenmax allowedmin allowedmax
295         if(allowedmax < forbiddenmin || allowedmin > forbiddenmax)
296         {
297                 // forbidden doesn't matter in this case
298                 centerprint_start_y = bound(allowedmin, preferred, allowedmax);
299         }
300         //  allowedmin forbiddenmin allowedmax forbiddenmax
301         else if(allowedmin < forbiddenmin && allowedmax < forbiddenmax)
302         {
303                 centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
304         }
305         //  allowedmin forbiddenmin forbiddenmax allowedmax
306         else if(allowedmin < forbiddenmin)
307         {
308                 // make sure the forbidden zone is not covered
309                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
310                         centerprint_start_y = bound(allowedmin, preferred, forbiddenmin);
311                 else
312                         centerprint_start_y = bound(forbiddenmax, preferred, allowedmin);
313         }
314         //  forbiddenmin allowedmin allowedmax forbiddenmax
315         else if(allowedmax < forbiddenmax)
316         {
317                 // it's better to leave the allowed zone (overlap with scoreboard) than
318                 // to cover the forbidden zone (crosshair)
319                 if(preferred > (forbiddenmin + forbiddenmax) * 0.5)
320                         centerprint_start_y = forbiddenmax;
321                 else
322                         centerprint_start_y = forbiddenmin;
323         }
324         //  forbiddenmin allowedmin forbiddenmax allowedmax
325         else
326         {
327                 centerprint_start_y = bound(forbiddenmax, preferred, allowedmax);
328         }
329 #else
330         centerprint_start_y =
331                 min(
332                         max(
333                                 max(scoreboard_bottom, vid_conheight * 0.5 + 16),
334                                 (havail - h)/2
335                         ),
336                         havail - h
337                 );
338 #endif
339
340         centerprint_num = i;
341         centerprint_expire = time + cvar("scr_centertime");
342 }
343
344 void HUD_DrawCenterPrint (void)
345 {
346         float i;
347         vector pos;
348         string ts;
349         float a;
350
351         //if(time > centerprint_expire)
352         //      return;
353
354         //a = bound(0, 1 - 2 * (time - centerprint_expire), 1);
355         a = bound(0, 1 - 4 * (time - centerprint_expire), 1);
356         //sz = 1.2 / (a + 0.2);
357
358         if(a <= 0)
359                 return;
360
361         pos = centerprint_start;
362         for (i=0; i<centerprint_num; i = i + 1)
363         {
364                 pos_x = (vid_conwidth - centerprint_width[i]) * 0.5;
365                 ts = centerprint_messages[i];
366                 if (ts != "")
367                 {
368                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
369                         drawcolorcodedstring(pos, ts, centerprint_fontsize, a, DRAWFLAG_NORMAL);
370                         //  - '0 0.5 0' * (sz - 1) * centerprint_fontsize_x - '0.5 0 0' * (sz - 1) * centerprint_width[i] * centerprint_fontsize_y, centerprint_fontsize * sz
371                         pos_y = pos_y + centerprint_fontsize_y;
372                 }
373                 else
374                         // half height for empty lines looks better
375                         pos_y = pos_y + centerprint_fontsize_y * 0.5;
376         }
377 }
378
379 void drawstringright(vector position, string text, vector scale, vector rgb, float alpha, float flag)
380 {
381         position_x -= 2 / 3 * strlen(text) * scale_x;
382         drawstring(position, text, scale, rgb, alpha, flag);
383 }
384
385 void drawstringcenter(vector position, string text, vector scale, vector rgb, float alpha, float flag)
386 {
387         position_x = 0.5 * (vid_conwidth - 0.6025 * strlen(text) * scale_x);
388         drawstring(position, text, scale, rgb, alpha, flag);
389 }
390
391 // return the string of the given race place
392 string race_PlaceName(float pos) {
393         if(pos == 1)
394                 return "1st";
395         else if(pos == 2)
396                 return "2nd";
397         else if(pos == 3)
398                 return "3rd";
399         else
400                 return strcat(ftos(pos), "th");
401 }
402
403 // return the string of the onscreen race timer
404 string MakeRaceString(float cp, float mytime, float histime, float lapdelta, string hisname)
405 {
406         string col;
407         string timestr;
408         string cpname;
409         string lapstr;
410         lapstr = "";
411
412         if(histime == 0) // goal hit
413         {
414                 if(mytime > 0)
415                 {
416                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
417                         col = "^1";
418                 }
419                 else if(mytime == 0)
420                 {
421                         timestr = "+0.0";
422                         col = "^3";
423                 }
424                 else
425                 {
426                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
427                         col = "^2";
428                 }
429
430                 if(lapdelta > 0)
431                 {
432                         lapstr = strcat(" (-", ftos(lapdelta), "L)");
433                         col = "^2";
434                 }
435                 else if(lapdelta < 0)
436                 {
437                         lapstr = strcat(" (+", ftos(-lapdelta), "L)");
438                         col = "^1";
439                 }
440         }
441         else if(histime > 0) // anticipation
442         {
443                 if(mytime >= histime)
444                         timestr = strcat("+", ftos_decimals(mytime - histime, TIME_DECIMALS));
445                 else
446                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(histime));
447                 col = "^3";
448         }
449         else
450                 col = "^7";
451
452         if(cp == 254)
453                 cpname = "Start line";
454         else if(cp == 255)
455                 cpname = "Finish line";
456         else if(cp)
457                 cpname = strcat("Intermediate ", ftos(cp));
458         else
459                 cpname = "Finish line";
460
461         if(histime < 0)
462                 return strcat(col, cpname);
463         else if(hisname == "")
464                 return strcat(col, cpname, " (", timestr, ")");
465         else
466                 return strcat(col, cpname, " (", timestr, " ", strcat(hisname, col, lapstr), ")");
467 }
468
469 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
470 float race_CheckName(string net_name) {
471         float i;
472         for (i=RANKINGS_CNT-1;i>=0;--i)
473                 if(grecordholder[i] == net_name)
474                         return i+1;
475         return 0;
476 }
477
478 /*
479 ==================
480 HUD panels
481 ==================
482 */
483
484 // Save the config
485 void HUD_Panel_ExportCfg(string cfgname)
486 {
487         float fh;
488         fh = fopen(strcat("hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg"), FILE_WRITE);
489         if(fh >= 0)
490         {
491                 fputs(fh, strcat("seta hud_skin \"", cvar_string("hud_skin"), "\"", "\n"));
492                 fputs(fh, strcat("seta hud_bg \"", cvar_string("hud_bg"), "\"", "\n"));
493                 fputs(fh, strcat("seta hud_bg_color \"", cvar_string("hud_bg_color"), "\"", "\n"));
494                 fputs(fh, strcat("seta hud_bg_color_team \"", cvar_string("hud_bg_color_team"), "\"", "\n"));
495                 fputs(fh, strcat("seta hud_bg_alpha \"", cvar_string("hud_bg_alpha"), "\"", "\n"));
496                 fputs(fh, strcat("seta hud_bg_border \"", cvar_string("hud_bg_border"), "\"", "\n"));
497                 fputs(fh, strcat("seta hud_bg_padding \"", cvar_string("hud_bg_padding"), "\"", "\n"));
498                 fputs(fh, strcat("seta hud_fg_alpha \"", cvar_string("hud_fg_alpha"), "\"", "\n"));
499                 fputs(fh, "\n");
500
501                 fputs(fh, strcat("seta hud_dock \"", cvar_string("hud_dock"), "\"", "\n"));
502                 fputs(fh, strcat("seta hud_dock_color \"", cvar_string("hud_dock_color"), "\"", "\n"));
503                 fputs(fh, strcat("seta hud_dock_color_team \"", cvar_string("hud_dock_color_team"), "\"", "\n"));
504                 fputs(fh, strcat("seta hud_dock_alpha \"", ftos(cvar("hud_dock_alpha")), "\"", "\n"));
505                 fputs(fh, "\n");
506
507                 fputs(fh, strcat("seta hud_progressbar_alpha ", ftos(cvar("hud_progressbar_alpha")), "\n"));
508                 fputs(fh, strcat("seta hud_progressbar_strength_color \"", cvar_string("hud_progressbar_strength_color"), "\"", "\n"));
509                 fputs(fh, strcat("seta hud_progressbar_shield_color \"", cvar_string("hud_progressbar_shield_color"), "\"", "\n"));
510                 fputs(fh, strcat("seta hud_progressbar_health_color \"", cvar_string("hud_progressbar_health_color"), "\"", "\n"));
511                 fputs(fh, strcat("seta hud_progressbar_armor_color \"", cvar_string("hud_progressbar_armor_color"), "\"", "\n"));
512                 fputs(fh, strcat("seta hud_progressbar_fuel_color \"", cvar_string("hud_progressbar_fuel_color"), "\"", "\n"));
513                 fputs(fh, strcat("seta hud_progressbar_nexball_color \"", cvar_string("hud_progressbar_nexball_color"), "\"", "\n"));
514                 fputs(fh, "\n");
515
516                 // common cvars for all panels
517                 float i;
518                 for (i = 0; i < HUD_PANEL_NUM; ++i)
519                 {
520                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), " ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i)))), "\n"));
521                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_pos \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_pos")), "\"", "\n"));
522                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_size \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_size")), "\"", "\n"));
523                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg")), "\"", "\n"));
524                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_color \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_color")), "\"", "\n"));
525                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_color_team \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_color_team")), "\"", "\n"));
526                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_alpha \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_alpha")), "\"", "\n"));
527                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_border \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_border")), "\"", "\n"));
528                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_bg_padding \"", cvar_string(strcat("hud_", HUD_Panel_GetName(i), "_bg_padding")), "\"", "\n"));
529                         switch(i) {
530                                 case HUD_PANEL_WEAPONICONS:
531                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_accuracy_yellow ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_accuracy_yellow"))), "\n"));
532                                         break;
533                                 case HUD_PANEL_INVENTORY:
534                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_onlycurrent ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_onlycurrent"))), "\n"));
535                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_iconalign ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
536                                         break;
537                                 case HUD_PANEL_POWERUPS:
538                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
539                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_iconalign ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
540                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_baralign ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
541                                         break;
542                                 case HUD_PANEL_HEALTHARMOR:
543                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
544                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_iconalign ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
545                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_baralign ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_mirror"))), "\n"));
546                                         break;
547                                 case HUD_PANEL_NOTIFY:
548                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_flip ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_flip"))), "\n"));
549                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_info_top ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_info_top"))), "\n"));
550                                         break;
551                                 case HUD_PANEL_RADAR:
552                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_foreground_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_foreground_alpha"))), "\n"));
553                                 case HUD_PANEL_VOTE:
554                                         fputs(fh, strcat("seta hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha ", ftos(cvar(strcat("hud_", HUD_Panel_GetName(i), "_alreadyvoted_alpha"))), "\n"));
555                                         break;
556                         }
557                         fputs(fh, "\n");
558                 }
559
560                 print("^2Successfully exported to hud_", cvar_string("hud_skin"), "_", cfgname, ".cfg! (Note: It's saved in data/data/)\n");
561         }
562         fclose(fh);
563 }
564
565 // return active status of panel
566 float HUD_Panel_CheckActive(float id)
567 {
568         if (cvar_or(strcat("hud_", HUD_Panel_GetName(id)), 1))
569                 return 1;
570         return 0;
571 }
572
573 // return size of given panel
574 vector HUD_Panel_GetSize(float id)
575 {
576         vector mySize;
577         mySize = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_size")));
578
579         mySize = eX * mySize_x * vid_conwidth + eY * mySize_y * vid_conheight;
580
581         if(disable_menu_alphacheck == 2 && id == highlightedPanel)
582         {
583                 vector menu_enable_maxsize, menu_enable_size;
584                 menu_enable_maxsize = '0 0 0'; // shut up FTEQCC
585                 menu_enable_maxsize_x = 0.3 * vid_conwidth;
586                 menu_enable_maxsize_y = 0.18 * vid_conheight;
587                 if(mySize_x > mySize_y)
588                 {
589                         if(mySize_y > menu_enable_maxsize_y)
590                         {
591                                 menu_enable_size_y = menu_enable_maxsize_y;
592                                 menu_enable_size_x = mySize_x * (menu_enable_maxsize_y/mySize_y);
593                                 mySize = (1 - cvar("_menu_alpha")) * mySize + (cvar("_menu_alpha")) * menu_enable_size;
594                         }
595                 }
596                 else
597                 {
598                         if(mySize_x > menu_enable_maxsize_x)
599                         {
600                                 menu_enable_size_x = menu_enable_maxsize_x;
601                                 menu_enable_size_y = mySize_y * (menu_enable_maxsize_x/mySize_x);
602                                 mySize = (1 - cvar("_menu_alpha")) * mySize + (cvar("_menu_alpha")) * menu_enable_size;
603                         }
604                 }
605         }
606         return mySize;
607 }
608
609 // return pos of given panel
610 vector HUD_Panel_GetPos(float id)
611 {
612         vector pos;
613         pos = stov(cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_pos")));
614
615         pos = eX * pos_x * vid_conwidth + eY * pos_y * vid_conheight;
616
617         if(disable_menu_alphacheck == 2 && id == highlightedPanel)
618         {
619                 vector mySize, menu_enable_panelpos;
620                 mySize = HUD_Panel_GetSize(id);
621                 if(mySize_x > mySize_y)
622                         menu_enable_panelpos = eX * 0.5 * vid_conwidth - eX * 0.5 * mySize_x + eY * 0.82 * vid_conheight;
623                 else
624                         menu_enable_panelpos = eY * 0.5 * vid_conheight - eY * 0.5 * mySize_y + eX * 0.7 * vid_conwidth;
625                 pos = (1 - cvar("_menu_alpha")) * pos + (cvar("_menu_alpha")) * menu_enable_panelpos;
626         }
627         return pos;
628 }
629
630 float HUD_Panel_GetBorder(float id)
631 {
632         string border;
633         border = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_border"));
634         if(border == "")
635                 border = cvar_string("hud_bg_border");
636         return stof(border);
637 }
638
639 vector HUD_Panel_GetColor(float id)
640 {
641         float f;
642         vector color_vec;
643         string color;
644
645         // fetch per-panel color
646         if(teamplay && cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color_team")) != "") {
647                 f = stof(getplayerkey(self.sv_entnum, "colors"));
648                 color = vtos(colormapPaletteColor(mod(f, 16), 1) * cvar(strcat("hud_", HUD_Panel_GetName(id), "_bg_color_team")));
649         }
650         else
651                 color = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_color"));
652         color_vec = stov(color);
653
654         if(color == "") { // fetch default color
655                 color = cvar_string("hud_bg_color");
656                 color_vec = stov(color);
657                 if(teamplay && cvar(strcat("hud_bg_color_team"))) {
658                         f = stof(getplayerkey(self.sv_entnum, "colors"));
659                         color_vec = colormapPaletteColor(mod(f, 16), 1) * cvar("hud_bg_color_team");
660                 }
661                 else if(color == "shirt") {
662                         f = stof(getplayerkey(self.sv_entnum, "colors"));
663                         color_vec = colormapPaletteColor(floor(f / 16), 0);
664                 }
665                 else if(color == "pants") {
666                         f = stof(getplayerkey(self.sv_entnum, "colors"));
667                         color_vec = colormapPaletteColor(mod(f, 16), 1);
668                 }
669         }
670         else if(color == "shirt") {
671                 f = stof(getplayerkey(self.sv_entnum, "colors"));
672                 color_vec = colormapPaletteColor(floor(f / 16), 0);
673         }
674         else if(color == "pants") {
675                 f = stof(getplayerkey(self.sv_entnum, "colors"));
676                 color_vec = colormapPaletteColor(mod(f, 16), 1);
677         }
678         return color_vec;
679 }
680
681 vector HUD_Panel_Dock_GetColor(void)
682 {
683         float f;
684         vector color_vec;
685         string color;
686         color = cvar_string("hud_dock_color");
687         color_vec = stov(color);
688         if(teamplay && cvar(strcat("hud_dock_color_team"))) {
689                 f = stof(getplayerkey(self.sv_entnum, "colors"));
690                 color_vec = colormapPaletteColor(mod(f, 16), 1) * cvar("hud_dock_color_team");
691         }
692         else if(color == "shirt") {
693                 f = stof(getplayerkey(self.sv_entnum, "colors"));
694                 color_vec = colormapPaletteColor(floor(f / 16), 0);
695         }
696         else if(color == "pants") {
697                 f = stof(getplayerkey(self.sv_entnum, "colors"));
698                 color_vec = colormapPaletteColor(mod(f, 16), 1);
699         }
700         return color_vec;
701 }
702
703 float HUD_Panel_GetBgAlpha(float id)
704 {
705         string alpha;
706         alpha = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_alpha"));
707         if(alpha == "")
708                 alpha = cvar_string("hud_bg_alpha");
709
710         if(hud_configure && disable_menu_alphacheck == 2 && highlightedPanel == id) // do not set a minalpha cap when showing the config dialog for this panel
711                 alpha = ftos((1 - cvar("_menu_alpha")) * max(cvar("hud_configure_bg_minalpha"), stof(alpha)) + (cvar("_menu_alpha")) * stof(alpha));
712         else if(hud_configure)
713                 alpha = ftos(max(cvar("hud_configure_bg_minalpha"), stof(alpha)));
714
715         if(hud_configure && !cvar(strcat("hud_", HUD_Panel_GetName(id)))) // ALWAYS show disabled panels at 0.25 alpha when in config mode
716                 return 0.25;
717
718         if(disable_menu_alphacheck == 2 && highlightedPanel == id) // don't fade this panel when showing the panel-specific menu dialog
719                 return stof(alpha);
720         else
721                 return stof(alpha) * menu_fade_alpha;
722 }
723
724 float HUD_Panel_GetFgAlpha(float id)
725 {
726         float alpha;
727         alpha = hud_fg_alpha;
728
729         if(hud_configure && !cvar(strcat("hud_", HUD_Panel_GetName(id)))) // ALWAYS show disabled panels at 0.25 alpha when in config mode
730                 return 0.25;
731
732         if(disable_menu_alphacheck == 2 && highlightedPanel == id) // don't fade this panel when showing the panel-specific menu dialog
733                 return alpha;
734         else
735                 return alpha * menu_fade_alpha;
736 }
737
738 float HUD_Panel_GetPadding(float id)
739 {
740         string padding;
741         padding = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg_padding"));
742         if(padding == "")
743                 padding = cvar_string("hud_bg_padding");
744
745         vector mySize;
746         mySize = HUD_Panel_GetSize(id);
747         float smallestsize;
748         smallestsize = min(mySize_x, mySize_y);
749         return min(smallestsize/2 - 5, stof(padding));
750 }
751
752 // draw the background/borders
753 void HUD_Panel_DrawBg(float id, vector pos, vector mySize, float alpha)
754 {
755         if(!hud_configure && cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg")) == "0")
756                 return;
757
758         string bg;
759         bg = cvar_string(strcat("hud_", HUD_Panel_GetName(id), "_bg"));
760         if(bg == "")
761                 bg = cvar_string("hud_bg");
762
763         if(bg == "0" && hud_configure) {
764                 bg = "border"; // we probably want to see a background in config mode at all times...
765         }
766
767         if(bg != "0")
768         {
769                 float border;
770                 border = max(0.0000001, HUD_Panel_GetBorder(id)); // draw_BorderPicture does not like border = 0
771
772                 vector color;
773                 color = HUD_Panel_GetColor(id);
774
775                 if(alpha)
776                         alpha = HUD_Panel_GetBgAlpha(id) * alpha; // allow panels to fade in/out by passing an alpha value
777                 else
778                         alpha = HUD_Panel_GetBgAlpha(id);
779
780                 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));
781         }
782 }
783
784 void HUD_Panel_DrawProgressBar(vector pos, float vertical, vector mySize, vector color, float alpha, float drawflag)
785 {
786         if(!alpha)
787                 return;
788
789         string pic;
790         pic = strcat("gfx/hud/", cvar_string("hud_skin"), "/");
791         if(vertical) {
792                 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);
793                 if(mySize_y/mySize_x > 2)
794                         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);
795                 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);
796         } else {
797                 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);
798                 if(mySize_x/mySize_y > 2)
799                         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);
800                 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);
801         }
802 }
803
804 vector HUD_Panel_GetProgressBarColor(string item)
805 {
806         return stov(cvar_string(strcat("hud_progressbar_", item, "_color")));
807 }
808
809 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
810 vector HUD_Panel_CheckMove(float id, vector myPos, vector mySize)
811 {
812         float i;
813
814         vector myTarget;
815         myTarget = myPos;
816
817         vector targPos;
818         vector targSize;
819         vector myCenter;
820         vector targCenter;
821         myCenter = '0 0 0'; // shut up fteqcc, there IS a reference
822         targCenter = '0 0 0'; // shut up fteqcc, there IS a reference
823
824         for (i = 0; i < HUD_PANEL_NUM; ++i) {
825                 if(i == id || !HUD_Panel_CheckActive(i))
826                         continue;
827
828                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * HUD_Panel_GetBorder(id);
829                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * HUD_Panel_GetBorder(id);
830
831                 if(myPos_y + mySize_y < targPos_y)
832                         continue;
833                 if(myPos_y > targPos_y + targSize_y)
834                         continue;
835
836                 if(myPos_x + mySize_x < targPos_x)
837                         continue;
838                 if(myPos_x > targPos_x + targSize_x)
839                         continue;
840
841                 // OK, there IS a collision.
842
843                 myCenter_x = myPos_x + 0.5 * mySize_x;
844                 myCenter_y = myPos_y + 0.5 * mySize_y;
845
846                 targCenter_x = targPos_x + 0.5 * targSize_x;
847                 targCenter_y = targPos_y + 0.5 * targSize_y;
848
849                 if(myCenter_x < targCenter_x && myCenter_y < targCenter_y) // top left (of the target panel)
850                 {
851                         if(myPos_x + mySize_x - targPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
852                                 myTarget_x = targPos_x - mySize_x;
853                         else // push it upwards
854                                 myTarget_y = targPos_y - mySize_y;
855                 }
856                 else if(myCenter_x > targCenter_x && myCenter_y < targCenter_y) // top right
857                 {
858                         if(targPos_x + targSize_x - myPos_x < myPos_y + mySize_y - targPos_y) // push it to the side
859                                 myTarget_x = targPos_x + targSize_x;
860                         else // push it upwards
861                                 myTarget_y = targPos_y - mySize_y;
862                 }
863                 else if(myCenter_x < targCenter_x && myCenter_y > targCenter_y) // bottom left
864                 {
865                         if(myPos_x + mySize_x - targPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
866                                 myTarget_x = targPos_x - mySize_x;
867                         else // push it downwards
868                                 myTarget_y = targPos_y + targSize_y;
869                 }
870                 else if(myCenter_x > targCenter_x && myCenter_y > targCenter_y) // bottom right
871                 {
872                         if(targPos_x + targSize_x - myPos_x < targPos_y + targSize_y - myPos_y) // push it to the side
873                                 myTarget_x = targPos_x + targSize_x;
874                         else // push it downwards
875                                 myTarget_y = targPos_y + targSize_y;
876                 }
877         }
878
879         return myTarget;
880 }
881
882 void HUD_Panel_SetPos(float id, vector pos)
883 {
884         vector oldPos;
885         oldPos = HUD_Panel_GetPos(id);
886
887         vector mySize;
888         mySize = HUD_Panel_GetSize(id);
889
890         if(cvar("hud_configure_checkcollisions"))
891                 pos = HUD_Panel_CheckMove(id, pos, mySize);
892
893         pos_x = bound(0, pos_x, vid_conwidth - mySize_x);
894         pos_y = bound(0, pos_y, vid_conheight - mySize_y);
895
896         if(cvar("hud_configure_grid"))
897         {
898                 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;
899                 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;
900         }
901
902         string s;
903         s = strcat(ftos(pos_x/vid_conwidth), " ", ftos(pos_y/vid_conheight));
904
905         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_pos"), s);
906 }
907
908 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
909 vector HUD_Panel_CheckResize(float id, vector mySize, vector resizeorigin) {
910         float i;
911
912         float targBorder;
913         vector targPos;
914         vector targSize;
915         vector targEndPos;
916         vector dist;
917
918         for (i = 0; i < HUD_PANEL_NUM; ++i) {
919                 if(i == id || !HUD_Panel_CheckActive(i))
920                         continue;
921
922                 targBorder = HUD_Panel_GetBorder(i);
923                 targPos = HUD_Panel_GetPos(i) - '1 1 0' * targBorder;
924                 targSize = HUD_Panel_GetSize(i) + '2 2 0' * targBorder;
925                 targEndPos = targPos + targSize;
926
927                 // resizeorigin is WITHIN target panel, just abort any collision testing against that particular panel to produce expected behaviour!
928                 if(resizeorigin_x > targPos_x && resizeorigin_x < targPos_x + targSize_x && resizeorigin_y > targPos_y && resizeorigin_y < targPos_y + targSize_y)
929                         continue;
930
931                 if (resizeCorner == 1)
932                 {
933                         // check if this panel is on our way
934                         if (resizeorigin_x <= targPos_x)
935                                 continue;
936                         if (resizeorigin_y <= targPos_y)
937                                 continue;
938                         if (targEndPos_x <= resizeorigin_x - mySize_x)
939                                 continue;
940                         if (targEndPos_y <= resizeorigin_y - mySize_y)
941                                 continue;
942
943                         // there is a collision:
944                         // detect which side of the panel we are facing is actually limiting the resizing
945                         // (which side the resize direction finds for first) and reduce the size up to there
946                         //
947                         // dist is the distance between resizeorigin and the "analogous" point of the panel
948                         // in this case resizeorigin (bottom-right point) and the bottom-right point of the panel
949                         dist_x = resizeorigin_x - targEndPos_x;
950                         dist_y = resizeorigin_y - targEndPos_y;
951                         if (dist_y < 0)
952                                 mySize_x = min(mySize_x, dist_x);
953                         else
954                                 mySize_y = min(mySize_y, dist_y);
955                 }
956                 else if (resizeCorner == 2)
957                 {
958                         if (resizeorigin_x >= targEndPos_x)
959                                 continue;
960                         if (resizeorigin_y <= targPos_y)
961                                 continue;
962                         if (targPos_x >= resizeorigin_x + mySize_x)
963                                 continue;
964                         if (targEndPos_y <= resizeorigin_y - mySize_y)
965                                 continue;
966
967                         dist_x = targPos_x - resizeorigin_x;
968                         dist_y = resizeorigin_y - targEndPos_y;
969                         if (dist_y < 0)
970                                 mySize_x = min(mySize_x, dist_x);
971                         else
972                                 mySize_y = min(mySize_y, dist_y);
973                 }
974                 else if (resizeCorner == 3)
975                 {
976                         if (resizeorigin_x <= targPos_x)
977                                 continue;
978                         if (resizeorigin_y >= targEndPos_y)
979                                 continue;
980                         if (targEndPos_x <= resizeorigin_x - mySize_x)
981                                 continue;
982                         if (targPos_y >= resizeorigin_y + mySize_y)
983                                 continue;
984
985                         dist_x = resizeorigin_x - targEndPos_x;
986                         dist_y = targPos_y - resizeorigin_y;
987                         if (dist_y < 0)
988                                 mySize_x = min(mySize_x, dist_x);
989                         else
990                                 mySize_y = min(mySize_y, dist_y);
991                 }
992                 else if (resizeCorner == 4)
993                 {
994                         if (resizeorigin_x >= targEndPos_x)
995                                 continue;
996                         if (resizeorigin_y >= targEndPos_y)
997                                 continue;
998                         if (targPos_x >= resizeorigin_x + mySize_x)
999                                 continue;
1000                         if (targPos_y >= resizeorigin_y + mySize_y)
1001                                 continue;
1002
1003                         dist_x = targPos_x - resizeorigin_x;
1004                         dist_y = targPos_y - resizeorigin_y;
1005                         if (dist_y < 0)
1006                                 mySize_x = min(mySize_x, dist_x);
1007                         else
1008                                 mySize_y = min(mySize_y, dist_y);
1009                 }
1010                 if(cvar("hud_configure_checkcollisions_debug"))
1011                         drawfill(targPos + '1 1 0' * targBorder, targSize - '2 2 0' * targBorder, '1 1 0', .3, DRAWFLAG_NORMAL);
1012         }
1013
1014         return mySize;
1015 }
1016
1017 void HUD_Panel_SetPosSize(float id)
1018 {
1019         vector resizeorigin;
1020         resizeorigin = panel_click_resizeorigin;
1021         vector mySize, myPos;
1022
1023         if(resizeCorner == 1) {
1024                 mySize_x = resizeorigin_x - (mousepos_x - panel_click_distance_x);
1025                 mySize_y = resizeorigin_y - (mousepos_y - panel_click_distance_y);
1026         } else if(resizeCorner == 2) {          
1027                 mySize_x = mousepos_x + panel_click_distance_x - resizeorigin_x;
1028                 mySize_y = panel_click_distance_y + resizeorigin_y - mousepos_y;
1029         } else if(resizeCorner == 3) {
1030                 mySize_x = resizeorigin_x + panel_click_distance_x - mousepos_x;
1031                 mySize_y = mousepos_y + panel_click_distance_y - resizeorigin_y;
1032         } else { // resizeCorner == 4
1033                 mySize_x = mousepos_x - (resizeorigin_x - panel_click_distance_x);
1034                 mySize_y = mousepos_y - (resizeorigin_y - panel_click_distance_y);
1035         }
1036
1037         // minimum panel size cap
1038         mySize_x = max(0.025 * vid_conwidth, mySize_x);
1039         mySize_y = max(0.025 * vid_conheight, mySize_y);
1040
1041         if(id == HUD_PANEL_CHAT) // some panels have their own restrictions, like the chat panel (which actually only moves the engine chat print around). Looks bad if it's too small.
1042         {
1043                 mySize_x = max(17 * cvar("con_chatsize"), mySize_x);
1044                 mySize_y = max(2 * cvar("con_chatsize") + 2 * HUD_Panel_GetPadding(id), mySize_y);
1045         }
1046
1047         // collision testing|
1048         // -----------------+
1049
1050         // we need to know pos at this stage, but it might still change later if we hit a screen edge/other panel (?)
1051         if(resizeCorner == 1) {
1052                 myPos_x = resizeorigin_x - mySize_x;
1053                 myPos_y = resizeorigin_y - mySize_y;
1054         } else if(resizeCorner == 2) {
1055                 myPos_x = resizeorigin_x;
1056                 myPos_y = resizeorigin_y - mySize_y;
1057         } else if(resizeCorner == 3) {
1058                 myPos_x = resizeorigin_x - mySize_x;
1059                 myPos_y = resizeorigin_y;
1060         } else { // resizeCorner == 4
1061                 myPos_x = resizeorigin_x;
1062                 myPos_y = resizeorigin_y;
1063         }
1064
1065         // left/top screen edges
1066         if(myPos_x < 0)
1067                 mySize_x = mySize_x + myPos_x;
1068         if(myPos_y < 0)
1069                 mySize_y = mySize_y + myPos_y;
1070
1071         // bottom/right screen edges
1072         if(myPos_x + mySize_x > vid_conwidth)
1073                 mySize_x = vid_conwidth - myPos_x;
1074         if(myPos_y + mySize_y > vid_conheight)
1075                 mySize_y = vid_conheight - myPos_y;
1076
1077         if(cvar("hud_configure_checkcollisions_debug"))
1078                 drawfill(myPos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
1079
1080         // before checkresize, otherwise panel can be snapped partially inside another panel or panel aspect ratio can be broken
1081         if(cvar("hud_configure_grid"))
1082         {
1083                 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;
1084                 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;
1085         }
1086
1087         if(cvar("hud_configure_checkcollisions"))
1088         {
1089                 mySize = HUD_Panel_CheckResize(id, mySize, resizeorigin);
1090         }
1091
1092         // 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)
1093         mySize_x = max(0.025 * vid_conwidth, mySize_x);
1094         mySize_y = max(0.025 * vid_conheight, mySize_y);
1095
1096         // do another pos check, as size might have changed by now
1097         if(resizeCorner == 1) {
1098                 myPos_x = resizeorigin_x - mySize_x;
1099                 myPos_y = resizeorigin_y - mySize_y;
1100         } else if(resizeCorner == 2) {
1101                 myPos_x = resizeorigin_x;
1102                 myPos_y = resizeorigin_y - mySize_y;
1103         } else if(resizeCorner == 3) {
1104                 myPos_x = resizeorigin_x - mySize_x;
1105                 myPos_y = resizeorigin_y;
1106         } else { // resizeCorner == 4
1107                 myPos_x = resizeorigin_x;
1108                 myPos_y = resizeorigin_y;
1109         }
1110
1111         if(cvar("hud_configure_checkcollisions_debug"))
1112         if(cvar("hud_configure_checkcollisions"))
1113                 drawfill(myPos, mySize, '0 1 0', .3, DRAWFLAG_NORMAL);
1114
1115         string s;
1116         s = strcat(ftos(mySize_x/vid_conwidth), " ", ftos(mySize_y/vid_conheight));
1117         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_size"), s);
1118
1119         s = strcat(ftos(myPos_x/vid_conwidth), " ", ftos(myPos_y/vid_conheight));
1120         cvar_set(strcat("hud_", HUD_Panel_GetName(id), "_pos"), s);
1121 }
1122
1123 float mouseClicked;
1124 float prevMouseClicked; // previous state
1125 float prevMouseClickedTime; // time during previous mouse click, to check for doubleclicks
1126 vector prevMouseClickedPos; // pos during previous mouse click, to check for doubleclicks
1127
1128 float menu_enabled;
1129 float menu_enabled_time;
1130 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
1131 {
1132         if(!hud_configure)
1133                 return false;
1134
1135         // allow console bind to work
1136         string con_keys;
1137         float keys;
1138         con_keys = findkeysforcommand("toggleconsole");
1139         keys = tokenize(con_keys);
1140
1141         float hit_con_bind, i;
1142         for (i = 0; i < keys; ++i)
1143         {
1144                 if(nPrimary == stof(argv(i)))
1145                         hit_con_bind = 1;
1146         }
1147
1148         if(nPrimary == K_MOUSE1)
1149         {
1150                 if(bInputType == 0) { // key pressed
1151                         mouseClicked = 1;
1152                         return true;
1153                 }
1154                 else if(bInputType == 1) {// key released
1155                         mouseClicked = 0;
1156                         return true;
1157                 }
1158         }
1159         else if(nPrimary == K_ESCAPE)
1160         {
1161                 disable_menu_alphacheck = 1;
1162                 menu_enabled = 1;
1163                 menu_enabled_time = time;
1164                 localcmd("menu_showhudexit\n");
1165         }
1166
1167         else if(hit_con_bind)
1168                 return false;
1169
1170         return true; // Suppress ALL other input
1171 }
1172
1173 void HUD_Panel_Mouse()
1174 {
1175         // 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
1176         if (menu_enabled == 0) // menu dialog closed, enable normal alpha stuff again
1177                 disable_menu_alphacheck = 0;
1178         if (cvar("_menu_alpha") == 0 && time - menu_enabled_time > 0.5)
1179                 menu_enabled = 0;
1180
1181         /*
1182         print("Disable menu_alphacheck: ", ftos(disable_menu_alphacheck), "\n");
1183         print("Highlighted: ", ftos(highlightedPanel), "\n");
1184         print("Menu alpha: ", cvar_string("_menu_alpha"), "\n");
1185         */
1186
1187         if(mouseClicked == 0 && disable_menu_alphacheck != 2) { // don't reset these variables in disable_menu_alphacheck mode 2!
1188                 highlightedPanel = -1;
1189                 highlightedAction = 0;
1190         }
1191
1192         mousepos = mousepos + getmousepos();
1193
1194         mousepos_x = bound(0, mousepos_x, vid_conwidth);
1195         mousepos_y = bound(0, mousepos_y, vid_conheight);
1196
1197         drawpic(mousepos, strcat("gfx/menu/", cvar_string("menu_skin"), "/cursor.tga"), '32 32 0', '1 1 1', 1, DRAWFLAG_NORMAL);
1198
1199         if(mouseClicked)
1200         {
1201                 float i, border;
1202                 vector panelPos;
1203                 vector panelSize;
1204
1205                 for(i = 0; i < HUD_PANEL_NUM; ++i)
1206                 {
1207                         panelPos = HUD_Panel_GetPos(i);
1208                         panelSize = HUD_Panel_GetSize(i);
1209                         border = HUD_Panel_GetBorder(i);
1210                         if(prevMouseClicked == 0) {
1211                                 // move
1212                                 if(mousepos_x >= panelPos_x && mousepos_y >= panelPos_y && mousepos_x <= panelPos_x + panelSize_x && mousepos_y <= panelPos_y + panelSize_y)
1213                                 {
1214                                         highlightedPanel = i;
1215                                         highlightedAction = 1;
1216                                 }
1217                                 // resize from topleft border
1218                                 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)
1219                                 {
1220                                         highlightedPanel = i;
1221                                         highlightedAction = 2;
1222                                         resizeCorner = 1;
1223                                 }
1224                                 // resize from topright border
1225                                 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)
1226                                 {
1227                                         highlightedPanel = i;
1228                                         highlightedAction = 2;
1229                                         resizeCorner = 2;
1230                                 }
1231                                 // resize from bottomleft border
1232                                 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)
1233                                 {
1234                                         highlightedPanel = i;
1235                                         highlightedAction = 2;
1236                                         resizeCorner = 3;
1237                                 }
1238                                 // resize from bottomright border
1239                                 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)
1240                                 {
1241                                         highlightedPanel = i;
1242                                         highlightedAction = 2;
1243                                         resizeCorner = 4;
1244                                 }
1245                         }
1246
1247                         if(highlightedPanel == i)
1248                         {
1249                                 if(prevMouseClicked == 0)
1250                                 {
1251                                         if(highlightedAction == 1)
1252                                                 panel_click_distance = mousepos - panelPos;
1253                                         else if(highlightedAction == 2)
1254                                         {
1255                                                 if(resizeCorner == 1) {
1256                                                         panel_click_distance = mousepos - panelPos;
1257                                                         panel_click_resizeorigin = panelPos + panelSize;
1258                                                 } else if(resizeCorner == 2) {
1259                                                         panel_click_distance_x = panelSize_x - mousepos_x + panelPos_x;
1260                                                         panel_click_distance_y = mousepos_y - panelPos_y;
1261                                                         panel_click_resizeorigin = panelPos + eY * panelSize_y;
1262                                                 } else if(resizeCorner == 3) {
1263                                                         panel_click_distance_x = mousepos_x - panelPos_x;
1264                                                         panel_click_distance_y = panelSize_y - mousepos_y + panelPos_y;
1265                                                         panel_click_resizeorigin = panelPos + eX * panelSize_x;
1266                                                 } else if(resizeCorner == 4) {
1267                                                         panel_click_distance = panelSize - mousepos + panelPos;
1268                                                         panel_click_resizeorigin = panelPos;
1269                                                 }
1270                                         }       
1271                                 }
1272
1273                                 if(cvar("hud_configure_checkcollisions_debug"))
1274                                         drawfill(panelPos, panelSize, '1 0 0', .3, DRAWFLAG_NORMAL);
1275
1276                                 if(highlightedAction == 1)
1277                                         HUD_Panel_SetPos(i, mousepos - panel_click_distance);
1278                                 else if(highlightedAction == 2)
1279                                         HUD_Panel_SetPosSize(i);
1280                         }
1281                 }
1282
1283                 // doubleclick check
1284                 if(time - prevMouseClickedTime < 0.4 && prevMouseClicked == 0 && prevMouseClickedPos == mousepos && highlightedPanel >= 0)
1285                 {
1286                         mouseClicked = 0; // to prevent spam, I guess.
1287                         disable_menu_alphacheck = 2;
1288                         menu_enabled = 1;
1289                         menu_enabled_time = time;
1290                         localcmd("menu_showhudoptions ", ftos(highlightedPanel), "\n");
1291                         return;
1292                 }
1293                 if(prevMouseClicked == 0)
1294                 {
1295                         prevMouseClickedTime = time;
1296                         prevMouseClickedPos = mousepos;
1297                 }
1298         }
1299         prevMouseClicked = mouseClicked;
1300 }
1301
1302 // Weapon icons (#0)
1303 //
1304 float weaponspace[10];
1305 void HUD_WeaponIcons_Clear()
1306 {
1307         float idx;
1308         for(idx = 0; idx < 10; ++idx)
1309                 weaponspace[idx] = 0;
1310 }
1311
1312 entity weaponorder[WEP_MAXCOUNT];
1313 void weaponorder_swap(float i, float j, entity pass)
1314 {
1315         entity h;
1316         h = weaponorder[i];
1317         weaponorder[i] = weaponorder[j];
1318         weaponorder[j] = h;
1319 }
1320
1321 string weaponorder_cmp_str_save;
1322 string weaponorder_cmp_str;
1323 float weaponorder_cmp(float i, float j, entity pass)
1324 {
1325         float ai, aj;
1326         ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
1327         aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
1328         return aj - ai; // the string is in REVERSE order (higher prio at the right is what we want, but higher prio first is the string)
1329 }
1330
1331 void HUD_WeaponIcons(void)
1332 {
1333         float id = HUD_PANEL_WEAPONICONS;
1334         float alpha, stat_weapons; // "constants"
1335         vector pos, mySize, accuracy_color;
1336         float i, weapid, fade, weapon_stats, weapon_hit, weapon_damage, weapon_cnt; // variables
1337
1338         pos = HUD_Panel_GetPos(id);
1339         mySize = HUD_Panel_GetSize(id);
1340
1341         stat_weapons = getstati(STAT_WEAPONS);
1342         weapon_cnt = 0;
1343         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1344         {
1345                 self = get_weaponinfo(i);
1346                 if(self.impulse >= 0)
1347                         ++weapon_cnt;
1348         }
1349
1350         // TODO make this configurable
1351         weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
1352
1353         if(weaponorder_cmp_str != weaponorder_cmp_str_save)
1354         {
1355                 if(weaponorder_cmp_str_save)
1356                         strunzone(weaponorder_cmp_str_save);
1357                 weaponorder_cmp_str_save = strzone(weaponorder_cmp_str);
1358                 weapon_cnt = 0;
1359                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
1360                 {
1361                         self = get_weaponinfo(i);
1362                         if(self.impulse >= 0)
1363                         {
1364                                 weaponorder[weapon_cnt] = self;
1365                                 ++weapon_cnt;
1366                         }
1367                 }
1368                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
1369         }
1370
1371         HUD_Panel_DrawBg(id, pos, mySize, 0);
1372         float padding;
1373         padding = HUD_Panel_GetPadding(id);
1374         if(padding)
1375         {
1376                 pos += '1 1 0' * padding;
1377                 mySize -= '2 2 0' * padding;
1378         }
1379
1380         // hits
1381         weapon_stats = getstati(STAT_DAMAGE_HITS);
1382         weapon_number = weapon_stats & 63;
1383         weapon_hits[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1384         // fired
1385         weapon_stats = getstati(STAT_DAMAGE_FIRED);
1386         weapon_number = weapon_stats & 63;
1387         weapon_fired[weapon_number-WEP_FIRST] = floor(weapon_stats / 64);
1388
1389         if(cvar_or("hud_weaponicons_fade", 1))
1390         {
1391                 fade = 3.2 - 2 * (time - weapontime);
1392                 fade = bound(0.7, fade, 1);
1393         }
1394         else
1395                 fade = 1;
1396
1397         HUD_WeaponIcons_Clear();
1398
1399         float rows, columns;
1400         rows = mySize_y/mySize_x;
1401         rows = bound(1, floor((sqrt(4 * (2/1) * rows * WEP_COUNT + rows * rows) + rows + 0.5) / 2), WEP_COUNT);
1402         //                               ^^^ weapon icon aspect goes here
1403
1404         columns = ceil(WEP_COUNT/rows);
1405         float row, column;
1406         for(i = 0; i < weapon_cnt; ++i)
1407         {
1408                 self = weaponorder[i];
1409                 weapid = self.impulse;
1410
1411                 alpha = (self.weapon == activeweapon) ? 1 : 0.6;
1412
1413                 weapon_hit = weapon_hits[self.weapon-WEP_FIRST];
1414                 weapon_damage = weapon_fired[self.weapon-WEP_FIRST];
1415
1416                 // draw background behind currently selected weapon
1417                 if(self.weapon == activeweapon)
1418                         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);
1419
1420                 // draw the weapon accuracy on the HUD
1421                 if(hud_accuracy_hud && !(gametype == GAME_RACE || gametype == GAME_CTS))
1422                 {
1423                         if(weapon_damage)
1424                                 weapon_stats = floor(100 * weapon_hit / weapon_damage);
1425
1426                         accuracy_color = HUD_AccuracyColor(weapon_stats);
1427                         if(weapon_damage)
1428                                 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);
1429                 }
1430
1431                 // draw the weapon icon
1432                 if((self.impulse >= 0) && (stat_weapons & self.weapons))
1433                 {
1434                         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);
1435
1436                         if(cvar_or("hud_weaponicons_number", 1))
1437                                 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);
1438                 }
1439                 // draw a "ghost weapon icon" if you don't have the weapon
1440                 else
1441                 {
1442                         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);
1443                 }
1444
1445                 ++row;
1446                 if(row >= rows)
1447                 {
1448                         row = 0;
1449                         ++column;
1450                 }
1451         }
1452
1453 }
1454
1455 // Inventory (#1)
1456 //
1457 float GetAmmoStat(float i)
1458 {
1459         switch(i)
1460         {
1461                 case 0: return STAT_SHELLS;
1462                 case 1: return STAT_NAILS;
1463                 case 2: return STAT_ROCKETS;
1464                 case 3: return STAT_CELLS;
1465                 case 4: return STAT_FUEL;
1466                 default: return -1;
1467         }
1468 }
1469
1470 float GetAmmoItemCode(float i)
1471 {
1472         switch(i)
1473         {
1474                 case 0: return IT_SHELLS;
1475                 case 1: return IT_NAILS;
1476                 case 2: return IT_ROCKETS;
1477                 case 3: return IT_CELLS;
1478                 case 4: return IT_FUEL;
1479                 default: return -1;
1480         }
1481 }
1482
1483 string GetAmmoPicture(float i)
1484 {
1485         switch(i)
1486         {
1487                 case 0: return "ammo_shells";
1488                 case 1: return "ammo_bullets";
1489                 case 2: return "ammo_rockets";
1490                 case 3: return "ammo_cells";
1491                 case 4: return "ammo_fuel";
1492                 default: return "";
1493         }
1494 }
1495
1496 void DrawAmmoItem(vector myPos, vector mySize, float itemcode, float currently_selected)
1497 {
1498         float a;
1499         a = getstati(GetAmmoStat(itemcode)); // how much ammo do we have of type itemcode?
1500         if(hud_configure)
1501                 a = 100;
1502
1503         vector color;
1504         if(a < 10)
1505                 color = '0.7 0 0';
1506         else
1507                 color = '1 1 1';
1508
1509         float alpha;
1510         if(currently_selected)
1511                 alpha = 1;
1512         else
1513                 alpha = 0.7;
1514
1515         vector newSize, newPos;
1516         if(mySize_x/mySize_y > 3)
1517         {
1518                 newSize_x = 3 * mySize_y;
1519                 newSize_y = mySize_y;
1520
1521                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
1522                 newPos_y = myPos_y;
1523         }
1524         else
1525         {
1526                 newSize_y = 1/3 * mySize_x;
1527                 newSize_x = mySize_x;
1528
1529                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
1530                 newPos_x = myPos_x;
1531         }
1532
1533         vector picpos, numpos;
1534         if(cvar("hud_inventory_iconalign"))
1535         {
1536                 numpos = newPos;
1537                 picpos = newPos + eX * 2 * newSize_y;
1538         }
1539         else
1540         {
1541                 numpos = newPos + eX * newSize_y;
1542                 picpos = newPos;
1543         }
1544
1545         if (currently_selected)
1546                 drawpic_skin(newPos, "ammo_current_bg", newSize, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_INVENTORY), DRAWFLAG_NORMAL);
1547
1548         drawfont = hud_bigfont;
1549         drawstring_aspect(numpos, ftos(a), eX * (2/3) * newSize_x + eY * newSize_y, newSize_y, color, HUD_Panel_GetFgAlpha(HUD_PANEL_INVENTORY) * alpha, DRAWFLAG_NORMAL);
1550         drawfont = hud_font;
1551         drawpic_skin(picpos, GetAmmoPicture(itemcode), '1 1 0' * newSize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_INVENTORY) * alpha, DRAWFLAG_NORMAL);
1552 }
1553
1554 void HUD_Inventory(void)
1555 {
1556         float id = HUD_PANEL_INVENTORY;
1557         float i, currently_selected;
1558
1559         vector pos, mySize;
1560         pos = HUD_Panel_GetPos(id);
1561         mySize = HUD_Panel_GetSize(id);
1562
1563         HUD_Panel_DrawBg(id, pos, mySize, 0);
1564         float padding;
1565         padding = HUD_Panel_GetPadding(id);
1566         if(padding)
1567         {
1568                 pos += '1 1 0' * padding;
1569                 mySize -= '2 2 0' * padding;
1570         }
1571
1572         float rows, columns;
1573         rows = mySize_y/mySize_x;
1574         rows = bound(1, floor((sqrt(4 * (3/1) * rows * AMMO_COUNT + rows * rows) + rows + 0.5) / 2), AMMO_COUNT);
1575         //                               ^^^ ammo item aspect goes here
1576
1577         columns = ceil(AMMO_COUNT/rows);
1578
1579         float row, column;
1580         // ammo
1581         for (i = 0; i < AMMO_COUNT; ++i) {
1582                 currently_selected = getstati(STAT_ITEMS) & GetAmmoItemCode(i);
1583                 if(cvar("hud_inventory_onlycurrent")) {
1584                         if(hud_configure)
1585                                 i = 2;
1586                         if (currently_selected || hud_configure)
1587                                 DrawAmmoItem(pos, mySize, i, currently_selected);
1588                         break;
1589                 } else {
1590                         DrawAmmoItem(pos + eX * column * mySize_x*(1/columns) + eY * row * mySize_y*(1/rows), eX * mySize_x*(1/columns) + eY * mySize_y*(1/rows), i, currently_selected);
1591                         ++row;
1592                         if(row >= rows)
1593                         {
1594                                 row = 0;
1595                                 column = column + 1;
1596                         }
1597                 }
1598         }
1599 }
1600
1601 void DrawNumIcon(float id, vector myPos, vector mySize, float x, string icon, float left, vector color)
1602 {
1603         vector newSize, newPos;
1604         if(mySize_x/mySize_y > 3)
1605         {
1606                 newSize_x = 3 * mySize_y;
1607                 newSize_y = mySize_y;
1608
1609                 newPos_x = myPos_x + (mySize_x - newSize_x) / 2;
1610                 newPos_y = myPos_y;
1611         }
1612         else
1613         {
1614                 newSize_y = 1/3 * mySize_x;
1615                 newSize_x = mySize_x;
1616
1617                 newPos_y = myPos_y + (mySize_y - newSize_y) / 2;
1618                 newPos_x = myPos_x;
1619         }
1620
1621         vector picpos, numpos;
1622         float iconalign;
1623         iconalign = cvar(strcat("hud_", HUD_Panel_GetName(id), "_iconalign"));
1624         if(left)
1625         {
1626                 if(iconalign == 1 || iconalign == 3) // right align
1627                 {
1628                         numpos = newPos;
1629                         picpos = newPos + eX * 2 * newSize_y;
1630                 }
1631                 else // left align
1632                 {
1633                         numpos = newPos + eX * newSize_y;
1634                         picpos = newPos;
1635                 }
1636         }
1637         else
1638         {
1639                 if(iconalign == 0 || iconalign == 3) // left align
1640                 {
1641                         numpos = newPos + eX * newSize_y;
1642                         picpos = newPos;
1643                 } 
1644                 else // right align
1645                 {
1646                         numpos = newPos;
1647                         picpos = newPos + eX * 2 * newSize_y;
1648                 }
1649         }
1650
1651         drawfont = hud_bigfont;
1652         drawstring_aspect(numpos, ftos(x), eX * (2/3) * newSize_x + eY * newSize_y, newSize_y, color, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1653         drawfont = hud_font;
1654         drawpic_skin(picpos, icon, '1 1 0' * newSize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1655 }
1656
1657 // Powerups (#2)
1658 //
1659 void HUD_Powerups(void) {
1660         float id = HUD_PANEL_POWERUPS;
1661         float stat_items;
1662         stat_items = getstati(STAT_ITEMS);
1663
1664         if(!hud_configure)
1665         {
1666                 if not(stat_items & IT_STRENGTH)
1667                         if not(stat_items & IT_INVINCIBLE)
1668                                 return;
1669
1670                 if (getstati(STAT_HEALTH) <= 0)
1671                         return;
1672         }
1673
1674         vector pos, mySize;
1675         pos = HUD_Panel_GetPos(id);
1676         mySize = HUD_Panel_GetSize(id);
1677
1678         HUD_Panel_DrawBg(id, pos, mySize, 0);
1679         float padding;
1680         padding = HUD_Panel_GetPadding(id);
1681         if(padding)
1682         {
1683                 pos += '1 1 0' * padding;
1684                 mySize -= '2 2 0' * padding;
1685         }
1686
1687         float strength_time, shield_time;
1688
1689         strength_time = bound(0, getstatf(STAT_STRENGTH_FINISHED) - time, 99);
1690         shield_time = bound(0, getstatf(STAT_INVINCIBLE_FINISHED) - time, 99);
1691
1692         if(hud_configure)
1693         {
1694                 strength_time = 15;
1695                 shield_time = 27;
1696         }
1697
1698         vector barpos, barsize;
1699         vector picpos;
1700         vector numpos;
1701
1702         string leftname, rightname;
1703         float leftcnt, rightcnt;
1704         float leftexact, rightexact;
1705         float leftalpha, rightalpha;
1706         if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) {
1707                 leftname = "strength";
1708                 leftcnt = ceil(strength_time);
1709                 leftexact = strength_time;
1710
1711                 rightname = "shield";
1712                 rightcnt = ceil(shield_time);
1713                 rightexact = shield_time;
1714         } else {
1715                 leftname = "shield";
1716                 leftcnt = ceil(shield_time);
1717                 leftexact = shield_time;
1718
1719                 rightname = "strength";
1720                 rightcnt = ceil(strength_time);
1721                 rightexact = strength_time;
1722         }
1723         leftalpha = bound(0, leftexact, 1);
1724         rightalpha = bound(0, rightexact, 1);
1725
1726         float baralign, iconalign;
1727         baralign = cvar(strcat("hud_", HUD_Panel_GetName(id), "_baralign"));
1728         iconalign = cvar(strcat("hud_", HUD_Panel_GetName(id), "_iconalign"));
1729
1730         if (mySize_x/mySize_y > 4)
1731         {
1732                 if(leftcnt)
1733                 {
1734                         if(baralign == 1 || baralign == 3) { // right align
1735                                 barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/30);
1736                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1737                         } else { // left align
1738                                 barpos = pos;
1739                                 barsize = eX * 0.5 * mySize_x * min(1, leftcnt/30) + eY * mySize_y;
1740                         }
1741
1742                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1743                         DrawNumIcon(id, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, '1 1 1');
1744                         // TODO: expand
1745                         //if(leftcnt <= 5)
1746                         //      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));
1747                         //else
1748                         //      drawpic_skin(picpos, leftname, eX * (1/6) * mySize_x + eY * mySize_y, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1749                 }
1750
1751                 if(rightcnt)
1752                 {
1753                         if(baralign == 0 || baralign == 3) { // left align
1754                                 barpos = pos + eX * 0.5 * mySize_x;
1755                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1756                         } else { // right align
1757                                 barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/30);
1758                                 barsize = eX * 0.5 * mySize_x * min(1, rightcnt/30) + eY * mySize_y;
1759                         }
1760
1761                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1762                         DrawNumIcon(id, pos + eX * 0.5 * mySize_x, eX * 0.5 * mySize_x + eY * mySize_y, rightcnt, rightname, 0, '1 1 1');
1763                 }
1764         }
1765         else if (mySize_x/mySize_y > 1.5)
1766         {
1767                 if(leftcnt)
1768                 {
1769                         if(baralign == 1 || baralign == 3) { // right align
1770                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/30);
1771                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1772                         } else { // left align
1773                                 barpos = pos;
1774                                 barsize = eX * mySize_x * min(1, leftcnt/30) + eY * 0.5 * mySize_y;
1775                         }
1776
1777                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1778                         DrawNumIcon(id, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, '1 1 1');
1779                 }
1780
1781                 if(rightcnt)
1782                 {
1783                         if(baralign == 0 || baralign == 3) { // left align
1784                                 barpos = pos + eY * 0.5 * mySize_y;
1785                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1786                         } else { // right align
1787                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1788                                 barsize = eX * mySize_x * min(1, rightcnt/30) + eY * 0.5 * mySize_y;
1789                         }
1790
1791                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1792                         DrawNumIcon(id, pos + eY * 0.5 * mySize_y, eX * mySize_x + eY * 0.5 * mySize_y, rightcnt, rightname, 0, '1 1 1');
1793                 }
1794         }
1795         else
1796         {
1797                 if(leftcnt)
1798                 {
1799                         if(baralign == 1 || baralign == 3) { // down align
1800                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/30);
1801                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1802                         } else { // up align
1803                                 barpos = pos;
1804                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/30);
1805                         }
1806
1807                         if(iconalign == 1 || iconalign == 3) { // down align
1808                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
1809                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
1810                         } else { // up align
1811                                 picpos = pos + eX * 0.05 * mySize_x;
1812                                 numpos = pos + eY * 0.4 * mySize_x;
1813                         }
1814
1815                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1816                         if(leftcnt <= 5)
1817                                 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));
1818                         else
1819                                 drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1820                         drawstring_aspect(numpos, ftos(leftcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, 0.25 * mySize_x, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1821                 }
1822
1823                 if(rightcnt)
1824                 {
1825                         if(baralign == 0 || baralign == 3) { // up align
1826                                 barpos = pos + eX * 0.5 * mySize_x;
1827                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1828                         } else { // down align
1829                                 barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/30) + eX * 0.5 * mySize_x;
1830                                 barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/30);
1831                         }
1832
1833                         if(iconalign == 0 || iconalign == 3) { // up align
1834                                 picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
1835                                 numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
1836                         } else { // down align
1837                                 picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
1838                                 numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
1839                         }
1840
1841                         HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1842                         if(rightcnt <= 5)
1843                                 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));
1844                         else
1845                                 drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1846                         drawstring_aspect(numpos, ftos(rightcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, 0.25 * mySize_x, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1847                 }
1848         }
1849 }
1850
1851 // Health/armor (#3)
1852 //
1853 void HUD_HealthArmor(void)
1854 {
1855         float id = HUD_PANEL_HEALTHARMOR;
1856         vector pos, mySize;
1857         pos = HUD_Panel_GetPos(id);
1858         mySize = HUD_Panel_GetSize(id);
1859
1860         HUD_Panel_DrawBg(id, pos, mySize, 0);
1861         float padding;
1862         padding = HUD_Panel_GetPadding(id);
1863         if(padding)
1864         {
1865                 pos += '1 1 0' * padding;
1866                 mySize -= '2 2 0' * padding;
1867         }
1868
1869         float armor, health;
1870         armor = getstati(STAT_ARMOR);
1871         health = getstati(STAT_HEALTH);
1872
1873         float fuel;
1874         fuel = getstati(GetAmmoStat(4)); // how much fuel do we have?
1875
1876         if(hud_configure)
1877         {
1878                 armor = 150;
1879                 health = 100;
1880                 fuel = 70;
1881         }
1882
1883         if(health <= 0)
1884                 return;
1885
1886         vector barpos, barsize;
1887         vector picpos;
1888         vector numpos;
1889
1890         float baralign, iconalign;
1891         baralign = cvar(strcat("hud_", HUD_Panel_GetName(id), "_baralign"));
1892         iconalign = cvar(strcat("hud_", HUD_Panel_GetName(id), "_iconalign"));
1893
1894         if(cvar("hud_healtharmor") == 2) // combined health and armor display
1895         {
1896                 vector v;
1897                 v = healtharmor_maxdamage(health, armor, armorblockpercent);
1898
1899                 float x;
1900                 x = floor(v_x + 1);
1901
1902                 if(baralign == 1 || baralign == 3) { // right align
1903                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, x/400);
1904                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
1905                 } else { // left align
1906                         barpos = pos;
1907                         barsize = eX * mySize_x * min(1, x/400) + eY * mySize_y;
1908                 }
1909
1910                 string biggercount;
1911                 if(v_z) // NOT fully armored
1912                 {
1913                         biggercount = "health";
1914                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("health"), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1915                         if(armor)
1916                                 drawpic_skin(pos + eX * mySize_x - eX * 0.5 * mySize_y, "armor", '0.5 0.5 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id) * armor / health, DRAWFLAG_NORMAL);
1917                 }
1918                 else
1919                 {
1920                         biggercount = "armor";
1921                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("armor"), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1922                         if(health)
1923                                 drawpic_skin(pos + eX * mySize_x - eX * 0.5 * mySize_y, "health", '0.5 0.5 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1924                 }
1925                 DrawNumIcon(id, pos, mySize, x, biggercount, 1, HUD_Get_Num_Color(x, 2 * 200));
1926
1927                 // fuel
1928                 if(fuel)
1929                 {
1930                         if(baralign == 0 || baralign == 3) { // left align
1931                                 barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
1932                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
1933                         } else {
1934                                 barpos = pos;
1935                                 barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
1936                         }
1937                         HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("fuel"), HUD_Panel_GetFgAlpha(id) * 0.8, DRAWFLAG_NORMAL);
1938                 }
1939         }
1940         else
1941         {
1942                 string leftname, rightname;
1943                 float leftcnt, rightcnt;
1944                 float leftactive, rightactive;
1945                 float leftalpha, rightalpha;
1946                 if (cvar(strcat("hud_", HUD_Panel_GetName(id), "_flip"))) { // old style layout with armor left/top of health
1947                         leftname = "armor";
1948                         leftcnt = armor;
1949                         if(leftcnt)
1950                                 leftactive = 1;
1951                         leftalpha = min((armor+10)/55, 1);
1952
1953                         rightname = "health";
1954                         rightcnt = health;
1955                         rightactive = 1;
1956                         rightalpha = 1;
1957                 } else {
1958                         leftname = "health";
1959                         leftcnt = health;
1960                         leftactive = 1;
1961                         leftalpha = 1;
1962
1963                         rightname = "armor";
1964                         rightcnt = armor;
1965                         if(rightcnt)
1966                                 rightactive = 1;
1967                         rightalpha = min((armor+10)/55, 1);
1968                 }
1969
1970                 if (mySize_x/mySize_y > 4)
1971                 {
1972                         if(leftactive)
1973                         {
1974                                 if(baralign == 1 || baralign == 3) { // right align
1975                                         barpos = pos + eX * 0.5 * mySize_x - eX * 0.5 * mySize_x * min(1, leftcnt/200);
1976                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1977                                 } else { // left align
1978                                         barpos = pos;
1979                                         barsize = eX * 0.5 * mySize_x * min(1, leftcnt/200) + eY * mySize_y;
1980                                 }
1981
1982                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1983                                 DrawNumIcon(id, pos, eX * 0.5 * mySize_x + eY * mySize_y, leftcnt, leftname, 1, HUD_Get_Num_Color(leftcnt, 200));
1984                         }
1985
1986                         if(rightactive)
1987                         {
1988                                 if(baralign == 0 || baralign == 3) { // left align
1989                                         barpos = pos + eX * 0.5 * mySize_x;
1990                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1991                                 } else { // right align
1992                                         barpos = pos + eX * mySize_x - eX * 0.5 * mySize_x * min(1, rightcnt/200);
1993                                         barsize = eX * 0.5 * mySize_x * min(1, rightcnt/200) + eY * mySize_y;
1994                                 }
1995
1996                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
1997                                 DrawNumIcon(id, pos + eX * 0.5 * mySize_x, eX * 0.5 * mySize_x + eY * mySize_y, rightcnt, rightname, 0, HUD_Get_Num_Color(leftcnt, 200));
1998                         }
1999
2000                         if(fuel)
2001                         {
2002                                 if(baralign == 0 || baralign == 3) { // left align
2003                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2004                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2005                                 } else {
2006                                         barpos = pos;
2007                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.2 * mySize_y;
2008                                 }
2009                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("fuel"), HUD_Panel_GetFgAlpha(id) * 0.8, DRAWFLAG_NORMAL);
2010                         }
2011                 }
2012                 else if (mySize_x/mySize_y > 1.5)
2013                 {
2014                         if(leftactive)
2015                         {
2016                                 if(baralign == 1 || baralign == 3) { // right align
2017                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, leftcnt/200);
2018                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2019                                 } else { // left align
2020                                         barpos = pos;
2021                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2022                                 }
2023
2024                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2025                                 DrawNumIcon(id, pos, eX * mySize_x + eY * 0.5 * mySize_y, leftcnt, leftname, 1, HUD_Get_Num_Color(leftcnt, 200));
2026                         }
2027
2028                         if(rightactive)
2029                         {
2030                                 if(baralign == 0 || baralign == 3) { // left align
2031                                         barpos = pos + eY * 0.5 * mySize_y;
2032                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2033                                 } else { // right align
2034                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2035                                         barsize = eX * mySize_x * min(1, rightcnt/200) + eY * 0.5 * mySize_y;
2036                                 }
2037
2038                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2039                                 DrawNumIcon(id, pos + eY * 0.5 * mySize_y, eX * mySize_x + eY * 0.5 * mySize_y, rightcnt, rightname, 0, HUD_Get_Num_Color(leftcnt, 200));
2040                         }
2041
2042                         if(fuel)
2043                         {
2044                                 if(baralign == 0 || baralign == 3) { // left align
2045                                         barpos = pos + eX * mySize_x - eX * mySize_x * min(1, fuel/100);
2046                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2047                                 } else {
2048                                         barpos = pos;
2049                                         barsize = eX * mySize_x * min(1, fuel/100) + eY * 0.1 * mySize_y;
2050                                 }
2051                                 HUD_Panel_DrawProgressBar(barpos, 0, barsize, HUD_Panel_GetProgressBarColor("fuel"), HUD_Panel_GetFgAlpha(id) * 0.8, DRAWFLAG_NORMAL);
2052                         }
2053                 }
2054                 else
2055                 {
2056                         if(leftactive)
2057                         {
2058                                 if(baralign == 1 || baralign == 3) { // down align
2059                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, leftcnt/200);
2060                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2061                                 } else { // up align
2062                                         barpos = pos;
2063                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, leftcnt/200);
2064                                 }
2065
2066                                 if(iconalign == 1 || iconalign == 3) { // down align
2067                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x);
2068                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x;
2069                                 } else { // up align
2070                                         picpos = pos + eX * 0.05 * mySize_x;
2071                                         numpos = pos + eY * 0.4 * mySize_x;
2072                                 }
2073
2074                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor(leftname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2075                                 drawpic_skin(picpos, leftname, '0.4 0.4 0' * mySize_x, '1 1 1', leftalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2076                                 drawstring_aspect(numpos, ftos(leftcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, 0.25 * mySize_x, HUD_Get_Num_Color(leftcnt, 200), HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2077                         }
2078
2079                         if(rightactive)
2080                         {
2081                                 if(baralign == 0 || baralign == 3) { // up align
2082                                         barpos = pos + eX * 0.5 * mySize_x;
2083                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2084                                 } else { // down align
2085                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, rightcnt/200) + eX * 0.5 * mySize_x;
2086                                         barsize = eX * 0.5 * mySize_x + eY * mySize_y * min(1, rightcnt/200);
2087                                 }
2088
2089                                 if(iconalign == 0 || iconalign == 3) { // up align
2090                                         picpos = pos + eX * 0.05 * mySize_x + eX * 0.5 * mySize_x;
2091                                         numpos = pos + eY * 0.4 * mySize_x + eX * 0.5 * mySize_x;
2092                                 } else { // down align
2093                                         picpos = pos + eX * 0.05 * mySize_x + eY * (mySize_y - 0.65 * mySize_x) + eX * 0.5 * mySize_x;
2094                                         numpos = pos + eY * mySize_y - eY * 0.25 * mySize_x + eX * 0.5 * mySize_x;
2095                                 }
2096
2097                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor(rightname), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2098                                 drawpic_skin(picpos, rightname, '0.4 0.4 0' * mySize_x, '1 1 1', rightalpha * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2099                                 drawstring_aspect(numpos, ftos(rightcnt), eX * 0.5 * mySize_x + eY * 0.25 * mySize_x, 0.25 * mySize_x, HUD_Get_Num_Color(rightcnt, 200), HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2100                         }
2101
2102                         if(fuel)
2103                         {
2104                                 if(baralign == 0 || baralign == 3) { // left align
2105                                         barpos = pos;
2106                                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2107                                 } else {
2108                                         barpos = pos + eY * mySize_y - eY * mySize_y * min(1, fuel/100);
2109                                         barsize = eX * 0.05 * mySize_x + eY * mySize_y * min(1, fuel/100);
2110                                 }
2111                                 HUD_Panel_DrawProgressBar(barpos, 1, barsize, HUD_Panel_GetProgressBarColor("fuel"), HUD_Panel_GetFgAlpha(id) * 0.8, DRAWFLAG_NORMAL);
2112                         }
2113                 }
2114         }
2115 }
2116
2117 // ___TODO___ !!!
2118 // Notification area (#4)
2119 //
2120
2121 string Weapon_SuicideMessage(float deathtype)
2122 {
2123         w_deathtype = deathtype;
2124         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_SUICIDEMESSAGE);
2125         return w_deathtypestring;
2126 }
2127
2128 string Weapon_KillMessage(float deathtype)
2129 {
2130         w_deathtype = deathtype;
2131         get_weaponinfo(DEATH_WEAPONOF(deathtype)).weapon_func(WR_KILLMESSAGE);
2132         return w_deathtypestring;
2133 }
2134
2135 float killnotify_times[10];
2136 float killnotify_deathtype[10];
2137 float killnotify_actiontype[10]; // 0 = "Y [used by] X", 1 = "X [did action to] Y"
2138 string killnotify_attackers[10];
2139 string killnotify_victims[10];
2140 void HUD_KillNotify_Push(string attacker, string victim, float actiontype, float wpn)
2141 {
2142         float i;
2143         for (i = 9; i > 0; --i) {
2144                 killnotify_times[i] = killnotify_times[i-1];
2145                 killnotify_deathtype[i] = killnotify_deathtype[i-1];
2146                 killnotify_actiontype[i] = killnotify_actiontype[i-1];
2147                 if(killnotify_attackers[i])
2148                         strunzone(killnotify_attackers[i]);
2149                 killnotify_attackers[i] = strzone(killnotify_attackers[i-1]);
2150                 if(killnotify_victims[i])
2151                         strunzone(killnotify_victims[i]);
2152                 killnotify_victims[i] = strzone(killnotify_victims[i-1]);
2153         }
2154         killnotify_times[0] = time;
2155         killnotify_deathtype[0] = wpn;
2156         killnotify_actiontype[0] = actiontype;
2157         if(killnotify_attackers[0])
2158                 strunzone(killnotify_attackers[0]);
2159         killnotify_attackers[0] = strzone(attacker);
2160         if(killnotify_victims[0])
2161                 strunzone(killnotify_victims[0]);
2162         killnotify_victims[0] = strzone(victim);
2163 }
2164
2165 void HUD_KillNotify(string s1, string s2, string s3, float type, float msg)
2166 {
2167         float w;
2168         float alsoprint;
2169         alsoprint = (cvar("hud_notify_print") || !HUD_Panel_CheckActive(4)); // print message to console if: notify panel disabled, or cvar to do so enabled
2170         
2171         if(msg == MSG_SUICIDE) {
2172                 // TODO: cl_gentle
2173                 w = DEATH_WEAPONOF(type);
2174                 if(WEP_VALID(w)) {
2175                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2176                         if (alsoprint)
2177                                 print("^1", s1, "^1 ", Weapon_SuicideMessage(type), "\n");
2178                 } else if (type == DEATH_KILL) {
2179                         HUD_KillNotify_Push(s1, "", 0, DEATH_KILL);
2180                         if (alsoprint)
2181                                 print ("^1",s1, "^1 couldn't take it anymore\n");
2182                 } else if (type == DEATH_ROT) {
2183                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2184                         if (alsoprint)
2185                                 print ("^1",s1, "^1 died\n");
2186                 } else if (type == DEATH_NOAMMO) {
2187                         HUD_KillNotify_Push(s1, "", 0, DEATH_NOAMMO);
2188                         if (alsoprint)
2189                                 print ("^7",s1, "^7 committed suicide. What's the point of living without ammo?\n");
2190                 } else if (type == DEATH_CAMP) {
2191                         HUD_KillNotify_Push(s1, "", 0, DEATH_CAMP);
2192                         if (alsoprint)
2193                                 print ("^1",s1, "^1 thought they found a nice camping ground\n");
2194                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2195                         HUD_KillNotify_Push(s1, "", 0, type);
2196                         if (alsoprint)
2197                                 print ("^1",s1, "^1 didn't become friends with the Lord of Teamplay\n");
2198                 } else if (type == DEATH_CHEAT) {
2199                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2200                         if (alsoprint)
2201                                 print ("^1",s1, "^1 unfairly eliminated themself\n");
2202                 } else if (type == DEATH_FIRE) {
2203                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2204                         if (alsoprint)
2205                                 print ("^1",s1, "^1 burned to death\n");
2206                 } else if (type != DEATH_TEAMCHANGE && type != DEATH_QUIET) {
2207                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2208                         if (alsoprint)
2209                                 print ("^1",s1, "^1 couldn't resist the urge to self-destruct\n");
2210                 } 
2211                 
2212                 if (stof(s2) > 2) // killcount > 2
2213                         print ("^1",s1,"^1 ended it all after a ",s2," kill spree\n");
2214         } else if(msg == MSG_KILL) {
2215                 w = DEATH_WEAPONOF(type);
2216                 if(WEP_VALID(w)) {
2217                         HUD_KillNotify_Push(s2, s1, 1, w);
2218                         if (alsoprint)
2219                                 print("^1", s1, "^1 ", Weapon_KillMessage(type), "\n");
2220                 }
2221                 else if(type == KILL_TEAM_RED || type == KILL_TEAM_BLUE || type == KILL_TEAM_SPREE) {
2222                         HUD_KillNotify_Push(s1, s2, 1, type);
2223                         if(alsoprint)
2224                         {
2225                                 if(cvar("cl_gentle")) {
2226                                         print ("^1", s1, "^1 took action against a team mate\n");
2227                                 } else {
2228                                         print ("^1", s1, "^1 mows down a team mate\n");
2229                                 }
2230                         }
2231                         if (stof(s2) > 2 && type == KILL_TEAM_SPREE) {
2232                                 if(cvar("cl_gentle"))
2233                                         print ("^1",s1,"^1 ended a ",s3," scoring spree by going against a team mate\n");
2234                                 else
2235                                         print ("^1",s1,"^1 ended a ",s3," kill spree by killing a team mate\n");
2236                         }
2237                         else if (stof(s2) > 2) {
2238                                 if(cvar("cl_gentle"))
2239                                         print ("^1",s1,"'s ^1",s3," scoring spree was ended by a team mate!\n");
2240                                 else
2241                                         print ("^1",s1,"'s ^1",s3," kill spree was ended by a team mate!\n");
2242                         }
2243                 }
2244                 else if(type == KILL_FIRST_BLOOD)
2245                         print("^1",s1, "^1 drew first blood", "\n");
2246                 // TODO: icon!
2247                 else if (type == DEATH_TELEFRAG)
2248                         print ("^1",s1, "^1 was telefragged by ", s2, "\n");
2249                 else if (type == DEATH_DROWN) {
2250                         HUD_KillNotify_Push(s2, s1, 1, DEATH_DROWN);
2251                         if(alsoprint)
2252                                 print ("^1",s1, "^1 was drowned by ", s2, "\n");
2253                 }
2254                 else if (type == DEATH_SLIME) {
2255                         HUD_KillNotify_Push(s2, s1, 1, DEATH_SLIME);
2256                         if(alsoprint)
2257                                 print ("^1",s1, "^1 was slimed by ", s2, "\n");
2258                 }
2259                 else if (type == DEATH_LAVA) {
2260                         HUD_KillNotify_Push(s2, s1, 1, DEATH_LAVA);
2261                         if(alsoprint)
2262                                 print ("^1",s1, "^1 was cooked by ", s2, "\n");
2263                 }
2264                 else if (type == DEATH_FALL) {
2265                         HUD_KillNotify_Push(s2, s1, 1, DEATH_FALL);
2266                         if(alsoprint)
2267                                 print ("^1",s1, "^1 was grounded by ", s2, "\n");
2268                 }
2269                 else if (type == DEATH_SHOOTING_STAR) {
2270                         HUD_KillNotify_Push(s2, s1, 1, DEATH_SHOOTING_STAR);
2271                         if(alsoprint)
2272                                 print ("^1",s1, "^1 was shot into space by ", s2, "\n");
2273                 }
2274                 else if (type == DEATH_SWAMP) {
2275                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2276                         if(alsoprint)
2277                                 print ("^1",s1, "^1 was conserved by ", s2, "\n");
2278                 }
2279                 else if (type == DEATH_HURTTRIGGER)
2280                 {
2281                         HUD_KillNotify_Push(s2, s1, 1, DEATH_HURTTRIGGER);
2282                         if(alsoprint)
2283                                 print("^1",s1, "^1 was thrown into a world of hurt by ", s2, "\n");
2284                 } else if(type == DEATH_SBCRUSH) {
2285                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2286                         if(alsoprint)
2287                                 print ("^1",s1, "^1 was crushed by ^1", s2, "\n");
2288                 } else if(type == DEATH_SBMINIGUN) {
2289                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2290                         if(alsoprint)
2291                                 print ("^1",s1, "^1 got shredded by ^1", s2, "\n");
2292                 } else if(type == DEATH_SBROCKET) {
2293                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2294                         if(alsoprint)
2295                                 print ("^1",s1, "^1 was blased to bits by ^1", s2, "\n");
2296                 } else if(type == DEATH_SBBLOWUP) {
2297                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2298                         if(alsoprint)
2299                                 print ("^1",s1, "^1 got caught in the destruction of ^1", s2, "'s vehicle\n");
2300                 } else if(type == DEATH_WAKIGUN) {
2301                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2302                         if(alsoprint)
2303                                 print ("^1",s1, "^1 was bolted down by ^1", s2, "\n");
2304                 } else if(type == DEATH_WAKIROCKET) {
2305                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2306                         if(alsoprint)
2307                                 print ("^1",s1, "^1 could find no shelter from ^1", s2, "'s rockets\n");
2308                 } else if(type == DEATH_WAKIBLOWUP) {
2309                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2310                         if(alsoprint)
2311                                 print ("^1",s1, "^1 dies when ^1", s2, "'s wakizashi dies.\n");
2312                 } else if(type == DEATH_TURRET) {
2313                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2314                         if(alsoprint)
2315                                 print ("^1",s1, "^1 was pushed into the line of fire by ^1", s2, "\n");
2316                 } else if(type == DEATH_TOUCHEXPLODE) {
2317                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2318                         if(alsoprint)
2319                                 print ("^1",s1, "^1 was pushed into an accident by ^1", s2, "\n");
2320                 } else if(type == DEATH_CHEAT) {
2321                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2322                         if(alsoprint)
2323                                 print ("^1",s1, "^1 was unfairly eliminated by ^1", s2, "\n");
2324                 } else if (type == DEATH_FIRE) {
2325                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2326                         if(alsoprint)
2327                                 print ("^1",s1, "^1 was burnt to death by ^1", s2, "\n");
2328                 } else if (type == DEATH_CUSTOM) {
2329                         HUD_KillNotify_Push(s2, s1, 1, DEATH_CUSTOM);
2330                         if(alsoprint)
2331                                 print ("^1",s1, "^1 ", s2, "\n");
2332                 } else {
2333                         HUD_KillNotify_Push(s2, s1, 1, DEATH_GENERIC);
2334                         if(alsoprint)
2335                                 print ("^1",s1, "^1 was fragged by ", s2, "\n");
2336                 }
2337         } else if(msg == MSG_SPREE) {
2338                 if(type == KILL_END_SPREE) {
2339                         if(cvar("cl_gentle"))
2340                                 print ("^1",s1,"'s ^1", s2, " scoring spree was ended by ", s3, "\n");
2341                         else
2342                                 print ("^1",s1,"'s ^1", s2, " kill spree was ended by ", s3, "\n");
2343                 } else if(type == KILL_SPREE) {
2344                         if(cvar("cl_gentle"))
2345                                 print ("^1",s1,"^1 made ",s2," scores in a row\n");
2346                         else
2347                                 print ("^1",s1,"^1 has ",s2," frags in a row\n");
2348                 } else if(type == KILL_SPREE_3) {
2349                         if(cvar("cl_gentle"))
2350                                 print (s1,"^7 made a ^1TRIPLE SCORE\n");
2351                         else
2352                                 print (s1,"^7 made a ^1TRIPLE FRAG\n");
2353                 } else if(type == KILL_SPREE_5) {
2354                         if(cvar("cl_gentle"))
2355                                 print (s1,"^7 unleashes ^1SCORING RAGE\n");
2356                         else
2357                                 print (s1,"^7 unleashes ^1RAGE\n");
2358                 } else if(type == KILL_SPREE_10) {
2359                         if(cvar("cl_gentle"))
2360                                 print (s1,"^7 made ^1TEN SCORES IN A ROW!\n");
2361                         else
2362                                 print (s1,"^7 starts the ^1MASSACRE!\n");
2363                 } else if(type == KILL_SPREE_15) {
2364                         if(cvar("cl_gentle"))
2365                                 print (s1,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
2366                         else
2367                                 print (s1,"^7 executes ^1MAYHEM!\n");
2368                 } else if(type == KILL_SPREE_20) {
2369                         if(cvar("cl_gentle"))
2370                                 print (s1,"^7 made ^1TWENTY SCORES IN A ROW!\n");
2371                         else
2372                                 print (s1,"^7 is a ^1BERSERKER!\n");
2373                 } else if(type == KILL_SPREE_25) {
2374                         if(cvar("cl_gentle"))
2375                                 print (s1,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
2376                         else
2377                                 print (s1,"^7 inflicts ^1CARNAGE!\n");
2378                 } else if(type == KILL_SPREE_30) {
2379                         if(cvar("cl_gentle"))
2380                                 print (s1,"^7 made ^1THIRTY SCORES IN A ROW!\n");
2381                         else
2382                                 print (s1,"^7 unleashes ^1ARMAGEDDON!\n");
2383                 }
2384         } else if(msg == MSG_KILL_ACTION) { // wtf is this? isnt it basically the same as MSG_SUICIDE?
2385                 if (type == DEATH_DROWN) {
2386                         HUD_KillNotify_Push(s1, "", 0, DEATH_DROWN);
2387                         if(alsoprint)
2388                         {
2389                                 if(cvar("cl_gentle"))
2390                                         print ("^1",s1, "^1 was in the water for too long\n");
2391                                 else
2392                                         print ("^1",s1, "^1 drowned\n");
2393                         }
2394                 } else if (type == DEATH_SLIME) {
2395                         HUD_KillNotify_Push(s1, "", 0, DEATH_SLIME);
2396                         if(alsoprint)
2397                                 print ("^1",s1, "^1 was slimed\n");
2398                 } else if (type == DEATH_LAVA) {
2399                         HUD_KillNotify_Push(s1, "", 0, DEATH_LAVA);
2400                         if(alsoprint)
2401                         {
2402                                 if(cvar("cl_gentle"))
2403                                         print ("^1",s1, "^1 found a hot place\n");
2404                                 else
2405                                         print ("^1",s1, "^1 turned into hot slag\n");
2406                         }
2407                 } else if (type == DEATH_FALL) {
2408                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2409                         if(alsoprint)
2410                         {
2411                                 if(cvar("cl_gentle"))
2412                                         print ("^1",s1, "^1 tested gravity (and it worked)\n");
2413                                 else
2414                                         print ("^1",s1, "^1 hit the ground with a crunch\n");
2415                         }
2416                 } else if (type == DEATH_SHOOTING_STAR) {
2417                         HUD_KillNotify_Push(s1, "", 0, DEATH_SHOOTING_STAR);
2418                         if(alsoprint)
2419                                 print ("^1",s1, "^1 became a shooting star\n");
2420                 } else if (type == DEATH_SWAMP) {
2421                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2422                         if(alsoprint)
2423                         {
2424                                 if(cvar("cl_gentle"))
2425                                         print ("^1",s1, "^1 discovered a swamp\n");
2426                                 else
2427                                         print ("^1",s1, "^1 is now conserved for centuries to come\n");
2428                         }
2429                 } else if(type == DEATH_TURRET) {
2430                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2431                         if(alsoprint)
2432                                 print ("^1",s1, "^1 was mowed down by a turret \n");
2433                 } else if (type == DEATH_CUSTOM) {
2434                         HUD_KillNotify_Push(s1, "", 0, DEATH_CUSTOM);
2435                         if(alsoprint)
2436                                 print ("^1",s1, "^1 ", s2, "\n");
2437                 } else if (type == DEATH_HURTTRIGGER) {
2438                         HUD_KillNotify_Push(s1, "", 0, DEATH_HURTTRIGGER);
2439                         if(alsoprint)
2440                                 print ("^1",s1, "^1 was in the wrong place\n");
2441                 } else if(type == DEATH_TOUCHEXPLODE) {
2442                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2443                         if(alsoprint)
2444                                 print ("^1",s1, "^1 died in an accident\n");
2445                 } else if(type == DEATH_CHEAT) {
2446                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2447                         if(alsoprint)
2448                                 print ("^1",s1, "^1 was unfairly eliminated\n");
2449                 } else if(type == DEATH_FIRE) {
2450                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2451                         if(alsoprint)
2452                         {
2453                                 if(cvar("cl_gentle"))
2454                                         print ("^1",s1, "^1 felt a little hot\n");
2455                                 else
2456                                         print ("^1",s1, "^1 burnt to death\n");
2457                                 }
2458                 } else {
2459                         HUD_KillNotify_Push(s1, "", 0, DEATH_GENERIC);
2460                         if(alsoprint)
2461                         {
2462                                 if(cvar("cl_gentle"))
2463                                         print ("^1",s1, "^1 needs a restart\n");
2464                                 else
2465                                         print ("^1",s1, "^1 died\n");
2466                         }
2467                 }
2468         } else if(msg == MSG_KILL_ACTION_SPREE) {
2469                 if(cvar("cl_gentle"))
2470                         print ("^1",s1,"^1 needs a restart after a ",s2," scoring spree\n");
2471                 else
2472                         print ("^1",s1,"^1 died with a ",s2," kill spree\n");
2473         } else if(msg == MSG_INFO) {
2474                 if(type == INFO_GOTFLAG) { // here, s2 is the flag name
2475                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2476                         print(s1, "^7 got the ", s2, "\n");
2477                 } else if(type == INFO_LOSTFLAG) {
2478                         HUD_KillNotify_Push(s1, s2, 0, INFO_LOSTFLAG);
2479                         print(s1, "^7 lost the ", s2, "\n");
2480                 } else if(type == INFO_PICKUPFLAG) {
2481                         HUD_KillNotify_Push(s1, s2, 0, INFO_GOTFLAG);
2482                         print(s1, "^7 picked up the ", s2, "\n");
2483                 } else if(type == INFO_RETURNFLAG) {
2484                         HUD_KillNotify_Push(s1, s2, 0, INFO_RETURNFLAG);
2485                         print(s1, "^7 returned the ", s2, "\n");
2486                 }
2487         }
2488 }
2489
2490 #define DAMAGE_CENTERPRINT_SPACER NEWLINES
2491
2492 void HUD_Centerprint(string s1, string s2, float type, float msg)
2493 {
2494         if(msg == MSG_SUICIDE) {
2495                 if (type == DEATH_TEAMCHANGE) {
2496                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You are now on: ", s1));
2497                 } else if (type == DEATH_AUTOTEAMCHANGE) {
2498                         centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "You have been moved into a different team to improve team balance\nYou are now on: ", s1));
2499                 } else if (type == DEATH_CAMP) {
2500                         if(cvar("cl_gentle"))
2501                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Reconsider your tactics, camper!"));
2502                         else
2503                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Die camper!"));
2504                 } else if (type == DEATH_NOAMMO) {
2505                         if(cvar("cl_gentle"))
2506                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You are reinserted into the game for running out of ammo..."));
2507                         else
2508                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were killed for running out of ammo..."));
2509                 } else if (type == DEATH_ROT) {
2510                         if(cvar("cl_gentle"))
2511                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to preserve your health"));
2512                         else
2513                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You grew too old without taking your medicine"));
2514                 } else if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2515                         if(cvar("cl_gentle"))
2516                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't go against team mates!"));
2517                         else
2518                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't shoot your team mates!"));
2519                 } else if (type == DEATH_QUIET) {
2520                         // do nothing
2521                 } else if (type == DEATH_KILL) {
2522                         if(cvar("cl_gentle"))
2523                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to be more careful!"));
2524                         else
2525                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You killed your own dumb self!"));
2526                 }
2527         } else if(msg == MSG_KILL) {
2528                 if (type == KILL_TEAM_RED || type == KILL_TEAM_BLUE) {
2529                         if(cvar("cl_gentle")) {
2530                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You went against", s1, ",a team mate!"));
2531                         } else {
2532                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You fragged ", s1, ", a team mate!"));
2533                         }
2534                 } else if (type == KILL_FIRST_BLOOD) {
2535                         if(cvar("cl_gentle")) {
2536                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First score"));
2537                         } else {
2538                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First blood"));
2539                         }
2540                 } else if (type == KILL_FIRST_VICTIM) {
2541                         if(cvar("cl_gentle")) {
2542                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First casualty"));
2543                         } else {
2544                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1First victim"));
2545                         }
2546                 } else if (type == KILL_TYPEFRAG) { // s2 contains "advanced kill messages" such as ping, handicap...
2547                         if(cvar("cl_gentle")) {
2548                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You scored against ^7", s1, "^1 who was typing!", s2));
2549                         } else {
2550                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You typefragged ^7", s1, s2));
2551                         }
2552                 } else if (type == KILL_TYPEFRAGGED) {
2553                         if(cvar("cl_gentle")) {
2554                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, "^1 while you were typing!", s2));
2555                         } else {
2556                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were typefragged by ^7", s1, s2));
2557                         }
2558                 } else if (type == KILL_FRAG) {
2559                         if(cvar("cl_gentle")) {
2560                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You scored against ^7", s1, s2));
2561                         } else {
2562                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^4You fragged ^7", s1, s2));
2563                         }
2564                 } else if (type == KILL_FRAGGED) {
2565                         if(cvar("cl_gentle")) {
2566                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were scored against by ^7", s1, s2));
2567                         } else {
2568                                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were fragged by ^7", s1, s2));
2569                         }
2570                 }
2571         } else if(msg == MSG_KILL_ACTION) {
2572                 // TODO: invent more centerprints here?
2573                 centerprint(strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
2574         }
2575 }
2576
2577 void HUD_Notify (void)
2578 {
2579         float id = HUD_PANEL_NOTIFY;
2580         vector pos, mySize;
2581         pos = HUD_Panel_GetPos(id);
2582         mySize = HUD_Panel_GetSize(id);
2583
2584         HUD_Panel_DrawBg(id, pos, mySize, 0);
2585         float padding;
2586         padding = HUD_Panel_GetPadding(id);
2587         if(padding)
2588         {
2589                 pos += '1 1 0' * padding;
2590                 mySize -= '2 2 0' * padding;
2591         }
2592
2593         float entries, height;
2594         entries = bound(1, floor(14 * mySize_y/mySize_x), 12);
2595         height = mySize_y/entries;
2596         entries -= 2; // top/bottom two lines reserved for info messaged, such as spec instructions
2597         
2598         vector fontsize;
2599         fontsize = '0.5 0.5 0' * height;
2600
2601         float a;
2602         float when;
2603         when = cvar("hud_notify_time");
2604         float fadetime;
2605         fadetime = cvar("hud_notify_fadetime");
2606
2607         string s;
2608
2609         vector pos_attacker, pos_victim;
2610         vector weap_pos;
2611         float width_attacker, width_victim;
2612         string attacker, victim;
2613
2614         float i, j;
2615         for(j = 0; j < entries; ++j)
2616         {
2617                 if(cvar("hud_notify_flip"))
2618                         i = j;
2619                 else // rather nasty hack for ordering items from the bottom up
2620                         i = entries - j - 1;
2621                 if(cvar("hud_notify_info_top"))
2622                         i += 2; // top/bottom two lines reserved for info messaged, such as spec instructions
2623
2624                 if(fadetime)
2625                 {
2626                         if(killnotify_times[j] + when > time)
2627                                 a = 1;
2628                         else
2629                                 a = bound(0, (killnotify_times[j] + when + fadetime - time) / fadetime, 1);
2630                 }
2631                 else
2632                 {
2633                         if(killnotify_times[j] + when > time)
2634                                 a = 1;
2635                         else
2636                                 a = 0;
2637                 }
2638
2639                 // TODO: maybe print in team colors?
2640                 // TODO: maybe this could be cleaned up somehow...
2641                 // TODO: less copypaste code below
2642                 //
2643                 // Y [used by] X
2644                 if(killnotify_actiontype[j] == 0 && !hud_configure) 
2645                 {
2646                         attacker = textShortenToWidth(killnotify_attackers[j], mySize_x - 2 * height, fontsize, stringwidth_colors);
2647
2648                         width_attacker = stringwidth(attacker, TRUE, fontsize);
2649                         pos_attacker = pos + eX * 0.5 * (mySize_x - width_attacker + 2 * height) + eY * 0.5 * fontsize_y + eY * i * height;
2650
2651                         weap_pos = pos + eX * 0.5 * (mySize_x - width_attacker) - eX * height + eY * i * height;
2652                         if(killnotify_deathtype[j] == DEATH_GENERIC)
2653                         {
2654                                 drawpic_skin(weap_pos, "notify_death", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2655                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2656                         }
2657                         else if(killnotify_deathtype[j] == DEATH_NOAMMO)
2658                         {
2659                                 drawpic_skin(weap_pos, "notify_outofammo", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2660                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2661                         }
2662                         else if(killnotify_deathtype[j] == DEATH_KILL)
2663                         {
2664                                 drawpic_skin(weap_pos, "notify_selfkill", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2665                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2666                         }
2667                         else if(killnotify_deathtype[j] == DEATH_CAMP)
2668                         {
2669                                 drawpic_skin(weap_pos, "notify_camping", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2670                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2671                         }
2672                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
2673                         {
2674                                 drawpic_skin(weap_pos, "notify_teamkill", '2 1 0' * height, '1 0 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2675                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2676                         }
2677                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
2678                         {
2679                                 drawpic_skin(weap_pos, "notify_teamkill", '2 1 0' * height, '0 0 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2680                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2681                         }
2682                         else if(killnotify_deathtype[j] == DEATH_DROWN)
2683                         {
2684                                 drawpic_skin(weap_pos, "notify_water", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2685                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2686                         }
2687                         else if(killnotify_deathtype[j] == DEATH_SLIME)
2688                         {
2689                                 drawpic_skin(weap_pos, "notify_slime", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2690                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2691                         }
2692                         else if(killnotify_deathtype[j] == DEATH_LAVA)
2693                         {
2694                                 drawpic_skin(weap_pos, "notify_lava", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2695                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2696                         }
2697                         else if(killnotify_deathtype[j] == DEATH_FALL)
2698                         {
2699                                 drawpic_skin(weap_pos, "notify_fall", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2700                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2701                         }
2702                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
2703                         {
2704                                 drawpic_skin(weap_pos, "notify_shootingstar", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2705                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2706                         }
2707                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM)
2708                         {
2709                                 drawpic_skin(weap_pos, "notify_void", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2710                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2711                         }
2712                         else if(killnotify_deathtype[j] == INFO_GOTFLAG)
2713                         {
2714                                 if(killnotify_victims[j] == "^1RED^7 flag")
2715                                         s = "red";
2716                                 else
2717                                         s = "blue";
2718                                 drawpic_skin(weap_pos, strcat("flag_", s, "_carrying"), '1 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2719                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2720                         }
2721                         else if(killnotify_deathtype[j] == INFO_RETURNFLAG)
2722                         {
2723                                 if(killnotify_victims[j] == "^1RED^7 flag")
2724                                         s = "red";
2725                                 else
2726                                         s = "blue";
2727                                 drawpic_skin(weap_pos, strcat("flag_", s, "_taken"), '1 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2728                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2729                         }
2730                         else if(killnotify_deathtype[j] == INFO_LOSTFLAG)
2731                         {
2732                                 if(killnotify_victims[j] == "^1RED^7 flag")
2733                                         s = "red";
2734                                 else
2735                                         s = "blue";
2736                                 drawpic_skin(weap_pos, strcat("flag_", s, "_lost"), '1 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2737                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2738                         }
2739                 }
2740                 // X [did action to] Y
2741                 else
2742                 {
2743                         if(hud_configure)
2744                         {
2745                                 attacker = textShortenToWidth("Player1", 0.5 * mySize_x - height, fontsize, stringwidth_colors);
2746                                 victim = textShortenToWidth("Player2", 0.5 * mySize_x - height, fontsize, stringwidth_colors);
2747                                 a = bound(0, (when - j) / 4, 1);
2748                         }
2749                         else
2750                         {
2751                                 attacker = textShortenToWidth(killnotify_attackers[j], 0.5 * mySize_x - height, fontsize, stringwidth_colors);
2752                                 victim = textShortenToWidth(killnotify_victims[j], 0.5 * mySize_x - height, fontsize, stringwidth_colors);
2753                         }
2754                         width_attacker = stringwidth(attacker, TRUE, fontsize);
2755                         width_victim = stringwidth(victim, TRUE, fontsize);
2756                         pos_attacker = pos + eX * 0.5 * ((0.5 * mySize_x - height) - width_attacker) + eY * 0.5 * fontsize_y + eY * i * height;
2757                         pos_victim = pos + eX * 0.5 * ((0.5 * mySize_x - height) - width_victim) + eY * 0.5 * fontsize_y + eX * 0.5 * mySize_x + eX * height + eY * i * height;
2758                         weap_pos = pos + eX * 0.5 * mySize_x - eX * height + eY * i * height;
2759
2760                         if(hud_configure) // example actions for config mode
2761                         {
2762                                 drawpic_skin(weap_pos, strcat("weapon", "electro"), '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2763                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2764                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2765                         }
2766                         else if(WEP_VALID(killnotify_deathtype[j]))
2767                         {
2768                                 self = get_weaponinfo(killnotify_deathtype[j]);
2769                                 drawpic_skin(weap_pos, strcat("weapon", self.netname), '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2770                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2771                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2772                         }
2773                         else if(killnotify_deathtype[j] == KILL_TEAM_RED)
2774                         {
2775                                 drawpic_skin(weap_pos, "notify_teamkill", '2 1 0' * height, '1 0 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2776                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2777                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2778                         }
2779                         else if(killnotify_deathtype[j] == KILL_TEAM_BLUE)
2780                         {
2781                                 drawpic_skin(weap_pos, "notify_teamkill", '2 1 0' * height, '0 0 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2782                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2783                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2784                         }
2785                         else if(killnotify_deathtype[j] == DEATH_DROWN)
2786                         {
2787                                 drawpic_skin(weap_pos, "notify_water", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2788                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2789                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2790                         }
2791                         else if(killnotify_deathtype[j] == DEATH_SLIME)
2792                         {
2793                                 drawpic_skin(weap_pos, "notify_slime", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2794                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2795                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2796                         }
2797                         else if(killnotify_deathtype[j] == DEATH_LAVA)
2798                         {
2799                                 drawpic_skin(weap_pos, "notify_lava", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2800                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2801                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2802                         }
2803                         else if(killnotify_deathtype[j] == DEATH_FALL)
2804                         {
2805                                 drawpic_skin(weap_pos, "notify_fall", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2806                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2807                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2808                         }
2809                         else if(killnotify_deathtype[j] == DEATH_SHOOTING_STAR)
2810                         {
2811                                 drawpic_skin(weap_pos, "notify_shootingstar", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2812                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2813                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2814                         }
2815                         else if(killnotify_deathtype[j] == DEATH_HURTTRIGGER || killnotify_deathtype[j] == DEATH_CUSTOM) // DEATH_CUSTOM is also void, right?
2816                         {
2817                                 drawpic_skin(weap_pos, "notify_void", '2 1 0' * height, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2818                                 drawcolorcodedstring(pos_attacker, attacker, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2819                                 drawcolorcodedstring(pos_victim, victim, fontsize, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
2820                         }
2821                 }
2822
2823         }
2824
2825         // TODO: separate panel for these?
2826         // Info messages
2827         //
2828         entity tm;
2829         vector o;
2830         o = pos;
2831         if(cvar("hud_notify_info_top"))
2832                 o = pos + eY;
2833         else
2834                 o = pos + eY * mySize_y - eY * 2 * height;
2835         
2836         if(spectatee_status && !intermission)
2837         {
2838                 //drawfont = hud_bigfont;
2839                 if(spectatee_status == -1)
2840                         s = "^1Observing";
2841                 else
2842                         s = GetPlayerName(spectatee_status - 1);
2843
2844                 //s = textShortenToWidth(s, mySize_y, 0.5 * height, stringwidth_colors);
2845                 //drawcolorcodedstring(pos + eY * 0.25 * height, s, 0.5 * height, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2846                 //drawfont = hud_font;
2847
2848                 // spectator text in the upper right corner
2849                 if(spectatee_status == -1)
2850                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 to spectate");
2851                 else
2852                         s = strcat("^1Press ^3", getcommandkey("primary fire", "+attack"), "^1 for another player");
2853                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2854                 o += eY * fontsize_y;
2855
2856                 if(spectatee_status == -1)
2857                         s = strcat("^1Use ^3", getcommandkey("next weapon", "weapnext"), "^1 or ^3", getcommandkey("previous weapon", "weapprev"), "^1 to change the speed");
2858                 else
2859                         s = strcat("^1Press ^3", getcommandkey("secondary fire", "+attack2"), "^1 to observe");
2860                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2861                 o += eY * fontsize_y;
2862
2863                 s = strcat("^1Press ^3", getcommandkey("server info", "+show_info"), "^1 for gamemode info");
2864                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2865                 o += eY * fontsize_y;
2866
2867                 if(gametype == GAME_ARENA)
2868                         s = "^1Wait for your turn to join";
2869                 else if(gametype == GAME_LMS)
2870                 {
2871                         entity sk;
2872                         sk = playerslots[player_localentnum - 1];
2873                         if(sk.(scores[ps_primary]) >= 666)
2874                                 s = "^1Match has already begun";
2875                         else if(sk.(scores[ps_primary]) > 0)
2876                                 s = "^1You have no more lives left";
2877                         else
2878                                 s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
2879                 }
2880                 else
2881                         s = strcat("^1Press ^3", getcommandkey("jump", "+jump"), "^1 to join");
2882                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2883                 o += eY * fontsize_y;
2884
2885                 //show restart countdown:
2886                 if (time < getstatf(STAT_GAMESTARTTIME)) {
2887                         float countdown;
2888                         //we need to ceil, otherwise the countdown would be off by .5 when using round()
2889                         countdown = ceil(getstatf(STAT_GAMESTARTTIME) - time);
2890                         s = strcat("^1Game starts in ^3", ftos(countdown), "^1 seconds");
2891                         drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2892                         o += eY * fontsize_y;
2893                 }
2894         }
2895         if(warmup_stage && !intermission)
2896         {
2897                 s = "^2Currently in ^1warmup^2 stage!";
2898                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2899                 o += eY * fontsize_y;
2900         }
2901
2902         string blinkcolor;
2903         if(mod(time, 1) >= 0.5)
2904                 blinkcolor = "^1";
2905         else
2906                 blinkcolor = "^3";
2907
2908         if(ready_waiting && !intermission && !spectatee_status)
2909         {
2910                 if(ready_waiting_for_me)
2911                 {
2912                         if(warmup_stage)
2913                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " to end warmup");
2914                         else
2915                                 s = strcat(blinkcolor, "Press ^3", getcommandkey("ready", "ready"), blinkcolor, " once you are ready");
2916                 }
2917                 else
2918                 {
2919                         if(warmup_stage)
2920                                 s = strcat("^2Waiting for others to ready up to end warmup...");
2921                         else
2922                                 s = strcat("^2Waiting for others to ready up...");
2923                 }
2924                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2925                 o += eY * fontsize_y;
2926         }
2927         else if(warmup_stage && !intermission && !spectatee_status)
2928         {
2929                 s = strcat("^2Press ^3", getcommandkey("ready", "ready"), "^2 to end warmup");
2930                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2931                 o += eY * fontsize_y;
2932         }
2933
2934         if(teamplay && !intermission && !spectatee_status && gametype != GAME_CA && teamnagger)
2935         {
2936                 float ts_min, ts_max;
2937                 tm = teams.sort_next;
2938                 if (tm)
2939                 {
2940                         for(; tm.sort_next; tm = tm.sort_next)
2941                         {
2942                                 if(!tm.team_size || tm.team == COLOR_SPECTATOR)
2943                                         continue;
2944                                 if(!ts_min) ts_min = tm.team_size;
2945                                 else ts_min = min(ts_min, tm.team_size);
2946                                 if(!ts_max) ts_max = tm.team_size;
2947                                 else ts_max = max(ts_max, tm.team_size);
2948                         }
2949                         if ((ts_max - ts_min) > 1)
2950                         {
2951                                 s = strcat(blinkcolor, "Teamnumbers are unbalanced!");
2952                                 tm = GetTeam(myteam, false);
2953                                 if (tm)
2954                                 if (tm.team != COLOR_SPECTATOR)
2955                                 if (tm.team_size == ts_max)
2956                                         s = strcat(s, " Press ^3", getcommandkey("team menu", "menu_showteamselect"), blinkcolor, " to adjust");
2957
2958                                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2959                                 o += eY * fontsize_y;
2960                         }
2961                 }
2962         }
2963         if(hud_configure)
2964         {
2965                 s = "^7Press ^3ESC ^7to show HUD options.";
2966                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2967                 o += eY * fontsize_y;
2968                 s = "^3Doubleclick a panel for panel-specific options.";
2969                 drawcolorcodedstring(o, s, fontsize, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
2970                 o += eY * fontsize_y;
2971         }
2972 }
2973
2974 // Timer (#5)
2975 //
2976 string seconds_tostring(float sec)
2977 {
2978         float minutes;
2979         minutes = floor(sec / 60);
2980
2981         sec -= minutes * 60;
2982
2983         string s;
2984         s = ftos(100 + sec);
2985
2986         return strcat(ftos(minutes), ":", substring(s, 1, 3));
2987 }
2988
2989 void HUD_Timer(void)
2990 {
2991         float id = HUD_PANEL_TIMER;
2992         vector pos, mySize;
2993         pos = HUD_Panel_GetPos(id);
2994         mySize = HUD_Panel_GetSize(id);
2995
2996         HUD_Panel_DrawBg(id, pos, mySize, 0);
2997         float padding;
2998         padding = HUD_Panel_GetPadding(id);
2999         if(padding)
3000         {
3001                 pos += '1 1 0' * padding;
3002                 mySize -= '2 2 0' * padding;
3003         }
3004
3005         string timer;
3006         float timelimit, elapsedTime, timeleft, minutesLeft;
3007
3008         timelimit = getstatf(STAT_TIMELIMIT);
3009
3010         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
3011         timeleft = ceil(timeleft);
3012
3013         minutesLeft = floor(timeleft / 60);
3014
3015         vector timer_color;
3016         if(minutesLeft >= 5 || warmup_stage || timelimit == 0) //don't use red or yellow in warmup or when there is no timelimit
3017                 timer_color = '1 1 1'; //white
3018         else if(minutesLeft >= 1)
3019                 timer_color = '1 1 0'; //yellow
3020         else
3021                 timer_color = '1 0 0'; //red
3022
3023         if (cvar("hud_timer_increment") || timelimit == 0 || warmup_stage) {
3024                 if (time < getstatf(STAT_GAMESTARTTIME)) {
3025                         //while restart is still active, show 00:00
3026                         timer = seconds_tostring(0);
3027                 } else {
3028                         elapsedTime = floor(time - getstatf(STAT_GAMESTARTTIME)); //127
3029                         timer = seconds_tostring(elapsedTime);
3030                 }
3031         } else {
3032                 timer = seconds_tostring(timeleft);
3033         }
3034
3035         drawfont = hud_bigfont;
3036         drawstring_aspect(pos, timer, mySize, mySize_y, timer_color, HUD_Panel_GetFgAlpha(HUD_PANEL_TIMER), DRAWFLAG_NORMAL);
3037         drawfont = hud_font;
3038 }
3039
3040 // Radar (#6)
3041 //
3042 void HUD_Radar(void)
3043 {
3044         float id = HUD_PANEL_RADAR;
3045         vector pos, mySize;
3046         pos = HUD_Panel_GetPos(id);
3047         mySize = HUD_Panel_GetSize(id);
3048
3049         HUD_Panel_DrawBg(id, pos, mySize, 0);
3050         float padding;
3051         padding = HUD_Panel_GetPadding(id);
3052         if(padding)
3053         {
3054                 pos += '1 1 0' * padding;
3055                 mySize -= '2 2 0' * padding;
3056         }
3057
3058         local float color1, color2; // color already declared as a global in hud.qc
3059         local vector rgb;
3060         local entity tm;
3061         float scale2d, normalsize, bigsize;
3062         float f;
3063
3064         teamradar_origin2d = pos + 0.5 * mySize;
3065         teamradar_size2d = mySize;
3066
3067         if(minimapname == "")
3068                 return;
3069
3070         teamradar_loadcvars();
3071
3072         switch(hud_radar_zoommode)
3073         {
3074                 default:
3075                 case 0:
3076                         f = current_zoomfraction;
3077                         break;
3078                 case 1:
3079                         f = 1 - current_zoomfraction;
3080                         break;
3081                 case 2:
3082                         f = 0;
3083                         break;
3084                 case 3:
3085                         f = 1;
3086                         break;
3087         }
3088
3089         switch(hud_radar_rotation)
3090         {
3091                 case 0:
3092                         teamradar_angle = view_angles_y - 90;
3093                         break;
3094                 default:
3095                         teamradar_angle = 90 * hud_radar_rotation;
3096                         break;
3097         }
3098
3099         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
3100         teamradar_size2d = mySize;
3101
3102         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
3103
3104         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
3105         if(hud_radar_rotation == 0)
3106         {
3107                 // max-min distance must fit the radar in any rotation
3108                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
3109         }
3110         else
3111         {
3112                 vector c0, c1, c2, c3, span;
3113                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
3114                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
3115                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
3116                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
3117                 span = '0 0 0';
3118                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
3119                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
3120
3121                 // max-min distance must fit the radar in x=x, y=y
3122                 bigsize = min(
3123                         teamradar_size2d_x * scale2d / (1.05 * span_x),
3124                         teamradar_size2d_y * scale2d / (1.05 * span_y)
3125                 );
3126         }
3127
3128         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_radar_scale;
3129         if(bigsize > normalsize)
3130                 normalsize = bigsize;
3131
3132         teamradar_size =
3133                   f * bigsize
3134                 + (1 - f) * normalsize;
3135         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
3136                   f * (mi_min + mi_max) * 0.5
3137                 + (1 - f) * view_origin);
3138
3139         color1 = GetPlayerColor(player_localentnum-1);
3140         rgb = GetTeamRGB(color1);
3141
3142         drawsetcliparea(
3143                 pos_x,
3144                 pos_y,
3145                 mySize_x,
3146                 mySize_y
3147         );
3148
3149         draw_teamradar_background(hud_radar_background_alpha, hud_radar_foreground_alpha);
3150
3151         for(tm = world; (tm = find(tm, classname, "radarlink")); )
3152                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
3153         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
3154                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, HUD_Panel_GetFgAlpha(id));
3155         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
3156         {
3157                 color2 = GetPlayerColor(tm.sv_entnum);
3158                 //if(color == COLOR_SPECTATOR || color == color2)
3159                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
3160         }
3161         draw_teamradar_player(view_origin, view_angles, '1 1 1');
3162
3163         drawresetcliparea();
3164 };
3165
3166 // Score (#7)
3167 //
3168 void HUD_Score(void)
3169 {
3170         float id = HUD_PANEL_SCORE;
3171         vector pos, mySize;
3172         pos = HUD_Panel_GetPos(id);
3173         mySize = HUD_Panel_GetSize(id);
3174
3175         HUD_Panel_DrawBg(id, pos, mySize, 0);
3176         float padding;
3177         padding = HUD_Panel_GetPadding(id);
3178         if(padding)
3179         {
3180                 pos += '1 1 0' * padding;
3181                 mySize -= '2 2 0' * padding;
3182         }
3183
3184         float score, distribution, leader;
3185         float score_len;
3186         vector distribution_color;
3187         entity tm, pl, me;
3188         me = (spectatee_status > 0) ? playerslots[spectatee_status - 1] : playerslots[player_localentnum - 1];
3189
3190         // TODO... this (race part) still uses constant coordinates :/
3191         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
3192                 /*pl = players.sort_next;
3193                 if(pl == me)
3194                         pl = pl.sort_next;
3195                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
3196                         if(pl.scores[ps_primary] == 0)
3197                                 pl = world;
3198
3199                 score = me.(scores[ps_primary]);
3200
3201                 float racemin, racesec, racemsec;
3202                 float distsec, distmsec, minusplus;
3203
3204                 racemin = floor(score/(60 * TIME_FACTOR));
3205                 racesec = floor((score - racemin*(60 * TIME_FACTOR))/TIME_FACTOR);
3206                 racemsec = score - racemin*60*TIME_FACTOR - racesec*TIME_FACTOR;
3207
3208                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
3209                         // distribution display
3210                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3211
3212                         if (distribution < TIME_FACTOR && distribution > -TIME_FACTOR)
3213                                 distmsec = fabs(distribution);
3214                         else {
3215                                 distsec = floor(fabs(distribution)/TIME_FACTOR);
3216                                 distmsec = fabs(distribution) - distsec*TIME_FACTOR;
3217                                 if (distribution < 0)
3218                                         distsec = -distsec;
3219                         }
3220
3221                         if (distribution <= 0) {
3222                                 distribution_color = eY;
3223                                 minusplus = 1; // minusplus 1: always prefix with minus sign
3224                         }
3225                         else {
3226                                 distribution_color = eX;
3227                                 minusplus = 2; // minusplus 1: always prefix with plus sign
3228                         }
3229                         //HUD_DrawXNum(bottomright - '0 48 0' - '16 0 0' * TIME_DECIMALS, distmsec, -TIME_DECIMALS, 0, 16, distribution_color, 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3230                         //HUD_DrawXNum(bottomright - '68 48 0' - '16 0 0' * TIME_DECIMALS, distsec, 4, minusplus, 16, distribution_color, 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3231                         drawpic_skin(bottomright - '10 48 0' - '16 0 0' * TIME_DECIMALS, "num_dot", '16 16 0', distribution_color, HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE);
3232                 }
3233                 // race record display
3234                 if (distribution <= 0 || distribution == score) // draw the highlight background behind the timer if we have the lead
3235                         drawpic_skin(bottomright - '0 32 0' - '32 0 0' * (4 + TIME_DECIMALS), "num_leading_4", '0 28 0' + '32 0 0' * (4 + TIME_DECIMALS), '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3236
3237                 //HUD_DrawXNum(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0', racemsec, -TIME_DECIMALS, 0, 30, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3238                 //HUD_DrawXNum(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0'  - '66 0 0', racesec, -2, 0, 30, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3239                 drawpic_skin(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '18 0 0', "num_dot", '30 30 0', '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE);
3240
3241                 //HUD_DrawXNum(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '132 0 0', racemin, -2, 0, 30, '1 1 1', 0, 0, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3242                 drawpic_skin(bottomright - '0 32 0' - TIME_DECIMALS * '30 0 0' - '84 0 0', "num_colon", '30 30 0', '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_ADDITIVE);
3243                 */
3244         } else if (!teamplay) { // non-teamgames
3245                 // me vector := [team/connected frags id]
3246                 pl = players.sort_next;
3247                 if(pl == me)
3248                         pl = pl.sort_next;
3249
3250                 if(hud_configure)
3251                         distribution = 42;
3252                 else if(pl)
3253                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
3254                 else
3255                         distribution = 0;
3256
3257                 score = me.(scores[ps_primary]);
3258                 if(hud_configure)
3259                         score = 123;
3260
3261                 if(distribution >= 5) {
3262                         distribution_color = eY;
3263                         leader = 1;
3264                 } else if(distribution >= 0) {
3265                         distribution_color = '1 1 1';
3266                         leader = 1;
3267                 } else if(distribution >= -5)
3268                         distribution_color = '1 1 0';
3269                 else
3270                         distribution_color = eX;
3271
3272                 score_len = strlen(ftos(score));
3273
3274                 drawstring_aspect(pos + eX * 0.75 * mySize_x, ftos(distribution), eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, (1/3) * mySize_y, distribution_color, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3275                 if (leader)
3276                         drawpic_skin(pos, strcat("num_leading_", ftos(score_len)), eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3277                 drawfont = hud_bigfont;
3278                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, mySize_y, distribution_color, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3279                 drawfont = hud_font;
3280         } else { // teamgames
3281                 float max_fragcount;
3282                 max_fragcount = -99;
3283
3284                 float teamnum;
3285                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
3286                         if(tm.team == COLOR_SPECTATOR || (!tm.team_size && !hud_configure)) // no players? don't display
3287                                 continue;
3288                         score = tm.(teamscores[ts_primary]);
3289                         if(hud_configure)
3290                                 score = 123;
3291                         leader = 0;
3292                         
3293                         score_len = strlen(ftos(score));
3294
3295                         if (score > max_fragcount)
3296                                 max_fragcount = score;
3297
3298                         if(tm.team == myteam) {
3299                                 if (max_fragcount == score)
3300                                         leader = 1;
3301                                 if (leader)
3302                                         drawpic_skin(pos, strcat("num_leading_", ftos(score_len)), eX * 0.75 * mySize_x + eY * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3303                                 drawfont = hud_bigfont;
3304                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize_x + eY * mySize_y, mySize_y, GetTeamRGB(tm.team) * 0.8, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3305                                 drawfont = hud_font;
3306                         } else {
3307                                 if (max_fragcount == score)
3308                                         leader = 1;
3309                                 if (leader)
3310                                         drawpic_skin(pos + eX * 0.75 * mySize_x + eY * (1/3) * teamnum * mySize_y, strcat("num_leading_", ftos(score_len)), eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3311                                 drawstring_aspect(pos + eX * 0.75 * mySize_x + eY * (1/3) * teamnum * mySize_y, ftos(score), eX * 0.25 * mySize_x + eY * (1/3) * mySize_y, (1/3) * mySize_y, GetTeamRGB(tm.team) * 0.8, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3312                                 teamnum += 1;
3313                         }
3314                 }
3315         }
3316 }
3317
3318 // Race timer (#8)
3319 //
3320 void HUD_RaceTimer (void) {
3321         float id = HUD_PANEL_RACETIMER;
3322         vector pos, mySize;
3323         pos = HUD_Panel_GetPos(id);
3324         mySize = HUD_Panel_GetSize(id);
3325
3326         HUD_Panel_DrawBg(id, pos, mySize, 0);
3327         float padding;
3328         padding = HUD_Panel_GetPadding(id);
3329         if(padding)
3330         {
3331                 pos += '1 1 0' * padding;
3332                 mySize -= '2 2 0' * padding;
3333         }
3334
3335         // always force 2:1 aspect
3336         vector newSize;
3337         if(mySize_x/mySize_y > 2)
3338         {
3339                 newSize_x = 2 * mySize_y;
3340                 newSize_y = mySize_y;
3341
3342                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3343         }
3344         else
3345         {
3346                 newSize_y = 1/2 * mySize_x;
3347                 newSize_x = mySize_x;
3348
3349                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3350         }
3351         mySize = newSize;
3352
3353         drawfont = hud_bigfont;
3354         float a, t;
3355         string s, forcetime;
3356
3357         if(hud_configure)
3358         {
3359                 s = "0:13:37";
3360                 drawstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, FALSE, '0.60 0.60 0' * mySize_y), s, '0.60 0.60 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3361                 s = "^1Intermediate 1 (+15.42)";
3362                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.20 * mySize_y) + eY * 0.60 * mySize_y, s, '1 1 0' * 0.20 * mySize_y, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3363                 s = strcat("^1PENALTY: ", ftos_decimals(20 * 0.1, 1), " (missing a checkpoint)");
3364                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.20 * mySize_y) + eY * 0.80 * mySize_y, s, '1 1 0' * 0.20 * mySize_y, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3365         }
3366         else if(race_checkpointtime)
3367         {
3368                 a = bound(0, 2 - (time - race_checkpointtime), 1);
3369                 s = "";
3370                 forcetime = "";
3371                 if(a > 0) // just hit a checkpoint?
3372                 {
3373                         if(race_checkpoint != 254)
3374                         {
3375                                 if(race_time && race_previousbesttime)
3376                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, 0, race_previousbestname);
3377                                 else
3378                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, race_previousbestname);
3379                                 if(race_time)
3380                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
3381                         }
3382                 }
3383                 else
3384                 {
3385                         if(race_laptime && race_nextbesttime && race_nextcheckpoint != 254)
3386                         {
3387                                 a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
3388                                 if(a > 0) // next one?
3389                                 {
3390                                         s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), 0, race_nextbestname);
3391                                 }
3392                         }
3393                 }
3394
3395                 if(s != "" && a > 0)
3396                 {
3397                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3398                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3399                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3400                 }
3401
3402                 if(race_penaltytime)
3403                 {
3404                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
3405                         if(a > 0)
3406                         {
3407                                 s = strcat("^1PENALTY: ", ftos_decimals(race_penaltytime * 0.1, 1), " (", race_penaltyreason, ")");
3408                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3409                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3410                                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.8 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3411                         }
3412                 }
3413
3414                 if(forcetime != "")
3415                 {
3416                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
3417                         //drawstring_expanding(m - '16 0 0' * stringwidth(forcetime, FALSE), forcetime, '32 32 0', '1 1 1', HUD_Panel_GetFgAlpha(id), 0, a);
3418                         drawstring_expanding(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(forcetime, FALSE, '1 1 0' * 0.6 * mySize_y), forcetime, '1 1 0' * 0.6 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id), 0, a);
3419                 }
3420                 else
3421                         a = 1;
3422
3423                 if(race_laptime && race_checkpoint != 255)
3424                 {
3425                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
3426                         //drawstring(m - '16 0 0' * stringwidth(s, FALSE), s, '32 32 0', '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3427                         drawstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, FALSE, '0.6 0.6 0' * mySize_y), s, '0.6 0.6 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3428                 }
3429         }
3430         else
3431         {
3432                 if(race_mycheckpointtime)
3433                 {
3434                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
3435                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -!race_mycheckpointenemy, race_mycheckpointlapsdelta, race_mycheckpointenemy);
3436                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3437                         //drawcolorcodedstring(m - '0 16 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3438                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3439                 }
3440                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
3441                 {
3442                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
3443                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -!race_othercheckpointenemy, race_othercheckpointlapsdelta, race_othercheckpointenemy);
3444                         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3445                         //drawcolorcodedstring(m - '0 0 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3446                         drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3447                 }
3448
3449                 if(race_penaltytime && !race_penaltyaccumulator)
3450                 {
3451                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
3452                         a = bound(0, (1 + t - time), 1);
3453                         if(a > 0)
3454                         {
3455                                 if(time < t)
3456                                         s = strcat("^1PENALTY: ", ftos_decimals(t - time, 1), " (", race_penaltyreason, ")");
3457                                 else
3458                                         s = strcat("^2PENALTY: 0.0 (", race_penaltyreason, ")");
3459                                 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0); // work around DP bug (set OFS_PARAM5 to 0)
3460                                 //drawcolorcodedstring(m - '0 32 0' - '8 0 0' * stringwidth(s, TRUE), s, '16 16 0', HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3461                                 drawcolorcodedstring(pos + eX * 0.5 * mySize_x - '0.5 0 0' * stringwidth(s, TRUE, '1 1 0' * 0.2 * mySize_y) + eY * 0.6 * mySize_y, s, '1 1 0' * 0.2 * mySize_y, HUD_Panel_GetFgAlpha(id) * a, DRAWFLAG_NORMAL);
3462                         }
3463                 }
3464         }
3465
3466         drawfont = hud_font;
3467 }
3468
3469 // Vote window (#9)
3470 //
3471 float vote_yescount;
3472 float vote_nocount;
3473 float vote_needed;
3474 float vote_highlighted; // currently selected vote
3475
3476 float vote_active; // is there an active vote?
3477 float vote_prev; // previous state of vote_active to check for a change
3478 float vote_alpha;
3479 float vote_change; // "time" when vote_active changed
3480
3481 void HUD_VoteWindow(void) 
3482 {
3483         float id = HUD_PANEL_VOTE;
3484         vector pos, mySize;
3485         pos = HUD_Panel_GetPos(id);
3486         mySize = HUD_Panel_GetSize(id);
3487
3488         string s;
3489         float a;
3490         if(vote_active != vote_prev) {
3491                 vote_change = time;
3492                 vote_prev = vote_active;
3493         }
3494
3495         if(vote_active || hud_configure)
3496                 vote_alpha = bound(0, (time - vote_change) * 2, 1);
3497         else
3498                 vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);
3499
3500         if(hud_configure)
3501         {
3502                 vote_yescount = 3;
3503                 vote_nocount = 2;
3504                 vote_needed = 4;
3505         }
3506
3507         if(!vote_alpha)
3508                 return;
3509
3510         a = vote_alpha * bound(cvar_or("hud_vote_alreadyvoted_alpha", 0.75), 1 - vote_highlighted, 1);
3511
3512         HUD_Panel_DrawBg(id, pos, mySize, a);
3513         float padding;
3514         padding = HUD_Panel_GetPadding(id);
3515         if(padding)
3516         {
3517                 pos += '1 1 0' * padding;
3518                 mySize -= '2 2 0' * padding;
3519         }
3520
3521         // always force 2:1 aspect
3522         vector newSize;
3523         if(mySize_x/mySize_y > 2)
3524         {
3525                 newSize_x = 2 * mySize_y;
3526                 newSize_y = mySize_y;
3527
3528                 pos_x = pos_x + (mySize_x - newSize_x) / 2;
3529         }
3530         else
3531         {
3532                 newSize_y = 1/2 * mySize_x;
3533                 newSize_x = mySize_x;
3534
3535                 pos_y = pos_y + (mySize_y - newSize_y) / 2;
3536         }
3537         mySize = newSize;
3538
3539         drawpic_skin(pos, "voteprogress_back", mySize, '1 1 1', a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3540
3541         s = "A vote has been called for: ";
3542         drawstring_aspect(pos + eY * (1/12) * mySize_y, s, eX * mySize_x + eY * (3/12) * mySize_y, mySize_y*(3/12), '1 1 1', a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3543         s = textShortenToWidth(vote_called_vote, mySize_x, '1 1 0' * mySize_y * (3/12), stringwidth_colors); // TODO: broken?
3544         if(hud_configure)
3545                 s = "Configure the HUD";
3546         drawstring_aspect(pos + eY * (4/12) * mySize_y, s, eX * mySize_x + eY * (2/12) * mySize_y, mySize_y*(2/12), '1 1 1', a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3547
3548         // print the yes/no counts
3549         s = strcat("Yes: ", ftos(vote_yescount));
3550         drawstring(pos + '0 0.6 0' * mySize_y + '0.02 0 0' * mySize_x, s, '1 1 0' * mySize_y*(1/6) , eY, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3551         s = strcat("No: ", ftos(vote_nocount));
3552         drawstring(pos + '0 0.6 0' * mySize_y + '0.98 0 0' * mySize_x - eX * stringwidth(s, FALSE, '1 1 0' * mySize_y*(1/6)), s, '1 1 0' * mySize_y*(1/6), eX, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3553
3554         // draw the progress bars
3555         drawsetcliparea(pos_x, pos_y, mySize_x * 0.5 * (vote_yescount/vote_needed), mySize_y);
3556         drawpic_skin(pos, "voteprogress_prog", mySize, eY, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3557
3558         drawsetcliparea(pos_x + mySize_x - mySize_x * 0.5 * (vote_nocount/vote_needed), pos_y, mySize_x * 0.5, mySize_y);
3559         drawpic_skin(pos, "voteprogress_prog", mySize, eX, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3560
3561         // draw the highlights
3562         if(vote_highlighted == 1) {
3563                 drawsetcliparea(pos_x, pos_y, mySize_x * 0.5, mySize_y);
3564                 drawpic_skin(pos, "voteprogress_voted", mySize, eY, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3565         }
3566         else if(vote_highlighted == 2) {
3567                 drawsetcliparea(pos_x + 0.5 * mySize_x, pos_y, mySize_x * 0.5, mySize_y);
3568                 drawpic_skin(pos, "voteprogress_voted", mySize, eX, a * HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
3569         }
3570
3571         drawresetcliparea();
3572
3573         if(!vote_active) {
3574                 vote_highlighted = 0;
3575         }
3576 }
3577
3578 // Mod icons panel (#10)
3579 //
3580
3581 float mod_active; // is there any active mod icon?
3582
3583 // CTF HUD modicon section
3584 float redflag_prevframe, blueflag_prevframe; // status during previous frame
3585 float redflag_prevstatus, blueflag_prevstatus; // last remembered status
3586 float redflag_statuschange_time, blueflag_statuschange_time; // time when the status changed
3587
3588 void HUD_Mod_CTF_Reset(void)
3589 {
3590         redflag_prevstatus = blueflag_prevstatus = redflag_prevframe = blueflag_prevframe = redflag_statuschange_time = blueflag_statuschange_time = 0;
3591 }
3592
3593 void HUD_Mod_CTF(vector pos, vector mySize)
3594 {
3595         vector redflag_pos, blueflag_pos;
3596         vector flag_size;
3597         float f; // every function should have that
3598
3599         float redflag, blueflag; // current status
3600         float redflag_statuschange_elapsedtime, blueflag_statuschange_elapsedtime; // time since the status changed
3601         float stat_items;
3602
3603         stat_items = getstati(STAT_ITEMS);
3604         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
3605         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
3606         
3607         if(redflag || blueflag)
3608                 mod_active = 1;
3609         else
3610                 mod_active = 0;
3611
3612         if(hud_configure)
3613         {
3614                 redflag = 1;
3615                 blueflag = 2;
3616         }
3617
3618         // when status CHANGES, set old status into prevstatus and current status into status
3619         if (redflag != redflag_prevframe)
3620         {
3621                 redflag_statuschange_time = time;
3622                 redflag_prevstatus = redflag_prevframe;
3623                 redflag_prevframe = redflag;
3624         }
3625
3626         if (blueflag != blueflag_prevframe)
3627         {
3628                 blueflag_statuschange_time = time;
3629                 blueflag_prevstatus = blueflag_prevframe;
3630                 blueflag_prevframe = blueflag;
3631         }
3632
3633         redflag_statuschange_elapsedtime = time - redflag_statuschange_time;
3634         blueflag_statuschange_elapsedtime = time - blueflag_statuschange_time;
3635
3636         float BLINK_FACTOR = 0.15;
3637         float BLINK_BASE = 0.85;
3638         // note:
3639         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
3640         // thus
3641         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
3642         // ensure RMS == 1
3643         float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
3644
3645         string red_icon, red_icon_prevstatus;
3646         float red_alpha, red_alpha_prevstatus;
3647         red_alpha = red_alpha_prevstatus = 1;
3648         switch(redflag) {
3649                 case 1: red_icon = "flag_red_taken"; break;
3650                 case 2: red_icon = "flag_red_lost"; break;
3651                 case 3: red_icon = "flag_red_carrying"; red_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3652                 default:
3653                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
3654                                 red_icon = "flag_red_shielded";
3655                         else
3656                                 red_icon = string_null;
3657                         break;
3658         }
3659         switch(redflag_prevstatus) {
3660                 case 1: red_icon_prevstatus = "flag_red_taken"; break;
3661                 case 2: red_icon_prevstatus = "flag_red_lost"; break;
3662                 case 3: red_icon_prevstatus = "flag_red_carrying"; red_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3663                 default:
3664                         if(redflag == 3)
3665                                 red_icon_prevstatus = "flag_red_carrying"; // make it more visible
3666                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM2))
3667                                 red_icon_prevstatus = "flag_red_shielded";
3668                         else
3669                                 red_icon_prevstatus = string_null;
3670                         break;
3671         }
3672
3673         string blue_icon, blue_icon_prevstatus;
3674         float blue_alpha, blue_alpha_prevstatus;
3675         blue_alpha = blue_alpha_prevstatus = 1;
3676         switch(blueflag) {
3677                 case 1: blue_icon = "flag_blue_taken"; break;
3678                 case 2: blue_icon = "flag_blue_lost"; break;
3679                 case 3: blue_icon = "flag_blue_carrying"; blue_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3680                 default:
3681                         if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
3682                                 blue_icon = "flag_blue_shielded";
3683                         else
3684                                 blue_icon = string_null;
3685                         break;
3686         }
3687         switch(blueflag_prevstatus) {
3688                 case 1: blue_icon_prevstatus = "flag_blue_taken"; break;
3689                 case 2: blue_icon_prevstatus = "flag_blue_lost"; break;
3690                 case 3: blue_icon_prevstatus = "flag_blue_carrying"; blue_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break;
3691                 default:
3692                         if(blueflag == 3)
3693                                 blue_icon_prevstatus = "flag_blue_carrying"; // make it more visible
3694                         else if((stat_items & IT_CTF_SHIELDED) && (myteam == COLOR_TEAM1))
3695                                 blue_icon_prevstatus = "flag_blue_shielded";
3696                         else
3697                                 blue_icon_prevstatus = string_null;
3698                         break;
3699         }
3700
3701         if(mySize_x > mySize_y) {
3702                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
3703                         redflag_pos = pos;
3704                         blueflag_pos = pos + eX * 0.5 * mySize_x;
3705                 } else {
3706                         blueflag_pos = pos;
3707                         redflag_pos = pos + eX * 0.5 * mySize_x;
3708                 }
3709                 flag_size = eX * 0.5 * mySize_x + eY * mySize_y;
3710         } else {
3711                 if (myteam == COLOR_TEAM1) { // always draw own flag on left
3712                         redflag_pos = pos;
3713                         blueflag_pos = pos + eY * 0.5 * mySize_y;
3714                 } else {
3715                         blueflag_pos = pos;
3716                         redflag_pos = pos + eY * 0.5 * mySize_y;
3717                 }
3718                 flag_size = eY * 0.5 * mySize_y + eX * mySize_x;
3719         }
3720
3721         f = bound(0, redflag_statuschange_elapsedtime*2, 1);
3722         if(red_icon_prevstatus && f < 1)
3723                 drawpic_skin_expanding(redflag_pos, red_icon_prevstatus, flag_size, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * red_alpha_prevstatus, DRAWFLAG_NORMAL, f);
3724         if(red_icon)
3725                 drawpic_skin(redflag_pos, red_icon, flag_size, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * red_alpha * f, DRAWFLAG_NORMAL);
3726
3727         f = bound(0, blueflag_statuschange_elapsedtime*2, 1);
3728         if(blue_icon_prevstatus && f < 1)
3729                 drawpic_skin_expanding(blueflag_pos, blue_icon_prevstatus, flag_size, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * blue_alpha_prevstatus, DRAWFLAG_NORMAL, f);
3730         if(blue_icon)
3731                 drawpic_skin(blueflag_pos, blue_icon, flag_size, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * blue_alpha * f, DRAWFLAG_NORMAL);
3732 }
3733
3734 // Keyhunt HUD modicon section
3735 float kh_runheretime;
3736
3737 void HUD_Mod_KH_Reset(void)
3738 {
3739         kh_runheretime = 0;
3740 }
3741
3742 void HUD_Mod_KH(vector pos, vector mySize)
3743 {
3744         mod_active = 1; // keyhunt should never hide the mod icons panel
3745         float kh_keys;
3746         float keyteam;
3747         float a, aa;
3748         vector p, pa, kh_size, kh_asize;
3749
3750         kh_keys = getstati(STAT_KH_KEYS);
3751
3752         p_x = pos_x;
3753         if(mySize_x > mySize_y)
3754         {
3755                 p_y = pos_y + 0.25 * mySize_y;
3756                 pa = p - eY * 0.25 * mySize_y;
3757
3758                 kh_size_x = mySize_x * 0.25;
3759                 kh_size_y = 0.75 * mySize_y;
3760                 kh_asize_x = mySize_x * 0.25;
3761                 kh_asize_y = mySize_y * 0.25;
3762         }
3763         else
3764         {
3765                 p_y = pos_y + 0.125 * mySize_y;
3766                 pa = p - eY * 0.125 * mySize_y;
3767
3768                 kh_size_x = mySize_x * 0.5;
3769                 kh_size_y = 0.375 * mySize_y;
3770                 kh_asize_x = mySize_x * 0.5;
3771                 kh_asize_y = mySize_y * 0.125;
3772         }
3773
3774         float i, key;
3775
3776         float keycount;
3777         keycount = 0;
3778         for(i = 0; i < 4; ++i)
3779         {
3780                 key = floor(kh_keys / pow(32, i)) & 31;
3781                 keyteam = key - 1;
3782                 if(keyteam == 30 && keycount <= 4)
3783                         keycount += 4;
3784                 if(keyteam == myteam || keyteam == -1 || keyteam == 30)
3785                         keycount += 1;
3786         }
3787
3788         // this yields 8 exactly if "RUN HERE" shows
3789
3790         if(keycount == 8)
3791         {
3792                 if(!kh_runheretime)
3793                         kh_runheretime = time;
3794                 pa_y -= fabs(sin((time - kh_runheretime) * 3.5)) * 6; // make the arrows jump in case of RUN HERE
3795         }
3796         else
3797                 kh_runheretime = 0;
3798
3799         for(i = 0; i < 4; ++i)
3800         {
3801                 key = floor(kh_keys / pow(32, i)) & 31;
3802                 keyteam = key - 1;
3803                 switch(keyteam)
3804                 {
3805                         case 30: // my key
3806                                 keyteam = myteam;
3807                                 a = 1;
3808                                 aa = 1;
3809                                 break;
3810                         case -1: // no key
3811                                 a = 0;
3812                                 aa = 0;
3813                                 break;
3814                         default: // owned or dropped
3815                                 a = 0.2;
3816                                 aa = 0.5;
3817                                 break;
3818                 }
3819                 a = a * HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS);
3820                 aa = aa * HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS);
3821                 if(a > 0)
3822                 {
3823                         switch(keyteam)
3824                         {
3825                                 case COLOR_TEAM1:
3826                                         drawpic_skin(pa, "kh_redarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3827                                         break;
3828                                 case COLOR_TEAM2:
3829                                         drawpic_skin(pa, "kh_bluearrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3830                                         break;
3831                                 case COLOR_TEAM3:
3832                                         drawpic_skin(pa, "kh_yellowarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3833                                         break;
3834                                 case COLOR_TEAM4:
3835                                         drawpic_skin(pa, "kh_pinkarrow", kh_asize, '1 1 1', aa, DRAWFLAG_NORMAL);  // show 30% alpha key
3836                                         break;
3837                                 default:
3838                                         break;
3839                         }
3840                         switch(i) // YAY! switch(i) inside a for loop for i. DailyWTF, here we come!
3841                         {
3842                                 case 0:
3843                                         drawpic_skin(p, "kh_red", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3844                                         break;
3845                                 case 1:
3846                                         drawpic_skin(p, "kh_blue", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3847                                         break;
3848                                 case 2:
3849                                         drawpic_skin(p, "kh_yellow", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3850                                         break;
3851                                 case 3:
3852                                         drawpic_skin(p, "kh_pink", kh_size, '1 1 1', a, DRAWFLAG_NORMAL);  // show 30% alpha key
3853                                         break;
3854                         }
3855                 }
3856                 if(mySize_x > mySize_y)
3857                 {
3858                         p_x += 0.25 * mySize_x;
3859                         pa_x += 0.25 * mySize_x;
3860                 }
3861                 else
3862                 {
3863                         if(i == 1)
3864                         {
3865                                 p_y = pos_y + 0.625 * mySize_y;
3866                                 pa_y = pos_y + 0.5 * mySize_y;
3867                                 p_x = pos_x;
3868                                 pa_x = pos_x;
3869                         }
3870                         else
3871                         {
3872                                 p_x += 0.5 * mySize_x;
3873                                 pa_x += 0.5 * mySize_x;
3874                         }
3875                 }
3876         }
3877 }
3878
3879 // Nexball HUD mod icon
3880 void HUD_Mod_NexBall(vector pos, vector mySize)
3881 {
3882         float stat_items, nb_pb_starttime, dt, p;
3883
3884         stat_items = getstati(STAT_ITEMS);
3885         nb_pb_starttime = getstatf(STAT_NB_METERSTART);
3886
3887         if (stat_items & IT_KEY1)
3888                 mod_active = 1;
3889         else
3890                 mod_active = 0;
3891
3892         //Manage the progress bar if any
3893         if (nb_pb_starttime > 0)
3894         {
3895                 dt = mod(time - nb_pb_starttime, nb_pb_period);
3896                 // one period of positive triangle
3897                 p = 2 * dt / nb_pb_period;
3898                 if (p > 1)
3899                         p = 2 - p;
3900
3901                 //Draw the filling
3902                 vector barsize;
3903                 float vertical;
3904                 if(mySize_x > mySize_y)
3905                 {
3906                         barsize = eX * p * mySize_x + eY * mySize_y;
3907                         vertical = 0;
3908                 }
3909                 else
3910                 {
3911                         barsize = eX * mySize_x + eY * p * mySize_y;
3912                         vertical = 1;
3913                 }
3914                 HUD_Panel_DrawProgressBar(pos, vertical, barsize, HUD_Panel_GetProgressBarColor("nexball"), cvar("hud_progressbar_alpha") * HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3915         }
3916
3917         if (stat_items & IT_KEY1)
3918                 drawpic_skin(pos, "nexball_carrying", eX * mySize_x + eY * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3919 }
3920
3921 // Race/CTS HUD mod icons
3922 float crecordtime_prev; // last remembered crecordtime
3923 float crecordtime_change_time; // time when crecordtime last changed
3924 float srecordtime_prev; // last remembered srecordtime
3925 float srecordtime_change_time; // time when srecordtime last changed
3926
3927 float race_status_time;
3928 float race_status_prev;
3929 string race_status_name_prev;
3930 void HUD_Mod_Race(vector pos, vector mySize)
3931 {
3932         mod_active = 1; // race should never hide the mod icons panel
3933         entity me;
3934         me = playerslots[player_localentnum - 1];
3935         float t, score;
3936         float f; // yet another function has this
3937         score = me.(scores[ps_primary]);
3938
3939         if not((scores_flags[ps_primary] & SFL_TIME) && !teamplay) // race/cts record display on HUD
3940                 return; // no records in the actual race
3941
3942         drawfont = hud_bigfont;
3943
3944         // clientside personal record
3945         string rr;
3946         if(gametype == GAME_CTS)
3947                 rr = CTS_RECORD;
3948         else
3949                 rr = RACE_RECORD;
3950         t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
3951
3952         if(score && (score < t || !t)) {
3953                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
3954                 if(cvar("cl_autodemo_delete_keeprecords"))
3955                 {
3956                         f = cvar("cl_autodemo_delete");
3957                         f &~= 1;
3958                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
3959                 }
3960         }
3961
3962         if(t != crecordtime_prev) {
3963                 crecordtime_prev = t;
3964                 crecordtime_change_time = time;
3965         }
3966         f = time - crecordtime_change_time;
3967
3968         if (f > 1) {
3969                 drawstring_aspect(pos, "Personal best ", eX * 0.5 * mySize_x + eY * 0.25 * mySize_y, 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3970                 drawstring(pos + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t), '1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3971         } else {
3972                 drawstring(pos, "Personal best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3973                 drawstring(pos + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t), '1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3974                 drawstring_expanding(pos, "Personal best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL, f);
3975                 drawstring_expanding(pos + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t), '1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL, f);
3976         }
3977
3978         // server record
3979         t = race_server_record;
3980         if(t != srecordtime_prev) {
3981                 srecordtime_prev = t;
3982                 srecordtime_change_time = time;
3983         }
3984         f = time - srecordtime_change_time;
3985
3986         if (f > 1) {
3987                 drawstring(pos + eY * 0.5 * mySize_y, "Server best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3988                 drawstring(pos + eY * 0.5 * mySize_y + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t),'1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3989         } else {
3990                 drawstring(pos + eY * 0.5 * mySize_y, "Server best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3991                 drawstring(pos + eY * 0.5 * mySize_y + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t),'1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL);
3992                 drawstring_expanding(pos + eY * 0.5 * mySize_y, "Server best ", '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL, f);
3993                 drawstring_expanding(pos + eY * 0.5 * mySize_y + eY * 0.2 * mySize_y, TIME_ENCODED_TOSTRING(t),'1 1 0' * 0.2 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS), DRAWFLAG_NORMAL, f);
3994         }
3995
3996         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
3997                 race_status_time = time + 5;
3998                 race_status_prev = race_status;
3999                 if (race_status_name_prev)
4000                         strunzone(race_status_name_prev);
4001                 race_status_name_prev = strzone(race_status_name);
4002         }
4003
4004         pos_x += mySize_x/2;
4005         // race "awards"
4006         float a;
4007         a = bound(0, race_status_time - time, 1);
4008
4009         string s;
4010         s = textShortenToWidth(race_status_name, mySize_y, '1 1 0' * 0.1 * mySize_y, stringwidth_colors);
4011
4012         float rank;
4013         if(race_status > 0)
4014                 rank = race_CheckName(race_status_name);
4015         string rankname;
4016         rankname = race_PlaceName(rank);
4017
4018         vector namepos;
4019         namepos = pos + '0.5 0.9 0' * mySize_y - eX * stringwidth(s, TRUE, '1 1 0' * 0.1 * mySize_y);
4020         vector rankpos;
4021         rankpos = pos + '0.5 0.25 0' * mySize_y - eX * stringwidth(rankname, TRUE, '1 1 0' * 0.15 * mySize_y);
4022
4023         if(race_status == 0)
4024                 drawpic_skin(pos, "race_newfail", '1 1 0' * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4025         else if(race_status == 1) {
4026                 drawpic_skin(pos, "race_newtime", '1 1 0' * 0.9 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4027                 drawcolorcodedstring(namepos, s, '1 1 0' * 0.1 * mySize_y, HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4028                 drawstring(rankpos, rankname, '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4029         } else if(race_status == 2) {
4030                 if(race_status_name == GetPlayerName(player_localentnum -1) || !race_myrank || race_myrank < rank)
4031                         drawpic_skin(pos, "race_newrankgreen", '1 1 0' * 0.9 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4032                 else
4033                         drawpic_skin(pos, "race_newrankyellow", '1 1 0' * 0.9 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4034                 drawcolorcodedstring(namepos, s, '1 1 0' * 0.1 * mySize_y, HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4035                 drawstring(rankpos, rankname, '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4036         } else if(race_status == 3) {
4037                 drawpic_skin(pos, "race_newrecordserver", '1 1 0' * 0.9 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4038                 drawcolorcodedstring(namepos, s, '1 1 0' * 0.1 * mySize_y, HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4039                 drawstring(rankpos, rankname, '1 1 0' * 0.15 * mySize_y, '1 1 1', HUD_Panel_GetFgAlpha(HUD_PANEL_MODICONS) * a, DRAWFLAG_NORMAL);
4040         }
4041
4042         if (race_status_time - time <= 0) {
4043                 race_status_prev = -1;
4044                 race_status = -1;
4045                 if(race_status_name)
4046                         strunzone(race_status_name);
4047                 race_status_name = string_null;
4048                 if(race_status_name_prev)
4049                         strunzone(race_status_name_prev);
4050                 race_status_name_prev = string_null;
4051         }
4052         drawfont = hud_font;
4053 }
4054
4055 float mod_prev; // previous state of mod_active to check for a change
4056 float mod_alpha;
4057 float mod_change; // "time" when mod_active changed
4058
4059 void HUD_ModIcons(void)
4060 {
4061         if (gametype != GAME_KEYHUNT && gametype != GAME_CTF && gametype != GAME_NEXBALL && gametype != GAME_CTS && gametype != GAME_RACE && !hud_configure)
4062                 return;
4063
4064         float id = HUD_PANEL_MODICONS;
4065         vector pos, mySize;
4066         pos = HUD_Panel_GetPos(id);
4067         mySize = HUD_Panel_GetSize(id);
4068
4069         if(mod_active != mod_prev) {
4070                 mod_change = time;
4071                 mod_prev = mod_active;
4072         }
4073
4074         if(mod_active || hud_configure)
4075                 mod_alpha = bound(0, (time - mod_change) * 2, 1);
4076         else
4077                 mod_alpha = bound(0, 1 - (time - mod_change) * 2, 1);
4078
4079         if(mod_alpha)
4080                 HUD_Panel_DrawBg(id, pos, mySize, mod_alpha);
4081
4082         float padding;
4083         padding = HUD_Panel_GetPadding(id);
4084         if(padding)
4085         {
4086                 pos += '1 1 0' * padding;
4087                 mySize -= '2 2 0' * padding;
4088         }
4089
4090         // these MUST be ran in order to update mod_active
4091         if(gametype == GAME_KEYHUNT)
4092                 HUD_Mod_KH(pos, mySize);
4093         else if(gametype == GAME_CTF || hud_configure)
4094                 HUD_Mod_CTF(pos, mySize); // forcealpha only needed for ctf icons, as only they are shown in config mode
4095         else if(gametype == GAME_NEXBALL)
4096                 HUD_Mod_NexBall(pos, mySize);
4097         else if(gametype == GAME_CTS || gametype == GAME_RACE)
4098                 HUD_Mod_Race(pos, mySize);
4099 }
4100
4101 // Draw pressed keys (#11)
4102 //
4103 void HUD_DrawPressedKeys(void)
4104 {
4105         float id = HUD_PANEL_PRESSEDKEYS;
4106         vector pos, mySize;
4107         pos = HUD_Panel_GetPos(id);
4108         mySize = HUD_Panel_GetSize(id);
4109
4110         HUD_Panel_DrawBg(id, pos, mySize, 0);
4111         float padding;
4112         padding = HUD_Panel_GetPadding(id);
4113         if(padding)
4114         {
4115                 pos += '1 1 0' * padding;
4116                 mySize -= '2 2 0' * padding;
4117         }
4118
4119         vector keysize;
4120         keysize = eX * mySize_x * (1/3) + eY * mySize_y * 0.5;
4121         float pressedkeys;
4122
4123         pressedkeys = getstatf(STAT_PRESSED_KEYS);
4124         drawpic_skin(pos, ((pressedkeys & KEY_CROUCH) ? "key_crouch_inv.tga" : "key_crouch.tga"), keysize, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4125         drawpic_skin(pos + eX * mySize_x * (1/3), ((pressedkeys & KEY_FORWARD) ? "key_forward_inv.tga" : "key_forward.tga"), keysize, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4126         drawpic_skin(pos + eX * mySize_x * (2/3), ((pressedkeys & KEY_JUMP) ? "key_jump_inv.tga" : "key_jump.tga"), keysize, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4127         drawpic_skin(pos + eY * 0.5 * mySize_y, ((pressedkeys & KEY_LEFT) ? "key_left_inv.tga" : "key_left.tga"), keysize, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4128         drawpic_skin(pos + eY * 0.5 * mySize_y + eX * mySize_x * (1/3), ((pressedkeys & KEY_BACKWARD) ? "key_backward_inv.tga" : "key_backward.tga"), keysize, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4129         drawpic_skin(pos + eY * 0.5 * mySize_y + eX * mySize_x * (2/3), ((pressedkeys & KEY_RIGHT) ? "key_right_inv.tga" : "key_right.tga"), keysize, '1 1 1', HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4130 }
4131
4132 // Handle chat as a panel (#12)
4133 //
4134 void HUD_Chat(void)
4135 {
4136         float id = HUD_PANEL_CHAT;
4137         vector pos, mySize;
4138         pos = HUD_Panel_GetPos(id);
4139         mySize = HUD_Panel_GetSize(id);
4140
4141         HUD_Panel_DrawBg(id, pos, mySize, 0);
4142         float padding;
4143         padding = HUD_Panel_GetPadding(id);
4144         if(padding)
4145         {
4146                 pos += '1 1 0' * padding;
4147                 mySize -= '2 2 0' * padding;
4148         }
4149
4150         cvar_set("con_chatrect", "1");
4151
4152         cvar_set("con_chatrect_x", ftos(pos_x/vid_conwidth));
4153         cvar_set("con_chatrect_y", ftos(pos_y/vid_conheight));
4154
4155         cvar_set("con_chatwidth", ftos(mySize_x/vid_conwidth));
4156         cvar_set("con_chat", ftos(floor(mySize_y/cvar("con_chatsize") - 0.5)));
4157
4158         if(hud_configure)
4159         {
4160                 float chatsize;
4161                 chatsize = cvar("con_chatsize");
4162                 cvar_set("con_chatrect_x", "9001"); // over 9000, we'll fake it instead for more control over alpha and such
4163                 float i, a;
4164                 for(i = 0; i < cvar("con_chat"); ++i)
4165                 {
4166                         if(i == cvar("con_chat") - 1)
4167                                 a = HUD_Panel_GetFgAlpha(id);
4168                         else
4169                                 a = HUD_Panel_GetFgAlpha(id) * floor(((i + 1) * 7 + cvar("con_chattime"))/45);
4170                         drawcolorcodedstring(pos + eY * i * chatsize, textShortenToWidth("^3Player^7: This is the chat area.", mySize_x, '1 1 0' * chatsize, stringwidth_colors), '1 1 0' * chatsize, a, DRAWFLAG_NORMAL);
4171                 }
4172         }
4173 }
4174
4175 // Engine info panel (#13)
4176 //
4177 float prevfps;
4178 float prevfps_time;
4179 float framecounter;
4180
4181 float frametimeavg;
4182 float frametimeavg1; // 1 frame ago
4183 float frametimeavg2; // 2 frames ago
4184 void HUD_EngineInfo(void)
4185 {
4186         float id = HUD_PANEL_ENGINEINFO;
4187         vector pos, mySize;
4188         pos = HUD_Panel_GetPos(id);
4189         mySize = HUD_Panel_GetSize(id);
4190
4191         HUD_Panel_DrawBg(id, pos, mySize, 0);
4192         float padding;
4193         padding = HUD_Panel_GetPadding(id);
4194         if(padding)
4195         {
4196                 pos += '1 1 0' * padding;
4197                 mySize -= '2 2 0' * padding;
4198         }
4199
4200         if(cvar("hud_engineinfo_framecounter_exponentialmovingaverage"))
4201         {
4202                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + frametime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
4203                 frametimeavg2 = frametimeavg1;
4204                 frametimeavg1 = frametimeavg;
4205                 
4206                 float weight;
4207                 weight = cvar("hud_engineinfo_framecounter_exponentialmovingaverage_new_weight");
4208                 if(frametime > 0.0001) // filter out insane values which sometimes seem to occur and throw off the average? If you are getting 10,000 fps or more, then you don't need a framerate counter.
4209                 {
4210                         if(fabs(prevfps - (1/frametimeavg)) > prevfps * cvar("hud_engineinfo_framecounter_exponentialmovingaverage_instantupdate_change_threshold")) // if there was a big jump in fps, just force prevfps at current (1/frametime) to make big updates instant
4211                                 prevfps = (1/frametime);
4212                         prevfps = (1 - weight) * prevfps + weight * (1/frametimeavg); // framecounter just used so there's no need for a new variable, think of it as "frametime average"
4213                 }
4214         }
4215         else
4216         {
4217                 framecounter += 1;
4218                 if(time - prevfps_time > cvar("hud_engineinfo_framecounter_time"))
4219                 {
4220                         prevfps = framecounter/cvar("hud_engineinfo_framecounter_time");
4221                         framecounter = 0;
4222                         prevfps_time = time;
4223                 }
4224         }
4225
4226         vector color;
4227         color = HUD_Get_Num_Color (prevfps, 100);
4228         drawfont = hud_bigfont;
4229         drawstring_aspect(pos, strcat("FPS: ", ftos_decimals(prevfps, cvar("hud_engineinfo_framecounter_decimals"))), mySize, mySize_y, color, HUD_Panel_GetFgAlpha(id), DRAWFLAG_NORMAL);
4230         drawfont = hud_font;
4231 }
4232 /*
4233 ==================
4234 Main HUD system
4235 ==================
4236 */
4237
4238 void HUD_ShowSpeed(void)
4239 {
4240         vector numsize;
4241         float pos, conversion_factor;
4242         string speed, zspeed, unit;
4243
4244         switch(cvar("cl_showspeed_unit"))
4245         {
4246                 default:
4247                 case 0:
4248                         unit = "";
4249                         conversion_factor = 1.0;
4250                         break;
4251                 case 1:
4252                         unit = " qu/s";
4253                         conversion_factor = 1.0;
4254                         break;
4255                 case 2:
4256                         unit = " m/s";
4257                         conversion_factor = 0.0254;
4258                         break;
4259                 case 3:
4260                         unit = " km/h";
4261                         conversion_factor = 0.0254 * 3.6;
4262                         break;
4263                 case 4:
4264                         unit = " mph";
4265                         conversion_factor = 0.0254 * 3.6 * 0.6213711922;
4266                         break;
4267                 case 5:
4268                         unit = " knots";
4269                         conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
4270                         break;
4271         }
4272
4273         speed = strcat(ftos(floor( vlen(pmove_vel - pmove_vel_z * '0 0 1') * conversion_factor + 0.5 )), unit);
4274
4275         numsize_x = numsize_y = cvar("cl_showspeed_size");
4276         pos = (vid_conheight - numsize_y) * cvar("cl_showspeed_position");
4277
4278         drawfont = hud_bigfont;
4279         drawstringcenter(eX + pos * eY, speed, numsize, '1 1 1', hud_fg_alpha, DRAWFLAG_NORMAL);
4280
4281         if (cvar("cl_showspeed_z") == 1) {
4282                 zspeed = strcat(ftos(fabs(floor( pmove_vel_z * conversion_factor + 0.5 ))), unit);
4283                 drawstringcenter(eX + pos * eY + numsize_y * eY, zspeed, numsize * 0.5, '1 1 1', hud_fg_alpha, DRAWFLAG_NORMAL);
4284         }
4285
4286         drawfont = hud_font;
4287 }
4288
4289 vector acc_prevspeed;
4290 float acc_prevtime;
4291 float acc_avg;
4292
4293 void HUD_ShowAcceleration(void)
4294 {
4295         float acceleration, sz, scale, alpha, f;
4296         vector pos, top, rgb;
4297         top_x = vid_conwidth/2;
4298         top_y = 0;
4299
4300         f = time - acc_prevtime;
4301         if(cvar("cl_showacceleration_z"))
4302                 acceleration = (vlen(pmove_vel) - vlen(acc_prevspeed)) * (1 / f);
4303         else
4304                 acceleration = (vlen(pmove_vel - '0 0 1' * pmove_vel_z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed_z)) * (1 / f);
4305         acc_prevspeed = pmove_vel;
4306         acc_prevtime = time;
4307
4308         f = bound(0, f * 10, 1);
4309         acc_avg = acc_avg * (1 - f) + acceleration * f;
4310         acceleration = acc_avg / getstatf(STAT_MOVEVARS_MAXSPEED);
4311
4312         pos = top - sz/2 * eY + (cvar("cl_showacceleration_position") * vid_conheight) * eY;
4313
4314         sz = cvar("cl_showacceleration_size");
4315         scale = cvar("cl_showacceleration_scale");
4316         alpha = cvar("cl_showacceleration_alpha");
4317         if (cvar("cl_showacceleration_color_custom"))
4318                 rgb = stov(cvar_string("cl_showacceleration_color"));
4319         else {
4320                 rgb = '1 1 1';
4321                 if (acceleration < 0) {
4322                         rgb = '1 .5 .5' - '0 .5 .5' * bound(0, -acceleration * 0.2, 1);
4323                 } else if (acceleration > 0) {
4324                         rgb = '.5 1 .5' - '.5 0 .5' * bound(0, +acceleration * 0.2, 1);
4325                 }
4326         }
4327
4328         if (acceleration > 0)
4329                 HUD_Panel_DrawProgressBar(pos, 0, acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_fg_alpha, DRAWFLAG_NORMAL);
4330         else if (acceleration < 0)
4331                 HUD_Panel_DrawProgressBar(pos + acceleration * scale * '40 0 0', 0, -acceleration * scale * '40 0 0' + sz * eY, rgb, alpha * hud_fg_alpha, DRAWFLAG_NORMAL);
4332 }
4333
4334 void HUD_Reset (void)
4335 {
4336         // reset gametype specific icons
4337         if(gametype == GAME_KEYHUNT)
4338                 HUD_Mod_KH_Reset();
4339         else if(gametype == GAME_CTF)
4340                 HUD_Mod_CTF_Reset();
4341 }
4342
4343 void HUD_Main (void)
4344 {
4345         if(disable_menu_alphacheck == 1)
4346                 menu_fade_alpha = 1;
4347         else
4348                 menu_fade_alpha = (1 - cvar("_menu_alpha"));
4349         hud_fg_alpha = cvar("hud_fg_alpha");
4350
4351         hud_border_thickness = bound(0, cvar("hud_border_thickness"), 5);
4352         hud_accuracy_border_thickness = bound(0, cvar_or("hud_accuracy_border_thickness", 1), 5);
4353         hud_color_bg_team = cvar("hud_color_bg_team");
4354
4355         hud_fontsize = HUD_GetFontsize("hud_fontsize");
4356         hud_fontsize_spec = HUD_GetFontsize("hud_fontsize_spec");
4357
4358         hud_configure = cvar("_hud_configure");
4359
4360         // Drawing stuff
4361
4362         // HUD configure visible grid
4363         if(hud_configure && cvar("hud_configure_grid") && cvar("hud_configure_grid_alpha"))
4364         {
4365                 float i;
4366                 // x-axis
4367                 for(i = 0; i < 1/bound(0.005, cvar("hud_configure_grid_x"), 0.2); ++i)
4368                 {
4369                         drawfill(eX * i * vid_conwidth * bound(0.005, cvar("hud_configure_grid_x"), 0.2), eX + eY * vid_conheight, '0.5 0.5 0.5', cvar("hud_configure_grid_alpha"), DRAWFLAG_NORMAL);
4370                 }
4371                 // y-axis
4372                 for(i = 0; i < 1/bound(0.005, cvar("hud_configure_grid_y"), 0.2); ++i)
4373                 {
4374                         drawfill(eY * i * vid_conheight * bound(0.005, cvar("hud_configure_grid_y"), 0.2), eY + eX * vid_conwidth, '0.5 0.5 0.5', cvar("hud_configure_grid_alpha"), DRAWFLAG_NORMAL);
4375                 }
4376         }
4377
4378         if(cvar_string("hud_dock") != "" && cvar_string("hud_dock") != "0")
4379                 drawpic('0 0 0', strcat("gfx/hud/", cvar_string("hud_skin"), "/", cvar_string("hud_dock")), eX * vid_conwidth + eY * vid_conheight, HUD_Panel_Dock_GetColor(), cvar("hud_dock_alpha") * menu_fade_alpha, DRAWFLAG_NORMAL); // no aspect ratio forcing on dock...
4380
4381         if(HUD_Panel_CheckActive(HUD_PANEL_RADAR) || hud_configure)
4382                 if(cvar_string("hud_radar") != "0" && (cvar("hud_radar") == 2 || teamplay))
4383                         HUD_Radar();
4384         if(HUD_Panel_CheckActive(HUD_PANEL_WEAPONICONS) || hud_configure)
4385                 HUD_WeaponIcons();
4386         if(HUD_Panel_CheckActive(HUD_PANEL_INVENTORY) || hud_configure)
4387                 HUD_Inventory();
4388         if(HUD_Panel_CheckActive(HUD_PANEL_POWERUPS) || hud_configure)
4389                 HUD_Powerups();
4390         if(HUD_Panel_CheckActive(HUD_PANEL_HEALTHARMOR) || hud_configure)
4391                 HUD_HealthArmor();
4392         if(HUD_Panel_CheckActive(HUD_PANEL_NOTIFY) || hud_configure)
4393                 HUD_Notify();
4394         if(HUD_Panel_CheckActive(HUD_PANEL_TIMER) || hud_configure)
4395                 HUD_Timer();
4396         if(HUD_Panel_CheckActive(HUD_PANEL_SCORE) || hud_configure)
4397                 HUD_Score();
4398         if(HUD_Panel_CheckActive(HUD_PANEL_RACETIMER) || hud_configure)
4399                 if(gametype == GAME_RACE || gametype == GAME_CTS || hud_configure)
4400                         HUD_RaceTimer();
4401         if(HUD_Panel_CheckActive(HUD_PANEL_VOTE) || hud_configure)
4402                 HUD_VoteWindow();
4403         if(HUD_Panel_CheckActive(HUD_PANEL_MODICONS) || hud_configure)
4404                 HUD_ModIcons();
4405         // TODO hud'ify
4406         if(HUD_Panel_CheckActive(HUD_PANEL_PRESSEDKEYS) || hud_configure)
4407                 if(spectatee_status > 0 || cvar("hud_pressedkeys") >= 2 || hud_configure)
4408                         HUD_DrawPressedKeys();
4409         if(HUD_Panel_CheckActive(HUD_PANEL_CHAT) || hud_configure)
4410                 HUD_Chat();
4411         else
4412                 cvar_set("con_chatrect", "0");
4413         if(HUD_Panel_CheckActive(HUD_PANEL_ENGINEINFO) || hud_configure)
4414                 HUD_EngineInfo();
4415
4416         // TODO hud_'ify these
4417         if (cvar("cl_showspeed"))
4418                 HUD_ShowSpeed();
4419         if (cvar("cl_showacceleration"))
4420                 HUD_ShowAcceleration();
4421
4422         if (hud_configure && spectatee_status) // try to join if we are in hud_configure mode, but still spectating (in order to get rid of motd and such)
4423                 localcmd("cmd selectteam auto; cmd join\n");
4424
4425         if (!hud_configure) // hud config mode disabled, enable normal alpha stuff again
4426                 disable_menu_alphacheck = 0;
4427 }