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