summaryrefslogtreecommitdiff
path: root/src/modules/sparkler/particles.c
blob: 5116e3a0f67ca1d8d074a3f18d3baacc67e0011b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#include "fb.h"

#include "chunker.h"
#include "container.h"
#include "bsp.h"
#include "list.h"
#include "particle.h"
#include "particles.h"
#include "v3f.h"

#define ZCONST	0.4f

/* private particle with all the particles bookkeeping... */
typedef struct _particle_t {
	list_head_t		siblings;	/* sibling particles */
	list_head_t		children;	/* children particles */

	particle_props_t	props;		/* we reference this in the public particle, I might change
						 * the way props are allocated so coding everything to use a
						 * reference for now.  It may make sense to have props allocated
						 * separately via their own chunker, and perform some mass operations
						 * against the list of chunks rather than chasing the pointers of
						 * the particle heirarchy. TODO
						 */
	particle_t		public;		/* the public particle_t is embedded */

	uint8_t			context[];	/* particle type-specific context [public.ops.context_size] */
} _particle_t;

struct particles_t {
	chunker_t		*chunker;	/* chunker for variably-sized particle allocation (includes context) */
	list_head_t		active;		/* top-level active list of particles heirarchy */
	bsp_t			*bsp;		/* bsp spatial index of the particles */
};


/* create a new particle system */
particles_t * particles_new(void)
{
	particles_t	*particles;

	particles = calloc(1, sizeof(particles_t));
	if (!particles) {
		return NULL;
	}

	particles->chunker = chunker_new(sizeof(_particle_t) * 128);
	if (!particles->chunker) {
		return NULL;
	}

	particles->bsp = bsp_new();
	if (!particles->bsp) {
		return NULL;
	}

	INIT_LIST_HEAD(&particles->active);

	return particles;
}


/* TODO: add a public interface for destroying particles?  for now we just return PARTICLE_DEAD in the sim */
static inline void _particles_free_particle(particles_t *particles, _particle_t *p)
{
	assert(p);

	particle_cleanup(particles, &p->public);
	chunker_free(p);
}


static inline void _particles_free(particles_t *particles, list_head_t *list)
{
	_particle_t	*p, *_p;

	assert(particles);
	assert(list);

	list_for_each_entry_safe(p, _p, list, siblings) {
		_particles_free(particles, &p->children);
		_particles_free_particle(particles, p);
	}
}


/* free up all the particles */
void particles_free(particles_t *particles)
{
	assert(particles);

	_particles_free(particles, &particles->active);
}


/* reclaim a dead particle, moving it to the free list */
static void particles_reap_particle(particles_t *particles, _particle_t *particle)
{
	assert(particles);
	assert(particle);

	if (!list_empty(&particle->children)) {
		/* adopt any orphaned children using the global parts list */
		list_splice(&particle->children, &particles->active);
	}

	list_del(&particle->siblings);
	bsp_delete_occupant(particles->bsp, &particle->public.occupant);
	_particles_free_particle(particles, particle);
}


/* add a particle to the specified list */
static inline int _particles_add_particle(particles_t *particles, list_head_t *list, particle_props_t *props, particle_ops_t *ops)
{
	_particle_t	*p;

	assert(particles);
	assert(ops);
	assert(list);

	p = chunker_alloc(particles->chunker, sizeof(_particle_t) + ops->context_size);
	if (!p) {
		return 0;
	}

	INIT_LIST_HEAD(&p->children);
	INIT_LIST_HEAD(&p->siblings);

	/* inherit the parent's properties and ops if they're not explicitly provided */
	if (props) {
		p->props = *props;
	} else {
		p->props.of_use = 0;
	}

	p->public.props = &p->props;
	p->public.ops = ops;

	if (ops->context_size) {
		p->public.ctxt = p->context;
	}

	if (!particle_init(particles, &p->public)) {
		/* XXX FIXME this shouldn't be normal, we don't want to allocate
		 * particles that cannot be initialized.  the rockets today set a cap
		 * by failing initialization, that's silly. */
		chunker_free(p);
		return 0;
	}

	p->public.props->of_use = 1;
	list_add(&p->siblings, list);
	bsp_add_occupant(particles->bsp, &p->public.occupant, &p->props.position);

	return 1;
}


/* add a new "top-level" particle of the specified props and ops taking from the provided parts list */
int particles_add_particle(particles_t *particles, particle_props_t *props, particle_ops_t *ops)
{
	assert(particles);

	return _particles_add_particle(particles, &particles->active, props, ops);
}


/* spawn a new child particle from a parent, initializing it via inheritance if desired */
void particles_spawn_particle(particles_t *particles, particle_t *parent, particle_props_t *props, particle_ops_t *ops)
{
	_particle_t	*p = container_of(parent, _particle_t, public);

	assert(particles);
	assert(parent);

	_particles_add_particle(particles, &p->children, props ? props : parent->props, ops ? ops : parent->ops);
}


