forked from mmccoo/minecraft_mmccoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubChunk.h
122 lines (99 loc) · 3.21 KB
/
SubChunk.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
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
#ifndef SUB_CHUNK_HH
#define SUB_CHUNK_HH
#include <string>
#include <cstring>
#include <map>
#include <sstream>
#include <cassert>
#include <iostream>
#include <BlockType.h>
class SubChunk {
struct Iter {
private:
struct Location {
Location(int x, int y, int z, uint8_t type) : x(x), y(y), z(z), type(type) { /* empty */ };
int x;
int y;
int z;
uint8_t type;
};
int curx;
int cury;
int curz;
int type_filter;
SubChunk *subchunk;
public:
Iter(int tf, SubChunk *sc) : curx(0), cury(0), curz(0), type_filter(tf), subchunk(sc) { /* empty */ }
static Iter get_end() {
Iter retval(-1, 0x0);
retval.curx = retval.cury = retval.curz = -1;
return retval;
};
// Previously provided by std::iterator - see update below
typedef Location value_type;
typedef std::ptrdiff_t difference_type;
typedef Location* pointer;
typedef Location& reference;
typedef std::input_iterator_tag iterator_category;
Location operator*() const {
assert(curx>=0 && cury>=0 && curz>=0 && subchunk);
return Location(subchunk->chunkx*16+curx, subchunk->chunky*16+cury, subchunk->chunkz*16+curz,
subchunk->get_type_at(curx,cury,curz));
}
bool operator==(const Iter& other) const {
return (other.curx == curx) && (other.cury == cury) && (other.curz == curz);
}
bool operator!=(const Iter& other) const { return !(*this == other); }
#if 0
// when do I have to have post incr?
intholder operator++(int) {
intholder ret(value_);
++*this;
return ret;
}
#endif
Iter& operator++() {
if ((curx==-1) || (cury==-1) || (curz==-1)) { curx = cury = curz = -1; }
while (1) {
curz++;
if (curz>=16) {
curz = 0;
curx++;
if (curx >= 16) {
curx = 0;
cury++;
if (cury>=16) {
curx = cury = curz = -1;
break;
}
}
}
//std::cout << "looing in " << curx << "," << cury << "," << curz << "\n";
if (type_filter != -1) {
if (type_filter != subchunk->get_type_at(curx,cury,curz)) {
continue;
}
}
break;
}
return *this;
};
};
public:
SubChunk();
int chunkx;
int chunky;
int chunkz;
uint8_t block_type_ids[16][16][16];
void set_type_at(int x, int y, int z, uint8_t type);
uint8_t get_type_at(int x, int y, int z);
Iter begin(int type_filter=-1) {
Iter retval(type_filter, this);
if (type_filter != -1 && (*retval).type != type_filter) {
++retval;
}
return retval;
}
Iter end() { return Iter::get_end(); }
};
#endif