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