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