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