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