]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/gibs.qc
8247f6a28c9f1b1c7d7b4dd3268bc301eee1ebcd
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / gibs.qc
1 #include "gibs.qh"
2
3 #include "autocvars.qh"
4 #include "defs.qh"
5 #include "main.qh"
6 #include "movetypes.qh"
7 #include "prandom.qh"
8 #include "rubble.qh"
9 #include "../common/constants.qh"
10 #include "../common/util.qh"
11
12 .float scale;
13 .float alpha;
14 .float cnt;
15 .float gravity;
16
17 void Gib_Delete()
18 {
19         remove(self);
20 }
21
22 string species_prefix(int specnum)
23 {
24         switch(specnum)
25         {
26                 case SPECIES_HUMAN:       return "";
27                 case SPECIES_ALIEN:       return "alien_";
28                 case SPECIES_ROBOT_SHINY: return "robot_";
29                 case SPECIES_ROBOT_RUSTY: return "robot_"; // use the same effects, only different gibs
30                 case SPECIES_ROBOT_SOLID: return "robot_"; // use the same effects, only different gibs
31                 case SPECIES_ANIMAL:      return "animal_";
32                 case SPECIES_RESERVED:    return "reserved_";
33                 default:         return "";
34         }
35 }
36
37 void Gib_setmodel(entity gib, string mdlname, int specnum)
38 {
39         switch(specnum)
40         {
41                 case SPECIES_ROBOT_RUSTY:
42                 case SPECIES_ROBOT_SHINY:
43                 case SPECIES_ROBOT_SOLID:
44                         if(specnum != SPECIES_ROBOT_SOLID || mdlname == "models/gibs/chunk.mdl")
45                         {
46                                 if(mdlname == "models/gibs/bloodyskull.md3")
47                                         setmodel(gib, "models/gibs/robo.md3");
48                                 else
49                                         setmodel(gib, strcat("models/gibs/robo", ftos(floor(random() * 8) + 1), ".md3"));
50                                 if(specnum == SPECIES_ROBOT_SHINY)
51                                 {
52                                         gib.skin = 1;
53                                         gib.colormod = '2 2 2';
54                                 }
55                                 gib.scale = 1;
56                                 break;
57                         }
58                 default:
59                         setmodel(gib, mdlname);
60                         gib.skin = specnum;
61                         break;
62         }
63 }
64
65 void new_te_bloodshower (int ef, vector org, float explosionspeed, int howmany)
66 {
67         float i, pmod;
68         pmod = autocvar_cl_particles_quality;
69         for (i = 0; i < 50 * pmod; ++i)
70                 pointparticles(ef, org, randomvec() * explosionspeed, howmany / 50);
71 }
72
73 void SUB_RemoveOnNoImpact()
74 {
75         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
76                 Gib_Delete();
77 }
78
79 void Gib_Touch()
80 {
81         // TODO maybe bounce of walls, make more gibs, etc.
82
83         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
84         {
85                 Gib_Delete();
86                 return;
87         }
88
89         if(!self.silent)
90                 sound(self, CH_PAIN, strcat("misc/gib_splat0", ftos(floor(prandom() * 4 + 1)), ".wav"), VOL_BASE, ATTEN_NORM);
91         pointparticles(particleeffectnum(strcat(species_prefix(self.cnt), "blood")), self.origin + '0 0 1', '0 0 30', 10);
92
93         Gib_Delete();
94 }
95
96 void Gib_Draw()
97 {
98         vector oldorg;
99         oldorg = self.origin;
100
101         Movetype_Physics_MatchTicrate(autocvar_cl_gibs_ticrate, autocvar_cl_gibs_sloppy);
102         if(wasfreed(self))
103                 return;
104
105         if(self.touch == Gib_Touch) // don't do this for the "chunk" thingie...
106                 // TODO somehow make it spray in a direction dependent on self.angles
107                 trailparticles(self, particleeffectnum(strcat(species_prefix(self.cnt), "TR_SLIGHTBLOOD")), oldorg, self.origin);
108         else
109                 trailparticles(self, particleeffectnum(strcat(species_prefix(self.cnt), "TR_BLOOD")), oldorg, self.origin);
110
111         self.renderflags = 0;
112
113         // make gibs die faster at low view quality
114         // if view_quality is 0.5, we want to have them die twice as fast
115         self.nextthink -= frametime * (1 / bound(0.01, view_quality, 1.00) - 1);
116
117         self.alpha = bound(0, self.nextthink - time, 1);
118
119         if(self.alpha < ALPHA_MIN_VISIBLE)
120         {
121                 self.drawmask = 0;
122                 Gib_Delete();
123         }
124 }
125
126 void TossGib (string mdlname, vector safeorg, vector org, vector vconst, vector vrand, int specnum, bool destroyontouch, bool issilent)
127 {
128         entity gib;
129
130         // TODO remove some gibs according to cl_nogibs
131         gib = RubbleNew("gib");
132         gib.classname = "gib";
133         gib.move_movetype = MOVETYPE_BOUNCE;
134         gib.gravity = 1;
135         gib.solid = SOLID_CORPSE;
136         gib.cnt = specnum;
137         gib.silent = issilent;
138         Gib_setmodel(gib, mdlname, specnum);
139
140         setsize (gib, '-8 -8 -8', '8 8 8');
141
142         gib.draw = Gib_Draw;
143         if(destroyontouch)
144                 gib.move_touch = Gib_Touch;
145         else
146                 gib.move_touch = SUB_RemoveOnNoImpact;
147
148         // don't spawn gibs inside solid - just don't
149         if(org != safeorg)
150         {
151                 tracebox(safeorg, gib.mins, gib.maxs, org, MOVE_NOMONSTERS, gib);
152                 org = trace_endpos;
153         }
154
155         gib.move_origin = org;
156         setorigin(gib, org);
157         gib.move_velocity = vconst * autocvar_cl_gibs_velocity_scale + vrand * autocvar_cl_gibs_velocity_random + '0 0 1' * autocvar_cl_gibs_velocity_up;
158         gib.move_avelocity = prandomvec() * vlen(gib.move_velocity) * autocvar_cl_gibs_avelocity_scale;
159         gib.move_time = time;
160         gib.damageforcescale = autocvar_cl_gibs_damageforcescale;
161
162         gib.nextthink = time + autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15);
163         gib.drawmask = MASK_NORMAL;
164
165         RubbleLimit("gib", autocvar_cl_gibs_maxcount, Gib_Delete);
166 }
167
168 void Ent_GibSplash(bool isNew)
169 {
170         int amount, type, specnum;
171         vector org, vel;
172         string specstr;
173         bool issilent;
174         string gentle_prefix = "morphed_";
175
176         float randomvalue;
177         int c;
178
179         type = ReadByte(); // gibbage type
180         amount = ReadByte() / 16.0; // gibbage amount
181         org.x = ReadShort() * 4 + 2;
182         org.y = ReadShort() * 4 + 2;
183         org.z = ReadShort() * 4 + 2;
184         vel = decompressShortVector(ReadShort());
185
186         float cl_gentle_gibs = autocvar_cl_gentle_gibs;
187         if(cl_gentle_gibs || autocvar_cl_gentle)
188                 type |= 0x80; // set gentle bit
189
190         if(type & 0x80)
191         {
192                 if(cl_gentle_gibs == 2)
193                         gentle_prefix = "";
194                 else if(cl_gentle_gibs == 3)
195                         gentle_prefix = "happy_";
196         }
197         else if(autocvar_cl_particlegibs)
198         {
199                 type |= 0x80;
200                 gentle_prefix = "particlegibs_";
201         }
202
203         if (!(cl_gentle_gibs || autocvar_cl_gentle))
204                 amount *= 1 - autocvar_cl_nogibs;
205
206         if(autocvar_ekg)
207                 amount *= 5;
208
209         if(amount <= 0 || !isNew)
210                 return;
211
212         setorigin(self, org); // for the sounds
213
214         specnum = (type & 0x78) / 8; // blood/gibmodel type: using four bits (0..7, bit indexes 3,4,5)
215         issilent = (type & 0x40);
216         type = type & 0x87; // remove the species bits: bit 7 = gentle, bit 0,1,2 = kind of gib
217         specstr = species_prefix(specnum);
218
219         switch(type)
220         {
221                 case 0x01:
222                         if(!issilent)
223                                 sound (self, CH_PAIN, "misc/gib.wav", VOL_BASE, ATTEN_NORM);
224
225                         if(prandom() < amount)
226                                 TossGib ("models/gibs/eye.md3", org, org, vel, prandomvec() * 150, specnum, 0, issilent);
227                         new_te_bloodshower(particleeffectnum(strcat(specstr, "bloodshower")), org, 1200, amount);
228                         if(prandom() < amount)
229                                 TossGib ("models/gibs/bloodyskull.md3", org, org + 16 * prandomvec(), vel, prandomvec() * 100, specnum, 0, issilent);
230
231                         for(c = 0; c < amount; ++c)
232                         {
233                                 randomvalue = amount - c;
234
235                                 if(prandom() < randomvalue)
236                                         TossGib ("models/gibs/arm.md3", org, org + 16 * prandomvec() + '0 0 8', vel, prandomvec() * (prandom() * 120 + 90), specnum,0, issilent);
237                                 if(prandom() < randomvalue)
238                                         TossGib ("models/gibs/arm.md3", org, org + 16 * prandomvec() + '0 0 8', vel, prandomvec() * (prandom() * 120 + 90), specnum,0, issilent);
239                                 if(prandom() < randomvalue)
240                                         TossGib ("models/gibs/chest.md3", org, org + 16 * prandomvec(), vel, prandomvec() * (prandom() * 120 + 80), specnum,0, issilent);
241                                 if(prandom() < randomvalue)
242                                         TossGib ("models/gibs/smallchest.md3", org, org + 16 * prandomvec(), vel, prandomvec() * (prandom() * 120 + 80), specnum,0, issilent);
243                                 if(prandom() < randomvalue)
244                                         TossGib ("models/gibs/leg1.md3", org, org + 16 * prandomvec() + '0 0 -5', vel, prandomvec() * (prandom() * 120 + 85), specnum,0, issilent);
245                                 if(prandom() < randomvalue)
246                                         TossGib ("models/gibs/leg2.md3", org, org + 16 * prandomvec() + '0 0 -5', vel, prandomvec() * (prandom() * 120 + 85), specnum,0, issilent);
247
248                                 // these splat on impact
249                                 if(prandom() < randomvalue)
250                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent);
251                                 if(prandom() < randomvalue)
252                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent);
253                                 if(prandom() < randomvalue)
254                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent);
255                                 if(prandom() < randomvalue)
256                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent);
257                         }
258                         break;
259                 case 0x02:
260                         pointparticles(particleeffectnum(strcat(specstr, "blood")), org, vel, amount * 16);
261                         break;
262                 case 0x03:
263                         if(prandom() < amount)
264                                 TossGib ("models/gibs/chunk.mdl", org, org, vel, prandomvec() * (prandom() * 30 + 20), specnum, 1, issilent); // TODO maybe adjust to more randomization?
265                         break;
266                 case 0x81:
267                         pointparticles(particleeffectnum(strcat(gentle_prefix, "damage_dissolve")), org, vel, amount);
268                         break;
269                 case 0x82:
270                         pointparticles(particleeffectnum(strcat(gentle_prefix, "damage_hit")), org, vel, amount * 16);
271                         break;
272                 case 0x83:
273                         // no gibs in gentle mode, sorry
274                         break;
275         }
276 }
277
278 void GibSplash_Precache()
279 {
280         precache_model("models/gibs/chunk.mdl");
281         precache_model("models/gibs/leg1.md3");
282         precache_model("models/gibs/leg2.md3");
283         precache_model("models/gibs/chest.md3");
284         precache_model("models/gibs/smallchest.md3");
285         precache_model("models/gibs/arm.md3");
286         precache_model("models/gibs/bloodyskull.md3");
287         precache_model("models/gibs/eye.md3");
288
289         precache_model("models/gibs/robo.md3");
290         precache_model("models/gibs/robo1.md3");
291         precache_model("models/gibs/robo2.md3");
292         precache_model("models/gibs/robo3.md3");
293         precache_model("models/gibs/robo4.md3");
294         precache_model("models/gibs/robo5.md3");
295         precache_model("models/gibs/robo6.md3");
296         precache_model("models/gibs/robo7.md3");
297         precache_model("models/gibs/robo8.md3");
298
299         precache_sound ("misc/gib.wav");
300     precache_sound ("misc/gib_splat01.wav");
301     precache_sound ("misc/gib_splat02.wav");
302     precache_sound ("misc/gib_splat03.wav");
303     precache_sound ("misc/gib_splat04.wav");
304 }