]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/shownames.qc
Fix small bug with names above players sometimes stuck on a intermediate alpha value...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / shownames.qc
1 #include "shownames.qh"
2
3 #include <client/draw.qh>
4 #include <client/hud/_mod.qh>
5 #include <client/resources.qh>
6 #include <client/view.qh>
7 #include <common/constants.qh>
8 #include <common/ent_cs.qh>
9 #include <common/net_linked.qh>
10 #include <common/teams.qh>
11 #include <lib/csqcmodel/cl_model.qh>
12
13 // this.isactive = player is in range and coordinates/status (health and armor) are up to date
14 // this.origin = player origin
15 // this.healthvalue
16 // this.armorvalue
17 // this.sameteam = player is on same team as local client
18 // this.fadedelay = time to wait before name tag starts fading in for enemies
19 // this.pointtime = last time you pointed at this player
20 // this.csqcmodel_isdead = value of csqcmodel_isdead to know when the player is dead or not
21
22 LinkedList shownames_ent;
23 STATIC_INIT(shownames_ent)
24 {
25         shownames_ent = LL_NEW();
26         for (int i = 0; i < maxclients; ++i)
27         {
28                 entity e = new_pure(shownames_tag);
29                 e.sv_entnum = i + 1;
30                 LL_PUSH(shownames_ent, e);
31         }
32 }
33
34 const float SHOWNAMES_FADESPEED = 4;
35 const float SHOWNAMES_FADEDELAY = 0.4;
36 void Draw_ShowNames(entity this)
37 {
38         if (this.sv_entnum == current_player + 1) // self or spectatee
39         {
40                 if (!autocvar_chase_active)
41                         return;
42
43                 if (!autocvar_hud_shownames_self
44                         && !(spectatee_status > 0 && time <= spectatee_status_changed_time + 1))
45                 {
46                         return;
47                 }
48         }
49
50         if (!this.sameteam && !autocvar_hud_shownames_enemies) return;
51         bool hit;
52         if (!autocvar_hud_shownames_crosshairdistance && this.sameteam)
53         {
54                 hit = true;
55         }
56         else
57         {
58                 traceline(view_origin, this.origin, MOVE_NORMAL, this);
59                 hit = !(trace_fraction < 1 && (trace_networkentity != this.sv_entnum && trace_ent.entnum != this.sv_entnum));
60         }
61         // handle tag fading
62         int overlap = -1;
63         vector o = project_3d_to_2d(this.origin + eZ * autocvar_hud_shownames_offset);
64         if (autocvar_hud_shownames_crosshairdistance)
65         {
66                 float d = autocvar_hud_shownames_crosshairdistance;
67                 float w = o.x - vid_conwidth / 2;
68                 float h = o.y - vid_conheight / 2;
69                 if (d * d > w * w + h * h) this.pointtime = time;
70                 if (this.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time)
71                         overlap = 1;
72                 else if(!autocvar_hud_shownames_crosshairdistance_antioverlap)
73                         overlap = 0;
74         }
75
76         // o.z is < 0 when o is behind me
77         #define OFF_SCREEN(o) (o.z < 0 || o.x < 0 || o.y < 0 || o.x > vid_conwidth || o.y > vid_conheight)
78         if (overlap == -1 && autocvar_hud_shownames_antioverlap)
79         {
80                 // fade tag out if another tag that is closer to you overlaps
81                 entity entcs = NULL;
82                 LL_EACH(shownames_ent, it != this, {
83                         entcs = entcs_receiver(i);
84                         if (!(entcs && entcs.has_sv_origin))
85                                 continue;
86                         vector eo = project_3d_to_2d(it.origin);
87                         if (OFF_SCREEN(eo)) continue;
88                         eo.z = 0;
89                         if (vdist((vec2(o) - eo), <, autocvar_hud_shownames_antioverlap_distance)
90                                 && vlen2(it.origin - view_origin) < vlen2(this.origin - view_origin))
91                         {
92                                 overlap = 1;
93                                 break;
94                         }
95                 });
96         }
97         if (!this.fadedelay) this.fadedelay = time + SHOWNAMES_FADEDELAY;
98         if (this.csqcmodel_isdead) // dead player, fade out slowly
99         {
100                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
101         }
102         else if (!this.sameteam && !hit) // view blocked, fade out
103         {
104                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
105                 this.fadedelay = 0; // reset fade in delay, enemy has left the view
106         }
107         else if (OFF_SCREEN(o) || overlap > 0) // out of view or tag overlap detected, fade out
108         {
109                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
110         }
111         else if (this.sameteam)  // fade in for team mates
112         {
113                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
114         }
115         else if (time > this.fadedelay || this.alpha > 0)  // fade in for enemies
116         {
117                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
118         }
119         float a = autocvar_hud_shownames_alpha * this.alpha;
120         if (!this.sameteam || (this.sv_entnum == player_localentnum))
121         {
122                 float f = entcs_GetAlpha(this.sv_entnum - 1);
123                 if (f == 0) f = 1;
124                 if (f < 0) f = 0;
125                 a *= f;
126         }
127         if (MUTATOR_CALLHOOK(ShowNames_Draw, this, a)) return;
128         a = M_ARGV(1, float);
129         float dist = -1; // dist will be calculated only when really needed to avoid wasting a vlen call
130         if (autocvar_hud_shownames_maxdistance)
131         {
132                 float max_dist = min(autocvar_hud_shownames_maxdistance, max_shot_distance);
133                 if (vdist(this.origin - view_origin, >=, max_dist))
134                         return;
135                 if (vdist(this.origin - view_origin, >=, autocvar_hud_shownames_mindistance))
136                 {
137                         float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
138                         if (dist == -1)
139                                 dist = vlen(this.origin - view_origin);
140                         a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
141                 }
142         }
143         else if (vdist(this.origin - view_origin, >=, max_shot_distance))
144                 return;
145         if (!a) return;
146         float resize = 1;
147         if (autocvar_hud_shownames_resize)  // limit resize so its never smaller than 0.5... gets unreadable
148         {
149                 if (vdist(this.origin - view_origin, >=, autocvar_hud_shownames_mindistance))
150                 {
151                         float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
152                         if (dist == -1)
153                                 dist = vlen(this.origin - view_origin);
154                         resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
155                 }
156         }
157         // draw the sprite image
158         if (o.z >= 0)
159         {
160                 o.z = 0;
161                 vector mySize = (vec2(autocvar_hud_shownames_aspect, 1)) * autocvar_hud_shownames_fontsize;
162                 vector myPos = o - vec2(0.5 * mySize.x, mySize.y);
163                 // size scaling
164                 mySize.x *= resize;
165                 mySize.y *= resize;
166                 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
167                 myPos.y += (mySize.y / resize - mySize.y);
168                 // this is where the origin of the string
169                 float namewidth = mySize.x;
170                 if (autocvar_hud_shownames_status && this.sameteam && !this.csqcmodel_isdead)
171                 {
172                         vector pos = myPos + eY * autocvar_hud_shownames_fontsize * resize;
173                         vector sz = vec2(0.5 * mySize.x, resize * autocvar_hud_shownames_statusbar_height);
174                         if (autocvar_hud_shownames_statusbar_highlight)
175                                 drawfill(pos + eX * 0.25 * mySize.x, sz, '0.7 0.7 0.7', a / 2, DRAWFLAG_NORMAL);
176                         if (this.healthvalue > 0)
177                         {
178                                 HUD_Panel_DrawProgressBar(pos, sz, "nametag_statusbar",
179                                         this.healthvalue / autocvar_hud_panel_healtharmor_maxhealth, false, 1, '1 0 0', a,
180                                         DRAWFLAG_NORMAL);
181                         }
182                         if (GetResource(this, RES_ARMOR) > 0)
183                         {
184                                 HUD_Panel_DrawProgressBar(pos + eX * 0.5 * mySize.x, sz, "nametag_statusbar",
185                                         GetResource(this, RES_ARMOR) / autocvar_hud_panel_healtharmor_maxarmor, false, 0, '0 1 0', a,
186                                         DRAWFLAG_NORMAL);
187                         }
188                 }
189                 string s = entcs_GetName(this.sv_entnum - 1);
190                 if ((autocvar_hud_shownames_decolorize == 1 && teamplay) || autocvar_hud_shownames_decolorize == 2)
191                         s = playername(s, entcs_GetTeam(this.sv_entnum - 1), true);
192                 drawfontscale = '1 1 0' * resize;
193                 s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
194                 float width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
195                 myPos.x = o.x - (width * resize) / 2;
196                 drawcolorcodedstring(myPos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
197                 drawfontscale = '1 1 0';
198         }
199 }
200
201 void Draw_ShowNames_All()
202 {
203         if (!autocvar_hud_shownames) return;
204         LL_EACH(shownames_ent, true, {
205                 entity entcs = entcs_receiver(i);
206                 if (!entcs)
207                 {
208                         make_pure(it);
209                         continue;
210                 }
211                 make_impure(it);
212                 assert(getthink(entcs), eprint(entcs));
213                 getthink(entcs)(entcs);
214                 if (!entcs.has_origin) continue;
215                 if (entcs.m_entcs_private)
216                 {
217                         it.healthvalue = entcs.healthvalue;
218                         SetResourceExplicit(it, RES_ARMOR, GetResource(entcs, RES_ARMOR));
219                         it.sameteam = true;
220                 }
221                 else
222                 {
223                         it.healthvalue = 0;
224                         SetResourceExplicit(it, RES_ARMOR, 0);
225                         it.sameteam = false;
226                 }
227                 bool dead = entcs_IsDead(i) || entcs_IsSpectating(i);
228                 if ((!it.csqcmodel_isdead || it.alpha > 0) && entcs.origin != it.origin)
229                         setorigin(it, entcs.origin);
230                 it.csqcmodel_isdead = dead;
231                 Draw_ShowNames(it);
232         });
233 }