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