1 #include "damagetext.qh"
3 #define DAMAGETEXT_PRECISION_MULTIPLIER 128
4 #define DAMAGETEXT_SHORT_LIMIT 256 // the smallest value that we can't send as short - 2^15 (signed short) / DAMAGETEXT_PRECISION_MULTIPLIER
6 const int DTFLAG_SAMETEAM = BIT(0);
7 const int DTFLAG_BIG_HEALTH = BIT(1);
8 const int DTFLAG_BIG_ARMOR = BIT(2);
9 const int DTFLAG_BIG_POTENTIAL = BIT(3);
10 const int DTFLAG_NO_ARMOR = BIT(4);
11 const int DTFLAG_NO_POTENTIAL = BIT(5);
13 REGISTER_MUTATOR(damagetext, true);
17 // no translatable cvar description please
18 AUTOCVAR_SAVE(cl_damagetext, bool, true, "Draw damage dealt where you hit the enemy");
19 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");
20 AUTOCVAR_SAVE(cl_damagetext_format_verbose, bool, false, "{health} shows {potential_health} too when they differ; {total} shows {potential} too when they differ");
21 AUTOCVAR_SAVE(cl_damagetext_format_hide_redundant, bool, false, "hide {armor} if 0; hide {potential} and {potential_health} when same as actual");
22 STATIC_INIT(DamageText_LegacyFormat) {
23 // damagetext used to be off by default and the cvar got saved in people's configs along with the old format
24 // enable damagetext while updating the format for a one time effect
25 if (strstrofs(autocvar_cl_damagetext_format, "{", 0) < 0) {
26 localcmd("\nseta cl_damagetext 1\n");
27 localcmd("\nseta cl_damagetext_format -{total}\n");
30 AUTOCVAR_SAVE(cl_damagetext_color, vector, '1 1 0', "Damage text color");
31 AUTOCVAR_SAVE(cl_damagetext_color_per_weapon, bool, false, "Damage text uses weapon color");
32 AUTOCVAR_SAVE(cl_damagetext_size_min, float, 8, "Damage text font size for small damage");
33 AUTOCVAR_SAVE(cl_damagetext_size_min_damage, float, 0, "How much damage is considered small");
34 AUTOCVAR_SAVE(cl_damagetext_size_max, float, 16, "Damage text font size for large damage");
35 AUTOCVAR_SAVE(cl_damagetext_size_max_damage, float, 100, "How much damage is considered large");
36 AUTOCVAR_SAVE(cl_damagetext_alpha_start, float, 1, "Damage text initial alpha");
37 AUTOCVAR_SAVE(cl_damagetext_alpha_lifetime, float, 3, "Damage text lifetime in seconds");
38 AUTOCVAR_SAVE(cl_damagetext_velocity, vector, '0 0 20', "Damage text move direction");
39 AUTOCVAR_SAVE(cl_damagetext_offset, vector, '0 -40 0', "Damage text offset");
40 AUTOCVAR_SAVE(cl_damagetext_accumulate_range, float, 30, "Damage text spawned within this range is accumulated");
41 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");
42 AUTOCVAR_SAVE(cl_damagetext_friendlyfire, bool, true, "Show damage text for friendlyfire too");
43 AUTOCVAR_SAVE(cl_damagetext_friendlyfire_color, vector, '1 0 0', "Damage text color for friendlyfire");
47 CLASS(DamageText, Object)
48 ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
49 ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
50 ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size_min);
51 ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
52 ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime);
53 ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity);
54 ATTRIB(DamageText, m_group, int, 0);
55 ATTRIB(DamageText, m_friendlyfire, bool, false);
56 ATTRIB(DamageText, m_healthdamage, int, 0);
57 ATTRIB(DamageText, m_armordamage, int, 0);
58 ATTRIB(DamageText, m_potential_damage, int, 0);
59 ATTRIB(DamageText, m_deathtype, int, 0);
60 ATTRIB(DamageText, time_prev, float, time);
61 ATTRIB(DamageText, text, string, string_null);
63 void DamageText_draw2d(DamageText this) {
64 float dt = time - this.time_prev;
65 this.time_prev = time;
66 setorigin(this, this.origin + dt * this.velocity);
67 this.alpha -= dt * this.fade_rate;
68 if (this.alpha < 0) delete(this);
69 vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
70 if (pos.z >= 0 && this.m_size > 0) {
73 if (this.m_friendlyfire) {
74 rgb = this.m_color_friendlyfire;
78 if (autocvar_cl_damagetext_color_per_weapon) {
79 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
80 if (w != WEP_Null) rgb = w.wpcolor;
83 vector drawfontscale_save = drawfontscale;
84 drawfontscale = (this.m_size / autocvar_cl_damagetext_size_max) * '1 1 0';
85 drawcolorcodedstring2_builtin(pos, this.text, autocvar_cl_damagetext_size_max * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL);
86 drawfontscale = drawfontscale_save;
89 ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
91 void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype) {
92 this.m_healthdamage = _health;
93 this.m_armordamage = _armor;
94 this.m_potential_damage = _potential_damage;
95 this.m_deathtype = _deathtype;
96 setorigin(this, _origin);
97 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 string s = autocvar_cl_damagetext_format;
106 s = strreplace("{armor}", (
107 (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
109 : sprintf("%d", armor)
111 s = strreplace("{potential}", (
112 (this.m_potential_damage == this.m_healthdamage + this.m_armordamage && autocvar_cl_damagetext_format_hide_redundant)
114 : sprintf("%d", potential)
116 s = strreplace("{potential_health}", (
117 (this.m_potential_damage == this.m_healthdamage + this.m_armordamage && autocvar_cl_damagetext_format_hide_redundant)
119 : sprintf("%d", potential_health)
122 s = strreplace("{health}", (
123 (health == potential_health || !autocvar_cl_damagetext_format_verbose)
124 ? sprintf("%d", health)
125 : sprintf("%d (%d)", health, potential_health)
127 s = strreplace("{total}", (
128 (total == potential || !autocvar_cl_damagetext_format_verbose)
129 ? sprintf("%d", total)
130 : sprintf("%d (%d)", total, potential)
133 // futureproofing: remove any remaining (unknown) format strings in case we add new ones in the future
134 // so players can use them on new servers and still have working damagetext on old ones
136 int opening_pos = strstrofs(s, "{", 0);
137 if (opening_pos == -1) break;
138 int closing_pos = strstrofs(s, "}", opening_pos);
139 if (closing_pos == -1 || closing_pos <= opening_pos) break;
141 substring(s, 0, opening_pos),
142 substring_range(s, closing_pos + 1, strlen(s))
146 if (this.text) strunzone(this.text);
147 this.text = strzone(s);
149 float size_range = autocvar_cl_damagetext_size_max - autocvar_cl_damagetext_size_min;
150 float damage_range = autocvar_cl_damagetext_size_max_damage - autocvar_cl_damagetext_size_min_damage;
151 float scale_factor = size_range / damage_range;
153 autocvar_cl_damagetext_size_min,
154 (potential - autocvar_cl_damagetext_size_min_damage) * scale_factor + autocvar_cl_damagetext_size_min,
155 autocvar_cl_damagetext_size_max);
158 CONSTRUCTOR(DamageText, int _group, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) {
159 CONSTRUCT(DamageText);
160 this.m_group = _group;
161 this.m_friendlyfire = _friendlyfire;
162 DamageText_update(this, _origin, _health, _armor, _potential_damage, _deathtype);
163 IL_PUSH(g_drawables_2d, this);
166 DESTRUCTOR(DamageText) {
167 if (this.text) strunzone(this.text);
172 REGISTER_NET_TEMP(damagetext)
175 AUTOCVAR(sv_damagetext, int, 2, "<= 0: disabled, >= 1: spectators, >= 2: players, >= 3: all players");
176 #define SV_DAMAGETEXT_DISABLED() (autocvar_sv_damagetext <= 0 /* disabled */)
177 #define SV_DAMAGETEXT_SPECTATORS_ONLY() (autocvar_sv_damagetext >= 1 /* spectators only */)
178 #define SV_DAMAGETEXT_PLAYERS() (autocvar_sv_damagetext >= 2 /* players */)
179 #define SV_DAMAGETEXT_ALL() (autocvar_sv_damagetext >= 3 /* all players */)
180 MUTATOR_HOOKFUNCTION(damagetext, PlayerDamaged) {
181 if (SV_DAMAGETEXT_DISABLED()) return;
182 const entity attacker = M_ARGV(0, entity);
183 const entity hit = M_ARGV(1, entity); if (hit == attacker) return;
184 const float health = M_ARGV(2, float);
185 const float armor = M_ARGV(3, float);
186 const int deathtype = M_ARGV(5, int);
187 const float potential_damage = M_ARGV(6, float);
188 const vector location = hit.origin;
189 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
191 (SV_DAMAGETEXT_ALL()) ||
192 (SV_DAMAGETEXT_PLAYERS() && it == attacker) ||
193 (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_SPEC(it) && it.enemy == attacker) ||
194 (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_OBSERVER(it))
197 if (SAME_TEAM(hit, attacker)) flags |= DTFLAG_SAMETEAM;
198 if (health >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_HEALTH;
199 if (armor >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_ARMOR;
200 if (potential_damage >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_POTENTIAL;
201 if (!armor) flags |= DTFLAG_NO_ARMOR;
202 if (fabs((armor + health) - potential_damage) < 0.0001) flags |= DTFLAG_NO_POTENTIAL;
205 WriteHeader(MSG_ONE, damagetext);
206 WriteEntity(MSG_ONE, hit);
207 WriteCoord(MSG_ONE, location.x);
208 WriteCoord(MSG_ONE, location.y);
209 WriteCoord(MSG_ONE, location.z);
210 WriteInt24_t(MSG_ONE, deathtype);
211 WriteByte(MSG_ONE, flags);
213 // we need to send a few decimal places to minimize errors when accumulating damage
214 // sending them multiplied saves bandwidth compared to using WriteCoord,
215 // however if the multiplied damage would be too much for (signed) short, we send an int24
216 if (flags & DTFLAG_BIG_HEALTH) WriteInt24_t(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER);
217 else WriteShort(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER);
218 if (!(flags & DTFLAG_NO_ARMOR))
220 if (flags & DTFLAG_BIG_ARMOR) WriteInt24_t(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER);
221 else WriteShort(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER);
223 if (!(flags & DTFLAG_NO_POTENTIAL))
225 if (flags & DTFLAG_BIG_POTENTIAL) WriteInt24_t(MSG_ONE, potential_damage * DAMAGETEXT_PRECISION_MULTIPLIER);
226 else WriteShort(MSG_ONE, potential_damage * DAMAGETEXT_PRECISION_MULTIPLIER);
234 NET_HANDLE(damagetext, bool isNew)
236 int group = ReadShort();
237 vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
238 int deathtype = ReadInt24_t();
239 int flags = ReadByte();
240 bool friendlyfire = flags & DTFLAG_SAMETEAM;
242 int health, armor, potential_damage;
243 if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
244 else health = ReadShort();
245 if (flags & DTFLAG_NO_ARMOR) armor = 0;
246 else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
247 else armor = ReadShort();
248 if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
249 else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
250 else potential_damage = ReadShort();
253 if (autocvar_cl_damagetext) {
254 if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) {
257 if (autocvar_cl_damagetext_accumulate_range) {
258 for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
259 if (e.instanceOfDamageText && e.m_group == group && e.alpha > autocvar_cl_damagetext_accumulate_alpha_rel * autocvar_cl_damagetext_alpha_start) {
260 DamageText_update(e, location, e.m_healthdamage + health, e.m_armordamage + armor, e.m_potential_damage + potential_damage, deathtype);
265 make_impure(NEW(DamageText, group, location, health, armor, potential_damage, deathtype, friendlyfire));
272 #include <menu/gamesettings.qh>
274 CLASS(XonoticDamageTextSettings, XonoticTab)
275 REGISTER_SETTINGS(damagetext, NEW(XonoticDamageTextSettings));
276 ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text"));
277 ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9);
278 ATTRIB(XonoticDamageTextSettings, rows, float, 15.5);
279 ATTRIB(XonoticDamageTextSettings, columns, float, 5);
280 INIT(XonoticDamageTextSettings) { this.configureDialog(this); }
281 METHOD(XonoticDamageTextSettings, showNotify, void(entity this)) { loadAllCvars(this); }
282 METHOD(XonoticDamageTextSettings, fill, void(entity this))
285 this.gotoRC(this, 0, 1); this.setFirstColumn(this, this.currentColumn);
286 this.TD(this, 1, 3, makeXonoticCheckBox(0, "cl_damagetext", _("Draw damage numbers")));
288 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Font size minimum:")));
289 setDependent(e, "cl_damagetext", 1, 1);
290 this.TD(this, 1, 2, e = makeXonoticSlider(0, 50, 1, "cl_damagetext_size_min"));
291 setDependent(e, "cl_damagetext", 1, 1);
293 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Font size maximum:")));
294 setDependent(e, "cl_damagetext", 1, 1);
295 this.TD(this, 1, 2, e = makeXonoticSlider(0, 50, 1, "cl_damagetext_size_max"));
296 setDependent(e, "cl_damagetext", 1, 1);
298 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Accumulate range:")));
299 setDependent(e, "cl_damagetext", 1, 1);
300 this.TD(this, 1, 2, e = makeXonoticSlider(0, 500, 1, "cl_damagetext_accumulate_range"));
301 setDependent(e, "cl_damagetext", 1, 1);
303 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Lifetime:")));
304 setDependent(e, "cl_damagetext", 1, 1);
305 this.TD(this, 1, 2, e = makeXonoticSlider(0, 10, 1, "cl_damagetext_alpha_lifetime"));
306 setDependent(e, "cl_damagetext", 1, 1);
308 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
309 setDependent(e, "cl_damagetext", 1, 1);
310 this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_color", "cl_damagetext_color"));
311 setDependent(e, "cl_damagetext", 1, 1);
315 this.TD(this, 1, 3, e = makeXonoticCheckBox(0, "cl_damagetext_friendlyfire", _("Draw damage numbers for friendly fire")));
316 setDependent(e, "cl_damagetext", 1, 1);
318 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
319 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
320 this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_friendlyfire_color", "cl_damagetext_friendlyfire_color"));
321 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
324 ENDCLASS(XonoticDamageTextSettings)