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)
24 float since_hit = time - this.hit_time;
25 // can't use `dt = hit_time - prev_update_time` because shrinking wouldn't be linear
26 float size = this.m_size - since_hit * this.m_shrink_rate * this.m_size;
27 float alpha_ = this.alpha - since_hit * this.fade_rate;
28 bool haslifetime = (autocvar_cl_damagetext_lifetime < 0 // negative ignores lifetime
29 || (since_hit < autocvar_cl_damagetext_lifetime));
30 if (alpha_ <= 0 || size <= 0 || !haslifetime)
32 IL_REMOVE(g_damagetext, this);
38 if (this.m_screen_coords)
39 screen_pos = this.origin + since_hit * autocvar_cl_damagetext_2d_velocity;
42 vector forward, right, up;
43 MAKE_VECTORS(view_angles, forward, right, up);
45 vector world_offset = since_hit * autocvar_cl_damagetext_velocity_world
46 + autocvar_cl_damagetext_offset_world;
47 vector world_pos = this.origin
48 + world_offset.x * forward
49 + world_offset.y * right
50 + world_offset.z * up;
52 screen_pos = project_3d_to_2d(world_pos)
53 + (since_hit * autocvar_cl_damagetext_velocity_screen)
54 + autocvar_cl_damagetext_offset_screen;
56 screen_pos.y += size / 2;
58 // strip trailing spaces
59 string dtext = this.text;
60 int j = strlen(dtext) - 1;
61 while (substring(dtext, j, 1) == " " && j >= 0)
63 if (j < strlen(dtext) - 1)
64 dtext = substring(dtext, 0, j + 1);
66 // strip leading spaces
68 while (substring(dtext, j, 1) == " " && j < strlen(dtext))
71 dtext = substring(dtext, j, strlen(dtext) - j);
74 screen_pos.x -= stringwidth(dtext, true, hud_fontsize * 2) * 0.5;
76 if (screen_pos.z >= 0)
80 if (this.m_friendlyfire)
81 rgb = this.m_color_friendlyfire;
85 if (autocvar_cl_damagetext_color_per_weapon)
87 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
88 if (w != WEP_Null) rgb = w.wpcolor;
91 vector drawfontscale_save = drawfontscale;
92 drawfontscale = (size / autocvar_cl_damagetext_size_max) * '1 1 0';
93 screen_pos.y -= drawfontscale.x * size / 2;
94 drawcolorcodedstring2_builtin(screen_pos, this.text,
95 autocvar_cl_damagetext_size_max * '1 1 0',
96 rgb, alpha_, DRAWFLAG_NORMAL);
97 drawfontscale = drawfontscale_save;
100 ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
102 void DamageText_update(DamageText this, vector _origin, bool screen_coords,
103 int _health, int _armor, int _potential_damage, int _deathtype)
105 this.m_healthdamage = _health;
106 this.m_armordamage = _armor;
107 this.m_potential_damage = _potential_damage;
108 this.m_deathtype = _deathtype;
109 this.hit_time = time;
110 setorigin(this, _origin);
111 this.m_screen_coords = screen_coords;
112 if (this.m_screen_coords)
113 this.alpha = autocvar_cl_damagetext_2d_alpha_start;
115 this.alpha = autocvar_cl_damagetext_alpha_start;
117 int health = rint(this.m_healthdamage
118 / DAMAGETEXT_PRECISION_MULTIPLIER);
119 int armor = rint(this.m_armordamage
120 / DAMAGETEXT_PRECISION_MULTIPLIER);
121 int total = rint((this.m_healthdamage + this.m_armordamage)
122 / DAMAGETEXT_PRECISION_MULTIPLIER);
123 int potential = rint(this.m_potential_damage
124 / DAMAGETEXT_PRECISION_MULTIPLIER);
125 int potential_health = rint((this.m_potential_damage - this.m_armordamage)
126 / DAMAGETEXT_PRECISION_MULTIPLIER);
128 bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage,
129 this.m_potential_damage, 5);
131 string s = autocvar_cl_damagetext_format;
132 s = strreplace("{armor}", (
133 (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
135 : sprintf("%d", armor)
137 s = strreplace("{potential}", (
138 (redundant && autocvar_cl_damagetext_format_hide_redundant)
140 : sprintf("%d", potential)
142 s = strreplace("{potential_health}", (
143 (redundant && autocvar_cl_damagetext_format_hide_redundant)
145 : sprintf("%d", potential_health)
148 s = strreplace("{health}", (
149 (health == potential_health || !autocvar_cl_damagetext_format_verbose)
150 ? sprintf("%d", health)
151 : sprintf("%d (%d)", health, potential_health)
153 s = strreplace("{total}", (
154 (total == potential || !autocvar_cl_damagetext_format_verbose)
155 ? sprintf("%d", total)
156 : sprintf("%d (%d)", total, potential)
159 // futureproofing: remove any remaining (unknown) format strings
160 // in case we add new ones in the future so players can use them
161 // on new servers and still have working damagetext on old ones
164 int opening_pos = strstrofs(s, "{", 0);
165 if (opening_pos == -1) break;
166 int closing_pos = strstrofs(s, "}", opening_pos);
167 if (closing_pos == -1 || closing_pos <= opening_pos) break;
169 substring(s, 0, opening_pos),
170 substring_range(s, closing_pos + 1, strlen(s))
174 strcpy(this.text, s);
176 this.m_size = map_bound_ranges(potential,
177 autocvar_cl_damagetext_size_min_damage, autocvar_cl_damagetext_size_max_damage,
178 autocvar_cl_damagetext_size_min, autocvar_cl_damagetext_size_max);
181 CONSTRUCTOR(DamageText, int _group, vector _origin, bool _screen_coords,
182 int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire)
184 CONSTRUCT(DamageText);
185 this.m_group = _group;
186 this.m_friendlyfire = _friendlyfire;
187 this.m_screen_coords = _screen_coords;
190 if (autocvar_cl_damagetext_2d_alpha_lifetime)
191 this.fade_rate = 1 / autocvar_cl_damagetext_2d_alpha_lifetime;
192 if (autocvar_cl_damagetext_2d_size_lifetime)
193 this.m_shrink_rate = 1 / autocvar_cl_damagetext_2d_size_lifetime;
197 if (autocvar_cl_damagetext_alpha_lifetime)
198 this.fade_rate = 1 / autocvar_cl_damagetext_alpha_lifetime;
199 this.m_shrink_rate = 0;
201 DamageText_update(this, _origin, _screen_coords,
202 _health, _armor, _potential_damage, _deathtype);
203 IL_PUSH(g_damagetext, this);
206 DESTRUCTOR(DamageText)
209 --DamageText_screen_count;
213 float current_alpha(entity damage_text)
215 // alpha doesn't change - actual alpha is always calculated from the initial value
216 return damage_text.alpha - (time - damage_text.hit_time) * damage_text.fade_rate;
219 NET_HANDLE(damagetext, bool isNew)
222 int server_entity_index = ReadByte();
223 int deathtype = ReadInt24_t();
224 int flags = ReadByte();
225 bool friendlyfire = flags & DTFLAG_SAMETEAM;
227 int health, armor, potential_damage;
228 if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
229 else health = ReadShort();
230 if (flags & DTFLAG_NO_ARMOR) armor = 0;
231 else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
232 else armor = ReadShort();
233 if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
234 else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
235 else potential_damage = ReadShort();
239 if (autocvar_cl_damagetext == 0) return;
242 if (autocvar_cl_damagetext_friendlyfire == 0)
245 if (autocvar_cl_damagetext_friendlyfire == 1
246 && health == 0 && armor == 0)
250 int client_entity_index = server_entity_index - 1;
251 entity entcs = entcs_receiver(client_entity_index);
253 bool can_use_3d = entcs && entcs.has_origin;
254 bool too_close = vdist(entcs.origin - view_origin, <, autocvar_cl_damagetext_2d_close_range);
255 bool prefer_in_view = autocvar_cl_damagetext_2d_out_of_view && !projected_on_screen(project_3d_to_2d(entcs.origin));
256 bool prefer_2d = spectatee_status != -1 && autocvar_cl_damagetext_2d && (too_close || prefer_in_view);
260 entity entDT = NULL; // which DT to update
262 float alphathreshold = autocvar_cl_damagetext_accumulate_alpha_rel
263 * autocvar_cl_damagetext_alpha_start;
265 // check if this entity already has a DamageText for it
266 IL_EACH(g_damagetext, it.m_group == server_entity_index, {
267 // if the time window where damage accumulates closes,
268 // disown the parent entity from this DamageText
269 // and (likely) give the entity a new DT afterwards
270 // this should only cancel damage accumulation for this DT
271 if ((autocvar_cl_damagetext_accumulate_lifetime >= 0) // negative never disowns
272 && (time - it.hit_time > autocvar_cl_damagetext_accumulate_lifetime)
273 && (current_alpha(it) > alphathreshold))
279 health += it.m_healthdamage;
280 armor += it.m_armordamage;
281 potential_damage += it.m_potential_damage;
288 if (can_use_3d && !prefer_2d)
292 position = entcs.origin;
297 // 3D DamageTexts can later be changed into 2D,
298 // increment this here just in case
299 ++DamageText_screen_count;
302 else if (autocvar_cl_damagetext_2d && spectatee_status != -1)
304 // screen coords only
306 position = vec2(vid_conwidth * autocvar_cl_damagetext_2d_pos.x,
307 vid_conheight * autocvar_cl_damagetext_2d_pos.y);
312 // offset when hitting multiple enemies, dmgtext would overlap
313 position += autocvar_cl_damagetext_2d_overlap_offset * DamageText_screen_count++;
319 DamageText_update(entDT, position, is2d, health, armor, potential_damage, deathtype);
323 NEW(DamageText, server_entity_index, position, is2d,
324 health, armor, potential_damage, deathtype, friendlyfire);