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