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