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