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