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