]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext/damagetext.qc
damagetext: hide redundant
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / damagetext / damagetext.qc
1 #include "damagetext.qh"
2
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
5
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);
12
13 REGISTER_MUTATOR(damagetext, true);
14
15 #if defined(CSQC) || defined(MENUQC)
16 // no translatable cvar description please
17 AUTOCVAR_SAVE(cl_damagetext,                        bool,   true,       "Draw damage dealt where you hit the enemy");
18 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");
19 AUTOCVAR_SAVE(cl_damagetext_format_verbose,         bool,   false,      "{health} shows {potential_health} too when they differ; {total} shows {potential} too when they differ");
20 AUTOCVAR_SAVE(cl_damagetext_format_hide_redundant,  bool,   false,      "hide {armor} if 0 and potential when same as actual"); // TODO cfg
21 STATIC_INIT(DamageText_LegacyFormat) {
22     if (strstrofs(autocvar_cl_damagetext_format, "{", 0) < 0) autocvar_cl_damagetext_format = "-{total}";
23 }
24 AUTOCVAR_SAVE(cl_damagetext_color,                  vector, '1 1 0',    "Damage text color");
25 AUTOCVAR_SAVE(cl_damagetext_color_per_weapon,       bool,   false,      "Damage text uses weapon color");
26 AUTOCVAR_SAVE(cl_damagetext_size,                   float,  8,          "Damage text font size");
27 AUTOCVAR_SAVE(cl_damagetext_alpha_start,            float,  1,          "Damage text initial alpha");
28 AUTOCVAR_SAVE(cl_damagetext_alpha_lifetime,         float,  3,          "Damage text lifetime in seconds");
29 AUTOCVAR_SAVE(cl_damagetext_velocity,               vector, '0 0 20',   "Damage text move direction");
30 AUTOCVAR_SAVE(cl_damagetext_offset,                 vector, '0 -40 0',  "Damage text offset");
31 AUTOCVAR_SAVE(cl_damagetext_accumulate_range,       float,  30,         "Damage text spawned within this range is accumulated");
32 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");
33 AUTOCVAR_SAVE(cl_damagetext_friendlyfire,           bool,   true,       "Show damage text for friendlyfire too");
34 AUTOCVAR_SAVE(cl_damagetext_friendlyfire_color,     vector, '1 0 0',    "Damage text color for friendlyfire");
35 #endif
36
37 #ifdef CSQC
38 CLASS(DamageText, Object)
39     ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
40     ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
41     ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size);
42     ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
43     ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime);
44     ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity);
45     ATTRIB(DamageText, m_group, int, 0);
46     ATTRIB(DamageText, m_friendlyfire, bool, false);
47     ATTRIB(DamageText, m_healthdamage, int, 0);
48     ATTRIB(DamageText, m_armordamage, int, 0);
49     ATTRIB(DamageText, m_potential_damage, int, 0);
50     ATTRIB(DamageText, m_deathtype, int, 0);
51     ATTRIB(DamageText, time_prev, float, time);
52
53     void DamageText_draw2d(DamageText this) {
54         float dt = time - this.time_prev;
55         this.time_prev = time;
56         setorigin(this, this.origin + dt * this.velocity);
57         this.alpha -= dt * this.fade_rate;
58         if (this.alpha < 0) delete(this);
59         vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
60         if (pos.z >= 0 && this.m_size > 0) {
61             pos.z = 0;
62             vector rgb;
63             if (this.m_friendlyfire) {
64                 rgb = this.m_color_friendlyfire;
65             }
66             else {
67                 rgb = this.m_color;
68             }
69             if (autocvar_cl_damagetext_color_per_weapon) {
70                 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
71                 if (w != WEP_Null) rgb = w.wpcolor;
72             }
73
74             int health = rint(this.m_healthdamage / DAMAGETEXT_PRECISION_MULTIPLIER);
75             int armor = rint(this.m_armordamage / DAMAGETEXT_PRECISION_MULTIPLIER);
76             int total = rint((this.m_healthdamage + this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
77             int potential = rint(this.m_potential_damage / DAMAGETEXT_PRECISION_MULTIPLIER);
78             int potential_health = rint((this.m_potential_damage - this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
79
80             string s = autocvar_cl_damagetext_format; // TODO move to update?
81             s = strreplace("{armor}", (
82                 (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
83                     ? ""
84                     : sprintf("%d", armor)
85                 ), s);
86             s = strreplace("{potential}", (
87                 (this.m_potential_damage == this.m_healthdamage + this.m_armordamage && autocvar_cl_damagetext_format_hide_redundant)
88                     ? ""
89                     : sprintf("%d", potential)
90                 ), s);
91             s = strreplace("{potential_health}", (
92                 (this.m_potential_damage - this.m_armordamage == this.m_healthdamage && autocvar_cl_damagetext_format_hide_redundant)
93                     ? ""
94                     : sprintf("%d", potential_health)
95                 ), s);
96
97             s = strreplace("{health}", (
98                 (health == potential_health || !autocvar_cl_damagetext_format_verbose)
99                     ? sprintf("%d",      health)
100                     : sprintf("%d (%d)", health, potential_health)
101                 ), s);
102             s = strreplace("{total}", (
103                 (total == potential || !autocvar_cl_damagetext_format_verbose)
104                     ? sprintf("%d",      total)
105                     : sprintf("%d (%d)", total, potential)
106                 ), s);
107             drawcolorcodedstring2_builtin(pos, s, this.m_size * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL);
108         }
109     }
110     ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
111
112     void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype) {
113         this.m_healthdamage = _health;
114         this.m_armordamage = _armor;
115         this.m_potential_damage = _potential_damage;
116         this.m_deathtype = _deathtype;
117         setorigin(this, _origin);
118         this.alpha = autocvar_cl_damagetext_alpha_start;
119     }
120
121     CONSTRUCTOR(DamageText, int _group, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) {
122         CONSTRUCT(DamageText);
123         this.m_group = _group;
124         this.m_friendlyfire = _friendlyfire;
125         DamageText_update(this, _origin, _health, _armor, _potential_damage, _deathtype);
126                 IL_PUSH(g_drawables_2d, this);
127     }
128 ENDCLASS(DamageText)
129 #endif
130
131 REGISTER_NET_TEMP(damagetext)
132
133 #ifdef SVQC
134 AUTOCVAR(sv_damagetext, int, 2, _("<= 0: disabled, >= 1: spectators, >= 2: players, >= 3: all players"));
135 #define SV_DAMAGETEXT_DISABLED()        (autocvar_sv_damagetext <= 0 /* disabled */)
136 #define SV_DAMAGETEXT_SPECTATORS_ONLY() (autocvar_sv_damagetext >= 1 /* spectators only */)
137 #define SV_DAMAGETEXT_PLAYERS()         (autocvar_sv_damagetext >= 2 /* players */)
138 #define SV_DAMAGETEXT_ALL()             (autocvar_sv_damagetext >= 3 /* all players */)
139 MUTATOR_HOOKFUNCTION(damagetext, PlayerDamaged) {
140     if (SV_DAMAGETEXT_DISABLED()) return;
141     const entity attacker = M_ARGV(0, entity);
142     const entity hit = M_ARGV(1, entity); if (hit == attacker) return;
143     const float health = M_ARGV(2, float);
144     const float armor = M_ARGV(3, float);
145     const int deathtype = M_ARGV(5, int);
146     const float potential_damage = M_ARGV(6, float);
147     const vector location = hit.origin;
148     FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
149         if (
150             (SV_DAMAGETEXT_ALL()) ||
151             (SV_DAMAGETEXT_PLAYERS() && it == attacker) ||
152             (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_SPEC(it) && it.enemy == attacker) ||
153             (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_OBSERVER(it))
154         ) {
155             int flags = 0;
156             if (SAME_TEAM(hit, attacker)) flags |= DTFLAG_SAMETEAM;
157             if (health >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_HEALTH;
158             if (armor >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_ARMOR;
159             if (potential_damage >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_POTENTIAL;
160             if (!armor) flags |= DTFLAG_NO_ARMOR;
161             if (fabs((armor + health) - potential_damage) < 0.0001) flags |= DTFLAG_NO_POTENTIAL;
162
163             msg_entity = it;
164             WriteHeader(MSG_ONE, damagetext);
165             WriteEntity(MSG_ONE, hit);
166             WriteCoord(MSG_ONE, location.x);
167             WriteCoord(MSG_ONE, location.y);
168             WriteCoord(MSG_ONE, location.z);
169             WriteInt24_t(MSG_ONE, deathtype);
170             WriteByte(MSG_ONE, flags);
171
172             // we need to send a few decimal places to minimize errors when accumulating damage
173             // sending them multiplied saves bandwidth compared to using WriteCoord,
174             // however if the multiplied damage would be too much for (signed) short, we send an int24
175             if (flags & DTFLAG_BIG_HEALTH) WriteInt24_t(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER);
176             else WriteShort(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER);
177             if (!(flags & DTFLAG_NO_ARMOR))
178             {
179                 if (flags & DTFLAG_BIG_ARMOR) WriteInt24_t(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER);
180                 else WriteShort(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER);
181             }
182             if (!(flags & DTFLAG_NO_POTENTIAL))
183             {
184                 if (flags & DTFLAG_BIG_POTENTIAL) WriteInt24_t(MSG_ONE, potential_damage * DAMAGETEXT_PRECISION_MULTIPLIER);
185                 else WriteShort(MSG_ONE, potential_damage * DAMAGETEXT_PRECISION_MULTIPLIER);
186                         }
187         }
188     ));
189 }
190 #endif
191
192 #ifdef CSQC
193 NET_HANDLE(damagetext, bool isNew)
194 {
195     int group = ReadShort();
196     vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
197     int deathtype = ReadInt24_t();
198     int flags = ReadByte();
199     bool friendlyfire = flags & DTFLAG_SAMETEAM;
200
201     int health, armor, potential_damage;
202     if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
203     else health = ReadShort();
204     if (flags & DTFLAG_NO_ARMOR) armor = 0;
205     else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
206     else armor = ReadShort();
207     if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
208     else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
209     else potential_damage = ReadShort();
210
211     return = true;
212     if (autocvar_cl_damagetext) {
213         if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) {
214             return;
215         }
216         if (autocvar_cl_damagetext_accumulate_range) {
217             for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
218                 if (e.instanceOfDamageText && e.m_group == group && e.alpha > autocvar_cl_damagetext_accumulate_alpha_rel * autocvar_cl_damagetext_alpha_start) {
219                     DamageText_update(e, location, e.m_healthdamage + health, e.m_armordamage + armor, e.m_potential_damage + potential_damage, deathtype);
220                     return;
221                 }
222             }
223         }
224         make_impure(NEW(DamageText, group, location, health, armor, potential_damage, deathtype, friendlyfire));
225     }
226 }
227 #endif
228
229 #ifdef MENUQC
230
231 #include <menu/gamesettings.qh>
232
233 CLASS(XonoticDamageTextSettings, XonoticTab)
234     REGISTER_SETTINGS(damagetext, NEW(XonoticDamageTextSettings));
235     ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text"));
236     ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9);
237     ATTRIB(XonoticDamageTextSettings, rows, float, 15.5);
238     ATTRIB(XonoticDamageTextSettings, columns, float, 5);
239     INIT(XonoticDamageTextSettings) { this.configureDialog(this); }
240     METHOD(XonoticDamageTextSettings, showNotify, void(entity this)) { loadAllCvars(this); }
241     METHOD(XonoticDamageTextSettings, fill, void(entity this))
242     {
243         entity e;
244         this.gotoRC(this, 0, 1); this.setFirstColumn(this, this.currentColumn);
245             this.TD(this, 1, 3, makeXonoticCheckBox(0, "cl_damagetext", _("Draw damage numbers")));
246         this.TR(this);
247             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Font size:")));
248                 setDependent(e, "cl_damagetext", 1, 1);
249             this.TD(this, 1, 2, e = makeXonoticSlider(0, 50, 1, "cl_damagetext_size"));
250                 setDependent(e, "cl_damagetext", 1, 1);
251         this.TR(this);
252             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Accumulate range:")));
253                 setDependent(e, "cl_damagetext", 1, 1);
254             this.TD(this, 1, 2, e = makeXonoticSlider(0, 500, 1, "cl_damagetext_accumulate_range"));
255                 setDependent(e, "cl_damagetext", 1, 1);
256         this.TR(this);
257             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Lifetime:")));
258                 setDependent(e, "cl_damagetext", 1, 1);
259             this.TD(this, 1, 2, e = makeXonoticSlider(0, 10, 1, "cl_damagetext_alpha_lifetime"));
260                 setDependent(e, "cl_damagetext", 1, 1);
261         this.TR(this);
262             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
263                 setDependent(e, "cl_damagetext", 1, 1);
264             this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_color", "cl_damagetext_color"));
265                 setDependent(e, "cl_damagetext", 1, 1);
266         this.TR(this);
267         this.TR(this);
268         // friendly fire
269             this.TD(this, 1, 3, e = makeXonoticCheckBox(0, "cl_damagetext_friendlyfire", _("Draw damage numbers for friendly fire")));
270                 setDependent(e, "cl_damagetext", 1, 1);
271         this.TR(this);
272             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
273                 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
274             this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_friendlyfire_color", "cl_damagetext_friendlyfire_color"));
275                 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
276         this.TR(this);
277     }
278 ENDCLASS(XonoticDamageTextSettings)
279 #endif