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