-#ifndef MUTATOR_DAMAGETEXT_H
-#define MUTATOR_DAMAGETEXT_H
+#include "damagetext.qh"
-#ifdef MENUQC
-#include <menu/xonotic/tab.qh>
-#endif
-
-#endif
+#define DAMAGETEXT_PRECISION_MULTIPLIER 128
+#define DAMAGETEXT_MAX_SHORT 255 // 2^15 (signed short) / DAMAGETEXT_PRECISION_MULTIPLIER - 1
-#ifdef IMPLEMENTATION
REGISTER_MUTATOR(damagetext, true);
#if defined(CSQC) || defined(MENUQC)
// no translatable cvar description please
-AUTOCVAR_SAVE(cl_damagetext, bool, false, "Draw damage dealt where you hit the enemy");
+AUTOCVAR_SAVE(cl_damagetext, bool, true, "Draw damage dealt where you hit the enemy");
AUTOCVAR_SAVE(cl_damagetext_format, string, "-{total}", "How to format the damage text. {health}, {armor}, {total}");
STATIC_INIT(DamageText_LegacyFormat) {
if (strstrofs(autocvar_cl_damagetext_format, "{", 0) < 0) autocvar_cl_damagetext_format = "-{total}";
#ifdef CSQC
CLASS(DamageText, Object)
- ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color)
- ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color)
- ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size)
- ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start)
- ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime)
- ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity)
- ATTRIB(DamageText, m_group, int, 0)
- ATTRIB(DamageText, m_friendlyfire, bool, false)
- ATTRIB(DamageText, m_damage, int, 0)
- ATTRIB(DamageText, m_armordamage, int, 0)
- ATTRIB(DamageText, m_deathtype, int, 0)
- ATTRIB(DamageText, time_prev, float, time)
+ ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color);
+ ATTRIB(DamageText, m_color_friendlyfire, vector, autocvar_cl_damagetext_friendlyfire_color);
+ ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size);
+ ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start);
+ ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime);
+ ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity);
+ ATTRIB(DamageText, m_group, int, 0);
+ ATTRIB(DamageText, m_friendlyfire, bool, false);
+ ATTRIB(DamageText, m_damage, int, 0);
+ ATTRIB(DamageText, m_armordamage, int, 0);
+ ATTRIB(DamageText, m_deathtype, int, 0);
+ ATTRIB(DamageText, time_prev, float, time);
void DamageText_draw2d(DamageText this) {
float dt = time - this.time_prev;
this.time_prev = time;
setorigin(this, this.origin + dt * this.velocity);
this.alpha -= dt * this.fade_rate;
- if (this.alpha < 0) remove(this);
+ if (this.alpha < 0) delete(this);
vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
if (pos.z >= 0 && this.m_size > 0) {
pos.z = 0;
if (w != WEP_Null) rgb = w.wpcolor;
}
string s = autocvar_cl_damagetext_format;
- s = strreplace("{health}", sprintf("%d", this.m_damage), s);
- s = strreplace("{armor}", sprintf("%d", this.m_armordamage), s);
- s = strreplace("{total}", sprintf("%d", this.m_damage + this.m_armordamage), s);
+ s = strreplace("{health}", sprintf("%d", rint(this.m_damage / DAMAGETEXT_PRECISION_MULTIPLIER)), s);
+ s = strreplace("{armor}", sprintf("%d", rint(this.m_armordamage / DAMAGETEXT_PRECISION_MULTIPLIER)), s);
+ s = strreplace("{total}", sprintf("%d", rint((this.m_damage + this.m_armordamage) / DAMAGETEXT_PRECISION_MULTIPLIER)), s);
drawcolorcodedstring2_builtin(pos, s, this.m_size * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL);
}
}
- ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d)
+ ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d);
void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _deathtype) {
this.m_damage = _health;
this.m_group = _group;
this.m_friendlyfire = _friendlyfire;
DamageText_update(this, _origin, _health, _armor, _deathtype);
+ IL_PUSH(g_drawables_2d, this);
}
ENDCLASS(DamageText)
#endif
if (SV_DAMAGETEXT_DISABLED()) return;
const entity attacker = M_ARGV(0, entity);
const entity hit = M_ARGV(1, entity); if (hit == attacker) return;
- const int health = M_ARGV(2, int);
- const int armor = M_ARGV(3, int);
+ const float health = M_ARGV(2, float);
+ const float armor = M_ARGV(3, float);
const int deathtype = M_ARGV(5, int);
const vector location = hit.origin;
FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
(SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_SPEC(it) && it.enemy == attacker) ||
(SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_OBSERVER(it))
) {
+ int flags = SAME_TEAM(hit, attacker); // BIT(0)
+ if (health > DAMAGETEXT_MAX_SHORT) flags |= BIT(1);
+ if (armor > DAMAGETEXT_MAX_SHORT) flags |= BIT(2);
+
msg_entity = it;
WriteHeader(MSG_ONE, damagetext);
- WriteShort(MSG_ONE, rint(health));
- WriteShort(MSG_ONE, rint(armor));
WriteEntity(MSG_ONE, hit);
WriteCoord(MSG_ONE, location.x);
WriteCoord(MSG_ONE, location.y);
WriteCoord(MSG_ONE, location.z);
WriteInt24_t(MSG_ONE, deathtype);
- WriteByte(MSG_ONE, SAME_TEAM(hit, attacker));
+ WriteByte(MSG_ONE, flags);
+
+ // we need to send a few decimal places to minimize errors when accumulating damage
+ // sending them multiplied saves bandwidth compared to using WriteCoord,
+ // however if the multiplied damage would be too much for (signed) short, we send an int24
+ if (health > DAMAGETEXT_MAX_SHORT) WriteInt24_t(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER);
+ else WriteShort(MSG_ONE, health * DAMAGETEXT_PRECISION_MULTIPLIER);
+ if (armor > DAMAGETEXT_MAX_SHORT) WriteInt24_t(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER);
+ else WriteShort(MSG_ONE, armor * DAMAGETEXT_PRECISION_MULTIPLIER);
}
));
}
#ifdef CSQC
NET_HANDLE(damagetext, bool isNew)
{
- int health = ReadShort();
- int armor = ReadShort();
int group = ReadShort();
vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
int deathtype = ReadInt24_t();
- bool friendlyfire = ReadByte();
+ int flags = ReadByte();
+ bool friendlyfire = flags & 1;
+
+ int health, armor;
+ if (flags & BIT(1)) health = ReadInt24_t();
+ else health = ReadShort();
+ if (flags & BIT(2)) armor = ReadInt24_t();
+ else armor = ReadShort();
+
return = true;
if (autocvar_cl_damagetext) {
if (friendlyfire && !autocvar_cl_damagetext_friendlyfire) {
}
}
}
- NEW(DamageText, group, location, health, armor, deathtype, friendlyfire);
+ make_impure(NEW(DamageText, group, location, health, armor, deathtype, friendlyfire));
}
}
#endif
#ifdef MENUQC
+
+#include <menu/gamesettings.qh>
+
CLASS(XonoticDamageTextSettings, XonoticTab)
- #include <menu/gamesettings.qh>
REGISTER_SETTINGS(damagetext, NEW(XonoticDamageTextSettings));
- ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text"))
- ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9)
- ATTRIB(XonoticDamageTextSettings, rows, float, 15.5)
- ATTRIB(XonoticDamageTextSettings, columns, float, 5)
+ ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text"));
+ ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9);
+ ATTRIB(XonoticDamageTextSettings, rows, float, 15.5);
+ ATTRIB(XonoticDamageTextSettings, columns, float, 5);
INIT(XonoticDamageTextSettings) { this.configureDialog(this); }
METHOD(XonoticDamageTextSettings, showNotify, void(entity this)) { loadAllCvars(this); }
METHOD(XonoticDamageTextSettings, fill, void(entity this))
setDependent(e, "cl_damagetext", 1, 1);
this.TR(this);
this.TR(this);
- // friendly fire
this.TD(this, 1, 3, e = makeXonoticCheckBox(0, "cl_damagetext_friendlyfire", _("Draw damage numbers for friendly fire")));
setDependent(e, "cl_damagetext", 1, 1);
this.TR(this);
- this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color (Friendly Fire):")));
+ this.TD(this, 1, 1, e = makeXonoticTextLabel(0, _("Color:")));
setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
this.TD(this, 2, 2, e = makeXonoticColorpickerString("cl_damagetext_friendlyfire_color", "cl_damagetext_friendlyfire_color"));
setDependentAND(e, "cl_damagetext", 1, 1, "cl_damagetext_friendlyfire", 1, 1);
}
ENDCLASS(XonoticDamageTextSettings)
#endif
-#endif