1 #include "cl_damagetext.qh"
3 // no translatable cvar description please
4 AUTOCVAR_SAVE(cl_damagetext, bool, true, "Draw damage dealt where you hit the enemy");
5 AUTOCVAR_SAVE(cl_damagetext_format, string, "-{total}", "How to format the damage text. {health}, {armor}, {total}, {potential}: full damage not capped to target's health, {potential_health}: health damage not capped to target's health");
6 AUTOCVAR_SAVE(cl_damagetext_format_verbose, bool, false, "{health} shows {potential_health} too when they differ; {total} shows {potential} too when they differ");
7 AUTOCVAR_SAVE(cl_damagetext_format_hide_redundant, bool, false, "hide {armor} if 0; hide {potential} and {potential_health} when same as actual");
8 STATIC_INIT(DamageText_LegacyFormat) {
9 // damagetext used to be off by default and the cvar got saved in people's configs along with the old format
10 // enable damagetext while updating the format for a one time effect
11 if (strstrofs(autocvar_cl_damagetext_format, "{", 0) < 0) {
12 localcmd("\nseta cl_damagetext 1\n");
13 localcmd("\nseta cl_damagetext_format -{total}\n");
16 AUTOCVAR_SAVE(cl_damagetext_color, vector, '1 1 0', "Damage text color");
17 AUTOCVAR_SAVE(cl_damagetext_color_per_weapon, bool, false, "Damage text uses weapon color");
18 AUTOCVAR_SAVE(cl_damagetext_size_min, float, 10, "Damage text font size for small damage");
19 AUTOCVAR_SAVE(cl_damagetext_size_min_damage, float, 25, "How much damage is considered small");
20 AUTOCVAR_SAVE(cl_damagetext_size_max, float, 16, "Damage text font size for large damage");
21 AUTOCVAR_SAVE(cl_damagetext_size_max_damage, float, 140, "How much damage is considered large");
22 AUTOCVAR_SAVE(cl_damagetext_alpha_start, float, 1, "Damage text initial alpha");
23 AUTOCVAR_SAVE(cl_damagetext_alpha_lifetime, float, 3, "Damage text lifetime in seconds");
24 AUTOCVAR_SAVE(cl_damagetext_velocity, vector, '0 0 20', "Damage text move direction");
25 AUTOCVAR_SAVE(cl_damagetext_offset, vector, '0 -40 0', "Damage text offset");
26 AUTOCVAR_SAVE(cl_damagetext_accumulate_range, float, 30, "Damage text spawned within this range is accumulated");
27 AUTOCVAR_SAVE(cl_damagetext_accumulate_alpha_rel, float, 0.65, "Only update existing damage text when it's above this much percentage (0 to 1) of the starting alpha");
28 AUTOCVAR_SAVE(cl_damagetext_friendlyfire, bool, true, "Show damage text for friendlyfire too");
29 AUTOCVAR_SAVE(cl_damagetext_friendlyfire_color, vector, '1 0 0', "Damage text color for friendlyfire");
31 CLASS(DamageText, Object)
32 ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
33 ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
34 ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size_min);
35 ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
36 ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime);
37 ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity);
38 ATTRIB(DamageText, m_group, int, 0);
39 ATTRIB(DamageText, m_friendlyfire, bool, false);
40 ATTRIB(DamageText, m_healthdamage, int, 0);
41 ATTRIB(DamageText, m_armordamage, int, 0);
42 ATTRIB(DamageText, m_potential_damage, int, 0);
43 ATTRIB(DamageText, m_deathtype, int, 0);
44 ATTRIB(DamageText, time_prev, float, time);
45 ATTRIB(DamageText, text, string, string_null);
47 void DamageText_draw2d(DamageText this) {
48 float dt = time - this.time_prev;
49 this.time_prev = time;
50 setorigin(this, this.origin + dt * this.velocity);
51 this.alpha -= dt * this.fade_rate;
57 vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
58 if (pos.z >= 0 && this.m_size > 0) {
61 if (this.m_friendlyfire) {
62 rgb = this.m_color_friendlyfire;
66 if (autocvar_cl_damagetext_color_per_weapon) {
67 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
68 if (w != WEP_Null) rgb = w.wpcolor;
71 vector drawfontscale_save = drawfontscale;
72 drawfontscale = (this.m_size / autocvar_cl_damagetext_size_max) * '1 1 0';
73 drawcolorcodedstring2_builtin(pos, this.text, autocvar_cl_damagetext_size_max * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL);
74 drawfontscale = drawfontscale_save;
77 ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
79 void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype) {
80 this.m_healthdamage = _health;
81 this.m_armordamage = _armor;
82 this.m_potential_damage = _potential_damage;
83 this.m_deathtype = _deathtype;
84 setorigin(this, _origin);
85 this.alpha = autocvar_cl_damagetext_alpha_start;
87 int health = rint(this.m_healthdamage / DAMAGETEXT_PRECISION_MULTIPLIER);
88 int armor = rint(this.m_armordamage / DAMAGETEXT_PRECISION_MULTIPLIER);
89 int total = rint((this.m_healthdamage + this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
90 int potential = rint(this.m_potential_damage / DAMAGETEXT_PRECISION_MULTIPLIER);
91 int potential_health = rint((this.m_potential_damage - this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
93 bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage, this.m_potential_damage, 5);
95 string s = autocvar_cl_damagetext_format;
96 s = strreplace("{armor}", (
97 (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
99 : sprintf("%d", armor)
101 s = strreplace("{potential}", (
102 (redundant && autocvar_cl_damagetext_format_hide_redundant)
104 : sprintf("%d", potential)
106 s = strreplace("{potential_health}", (
107 (redundant && autocvar_cl_damagetext_format_hide_redundant)
109 : sprintf("%d", potential_health)
112 s = strreplace("{health}", (
113 (health == potential_health || !autocvar_cl_damagetext_format_verbose)
114 ? sprintf("%d", health)
115 : sprintf("%d (%d)", health, potential_health)
117 s = strreplace("{total}", (
118 (total == potential || !autocvar_cl_damagetext_format_verbose)
119 ? sprintf("%d", total)
120 : sprintf("%d (%d)", total, potential)
123 // futureproofing: remove any remaining (unknown) format strings in case we add new ones in the future
124 // so players can use them on new servers and still have working damagetext on old ones
126 int opening_pos = strstrofs(s, "{", 0);
127 if (opening_pos == -1) break;
128 int closing_pos = strstrofs(s, "}", opening_pos);
129 if (closing_pos == -1 || closing_pos <= opening_pos) break;
131 substring(s, 0, opening_pos),
132 substring_range(s, closing_pos + 1, strlen(s))
136 if (this.text) strunzone(this.text);
137 this.text = strzone(s);
139 float size_range = autocvar_cl_damagetext_size_max - autocvar_cl_damagetext_size_min;
140 float damage_range = autocvar_cl_damagetext_size_max_damage - autocvar_cl_damagetext_size_min_damage;
141 float scale_factor = size_range / damage_range;
143 autocvar_cl_damagetext_size_min,
144 (potential - autocvar_cl_damagetext_size_min_damage) * scale_factor + autocvar_cl_damagetext_size_min,
145 autocvar_cl_damagetext_size_max);
148 CONSTRUCTOR(DamageText, int _group, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) {
149 CONSTRUCT(DamageText);
150 this.m_group = _group;
151 this.m_friendlyfire = _friendlyfire;
152 DamageText_update(this, _origin, _health, _armor, _potential_damage, _deathtype);
153 IL_PUSH(g_drawables_2d, this);
156 DESTRUCTOR(DamageText) {
157 if (this.text) strunzone(this.text);
161 NET_HANDLE(damagetext, bool isNew)
163 int group = ReadShort();
164 vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
165 int deathtype = ReadInt24_t();
166 int flags = ReadByte();
167 bool friendlyfire = flags & DTFLAG_SAMETEAM;
169 int health, armor, potential_damage;
170 if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
171 else health = ReadShort();
172 if (flags & DTFLAG_NO_ARMOR) armor = 0;
173 else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
174 else armor = ReadShort();
175 if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
176 else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
177 else potential_damage = ReadShort();
180 if (autocvar_cl_damagetext) {
181 if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) {
184 if (autocvar_cl_damagetext_accumulate_range) {
185 for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
186 if (e.instanceOfDamageText && e.m_group == group && e.alpha > autocvar_cl_damagetext_accumulate_alpha_rel * autocvar_cl_damagetext_alpha_start) {
187 DamageText_update(e, location, e.m_healthdamage + health, e.m_armordamage + armor, e.m_potential_damage + potential_damage, deathtype);
192 make_impure(NEW(DamageText, group, location, health, armor, potential_damage, deathtype, friendlyfire));