]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/shownames.qc
if two name tags would overlap, hide the one that is further away from you
[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         vector o, eo;
32         o = project_3d_to_2d(self.origin);
33
34         // fade tag out if another tag that is closer to you overlaps
35         entity e;
36         float overlap;
37         for(e = world; (e = find(e, classname, "shownames_tag")); )
38         {
39             if(e == self)
40                 continue;
41             eo = project_3d_to_2d(e.origin);
42             if not(eo_z < 0 || eo_x < 0 || eo_y < 0 || eo_x > vid_conwidth || eo_y > vid_conheight)
43             {
44                 eo_z = 0;
45                 if(vlen((eX * o_x + eY * o_y) - eo) < autocvar_hud_shownames_antioverlap_distance && vlen(self.origin - view_origin) > vlen(e.origin - view_origin))
46                 {
47                     overlap = TRUE;
48                     break;
49                 }
50             }
51         }
52
53         if(!self.sameteam && trace_endpos != view_origin) // out of view, fade out
54             self.alpha = max(0, self.alpha - SHOWNAMES_FADESPEED * frametime);
55         else if(!self.healthvalue) // dead player, fade out slowly
56             self.alpha = max(0, self.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
57         else if(overlap) // tag overlap detected, fade out
58             self.alpha = max(0, self.alpha - SHOWNAMES_FADESPEED * frametime);
59         else // fade in
60             self.alpha = min(1, self.alpha + SHOWNAMES_FADESPEED * frametime);
61
62         if(!self.alpha)
63             return;
64
65         float dist;
66         dist = vlen(self.origin - view_origin);
67
68         float a;
69         a = autocvar_hud_shownames_alpha;
70         a *= self.alpha;
71         if(autocvar_hud_shownames_maxdistance)
72         {
73             if(dist >= autocvar_hud_shownames_maxdistance)
74                 return;
75             a *= ((autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance) - max(0, dist - autocvar_hud_shownames_mindistance)) / (autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance);
76         }
77
78         if(!a)
79             return;
80
81         float resize;
82         resize = 1;
83         if(autocvar_hud_shownames_resize) // limit resize so its never smaller than 0.5... gets unreadable
84             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);
85
86         // draw the sprite image
87         if not(o_z < 0 || o_x < 0 || o_y < 0 || o_x > vid_conwidth || o_y > vid_conheight)
88         {
89             o_z = 0;
90
91             vector myPos, mySize;
92             mySize = (eX * autocvar_hud_shownames_aspect + eY) * autocvar_hud_shownames_height;
93             myPos = o - '0.5 0 0' * mySize_x - '0 1 0' * mySize_y;
94
95             // size scaling
96             mySize_x *= resize;
97             mySize_y *= resize;
98
99             myPos_x += 0.5 * (mySize_x / resize - mySize_x);
100             myPos_y += (mySize_y / resize - mySize_y);
101
102             vector iconpos, iconsize; // these determine icon position/size, if any
103             vector namepos; // this is where the origin of the string
104             float namesize; // total area where we can draw the string
105
106             iconpos = myPos;
107
108             if(autocvar_hud_shownames_status && teamplay)
109             {
110                 if(self.sameteam)
111                 {
112                     iconsize = eX * 2 * mySize_y + eY * mySize_y;
113                     // "ghost" backgrounds
114                     drawpic_aspect_skin(iconpos, "health", '1 1 0' * iconsize_y, '0 0 0', a * 0.5, DRAWFLAG_NORMAL);
115                     drawpic_aspect_skin(iconpos + '0.5 0 0' * iconsize_x, "armor", '1 1 0' * iconsize_y, '0 0 0', a * 0.5, DRAWFLAG_NORMAL);
116
117                     if(self.healthvalue > 0)
118                     {
119                         drawsetcliparea(0, myPos_y + iconsize_y - iconsize_y * min(1, self.healthvalue/autocvar_hud_panel_healtharmor_maxhealth), vid_conwidth, myPos_y + iconsize_y);
120                         drawpic_aspect_skin(iconpos, "health", '1 1 0' * iconsize_y, '1 1 1', a, DRAWFLAG_NORMAL);
121                     }
122
123                     if(self.armorvalue > 0)
124                     {
125                         drawsetcliparea(0, myPos_y + iconsize_y - iconsize_y * min(1, self.armorvalue/autocvar_hud_panel_healtharmor_maxarmor), vid_conwidth, myPos_y + iconsize_y);
126                         drawpic_aspect_skin(iconpos + '0.5 0 0' * iconsize_x, "armor", '1 1 0' * iconsize_y, '1 1 1', a, DRAWFLAG_NORMAL);
127                     }
128                     drawresetcliparea();
129                 }
130                 else if(autocvar_hud_shownames_status == 2)
131                 {
132                     iconsize = eX * 2 * mySize_y + eY * mySize_y;
133                     drawpic_aspect_skin(iconpos, "health_unknown", '1 1 0' * iconsize_y, '0 0 0', a, DRAWFLAG_NORMAL);
134                     drawpic_aspect_skin(iconpos + '0.5 0 0' * iconsize_x, "armor_unknown", '1 1 0' * iconsize_y, '0 0 0', a, DRAWFLAG_NORMAL);
135                 }
136             }
137
138             namepos = myPos + eX * 2 * iconsize_y + eY * 0.5 * resize * (autocvar_hud_shownames_height - autocvar_hud_shownames_fontsize);
139             namesize = mySize_x - 2 * iconsize_y;
140
141             string s;
142             s = GetPlayerName(self.the_entnum-1);
143             if((autocvar_hud_shownames_decolorize == 1 && teamplay) || autocvar_hud_shownames_decolorize == 2)
144                 s = playername(s, GetPlayerColor(self.the_entnum-1));
145
146             drawfontscale = '1 1 0' * resize;
147             s = textShortenToWidth(s, namesize, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
148
149             float width;
150             width = stringwidth(s, TRUE, '1 1 0' * autocvar_hud_shownames_fontsize);
151
152             if (width != namesize)
153                 namepos_x += (namesize - width) / 2;
154             drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
155             drawfontscale = '1 1 0';
156         }
157     }
158 }