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