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