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