-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrid.h
More file actions
55 lines (44 loc) · 1.04 KB
/
grid.h
File metadata and controls
55 lines (44 loc) · 1.04 KB
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
#ifndef grid_HEADER
#define grid_HEADER
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "point.h"
#include <assert.h>
#include <stdlib.h>
struct grid {
grid(point maxcorner)
: maxcorner(maxcorner), count(0) {
passable = (bool*)calloc(maxcorner[0] * maxcorner[1] * maxcorner[2],
sizeof(bool));
}
~grid() { free(passable); }
bool isPassable(point p) const {
++count;
return inBounds(p) && passable[index(p)];
}
void dig(point p) {
passable[index(p)] = true;
}
unsigned max(unsigned i) const {
return maxcorner[i];
}
void zeroCount() { count = 0; }
unsigned getCount() const { return count; }
private:
bool inBounds(point p) const {
for (unsigned i = 0; i < 3; ++i) {
if (p[i] >= maxcorner[i]) { return false; }
}
return true;
}
unsigned index(point p) const {
assert(inBounds(p));
return p[0] + maxcorner[0] * p[1]
+ maxcorner[0] * maxcorner[1] * p[2];
}
point maxcorner;
bool *passable; // 1 => passable
mutable unsigned count;
};
#endif