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