/* plural version of particle_add(); adds multiple "top-level" particles of uniform props and ops */
void particles_add_particles(particles_t *particles, particle_props_t *props, particle_ops_t *ops, int num)
{
	int	i;

	assert(particles);

	for (i = 0; i < num; i++) {
		_particles_add_particle(particles, &particles->active, props, ops);
	}
}


/* Simple accessor to get the bsp pointer, the bsp is special because we don't want to do
 * callbacks per-occupant, so the bsp_occupant_t and search functions are used directly by
 * the per-particle code needing nearest-neighbor search.  that requires an accessor since
 * particles_t is opaque.  This seemed less shitty than opening up particles_t.
 */
bsp_t * particles_bsp(particles_t *particles)
{
	assert(particles);
	assert(particles->bsp);

	return particles->bsp;
}


static inline void _particles_draw(particles_t *particles, list_head_t *list, fb_fragment_t *fragment)
{
	float		w2 = fragment->frame_width * .5f, h2 = fragment->frame_height * .5f;
	_particle_t	*p;

	assert(particles);
	assert(list);
	assert(fragment);

	list_for_each_entry(p, list, siblings) {
		int	x, y;

		/* project the 3d coordinates onto the 2d plane */
		x = (p->props.position.x / (p->props.position.z - ZCONST) * w2) + w2;
		y = (p->props.position.y / (p->props.position.z - ZCONST) * h2) + h2;

		particle_draw(particles, &p->public, x, y, fragment);

		if (!list_empty(&p->children)) {
			_particles_draw(particles, &p->children, fragment);
		}
	}
}


/* draw all of the particles, currently called in heirarchical order */
void particles_draw(particles_t *particles, fb_fragment_t *fragment)
{
	assert(particles);

	_particles_draw(particles, &particles->active, fragment);
}


static inline particle_status_t _particles_sim(particles_t *particles, list_head_t *list)
{
	particle_status_t	ret = PARTICLE_DEAD, s;
	_particle_t		*p, *_p;

	assert(particles);
	assert(list);

	list_for_each_entry_safe(p, _p, list, siblings) {
		if ((s = particle_sim(particles, &p->public)) == PARTICLE_ALIVE) {
			ret = PARTICLE_ALIVE;

			if (!list_empty(&p->children) &&
			    _particles_sim(particles, &p->children) == PARTICLE_ALIVE) {
				ret = PARTICLE_ALIVE;
			}
		} else {
			particles_reap_particle(particles, p);
		}
	}

	return ret;
}


/* simulate the particles, call the sim method of every particle in the heirarchy, this is what makes the particles dynamic */
/* if any paticle is still living, we return PARTICLE_ALIVE, to inform the caller when everything's dead */
particle_status_t particles_sim(particles_t *particles)
{
	assert(particles);

	return _particles_sim(particles, &particles->active);
}


/* "age" the particle by applying its properties for one step */
static inline void _particle_age(particles_t *particles, _particle_t *p)
{
#if 1
	if (p->props.mass > 0.0f) {
		/* gravity, TODO: mass isn't applied. */
		static v3f_t	gravity = v3f_init(0.0f, -0.05f, 0.0f);

		p->props.direction = v3f_add(&p->props.direction, &gravity);
		p->props.direction = v3f_normalize(&p->props.direction);
	}
#endif

#if 1
	/* some drag/resistance proportional to velocity TODO: integrate mass */
	if (p->props.velocity > 0.0f) {
		p->props.velocity -= ((p->props.velocity * p->props.velocity * p->props.drag));
		if (p->props.velocity < 0.0f) {
			p->props.velocity = 0;
		}
	}
#endif

	/* regular movement */
	if (p->props.velocity > 0.0f) {
		v3f_t	movement = v3f_mult_scalar(&p->props.direction, p->props.velocity);

		p->props.position = v3f_add(&p->props.position, &movement);
		bsp_move_occupant(particles->bsp, &p->public.occupant, &p->props.position);
	}
}


static void _particles_age(particles_t *particles, list_head_t *list)
{
	_particle_t	*p;

	assert(particles);
	assert(list);

	/* TODO: since this *only* involves the properties struct, if they were
	 * allocated from a separate slab containing only properties, it'd be
	 * more efficient to iterate across property arrays and skip inactive
	 * entries.  This heirarchical pointer-chasing recursion isn't
	 * particularly good for cache utilization.
	 */
	list_for_each_entry(p, list, siblings) {

		if (!p->props.virtual) {
			_particle_age(particles, p);
		}

		if (!list_empty(&p->children)) {
			_particles_age(particles, &p->children);
		}
	}
}


/* advance time for all the particles (move them), this doesn't currently invoke any part-specific helpers, it's just applying
 * physics-type stuff, moving particles according to their velocities, directions, mass, drag, gravity etc... */
void particles_age(particles_t *particles)
{
	assert(particles);

	_particles_age(particles, &particles->active);
}
© All Rights Reserved