]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc
Merge branch 'Mario/cts_respawn_clear' into 'master'
[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
47     void DamageText_draw2d(DamageText this) {
48         float dt = time - this.time_prev;
49         this.time_prev = time;
50         setorigin(this, this.origin + dt * this.velocity);
51         this.alpha -= dt * this.fade_rate;
52         if (this.alpha < 0) delete(this);
53         vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
54         if (pos.z >= 0 && this.m_size > 0) {
55             pos.z = 0;
56             vector rgb;
57             if (this.m_friendlyfire) {
58                 rgb = this.m_color_friendlyfire;
59             } else {
60                 rgb = this.m_color;
61             }
62             if (autocvar_cl_damagetext_color_per_weapon) {
63                 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
64                 if (w != WEP_Null) rgb = w.wpcolor;
65             }
66
67             vector drawfontscale_save = drawfontscale;
68             drawfontscale = (this.m_size / autocvar_cl_damagetext_size_max) * '1 1 0';
69             drawcolorcodedstring2_builtin(pos, this.text, autocvar_cl_damagetext_size_max * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL);
70             drawfontscale = drawfontscale_save;
71         }
72     }
73     ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
74
75     void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype) {
76         this.m_healthdamage = _health;
77         this.m_armordamage = _armor;
78         this.m_potential_damage = _potential_damage;
79         this.m_deathtype = _deathtype;
80         setorigin(this, _origin);
81         this.alpha = autocvar_cl_damagetext_alpha_start;
82
83         int health = rint(this.m_healthdamage / DAMAGETEXT_PRECISION_MULTIPLIER);
84         int armor = rint(this.m_armordamage / DAMAGETEXT_PRECISION_MULTIPLIER);
85         int total = rint((this.m_healthdamage + this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
86         int potential = rint(this.m_potential_damage / DAMAGETEXT_PRECISION_MULTIPLIER);
87         int potential_health = rint((this.m_potential_damage - this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER);
88
89         bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage, this.m_potential_damage, 5);
90
91         string s = autocvar_cl_damagetext_format;
92         s = strreplace("{armor}", (
93             (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
94                 ? ""
95                 : sprintf("%d", armor)
96             ), s);
97         s = strreplace("{potential}", (
98             (redundant && autocvar_cl_damagetext_format_hide_redundant)
99                 ? ""
100                 : sprintf("%d", potential)
101             ), s);
102         s = strreplace("{potential_health}", (
103             (redundant && autocvar_cl_damagetext_format_hide_redundant)
104                 ? ""
105                 : sprintf("%d", potential_health)
106             ), s);
107
108         s = strreplace("{health}", (
109             (health == potential_health || !autocvar_cl_damagetext_format_verbose)
110                 ? sprintf("%d",      health)
111                 : sprintf("%d (%d)", health, potential_health)
112             ), s);
113         s = strreplace("{total}", (
114             (total == potential || !autocvar_cl_damagetext_format_verbose)
115                 ? sprintf("%d",      total)
116                 : sprintf("%d (%d)", total, potential)
117             ), s);
118
119         // futureproofing: remove any remaining (unknown) format strings in case we add new ones in the future
120         // so players can use them on new servers and still have working damagetext on old ones
121         while (true) {
122             int opening_pos = strstrofs(s, "{", 0);
123             if (opening_pos == -1) break;
124             int closing_pos = strstrofs(s, "}", opening_pos);
125             if (closing_pos == -1 || closing_pos <= opening_pos) break;
126             s = strcat(
127                 substring(s, 0, opening_pos),
128                 substring_range(s, closing_pos + 1, strlen(s))
129             );
130         }
131
132         if (this.text) strunzone(this.text);
133         this.text = strzone(s);
134
135         float size_range = autocvar_cl_damagetext_size_max - autocvar_cl_damagetext_size_min;
136         float damage_range = autocvar_cl_damagetext_size_max_damage - autocvar_cl_damagetext_size_min_damage;
137         float scale_factor = size_range / damage_range;
138         this.m_size = bound(
139             autocvar_cl_damagetext_size_min,
140             (potential - autocvar_cl_damagetext_size_min_damage) * scale_factor + autocvar_cl_damagetext_size_min,
141             autocvar_cl_damagetext_size_max);
142     }
143
144     CONSTRUCTOR(DamageText, int _group, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) {
145         CONSTRUCT(DamageText);
146         this.m_group = _group;
147         this.m_friendlyfire = _friendlyfire;
148         DamageText_update(this, _origin, _health, _armor, _potential_damage, _deathtype);
149                 IL_PUSH(g_drawables_2d, this);
150     }
151
152     DESTRUCTOR(DamageText) {
153         if (this.text) strunzone(this.text);
154     }
155 ENDCLASS(DamageText)
156
157 NET_HANDLE(damagetext, bool isNew)
158 {
159     int group = ReadShort();
160     vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
161     int deathtype = ReadInt24_t();
162     int flags = ReadByte();
163     bool friendlyfire = flags & DTFLAG_SAMETEAM;
164
165     int health, armor, potential_damage;
166     if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
167     else health = ReadShort();
168     if (flags & DTFLAG_NO_ARMOR) armor = 0;
169     else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
170     else armor = ReadShort();
171     if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
172     else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
173     else potential_damage = ReadShort();
174
175     return = true;
176     if (autocvar_cl_damagetext) {
177         if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) {
178             return;
179         }
180         if (autocvar_cl_damagetext_accumulate_range) {
181             for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
182                 if (e.instanceOfDamageText && e.m_group == group && e.alpha > autocvar_cl_damagetext_accumulate_alpha_rel * autocvar_cl_damagetext_alpha_start) {
183                     DamageText_update(e, location, e.m_healthdamage + health, e.m_armordamage + armor, e.m_potential_damage + potential_damage, deathtype);
184                     return;
185                 }
186             }
187         }
188         make_impure(NEW(DamageText, group, location, health, armor, potential_damage, deathtype, friendlyfire));
189     }
190 }