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