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