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