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