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