]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/shownames.qc
Fade out tags of dead players, split health/armor values into two separate sendflags...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / shownames.qc
1 // self.isactive = player is in range and coordinates/status (health and armor) are up to date
2 // self.origin = player origin TODO: should maybe move this so it's the origin of the shownames tag already in SSQC for culling?
3 // self.healthvalue
4 // self.armorvalue
5 // self.sameteam = player is on same team as local client
6 //
7 const float SHOWNAMES_FADESPEED = 4;
8 void Draw_ShowNames()
9 {
10     if(!autocvar_hud_shownames)
11         return;
12
13     if(self.sameteam || (!self.sameteam && autocvar_hud_shownames_enemies))
14     {
15         InterpolateOrigin_Do();
16
17         if(!self.sameteam)
18         {
19             traceline(self.origin, view_origin, 1, self);
20
21             /* WIP, why does trace_ent != self not work as intended here?
22             if(autocvar_hud_shownames_enemies != 2) // player has to point at enemy if so
23             {
24                 traceline(view_origin, view_origin + view_forward * MAX_SHOT_DISTANCE, MOVETYPE_FLY, world);
25                 print("trace_endpos: ", vtos(trace_endpos), " view_origin: ", vtos(view_origin), "\n");
26                 if(trace_ent != self)
27                     return;
28             }*/
29         }
30
31         if(!self.sameteam && trace_endpos != view_origin) // out of view, fade out
32             self.alpha = max(0, self.alpha - SHOWNAMES_FADESPEED * frametime);
33         else if(!self.healthvalue) // dead player, fade out slowly
34             self.alpha = max(0, self.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
35         else // fade in
36             self.alpha = min(1, self.alpha + SHOWNAMES_FADESPEED * frametime);
37
38         if(!self.alpha)
39             return;
40
41         float dist;
42         dist = vlen(self.origin - view_origin);
43
44         float a;
45         a = autocvar_hud_shownames_alpha;
46         if(self.alpha)
47             a *= self.alpha;
48         if(autocvar_hud_shownames_maxdistance)
49         {
50             if(dist >= autocvar_hud_shownames_maxdistance)
51                 return;
52             a *= ((autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance) - max(0, dist - autocvar_hud_shownames_mindistance)) / (autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance);
53         }
54
55         float resize;
56         resize = 1;
57         if(autocvar_hud_shownames_resize) // limit resize so its never smaller than 0.5... gets unreadable
58             resize = 0.5 + 0.5 * ((autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance) - max(0, dist - autocvar_hud_shownames_mindistance)) / (autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance);
59
60         // draw the sprite image
61         vector o;
62         o = project_3d_to_2d(self.origin);
63
64         if not(o_z < 0 || o_x < 0 || o_y < 0 || o_x > vid_conwidth || o_y > vid_conheight)
65         {
66             o_z = 0;
67
68             vector myPos, mySize;
69             mySize = (eX * autocvar_hud_shownames_aspect + eY) * autocvar_hud_shownames_height;
70             myPos = o - '0.5 0 0' * mySize_x - '0 1 0' * mySize_y;
71
72             // size scaling
73             mySize_x *= resize;
74             mySize_y *= resize;
75
76             myPos_x += 0.5 * (mySize_x / resize - mySize_x);
77             myPos_y += (mySize_y / resize - mySize_y);
78
79             vector iconpos, iconsize; // these determine icon position/size, if any
80             vector namepos; // this is where the origin of the string
81             float namesize; // total area where we can draw the string
82
83             iconpos = myPos;
84
85             if(autocvar_hud_shownames_status && teamplay)
86             {
87                 if(self.sameteam)
88                 {
89                     iconsize = eX * 2 * mySize_y + eY * mySize_y;
90                     // "ghost" backgrounds
91                     drawpic_aspect_skin(iconpos, "health", '1 1 0' * iconsize_y, '0 0 0', a * 0.5, DRAWFLAG_NORMAL);
92                     drawpic_aspect_skin(iconpos + '0.5 0 0' * iconsize_x, "armor", '1 1 0' * iconsize_y, '0 0 0', a * 0.5, DRAWFLAG_NORMAL);
93
94                     if(self.healthvalue > 0)
95                     {
96                         drawsetcliparea(0, myPos_y + iconsize_y - iconsize_y * min(1, self.healthvalue/autocvar_hud_panel_healtharmor_maxhealth), vid_conwidth, myPos_y + iconsize_y);
97                         drawpic_aspect_skin(iconpos, "health", '1 1 0' * iconsize_y, '1 1 1', a, DRAWFLAG_NORMAL);
98                     }
99
100                     if(self.armorvalue > 0)
101                     {
102                         drawsetcliparea(0, myPos_y + iconsize_y - iconsize_y * min(1, self.armorvalue/autocvar_hud_panel_healtharmor_maxarmor), vid_conwidth, myPos_y + iconsize_y);
103                         drawpic_aspect_skin(iconpos + '0.5 0 0' * iconsize_x, "armor", '1 1 0' * iconsize_y, '1 1 1', a, DRAWFLAG_NORMAL);
104                     }
105                     drawresetcliparea();
106                 }
107                 else if(autocvar_hud_shownames_status == 2)
108                 {
109                     iconsize = eX * 2 * mySize_y + eY * mySize_y;
110                     drawpic_aspect_skin(iconpos, "health_unknown", '1 1 0' * iconsize_y, '0 0 0', a, DRAWFLAG_NORMAL);
111                     drawpic_aspect_skin(iconpos + '0.5 0 0' * iconsize_x, "armor_unknown", '1 1 0' * iconsize_y, '0 0 0', a, DRAWFLAG_NORMAL);
112                 }
113             }
114
115             namepos = myPos + eX * 2 * iconsize_y + eY * 0.5 * resize * (autocvar_hud_shownames_height - autocvar_hud_shownames_fontsize);
116             namesize = mySize_x - 2 * iconsize_y;
117
118             string s;
119             s = GetPlayerName(self.the_entnum-1);
120             if((autocvar_hud_shownames_decolorize == 1 && teamplay) || autocvar_hud_shownames_decolorize == 2)
121                 s = playername(s, GetPlayerColor(self.the_entnum-1));
122
123             drawfontscale = '1 1 0' * resize;
124             s = textShortenToWidth(s, namesize, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
125
126             float width;
127             width = stringwidth(s, TRUE, '1 1 0' * autocvar_hud_shownames_fontsize);
128
129             if (width != namesize)
130                 namepos_x += (namesize - width) / 2;
131             drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
132             drawfontscale = '1 1 0';
133         }
134     }
135 }