Merge branch 'master' of git://github.com/BTAxis/naev into testmission
[naev.git] / src / debris.c
blob2d83a4c187171d59850bdba2e637a48e3c512111
1 /*
2 * See Licensing and Copyright notice in naev.h
3 */
5 /**
6 * @file debris.c
8 * @brief Handles scattering debris around.
9 */
12 #include "debris.h"
14 #include "naev.h"
16 #include "log.h"
17 #include "pilot.h"
18 #include "spfx.h"
19 #include "rng.h"
22 static int *debris_spfx = NULL; /**< Debris special effects. */
23 static int debris_nspfx = 0; /**< Number of debris special effects. */
26 /**
27 * @brief Cleans up after the debris.
29 void debris_cleanup (void)
31 free(debris_spfx);
32 debris_spfx = NULL;
36 /**
37 * @brief Loads the debris spfx into an array.
39 static int debris_load (void)
41 int i;
42 char buf[32];
44 /* Calculate amount. */
45 i = 0;
46 do {
47 snprintf( buf, 32, "Dbr%d", i );
48 i++;
49 } while (spfx_get(buf) != -1);
50 debris_nspfx = i-1;
52 /* Check to make sure they exist. */
53 if (debris_nspfx <= 0) {
54 WARN("No debris special effects found.");
55 return -1;
58 /* Allocate. */
59 debris_spfx = malloc( sizeof(int) * debris_nspfx );
61 /* Second pass to fill. */
62 for (i=0; i<debris_nspfx; i++) {
63 snprintf( buf, 32, "Dbr%d", i );
64 debris_spfx[i] = spfx_get(buf);
67 return 0;
71 /**
72 * @brief Creates a cloud of debris.
74 * @param mass Mass of the debris cloud.
75 * @param x X position to center cloud.
76 * @param y Y position to center cloud.
78 void debris_add( double mass, double r, double px, double py,
79 double vx, double vy )
81 int i, n;
82 double npx,npy, nvx,nvy;
83 double a, d;
85 /* Lazy allocator. */
86 if (debris_spfx == NULL)
87 if (debris_load() < 0)
88 return;
90 /* Get number of debris to render. */
91 n = (int) ceil( sqrt(mass) / 1.5 );
93 /* Now add the spfx. */
94 for (i=0; i<n; i++) {
95 /* Get position. */
96 d = r/2. * RNG_2SIGMA();
97 a = RNGF()*2*M_PI;
98 npx = px + d*cos(a);
99 npy = py + d*sin(a);
101 /* Get velocity. */
102 d = n * RNG_2SIGMA();
103 a = RNGF()*2*M_PI;
104 nvx = vx + d*cos(a);
105 nvy = vy + d*sin(a);
107 /* Createsprite. */
108 spfx_add( debris_spfx[ RNG( 0, debris_nspfx-1 ) ],
109 npx, npy, nvx, nvy, RNG(0,1) );