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