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