]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/shownames.qc
Merge remote branch 'origin/master' into fruitiex/animations
[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 //
7 const float SHOWNAMES_FADESPEED = 4;
8 void Draw_ShowNames(entity ent)
9 {
10         if(!autocvar_hud_shownames)
11                 return;
12
13         if(ent.sv_entnum == player_localentnum && !autocvar_chase_active)
14                 return;
15
16         makevectors(view_angles);
17
18         if(ent.sameteam || (!ent.sameteam && autocvar_hud_shownames_enemies))
19         {
20                 ent.origin_z += autocvar_hud_shownames_offset;
21
22                 float hit;
23                 if(ent.sameteam)
24                 {
25                         hit = 1;
26                 }
27                 else
28                 {
29                         traceline(view_origin, ent.origin, MOVE_NORMAL, ent);
30                         if(trace_fraction < 1 && trace_networkentity != ent.sv_entnum)
31                                 hit = 0;
32                         else
33                                 hit = 1;
34                 }
35
36                 vector o, eo;
37                 o = project_3d_to_2d(ent.origin);
38                 float overlap, onscreen;
39
40                 if(autocvar_hud_shownames_antioverlap)
41                 {
42                         // fade tag out if another tag that is closer to you overlaps
43                         entity e;
44                         for(e = world; (e = find(e, classname, "shownames_tag")); )
45                         {
46                                 if(e == ent)
47                                         continue;
48                                 eo = project_3d_to_2d(e.origin);
49                                 if not(eo_z < 0 || eo_x < 0 || eo_y < 0 || eo_x > vid_conwidth || eo_y > vid_conheight)
50                                 {
51                                         eo_z = 0;
52                                         if(vlen((eX * o_x + eY * o_y) - eo) < autocvar_hud_shownames_antioverlap_distance && vlen(ent.origin - view_origin) > vlen(e.origin - view_origin))
53                                         {
54                                                 overlap = TRUE;
55                                                 break;
56                                         }
57                                 }
58                         }
59                 }
60
61                 onscreen = (o_z >= 0 && o_x >= 0 && o_y >= 0 && o_x <= vid_conwidth && o_y <= vid_conheight);
62
63                 if(!ent.sameteam && (!onscreen || !hit)) // out of view, fade out
64                         ent.alpha = max(0, ent.alpha - SHOWNAMES_FADESPEED * frametime);
65                 else if(ent.healthvalue < 1) // dead player, fade out slowly
66                         ent.alpha = max(0, ent.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
67                 else if(overlap) // tag overlap detected, fade out
68                         ent.alpha = max(0, ent.alpha - SHOWNAMES_FADESPEED * frametime);
69                 else // fade in
70                         ent.alpha = min(1, ent.alpha + SHOWNAMES_FADESPEED * frametime);
71
72                 if(!ent.alpha)
73                         return;
74
75                 float dist;
76                 dist = vlen(ent.origin - view_origin);
77
78                 float a;
79                 a = autocvar_hud_shownames_alpha;
80                 a *= ent.alpha;
81                 if(autocvar_hud_shownames_maxdistance)
82                 {
83                         if(dist >= autocvar_hud_shownames_maxdistance)
84                                 return;
85                         a *= ((autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance) - max(0, dist - autocvar_hud_shownames_mindistance)) / (autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance);
86                 }
87
88                 if(!a)
89                         return;
90
91                 float resize;
92                 resize = 1;
93                 if(autocvar_hud_shownames_resize) // limit resize so its never smaller than 0.5... gets unreadable
94                         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);
95
96                 // draw the sprite image
97                 if(o_z >= 0)
98                 {
99                         o_z = 0;
100
101                         vector myPos, mySize;
102                         mySize = (eX * autocvar_hud_shownames_aspect + eY) * autocvar_hud_shownames_fontsize;
103                         myPos = o - '0.5 0 0' * mySize_x - '0 1 0' * mySize_y;
104
105                         // size scaling
106                         mySize_x *= resize;
107                         mySize_y *= resize;
108
109                         myPos_x += 0.5 * (mySize_x / resize - mySize_x);
110                         myPos_y += (mySize_y / resize - mySize_y);
111
112                         vector namepos; // this is where the origin of the string
113                         float namewidth;
114
115                         namepos = myPos;
116                         namewidth = mySize_x;
117
118                         if(autocvar_hud_shownames_status && teamplay)
119                         {
120                                 if(ent.sameteam)
121                                 {
122                                         if(ent.healthvalue > 0)
123                                         {
124                                                 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);
125
126                                                 if(ent.armorvalue > 0)
127                                                         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);
128                                         }
129                                 }
130                         }
131
132                         string s;
133                         s = GetPlayerName(ent.sv_entnum-1);
134                         if((autocvar_hud_shownames_decolorize == 1 && teamplay) || autocvar_hud_shownames_decolorize == 2)
135                                 s = playername(s, GetPlayerColor(ent.sv_entnum-1));
136
137                         drawfontscale = '1 1 0' * resize;
138                         s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
139
140                         float width;
141                         width = stringwidth(s, TRUE, '1 1 0' * autocvar_hud_shownames_fontsize);
142
143                         if (width != namewidth)
144                                 namepos_x += (namewidth - width) / 2;
145                         drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
146                         drawfontscale = '1 1 0';
147                 }
148         }
149 }
150
151 entity shownames_ent[255];
152 void Draw_ShowNames_All()
153 {
154         float i;
155         for(i = 0; i < maxclients; ++i)
156         {
157                 float t;
158                 t = GetPlayerColor(i);
159                 if(t == COLOR_SPECTATOR)
160                         continue;
161
162                 entity e;
163                 e = shownames_ent[i];
164                 if(!e)
165                 {
166                         e = spawn();
167                         e.classname = "shownames_tag";
168                         e.sv_entnum = i+1;
169                         shownames_ent[i] = e;
170                 }
171
172                 entity entcs;
173                 entcs = entcs_receiver[i];
174                 if(entcs)
175                 {
176                         e.healthvalue = entcs.healthvalue;
177                         e.armorvalue = entcs.armorvalue;
178                         e.sameteam = 1; /* (teamplay && (t == myteam)); */
179                 }
180                 else
181                 {
182                         e.healthvalue = 2342;
183                         e.armorvalue = 0;
184                         e.sameteam = 0;
185                 }
186
187                 e.origin = getplayerorigin(i);
188                 if(e.origin == GETPLAYERORIGIN_ERROR)
189                         continue;
190
191                 Draw_ShowNames(e);
192         }
193 }