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