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