]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/shownames.qc
take3: format 903 files
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / shownames.qc
1 #include "shownames.qh"
2
3 #include "autocvars.qh"
4 #include "miscfunctions.qh"
5 #include "hud/_mod.qh"
6
7 #include <common/ent_cs.qh>
8 #include <common/constants.qh>
9 #include <common/net_linked.qh>
10 #include <common/mapinfo.qh>
11 #include <common/teams.qh>
12
13 #include <lib/csqcmodel/cl_model.qh>
14
15 // this.isactive = player is in range and coordinates/status (health and armor) are up to date
16 // this.origin = player origin
17 // this.healthvalue
18 // this.armorvalue
19 // this.sameteam = player is on same team as local client
20 // this.fadedelay = time to wait before name tag starts fading in for enemies
21 // this.pointtime = last time you pointed at this player
22 // this.csqcmodel_isdead = value of csqcmodel_isdead to know when the player is dead or not
23
24 LinkedList shownames_ent;
25 STATIC_INIT(shownames_ent)
26 {
27         shownames_ent = LL_NEW();
28         for (int i = 0; i < maxclients; ++i) {
29                 entity e = new_pure(shownames_tag);
30                 e.sv_entnum = i + 1;
31                 LL_PUSH(shownames_ent, e);
32         }
33 }
34
35 const float SHOWNAMES_FADESPEED = 4;
36 const float SHOWNAMES_FADEDELAY = 0.4;
37 void Draw_ShowNames(entity this)
38 {
39         if (this.sv_entnum == (current_player + 1)) { // self or spectatee
40                 if (!(autocvar_hud_shownames_self && autocvar_chase_active)) { return; } }
41         if (!this.sameteam && !autocvar_hud_shownames_enemies) { return; }
42         bool hit;
43         if (!autocvar_hud_shownames_crosshairdistance && this.sameteam) {
44                 hit = true;
45         } else {
46                 traceline(view_origin, this.origin, MOVE_NORMAL, this);
47                 hit = !(trace_fraction < 1 && (trace_networkentity != this.sv_entnum && trace_ent.entnum != this.sv_entnum));
48         }
49         // handle tag fading
50         int overlap = -1;
51         vector o = project_3d_to_2d(this.origin + eZ * autocvar_hud_shownames_offset);
52         if (autocvar_hud_shownames_crosshairdistance) {
53                 float d = autocvar_hud_shownames_crosshairdistance;
54                 float w = o.x - vid_conwidth / 2;
55                 float h = o.y - vid_conheight / 2;
56                 if (d * d > w * w + h * h) { this.pointtime = time; }
57                 if (this.pointtime + autocvar_hud_shownames_crosshairdistance_time <= time) {
58                         overlap = 1;
59                 } else if (!autocvar_hud_shownames_crosshairdistance_antioverlap) {
60                         overlap = 0;
61                 }
62         }
63
64         if (overlap == -1 && autocvar_hud_shownames_antioverlap) {
65                 // fade tag out if another tag that is closer to you overlaps
66                 entity entcs = NULL;
67                 LL_EACH(shownames_ent, it != this, {
68                         entcs = entcs_receiver(i);
69                         if (!(entcs && entcs.has_sv_origin)) {
70                                 continue;
71                         }
72                         vector eo = project_3d_to_2d(it.origin);
73                         if (eo.z < 0 || eo.x < 0 || eo.y < 0 || eo.x > vid_conwidth || eo.y > vid_conheight) { continue; }
74                         eo.z = 0;
75                         if (vdist((vec2(o) - eo), <, autocvar_hud_shownames_antioverlap_distance)
76                         && vlen2(it.origin - view_origin) < vlen2(this.origin - view_origin)) {
77                                 overlap = 1;
78                                 break;
79                         }
80                 });
81         }
82         bool onscreen = (o.z >= 0 && o.x >= 0 && o.y >= 0 && o.x <= vid_conwidth && o.y <= vid_conheight);
83         if (!this.fadedelay) { this.fadedelay = time + SHOWNAMES_FADEDELAY; }
84         if (this.csqcmodel_isdead) { // dead player, fade out slowly
85                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * 0.25 * frametime);
86         } else if (!onscreen || (!this.sameteam && !hit)) { // out of view, fade out
87                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
88                 this.fadedelay = 0; // reset fade in delay, enemy has left the view
89         } else if (overlap > 0) { // tag overlap detected, fade out
90                 this.alpha = max(0, this.alpha - SHOWNAMES_FADESPEED * frametime);
91         } else if (this.sameteam) { // fade in for team mates
92                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
93         } else if (time > this.fadedelay) { // fade in for enemies
94                 this.alpha = min(1, this.alpha + SHOWNAMES_FADESPEED * frametime);
95         }
96         float a = autocvar_hud_shownames_alpha * this.alpha;
97         // multiply by player alpha
98         if (!this.sameteam || (this.sv_entnum == player_localentnum)) {
99                 float f = entcs_GetAlpha(this.sv_entnum - 1);
100                 if (f == 0) { f = 1; }
101                 if (f < 0) { f = 0; }
102                 // FIXME: alpha is negative when dead, breaking death fade
103                 if (!this.csqcmodel_isdead) { a *= f; }
104         }
105         if (a < ALPHA_MIN_VISIBLE && gametype != MAPINFO_TYPE_CTS) { return; }
106         if (vdist(this.origin - view_origin, >=, max_shot_distance)) { return; }
107         float dist = vlen(this.origin - view_origin);
108         if (autocvar_hud_shownames_maxdistance) {
109                 if (dist >= autocvar_hud_shownames_maxdistance) { return; }
110                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
111                 a *= (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
112         }
113         if (!a) { return; }
114         float resize = 1;
115         if (autocvar_hud_shownames_resize) { // limit resize so its never smaller than 0.5... gets unreadable
116                 float f = autocvar_hud_shownames_maxdistance - autocvar_hud_shownames_mindistance;
117                 resize = 0.5 + 0.5 * (f - max(0, dist - autocvar_hud_shownames_mindistance)) / f;
118         }
119         // draw the sprite image
120         if (o.z >= 0) {
121                 o.z = 0;
122                 vector mySize = (vec2(autocvar_hud_shownames_aspect, 1)) * autocvar_hud_shownames_fontsize;
123                 vector myPos = o - vec2(0.5 * mySize.x, mySize.y);
124                 // size scaling
125                 mySize.x *= resize;
126                 mySize.y *= resize;
127                 myPos.x += 0.5 * (mySize.x / resize - mySize.x);
128                 myPos.y += (mySize.y / resize - mySize.y);
129                 // this is where the origin of the string
130                 vector namepos = myPos;
131                 float namewidth = mySize.x;
132                 if (autocvar_hud_shownames_status && this.sameteam) {
133                         vector pos = namepos + eY * autocvar_hud_shownames_fontsize * resize;
134                         vector sz = vec2(0.5 * mySize.x, resize * autocvar_hud_shownames_statusbar_height);
135                         if (this.healthvalue > 0) {
136                                 HUD_Panel_DrawProgressBar(pos, sz, "nametag_statusbar",
137                                         this.healthvalue / autocvar_hud_panel_healtharmor_maxhealth, false, 1, '1 0 0', a,
138                                         DRAWFLAG_NORMAL);
139                         }
140                         if (this.armorvalue > 0) {
141                                 HUD_Panel_DrawProgressBar(pos + eX * 0.5 * mySize.x, sz, "nametag_statusbar",
142                                         this.armorvalue / autocvar_hud_panel_healtharmor_maxarmor, false, 0, '0 1 0', a,
143                                         DRAWFLAG_NORMAL);
144                         }
145                 }
146                 string s = entcs_GetName(this.sv_entnum - 1);
147                 if ((autocvar_hud_shownames_decolorize == 1 && teamplay)
148                         || autocvar_hud_shownames_decolorize == 2) { s = playername(s, entcs_GetTeam(this.sv_entnum - 1)); }
149                 drawfontscale = '1 1 0' * resize;
150                 s = textShortenToWidth(s, namewidth, '1 1 0' * autocvar_hud_shownames_fontsize, stringwidth_colors);
151                 float width = stringwidth(s, true, '1 1 0' * autocvar_hud_shownames_fontsize);
152                 if (width != namewidth) { namepos.x += (namewidth - width) / 2; }
153                 drawcolorcodedstring(namepos, s, '1 1 0' * autocvar_hud_shownames_fontsize, a, DRAWFLAG_NORMAL);
154                 drawfontscale = '1 1 0';
155         }
156 }
157
158 void Draw_ShowNames_All()
159 {
160         if (!autocvar_hud_shownames) { return; }
161         LL_EACH(shownames_ent, true, {
162                 entity entcs = entcs_receiver(i);
163                 if (!entcs) {
164                         make_pure(it);
165                         continue;
166                 }
167                 make_impure(it);
168                 assert(getthink(entcs), eprint(entcs));
169                 getthink(entcs)(entcs);
170                 if (!entcs.has_origin) { continue; }
171                 if (entcs.m_entcs_private) {
172                         it.healthvalue = entcs.healthvalue;
173                         it.armorvalue = entcs.armorvalue;
174                         it.sameteam = true;
175                 } else {
176                         it.healthvalue = 0;
177                         it.armorvalue = 0;
178                         it.sameteam = false;
179                 }
180                 bool dead = entcs_IsDead(i) || entcs_IsSpectating(i);
181                 if (!it.csqcmodel_isdead) { setorigin(it, entcs.origin); }
182                 it.csqcmodel_isdead = dead;
183                 Draw_ShowNames(it);
184         });
185 }