naev 0.12.5
debris.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
9
11#include "naev.h"
13
14#include "debris.h"
15
16#include "log.h"
17#include "nstring.h"
18#include "pilot.h"
19#include "rng.h"
20#include "spfx.h"
21
22static int *debris_spfx = NULL;
23static int debris_nspfx = 0;
24
28void debris_cleanup( void )
29{
30 free( debris_spfx );
31 debris_spfx = NULL;
32}
33
39static int debris_load( void )
40{
41 int i;
42 char buf[32];
43
44 /* Calculate amount. */
45 i = 0;
46 do {
47 snprintf( buf, sizeof( buf ), "Dbr%d", i );
48 i++;
49 } while ( spfx_get( buf ) != -1 );
50 debris_nspfx = i - 1;
51
52 /* Check to make sure they exist. */
53 if ( debris_nspfx <= 0 ) {
54 WARN( _( "No debris special effects found." ) );
55 return -1;
56 }
57
58 /* Allocate. */
59 debris_spfx = malloc( sizeof( int ) * debris_nspfx );
60
61 /* Second pass to fill. */
62 for ( i = 0; i < debris_nspfx; i++ ) {
63 snprintf( buf, sizeof( buf ), "Dbr%d", i );
64 debris_spfx[i] = spfx_get( buf );
65 }
66
67 return 0;
68}
69
80void debris_add( double mass, double r, double px, double py, double vx,
81 double vy )
82{
83 int n;
84
85 if ( !space_needsEffects() )
86 return;
87
88 /* Lazy allocator. */
89 if ( debris_spfx == NULL )
90 if ( debris_load() < 0 )
91 return;
92
93 /* Get number of debris to render. */
94 n = (int)ceil( sqrt( mass ) / 1.5 );
95
96 /* Now add the spfx. */
97 for ( int i = 0; i < n; i++ ) {
98 double npx, npy, nvx, nvy;
99 double a, d;
100
101 /* Get position. */
102 d = r / 2. * RNG_2SIGMA();
103 a = RNGF() * 2. * M_PI;
104 npx = px + d * cos( a );
105 npy = py + d * sin( a );
106
107 /* Get velocity. */
108 d = n * RNG_2SIGMA();
109 a = RNGF() * 2. * M_PI;
110 nvx = vx + d * cos( a );
111 nvy = vy + d * sin( a );
112
113 /* Create sprite. */
114 spfx_add( debris_spfx[RNG( 0, debris_nspfx - 1 )], npx, npy, nvx, nvy,
115 RNG( 0, 1 ) );
116 }
117}
static int debris_nspfx
Definition debris.c:23
static int debris_load(void)
Loads the debris spfx into an array.
Definition debris.c:39
void debris_cleanup(void)
Cleans up after the debris.
Definition debris.c:28
static int * debris_spfx
Definition debris.c:22
void debris_add(double mass, double r, double px, double py, double vx, double vy)
Creates a cloud of debris.
Definition debris.c:80
Header file with generic functions and naev-specifics.
static const double d[]
Definition rng.c:263
int space_needsEffects(void)
returns whether or not we're simulating with effects.
Definition space.c:1609
int spfx_get(const char *name)
Gets the id of an spfx based on name.
Definition spfx.c:355
void spfx_add(int effect, const double px, const double py, const double vx, const double vy, int layer)
Creates a new special effect.
Definition spfx.c:504