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