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