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