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