]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_violence.qc
Add a basic cvar check to avoid running code uselessly
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / g_violence.qc
1 float Violence_GibSplash_SendEntity(entity to, float sf)
2 {
3         WriteByte(MSG_ENTITY, ENT_CLIENT_GIBSPLASH);
4         WriteByte(MSG_ENTITY, self.state); // actually type
5         WriteByte(MSG_ENTITY, bound(1, self.cnt * 16, 255)); // gibbage amount multiplier
6         WriteByte(MSG_ENTITY, self.team); // player num
7         WriteShort(MSG_ENTITY, floor(self.origin_x / 4)); // not using a coord here, as gibs don't need this accuracy
8         WriteShort(MSG_ENTITY, floor(self.origin_y / 4)); // not using a coord here, as gibs don't need this accuracy
9         WriteShort(MSG_ENTITY, floor(self.origin_z / 4)); // not using a coord here, as gibs don't need this accuracy
10         WriteShort(MSG_ENTITY, self.oldorigin_x); // acrually compressed velocity
11         return TRUE;
12 }
13
14 // TODO maybe convert this to a TE?
15 void Violence_GibSplash_At(vector org, vector dir, float type, float amount, entity gibowner, entity attacker)
16 {
17         if(g_cts) // no gibs in CTS
18                 return;
19
20         entity e;
21
22         e = spawn();
23         e.classname = "gibsplash";
24         e.cnt = amount;
25         e.state = type; // should stay smaller than 15
26         if(!sound_allowed(MSG_BROADCAST, gibowner) || !sound_allowed(MSG_BROADCAST, attacker))
27                 e.state |= 0x40; // "silence" bit
28         e.state |= 8 * self.species; // gib type, ranges from 0 to 15
29
30         // if this is a copied dead body, send the num of its player instead
31         if(self.classname == "body")
32                 e.team = num_for_edict(self.owner);
33         else
34                 e.team = num_for_edict(self);
35
36         setorigin(e, org);
37         e.velocity = dir;
38
39         e.oldorigin_x = compressShortVector(e.velocity);
40
41         Net_LinkEntity(e, FALSE, 0.2, Violence_GibSplash_SendEntity);
42 }
43
44 void Violence_GibSplash(entity source, float type, float amount, entity attacker)
45 {
46         Violence_GibSplash_At(source.origin + source.view_ofs, source.velocity, type, amount, source, attacker);
47 }
48
49 // damage effect
50
51 .float lifetime;
52 .entity damageeffect_repeater;
53
54 float Violence_DamageEffect_SendEntity(entity to, float sf)
55 {
56         // if the client doesn't have the effect enabled, don't send to him and waste bandwidth
57         if not(to.cvar_cl_damageeffect)
58                 return FALSE;
59         // if the client cannot see the damaged player, avoid sending and further save bandwidth
60         if not(checkpvs(to.origin + to.view_ofs, self))
61                 return FALSE;
62
63         WriteByte(MSG_ENTITY, ENT_CLIENT_DAMAGEEFFECT);
64         WriteByte(MSG_ENTITY, self.cnt); // the damage weapon
65         WriteByte(MSG_ENTITY, self.state); // species
66         WriteByte(MSG_ENTITY, self.team); // player entnum
67         WriteCoord(MSG_ENTITY, floor(self.origin_x));
68         WriteCoord(MSG_ENTITY, floor(self.origin_y));
69         WriteCoord(MSG_ENTITY, floor(self.origin_z));
70         return TRUE;
71 }
72
73 void Violence_DamageEffect(entity pl, float type)
74 {
75         entity e;
76
77         e = spawn();
78         e.classname = "damageeffect";
79         e.cnt = type;
80         e.state |= 8 * pl.species; // gib type, ranges from 0 to 15
81
82         // if this is a copied dead body, send the num of its player instead
83         if(pl.classname == "body")
84                 e.team = num_for_edict(pl.owner);
85         else
86                 e.team = num_for_edict(pl);
87
88         setorigin(e, pl.origin);
89
90         Net_LinkEntity(e, FALSE, 0.2, Violence_DamageEffect_SendEntity);
91 }
92
93 void Violence_DamageEffect_Remove(entity pl);
94 void Violence_DamageEffect_DoRepeat()
95 {
96         if(time > self.lifetime || (self.owner.classname != "player" && self.owner.classname != "body"))
97         {
98                 Violence_DamageEffect_Remove(self.owner);
99                 return;
100         }
101
102         Violence_DamageEffect(self.owner, self.cnt);
103         self.nextthink = time + autocvar_sv_damageeffect_tick;
104 }
105
106 void Violence_DamageEffect_SetRepeat(entity pl, float damage, float type)
107 {
108         if not(autocvar_sv_damageeffect_tick && autocvar_sv_damageeffect_lifetime)
109                 return;
110         if(sv_gentle || !type)
111                 return; // return if gentle mode is enabled or the damage was not caused by a weapon
112
113         // if a repeater doesn't exist, spawn one, else update the existing one
114         if(pl.damageeffect_repeater == world)
115         {
116                 pl.damageeffect_repeater = spawn();
117                 pl.damageeffect_repeater.classname = "damageeffect_repeater";
118                 pl.damageeffect_repeater.owner = pl;
119                 pl.damageeffect_repeater.think = Violence_DamageEffect_DoRepeat;
120
121                 pl.damageeffect_repeater.lifetime = time + (autocvar_sv_damageeffect_lifetime * damage);
122         }
123         else
124         {
125                 // if the repeater is being updated, increase its lifetime instead of re-setting it entirely
126                 // this fixes the shotgun among other things, where only the damage of one bullet would be taken into account
127                 pl.damageeffect_repeater.lifetime += (autocvar_sv_damageeffect_lifetime * damage);
128         }
129
130         if(autocvar_sv_damageeffect_lifetime_max)
131                 pl.damageeffect_repeater.lifetime = bound(0, pl.damageeffect_repeater.lifetime, time + autocvar_sv_damageeffect_lifetime_max);
132
133         pl.damageeffect_repeater.cnt = type;
134         pl.damageeffect_repeater.nextthink = time;
135 }
136
137 void Violence_DamageEffect_Remove(entity pl)
138 {
139         pl.damageeffect_repeater.nextthink = 0;
140         remove(pl.damageeffect_repeater);
141         pl.damageeffect_repeater = world;
142 }
143
144 void Violence_DamageEffect_Copy(entity old_pl, entity pl)
145 {
146         if(pl.damageeffect_repeater != world)
147                 Violence_DamageEffect_Remove(pl);
148
149         Violence_DamageEffect_SetRepeat(pl, 0, old_pl.damageeffect_repeater.cnt); // spawn a new repeater
150         pl.damageeffect_repeater.lifetime = old_pl.damageeffect_repeater.lifetime; // copy the lifetime
151 }