]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/shownames.qc
Move gametype registers into their own files, fallback now selects a supported gamety...
[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/gamemodes/_mod.qh>
11 #include <common/net_linked.qh>
12 #include <common/mapinfo.qh>
13 #include <common/teams.qh>
14
15 #include <lib/csqcmodel/cl_model.qh>
16
17 // this.isactive = player is in range and coordinates/status (health and armor) are up to date
18 // this.origin = player origin
19 // this.healthvalue
20 // this.armorvalue
21 // this.sameteam = player is on same team as local client
22 // this.fadedelay = time to wait before name tag starts fading in for enemies
23 // this.pointtime = last time you pointed at this player
24 // this.csqcmodel_isdead = value of csqcmodel_isdead to know when the player is dead or not
25
26 LinkedList shownames_ent;
27 STATIC_INIT(shownames_ent)
28 {
29         shownames_ent = LL_NEW();
30         for (int i = 0; i < maxclients; ++i)
31         {
32                 entity e = new_pure(shownames_tag);
33                 e.sv_entnum = i + 1;
34                 LL_PUSH(shownames_ent, e);
35         }
36 }
37
38 const float SHOWNAMES_FADESPEED = 4;
39 const float SHOWNAMES_FADEDELAY = 0.4;
40 void Draw_ShowNames(entity this)
41 {
42         if (this.sv_entnum == current_player + 1) // self or spectatee
43         {
44                 if (!autocvar_chase_active)
45                         return;
46
47                 if (!autocvar_hud_shownames_self
48                         && !(spectatee_status > 0 && time <= spectatee_status_changed_time + 1))
49                 {
50                         return;
51                 }
52         }
53
54         if (!this.sameteam && !autocvar_hud_shownames_enemies) return;
55         bool hit;
56         if (!autocvar_hud_shownames_crosshairdistance && this.sameteam)
57         {
58                 hit = true;
59         }
60         else
61         {
62                 traceline(view_origin, this.origin, MOVE_NORMAL, this);
63                 hit = !(trace_fraction < 1 && (trace_networkentity != this.sv_entnum && trace_ent.entnum != this.sv_entnum));
64         }
65         // handle tag fading
66         int overlap = -1;
67         vector o = project_3d_to_2d(this.origin + eZ * autocvar_hud_shownames_offset);
68         if (autocvar_hud_shownames_crosshairdistance)
69         {
70                 float d = autocvar_hud_shownames_crosshairdistance;
71                 float w = o.x - vid_conwidth / 2;
72                 float h = o.y - vid_conheight / 2;
73                 if (d * d > w * w + h * h) this.pointtime = time;
74                 if (this.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time)
75                         overlap = 1;
76                 else if(!autocvar_hud_shownames_crosshairdistance_antioverlap)
77                         overlap = 0;
78         }
79
80         if (overlap == -1 && autocvar_hud_shownames_antioverlap)
81         {
82                 // fade tag out if another tag that is closer to you overlaps
83                 entity entcs = NULL;
84                 LL_EACH(shownames_ent, it != this, {
85                         entcs = entcs_receiver(i);
86                         if (!(entcs && entcs.has_sv_origin))
87                                 continue;
88                         vector eo = project_3d_to_2d(it.origin);
89                         if (eo.z < 0 || eo.x < 0 || eo.y < 0 || eo.x > vid_conwidth || eo.y > vid_conheight) continue;
90                         eo.z = 0;
91                         if (vdist((vec2(o) - eo), <, autocvar_hud_shownames_antioverlap_distance)
92                                 && vlen2(it.origin - view_origin) < vlen2(this.origin - view_origin))
93                         {
94                                 overlap = 1;
95                                 break;
96                         }
97                 });
98         }
99         bool onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
100         if (!this.fadedelay) this.fadedelay = time + SHOWNAMES_FADEDELAY;
101         if (this.csqcmodel_isdead) // dead player, fade out slowly
102         {
103                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
104         }
105         else if (!onscreen || (!this.sameteam && !hit)) // out of view, fade out
106         {
107                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
108                 this.fadedelay = 0; // reset fade in delay, enemy has left the view
109         }
110         else if (overlap > 0) // tag overlap detected, fade out
111         {
112                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
113         }
114         else if (this.sameteam)  // fade in for team mates
115         {
116                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
117         }
118         else if (time > this.fadedelay)  // fade in for enemies
119         {
120                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
121         }
122         float a = autocvar_hud_shownames_alpha * this.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                 a *= f;
129         }
130         if (a < ALPHA_MIN_VISIBLE && ISGAMETYPE(CTS)) return;
131         if (vdist(this.origin - view_origin, >=, max_shot_distance)) return;
132         float dist = vlen(this.origin - view_origin);
133         if (autocvar_hud_shownames_maxdistance)
134         {
135                 if (dist >= autocvar_hud_shownames_maxdistance) return;
136                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
137                 a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
138         }
139         if (!a) return;
140         float resize = 1;
141         if (autocvar_hud_shownames_resize)  // limit resize so its never smaller than 0.5... gets unreadable
142         {
143                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
144                 resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
145         }
146         // draw the sprite image
147         if (o.z >= 0)
148         {
149                 o.z = 0;
150                 vector mySize = (vec2(autocvar_hud_shownames_aspect, 1)) * autocvar_hud_shownames_fontsize;
151                 vector myPos = o - vec2(0.5 * mySize.x, mySize.y);
152                 // size scaling
153                 mySize.x *= resize;
154                 mySize.y *= resize;
155                 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
156                 myPos.y += (mySize.y / resize - mySize.y);
157                 // this is where the origin of the string
158                 float namewidth = mySize.x;
159                 if (autocvar_hud_shownames_status && this.sameteam)
160                 {
161                         vector pos = myPos + eY * autocvar_hud_shownames_fontsize * resize;
162                         vector sz = vec2(0.5 * mySize.x, resize * autocvar_hud_shownames_statusbar_height);
163                         if (autocvar_hud_shownames_statusbar_highlight)
164                                 drawfill(pos + eX * 0.25 * mySize.x, sz, '0.7 0.7 0.7', a / 2, DRAWFLAG_NORMAL);
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 (GetResource(this, RES_ARMOR) > 0)
172                         {
173                                 HUD_Panel_DrawProgressBar(pos + eX * 0.5 * mySize.x, sz, "nametag_statusbar",
174                                         GetResource(this, RES_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) || autocvar_hud_shownames_decolorize == 2)
180                         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                 myPos.x = o.x - (width * resize) / 2;
185                 drawcolorcodedstring(myPos, 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                         SetResourceExplicit(it, RES_ARMOR, GetResource(entcs, RES_ARMOR));
208                         it.sameteam = true;
209                 }
210                 else
211                 {
212                         it.healthvalue = 0;
213                         SetResourceExplicit(it, RES_ARMOR, 0);
214                         it.sameteam = false;
215                 }
216                 bool dead = entcs_IsDead(i) || entcs_IsSpectating(i);
217                 if ((!it.csqcmodel_isdead || it.alpha > 0) && entcs.origin != it.origin)
218                         setorigin(it, entcs.origin);
219                 it.csqcmodel_isdead = dead;
220                 Draw_ShowNames(it);
221         });
222 }