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