]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/shownames.qc
28b04b1d2ec7ccbba12bfa85c9494b7bdc10d96d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / shownames.qc
1 #include "shownames.qh"
2
3 #include "autocvars.qh"
4 #include "defs.qh"
5 #include "hud.qh"
6 #include "main.qh"
7 #include "miscfunctions.qh"
8 #include "../common/constants.qh"
9 #include "../common/mapinfo.qh"
10 #include "../common/teams.qh"
11 #include "../common/util.qh"
12 #include "../csqcmodellib/cl_model.qh"
13 #include "../dpdefs/csprogsdefs.qh"
14
15
16 // self.isactive = player is in range and coordinates/status (health and armor) are up to date
17 // self.origin = player origin TODO: should maybe move this so it's the origin of the shownames tag already in SSQC for culling?
18 // self.healthvalue
19 // self.armorvalue
20 // self.sameteam = player is on same team as local client
21 // self.fadedelay = time to wait before name tag starts fading in for enemies
22 // self.pointtime = last time you pointed at this player
23 // self.csqcmodel_isdead = value of csqcmodel_isdead to know when the player is dead or not
24
25 const float SHOWNAMES_FADESPEED = 4;
26 const float SHOWNAMES_FADEDELAY = 0.4;
27 void Draw_ShowNames(entity ent)
28 {
29         if(!autocvar_hud_shownames)
30                 return;
31
32         if(ent.sv_entnum == player_localentnum) // ent is me or person i'm spectating
33                 if(!(autocvar_hud_shownames_self && autocvar_chase_active))
34                         return;
35
36         if(ent.sameteam || (!ent.sameteam && autocvar_hud_shownames_enemies))
37         {
38                 ent.origin_z += autocvar_hud_shownames_offset;
39
40                 float hit;
41                 if(ent.sameteam && !autocvar_hud_shownames_crosshairdistance)
42                 {
43                         hit = 1;
44                 }
45                 else
46                 {
47                         traceline(view_origin, ent.origin, MOVE_NORMAL, ent);
48                         if(trace_fraction < 1 && (trace_networkentity != ent.sv_entnum && trace_ent.entnum != ent.sv_entnum))
49                                 hit = 0;
50                         else
51                                 hit = 1;
52                 }
53
54                 // handle tag fading
55                 float overlap = false, onscreen, crosshairdistance;
56                 vector o, eo;
57
58                 o = project_3d_to_2d(ent.origin);
59
60                 if(autocvar_hud_shownames_antioverlap)
61                 {
62                         // fade tag out if another tag that is closer to you overlaps
63                         entity e;
64                         for(e = world; (e = find(e, classname, "shownames_tag")); )
65                         {
66                                 if(e == ent)
67                                         continue;
68                                 eo = project_3d_to_2d(e.origin);
69                                 if (!(eo.z < 0 || eo.x < 0 || eo.y < 0 || eo.x > vid_conwidth || eo.y > vid_conheight))
70                                 {
71                                         eo.z = 0;
72                                         if(vlen((eX * o.x + eY * o.y) - eo) < autocvar_hud_shownames_antioverlap_distance && vlen(ent.origin - view_origin) > vlen(e.origin - view_origin))
73                                         {
74                                                 overlap = true;
75                                                 break;
76                                         }
77                                 }
78                         }
79                 }
80
81                 onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
82                 crosshairdistance = sqrt( pow(o.x - vid_conwidth/2, 2) + pow(o.y - vid_conheight/2, 2) );
83
84                 if(autocvar_hud_shownames_crosshairdistance)
85                 {
86                         if(autocvar_hud_shownames_crosshairdistance > crosshairdistance)
87                                 ent.pointtime = time;
88
89                         if (ent.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time)
90                                 overlap = true;
91                         else
92                                 overlap = (autocvar_hud_shownames_crosshairdistance_antioverlap ? overlap : false); // override what antioverlap says unless allowed by cvar.
93                 }
94
95                 if(!ent.fadedelay)
96                         ent.fadedelay = time + SHOWNAMES_FADEDELAY;
97
98                 if(!ent.sameteam && (!onscreen || !hit)) // out of view, fade out
99                 {
100                         ent.alpha = max(0, ent.alpha - SHOWNAMES_FADESPEED * frametime);
101                         ent.fadedelay = 0; // reset fade in delay, enemy has left the view
102                 }
103                 else if(ent.csqcmodel_isdead) // dead player, fade out slowly
104                         ent.alpha = max(0, ent.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
105                 else if(overlap) // tag overlap detected, fade out
106                         ent.alpha = max(0, ent.alpha - SHOWNAMES_FADESPEED * frametime);
107                 else if(ent.sameteam) // fade in for team mates
108                         ent.alpha = min(1, ent.alpha + SHOWNAMES_FADESPEED * frametime);
109                 else if(time > ent.fadedelay) // fade in for enemies
110                         ent.alpha = min(1, ent.alpha + SHOWNAMES_FADESPEED * frametime);
111
112                 // multiply by player alpha
113                 if(!ent.sameteam || (ent.sv_entnum == player_localentnum))
114                         ent.alpha *= getplayeralpha(ent.sv_entnum-1);
115
116                 if(ent.alpha < ALPHA_MIN_VISIBLE && gametype != MAPINFO_TYPE_CTS)
117                         return;
118
119                 float dist;
120                 dist = vlen(ent.origin - view_origin);
121
122                 float a;
123                 a = autocvar_hud_shownames_alpha;
124                 a *= ent.alpha;
125                 if(autocvar_hud_shownames_maxdistance)
126                 {
127                         if(dist >= autocvar_hud_shownames_maxdistance)
128                                 return;
129                         a *= ((autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance) - max(0, dist - autocvar_hud_shownames_mindistance)) / (autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance);
130                 }
131
132                 if(!a)
133                         return;
134
135                 float resize;
136                 resize = 1;
137                 if(autocvar_hud_shownames_resize) // limit resize so its never smaller than 0.5... gets unreadable
138                         resize = 0.5 + 0.5 * ((autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance) - max(0, dist - autocvar_hud_shownames_mindistance)) / (autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance);
139
140                 // draw the sprite image
141                 if(o.z >= 0)
142                 {
143                         o.z = 0;
144
145                         vector myPos, mySize;
146                         mySize = (eX * autocvar_hud_shownames_aspect + eY) * autocvar_hud_shownames_fontsize;
147                         myPos = o - '0.5 0 0' * mySize.x - '0 1 0' * mySize.y;
148
149                         // size scaling
150                         mySize.x *= resize;
151                         mySize.y *= resize;
152
153                         myPos.x += 0.5 * (mySize.x / resize - mySize.x);
154                         myPos.y += (mySize.y / resize - mySize.y);
155
156                         vector namepos; // this is where the origin of the string
157                         float namewidth;
158
159                         namepos = myPos;
160                         namewidth = mySize.x;
161
162                         if(autocvar_hud_shownames_status && teamplay)
163                         {
164                                 if(ent.sameteam)
165                                 {
166                                         if(ent.healthvalue > 0)
167                                         {
168                                                 HUD_Panel_DrawProgressBar(namepos + '0 1 0' * autocvar_hud_shownames_fontsize * resize, eX * 0.5 * mySize.x + eY * resize * autocvar_hud_shownames_statusbar_height, "nametag_statusbar", ent.healthvalue/autocvar_hud_panel_healtharmor_maxhealth, 0, 1, '1 0 0', a, DRAWFLAG_NORMAL);
169
170                                                 if(ent.armorvalue > 0)
171                                                         HUD_Panel_DrawProgressBar(namepos + '0 1 0' * autocvar_hud_shownames_fontsize * resize + eX * 0.5 * mySize.x, eX * 0.5 * mySize.x + eY * resize * autocvar_hud_shownames_statusbar_height, "nametag_statusbar", ent.armorvalue/autocvar_hud_panel_healtharmor_maxarmor, 0, 0, '0 1 0', a, DRAWFLAG_NORMAL);
172                                         }
173                                 }
174                         }
175
176                         string s;
177                         s = GetPlayerName(ent.sv_entnum-1);
178                         if((autocvar_hud_shownames_decolorize == 1 && teamplay) || autocvar_hud_shownames_decolorize == 2)
179                                 s = playername(s, GetPlayerColor(ent.sv_entnum-1));
180
181                         drawfontscale = '1 1 0' * resize;
182                         s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
183
184                         float width;
185                         width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
186
187                         if (width != namewidth)
188                                 namepos.x += (namewidth - width) / 2;
189                         drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
190                         drawfontscale = '1 1 0';
191                 }
192         }
193 }
194
195 entity shownames_ent[255];
196 void Draw_ShowNames_All()
197 {
198         int i;
199         for(i = 0; i < maxclients; ++i)
200         {
201                 float t;
202                 t = GetPlayerColor(i);
203                 if(t == NUM_SPECTATOR)
204                         continue;
205
206                 entity e;
207                 e = shownames_ent[i];
208                 if(!e)
209                 {
210                         e = spawn();
211                         e.classname = "shownames_tag";
212                         e.sv_entnum = i+1;
213                         shownames_ent[i] = e;
214                 }
215
216                 entity entcs;
217                 entcs = entcs_receiver[i];
218                 if(entcs)
219                 {
220                         e.healthvalue = entcs.healthvalue;
221                         e.armorvalue = entcs.armorvalue;
222                         e.sameteam = 1; /* (teamplay && (t == myteam)); */
223                 }
224                 else
225                 {
226                         e.healthvalue = 2342;
227                         e.armorvalue = 0;
228                         e.sameteam = 0;
229                 }
230
231                 setorigin(e, getplayerorigin(i));
232                 if(e.origin == GETPLAYERORIGIN_ERROR)
233                         continue;
234
235                 e.csqcmodel_isdead = getplayerisdead(i);
236
237                 Draw_ShowNames(e);
238         }
239 }