#include "stdafx.h" #include "net.minecraft.world.level.h" #include "net.minecraft.world.level.biome.h" #include "net.minecraft.world.level.levelgen.h" #include "net.minecraft.world.level.levelgen.feature.h" #include "net.minecraft.world.level.levelgen.structure.h" #include "net.minecraft.world.level.levelgen.synth.h" #include "net.minecraft.world.level.tile.h" #include "net.minecraft.world.level.storage.h" #include "net.minecraft.world.entity.h" #include "FlatLevelSource.h" #include "OceanMonumentFeature.h" FlatLevelSource::FlatLevelSource(Level *level, int64_t seed, bool generateStructures) { m_XZSize = level->getLevelData()->getXZSize(); this->level = level; this->generateStructures = generateStructures; this->random = new Random(seed); this->pprandom = new Random(seed); generatorInfo = FlatGeneratorInfo::fromValue(level->getLevelData()->getGeneratorOptions()); memset(layerIds, 0, sizeof(layerIds)); memset(layerData, 0, sizeof(layerData)); hasLayerData = false; int seaLevel = 0; int airRun = 0; for (auto &layer : *generatorInfo.getLayers()) { for (int y = layer.getStart(); y < layer.getStart() + layer.getHeight() && y < FlatGeneratorInfo::MAX_HEIGHT; ++y) { layerIds[y] = static_cast(layer.getId()); layerData[y] = static_cast(layer.getData() & 15); if (layerData[y] != 0) hasLayerData = true; } if (layer.getId() == 0) { airRun += layer.getHeight(); } else { seaLevel += layer.getHeight() + airRun; airRun = 0; } } level->seaLevel = seaLevel; caveFeature = generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_CAVES) ? new LargeCaveFeature() : nullptr; canyonFeature = generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_RAVINES) ? new CanyonFeature() : nullptr; auto *structures = generatorInfo.getStructures(); villageFeature = nullptr; strongholdFeature = nullptr; mineShaftFeature = nullptr; scatteredFeature = nullptr; oceanMonument = nullptr; if (generateStructures) { if (generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_VILLAGE)) villageFeature = new VillageFeature((*structures)[FlatGeneratorInfo::STRUCTURE_VILLAGE], m_XZSize); if (generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_STRONGHOLD)) strongholdFeature = new StrongholdFeature((*structures)[FlatGeneratorInfo::STRUCTURE_STRONGHOLD]); if (generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_MINESHAFT)) mineShaftFeature = new MineShaftFeature((*structures)[FlatGeneratorInfo::STRUCTURE_MINESHAFT]); if (generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_BIOME_SPECIFIC)) scatteredFeature = new RandomScatteredLargeFeature((*structures)[FlatGeneratorInfo::STRUCTURE_BIOME_SPECIFIC]); if (generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_OCEAN_MONUMENT)) oceanMonument = new OceanMonumentFeature((*structures)[FlatGeneratorInfo::STRUCTURE_OCEAN_MONUMENT]); } decorations = generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_BIOME_DECORATION); dungeons = generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_DUNGEON); lakes = generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_LAKE); lavaLakes = generatorInfo.hasStructure(FlatGeneratorInfo::STRUCTURE_LAVA_LAKE); } FlatLevelSource::~FlatLevelSource() { delete random; delete pprandom; delete caveFeature; delete canyonFeature; delete villageFeature; delete strongholdFeature; delete mineShaftFeature; delete scatteredFeature; delete oceanMonument; } void FlatLevelSource::prepareHeights(byteArray blocks) { int height = blocks.length / (16 * 16); if (height > FlatGeneratorInfo::MAX_HEIGHT) height = FlatGeneratorInfo::MAX_HEIGHT; for (int xc = 0; xc < 16; xc++) { for (int zc = 0; zc < 16; zc++) { for (int yc = 0; yc < height; yc++) { blocks[xc << 11 | zc << 7 | yc] = static_cast(layerIds[yc]); } } } } LevelChunk *FlatLevelSource::create(int x, int z) { return getChunk(x, z); } LevelChunk *FlatLevelSource::getChunk(int xOffs, int zOffs) { random->setSeed(xOffs * 341873128712l + zOffs * 132897987541l); int chunksSize = Level::genDepth * 16 * 16; byte *tileData = static_cast(XPhysicalAlloc(chunksSize, MAXULONG_PTR, 4096, PAGE_READWRITE)); XMemSet128(tileData,0,chunksSize); byteArray blocks = byteArray(tileData,chunksSize); prepareHeights(blocks); if (caveFeature != nullptr) caveFeature->apply(this, level, xOffs, zOffs, blocks); if (canyonFeature != nullptr) canyonFeature->apply(this, level, xOffs, zOffs, blocks); if (mineShaftFeature != nullptr) mineShaftFeature->apply(this, level, xOffs, zOffs, blocks); if (villageFeature != nullptr) villageFeature->apply(this, level, xOffs, zOffs, blocks); if (strongholdFeature != nullptr) strongholdFeature->apply(this, level, xOffs, zOffs, blocks); if (scatteredFeature != nullptr) scatteredFeature->apply(this, level, xOffs, zOffs, blocks); if (oceanMonument != nullptr) oceanMonument->apply(this, level, xOffs, zOffs, blocks); byteArray nibbleData; if (hasLayerData) { nibbleData = byteArray(chunksSize); memset(nibbleData.data, 0, chunksSize); for (int z = 0; z < 16; ++z) { for (int x = 0; x < 16; ++x) { for (int y = 0; y < FlatGeneratorInfo::MAX_HEIGHT && y < Level::genDepth; ++y) { int rawIdx = (x << 11) | (z << 7) | y; byte val = layerData[y]; if (val == 0 || blocks[rawIdx] != static_cast(layerIds[y])) continue; int byteIdx = rawIdx >> 1; if (rawIdx & 1) nibbleData.data[byteIdx] = static_cast((nibbleData.data[byteIdx] & 0x0F) | (val << 4)); else nibbleData.data[byteIdx] = static_cast((nibbleData.data[byteIdx] & 0xF0) | val); } } } } LevelChunk *levelChunk = new LevelChunk(level, blocks, xOffs, zOffs); if (hasLayerData) { levelChunk->setDataData(nibbleData); delete [] nibbleData.data; } XPhysicalFree(tileData); byteArray chunkBiomes = levelChunk->getBiomes(); if (chunkBiomes.data != nullptr) { for (int z = 0; z < 16; ++z) for (int x = 0; x < 16; ++x) { Biome *biome = level->getBiomeSource()->getBiome(xOffs * 16 + x, zOffs * 16 + z); if (biome != nullptr && (z << 4 | x) < static_cast(chunkBiomes.length)) chunkBiomes[(z << 4) | x] = static_cast(biome->id & 0xff); } } levelChunk->recalcHeightmap(); return levelChunk; } bool FlatLevelSource::hasChunk(int x, int y) { return true; } void FlatLevelSource::postProcess(ChunkSource *parent, int xt, int zt) { HeavyTile::instaFall = true; int xo = xt * 16; int zo = zt * 16; Biome *biome = level->getBiome(xo + 16, zo + 16); pprandom->setSeed(level->getSeed()); int64_t xScale = pprandom->nextLong() / 2 * 2 + 1; int64_t zScale = pprandom->nextLong() / 2 * 2 + 1; pprandom->setSeed(((xt * xScale) + (zt * zScale)) ^ level->getSeed()); bool hasVillage = false; if (mineShaftFeature != nullptr) mineShaftFeature->postProcess(level, pprandom, xt, zt); if (villageFeature != nullptr) hasVillage = villageFeature->postProcess(level, pprandom, xt, zt); if (strongholdFeature != nullptr) strongholdFeature->postProcess(level, pprandom, xt, zt); if (scatteredFeature != nullptr) scatteredFeature->postProcess(level, pprandom, xt, zt); if (oceanMonument != nullptr) oceanMonument->postProcess(level, pprandom, xt, zt); if (lakes && !hasVillage && pprandom->nextInt(4) == 0) { int x = xo + pprandom->nextInt(16) + 8; int y = pprandom->nextInt(Level::genDepth); int z = zo + pprandom->nextInt(16) + 8; LakeFeature calmWater(Tile::water_Id); calmWater.place(level, pprandom, x, y, z); } if (lavaLakes && !hasVillage && pprandom->nextInt(8) == 0) { int x = xo + pprandom->nextInt(16) + 8; int y = pprandom->nextInt(pprandom->nextInt(Level::genDepth - 8) + 8); int z = zo + pprandom->nextInt(16) + 8; if (y < level->seaLevel || pprandom->nextInt(10) == 0) { LakeFeature calmLava(Tile::lava_Id); calmLava.place(level, pprandom, x, y, z); } } if (dungeons) { for (int i = 0; i < 8; i++) { int x = xo + pprandom->nextInt(16) + 8; int y = pprandom->nextInt(Level::genDepth); int z = zo + pprandom->nextInt(16) + 8; MonsterRoomFeature mrf; mrf.place(level, pprandom, x, y, z); } } if (decorations && biome != nullptr) { biome->decorate(level, pprandom, xo, zo); } app.processSchematics(parent->getChunk(xt,zt)); HeavyTile::instaFall = false; } bool FlatLevelSource::save(bool force, ProgressListener *progressListener) { return true; } bool FlatLevelSource::tick() { return false; } bool FlatLevelSource::shouldSave() { return true; } wstring FlatLevelSource::gatherStats() { return L"FlatLevelSource"; } vector *FlatLevelSource::getMobsAt(MobCategory *mobCategory, int x, int y, int z) { Biome *biome = level->getBiome(x, z); if (biome == NULL) { return nullptr; } if (mobCategory == MobCategory::monster) { if (scatteredFeature != nullptr && scatteredFeature->isSwamphut(x, y, z)) return scatteredFeature->getSwamphutEnemies(); if (oceanMonument != nullptr && oceanMonument->isInsideFeature(x, y, z)) return oceanMonument->getMonumentEnemies(); } return biome->getMobs(mobCategory); } TilePos *FlatLevelSource::findNearestMapFeature(Level *level, const wstring& featureName, int x, int y, int z) { if (LargeFeature::STRONGHOLD == featureName && strongholdFeature != nullptr) { return strongholdFeature->getNearestGeneratedFeature(level, x, y, z); } return nullptr; } void FlatLevelSource::recreateLogicStructuresForChunk(int chunkX, int chunkZ) { if (mineShaftFeature != nullptr) mineShaftFeature->apply(this, level, chunkX, chunkZ, byteArray()); if (villageFeature != nullptr) villageFeature->apply(this, level, chunkX, chunkZ, byteArray()); if (strongholdFeature != nullptr) strongholdFeature->apply(this, level, chunkX, chunkZ, byteArray()); if (scatteredFeature != nullptr) scatteredFeature->apply(this, level, chunkX, chunkZ, byteArray()); }