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