Skip to content

Commit

Permalink
test(CustomWorldGenerator): incompletely replicate FlatWorldGenerator…
Browse files Browse the repository at this point in the history
… (to be completed).
  • Loading branch information
Lovelylavender4 committed Feb 8, 2024
1 parent e7600a8 commit 450f751
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 47 deletions.
188 changes: 161 additions & 27 deletions src/ll/test/customWorldGenerator/MyFlatWorldGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "ll/api/utils/RandomUtils.h"

#include "ll/api/memory/Memory.h"
#include "mc/common/wrapper/FeatureRefTraits.h"
#include "mc/common/wrapper/WeakRefT.h"
#include "mc/deps/core/debug/log/ContentLog.h"
Expand All @@ -14,77 +15,210 @@
#include "mc/util/random/XoroshiroPositionalRandomFactory.h"
#include "mc/world/level/BlockPalette.h"
#include "mc/world/level/BlockSource.h"
#include "mc/world/level/BlockVolumeTarget.h"
#include "mc/world/level/ChunkBlockPos.h"
#include "mc/world/level/ChunkPos.h"
#include "mc/world/level/Level.h"
#include "mc/world/level/MolangVariableMap.h"
#include "mc/world/level/WorldBlockTarget.h"
#include "mc/world/level/WorldGenContext.h"
#include "mc/world/level/biome/Biome.h"
#include "mc/world/level/biome/registry/BiomeRegistry.h"
#include "mc/world/level/biome/source/BiomeArea.h"
#include "mc/world/level/biome/source/FixedBiomeSource.h"
#include "mc/world/level/biome/surface/PerlinNoise.h"
#include "mc/world/level/block/Block.h"
#include "mc/world/level/block/BlockVolume.h"
#include "mc/world/level/block/registry/BlockTypeRegistry.h"
#include "mc/world/level/block/utils/BedrockBlockNames.h"
#include "mc/world/level/block/utils/VanillaBlockTypeIds.h"
#include "mc/world/level/chunk/ChunkViewSource.h"
#include "mc/world/level/chunk/LevelChunk.h"
#include "mc/world/level/dimension/Dimension.h"
#include "mc/world/level/dimension/OverworldBrightnessRamp.h"
#include "mc/world/level/levelgen/WorldGenerator.h"
#include "mc/world/level/levelgen/feature/BambooFeature.h"
#include "mc/world/level/levelgen/feature/DesertWellFeature.h"
#include "mc/world/level/levelgen/feature/FlowerFeature.h"
#include "mc/world/level/levelgen/feature/LakeFeature.h"
#include "mc/world/level/levelgen/feature/VanillaTreeFeature.h"
#include "mc/world/level/levelgen/feature/helpers/RenderParams.h"
#include "mc/world/level/levelgen/feature/registry/FeatureRegistry.h"
#include "mc/world/level/levelgen/flat/FlatWorldGenerator.h"
#include "mc/world/level/levelgen/flat/FlatWorldGeneratorOptions.h"
#include "mc/world/level/levelgen/structure/StructureFeatureRegistry.h"
#include "mc/world/level/levelgen/structure/StructureFeatureType.h"
#include "mc/world/level/levelgen/v1/Aquifer.h"
#include "mc/world/level/levelgen/v1/ChunkLocalNoiseCache.h"
#include "mc/world/level/levelgen/v1/NetherGenerator.h"
#include "mc/world/level/levelgen/v1/OverworldGenerator2d.h"
#include "mc/world/level/levelgen/v1/OverworldGeneratorMultinoise.h"
#include "mc/world/level/levelgen/v1/WorldGenCache.h"
#include "mc/world/level/storage/LevelData.h"

#include "mc/world/components/FeatureHelper.h"

