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