-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcars-storage.h
68 lines (52 loc) · 1.17 KB
/
cars-storage.h
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
const u32 CARS_PER_BLOCK = 512;
// Car storage needs to:
// - Add car
// - Remove car (Remove all cars with marker)
// - Loop through all cars
//
// Therefore linked list of blocks, with free list for deallocated blocks.
// - When adding a car:
// - Loop through blocks until found one with space
// - If all blocks full
// - Try get block from free_chain
// - Else allocate new block
// - Add to start of chain
// - When removing a car:
// - Set dead flag
// - update_dead_cars routine:
// - For all cars with dead flag:
// - If no cars left in block
// - Add to start of free_chain
// - Re-link previous block to next block
struct Car
{
b32 update_next_frame;
b32 dead;
u32 id;
s32 value;
WorldSpace cell_pos;
vec2 direction;
u32 pause_left;
vec2 unpause_direction;
CellType updated_cell_type;
ParticleSource *particle_source;
};
struct CarsBlock
{
Car cars[CARS_PER_BLOCK];
u32 next_free_in_block;
CarsBlock *next_block;
#ifdef DEBUG_BLOCK_COLORS
vec4 c;
#endif
};
struct Cars
{
CarsBlock *first_block;
CarsBlock *free_chain;
};
struct CarsIterator
{
CarsBlock *cars_block;
u32 car_index;
};