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