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