5fb4b1bf4cc3615bf98aefffab3b6e2a7db7eaca
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / damagetext / damagetext.qc
1 #ifndef MUTATOR_DAMAGETEXT_H
2 #define MUTATOR_DAMAGETEXT_H
3
4 #ifdef MENUQC
5 #include <menu/xonotic/tab.qc>
6 #endif
7
8 #endif
9
10 #ifdef IMPLEMENTATION
11 REGISTER_MUTATOR(damagetext, true);
12
13 #if defined(CSQC) || defined(MENUQC)
14 AUTOCVAR_SAVE(cl_damagetext,                    bool,   false,      _("Draw damage dealt. 0: disabled, 1: enabled"));
15 AUTOCVAR_SAVE(cl_damagetext_format,             string, "-%3$d",    _("How to format the damage text. 1$ is health, 2$ is armor, 3$ is both"));
16 AUTOCVAR_SAVE(cl_damagetext_color,              vector, '1 1 0',    _("Default damage text color"));
17 AUTOCVAR_SAVE(cl_damagetext_color_per_weapon,   bool,   false,      _("Damage text uses weapon color"));
18 AUTOCVAR_SAVE(cl_damagetext_size,               float,  8,          _("Damage text font size"));
19 AUTOCVAR_SAVE(cl_damagetext_alpha_start,        float,  1,          _("Damage text initial alpha"));
20 AUTOCVAR_SAVE(cl_damagetext_alpha_lifetime,     float,  3,          _("Damage text lifetime in seconds"));
21 AUTOCVAR_SAVE(cl_damagetext_velocity,           vector, '0 0 20',   _("Damage text move direction"));
22 AUTOCVAR_SAVE(cl_damagetext_offset,             vector, '0 -40 0',  _("Damage text offset"));
23 AUTOCVAR_SAVE(cl_damagetext_accumulate_range,   float,  30,         _("Damage text spawned within this range is accumulated"));
24 #endif
25
26 #ifdef CSQC
27 CLASS(DamageText, Object)
28     ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color)
29     ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size)
30     ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start)
31     ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime)
32     ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity)
33     ATTRIB(DamageText, m_group, int, 0)
34     ATTRIB(DamageText, m_damage, int, 0)
35     ATTRIB(DamageText, m_armordamage, int, 0)
36     ATTRIB(DamageText, m_deathtype, int, 0)
37     ATTRIB(DamageText, time_prev, float, time)
38
39     void DamageText_draw2d(DamageText this) {
40         float dt = time - this.time_prev;
41         this.time_prev = time;
42         setorigin(this, this.origin + dt * this.velocity);
43         this.alpha -= dt * this.fade_rate;
44         if (this.alpha < 0) remove(this);
45         vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
46         if (pos.z >= 0 && this.m_size > 0) {
47             pos.z = 0;
48             vector rgb = this.m_color;
49             if (autocvar_cl_damagetext_color_per_weapon) {
50                 Weapon w = DEATH_WEAPONOF(this.m_deathtype);
51                 if (w != WEP_Null) rgb = w.wpcolor;
52             }
53             string s = sprintf(autocvar_cl_damagetext_format, this.m_damage, this.m_armordamage, this.m_damage + this.m_armordamage);
54             drawcolorcodedstring2(pos, s, this.m_size * '1 1 0', rgb, this.alpha, DRAWFLAG_NORMAL);
55         }
56     }
57     ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d)
58
59     void DamageText_update(DamageText this, vector _origin, int _health, int _armor, int _deathtype) {
60         this.m_damage = _health;
61         this.m_armordamage = _armor;
62         this.m_deathtype = _deathtype;
63         setorigin(this, _origin);
64         this.alpha = 1;
65     }
66
67     CONSTRUCTOR(DamageText, int _group, vector _origin, int _health, int _armor, int _deathtype) {
68         CONSTRUCT(DamageText);
69         this.m_group = _group;
70         DamageText_update(this, _origin, _health, _armor, _deathtype);
71     }
72 ENDCLASS(DamageText)
73 #endif
74
75 REGISTER_NET_TEMP(damagetext)
76
77 #ifdef SVQC
78 AUTOCVAR(sv_damagetext, int, 2, _("<= 0: disabled, >= 1: spectators, >= 2: players, >= 3: all players"));
79 #define SV_DAMAGETEXT_DISABLED()        (autocvar_sv_damagetext <= 0 /* disabled */)
80 #define SV_DAMAGETEXT_SPECTATORS_ONLY() (autocvar_sv_damagetext >= 1 /* spectators only */)
81 #define SV_DAMAGETEXT_PLAYERS()         (autocvar_sv_damagetext >= 2 /* players */)
82 #define SV_DAMAGETEXT_ALL()             (autocvar_sv_damagetext >= 3 /* all players */)
83 MUTATOR_HOOKFUNCTION(damagetext, PlayerDamaged) {
84     if (SV_DAMAGETEXT_DISABLED()) return;
85     const entity attacker = MUTATOR_ARGV(0, entity);
86     const entity hit = MUTATOR_ARGV(1, entity); if (hit == attacker) return;
87     const int health = MUTATOR_ARGV(0, int);
88     const int armor = MUTATOR_ARGV(1, int);
89     const int deathtype = MUTATOR_ARGV(2, int);
90     const vector location = hit.origin;
91     FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
92         if (
93             (SV_DAMAGETEXT_ALL()) ||
94             (SV_DAMAGETEXT_PLAYERS() && it == attacker) ||
95             (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_SPEC(it) && it.enemy == attacker) ||
96             (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_OBSERVER(it))
97         ) {
98             msg_entity = it;
99             WriteHeader(MSG_ONE, damagetext);
100             WriteShort(MSG_ONE, health);
101             WriteShort(MSG_ONE, armor);
102             WriteEntity(MSG_ONE, hit);
103             WriteCoord(MSG_ONE, location.x);
104             WriteCoord(MSG_ONE, location.y);
105             WriteCoord(MSG_ONE, location.z);
106             WriteInt24_t(MSG_ONE, deathtype);
107         }
108     ));
109 }
110 #endif
111
112 #ifdef CSQC
113 NET_HANDLE(damagetext, bool isNew)
114 {
115     int health = ReadShort();
116     int armor = ReadShort();
117     int group = ReadShort();
118     vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
119     int deathtype = ReadInt24_t();
120     return = true;
121     if (autocvar_cl_damagetext) {
122         if (autocvar_cl_damagetext_accumulate_range) {
123             for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
124                 if (e.instanceOfDamageText && e.m_group == group) {
125                     DamageText_update(e, location, e.m_damage + health, e.m_armordamage + armor, deathtype);
126                     return;
127                 }
128             }
129         }
130         NEW(DamageText, group, location, health, armor, deathtype);
131     }
132 }
133 #endif
134
135 #ifdef MENUQC
136 CLASS(XonoticDamageTextSettings, XonoticTab)
137     #include <menu/gamesettings.qh>
138     REGISTER_SETTINGS(damagetext, NEW(XonoticDamageTextSettings));
139     ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text"))
140     ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9)
141     ATTRIB(XonoticDamageTextSettings, rows, float, 15.5)
142     ATTRIB(XonoticDamageTextSettings, columns, float, 5)
143     INIT(XonoticDamageTextSettings) { this.configureDialog(this); }
144     METHOD(XonoticDamageTextSettings, showNotify, void(entity this)) { loadAllCvars(this); }
145     METHOD(XonoticDamageTextSettings, fill, void(entity this))
146     {
147         this.gotoRC(this, 0, 1); this.setFirstColumn(this, this.currentColumn);
148             this.TD(this, 1, 3, makeXonoticCheckBox(0, "cl_damagetext", _("Draw damage numbers")));
149         this.TR(this);
150             this.TD(this, 1, 1, makeXonoticTextLabel(0, _("Font size:")));
151             this.TD(this, 1, 2, makeXonoticSlider(0, 50, 1, "cl_damagetext_size"));
152         this.TR(this);
153             this.TD(this, 1, 1, makeXonoticTextLabel(0, _("Accumulate range:")));
154             this.TD(this, 1, 2, makeXonoticSlider(0, 500, 1, "cl_damagetext_accumulate_range"));
155         this.TR(this);
156             this.TD(this, 1, 1, makeXonoticTextLabel(0, _("Lifetime:")));
157             this.TD(this, 1, 2, makeXonoticSlider(0, 10, 1, "cl_damagetext_alpha_lifetime"));
158         this.TR(this);
159             this.TD(this, 1, 1, makeXonoticTextLabel(0, _("Color:")));
160             this.TD(this, 2, 2, makeXonoticColorpickerString("cl_damagetext_color", "cl_damagetext_color"));
161     }
162 ENDCLASS(XonoticDamageTextSettings)
163 #endif
164 #endif