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