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