]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc
wip non-wallhacky damagetext
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / damagetext / cl_damagetext.qc
1 #include "cl_damagetext.qh"
2
3 // no translatable cvar description please
4 AUTOCVAR_SAVE(cl_damagetext,                        bool,   true,       "Draw damage dealt where you hit the enemy");
5 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");
6 AUTOCVAR_SAVE(cl_damagetext_format_verbose,         bool,   false,      "{health} shows {potential_health} too when they differ; {total} shows {potential} too when they differ");
7 AUTOCVAR_SAVE(cl_damagetext_format_hide_redundant,  bool,   false,      "hide {armor} if 0; hide {potential} and {potential_health} when same as actual");
8 STATIC_INIT(DamageText_LegacyFormat) {
9     // damagetext used to be off by default and the cvar got saved in people's configs along with the old format
10     // enable damagetext while updating the format for a one time effect
11     if (strstrofs(autocvar_cl_damagetext_format, "{", 0) < 0) {
12         localcmd("\nseta cl_damagetext 1\n");
13         localcmd("\nseta cl_damagetext_format -{total}\n");
14     };
15 }
16 AUTOCVAR_SAVE(cl_damagetext_color,                  vector, '1 1 0',    "Damage text color");
17 AUTOCVAR_SAVE(cl_damagetext_color_per_weapon,       bool,   false,      "Damage text uses weapon color");
18 AUTOCVAR_SAVE(cl_damagetext_size_min,               float,  10,          "Damage text font size for small damage");
19 AUTOCVAR_SAVE(cl_damagetext_size_min_damage,        float,  25,          "How much damage is considered small");
20 AUTOCVAR_SAVE(cl_damagetext_size_max,               float,  16,         "Damage text font size for large damage");
21 AUTOCVAR_SAVE(cl_damagetext_size_max_damage,        float,  140,        "How much damage is considered large");
22 AUTOCVAR_SAVE(cl_damagetext_alpha_start,            float,  1,          "Damage text initial alpha");
23 AUTOCVAR_SAVE(cl_damagetext_alpha_lifetime,         float,  3,          "Damage text lifetime in seconds");
24 AUTOCVAR_SAVE(cl_damagetext_velocity,               vector, '0 0 20',   "Damage text move direction");
25 AUTOCVAR_SAVE(cl_damagetext_offset,                 vector, '0 -40 0',  "Damage text offset");
26 AUTOCVAR_SAVE(cl_damagetext_accumulate_range,       float,  30,         "Damage text spawned within this range is accumulated");
27 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");
28 AUTOCVAR_SAVE(cl_damagetext_friendlyfire,           bool,   true,       "Show damage text for friendlyfire too");
29 AUTOCVAR_SAVE(cl_damagetext_friendlyfire_color,     vector, '1 0 0',    "Damage text color for friendlyfire");
30
31 CLASS(DamageText, Object)
32     ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
33     ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
34     ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size_min);
35     ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
36     ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime);
37     ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity);
38     ATTRIB(DamageText, m_group, int, 0);
39     ATTRIB(DamageText, m_friendlyfire, bool, false);
40     ATTRIB(DamageText, m_healthdamage, int, 0);
41     ATTRIB(DamageText, m_armordamage, int, 0);
42     ATTRIB(DamageText, m_potential_damage, int, 0);
43     ATTRIB(DamageText, m_deathtype, int, 0);
44     ATTRIB(DamageText, time_prev, float, time);
45     ATTRIB(DamageText, text, string, string_null);
46     ATTRIB(DamageText, m_screen_coords, bool, false);
47
48     void DamageText_draw2d(DamageText this) {
49         float dt = time - this.time_prev;
50         this.time_prev = time;
51         this.alpha -= dt * this.fade_rate;
52         if (this.alpha < 0) {
53             delete(this);
54             return;
55         }
56         vector screen_pos;
57         if (this.m_screen_coords) {
58             setorigin(this, this.origin + dt * '0 -20 0'); // TODO unhardcode
59             screen_pos = this.origin;
60         } else {
61             setorigin(this, this.origin + dt * this.velocity); // TODO why this.velocity instead of cvar?
62             screen_pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
63         }
64         if (screen_pos.z >= 0 && this.m_size > 0) {
65             screen_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             vector drawfontscale_save = drawfontscale;
78             drawfontscale = (this.m_size / autocvar_cl_damagetext_size_max) * '1 1 0';
79             drawcolorcodedstring2_builtin(screen_pos, this.text, autocvar_cl_damagetext_size_max * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL);
80             drawfontscale = drawfontscale_save;
81         }
82     }
83     ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
84
85     void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype) {
86         this.m_healthdamage = _health;
87         this.m_armordamage = _armor;
88         this.m_potential_damage = _potential_damage;
89         this.m_deathtype = _deathtype;
90         setorigin(this, _origin);
91         this.alpha = autocvar_cl_damagetext_alpha_start;
92
93         int health = rint(this.m_healthdamage / DAMAGETEXT_PRECISION_MULTIPLIER);
94         int armor = rint(this.m_armordamage / DAMAGETEXT_PRECISION_MULTIPLIER);
95         int total = rint((this.m_healthdamage + this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
96         int potential = rint(this.m_potential_damage / DAMAGETEXT_PRECISION_MULTIPLIER);
97         int potential_health = rint((this.m_potential_damage - this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
98
99         bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage, this.m_potential_damage, 5);
100
101         string s = autocvar_cl_damagetext_format;
102         s = strreplace("{armor}", (
103             (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
104                 ? ""
105                 : sprintf("%d", armor)
106             ), s);
107         s = strreplace("{potential}", (
108             (redundant && autocvar_cl_damagetext_format_hide_redundant)
109                 ? ""
110                 : sprintf("%d", potential)
111             ), s);
112         s = strreplace("{potential_health}", (
113             (redundant && autocvar_cl_damagetext_format_hide_redundant)
114                 ? ""
115                 : sprintf("%d", potential_health)
116             ), s);
117
118         s = strreplace("{health}", (
119             (health == potential_health || !autocvar_cl_damagetext_format_verbose)
120                 ? sprintf("%d",      health)
121                 : sprintf("%d (%d)", health, potential_health)
122             ), s);
123         s = strreplace("{total}", (
124             (total == potential || !autocvar_cl_damagetext_format_verbose)
125                 ? sprintf("%d",      total)
126                 : sprintf("%d (%d)", total, potential)
127             ), s);
128
129         // futureproofing: remove any remaining (unknown) format strings in case we add new ones in the future
130         // so players can use them on new servers and still have working damagetext on old ones
131         while (true) {
132             int opening_pos = strstrofs(s, "{", 0);
133             if (opening_pos == -1) break;
134             int closing_pos = strstrofs(s, "}", opening_pos);
135             if (closing_pos == -1 || closing_pos <= opening_pos) break;
136             s = strcat(
137                 substring(s, 0, opening_pos),
138                 substring_range(s, closing_pos + 1, strlen(s))
139             );
140         }
141
142         if (this.text) strunzone(this.text);
143         this.text = strzone(s);
144
145         float size_range = autocvar_cl_damagetext_size_max - autocvar_cl_damagetext_size_min;
146         float damage_range = autocvar_cl_damagetext_size_max_damage - autocvar_cl_damagetext_size_min_damage;
147         float scale_factor = size_range / damage_range;
148         this.m_size = bound(
149             autocvar_cl_damagetext_size_min,
150             (potential - autocvar_cl_damagetext_size_min_damage) * scale_factor + autocvar_cl_damagetext_size_min,
151             autocvar_cl_damagetext_size_max);
152     }
153
154     CONSTRUCTOR(DamageText, int _group, vector _origin, bool _screen_coords, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) {
155         CONSTRUCT(DamageText);
156         this.m_group = _group;
157         this.m_friendlyfire = _friendlyfire;
158         this.m_screen_coords = _screen_coords;
159         DamageText_update(this, _origin, _health, _armor, _potential_damage, _deathtype);
160         IL_PUSH(g_drawables_2d, this);
161     }
162
163     DESTRUCTOR(DamageText) {
164         if (this.text) strunzone(this.text);
165     }
166 ENDCLASS(DamageText)
167
168 NET_HANDLE(damagetext, bool isNew)
169 {
170     int server_entity_index = ReadShort();
171     int deathtype = ReadInt24_t();
172     int flags = ReadByte();
173     bool friendlyfire = flags & DTFLAG_SAMETEAM;
174
175     int health, armor, potential_damage;
176     if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
177     else health = ReadShort();
178     if (flags & DTFLAG_NO_ARMOR) armor = 0;
179     else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
180     else armor = ReadShort();
181     if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
182     else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
183     else potential_damage = ReadShort();
184
185     return = true;
186     if (!autocvar_cl_damagetext) return;
187     if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) return;
188
189     int client_entity_index = server_entity_index - 1;
190     entity entcs = entcs_receiver(client_entity_index);
191     if (!entcs || !entcs.has_origin) {
192         vector screen_pos = '0 0 0';
193         screen_pos.x = vid_conwidth * 0.45; // TODO unhardcode
194         screen_pos.y = vid_conheight * 0.45;
195         // TODO accumulation
196         // TODO when hitting multiple enemies dmgtext would overlap
197         make_impure(NEW(DamageText, server_entity_index, screen_pos, true, health, armor, potential_damage, deathtype, friendlyfire));
198     } else {
199         if (autocvar_cl_damagetext_accumulate_range) {
200             for (entity e = findradius(entcs.origin, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
201                 if (e.instanceOfDamageText
202                     && !e.m_screen_coords // we're using origin for both world coords and screen coords so avoid mismatches
203                     && e.m_group == server_entity_index
204                     && e.alpha > autocvar_cl_damagetext_accumulate_alpha_rel * autocvar_cl_damagetext_alpha_start) {
205                     DamageText_update(e, entcs.origin, e.m_healthdamage + health, e.m_armordamage + armor, e.m_potential_damage + potential_damage, deathtype);
206                     return;
207                 }
208             }
209         }
210         make_impure(NEW(DamageText, server_entity_index, entcs.origin, false, health, armor, potential_damage, deathtype, friendlyfire));
211     }
212 }