-
Notifications
You must be signed in to change notification settings - Fork 8
/
CaveVectors.cpp
103 lines (73 loc) · 2.95 KB
/
CaveVectors.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
#include <CaveVectors.h>
#include <sstream>
#include <fstream>
//https://github.com/nlohmann/json#json-as-first-class-data-type
// this json header is copied from the above github. I don't know how to include
// a single file as a submodule and the git report itself is pretty large.
#include <nlohmann/json.hpp>
CaveVectors::CaveVectors(MinecraftWorld& world)
{
for (auto scix : world.chunks_by_y) {
int chunkx = scix.first;
UNUSED(chunkx);
for (auto sciz : scix.second) {
int chunkz = sciz.first;
UNUSED(chunkz);
Grid16 &chunk_top_earthly = world.top_earthly[chunkx][chunkz];
for (auto sciy : sciz.second) {
int chunky = sciy.first;
UNUSED(chunky);
auto sc = sciy.second;
for (auto iter=sc->begin(); iter!=sc->end(); ++iter) {
auto loc = *iter;
// The loc has real world coords.
int rawx = loc.x - chunkx*16;
int rawz = loc.z - chunkz*16;
if (loc.y >= chunk_top_earthly[rawx][rawz]) { continue; }
BlockType bt = BlockType::get_block_type_by_id(loc.type);
if (bt.is_air() || bt.is_liquid()) {
add_rectangle_to_polygon_set(polysets[loc.y/world.y_resolution], loc.x, loc.z, loc.x+1, loc.z+1);
}
}
}
}
}
}
void CaveVectors::write(std::string filename)
{
nlohmann::json top;
top["type"] = "FeatureCollection";
PolygonHolesSet prev;
for(auto iter = polysets.rbegin(); iter!=polysets.rend(); iter++) {
int elevation = (*iter).first;
// we're going from upper elevations on down. each layer, merge in the polys from above.
PolygonHolesSet &ps = (*iter).second;
PolygonHolesSet merged;
#if 0
prev.insert(prev.end(), ps.begin(), ps.end());
std::cout << "for elevation: " << elevation << " we have " << prev.size() << " going in and ";
gtl::assign(merged, prev);
// can't I just assign above?
prev = merged;
std::cout << merged.size() << " coming out\n" << std::flush;
#else
std::cout << "for elevation: " << elevation << " we have " << ps.size() << " going in and ";
gtl::assign(merged, ps);
std::cout << merged.size() << " coming out\n" << std::flush;
#endif
for (auto poly : merged) {
std::optional<nlohmann::json> coords = polygon_to_json(poly);
if (!coords) { continue; }
nlohmann::json feature;
feature["type"] = "Feature";
feature["properties"]["elevation"] = elevation;
feature["geometry"]["type"] = "Polygon";
feature["geometry"]["coordinates"] = *coords;
top["features"].push_back(feature);
}
}
std::ofstream file;
file.open(filename);
file << top.dump(2);
file.close();
}