-
Notifications
You must be signed in to change notification settings - Fork 1
/
cells-storage.cpp
167 lines (146 loc) · 3.7 KB
/
cells-storage.cpp
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
Cell *
get_cell_from_quad(QuadTree *tree, u32 x, u32 y, b32 create_new = false)
{
Cell *cell = 0;
for (u32 cell_index = 0;
cell_index < tree->used;
++cell_index)
{
Cell *test_cell = tree->cells + cell_index;
if ((test_cell->x == x) &&
(test_cell->y == y))
{
cell = test_cell;
break;
}
}
if (create_new && !cell && tree->used != QUAD_STORE_N)
{
cell = tree->cells + tree->used++;
zero(cell, Cell);
cell->x = x;
cell->y = y;
}
return cell;
}
QuadTree *
create_tree(Memory *memory, Rectangle bounds)
{
QuadTree * tree = 0;
if (memory)
{
tree = push_struct(memory, QuadTree);
tree->bounds = bounds;
}
return tree;
}
Cell **
get_cell_from_hash(Maze *maze, u32 x, u32 y)
{
u32 hash = (7 * x + 13 * y) % CELL_CACHE_SIZE;
Cell **hash_slot = maze->cache_hash + hash;
return hash_slot;
}
Cell *
find_or_create_cell(Maze *maze, u32 x, u32 y, Memory *memory = 0)
{
QuadTree * tree = &(maze->tree);
Cell *cell = 0;
Cell *hash_cell = *get_cell_from_hash(maze, x, y);
if (hash_cell && hash_cell->x == x && hash_cell->y == y)
{
log(L_CellsStorage, u8("Got cell from hash"));
cell = hash_cell;
}
else
{
while (tree && !(cell = get_cell_from_quad(tree, x, y, memory != 0)))
{
Rectangle top_right_bounds = get_top_right(tree->bounds);
Rectangle top_left_bounds = get_top_left(tree->bounds);
Rectangle bottom_right_bounds = get_bottom_right(tree->bounds);
Rectangle bottom_left_bounds = get_bottom_left(tree->bounds);
if (in_rectangle(Vec2(x, y), top_right_bounds))
{
if (!tree->top_right)
{
tree->top_right = create_tree(memory, top_right_bounds);
}
tree = tree->top_right;
}
else if (in_rectangle(Vec2(x, y), top_left_bounds))
{
if (!tree->top_left)
{
tree->top_left = create_tree(memory, top_left_bounds);
}
tree = tree->top_left;
}
else if (in_rectangle(Vec2(x, y), bottom_right_bounds))
{
if (!tree->bottom_right)
{
tree->bottom_right = create_tree(memory, bottom_right_bounds);
}
tree = tree->bottom_right;
}
else if (in_rectangle(Vec2(x, y), bottom_left_bounds))
{
if (!tree->bottom_left)
{
tree->bottom_left = create_tree(memory, bottom_left_bounds);
}
tree = tree->bottom_left;
}
else
{
// NOTE: If it gets here: it is in old-tree but not any of it's
// subdivisions, therefore: doesn't exist.
break;
}
}
log(L_CellsStorage, u8("Got cell from quadtree"));
// Add all cells in quadtree block to hash cache
if (cell)
{
log(L_CellsStorage, u8("Putting block of cells in hash"));
for (u32 cell_index = 0;
cell_index < tree->used;
++cell_index)
{
Cell *cell_to_cache = tree->cells + cell_index;
Cell **hash_slot = get_cell_from_hash(maze, cell->x, cell->y);
*hash_slot = cell_to_cache;
}
}
}
return cell;
}
void
clear_cells_from_quadtree(QuadTree *tree)
{
if (tree)
{
tree->used = 0;
clear_cells_from_quadtree(tree->top_right);
clear_cells_from_quadtree(tree->top_left);
clear_cells_from_quadtree(tree->bottom_right);
clear_cells_from_quadtree(tree->bottom_left);
}
}
void
clear_maze(Maze *maze)
{
clear_cells_from_quadtree(&maze->tree);
zero_n(&maze->cache_hash, Cell*, CELL_CACHE_SIZE);
}
Cell *
get_cell(Maze *maze, u32 x, u32 y)
{
return find_or_create_cell(maze, x, y);
}
Cell *
create_new_cell(Maze *maze, u32 x, u32 y, Memory *memory)
{
return find_or_create_cell(maze, x, y, memory);
}