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