Merge branch 'master' into terencehill/lms_itemtimes_fix
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / gibs.qc
1 #ifdef IMPLEMENTATION
2 REGISTER_NET_TEMP(net_gibsplash)
3
4 #ifdef SVQC
5
6 .int state;
7
8 bool Violence_GibSplash_SendEntity(entity this, entity to, int sf)
9 {
10         int channel = MSG_ONE;
11         msg_entity = to;
12         WriteHeader(channel, net_gibsplash);
13         WriteByte(channel, this.state); // actually type
14         WriteByte(channel, bound(1, this.cnt * 16, 255)); // gibbage amount multiplier
15         WriteShort(channel, floor(this.origin.x / 4)); // not using a coord here, as gibs don't need this accuracy
16         WriteShort(channel, floor(this.origin.y / 4)); // not using a coord here, as gibs don't need this accuracy
17         WriteShort(channel, floor(this.origin.z / 4)); // not using a coord here, as gibs don't need this accuracy
18         WriteShort(channel, this.oldorigin.x); // acrually compressed velocity
19         return true;
20 }
21
22 void Violence_GibSplash_At(vector org, vector dir, float type, float amount, entity gibowner, entity attacker)
23 {SELFPARAM();
24         if(g_cts) // no gibs in CTS
25                 return;
26
27         entity e = new(gibsplash);
28         e.cnt = amount;
29         e.state = type; // should stay smaller than 15
30         if(!sound_allowed(MSG_BROADCAST, gibowner) || !sound_allowed(MSG_BROADCAST, attacker))
31                 e.state |= 0x40; // "silence" bit
32         e.state |= 8 * self.species; // gib type, ranges from 0 to 15
33
34         // if this is a copied dead body, send the num of its player instead
35         // TODO: remove this field, read from model txt files
36         if(self.classname == "body")
37                 e.team = etof(self.enemy);
38         else
39                 e.team = etof(self);
40
41         setorigin(e, org);
42         e.velocity = dir;
43
44         e.oldorigin_x = compressShortVector(e.velocity);
45
46         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(Violence_GibSplash_SendEntity(e, it, 0)));
47         remove(e);
48 }
49
50 void Violence_GibSplash(entity source, float type, float amount, entity attacker)
51 {
52         Violence_GibSplash_At(source.origin + source.view_ofs, source.velocity, type, amount, source, attacker);
53 }
54 #endif
55
56 #ifdef CSQC
57
58 .vector colormod;
59 .bool silent;
60
61 #include "rubble.qh"
62 #include <common/physics/movetypes/movetypes.qh>
63
64 .float scale;
65 .float alpha;
66 .float cnt;
67 .float gravity;
68
69 void Gib_Delete()
70 {SELFPARAM();
71         remove(self);
72 }
73
74 string species_prefix(int specnum);
75
76 void Gib_setmodel(entity gib, string mdlname, int specnum)
77 {
78         switch(specnum)
79         {
80                 case SPECIES_ROBOT_RUSTY:
81                 case SPECIES_ROBOT_SHINY:
82                 case SPECIES_ROBOT_SOLID:
83                         if(specnum != SPECIES_ROBOT_SOLID || mdlname == "models/gibs/chunk.mdl")
84                         {
85                                 if(mdlname == "models/gibs/bloodyskull.md3")
86                                         setmodel(gib, MDL_GIB_ROBO);
87                                 else
88                                         setmodel(gib, MDL_GIB_ROBO_RANDOM());
89                                 if(specnum == SPECIES_ROBOT_SHINY)
90                                 {
91                                         gib.skin = 1;
92                                         gib.colormod = '2 2 2';
93                                 }
94                                 gib.scale = 1;
95                                 break;
96                         }
97                 default:
98                         _setmodel(gib, mdlname);
99                         gib.skin = specnum;
100                         break;
101         }
102 }
103
104 void new_te_bloodshower (int ef, vector org, float explosionspeed, int howmany)
105 {
106         float i, pmod;
107         pmod = autocvar_cl_particles_quality;
108         for (i = 0; i < 50 * pmod; ++i)
109                 __pointparticles(ef, org, randomvec() * explosionspeed, howmany / 50);
110 }
111
112 void SUB_RemoveOnNoImpact()
113 {
114         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
115                 Gib_Delete();
116 }
117
118 void Gib_Touch()
119 {SELFPARAM();
120         // TODO maybe bounce of walls, make more gibs, etc.
121
122         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
123         {
124                 Gib_Delete();
125                 return;
126         }
127
128         if(!self.silent)
129                 sound(self, CH_PAIN, SND_GIB_SPLAT_RANDOM(), VOL_BASE, ATTEN_NORM);
130         __pointparticles(_particleeffectnum(strcat(species_prefix(self.cnt), "blood")), self.origin + '0 0 1', '0 0 30', 10);
131
132         Gib_Delete();
133 }
134
135 void Gib_Draw(entity this)
136 {
137         vector oldorg;
138         oldorg = self.origin;
139
140         Movetype_Physics_MatchTicrate(self, autocvar_cl_gibs_ticrate, autocvar_cl_gibs_sloppy);
141         if(wasfreed(self))
142                 return;
143
144         if(self.touch == Gib_Touch) // don't do this for the "chunk" thingie...
145                 // TODO somehow make it spray in a direction dependent on self.angles
146                 __trailparticles(self, _particleeffectnum(strcat(species_prefix(self.cnt), EFFECT_TR_SLIGHTBLOOD.eent_eff_name)), oldorg, self.origin);
147         else
148                 __trailparticles(self, _particleeffectnum(strcat(species_prefix(self.cnt), EFFECT_TR_BLOOD.eent_eff_name)), oldorg, self.origin);
149
150         self.renderflags = 0;
151
152         // make gibs die faster at low view quality
153         // if view_quality is 0.5, we want to have them die twice as fast
154         self.nextthink -= frametime * (1 / bound(0.01, view_quality, 1.00) - 1);
155
156         self.alpha = bound(0, self.nextthink - time, 1);
157
158         if(self.alpha < ALPHA_MIN_VISIBLE)
159         {
160                 self.drawmask = 0;
161                 Gib_Delete();
162         }
163 }
164
165 void TossGib (string mdlname, vector safeorg, vector org, vector vconst, vector vrand, int specnum, bool destroyontouch, bool issilent)
166 {
167         entity gib;
168
169         // TODO remove some gibs according to cl_nogibs
170         gib = RubbleNew("gib");
171         gib.move_movetype = MOVETYPE_BOUNCE;
172         gib.gravity = 1;
173         gib.solid = SOLID_CORPSE;
174         gib.cnt = specnum;
175         gib.silent = issilent;
176         Gib_setmodel(gib, mdlname, specnum);
177
178         setsize (gib, '-8 -8 -8', '8 8 8');
179
180         gib.draw = Gib_Draw;
181         if(destroyontouch)
182                 gib.move_touch = Gib_Touch;
183         else
184                 gib.move_touch = SUB_RemoveOnNoImpact;
185
186         // don't spawn gibs inside solid - just don't
187         if(org != safeorg)
188         {
189                 tracebox(safeorg, gib.mins, gib.maxs, org, MOVE_NOMONSTERS, gib);
190                 org = trace_endpos;
191         }
192
193         gib.move_origin = org;
194         setorigin(gib, org);
195         gib.move_velocity = vconst * autocvar_cl_gibs_velocity_scale + vrand * autocvar_cl_gibs_velocity_random + '0 0 1' * autocvar_cl_gibs_velocity_up;
196         gib.move_avelocity = prandomvec() * vlen(gib.move_velocity) * autocvar_cl_gibs_avelocity_scale;
197         gib.move_time = time;
198         gib.damageforcescale = autocvar_cl_gibs_damageforcescale;
199
200         gib.nextthink = time + autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15);
201         gib.drawmask = MASK_NORMAL;
202
203         RubbleLimit("gib", autocvar_cl_gibs_maxcount, Gib_Delete);
204 }
205
206 NET_HANDLE(net_gibsplash, bool isNew)
207 {
208         Net_Accept(net_gibsplash);
209
210         string gentle_prefix = "morphed_";
211
212         int type = ReadByte(); // gibbage type
213         int amount = ReadByte() / 16.0; // gibbage amount
214         vector org;
215         org.x = ReadShort() * 4 + 2;
216         org.y = ReadShort() * 4 + 2;
217         org.z = ReadShort() * 4 + 2;
218         vector vel = decompressShortVector(ReadShort());
219
220         return = true;
221
222         float cl_gentle_gibs = autocvar_cl_gentle_gibs;
223         if(cl_gentle_gibs || autocvar_cl_gentle)
224                 type |= 0x80; // set gentle bit
225
226         if(type & 0x80)
227         {
228                 if(cl_gentle_gibs == 2)
229                         gentle_prefix = "";
230                 else if(cl_gentle_gibs == 3)
231                         gentle_prefix = "happy_";
232         }
233         else if(autocvar_cl_particlegibs)
234         {
235                 type |= 0x80;
236                 gentle_prefix = "particlegibs_";
237         }
238
239         if (!(cl_gentle_gibs || autocvar_cl_gentle))
240                 amount *= 1 - autocvar_cl_nogibs;
241
242         if(autocvar_ekg)
243                 amount *= 5;
244
245         if(amount <= 0 || !isNew)
246                 return;
247
248         setorigin(this, org); // for the sounds
249
250         int specnum = (type & 0x78) / 8; // blood/gibmodel type: using four bits (0..7, bit indexes 3,4,5)
251         bool issilent = (type & 0x40);
252         type = type & 0x87; // remove the species bits: bit 7 = gentle, bit 0,1,2 = kind of gib
253         string specstr = species_prefix(specnum);
254
255         switch(type)
256         {
257                 case 0x01:
258                         if(!issilent)
259                                 sound (this, CH_PAIN, SND_GIB, VOL_BASE, ATTEN_NORM);
260
261                         if(prandom() < amount)
262                                 TossGib ("models/gibs/eye.md3", org, org, vel, prandomvec() * 150, specnum, 0, issilent);
263                         new_te_bloodshower(_particleeffectnum(strcat(specstr, "bloodshower")), org, 1200, amount);
264                         if(prandom() < amount)
265                                 TossGib ("models/gibs/bloodyskull.md3", org, org + 16 * prandomvec(), vel, prandomvec() * 100, specnum, 0, issilent);
266
267                         for(int c = 0; c < amount; ++c)
268                         {
269                                 int randomvalue = amount - c;
270
271                                 if(prandom() < randomvalue)
272                                         TossGib ("models/gibs/arm.md3", org, org + 16 * prandomvec() + '0 0 8', vel, prandomvec() * (prandom() * 120 + 90), specnum,0, issilent);
273                                 if(prandom() < randomvalue)
274                                         TossGib ("models/gibs/arm.md3", org, org + 16 * prandomvec() + '0 0 8', vel, prandomvec() * (prandom() * 120 + 90), specnum,0, issilent);
275                                 if(prandom() < randomvalue)
276                                         TossGib ("models/gibs/chest.md3", org, org + 16 * prandomvec(), vel, prandomvec() * (prandom() * 120 + 80), specnum,0, issilent);
277                                 if(prandom() < randomvalue)
278                                         TossGib ("models/gibs/smallchest.md3", org, org + 16 * prandomvec(), vel, prandomvec() * (prandom() * 120 + 80), specnum,0, issilent);
279                                 if(prandom() < randomvalue)
280                                         TossGib ("models/gibs/leg1.md3", org, org + 16 * prandomvec() + '0 0 -5', vel, prandomvec() * (prandom() * 120 + 85), specnum,0, issilent);
281                                 if(prandom() < randomvalue)
282                                         TossGib ("models/gibs/leg2.md3", org, org + 16 * prandomvec() + '0 0 -5', vel, prandomvec() * (prandom() * 120 + 85), specnum,0, issilent);
283
284                                 // these splat on impact
285                                 if(prandom() < randomvalue)
286                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent);
287                                 if(prandom() < randomvalue)
288                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent);
289                                 if(prandom() < randomvalue)
290                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent);
291                                 if(prandom() < randomvalue)
292                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent);
293                         }
294                         break;
295                 case 0x02:
296                         __pointparticles(_particleeffectnum(strcat(specstr, "blood")), org, vel, amount * 16);
297                         break;
298                 case 0x03:
299                         if(prandom() < amount)
300                                 TossGib ("models/gibs/chunk.mdl", org, org, vel, prandomvec() * (prandom() * 30 + 20), specnum, 1, issilent); // TODO maybe adjust to more randomization?
301                         break;
302                 case 0x81:
303                         __pointparticles(_particleeffectnum(strcat(gentle_prefix, "damage_dissolve")), org, vel, amount);
304                         break;
305                 case 0x82:
306                         __pointparticles(_particleeffectnum(strcat(gentle_prefix, "damage_hit")), org, vel, amount * 16);
307                         break;
308                 case 0x83:
309                         // no gibs in gentle mode, sorry
310                         break;
311         }
312         remove(this);
313 }
314 #endif
315
316 #endif