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