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