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