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