Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
cl_damagetext.qc
Go to the documentation of this file.
1#include "cl_damagetext.qh"
2
19
21
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 {
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
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)
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
86 {
87 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
88 if (w != WEP_Null) rgb = w.m_color;
89 }
90
91 vector drawfontscale_save = drawfontscale;
93 screen_pos.y -= drawfontscale.x * size / 2;
94 drawcolorcodedstring2_builtin(screen_pos, this.text,
96 rgb, alpha_, DRAWFLAG_NORMAL);
97 drawfontscale = drawfontscale_save;
98 }
99 }
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)
114 else
116
117 int health = rint(this.m_healthdamage
119 int armor = rint(this.m_armordamage
121 int total = rint((this.m_healthdamage + this.m_armordamage)
123 int potential = rint(this.m_potential_damage
125 int potential_health = rint((this.m_potential_damage - this.m_armordamage)
127
128 bool redundant = almost_equals_eps(this.m_healthdamage + this.m_armordamage,
129 this.m_potential_damage, 5);
130
132 s = strreplace("{armor}", (
134 ? ""
135 : sprintf("%d", armor)
136 ), s);
137 s = strreplace("{potential}", (
139 ? ""
140 : sprintf("%d", potential)
141 ), s);
142 s = strreplace("{potential_health}", (
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,
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 {
185 this.m_group = _group;
186 this.m_friendlyfire = _friendlyfire;
187 this.m_screen_coords = _screen_coords;
188 if (_screen_coords)
189 {
194 }
195 else
196 {
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
207 {
208 strfree(this.text);
209 --DamageText_screen_count;
210 }
212
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
219NET_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;
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 {
243 return;
244
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);
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
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
272 if (flags & DTFLAG_STOP_ACCUMULATION
273 || (current_alpha(it) < alphathreshold)
274 || ((autocvar_cl_damagetext_accumulate_lifetime >= 0) // negative never disowns
275 && (time - it.hit_time > autocvar_cl_damagetext_accumulate_lifetime)))
276 {
277 it.m_group = 0;
278 }
279 else
280 {
281 health += it.m_healthdamage;
282 armor += it.m_armordamage;
283 potential_damage += it.m_potential_damage;
284
285 entDT = it;
286 }
287 break;
288 });
289
290 if (can_use_3d && !prefer_2d)
291 {
292 // world coords
293 is2d = false;
294 position = entcs.origin;
295
296 if (entDT)
297 goto updateDT;
298
299 // 3D DamageTexts can later be changed into 2D,
300 // increment this here just in case
301 ++DamageText_screen_count;
302 goto spawnnewDT;
303 }
305 {
306 // screen coords only
307 is2d = true;
310
311 if (entDT)
312 goto updateDT;
313
314 // offset when hitting multiple enemies, dmgtext would overlap
315 position += autocvar_cl_damagetext_2d_overlap_offset * DamageText_screen_count++;
316 goto spawnnewDT;
317 }
318 return;
319
320LABEL(updateDT)
321 DamageText_update(entDT, position, is2d, health, armor, potential_damage, deathtype);
322 return;
323
324LABEL(spawnnewDT)
325 NEW(DamageText, server_entity_index, position, is2d,
326 health, armor, potential_damage, deathtype, friendlyfire);
327 return;
328}
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
var float(entity ent) nudgeoutofsolid_OrFallback
float current_alpha(entity damage_text)
float autocvar_cl_damagetext_alpha_lifetime
bool autocvar_cl_damagetext
bool autocvar_cl_damagetext_format_hide_redundant
float autocvar_cl_damagetext_size_min
float autocvar_cl_damagetext_2d_close_range
vector autocvar_cl_damagetext_friendlyfire_color
float autocvar_cl_damagetext_size_min_damage
vector autocvar_cl_damagetext_offset_screen
float autocvar_cl_damagetext_2d_alpha_start
vector autocvar_cl_damagetext_velocity_world
float autocvar_cl_damagetext_accumulate_alpha_rel
float autocvar_cl_damagetext_size_max_damage
float autocvar_cl_damagetext_size_max
bool autocvar_cl_damagetext_2d_out_of_view
vector autocvar_cl_damagetext_2d_pos
vector autocvar_cl_damagetext_color
vector autocvar_cl_damagetext_offset_world
float autocvar_cl_damagetext_alpha_start
string autocvar_cl_damagetext_format
bool autocvar_cl_damagetext_color_per_weapon
float autocvar_cl_damagetext_2d_alpha_lifetime
vector autocvar_cl_damagetext_2d_overlap_offset
bool autocvar_cl_damagetext_format_verbose
float autocvar_cl_damagetext_2d_size_lifetime
vector autocvar_cl_damagetext_velocity_screen
vector autocvar_cl_damagetext_2d_velocity
int autocvar_cl_damagetext_friendlyfire
bool autocvar_cl_damagetext_2d
DamageText(int _group, vector _origin, bool _screen_coords, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire)
void DamageText_draw2d(DamageText this)
float m_shrink_rate
int m_potential_damage
static int screen_count
bool m_screen_coords
vector m_color_friendlyfire
bool m_friendlyfire
vector m_color
float fade_rate
void DamageText_update(DamageText this, vector _origin, bool screen_coords, int _health, int _armor, int _potential_damage, int _deathtype)
fields which are explicitly/manually set are marked with "M", fields set automatically are marked wit...
Definition weapon.qh:44
vector m_color
M: color : waypointsprite color.
Definition weapon.qh:59
vector drawfontscale
Definition draw.qh:3
vector view_origin
Definition main.qh:109
vector hud_fontsize
Definition main.qh:77
IntrusiveList g_damagetext
Definition main.qh:93
int spectatee_status
the -1 disables HUD panels before CSQC receives necessary data
Definition main.qh:197
#define LABEL(id)
Definition compiler.qh:34
float flags
const float DRAWFLAG_NORMAL
vector view_angles
float time
vector size
vector origin
#define false
Definition csprogsdefs.qh:6
#define stringwidth
const int DTFLAG_BIG_HEALTH
Definition damagetext.qh:7
const int DTFLAG_NO_POTENTIAL
Definition damagetext.qh:11
const int DTFLAG_BIG_ARMOR
Definition damagetext.qh:8
const int DTFLAG_SAMETEAM
Definition damagetext.qh:6
const int DTFLAG_BIG_POTENTIAL
Definition damagetext.qh:9
#define DAMAGETEXT_PRECISION_MULTIPLIER
Definition damagetext.qh:3
const int DTFLAG_NO_ARMOR
Definition damagetext.qh:10
#define DEATH_WEAPONOF(t)
Definition all.qh:45
#define MAKE_VECTORS(angles, forward, right, up)
Same as the makevectors builtin but uses the provided locals instead of the v_* globals.
#define strstrofs
#define strlen
#define entcs_receiver(...)
Definition ent_cs.qh:65
entity entcs
Definition ent_cs.qh:34
ERASEABLE void IL_REMOVE(IntrusiveList this, entity it)
Remove any element, anywhere in the list.
ERASEABLE entity IL_PUSH(IntrusiveList this, entity it)
Push to tail.
#define IL_EACH(this, cond, body)
noref float vid_conwidth
Definition draw.qh:8
noref float vid_conheight
Definition draw.qh:9
#define NET_HANDLE(id, param)
Definition net.qh:15
int ReadInt24_t()
Definition net.qh:355
int ReadByte()
ERASEABLE float map_bound_ranges(float value, float src_min, float src_max, float dest_min, float dest_max)
Same as map_ranges except that values outside the source range are clamped to min or max.
Definition math.qh:372
ERASEABLE float almost_equals_eps(float a, float b, float times_eps)
Definition math.qh:226
string substring(string s, float start, float length)
float rint(float f)
string string_null
Definition nil.qh:9
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
#define NEW(cname,...)
Definition oo.qh:117
#define STATIC_ATTRIB(cname, name, type, val)
Definition oo.qh:243
#define make_pure(e)
direct use is
Definition oo.qh:13
#define CLASS(...)
Definition oo.qh:145
#define ENDCLASS(cname)
Definition oo.qh:281
#define DESTRUCTOR(cname)
Definition oo.qh:220
#define ATTRIB(...)
Definition oo.qh:148
#define CONSTRUCTOR(cname,...)
Definition oo.qh:213
#define CONSTRUCT(cname,...)
Definition oo.qh:123
#define NULL
Definition post.qh:14
float health
Legacy fields for the resources. To be removed.
Definition resources.qh:9
vector
Definition self.qh:92
entity this
Definition self.qh:72
ERASEABLE string substring_range(string s, float b, float e)
Definition string.qh:292
#define strfree(this)
Definition string.qh:59
#define strcpy(this, s)
Definition string.qh:52
#define vdist(v, cmp, f)
Vector distance comparison, avoids sqrt()
Definition vector.qh:8
#define vec2(...)
Definition vector.qh:90
vector project_3d_to_2d(vector vec)
Definition view.qc:373
bool projected_on_screen(vector screen_pos)
Definition view.qc:379