Merge branch 'master' into mirceakitsune/damage_effects
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / gibs.qc
1 .float silent;
2
3 void Gib_Delete()
4 {
5         remove(self);
6 }
7
8 string species_prefix(float 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, float 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 (float ef, vector org, float explosionspeed, float 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, ATTN_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, float specnum, float destroyontouch, float issilent, float gibownernum)
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.team = gibownernum - 1;
125         Gib_setmodel(gib, mdlname, specnum);
126
127         setsize (gib, '-8 -8 -8', '8 8 8');
128
129         gib.draw = Gib_Draw;
130         if(destroyontouch)
131                 gib.move_touch = Gib_Touch;
132         else
133                 gib.move_touch = SUB_RemoveOnNoImpact;
134
135         // don't spawn gibs inside solid - just don't
136         if(org != safeorg)
137         {
138                 tracebox(safeorg, gib.mins, gib.maxs, org, MOVE_NOMONSTERS, gib);
139                 org = trace_endpos;
140         }
141
142         gib.move_origin = gib.origin = 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(float isNew)
155 {
156         float amount, type, specnum, entnumber;
157         vector org, vel;
158         string specstr;
159         float issilent;
160         string gentle_prefix;
161
162         float c, randomvalue;
163
164         type = ReadByte(); // gibbage type
165         amount = ReadByte() / 16.0; // gibbage amount
166         entnumber = ReadByte(); // player num
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                 else
183                         gentle_prefix = "morphed_";
184         }
185         else if(autocvar_cl_particlegibs)
186         {
187                 type |= 0x80;
188                 gentle_prefix = "particlegibs_";
189         }
190
191         if not(cl_gentle_gibs || autocvar_cl_gentle)
192                 amount *= 1 - autocvar_cl_nogibs;
193
194         if(autocvar_ekg)
195                 amount *= 5;
196
197         if(amount <= 0 || !isNew)
198                 return;
199
200         self.origin = org; // for the sounds
201
202         specnum = (type & 0x78) / 8; // blood/gibmodel type: using four bits (0..7, bit indexes 3,4,5)
203         issilent = (type & 0x40);
204         type = type & 0x87; // remove the species bits: bit 7 = gentle, bit 0,1,2 = kind of gib
205         specstr = species_prefix(specnum);
206
207         switch(type)
208         {
209                 case 0x01:
210                         if(!issilent)
211                                 sound (self, CH_PAIN, "misc/gib.wav", VOL_BASE, ATTN_NORM);
212
213                         if(prandom() < amount)
214                                 TossGib ("models/gibs/eye.md3", org, org, vel, prandomvec() * 150, specnum, 0, issilent, entnumber);
215                         new_te_bloodshower(particleeffectnum(strcat(specstr, "bloodshower")), org, 1200, amount);
216                         if(prandom() < amount)
217                                 TossGib ("models/gibs/bloodyskull.md3", org, org + 16 * prandomvec(), vel, prandomvec() * 100, specnum, 0, issilent, entnumber);
218
219                         for(c = 0; c < amount; ++c)
220                         {
221                                 randomvalue = amount - c;
222
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, entnumber);
225                                 if(prandom() < randomvalue)
226                                         TossGib ("models/gibs/arm.md3", org, org + 16 * prandomvec() + '0 0 8', vel, prandomvec() * (prandom() * 120 + 90), specnum,0, issilent, entnumber);
227                                 if(prandom() < randomvalue)
228                                         TossGib ("models/gibs/chest.md3", org, org + 16 * prandomvec(), vel, prandomvec() * (prandom() * 120 + 80), specnum,0, issilent, entnumber);
229                                 if(prandom() < randomvalue)
230                                         TossGib ("models/gibs/smallchest.md3", org, org + 16 * prandomvec(), vel, prandomvec() * (prandom() * 120 + 80), specnum,0, issilent, entnumber);
231                                 if(prandom() < randomvalue)
232                                         TossGib ("models/gibs/leg1.md3", org, org + 16 * prandomvec() + '0 0 -5', vel, prandomvec() * (prandom() * 120 + 85), specnum,0, issilent, entnumber);
233                                 if(prandom() < randomvalue)
234                                         TossGib ("models/gibs/leg2.md3", org, org + 16 * prandomvec() + '0 0 -5', vel, prandomvec() * (prandom() * 120 + 85), specnum,0, issilent, entnumber);
235
236                                 // these splat on impact
237                                 if(prandom() < randomvalue)
238                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent, entnumber);
239                                 if(prandom() < randomvalue)
240                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent, entnumber);
241                                 if(prandom() < randomvalue)
242                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent, entnumber);
243                                 if(prandom() < randomvalue)
244                                         TossGib ("models/gibs/chunk.mdl", org, org + 16 * prandomvec(), vel, prandomvec() * 450, specnum,1, issilent, entnumber);
245                         }
246                         break;
247                 case 0x02:
248                         pointparticles(particleeffectnum(strcat(specstr, "blood")), org, vel, amount * 16);
249                         break;
250                 case 0x03:
251                         if(prandom() < amount)
252                                 TossGib ("models/gibs/chunk.mdl", org, org, vel, prandomvec() * (prandom() * 30 + 20), specnum, 1, issilent, entnumber); // TODO maybe adjust to more randomization?
253                         break;
254                 case 0x81:
255                         pointparticles(particleeffectnum(strcat(gentle_prefix, "damage_dissolve")), org, vel, amount);
256                         break;
257                 case 0x82:
258                         pointparticles(particleeffectnum(strcat(gentle_prefix, "damage_hit")), org, vel, amount * 16);
259                         break;
260                 case 0x83:
261                         // no gibs in gentle mode, sorry
262                         break;
263         }
264 }
265
266 void GibSplash_Precache()
267 {
268         precache_model("models/gibs/chunk.mdl");
269         precache_model("models/gibs/leg1.md3");
270         precache_model("models/gibs/leg2.md3");
271         precache_model("models/gibs/chest.md3");
272         precache_model("models/gibs/smallchest.md3");
273         precache_model("models/gibs/arm.md3");
274         precache_model("models/gibs/bloodyskull.md3");
275         precache_model("models/gibs/eye.md3");
276
277         precache_model("models/gibs/robo.md3");
278         precache_model("models/gibs/robo1.md3");
279         precache_model("models/gibs/robo2.md3");
280         precache_model("models/gibs/robo3.md3");
281         precache_model("models/gibs/robo4.md3");
282         precache_model("models/gibs/robo5.md3");
283         precache_model("models/gibs/robo6.md3");
284         precache_model("models/gibs/robo7.md3");
285         precache_model("models/gibs/robo8.md3");
286
287         precache_sound ("misc/gib.wav");
288     precache_sound ("misc/gib_splat01.wav");
289     precache_sound ("misc/gib_splat02.wav");
290     precache_sound ("misc/gib_splat03.wav");
291     precache_sound ("misc/gib_splat04.wav");
292 }