new gib types: alien and robot_shiny, and blood types, depending on playermodel
[divverent/nexuiz.git] / data / qcsrc / client / gibs.qc
1 string species_prefix(float specnum)
2 {
3         switch(specnum)
4         {
5                 case SPECIES_HUMAN:       return "";
6                 case SPECIES_ALIEN:       return "alien_";
7                 case SPECIES_ROBOT_SHINY: return "robot_";
8                 case SPECIES_ROBOT_RUSTY: return "robot_"; // use the same effects, only different gibs
9                 case SPECIES_ANIMAL:      return "animal_";
10                 case SPECIES_RESERVED:    return "reserved_";
11                 default:         return "";
12         }
13 }
14
15 void SUB_RemoveOnNoImpact()
16 {
17         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
18                 remove(self);
19 }
20
21 void Gib_Touch()
22 {
23         // TODO maybe bounce of walls, make more gibs, etc.
24
25         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
26         {
27                 remove(self);
28                 return;
29         }
30         
31         sound(self, CHAN_PAIN, strcat("misc/gib_splat0", ftos(floor(prandom() * 4 + 1)), ".wav"), VOL_BASE, ATTN_NORM);
32         pointparticles(particleeffectnum(strcat(species_prefix(self.cnt), "_blood")), self.origin + '0 0 1', '0 0 30', 10);
33
34         remove(self);
35 }
36
37 void Gib_Draw()
38 {
39         vector oldorg;
40         oldorg = self.origin;
41
42         Movetype_Physics(FALSE);
43         if(wasfreed(self))
44                 return;
45         
46         if(self.touch == Gib_Touch) // don't do this for the "chunk" thingie...
47                 trailparticles(self, particleeffectnum(strcat(species_prefix(self.cnt), "TR_SLIGHTBLOOD")), oldorg, self.origin);
48         else
49                 trailparticles(self, particleeffectnum(strcat(species_prefix(self.cnt), "TR_BLOOD")), oldorg, self.origin);
50
51         self.renderflags = 0;
52         self.alpha = bound(0, self.nextthink - time, 1);
53
54         if(self.alpha < ALPHA_MIN_VISIBLE)
55                 remove(self);
56         else
57                 R_AddEntity(self);
58 }
59
60 void TossGib (string mdlname, vector org, vector vconst, vector vrand, float specnum, float destroyontouch)
61 {
62         entity gib;
63
64         // TODO remove some gibs according to cl_nogibs
65         
66         gib = spawn();
67         gib.classname = "gib";
68         gib.move_movetype = MOVETYPE_BOUNCE;
69         gib.gravity = 1;
70         gib.solid = SOLID_CORPSE;
71         gib.cnt = specnum;
72
73         setmodel (gib, mdlname); // precision set above
74         gib.skin = specnum;
75
76         setsize (gib, '-8 -8 -8', '8 8 8');
77                                           
78         gib.draw = Gib_Draw;
79         if(destroyontouch)
80                 gib.move_touch = Gib_Touch;
81         else
82                 gib.move_touch = SUB_RemoveOnNoImpact;
83
84         gib.move_origin = gib.origin = org;
85         gib.move_velocity = vconst * cvar_or("cl_gibs_velocity_scale", 1) + vrand * cvar_or("cl_gibs_velocity_random", 1) + '0 0 1' * cvar("cl_gibs_velocity_up");
86         gib.move_avelocity = prandomvec() * vlen(gib.move_velocity);
87         gib.move_time = time;
88         gib.damageforcescale = cvar_or("cl_gibs_damageforcescale", 3.5);
89
90         gib.nextthink = time + cvar_or("cl_gibs_lifetime", 14) * (1 + prandom() * 0.15);
91 }
92
93 void Ent_GibSplash()
94 {
95         float amount, type, specnum;
96         vector org, vel, mi, ma;
97         string specstr;
98
99         float c, gibfactor, randomvalue;
100
101         type = ReadByte(); // gibbage type
102         amount = ReadByte() / 16.0; // gibbage amount
103         org_x = ReadShort() * 4 + 2;
104         org_y = ReadShort() * 4 + 2;
105         org_z = ReadShort() * 4 + 2;
106         vel = decompressShortVector(ReadShort());
107         mi = decompressShortVector(ReadShort());
108         ma = decompressShortVector(ReadShort());
109
110         if(cvar("cl_gentle"))
111                 type |= 0x80; // set gentle bit
112         
113         gibfactor = 1 - cvar("cl_nogibs");
114         if(gibfactor <= 0)
115                 return;
116         
117         amount *= gibfactor;
118
119         if(cvar("ekg"))
120                 amount *= 5;
121         
122         self.origin = org; // for the sounds
123
124         specnum = (type & 0x78) / 8; // blood/gibmodel type: using four bits (0..15, bit indexes 3,4,5,6)
125         type = type & 0x87; // remove the species bits: bit 7 = gentle, bit 0,1,2 = kind of gib
126         specstr = species_prefix(specnum);
127
128         switch(type)
129         {
130                 case 0x01:
131                         sound (self, CHAN_PAIN, "misc/gib.wav", VOL_BASE, ATTN_NORM);
132
133                         if(prandom() < amount)
134                                 TossGib ("models/gibs/eye.md3", org, vel, prandomvec() * 150, specnum, 0);
135                         te_bloodshower(org + mi, org + ma, 1200, 1000 * amount);
136                         if(prandom() < amount)
137                                 TossGib ("models/gibs/bloodyskull.md3", org, vel, prandomvec() * 100, specnum, 0);
138
139                         for(c = 0; c < amount; ++c)
140                         {
141                                 randomvalue = amount - c;
142                                         
143                                 if(prandom() < randomvalue)
144                                         TossGib ("models/gibs/arm.md3", org, vel, prandomvec() * (prandom() * 120 + 90), specnum,0);
145                                 if(prandom() < randomvalue)
146                                         TossGib ("models/gibs/arm.md3", org, vel, prandomvec() * (prandom() * 120 + 90), specnum,0);
147                                 if(prandom() < randomvalue)
148                                         TossGib ("models/gibs/chest.md3", org + '0 0 -12', vel, prandomvec() * (prandom() * 120 + 80), specnum,0);
149                                 if(prandom() < randomvalue)
150                                         TossGib ("models/gibs/smallchest.md3", org, vel, prandomvec() * (prandom() * 120 + 80), specnum,0);
151                                 if(prandom() < randomvalue)
152                                         TossGib ("models/gibs/leg1.md3", org + '0 0 -5', vel, prandomvec() * (prandom() * 120 + 85), specnum,0);
153                                 if(prandom() < randomvalue)
154                                         TossGib ("models/gibs/leg2.md3", org + '0 0 -9', vel, prandomvec() * (prandom() * 120 + 85), specnum,0);
155
156                                 // these splat on impact
157                                 if(prandom() < randomvalue)
158                                         TossGib ("models/gibs/chunk.mdl", org, vel, prandomvec() * 450, specnum,1);
159                                 if(prandom() < randomvalue)
160                                         TossGib ("models/gibs/chunk.mdl", org, vel, prandomvec() * 450, specnum,1);
161                                 if(prandom() < randomvalue)
162                                         TossGib ("models/gibs/chunk.mdl", org, vel, prandomvec() * 450, specnum,1);
163                                 if(prandom() < randomvalue)
164                                         TossGib ("models/gibs/chunk.mdl", org, vel, prandomvec() * 450, specnum,1);
165                         }
166                         break;
167                 case 0x02:
168                         pointparticles(particleeffectnum(strcat(specstr, "blood")), org, vel, amount * 16);
169                         break;
170                 case 0x03:
171                         if(prandom() < amount)
172                                 TossGib ("models/gibs/chunk.mdl", org, vel, prandomvec() * (prandom() * 30 + 20), specnum, 1); // TODO maybe adjust to more randomization?
173                         break;
174                 case 0x81:
175                         pointparticles(particleeffectnum("damage_dissolve"), org, vel, amount);
176                         break;
177                 case 0x82:
178                         pointparticles(particleeffectnum("damage_hit"), org, vel, amount * 16);
179                         break;
180                 case 0x83:
181                         // no gibs in gentle mode, sorry
182                         break;
183         }
184 }
185
186 void GibSplash_Precache()
187 {
188         precache_model("models/gibs/chunk.mdl");
189         precache_model("models/gibs/leg1.md3");
190         precache_model("models/gibs/leg2.md3");
191         precache_model("models/gibs/chest.md3");
192         precache_model("models/gibs/smallchest.md3");
193         precache_model("models/gibs/arm.md3");
194         precache_model("models/gibs/bloodyskull.md3");
195         precache_model("models/gibs/eye.md3");
196
197         precache_sound ("misc/gib.wav");
198     precache_sound ("misc/gib_splat01.wav");
199     precache_sound ("misc/gib_splat02.wav");
200     precache_sound ("misc/gib_splat03.wav");
201     precache_sound ("misc/gib_splat04.wav");
202 }