]> icculus.org git repositories - divverent/darkplaces.git/blob - r_part.c
every malloc/calloc/free converted to qmalloc/qfree with tracking (memstats command...
[divverent/darkplaces.git] / r_part.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                   4096    // 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 // LordHavoc: added dust, smoke, snow, bloodcloud, and many others
27 typedef enum {
28         pt_static, pt_grav, pt_blob, pt_blob2, pt_smoke, pt_snow, pt_rain, pt_bloodcloud, pt_fallfadespark, pt_bubble, pt_fade, pt_smokecloud, pt_splash
29 } ptype_t;
30
31 typedef struct particle_s
32 {
33         vec3_t          org;
34         float           color;
35         vec3_t          vel;
36         float           die;
37         ptype_t         type;
38         float           scale;
39         short           texnum;
40         float           alpha; // 0-255
41         float           time2; // used for various things (snow fluttering, for example)
42         vec3_t          oldorg;
43         vec3_t          vel2; // used for snow fluttering (base velocity, wind for instance)
44 } particle_t;
45
46 int             ramp1[8] = {0x6f, 0x6d, 0x6b, 0x69, 0x67, 0x65, 0x63, 0x61};
47 int             ramp2[8] = {0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x68, 0x66};
48 int             ramp3[8] = {0x6d, 0x6b, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01};
49
50 int             particletexture;
51 int             smokeparticletexture[8];
52 int             rainparticletexture;
53 int             bubbleparticletexture;
54
55 particle_t      *particles;
56 int                     r_numparticles;
57
58 vec3_t                  r_pright, r_pup, r_ppn;
59
60 int                     numparticles;
61 particle_t      **freeparticles; // list used only in compacting particles array
62
63 // LordHavoc: reduced duplicate code, and allow particle allocation system independence
64 #define ALLOCPARTICLE \
65         if (numparticles >= r_numparticles)\
66                 return;\
67         p = &particles[numparticles++];
68
69 cvar_t r_particles = {"r_particles", "1"};
70 cvar_t r_dynamicparticles = {"r_dynamicparticles", "0", TRUE};
71
72 byte shadebubble(float dx, float dy, vec3_t light)
73 {
74         float   dz, f, dot;
75         vec3_t  normal;
76         if ((dx*dx+dy*dy) < 1) // it does hit the sphere
77         {
78                 dz = 1 - (dx*dx+dy*dy);
79                 f = 0;
80                 // back side
81                 normal[0] = dx;normal[1] = dy;normal[2] = dz;
82                 VectorNormalize(normal);
83                 dot = DotProduct(normal, light);
84                 if (dot > 0.5) // interior reflection
85                         f += ((dot *  2) - 1);
86                 else if (dot < -0.5) // exterior reflection
87                         f += ((dot * -2) - 1);
88                 // front side
89                 normal[0] = dx;normal[1] = dy;normal[2] = -dz;
90                 VectorNormalize(normal);
91                 dot = DotProduct(normal, light);
92                 if (dot > 0.5) // interior reflection
93                         f += ((dot *  2) - 1);
94                 else if (dot < -0.5) // exterior reflection
95                         f += ((dot * -2) - 1);
96                 f *= 128;
97                 f += 16; // just to give it a haze so you can see the outline
98                 f = bound(0, f, 255);
99                 return (byte) f;
100         }
101         else
102                 return 0;
103 }
104
105 void R_InitParticleTexture (void)
106 {
107         int             x,y,d,i,m;
108         float   dx, dy;
109         byte    data[32][32][4], noise1[128][128], noise2[128][128];
110         vec3_t  light;
111
112         for (y = 0;y < 32;y++)
113         {
114                 dy = y - 16;
115                 for (x = 0;x < 32;x++)
116                 {
117                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
118                         dx = x - 16;
119                         d = (255 - (dx*dx+dy*dy));
120                         if (d < 0) d = 0;
121                         data[y][x][3] = (byte) d;
122                 }
123         }
124         particletexture = GL_LoadTexture ("particletexture", 32, 32, &data[0][0][0], true, true, 4);
125
126         for (i = 0;i < 8;i++)
127         {
128                 do
129                 {
130                         fractalnoise(&noise1[0][0], 128, 8);
131                         fractalnoise(&noise2[0][0], 128, 16);
132                         m = 0;
133                         for (y = 0;y < 32;y++)
134                         {
135                                 dy = y - 16;
136                                 for (x = 0;x < 32;x++)
137                                 {
138                                         int j;
139                                         j = (noise1[y][x] - 128) * 2 + 128;
140                                         if (j < 0) j = 0;
141                                         if (j > 255) j = 255;
142                                         data[y][x][0] = data[y][x][1] = data[y][x][2] = j;
143                                         dx = x - 16;
144                                         d = (noise2[y][x] - 128) * 4 + 128;
145                                         if (d > 0)
146                                         {
147                                                 d = (d * (255 - (int) (dx*dx+dy*dy))) >> 8;
148                                                 //j = (sqrt(dx*dx+dy*dy) * 2.0f - 16.0f);
149                                                 //if (j > 0)
150                                                 //      d = (d * (255 - j*j)) >> 8;
151                                                 if (d < 0) d = 0;
152                                                 if (d > 255) d = 255;
153                                                 data[y][x][3] = (byte) d;
154                                                 if (m < d)
155                                                         m = d;
156                                         }
157                                 }
158                         }
159                 }
160                 while (m < 192);
161
162                 smokeparticletexture[i] = GL_LoadTexture (va("smokeparticletexture%d", i), 32, 32, &data[0][0][0], true, true, 4);
163         }
164
165         light[0] = 1;light[1] = 1;light[2] = 1;
166         VectorNormalize(light);
167         for (x=0 ; x<32 ; x++)
168         {
169                 for (y=0 ; y<32 ; y++)
170                 {
171                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
172                         data[y][x][3] = shadebubble((x - 16) * (1.0 / 8.0), y < 24 ? (y - 24) * (1.0 / 24.0) : (y - 24) * (1.0 / 8.0), light);
173                 }
174         }
175         rainparticletexture = GL_LoadTexture ("rainparticletexture", 32, 32, &data[0][0][0], true, true, 4);
176
177         light[0] = 1;light[1] = 1;light[2] = 1;
178         VectorNormalize(light);
179         for (x=0 ; x<32 ; x++)
180         {
181                 for (y=0 ; y<32 ; y++)
182                 {
183                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
184                         data[y][x][3] = shadebubble((x - 16) * (1.0 / 16.0), (y - 16) * (1.0 / 16.0), light);
185                 }
186         }
187         bubbleparticletexture = GL_LoadTexture ("bubbleparticletexture", 32, 32, &data[0][0][0], true, true, 4);
188 }
189
190 void r_part_start()
191 {
192         particles = (particle_t *) qmalloc(r_numparticles * sizeof(particle_t));
193         freeparticles = (void *) qmalloc(r_numparticles * sizeof(particle_t *));
194         R_InitParticleTexture ();
195 }
196
197 void r_part_shutdown()
198 {
199         qfree(particles);
200         qfree(freeparticles);
201 }
202
203 /*
204 ===============
205 R_InitParticles
206 ===============
207 */
208 void R_Particles_Init (void)
209 {
210         int             i;
211
212         i = COM_CheckParm ("-particles");
213
214         if (i)
215         {
216                 r_numparticles = (int)(atoi(com_argv[i+1]));
217                 if (r_numparticles < ABSOLUTE_MIN_PARTICLES)
218                         r_numparticles = ABSOLUTE_MIN_PARTICLES;
219         }
220         else
221         {
222                 r_numparticles = MAX_PARTICLES;
223         }
224
225         Cvar_RegisterVariable (&r_particles);
226         Cvar_RegisterVariable (&r_dynamicparticles);
227
228         R_RegisterModule("R_Particles", r_part_start, r_part_shutdown);
229 }
230
231 #define particle(ptype, pcolor, ptex, pscale, palpha, ptime, px, py, pz, pvx, pvy, pvz)\
232 {\
233         particle_t      *p;\
234         ALLOCPARTICLE\
235         p->type = (ptype);\
236         p->color = (pcolor);\
237         p->texnum = (ptex);\
238         p->scale = (pscale);\
239         p->alpha = (palpha);\
240         p->die = cl.time + (ptime);\
241         p->org[0] = (px);\
242         p->org[1] = (py);\
243         p->org[2] = (pz);\
244         p->vel[0] = (pvx);\
245         p->vel[1] = (pvy);\
246         p->vel[2] = (pvz);\
247 }
248 #define particle2(ptype, pcolor, ptex, pscale, palpha, ptime, pbase, poscale, pvscale)\
249 {\
250         particle_t      *p;\
251         ALLOCPARTICLE\
252         p->type = (ptype);\
253         p->color = (pcolor);\
254         p->texnum = (ptex);\
255         p->scale = (pscale);\
256         p->alpha = (palpha);\
257         p->die = cl.time + (ptime);\
258         p->org[0] = lhrandom(-(poscale), (poscale)) + (pbase)[0];\
259         p->org[1] = lhrandom(-(poscale), (poscale)) + (pbase)[1];\
260         p->org[2] = lhrandom(-(poscale), (poscale)) + (pbase)[2];\
261         p->vel[0] = lhrandom(-(pvscale), (pvscale));\
262         p->vel[1] = lhrandom(-(pvscale), (pvscale));\
263         p->vel[2] = lhrandom(-(pvscale), (pvscale));\
264 }
265 #define particle3(ptype, pcolor, ptex, pscale, palpha, ptime, pbase, pscalex, pscaley, pscalez, pvscalex, pvscaley, pvscalez)\
266 {\
267         particle_t      *p;\
268         ALLOCPARTICLE\
269         p->type = (ptype);\
270         p->color = (pcolor);\
271         p->texnum = (ptex);\
272         p->scale = (pscale);\
273         p->alpha = (palpha);\
274         p->die = cl.time + (ptime);\
275         p->org[0] = lhrandom(-(pscalex), (pscalex)) + (pbase)[0];\
276         p->org[1] = lhrandom(-(pscaley), (pscaley)) + (pbase)[1];\
277         p->org[2] = lhrandom(-(pscalez), (pscalez)) + (pbase)[2];\
278         p->vel[0] = lhrandom(-(pvscalex), (pvscalex));\
279         p->vel[1] = lhrandom(-(pvscaley), (pvscaley));\
280         p->vel[2] = lhrandom(-(pvscalez), (pvscalez));\
281 }
282 /*
283 void particle(int type, int color, int tex, float scale, int alpha, float time, float x, float y, float z, float vx, float vy, float vz)
284 {
285         particle_t      *p;
286         ALLOCPARTICLE
287
288         p->type = type;
289         p->color = color;
290         p->texnum = tex;
291         p->scale = scale;
292         p->alpha = alpha;
293         p->die = cl.time + time;
294         p->org[0] = x;
295         p->org[1] = y;
296         p->org[2] = z;
297         p->vel[0] = vx;
298         p->vel[1] = vy;
299         p->vel[2] = vz;
300 }
301 void particle2(int type, int color, int tex, float scale, int alpha, float time, vec3_t base, float oscale, float vscale)
302 {
303         particle_t      *p;
304         ALLOCPARTICLE
305
306         p->type = type;
307         p->color = color;
308         p->texnum = tex;
309         p->scale = scale;
310         p->alpha = alpha;
311         p->die = cl.time + time;
312         p->org[0] = lhrandom(-oscale, oscale) + base[0];
313         p->org[1] = lhrandom(-oscale, oscale) + base[1];
314         p->org[2] = lhrandom(-oscale, oscale) + base[2];
315         p->vel[0] = lhrandom(-vscale, vscale);
316         p->vel[1] = lhrandom(-vscale, vscale);
317         p->vel[2] = lhrandom(-vscale, vscale);
318 }
319 void particle3(int type, int color, int tex, float scale, int alpha, float time, vec3_t base, float scalex, float scaley, float scalez, float vscalex, float vscaley, float vscalez)
320 {
321         particle_t      *p;
322         ALLOCPARTICLE
323
324         p->type = type;
325         p->color = color;
326         p->texnum = tex;
327         p->scale = scale;
328         p->alpha = alpha;
329         p->die = cl.time + time;
330         p->org[0] = lhrandom(-scalex, scalex) + base[0];
331         p->org[1] = lhrandom(-scaley, scaley) + base[1];
332         p->org[2] = lhrandom(-scalez, scalez) + base[2];
333         p->vel[0] = lhrandom(-vscalex, vscalex);
334         p->vel[1] = lhrandom(-vscaley, vscaley);
335         p->vel[2] = lhrandom(-vscalez, vscalez);
336 }
337 */
338
339 /*
340 ===============
341 R_EntityParticles
342 ===============
343 */
344
345 #define NUMVERTEXNORMALS        162
346 extern  float   r_avertexnormals[NUMVERTEXNORMALS][3];
347 vec3_t  avelocities[NUMVERTEXNORMALS];
348 float   beamlength = 16;
349 vec3_t  avelocity = {23, 7, 3};
350 float   partstep = 0.01;
351 float   timescale = 0.01;
352
353 void R_EntityParticles (entity_t *ent)
354 {
355         int                     count;
356         int                     i;
357         float           angle;
358         float           sp, sy, cp, cy;
359         vec3_t          forward;
360         float           dist;
361         if (!r_particles.value) return; // LordHavoc: particles are optional
362         
363         dist = 64;
364         count = 50;
365
366         if (!avelocities[0][0])
367                 for (i=0 ; i<NUMVERTEXNORMALS*3 ; i++)
368                         avelocities[0][i] = (rand()&255) * 0.01;
369
370         for (i=0 ; i<NUMVERTEXNORMALS ; i++)
371         {
372                 angle = cl.time * avelocities[i][0];
373                 sy = sin(angle);
374                 cy = cos(angle);
375                 angle = cl.time * avelocities[i][1];
376                 sp = sin(angle);
377                 cp = cos(angle);
378         
379                 forward[0] = cp*cy;
380                 forward[1] = cp*sy;
381                 forward[2] = -sp;
382
383                 particle(pt_static, 0x6f, particletexture, 2, 255, 0, ent->origin[0] + r_avertexnormals[i][0]*dist + forward[0]*beamlength, ent->origin[1] + r_avertexnormals[i][1]*dist + forward[1]*beamlength, ent->origin[2] + r_avertexnormals[i][2]*dist + forward[2]*beamlength, 0, 0, 0);
384         }
385 }
386
387
388 /*
389 ===============
390 R_ClearParticles
391 ===============
392 */
393 void R_ClearParticles (void)
394 {
395 //      int             i;
396 //      free_particles = &particles[0];
397 //      active_particles = NULL;
398
399 //      for (i=0 ;i<r_numparticles ; i++)
400 //              particles[i].next = &particles[i+1];
401 //      particles[r_numparticles-1].next = NULL;
402
403         numparticles = 0;
404 }
405
406
407 void R_ReadPointFile_f (void)
408 {
409         FILE    *f;
410         vec3_t  org;
411         int             r;
412         int             c;
413         char    name[MAX_OSPATH];
414         
415         sprintf (name,"maps/%s.pts", sv.name);
416
417         COM_FOpenFile (name, &f, false);
418         if (!f)
419         {
420                 Con_Printf ("couldn't open %s\n", name);
421                 return;
422         }
423         
424         Con_Printf ("Reading %s...\n", name);
425         c = 0;
426         for (;;)
427         {
428                 r = fscanf (f,"%f %f %f\n", &org[0], &org[1], &org[2]);
429                 if (r != 3)
430                         break;
431                 c++;
432                 
433                 if (numparticles >= r_numparticles)
434                 {
435                         Con_Printf ("Not enough free particles\n");
436                         break;
437                 }
438                 particle(pt_static, (-c)&15, particletexture, 2, 255, 99999, org[0], org[1], org[2], 0, 0, 0);
439         }
440
441         fclose (f);
442         Con_Printf ("%i points read\n", c);
443 }
444
445 /*
446 ===============
447 R_ParseParticleEffect
448
449 Parse an effect out of the server message
450 ===============
451 */
452 void R_ParseParticleEffect (void)
453 {
454         vec3_t          org, dir;
455         int                     i, count, msgcount, color;
456         
457         for (i=0 ; i<3 ; i++)
458                 org[i] = MSG_ReadCoord ();
459         for (i=0 ; i<3 ; i++)
460                 dir[i] = MSG_ReadChar () * (1.0/16);
461         msgcount = MSG_ReadByte ();
462         color = MSG_ReadByte ();
463
464 if (msgcount == 255)
465         count = 1024;
466 else
467         count = msgcount;
468         
469         R_RunParticleEffect (org, dir, color, count);
470 }
471         
472 /*
473 ===============
474 R_ParticleExplosion
475
476 ===============
477 */
478 void R_ParticleExplosion (vec3_t org, int smoke)
479 {
480         int                     i;
481         if (!r_particles.value) return; // LordHavoc: particles are optional
482
483         particle(pt_smokecloud, (rand()&7) + 8, smokeparticletexture[rand()&7], 30, 255, 2, org[0], org[1], org[2], 0, 0, 0);
484
485         i = Mod_PointInLeaf(org, cl.worldmodel)->contents;
486         if (i == CONTENTS_SLIME || i == CONTENTS_WATER)
487         {
488                 for (i=0 ; i<128 ; i++)
489                         particle2(pt_bubble, (rand()&3) + 12, bubbleparticletexture, lhrandom(1, 2), 255, 2, org, 16, 96);
490         }
491         else
492         {
493                 for (i = 0;i < 256;i++)
494                         particle(pt_fallfadespark, ramp3[rand()%6], particletexture, 1.5, lhrandom(128, 255), 5, lhrandom(-16, 16) + org[0], lhrandom(-16, 16) + org[1], lhrandom(-16, 16) + org[2], lhrandom(-192, 192), lhrandom(-192, 192), lhrandom(-192, 192) + 192);
495         }
496
497 }
498
499 /*
500 ===============
501 R_ParticleExplosion2
502
503 ===============
504 */
505 void R_ParticleExplosion2 (vec3_t org, int colorStart, int colorLength)
506 {
507         int                     i;
508         if (!r_particles.value) return; // LordHavoc: particles are optional
509
510         for (i = 0;i < 512;i++)
511                 particle2(pt_fade, colorStart + (i % colorLength), particletexture, 1.5, 255, 0.3, org, 8, 192);
512 }
513
514 /*
515 ===============
516 R_BlobExplosion
517
518 ===============
519 */
520 void R_BlobExplosion (vec3_t org)
521 {
522         int                     i;
523         if (!r_particles.value) return; // LordHavoc: particles are optional
524         
525         for (i=0 ; i<512 ; i++)
526                 particle3(pt_blob, 66+(rand()%6), particletexture, 2, 255, lhrandom(1, 1.4), org, 16, 16, 16, 4, 4, 128);
527         for (i=0 ; i<512 ; i++)
528                 particle3(pt_blob2, 150+(rand()%6), particletexture, 2, 255, lhrandom(1, 1.4), org, 16, 16, 16, 4, 4, 128);
529 }
530
531 /*
532 ===============
533 R_RunParticleEffect
534
535 ===============
536 */
537 void R_RunParticleEffect (vec3_t org, vec3_t dir, int color, int count)
538 {
539         if (!r_particles.value) return; // LordHavoc: particles are optional
540         
541         if (count == 1024)
542         {
543                 R_ParticleExplosion(org, false);
544                 return;
545         }
546         color &= ~7;
547         if (count & 7)
548         {
549                 particle2(pt_fade, color + (rand()&7), particletexture, 6, (count & 7) * 16 + (rand()&15), 1, org, 8, 15);
550                 count &= ~7;
551         }
552         count >>= 3;
553         while (count--)
554                 particle2(pt_fade, color + (rand()&7), particletexture, 6, 128, 1, org, 8, 15);
555 }
556
557 // LordHavoc: added this for spawning sparks/dust (which have strong gravity)
558 /*
559 ===============
560 R_SparkShower
561 ===============
562 */
563 void R_SparkShower (vec3_t org, vec3_t dir, int count)
564 {
565         if (!r_particles.value) return; // LordHavoc: particles are optional
566
567         // smoke puff
568         particle(pt_smoke, 12+(rand()&3), smokeparticletexture[rand()&7], 8, 160, 99, org[0], org[1], org[2], 0, 0, 0);
569         // sparks
570         while(count--)
571 //              particle2(pt_fallfadespark, ramp3[rand()%6], particletexture, 1, lhrandom(0, 255), 5, org, 4, 96);
572                 particle(pt_fallfadespark, ramp3[rand()%6], particletexture, 1, lhrandom(0, 255), 5, lhrandom(-4, 4) + org[0], lhrandom(-4, 4) + org[1], lhrandom(-4, 4) + org[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64) + 64);
573 }
574
575 void R_BloodPuff (vec3_t org)
576 {
577         if (!r_particles.value) return; // LordHavoc: particles are optional
578
579         particle(pt_bloodcloud, 251 /*68+(rand()&3)*/, smokeparticletexture[rand()&7], 12, 128, 99, org[0], org[1], org[2], 0, 0, 0);
580         particle(pt_bloodcloud, 251 /*68+(rand()&3)*/, smokeparticletexture[rand()&7], 10, 128, 99, org[0] + lhrandom(-4, 4), org[1] + lhrandom(-4, 4), org[2] + lhrandom(-4, 4), 0, 0, 0);
581         particle(pt_bloodcloud, 251 /*68+(rand()&3)*/, smokeparticletexture[rand()&7], 8, 128, 99, org[0] + lhrandom(-4, 4), org[1] + lhrandom(-4, 4), org[2] + lhrandom(-4, 4), 0, 0, 0);
582 }
583
584 void R_BloodShower (vec3_t mins, vec3_t maxs, float velspeed, int count)
585 {
586         int                     j;
587         particle_t      *p;
588         vec3_t          diff;
589         vec3_t          center;
590         vec3_t          velscale;
591         if (!r_particles.value) return; // LordHavoc: particles are optional
592
593         VectorSubtract(maxs, mins, diff);
594         center[0] = (mins[0] + maxs[0]) * 0.5;
595         center[1] = (mins[1] + maxs[1]) * 0.5;
596         center[2] = (mins[2] + maxs[2]) * 0.5;
597         // FIXME: change velspeed back to 2.0x after fixing mod
598         velscale[0] = velspeed * 0.5 / diff[0];
599         velscale[1] = velspeed * 0.5 / diff[1];
600         velscale[2] = velspeed * 0.5 / diff[2];
601         
602         while (count--)
603         {
604                 ALLOCPARTICLE
605
606                 p->texnum = smokeparticletexture[rand()&7];
607                 p->scale = lhrandom(4, 6);
608                 p->alpha = 96 + (rand()&63);
609                 p->die = cl.time + 2;
610                 p->type = pt_bloodcloud;
611                 p->color = 251; //(rand()&3)+68;
612                 for (j=0 ; j<3 ; j++)
613                 {
614                         p->org[j] = diff[j] * (float) (rand()%1024) * (1.0 / 1024.0) + mins[j];
615                         p->vel[j] = (p->org[j] - center[j]) * velscale[j];
616                 }
617         }
618 }
619
620 void R_ParticleCube (vec3_t mins, vec3_t maxs, vec3_t dir, int count, int colorbase, int gravity, int randomvel)
621 {
622         int                     j;
623         particle_t      *p;
624         vec3_t          diff;
625         float           t;
626         if (!r_particles.value) return; // LordHavoc: particles are optional
627         if (maxs[0] <= mins[0]) {t = mins[0];mins[0] = maxs[0];maxs[0] = t;}
628         if (maxs[1] <= mins[1]) {t = mins[1];mins[1] = maxs[1];maxs[1] = t;}
629         if (maxs[2] <= mins[2]) {t = mins[2];mins[2] = maxs[2];maxs[2] = t;}
630
631         VectorSubtract(maxs, mins, diff);
632         
633         while (count--)
634         {
635                 ALLOCPARTICLE
636
637                 p->texnum = particletexture;
638                 p->scale = 6;
639                 p->alpha = 255;
640                 p->die = cl.time + 1 + (rand()&15)*0.0625;
641                 if (gravity)
642                         p->type = pt_grav;
643                 else
644                         p->type = pt_static;
645                 p->color = colorbase + (rand()&3);
646                 for (j=0 ; j<3 ; j++)
647                 {
648                         p->org[j] = diff[j] * (float) (rand()&1023) * (1.0 / 1024.0) + mins[j];
649                         if (randomvel)
650                                 p->vel[j] = dir[j] + (rand()%randomvel)-(randomvel*0.5);
651                         else
652                                 p->vel[j] = 0;
653                 }
654         }
655 }
656
657 void R_ParticleRain (vec3_t mins, vec3_t maxs, vec3_t dir, int count, int colorbase, int type)
658 {
659         int                     i;
660         particle_t      *p;
661         vec3_t          diff;
662         vec3_t          org;
663         vec3_t          vel;
664         float           t, z;
665         if (!r_particles.value) return; // LordHavoc: particles are optional
666         if (maxs[0] <= mins[0]) {t = mins[0];mins[0] = maxs[0];maxs[0] = t;}
667         if (maxs[1] <= mins[1]) {t = mins[1];mins[1] = maxs[1];maxs[1] = t;}
668         if (maxs[2] <= mins[2]) {t = mins[2];mins[2] = maxs[2];maxs[2] = t;}
669         if (dir[2] < 0) // falling
670         {
671                 t = (maxs[2] - mins[2]) / -dir[2];
672                 z = maxs[2];
673         }
674         else // rising??
675         {
676                 t = (maxs[2] - mins[2]) / dir[2];
677                 z = mins[2];
678         }
679         if (t < 0 || t > 2) // sanity check
680                 t = 2;
681         t += cl.time;
682
683         VectorSubtract(maxs, mins, diff);
684         
685         for (i=0 ; i<count ; i++)
686         {
687                 ALLOCPARTICLE
688
689                 vel[0] = dir[0] + (rand()&31) - 16;
690                 vel[1] = dir[1] + (rand()&31) - 16;
691                 vel[2] = dir[2] + (rand()&63) - 32;
692                 org[0] = diff[0] * (float) (rand()&1023) * (1.0 / 1024.0) + mins[0];
693                 org[1] = diff[1] * (float) (rand()&1023) * (1.0 / 1024.0) + mins[1];
694                 org[2] = z;
695
696                 p->alpha = 255;
697                 p->die = t;
698                 if (type == 1)
699                 {
700                         p->scale = 2;
701                         p->texnum = particletexture;
702                         p->type = pt_snow;
703                 }
704                 else // 0
705                 {
706                         p->scale = 3;
707                         p->texnum = rainparticletexture;
708                         p->type = pt_rain;
709                 }
710                 p->color = colorbase + (rand()&3);
711                 VectorCopy(org, p->org);
712                 VectorCopy(vel, p->vel);
713                 VectorCopy(vel, p->vel2);
714         }
715 }
716
717
718 /*
719 ===============
720 R_LavaSplash
721
722 ===============
723 */
724 void R_LavaSplash (vec3_t org)
725 {
726         int                     i, j;
727         particle_t      *p;
728         float           vel;
729         vec3_t          dir;
730         if (!r_particles.value) return; // LordHavoc: particles are optional
731
732         for (i=-128 ; i<128 ; i+=16)
733                 for (j=-128 ; j<128 ; j+=16)
734                 {
735                         ALLOCPARTICLE
736                 
737                         p->texnum = particletexture;
738                         p->scale = 10;
739                         p->alpha = 128;
740                         p->die = cl.time + 2 + (rand()&31) * 0.02;
741                         p->color = 224 + (rand()&7);
742                         p->type = pt_grav;
743                         
744                         dir[0] = j + (rand()&7);
745                         dir[1] = i + (rand()&7);
746                         dir[2] = 256;
747
748                         p->org[0] = org[0] + dir[0];
749                         p->org[1] = org[1] + dir[1];
750                         p->org[2] = org[2] + (rand()&63);
751
752                         VectorNormalize (dir);                                          
753                         vel = 50 + (rand()&63);
754                         VectorScale (dir, vel, p->vel);
755                 }
756 }
757
758 /*
759 ===============
760 R_TeleportSplash
761
762 ===============
763 */
764 void R_TeleportSplash (vec3_t org)
765 {
766         int                     i, j, k;
767         particle_t      *p;
768         if (!r_particles.value) return; // LordHavoc: particles are optional
769
770         for (i=-16 ; i<16 ; i+=8)
771                 for (j=-16 ; j<16 ; j+=8)
772                         for (k=-24 ; k<32 ; k+=8)
773                         {
774                                 ALLOCPARTICLE
775                 
776                                 p->texnum = particletexture;
777                                 p->scale = 1;
778                                 p->alpha = lhrandom(32,128);
779                                 p->die = cl.time + 5;
780                                 p->color = 254;
781                                 p->type = pt_fade;
782                                 
783                                 p->org[0] = org[0] + i + (rand()&7);
784                                 p->org[1] = org[1] + j + (rand()&7);
785                                 p->org[2] = org[2] + k + (rand()&7);
786         
787                                 p->vel[0] = i*2 + (rand()%25) - 12;
788                                 p->vel[1] = j*2 + (rand()%25) - 12;
789                                 p->vel[2] = k*2 + (rand()%25) - 12 + 40;
790                         }
791 }
792
793 void R_RocketTrail (vec3_t start, vec3_t end, int type, entity_t *ent)
794 {
795         vec3_t          vec;
796         float           len, dec = 0, t, nt, speed;
797         int                     j, contents, bubbles;
798         particle_t      *p;
799         if (!r_particles.value) return; // LordHavoc: particles are optional
800
801         t = cl.oldtime;
802         nt = cl.time;
803         if (ent->trail_leftover < 0)
804                 ent->trail_leftover = 0;
805         t += ent->trail_leftover;
806         ent->trail_leftover -= (cl.time - cl.oldtime);
807         if (t >= cl.time)
808                 return;
809
810         contents = Mod_PointInLeaf(start, cl.worldmodel)->contents;
811         if (contents == CONTENTS_SKY || contents == CONTENTS_LAVA)
812                 return;
813
814         VectorSubtract (end, start, vec);
815         len = VectorNormalizeLength (vec);
816         if (len <= 0.01f)
817                 return;
818         speed = len / (nt - t);
819
820         bubbles = (contents == CONTENTS_WATER || contents == CONTENTS_SLIME);
821
822         while (t < nt)
823         {
824                 ALLOCPARTICLE
825                 
826                 p->vel[0] = p->vel[1] = p->vel[2] = 0;
827                 p->die = cl.time + 2;
828
829                 switch (type)
830                 {
831                         case 0: // rocket trail
832                         case 1: // grenade trail
833                                 if (bubbles)
834                                 {
835                                         dec = type == 0 ? 0.01f : 0.02f;
836                                         p->texnum = bubbleparticletexture;
837                                         p->scale = lhrandom(1,2);
838                                         p->alpha = 255;
839                                         p->color = 254;
840                                         p->type = pt_bubble;
841                                         p->die = cl.time + 2;
842                                         for (j=0 ; j<3 ; j++)
843                                         {
844                                                 p->vel[j] = (rand()&31)-16;
845                                                 p->org[j] = start[j] + ((rand()&3)-2);
846                                         }
847                                 }
848                                 else
849                                 {
850                                         dec = type == 0 ? 0.01f : 0.02f;
851                                         p->texnum = smokeparticletexture[rand()&7];
852                                         p->scale = lhrandom(4, 8);
853                                         p->alpha = 160; //128 + (rand()&63);
854                                         p->color = 254;
855                                         p->type = pt_smoke;
856                                         p->die = cl.time + 10000;
857                                         VectorCopy(start, p->org);
858                                         if (type == 0)
859                                         {
860                                                 particle(pt_fallfadespark, 0x68 + (rand() & 7), particletexture, 1, lhrandom(64, 128), 5, start[0], start[1], start[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64));
861                                                 particle(pt_fallfadespark, 0x68 + (rand() & 7), particletexture, 1, lhrandom(64, 128), 5, start[0], start[1], start[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64));
862                                                 particle(pt_fallfadespark, 0x68 + (rand() & 7), particletexture, 1, lhrandom(64, 128), 5, start[0], start[1], start[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64));
863                                                 particle(pt_fallfadespark, 0x68 + (rand() & 7), particletexture, 1, lhrandom(64, 128), 5, start[0], start[1], start[2], lhrandom(-64, 64), lhrandom(-64, 64), lhrandom(-64, 64));
864                                         }
865                                 }
866                                 break;
867
868                                 /*
869                         case 1: // smoke smoke
870                                 dec = 0.016f;
871                                 p->texnum = smokeparticletexture;
872                                 p->scale = lhrandom(6,9);
873                                 p->alpha = 64;
874                                 if (r_smokecolor.value)
875                                         p->color = r_smokecolor.value;
876                                 else
877                                         p->color = (rand()&3)+12;
878                                 p->type = pt_smoke;
879                                 p->die = cl.time + 1;
880                                 VectorCopy(start, p->org);
881                                 break;
882                                 */
883
884                         case 2: // blood
885                         case 4: // slight blood
886                                 dec = 0.025f;
887                                 p->texnum = smokeparticletexture[rand()&7];
888                                 p->scale = lhrandom(4, 6);
889                                 p->alpha = type == 4 ? 192 : 255;
890                                 p->color = 251; //(rand()&3)+68;
891                                 p->type = pt_bloodcloud;
892                                 p->die = cl.time + 9999;
893                                 for (j=0 ; j<3 ; j++)
894                                 {
895                                         p->vel[j] = (rand()&15)-8;
896                                         p->org[j] = start[j] + ((rand()&3)-2);
897                                 }
898                                 break;
899
900                         case 3:
901                         case 5: // tracer
902                                 dec = 0.02f;
903                                 p->texnum = smokeparticletexture[rand()&7];
904                                 p->scale = 4;
905                                 p->alpha = 64 + (rand()&31);
906                                 p->color = type == 3 ? 56 : 234;
907                                 p->type = pt_fade;
908                                 p->die = cl.time + 10000;
909                                 VectorCopy(start, p->org);
910                                 break;
911
912                         case 6: // voor trail
913                                 dec = 0.05f; // sparse trail
914                                 p->texnum = smokeparticletexture[rand()&7];
915                                 p->scale = lhrandom(3, 5);
916                                 p->alpha = 255;
917                                 p->color = 9*16 + 8 + (rand()&3);
918                                 p->type = pt_fade;
919                                 p->die = cl.time + 2;
920                                 for (j=0 ; j<3 ; j++)
921                                 {
922                                         p->vel[j] = (rand()&15)-8;
923                                         p->org[j] = start[j] + ((rand()&3)-2);
924                                 }
925                                 break;
926
927                         case 7: // Nehahra smoke tracer
928                                 dec = 0.14f;
929                                 p->texnum = smokeparticletexture[rand()&7];
930                                 p->scale = lhrandom(8, 12);
931                                 p->alpha = 64;
932                                 p->color = (rand()&3)+12;
933                                 p->type = pt_smoke;
934                                 p->die = cl.time + 10000;
935                                 for (j=0 ; j<3 ; j++)
936                                         p->org[j] = start[j] + ((rand()&3)-2);
937                                 break;
938                 }
939                 
940                 t += dec;
941                 dec *= speed;
942                 VectorMA (start, dec, vec, start);
943         }
944         ent->trail_leftover = t - cl.time;
945 }
946
947 void R_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent)
948 {
949         vec3_t          vec;
950         int                     len;
951         if (!r_particles.value) return; // LordHavoc: particles are optional
952
953         VectorSubtract (end, start, vec);
954         len = (int) (VectorNormalizeLength (vec) * (1.0f / 3.0f));
955         VectorScale(vec, 3, vec);
956         while (len--)
957         {
958                 particle(pt_smoke, color, particletexture, 8, 192, 99, start[0], start[1], start[2], 0, 0, 0);
959                 VectorAdd (start, vec, start);
960         }
961 }
962
963
964 /*
965 ===============
966 R_DrawParticles
967 ===============
968 */
969 extern  cvar_t  sv_gravity;
970 void R_CompleteLightPoint (vec3_t color, vec3_t p);
971
972 void TraceLine (vec3_t start, vec3_t end, vec3_t impact);
973
974 void R_DrawParticles (void)
975 {
976         particle_t              *p;
977         int                             i, r,g,b,a;
978         float                   gravity, dvel, frametime, scale, scale2, minparticledist;
979         byte                    *color24;
980         vec3_t                  up, right, uprightangles, forward2, up2, right2, tempcolor, v;
981         int                             activeparticles, maxparticle, j, k;
982
983         // LordHavoc: early out condition
984         if (!numparticles)
985                 return;
986
987         VectorScale (vup, 1.5, up);
988         VectorScale (vright, 1.5, right);
989
990         uprightangles[0] = 0;
991         uprightangles[1] = r_refdef.viewangles[1];
992         uprightangles[2] = 0;
993         AngleVectors (uprightangles, forward2, right2, up2);
994
995         frametime = cl.time - cl.oldtime;
996         gravity = frametime * sv_gravity.value;
997         dvel = 1+4*frametime;
998
999         minparticledist = DotProduct(r_refdef.vieworg, vpn) + 16.0f;
1000
1001         activeparticles = 0;
1002         maxparticle = -1;
1003         j = 0;
1004         for (k = 0, p = particles;k < numparticles;k++, p++)
1005         {
1006                 if (p->die < cl.time)
1007                 {
1008                         freeparticles[j++] = p;
1009                         continue;
1010                 }
1011                 maxparticle = k;
1012                 activeparticles++;
1013
1014                 // LordHavoc: only render if not too close
1015                 if (DotProduct(p->org, vpn) >= minparticledist)
1016                 {
1017                         color24 = (byte *) &d_8to24table[(int)p->color];
1018                         r = color24[0];
1019                         g = color24[1];
1020                         b = color24[2];
1021                         a = p->alpha;
1022                         if (r_dynamicparticles.value)
1023                         {
1024                                 R_CompleteLightPoint(tempcolor, p->org);
1025                                 r = (r * (int) tempcolor[0]) >> 7;
1026                                 g = (g * (int) tempcolor[1]) >> 7;
1027                                 b = (b * (int) tempcolor[2]) >> 7;
1028                         }
1029                         transpolybegin(p->texnum, 0, p->texnum, TPOLYTYPE_ALPHA);
1030                         scale = p->scale * -0.5;scale2 = p->scale * 0.5;
1031                         if (p->texnum == rainparticletexture) // rain streak
1032                         {
1033                                 transpolyvert(p->org[0] + up2[0]*scale  + right2[0]*scale , p->org[1] + up2[1]*scale  + right2[1]*scale , p->org[2] + up2[2]*scale  + right2[2]*scale , 0,1,r,g,b,a);
1034                                 transpolyvert(p->org[0] + up2[0]*scale2 + right2[0]*scale , p->org[1] + up2[1]*scale2 + right2[1]*scale , p->org[2] + up2[2]*scale2 + right2[2]*scale , 0,0,r,g,b,a);
1035                                 transpolyvert(p->org[0] + up2[0]*scale2 + right2[0]*scale2, p->org[1] + up2[1]*scale2 + right2[1]*scale2, p->org[2] + up2[2]*scale2 + right2[2]*scale2, 1,0,r,g,b,a);
1036                                 transpolyvert(p->org[0] + up2[0]*scale  + right2[0]*scale2, p->org[1] + up2[1]*scale  + right2[1]*scale2, p->org[2] + up2[2]*scale  + right2[2]*scale2, 1,1,r,g,b,a);
1037                         }
1038                         else
1039                         {
1040                                 transpolyvert(p->org[0] + up[0]*scale  + right[0]*scale , p->org[1] + up[1]*scale  + right[1]*scale , p->org[2] + up[2]*scale  + right[2]*scale , 0,1,r,g,b,a);
1041                                 transpolyvert(p->org[0] + up[0]*scale2 + right[0]*scale , p->org[1] + up[1]*scale2 + right[1]*scale , p->org[2] + up[2]*scale2 + right[2]*scale , 0,0,r,g,b,a);
1042                                 transpolyvert(p->org[0] + up[0]*scale2 + right[0]*scale2, p->org[1] + up[1]*scale2 + right[1]*scale2, p->org[2] + up[2]*scale2 + right[2]*scale2, 1,0,r,g,b,a);
1043                                 transpolyvert(p->org[0] + up[0]*scale  + right[0]*scale2, p->org[1] + up[1]*scale  + right[1]*scale2, p->org[2] + up[2]*scale  + right[2]*scale2, 1,1,r,g,b,a);
1044                         }
1045                         transpolyend();
1046                 }
1047
1048                 VectorCopy(p->org, p->oldorg);
1049                 p->org[0] += p->vel[0]*frametime;
1050                 p->org[1] += p->vel[1]*frametime;
1051                 p->org[2] += p->vel[2]*frametime;
1052                 
1053                 switch (p->type)
1054                 {
1055                 case pt_static:
1056                         break;
1057
1058                 case pt_blob:
1059                         for (i=0 ; i<3 ; i++)
1060                                 p->vel[i] *= dvel;
1061                         break;
1062
1063                 case pt_blob2:
1064                         for (i=0 ; i<2 ; i++)
1065                                 p->vel[i] *= dvel;
1066                         break;
1067
1068                 case pt_grav:
1069                         p->vel[2] -= gravity;
1070                         break;
1071                 case pt_snow:
1072                         if (cl.time > p->time2)
1073                         {
1074                                 p->time2 = cl.time + (rand() & 3) * 0.1;
1075                                 p->vel[0] = (rand()&63)-32 + p->vel2[0];
1076                                 p->vel[1] = (rand()&63)-32 + p->vel2[1];
1077                                 p->vel[2] = (rand()&63)-32 + p->vel2[2];
1078                         }
1079                         break;
1080                 case pt_bloodcloud:
1081 //                      if (Mod_PointInLeaf(p->org, cl.worldmodel)->contents != CONTENTS_EMPTY)
1082 //                      {
1083 //                              p->die = -1;
1084 //                              break;
1085 //                      }
1086                         p->scale += frametime * 16;
1087                         p->alpha -= frametime * 512;
1088                         if (p->alpha < 1 || p->scale < 1)
1089                                 p->die = -1;
1090                         break;
1091                 case pt_fallfadespark:
1092                         p->alpha -= frametime * 256;
1093                         p->vel[2] -= gravity;
1094                         if (p->alpha < 1)
1095                                 p->die = -1;
1096                         break;
1097                 case pt_fade:
1098                         p->alpha -= frametime * 512;
1099                         if (p->alpha < 1)
1100                                 p->die = -1;
1101                         break;
1102                 case pt_bubble:
1103                         a = Mod_PointInLeaf(p->org, cl.worldmodel)->contents;
1104                         if (a != CONTENTS_WATER && a != CONTENTS_SLIME)
1105                         {
1106                                 p->texnum = smokeparticletexture[rand()&7];
1107                                 p->type = pt_splash;
1108                                 p->alpha = 96;
1109                                 p->scale = 5;
1110                                 p->vel[0] = p->vel[1] = p->vel[2] = 0;
1111                                 p->die = cl.time + 1000;
1112 //                              p->die = -1;
1113                         }
1114                         p->vel[2] += gravity * 0.25;
1115                         p->vel[0] *= (1 - (frametime * 0.0625));
1116                         p->vel[1] *= (1 - (frametime * 0.0625));
1117                         p->vel[2] *= (1 - (frametime * 0.0625));
1118                         if (cl.time > p->time2)
1119                         {
1120                                 p->time2 = cl.time + lhrandom(0, 0.5);
1121                                 p->vel[0] += lhrandom(-32,32);
1122                                 p->vel[1] += lhrandom(-32,32);
1123                                 p->vel[2] += lhrandom(-32,32);
1124                         }
1125                         p->alpha -= frametime * 64;
1126                         if (p->alpha < 1)
1127                                 p->die = -1;
1128                         break;
1129 // LordHavoc: for smoke trails
1130                 case pt_smoke:
1131                         p->scale += frametime * 16;
1132                         p->alpha -= frametime * 384;
1133                         if (p->alpha < 16)
1134                                 p->die = -1;
1135                         break;
1136                 case pt_smokecloud:
1137                         p->scale += frametime * 64;
1138                         p->alpha -= frametime * 384;
1139                         if (p->alpha < 16)
1140                                 p->die = -1;
1141                         break;
1142                 case pt_splash:
1143                         p->scale += frametime * 24;
1144                         p->alpha -= frametime * 512;
1145                         if (p->alpha < 1)
1146                                 p->die = -1;
1147                         break;
1148                 case pt_rain:
1149                         a = Mod_PointInLeaf(p->org, cl.worldmodel)->contents;
1150                         if (a != CONTENTS_EMPTY && a != CONTENTS_SKY)
1151                         {
1152                                 if (a == CONTENTS_SOLID && Mod_PointInLeaf(p->oldorg, cl.worldmodel)->contents == CONTENTS_SOLID)
1153                                         break; // still in solid
1154                                 p->die = cl.time + 1000;
1155                                 switch (a)
1156                                 {
1157                                 case CONTENTS_LAVA:
1158                                 case CONTENTS_SLIME:
1159                                         p->texnum = smokeparticletexture[rand()&7];
1160                                         p->type = pt_smokecloud;
1161                                         p->alpha = 64;
1162                                         p->vel[2] = 96;
1163                                         break;
1164                                 case CONTENTS_WATER:
1165                                         p->texnum = smokeparticletexture[rand()&7];
1166                                         p->type = pt_splash;
1167                                         p->alpha = 96;
1168                                         p->scale = 5;
1169                                         p->vel[0] = p->vel[1] = p->vel[2] = 0;
1170 //                                      p->texnum = bubbleparticletexture;
1171 //                                      p->type = pt_bubble;
1172 //                                      p->vel[2] *= 0.1;
1173                                         break;
1174                                 default: // CONTENTS_SOLID and any others
1175                                         TraceLine(p->oldorg, p->org, v);
1176                                         VectorCopy(v, p->org);
1177                                         p->texnum = smokeparticletexture[rand()&7];
1178                                         p->type = pt_splash;
1179                                         p->alpha = 96;
1180                                         p->scale = 5;
1181                                         p->vel[0] = p->vel[1] = p->vel[2] = 0;
1182                                         p->die = cl.time + 1000;
1183                                         break;
1184                                 }
1185                         }
1186                         break;
1187                 }
1188         }
1189         // fill in gaps to compact the array
1190         i = 0;
1191         while (maxparticle >= activeparticles)
1192         {
1193                 *freeparticles[i++] = particles[maxparticle--];
1194                 while (maxparticle >= activeparticles && particles[maxparticle].die < cl.time)
1195                         maxparticle--;
1196         }
1197         numparticles = activeparticles;
1198 }
1199