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", this.m_damage), s);
61 s = strreplace("{armor}", sprintf("%d", this.m_armordamage), s);
62 s = strreplace("{total}", sprintf("%d", this.m_damage + this.m_armordamage), 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);
111 WriteShort(MSG_ONE, rint(health));
112 WriteShort(MSG_ONE, rint(armor));
113 WriteEntity(MSG_ONE, hit);
114 WriteCoord(MSG_ONE, location.x);
115 WriteCoord(MSG_ONE, location.y);
116 WriteCoord(MSG_ONE, location.z);
117 WriteInt24_t(MSG_ONE, deathtype);
118 WriteByte(MSG_ONE, SAME_TEAM(hit, attacker));
125 NET_HANDLE(damagetext, bool isNew)
127 int health = ReadShort();
128 int armor = ReadShort();
129 int group = ReadShort();
130 vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
131 int deathtype = ReadInt24_t();
132 bool friendlyfire = ReadByte();
134 if (autocvar_cl_damagetext) {
135 if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) {
138 if (autocvar_cl_damagetext_accumulate_range) {
139 for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
140 if (e.instanceOfDamageText && e.m_group == group) {
141 DamageText_update(e, location, e.m_damage + health, e.m_armordamage + armor, deathtype);
146 make_impure(NEW(DamageText, group, location, health, armor, deathtype, friendlyfire));
153 #include <menu/gamesettings.qh>
155 CLASS(XonoticDamageTextSettings, XonoticTab)
156 REGISTER_SETTINGS(damagetext, NEW(XonoticDamageTextSettings));
157 ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text"));
158 ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9);
159 ATTRIB(XonoticDamageTextSettings, rows, float, 15.5);
160 ATTRIB(XonoticDamageTextSettings, columns, float, 5);
161 INIT(XonoticDamageTextSettings) { this.configureDialog(this); }
162 METHOD(XonoticDamageTextSettings, showNotify, void(entity this)) { loadAllCvars(this); }
163 METHOD(XonoticDamageTextSettings, fill, void(entity this))
166 this.gotoRC(this, 0, 1); this.setFirstColumn(this, this.currentColumn);
167 this.TD(this, 1, 3, makeXonoticCheckBox(0, "cl_damagetext", _("Draw damage numbers")));
169 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Font size:")));
170 setDependent(e, "cl_damagetext", 1, 1);
171 this.TD(this, 1, 2, e = makeXonoticSlider(0, 50, 1, "cl_damagetext_size"));
172 setDependent(e, "cl_damagetext", 1, 1);
174 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Accumulate range:")));
175 setDependent(e, "cl_damagetext", 1, 1);
176 this.TD(this, 1, 2, e = makeXonoticSlider(0, 500, 1, "cl_damagetext_accumulate_range"));
177 setDependent(e, "cl_damagetext", 1, 1);
179 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Lifetime:")));
180 setDependent(e, "cl_damagetext", 1, 1);
181 this.TD(this, 1, 2, e = makeXonoticSlider(0, 10, 1, "cl_damagetext_alpha_lifetime"));
182 setDependent(e, "cl_damagetext", 1, 1);
184 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
185 setDependent(e, "cl_damagetext", 1, 1);
186 this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_color", "cl_damagetext_color"));
187 setDependent(e, "cl_damagetext", 1, 1);
190 this.TD(this, 1, 3, e = makeXonoticCheckBox(0, "cl_damagetext_friendlyfire", _("Draw damage numbers for friendly fire")));
191 setDependent(e, "cl_damagetext", 1, 1);
193 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
194 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
195 this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_friendlyfire_color", "cl_damagetext_friendlyfire_color"));
196 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
199 ENDCLASS(XonoticDamageTextSettings)