8a7d225bff7bdc319260e5e0648a2aaff9b7aa08
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / shownames.qc
1 #include "shownames.qh"
2
3 #include "autocvars.qh"
4 #include "miscfunctions.qh"
5 #include "hud/_mod.qh"
6
7 #include <common/ent_cs.qh>
8 #include <common/constants.qh>
9 #include <common/net_linked.qh>
10 #include <common/mapinfo.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                 if (!(autocvar_hud_shownames_self && autocvar_chase_active)) return;
42         if (!this.sameteam && !autocvar_hud_shownames_enemies) return;
43         bool hit;
44         if (!autocvar_hud_shownames_crosshairdistance && this.sameteam)
45         {
46                 hit = true;
47         }
48         else
49         {
50                 traceline(view_origin, this.origin, MOVE_NORMAL, this);
51                 hit = !(trace_fraction < 1 && (trace_networkentity != this.sv_entnum && trace_ent.entnum != this.sv_entnum));
52         }
53         // handle tag fading
54         int overlap = -1;
55         vector o = project_3d_to_2d(this.origin + eZ * autocvar_hud_shownames_offset);
56         if (autocvar_hud_shownames_crosshairdistance)
57         {
58                 float d = autocvar_hud_shownames_crosshairdistance;
59                 float w = o.x - vid_conwidth / 2;
60                 float h = o.y - vid_conheight / 2;
61                 if (d * d > w * w + h * h) this.pointtime = time;
62                 if (this.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time)
63                         overlap = 1;
64                 else if(!autocvar_hud_shownames_crosshairdistance_antioverlap)
65                         overlap = 0;
66         }
67
68         if (overlap == -1 && autocvar_hud_shownames_antioverlap)
69         {
70                 // fade tag out if another tag that is closer to you overlaps
71                 entity entcs = NULL;
72                 LL_EACH(shownames_ent, it != this, {
73                         entcs = entcs_receiver(i);
74                         if (!(entcs && entcs.has_sv_origin))
75                                 continue;
76                         vector eo = project_3d_to_2d(it.origin);
77                         if (eo.z < 0 || eo.x < 0 || eo.y < 0 || eo.x > vid_conwidth || eo.y > vid_conheight) continue;
78                         eo.z = 0;
79                         if (vdist((vec2(o) - eo), <, autocvar_hud_shownames_antioverlap_distance)
80                             && vlen2(it.origin - view_origin) < vlen2(this.origin - view_origin))
81                         {
82                                 overlap = 1;
83                                 break;
84                         }
85                 });
86         }
87         bool onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
88         if (!this.fadedelay) this.fadedelay = time + SHOWNAMES_FADEDELAY;
89         if (this.csqcmodel_isdead)                                                                   // dead player, fade out slowly
90         {
91                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
92         }
93         else if (!onscreen || (!this.sameteam && !hit)) // out of view, fade out
94         {
95                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
96                 this.fadedelay = 0;                         // reset fade in delay, enemy has left the view
97         }
98         else if (overlap > 0) // tag overlap detected, fade out
99         {
100                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
101         }
102         else if (this.sameteam)  // fade in for team mates
103         {
104                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
105         }
106         else if (time > this.fadedelay)  // fade in for enemies
107         {
108                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
109         }
110         float a = autocvar_hud_shownames_alpha * this.alpha;
111         // multiply by player alpha
112         if (!this.sameteam || (this.sv_entnum == player_localentnum))
113         {
114                 float f = entcs_GetAlpha(this.sv_entnum - 1);
115                 if (f == 0) f = 1;
116                 if (f < 0) f = 0;
117                 // FIXME: alpha is negative when dead, breaking death fade
118                 if (!this.csqcmodel_isdead) a *= f;
119         }
120         if (a < ALPHA_MIN_VISIBLE && gametype != MAPINFO_TYPE_CTS) return;
121         if (vdist(this.origin - view_origin, >=, max_shot_distance)) return;
122         float dist = vlen(this.origin - view_origin);
123         if (autocvar_hud_shownames_maxdistance)
124         {
125                 if (dist >= autocvar_hud_shownames_maxdistance) return;
126                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
127                 a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
128         }
129         if (!a) return;
130         float resize = 1;
131         if (autocvar_hud_shownames_resize)  // limit resize so its never smaller than 0.5... gets unreadable
132         {
133                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
134                 resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
135         }
136         // draw the sprite image
137         if (o.z >= 0)
138         {
139                 o.z = 0;
140                 vector mySize = (vec2(autocvar_hud_shownames_aspect, 1)) * autocvar_hud_shownames_fontsize;
141                 vector myPos = o - vec2(0.5 * mySize.x, mySize.y);
142                 // size scaling
143                 mySize.x *= resize;
144                 mySize.y *= resize;
145                 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
146                 myPos.y += (mySize.y / resize - mySize.y);
147                 // this is where the origin of the string
148                 vector namepos = myPos;
149                 float namewidth = mySize.x;
150                 if (autocvar_hud_shownames_status && this.sameteam)
151                 {
152                         vector pos = namepos + eY * autocvar_hud_shownames_fontsize * resize;
153                         vector sz = vec2(0.5 * mySize.x, resize * autocvar_hud_shownames_statusbar_height);
154                         if (this.healthvalue > 0)
155                         {
156                                 HUD_Panel_DrawProgressBar(pos, sz, "nametag_statusbar",
157                                         this.healthvalue / autocvar_hud_panel_healtharmor_maxhealth, false, 1, '1 0 0', a,
158                                         DRAWFLAG_NORMAL);
159                         }
160                         if (this.armorvalue > 0)
161                         {
162                                 HUD_Panel_DrawProgressBar(pos + eX * 0.5 * mySize.x, sz, "nametag_statusbar",
163                                         this.armorvalue / autocvar_hud_panel_healtharmor_maxarmor, false, 0, '0 1 0', a,
164                                         DRAWFLAG_NORMAL);
165                         }
166                 }
167                 string s = entcs_GetName(this.sv_entnum - 1);
168                 if ((autocvar_hud_shownames_decolorize == 1 && teamplay)
169                     || autocvar_hud_shownames_decolorize == 2) s = playername(s, entcs_GetTeam(this.sv_entnum - 1));
170                 drawfontscale = '1 1 0' * resize;
171                 s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
172                 float width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
173                 if (width != namewidth) namepos.x += (namewidth - width) / 2;
174                 drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
175                 drawfontscale = '1 1 0';
176         }
177 }
178
179 void Draw_ShowNames_All()
180 {
181         if (!autocvar_hud_shownames) return;
182         LL_EACH(shownames_ent, true, {
183                 entity entcs = entcs_receiver(i);
184                 if (!entcs)
185                 {
186                         make_pure(it);
187                         continue;
188                 }
189                 make_impure(it);
190                 assert(getthink(entcs), eprint(entcs));
191                 getthink(entcs)(entcs);
192                 if (!entcs.has_origin) continue;
193                 if (entcs.m_entcs_private)
194                 {
195                         it.healthvalue = entcs.healthvalue;
196                         it.armorvalue = entcs.armorvalue;
197                         it.sameteam = true;
198                 }
199                 else
200                 {
201                         it.healthvalue = 0;
202                         it.armorvalue = 0;
203                         it.sameteam = false;
204                 }
205                 bool dead = entcs_IsDead(i) || entcs_IsSpectating(i);
206                 if (!it.csqcmodel_isdead) setorigin(it, entcs.origin);
207                 it.csqcmodel_isdead = dead;
208                 Draw_ShowNames(it);
209         });
210 }