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