the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "net.minecraft.world.entity.ai.village.h"
3#include "net.minecraft.world.level.h"
4#include "net.minecraft.world.level.tile.h"
5#include "net.minecraft.world.phys.h"
6#include "BasicTypeContainers.h"
7#include "Villages.h"
8
9const wstring Villages::VILLAGE_FILE_ID = L"villages";
10
11Villages::Villages(const wstring &id) : SavedData(id)
12{
13 _tick = 0;
14 level = NULL;
15}
16
17Villages::Villages(Level *level) : SavedData(VILLAGE_FILE_ID)
18{
19 _tick = 0;
20 this->level = level;
21}
22
23Villages::~Villages()
24{
25 for(AUTO_VAR(it,queries.begin()); it != queries.end(); ++it) delete *it;
26}
27
28void Villages::setLevel(Level *level)
29{
30 this->level = level;
31
32 //for (Village village : villages)
33 for(AUTO_VAR(it, villages.begin()); it != villages.end(); ++it)
34 {
35 shared_ptr<Village> village = *it;
36 village->setLevel(level);
37 }
38}
39
40void Villages::queryUpdateAround(int x, int y, int z)
41{
42 if (queries.size() > 64) return;
43 if (!hasQuery(x, y, z)) queries.push_back(new Pos(x, y, z));
44}
45
46void Villages::tick()
47{
48 ++_tick;
49 //for (Village village : villages)
50 for(AUTO_VAR(it, villages.begin()); it != villages.end(); ++it)
51 {
52 shared_ptr<Village> village = *it;
53 village->tick(_tick);
54 }
55 removeVillages();
56 processNextQuery();
57 cluster();
58
59 if ((_tick % 400) == 0)
60 {
61 setDirty();
62 }
63}
64
65void Villages::removeVillages()
66{
67 for(AUTO_VAR(it, villages.begin()); it != villages.end(); )
68 {
69 shared_ptr<Village> village = *it;
70 if (village->canRemove())
71 {
72 it = villages.erase(it);
73 setDirty();
74 }
75 else
76 {
77 ++it;
78 }
79 }
80}
81
82vector<shared_ptr<Village> > *Villages::getVillages()
83{
84 return &villages;
85}
86
87shared_ptr<Village> Villages::getClosestVillage(int x, int y, int z, int maxDist)
88{
89 shared_ptr<Village> closest = nullptr;
90 float closestDistSqr = Float::MAX_VALUE;
91 for(AUTO_VAR(it, villages.begin()); it != villages.end(); ++it)
92 {
93 shared_ptr<Village> village = *it;
94 float distSqr = village->getCenter()->distSqr(x, y, z);
95 if (distSqr >= closestDistSqr) continue;
96
97 float requiredDist = maxDist + village->getRadius();
98 if (distSqr > requiredDist * requiredDist) continue;
99
100 closest = village;
101 closestDistSqr = distSqr;
102 }
103 return closest;
104}
105
106void Villages::processNextQuery()
107{
108 if (queries.empty()) return;
109 Pos *q = queries.front();
110 queries.pop_front();
111 addDoorInfos(q);
112 delete q;
113}
114
115void Villages::cluster()
116{
117 // note doesn't merge or split existing villages
118 for(AUTO_VAR(it, unclustered.begin()); it != unclustered.end(); ++it)
119 {
120 shared_ptr<DoorInfo> di = *it;
121
122 bool found = false;
123 for(AUTO_VAR(itV, villages.begin()); itV != villages.end(); ++itV)
124 {
125 shared_ptr<Village> village = *itV;
126 int dist = (int) village->getCenter()->distSqr(di->x, di->y, di->z);
127 int radius = MaxDoorDist + village->getRadius();
128 if (dist > radius * radius) continue;
129 village->addDoorInfo(di);
130 found = true;
131 break;
132 }
133 if (found) continue;
134
135 // create new Village
136 shared_ptr<Village> village = shared_ptr<Village>(new Village(level));
137 village->addDoorInfo(di);
138 villages.push_back(village);
139 setDirty();
140 }
141 unclustered.clear();
142}
143
144void Villages::addDoorInfos(Pos *pos)
145{
146 int scanX = 16, scanY = 4, scanZ = 16;
147 for (int xx = pos->x - scanX; xx < pos->x + scanX; xx++)
148 {
149 for (int yy = pos->y - scanY; yy < pos->y + scanY; yy++)
150 {
151 for (int zz = pos->z - scanZ; zz < pos->z + scanZ; zz++)
152 {
153 if (isDoor(xx, yy, zz))
154 {
155 shared_ptr<DoorInfo> currentDoor = getDoorInfo(xx, yy, zz);
156 if (currentDoor == NULL) createDoorInfo(xx, yy, zz);
157 else currentDoor->timeStamp = _tick;
158 }
159 }
160 }
161 }
162}
163
164shared_ptr<DoorInfo> Villages::getDoorInfo(int x, int y, int z)
165{
166 //for (DoorInfo di : unclustered)
167 for(AUTO_VAR(it,unclustered.begin()); it != unclustered.end(); ++it)
168 {
169 shared_ptr<DoorInfo> di = *it;
170 if (di->x == x && di->z == z && abs(di->y - y) <= 1) return di;
171 }
172 //for (Village v : villages)
173 for(AUTO_VAR(it,villages.begin()); it != villages.end(); ++it)
174 {
175 shared_ptr<Village> v = *it;
176 shared_ptr<DoorInfo> di = v->getDoorInfo(x, y, z);
177 if (di != NULL) return di;
178 }
179 return nullptr;
180}
181
182void Villages::createDoorInfo(int x, int y, int z)
183{
184 int dir = ((DoorTile *) Tile::door_wood)->getDir(level, x, y, z);
185 if (dir == 0 || dir == 2)
186 {
187 int canSeeX = 0;
188 for (int i = -5; i < 0; ++i)
189 if (level->canSeeSky(x + i, y, z)) canSeeX--;
190 for (int i = 1; i <= 5; ++i)
191 if (level->canSeeSky(x + i, y, z)) canSeeX++;
192 if (canSeeX != 0) unclustered.push_back(shared_ptr<DoorInfo>(new DoorInfo(x, y, z, canSeeX > 0 ? -2 : 2, 0, _tick)));
193 }
194 else
195 {
196 int canSeeZ = 0;
197 for (int i = -5; i < 0; ++i)
198 if (level->canSeeSky(x, y, z + i)) canSeeZ--;
199 for (int i = 1; i <= 5; ++i)
200 if (level->canSeeSky(x, y, z + i)) canSeeZ++;
201 if (canSeeZ != 0) unclustered.push_back(shared_ptr<DoorInfo>(new DoorInfo(x, y, z, 0, canSeeZ > 0 ? -2 : 2, _tick)));
202 }
203}
204
205bool Villages::hasQuery(int x, int y, int z)
206{
207 //for (Pos pos : queries)
208 for(AUTO_VAR(it, queries.begin()); it != queries.end(); ++it)
209 {
210 Pos *pos = *it;
211 if (pos->x == x && pos->y == y && pos->z == z) return true;
212 }
213 return false;
214}
215
216bool Villages::isDoor(int x, int y, int z)
217{
218 int tileId = level->getTile(x, y, z);
219 return tileId == Tile::door_wood_Id;
220}
221
222void Villages::load(CompoundTag *tag)
223{
224 _tick = tag->getInt(L"Tick");
225 ListTag<CompoundTag> *villageTags = (ListTag<CompoundTag> *) tag->getList(L"Villages");
226 for (int i = 0; i < villageTags->size(); i++)
227 {
228 CompoundTag *compoundTag = villageTags->get(i);
229 shared_ptr<Village> village = shared_ptr<Village>(new Village());
230 village->readAdditionalSaveData(compoundTag);
231 villages.push_back(village);
232 }
233}
234
235void Villages::save(CompoundTag *tag)
236{
237 tag->putInt(L"Tick", _tick);
238 ListTag<CompoundTag> *villageTags = new ListTag<CompoundTag>(L"Villages");
239 //for (Village village : villages)
240 for(AUTO_VAR(it, villages.begin()); it != villages.end(); ++it)
241 {
242 shared_ptr<Village> village = *it;
243 CompoundTag *villageTag = new CompoundTag(L"Village");
244 village->addAdditonalSaveData(villageTag);
245 villageTags->add(villageTag);
246 }
247 tag->put(L"Villages", villageTags);
248}