class MyFlatWorldGenerator : public FlatWorldGenerator {

class MyFlatWorldGenerator : public WorldGenerator {
public:
uint mSeed;
std::vector<Block const*> mPrototypeBlocks; // this+0x90
BlockVolume mPrototype; // this+0xA8
Biome const* mBiome; // this+0xD0
std::unique_ptr<FixedBiomeSource> mBiomeSource; // this+0xD8
uint mSeed;
int mPostProcessCallCount;

public:
MyFlatWorldGenerator(Dimension& dimension, uint seed, Json::Value value)
: FlatWorldGenerator(dimension, seed, value),
mSeed(seed) {}
: WorldGenerator(dimension),
mSeed(seed),
mPostProcessCallCount(0) {
LevelData& levelData = dimension.getLevel().getLevelData();
FlatWorldGeneratorOptions generationOptions;

if (!generationOptions._load(value, levelData)) {
generationOptions._load(FlatWorldGeneratorOptions::getLayers(levelData), levelData);
}

Biome* biome = this->mLevel->getBiomeRegistry().lookupById(generationOptions.mBiomeId);
if (!biome) {
biome = this->mLevel->getBiomeRegistry().lookupById(1);
}

this->mBiomeSource = std::make_unique<FixedBiomeSource>(*biome);
this->_generatePrototypeBlockValues(generationOptions, dimension.getMinHeight());
this->mBiome = biome;
}

~MyFlatWorldGenerator() {}
virtual ~MyFlatWorldGenerator() {}

public:
virtual bool postProcess(ChunkViewSource& chunkViewSource) {
BlockSource blockSource(getLevel(), getDimension(), chunkViewSource, 0, 1, 0);
Random random;

// auto r = this->getLevel()
// .getFeatureRegistry()
// .lookupByName("minecraft:bamboo_feature")
// .tryUnwrap<BambooFeature>();
// if (!r.has_value()) {
// ll::logger.error("Error! tryUnwrap<BambooFeature> returned a null value!");
// return true;
// }
// BlockSource blockSource(getLevel(), getDimension(), chunkViewSource, 0, 1, 0);
// Random random;
// if (mPostProcessCallCount < 1) {
// mPostProcessCallCount++;
// MolangVariableMap MolangVariableMap;
// WorldGenContext worldGenContext;
// RenderParams renderParams;

// r->place(blockSource, {14, -60, 14}, random);
// // VanillaTreeFeature(IFeature)
// // BDS1.20.51 CherrySaplingBlock::_growTree
// BlockPos cherryTreeGeneratePos{1, 0, 1};
// // /*crash*/ renderParams = FeatureHelper::makeFeatureRenderParams(blockSource, cherryTreeGeneratePos,
// // MolangVariableMap);
// blockSource.setBlockNoUpdate(
// cherryTreeGeneratePos.x,
// cherryTreeGeneratePos.y,
// cherryTreeGeneratePos.z,
// BlockTypeRegistry::getDefaultBlockState(BedrockBlockNames::Air, 1)
// );
// WorldBlockTarget worldBlockTargetForCherryTree(blockSource, worldGenContext);
// auto cherryTree = this->getLevel()
// .getFeatureRegistry()
// .lookupByName("minecraft:cherry_tree_feature")
// .tryUnwrap<VanillaTreeFeature>();
// if (!cherryTree.has_value()) {
// ll::logger.error("Error! tryUnwrap<VanillaTreeFeature> returned a null value!");
// return true;
// }
// cherryTree->place(worldBlockTargetForCherryTree, cherryTreeGeneratePos, random, renderParams);

LakeFeature wlakeFeature(
BlockTypeRegistry::getDefaultBlockState(VanillaBlockTypeIds::Water, 1),
XoroshiroPositionalRandomFactory{ll::random_utils::rand<uint64>(), ll::random_utils::rand<uint64>()}
);
wlakeFeature.place(blockSource, {14, 0, 14}, random);
// //// BambooFeature(Feature)
// // auto bamboo =
// //
// this->getLevel().getFeatureRegistry().lookupByName("minecraft:bamboo_feature").tryUnwrap<BambooFeature>();
// // if (!bamboo.has_value()) {
// // ll::logger.error("Error! tryUnwrap<BambooFeature> returned a null value!");
// // return true;
// // }

LakeFeature llakeFeature(
BlockTypeRegistry::getDefaultBlockState(VanillaBlockTypeIds::Lava, 1),
XoroshiroPositionalRandomFactory{ll::random_utils::rand<uint64>(), ll::random_utils::rand<uint64>()}
);
llakeFeature.place(blockSource, {-14, 0, -14}, random);
// // bamboo->place(blockSource, {24, 0, 24}, random);

// //// LakeFeature(Feature)
// // LakeFeature wlakeFeature(
// // BlockTypeRegistry::getDefaultBlockState(VanillaBlockTypeIds::Water, 1),
// // XoroshiroPositionalRandomFactory{ll::random_utils::rand<uint64>(),
// ll::random_utils::rand<uint64>()}
// //);
// // wlakeFeature.place(blockSource, {14, 0, 14}, random);

// // LakeFeature llakeFeature(
// // BlockTypeRegistry::getDefaultBlockState(VanillaBlockTypeIds::Lava, 1),
// // XoroshiroPositionalRandomFactory{ll::random_utils::rand<uint64>(),
// ll::random_utils::rand<uint64>()}
// //);
// // llakeFeature.place(blockSource, {-14, 0, -14}, random);
//}
return true;
}

public:
virtual void prepareHeights(BlockVolume&, const ChunkPos&, bool) {}

virtual void
prepareAndComputeHeights(BlockVolume&, const ChunkPos&, std::vector<short, std::allocator<short>>&, bool, int) {}

virtual BiomeArea getBiomeArea(const BoundingBox& area, uint scale) const {
return mBiomeSource->getBiomeArea(area, scale);
}

virtual const BiomeSource& getBiomeSource() const { return *mBiomeSource; }

virtual WorldGenerator::BlockVolumeDimensions getBlockVolumeDimensions() const {
return {mPrototype.mWidth, mPrototype.mDepth, mPrototype.mHeight};
}

virtual BlockPos WorldGenerator::findSpawnPosition() const { return {0, 0x7FFF}; }

virtual void decorateWorldGenLoadChunk(Biome&, LevelChunk&, BlockVolumeTarget&, Random&, const ChunkPos&) const {}

virtual void decorateWorldGenPostProcess(Biome&, LevelChunk&, BlockSource&, Random&) const {}

public:
virtual void loadChunk(LevelChunk& levelchunk, bool forceImmediateReplacementDataLoad) {
levelchunk.setBlockVolume(mPrototype, 0);
levelchunk.recomputeHeightMap(0);

if (!levelchunk.getGenerator()) {
levelchunk._setGenerator(this);
}

ChunkLocalNoiseCache chunkLocalNoiseCache;
mBiomeSource->fillBiomes(levelchunk, chunkLocalNoiseCache);

levelchunk.setSaved();
levelchunk.changeState(ChunkState::Generating, ChunkState::Generated);
}

public:
void _generatePrototypeBlockValues(FlatWorldGeneratorOptions const& layersDesc, short minHeight) {
/**
* @note The following code restoration is incorrect and awaits correction by someone with the opportunity.
*/
// Block const* defaultBlockState = &BlockTypeRegistry::getDefaultBlockState(BedrockBlockNames::Air, 1);
//
// int n = 0;
//
// for (const auto& layer : layersDesc.mBlockLayers) {
// for (int i = 0; i < layer.mNumLayers; ++i) {
// mPrototypeBlocks.push_back(layer.mBlock);
// }
// n += layer.mNumLayers;
// }
// buffer_span_mut<Block const*> buffer;
// buffer.mBegin = &mPrototypeBlocks[0];
// buffer.mEnd = &mPrototypeBlocks.back();
// BlockVolume blockVolume(buffer, 16, n, 16, *defaultBlockState, minHeight);
// mPrototype = blockVolume;

LL_SYMBOL_CALL(
"?_generatePrototypeBlockValues@FlatWorldGenerator@@AEAAXAEBVFlatWorldGeneratorOptions@@F@Z",
void,
MyFlatWorldGenerator*,
FlatWorldGeneratorOptions const&,
short
)
(this, layersDesc, minHeight);
}
};
40 changes: 20 additions & 20 deletions src/ll/test/customWorldGenerator/TestWorldGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@
// // auto& layer = levelData.getFlatWorldGeneratorOptions();
// std::string layerstr =
// R"({
// "biome_id": 192,
// "world_version": "version.post_1_18",
// "block_layers": [
// {
// "block_name": "minecraft:bedrock",
// "count": 1
// },
// {
// "block_name": "minecraft:cherry_log",
// "count": 2
// },
// {
// "block_name": "minecraft:mycelium",
// "count": 61
// }
// ],
// "encoding_version": 6,
// "structure_options": null
// })";
// "biome_id": 4,
// "world_version": "version.post_1_18",
// "block_layers": [
// {
// "block_name": "minecraft:bedrock",
// "count": 1
// },
// {
// "block_name": "minecraft:cherry_log",
// "count": 2
// },
// {
// "block_name": "minecraft:grass",
// "count": 61
// }
// ],
// "encoding_version": 6,
// "structure_options": null
// })";
// Json::Value layer;
// Json::Reader reader;
// reader.parse(layerstr, layer, 0);
//
// ll::logger.warn("Superflat worlds will be created based on the following option:{}", layer.toStyledString());
//
// return std::make_unique<MyFlatWorldGenerator>(*this, level.getSeed(), layer);
// }
// }

0 comments on commit 450f751

Please sign in to comment.