]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc
f9fcb2e5832658e1071c5f4621a7fe344721169b
[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_screen,        vector, '0 -20 0',  "Damage text move direction (screen coordinates)");
25 AUTOCVAR_SAVE(cl_damagetext_velocity_world,         vector, '0 0 0',    "Damage text move direction (world coordinates)");
26 AUTOCVAR_SAVE(cl_damagetext_offset_screen,          vector, '0 -40 0',  "Damage text offset (screen coordinates)");
27 AUTOCVAR_SAVE(cl_damagetext_offset_world,           vector, '0 0 0',    "Damage text offset (world coordinates)");
28 AUTOCVAR_SAVE(cl_damagetext_accumulate_range,       float,  30,         "Damage text spawned within this range is accumulated");
29 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");
30 AUTOCVAR_SAVE(cl_damagetext_friendlyfire,           bool,   true,       "Show damage text for friendlyfire too");
31 AUTOCVAR_SAVE(cl_damagetext_friendlyfire_color,     vector, '1 0 0',    "Damage text color for friendlyfire");
32
33 AUTOCVAR_SAVE(cl_damagetext_2d,                     bool,   true,       "Show damagetext in 2D coordinates if the enemy's location is not known");
34 AUTOCVAR_SAVE(cl_damagetext_2d_pos,                 vector, '0.47 0.53 0',     "2D damage text initial position (X and Y between 0 and 1)");
35 AUTOCVAR_SAVE(cl_damagetext_2d_alpha_start,         float,  1,          "2D damage text initial alpha");
36 AUTOCVAR_SAVE(cl_damagetext_2d_alpha_lifetime,      float,  1.3,        "2D damage text lifetime (alpha fading) in seconds");
37 AUTOCVAR_SAVE(cl_damagetext_2d_size_lifetime,       float,  3,          "2D damage text lifetime (size shrinking) in seconds");
38 AUTOCVAR_SAVE(cl_damagetext_2d_velocity,            vector, '-25 0 0',  "2D damage text move direction (screen coordinates)");
39 AUTOCVAR_SAVE(cl_damagetext_2d_overlap_offset,      vector, '0 -15 0',  "Offset 2D damage text by this much to prevent overlapping (screen coordinates)");
40 AUTOCVAR_SAVE(cl_damagetext_2d_close_range,         float,  125,        "Always use 2D damagetext for hits closer that this");
41 AUTOCVAR_SAVE(cl_damagetext_2d_out_of_view,         bool,   true,       "Always use 2D damagetext for hits that occured off-screen");
42
43 CLASS(DamageText, Object)
44     ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
45     ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
46     ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size_min);
47     ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
48     ATTRIB(DamageText, fade_rate, float, 0);
49     ATTRIB(DamageText, m_shrink_rate, float, 0);
50     ATTRIB(DamageText, m_group, int, 0);
51     ATTRIB(DamageText, m_friendlyfire, bool, false);
52     ATTRIB(DamageText, m_healthdamage, int, 0);
53     ATTRIB(DamageText, m_armordamage, int, 0);
54     ATTRIB(DamageText, m_potential_damage, int, 0);
55     ATTRIB(DamageText, m_deathtype, int, 0);
56     ATTRIB(DamageText, hit_time, float, 0);
57     ATTRIB(DamageText, text, string, string_null);
58     ATTRIB(DamageText, m_screen_coords, bool, false);
59
60     STATIC_ATTRIB(DamageText, screen_first, DamageText, NULL);
61     STATIC_ATTRIB(DamageText, screen_count, int, 0);
62
63     void DamageText_draw2d(DamageText this) {
64         float since_hit = time - this.hit_time;
65         float size = this.m_size - since_hit * this.m_shrink_rate * this.m_size;
66         float alpha_ = this.alpha - since_hit * this.fade_rate;
67         if (alpha_ <= 0 || size <= 0) {
68             delete(this);
69             return;
70         }
71         vector screen_pos;
72         if (this.m_screen_coords) {
73             screen_pos = this.origin + since_hit * autocvar_cl_damagetext_2d_velocity;
74         } else {
75             makevectors(view_angles);
76             vector world_offset = since_hit * autocvar_cl_damagetext_velocity_world + autocvar_cl_damagetext_offset_world;
77             vector world_pos = this.origin + world_offset.x * v_forward + world_offset.y * v_right + world_offset.z * v_up;
78             screen_pos = project_3d_to_2d(world_pos) + since_hit * autocvar_cl_damagetext_velocity_screen + autocvar_cl_damagetext_offset_screen;
79         }
80         if (screen_pos.z >= 0) {
81             screen_pos.z = 0;
82             vector rgb;
83             if (this.m_friendlyfire) {
84                 rgb = this.m_color_friendlyfire;
85             } else {
86                 rgb = this.m_color;
87             }
88             if (autocvar_cl_damagetext_color_per_weapon) {
89                 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
90                 if (w != WEP_Null) rgb = w.wpcolor;
91             }
92
93             vector drawfontscale_save = drawfontscale;
94             drawfontscale = (size / autocvar_cl_damagetext_size_max) * '1 1 0';
95             drawcolorcodedstring2_builtin(screen_pos, this.text, autocvar_cl_damagetext_size_max * '1 1 0', rgb, alpha_, DRAWFLAG_NORMAL);
96             drawfontscale = drawfontscale_save;
97         }
98     }
99     ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
100
101     void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype) {
102         this.m_healthdamage = _health;
103         this.m_armordamage = _armor;
104         this.m_potential_damage = _potential_damage;
105         this.m_deathtype = _deathtype;
106         setorigin(this, _origin);
107         if (this.m_screen_coords) {
108             this.alpha = autocvar_cl_damagetext_2d_alpha_start;
109         } else {
110             this.alpha = autocvar_cl_damagetext_alpha_start;
111         }
112         this.hit_time = time;
113
114         int health = rint(this.m_healthdamage / DAMAGETEXT_PRECISION_MULTIPLIER);
115         int armor = rint(this.m_armordamage / DAMAGETEXT_PRECISION_MULTIPLIER);
116         int total = rint((this.m_healthdamage + this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
117         int potential = rint(this.m_potential_damage / DAMAGETEXT_PRECISION_MULTIPLIER);
118         int potential_health = rint((this.m_potential_damage - this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
119
120         bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage, this.m_potential_damage, 5);
121
122         string s = autocvar_cl_damagetext_format;
123         s = strreplace("{armor}", (
124             (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
125                 ? ""
126                 : sprintf("%d", armor)
127             ), s);
128         s = strreplace("{potential}", (
129             (redundant && autocvar_cl_damagetext_format_hide_redundant)
130                 ? ""
131                 : sprintf("%d", potential)
132             ), s);
133         s = strreplace("{potential_health}", (
134             (redundant && autocvar_cl_damagetext_format_hide_redundant)
135                 ? ""
136                 : sprintf("%d", potential_health)
137             ), s);
138
139         s = strreplace("{health}", (
140             (health == potential_health || !autocvar_cl_damagetext_format_verbose)
141                 ? sprintf("%d",      health)
142                 : sprintf("%d (%d)", health, potential_health)
143             ), s);
144         s = strreplace("{total}", (
145             (total == potential || !autocvar_cl_damagetext_format_verbose)
146                 ? sprintf("%d",      total)
147                 : sprintf("%d (%d)", total, potential)
148             ), s);
149
150         // futureproofing: remove any remaining (unknown) format strings in case we add new ones in the future
151         // so players can use them on new servers and still have working damagetext on old ones
152         while (true) {
153             int opening_pos = strstrofs(s, "{", 0);
154             if (opening_pos == -1) break;
155             int closing_pos = strstrofs(s, "}", opening_pos);
156             if (closing_pos == -1 || closing_pos <= opening_pos) break;
157             s = strcat(
158                 substring(s, 0, opening_pos),
159                 substring_range(s, closing_pos + 1, strlen(s))
160             );
161         }
162
163         if (this.text) strunzone(this.text);
164         this.text = strzone(s);
165
166         this.m_size = map_bound_ranges(potential,
167             autocvar_cl_damagetext_size_min_damage, autocvar_cl_damagetext_size_max_damage,
168             autocvar_cl_damagetext_size_min, autocvar_cl_damagetext_size_max);
169     }
170
171     CONSTRUCTOR(DamageText, int _group, vector _origin, bool _screen_coords, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) {
172         CONSTRUCT(DamageText);
173         this.m_group = _group;
174         this.m_friendlyfire = _friendlyfire;
175         this.m_screen_coords = _screen_coords;
176         if (_screen_coords) {
177             this.fade_rate = 1 / autocvar_cl_damagetext_2d_alpha_lifetime;
178             this.m_shrink_rate = 1 / autocvar_cl_damagetext_2d_size_lifetime;
179         } else {
180             this.fade_rate = 1 / autocvar_cl_damagetext_alpha_lifetime;
181             this.m_shrink_rate = 0;
182         }
183         DamageText_update(this, _origin, _health, _armor, _potential_damage, _deathtype);
184         IL_PUSH(g_drawables_2d, this);
185     }
186
187     DESTRUCTOR(DamageText) {
188         if (this.text) strunzone(this.text);
189         if (this == DamageText_screen_first) {
190             // start from 0 offset again, hopefully, others (if any) will have faded away by now
191             DamageText_screen_first = NULL;
192             DamageText_screen_count = 0;
193         }
194     }
195 ENDCLASS(DamageText)
196
197 NET_HANDLE(damagetext, bool isNew)
198 {
199     int server_entity_index = ReadByte();
200     int deathtype = ReadInt24_t();
201     int flags = ReadByte();
202     bool friendlyfire = flags & DTFLAG_SAMETEAM;
203
204     int health, armor, potential_damage;
205     if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
206     else health = ReadShort();
207     if (flags & DTFLAG_NO_ARMOR) armor = 0;
208     else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
209     else armor = ReadShort();
210     if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
211     else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
212     else potential_damage = ReadShort();
213
214     return = true;
215     if (!autocvar_cl_damagetext) return;
216     if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) return;
217
218     int client_entity_index = server_entity_index - 1;
219     entity entcs = entcs_receiver(client_entity_index);
220
221     bool can_use_3d = entcs && entcs.has_origin;
222     bool too_close = vdist(entcs.origin - view_origin, <, autocvar_cl_damagetext_2d_close_range);
223     bool prefer_in_view = autocvar_cl_damagetext_2d_out_of_view && !projected_on_screen(project_3d_to_2d(entcs.origin));
224     bool prefer_2d = spectatee_status != -1 && autocvar_cl_damagetext_2d && (too_close || prefer_in_view);
225
226     if (can_use_3d && !prefer_2d) {
227         // world coords
228         if (autocvar_cl_damagetext_accumulate_range) {
229             for (entity e = findradius(entcs.origin, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
230                 if (e.instanceOfDamageText
231                     && !e.m_screen_coords // we're using origin for both world coords and screen coords so avoid mismatches
232                     && e.m_group == server_entity_index
233                     && e.alpha > autocvar_cl_damagetext_accumulate_alpha_rel * autocvar_cl_damagetext_alpha_start) {
234                     DamageText_update(e, entcs.origin, e.m_healthdamage + health, e.m_armordamage + armor, e.m_potential_damage + potential_damage, deathtype);
235                     return;
236                 }
237             }
238         }
239         make_impure(NEW(DamageText, server_entity_index, entcs.origin, false, health, armor, potential_damage, deathtype, friendlyfire));
240     } else if (autocvar_cl_damagetext_2d && spectatee_status != -1) {
241         // never show 2d damagetext when observing - might be a bug in .has_origin
242
243         // screen coords only
244         vector screen_pos = vec2(vid_conwidth * autocvar_cl_damagetext_2d_pos.x, vid_conheight * autocvar_cl_damagetext_2d_pos.y);
245         IL_EACH(g_drawables_2d, it.instanceOfDamageText && it.m_screen_coords && it.m_group == server_entity_index, {
246             DamageText_update(it, screen_pos, it.m_healthdamage + health, it.m_armordamage + armor, it.m_potential_damage + potential_damage, deathtype);
247             return;
248         });
249
250         // when hitting multiple enemies, dmgtext would overlap
251         if (DamageText_screen_first == NULL) {
252             DamageText dt = NEW(DamageText, server_entity_index, screen_pos, true, health, armor, potential_damage, deathtype, friendlyfire);
253             make_impure(dt);
254             DamageText_screen_first = dt;
255             DamageText_screen_count = 1;
256         } else {
257             screen_pos += autocvar_cl_damagetext_2d_overlap_offset * DamageText_screen_count;
258             DamageText_screen_count++;
259             make_impure(NEW(DamageText, server_entity_index, screen_pos, true, health, armor, potential_damage, deathtype, friendlyfire));
260         }
261     }
262 }