]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_particles.c
Merged r_particles.c back into cl_particles.c to reduce overhead (renderparticle...
[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                         /*
1192                         // build renderparticle for renderer to use
1193                         r->orientation = p->orientation;
1194                         r->additive = p->additive;
1195                         r->dir[0] = p->vel2[0];
1196                         r->dir[1] = p->vel2[1];
1197                         r->dir[2] = p->vel2[2];
1198                         r->org[0] = p->org[0];
1199                         r->org[1] = p->org[1];
1200                         r->org[2] = p->org[2];
1201                         r->tex = p->tex;
1202                         r->scalex = p->scalex * cl_particles_size.value;
1203                         r->scaley = p->scaley * cl_particles_size.value;
1204                         r->dynlight = p->dynlight;
1205                         r->color[0] = p->color[0] * (1.0f / 255.0f);
1206                         r->color[1] = p->color[1] * (1.0f / 255.0f);
1207                         r->color[2] = p->color[2] * (1.0f / 255.0f);
1208                         r->color[3] = p->alpha * (1.0f / 255.0f);
1209                         r++;
1210                         */
1211                 }
1212         }
1213         //r_refdef.numparticles = r - r_refdef.particles;
1214         // fill in gaps to compact the array
1215         i = 0;
1216         while (maxparticle >= activeparticles)
1217         {
1218                 *freeparticles[i++] = particles[maxparticle--];
1219                 while (maxparticle >= activeparticles && particles[maxparticle].die < cl.time)
1220                         maxparticle--;
1221         }
1222         cl_numparticles = activeparticles;
1223
1224         if (pressureused)
1225         {
1226                 activeparticles = 0;
1227                 for (i = 0, p = particles;i < cl_numparticles;i++, p++)
1228                         if (p->pressure)
1229                                 freeparticles[activeparticles++] = p;
1230
1231                 if (activeparticles)
1232                 {
1233                         for (i = 0, p = particles;i < cl_numparticles;i++, p++)
1234                         {
1235                                 for (j = 0;j < activeparticles;j++)
1236                                 {
1237                                         if (freeparticles[j] != p)
1238                                         {
1239                                                 float dist, diff[3];
1240                                                 VectorSubtract(p->org, freeparticles[j]->org, diff);
1241                                                 dist = DotProduct(diff, diff);
1242                                                 if (dist < 4096 && dist >= 1)
1243                                                 {
1244                                                         dist = freeparticles[j]->scalex * 4.0f * frametime / sqrt(dist);
1245                                                         VectorMA(p->vel, dist, diff, p->vel);
1246                                                         //dist = freeparticles[j]->scalex * 4.0f * frametime / dist;
1247                                                         //VectorMA(p->vel, dist, freeparticles[j]->vel, p->vel);
1248                                                 }
1249                                         }
1250                                 }
1251                         }
1252                 }
1253         }
1254 }
1255
1256 static rtexturepool_t *particletexturepool;
1257
1258 static rtexture_t *particlefonttexture;
1259 // [0] is normal, [1] is fog, they may be the same
1260 static particletexture_t particletexture[MAX_PARTICLETEXTURES][2];
1261
1262 static cvar_t r_drawparticles = {0, "r_drawparticles", "1"};
1263 static cvar_t r_particles_lighting = {0, "r_particles_lighting", "1"};
1264
1265 static qbyte shadebubble(float dx, float dy, vec3_t light)
1266 {
1267         float   dz, f, dot;
1268         vec3_t  normal;
1269         dz = 1 - (dx*dx+dy*dy);
1270         if (dz > 0) // it does hit the sphere
1271         {
1272                 f = 0;
1273                 // back side
1274                 normal[0] = dx;normal[1] = dy;normal[2] = dz;
1275                 VectorNormalize(normal);
1276                 dot = DotProduct(normal, light);
1277                 if (dot > 0.5) // interior reflection
1278                         f += ((dot *  2) - 1);
1279                 else if (dot < -0.5) // exterior reflection
1280                         f += ((dot * -2) - 1);
1281                 // front side
1282                 normal[0] = dx;normal[1] = dy;normal[2] = -dz;
1283                 VectorNormalize(normal);
1284                 dot = DotProduct(normal, light);
1285                 if (dot > 0.5) // interior reflection
1286                         f += ((dot *  2) - 1);
1287                 else if (dot < -0.5) // exterior reflection
1288                         f += ((dot * -2) - 1);
1289                 f *= 128;
1290                 f += 16; // just to give it a haze so you can see the outline
1291                 f = bound(0, f, 255);
1292                 return (qbyte) f;
1293         }
1294         else
1295                 return 0;
1296 }
1297
1298 static void setuptex(int cltexnum, int fog, int rtexnum, qbyte *data, qbyte *particletexturedata)
1299 {
1300         int basex, basey, y;
1301         basex = ((rtexnum >> 0) & 7) * 32;
1302         basey = ((rtexnum >> 3) & 7) * 32;
1303         particletexture[cltexnum][fog].s1 = (basex + 1) / 256.0f;
1304         particletexture[cltexnum][fog].t1 = (basey + 1) / 256.0f;
1305         particletexture[cltexnum][fog].s2 = (basex + 31) / 256.0f;
1306         particletexture[cltexnum][fog].t2 = (basey + 31) / 256.0f;
1307         for (y = 0;y < 32;y++)
1308                 memcpy(particletexturedata + ((basey + y) * 256 + basex) * 4, data + y * 32 * 4, 32 * 4);
1309 }
1310
1311 static void R_InitParticleTexture (void)
1312 {
1313         int             x,y,d,i,m;
1314         float   dx, dy, radius, f, f2;
1315         qbyte   data[32][32][4], noise1[64][64], noise2[64][64];
1316         vec3_t  light;
1317         qbyte   particletexturedata[256*256*4];
1318
1319         memset(particletexturedata, 255, sizeof(particletexturedata));
1320
1321         // the particletexture[][] array numbers must match the cl_part.c textures
1322         // smoke/blood
1323         for (i = 0;i < 8;i++)
1324         {
1325                 do
1326                 {
1327                         fractalnoise(&noise1[0][0], 64, 4);
1328                         fractalnoise(&noise2[0][0], 64, 8);
1329                         m = 0;
1330                         for (y = 0;y < 32;y++)
1331                         {
1332                                 dy = y - 16;
1333                                 for (x = 0;x < 32;x++)
1334                                 {
1335                                         d = (noise1[y][x] - 128) * 2 + 128;
1336                                         d = bound(0, d, 255);
1337                                         data[y][x][0] = data[y][x][1] = data[y][x][2] = d;
1338                                         dx = x - 16;
1339                                         d = (noise2[y][x] - 128) * 3 + 192;
1340                                         if (d > 0)
1341                                                 d = (d * (256 - (int) (dx*dx+dy*dy))) >> 8;
1342                                         d = bound(0, d, 255);
1343                                         data[y][x][3] = (qbyte) d;
1344                                         if (m < d)
1345                                                 m = d;
1346                                 }
1347                         }
1348                 }
1349                 while (m < 224);
1350
1351                 setuptex(i + 0, 0, i + 0, &data[0][0][0], particletexturedata);
1352                 for (y = 0;y < 32;y++)
1353                         for (x = 0;x < 32;x++)
1354                                 data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1355                 setuptex(i + 0, 1, i + 8, &data[0][0][0], particletexturedata);
1356         }
1357
1358         // rain splash
1359         for (i = 0;i < 16;i++)
1360         {
1361                 radius = i * 3.0f / 16.0f;
1362                 f2 = 255.0f * ((15.0f - i) / 15.0f);
1363                 for (y = 0;y < 32;y++)
1364                 {
1365                         dy = (y - 16) * 0.25f;
1366                         for (x = 0;x < 32;x++)
1367                         {
1368                                 dx = (x - 16) * 0.25f;
1369                                 data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1370                                 f = (1.0 - fabs(radius - sqrt(dx*dx+dy*dy))) * f2;
1371                                 f = bound(0.0f, f, 255.0f);
1372                                 data[y][x][3] = (int) f;
1373                         }
1374                 }
1375                 setuptex(i + 8, 0, i + 16, &data[0][0][0], particletexturedata);
1376                 setuptex(i + 8, 1, i + 16, &data[0][0][0], particletexturedata);
1377         }
1378
1379         // normal particle
1380         for (y = 0;y < 32;y++)
1381         {
1382                 dy = y - 16;
1383                 for (x = 0;x < 32;x++)
1384                 {
1385                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1386                         dx = x - 16;
1387                         d = (256 - (dx*dx+dy*dy));
1388                         d = bound(0, d, 255);
1389                         data[y][x][3] = (qbyte) d;
1390                 }
1391         }
1392         setuptex(24, 0, 32, &data[0][0][0], particletexturedata);
1393         setuptex(24, 1, 32, &data[0][0][0], particletexturedata);
1394
1395         // rain
1396         light[0] = 1;light[1] = 1;light[2] = 1;
1397         VectorNormalize(light);
1398         for (y = 0;y < 32;y++)
1399         {
1400                 for (x = 0;x < 32;x++)
1401                 {
1402                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1403                         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);
1404                 }
1405         }
1406         setuptex(25, 0, 33, &data[0][0][0], particletexturedata);
1407         setuptex(25, 1, 33, &data[0][0][0], particletexturedata);
1408
1409         // bubble
1410         light[0] = 1;light[1] = 1;light[2] = 1;
1411         VectorNormalize(light);
1412         for (y = 0;y < 32;y++)
1413         {
1414                 for (x = 0;x < 32;x++)
1415                 {
1416                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1417                         data[y][x][3] = shadebubble((x - 16) * (1.0 / 16.0), (y - 16) * (1.0 / 16.0), light);
1418                 }
1419         }
1420         setuptex(26, 0, 34, &data[0][0][0], particletexturedata);
1421         setuptex(26, 1, 34, &data[0][0][0], particletexturedata);
1422
1423         // rocket flare
1424         for (y = 0;y < 32;y++)
1425         {
1426                 dy = y - 16;
1427                 for (x = 0;x < 32;x++)
1428                 {
1429                         dx = x - 16;
1430                         d = (2048.0f / (dx*dx+dy*dy+1)) - 8.0f;
1431                         data[y][x][0] = bound(0, d * 1.0f, 255);
1432                         data[y][x][1] = bound(0, d * 0.8f, 255);
1433                         data[y][x][2] = bound(0, d * 0.5f, 255);
1434                         data[y][x][3] = bound(0, d * 1.0f, 255);
1435                 }
1436         }
1437         setuptex(27, 0, 35, &data[0][0][0], particletexturedata);
1438         for (y = 0;y < 32;y++)
1439                 for (x = 0;x < 32;x++)
1440                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1441         setuptex(28, 1, 36, &data[0][0][0], particletexturedata);
1442
1443         particlefonttexture = R_LoadTexture (particletexturepool, "particlefont", 256, 256, particletexturedata, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
1444 }
1445
1446 static void r_part_start(void)
1447 {
1448         particletexturepool = R_AllocTexturePool();
1449         R_InitParticleTexture ();
1450 }
1451
1452 static void r_part_shutdown(void)
1453 {
1454         R_FreeTexturePool(&particletexturepool);
1455 }
1456
1457 static void r_part_newmap(void)
1458 {
1459 }
1460
1461 void R_Particles_Init (void)
1462 {
1463         Cvar_RegisterVariable(&r_drawparticles);
1464         Cvar_RegisterVariable(&r_particles_lighting);
1465         R_RegisterModule("R_Particles", r_part_start, r_part_shutdown, r_part_newmap);
1466 }
1467
1468 int partindexarray[6] = {0, 1, 2, 0, 2, 3};
1469
1470 void R_DrawParticles (void)
1471 {
1472         //renderparticle_t *r;
1473         int i, lighting;
1474         float minparticledist, org[3], uprightangles[3], up2[3], right2[3], v[3], right[3], up[3], tvxyz[4][4], tvst[4][2], fog, ifog, fogvec[3];
1475         mleaf_t *leaf;
1476         particletexture_t *tex, *texfog;
1477         rmeshinfo_t m;
1478         particle_t *p;
1479
1480         // LordHavoc: early out conditions
1481         //if ((!r_refdef.numparticles) || (!r_drawparticles.integer))
1482         if ((!cl_numparticles) || (!r_drawparticles.integer))
1483                 return;
1484
1485         lighting = r_particles_lighting.integer;
1486         if (!r_dynamic.integer)
1487                 lighting = 0;
1488
1489         c_particles += cl_numparticles; //r_refdef.numparticles;
1490
1491         uprightangles[0] = 0;
1492         uprightangles[1] = r_refdef.viewangles[1];
1493         uprightangles[2] = 0;
1494         AngleVectors (uprightangles, NULL, right2, up2);
1495
1496         minparticledist = DotProduct(r_origin, vpn) + 16.0f;
1497
1498         // LordHavoc: this meshinfo must match up with R_Mesh_DrawDecal
1499         // LordHavoc: the commented out lines are hardwired behavior in R_Mesh_DrawDecal
1500         memset(&m, 0, sizeof(m));
1501         m.transparent = true;
1502         m.blendfunc1 = GL_SRC_ALPHA;
1503         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1504         m.numtriangles = 2;
1505         m.index = partindexarray;
1506         m.numverts = 4;
1507         m.vertex = &tvxyz[0][0];
1508         m.vertexstep = sizeof(float[4]);
1509         m.tex[0] = R_GetTexture(particlefonttexture);
1510         m.texcoords[0] = &tvst[0][0];
1511         m.texcoordstep[0] = sizeof(float[2]);
1512
1513         for (i = 0/*, r = r_refdef.particles*/, p = particles;i < /*r_refdef.numparticles*/cl_numparticles;i++, p++)
1514         {
1515                 // LordHavoc: only render if not too close
1516                 if (DotProduct(p->org, vpn) < minparticledist)
1517                         continue;
1518
1519                 // LordHavoc: check if it's in a visible leaf
1520                 leaf = Mod_PointInLeaf(p->org, cl.worldmodel);
1521                 if (leaf->visframe != r_framecount)
1522                         continue;
1523
1524                 VectorCopy(p->org, org);
1525                 if (p->orientation == PARTICLE_BILLBOARD)
1526                 {
1527                         VectorScale(vright, p->scalex, right);
1528                         VectorScale(vup, p->scaley, up);
1529                 }
1530                 else if (p->orientation == PARTICLE_UPRIGHT_FACING)
1531                 {
1532                         VectorScale(right2, p->scalex, right);
1533                         VectorScale(up2, p->scaley, up);
1534                 }
1535                 else if (p->orientation == PARTICLE_ORIENTED_DOUBLESIDED)
1536                 {
1537                         // double-sided
1538                         if (DotProduct(p->vel2, r_origin) > DotProduct(p->vel2, org))
1539                         {
1540                                 VectorNegate(p->vel2, v);
1541                                 VectorVectors(v, right, up);
1542                         }
1543                         else
1544                                 VectorVectors(p->vel2, right, up);
1545                         VectorScale(right, p->scalex, right);
1546                         VectorScale(up, p->scaley, up);
1547                 }
1548                 else
1549                         Host_Error("R_DrawParticles: unknown particle orientation %i\n", p->orientation);
1550
1551                 m.cr = p->color[0] * (1.0f / 255.0f);
1552                 m.cg = p->color[1] * (1.0f / 255.0f);
1553                 m.cb = p->color[2] * (1.0f / 255.0f);
1554                 m.ca = p->alpha * (1.0f / 255.0f);
1555                 if (lighting >= 1 && (p->dynlight || lighting >= 2))
1556                 {
1557                         R_CompleteLightPoint(v, org, true, leaf);
1558                         m.cr *= v[0];
1559                         m.cg *= v[1];
1560                         m.cb *= v[2];
1561                 }
1562
1563                 tex = &particletexture[p->tex][0];
1564
1565                 tvxyz[0][0] = org[0] - right[0] - up[0];
1566                 tvxyz[0][1] = org[1] - right[1] - up[1];
1567                 tvxyz[0][2] = org[2] - right[2] - up[2];
1568                 tvxyz[1][0] = org[0] - right[0] + up[0];
1569                 tvxyz[1][1] = org[1] - right[1] + up[1];
1570                 tvxyz[1][2] = org[2] - right[2] + up[2];
1571                 tvxyz[2][0] = org[0] + right[0] + up[0];
1572                 tvxyz[2][1] = org[1] + right[1] + up[1];
1573                 tvxyz[2][2] = org[2] + right[2] + up[2];
1574                 tvxyz[3][0] = org[0] + right[0] - up[0];
1575                 tvxyz[3][1] = org[1] + right[1] - up[1];
1576                 tvxyz[3][2] = org[2] + right[2] - up[2];
1577                 tvst[0][0] = tex->s1;
1578                 tvst[0][1] = tex->t1;
1579                 tvst[1][0] = tex->s1;
1580                 tvst[1][1] = tex->t2;
1581                 tvst[2][0] = tex->s2;
1582                 tvst[2][1] = tex->t2;
1583                 tvst[3][0] = tex->s2;
1584                 tvst[3][1] = tex->t1;
1585
1586                 if (p->additive)
1587                 {
1588                         m.blendfunc2 = GL_ONE;
1589                         fog = 0;
1590                         if (fogenabled)
1591                         {
1592                                 texfog = &particletexture[p->tex][1];
1593                                 VectorSubtract(org, r_origin, fogvec);
1594                                 ifog = 1 - exp(fogdensity/DotProduct(fogvec,fogvec));
1595                                 if (ifog < (1.0f - (1.0f / 64.0f)))
1596                                 {
1597                                         if (ifog >= (1.0f / 64.0f))
1598                                         {
1599                                                 // partially fogged, darken it
1600                                                 m.cr *= ifog;
1601                                                 m.cg *= ifog;
1602                                                 m.cb *= ifog;
1603                                                 R_Mesh_Draw(&m);
1604                                         }
1605                                 }
1606                                 else
1607                                         R_Mesh_Draw(&m);
1608                         }
1609                         else
1610                                 R_Mesh_Draw(&m);
1611                 }
1612                 else
1613                 {
1614                         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1615                         fog = 0;
1616                         if (fogenabled)
1617                         {
1618                                 texfog = &particletexture[p->tex][1];
1619                                 VectorSubtract(org, r_origin, fogvec);
1620                                 fog = exp(fogdensity/DotProduct(fogvec,fogvec));
1621                                 if (fog >= (1.0f / 64.0f))
1622                                 {
1623                                         if (fog >= (1.0f - (1.0f / 64.0f)))
1624                                         {
1625                                                 // fully fogged, just use the fog texture and render as alpha
1626                                                 m.cr = fogcolor[0];
1627                                                 m.cg = fogcolor[1];
1628                                                 m.cb = fogcolor[2];
1629                                                 tvst[0][0] = texfog->s1;
1630                                                 tvst[0][1] = texfog->t1;
1631                                                 tvst[1][0] = texfog->s1;
1632                                                 tvst[1][1] = texfog->t2;
1633                                                 tvst[2][0] = texfog->s2;
1634                                                 tvst[2][1] = texfog->t2;
1635                                                 tvst[3][0] = texfog->s2;
1636                                                 tvst[3][1] = texfog->t1;
1637                                                 R_Mesh_Draw(&m);
1638                                         }
1639                                         else
1640                                         {
1641                                                 // partially fogged, darken the first pass
1642                                                 ifog = 1 - fog;
1643                                                 m.cr *= ifog;
1644                                                 m.cg *= ifog;
1645                                                 m.cb *= ifog;
1646                                                 if (tex->s1 == texfog->s1 && tex->t1 == texfog->t1)
1647                                                 {
1648                                                         // fog texture is the same as the base, just change the color
1649                                                         m.cr += fogcolor[0] * fog;
1650                                                         m.cg += fogcolor[1] * fog;
1651                                                         m.cb += fogcolor[2] * fog;
1652                                                         R_Mesh_Draw(&m);
1653                                                 }
1654                                                 else
1655                                                 {
1656                                                         // render the first pass (alpha), then do additive fog
1657                                                         R_Mesh_Draw(&m);
1658
1659                                                         m.blendfunc2 = GL_ONE;
1660                                                         m.cr = fogcolor[0] * fog;
1661                                                         m.cg = fogcolor[1] * fog;
1662                                                         m.cb = fogcolor[2] * fog;
1663                                                         tvst[0][0] = texfog->s1;
1664                                                         tvst[0][1] = texfog->t1;
1665                                                         tvst[1][0] = texfog->s1;
1666                                                         tvst[1][1] = texfog->t2;
1667                                                         tvst[2][0] = texfog->s2;
1668                                                         tvst[2][1] = texfog->t2;
1669                                                         tvst[3][0] = texfog->s2;
1670                                                         tvst[3][1] = texfog->t1;
1671                                                         R_Mesh_Draw(&m);
1672                                                 }
1673                                         }
1674                                 }
1675                                 else
1676                                         R_Mesh_Draw(&m);
1677                         }
1678                         else
1679                                 R_Mesh_Draw(&m);
1680                 }
1681         }
1682 }