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