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