]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext/damagetext.qc
fix unzoning
[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     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     ATTRIB(DamageText, text, string, string_null);
53
54     void DamageText_draw2d(DamageText this) {
55         float dt = time - this.time_prev;
56         this.time_prev = time;
57         setorigin(this, this.origin + dt * this.velocity);
58         this.alpha -= dt * this.fade_rate;
59         if (this.alpha < 0) delete(this);
60         vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
61         if (pos.z >= 0 && this.m_size > 0) {
62             pos.z = 0;
63             vector rgb;
64             if (this.m_friendlyfire) {
65                 rgb = this.m_color_friendlyfire;
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             drawcolorcodedstring2_builtin(pos, this.text, this.m_size * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL);
75         }
76     }
77     ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
78
79     void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype) {
80         this.m_healthdamage = _health;
81         this.m_armordamage = _armor;
82         this.m_potential_damage = _potential_damage;
83         this.m_deathtype = _deathtype;
84         setorigin(this, _origin);
85         this.alpha = autocvar_cl_damagetext_alpha_start;
86
87         int health = rint(this.m_healthdamage / DAMAGETEXT_PRECISION_MULTIPLIER);
88         int armor = rint(this.m_armordamage / DAMAGETEXT_PRECISION_MULTIPLIER);
89         int total = rint((this.m_healthdamage + this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
90         int potential = rint(this.m_potential_damage / DAMAGETEXT_PRECISION_MULTIPLIER);
91         int potential_health = rint((this.m_potential_damage - this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
92
93         string s = autocvar_cl_damagetext_format;
94         s = strreplace("{armor}", (
95             (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
96                 ? ""
97                 : sprintf("%d", armor)
98             ), s);
99         s = strreplace("{potential}", (
100             (this.m_potential_damage == this.m_healthdamage + this.m_armordamage && autocvar_cl_damagetext_format_hide_redundant)
101                 ? ""
102                 : sprintf("%d", potential)
103             ), s);
104         s = strreplace("{potential_health}", (
105             (this.m_potential_damage - this.m_armordamage == this.m_healthdamage && autocvar_cl_damagetext_format_hide_redundant)
106                 ? ""
107                 : sprintf("%d", potential_health)
108             ), s);
109
110         s = strreplace("{health}", (
111             (health == potential_health || !autocvar_cl_damagetext_format_verbose)
112                 ? sprintf("%d",      health)
113                 : sprintf("%d (%d)", health, potential_health)
114             ), s);
115         s = strreplace("{total}", (
116             (total == potential || !autocvar_cl_damagetext_format_verbose)
117                 ? sprintf("%d",      total)
118                 : sprintf("%d (%d)", total, potential)
119             ), s);
120         if (this.text) strunzone(this.text);
121         this.text = strzone(s);
122     }
123
124     CONSTRUCTOR(DamageText, int _group, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) {
125         CONSTRUCT(DamageText);
126         this.m_group = _group;
127         this.m_friendlyfire = _friendlyfire;
128         DamageText_update(this, _origin, _health, _armor, _potential_damage, _deathtype);
129                 IL_PUSH(g_drawables_2d, this);
130     }
131
132     DESTRUCTOR(DamageText) {
133         if (this.text) strunzone(this.text);
134     }
135 ENDCLASS(DamageText)
136 #endif
137
138 REGISTER_NET_TEMP(damagetext)
139
140 #ifdef SVQC
141 AUTOCVAR(sv_damagetext, int, 2, _("<= 0: disabled, >= 1: spectators, >= 2: players, >= 3: all players"));
142 #define SV_DAMAGETEXT_DISABLED()        (autocvar_sv_damagetext <= 0 /* disabled */)
143 #define SV_DAMAGETEXT_SPECTATORS_ONLY() (autocvar_sv_damagetext >= 1 /* spectators only */)
144 #define SV_DAMAGETEXT_PLAYERS()         (autocvar_sv_damagetext >= 2 /* players */)
145 #define SV_DAMAGETEXT_ALL()             (autocvar_sv_damagetext >= 3 /* all players */)
146 MUTATOR_HOOKFUNCTION(damagetext, PlayerDamaged) {
147     if (SV_DAMAGETEXT_DISABLED()) return;
148     const entity attacker = M_ARGV(0, entity);
149     const entity hit = M_ARGV(1, entity); if (hit == attacker) return;
150     const float health = M_ARGV(2, float);
151     const float armor = M_ARGV(3, float);
152     const int deathtype = M_ARGV(5, int);
153     const float potential_damage = M_ARGV(6, float);
154     const vector location = hit.origin;
155     FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
156         if (
157             (SV_DAMAGETEXT_ALL()) ||
158             (SV_DAMAGETEXT_PLAYERS() && it == attacker) ||
159             (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_SPEC(it) && it.enemy == attacker) ||
160             (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_OBSERVER(it))
161         ) {
162             int flags = 0;
163             if (SAME_TEAM(hit, attacker)) flags |= DTFLAG_SAMETEAM;
164             if (health >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_HEALTH;
165             if (armor >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_ARMOR;
166             if (potential_damage >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_POTENTIAL;
167             if (!armor) flags |= DTFLAG_NO_ARMOR;
168             if (fabs((armor + health) - potential_damage) < 0.0001) flags |= DTFLAG_NO_POTENTIAL;
169
170             msg_entity = it;
171             WriteHeader(MSG_ONE, damagetext);
172             WriteEntity(MSG_ONE, hit);
173             WriteCoord(MSG_ONE, location.x);
174             WriteCoord(MSG_ONE, location.y);
175             WriteCoord(MSG_ONE, location.z);
176             WriteInt24_t(MSG_ONE, deathtype);
177             WriteByte(MSG_ONE, flags);
178
179             // we need to send a few decimal places to minimize errors when accumulating damage
180             // sending them multiplied saves bandwidth compared to using WriteCoord,
181             // however if the multiplied damage would be too much for (signed) short, we send an int24
182             if (flags & DTFLAG_BIG_HEALTH) WriteInt24_t(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER);
183             else WriteShort(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER);
184             if (!(flags & DTFLAG_NO_ARMOR))
185             {
186                 if (flags & DTFLAG_BIG_ARMOR) WriteInt24_t(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER);
187                 else WriteShort(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER);
188             }
189             if (!(flags & DTFLAG_NO_POTENTIAL))
190             {
191                 if (flags & DTFLAG_BIG_POTENTIAL) WriteInt24_t(MSG_ONE, potential_damage * DAMAGETEXT_PRECISION_MULTIPLIER);
192                 else WriteShort(MSG_ONE, potential_damage * DAMAGETEXT_PRECISION_MULTIPLIER);
193                         }
194         }
195     ));
196 }
197 #endif
198
199 #ifdef CSQC
200 NET_HANDLE(damagetext, bool isNew)
201 {
202     int group = ReadShort();
203     vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
204     int deathtype = ReadInt24_t();
205     int flags = ReadByte();
206     bool friendlyfire = flags & DTFLAG_SAMETEAM;
207
208     int health, armor, potential_damage;
209     if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
210     else health = ReadShort();
211     if (flags & DTFLAG_NO_ARMOR) armor = 0;
212     else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
213     else armor = ReadShort();
214     if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
215     else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
216     else potential_damage = ReadShort();
217
218     return = true;
219     if (autocvar_cl_damagetext) {
220         if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) {
221             return;
222         }
223         if (autocvar_cl_damagetext_accumulate_range) {
224             for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
225                 if (e.instanceOfDamageText && e.m_group == group && e.alpha > autocvar_cl_damagetext_accumulate_alpha_rel * autocvar_cl_damagetext_alpha_start) {
226                     DamageText_update(e, location, e.m_healthdamage + health, e.m_armordamage + armor, e.m_potential_damage + potential_damage, deathtype);
227                     return;
228                 }
229             }
230         }
231         make_impure(NEW(DamageText, group, location, health, armor, potential_damage, deathtype, friendlyfire));
232     }
233 }
234 #endif
235
236 #ifdef MENUQC
237
238 #include <menu/gamesettings.qh>
239
240 CLASS(XonoticDamageTextSettings, XonoticTab)
241     REGISTER_SETTINGS(damagetext, NEW(XonoticDamageTextSettings));
242     ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text"));
243     ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9);
244     ATTRIB(XonoticDamageTextSettings, rows, float, 15.5);
245     ATTRIB(XonoticDamageTextSettings, columns, float, 5);
246     INIT(XonoticDamageTextSettings) { this.configureDialog(this); }
247     METHOD(XonoticDamageTextSettings, showNotify, void(entity this)) { loadAllCvars(this); }
248     METHOD(XonoticDamageTextSettings, fill, void(entity this))
249     {
250         entity e;
251         this.gotoRC(this, 0, 1); this.setFirstColumn(this, this.currentColumn);
252             this.TD(this, 1, 3, makeXonoticCheckBox(0, "cl_damagetext", _("Draw damage numbers")));
253         this.TR(this);
254             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Font size:")));
255                 setDependent(e, "cl_damagetext", 1, 1);
256             this.TD(this, 1, 2, e = makeXonoticSlider(0, 50, 1, "cl_damagetext_size"));
257                 setDependent(e, "cl_damagetext", 1, 1);
258         this.TR(this);
259             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Accumulate range:")));
260                 setDependent(e, "cl_damagetext", 1, 1);
261             this.TD(this, 1, 2, e = makeXonoticSlider(0, 500, 1, "cl_damagetext_accumulate_range"));
262                 setDependent(e, "cl_damagetext", 1, 1);
263         this.TR(this);
264             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Lifetime:")));
265                 setDependent(e, "cl_damagetext", 1, 1);
266             this.TD(this, 1, 2, e = makeXonoticSlider(0, 10, 1, "cl_damagetext_alpha_lifetime"));
267                 setDependent(e, "cl_damagetext", 1, 1);
268         this.TR(this);
269             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
270                 setDependent(e, "cl_damagetext", 1, 1);
271             this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_color", "cl_damagetext_color"));
272                 setDependent(e, "cl_damagetext", 1, 1);
273         this.TR(this);
274         this.TR(this);
275         // friendly fire
276             this.TD(this, 1, 3, e = makeXonoticCheckBox(0, "cl_damagetext_friendlyfire", _("Draw damage numbers for friendly fire")));
277                 setDependent(e, "cl_damagetext", 1, 1);
278         this.TR(this);
279             this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
280                 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
281             this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_friendlyfire_color", "cl_damagetext_friendlyfire_color"));
282                 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
283         this.TR(this);
284     }
285 ENDCLASS(XonoticDamageTextSettings)
286 #endif