]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc
Fix FL_WEAPON flag overlapping FL_JUMPRELEASED. This unintentional change was introdu...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / damagetext / cl_damagetext.qc
1 #include "cl_damagetext.qh"
2
3 CLASS(DamageText, Object)
4         ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
5         ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
6         ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size_min);
7         ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
8         ATTRIB(DamageText, fade_rate, float, 0);
9         ATTRIB(DamageText, m_shrink_rate, float, 0);
10         ATTRIB(DamageText, m_group, int, 0);
11         ATTRIB(DamageText, m_friendlyfire, bool, false);
12         ATTRIB(DamageText, m_healthdamage, int, 0);
13         ATTRIB(DamageText, m_armordamage, int, 0);
14         ATTRIB(DamageText, m_potential_damage, int, 0);
15         ATTRIB(DamageText, m_deathtype, int, 0);
16         ATTRIB(DamageText, hit_time, float, 0);
17         ATTRIB(DamageText, text, string, string_null);
18         ATTRIB(DamageText, m_screen_coords, bool, false);
19
20         STATIC_ATTRIB(DamageText, screen_count, int, 0);
21
22         void DamageText_draw2d(DamageText this)
23         {
24                 float since_hit = time - this.hit_time;
25                 // can't use `dt = hit_time - prev_update_time` because shrinking wouldn't be linear
26                 float size = this.m_size - since_hit * this.m_shrink_rate * this.m_size;
27                 float alpha_ = this.alpha - since_hit * this.fade_rate;
28                 bool haslifetime = (autocvar_cl_damagetext_lifetime < 0 // negative ignores lifetime
29                                 || (since_hit < autocvar_cl_damagetext_lifetime));
30                 if (alpha_ <= 0 || size <= 0 || !haslifetime)
31                 {
32                         IL_REMOVE(g_damagetext, this);
33                         delete(this);
34                         return;
35                 }
36
37                 vector screen_pos;
38                 if (this.m_screen_coords)
39                         screen_pos = this.origin + since_hit * autocvar_cl_damagetext_2d_velocity;
40                 else
41                 {
42                         vector forward, right, up;
43                         MAKE_VECTORS(view_angles, forward, right, up);
44
45                         vector world_offset = since_hit * autocvar_cl_damagetext_velocity_world
46                                         + autocvar_cl_damagetext_offset_world;
47                         vector world_pos = this.origin
48                                          + world_offset.x * forward
49                                          + world_offset.y * right
50                                          + world_offset.z * up;
51
52                         screen_pos = project_3d_to_2d(world_pos)
53                                 + (since_hit * autocvar_cl_damagetext_velocity_screen)
54                                 + autocvar_cl_damagetext_offset_screen;
55                 }
56                 screen_pos.y += size / 2;
57
58                 // strip trailing spaces
59                 string dtext = this.text;
60                 int j = strlen(dtext) - 1;
61                 while (substring(dtext, j, 1) == " " && j >= 0)
62                         --j;
63                 if (j < strlen(dtext) - 1)
64                         dtext = substring(dtext, 0, j + 1);
65
66                 // strip leading spaces
67                 j = 0;
68                 while (substring(dtext, j, 1) == " " && j < strlen(dtext))
69                         ++j;
70                 if (j > 0)
71                         dtext = substring(dtext, j, strlen(dtext) - j);
72
73                 // Center damage text
74                 screen_pos.x -= stringwidth(dtext, true, hud_fontsize * 2) * 0.5;
75
76                 if (screen_pos.z >= 0)
77                 {
78                         screen_pos.z = 0;
79                         vector rgb;
80                         if (this.m_friendlyfire)
81                                 rgb = this.m_color_friendlyfire;
82                         else
83                                 rgb = this.m_color;
84
85                         if (autocvar_cl_damagetext_color_per_weapon)
86                         {
87                                 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
88                                 if (w != WEP_Null) rgb = w.wpcolor;
89                         }
90
91                         vector drawfontscale_save = drawfontscale;
92                         drawfontscale = (size / autocvar_cl_damagetext_size_max) * '1 1 0';
93                         screen_pos.y -= drawfontscale.x * size / 2;
94                         drawcolorcodedstring2_builtin(screen_pos, this.text,
95                                         autocvar_cl_damagetext_size_max * '1 1 0',
96                                         rgb, alpha_, DRAWFLAG_NORMAL);
97                         drawfontscale = drawfontscale_save;
98                 }
99         }
100         ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
101
102         void DamageText_update(DamageText this, vector _origin, bool screen_coords,
103                         int _health, int _armor, int _potential_damage, int _deathtype)
104         {
105                 this.m_healthdamage = _health;
106                 this.m_armordamage = _armor;
107                 this.m_potential_damage = _potential_damage;
108                 this.m_deathtype = _deathtype;
109                 this.hit_time = time;
110                 setorigin(this, _origin);
111                 this.m_screen_coords = screen_coords;
112                 if (this.m_screen_coords)
113                         this.alpha = autocvar_cl_damagetext_2d_alpha_start;
114                 else
115                         this.alpha = autocvar_cl_damagetext_alpha_start;
116
117                 int health           = rint(this.m_healthdamage
118                                      / DAMAGETEXT_PRECISION_MULTIPLIER);
119                 int armor            = rint(this.m_armordamage
120                                      / DAMAGETEXT_PRECISION_MULTIPLIER);
121                 int total            = rint((this.m_healthdamage + this.m_armordamage)
122                                      / DAMAGETEXT_PRECISION_MULTIPLIER);
123                 int potential        = rint(this.m_potential_damage
124                                      / DAMAGETEXT_PRECISION_MULTIPLIER);
125                 int potential_health = rint((this.m_potential_damage - this.m_armordamage)
126                                      / DAMAGETEXT_PRECISION_MULTIPLIER);
127
128                 bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage,
129                                 this.m_potential_damage, 5);
130
131                 string s = autocvar_cl_damagetext_format;
132                 s = strreplace("{armor}", (
133                         (this.m_armordamage == 0 && autocvar_cl_damagetext_format_hide_redundant)
134                                 ? ""
135                                 : sprintf("%d", armor)
136                         ), s);
137                 s = strreplace("{potential}", (
138                         (redundant && autocvar_cl_damagetext_format_hide_redundant)
139                                 ? ""
140                                 : sprintf("%d", potential)
141                         ), s);
142                 s = strreplace("{potential_health}", (
143                         (redundant && autocvar_cl_damagetext_format_hide_redundant)
144                                 ? ""
145                                 : sprintf("%d", potential_health)
146                         ), s);
147
148                 s = strreplace("{health}", (
149                         (health == potential_health || !autocvar_cl_damagetext_format_verbose)
150                                 ? sprintf("%d",      health)
151                                 : sprintf("%d (%d)", health, potential_health)
152                         ), s);
153                 s = strreplace("{total}", (
154                         (total == potential || !autocvar_cl_damagetext_format_verbose)
155                                 ? sprintf("%d",      total)
156                                 : sprintf("%d (%d)", total, potential)
157                         ), s);
158
159                 // futureproofing: remove any remaining (unknown) format strings
160                 // in case we add new ones in the future so players can use them
161                 // on new servers and still have working damagetext on old ones
162                 while (true)
163                 {
164                         int opening_pos = strstrofs(s, "{", 0);
165                         if (opening_pos == -1) break;
166                         int closing_pos = strstrofs(s, "}", opening_pos);
167                         if (closing_pos == -1 || closing_pos <= opening_pos) break;
168                         s = strcat(
169                                 substring(s, 0, opening_pos),
170                                 substring_range(s, closing_pos + 1, strlen(s))
171                         );
172                 }
173
174                 strcpy(this.text, s);
175
176                 this.m_size = map_bound_ranges(potential,
177                         autocvar_cl_damagetext_size_min_damage, autocvar_cl_damagetext_size_max_damage,
178                         autocvar_cl_damagetext_size_min,        autocvar_cl_damagetext_size_max);
179         }
180
181         CONSTRUCTOR(DamageText, int _group, vector _origin, bool _screen_coords,
182                 int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire)
183         {
184                 CONSTRUCT(DamageText);
185                 this.m_group = _group;
186                 this.m_friendlyfire = _friendlyfire;
187                 this.m_screen_coords = _screen_coords;
188                 if (_screen_coords)
189                 {
190                         if (autocvar_cl_damagetext_2d_alpha_lifetime)
191                                 this.fade_rate = 1 / autocvar_cl_damagetext_2d_alpha_lifetime;
192                         if (autocvar_cl_damagetext_2d_size_lifetime)
193                                 this.m_shrink_rate = 1 / autocvar_cl_damagetext_2d_size_lifetime;
194                 }
195                 else
196                 {
197                         if (autocvar_cl_damagetext_alpha_lifetime)
198                                 this.fade_rate = 1 / autocvar_cl_damagetext_alpha_lifetime;
199                         this.m_shrink_rate = 0;
200                 }
201                 DamageText_update(this, _origin, _screen_coords,
202                         _health, _armor, _potential_damage, _deathtype);
203                 IL_PUSH(g_damagetext, this);
204         }
205
206         DESTRUCTOR(DamageText)
207         {
208                 strfree(this.text);
209                 --DamageText_screen_count;
210         }
211 ENDCLASS(DamageText)
212
213 float current_alpha(entity damage_text)
214 {
215         // alpha doesn't change - actual alpha is always calculated from the initial value
216         return damage_text.alpha - (time - damage_text.hit_time) * damage_text.fade_rate;
217 }
218
219 NET_HANDLE(damagetext, bool isNew)
220 {
221         make_pure(this);
222         int server_entity_index = ReadByte();
223         int deathtype = ReadInt24_t();
224         int flags = ReadByte();
225         bool friendlyfire = flags & DTFLAG_SAMETEAM;
226
227         int health, armor, potential_damage;
228         if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t();
229         else health = ReadShort();
230         if (flags & DTFLAG_NO_ARMOR) armor = 0;
231         else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t();
232         else armor = ReadShort();
233         if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor;
234         else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t();
235         else potential_damage = ReadShort();
236
237         return = true;
238         if (!isNew) return;
239         if (autocvar_cl_damagetext == 0) return;
240         if (friendlyfire)
241         {
242                 if (autocvar_cl_damagetext_friendlyfire == 0)
243                         return;
244
245                 if (autocvar_cl_damagetext_friendlyfire == 1
246                 && health == 0 && armor == 0)
247                         return;
248         }
249
250         int client_entity_index = server_entity_index - 1;
251         entity entcs = entcs_receiver(client_entity_index);
252
253         bool can_use_3d = entcs && entcs.has_origin;
254         bool too_close = vdist(entcs.origin - view_origin, <, autocvar_cl_damagetext_2d_close_range);
255         bool prefer_in_view = autocvar_cl_damagetext_2d_out_of_view && !projected_on_screen(project_3d_to_2d(entcs.origin));
256         bool prefer_2d = spectatee_status != -1 && autocvar_cl_damagetext_2d && (too_close || prefer_in_view);
257
258         vector position;
259         bool is2d;
260         entity entDT = NULL; // which DT to update
261
262         float alphathreshold = autocvar_cl_damagetext_accumulate_alpha_rel
263                         * autocvar_cl_damagetext_alpha_start;
264
265         // check if this entity already has a DamageText for it
266         IL_EACH(g_damagetext, it.m_group == server_entity_index, {
267                 // if the time window where damage accumulates closes,
268                 // disown the parent entity from this DamageText
269                 // and (likely) give the entity a new DT afterwards
270                 // this should only cancel damage accumulation for this DT
271                 if ((autocvar_cl_damagetext_accumulate_lifetime >= 0) // negative never disowns
272                 && (time - it.hit_time > autocvar_cl_damagetext_accumulate_lifetime)
273                 && (current_alpha(it) > alphathreshold))
274                 {
275                         it.m_group = 0;
276                 }
277                 else
278                 {
279                         health += it.m_healthdamage;
280                         armor += it.m_armordamage;
281                         potential_damage += it.m_potential_damage;
282
283                         entDT = it;
284                 }
285                 break;
286         });
287
288         if (can_use_3d && !prefer_2d)
289         {
290                 // world coords
291                 is2d = false;
292                 position = entcs.origin;
293
294                 if (entDT)
295                         goto updateDT;
296
297                 // 3D DamageTexts can later be changed into 2D,
298                 // increment this here just in case
299                 ++DamageText_screen_count;
300                 goto spawnnewDT;
301         }
302         else if (autocvar_cl_damagetext_2d && spectatee_status != -1)
303         {
304                 // screen coords only
305                 is2d = true;
306                 position = vec2(vid_conwidth  * autocvar_cl_damagetext_2d_pos.x,
307                                 vid_conheight * autocvar_cl_damagetext_2d_pos.y);
308
309                 if (entDT)
310                         goto updateDT;
311
312                 // offset when hitting multiple enemies, dmgtext would overlap
313                 position += autocvar_cl_damagetext_2d_overlap_offset * DamageText_screen_count++;
314                 goto spawnnewDT;
315         }
316         return;
317
318 LABEL(updateDT)
319         DamageText_update(entDT, position, is2d, health, armor, potential_damage, deathtype);
320         return;
321
322 LABEL(spawnnewDT)
323         NEW(DamageText, server_entity_index, position, is2d,
324                 health, armor, potential_damage, deathtype, friendlyfire);
325         return;
326 }