]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc
remove commented out code
[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 coordinated)");
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.5,        "2D damage text lifetime (alpha fading) in seconds");
33 AUTOCVAR_SAVE(cl_damagetext_2d_size_lifetime,       float,  3,          "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
36 CLASS(DamageText, Object)
37     ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
38     ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
39     ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size_min);
40     ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
41     ATTRIB(DamageText, fade_rate, float, 0);
42     ATTRIB(DamageText, m_shrink_rate, float, 0);
43     ATTRIB(DamageText, m_group, int, 0);
44     ATTRIB(DamageText, m_friendlyfire, bool, false);
45     ATTRIB(DamageText, m_healthdamage, int, 0);
46     ATTRIB(DamageText, m_armordamage, int, 0);
47     ATTRIB(DamageText, m_potential_damage, int, 0);
48     ATTRIB(DamageText, m_deathtype, int, 0);
49     ATTRIB(DamageText, hit_time, float, 0);
50     ATTRIB(DamageText, text, string, string_null);
51     ATTRIB(DamageText, m_screen_coords, bool, false);
52
53     void DamageText_draw2d(DamageText this) {
54         float since_hit = time - this.hit_time;
55         float size = this.m_size - since_hit * this.m_shrink_rate * this.m_size;
56         float alpha_ = this.alpha - since_hit * this.fade_rate;
57         if (alpha_ < 0) {
58             delete(this);
59             return;
60         }
61         vector screen_pos;
62         if (this.m_screen_coords) {
63             screen_pos = this.origin + since_hit * autocvar_cl_damagetext_2d_velocity;
64         } else {
65             screen_pos = project_3d_to_2d(this.origin + since_hit * autocvar_cl_damagetext_velocity) + autocvar_cl_damagetext_offset;
66         }
67         if (screen_pos.z >= 0 && size > 0) {
68             screen_pos.z = 0;
69             vector rgb;
70             if (this.m_friendlyfire) {
71                 rgb = this.m_color_friendlyfire;
72             } else {
73                 rgb = this.m_color;
74             }
75             if (autocvar_cl_damagetext_color_per_weapon) {
76                 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
77                 if (w != WEP_Null) rgb = w.wpcolor;
78             }
79
80             vector drawfontscale_save = drawfontscale;
81             drawfontscale = (size / autocvar_cl_damagetext_size_max) * '1 1 0';
82             drawcolorcodedstring2_builtin(screen_pos, this.text, autocvar_cl_damagetext_size_max * '1 1 0', rgb, alpha_, DRAWFLAG_NORMAL);
83             drawfontscale = drawfontscale_save;
84         }
85     }
86     ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
87
88     void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype) {
89         this.m_healthdamage = _health;
90         this.m_armordamage = _armor;
91         this.m_potential_damage = _potential_damage;
92         this.m_deathtype = _deathtype;
93         setorigin(this, _origin);
94         if (this.m_screen_coords) {
95             this.alpha = autocvar_cl_damagetext_2d_alpha_start;
96         } else {
97             this.alpha = autocvar_cl_damagetext_alpha_start;
98         }
99         this.hit_time = time;
100
101         int health = rint(this.m_healthdamage / DAMAGETEXT_PRECISION_MULTIPLIER);
102         int armor = rint(this.m_armordamage / DAMAGETEXT_PRECISION_MULTIPLIER);
103         int total = rint((this.m_healthdamage + this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
104         int potential = rint(this.m_potential_damage / DAMAGETEXT_PRECISION_MULTIPLIER);
105         int potential_health = rint((this.m_potential_damage - this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
106
107         bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage, this.m_potential_damage, 5);
108
109         string s = autocvar_cl_damagetext_format;
110         s = strreplace("{armor}", (
111             (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
112                 ? ""
113                 : sprintf("%d", armor)
114             ), s);
115         s = strreplace("{potential}", (
116             (redundant && autocvar_cl_damagetext_format_hide_redundant)
117                 ? ""
118                 : sprintf("%d", potential)
119             ), s);
120         s = strreplace("{potential_health}", (
121             (redundant && autocvar_cl_damagetext_format_hide_redundant)
122                 ? ""
123                 : sprintf("%d", potential_health)
124             ), s);
125
126         s = strreplace("{health}", (
127             (health == potential_health || !autocvar_cl_damagetext_format_verbose)
128                 ? sprintf("%d",      health)
129                 : sprintf("%d (%d)", health, potential_health)
130             ), s);
131         s = strreplace("{total}", (
132             (total == potential || !autocvar_cl_damagetext_format_verbose)
133                 ? sprintf("%d",      total)
134                 : sprintf("%d (%d)", total, potential)
135             ), s);
136
137         // futureproofing: remove any remaining (unknown) format strings in case we add new ones in the future
138         // so players can use them on new servers and still have working damagetext on old ones
139         while (true) {
140             int opening_pos = strstrofs(s, "{", 0);
141             if (opening_pos == -1) break;
142             int closing_pos = strstrofs(s, "}", opening_pos);
143             if (closing_pos == -1 || closing_pos <= opening_pos) break;
144             s = strcat(
145                 substring(s, 0, opening_pos),
146                 substring_range(s, closing_pos + 1, strlen(s))
147             );
148         }
149
150         if (this.text) strunzone(this.text);
151         this.text = strzone(s);
152
153         float size_range = autocvar_cl_damagetext_size_max - autocvar_cl_damagetext_size_min;
154         float damage_range = autocvar_cl_damagetext_size_max_damage - autocvar_cl_damagetext_size_min_damage;
155         float scale_factor = size_range / damage_range;
156         this.m_size = bound(
157             autocvar_cl_damagetext_size_min,
158             (potential - autocvar_cl_damagetext_size_min_damage) * scale_factor + autocvar_cl_damagetext_size_min,
159             autocvar_cl_damagetext_size_max);
160     }
161
162     CONSTRUCTOR(DamageText, int _group, vector _origin, bool _screen_coords, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) {
163         CONSTRUCT(DamageText);
164         this.m_group = _group;
165         this.m_friendlyfire = _friendlyfire;
166         this.m_screen_coords = _screen_coords;
167         if (_screen_coords) {
168             this.fade_rate = 1 / autocvar_cl_damagetext_2d_alpha_lifetime;
169             this.m_shrink_rate = 1 / autocvar_cl_damagetext_2d_size_lifetime;
170             //this.m_shrink_rate = autocvar_cl_damagetext_2d_shrink_rate;
171         } else {
172             this.fade_rate = 1 / autocvar_cl_damagetext_alpha_lifetime;
173             this.m_shrink_rate = 0;
174         }
175         DamageText_update(this, _origin, _health, _armor, _potential_damage, _deathtype);
176         IL_PUSH(g_drawables_2d, this);
177     }
178
179     DESTRUCTOR(DamageText) {
180         if (this.text) strunzone(this.text);
181     }
182 ENDCLASS(DamageText)
183
184 // TODO
185 DamageText screen_damagetext_first;
186 int screen_damagetext_count = 0;
187
188 NET_HANDLE(damagetext, bool isNew)
189 {
190     int server_entity_index = ReadShort();
191     int deathtype = ReadInt24_t();
192     int flags = ReadByte();
193     bool friendlyfire = flags & DTFLAG_SAMETEAM;
194
195     int health, armor, potential_damage;
196     if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
197     else health = ReadShort();
198     if (flags & DTFLAG_NO_ARMOR) armor = 0;
199     else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
200     else armor = ReadShort();
201     if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
202     else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
203     else potential_damage = ReadShort();
204
205     return = true;
206     if (!autocvar_cl_damagetext) return;
207     if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) return;
208
209     int client_entity_index = server_entity_index - 1;
210     entity entcs = entcs_receiver(client_entity_index);
211     if (!entcs || !entcs.has_origin) { // TODO also when off-screen or too close (optional)
212         // screen coords only
213         vector screen_pos = '0 0 0';
214         screen_pos.x = vid_conwidth * autocvar_cl_damagetext_2d_pos.x;
215         screen_pos.y = vid_conheight * autocvar_cl_damagetext_2d_pos.y;
216
217         IL_EACH(g_drawables_2d, it.instanceOfDamageText && it.m_screen_coords && it.m_group == server_entity_index, {
218             DamageText_update(it, screen_pos, it.m_healthdamage + health, it.m_armordamage + armor, it.m_potential_damage + potential_damage, deathtype);
219             return;
220         });
221
222         // TODO when hitting multiple enemies dmgtext would overlap
223         screen_damagetext_first = screen_damagetext_first;
224
225         make_impure(NEW(DamageText, server_entity_index, screen_pos, true, health, armor, potential_damage, deathtype, friendlyfire));
226     } else {
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     }
241 }