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