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