gl backend now has functions for color array and flat color states (GL_UseColorArray...
[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 #ifdef WORKINGLQUAKE
24 #define lhrandom(MIN,MAX) ((rand() & 32767) * (((MAX)-(MIN)) * (1.0f / 32767.0f)) + (MIN))
25 #define NUMVERTEXNORMALS        162
26 siextern float r_avertexnormals[NUMVERTEXNORMALS][3];
27 #define m_bytenormals r_avertexnormals
28 #define VectorNormalizeFast VectorNormalize
29 #define Mod_PointContents(v,m) (Mod_PointInLeaf(v,m)->contents)
30 typedef unsigned char qbyte;
31 #define cl_stainmaps.integer 0
32 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
33 {
34 }
35 #define CL_EntityParticles R_EntityParticles
36 #define CL_ReadPointFile_f R_ReadPointFile_f
37 #define CL_ParseParticleEffect R_ParseParticleEffect
38 #define CL_ParticleExplosion R_ParticleExplosion
39 #define CL_ParticleExplosion2 R_ParticleExplosion2
40 #define CL_BlobExplosion R_BlobExplosion
41 #define CL_RunParticleEffect R_RunParticleEffect
42 #define CL_LavaSplash R_LavaSplash
43 #define CL_RocketTrail2 R_RocketTrail2
44 void R_CalcBeamVerts (float *vert, vec3_t org1, vec3_t org2, float width)
45 {
46         vec3_t right1, right2, diff, normal;
47
48         VectorSubtract (org2, org1, normal);
49         VectorNormalizeFast (normal);
50
51         // calculate 'right' vector for start
52         VectorSubtract (r_origin, org1, diff);
53         VectorNormalizeFast (diff);
54         CrossProduct (normal, diff, right1);
55
56         // calculate 'right' vector for end
57         VectorSubtract (r_origin, org2, diff);
58         VectorNormalizeFast (diff);
59         CrossProduct (normal, diff, right2);
60
61         vert[ 0] = org1[0] + width * right1[0];
62         vert[ 1] = org1[1] + width * right1[1];
63         vert[ 2] = org1[2] + width * right1[2];
64         vert[ 4] = org1[0] - width * right1[0];
65         vert[ 5] = org1[1] - width * right1[1];
66         vert[ 6] = org1[2] - width * right1[2];
67         vert[ 8] = org2[0] - width * right2[0];
68         vert[ 9] = org2[1] - width * right2[1];
69         vert[10] = org2[2] - width * right2[2];
70         vert[12] = org2[0] + width * right2[0];
71         vert[13] = org2[1] + width * right2[1];
72         vert[14] = org2[2] + width * right2[2];
73 }
74 void fractalnoise(qbyte *noise, int size, int startgrid)
75 {
76         int x, y, g, g2, amplitude, min, max, size1 = size - 1, sizepower, gridpower;
77         int *noisebuf;
78 #define n(x,y) noisebuf[((y)&size1)*size+((x)&size1)]
79
80         for (sizepower = 0;(1 << sizepower) < size;sizepower++);
81         if (size != (1 << sizepower))
82                 Sys_Error("fractalnoise: size must be power of 2\n");
83
84         for (gridpower = 0;(1 << gridpower) < startgrid;gridpower++);
85         if (startgrid != (1 << gridpower))
86                 Sys_Error("fractalnoise: grid must be power of 2\n");
87
88         startgrid = bound(0, startgrid, size);
89
90         amplitude = 0xFFFF; // this gets halved before use
91         noisebuf = malloc(size*size*sizeof(int));
92         memset(noisebuf, 0, size*size*sizeof(int));
93
94         for (g2 = startgrid;g2;g2 >>= 1)
95         {
96                 // brownian motion (at every smaller level there is random behavior)
97                 amplitude >>= 1;
98                 for (y = 0;y < size;y += g2)
99                         for (x = 0;x < size;x += g2)
100                                 n(x,y) += (rand()&amplitude);
101
102                 g = g2 >> 1;
103                 if (g)
104                 {
105                         // subdivide, diamond-square algorithm (really this has little to do with squares)
106                         // diamond
107                         for (y = 0;y < size;y += g2)
108                                 for (x = 0;x < size;x += g2)
109                                         n(x+g,y+g) = (n(x,y) + n(x+g2,y) + n(x,y+g2) + n(x+g2,y+g2)) >> 2;
110                         // square
111                         for (y = 0;y < size;y += g2)
112                                 for (x = 0;x < size;x += g2)
113                                 {
114                                         n(x+g,y) = (n(x,y) + n(x+g2,y) + n(x+g,y-g) + n(x+g,y+g)) >> 2;
115                                         n(x,y+g) = (n(x,y) + n(x,y+g2) + n(x-g,y+g) + n(x+g,y+g)) >> 2;
116                                 }
117                 }
118         }
119         // find range of noise values
120         min = max = 0;
121         for (y = 0;y < size;y++)
122                 for (x = 0;x < size;x++)
123                 {
124                         if (n(x,y) < min) min = n(x,y);
125                         if (n(x,y) > max) max = n(x,y);
126                 }
127         max -= min;
128         max++;
129         // normalize noise and copy to output
130         for (y = 0;y < size;y++)
131                 for (x = 0;x < size;x++)
132                         *noise++ = (qbyte) (((n(x,y) - min) * 256) / max);
133         free(noisebuf);
134 #undef n
135 }
136 void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up)
137 {
138         float d;
139
140         right[0] = forward[2];
141         right[1] = -forward[0];
142         right[2] = forward[1];
143
144         d = DotProduct(forward, right);
145         right[0] -= d * forward[0];
146         right[1] -= d * forward[1];
147         right[2] -= d * forward[2];
148         VectorNormalizeFast(right);
149         CrossProduct(right, forward, up);
150 }
151 #else
152 #include "cl_collision.h"
153 #endif
154
155 #define MAX_PARTICLES                   8192    // default max # of particles at one time
156 #define ABSOLUTE_MIN_PARTICLES  512             // no fewer than this no matter what's on the command line
157
158 typedef enum
159 {
160         pt_static, pt_rain, pt_bubble, pt_blood
161 }
162 ptype_t;
163
164 #define PARTICLE_INVALID 0
165 #define PARTICLE_BILLBOARD 1
166 #define PARTICLE_BEAM 2
167 #define PARTICLE_ORIENTED_DOUBLESIDED 3
168
169 #define P_TEXNUM_FIRSTBIT 0
170 #define P_TEXNUM_BITS 6
171 #define P_ORIENTATION_FIRSTBIT (P_TEXNUM_FIRSTBIT + P_TEXNUM_BITS)
172 #define P_ORIENTATION_BITS 2
173 #define P_FLAGS_FIRSTBIT (P_ORIENTATION_FIRSTBIT + P_ORIENTATION_BITS)
174 //#define P_DYNLIGHT (1 << (P_FLAGS_FIRSTBIT + 0))
175 #define P_ADDITIVE (1 << (P_FLAGS_FIRSTBIT + 1))
176
177 typedef struct particle_s
178 {
179         ptype_t         type;
180         unsigned int    flags; // dynamically lit, orientation, additive blending, texnum
181         vec3_t          org;
182         vec3_t          vel;
183         float           die;
184         float           scalex;
185         float           scaley;
186         float           alpha; // 0-255
187         float           alphafade; // how much alpha reduces per second
188         float           time2; // used for various things (snow fluttering, for example)
189         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)
190         float           gravity; // how much gravity affects this particle (1.0 = normal gravity, 0.0 = none)
191         vec3_t          oldorg;
192         vec3_t          vel2; // used for snow fluttering (base velocity, wind for instance)
193         float           friction; // how much air friction affects this object (objects with a low mass/size ratio tend to get more air friction)
194         float           pressure; // if non-zero, apply pressure to other particles
195         qbyte           color[4];
196 }
197 particle_t;
198
199 static int particlepalette[256] =
200 {
201         0x000000,0x0f0f0f,0x1f1f1f,0x2f2f2f,0x3f3f3f,0x4b4b4b,0x5b5b5b,0x6b6b6b,
202         0x7b7b7b,0x8b8b8b,0x9b9b9b,0xababab,0xbbbbbb,0xcbcbcb,0xdbdbdb,0xebebeb,
203         0x0f0b07,0x170f0b,0x1f170b,0x271b0f,0x2f2313,0x372b17,0x3f2f17,0x4b371b,
204         0x533b1b,0x5b431f,0x634b1f,0x6b531f,0x73571f,0x7b5f23,0x836723,0x8f6f23,
205         0x0b0b0f,0x13131b,0x1b1b27,0x272733,0x2f2f3f,0x37374b,0x3f3f57,0x474767,
206         0x4f4f73,0x5b5b7f,0x63638b,0x6b6b97,0x7373a3,0x7b7baf,0x8383bb,0x8b8bcb,
207         0x000000,0x070700,0x0b0b00,0x131300,0x1b1b00,0x232300,0x2b2b07,0x2f2f07,
208         0x373707,0x3f3f07,0x474707,0x4b4b0b,0x53530b,0x5b5b0b,0x63630b,0x6b6b0f,
209         0x070000,0x0f0000,0x170000,0x1f0000,0x270000,0x2f0000,0x370000,0x3f0000,
210         0x470000,0x4f0000,0x570000,0x5f0000,0x670000,0x6f0000,0x770000,0x7f0000,
211         0x131300,0x1b1b00,0x232300,0x2f2b00,0x372f00,0x433700,0x4b3b07,0x574307,
212         0x5f4707,0x6b4b0b,0x77530f,0x835713,0x8b5b13,0x975f1b,0xa3631f,0xaf6723,
213         0x231307,0x2f170b,0x3b1f0f,0x4b2313,0x572b17,0x632f1f,0x733723,0x7f3b2b,
214         0x8f4333,0x9f4f33,0xaf632f,0xbf772f,0xcf8f2b,0xdfab27,0xefcb1f,0xfff31b,
215         0x0b0700,0x1b1300,0x2b230f,0x372b13,0x47331b,0x533723,0x633f2b,0x6f4733,
216         0x7f533f,0x8b5f47,0x9b6b53,0xa77b5f,0xb7876b,0xc3937b,0xd3a38b,0xe3b397,
217         0xab8ba3,0x9f7f97,0x937387,0x8b677b,0x7f5b6f,0x775363,0x6b4b57,0x5f3f4b,
218         0x573743,0x4b2f37,0x43272f,0x371f23,0x2b171b,0x231313,0x170b0b,0x0f0707,
219         0xbb739f,0xaf6b8f,0xa35f83,0x975777,0x8b4f6b,0x7f4b5f,0x734353,0x6b3b4b,
220         0x5f333f,0x532b37,0x47232b,0x3b1f23,0x2f171b,0x231313,0x170b0b,0x0f0707,
221         0xdbc3bb,0xcbb3a7,0xbfa39b,0xaf978b,0xa3877b,0x977b6f,0x876f5f,0x7b6353,
222         0x6b5747,0x5f4b3b,0x533f33,0x433327,0x372b1f,0x271f17,0x1b130f,0x0f0b07,
223         0x6f837b,0x677b6f,0x5f7367,0x576b5f,0x4f6357,0x475b4f,0x3f5347,0x374b3f,
224         0x2f4337,0x2b3b2f,0x233327,0x1f2b1f,0x172317,0x0f1b13,0x0b130b,0x070b07,
225         0xfff31b,0xefdf17,0xdbcb13,0xcbb70f,0xbba70f,0xab970b,0x9b8307,0x8b7307,
226         0x7b6307,0x6b5300,0x5b4700,0x4b3700,0x3b2b00,0x2b1f00,0x1b0f00,0x0b0700,
227         0x0000ff,0x0b0bef,0x1313df,0x1b1bcf,0x2323bf,0x2b2baf,0x2f2f9f,0x2f2f8f,
228         0x2f2f7f,0x2f2f6f,0x2f2f5f,0x2b2b4f,0x23233f,0x1b1b2f,0x13131f,0x0b0b0f,
229         0x2b0000,0x3b0000,0x4b0700,0x5f0700,0x6f0f00,0x7f1707,0x931f07,0xa3270b,
230         0xb7330f,0xc34b1b,0xcf632b,0xdb7f3b,0xe3974f,0xe7ab5f,0xefbf77,0xf7d38b,
231         0xa77b3b,0xb79b37,0xc7c337,0xe7e357,0x7fbfff,0xabe7ff,0xd7ffff,0x670000,
232         0x8b0000,0xb30000,0xd70000,0xff0000,0xfff393,0xfff7c7,0xffffff,0x9f5b53
233 };
234
235 //static int explosparkramp[8] = {0x4b0700, 0x6f0f00, 0x931f07, 0xb7330f, 0xcf632b, 0xe3974f, 0xffe7b5, 0xffffff};
236
237 // these must match r_part.c's textures
238 static const int tex_smoke[8] = {0, 1, 2, 3, 4, 5, 6, 7};
239 static const int tex_rainsplash[16] = {8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
240 static const int tex_particle = 24;
241 static const int tex_rain = 25;
242 static const int tex_bubble = 26;
243
244 static int                      cl_maxparticles;
245 static int                      cl_numparticles;
246 static particle_t       *particles;
247 static particle_t       **freeparticles; // list used only in compacting particles array
248
249 cvar_t cl_particles = {CVAR_SAVE, "cl_particles", "1"};
250 cvar_t cl_particles_size = {CVAR_SAVE, "cl_particles_size", "1"};
251 cvar_t cl_particles_bloodshowers = {CVAR_SAVE, "cl_particles_bloodshowers", "1"};
252 cvar_t cl_particles_blood = {CVAR_SAVE, "cl_particles_blood", "1"};
253 cvar_t cl_particles_blood_size = {CVAR_SAVE, "cl_particles_blood_size", "8"};
254 cvar_t cl_particles_blood_alpha = {CVAR_SAVE, "cl_particles_blood_alpha", "0.5"};
255 cvar_t cl_particles_bulletimpacts = {CVAR_SAVE, "cl_particles_bulletimpacts", "1"};
256 cvar_t cl_particles_smoke = {CVAR_SAVE, "cl_particles_smoke", "1"};
257 cvar_t cl_particles_sparks = {CVAR_SAVE, "cl_particles_sparks", "1"};
258 cvar_t cl_particles_bubbles = {CVAR_SAVE, "cl_particles_bubbles", "1"};
259
260 #ifndef WORKINGLQUAKE
261 static mempool_t *cl_part_mempool;
262 #endif
263
264 void CL_Particles_Clear(void)
265 {
266         cl_numparticles = 0;
267 }
268
269 /*
270 ===============
271 CL_InitParticles
272 ===============
273 */
274 void CL_ReadPointFile_f (void);
275 void CL_Particles_Init (void)
276 {
277         int             i;
278
279         i = COM_CheckParm ("-particles");
280
281         if (i && i < com_argc - 1)
282         {
283                 cl_maxparticles = (int)(atoi(com_argv[i+1]));
284                 if (cl_maxparticles < ABSOLUTE_MIN_PARTICLES)
285                         cl_maxparticles = ABSOLUTE_MIN_PARTICLES;
286         }
287         else
288                 cl_maxparticles = MAX_PARTICLES;
289
290         Cmd_AddCommand ("pointfile", CL_ReadPointFile_f);
291
292         Cvar_RegisterVariable (&cl_particles);
293         Cvar_RegisterVariable (&cl_particles_size);
294         Cvar_RegisterVariable (&cl_particles_bloodshowers);
295         Cvar_RegisterVariable (&cl_particles_blood);
296         Cvar_RegisterVariable (&cl_particles_blood_size);
297         Cvar_RegisterVariable (&cl_particles_blood_alpha);
298         Cvar_RegisterVariable (&cl_particles_bulletimpacts);
299         Cvar_RegisterVariable (&cl_particles_smoke);
300         Cvar_RegisterVariable (&cl_particles_sparks);
301         Cvar_RegisterVariable (&cl_particles_bubbles);
302
303 #ifdef WORKINGLQUAKE
304         particles = (particle_t *) Hunk_AllocName(cl_maxparticles * sizeof(particle_t), "particles");
305         freeparticles = (void *) Hunk_AllocName(cl_maxparticles * sizeof(particle_t *), "particles");
306 #else
307         cl_part_mempool = Mem_AllocPool("CL_Part");
308         particles = (particle_t *) Mem_Alloc(cl_part_mempool, cl_maxparticles * sizeof(particle_t));
309         freeparticles = (void *) Mem_Alloc(cl_part_mempool, cl_maxparticles * sizeof(particle_t *));
310 #endif
311         cl_numparticles = 0;
312 }
313
314 #define particle(ptype, porientation, pcolor1, pcolor2, ptex, plight, padditive, pscalex, pscaley, palpha, palphafade, ptime, pgravity, pbounce, px, py, pz, pvx, pvy, pvz, ptime2, pvx2, pvy2, pvz2, pfriction, ppressure)\
315 {\
316         if (cl_numparticles >= cl_maxparticles)\
317                 return;\
318         {\
319                 particle_t      *part;\
320                 int tempcolor, tempcolor2, cr1, cg1, cb1, cr2, cg2, cb2;\
321                 unsigned int partflags;\
322                 partflags = ((porientation) << P_ORIENTATION_FIRSTBIT) | ((ptex) << P_TEXNUM_FIRSTBIT);\
323                 if (padditive)\
324                         partflags |= P_ADDITIVE;\
325                 /*if (plight)*/\
326                 /*      partflags |= P_DYNLIGHT;*/\
327                 tempcolor = (pcolor1);\
328                 tempcolor2 = (pcolor2);\
329                 cr2 = ((tempcolor2) >> 16) & 0xFF;\
330                 cg2 = ((tempcolor2) >> 8) & 0xFF;\
331                 cb2 = (tempcolor2) & 0xFF;\
332                 if (tempcolor != tempcolor2)\
333                 {\
334                         cr1 = ((tempcolor) >> 16) & 0xFF;\
335                         cg1 = ((tempcolor) >> 8) & 0xFF;\
336                         cb1 = (tempcolor) & 0xFF;\
337                         tempcolor = rand() & 0xFF;\
338                         cr2 = (((cr2 - cr1) * tempcolor) >> 8) + cr1;\
339                         cg2 = (((cg2 - cg1) * tempcolor) >> 8) + cg1;\
340                         cb2 = (((cb2 - cb1) * tempcolor) >> 8) + cb1;\
341                 }\
342                 part = &particles[cl_numparticles++];\
343                 part->type = (ptype);\
344                 part->color[0] = cr2;\
345                 part->color[1] = cg2;\
346                 part->color[2] = cb2;\
347                 part->color[3] = 0xFF;\
348                 part->flags = partflags;\
349                 part->scalex = (pscalex);\
350                 part->scaley = (pscaley);\
351                 part->alpha = (palpha);\
352                 part->alphafade = (palphafade);\
353                 part->die = cl.time + (ptime);\
354                 part->gravity = (pgravity);\
355                 part->bounce = (pbounce);\
356                 part->org[0] = (px);\
357                 part->org[1] = (py);\
358                 part->org[2] = (pz);\
359                 part->vel[0] = (pvx);\
360                 part->vel[1] = (pvy);\
361                 part->vel[2] = (pvz);\
362                 part->time2 = (ptime2);\
363                 part->vel2[0] = (pvx2);\
364                 part->vel2[1] = (pvy2);\
365                 part->vel2[2] = (pvz2);\
366                 part->friction = (pfriction);\
367                 part->pressure = (ppressure);\
368         }\
369 }
370
371 /*
372 ===============
373 CL_EntityParticles
374 ===============
375 */
376 void CL_EntityParticles (entity_t *ent)
377 {
378         int                     i;
379         float           angle;
380         float           sp, sy, cp, cy;
381         vec3_t          forward;
382         float           dist;
383         float           beamlength;
384         static vec3_t avelocities[NUMVERTEXNORMALS];
385         if (!cl_particles.integer) return;
386
387         dist = 64;
388         beamlength = 16;
389
390         if (!avelocities[0][0])
391                 for (i=0 ; i<NUMVERTEXNORMALS*3 ; i++)
392                         avelocities[0][i] = (rand()&255) * 0.01;
393
394         for (i=0 ; i<NUMVERTEXNORMALS ; i++)
395         {
396                 angle = cl.time * avelocities[i][0];
397                 sy = sin(angle);
398                 cy = cos(angle);
399                 angle = cl.time * avelocities[i][1];
400                 sp = sin(angle);
401                 cp = cos(angle);
402
403                 forward[0] = cp*cy;
404                 forward[1] = cp*sy;
405                 forward[2] = -sp;
406
407 #ifdef WORKINGLQUAKE
408                 particle(pt_static, PARTICLE_BILLBOARD, particlepalette[0x6f], particlepalette[0x6f], tex_particle, false, false, 2, 2, 255, 0, 0, 0, 0, ent->origin[0] + m_bytenormals[i][0]*dist + forward[0]*beamlength, ent->origin[1] + m_bytenormals[i][1]*dist + forward[1]*beamlength, ent->origin[2] + m_bytenormals[i][2]*dist + forward[2]*beamlength, 0, 0, 0, 0, 0, 0, 0, 0, 0);
409 #else
410                 particle(pt_static, PARTICLE_BILLBOARD, particlepalette[0x6f], particlepalette[0x6f], tex_particle, false, false, 2, 2, 255, 0, 0, 0, 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);
411 #endif
412         }
413 }
414
415
416 void CL_ReadPointFile_f (void)
417 {
418         vec3_t  org;
419         int             r, c;
420         char    *pointfile = NULL, *pointfilepos, *t, tchar;
421 #if WORKINGLQUAKE
422         char    name[MAX_OSPATH];
423         
424         sprintf (name,"maps/%s.pts", cl.worldmodel->name);
425         COM_FOpenFile (name, &f);
426         if (f)
427         {
428                 int pointfilelength;
429                 fseek(f, 0, SEEK_END);
430                 pointfilelength = ftell(f);
431                 fseek(f, 0, SEEK_SET);
432                 pointfile = malloc(pointfilelength + 1);
433                 fread(pointfile, 1, pointfilelength, f);
434                 pointfile[pointfilelength] = 0;
435                 fclose(f);
436         }
437 #else
438         pointfile = COM_LoadFile(va("maps/%s.pts", cl.worldmodel->name), true);
439 #endif
440         if (!pointfile)
441         {
442                 Con_Printf ("couldn't open %s.pts\n", cl.worldmodel->name);
443                 return;
444         }
445
446         Con_Printf ("Reading %s.pts...\n", cl.worldmodel->name);
447         c = 0;
448         pointfilepos = pointfile;
449         while (*pointfilepos)
450         {
451                 while (*pointfilepos == '\n' || *pointfilepos == '\r')
452                         pointfilepos++;
453                 if (!*pointfilepos)
454                         break;
455                 t = pointfilepos;
456                 while (*t && *t != '\n' && *t != '\r')
457                         t++;
458                 tchar = *t;
459                 *t = 0;
460                 r = sscanf (pointfilepos,"%f %f %f", &org[0], &org[1], &org[2]);
461                 *t = tchar;
462                 pointfilepos = t;
463                 if (r != 3)
464                         break;
465                 c++;
466
467                 if (cl_numparticles >= cl_maxparticles)
468                 {
469                         Con_Printf ("Not enough free particles\n");
470                         break;
471                 }
472                 particle(pt_static, PARTICLE_BILLBOARD, particlepalette[(-c)&15], particlepalette[(-c)&15], tex_particle, false, false, 2, 2, 255, 0, 99999, 0, 0, org[0], org[1], org[2], 0, 0, 0, 0, 0, 0, 0, 0, 0);
473         }
474
475 #ifdef WORKINGLQUAKE
476         free(pointfile);
477 #else
478         Mem_Free(pointfile);
479 #endif
480         Con_Printf ("%i points read\n", c);
481 }
482
483 /*
484 ===============
485 CL_ParseParticleEffect
486
487 Parse an effect out of the server message
488 ===============
489 */
490 void CL_ParseParticleEffect (void)
491 {
492         vec3_t org, dir;
493         int i, count, msgcount, color;
494
495         for (i=0 ; i<3 ; i++)
496                 org[i] = MSG_ReadCoord ();
497         for (i=0 ; i<3 ; i++)
498                 dir[i] = MSG_ReadChar () * (1.0/16);
499         msgcount = MSG_ReadByte ();
500         color = MSG_ReadByte ();
501
502         if (msgcount == 255)
503                 count = 1024;
504         else
505                 count = msgcount;
506
507         CL_RunParticleEffect (org, dir, color, count);
508 }
509
510 /*
511 ===============
512 CL_ParticleExplosion
513
514 ===============
515 */
516 void CL_ParticleExplosion (vec3_t org)
517 {
518         int i, k;
519         //vec3_t v;
520         //vec3_t v2;
521         if (cl_stainmaps.integer)
522                 R_Stain(org, 96, 80, 80, 80, 64, 176, 176, 176, 64);
523
524         i = Mod_PointContents(org, cl.worldmodel);
525         if ((i == CONTENTS_SLIME || i == CONTENTS_WATER) && cl_particles.integer && cl_particles_bubbles.integer)
526         {
527                 for (i = 0;i < 128;i++)
528                 {
529                         particle(pt_bubble, PARTICLE_BILLBOARD, 0x404040, 0x808080, tex_bubble, false, true, 2, 2, lhrandom(128, 255), 256, 9999, -0.25, 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, (1.0 / 16.0), 0);
530                 }
531         }
532         else
533         {
534                 /*
535                 // LordHavoc: smoke effect similar to UT2003, chews fillrate too badly up close
536                 // smoke puff
537                 if (cl_particles_smoke.integer)
538                 {
539                         for (i = 0;i < 64;i++)
540                         {
541 #ifdef WORKINGLQUAKE
542                                 v2[0] = lhrandom(-64, 64);
543                                 v2[1] = lhrandom(-64, 64);
544                                 v2[2] = lhrandom(-8, 24);
545 #else
546                                 for (k = 0;k < 16;k++)
547                                 {
548                                         v[0] = org[0] + lhrandom(-64, 64);
549                                         v[1] = org[1] + lhrandom(-64, 64);
550                                         v[2] = org[2] + lhrandom(-8, 24);
551                                         if (CL_TraceLine(org, v, v2, NULL, 0, true, NULL) >= 0.1)
552                                                 break;
553                                 }
554                                 VectorSubtract(v2, org, v2);
555 #endif
556                                 VectorScale(v2, 2.0f, v2);
557                                 particle(pt_static, PARTICLE_BILLBOARD, 0x101010, 0x202020, tex_smoke[rand()&7], true, true, 12, 12, 255, 512, 9999, 0, 0, org[0], org[1], org[2], v2[0], v2[1], v2[2], 0, 0, 0, 0, 0, 0);
558                         }
559                 }
560                 */
561
562                 if (cl_particles_sparks.integer)
563                 {
564                         // sparks
565                         for (i = 0;i < 256;i++)
566                         {
567                                 k = particlepalette[0x68 + (rand() & 7)];
568                                 particle(pt_static, PARTICLE_BEAM, k, k, tex_particle, false, true, 1.5f, 0.05f, lhrandom(0, 255), 512, 9999, 1, 0, org[0], org[1], org[2], lhrandom(-192, 192), lhrandom(-192, 192), lhrandom(-192, 192) + 160, 0, 0, 0, 0, 0, 0);
569                         }
570                 }
571         }
572
573         if (cl_explosions.integer)
574                 R_NewExplosion(org);
575 }
576
577 /*
578 ===============
579 CL_ParticleExplosion2
580
581 ===============
582 */
583 void CL_ParticleExplosion2 (vec3_t org, int colorStart, int colorLength)
584 {
585         int i, k;
586         if (!cl_particles.integer) return;
587
588         for (i = 0;i < 512;i++)
589         {
590                 k = particlepalette[colorStart + (i % colorLength)];
591                 particle(pt_static, PARTICLE_BILLBOARD, k, k, tex_particle, false, false, 1.5, 1.5, 255, 384, 0.3, 0, 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), 0, 0, 0, 0, 1, 0);
592         }
593 }
594
595 /*
596 ===============
597 CL_BlobExplosion
598
599 ===============
600 */
601 void CL_BlobExplosion (vec3_t org)
602 {
603         if (cl_stainmaps.integer)
604                 R_Stain(org, 96, 80, 80, 80, 64, 176, 176, 176, 64);
605
606         if (cl_explosions.integer)
607                 R_NewExplosion(org);
608 }
609
610 /*
611 ===============
612 CL_RunParticleEffect
613
614 ===============
615 */
616 void CL_RunParticleEffect (vec3_t org, vec3_t dir, int color, int count)
617 {
618         int k;
619
620         if (count == 1024)
621         {
622                 CL_ParticleExplosion(org);
623                 return;
624         }
625         if (!cl_particles.integer) return;
626         while (count--)
627         {
628                 k = particlepalette[color + (rand()&7)];
629                 particle(pt_static, PARTICLE_BILLBOARD, k, k, tex_particle, false, false, 1, 1, 255, 512, 9999, 0, 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), 0, 0, 0, 0, 0, 0);
630         }
631 }
632
633 // LordHavoc: added this for spawning sparks/dust (which have strong gravity)
634 /*
635 ===============
636 CL_SparkShower
637 ===============
638 */
639 void CL_SparkShower (vec3_t org, vec3_t dir, int count)
640 {
641         int k;
642         if (!cl_particles.integer) return;
643
644         if (cl_stainmaps.integer)
645                 R_Stain(org, 32, 96, 96, 96, 24, 128, 128, 128, 24);
646
647         if (cl_particles_bulletimpacts.integer)
648         {
649                 // smoke puff
650                 if (cl_particles_smoke.integer)
651                 {
652                         k = count / 4;
653                         while(k--)
654                         {
655                                 particle(pt_static, PARTICLE_BILLBOARD, 0x101010, 0x202020, tex_smoke[rand()&7], true, true, 4, 4, 255, 1024, 9999, -0.2, 0, org[0] + 0.125f * lhrandom(-count, count), org[1] + 0.125f * lhrandom (-count, count), org[2] + 0.125f * lhrandom(-count, count), lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(0, 16), 0, 0, 0, 0, 0, 0);
656                         }
657                 }
658
659                 if (cl_particles_sparks.integer)
660                 {
661                         // sparks
662                         while(count--)
663                         {
664                                 k = particlepalette[0x68 + (rand() & 7)];
665                                 particle(pt_static, PARTICLE_BEAM, k, k, tex_particle, false, true, 0.4f, 0.015f, lhrandom(64, 255), 512, 9999, 1, 0, org[0], org[1], org[2], lhrandom(-64, 64) + dir[0], lhrandom(-64, 64) + dir[1], lhrandom(0, 128) + dir[2], 0, 0, 0, 0, 0, 0);
666                         }
667                 }
668         }
669 }
670
671 void CL_PlasmaBurn (vec3_t org)
672 {
673         if (cl_stainmaps.integer)
674                 R_Stain(org, 48, 96, 96, 96, 32, 128, 128, 128, 32);
675 }
676
677 static float bloodcount = 0;
678 void CL_BloodPuff (vec3_t org, vec3_t vel, int count)
679 {
680         float s, r, a;
681         // bloodcount is used to accumulate counts too small to cause a blood particle
682         if (!cl_particles.integer) return;
683         if (!cl_particles_blood.integer) return;
684
685         s = count + 32.0f;
686         count *= 5.0f;
687         if (count > 1000)
688                 count = 1000;
689         bloodcount += count;
690         r = cl_particles_blood_size.value;
691         a = cl_particles_blood_alpha.value * 255;
692         while(bloodcount > 0)
693         {
694                 particle(pt_blood, PARTICLE_BILLBOARD, 0x000000, 0x200000, tex_smoke[rand()&7], true, false, r, r, a, a * 0.5, 9999, 0, -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);
695                 bloodcount -= r;
696         }
697 }
698
699 void CL_BloodShower (vec3_t mins, vec3_t maxs, float velspeed, int count)
700 {
701         float r;
702         float a;
703         vec3_t diff, center, velscale;
704         if (!cl_particles.integer) return;
705         if (!cl_particles_bloodshowers.integer) return;
706         if (!cl_particles_blood.integer) return;
707
708         VectorSubtract(maxs, mins, diff);
709         center[0] = (mins[0] + maxs[0]) * 0.5;
710         center[1] = (mins[1] + maxs[1]) * 0.5;
711         center[2] = (mins[2] + maxs[2]) * 0.5;
712         // FIXME: change velspeed back to 2.0x after fixing mod
713         velscale[0] = velspeed * 2.0 / diff[0];
714         velscale[1] = velspeed * 2.0 / diff[1];
715         velscale[2] = velspeed * 2.0 / diff[2];
716
717         bloodcount += count * 5.0f;
718         r = cl_particles_blood_size.value;
719         a = cl_particles_blood_alpha.value * 255;
720         while (bloodcount > 0)
721         {
722                 vec3_t org, vel;
723                 org[0] = lhrandom(mins[0], maxs[0]);
724                 org[1] = lhrandom(mins[1], maxs[1]);
725                 org[2] = lhrandom(mins[2], maxs[2]);
726                 vel[0] = (org[0] - center[0]) * velscale[0];
727                 vel[1] = (org[1] - center[1]) * velscale[1];
728                 vel[2] = (org[2] - center[2]) * velscale[2];
729                 bloodcount -= r;
730                 particle(pt_blood, PARTICLE_BILLBOARD, 0x000000, 0x200000, tex_smoke[rand()&7], true, false, r, r, a, a * 0.5, 9999, 0, -1, org[0], org[1], org[2], vel[0], vel[1], vel[2], 0, 0, 0, 0, 1, 0);
731         }
732 }
733
734 void CL_ParticleCube (vec3_t mins, vec3_t maxs, vec3_t dir, int count, int colorbase, int gravity, int randomvel)
735 {
736         int k;
737         float t;
738         if (!cl_particles.integer) return;
739         if (maxs[0] <= mins[0]) {t = mins[0];mins[0] = maxs[0];maxs[0] = t;}
740         if (maxs[1] <= mins[1]) {t = mins[1];mins[1] = maxs[1];maxs[1] = t;}
741         if (maxs[2] <= mins[2]) {t = mins[2];mins[2] = maxs[2];maxs[2] = t;}
742
743         while (count--)
744         {
745                 k = particlepalette[colorbase + (rand()&3)];
746                 particle(pt_static, PARTICLE_BILLBOARD, k, k, tex_particle, false, false, 2, 2, 255, 0, lhrandom(1, 2), gravity ? 1 : 0, 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);
747         }
748 }
749
750 void CL_ParticleRain (vec3_t mins, vec3_t maxs, vec3_t dir, int count, int colorbase, int type)
751 {
752         int k;
753         float t, z, minz, maxz;
754         if (!cl_particles.integer) return;
755         if (maxs[0] <= mins[0]) {t = mins[0];mins[0] = maxs[0];maxs[0] = t;}
756         if (maxs[1] <= mins[1]) {t = mins[1];mins[1] = maxs[1];maxs[1] = t;}
757         if (maxs[2] <= mins[2]) {t = mins[2];mins[2] = maxs[2];maxs[2] = t;}
758         if (dir[2] < 0) // falling
759         {
760                 t = (maxs[2] - mins[2]) / -dir[2];
761                 z = maxs[2];
762         }
763         else // rising??
764         {
765                 t = (maxs[2] - mins[2]) / dir[2];
766                 z = mins[2];
767         }
768         if (t < 0 || t > 2) // sanity check
769                 t = 2;
770
771         minz = z - fabs(dir[2]) * 0.1;
772         maxz = z + fabs(dir[2]) * 0.1;
773         minz = bound(mins[2], minz, maxs[2]);
774         maxz = bound(mins[2], maxz, maxs[2]);
775
776         switch(type)
777         {
778         case 0:
779                 count *= 4; // ick, this should be in the mod or maps?
780
781                 while(count--)
782                 {
783                         k = particlepalette[colorbase + (rand()&3)];
784                         particle(pt_rain, PARTICLE_BEAM, k, k, tex_particle, true, true, 0.5, 0.02, lhrandom(8, 16), 0, t, 0, 0, lhrandom(mins[0], maxs[0]), lhrandom(mins[1], maxs[1]), lhrandom(minz, maxz), dir[0], dir[1], dir[2], cl.time + 9999, dir[0], dir[1], dir[2], 0, 0);
785                 }
786                 break;
787         case 1:
788                 while(count--)
789                 {
790                         k = particlepalette[colorbase + (rand()&3)];
791                         particle(pt_rain, PARTICLE_BILLBOARD, k, k, tex_particle, false, true, 1, 1, lhrandom(64, 128), 0, t, 0, 0, lhrandom(mins[0], maxs[0]), lhrandom(mins[1], maxs[1]), lhrandom(minz, maxz), dir[0], dir[1], dir[2], 0, dir[0], dir[1], dir[2], 0, 0);
792                 }
793                 break;
794         default:
795                 Host_Error("CL_ParticleRain: unknown type %i (0 = rain, 1 = snow)\n", type);
796         }
797 }
798
799 void CL_Stardust (vec3_t mins, vec3_t maxs, int count)
800 {
801         int k;
802         float t;
803         vec3_t o, v, center;
804         if (!cl_particles.integer) return;
805
806         if (maxs[0] <= mins[0]) {t = mins[0];mins[0] = maxs[0];maxs[0] = t;}
807         if (maxs[1] <= mins[1]) {t = mins[1];mins[1] = maxs[1];maxs[1] = t;}
808         if (maxs[2] <= mins[2]) {t = mins[2];mins[2] = maxs[2];maxs[2] = t;}
809
810         center[0] = (mins[0] + maxs[0]) * 0.5f;
811         center[1] = (mins[1] + maxs[1]) * 0.5f;
812         center[2] = (mins[2] + maxs[2]) * 0.5f;
813
814         while (count--)
815         {
816                 k = particlepalette[224 + (rand()&15)];
817                 o[0] = lhrandom(mins[0], maxs[0]);
818                 o[1] = lhrandom(mins[1], maxs[1]);
819                 o[2] = lhrandom(mins[2], maxs[2]);
820                 VectorSubtract(o, center, v);
821                 VectorNormalizeFast(v);
822                 VectorScale(v, 100, v);
823                 v[2] += sv_gravity.value * 0.15f;
824                 particle(pt_static, PARTICLE_BILLBOARD, 0x903010, 0xFFD030, tex_particle, false, true, 1.5, 1.5, lhrandom(64, 128), 128, 9999, 1, 0, o[0], o[1], o[2], v[0], v[1], v[2], 0, 0, 0, 0, 0, 0);
825         }
826 }
827
828 void CL_FlameCube (vec3_t mins, vec3_t maxs, int count)
829 {
830         int k;
831         float t;
832         if (!cl_particles.integer) return;
833         if (maxs[0] <= mins[0]) {t = mins[0];mins[0] = maxs[0];maxs[0] = t;}
834         if (maxs[1] <= mins[1]) {t = mins[1];mins[1] = maxs[1];maxs[1] = t;}
835         if (maxs[2] <= mins[2]) {t = mins[2];mins[2] = maxs[2];maxs[2] = t;}
836
837         while (count--)
838         {
839                 k = particlepalette[224 + (rand()&15)];
840                 particle(pt_static, PARTICLE_BILLBOARD, k, k, tex_particle, false, true, 4, 4, lhrandom(64, 128), 384, 9999, -1, 0, lhrandom(mins[0], maxs[0]), lhrandom(mins[1], maxs[1]), lhrandom(mins[2], maxs[2]), lhrandom(-32, 32), lhrandom(-32, 32), lhrandom(0, 64), 0, 0, 0, 0, 1, 0);
841                 if (count & 1)
842                         particle(pt_static, PARTICLE_BILLBOARD, 0x303030, 0x606060, tex_smoke[rand()&7], false, true, 6, 6, lhrandom(48, 96), 64, 9999, 0, 0, lhrandom(mins[0], maxs[0]), lhrandom(mins[1], maxs[1]), lhrandom(mins[2], maxs[2]), lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(0, 32), 0, 0, 0, 0, 0, 0);
843         }
844 }
845
846 void CL_Flames (vec3_t org, vec3_t vel, int count)
847 {
848         int k;
849         if (!cl_particles.integer) return;
850
851         while (count--)
852         {
853                 k = particlepalette[224 + (rand()&15)];
854                 particle(pt_static, PARTICLE_BILLBOARD, k, k, tex_particle, false, true, 4, 4, lhrandom(64, 128), 384, 9999, -1, 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);
855         }
856 }
857
858
859
860 /*
861 ===============
862 CL_LavaSplash
863
864 ===============
865 */
866 void CL_LavaSplash (vec3_t origin)
867 {
868         int                     i, j, k;
869         float           vel;
870         vec3_t          dir, org;
871         if (!cl_particles.integer) return;
872
873         for (i=-128 ; i<128 ; i+=16)
874         {
875                 for (j=-128 ; j<128 ; j+=16)
876                 {
877                         dir[0] = j + lhrandom(0, 8);
878                         dir[1] = i + lhrandom(0, 8);
879                         dir[2] = 256;
880                         org[0] = origin[0] + dir[0];
881                         org[1] = origin[1] + dir[1];
882                         org[2] = origin[2] + lhrandom(0, 64);
883                         vel = lhrandom(50, 120) / VectorLength(dir); // normalize and scale
884                         k = particlepalette[224 + (rand()&7)];
885                         particle(pt_static, PARTICLE_BILLBOARD, k, k, tex_particle, false, true, 7, 7, 255, 192, 9999, 0.05, 0, org[0], org[1], org[2], dir[0] * vel, dir[1] * vel, dir[2] * vel, 0, 0, 0, 0, 0, 0);
886                 }
887         }
888 }
889
890 /*
891 ===============
892 CL_TeleportSplash
893
894 ===============
895 */
896 #if WORKINGLQUAKE
897 void R_TeleportSplash (vec3_t org)
898 {
899         int i, j, k;
900         if (!cl_particles.integer) return;
901
902         for (i=-16 ; i<16 ; i+=8)
903                 for (j=-16 ; j<16 ; j+=8)
904                         for (k=-24 ; k<32 ; k+=8)
905                                 particle(pt_static, PARTICLE_BILLBOARD, 0xA0A0A0, 0xFFFFFF, tex_particle, false, true, 10, 10, lhrandom(64, 128), 256, 9999, 0, 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), 0, 0, 0, 0, 1, 0);
906 }
907 #endif
908
909 #ifdef WORKINGLQUAKE
910 void R_RocketTrail (vec3_t start, vec3_t end, int type)
911 #else
912 void CL_RocketTrail (vec3_t start, vec3_t end, int type, entity_t *ent)
913 #endif
914 {
915         vec3_t vec, dir, vel, pos;
916         float len, dec, speed, r;
917         int contents, smoke, blood, bubbles;
918
919         VectorSubtract(end, start, dir);
920         VectorNormalize(dir);
921
922         VectorSubtract (end, start, vec);
923 #ifdef WORKINGLQUAKE
924         len = VectorNormalize (vec);
925         dec = 0;
926         speed = 1.0f / cl.frametime;
927         VectorSubtract(end, start, vel);
928 #else
929         len = VectorNormalizeLength (vec);
930         dec = -ent->persistent.trail_time;
931         ent->persistent.trail_time += len;
932         if (ent->persistent.trail_time < 0.01f)
933                 return;
934
935         // if we skip out, leave it reset
936         ent->persistent.trail_time = 0.0f;
937
938         speed = 1.0f / (ent->state_current.time - ent->state_previous.time);
939         VectorSubtract(ent->state_current.origin, ent->state_previous.origin, vel);
940 #endif
941         VectorScale(vel, speed, vel);
942
943         // advance into this frame to reach the first puff location
944         VectorMA(start, dec, vec, pos);
945         len -= dec;
946
947         contents = Mod_PointContents(pos, cl.worldmodel);
948         if (contents == CONTENTS_SKY || contents == CONTENTS_LAVA)
949                 return;
950
951         smoke = cl_particles.integer && cl_particles_smoke.integer;
952         blood = cl_particles.integer && cl_particles_blood.integer;
953         bubbles = cl_particles.integer && cl_particles_bubbles.integer && (contents == CONTENTS_WATER || contents == CONTENTS_SLIME);
954
955         while (len >= 0)
956         {
957                 switch (type)
958                 {
959                         case 0: // rocket trail
960                                 dec = 3;
961                                 if (smoke)
962                                 {
963                                         particle(pt_static, PARTICLE_BILLBOARD, 0x303030, 0x606060, tex_smoke[rand()&7], false, true, dec, dec, 32, 64, 9999, 0, 0, pos[0], pos[1], pos[2], lhrandom(-5, 5), lhrandom(-5, 5), lhrandom(-5, 5), 0, 0, 0, 0, 0, 0);
964                                         particle(pt_static, PARTICLE_BILLBOARD, 0x801010, 0xFFA020, tex_smoke[rand()&7], false, true, dec, dec, 128, 768, 9999, 0, 0, pos[0], pos[1], pos[2], lhrandom(-20, 20), lhrandom(-20, 20), lhrandom(-20, 20), 0, 0, 0, 0, 0, 0);
965                                 }
966                                 if (bubbles)
967                                 {
968                                         r = lhrandom(1, 2);
969                                         particle(pt_bubble, PARTICLE_BILLBOARD, 0x404040, 0x808080, tex_bubble, false, true, r, r, lhrandom(64, 255), 256, 9999, -0.25, 1.5, pos[0], pos[1], pos[2], lhrandom(-16, 16), lhrandom(-16, 16), lhrandom(-16, 16), 0, 0, 0, 0, (1.0 / 16.0), 0);
970                                 }
971                                 break;
972
973                         case 1: // grenade trail
974                                 // FIXME: make it gradually stop smoking
975                                 dec = 3;
976                                 if (cl_particles.integer && cl_particles_smoke.integer)
977                                 {
978                                         particle(pt_static, PARTICLE_BILLBOARD, 0x303030, 0x606060, tex_smoke[rand()&7], false, true, dec, dec, 32, 96, 9999, 0, 0, pos[0], pos[1], pos[2], lhrandom(-5, 5), lhrandom(-5, 5), lhrandom(-5, 5), 0, 0, 0, 0, 0, 0);
979                                 }
980                                 break;
981
982
983                         case 2: // blood
984                         case 4: // slight blood
985                                 dec = cl_particles_blood_size.value;
986                                 if (blood)
987                                 {
988                                         particle(pt_blood, PARTICLE_BILLBOARD, 0x100000, 0x280000, tex_smoke[rand()&7], true, false, dec, dec, cl_particles_blood_alpha.value * 255.0f, cl_particles_blood_alpha.value * 255.0f * 0.5, 9999, 0, -1, pos[0], pos[1], pos[2], vel[0] * 0.5f + lhrandom(-64, 64), vel[1] * 0.5f + lhrandom(-64, 64), vel[2] * 0.5f + lhrandom(-64, 64), 0, 0, 0, 0, 1, 0);
989                                 }
990                                 break;
991
992                         case 3: // green tracer
993                                 dec = 6;
994                                 if (smoke)
995                                 {
996                                         particle(pt_static, PARTICLE_BILLBOARD, 0x002000, 0x003000, tex_particle, false, true, dec, dec, 128, 384, 9999, 0, 0, pos[0], pos[1], pos[2], lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(-8, 8), 0, 0, 0, 0, 0, 0);
997                                 }
998                                 break;
999
1000                         case 5: // flame tracer
1001                                 dec = 6;
1002                                 if (smoke)
1003                                 {
1004                                         particle(pt_static, PARTICLE_BILLBOARD, 0x301000, 0x502000, tex_particle, false, true, dec, dec, 128, 384, 9999, 0, 0, pos[0], pos[1], pos[2], lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(-8, 8), 0, 0, 0, 0, 0, 0);
1005                                 }
1006                                 break;
1007
1008                         case 6: // voor trail
1009                                 dec = 6;
1010                                 if (smoke)
1011                                 {
1012                                         particle(pt_static, PARTICLE_BILLBOARD, 0x502030, 0x502030, tex_particle, false, true, dec, dec, 128, 384, 9999, 0, 0, pos[0], pos[1], pos[2], lhrandom(-8, 8), lhrandom(-8, 8), lhrandom(-8, 8), 0, 0, 0, 0, 0, 0);
1013                                 }
1014                                 break;
1015
1016                         case 7: // Nehahra smoke tracer
1017                                 dec = 7;
1018                                 if (smoke)
1019                                 {
1020                                         particle(pt_static, PARTICLE_BILLBOARD, 0x303030, 0x606060, tex_smoke[rand()&7], true, false, dec, dec, 64, 320, 9999, 0, 0, pos[0], pos[1], pos[2], lhrandom(-4, 4), lhrandom(-4, 4), lhrandom(0, 16), 0, 0, 0, 0, 0, 0);
1021                                 }
1022                                 break;
1023                 }
1024
1025                 // advance to next time and position
1026                 len -= dec;
1027                 VectorMA (pos, dec, vec, pos);
1028         }
1029 #ifndef WORKINGLQUAKE
1030         ent->persistent.trail_time = len;
1031 #endif
1032 }
1033
1034 void CL_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent)
1035 {
1036         vec3_t vec, pos;
1037         int len;
1038         if (!cl_particles.integer) return;
1039         if (!cl_particles_smoke.integer) return;
1040
1041         VectorCopy(start, pos);
1042         VectorSubtract (end, start, vec);
1043 #ifdef WORKINGLQUAKE
1044         len = (int) (VectorNormalize (vec) * (1.0f / 3.0f));
1045 #else
1046         len = (int) (VectorNormalizeLength (vec) * (1.0f / 3.0f));
1047 #endif
1048         VectorScale(vec, 3, vec);
1049         color = particlepalette[color];
1050         while (len--)
1051         {
1052                 particle(pt_static, PARTICLE_BILLBOARD, color, color, tex_particle, false, false, 5, 5, 128, 320, 9999, 0, 0, pos[0], pos[1], pos[2], 0, 0, 0, 0, 0, 0, 0, 0, 0);
1053                 VectorAdd (pos, vec, pos);
1054         }
1055 }
1056
1057
1058 /*
1059 ===============
1060 CL_MoveParticles
1061 ===============
1062 */
1063 void CL_MoveParticles (void)
1064 {
1065         particle_t *p;
1066         int i, activeparticles, maxparticle, j, a, pressureused = false, content;
1067         float gravity, dvel, bloodwaterfade, frametime, f, dist, normal[3], v[3], org[3];
1068
1069         // LordHavoc: early out condition
1070         if (!cl_numparticles)
1071                 return;
1072
1073 #ifdef WORKINGLQUAKE
1074         frametime = cl.frametime;
1075 #else
1076         frametime = cl.time - cl.oldtime;
1077 #endif
1078         gravity = frametime * sv_gravity.value;
1079         dvel = 1+4*frametime;
1080         bloodwaterfade = max(cl_particles_blood_alpha.value, 0.01f) * frametime * 128.0f;
1081
1082         activeparticles = 0;
1083         maxparticle = -1;
1084         j = 0;
1085         for (i = 0, p = particles;i < cl_numparticles;i++, p++)
1086         {
1087                 content = 0;
1088                 VectorCopy(p->org, p->oldorg);
1089                 VectorMA(p->org, frametime, p->vel, p->org);
1090                 VectorCopy(p->org, org);
1091 #ifndef WORKINGLQUAKE
1092                 if (p->bounce)
1093                 {
1094                         if (CL_TraceLine(p->oldorg, p->org, v, normal, 0, true, NULL) < 1)
1095                         {
1096                                 VectorCopy(v, p->org);
1097                                 if (p->bounce < 0)
1098                                 {
1099                                         // assume it's blood (lame, but...)
1100                                         if (cl_stainmaps.integer)
1101                                                 R_Stain(v, 32, 32, 16, 16, p->alpha * p->scalex * (1.0f / 40.0f), 192, 48, 48, p->alpha * p->scalex * (1.0f / 40.0f));
1102                                         p->die = -1;
1103                                         freeparticles[j++] = p;
1104                                         continue;
1105                                 }
1106                                 else
1107                                 {
1108                                         dist = DotProduct(p->vel, normal) * -p->bounce;
1109                                         VectorMA(p->vel, dist, normal, p->vel);
1110                                         if (DotProduct(p->vel, p->vel) < 0.03)
1111                                                 VectorClear(p->vel);
1112                                 }
1113                         }
1114                 }
1115 #endif
1116                 p->vel[2] -= p->gravity * gravity;
1117                 p->alpha -= p->alphafade * frametime;
1118                 if (p->friction)
1119                 {
1120                         f = p->friction * frametime;
1121                         if (!content)
1122                                 content = Mod_PointContents(p->org, cl.worldmodel);
1123                         if (content != CONTENTS_EMPTY)
1124                                 f *= 4;
1125                         f = 1.0f - f;
1126                         VectorScale(p->vel, f, p->vel);
1127                 }
1128
1129                 if (p->type != pt_static)
1130                 {
1131                         switch (p->type)
1132                         {
1133                         case pt_blood:
1134                                 if (!content)
1135                                         content = Mod_PointContents(p->org, cl.worldmodel);
1136                                 a = content;
1137                                 if (a != CONTENTS_EMPTY)
1138                                 {
1139                                         if (a == CONTENTS_WATER || a == CONTENTS_SLIME)
1140                                         {
1141                                                 p->scalex += frametime * cl_particles_blood_size.value;
1142                                                 p->scaley += frametime * cl_particles_blood_size.value;
1143                                                 //p->alpha -= bloodwaterfade;
1144                                         }
1145                                         else
1146                                                 p->die = -1;
1147                                 }
1148                                 else
1149                                         p->vel[2] -= gravity;
1150                                 break;
1151                         case pt_bubble:
1152                                 if (!content)
1153                                         content = Mod_PointContents(p->org, cl.worldmodel);
1154                                 if (content != CONTENTS_WATER && content != CONTENTS_SLIME)
1155                                 {
1156                                         p->die = -1;
1157                                         break;
1158                                 }
1159                                 break;
1160                         case pt_rain:
1161                                 if (cl.time > p->time2)
1162                                 {
1163                                         // snow flutter
1164                                         p->time2 = cl.time + (rand() & 3) * 0.1;
1165                                         p->vel[0] = lhrandom(-32, 32) + p->vel2[0];
1166                                         p->vel[1] = lhrandom(-32, 32) + p->vel2[1];
1167                                         p->vel[2] = /*lhrandom(-32, 32) +*/ p->vel2[2];
1168                                 }
1169                                 if (!content)
1170                                         content = Mod_PointContents(p->org, cl.worldmodel);
1171                                 a = content;
1172                                 if (a != CONTENTS_EMPTY && a != CONTENTS_SKY)
1173                                         p->die = -1;
1174                                 break;
1175                         default:
1176                                 printf("unknown particle type %i\n", p->type);
1177                                 p->die = -1;
1178                                 break;
1179                         }
1180                 }
1181
1182                 // remove dead particles
1183                 if (p->alpha < 1 || p->die < cl.time)
1184                         freeparticles[j++] = p;
1185                 else
1186                 {
1187                         maxparticle = i;
1188                         activeparticles++;
1189                         if (p->pressure)
1190                                 pressureused = true;
1191                 }
1192         }
1193         // fill in gaps to compact the array
1194         i = 0;
1195         while (maxparticle >= activeparticles)
1196         {
1197                 *freeparticles[i++] = particles[maxparticle--];
1198                 while (maxparticle >= activeparticles && particles[maxparticle].die < cl.time)
1199                         maxparticle--;
1200         }
1201         cl_numparticles = activeparticles;
1202
1203         if (pressureused)
1204         {
1205                 activeparticles = 0;
1206                 for (i = 0, p = particles;i < cl_numparticles;i++, p++)
1207                         if (p->pressure)
1208                                 freeparticles[activeparticles++] = p;
1209
1210                 if (activeparticles)
1211                 {
1212                         for (i = 0, p = particles;i < cl_numparticles;i++, p++)
1213                         {
1214                                 for (j = 0;j < activeparticles;j++)
1215                                 {
1216                                         if (freeparticles[j] != p)
1217                                         {
1218                                                 float dist, diff[3];
1219                                                 VectorSubtract(p->org, freeparticles[j]->org, diff);
1220                                                 dist = DotProduct(diff, diff);
1221                                                 if (dist < 4096 && dist >= 1)
1222                                                 {
1223                                                         dist = freeparticles[j]->scalex * 4.0f * frametime / sqrt(dist);
1224                                                         VectorMA(p->vel, dist, diff, p->vel);
1225                                                 }
1226                                         }
1227                                 }
1228                         }
1229                 }
1230         }
1231 }
1232
1233 #define MAX_PARTICLETEXTURES 64
1234 // particletexture_t is a rectangle in the particlefonttexture
1235 typedef struct
1236 {
1237         float s1, t1, s2, t2;
1238 }
1239 particletexture_t;
1240
1241 #if WORKINGLQUAKE
1242 static int particlefonttexture;
1243 #else
1244 static rtexturepool_t *particletexturepool;
1245 static rtexture_t *particlefonttexture;
1246 #endif
1247 static particletexture_t particletexture[MAX_PARTICLETEXTURES];
1248
1249 static cvar_t r_drawparticles = {0, "r_drawparticles", "1"};
1250
1251 static qbyte shadebubble(float dx, float dy, vec3_t light)
1252 {
1253         float dz, f, dot;
1254         vec3_t normal;
1255         dz = 1 - (dx*dx+dy*dy);
1256         if (dz > 0) // it does hit the sphere
1257         {
1258                 f = 0;
1259                 // back side
1260                 normal[0] = dx;normal[1] = dy;normal[2] = dz;
1261                 VectorNormalize(normal);
1262                 dot = DotProduct(normal, light);
1263                 if (dot > 0.5) // interior reflection
1264                         f += ((dot *  2) - 1);
1265                 else if (dot < -0.5) // exterior reflection
1266                         f += ((dot * -2) - 1);
1267                 // front side
1268                 normal[0] = dx;normal[1] = dy;normal[2] = -dz;
1269                 VectorNormalize(normal);
1270                 dot = DotProduct(normal, light);
1271                 if (dot > 0.5) // interior reflection
1272                         f += ((dot *  2) - 1);
1273                 else if (dot < -0.5) // exterior reflection
1274                         f += ((dot * -2) - 1);
1275                 f *= 128;
1276                 f += 16; // just to give it a haze so you can see the outline
1277                 f = bound(0, f, 255);
1278                 return (qbyte) f;
1279         }
1280         else
1281                 return 0;
1282 }
1283
1284 static void setuptex(int cltexnum, int rtexnum, qbyte *data, qbyte *particletexturedata)
1285 {
1286         int basex, basey, y;
1287         basex = ((rtexnum >> 0) & 7) * 32;
1288         basey = ((rtexnum >> 3) & 7) * 32;
1289         particletexture[cltexnum].s1 = (basex + 1) / 256.0f;
1290         particletexture[cltexnum].t1 = (basey + 1) / 256.0f;
1291         particletexture[cltexnum].s2 = (basex + 31) / 256.0f;
1292         particletexture[cltexnum].t2 = (basey + 31) / 256.0f;
1293         for (y = 0;y < 32;y++)
1294                 memcpy(particletexturedata + ((basey + y) * 256 + basex) * 4, data + y * 32 * 4, 32 * 4);
1295 }
1296
1297 static void R_InitParticleTexture (void)
1298 {
1299         int x,y,d,i,m;
1300         float dx, dy, radius, f, f2;
1301         qbyte data[32][32][4], noise1[64][64], noise2[64][64];
1302         vec3_t light;
1303         qbyte particletexturedata[256*256*4];
1304
1305         memset(particletexturedata, 255, sizeof(particletexturedata));
1306
1307         // the particletexture[][] array numbers must match the cl_part.c textures
1308         // smoke/blood
1309         for (i = 0;i < 8;i++)
1310         {
1311                 do
1312                 {
1313                         fractalnoise(&noise1[0][0], 64, 4);
1314                         fractalnoise(&noise2[0][0], 64, 8);
1315                         m = 0;
1316                         for (y = 0;y < 32;y++)
1317                         {
1318                                 dy = y - 16;
1319                                 for (x = 0;x < 32;x++)
1320                                 {
1321                                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1322                                         dx = x - 16;
1323                                         d = (noise2[y][x] - 128) * 3 + 192;
1324                                         if (d > 0)
1325                                                 d = (d * (256 - (int) (dx*dx+dy*dy))) >> 8;
1326                                         d = (d * noise1[y][x]) >> 7;
1327                                         d = bound(0, d, 255);
1328                                         data[y][x][3] = (qbyte) d;
1329                                         if (m < d)
1330                                                 m = d;
1331                                 }
1332                         }
1333                 }
1334                 while (m < 224);
1335
1336                 setuptex(i + 0, i + 0, &data[0][0][0], particletexturedata);
1337         }
1338
1339         // rain splash
1340         for (i = 0;i < 16;i++)
1341         {
1342                 radius = i * 3.0f / 16.0f;
1343                 f2 = 255.0f * ((15.0f - i) / 15.0f);
1344                 for (y = 0;y < 32;y++)
1345                 {
1346                         dy = (y - 16) * 0.25f;
1347                         for (x = 0;x < 32;x++)
1348                         {
1349                                 dx = (x - 16) * 0.25f;
1350                                 data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1351                                 f = (1.0 - fabs(radius - sqrt(dx*dx+dy*dy))) * f2;
1352                                 f = bound(0.0f, f, 255.0f);
1353                                 data[y][x][3] = (int) f;
1354                         }
1355                 }
1356                 setuptex(i + 8, i + 16, &data[0][0][0], particletexturedata);
1357         }
1358
1359         // normal particle
1360         for (y = 0;y < 32;y++)
1361         {
1362                 dy = y - 16;
1363                 for (x = 0;x < 32;x++)
1364                 {
1365                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1366                         dx = x - 16;
1367                         d = (256 - (dx*dx+dy*dy));
1368                         d = bound(0, d, 255);
1369                         data[y][x][3] = (qbyte) d;
1370                 }
1371         }
1372         setuptex(24, 32, &data[0][0][0], particletexturedata);
1373
1374         // rain
1375         light[0] = 1;light[1] = 1;light[2] = 1;
1376         VectorNormalize(light);
1377         for (y = 0;y < 32;y++)
1378         {
1379                 for (x = 0;x < 32;x++)
1380                 {
1381                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1382                         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);
1383                 }
1384         }
1385         setuptex(25, 33, &data[0][0][0], particletexturedata);
1386
1387         // bubble
1388         light[0] = 1;light[1] = 1;light[2] = 1;
1389         VectorNormalize(light);
1390         for (y = 0;y < 32;y++)
1391         {
1392                 for (x = 0;x < 32;x++)
1393                 {
1394                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
1395                         data[y][x][3] = shadebubble((x - 16) * (1.0 / 16.0), (y - 16) * (1.0 / 16.0), light);
1396                 }
1397         }
1398         setuptex(26, 34, &data[0][0][0], particletexturedata);
1399
1400 #if WORKINGLQUAKE
1401         glBindTexture(GL_TEXTURE_2D, (particlefonttexture = gl_extension_number++));
1402         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1403         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1404 #else
1405         particlefonttexture = R_LoadTexture (particletexturepool, "particlefont", 256, 256, particletexturedata, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
1406 #endif
1407 }
1408
1409 static void r_part_start(void)
1410 {
1411         particletexturepool = R_AllocTexturePool();
1412         R_InitParticleTexture ();
1413 }
1414
1415 static void r_part_shutdown(void)
1416 {
1417         R_FreeTexturePool(&particletexturepool);
1418 }
1419
1420 static void r_part_newmap(void)
1421 {
1422         cl_numparticles = 0;
1423 }
1424
1425 void R_Particles_Init (void)
1426 {
1427         Cvar_RegisterVariable(&r_drawparticles);
1428 #ifdef WORKINGLQUAKE
1429         r_part_start();
1430 #else
1431         R_RegisterModule("R_Particles", r_part_start, r_part_shutdown, r_part_newmap);
1432 #endif
1433 }
1434
1435 #ifdef WORKINGLQUAKE
1436 void R_InitParticles(void)
1437 {
1438         CL_Particles_Init();
1439         R_Particles_Init();
1440 }
1441
1442 float varray_vertex[16];
1443 #endif
1444
1445 void R_DrawParticleCallback(const void *calldata1, int calldata2)
1446 {
1447         int additive, texnum, orientation;
1448         float org[3], up2[3], v[3], right[3], up[3], fog, ifog, fogvec[3], cr, cg, cb, ca;
1449         particletexture_t *tex;
1450 #ifndef WORKINGLQUAKE
1451         rmeshstate_t m;
1452 #endif
1453         const particle_t *p = calldata1;
1454
1455         VectorCopy(p->org, org);
1456         orientation = (p->flags >> P_ORIENTATION_FIRSTBIT) & ((1 << P_ORIENTATION_BITS) - 1);
1457         texnum = (p->flags >> P_TEXNUM_FIRSTBIT) & ((1 << P_TEXNUM_BITS) - 1);
1458         //dynlight = p->flags & P_DYNLIGHT;
1459         additive = p->flags & P_ADDITIVE;
1460
1461 #ifdef WORKINGLQUAKE
1462         if (additive)
1463                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1464         else
1465                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1466 #else
1467         memset(&m, 0, sizeof(m));
1468         m.blendfunc1 = GL_SRC_ALPHA;
1469         if (additive)
1470                 m.blendfunc2 = GL_ONE;
1471         else
1472                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1473         m.tex[0] = R_GetTexture(particlefonttexture);
1474         R_Mesh_Matrix(&r_identitymatrix);
1475         R_Mesh_State(&m);
1476 #endif
1477
1478         tex = &particletexture[texnum];
1479         cr = p->color[0] * (1.0f / 255.0f);
1480         cg = p->color[1] * (1.0f / 255.0f);
1481         cb = p->color[2] * (1.0f / 255.0f);
1482         ca = p->alpha * (1.0f / 255.0f);
1483 #ifndef WORKINGLQUAKE
1484         if (fogenabled)
1485         {
1486                 VectorSubtract(org, r_origin, fogvec);
1487                 fog = exp(fogdensity/DotProduct(fogvec,fogvec));
1488                 ifog = 1 - fog;
1489                 cr = cr * ifog;
1490                 cg = cg * ifog;
1491                 cb = cb * ifog;
1492                 if (!additive)
1493                 {
1494                         cr += fogcolor[0] * fog;
1495                         cg += fogcolor[1] * fog;
1496                         cb += fogcolor[2] * fog;
1497                 }
1498         }
1499         cr *= r_colorscale;
1500         cg *= r_colorscale;
1501         cb *= r_colorscale;
1502
1503         varray_texcoord[0][0] = tex->s2;varray_texcoord[0][1] = tex->t1;
1504         varray_texcoord[0][2] = tex->s1;varray_texcoord[0][3] = tex->t1;
1505         varray_texcoord[0][4] = tex->s1;varray_texcoord[0][5] = tex->t2;
1506         varray_texcoord[0][6] = tex->s2;varray_texcoord[0][7] = tex->t2;
1507 #endif
1508
1509         if (orientation == PARTICLE_BEAM)
1510         {
1511                 VectorMA(p->org, -p->scaley, p->vel, v);
1512                 VectorMA(p->org, p->scaley, p->vel, up2);
1513                 R_CalcBeamVerts(varray_vertex, v, up2, p->scalex);
1514         }
1515         else if (orientation == PARTICLE_BILLBOARD)
1516         {
1517                 VectorScale(vright, p->scalex, right);
1518                 VectorScale(vup, p->scaley, up);
1519                 varray_vertex[ 0] = org[0] + right[0] - up[0];
1520                 varray_vertex[ 1] = org[1] + right[1] - up[1];
1521                 varray_vertex[ 2] = org[2] + right[2] - up[2];
1522                 varray_vertex[ 4] = org[0] - right[0] - up[0];
1523                 varray_vertex[ 5] = org[1] - right[1] - up[1];
1524                 varray_vertex[ 6] = org[2] - right[2] - up[2];
1525                 varray_vertex[ 8] = org[0] - right[0] + up[0];
1526                 varray_vertex[ 9] = org[1] - right[1] + up[1];
1527                 varray_vertex[10] = org[2] - right[2] + up[2];
1528                 varray_vertex[12] = org[0] + right[0] + up[0];
1529                 varray_vertex[13] = org[1] + right[1] + up[1];
1530                 varray_vertex[14] = org[2] + right[2] + up[2];
1531         }
1532         else if (orientation == PARTICLE_ORIENTED_DOUBLESIDED)
1533         {
1534                 // double-sided
1535                 if (DotProduct(p->vel2, r_origin) > DotProduct(p->vel2, org))
1536                 {
1537                         VectorNegate(p->vel2, v);
1538                         VectorVectors(v, right, up);
1539                 }
1540                 else
1541                         VectorVectors(p->vel2, right, up);
1542                 VectorScale(right, p->scalex, right);
1543                 VectorScale(up, p->scaley, up);
1544                 varray_vertex[ 0] = org[0] + right[0] - up[0];
1545                 varray_vertex[ 1] = org[1] + right[1] - up[1];
1546                 varray_vertex[ 2] = org[2] + right[2] - up[2];
1547                 varray_vertex[ 4] = org[0] - right[0] - up[0];
1548                 varray_vertex[ 5] = org[1] - right[1] - up[1];
1549                 varray_vertex[ 6] = org[2] - right[2] - up[2];
1550                 varray_vertex[ 8] = org[0] - right[0] + up[0];
1551                 varray_vertex[ 9] = org[1] - right[1] + up[1];
1552                 varray_vertex[10] = org[2] - right[2] + up[2];
1553                 varray_vertex[12] = org[0] + right[0] + up[0];
1554                 varray_vertex[13] = org[1] + right[1] + up[1];
1555                 varray_vertex[14] = org[2] + right[2] + up[2];
1556         }
1557         else
1558                 Host_Error("R_DrawParticles: unknown particle orientation %i\n", orientation);
1559 #if WORKINGLQUAKE
1560         glBegin(GL_QUADS);
1561         glColor4f(cr, cg, cb, ca);
1562         glTexCoord2f(tex->s2, tex->t1);glVertex3f(varray_vertex[ 0], varray_vertex[ 1], varray_vertex[ 2]);
1563         glTexCoord2f(tex->s1, tex->t1);glVertex3f(varray_vertex[ 4], varray_vertex[ 5], varray_vertex[ 6]);
1564         glTexCoord2f(tex->s1, tex->t2);glVertex3f(varray_vertex[ 8], varray_vertex[ 9], varray_vertex[10]);
1565         glTexCoord2f(tex->s2, tex->t2);glVertex3f(varray_vertex[12], varray_vertex[13], varray_vertex[14]);
1566         glEnd();
1567 #else
1568         GL_Color(cr, cg, cb, ca);
1569         R_Mesh_Draw(4, 2, polygonelements);
1570 #endif
1571 }
1572
1573 void R_DrawParticles (void)
1574 {
1575         int i;
1576         float minparticledist;
1577         particle_t *p;
1578
1579 #ifdef WORKINGLQUAKE
1580         CL_MoveParticles();
1581 #endif
1582
1583         // LordHavoc: early out conditions
1584         if ((!cl_numparticles) || (!r_drawparticles.integer))
1585                 return;
1586
1587         minparticledist = DotProduct(r_origin, vpn) + 16.0f;
1588
1589 #ifdef WORKINGLQUAKE
1590         glBindTexture(GL_TEXTURE_2D, particlefonttexture);
1591         glEnable(GL_BLEND);
1592         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1593         glDepthMask(0);
1594         // LordHavoc: only render if not too close
1595         for (i = 0, p = particles;i < cl_numparticles;i++, p++)
1596                 if (DotProduct(p->org, vpn) >= minparticledist)
1597                         R_DrawParticleCallback(p, 0);
1598         glDepthMask(1);
1599         glDisable(GL_BLEND);
1600         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1601 #else
1602         // LordHavoc: only render if not too close
1603         c_particles += cl_numparticles;
1604         for (i = 0, p = particles;i < cl_numparticles;i++, p++)
1605                 if (DotProduct(p->org, vpn) >= minparticledist)
1606                         R_MeshQueue_AddTransparent(p->org, R_DrawParticleCallback, p, 0);
1607 #endif
1608 }
1609