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