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