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