]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc
9a23879aeeb64add4a9c478394fb87e5199554a9
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / damagetext / cl_damagetext.qc
1 #include "cl_damagetext.qh"
2
3 CLASS(DamageText, Object)
4     ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
5     ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
6     ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size_min);
7     ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
8     ATTRIB(DamageText, fade_rate, float, 0);
9     ATTRIB(DamageText, m_shrink_rate, float, 0);
10     ATTRIB(DamageText, m_group, int, 0);
11     ATTRIB(DamageText, m_friendlyfire, bool, false);
12     ATTRIB(DamageText, m_healthdamage, int, 0);
13     ATTRIB(DamageText, m_armordamage, int, 0);
14     ATTRIB(DamageText, m_potential_damage, int, 0);
15     ATTRIB(DamageText, m_deathtype, int, 0);
16     ATTRIB(DamageText, hit_time, float, 0);
17     ATTRIB(DamageText, text, string, string_null);
18     ATTRIB(DamageText, m_screen_coords, bool, false);
19
20     STATIC_ATTRIB(DamageText, screen_count, int, 0);
21
22     void DamageText_draw2d(DamageText this) {
23         float since_hit = time - this.hit_time;
24         // can't use `dt = hit_time - prev_update_time` because shrinking wouldn't be linear
25         float size = this.m_size - since_hit * this.m_shrink_rate * this.m_size;
26         float alpha_ = this.alpha - since_hit * this.fade_rate;
27         if (alpha_ <= 0 || size <= 0) {
28             IL_REMOVE(g_damagetext, this);
29             delete(this);
30             return;
31         }
32
33         vector screen_pos;
34         if (this.m_screen_coords) {
35             screen_pos = this.origin + since_hit * autocvar_cl_damagetext_2d_velocity;
36         } else {
37             vector forward, right, up;
38             MAKE_VECTORS(view_angles, forward, right, up);
39             vector world_offset = since_hit * autocvar_cl_damagetext_velocity_world + autocvar_cl_damagetext_offset_world;
40             vector world_pos = this.origin + world_offset.x * forward + world_offset.y * right + world_offset.z * up;
41             screen_pos = project_3d_to_2d(world_pos) + since_hit * autocvar_cl_damagetext_velocity_screen + autocvar_cl_damagetext_offset_screen;
42         }
43         screen_pos.y += size / 2;
44
45         // strip trailing spaces
46         string dtext = this.text;
47         int j = strlen(dtext) - 1;
48         while (substring(dtext, j, 1) == " " && j >= 0)
49             --j;
50         if (j < strlen(dtext) - 1)
51             dtext = substring(dtext, 0, j + 1);
52
53         // strip leading spaces
54         j = 0;
55         while (substring(dtext, j, 1) == " " && j < strlen(dtext))
56             ++j;
57         if (j > 0)
58             dtext = substring(dtext, j, strlen(dtext) - j);
59
60         // Center damage text
61         screen_pos.x -= stringwidth(dtext, true, hud_fontsize * 2) * 0.5;
62
63         if (screen_pos.z >= 0) {
64             screen_pos.z = 0;
65             vector rgb;
66             if (this.m_friendlyfire) {
67                 rgb = this.m_color_friendlyfire;
68             } else {
69                 rgb = this.m_color;
70             }
71             if (autocvar_cl_damagetext_color_per_weapon) {
72                 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
73                 if (w != WEP_Null) rgb = w.wpcolor;
74             }
75
76             vector drawfontscale_save = drawfontscale;
77             drawfontscale = (size / autocvar_cl_damagetext_size_max) * '1 1 0';
78             screen_pos.y -= drawfontscale.x * size / 2;
79             drawcolorcodedstring2_builtin(screen_pos, this.text, autocvar_cl_damagetext_size_max * '1 1 0', rgb, alpha_, DRAWFLAG_NORMAL);
80             drawfontscale = drawfontscale_save;
81         }
82     }
83     ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
84
85     void DamageText_update(DamageText this, vector _origin, bool screen_coords, int _health, int _armor, int _potential_damage, int _deathtype) {
86         this.m_healthdamage = _health;
87         this.m_armordamage = _armor;
88         this.m_potential_damage = _potential_damage;
89         this.m_deathtype = _deathtype;
90         this.hit_time = time;
91         setorigin(this, _origin);
92         this.m_screen_coords = screen_coords;
93         if (this.m_screen_coords) {
94             this.alpha = autocvar_cl_damagetext_2d_alpha_start;
95         } else {
96             this.alpha = autocvar_cl_damagetext_alpha_start;
97         }
98
99         int health = rint(this.m_healthdamage / DAMAGETEXT_PRECISION_MULTIPLIER);
100         int armor = rint(this.m_armordamage / DAMAGETEXT_PRECISION_MULTIPLIER);
101         int total = rint((this.m_healthdamage + this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
102         int potential = rint(this.m_potential_damage / DAMAGETEXT_PRECISION_MULTIPLIER);
103         int potential_health = rint((this.m_potential_damage - this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
104
105         bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage, this.m_potential_damage, 5);
106
107         string s = autocvar_cl_damagetext_format;
108         s = strreplace("{armor}", (
109             (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
110                 ? ""
111                 : sprintf("%d", armor)
112             ), s);
113         s = strreplace("{potential}", (
114             (redundant && autocvar_cl_damagetext_format_hide_redundant)
115                 ? ""
116                 : sprintf("%d", potential)
117             ), s);
118         s = strreplace("{potential_health}", (
119             (redundant && autocvar_cl_damagetext_format_hide_redundant)
120                 ? ""
121                 : sprintf("%d", potential_health)
122             ), s);
123
124         s = strreplace("{health}", (
125             (health == potential_health || !autocvar_cl_damagetext_format_verbose)
126                 ? sprintf("%d",      health)
127                 : sprintf("%d (%d)", health, potential_health)
128             ), s);
129         s = strreplace("{total}", (
130             (total == potential || !autocvar_cl_damagetext_format_verbose)
131                 ? sprintf("%d",      total)
132                 : sprintf("%d (%d)", total, potential)
133             ), s);
134
135         // futureproofing: remove any remaining (unknown) format strings in case we add new ones in the future
136         // so players can use them on new servers and still have working damagetext on old ones
137         while (true) {
138             int opening_pos = strstrofs(s, "{", 0);
139             if (opening_pos == -1) break;
140             int closing_pos = strstrofs(s, "}", opening_pos);
141             if (closing_pos == -1 || closing_pos <= opening_pos) break;
142             s = strcat(
143                 substring(s, 0, opening_pos),
144                 substring_range(s, closing_pos + 1, strlen(s))
145             );
146         }
147
148         strcpy(this.text, s);
149
150         this.m_size = map_bound_ranges(potential,
151             autocvar_cl_damagetext_size_min_damage, autocvar_cl_damagetext_size_max_damage,
152             autocvar_cl_damagetext_size_min, autocvar_cl_damagetext_size_max);
153     }
154
155     CONSTRUCTOR(DamageText, int _group, vector _origin, bool _screen_coords, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) {
156         CONSTRUCT(DamageText);
157         this.m_group = _group;
158         this.m_friendlyfire = _friendlyfire;
159         this.m_screen_coords = _screen_coords;
160         if (_screen_coords) {
161             if (autocvar_cl_damagetext_2d_alpha_lifetime)
162                 this.fade_rate = 1 / autocvar_cl_damagetext_2d_alpha_lifetime;
163             if (autocvar_cl_damagetext_2d_size_lifetime)
164                 this.m_shrink_rate = 1 / autocvar_cl_damagetext_2d_size_lifetime;
165         } else {
166             if (autocvar_cl_damagetext_alpha_lifetime)
167                 this.fade_rate = 1 / autocvar_cl_damagetext_alpha_lifetime;
168             this.m_shrink_rate = 0;
169         }
170         DamageText_update(this, _origin, _screen_coords, _health, _armor, _potential_damage, _deathtype);
171         IL_PUSH(g_damagetext, this);
172     }
173
174     DESTRUCTOR(DamageText) {
175         strfree(this.text);
176         --DamageText_screen_count;
177     }
178 ENDCLASS(DamageText)
179
180 float current_alpha(entity damage_text) {
181     // alpha doesn't change - actual alpha is always calculated from the initial value
182     return damage_text.alpha - (time - damage_text.hit_time) * damage_text.fade_rate;
183 }
184
185 NET_HANDLE(damagetext, bool isNew)
186 {
187     make_pure(this);
188     int server_entity_index = ReadByte();
189     int deathtype = ReadInt24_t();
190     int flags = ReadByte();
191     bool friendlyfire = flags & DTFLAG_SAMETEAM;
192
193     int health, armor, potential_damage;
194     if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
195     else health = ReadShort();
196     if (flags & DTFLAG_NO_ARMOR) armor = 0;
197     else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
198     else armor = ReadShort();
199     if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
200     else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
201     else potential_damage = ReadShort();
202
203     return = true;
204     if (!isNew) return;
205     if (autocvar_cl_damagetext == 0) return;
206     if (friendlyfire) {
207         if (autocvar_cl_damagetext_friendlyfire == 0) return;
208         if (autocvar_cl_damagetext_friendlyfire == 1 && health == 0 && armor == 0) return;
209     }
210
211     int client_entity_index = server_entity_index - 1;
212     entity entcs = entcs_receiver(client_entity_index);
213
214     bool can_use_3d = entcs && entcs.has_origin;
215     bool too_close = vdist(entcs.origin - view_origin, <, autocvar_cl_damagetext_2d_close_range);
216     bool prefer_in_view = autocvar_cl_damagetext_2d_out_of_view && !projected_on_screen(project_3d_to_2d(entcs.origin));
217     bool prefer_2d = spectatee_status != -1 && autocvar_cl_damagetext_2d && (too_close || prefer_in_view);
218
219     if (can_use_3d && !prefer_2d) {
220         // world coords
221         IL_EACH(g_damagetext, it.m_group == server_entity_index, {
222             if (current_alpha(it) > autocvar_cl_damagetext_accumulate_alpha_rel * autocvar_cl_damagetext_alpha_start) {
223                 DamageText_update(it, entcs.origin, false, it.m_healthdamage + health, it.m_armordamage + armor, it.m_potential_damage + potential_damage, deathtype);
224                 return;
225             }
226         });
227         ++DamageText_screen_count; //3D DamageTexts can later be changed into 2D, increment this just in case
228         NEW(DamageText, server_entity_index, entcs.origin, false, health, armor, potential_damage, deathtype, friendlyfire);
229     } else if (autocvar_cl_damagetext_2d && spectatee_status != -1) {
230         // screen coords only
231         vector screen_pos = vec2(vid_conwidth * autocvar_cl_damagetext_2d_pos.x, vid_conheight * autocvar_cl_damagetext_2d_pos.y);
232         IL_EACH(g_damagetext, it.m_group == server_entity_index, {
233             DamageText_update(it, screen_pos, true, it.m_healthdamage + health, it.m_armordamage + armor, it.m_potential_damage + potential_damage, deathtype);
234             return;
235         });
236
237         // offset when hitting multiple enemies, dmgtext would overlap
238         screen_pos += autocvar_cl_damagetext_2d_overlap_offset * DamageText_screen_count++;
239         NEW(DamageText, server_entity_index, screen_pos, true, health, armor, potential_damage, deathtype, friendlyfire);
240     }
241 }