1 #include "shownames.qh"
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>
13 // this.isactive = player is in range and coordinates/status (health and armor) are up to date
14 // this.origin = player origin
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
22 LinkedList shownames_ent;
23 STATIC_INIT(shownames_ent)
25 shownames_ent = LL_NEW();
26 for (int i = 0; i < maxclients; ++i)
28 entity e = new_pure(shownames_tag);
30 LL_PUSH(shownames_ent, e);
34 const float SHOWNAMES_FADESPEED = 4;
35 const float SHOWNAMES_FADEDELAY = 0.4;
36 void Draw_ShowNames(entity this)
38 if (this.sv_entnum == current_player + 1) // self or spectatee
40 if (!autocvar_chase_active)
43 if (!autocvar_hud_shownames_self
44 && !(spectatee_status > 0 && time <= spectatee_status_changed_time + 1))
50 if (!this.sameteam && !autocvar_hud_shownames_enemies) return;
52 if (!autocvar_hud_shownames_crosshairdistance && this.sameteam)
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));
63 vector o = project_3d_to_2d(this.origin + eZ * autocvar_hud_shownames_offset);
64 if (autocvar_hud_shownames_crosshairdistance)
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)
72 else if(!autocvar_hud_shownames_crosshairdistance_antioverlap)
76 if (overlap == -1 && autocvar_hud_shownames_antioverlap)
78 // fade tag out if another tag that is closer to you overlaps
80 LL_EACH(shownames_ent, it != this, {
81 entcs = entcs_receiver(i);
82 if (!(entcs && entcs.has_sv_origin))
84 vector eo = project_3d_to_2d(it.origin);
85 if (eo.z < 0 || eo.x < 0 || eo.y < 0 || eo.x > vid_conwidth || eo.y > vid_conheight) continue;
87 if (vdist((vec2(o) - eo), <, autocvar_hud_shownames_antioverlap_distance)
88 && vlen2(it.origin - view_origin) < vlen2(this.origin - view_origin))
95 bool onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
96 if (!this.fadedelay) this.fadedelay = time + SHOWNAMES_FADEDELAY;
97 if (this.csqcmodel_isdead) // dead player, fade out slowly
99 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
101 else if (!onscreen || (!this.sameteam && !hit)) // out of view, fade out
103 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
104 this.fadedelay = 0; // reset fade in delay, enemy has left the view
106 else if (overlap > 0) // tag overlap detected, fade out
108 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
110 else if (this.sameteam) // fade in for team mates
112 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
114 else if (time > this.fadedelay) // fade in for enemies
116 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
118 float a = autocvar_hud_shownames_alpha * this.alpha;
119 if (!this.sameteam || (this.sv_entnum == player_localentnum))
121 float f = entcs_GetAlpha(this.sv_entnum - 1);
126 if (MUTATOR_CALLHOOK(ShowNames_Draw, this, a)) return;
127 a = M_ARGV(1, float);
128 float dist = -1; // dist will be calculated only when really needed to avoid wasting a vlen call
129 if (autocvar_hud_shownames_maxdistance)
131 float max_dist = min(autocvar_hud_shownames_maxdistance, max_shot_distance);
132 if (vdist(this.origin - view_origin, >=, max_dist))
134 if (vdist(this.origin - view_origin, >=, autocvar_hud_shownames_mindistance))
136 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
138 dist = vlen(this.origin - view_origin);
139 a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
142 else if (vdist(this.origin - view_origin, >=, max_shot_distance))
146 if (autocvar_hud_shownames_resize) // limit resize so its never smaller than 0.5... gets unreadable
148 if (vdist(this.origin - view_origin, >=, autocvar_hud_shownames_mindistance))
150 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
152 dist = vlen(this.origin - view_origin);
153 resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
156 // draw the sprite image
160 vector mySize = (vec2(autocvar_hud_shownames_aspect, 1)) * autocvar_hud_shownames_fontsize;
161 vector myPos = o - vec2(0.5 * mySize.x, mySize.y);
165 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
166 myPos.y += (mySize.y / resize - mySize.y);
167 // this is where the origin of the string
168 float namewidth = mySize.x;
169 if (autocvar_hud_shownames_status && this.sameteam && !this.csqcmodel_isdead)
171 vector pos = myPos + eY * autocvar_hud_shownames_fontsize * resize;
172 vector sz = vec2(0.5 * mySize.x, resize * autocvar_hud_shownames_statusbar_height);
173 if (autocvar_hud_shownames_statusbar_highlight)
174 drawfill(pos + eX * 0.25 * mySize.x, sz, '0.7 0.7 0.7', a / 2, DRAWFLAG_NORMAL);
175 if (this.healthvalue > 0)
177 HUD_Panel_DrawProgressBar(pos, sz, "nametag_statusbar",
178 this.healthvalue / autocvar_hud_panel_healtharmor_maxhealth, false, 1, '1 0 0', a,
181 if (GetResource(this, RES_ARMOR) > 0)
183 HUD_Panel_DrawProgressBar(pos + eX * 0.5 * mySize.x, sz, "nametag_statusbar",
184 GetResource(this, RES_ARMOR) / autocvar_hud_panel_healtharmor_maxarmor, false, 0, '0 1 0', a,
188 string s = entcs_GetName(this.sv_entnum - 1);
189 if ((autocvar_hud_shownames_decolorize == 1 && teamplay) || autocvar_hud_shownames_decolorize == 2)
190 s = playername(s, entcs_GetTeam(this.sv_entnum - 1), true);
191 drawfontscale = '1 1 0' * resize;
192 s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
193 float width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
194 myPos.x = o.x - (width * resize) / 2;
195 drawcolorcodedstring(myPos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
196 drawfontscale = '1 1 0';
200 void Draw_ShowNames_All()
202 if (!autocvar_hud_shownames) return;
203 LL_EACH(shownames_ent, true, {
204 entity entcs = entcs_receiver(i);
211 assert(getthink(entcs), eprint(entcs));
212 getthink(entcs)(entcs);
213 if (!entcs.has_origin) continue;
214 if (entcs.m_entcs_private)
216 it.healthvalue = entcs.healthvalue;
217 SetResourceExplicit(it, RES_ARMOR, GetResource(entcs, RES_ARMOR));
223 SetResourceExplicit(it, RES_ARMOR, 0);
226 bool dead = entcs_IsDead(i) || entcs_IsSpectating(i);
227 if ((!it.csqcmodel_isdead || it.alpha > 0) && entcs.origin != it.origin)
228 setorigin(it, entcs.origin);
229 it.csqcmodel_isdead = dead;