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