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