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