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