1 #include "cl_damagetext.qh"
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);
20 STATIC_ATTRIB(DamageText, screen_count, int, 0);
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);
34 if (this.m_screen_coords) {
35 screen_pos = this.origin + since_hit * autocvar_cl_damagetext_2d_velocity;
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;
43 screen_pos.y += size / 2;
45 // strip trailing spaces
46 string dtext = this.text;
47 int j = strlen(dtext) - 1;
48 while (substring(dtext, j, 1) == " " && j >= 0)
50 if (j < strlen(dtext) - 1)
51 dtext = substring(dtext, 0, j + 1);
53 // strip leading spaces
55 while (substring(dtext, j, 1) == " " && j < strlen(dtext))
58 dtext = substring(dtext, j, strlen(dtext) - j);
61 screen_pos.x -= stringwidth(dtext, true, hud_fontsize * 2) * 0.5;
63 if (screen_pos.z >= 0) {
66 if (this.m_friendlyfire) {
67 rgb = this.m_color_friendlyfire;
71 if (autocvar_cl_damagetext_color_per_weapon) {
72 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
73 if (w != WEP_Null) rgb = w.wpcolor;
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;
83 ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
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;
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;
96 this.alpha = autocvar_cl_damagetext_alpha_start;
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);
105 bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage, this.m_potential_damage, 5);
107 string s = autocvar_cl_damagetext_format;
108 s = strreplace("{armor}", (
109 (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
111 : sprintf("%d", armor)
113 s = strreplace("{potential}", (
114 (redundant && autocvar_cl_damagetext_format_hide_redundant)
116 : sprintf("%d", potential)
118 s = strreplace("{potential_health}", (
119 (redundant && autocvar_cl_damagetext_format_hide_redundant)
121 : sprintf("%d", potential_health)
124 s = strreplace("{health}", (
125 (health == potential_health || !autocvar_cl_damagetext_format_verbose)
126 ? sprintf("%d", health)
127 : sprintf("%d (%d)", health, potential_health)
129 s = strreplace("{total}", (
130 (total == potential || !autocvar_cl_damagetext_format_verbose)
131 ? sprintf("%d", total)
132 : sprintf("%d (%d)", total, potential)
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
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;
143 substring(s, 0, opening_pos),
144 substring_range(s, closing_pos + 1, strlen(s))
148 strcpy(this.text, s);
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);
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;
166 if (autocvar_cl_damagetext_alpha_lifetime)
167 this.fade_rate = 1 / autocvar_cl_damagetext_alpha_lifetime;
168 this.m_shrink_rate = 0;
170 DamageText_update(this, _origin, _screen_coords, _health, _armor, _potential_damage, _deathtype);
171 IL_PUSH(g_damagetext, this);
174 DESTRUCTOR(DamageText) {
176 --DamageText_screen_count;
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;
185 NET_HANDLE(damagetext, bool isNew)
188 int server_entity_index = ReadByte();
189 int deathtype = ReadInt24_t();
190 int flags = ReadByte();
191 bool friendlyfire = flags & DTFLAG_SAMETEAM;
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();
205 if (autocvar_cl_damagetext == 0) return;
207 if (autocvar_cl_damagetext_friendlyfire == 0) return;
208 if (autocvar_cl_damagetext_friendlyfire == 1 && health == 0 && armor == 0) return;
211 int client_entity_index = server_entity_index - 1;
212 entity entcs = entcs_receiver(client_entity_index);
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);
219 if (can_use_3d && !prefer_2d) {
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);
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);
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);