1 #include "shownames.qh"
3 #include "autocvars.qh"
4 #include "miscfunctions.qh"
5 #include "resources.qh"
8 #include <common/ent_cs.qh>
9 #include <common/constants.qh>
10 #include <common/net_linked.qh>
11 #include <common/mapinfo.qh>
12 #include <common/teams.qh>
14 #include <lib/csqcmodel/cl_model.qh>
16 // this.isactive = player is in range and coordinates/status (health and armor) are up to date
17 // this.origin = player origin
20 // this.sameteam = player is on same team as local client
21 // this.fadedelay = time to wait before name tag starts fading in for enemies
22 // this.pointtime = last time you pointed at this player
23 // this.csqcmodel_isdead = value of csqcmodel_isdead to know when the player is dead or not
25 LinkedList shownames_ent;
26 STATIC_INIT(shownames_ent)
28 shownames_ent = LL_NEW();
29 for (int i = 0; i < maxclients; ++i)
31 entity e = new_pure(shownames_tag);
33 LL_PUSH(shownames_ent, e);
37 const float SHOWNAMES_FADESPEED = 4;
38 const float SHOWNAMES_FADEDELAY = 0.4;
39 void Draw_ShowNames(entity this)
41 if (this.sv_entnum == (current_player + 1)) // self or spectatee
42 if (!(autocvar_hud_shownames_self && autocvar_chase_active)) return;
43 if (!this.sameteam && !autocvar_hud_shownames_enemies) return;
45 if (!autocvar_hud_shownames_crosshairdistance && this.sameteam)
51 traceline(view_origin, this.origin, MOVE_NORMAL, this);
52 hit = !(trace_fraction < 1 && (trace_networkentity != this.sv_entnum && trace_ent.entnum != this.sv_entnum));
56 vector o = project_3d_to_2d(this.origin + eZ * autocvar_hud_shownames_offset);
57 if (autocvar_hud_shownames_crosshairdistance)
59 float d = autocvar_hud_shownames_crosshairdistance;
60 float w = o.x - vid_conwidth / 2;
61 float h = o.y - vid_conheight / 2;
62 if (d * d > w * w + h * h) this.pointtime = time;
63 if (this.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time)
65 else if(!autocvar_hud_shownames_crosshairdistance_antioverlap)
69 if (overlap == -1 && autocvar_hud_shownames_antioverlap)
71 // fade tag out if another tag that is closer to you overlaps
73 LL_EACH(shownames_ent, it != this, {
74 entcs = entcs_receiver(i);
75 if (!(entcs && entcs.has_sv_origin))
77 vector eo = project_3d_to_2d(it.origin);
78 if (eo.z < 0 || eo.x < 0 || eo.y < 0 || eo.x > vid_conwidth || eo.y > vid_conheight) continue;
80 if (vdist((vec2(o) - eo), <, autocvar_hud_shownames_antioverlap_distance)
81 && vlen2(it.origin - view_origin) < vlen2(this.origin - view_origin))
88 bool onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
89 if (!this.fadedelay) this.fadedelay = time + SHOWNAMES_FADEDELAY;
90 if (this.csqcmodel_isdead) // dead player, fade out slowly
92 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
94 else if (!onscreen || (!this.sameteam && !hit)) // out of view, fade out
96 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
97 this.fadedelay = 0; // reset fade in delay, enemy has left the view
99 else if (overlap > 0) // tag overlap detected, fade out
101 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
103 else if (this.sameteam) // fade in for team mates
105 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
107 else if (time > this.fadedelay) // fade in for enemies
109 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
111 float a = autocvar_hud_shownames_alpha * this.alpha;
112 // multiply by player alpha
113 if (!this.sameteam || (this.sv_entnum == player_localentnum))
115 float f = entcs_GetAlpha(this.sv_entnum - 1);
118 // FIXME: alpha is negative when dead, breaking death fade
119 if (!this.csqcmodel_isdead) a *= f;
121 if (a < ALPHA_MIN_VISIBLE && gametype != MAPINFO_TYPE_CTS) return;
122 if (vdist(this.origin - view_origin, >=, max_shot_distance)) return;
123 float dist = vlen(this.origin - view_origin);
124 if (autocvar_hud_shownames_maxdistance)
126 if (dist >= autocvar_hud_shownames_maxdistance) return;
127 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
128 a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
132 if (autocvar_hud_shownames_resize) // limit resize so its never smaller than 0.5... gets unreadable
134 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
135 resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
137 // draw the sprite image
141 vector mySize = (vec2(autocvar_hud_shownames_aspect, 1)) * autocvar_hud_shownames_fontsize;
142 vector myPos = o - vec2(0.5 * mySize.x, mySize.y);
146 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
147 myPos.y += (mySize.y / resize - mySize.y);
148 // this is where the origin of the string
149 vector namepos = myPos;
150 float namewidth = mySize.x;
151 if (autocvar_hud_shownames_status && this.sameteam)
153 vector pos = namepos + eY * autocvar_hud_shownames_fontsize * resize;
154 vector sz = vec2(0.5 * mySize.x, resize * autocvar_hud_shownames_statusbar_height);
155 if (this.healthvalue > 0)
157 HUD_Panel_DrawProgressBar(pos, sz, "nametag_statusbar",
158 this.healthvalue / autocvar_hud_panel_healtharmor_maxhealth, false, 1, '1 0 0', a,
161 if (GetResourceAmount(this, RESOURCE_ARMOR) > 0)
163 HUD_Panel_DrawProgressBar(pos + eX * 0.5 * mySize.x, sz, "nametag_statusbar",
164 GetResourceAmount(this, RESOURCE_ARMOR) / autocvar_hud_panel_healtharmor_maxarmor, false, 0, '0 1 0', a,
168 string s = entcs_GetName(this.sv_entnum - 1);
169 if ((autocvar_hud_shownames_decolorize == 1 && teamplay)
170 || autocvar_hud_shownames_decolorize == 2) s = playername(s, entcs_GetTeam(this.sv_entnum - 1));
171 drawfontscale = '1 1 0' * resize;
172 s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
173 float width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
174 if (width != namewidth) namepos.x += (namewidth - width) / 2;
175 drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
176 drawfontscale = '1 1 0';
180 void Draw_ShowNames_All()
182 if (!autocvar_hud_shownames) return;
183 LL_EACH(shownames_ent, true, {
184 entity entcs = entcs_receiver(i);
191 assert(getthink(entcs), eprint(entcs));
192 getthink(entcs)(entcs);
193 if (!entcs.has_origin) continue;
194 if (entcs.m_entcs_private)
196 it.healthvalue = entcs.healthvalue;
197 SetResourceAmountExplicit(it, RESOURCE_ARMOR, GetResourceAmount(entcs, RESOURCE_ARMOR));
203 SetResourceAmountExplicit(it, RESOURCE_ARMOR, 0);
206 bool dead = entcs_IsDead(i) || entcs_IsSpectating(i);
207 if (!it.csqcmodel_isdead) setorigin(it, entcs.origin);
208 it.csqcmodel_isdead = dead;