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