]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext.qc
Merge branch 'TimePath/cleanup'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / 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_size,               float,  8,          _("Damage text font size"));
18 AUTOCVAR_SAVE(cl_damagetext_alpha_start,        float,  1,          _("Damage text initial alpha"));
19 AUTOCVAR_SAVE(cl_damagetext_alpha_lifetime,     float,  3,          _("Damage text lifetime in seconds"));
20 AUTOCVAR_SAVE(cl_damagetext_velocity,           vector, '0 0 20',   _("Damage text move direction"));
21 AUTOCVAR_SAVE(cl_damagetext_offset,             vector, '0 -40 0',  _("Damage text offset"));
22 AUTOCVAR_SAVE(cl_damagetext_accumulate_range,   float,  30,         _("Damage text spawned within this range is accumulated"));
23 #endif
24
25 #ifdef CSQC
26 CLASS(DamageText, Object)
27     ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color)
28     ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size)
29     ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start)
30     ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime)
31     ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity)
32     ATTRIB(DamageText, m_group, int, 0)
33     ATTRIB(DamageText, m_damage, int, 0)
34     ATTRIB(DamageText, m_armordamage, int, 0)
35     ATTRIB(DamageText, time_prev, float, time)
36
37     void DamageText_draw2d(DamageText this) {
38         float dt = time - this.time_prev;
39         this.time_prev = time;
40         setorigin(this, this.origin + dt * this.velocity);
41         this.alpha -= dt * this.fade_rate;
42         if (this.alpha < 0) remove(this);
43         vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
44         if (pos.z >= 0 && this.m_size > 0) {
45             pos.z = 0;
46             string s = sprintf(autocvar_cl_damagetext_format, this.m_damage, this.m_armordamage, this.m_damage + this.m_armordamage);
47             drawcolorcodedstring2(pos, s, this.m_size * '1 1 0', this.m_color, this.alpha, DRAWFLAG_NORMAL);
48         }
49     }
50     ATTRIB(DamageText, draw2d, void(DamageText), DamageText_draw2d)
51
52     void DamageText_update(DamageText this, vector _origin, int _health, int _armor) {
53         this.m_damage = _health;
54         this.m_armordamage = _armor;
55         setorigin(this, _origin);
56         this.alpha = 1;
57     }
58
59     CONSTRUCTOR(DamageText, int _group, vector _origin, int _health, int _armor) {
60         CONSTRUCT(DamageText);
61         this.m_group = _group;
62         DamageText_update(this, _origin, _health, _armor);
63     }
64 ENDCLASS(DamageText)
65 #endif
66
67 #ifdef SVQC
68 AUTOCVAR(sv_damagetext, int, 2, _("<= 0: disabled, >= 1: spectators, >= 2: players, >= 3: all players"));
69 #define SV_DAMAGETEXT_DISABLED()        (autocvar_sv_damagetext <= 0 /* disabled */)
70 #define SV_DAMAGETEXT_SPECTATORS_ONLY() (autocvar_sv_damagetext >= 1 /* spectators only */)
71 #define SV_DAMAGETEXT_PLAYERS()         (autocvar_sv_damagetext >= 2 /* players */)
72 #define SV_DAMAGETEXT_ALL()             (autocvar_sv_damagetext >= 3 /* all players */)
73 MUTATOR_HOOKFUNCTION(damagetext, PlayerDamaged) {
74     if (SV_DAMAGETEXT_DISABLED()) return;
75     const entity attacker = mutator_argv_entity_0;
76     const entity hit = mutator_argv_entity_1; if (hit == attacker) return;
77     const int health = mutator_argv_int_0;
78     const int armor = mutator_argv_int_1;
79     const vector location = hit.origin;
80     entity e;
81     FOR_EACH_REALCLIENT(e) if (
82         (SV_DAMAGETEXT_ALL()) ||
83         (SV_DAMAGETEXT_PLAYERS() && e == attacker) ||
84         (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_SPEC(e) && e.enemy == attacker) ||
85         (SV_DAMAGETEXT_SPECTATORS_ONLY() && IS_OBSERVER(e))
86     ) {
87         msg_entity = e;
88         WriteByte(MSG_ONE, SVC_TEMPENTITY);
89         WriteMutator(MSG_ONE, damagetext);
90         WriteShort(MSG_ONE, health);
91         WriteShort(MSG_ONE, armor);
92         WriteEntity(MSG_ONE, hit);
93         WriteCoord(MSG_ONE, location.x);
94         WriteCoord(MSG_ONE, location.y);
95         WriteCoord(MSG_ONE, location.z);
96     }
97 }
98 #endif
99
100 #ifdef CSQC
101 MUTATOR_HOOKFUNCTION(damagetext, CSQC_Parse_TempEntity) {
102     if (MUTATOR_RETURNVALUE) return false;
103     if (!ReadMutatorEquals(mutator_argv_int_0, damagetext)) return false;
104     int health = ReadShort();
105     int armor = ReadShort();
106     int group = ReadShort();
107     vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
108     if (autocvar_cl_damagetext) {
109         if (autocvar_cl_damagetext_accumulate_range) {
110             for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
111                 if (e.instanceOfDamageText && e.m_group == group) {
112                     DamageText_update(e, location, e.m_damage + health, e.m_armordamage + armor);
113                     return true;
114                 }
115             }
116         }
117         NEW(DamageText, group, location, health, armor);
118     }
119     return true;
120 }
121 #endif
122
123 #ifdef MENUQC
124 CLASS(XonoticDamageTextSettings, XonoticTab)
125     #include "../../../menu/gamesettings.qh"
126     REGISTER_SETTINGS(damagetext, NEW(XonoticDamageTextSettings));
127     ATTRIB(XonoticDamageTextSettings, title, string, _("Damage text"))
128     ATTRIB(XonoticDamageTextSettings, intendedWidth, float, 0.9)
129     ATTRIB(XonoticDamageTextSettings, rows, float, 13)
130     ATTRIB(XonoticDamageTextSettings, columns, float, 5)
131     INIT(XonoticDamageTextSettings) { this.configureDialog(this); }
132     METHOD(XonoticDamageTextSettings, showNotify, void(entity this)) { loadAllCvars(this); }
133     METHOD(XonoticDamageTextSettings, fill, void(entity this))
134     {
135         this.gotoRC(this, 0, 1); this.setFirstColumn(this, this.currentColumn);
136             this.TD(this, 1, 3, makeXonoticCheckBox(0, "cl_damagetext", _("Draw damage numbers")));
137         this.TR(this);
138             this.TD(this, 1, 1, makeXonoticTextLabel(0, _("Font size:")));
139             this.TD(this, 1, 2, makeXonoticSlider(0, 50, 1, "cl_damagetext_size"));
140         this.TR(this);
141             this.TD(this, 1, 1, makeXonoticTextLabel(0, _("Accumulate range:")));
142             this.TD(this, 1, 2, makeXonoticSlider(0, 500, 1, "cl_damagetext_accumulate_range"));
143         this.TR(this);
144             this.TD(this, 1, 1, makeXonoticTextLabel(0, _("Lifetime:")));
145             this.TD(this, 1, 2, makeXonoticSlider(0, 10, 1, "cl_damagetext_alpha_lifetime"));
146         this.TR(this);
147             this.TD(this, 1, 1, makeXonoticTextLabel(0, _("Color:")));
148             this.TD(this, 2, 2, makeXonoticColorpickerString("cl_damagetext_color", "cl_damagetext_color"));
149     }
150 ENDCLASS(XonoticDamageTextSettings)
151 #endif
152 #endif