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