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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
#include "stdafx.h"
#include "StructureFeature.h"
#include "StructureStart.h"
#include "StructurePiece.h"
#include "ChunkPos.h"
#include "BoundingBox.h"
#include "net.minecraft.world.level.h"
#include "LevelData.h"
StructureFeature::StructureFeature()
{
#ifdef ENABLE_STRUCTURE_SAVING
savedData = nullptr;
#endif
}
StructureFeature::~StructureFeature()
{
for(auto& it : cachedStructures)
{
delete it.second;
}
}
void StructureFeature::addFeature(Level *level, int x, int z, int xOffs, int zOffs, byteArray blocks)
{
// this method is called for each chunk within 8 chunk's distance from
// the chunk being generated, but not all chunks are the sources of
// structures
restoreSavedData(level);
if (cachedStructures.find(ChunkPos::hashCode(x, z)) != cachedStructures.end())
{
return;
}
// clear random key
random->nextInt();
// 4J-PB - want to know if it's a superflat land, so we don't generate so many villages - we've changed the distance required between villages on the xbox
if (isFeatureChunk(x, z,level->getLevelData()->getGenerator() == LevelType::lvl_flat))
{
StructureStart *start = createStructureStart(x, z);
cachedStructures[ChunkPos::hashCode(x, z)] = start;
saveFeature(x, z, start);
}
}
bool StructureFeature::postProcess(Level *level, Random *random, int chunkX, int chunkZ)
{
restoreSavedData(level);
// 4J Stu - The x and z used to be offset by (+8) here, but that means we can miss out half structures on the edge of the world
// Normal feature generation offsets generation by half a chunk to ensure that it can generate the entire feature in chunks already created
// Structure features don't need this, as the PlaceBlock function only places blocks inside the BoundingBox specified, and parts
// of a struture piece can be added in more than one post-process call
int cx = (chunkX << 4); // + 8;
int cz = (chunkZ << 4); // + 8;
bool intersection = false;
for(auto& it : cachedStructures)
{
StructureStart *structureStart = it.second;
if (structureStart->isValid())
{
if (structureStart->getBoundingBox()->intersects(cx, cz, cx + 15, cz + 15))
{
BoundingBox *bb = new BoundingBox(cx, cz, cx + 15, cz + 15);
structureStart->postProcess(level, random, bb);
delete bb;
intersection = true;
// because some feature pieces are modified in the postProcess step, we need to save them again
saveFeature(structureStart->getChunkX(), structureStart->getChunkZ(), structureStart);
}
}
}
return intersection;
}
bool StructureFeature::isIntersection(int cellX, int cellZ)
{
restoreSavedData(level);
for(auto & it : cachedStructures)
{
StructureStart *structureStart = it.second;
if (structureStart->isValid())
{
if (structureStart->getBoundingBox()->intersects(cellX, cellZ, cellX, cellZ))
{
auto it2 = structureStart->getPieces()->begin();
while( it2 != structureStart->getPieces()->end() )
{
StructurePiece *next = *it2++;
if (next->getBoundingBox()->intersects(cellX, cellZ, cellX, cellZ))
{
return true;
}
}
}
}
}
return false;
}
bool StructureFeature::isInsideFeature(int cellX, int cellY, int cellZ)
{
restoreSavedData(level);
return getStructureAt(cellX, cellY, cellZ) != NULL;
}
StructureStart *StructureFeature::getStructureAt(int cellX, int cellY, int cellZ)
{
for(auto& it : cachedStructures)
{
StructureStart *pStructureStart = it.second;
if (pStructureStart->isValid())
{
if (pStructureStart->getBoundingBox()->intersects(cellX, cellZ, cellX, cellZ))
{
/*
Iterator<StructurePiece> it = structureStart.getPieces().iterator();
while (it.hasNext()) {
StructurePiece next = it.next();
if (next.getBoundingBox().isInside(cellX, cellY, cellZ)) {
return true;
}
*/
list<StructurePiece *> *pieces=pStructureStart->getPieces();
for (auto& piece : *pieces)
{
if ( piece->getBoundingBox()->isInside(cellX, cellY, cellZ) )
{
return pStructureStart;
}
}
}
}
}
return NULL;
}
bool StructureFeature::isInsideBoundingFeature(int cellX, int cellY, int cellZ)
{
restoreSavedData(level);
for(auto& it : cachedStructures)
{
StructureStart *structureStart = it.second;
if (structureStart->isValid())
{
return (structureStart->getBoundingBox()->intersects(cellX, cellZ, cellX, cellZ));
}
}
return false;
}
TilePos *StructureFeature::getNearestGeneratedFeature(Level *level, int cellX, int cellY, int cellZ)
{
// this is a hack that will "force" the feature to generate positions
// even if the player hasn't generated new chunks yet
this->level = level;
restoreSavedData(level);
random->setSeed(level->getSeed());
int64_t xScale = random->nextLong();
int64_t zScale = random->nextLong();
int64_t xx = (cellX >> 4) * xScale;
int64_t zz = (cellZ >> 4) * zScale;
random->setSeed(xx ^ zz ^ level->getSeed());
addFeature(level, cellX >> 4, cellZ >> 4, 0, 0, byteArray());
double minDistance = DBL_MAX;
TilePos *selected = NULL;
for(auto& it : cachedStructures)
{
StructureStart *pStructureStart = it.second;
if (pStructureStart->isValid())
{
//StructurePiece *pStructurePiece = pStructureStart->getPieces().get(0);
StructurePiece* pStructurePiece = * pStructureStart->getPieces()->begin();
TilePos *locatorPosition = pStructurePiece->getLocatorPosition();
int dx = locatorPosition->x - cellX;
int dy = locatorPosition->y - cellY;
int dz = locatorPosition->z - cellZ;
double dist = dx * dx + dy * dy + dz * dz;
if (dist < minDistance)
{
minDistance = dist;
selected = locatorPosition;
}
}
}
if (selected != NULL)
{
return selected;
}
else
{
vector<TilePos> *guesstimatedFeaturePositions = getGuesstimatedFeaturePositions();
if (guesstimatedFeaturePositions != NULL)
{
TilePos *pSelectedPos = new TilePos(0,0,0);
for(const auto& it : *guesstimatedFeaturePositions)
{
int dx = it.x - cellX;
int dy = it.y - cellY;
int dz = it.z - cellZ;
double dist = dx * dx + dy * dy + dz * dz;
if (dist < minDistance)
{
minDistance = dist;
pSelectedPos->x = it.x;
pSelectedPos->y = it.y;
pSelectedPos->z = it.z;
}
}
delete guesstimatedFeaturePositions;
return pSelectedPos;
}
}
return NULL;
}
vector<TilePos> *StructureFeature::getGuesstimatedFeaturePositions()
{
return NULL;
}
void StructureFeature::restoreSavedData(Level *level)
{
#ifdef ENABLE_STRUCTURE_SAVING
if (savedData == NULL)
{
savedData = dynamic_pointer_cast<StructureFeatureSavedData>( level->getSavedData(typeid(StructureFeatureSavedData), getFeatureName()) );
if (savedData == NULL)
{
savedData = shared_ptr<StructureFeatureSavedData>( new StructureFeatureSavedData(getFeatureName()) );
level->setSavedData(getFeatureName(), savedData);
}
else
{
CompoundTag *fullTag = savedData->getFullTag();
vector<Tag *> *allTags = fullTag->getAllTags();
for ( Tag *featureTag : *allTags )
{
if ( featureTag && featureTag->getId() == Tag::TAG_Compound)
{
CompoundTag *ct = (CompoundTag *) featureTag;
if (ct->contains(L"ChunkX") && ct->contains(L"ChunkZ"))
{
int cx = ct->getInt(L"ChunkX");
int cz = ct->getInt(L"ChunkZ");
StructureStart *start = StructureFeatureIO::loadStaticStart(ct, level);
// System.out.println("Loaded " + start.getClass().getSimpleName() + " from file");
cachedStructures[ChunkPos::hashCode(cx, cz)] = start;
}
}
}
delete allTags;
}
}
#endif
}
void StructureFeature::saveFeature(int chunkX, int chunkZ, StructureStart *feature)
{
#ifdef ENABLE_STRUCTURE_SAVING
savedData->putFeatureTag(feature->createTag(chunkX, chunkZ), chunkX, chunkZ);
savedData->setDirty();
#endif
}
|