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