1 #include "shownames.qh"
5 #include "../common/constants.qh"
6 #include "../common/mapinfo.qh"
7 #include "../common/teams.qh"
9 #include "../lib/csqcmodel/cl_model.qh"
11 // this.isactive = player is in range and coordinates/status (health and armor) are up to date
12 // this.origin = player origin
15 // this.sameteam = player is on same team as local client
16 // this.fadedelay = time to wait before name tag starts fading in for enemies
17 // this.pointtime = last time you pointed at this player
18 // this.csqcmodel_isdead = value of csqcmodel_isdead to know when the player is dead or not
20 LinkedList shownames_ent;
21 STATIC_INIT(shownames_ent)
23 shownames_ent = LL_NEW();
24 for (int i = 0; i < maxclients; ++i)
26 entity e = new_pure(shownames_tag);
28 LL_PUSH(shownames_ent, e);
32 const float SHOWNAMES_FADESPEED = 4;
33 const float SHOWNAMES_FADEDELAY = 0.4;
34 void Draw_ShowNames(entity this)
36 if (this.sv_entnum == player_localentnum) // self or spectatee
37 if (!(autocvar_hud_shownames_self && autocvar_chase_active)) return;
38 if (!this.sameteam && !autocvar_hud_shownames_enemies) return;
40 if (!autocvar_hud_shownames_crosshairdistance && this.sameteam)
46 traceline(view_origin, this.origin, MOVE_NORMAL, this);
47 hit = !(trace_fraction < 1 && (trace_networkentity != this.sv_entnum && trace_ent.entnum != this.sv_entnum));
51 vector o = project_3d_to_2d(this.origin + eZ * autocvar_hud_shownames_offset);
52 float dist = vlen(this.origin - view_origin);
53 if (autocvar_hud_shownames_antioverlap)
55 // fade tag out if another tag that is closer to you overlaps
56 LL_EACH(shownames_ent, it != this && entcs_receiver(i), {
57 vector eo = project_3d_to_2d(it.origin);
58 if (eo.z < 0 || eo.x < 0 || eo.y < 0 || eo.x > vid_conwidth || eo.y > vid_conheight) continue;
60 if (vdist(((eX * o.x + eY * o.y) - eo), <, autocvar_hud_shownames_antioverlap_distance)
61 && vdist((it.origin - view_origin), <, dist))
68 bool onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
69 if (autocvar_hud_shownames_crosshairdistance)
71 float d = autocvar_hud_shownames_crosshairdistance;
72 float w = o.x - vid_conwidth / 2;
73 float h = o.y - vid_conheight / 2;
74 if (d * d > w * w + h * h) this.pointtime = time;
75 if (this.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time) overlap = true;
76 else overlap = (autocvar_hud_shownames_crosshairdistance_antioverlap ? overlap : false); // override what antioverlap says unless allowed by cvar.
78 if (!this.fadedelay) this.fadedelay = time + SHOWNAMES_FADEDELAY;
79 if (this.csqcmodel_isdead) // dead player, fade out slowly
81 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
83 else if (!onscreen || (!this.sameteam && !hit)) // out of view, fade out
85 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
86 this.fadedelay = 0; // reset fade in delay, enemy has left the view
88 else if (overlap) // tag overlap detected, fade out
90 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
92 else if (this.sameteam) // fade in for team mates
94 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
96 else if (time > this.fadedelay) // fade in for enemies
98 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
100 float a = autocvar_hud_shownames_alpha * this.alpha;
101 // multiply by player alpha
102 if (!this.sameteam || (this.sv_entnum == player_localentnum))
104 float f = entcs_GetAlpha(this.sv_entnum - 1);
107 // FIXME: alpha is negative when dead, breaking death fade
108 if (!this.csqcmodel_isdead) a *= f;
110 if (a < ALPHA_MIN_VISIBLE && gametype != MAPINFO_TYPE_CTS) return;
111 if (autocvar_hud_shownames_maxdistance)
113 if (dist >= autocvar_hud_shownames_maxdistance) return;
114 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
115 a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
119 if (autocvar_hud_shownames_resize) // limit resize so its never smaller than 0.5... gets unreadable
121 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
122 resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
124 // draw the sprite image
128 vector mySize = (eX * autocvar_hud_shownames_aspect + eY) * autocvar_hud_shownames_fontsize;
129 vector myPos = o - '0.5 0 0' * mySize.x - '0 1 0' * mySize.y;
133 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
134 myPos.y += (mySize.y / resize - mySize.y);
135 // this is where the origin of the string
136 vector namepos = myPos;
137 float namewidth = mySize.x;
138 if (autocvar_hud_shownames_status && this.sameteam)
140 vector v = namepos + '0 1 0' * autocvar_hud_shownames_fontsize * resize;
141 vector s = eX * 0.5 * mySize.x + eY * resize * autocvar_hud_shownames_statusbar_height;
142 if (this.healthvalue > 0)
144 HUD_Panel_DrawProgressBar(v, s, "nametag_statusbar",
145 this.healthvalue / autocvar_hud_panel_healtharmor_maxhealth, false, 1, '1 0 0', a,
148 if (this.armorvalue > 0)
150 HUD_Panel_DrawProgressBar(v + eX * 0.5 * mySize.x, s, "nametag_statusbar",
151 this.armorvalue / autocvar_hud_panel_healtharmor_maxarmor, false, 0, '0 1 0', a,
155 string s = entcs_GetName(this.sv_entnum - 1);
156 if ((autocvar_hud_shownames_decolorize == 1 && teamplay)
157 || autocvar_hud_shownames_decolorize == 2) s = playername(s, entcs_GetTeam(this.sv_entnum - 1));
158 drawfontscale = '1 1 0' * resize;
159 s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
160 float width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
161 if (width != namewidth) namepos.x += (namewidth - width) / 2;
162 drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
163 drawfontscale = '1 1 0';
167 void Draw_ShowNames_All()
169 if (!autocvar_hud_shownames) return;
170 LL_EACH(shownames_ent, true, {
171 entity entcs = entcs_receiver(i);
178 assert(entcs.think, eprint(entcs));
179 WITH(entity, self, entcs, entcs.think());
180 if (!entcs.has_origin) continue;
181 if (entcs.m_entcs_private)
183 it.healthvalue = entcs.healthvalue;
184 it.armorvalue = entcs.armorvalue;
193 bool dead = entcs_IsDead(i) || entcs_IsSpectating(i);
194 if (!it.csqcmodel_isdead) setorigin(it, entcs.origin);
195 it.csqcmodel_isdead = dead;