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