]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/shownames.qc
Merge branch 'master' into TimePath/csqc_viewmodels
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / shownames.qc
1 #include "shownames.qh"
2
3 #include "hud/all.qh"
4
5 #include "../common/constants.qh"
6 #include "../common/mapinfo.qh"
7 #include "../common/teams.qh"
8
9 #include "../lib/csqcmodel/cl_model.qh"
10
11 // self.isactive = player is in range and coordinates/status (health and armor) are up to date
12 // self.origin = player origin
13 // self.healthvalue
14 // self.armorvalue
15 // self.sameteam = player is on same team as local client
16 // self.fadedelay = time to wait before name tag starts fading in for enemies
17 // self.pointtime = last time you pointed at this player
18 // self.csqcmodel_isdead = value of csqcmodel_isdead to know when the player is dead or not
19
20 LinkedList shownames_ent;
21 STATIC_INIT(shownames_ent)
22 {
23         shownames_ent = LL_NEW();
24         for (int i = 0; i < maxclients; ++i)
25         {
26                 entity e = new(shownames_tag);
27                 e.sv_entnum = i + 1;
28                 LL_PUSH(shownames_ent, e);
29         }
30 }
31
32 const float SHOWNAMES_FADESPEED = 4;
33 const float SHOWNAMES_FADEDELAY = 0.4;
34 void Draw_ShowNames(entity this)
35 {
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;
39         bool hit;
40         if (!autocvar_hud_shownames_crosshairdistance && this.sameteam)
41         {
42                 hit = true;
43         }
44         else
45         {
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));
48         }
49         // handle tag fading
50         bool overlap = false;
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)
54         {
55                 // fade tag out if another tag that is closer to you overlaps
56                 LL_EACH(shownames_ent, it != this && entcs_receiver(i), LAMBDA(
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;
59                         eo.z = 0;
60                         if (vlen((eX * o.x + eY * o.y) - eo) < autocvar_hud_shownames_antioverlap_distance
61                             && dist > vlen(it.origin - view_origin))
62                         {
63                                 overlap = true;
64                                 break;
65                         }
66                 ));
67         }
68         bool onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
69         float crosshairdistance = sqrt(pow(o.x - vid_conwidth / 2, 2) + pow(o.y - vid_conheight / 2, 2));
70         if (autocvar_hud_shownames_crosshairdistance)
71         {
72                 if (autocvar_hud_shownames_crosshairdistance > crosshairdistance) this.pointtime = time;
73                 if (this.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time) overlap = true;
74                 else overlap = (autocvar_hud_shownames_crosshairdistance_antioverlap ? overlap : false); // override what antioverlap says unless allowed by cvar.
75         }
76         if (!this.fadedelay) this.fadedelay = time + SHOWNAMES_FADEDELAY;
77         if (this.csqcmodel_isdead)                                                                   // dead player, fade out slowly
78         {
79                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
80         }
81         else if (!this.sameteam && (!onscreen || !hit)) // out of view, fade out
82         {
83                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
84                 this.fadedelay = 0;                         // reset fade in delay, enemy has left the view
85         }
86         else if (overlap)                               // tag overlap detected, fade out
87         {
88                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
89         }
90         else if (this.sameteam)  // fade in for team mates
91         {
92                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
93         }
94         else if (time > this.fadedelay)  // fade in for enemies
95         {
96                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
97         }
98         float a = autocvar_hud_shownames_alpha * this.alpha;
99         // multiply by player alpha
100         if (!this.sameteam || (this.sv_entnum == player_localentnum))
101         {
102                 float f = getplayeralpha(this.sv_entnum - 1);
103                 if (f == 0) f = 1;
104                 if (f < 0) f = 0;
105                 // FIXME: alpha is negative when dead, breaking death fade
106                 if (!this.csqcmodel_isdead) a *= f;
107         }
108         if (a < ALPHA_MIN_VISIBLE && gametype != MAPINFO_TYPE_CTS) return;
109         if (autocvar_hud_shownames_maxdistance)
110         {
111                 if (dist >= autocvar_hud_shownames_maxdistance) return;
112                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
113                 a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
114         }
115         if (!a) return;
116         float resize = 1;
117         if (autocvar_hud_shownames_resize)  // limit resize so its never smaller than 0.5... gets unreadable
118         {
119                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
120                 resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
121         }
122         // draw the sprite image
123         if (o.z >= 0)
124         {
125                 o.z = 0;
126                 vector mySize = (eX * autocvar_hud_shownames_aspect + eY) * autocvar_hud_shownames_fontsize;
127                 vector myPos = o - '0.5 0 0' * mySize.x - '0 1 0' * mySize.y;
128                 // size scaling
129                 mySize.x *= resize;
130                 mySize.y *= resize;
131                 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
132                 myPos.y += (mySize.y / resize - mySize.y);
133                 // this is where the origin of the string
134                 vector namepos = myPos;
135                 float namewidth = mySize.x;
136                 if (autocvar_hud_shownames_status && this.sameteam)
137                 {
138                         vector v = namepos + '0 1 0' * autocvar_hud_shownames_fontsize * resize;
139                         vector s = eX * 0.5 * mySize.x + eY * resize * autocvar_hud_shownames_statusbar_height;
140                         if (this.healthvalue > 0)
141                         {
142                                 HUD_Panel_DrawProgressBar(v, s, "nametag_statusbar",
143                                         this.healthvalue / autocvar_hud_panel_healtharmor_maxhealth, false, 1, '1 0 0', a,
144                                         DRAWFLAG_NORMAL);
145                         }
146                         if (this.armorvalue > 0)
147                         {
148                                 HUD_Panel_DrawProgressBar(v + eX * 0.5 * mySize.x, s, "nametag_statusbar",
149                                         this.armorvalue / autocvar_hud_panel_healtharmor_maxarmor, false, 0, '0 1 0', a,
150                                         DRAWFLAG_NORMAL);
151                         }
152                 }
153                 string s = GetPlayerName(this.sv_entnum - 1);
154                 if ((autocvar_hud_shownames_decolorize == 1 && teamplay)
155                     || autocvar_hud_shownames_decolorize == 2) s = playername(s, GetPlayerColor(this.sv_entnum - 1));
156                 drawfontscale = '1 1 0' * resize;
157                 s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
158                 float width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
159                 if (width != namewidth) namepos.x += (namewidth - width) / 2;
160                 drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
161                 drawfontscale = '1 1 0';
162         }
163 }
164
165 void Draw_ShowNames_All()
166 {
167         if (!autocvar_hud_shownames) return;
168         LL_EACH(shownames_ent, true, LAMBDA(
169                 entity entcs = entcs_receiver(i);
170                 if (!entcs) continue;
171                 WITH(entity, self, entcs, entcs.think());
172                 if (entcs.m_entcs_private)
173                 {
174                         it.healthvalue = entcs.healthvalue;
175                         it.armorvalue = entcs.armorvalue;
176                         it.sameteam = true;
177                 }
178                 else
179                 {
180                         it.healthvalue = 0;
181                         it.armorvalue = 0;
182                         it.sameteam = false;
183                 }
184                 bool dead = getplayerisdead(i);
185                 if (!it.csqcmodel_isdead && entcs.has_origin) setorigin(it, entcs.origin);
186                 it.csqcmodel_isdead = dead;
187                 Draw_ShowNames(it);
188         ));
189 }