aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/VillageFeature.cpp
blob: 5249256cd2a1ca2d9ee2def4f60986ce87d684f4 (plain)
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
#include "stdafx.h"
#include "VillageFeature.h"
#include "VillagePieces.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.biome.h"
#include "net.minecraft.world.level.dimension.h"

const wstring VillageFeature::OPTION_SIZE_MODIFIER = L"size";
const wstring VillageFeature::OPTION_SPACING = L"distance";

vector<Biome *> VillageFeature::allowedBiomes;

void VillageFeature::staticCtor()
{
	allowedBiomes.push_back( Biome::plains );
	allowedBiomes.push_back( Biome::desert );
}

void VillageFeature::_init(int iXZSize)
{
	villageSizeModifier = 0;
	townSpacing = 32;
	minTownSeparation = 8;

	m_iXZSize=iXZSize;
}

VillageFeature::VillageFeature(int iXZSize)
{
	_init(iXZSize);
}

VillageFeature::VillageFeature(unordered_map<wstring, wstring> options, int iXZSize)
{
	_init(iXZSize);

	for (auto& option : options)
	{
		if (option.first.compare(OPTION_SIZE_MODIFIER) == 0)
		{
			villageSizeModifier = Mth::getInt(option.second, villageSizeModifier, 0);
		}
		else if (option.first.compare(OPTION_SPACING) == 0)
		{
			townSpacing = Mth::getInt(option.second, townSpacing, minTownSeparation + 1);
		}
	}
}

wstring VillageFeature::getFeatureName()
{
	return L"Village";
}

bool VillageFeature::isFeatureChunk(int x, int z,bool bIsSuperflat)
{
	int townSpacing = this->townSpacing;

	if(!bIsSuperflat
#ifdef _LARGE_WORLDS
		&& level->dimension->getXZSize() < 128
#endif
		)
		{
			townSpacing= 16;// 4J change 32;
		}

		int xx = x;
		int zz = z;
		if (x < 0) x -= townSpacing - 1;
		if (z < 0) z -= townSpacing - 1;

		int xCenterTownChunk = x / townSpacing;
		int zCenterTownChunk = z / townSpacing;
		Random *r = level->getRandomFor(xCenterTownChunk, zCenterTownChunk, 10387312);
		xCenterTownChunk *= townSpacing;
		zCenterTownChunk *= townSpacing;
		xCenterTownChunk += r->nextInt(townSpacing - minTownSeparation);
		zCenterTownChunk += r->nextInt(townSpacing - minTownSeparation);
		x = xx;
		z = zz;

		bool forcePlacement = false;
		LevelGenerationOptions *levelGenOptions = app.getLevelGenerationOptions();
		if( levelGenOptions != NULL )
		{
			forcePlacement = levelGenOptions->isFeatureChunk(x,z,eFeature_Village);
		}

		if (forcePlacement || (x == xCenterTownChunk && z == zCenterTownChunk) )
		{
			bool biomeOk = level->getBiomeSource()->containsOnly(x * 16 + 8, z * 16 + 8, 0, allowedBiomes);
			if (biomeOk)
			{
				//app.DebugPrintf("Biome ok for Village at %d, %d\n",(x * 16 + 8),(z * 16 + 8));
				return true;
			}
		}

		return false;
}

StructureStart *VillageFeature::createStructureStart(int x, int z)
{
	// 4J added
	app.AddTerrainFeaturePosition(eTerrainFeature_Village,x,z);

	return new VillageStart(level, random, x, z, villageSizeModifier, m_iXZSize);
}

VillageFeature::VillageStart::VillageStart()
{
	valid = false;	// 4J added initialiser
	m_iXZSize = 0;
	// for reflection
}

VillageFeature::VillageStart::VillageStart(Level *level, Random *random, int chunkX, int chunkZ, int villageSizeModifier, int iXZSize)
{
	valid = false;	// 4J added initialiser
	m_iXZSize=iXZSize;

	list<VillagePieces::PieceWeight *> *pieceSet = VillagePieces::createPieceSet(random, villageSizeModifier);

	VillagePieces::StartPiece *startRoom = new VillagePieces::StartPiece(level->getBiomeSource(), 0, random, (chunkX << 4) + 2, (chunkZ << 4) + 2, pieceSet, villageSizeModifier, level);
	pieces.push_back(startRoom);
	startRoom->addChildren(startRoom, &pieces, random);

	vector<StructurePiece *> *pendingRoads = &startRoom->pendingRoads;
	vector<StructurePiece *> *pendingHouses = &startRoom->pendingHouses;
	while (!pendingRoads->empty() || !pendingHouses->empty())
	{

		// prioritize roads
		if (pendingRoads->empty())
		{
			int pos = random->nextInt((int)pendingHouses->size());
            auto it = pendingHouses->begin() + pos;
            StructurePiece *structurePiece = *it;
			pendingHouses->erase(it);
			structurePiece->addChildren(startRoom, &pieces, random);
		}
		else
		{
			int pos = random->nextInt((int)pendingRoads->size());
            auto it = pendingRoads->begin() + pos;
            StructurePiece *structurePiece = *it;
			pendingRoads->erase(it);
			structurePiece->addChildren(startRoom, &pieces, random);
		}
	}

	calculateBoundingBox();

	size_t count = 0;
	for(auto& piece : pieces)
	{
		if ( piece && dynamic_cast<VillagePieces::VillageRoadPiece *>(piece) )
		{
			++count;
		}
	}
	valid = count > 2;
}

bool VillageFeature::VillageStart::isValid()
{
	// 4J-PB - Adding a bounds check to ensure a village isn't over the edge of our world - we end up with half houses in that case
	if((boundingBox->x0<(-m_iXZSize/2)) || (boundingBox->x1>(m_iXZSize/2)) || (boundingBox->z0<(-m_iXZSize/2)) || (boundingBox->z1>(m_iXZSize/2)))
	{
		valid=false;
	}
	return valid;
}

void VillageFeature::VillageStart::addAdditonalSaveData(CompoundTag *tag)
{
	StructureStart::addAdditonalSaveData(tag);

	tag->putBoolean(L"Valid", valid);
}

void VillageFeature::VillageStart::readAdditonalSaveData(CompoundTag *tag)
{
	StructureStart::readAdditonalSaveData(tag);
	valid = tag->getBoolean(L"Valid");
}