-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVillageJSON.cpp
More file actions
99 lines (68 loc) · 2.91 KB
/
VillageJSON.cpp
File metadata and controls
99 lines (68 loc) · 2.91 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
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
#include <VillageJSON.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#define UNUSED(x) (void)(x)
void GenerateVillageJSON(MinecraftWorld &world, std::string filename)
{
boost::property_tree::ptree tree;
tree.put("type", "FeatureCollection");
boost::property_tree::ptree features;
for(auto it : world.villages) {
VillageType &village = it.second;
std::cout << "writing village json\n";
std::cout << village.get_string() << "\n";
std::cout << "dwellers:\n";
std::map<std::string, int> dcount;
for(long villagerid : village.get_dwellers()) {
std::optional<EntityType *> dweller = world.get_entity_by_id(villagerid);
if (!dweller) {
std::cout << "couldn't find dweller with id: " << villagerid << "\n";
continue;
}
std::cout << " " << villagerid << " " << (*dweller)->get_type() << "\n";
dcount[(*dweller)->get_type()]++;
}
boost::property_tree::ptree feature_node;
feature_node.put("type", "Feature");
feature_node.put("properties.name", village.get_name());
for (auto it : dcount) {
feature_node.put("properties.counts." + it.first, it.second);
}
// this seems like a goofy way to have a list.
boost::property_tree::ptree coords;
boost::property_tree::ptree coord;
auto bbox = village.get_bbox();
coords.clear();
for(int i : {bbox.xl, bbox.yl, bbox.zl, bbox.xh, bbox.yh, bbox.zh}) {
coord.put("", i);
coords.push_back(std::make_pair("", coord));
}
feature_node.add_child("properties.bounds", coords);
std::vector<std::pair<int,int> > rectpts = {
{ bbox.xl, bbox.zl },
{ bbox.xh, bbox.zl },
{ bbox.xh, bbox.zh },
{ bbox.xl, bbox.zh },
{ bbox.xl, bbox.zl }
};
boost::property_tree::ptree polygon;
for (std::pair<int,int> c : rectpts) {
coords.clear();
coord.put("", c.first);
coords.push_back(std::make_pair("", coord));
coord.put("", c.second);
coords.push_back(std::make_pair("", coord));
polygon.push_back(std::make_pair("", coords));
}
// geojson wants an array of first the polygon followed by the holes.
// in our case, we have no holes.
boost::property_tree::ptree polyholepair;
polyholepair.push_back(std::make_pair("", polygon));
feature_node.put("geometry.type", "Polygon");
feature_node.add_child("geometry.coordinates", polyholepair);
features.push_back(std::make_pair("", feature_node));
}
tree.add_child("features", features);
boost::property_tree::write_json(filename, tree);
std::cout << "wrote " << world.villages.size() << " block entities\n";
}