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