1 #include "shownames.qh"
5 #include <common/ent_cs.qh>
6 #include <common/constants.qh>
7 #include <common/net_linked.qh>
8 #include <common/mapinfo.qh>
9 #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
39 if (!(autocvar_hud_shownames_self && autocvar_chase_active)) return;
40 if (!this.sameteam && !autocvar_hud_shownames_enemies) return;
42 if (!autocvar_hud_shownames_crosshairdistance && this.sameteam)
48 traceline(view_origin, this.origin, MOVE_NORMAL, this);
49 hit = !(trace_fraction < 1 && (trace_networkentity != this.sv_entnum && trace_ent.entnum != this.sv_entnum));
53 vector o = project_3d_to_2d(this.origin + eZ * autocvar_hud_shownames_offset);
54 if (autocvar_hud_shownames_crosshairdistance)
56 float d = autocvar_hud_shownames_crosshairdistance;
57 float w = o.x - vid_conwidth / 2;
58 float h = o.y - vid_conheight / 2;
59 if (d * d > w * w + h * h) this.pointtime = time;
60 if (this.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time)
62 else if(!autocvar_hud_shownames_crosshairdistance_antioverlap)
66 float dist = vlen(this.origin - view_origin);
67 if (overlap == -1 && autocvar_hud_shownames_antioverlap)
69 // fade tag out if another tag that is closer to you overlaps
71 LL_EACH(shownames_ent, it != this, {
72 entcs = entcs_receiver(i);
73 if (!(entcs && entcs.has_sv_origin))
75 vector eo = project_3d_to_2d(it.origin);
76 if (eo.z < 0 || eo.x < 0 || eo.y < 0 || eo.x > vid_conwidth || eo.y > vid_conheight) continue;
78 if (vdist(((eX * o.x + eY * o.y) - eo), <, autocvar_hud_shownames_antioverlap_distance)
79 && vdist((it.origin - view_origin), <, dist))
86 bool onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
87 if (!this.fadedelay) this.fadedelay = time + SHOWNAMES_FADEDELAY;
88 if (this.csqcmodel_isdead) // dead player, fade out slowly
90 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
92 else if (!onscreen || (!this.sameteam && !hit)) // out of view, fade out
94 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
95 this.fadedelay = 0; // reset fade in delay, enemy has left the view
97 else if (overlap > 0) // tag overlap detected, fade out
99 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
101 else if (this.sameteam) // fade in for team mates
103 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
105 else if (time > this.fadedelay) // fade in for enemies
107 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
109 float a = autocvar_hud_shownames_alpha * this.alpha;
110 // multiply by player alpha
111 if (!this.sameteam || (this.sv_entnum == player_localentnum))
113 float f = entcs_GetAlpha(this.sv_entnum - 1);
116 // FIXME: alpha is negative when dead, breaking death fade
117 if (!this.csqcmodel_isdead) a *= f;
119 if (a < ALPHA_MIN_VISIBLE && gametype != MAPINFO_TYPE_CTS) return;
120 if (autocvar_hud_shownames_maxdistance)
122 if (dist >= autocvar_hud_shownames_maxdistance) return;
123 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
124 a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
128 if (autocvar_hud_shownames_resize) // limit resize so its never smaller than 0.5... gets unreadable
130 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
131 resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
133 // draw the sprite image
137 vector mySize = (eX * autocvar_hud_shownames_aspect + eY) * autocvar_hud_shownames_fontsize;
138 vector myPos = o - '0.5 0 0' * mySize.x - '0 1 0' * mySize.y;
142 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
143 myPos.y += (mySize.y / resize - mySize.y);
144 // this is where the origin of the string
145 vector namepos = myPos;
146 float namewidth = mySize.x;
147 if (autocvar_hud_shownames_status && this.sameteam)
149 vector v = namepos + '0 1 0' * autocvar_hud_shownames_fontsize * resize;
150 vector s = eX * 0.5 * mySize.x + eY * resize * autocvar_hud_shownames_statusbar_height;
151 if (this.healthvalue > 0)
153 HUD_Panel_DrawProgressBar(v, s, "nametag_statusbar",
154 this.healthvalue / autocvar_hud_panel_healtharmor_maxhealth, false, 1, '1 0 0', a,
157 if (this.armorvalue > 0)
159 HUD_Panel_DrawProgressBar(v + eX * 0.5 * mySize.x, s, "nametag_statusbar",
160 this.armorvalue / autocvar_hud_panel_healtharmor_maxarmor, false, 0, '0 1 0', a,
164 string s = entcs_GetName(this.sv_entnum - 1);
165 if ((autocvar_hud_shownames_decolorize == 1 && teamplay)
166 || autocvar_hud_shownames_decolorize == 2) s = playername(s, entcs_GetTeam(this.sv_entnum - 1));
167 drawfontscale = '1 1 0' * resize;
168 s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
169 float width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
170 if (width != namewidth) namepos.x += (namewidth - width) / 2;
171 drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
172 drawfontscale = '1 1 0';
176 void Draw_ShowNames_All()
178 if (!autocvar_hud_shownames) return;
179 LL_EACH(shownames_ent, true, {
180 entity entcs = entcs_receiver(i);
187 assert(getthink(entcs), eprint(entcs));
188 getthink(entcs)(entcs);
189 if (!entcs.has_origin) continue;
190 if (entcs.m_entcs_private)
192 it.healthvalue = entcs.healthvalue;
193 it.armorvalue = entcs.armorvalue;
202 bool dead = entcs_IsDead(i) || entcs_IsSpectating(i);
203 if (!it.csqcmodel_isdead) setorigin(it, entcs.origin);
204 it.csqcmodel_isdead = dead;