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