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