1 #include "damagetext.qh"
3 REGISTER_MUTATOR(damagetext, true);
5 #if defined(CSQC) || defined(MENUQC)
6 // no translatable cvar description please
7 AUTOCVAR_SAVE(cl_damagetext, bool, true, "Draw damage dealt where you hit the enemy");
8 AUTOCVAR_SAVE(cl_damagetext_format, string, "-{total}", "How to format the damage text. {health}, {armor}, {total}");
9 STATIC_INIT(DamageText_LegacyFormat) {
10 if (strstrofs(autocvar_cl_damagetext_format, "{", 0) < 0) autocvar_cl_damagetext_format = "-{total}";
12 AUTOCVAR_SAVE(cl_damagetext_color, vector, '1 1 0', "Damage text color");
13 AUTOCVAR_SAVE(cl_damagetext_color_per_weapon, bool, false, "Damage text uses weapon color");
14 AUTOCVAR_SAVE(cl_damagetext_size, float, 8, "Damage text font size");
15 AUTOCVAR_SAVE(cl_damagetext_alpha_start, float, 1, "Damage text initial alpha");
16 AUTOCVAR_SAVE(cl_damagetext_alpha_lifetime, float, 3, "Damage text lifetime in seconds");
17 AUTOCVAR_SAVE(cl_damagetext_velocity, vector, '0 0 20', "Damage text move direction");
18 AUTOCVAR_SAVE(cl_damagetext_offset, vector, '0 -40 0', "Damage text offset");
19 AUTOCVAR_SAVE(cl_damagetext_accumulate_range, float, 30, "Damage text spawned within this range is accumulated");
20 AUTOCVAR_SAVE(cl_damagetext_friendlyfire, bool, true, "Show damage text for friendlyfire too");
21 AUTOCVAR_SAVE(cl_damagetext_friendlyfire_color, vector, '1 0 0', "Damage text color for friendlyfire");
25 CLASS(DamageText, Object)
26 ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
27 ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
28 ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size);
29 ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
30 ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime);
31 ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity);
32 ATTRIB(DamageText, m_group, int, 0);
33 ATTRIB(DamageText, m_friendlyfire, bool, false);
34 ATTRIB(DamageText, m_damage, int, 0);
35 ATTRIB(DamageText, m_armordamage, int, 0);
36 ATTRIB(DamageText, m_deathtype, int, 0);
37 ATTRIB(DamageText, time_prev, float, time);
39 void DamageText_draw2d(DamageText this) {
40 float dt = time - this.time_prev;
41 this.time_prev = time;
42 setorigin(this, this.origin + dt * this.velocity);
43 this.alpha -= dt * this.fade_rate;
44 if (this.alpha < 0) delete(this);
45 vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
46 if (pos.z >= 0 && this.m_size > 0) {
49 if (this.m_friendlyfire) {
50 rgb = this.m_color_friendlyfire;
55 if (autocvar_cl_damagetext_color_per_weapon) {
56 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
57 if (w != WEP_Null) rgb = w.wpcolor;
59 string s = autocvar_cl_damagetext_format;
60 s = strreplace("{health}", sprintf("%d", rint(this.m_damage / 100)), s);
61 s = strreplace("{armor}", sprintf("%d", rint(this.m_armordamage / 100)), s);
62 s = strreplace("{total}", sprintf("%d", rint((this.m_damage + this.m_armordamage) / 100)), s);
63 drawcolorcodedstring2_builtin(pos, s, this.m_size * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL);
66 ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
68 void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _deathtype) {
69 this.m_damage = _health;
70 this.m_armordamage = _armor;
71 this.m_deathtype = _deathtype;
72 setorigin(this, _origin);
76 CONSTRUCTOR(DamageText, int _group, vector _origin, int _health, int _armor, int _deathtype, bool _friendlyfire) {
77 CONSTRUCT(DamageText);
78 this.m_group = _group;
79 this.m_friendlyfire = _friendlyfire;
80 DamageText_update(this, _origin, _health, _armor, _deathtype);
81 IL_PUSH(g_drawables_2d, this);
86 REGISTER_NET_TEMP(damagetext)
89 AUTOCVAR(sv_damagetext, int, 2, _("<= 0: disabled, >= 1: spectators, >= 2: players, >= 3: all players"));
90 #define SV_DAMAGETEXT_DISABLED() (autocvar_sv_damagetext <= 0 /* disabled */)
91 #define SV_DAMAGETEXT_SPECTATORS_ONLY() (autocvar_sv_damagetext >= 1 /* spectators only */)
92 #define SV_DAMAGETEXT_PLAYERS() (autocvar_sv_damagetext >= 2 /* players */)
93 #define SV_DAMAGETEXT_ALL() (autocvar_sv_damagetext >= 3 /* all players */)
94 MUTATOR_HOOKFUNCTION(damagetext, PlayerDamaged) {
95 if (SV_DAMAGETEXT_DISABLED()) return;
96 const entity attacker = M_ARGV(0, entity);
97 const entity hit = M_ARGV(1, entity); if (hit == attacker) return;
98 const int health = M_ARGV(2, int);
99 const int armor = M_ARGV(3, int);
100 const int deathtype = M_ARGV(5, int);
101 const vector location = hit.origin;
102 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
104 (SV_DAMAGETEXT_ALL()) ||
105 (SV_DAMAGETEXT_PLAYERS() && it == attacker) ||
106 (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_SPEC(it) && it.enemy == attacker) ||
107 (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_OBSERVER(it))
110 WriteHeader(MSG_ONE, damagetext);
112 // we need a few decimal places to avoid errors when accumulating damage
113 // sending them this way saves bandwidth compared to WriteCoord
114 WriteShort(MSG_ONE, health * 100);
115 WriteShort(MSG_ONE, armor * 100);
117 WriteEntity(MSG_ONE, hit);
118 WriteCoord(MSG_ONE, location.x);
119 WriteCoord(MSG_ONE, location.y);
120 WriteCoord(MSG_ONE, location.z);
121 WriteInt24_t(MSG_ONE, deathtype);
122 WriteByte(MSG_ONE, SAME_TEAM(hit, attacker));
129 NET_HANDLE(damagetext, bool isNew)
131 int health = ReadShort();
132 int armor = ReadShort();
133 int group = ReadShort();
134 vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
135 int deathtype = ReadInt24_t();
136 bool friendlyfire = ReadByte();
138 if (autocvar_cl_damagetext) {
139 if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) {
142 if (autocvar_cl_damagetext_accumulate_range) {
143 for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
144 if (e.instanceOfDamageText && e.m_group == group) {
145 DamageText_update(e, location, e.m_damage + health, e.m_armordamage + armor, deathtype);
150 make_impure(NEW(DamageText, group, location, health, armor, deathtype, friendlyfire));
157 #include <menu/gamesettings.qh>
159 CLASS(XonoticDamageTextSettings, XonoticTab)
160 REGISTER_SETTINGS(damagetext, NEW(XonoticDamageTextSettings));
161 ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text"));
162 ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9);
163 ATTRIB(XonoticDamageTextSettings, rows, float, 15.5);
164 ATTRIB(XonoticDamageTextSettings, columns, float, 5);
165 INIT(XonoticDamageTextSettings) { this.configureDialog(this); }
166 METHOD(XonoticDamageTextSettings, showNotify, void(entity this)) { loadAllCvars(this); }
167 METHOD(XonoticDamageTextSettings, fill, void(entity this))
170 this.gotoRC(this, 0, 1); this.setFirstColumn(this, this.currentColumn);
171 this.TD(this, 1, 3, makeXonoticCheckBox(0, "cl_damagetext", _("Draw damage numbers")));
173 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Font size:")));
174 setDependent(e, "cl_damagetext", 1, 1);
175 this.TD(this, 1, 2, e = makeXonoticSlider(0, 50, 1, "cl_damagetext_size"));
176 setDependent(e, "cl_damagetext", 1, 1);
178 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Accumulate range:")));
179 setDependent(e, "cl_damagetext", 1, 1);
180 this.TD(this, 1, 2, e = makeXonoticSlider(0, 500, 1, "cl_damagetext_accumulate_range"));
181 setDependent(e, "cl_damagetext", 1, 1);
183 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Lifetime:")));
184 setDependent(e, "cl_damagetext", 1, 1);
185 this.TD(this, 1, 2, e = makeXonoticSlider(0, 10, 1, "cl_damagetext_alpha_lifetime"));
186 setDependent(e, "cl_damagetext", 1, 1);
188 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
189 setDependent(e, "cl_damagetext", 1, 1);
190 this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_color", "cl_damagetext_color"));
191 setDependent(e, "cl_damagetext", 1, 1);
194 this.TD(this, 1, 3, e = makeXonoticCheckBox(0, "cl_damagetext_friendlyfire", _("Draw damage numbers for friendly fire")));
195 setDependent(e, "cl_damagetext", 1, 1);
197 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color (Friendly Fire):")));
198 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
199 this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_friendlyfire_color", "cl_damagetext_friendlyfire_color"));
200 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
203 ENDCLASS(XonoticDamageTextSettings)