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