]> icculus.org git repositories - taylor/freespace2.git/blob - src/particle/particle.cpp
Freespace 1 support
[taylor/freespace2.git] / src / particle / particle.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Particle/Particle.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code for particle system
16  *
17  * $Log$
18  * Revision 1.6  2003/05/25 02:30:43  taylor
19  * Freespace 1 support
20  *
21  * Revision 1.5  2002/06/17 06:33:10  relnev
22  * ryan's struct patch for gcc 2.95
23  *
24  * Revision 1.4  2002/06/09 04:41:25  relnev
25  * added copyright header
26  *
27  * Revision 1.3  2002/06/09 03:16:04  relnev
28  * added _splitpath.
29  *
30  * removed unneeded asm, old sdl 2d setup.
31  *
32  * fixed crash caused by opengl_get_region.
33  *
34  * Revision 1.2  2002/05/07 03:16:48  theoddone33
35  * The Great Newline Fix
36  *
37  * Revision 1.1.1.1  2002/05/03 03:28:10  root
38  * Initial import.
39  *
40  * 
41  * 11    7/21/99 8:10p Dave
42  * First run of supernova effect.
43  * 
44  * 10    7/15/99 9:20a Andsager
45  * FS2_DEMO initial checkin
46  * 
47  * 9     7/07/99 3:11p Dave
48  * Fix for uninitialized particle system data.
49  * 
50  * 8     4/22/99 11:06p Dave
51  * Final pass at beam weapons. Solidified a lot of stuff. All that remains
52  * now is to tweak and fix bugs as they come up. No new beam weapon
53  * features.
54  * 
55  * 7     1/29/99 12:47a Dave
56  * Put in sounds for beam weapon. A bunch of interface screens (tech
57  * database stuff).
58  * 
59  * 6     1/28/99 9:10a Andsager
60  * Particles increased in width, life, number.  Max particles increased
61  * 
62  * 5     1/27/99 9:56a Dave
63  * Temporary checkin of beam weapons for Dan to make cool sounds.
64  * 
65  * 4     1/24/99 11:37p Dave
66  * First full rev of beam weapons. Very customizable. Removed some bogus
67  * Int3()'s in low level net code.
68  * 
69  * 3     1/21/99 2:06p Dave
70  * Final checkin for multiplayer testing.
71  * 
72  * 2     10/07/98 10:53a Dave
73  * Initial checkin.
74  * 
75  * 1     10/07/98 10:50a Dave
76  * 
77  * 28    5/13/98 3:25p John
78  * Added code to make explosion impacts not get removed by other
79  * particles.
80  * 
81  * 27    5/11/98 10:06a John
82  * Added new particle for Adam
83  * 
84  * 26    4/30/98 11:31a Andsager
85  * Added particles to big ship explosions.  Modified particle_emit() to
86  * take optional range to increase range at which pariticles are created.
87  * 
88  * 25    4/17/98 1:42p Allender
89  * fixed NDEBUG build problems
90  * 
91  * 24    4/17/98 6:58a John
92  * Made particles not reduce in low mem conditions.
93  * 
94  * 23    4/15/98 4:21p John
95  * Made particles drop off with distance smoothly.  Made old particles get
96  * deleted by new ones.
97  * 
98  * 22    4/15/98 11:15a Adam
99  * upped MAX_PARTICLES to 1500 (from 800).  Primary weapon hit sparks were
100  * not finding slots.
101  * 
102  * 21    4/12/98 10:24a John
103  * Made particle drop off distance larger for larger detail levels.
104  * 
105  * 20    4/09/98 7:58p John
106  * Cleaned up tmapper code a bit.   Put NDEBUG around some ndebug stuff.
107  * Took out XPARENT flag settings in all the alpha-blended texture stuff.
108  * 
109  * 19    4/02/98 11:40a Lawrance
110  * check for #ifdef DEMO instead of #ifdef DEMO_RELEASE
111  * 
112  * 18    4/01/98 9:21p John
113  * Made NDEBUG, optimized build with no warnings or errors.
114  * 
115  * 17    4/01/98 10:57a Mike
116  * Reduce array sizes to save memory.
117  * 
118  * 16    3/31/98 5:18p John
119  * Removed demo/save/restore.  Made NDEBUG defined compile.  Removed a
120  * bunch of debug stuff out of player file.  Made model code be able to
121  * unload models and malloc out only however many models are needed.
122  *  
123  * 
124  * 15    3/30/98 4:02p John
125  * Made machines with < 32 MB of RAM use every other frame of certain
126  * bitmaps.   Put in code to keep track of how much RAM we've malloc'd.
127  * 
128  * 14    3/26/98 5:26p John
129  * added new paging code. nonfunctional.
130  * 
131  * 13    3/22/98 11:02a John
132  * Made a bunch of the detail levels actually do something
133  * 
134  * 12    2/13/98 5:01p John
135  * Made particles behind you not render
136  * 
137  * 11    2/06/98 7:28p John
138  * Made debris and particles not get created if > 200 m from eye.   Added
139  * max_velocity to asteroid's physics info for aiding in throwing out
140  * collision pairs.
141  * 
142  * 10    2/05/98 9:21p John
143  * Some new Direct3D code.   Added code to monitor a ton of stuff in the
144  * game.
145  * 
146  * 9     1/29/98 11:48a John
147  * Added new counter measure rendering as model code.   Made weapons be
148  * able to have impact explosion.
149  * 
150  * 8     1/26/98 5:10p John
151  * Took particle use out of registry.
152  * 
153  * 7     1/23/98 5:08p John
154  * Took L out of vertex structure used B (blue) instead.   Took all small
155  * fireballs out of fireball types and used particles instead.  Fixed some
156  * debris explosion things.  Restructured fireball code.   Restructured
157  * some lighting code.   Made dynamic lighting on by default. Made groups
158  * of lasers only cast one light.  Made fireballs not cast light.
159  * 
160  * 6     1/13/98 8:09p John
161  * Removed the old collision system that checked all pairs.   Added code
162  * to disable collisions and particles.
163  * 
164  * 5     1/02/98 5:04p John
165  * Several explosion related changes.  Made fireballs not be used as
166  * ani's.  Made ship spark system expell particles.  Took away impact
167  * explosion for weapon hitting ship... this needs to get added to weapon
168  * info and makes shield hit more obvious.  Only make sparks when hit
169  * hull, not shields.
170  * 
171  * 4     12/30/97 6:44p John
172  * Made g3_Draw_bitmap functions account for aspect of bitmap.
173  * 
174  * 3     12/26/97 5:42p Adam
175  * 
176  * 2     12/23/97 3:58p John
177  * Second rev of particles
178  * 
179  * 1     12/23/97 8:26a John
180  *
181  * $NoKeywords: $
182  */
183
184 #include "pstypes.h"
185 #include "freespace.h"
186 #include "linklist.h"
187 #include "3d.h" 
188 #include "bmpman.h"
189 #include "particle.h"
190 #include "osapi.h"
191 #include "object.h"
192 #include "timer.h"
193
194 typedef struct particle {
195         // old style data
196         vector  pos;                                    // position
197         vector  velocity;                       // velocity
198         float           age;                                    // How long it's been alive
199         float           max_life;                       // How much life we had
200         float           radius;                         // radius
201         int             type;                                   // type                                                                         // -1 = None
202         uint            optional_data;          // depends on type
203         int             nframes;                                // If an ani, how many frames?  
204
205         // new style data
206         float           tracer_length;          // if this is set, draw as a rod to simulate a "tracer" effect
207         short           attached_objnum;        // if this is set, pos is relative to the attached object. velocity is ignored
208         int             attached_sig;           // to check for dead/nonexistent objects
209         ubyte           reverse;                                // play any animations in reverse
210 } particle;
211
212 #if defined(FS2_DEMO) || defined(FS1_DEMO)
213         #define MAX_PARTICLES   500
214 #else
215         #define MAX_PARTICLES   2000    //      Reduced from 2000 to 800 by MK on 4/1/98.  Most I ever saw was 400 and the system recovers
216                                                                                         //      gracefully from running out of slots.
217                                                                                         // AP: Put it to 1500 on 4/15/98.  Primary hit sparks weren't finding open slots.  
218                                                                                         // Made todo item for John to force oldest smoke particles to give up their slots.
219 #endif
220
221 int Num_particles = 0;
222 particle Particles[MAX_PARTICLES];
223 int Next_particle = 0;
224
225 int Anim_bitmap_id_fire = -1;
226 int Anim_num_frames_fire = -1;
227
228 int Anim_bitmap_id_smoke = -1;
229 int Anim_num_frames_smoke = -1;
230
231 int Anim_bitmap_id_smoke2 = -1;
232 int Anim_num_frames_smoke2 = -1;
233
234 static int Particles_enabled = 1;
235
236 // Reset everything between levels
237 void particle_init()
238 {
239         int i;
240
241 //      Particles_enabled = os_config_read_uint( NULL, "UseParticles", 0 );
242
243         Num_particles = 0;
244         Next_particle = 0;
245
246         for (i=0; i<MAX_PARTICLES; i++ )        {
247                 Particles[i].type = -1;
248         }
249
250         if ( Anim_bitmap_id_fire == -1 )        {
251                 int fps;
252                 Anim_bitmap_id_fire = bm_load_animation( "particleexp01", &Anim_num_frames_fire, &fps, 1 );
253         }
254                 //Anim_bitmap_id = bm_load( "particleglow01" );
255                 //Anim_num_frames = 1;
256
257         if ( Anim_bitmap_id_smoke == -1 )       {
258                 int fps;
259                 Anim_bitmap_id_smoke = bm_load_animation( "particlesmoke01", &Anim_num_frames_smoke, &fps, 1 );
260         }
261
262         if ( Anim_bitmap_id_smoke2 == -1 )      {
263                 int fps;
264                 Anim_bitmap_id_smoke2 = bm_load_animation( "particlesmoke02", &Anim_num_frames_smoke2, &fps, 1 );
265         }
266
267 }
268
269 void particle_page_in()
270 {
271         int i;
272
273         for (i=0; i<MAX_PARTICLES; i++ )        {
274                 Particles[i].type = -1;
275         }
276
277         for (i=0; i<Anim_num_frames_fire; i++ ) {
278                 bm_page_in_texture( Anim_bitmap_id_fire + i );
279         }
280
281         for (i=0; i<Anim_num_frames_smoke; i++ )        {
282                 bm_page_in_texture( Anim_bitmap_id_smoke + i );
283         }
284
285         for (i=0; i<Anim_num_frames_smoke2; i++ )       {
286                 bm_page_in_texture( Anim_bitmap_id_smoke2 + i );
287         }
288 }
289
290 DCF(particles,"Turns particles on/off")
291 {
292         if ( Dc_command )       {       
293                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_NONE);                
294                 if ( Dc_arg_type & ARG_TRUE )   Particles_enabled = 1;  
295                 else if ( Dc_arg_type & ARG_FALSE ) Particles_enabled = 0;      
296                 else if ( Dc_arg_type & ARG_NONE ) Particles_enabled ^= 1;      
297         }       
298         if ( Dc_help )  dc_printf( "Usage: particles [bool]\nTurns particle system on/off.  If nothing passed, then toggles it.\n" );   
299         if ( Dc_status )        dc_printf( "particles are %s\n", (Particles_enabled?"ON":"OFF") );      
300
301 //      os_config_write_uint( NULL, "UseParticles", Particles_enabled );
302 }
303
304
305                 //mprintf(( "%s\n", text ));
306
307 int Num_particles_hwm = 0;
308
309 #ifndef NDEBUG
310 int Total_requested = 0;
311 int Total_killed = 0;
312 int next_message = -1;
313 #endif
314
315 // Creates a single particle. See the PARTICLE_?? defines for types.
316 void particle_create( particle_info *pinfo )
317 {
318         int particle_num;
319         particle *p;
320
321 #ifndef NDEBUG
322         if ( next_message == -1 )       {
323                 next_message = timestamp(10000);
324         }
325
326         if ( timestamp_elapsed(next_message)) {
327                 next_message = timestamp(10000);
328                 if ( Total_requested > 1 )      {
329                         nprintf(( "Particles", "Particles: Killed off %d%% of the particles\n", (Total_killed*100/Total_requested) ));
330                 }
331                 Total_requested = 0;
332                 Total_killed = 0;
333         }
334 #endif
335
336         if ( !Particles_enabled ) return;
337
338         #ifndef NDEBUG
339         Total_requested++;
340         #endif
341
342         int RetryCount = 0;
343         int FirstParticleFound = Next_particle;
344 KillAnother:
345         p = &Particles[Next_particle++];
346         if ( Next_particle >= MAX_PARTICLES )   {
347                 Next_particle = 0;
348         }
349
350         if ( p->type > -1 )     {
351                 // Only remove non-persistent ones
352                 if ( p->type != PARTICLE_BITMAP_PERSISTENT )    {
353                         p->type = -1;
354                         Num_particles--;
355                         #ifndef NDEBUG
356                         Total_killed++;
357                         #endif
358                 } else {
359                         RetryCount++;
360                         // Keep trying to find a non-persistent one until we searched through 1/3 the slots.
361                         if ( RetryCount < MAX_PARTICLES/3 )     {
362                                 goto KillAnother;                       
363                         } 
364                         // Couldn't find any non-persistent ones to remove, so just remove the
365                         // first one we would have removed.
366                         mprintf(( "DELETING A PERSISTENT PARTICLE!!! This is ok if this only happens rarely. Get John if not.\n" ));
367                         Next_particle = FirstParticleFound;
368                         p = &Particles[Next_particle++];
369                         if ( Next_particle >= MAX_PARTICLES )   {
370                                 Next_particle = 0;
371                         }
372                 }
373         }
374
375         // increment counter
376         Num_particles++;
377         if (Num_particles > Num_particles_hwm) {
378                 Num_particles_hwm = Num_particles;
379
380                 if ( Num_particles_hwm == MAX_PARTICLES )       {
381                         mprintf(( "All particle slots filled!\n" ));
382                 }
383                 //mprintf(( "Num_particles high water mark = %i\n", Num_particles_hwm));
384         }
385
386         // get objnum
387         particle_num = p-Particles;
388
389         // Init the particle data
390         p->pos = pinfo->pos;
391         p->velocity = pinfo->vel;
392         p->age = 0.0f;
393         p->max_life = pinfo->lifetime;
394         p->radius = pinfo->rad;
395         p->type = pinfo->type;
396         p->optional_data = pinfo->optional_data;
397         p->tracer_length = pinfo->tracer_length;
398         p->attached_objnum = pinfo->attached_objnum;
399         p->attached_sig = pinfo->attached_sig;
400         p->reverse = pinfo->reverse;
401
402         if ( (p->type == PARTICLE_BITMAP) || (p->type == PARTICLE_BITMAP_PERSISTENT) )  {
403                 int fps;
404                 bm_get_info( p->optional_data,NULL, NULL, NULL, &p->nframes, &fps );
405                 if ( p->nframes > 1 )   {
406                         // Recalculate max life for ani's
407                         p->max_life = i2fl(p->nframes) / i2fl(fps);
408                 }
409         } else {
410                 p->nframes = 1;
411         }
412 }
413
414 void particle_create( vector *pos, vector *vel, float lifetime, float rad, int type, uint optional_data )
415 {
416         particle_info pinfo;
417
418         // setup old data
419         pinfo.pos = *pos;
420         pinfo.vel = *vel;
421         pinfo.lifetime = lifetime;
422         pinfo.rad = rad;
423         pinfo.type = type;
424         pinfo.optional_data = optional_data;    
425
426         // setup new data
427         pinfo.tracer_length = -1.0f;
428         pinfo.attached_objnum = -1;
429         pinfo.attached_sig = -1;
430         pinfo.reverse = 0;
431
432         // lower level function
433         particle_create(&pinfo);
434 }
435
436 MONITOR( NumParticles );        
437
438 void particle_move_all(float frametime)
439 {
440         particle *p;
441
442         MONITOR_INC( NumParticles, Num_particles );     
443
444         if ( !Particles_enabled ) return;
445
446         p = Particles;
447
448         int i;
449         for ( i=0; i<MAX_PARTICLES; i++, p++ )  {
450                 
451                 if ( p->type == -1 )    {
452                         continue;
453                 }
454
455                 // bogus attached objnum
456                 if(p->attached_objnum >= MAX_OBJECTS){
457                         p->type = -1;
458
459                         // decrement counter
460                         Num_particles--;
461         
462                         Assert(Num_particles >= 0);
463                         continue;
464                 }
465
466                 // if the vector is attached to an object which has become invalid, kill if
467                 if(p->attached_objnum >= 0){
468                         // if the signature has changed, kill it
469                         if(p->attached_sig != Objects[p->attached_objnum].signature){
470                                 p->type = -1;
471
472                                 // decrement counter
473                                 Num_particles--;
474
475                                 Assert(Num_particles >= 0);
476                                 continue;
477                         }
478                 }
479                 // move as a regular particle
480                 else {
481                         // Move the particle
482                         vm_vec_scale_add2( &p->pos, &p->velocity, frametime );          
483                 }
484
485                 p->age += frametime;
486         
487                 if ( p->age > p->max_life )     {
488
489                         // If it's time expired, remove it from the used list and 
490                         // into the free list
491                         p->type = -1;
492
493                         // decrement counter
494                         Num_particles--;
495
496                         Assert(Num_particles >= 0);
497                 }
498         }
499 }
500
501 // kill all active particles
502 void particle_kill_all()
503 {
504         int idx;
505
506         // kill all active particles
507         Num_particles = 0;
508         Next_particle = 0;
509         Num_particles_hwm = 0;
510         for(idx=0; idx<MAX_PARTICLES; idx++){
511                 Particles[idx].type = -1;
512         }
513 }
514
515 MONITOR( NumParticlesRend );    
516
517 void particle_render_all()
518 {
519         particle *p;
520         ubyte flags;
521         float pct_complete;
522         float alpha;
523         vertex pos;
524         vector ts, te, temp;
525         int rotate = 1;
526
527         if ( !Particles_enabled ) return;
528
529         MONITOR_INC( NumParticlesRend, Num_particles ); 
530
531         int n = 0;
532         int nclipped = 0;
533
534         p = Particles;
535
536         int i;
537         for ( i=0; i<MAX_PARTICLES; i++, p++ )  {
538                 
539                 if ( p->type == -1 )    {
540                         continue;
541                 }
542
543                 n++;
544
545                 // pct complete for the particle
546                 pct_complete = p->age / p->max_life;
547
548                 // calculate the alpha to draw at
549                 alpha = 1.0f;   
550                         
551                 // if this is a tracer style particle, calculate tracer vectors
552                 if(p->tracer_length > 0.0f){                    
553                         ts = p->pos;
554                         temp = p->velocity;
555                         vm_vec_normalize_quick(&temp);
556                         vm_vec_scale_add(&te, &ts, &temp, p->tracer_length);
557
558                         // don't bother rotating
559                         rotate = 0;
560                 }
561                 // if this is an "attached" particle. move it
562                 else if(p->attached_objnum >= 0){
563                         // offset the vector, and transform to view coords
564                         // vm_vec_add(&te, &Objects[p->attached_objnum].pos, &p->pos);
565                         vm_vec_unrotate(&temp, &p->pos, &Objects[p->attached_objnum].orient);
566                         vm_vec_add2(&temp, &Objects[p->attached_objnum].pos);
567
568                         flags = g3_rotate_vertex(&pos, &temp);
569                         if(flags){                              
570                                 nclipped++;
571                                 continue;
572                         }
573                         
574                         // don't bother rotating again
575                         rotate = 0;
576                 }
577
578                 // rotate the vertex
579                 if(rotate){
580                         flags = g3_rotate_vertex( &pos, &p->pos );
581                         if ( flags )    {
582                                 nclipped++;
583                                 continue;
584                         }
585                 }
586
587                 switch( p->type )       {
588
589                         case PARTICLE_DEBUG:                            // A red sphere, no optional data required
590                                 gr_set_color( 255, 0, 0 );
591                                 g3_draw_sphere_ez( &p->pos, p->radius );
592                                 break;
593
594                         case PARTICLE_BITMAP:           
595                         case PARTICLE_BITMAP_PERSISTENT:
596                                 {       // A bitmap, optional data is the bitmap number                                 
597                                         int framenum = p->optional_data;
598
599                                         if ( p->nframes > 1 )   {
600                                                 int n = fl2i(pct_complete * p->nframes + 0.5);
601
602                                                 if ( n < 0 ) n = 0;
603                                                 else if ( n > p->nframes-1 ) n = p->nframes-1;
604
605                                                 framenum += n;
606                                         }
607
608                                         // set the bitmap
609                                         gr_set_bitmap( framenum, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, alpha );
610
611                                         // if this is a tracer style particle
612                                         if(p->tracer_length > 0.0f){                                    
613                                                 g3_draw_laser(&ts, p->radius, &te, p->radius, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f);
614                                         }
615                                         // draw as a regular bitmap
616                                         else {
617                                                 g3_draw_bitmap(&pos, (p-Particles)%8, p->radius, TMAP_FLAG_TEXTURED );
618                                         }
619                                         break;
620                                 }
621
622                         case PARTICLE_FIRE:     {
623                                         int framenum = fl2i(pct_complete * Anim_num_frames_fire + 0.5);
624
625                                         if ( framenum < 0 ) framenum = 0;
626                                         else if ( framenum > Anim_num_frames_fire-1 ) framenum = Anim_num_frames_fire-1;
627
628                                         /*
629                                         vertex pos;
630                                         flags = g3_rotate_vertex( &pos, &p->pos );
631                                         if ( flags )    {
632                                                 nclipped++;
633                                                 break;
634                                         }
635                                         */
636
637                                         // set the bitmap
638                                         gr_set_bitmap(p->reverse ? Anim_bitmap_id_fire+(Anim_num_frames_fire - framenum - 1) : Anim_bitmap_id_fire+framenum, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, alpha );
639
640                                         // if this is a tracer style particle
641                                         if(p->tracer_length > 0.0f){                                    
642                                                 g3_draw_laser(&ts, p->radius, &te, p->radius, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f);
643                                         }
644                                         // draw as a regular bitmap
645                                         else {
646                                                 g3_draw_bitmap(&pos, (p-Particles)%8, p->radius, TMAP_FLAG_TEXTURED );
647                                         }
648                                         break;
649                                 }
650
651                         case PARTICLE_SMOKE:    {
652                                         int framenum = fl2i(pct_complete * Anim_num_frames_smoke + 0.5);
653
654                                         if ( framenum < 0 ) framenum = 0;
655                                         else if ( framenum > Anim_num_frames_smoke-1 ) framenum = Anim_num_frames_smoke-1;
656
657                                         /*
658                                         vertex pos;
659                                         flags = g3_rotate_vertex( &pos, &p->pos );
660                                         if ( flags )    {
661                                                 nclipped++;
662                                                 break;
663                                         }
664                                         */
665
666                                         // set the bitmap
667                                         gr_set_bitmap(p->reverse ? Anim_bitmap_id_smoke+(Anim_num_frames_smoke - framenum - 1) : Anim_bitmap_id_smoke+framenum, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, alpha );
668
669                                         // if this is a tracer style particle
670                                         if(p->tracer_length > 0.0f){                    
671                                                 g3_draw_laser(&ts, p->radius, &te, p->radius, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f);
672                                         }
673                                         // draw as a regular bitmap
674                                         else {
675                                                 g3_draw_bitmap(&pos, (p-Particles)%8, p->radius, TMAP_FLAG_TEXTURED );
676                                         }
677                                         break;
678                                 }
679
680                         case PARTICLE_SMOKE2:   {
681                                         int framenum = fl2i(pct_complete * Anim_num_frames_smoke2 + 0.5);
682
683                                         if ( framenum < 0 ) framenum = 0;
684                                         else if ( framenum > Anim_num_frames_smoke2-1 ) framenum = Anim_num_frames_smoke2-1;
685
686                                         /*
687                                         vertex pos;
688                                         flags = g3_rotate_vertex( &pos, &p->pos );
689                                         if ( flags )    {
690                                                 nclipped++;
691                                                 break;
692                                         }
693                                         */
694
695                                         // set the bitmap
696                                         gr_set_bitmap(p->reverse ? Anim_bitmap_id_smoke2+(Anim_num_frames_smoke2 - framenum - 1) : Anim_bitmap_id_smoke2+framenum, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, alpha );
697                                         
698                                         // if this is a tracer style particle
699                                         if(p->tracer_length > 0.0f){                                    
700                                                 g3_draw_laser(&ts, p->radius, &te, p->radius, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f);
701                                         }
702                                         // draw as a regular bitmap
703                                         else {                                          
704                                                 g3_draw_bitmap(&pos, (p-Particles)%8, p->radius, TMAP_FLAG_TEXTURED );
705                                         }
706                                         break;
707                                 }
708                 }
709         }
710 //      mprintf(( "NP=%d, NCP=%d\n", n, nclipped ));
711 }
712
713
714
715
716 //============================================================================
717 //============== HIGH-LEVEL PARTICLE SYSTEM CREATION CODE ====================
718 //============================================================================
719
720 // Use a structure rather than pass a ton of parameters to particle_emit
721 /*
722 typedef struct particle_emitter {
723         int             num_low;                                // Lowest number of particles to create
724         int             num_high;                       // Highest number of particles to create
725         vector  pos;                                    // Where the particles emit from
726         vector  vel;                                    // Initial velocity of all the particles
727         float           lifetime;                       // How long the particles live
728         vector  normal;                         // What normal the particle emit arond
729         float           normal_variance;        //      How close they stick to that normal 0=good, 1=360 degree
730         float           min_vel;                                // How fast the slowest particle can move
731         float           max_vel;                                // How fast the fastest particle can move
732         float           min_rad;                                // Min radius
733         float           max_rad;                                // Max radius
734 } particle_emitter;
735 */
736
737 #if MAX_DETAIL_LEVEL != 4
738 #error Max details assumed to be 4 here
739 #endif
740 int detail_max_num[5] = { 0, 50, 75, 100, 125 };
741
742 // Creates a bunch of particles. You pass a structure
743 // rather than a bunch of parameters.
744 void particle_emit( particle_emitter *pe, int type, uint optional_data, float range )
745 {
746         int i, n;
747
748         if ( !Particles_enabled ) return;
749
750         int n1, n2;
751
752         // Account for detail
753         #if MAX_DETAIL_LEVEL != 4 
754         #error Code in Particle.cpp assumes MAX_DETAIL_LEVEL == 4
755         #endif
756
757         int percent = detail_max_num[Detail.num_particles];
758
759         //Particle rendering drops out too soon.  Seems to be around 150 m.  Is it detail level controllable?  I'd like it to be 500-1000 
760         float min_dist = 125.0f;
761         float dist = vm_vec_dist_quick( &pe->pos, &Eye_position ) / range;
762         if ( dist > min_dist )  {
763                 percent = fl2i( i2fl(percent)*min_dist / dist );
764                 if ( percent < 1 ) {
765                         return;
766                 }
767         }
768         //mprintf(( "Dist = %.1f, percent = %d%%\n", dist, percent ));
769
770         n1 = (pe->num_low*percent)/100;
771         n2 = (pe->num_high*percent)/100;
772
773         // How many to emit?
774         n = (rand() % (n2-n1+1)) + n1;
775         
776         if ( n < 1 ) return;
777
778
779         for (i=0; i<n; i++ )    {
780                 // Create a particle
781                 vector tmp_vel;
782                 vector normal;                          // What normal the particle emit arond
783
784                 float radius = (( pe->max_rad - pe->min_rad ) * frand()) + pe->min_rad;
785
786                 float speed = (( pe->max_vel - pe->min_vel ) * frand()) + pe->min_vel;
787
788                 float life = (( pe->max_life - pe->min_life ) * frand()) + pe->min_life;
789
790                 normal.xyz.x = pe->normal.xyz.x + (frand()*2.0f - 1.0f)*pe->normal_variance;
791                 normal.xyz.y = pe->normal.xyz.y + (frand()*2.0f - 1.0f)*pe->normal_variance;
792                 normal.xyz.z = pe->normal.xyz.z + (frand()*2.0f - 1.0f)*pe->normal_variance;
793                 vm_vec_normalize_safe( &normal );
794                 vm_vec_scale_add( &tmp_vel, &pe->vel, &normal, speed );
795
796                 particle_create( &pe->pos, &tmp_vel, life, radius, type, optional_data );
797         }
798 }