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