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