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