]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_particles.c
skip bbox check on bmodels because it is fast enough to trace against their hull...
[divverent/darkplaces.git] / cl_particles.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22
23 #define MAX_PARTICLES                   16384   // default max # of particles at one time
24 #define ABSOLUTE_MIN_PARTICLES  512             // no fewer than this no matter what's on the command line
25
26 typedef enum
27 {
28         pt_static, pt_grav, pt_blob, pt_blob2, pt_bulletsmoke, pt_smoke, pt_snow, pt_rain, pt_spark, pt_bubble, pt_fade, pt_steam, pt_splash, pt_splashpuff, pt_flame, pt_blood, pt_oneframe, pt_lavasplash, pt_raindropsplash, pt_underwaterspark, pt_explosionsplash
29 }
30 ptype_t;
31
32 typedef struct particle_s
33 {
34         ptype_t         type;
35         int                     orientation; // typically PARTICLE_BILLBOARD
36         vec3_t          org;
37         vec3_t          vel;
38         int                     additive;
39         int                     tex;
40         float           die;
41         float           scalex;
42         float           scaley;
43         float           alpha; // 0-255
44         float           time2; // used for various things (snow fluttering, for example)
45         float           bounce; // how much bounce-back from a surface the particle hits (0 = no physics, 1 = stop and slide, 2 = keep bouncing forever, 1.5 is typical)
46         vec3_t          oldorg;
47         vec3_t          vel2; // used for snow fluttering (base velocity, wind for instance)
48         float           friction; // how much air friction affects this object (objects with a low mass/size ratio tend to get more air friction)
49         float           pressure; // if non-zero, apply pressure to other particles
50         int                     dynlight; // if set the particle will be dynamically lit (if cl_dynamicparticles is on), used for smoke and blood
51         qbyte           color[4];
52 }
53 particle_t;
54
55 static int particlepalette[256] =
56 {
57         0x000000,0x0f0f0f,0x1f1f1f,0x2f2f2f,0x3f3f3f,0x4b4b4b,0x5b5b5b,0x6b6b6b,
58         0x7b7b7b,0x8b8b8b,0x9b9b9b,0xababab,0xbbbbbb,0xcbcbcb,0xdbdbdb,0xebebeb,
59         0x0f0b07,0x170f0b,0x1f170b,0x271b0f,0x2f2313,0x372b17,0x3f2f17,0x4b371b,
60         0x533b1b,0x5b431f,0x634b1f,0x6b531f,0x73571f,0x7b5f23,0x836723,0x8f6f23,
61         0x0b0b0f,0x13131b,0x1b1b27,0x272733,0x2f2f3f,0x37374b,0x3f3f57,0x474767,
62         0x4f4f73,0x5b5b7f,0x63638b,0x6b6b97,0x7373a3,0x7b7baf,0x8383bb,0x8b8bcb,
63         0x000000,0x070700,0x0b0b00,0x131300,0x1b1b00,0x232300,0x2b2b07,0x2f2f07,
64         0x373707,0x3f3f07,0x474707,0x4b4b0b,0x53530b,0x5b5b0b,0x63630b,0x6b6b0f,
65         0x070000,0x0f0000,0x170000,0x1f0000,0x270000,0x2f0000,0x370000,0x3f0000,
66         0x470000,0x4f0000,0x570000,0x5f0000,0x670000,0x6f0000,0x770000,0x7f0000,
67         0x131300,0x1b1b00,0x232300,0x2f2b00,0x372f00,0x433700,0x4b3b07,0x574307,
68         0x5f4707,0x6b4b0b,0x77530f,0x835713,0x8b5b13,0x975f1b,0xa3631f,0xaf6723,
69         0x231307,0x2f170b,0x3b1f0f,0x4b2313,0x572b17,0x632f1f,0x733723,0x7f3b2b,
70         0x8f4333,0x9f4f33,0xaf632f,0xbf772f,0xcf8f2b,0xdfab27,0xefcb1f,0xfff31b,
71         0x0b0700,0x1b1300,0x2b230f,0x372b13,0x47331b,0x533723,0x633f2b,0x6f4733,
72         0x7f533f,0x8b5f47,0x9b6b53,0xa77b5f,0xb7876b,0xc3937b,0xd3a38b,0xe3b397,
73         0xab8ba3,0x9f7f97,0x937387,0x8b677b,0x7f5b6f,0x775363,0x6b4b57,0x5f3f4b,
74         0x573743,0x4b2f37,0x43272f,0x371f23,0x2b171b,0x231313,0x170b0b,0x0f0707,
75         0xbb739f,0xaf6b8f,0xa35f83,0x975777,0x8b4f6b,0x7f4b5f,0x734353,0x6b3b4b,
76         0x5f333f,0x532b37,0x47232b,0x3b1f23,0x2f171b,0x231313,0x170b0b,0x0f0707,
77         0xdbc3bb,0xcbb3a7,0xbfa39b,0xaf978b,0xa3877b,0x977b6f,0x876f5f,0x7b6353,
78         0x6b5747,0x5f4b3b,0x533f33,0x433327,0x372b1f,0x271f17,0x1b130f,0x0f0b07,
79         0x6f837b,0x677b6f,0x5f7367,0x576b5f,0x4f6357,0x475b4f,0x3f5347,0x374b3f,
80         0x2f4337,0x2b3b2f,0x233327,0x1f2b1f,0x172317,0x0f1b13,0x0b130b,0x070b07,
81         0xfff31b,0xefdf17,0xdbcb13,0xcbb70f,0xbba70f,0xab970b,0x9b8307,0x8b7307,
82         0x7b6307,0x6b5300,0x5b4700,0x4b3700,0x3b2b00,0x2b1f00,0x1b0f00,0x0b0700,
83         0x0000ff,0x0b0bef,0x1313df,0x1b1bcf,0x2323bf,0x2b2baf,0x2f2f9f,0x2f2f8f,
84         0x2f2f7f,0x2f2f6f,0x2f2f5f,0x2b2b4f,0x23233f,0x1b1b2f,0x13131f,0x0b0b0f,
85         0x2b0000,0x3b0000,0x4b0700,0x5f0700,0x6f0f00,0x7f1707,0x931f07,0xa3270b,
86         0xb7330f,0xc34b1b,0xcf632b,0xdb7f3b,0xe3974f,0xe7ab5f,0xefbf77,0xf7d38b,
87         0xa77b3b,0xb79b37,0xc7c337,0xe7e357,0x7fbfff,0xabe7ff,0xd7ffff,0x670000,
88         0x8b0000,0xb30000,0xd70000,0xff0000,0xfff393,0xfff7c7,0xffffff,0x9f5b53
89 };
90
91 static int explosparkramp[8] = {0x4b0700, 0x6f0f00, 0x931f07, 0xb7330f, 0xcf632b, 0xe3974f, 0xffe7b5, 0xffffff};
92 //static int explounderwatersparkramp[8] = {0x00074b, 0x000f6f, 0x071f93, 0x0f33b7, 0x2b63cf, 0x4f97e3, 0xb5e7ff, 0xffffff};
93
94 // these must match r_part.c's textures
95 static const int tex_smoke[8] = {0, 1, 2, 3, 4, 5, 6, 7};
96 static const int tex_rainsplash[16] = {8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
97 static const int tex_particle = 24;
98 static const int tex_rain = 25;
99 static const int tex_bubble = 26;
100 static const int tex_rocketglow = 27;
101
102 static int                      cl_maxparticles;
103 static int                      cl_numparticles;
104 static particle_t       *particles;
105 static particle_t       **freeparticles; // list used only in compacting particles array
106 static renderparticle_t *cl_renderparticles;
107
108 static cvar_t cl_particles = {CVAR_SAVE, "cl_particles", "1"};
109 static cvar_t cl_particles_size = {CVAR_SAVE, "cl_particles_size", "1"};
110 static cvar_t cl_particles_bloodshowers = {CVAR_SAVE, "cl_particles_bloodshowers", "1"};
111 static cvar_t cl_particles_blood = {CVAR_SAVE, "cl_particles_blood", "1"};
112 static cvar_t cl_particles_blood_size_min = {CVAR_SAVE, "cl_particles_blood_size_min", "3"};
113 static cvar_t cl_particles_blood_size_max = {CVAR_SAVE, "cl_particles_blood_size_max", "15"};
114 static cvar_t cl_particles_blood_alpha = {CVAR_SAVE, "cl_particles_blood_alpha", "1"};
115 static cvar_t cl_particles_smoke = {CVAR_SAVE, "cl_particles_smoke", "1"};
116 static cvar_t cl_particles_sparks = {CVAR_SAVE, "cl_particles_sparks", "1"};
117 static cvar_t cl_particles_bubbles = {CVAR_SAVE, "cl_particles_bubbles", "1"};
118 static cvar_t cl_particles_explosions = {CVAR_SAVE, "cl_particles_explosions", "0"};
119
120 static mempool_t *cl_part_mempool;
121
122 void CL_Particles_Clear(void)
123 {
124         cl_numparticles = 0;
125 }
126
127 /*
128 ===============
129 CL_InitParticles
130 ===============
131 */
132 void CL_ReadPointFile_f (void);
133 void CL_Particles_Init (void)
134 {
135         int             i;
136
137         i = COM_CheckParm ("-particles");
138
139         if (i)
140         {
141                 cl_maxparticles = (int)(atoi(com_argv[i+1]));
142                 if (cl_maxparticles < ABSOLUTE_MIN_PARTICLES)
143                         cl_maxparticles = ABSOLUTE_MIN_PARTICLES;
144         }
145         else
146                 cl_maxparticles = MAX_PARTICLES;
147
148         Cmd_AddCommand ("pointfile", CL_ReadPointFile_f);
149
150         Cvar_RegisterVariable (&cl_particles);
151         Cvar_RegisterVariable (&cl_particles_size);
152         Cvar_RegisterVariable (&cl_particles_bloodshowers);
153         Cvar_RegisterVariable (&cl_particles_blood);
154         Cvar_RegisterVariable (&cl_particles_blood_size_min);
155         Cvar_RegisterVariable (&cl_particles_blood_size_max);
156         Cvar_RegisterVariable (&cl_particles_blood_alpha);
157         Cvar_RegisterVariable (&cl_particles_smoke);
158         Cvar_RegisterVariable (&cl_particles_sparks);
159         Cvar_RegisterVariable (&cl_particles_bubbles);
160         Cvar_RegisterVariable (&cl_particles_explosions);
161
162         cl_part_mempool = Mem_AllocPool("CL_Part");
163         particles = (particle_t *) Mem_Alloc(cl_part_mempool, cl_maxparticles * sizeof(particle_t));
164         freeparticles = (void *) Mem_Alloc(cl_part_mempool, cl_maxparticles * sizeof(particle_t *));
165         cl_numparticles = 0;
166
167         // FIXME: r_refdef stuff should be allocated somewhere else?
168         r_refdef.particles = cl_renderparticles = Mem_Alloc(cl_refdef_mempool, cl_maxparticles * sizeof(renderparticle_t));
169 }
170
171 #define particle(ptype, porientation, pcolor, ptex, plight, padditive, pscalex, pscaley, palpha, ptime, pbounce, px, py, pz, pvx, pvy, pvz, ptime2, pvx2, pvy2, pvz2, pfriction, ppressure)\
172 {\
173         particle_t      *part;\
174         int tempcolor;\
175         if (cl_numparticles >= cl_maxparticles)\
176                 return;\
177         part = &particles[cl_numparticles++];\
178         part->type = (ptype);\
179         tempcolor = (pcolor);\
180         part->color[0] = ((tempcolor) >> 16) & 0xFF;\
181         part->color[1] = ((tempcolor) >> 8) & 0xFF;\
182         part->color[2] = (tempcolor) & 0xFF;\
183         part->color[3] = 0xFF;\
184         part->tex = (ptex);\
185         part->orientation = (porientation);\
186         part->dynlight = (plight);\
187         part->additive = (padditive);\
188         part->scalex = (pscalex);\
189         part->scaley = (pscaley);\
190         part->alpha = (palpha);\
191         part->die = cl.time + (ptime);\
192         part->bounce = (pbounce);\
193         part->org[0] = (px);\
194         part->org[1] = (py);\
195         part->org[2] = (pz);\
196         part->vel[0] = (pvx);\
197         part->vel[1] = (pvy);\
198         part->vel[2] = (pvz);\
199         part->time2 = (ptime2);\
200         part->vel2[0] = (pvx2);\
201         part->vel2[1] = (pvy2);\
202         part->vel2[2] = (pvz2);\
203         part->friction = (pfriction);\
204         part->pressure = (ppressure);\
205 }
206
207 /*
208 ===============
209 CL_EntityParticles
210 ===============
211 */
212 void CL_EntityParticles (entity_t *ent)
213 {
214         int                     i;
215         float           angle;
216         float           sp, sy, cp, cy;
217         vec3_t          forward;
218         float           dist;
219         float           beamlength;
220         static vec3_t avelocities[NUMVERTEXNORMALS];
221         if (!cl_particles.integer) return;
222
223         dist = 64;
224         beamlength = 16;
225
226         if (!avelocities[0][0])
227                 for (i=0 ; i<NUMVERTEXNORMALS*3 ; i++)
228                         avelocities[0][i] = (rand()&255) * 0.01;
229
230         for (i=0 ; i<NUMVERTEXNORMALS ; i++)
231         {
232                 angle = cl.time * avelocities[i][0];
233                 sy = sin(angle);
234                 cy = cos(angle);
235                 angle = cl.time * avelocities[i][1];
236                 sp = sin(angle);
237                 cp = cos(angle);
238
239                 forward[0] = cp*cy;
240                 forward[1] = cp*sy;
241                 forward[2] = -sp;
242
243                 particle(pt_oneframe, PARTICLE_BILLBOARD, particlepalette[0x6f], tex_particle, false, false, 2, 2, 255, 9999, 0, ent->render.origin[0] + m_bytenormals[i][0]*dist + forward[0]*beamlength, ent->render.origin[1] + m_bytenormals[i][1]*dist + forward[1]*beamlength, ent->render.origin[2] + m_bytenormals[i][2]*dist + forward[2]*beamlength, 0, 0, 0, 0, 0, 0, 0, 0, 0);
244         }
245 }
246
247
248 void CL_ReadPointFile_f (void)
249 {
250         vec3_t  org;
251         int             r, c;
252         char    *pointfile, *pointfilepos, *t, tchar;
253
254         pointfile = COM_LoadFile(va("maps/%s.pts", sv.name), true);
255         if (!pointfile)
256         {
257                 Con_Printf ("couldn't open %s.pts\n", sv.name);
258                 return;
259         }
260
261         Con_Printf ("Reading %s.pts...\n", sv.name);
262         c = 0;
263         pointfilepos = pointfile;
264         while (*pointfilepos)
265         {
266                 while (*pointfilepos == '\n' || *pointfilepos == '\r')
267                         pointfilepos++;
268                 if (!*pointfilepos)
269                         break;
270                 t = pointfilepos;
271                 while (*t && *t != '\n' && *t != '\r')
272                         t++;
273                 tchar = *t;
274                 *t = 0;
275                 r = sscanf (pointfilepos,"%f %f %f", &org[0], &org[1], &org[2]);
276                 *t = tchar;
277                 pointfilepos = t;
278                 if (r != 3)
279                         break;
280                 c++;
281
282                 if (cl_numparticles >= cl_maxparticles)
283                 {
284                         Con_Printf ("Not enough free particles\n");
285                         break;
286                 }
287                 particle(pt_static, PARTICLE_BILLBOARD, particlepalette[(-c)&15], tex_particle, false, false, 2, 2, 255, 99999, 0, org[0], org[1], org[2], 0, 0, 0, 0, 0, 0, 0, 0, 0);
288         }
289
290         Mem_Free(pointfile);
291         Con_Printf ("%i points read\n", c);
292 }
293
294 /*
295 ===============
296 CL_ParseParticleEffect
297
298 Parse an effect out of the server message
299 ===============
300 */
301 void CL_ParseParticleEffect (void)
302 {
303         vec3_t          org, dir;
304         int                     i, count, msgcount, color;
305
306         for (i=0 ; i<3 ; i++)
307                 org[i] = MSG_ReadCoord ();
308         for (i=0 ; i<3 ; i++)
309                 dir[i] = MSG_ReadChar () * (1.0/16);
310         msgcount = MSG_ReadByte ();
311         color = MSG_ReadByte ();
312
313         if (msgcount == 255)
314                 count = 1024;
315         else
316                 count = msgcount;
317
318         CL_RunParticleEffect (org, dir, color, count);
319 }
320
321 /*
322 ===============
323 CL_ParticleExplosion
324
325 ===============
326 */
327 void CL_ParticleExplosion (vec3_t org, int smoke)
328 {
329         R_Stain(org, 96, 80, 80, 80, 128, 176, 176, 176, 128);
330         if (cl_particles.integer && cl_particles_explosions.integer)
331         {
332                 int i, j;
333                 float f;
334                 vec3_t v, end, ang;
335                 qbyte noise1[32*32], noise2[32*32];
336
337                 VectorClear(end); // hush MSVC
338                 i = Mod_PointInLeaf(org, cl.worldmodel)->contents;
339                 if (i == CONTENTS_SLIME || i == CONTENTS_WATER)
340                 {
341                         for (i = 0;i < 128;i++)
342                                 particle(pt_bubble, PARTICLE_BILLBOARD, 0xFFFFFF, tex_bubble, false, true, 2, 2, 255, 9999, 1.5, org[0] + lhrandom(-16, 16), org[1] + lhrandom(-16, 16), org[2] + lhrandom(-16, 16), lhrandom(-96, 96), lhrandom(-96, 96), lhrandom(-96, 96), 0, 0, 0, 0, 0, 0);
343
344                         ang[2] = lhrandom(0, 360);
345                         fractalnoisequick(noise1, 32, 4);
346                         fractalnoisequick(noise2, 32, 8);
347                         for (i = 0;i < 32;i++)
348                         {
349                                 for (j = 0;j < 32;j++)
350                                 {
351                                         VectorRandom(v);
352                                         VectorMA(org, 16, v, v);
353                                         TraceLine(org, v, end, NULL, 0, true);
354                                         ang[0] = (j + 0.5f) * (360.0f / 32.0f);
355                                         ang[1] = (i + 0.5f) * (360.0f / 32.0f);
356                                         AngleVectors(ang, v, NULL, NULL);
357                                         f = noise1[j*32+i] * 1.5f;
358                                         VectorScale(v, f, v);
359                                         particle(pt_underwaterspark, PARTICLE_BILLBOARD, noise2[j*32+i] * 0x010101, tex_smoke[rand()&7], false, true, 10, 10, lhrandom(128, 255), 9999, 1.5, end[0], end[1], end[2], v[0], v[1], v[2], 512.0f, 0, 0, 0, 2, 0);
360                                         VectorScale(v, 0.75, v);
361                                         particle(pt_underwaterspark, PARTICLE_BILLBOARD, explosparkramp[(noise2[j*32+i] >> 5)], tex_particle, false, true, 10, 10, lhrandom(128, 255), 9999, 1.5, end[0], end[1], end[2], v[0], v[1], v[2], 512.0f, 0, 0, 0, 2, 0);
362                                 }
363                         }
364                 }
365                 else
366                 {
367                         ang[2] = lhrandom(0, 360);
368                         fractalnoisequick(noise1, 32, 4);
369                         fractalnoisequick(noise2, 32, 8);
370                         for (i = 0;i < 32;i++)
371                         {
372                                 for (j = 0;j < 32;j++)
373                                 {
374                                         VectorRandom(v);
375                                         VectorMA(org, 16, v, v);
376                                         TraceLine(org, v, end, NULL, 0, true);
377                                         ang[0] = (j + 0.5f) * (360.0f / 32.0f);
378                                         ang[1] = (i + 0.5f) * (360.0f / 32.0f);
379                                         AngleVectors(ang, v, NULL, NULL);
380                                         f = noise1[j*32+i] * 1.5f;
381                                         VectorScale(v, f, v);
382                                         particle(pt_spark, PARTICLE_BILLBOARD, noise2[j*32+i] * 0x010101, tex_smoke[rand()&7], false, true, 10, 10, lhrandom(128, 255), 9999, 1.5, end[0], end[1], end[2], v[0], v[1], v[2] + 160.0f, 512.0f, 0, 0, 0, 2, 0);
383                                         VectorScale(v, 0.75, v);
384                                         particle(pt_spark, PARTICLE_BILLBOARD, explosparkramp[(noise2[j*32+i] >> 5)], tex_particle, false, true, 10, 10, lhrandom(128, 255), 9999, 1.5, end[0], end[1], end[2], v[0], v[1], v[2] + 160.0f, 512.0f, 0, 0, 0, 2, 0);
385                                 //      VectorRandom(v);
386                                 //      VectorScale(v, 384, v);
387                                 //      particle(pt_spark, PARTICLE_BILLBOARD, explosparkramp[rand()&7], tex_particle, false, true, 2, 2, lhrandom(16, 255), 9999, 1.5, end[0], end[1], end[2], v[0], v[1], v[2] + 160.0f, 512.0f, 0, 0, 0, 2, 0);
388                                 }
389                         }
390                 }
391         }
392         else
393         {
394                 /*
395                 int i;
396                 vec3_t v;
397                 for (i = 0;i < 256;i++)
398                 {
399                         do
400                         {
401                                 VectorRandom(v);
402                         }
403                         while(DotProduct(v,v) < 0.75);
404                         VectorScale(v, 512, v);
405                         particle(pt_spark, PARTICLE_BILLBOARD, explosparkramp[rand()&7], tex_particle, false, true, 4, 4, 255, 9999, 1.5, org[0], org[1], org[2], v[0], v[1], v[2] + 160.0f, 512.0f, 0, 0, 0, 2, 0);
406                 }
407                 */
408                 R_NewExplosion(org);
409         }
410 }
411
412 /*
413 ===============
414 CL_ParticleExplosion2
415
416 ===============
417 */
418 void CL_ParticleExplosion2 (vec3_t org, int colorStart, int colorLength)
419 {
420         int                     i;
421         if (!cl_particles.integer) return;
422
423         for (i = 0;i < 512;i++)
424                 particle(pt_fade, PARTICLE_BILLBOARD, particlepalette[colorStart + (i % colorLength)], tex_particle, false, false, 1.5, 1.5, 255, 0.3, 0, org[0] + lhrandom(-8, 8), org[1] + lhrandom(-8, 8), org[2] + lhrandom(-8, 8), lhrandom(-192, 192), lhrandom(-192, 192), lhrandom(-192, 192), 384, 0, 0, 0, 1, 0);
425 }
426
427 /*
428 ===============
429 CL_BlobExplosion
430
431 ===============
432 */
433 void CL_BlobExplosion (vec3_t org)
434 {
435         //int i;
436         if (!cl_particles.integer) return;
437
438         R_Stain(org, 96, 80, 80, 80, 128, 176, 176, 176, 128);
439         //R_Stain(org, 96, 96, 64, 96, 128, 160, 128, 160, 128);
440
441         R_NewExplosion(org);
442
443         //for (i = 0;i < 256;i++)
444         //      particle(pt_blob , PARTICLE_BILLBOARD, particlepalette[ 66+(rand()%6)], tex_particle, false, true, 4, 4, 255, 9999, 0, org[0] + lhrandom(-16, 16), org[1] + lhrandom(-16, 16), org[2] + lhrandom(-16, 16), lhrandom(-4, 4), lhrandom(-4, 4), lhrandom(-128, 128), 0, 0, 0, 0, 0, 0);
445         //for (i = 0;i < 256;i++)
446         //      particle(pt_blob2, PARTICLE_BILLBOARD, particlepalette[150+(rand()%6)], tex_particle, false, true, 4, 4, 255, 9999, 0, org[0] + lhrandom(-16, 16), org[1] + lhrandom(-16, 16), org[2] + lhrandom(-16, 16), lhrandom(-4, 4), lhrandom(-4, 4), lhrandom(-128, 128), 0, 0, 0, 0, 0, 0);
447 }
448
449 /*
450 ===============
451 CL_RunParticleEffect
452
453 ===============
454 */
455 void CL_RunParticleEffect (vec3_t org, vec3_t dir, int color, int count)
456 {
457         if (!cl_particles.integer) return;
458
459         if (count == 1024)
460         {
461                 CL_ParticleExplosion(org, false);
462                 return;
463         }
464         while (count--)
465                 particle(pt_fade, PARTICLE_BILLBOARD, particlepalette[color + (rand()&7)], tex_particle, false, false, 1, 1, 128, 9999, 0, org[0] + lhrandom(-8, 8), org[1] + lhrandom(-8, 8), org[2] + lhrandom(-8, 8), lhrandom(-15, 15), lhrandom(-15, 15), lhrandom(-15, 15), 384, 0, 0, 0, 0, 0);
466 }
467
468 // LordHavoc: added this for spawning sparks/dust (which have strong gravity)
469 /*
470 ===============
471 CL_SparkShower
472 ===============
473 */
474 void CL_SparkShower (vec3_t org, vec3_t dir, int count)
475 {
476         if (!cl_particles.integer) return;
477
478         R_Stain(org, 32, 96, 96, 96, 32, 128, 128, 128, 32);
479
480         // smoke puff
481         if (cl_particles_smoke.integer)
482                 particle(pt_bulletsmoke, PARTICLE_BILLBOARD, 0xFFFFFF /*0xA0A0A0*/, tex_smoke[rand()&7], true, true, 2, 2, 255, 9999, 0, org[0], org[1], org[2], lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(0, 16), 0, 0, 0, 0, 0, 0);
483
484         if (cl_particles_sparks.integer)
485         {
486                 // sparks
487                 while(count--)
488                         particle(pt_spark, PARTICLE_BILLBOARD, particlepalette[0x68 + (rand() & 7)], tex_particle, false, true, 1, 1, lhrandom(64, 128), 9999, 0, org[0], org[1], org[2], lhrandom(-64, 64) + dir[0], lhrandom(-64, 64) + dir[1], lhrandom(0, 128) + dir[2], 480, 0, 0, 0, 1, 0);
489         }
490 }
491
492 void CL_PlasmaBurn (vec3_t org)
493 {
494         if (!cl_particles.integer) return;
495
496         R_Stain(org, 48, 96, 96, 96, 48, 128, 128, 128, 48);
497 }
498
499 void CL_BloodPuff (vec3_t org, vec3_t vel, int count)
500 {
501         float r, s;
502         // bloodcount is used to accumulate counts too small to cause a blood particle
503         static int bloodcount = 0;
504         if (!cl_particles.integer) return;
505         if (!cl_particles_blood.integer) return;
506
507         s = count + 32.0f;
508         count *= 5.0f;
509         if (count > 1000)
510                 count = 1000;
511         bloodcount += count;
512         while(bloodcount > 0)
513         {
514                 r = lhrandom(cl_particles_blood_size_min.value, cl_particles_blood_size_max.value);
515                 particle(pt_blood, PARTICLE_BILLBOARD, 0x300000, tex_smoke[rand()&7], true, false, r, r, cl_particles_blood_alpha.value * 255, 9999, -1, org[0], org[1], org[2], vel[0] + lhrandom(-s, s), vel[1] + lhrandom(-s, s), vel[2] + lhrandom(-s, s), 0, 0, 0, 0, 1, 0);
516                 bloodcount -= r;
517         }
518 }
519
520 void CL_BloodShower (vec3_t mins, vec3_t maxs, float velspeed, int count)
521 {
522         float c;
523         float r;
524         vec3_t diff, center, velscale;
525         if (!cl_particles.integer) return;
526         if (!cl_particles_bloodshowers.integer) return;
527         if (!cl_particles_blood.integer) return;
528
529         VectorSubtract(maxs, mins, diff);
530         center[0] = (mins[0] + maxs[0]) * 0.5;
531         center[1] = (mins[1] + maxs[1]) * 0.5;
532         center[2] = (mins[2] + maxs[2]) * 0.5;
533         // FIXME: change velspeed back to 2.0x after fixing mod
534         velscale[0] = velspeed * 2.0 / diff[0];
535         velscale[1] = velspeed * 2.0 / diff[1];
536         velscale[2] = velspeed * 2.0 / diff[2];
537
538         c = count * 5;
539         while (c > 0)
540         {
541                 vec3_t org, vel;
542                 org[0] = lhrandom(mins[0], maxs[0]);
543                 org[1] = lhrandom(mins[1], maxs[1]);
544                 org[2] = lhrandom(mins[2], maxs[2]);
545                 vel[0] = (org[0] - center[0]) * velscale[0];
546                 vel[1] = (org[1] - center[1]) * velscale[1];
547                 vel[2] = (org[2] - center[2]) * velscale[2];
548                 r = lhrandom(cl_particles_blood_size_min.value, cl_particles_blood_size_max.value);
549                 c -= r;
550                 particle(pt_blood, PARTICLE_BILLBOARD, 0x300000, tex_smoke[rand()&7], true, false, r, r, cl_particles_blood_alpha.value * 255, 9999, -1, org[0], org[1], org[2], vel[0], vel[1], vel[2], 0, 0, 0, 0, 1, 0);
551         }
552 }
553
554 void CL_ParticleCube (vec3_t mins, vec3_t maxs, vec3_t dir, int count, int colorbase, int gravity, int randomvel)
555 {
556         float t;
557         if (!cl_particles.integer) return;
558         if (maxs[0] <= mins[0]) {t = mins[0];mins[0] = maxs[0];maxs[0] = t;}
559         if (maxs[1] <= mins[1]) {t = mins[1];mins[1] = maxs[1];maxs[1] = t;}
560         if (maxs[2] <= mins[2]) {t = mins[2];mins[2] = maxs[2];maxs[2] = t;}
561
562         while (count--)
563                 particle(gravity ? pt_grav : pt_static, PARTICLE_BILLBOARD, particlepalette[colorbase + (rand()&3)], tex_particle, false, false, 2, 2, 255, lhrandom(1, 2), 0, lhrandom(mins[0], maxs[0]), lhrandom(mins[1], maxs[1]), lhrandom(mins[2], maxs[2]), dir[0] + lhrandom(-randomvel, randomvel), dir[1] + lhrandom(-randomvel, randomvel), dir[2] + lhrandom(-randomvel, randomvel), 0, 0, 0, 0, 0, 0);
564 }
565
566 void CL_ParticleRain (vec3_t mins, vec3_t maxs, vec3_t dir, int count, int colorbase, int type)
567 {
568         vec3_t vel;
569         float t, z;
570         if (!cl_particles.integer) return;
571         if (maxs[0] <= mins[0]) {t = mins[0];mins[0] = maxs[0];maxs[0] = t;}
572         if (maxs[1] <= mins[1]) {t = mins[1];mins[1] = maxs[1];maxs[1] = t;}
573         if (maxs[2] <= mins[2]) {t = mins[2];mins[2] = maxs[2];maxs[2] = t;}
574         if (dir[2] < 0) // falling
575         {
576                 t = (maxs[2] - mins[2]) / -dir[2];
577                 z = maxs[2];
578         }
579         else // rising??
580         {
581                 t = (maxs[2] - mins[2]) / dir[2];
582                 z = mins[2];
583         }
584         if (t < 0 || t > 2) // sanity check
585                 t = 2;
586
587         switch(type)
588         {
589         case 0:
590                 count *= 4; // ick, this should be in the mod or maps?
591
592                 while(count--)
593                 {
594                         vel[0] = dir[0] + lhrandom(-16, 16);
595                         vel[1] = dir[1] + lhrandom(-16, 16);
596                         vel[2] = dir[2] + lhrandom(-32, 32);
597                         particle(pt_rain, PARTICLE_UPRIGHT_FACING, particlepalette[colorbase + (rand()&3)], tex_particle, true, true, 1, 64, 64, t, 0, lhrandom(mins[0], maxs[0]), lhrandom(mins[1], maxs[1]), z, vel[0], vel[1], vel[2], 0, vel[0], vel[1], vel[2], 0, 0);
598                 }
599                 break;
600         case 1:
601                 while(count--)
602                 {
603                         vel[0] = dir[0] + lhrandom(-16, 16);
604                         vel[1] = dir[1] + lhrandom(-16, 16);
605                         vel[2] = dir[2] + lhrandom(-32, 32);
606                         particle(pt_snow, PARTICLE_BILLBOARD, particlepalette[colorbase + (rand()&3)], tex_particle, false, true, 2, 2, 255, t, 0, lhrandom(mins[0], maxs[0]), lhrandom(mins[1], maxs[1]), z, vel[0], vel[1], vel[2], 0, vel[0], vel[1], vel[2], 0, 0);
607                 }
608                 break;
609         default:
610                 Host_Error("CL_ParticleRain: unknown type %i (0 = rain, 1 = snow)\n", type);
611         }
612 }
613
614 void CL_FlameCube (vec3_t mins, vec3_t maxs, int count)
615 {
616         float t;
617         if (!cl_particles.integer) return;
618         if (maxs[0] <= mins[0]) {t = mins[0];mins[0] = maxs[0];maxs[0] = t;}
619         if (maxs[1] <= mins[1]) {t = mins[1];mins[1] = maxs[1];maxs[1] = t;}
620         if (maxs[2] <= mins[2]) {t = mins[2];mins[2] = maxs[2];maxs[2] = t;}
621
622         while (count--)
623                 particle(pt_flame, PARTICLE_BILLBOARD, particlepalette[224 + (rand()&15)], tex_particle, false, true, 8, 8, 255, 9999, 1.1, lhrandom(mins[0], maxs[0]), lhrandom(mins[1], maxs[1]), lhrandom(mins[2], maxs[2]), lhrandom(-32, 32), lhrandom(-32, 32), lhrandom(-32, 64), 0, 0, 0, 0, 1, 0);
624 }
625
626 void CL_Flames (vec3_t org, vec3_t vel, int count)
627 {
628         if (!cl_particles.integer) return;
629
630         while (count--)
631                 particle(pt_flame, PARTICLE_BILLBOARD, particlepalette[224 + (rand()&15)], tex_particle, false, true, 8, 8, 255, 9999, 1.1, org[0], org[1], org[2], vel[0] + lhrandom(-128, 128), vel[1] + lhrandom(-128, 128), vel[2] + lhrandom(-128, 128), 0, 0, 0, 0, 1, 0);
632 }
633
634
635
636 /*
637 ===============
638 CL_LavaSplash
639
640 ===============
641 */
642 void CL_LavaSplash (vec3_t origin)
643 {
644         int                     i, j;
645         float           vel;
646         vec3_t          dir, org;
647         if (!cl_particles.integer) return;
648
649         for (i=-128 ; i<128 ; i+=16)
650         {
651                 for (j=-128 ; j<128 ; j+=16)
652                 {
653                         dir[0] = j + lhrandom(0, 8);
654                         dir[1] = i + lhrandom(0, 8);
655                         dir[2] = 256;
656                         org[0] = origin[0] + dir[0];
657                         org[1] = origin[1] + dir[1];
658                         org[2] = origin[2] + lhrandom(0, 64);
659                         vel = lhrandom(50, 120) / VectorLength(dir); // normalize and scale
660                         particle(pt_lavasplash, PARTICLE_BILLBOARD, particlepalette[224 + (rand()&7)], tex_particle, false, true, 7, 7, 255, 9999, 0, org[0], org[1], org[2], dir[0] * vel, dir[1] * vel, dir[2] * vel, 0, 0, 0, 0, 0, 0);
661                 }
662         }
663 }
664
665 /*
666 ===============
667 CL_TeleportSplash
668
669 ===============
670 */
671 void CL_TeleportSplash (vec3_t org)
672 {
673         int                     i, j, k;
674         if (!cl_particles.integer) return;
675
676         for (i=-16 ; i<16 ; i+=8)
677                 for (j=-16 ; j<16 ; j+=8)
678                         for (k=-24 ; k<32 ; k+=8)
679                                 //particle(pt_fade, PARTICLE_BILLBOARD, 0xFFFFFF, tex_particle, false, true, 1.5, 1.5, lhrandom(64, 128), 9999, 0, org[0] + i + lhrandom(0, 8), org[1] + j + lhrandom(0, 8), org[2] + k + lhrandom(0, 8), i*2 + lhrandom(-12.5, 12.5), j*2 + lhrandom(-12.5, 12.5), k*2 + lhrandom(27.5, 52.5), 384.0f, 0, 0, 0, 1, 0);
680                                 particle(pt_fade, PARTICLE_BILLBOARD, 0xFFFFFF, tex_particle, false, true, 10, 10, lhrandom(64, 128), 9999, 0, org[0] + i + lhrandom(0, 8), org[1] + j + lhrandom(0, 8), org[2] + k + lhrandom(0, 8), lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-256, 256), 256.0f, 0, 0, 0, 1, 0);
681 }
682
683 void CL_RocketTrail (vec3_t start, vec3_t end, int type, entity_t *ent)
684 {
685         vec3_t          vec, dir, vel, pos;
686         float           len, dec, speed;
687         int                     contents, bubbles/*, c*/;
688         if (!cl_particles.integer) return;
689
690         VectorSubtract(end, start, dir);
691         VectorNormalize(dir);
692
693         //if (type == 0 && host_frametime != 0) // rocket glow
694         //      particle(pt_oneframe, PARTICLE_BILLBOARD, 0xFFFFFF, tex_rocketglow, false, true, 24, 24, 255, 9999, 0, end[0] - 12 * dir[0], end[1] - 12 * dir[1], end[2] - 12 * dir[2], 0, 0, 0, 0, 0, 0, 0, 0, 0);
695
696         VectorSubtract (end, start, vec);
697         len = VectorNormalizeLength (vec);
698         dec = -ent->persistent.trail_time;
699         ent->persistent.trail_time += len;
700         if (ent->persistent.trail_time < 0.01f)
701                 return;
702
703         speed = 1.0f / (ent->state_current.time - ent->state_previous.time);
704         VectorSubtract(ent->state_current.origin, ent->state_previous.origin, vel);
705         VectorScale(vel, speed, vel);
706
707         // advance into this frame to reach the first puff location
708         VectorMA(start, dec, vec, pos);
709         len -= dec;
710
711         // if we skip out, leave it reset
712         ent->persistent.trail_time = 0.0f;
713
714         contents = Mod_PointInLeaf(pos, cl.worldmodel)->contents;
715         if (contents == CONTENTS_SKY || contents == CONTENTS_LAVA)
716                 return;
717
718         bubbles = (contents == CONTENTS_WATER || contents == CONTENTS_SLIME);
719
720         while (len >= 0)
721         {
722                 switch (type)
723                 {
724                         case 0: // rocket trail
725                                 if (!cl_particles_smoke.integer)
726                                         return;
727                                 //dec = 5;
728                                 //particle(pt_fade, PARTICLE_BILLBOARD, 0x707070, tex_particle, true, false, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(-8, 8), 256.0f, 0, 0, 0, 0, 0);
729                                 dec = 6;
730                                 particle(pt_fade, PARTICLE_BILLBOARD, 0x404040, tex_smoke[rand()&7], true, true, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], lhrandom(-5, 5), lhrandom(-5, 5), lhrandom(-5, 5), 256.0f, 0, 0, 0, 0, 0);
731                                 //particle(pt_fade, PARTICLE_BILLBOARD, 0x707070, tex_smoke[rand()&7], false, true, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], lhrandom(-10, 10), lhrandom(-10, 10), lhrandom(-10, 10), 128.0f, 0, 0, 0, 0, 0);
732                                 //dec = 10;
733                                 //particle(pt_smoke, PARTICLE_BILLBOARD, 0x707070, tex_smoke[rand()&7], false, true, 2, 2, 160, 9999, 0, pos[0], pos[1], pos[2], 0, 0, 0, 0, 0, 0, 0, 0, 0);
734                                 if (bubbles && cl_particles_bubbles.integer)
735                                 {
736                                         particle(pt_bubble, PARTICLE_BILLBOARD, 0xFFFFFF, tex_bubble, false, true, 2, 2, 255, 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-16, 16), lhrandom(-16, 16), lhrandom(-16, 16), 0, 0, 0, 0, 0, 0);
737                                         //particle(pt_bubble, PARTICLE_BILLBOARD, 0xFFFFFF, tex_bubble, false, true, 2, 2, 255, 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-16, 16), lhrandom(-16, 16), lhrandom(-16, 16), 0, 0, 0, 0, 0, 0);
738                                 }
739                                 else
740                                 {
741                                         //particle(pt_spark, PARTICLE_BILLBOARD, particlepalette[0x68 + (rand() & 7)], tex_particle, false, true, 2, 2, lhrandom(128, 255), 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64), 512.0f, 0, 0, 0, 1, 0);
742                                         //particle(pt_spark, PARTICLE_BILLBOARD, particlepalette[0x68 + (rand() & 7)], tex_particle, false, true, 2, 2, lhrandom(128, 255), 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64), 512.0f, 0, 0, 0, 1, 0);
743                                         //particle(pt_spark, PARTICLE_BILLBOARD, particlepalette[0x68 + (rand() & 7)], tex_particle, false, true, 1, 1, lhrandom(128, 255), 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-64, 64) - vel[0] * 0.0625, lhrandom(-64, 64) - vel[1] * 0.0625, lhrandom(-64, 64) - vel[2] * 0.0625, 512.0f, 0, 0, 0, 1, 0);
744                                         //particle(pt_spark, PARTICLE_BILLBOARD, particlepalette[0x68 + (rand() & 7)], tex_particle, false, true, 1, 1, lhrandom(128, 255), 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-64, 64) - vel[0] * 0.0625, lhrandom(-64, 64) - vel[1] * 0.0625, lhrandom(-64, 64) - vel[2] * 0.0625, 512.0f, 0, 0, 0, 1, 0);
745                                         //particle(pt_spark, PARTICLE_BILLBOARD, particlepalette[0x68 + (rand() & 7)], tex_particle, false, true, 1, 1, lhrandom(128, 255), 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-64, 64) - vel[0] * 0.0625, lhrandom(-64, 64) - vel[1] * 0.0625, lhrandom(-64, 64) - vel[2] * 0.0625, 512.0f, 0, 0, 0, 1, 0);
746                                         //particle(pt_spark, PARTICLE_BILLBOARD, particlepalette[0x68 + (rand() & 7)], tex_particle, false, true, 1, 1, lhrandom(128, 255), 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-64, 64) - vel[0] * 0.0625, lhrandom(-64, 64) - vel[1] * 0.0625, lhrandom(-64, 64) - vel[2] * 0.0625, 512.0f, 0, 0, 0, 1, 0);
747                                 }
748                                 break;
749
750                         case 1: // grenade trail
751                                 // FIXME: make it gradually stop smoking
752                                 if (!cl_particles_smoke.integer)
753                                         return;
754                                 //dec = 5;
755                                 //particle(pt_fade, PARTICLE_BILLBOARD, 0x707070, tex_particle, true, false, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(-8, 8), 256.0f, 0, 0, 0, 0, 0);
756                                 dec = 6;
757                                 particle(pt_fade, PARTICLE_BILLBOARD, 0x404040, tex_smoke[rand()&7], true, true, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], lhrandom(-5, 5), lhrandom(-5, 5), lhrandom(-5, 5), 256.0f, 0, 0, 0, 0, 0);
758                                 //particle(pt_smoke, PARTICLE_BILLBOARD, 0x404040, tex_smoke[rand()&7], false, true, 2, 2, 160, 9999, 0, pos[0], pos[1], pos[2], 0, 0, 0, 0, 0, 0, 0, 0, 0);
759                                 if (bubbles && cl_particles_bubbles.integer)
760                                 {
761                                         particle(pt_bubble, PARTICLE_BILLBOARD, 0xFFFFFF, tex_bubble, false, true, 2, 2, 255, 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-16, 16), lhrandom(-16, 16), lhrandom(-16, 16), 0, 0, 0, 0, 0, 0);
762                                         //particle(pt_bubble, PARTICLE_BILLBOARD, 0xFFFFFF, tex_bubble, false, true, 2, 2, 255, 9999, 1.5, pos[0], pos[1], pos[2], lhrandom(-16, 16), lhrandom(-16, 16), lhrandom(-16, 16), 0, 0, 0, 0, 0, 0);
763                                 }
764                                 break;
765
766
767                         case 2: // blood
768                                 if (!cl_particles_blood.integer)
769                                         return;
770                                 dec = lhrandom(cl_particles_blood_size_min.value, cl_particles_blood_size_max.value);
771                                 //particle(pt_blood, PARTICLE_BILLBOARD, 0x300000, tex_smoke[rand()&7], true, false, dec, dec, cl_particles_blood_alpha.value * 255.0f, 9999, -1, pos[0], pos[1], pos[2], vel[0] + lhrandom(-64, 64), vel[1] + lhrandom(-64, 64), vel[2] + lhrandom(-64, 64), 0, 0, 0, 0, 1, 0);
772                                 particle(pt_blood, PARTICLE_BILLBOARD, 0x300000, tex_smoke[rand()&7], true, false, dec, dec, cl_particles_blood_alpha.value * 255.0f, 9999, -1, pos[0], pos[1], pos[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64), 0, 0, 0, 0, 1, 0);
773                                 //c = ((rand() & 15) + 16) << 16;
774                                 //particle(pt_blood, PARTICLE_BILLBOARD, c, tex_particle, true, false, dec, dec, cl_particles_blood_alpha.value * 255.0f, 9999, -1, pos[0], pos[1], pos[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64), 0, 0, 0, 0, 1, 0);
775                                 break;
776
777                         case 4: // slight blood
778                                 if (!cl_particles_blood.integer)
779                                         return;
780                                 dec = lhrandom(cl_particles_blood_size_min.value, cl_particles_blood_size_max.value);
781                                 //particle(pt_blood, PARTICLE_BILLBOARD, 0x300000, tex_smoke[rand()&7], true, false, dec, dec, cl_particles_blood_alpha.value * 128.0f, 9999, -1, pos[0], pos[1], pos[2], vel[0] + lhrandom(-64, 64), vel[1] + lhrandom(-64, 64), vel[2] + lhrandom(-64, 64), 0, 0, 0, 0, 1, 0);
782                                 particle(pt_blood, PARTICLE_BILLBOARD, 0x300000, tex_smoke[rand()&7], true, false, dec, dec, cl_particles_blood_alpha.value * 128.0f, 9999, -1, pos[0], pos[1], pos[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64), 0, 0, 0, 0, 1, 0);
783                                 //c = ((rand() & 15) + 16) << 16;
784                                 //particle(pt_blood, PARTICLE_BILLBOARD, c, tex_particle, true, false, dec, dec, cl_particles_blood_alpha.value * 128.0f, 9999, -1, pos[0], pos[1], pos[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64), 0, 0, 0, 0, 1, 0);
785                                 break;
786
787                         case 3: // green tracer
788                                 dec = 6;
789                                 //particle(pt_fade, PARTICLE_BILLBOARD, 0x373707, tex_particle, false, true, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], 0, 0, 0, 384.0f, 0, 0, 0, 0, 0);
790                                 particle(pt_fade, PARTICLE_BILLBOARD, 0x373707, tex_particle, false, true, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(-8, 8), 384.0f, 0, 0, 0, 0, 0);
791                                 break;
792
793                         case 5: // flame tracer
794                                 dec = 6;
795                                 //particle(pt_fade, PARTICLE_BILLBOARD, 0xCF632B, tex_particle, false, true, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], 0, 0, 0, 384.0f, 0, 0, 0, 0, 0);
796                                 particle(pt_fade, PARTICLE_BILLBOARD, 0xCF632B, tex_particle, false, true, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(-8, 8), 384.0f, 0, 0, 0, 0, 0);
797                                 break;
798
799                         case 6: // voor trail
800                                 dec = 6;
801                                 //particle(pt_fade, PARTICLE_BILLBOARD, 0x47232B, tex_particle, false, true, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], 0, 0, 0, 384.0f, 0, 0, 0, 0, 0);
802                                 particle(pt_fade, PARTICLE_BILLBOARD, 0x47232B, tex_particle, false, true, dec, dec, 128, 9999, 0, pos[0], pos[1], pos[2], lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(-8, 8), 384.0f, 0, 0, 0, 0, 0);
803                                 break;
804
805                         case 7: // Nehahra smoke tracer
806                                 if (!cl_particles_smoke.integer)
807                                         return;
808                                 dec = 10;
809                                 particle(pt_smoke, PARTICLE_BILLBOARD, 0xC0C0C0, tex_smoke[rand()&7], true, false, dec, dec, 64, 9999, 0, pos[0], pos[1], pos[2], 0, 0, 0, 0, 0, 0, 0, 0, 0);
810                                 break;
811                 }
812
813                 // advance to next time and position
814                 len -= dec;
815                 VectorMA (pos, dec, vec, pos);
816         }
817         ent->persistent.trail_time = len;
818 }
819
820 void CL_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent)
821 {
822         vec3_t          vec, pos;
823         int                     len;
824         if (!cl_particles.integer) return;
825         if (!cl_particles_smoke.integer) return;
826
827         VectorCopy(start, pos);
828         VectorSubtract (end, start, vec);
829         len = (int) (VectorNormalizeLength (vec) * (1.0f / 3.0f));
830         VectorScale(vec, 3, vec);
831         color = particlepalette[color];
832         while (len--)
833         {
834                 particle(pt_smoke, PARTICLE_BILLBOARD, color, tex_particle, false, false, 5, 5, 128, 9999, 0, pos[0], pos[1], pos[2], 0, 0, 0, 0, 0, 0, 0, 0, 0);
835                 VectorAdd (pos, vec, pos);
836         }
837 }
838
839
840 /*
841 ===============
842 CL_MoveParticles
843 ===============
844 */
845 void CL_MoveParticles (void)
846 {
847         particle_t *p;
848         renderparticle_t *r, *rend;
849         int i, activeparticles, maxparticle, j, a, pressureused = false, content;
850         float gravity, dvel, frametime, f, dist, normal[3], v[3], org[3];
851
852         // LordHavoc: early out condition
853         if (!cl_numparticles)
854         {
855                 r_refdef.numparticles = 0;
856                 return;
857         }
858
859         frametime = cl.time - cl.oldtime;
860         if (!frametime)
861                 return; // if absolutely still, don't update particles
862         gravity = frametime * sv_gravity.value;
863         dvel = 1+4*frametime;
864
865         activeparticles = 0;
866         maxparticle = -1;
867         j = 0;
868         for (i = 0, p = particles, r = r_refdef.particles, rend = r + cl_maxparticles;i < cl_numparticles;i++, p++)
869         {
870                 if (p->die < cl.time)
871                 {
872                         freeparticles[j++] = p;
873                         continue;
874                 }
875
876                 content = 0;
877                 VectorCopy(p->org, p->oldorg);
878                 VectorMA(p->org, frametime, p->vel, p->org);
879                 VectorCopy(p->org, org);
880                 if (p->bounce)
881                 {
882                         if (TraceLine(p->oldorg, p->org, v, normal, 0, true) < 1)
883                         {
884                                 VectorCopy(v, p->org);
885                                 if (p->bounce < 0)
886                                 {
887                                         // assume it's blood (lame, but...)
888                                         R_Stain(v, 48, 64, 24, 24, p->alpha * p->scalex * p->scaley * (1.0f / 2048.0f), 192, 48, 48, p->alpha * p->scalex * p->scaley * (1.0f / 2048.0f));
889                                         p->die = -1;
890                                         freeparticles[j++] = p;
891                                         continue;
892                                 }
893                                 else
894                                 {
895                                         dist = DotProduct(p->vel, normal) * -p->bounce;
896                                         VectorMA(p->vel, dist, normal, p->vel);
897                                         if (DotProduct(p->vel, p->vel) < 0.03)
898                                                 VectorClear(p->vel);
899                                 }
900                         }
901                 }
902                 if (p->friction)
903                 {
904                         f = p->friction * frametime;
905                         if (!content)
906                                 content = Mod_PointInLeaf(p->org, cl.worldmodel)->contents;
907                         if (content != CONTENTS_EMPTY)
908                                 f *= 4;
909                         f = 1.0f - f;
910                         VectorScale(p->vel, f, p->vel);
911                 }
912
913                 switch (p->type)
914                 {
915                 case pt_static:
916                         break;
917
918                         // LordHavoc: drop-through because of shared code
919                 case pt_blob:
920                         p->vel[2] *= dvel;
921                 case pt_blob2:
922                         p->vel[0] *= dvel;
923                         p->vel[1] *= dvel;
924                         p->alpha -= frametime * 256;
925                         if (p->alpha < 1)
926                                 p->die = -1;
927                         break;
928
929                 case pt_grav:
930                         p->vel[2] -= gravity;
931                         break;
932                 case pt_lavasplash:
933                         p->vel[2] -= gravity * 0.05;
934                         p->alpha -= frametime * 192;
935                         if (p->alpha < 1)
936                                 p->die = -1;
937                         break;
938                 case pt_snow:
939                         if (cl.time > p->time2)
940                         {
941                                 p->time2 = cl.time + (rand() & 3) * 0.1;
942                                 p->vel[0] = (rand()&63)-32 + p->vel2[0];
943                                 p->vel[1] = (rand()&63)-32 + p->vel2[1];
944                                 p->vel[2] = (rand()&63)-32 + p->vel2[2];
945                         }
946                         if (!content)
947                                 content = Mod_PointInLeaf(p->org, cl.worldmodel)->contents;
948                         a = content;
949                         if (a != CONTENTS_EMPTY && a != CONTENTS_SKY)
950                         {
951                                 p->die = -1;
952                                 /*
953                                 if (a == CONTENTS_SOLID && Mod_PointInLeaf(p->oldorg, cl.worldmodel)->contents == CONTENTS_SOLID)
954                                         break; // still in solid
955                                 p->die = cl.time + 1000;
956                                 p->vel[0] = p->vel[1] = p->vel[2] = 0;
957                                 switch (a)
958                                 {
959                                 case CONTENTS_LAVA:
960                                 case CONTENTS_SLIME:
961                                         p->tex = tex_smoke[rand()&7];
962                                         p->orientation = PARTICLE_BILLBOARD;
963                                         p->type = pt_steam;
964                                         p->alpha = 96;
965                                         p->scalex = 5;
966                                         p->scaley = 5;
967                                         p->vel[2] = 96;
968                                         break;
969                                 case CONTENTS_WATER:
970                                         p->tex = tex_smoke[rand()&7];
971                                         p->orientation = PARTICLE_BILLBOARD;
972                                         p->type = pt_splash;
973                                         p->alpha = 96;
974                                         p->scalex = 5;
975                                         p->scaley = 5;
976                                         p->vel[2] = 96;
977                                         break;
978                                 default: // CONTENTS_SOLID and any others
979                                         TraceLine(p->oldorg, p->org, v, normal, 0, true);
980                                         VectorCopy(v, p->org);
981                                         p->tex = tex_smoke[rand()&7];
982                                         p->orientation = PARTICLE_BILLBOARD;
983                                         p->type = pt_fade;
984                                         p->time2 = 384.0f;
985                                         p->scalex = 5;
986                                         p->scaley = 5;
987                                         VectorClear(p->vel);
988                                         break;
989                                 }
990                                 */
991                         }
992                         break;
993                 case pt_blood:
994                         p->friction = 1;
995                         if (!content)
996                                 content = Mod_PointInLeaf(p->org, cl.worldmodel)->contents;
997                         a = content;
998                         if (a != CONTENTS_EMPTY)
999                         {
1000                                 if (a == CONTENTS_WATER || a == CONTENTS_SLIME)
1001                                 {
1002                                         //p->friction = 5;
1003                                         p->scalex += frametime * (cl_particles_blood_size_min.value + cl_particles_blood_size_max.value);
1004                                         p->scaley += frametime * (cl_particles_blood_size_min.value + cl_particles_blood_size_max.value);
1005                                         p->alpha -= frametime * max(cl_particles_blood_alpha.value, 0.01f) * 128.0f;
1006                                         //p->vel[2] += gravity * 0.25f;
1007                                         if (p->alpha < 1)
1008                                                 p->die = -1;
1009                                 }
1010                                 else
1011                                         p->die = -1;
1012                         }
1013                         else
1014                                 p->vel[2] -= gravity;
1015                         break;
1016                 case pt_spark:
1017                         p->alpha -= frametime * p->time2;
1018                         p->vel[2] -= gravity;
1019                         if (p->alpha < 1)
1020                                 p->die = -1;
1021                         else
1022                         {
1023                                 if (!content)
1024                                         content = Mod_PointInLeaf(p->org, cl.worldmodel)->contents;
1025                                 if (content != CONTENTS_EMPTY)
1026                                         p->die = -1;
1027                         }
1028                         break;
1029                 case pt_explosionsplash:
1030                         if (Mod_PointInLeaf(p->org, cl.worldmodel)->contents == CONTENTS_EMPTY)
1031                                 p->vel[2] -= gravity;
1032                         else
1033                                 p->alpha = 0;
1034                         p->scalex += frametime * 64.0f;
1035                         p->scaley += frametime * 64.0f;
1036                         p->alpha -= frametime * 1024.0f;
1037                         if (p->alpha < 1)
1038                                 p->die = -1;
1039                         break;
1040                 case pt_fade:
1041                         p->alpha -= frametime * p->time2;
1042                         if (p->alpha < 1)
1043                                 p->die = -1;
1044                         break;
1045                 case pt_bubble:
1046                         if (!content)
1047                                 content = Mod_PointInLeaf(p->org, cl.worldmodel)->contents;
1048                         if (content != CONTENTS_WATER && content != CONTENTS_SLIME)
1049                         {
1050                                 p->tex = tex_smoke[rand()&7];
1051                                 p->orientation = PARTICLE_BILLBOARD;
1052                                 p->type = pt_splashpuff;
1053                                 p->scalex = 4;
1054                                 p->scaley = 4;
1055                                 p->vel[0] = p->vel[1] = p->vel[2] = 0;
1056                                 break;
1057                         }
1058                         p->vel[0] *= (1 - (frametime * 0.0625));
1059                         p->vel[1] *= (1 - (frametime * 0.0625));
1060                         p->vel[2] = (p->vel[2] + gravity * 0.25) * (1 - (frametime * 0.0625));
1061                         if (cl.time > p->time2)
1062                         {
1063                                 p->time2 = cl.time + lhrandom(0, 0.5);
1064                                 p->vel[0] += lhrandom(-32,32);
1065                                 p->vel[1] += lhrandom(-32,32);
1066                                 p->vel[2] += lhrandom(-32,32);
1067                         }
1068                         p->alpha -= frametime * 256;
1069                         if (p->alpha < 1)
1070                                 p->die = -1;
1071                         break;
1072                 case pt_bulletsmoke:
1073                         p->scalex += frametime * 16;
1074                         p->scaley += frametime * 16;
1075                         p->alpha -= frametime * 1024;
1076                         p->vel[2] += gravity * 0.2;
1077                         if (p->alpha < 1)
1078                                 p->die = -1;
1079                         break;
1080                 case pt_smoke:
1081                         p->scalex += frametime * 16;
1082                         p->scaley += frametime * 16;
1083                         p->alpha -= frametime * 320;
1084                         //p->vel[2] += gravity * 0.2;
1085                         if (p->alpha < 1)
1086                                 p->die = -1;
1087                         break;
1088                 case pt_steam:
1089                         p->scalex += frametime * 48;
1090                         p->scaley += frametime * 48;
1091                         p->alpha -= frametime * 512;
1092                         p->vel[2] += gravity * 0.05;
1093                         if (p->alpha < 1)
1094                                 p->die = -1;
1095                         break;
1096                 case pt_splashpuff:
1097                         p->alpha -= frametime * 1024;
1098                         if (p->alpha < 1)
1099                                 p->die = -1;
1100                         break;
1101                 case pt_rain:
1102                         if (!content)
1103                                 content = Mod_PointInLeaf(p->org, cl.worldmodel)->contents;
1104                         a = content;
1105                         if (a != CONTENTS_EMPTY && a != CONTENTS_SKY)
1106                                 p->die = -1;
1107                         /*
1108                         f = 0;
1109                         b = Mod_PointInLeaf(p->oldorg, cl.worldmodel)->contents;
1110                         VectorCopy(p->oldorg, o);
1111                         while (f < 1)
1112                         {
1113                                 a = b;
1114                                 f = TraceLine(o, p->org, v, normal, a, true);
1115                                 b = traceline_endcontents;
1116                                 if (f < 1 && b != CONTENTS_EMPTY && b != CONTENTS_SKY)
1117                                 {
1118                                         #if 1
1119                                         p->die = -1;
1120                                         #else
1121                                         p->die = cl.time + 1000;
1122                                         p->vel[0] = p->vel[1] = p->vel[2] = 0;
1123                                         VectorCopy(v, p->org);
1124                                         switch (b)
1125                                         {
1126                                         case CONTENTS_LAVA:
1127                                         case CONTENTS_SLIME:
1128                                                 p->tex = tex_smoke[rand()&7];
1129                                                 p->orientation = PARTICLE_BILLBOARD;
1130                                                 p->type = pt_steam;
1131                                                 p->scalex = 3;
1132                                                 p->scaley = 3;
1133                                                 p->vel[2] = 96;
1134                                                 break;
1135                                         default: // water, solid, and anything else
1136                                                 p->tex = tex_rainsplash[0];
1137                                                 p->orientation = PARTICLE_ORIENTED_DOUBLESIDED;
1138                                                 p->time2 = 0;
1139                                                 VectorCopy(normal, p->vel2);
1140                                         //      VectorAdd(p->org, normal, p->org);
1141                                                 p->type = pt_raindropsplash;
1142                                                 p->scalex = 8;
1143                                                 p->scaley = 8;
1144                                                 break;
1145                                         }
1146                                         #endif
1147                                         break;
1148                                 }
1149                         }
1150                         */
1151                         break;
1152                         /*
1153                 case pt_raindropsplash:
1154                         p->time2 += frametime * 64.0f;
1155                         if (p->time2 >= 16.0f)
1156                         {
1157                                 p->die = -1;
1158                                 break;
1159                         }
1160                         p->tex = tex_rainsplash[(int) p->time2];
1161                         p->orientation = PARTICLE_ORIENTED_DOUBLESIDED;
1162                         break;
1163                         */
1164                 case pt_flame:
1165                         p->alpha -= frametime * 512;
1166                         p->vel[2] += gravity;
1167                         if (p->alpha < 16)
1168                                 p->die = -1;
1169                         break;
1170                 case pt_oneframe:
1171                         if (p->time2)
1172                                 p->die = -1;
1173                         p->time2 = 1;
1174                         break;
1175                 default:
1176                         printf("unknown particle type %i\n", p->type);
1177                         p->die = -1;
1178                         break;
1179                 }
1180
1181                 // LordHavoc: immediate removal of unnecessary particles (must be done to ensure compactor below operates properly in all cases)
1182                 if (p->die < cl.time)
1183                         freeparticles[j++] = p;
1184                 else
1185                 {
1186                         maxparticle = i;
1187                         activeparticles++;
1188                         if (p->pressure)
1189                                 pressureused = true;
1190
1191                         // build renderparticle for renderer to use
1192                         r->orientation = p->orientation;
1193                         r->additive = p->additive;
1194                         r->dir[0] = p->vel2[0];
1195                         r->dir[1] = p->vel2[1];
1196                         r->dir[2] = p->vel2[2];
1197                         r->org[0] = p->org[0];
1198                         r->org[1] = p->org[1];
1199                         r->org[2] = p->org[2];
1200                         r->tex = p->tex;
1201                         r->scalex = p->scalex * cl_particles_size.value;
1202                         r->scaley = p->scaley * cl_particles_size.value;
1203                         r->dynlight = p->dynlight;
1204                         r->color[0] = p->color[0] * (1.0f / 255.0f);
1205                         r->color[1] = p->color[1] * (1.0f / 255.0f);
1206                         r->color[2] = p->color[2] * (1.0f / 255.0f);
1207                         r->color[3] = p->alpha * (1.0f / 255.0f);
1208                         r++;
1209                 }
1210         }
1211         r_refdef.numparticles = r - r_refdef.particles;
1212         // fill in gaps to compact the array
1213         i = 0;
1214         while (maxparticle >= activeparticles)
1215         {
1216                 *freeparticles[i++] = particles[maxparticle--];
1217                 while (maxparticle >= activeparticles && particles[maxparticle].die < cl.time)
1218                         maxparticle--;
1219         }
1220         cl_numparticles = activeparticles;
1221
1222         if (pressureused)
1223         {
1224                 activeparticles = 0;
1225                 for (i = 0, p = particles;i < cl_numparticles;i++, p++)
1226                         if (p->pressure)
1227                                 freeparticles[activeparticles++] = p;
1228
1229                 if (activeparticles)
1230                 {
1231                         for (i = 0, p = particles;i < cl_numparticles;i++, p++)
1232                         {
1233                                 for (j = 0;j < activeparticles;j++)
1234                                 {
1235                                         if (freeparticles[j] != p)
1236                                         {
1237                                                 float dist, diff[3];
1238                                                 VectorSubtract(p->org, freeparticles[j]->org, diff);
1239                                                 dist = DotProduct(diff, diff);
1240                                                 if (dist < 4096 && dist >= 1)
1241                                                 {
1242                                                         dist = freeparticles[j]->scalex * 4.0f * frametime / sqrt(dist);
1243                                                         VectorMA(p->vel, dist, diff, p->vel);
1244                                                         //dist = freeparticles[j]->scalex * 4.0f * frametime / dist;
1245                                                         //VectorMA(p->vel, dist, freeparticles[j]->vel, p->vel);
1246                                                 }
1247                                         }
1248                                 }
1249                         }
1250                 }
1251         }
1252 }