]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/shownames.qc
Shownames: optimize
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / shownames.qc
1 #include "shownames.qh"
2
3 #include "hud/all.qh"
4
5 #include "../common/constants.qh"
6 #include "../common/mapinfo.qh"
7 #include "../common/teams.qh"
8
9 #include "../lib/csqcmodel/cl_model.qh"
10
11 // self.isactive = player is in range and coordinates/status (health and armor) are up to date
12 // self.origin = player origin
13 // self.healthvalue
14 // self.armorvalue
15 // self.sameteam = player is on same team as local client
16 // self.fadedelay = time to wait before name tag starts fading in for enemies
17 // self.pointtime = last time you pointed at this player
18 // self.csqcmodel_isdead = value of csqcmodel_isdead to know when the player is dead or not
19
20 const float SHOWNAMES_FADESPEED = 4;
21 const float SHOWNAMES_FADEDELAY = 0.4;
22 void Draw_ShowNames(entity this)
23 {
24         if (this.sv_entnum == player_localentnum)  // self or spectatee
25                 if (!(autocvar_hud_shownames_self && autocvar_chase_active)) return;
26         if (!this.sameteam && !autocvar_hud_shownames_enemies) return;
27         bool hit;
28         if (!autocvar_hud_shownames_crosshairdistance && this.sameteam)
29         {
30                 hit = true;
31         }
32         else
33         {
34                 traceline(view_origin, this.origin, MOVE_NORMAL, this);
35                 hit = !(trace_fraction < 1 && (trace_networkentity != this.sv_entnum && trace_ent.entnum != this.sv_entnum));
36         }
37         // handle tag fading
38         bool overlap = false;
39         vector o = project_3d_to_2d(this.origin + eZ * autocvar_hud_shownames_offset);
40         float dist = vlen(this.origin - view_origin);
41         if (autocvar_hud_shownames_antioverlap)
42         {
43                 // fade tag out if another tag that is closer to you overlaps
44                 for (entity e = world; (e = find(e, classname, "shownames_tag")); )
45                 {
46                         if (e == this) continue;
47                         vector eo = project_3d_to_2d(e.origin);
48                         if (eo.z < 0 || eo.x < 0 || eo.y < 0 || eo.x > vid_conwidth || eo.y > vid_conheight) continue;
49                         eo.z = 0;
50                         if (vlen((eX * o.x + eY * o.y) - eo) < autocvar_hud_shownames_antioverlap_distance
51                             && dist > vlen(e.origin - view_origin))
52                         {
53                                 overlap = true;
54                                 break;
55                         }
56                 }
57         }
58         bool onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
59         float crosshairdistance = sqrt(pow(o.x - vid_conwidth / 2, 2) + pow(o.y - vid_conheight / 2, 2));
60         if (autocvar_hud_shownames_crosshairdistance)
61         {
62                 if (autocvar_hud_shownames_crosshairdistance > crosshairdistance) this.pointtime = time;
63                 if (this.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time) overlap = true;
64                 else overlap = (autocvar_hud_shownames_crosshairdistance_antioverlap ? overlap : false); // override what antioverlap says unless allowed by cvar.
65         }
66         if (!this.fadedelay) this.fadedelay = time + SHOWNAMES_FADEDELAY;
67         if (this.csqcmodel_isdead)                                                                   // dead player, fade out slowly
68         {
69                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
70         }
71         else if (!this.sameteam && (!onscreen || !hit)) // out of view, fade out
72         {
73                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
74                 this.fadedelay = 0;                         // reset fade in delay, enemy has left the view
75         }
76         else if (overlap)                               // tag overlap detected, fade out
77         {
78                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
79         }
80         else if (this.sameteam)  // fade in for team mates
81         {
82                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
83         }
84         else if (time > this.fadedelay)  // fade in for enemies
85         {
86                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
87         }
88         float a = autocvar_hud_shownames_alpha * this.alpha;
89         // multiply by player alpha
90         if (!this.sameteam || (this.sv_entnum == player_localentnum))
91         {
92                 float f = getplayeralpha(this.sv_entnum - 1);
93                 if (f == 0) f = 1;
94                 if (f < 0) f = 0;
95                 // FIXME: alpha is negative when dead, breaking death fade
96                 if (!this.csqcmodel_isdead) a *= f;
97         }
98         if (a < ALPHA_MIN_VISIBLE && gametype != MAPINFO_TYPE_CTS) return;
99         if (autocvar_hud_shownames_maxdistance)
100         {
101                 if (dist >= autocvar_hud_shownames_maxdistance) return;
102                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
103                 a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
104         }
105         if (!a) return;
106         float resize = 1;
107         if (autocvar_hud_shownames_resize)  // limit resize so its never smaller than 0.5... gets unreadable
108         {
109                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
110                 resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
111         }
112         // draw the sprite image
113         if (o.z >= 0)
114         {
115                 o.z = 0;
116                 vector mySize = (eX * autocvar_hud_shownames_aspect + eY) * autocvar_hud_shownames_fontsize;
117                 vector myPos = o - '0.5 0 0' * mySize.x - '0 1 0' * mySize.y;
118                 // size scaling
119                 mySize.x *= resize;
120                 mySize.y *= resize;
121                 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
122                 myPos.y += (mySize.y / resize - mySize.y);
123                 // this is where the origin of the string
124                 vector namepos = myPos;
125                 float namewidth = mySize.x;
126                 if (autocvar_hud_shownames_status && this.sameteam)
127                 {
128                         vector v = namepos + '0 1 0' * autocvar_hud_shownames_fontsize * resize;
129                         vector s = eX * 0.5 * mySize.x + eY * resize * autocvar_hud_shownames_statusbar_height;
130                         if (this.healthvalue > 0)
131                         {
132                                 HUD_Panel_DrawProgressBar(v, s, "nametag_statusbar",
133                                         this.healthvalue / autocvar_hud_panel_healtharmor_maxhealth, false, 1, '1 0 0', a,
134                                         DRAWFLAG_NORMAL);
135                         }
136                         if (this.armorvalue > 0)
137                         {
138                                 HUD_Panel_DrawProgressBar(v + eX * 0.5 * mySize.x, s, "nametag_statusbar",
139                                         this.armorvalue / autocvar_hud_panel_healtharmor_maxarmor, false, 0, '0 1 0', a,
140                                         DRAWFLAG_NORMAL);
141                         }
142                 }
143                 string s = GetPlayerName(this.sv_entnum - 1);
144                 if ((autocvar_hud_shownames_decolorize == 1 && teamplay)
145                     || autocvar_hud_shownames_decolorize == 2) s = playername(s, GetPlayerColor(this.sv_entnum - 1));
146                 drawfontscale = '1 1 0' * resize;
147                 s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
148                 float width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
149                 if (width != namewidth) namepos.x += (namewidth - width) / 2;
150                 drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
151                 drawfontscale = '1 1 0';
152         }
153 }
154
155 LinkedList shownames_ent;
156 STATIC_INIT(shownames_ent)
157 {
158         shownames_ent = LL_NEW();
159         for (int i = 0; i < maxclients; ++i)
160         {
161                 entity e = new(shownames_tag);
162                 e.sv_entnum = i + 1;
163                 LL_PUSH(shownames_ent, e);
164         }
165 }
166
167 void Draw_ShowNames_All()
168 {
169         if (!autocvar_hud_shownames) return;
170         LL_EACH(shownames_ent, true, LAMBDA(
171                 entity entcs = entcs_receiver[i];
172                 if (!entcs) continue;
173                 if (entcs.m_entcs_private)
174                 {
175                         it.healthvalue = entcs.healthvalue;
176                         it.armorvalue = entcs.armorvalue;
177                         it.sameteam = true;
178                 }
179                 else
180                 {
181                         it.healthvalue = 0;
182                         it.armorvalue = 0;
183                         it.sameteam = false;
184                 }
185                 bool dead = getplayerisdead(i);
186                 if (!it.csqcmodel_isdead && entcs.has_origin) setorigin(it, entcs.origin);
187                 it.csqcmodel_isdead = dead;
188                 Draw_ShowNames(it);
189         ));
190 }