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