1#include "damagetext.qh" 2 3#include "lib/math.qh" 4 5#define DAMAGETEXT_PRECISION_MULTIPLIER 128 6#define DAMAGETEXT_SHORT_LIMIT 256 // the smallest value that we can't send as short - 2^15 (signed short) / DAMAGETEXT_PRECISION_MULTIPLIER 7 8const int DTFLAG_SAMETEAM = BIT(0); 9const int DTFLAG_BIG_HEALTH = BIT(1); 10const int DTFLAG_BIG_ARMOR = BIT(2); 11const int DTFLAG_BIG_POTENTIAL = BIT(3); 12const int DTFLAG_NO_ARMOR = BIT(4); 13const int DTFLAG_NO_POTENTIAL = BIT(5); 14 15REGISTER_MUTATOR(damagetext, true); 16 17// || defined(MENUQC) 18#if defined(CSQC) 19// no translatable cvar description please 20AUTOCVAR_SAVE(cl_damagetext, bool, true, "Draw damage dealt where you hit the enemy"); 21AUTOCVAR_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"); 22AUTOCVAR_SAVE(cl_damagetext_format_verbose, bool, false, "{health} shows {potential_health} too when they differ; {total} shows {potential} too when they differ"); 23AUTOCVAR_SAVE(cl_damagetext_format_hide_redundant, bool, false, "hide {armor} if 0; hide {potential} and {potential_health} when same as actual"); 24STATIC_INIT(DamageText_LegacyFormat) { 25 // damagetext used to be off by default and the cvar got saved in people's configs along with the old format 26 // enable damagetext while updating the format for a one time effect 27 if (strstrofs(autocvar_cl_damagetext_format, "{", 0) < 0) { 28 localcmd("\nseta cl_damagetext 1\n"); 29 localcmd("\nseta cl_damagetext_format -{total}\n"); 30 }; 31} 32AUTOCVAR_SAVE(cl_damagetext_color, vector, '1 1 0', "Damage text color"); 33AUTOCVAR_SAVE(cl_damagetext_color_per_weapon, bool, false, "Damage text uses weapon color"); 34AUTOCVAR_SAVE(cl_damagetext_size_min, float, 10, "Damage text font size for small damage"); 35AUTOCVAR_SAVE(cl_damagetext_size_min_damage, float, 25, "How much damage is considered small"); 36AUTOCVAR_SAVE(cl_damagetext_size_max, float, 16, "Damage text font size for large damage"); 37AUTOCVAR_SAVE(cl_damagetext_size_max_damage, float, 140, "How much damage is considered large"); 38AUTOCVAR_SAVE(cl_damagetext_alpha_start, float, 1, "Damage text initial alpha"); 39AUTOCVAR_SAVE(cl_damagetext_alpha_lifetime, float, 3, "Damage text lifetime in seconds"); 40AUTOCVAR_SAVE(cl_damagetext_velocity, vector, '0 0 20', "Damage text move direction"); 41AUTOCVAR_SAVE(cl_damagetext_offset, vector, '0 -40 0', "Damage text offset"); 42AUTOCVAR_SAVE(cl_damagetext_accumulate_range, float, 30, "Damage text spawned within this range is accumulated"); 43AUTOCVAR_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"); 44AUTOCVAR_SAVE(cl_damagetext_friendlyfire, bool, true, "Show damage text for friendlyfire too"); 45AUTOCVAR_SAVE(cl_damagetext_friendlyfire_color, vector, '1 0 0', "Damage text color for friendlyfire"); 46#endif 47 48#ifdef CSQC 49CLASS(DamageText, Object) 50 ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color); 51 ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color); 52 ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size_min); 53 ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start); 54 ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime); 55 ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity); 56 ATTRIB(DamageText, m_group, int, 0); 57 ATTRIB(DamageText, m_friendlyfire, bool, false); 58 ATTRIB(DamageText, m_healthdamage, int, 0); 59 ATTRIB(DamageText, m_armordamage, int, 0); 60 ATTRIB(DamageText, m_potential_damage, int, 0); 61 ATTRIB(DamageText, m_deathtype, int, 0); 62 ATTRIB(DamageText, time_prev, float, time); 63 ATTRIB(DamageText, text, string, string_null); 64 65 void DamageText_draw2d(DamageText this) { 66 float dt = time - this.time_prev; 67 this.time_prev = time; 68 setorigin(this, this.origin + dt * this.velocity); 69 this.alpha -= dt * this.fade_rate; 70 if (this.alpha < 0) delete(this); 71 vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset; 72 if (pos.z >= 0 && this.m_size > 0) { 73 pos.z = 0; 74 vector rgb; 75 if (this.m_friendlyfire) { 76 rgb = this.m_color_friendlyfire; 77 } else { 78 rgb = this.m_color; 79 } 80 if (autocvar_cl_damagetext_color_per_weapon) { 81 Weapon w = DEATH_WEAPONOF(this.m_deathtype); 82 if (w != WEP_Null) rgb = w.wpcolor; 83 } 84 85 vector drawfontscale_save = drawfontscale; 86 drawfontscale = (this.m_size / autocvar_cl_damagetext_size_max) * '1 1 0'; 87 drawcolorcodedstring2_builtin(pos, this.text, autocvar_cl_damagetext_size_max * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL); 88 drawfontscale = drawfontscale_save; 89 } 90 } 91 ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d); 92 93 void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _potential_damage, int _deathtype) { 94 this.m_healthdamage = _health; 95 this.m_armordamage = _armor; 96 this.m_potential_damage = _potential_damage; 97 this.m_deathtype = _deathtype; 98 setorigin(this, _origin); 99 this.alpha = autocvar_cl_damagetext_alpha_start; 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, 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 DamageText_update(this, _origin, _health, _armor, _potential_damage, _deathtype); 167 IL_PUSH(g_drawables_2d, this); 168 } 169 170 DESTRUCTOR(DamageText) { 171 if (this.text) strunzone(this.text); 172 } 173ENDCLASS(DamageText) 174#endif 175 176REGISTER_NET_TEMP(damagetext) 177 178#ifdef SVQC 179AUTOCVAR(sv_damagetext, int, 2, "<= 0: disabled, >= 1: spectators, >= 2: players, >= 3: all players"); 180#define SV_DAMAGETEXT_DISABLED() (autocvar_sv_damagetext <= 0 /* disabled */) 181#define SV_DAMAGETEXT_SPECTATORS_ONLY() (autocvar_sv_damagetext >= 1 /* spectators only */) 182#define SV_DAMAGETEXT_PLAYERS() (autocvar_sv_damagetext >= 2 /* players */) 183#define SV_DAMAGETEXT_ALL() (autocvar_sv_damagetext >= 3 /* all players */) 184MUTATOR_HOOKFUNCTION(damagetext, PlayerDamaged) { 185 if (SV_DAMAGETEXT_DISABLED()) return; 186 const entity attacker = M_ARGV(0, entity); 187 const entity hit = M_ARGV(1, entity); if (hit == attacker) return; 188 const float health = M_ARGV(2, float); 189 const float armor = M_ARGV(3, float); 190 const int deathtype = M_ARGV(5, int); 191 const float potential_damage = M_ARGV(6, float); 192 const vector location = hit.origin; 193 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA( 194 if ( 195 (SV_DAMAGETEXT_ALL()) || 196 (SV_DAMAGETEXT_PLAYERS() && it == attacker) || 197 (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_SPEC(it) && it.enemy == attacker) || 198 (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_OBSERVER(it)) 199 ) { 200 int flags = 0; 201 if (SAME_TEAM(hit, attacker)) flags |= DTFLAG_SAMETEAM; 202 if (health >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_HEALTH; 203 if (armor >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_ARMOR; 204 if (potential_damage >= DAMAGETEXT_SHORT_LIMIT) flags |= DTFLAG_BIG_POTENTIAL; 205 if (!armor) flags |= DTFLAG_NO_ARMOR; 206 if (almost_equals_eps(armor + health, potential_damage, 5)) flags |= DTFLAG_NO_POTENTIAL; 207 208 msg_entity = it; 209 WriteHeader(MSG_ONE, damagetext); 210 WriteEntity(MSG_ONE, hit); 211 WriteCoord(MSG_ONE, location.x); 212 WriteCoord(MSG_ONE, location.y); 213 WriteCoord(MSG_ONE, location.z); 214 WriteInt24_t(MSG_ONE, deathtype); 215 WriteByte(MSG_ONE, flags); 216 217 // we need to send a few decimal places to minimize errors when accumulating damage 218 // sending them multiplied saves bandwidth compared to using WriteCoord, 219 // however if the multiplied damage would be too much for (signed) short, we send an int24 220 if (flags & DTFLAG_BIG_HEALTH) WriteInt24_t(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER); 221 else WriteShort(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER); 222 if (!(flags & DTFLAG_NO_ARMOR)) 223 { 224 if (flags & DTFLAG_BIG_ARMOR) WriteInt24_t(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER); 225 else WriteShort(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER); 226 } 227 if (!(flags & DTFLAG_NO_POTENTIAL)) 228 { 229 if (flags & DTFLAG_BIG_POTENTIAL) WriteInt24_t(MSG_ONE, potential_damage * DAMAGETEXT_PRECISION_MULTIPLIER); 230 else WriteShort(MSG_ONE, potential_damage * DAMAGETEXT_PRECISION_MULTIPLIER); 231 } 232 } 233 )); 234} 235#endif 236 237#ifdef CSQC 238NET_HANDLE(damagetext, bool isNew) 239{ 240 int group = ReadShort(); 241 vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord()); 242 int deathtype = ReadInt24_t(); 243 int flags = ReadByte(); 244 bool friendlyfire = flags & DTFLAG_SAMETEAM; 245 246 int health, armor, potential_damage; 247 if (flags & DTFLAG_BIG_HEALTH) health = ReadInt24_t(); 248 else health = ReadShort(); 249 if (flags & DTFLAG_NO_ARMOR) armor = 0; 250 else if (flags & DTFLAG_BIG_ARMOR) armor = ReadInt24_t(); 251 else armor = ReadShort(); 252 if (flags & DTFLAG_NO_POTENTIAL) potential_damage = health + armor; 253 else if (flags & DTFLAG_BIG_POTENTIAL) potential_damage = ReadInt24_t(); 254 else potential_damage = ReadShort(); 255 256 return = true; 257 if (autocvar_cl_damagetext) { 258 if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) { 259 return; 260 } 261 if (autocvar_cl_damagetext_accumulate_range) { 262 for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) { 263 if (e.instanceOfDamageText && e.m_group == group && e.alpha > autocvar_cl_damagetext_accumulate_alpha_rel * autocvar_cl_damagetext_alpha_start) { 264 DamageText_update(e, location, e.m_healthdamage + health, e.m_armordamage + armor, e.m_potential_damage + potential_damage, deathtype); 265 return; 266 } 267 } 268 } 269 make_impure(NEW(DamageText, group, location, health, armor, potential_damage, deathtype, friendlyfire)); 270 } 271} 272#endif 273 274#ifdef MENUQC 275 276#include <menu/gamesettings.qh> 277 278CLASS(XonoticDamageTextSettings, XonoticTab) 279 REGISTER_SETTINGS(damagetext, NEW(XonoticDamageTextSettings)); 280 ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text")); 281 ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9); 282 ATTRIB(XonoticDamageTextSettings, rows, float, 15.5); 283 ATTRIB(XonoticDamageTextSettings, columns, float, 5); 284 INIT(XonoticDamageTextSettings) { this.configureDialog(this); } 285 METHOD(XonoticDamageTextSettings, showNotify, void(entity this)) { loadAllCvars(this); } 286 METHOD(XonoticDamageTextSettings, fill, void(entity this)) 287 { 288 entity e; 289 this.gotoRC(this, 0, 1); this.setFirstColumn(this, this.currentColumn); 290 this.TD(this, 1, 3, makeXonoticCheckBox(0, "cl_damagetext", _("Draw damage numbers"))); 291 this.TR(this); 292 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Font size minimum:"))); 293 setDependent(e, "cl_damagetext", 1, 1); 294 this.TD(this, 1, 2, e = makeXonoticSlider(0, 50, 1, "cl_damagetext_size_min")); 295 setDependent(e, "cl_damagetext", 1, 1); 296 this.TR(this); 297 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Font size maximum:"))); 298 setDependent(e, "cl_damagetext", 1, 1); 299 this.TD(this, 1, 2, e = makeXonoticSlider(0, 50, 1, "cl_damagetext_size_max")); 300 setDependent(e, "cl_damagetext", 1, 1); 301 this.TR(this); 302 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Accumulate range:"))); 303 setDependent(e, "cl_damagetext", 1, 1); 304 this.TD(this, 1, 2, e = makeXonoticSlider(0, 500, 1, "cl_damagetext_accumulate_range")); 305 setDependent(e, "cl_damagetext", 1, 1); 306 this.TR(this); 307 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Lifetime:"))); 308 setDependent(e, "cl_damagetext", 1, 1); 309 this.TD(this, 1, 2, e = makeXonoticSlider(0, 10, 1, "cl_damagetext_alpha_lifetime")); 310 setDependent(e, "cl_damagetext", 1, 1); 311 this.TR(this); 312 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:"))); 313 setDependent(e, "cl_damagetext", 1, 1); 314 this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_color", "cl_damagetext_color")); 315 setDependent(e, "cl_damagetext", 1, 1); 316 this.TR(this); 317 this.TR(this); 318 // friendly fire 319 this.TD(this, 1, 3, e = makeXonoticCheckBox(0, "cl_damagetext_friendlyfire", _("Draw damage numbers for friendly fire"))); 320 setDependent(e, "cl_damagetext", 1, 1); 321 this.TR(this); 322 this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:"))); 323 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1); 324 this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_friendlyfire_color", "cl_damagetext_friendlyfire_color")); 325 setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1); 326 this.TR(this); 327 } 328ENDCLASS(XonoticDamageTextSettings) 329#endif 330