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