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