]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/damagetext.qc
Show damage text for spectators
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / damagetext.qc
1 REGISTER_MUTATOR(damagetext, true);
2
3 #ifdef CSQC
4 vector autocvar_cl_damagetext_color = '1 1 0';
5 float autocvar_cl_damagetext_size = 8;
6 float autocvar_cl_damagetext_alpha_start = 1;
7 float autocvar_cl_damagetext_alpha_lifetime = 3;
8 vector autocvar_cl_damagetext_velocity = '0 0 20';
9 vector autocvar_cl_damagetext_offset = '0 -40 0';
10 float autocvar_cl_damagetext_accumulate_range = 300;
11
12 CLASS(DamageText, Object)
13     ATTRIB(DamageText, m_color, vector, autocvar_cl_damagetext_color)
14     ATTRIB(DamageText, m_size, float, autocvar_cl_damagetext_size)
15     ATTRIB(DamageText, alpha, float, autocvar_cl_damagetext_alpha_start)
16     ATTRIB(DamageText, fade_rate, float, 1 / autocvar_cl_damagetext_alpha_lifetime)
17     ATTRIB(DamageText, velocity, vector, autocvar_cl_damagetext_velocity)
18     ATTRIB(DamageText, m_group, int, 0)
19     ATTRIB(DamageText, m_damage, int, 0)
20     ATTRIB(DamageText, m_armordamage, int, 0)
21     ATTRIB(DamageText, time_prev, float, time)
22
23     void DamageText_draw() {
24         entity this = self;
25         float dt = time - this.time_prev;
26         this.time_prev = time;
27         setorigin(this, this.origin + dt * this.velocity);
28         this.alpha -= dt * this.fade_rate;
29         if (this.alpha < 0) remove(this);
30         vector pos = project_3d_to_2d(this.origin) + autocvar_cl_damagetext_offset;
31         if (pos.z >= 0 && this.m_size > 0) {
32             pos.z = 0;
33             string s = sprintf("-%d", this.m_damage + this.m_armordamage);
34             drawstring(pos, s, this.m_size * '1 1 0', this.m_color, this.alpha, DRAWFLAG_NORMAL);
35         }
36     }
37     ATTRIB(DamageText, draw2d, void(), DamageText_draw)
38
39     void DamageText_update(DamageText this, vector _origin, int _health, int _armor) {
40         this.m_damage = _health;
41         this.m_armordamage = _armor;
42         setorigin(this, _origin);
43         this.alpha = 1;
44     }
45
46     CONSTRUCTOR(DamageText, int _group, vector _origin, int _health, int _armor) {
47         CONSTRUCT(DamageText);
48         this.m_group = _group;
49         DamageText_update(this, _origin, _health, _armor);
50         return this;
51     }
52 ENDCLASS(DamageText)
53 #endif
54
55 #ifdef SVQC
56 MUTATOR_HOOKFUNCTION(damagetext, PlayerDamaged) {
57     const entity attacker = mutator_argv_entity_0;
58     const entity hit = mutator_argv_entity_1; if (hit == attacker) return;
59     const int health = mutator_argv_int_0;
60     const int armor = mutator_argv_int_1;
61     const vector location = hit.origin;
62     entity e;
63     FOR_EACH_REALCLIENT(e) if (e == attacker || (IS_SPEC(e) && e.enemy == attacker)) {
64         msg_entity = e;
65         WriteByte(MSG_ONE, SVC_TEMPENTITY);
66         WriteMutator(MSG_ONE, damagetext);
67         WriteShort(MSG_ONE, health);
68         WriteShort(MSG_ONE, armor);
69         WriteEntity(MSG_ONE, hit);
70         WriteCoord(MSG_ONE, location.x);
71         WriteCoord(MSG_ONE, location.y);
72         WriteCoord(MSG_ONE, location.z);
73     }
74 }
75 #endif
76
77 #ifdef CSQC
78 MUTATOR_HOOKFUNCTION(damagetext, CSQC_Parse_TempEntity) {
79     if (MUTATOR_RETURNVALUE) return false;
80     if (!ReadMutatorEquals(mutator_argv_int_0, damagetext)) return false;
81     int health = ReadShort();
82     int armor = ReadShort();
83     int group = ReadShort();
84     vector location = vec3(ReadCoord(), ReadCoord(), ReadCoord());
85     for (entity e = findradius(location, autocvar_cl_damagetext_accumulate_range); e; e = e.chain) {
86         if (e.instanceOfDamageText && e.m_group == group) {
87             DamageText_update(e, location, e.m_damage + health, e.m_armordamage + armor);
88             return true;
89         }
90     }
91     NEW(DamageText, group, location, health, armor);
92     return true;
93 }
94 #endif