Skip to content

Commit

Permalink
Implement NetherFlatLevelSource
Browse files Browse the repository at this point in the history
  • Loading branch information
GRAnimated committed Sep 7, 2024
1 parent 8add60b commit ec940e3
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 42 deletions.
32 changes: 16 additions & 16 deletions data/mcswitch_functions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -12133,7 +12133,7 @@ Address,Quality,Size,Name
0x00000071001a2558,U,000008,sub_71001A2558
0x00000071001a2560,U,000008,sub_71001A2560
0x00000071001a2568,U,000004,nullsub_287
0x00000071001a256c,U,000352,sub_71001A256C
0x00000071001a256c,O,000352,_ZN15HellFireFeature5placeEP5LevelR6RandomRK8BlockPos
0x00000071001a26cc,U,000568,sub_71001A26CC
0x00000071001a2904,O,000076,_ZN17HellSpringFeatureC1EP5Blockb
0x00000071001a2950,O,000916,_ZN17HellSpringFeature5placeEP5LevelR6RandomRK8BlockPos
Expand Down Expand Up @@ -16763,21 +16763,21 @@ Address,Quality,Size,Name
0x000000710026a62c,U,000096,_ZN15NetherDimension9getXZSizeEv
0x000000710026a68c,U,000032,_ZN15NetherDimension20getPlayerConstraintsERK8BlockPos
0x000000710026a6ac,U,000028,_ZN15NetherDimension19getBlockLightColourEv
0x000000710026a6c8,U,000200,_ZN21NetherFlatLevelSourceC1EP5Levelbx
0x000000710026a790,U,000072,_ZN21NetherFlatLevelSourceD1Ev
0x000000710026a7d8,U,000036,_ZN21NetherFlatLevelSourceD0Ev
0x000000710026a7fc,U,000168,_ZN21NetherFlatLevelSource14prepareHeightsEiiP11ChunkPrimer
0x000000710026a8a4,U,000632,_ZN21NetherFlatLevelSource13buildSurfacesEiiP11ChunkPrimer
0x000000710026ab1c,U,000324,_ZN21NetherFlatLevelSource11createChunkEii
0x000000710026ac60,U,000016,_ZN21NetherFlatLevelSource10lightChunkEP10LevelChunk
0x000000710026ac70,U,000604,_ZN21NetherFlatLevelSource11postProcessEii
0x000000710026aecc,U,000052,sub_710026AECC
0x000000710026a6c8,M,000200,_ZN21NetherFlatLevelSourceC1EP5Levelbx
0x000000710026a790,O,000072,_ZN21NetherFlatLevelSourceD1Ev
0x000000710026a7d8,O,000036,_ZN21NetherFlatLevelSourceD0Ev
0x000000710026a7fc,O,000168,_ZN21NetherFlatLevelSource14prepareHeightsEiiP11ChunkPrimer
0x000000710026a8a4,M,000632,_ZN21NetherFlatLevelSource13buildSurfacesEiiP11ChunkPrimer
0x000000710026ab1c,O,000324,_ZN21NetherFlatLevelSource11createChunkEii
0x000000710026ac60,O,000016,_ZN21NetherFlatLevelSource10lightChunkEP10LevelChunk
0x000000710026ac70,M,000604,_ZN21NetherFlatLevelSource11postProcessEii
0x000000710026aecc,O,000052,_ZN15HellFireFeatureC2Ev
0x000000710026af00,O,000052,_ZN15LightGemFeatureC1Ev
0x000000710026af34,U,000068,_ZN21NetherFlatLevelSource9getMobsAtEP11MobCategoryRK8BlockPos
0x000000710026af78,U,000008,_ZN21NetherFlatLevelSource22postProcessLoadedChunkEP10LevelChunkii
0x000000710026af80,U,000008,_ZN21NetherFlatLevelSource21findNearestMapFeatureEP5LevelRKNSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEERK8BlockPosb
0x000000710026af88,U,000008,_ZN21NetherFlatLevelSource14isPosInFeatureEP5LevelRKNSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEERK8BlockPos
0x000000710026af90,U,000004,_ZN21NetherFlatLevelSource31recreateLogicStructuresForChunkEP10LevelChunkii
0x000000710026af34,O,000068,_ZN21NetherFlatLevelSource9getMobsAtEP11MobCategoryRK8BlockPos
0x000000710026af78,O,000008,_ZN21NetherFlatLevelSource22postProcessLoadedChunkEP10LevelChunkii
0x000000710026af80,O,000008,_ZN21NetherFlatLevelSource21findNearestMapFeatureEP5LevelRKNSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEERK8BlockPosb
0x000000710026af88,O,000008,_ZN21NetherFlatLevelSource14isPosInFeatureEP5LevelRKNSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEERK8BlockPos
0x000000710026af90,O,000004,_ZN21NetherFlatLevelSource31recreateLogicStructuresForChunkEP10LevelChunkii
0x000000710026af94,U,000004,nullsub_384
0x000000710026af98,U,000824,_ZN17NetherLevelSourceC1EP5Levelbx
0x000000710026b2d0,U,000052,_ZN17HellPortalFeatureC2Ev
Expand Down Expand Up @@ -38764,7 +38764,7 @@ Address,Quality,Size,Name
0x00000071006035d0,U,000156,sub_71006035D0
0x000000710060366c,U,000016,sub_710060366C
0x000000710060367c,U,000452,sub_710060367C
0x0000007100603840,U,000008,sub_7100603840
0x0000007100603840,U,000008,_ZN13CMinecraftApp17processSchematicsEP10LevelChunk
0x0000007100603848,U,000008,sub_7100603848
0x0000007100603850,U,000008,_ZN13CMinecraftApp20loadDefaultGameRulesEv
0x0000007100603860,U,000008,sub_7100603860
Expand Down
4 changes: 4 additions & 0 deletions lib/windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ typedef bool BOOL;
typedef long long LARGE_INTEGER;

BOOL QueryPerformanceCounter(LARGE_INTEGER* lpPerformanceCount);

void* XPhysicalAlloc(unsigned long, unsigned long, unsigned long, unsigned int);
void XMemSet128(void*, int, unsigned int);
void XPhysicalFree(void*);
5 changes: 5 additions & 0 deletions src/Minecraft.Client/CMinecraftApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum eTMSAction {};
enum eFileExtensionType {};
enum eTMSFileType {};
enum eTPDFileType {};
class LevelChunk;

class CMinecraftApp {
public:
Expand All @@ -28,10 +29,14 @@ class CMinecraftApp {
virtual void GetTMSDLCInfoRead();
virtual void GetTMSXUIDsFileRead();
virtual void GetFileFromTPD(eTPDFileType, unsigned char*, unsigned int, unsigned char**, unsigned int*);

void processSchematics(LevelChunk*);
};

class CConsoleMinecraftApp : public CMinecraftApp {
public:
static CConsoleMinecraftApp* instance;

CConsoleMinecraftApp();
virtual ~CConsoleMinecraftApp();
void SetRichPresenceContext(int, int) override;
Expand Down
1 change: 1 addition & 0 deletions src/Minecraft.World/ArrayWithLength.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class arrayWithLength {
public:
arrayWithLength() {}
arrayWithLength(std::vector<T> initialData, int length);
arrayWithLength(T* initialData, int length);
arrayWithLength(int size, bool a2);

// NON_MATCHING
Expand Down
1 change: 1 addition & 0 deletions src/Minecraft.World/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class Random {
public:
Random();
Random(long long seed);
static double random();
int nextInt(int);
float nextFloat();
Expand Down
5 changes: 4 additions & 1 deletion src/Minecraft.World/level/Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Level : public LevelSource {
virtual void getBlockData(int, int, int) override;
virtual void getBlock(BlockPos const&) override;
virtual bool isEmptyBlock(BlockPos const&) override;
virtual void getBiome(BlockPos const&) override;
virtual Biome* getBiome(BlockPos const&) override;
virtual void getBiomeSource() override;
virtual void getMaxBuildHeight() override;
virtual bool isAllEmpty() override;
Expand Down Expand Up @@ -111,8 +111,11 @@ class Level : public LevelSource {

bool isOutsideBuildHeight(BlockPos const&);
LevelChunk* getChunkAt(BlockPos const&);
LevelChunk* getChunk(int, int);

LevelData* getLevelData();

long long getSeed();

void instaTick(BlockPos const&, BlockState const*, Random&);
};
3 changes: 3 additions & 0 deletions src/Minecraft.World/level/biome/Biome.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#pragma once

enum eMinecraftColour {};
class MobCategory;

class Biome {
public:
void setNameAndDescription(int, int);
void setPreviewColor(eMinecraftColour);
void setWaterSkyColor(eMinecraftColour, eMinecraftColour);

void getMobs(MobCategory*);
};
6 changes: 6 additions & 0 deletions src/Minecraft.World/level/chunk/LevelChunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ class Level;
class AABB;
class BiomeSource;
class Entity;
class ChunkPrimer;

class LevelChunk {
public:
LevelChunk(Level*, ChunkPrimer*, int, int);

enum EntityCreationType {};

virtual void setBlockData(arrayWithLength<unsigned char>);
virtual void getBlockData(arrayWithLength<unsigned char>);
virtual void getBlockDataRange(arrayWithLength<unsigned char>, int, int);
Expand Down Expand Up @@ -75,4 +79,6 @@ class LevelChunk {
virtual void compressBlocks();
virtual void compressData();
virtual void getReorderedBlocksAndData(int, int, int, int, int&, int);

char unk[0x2D0];
};
2 changes: 1 addition & 1 deletion src/Minecraft.World/level/levelgen/ChunkGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

ChunkGenerator::ChunkGenerator() {}

void ChunkGenerator::lightChunk(LevelChunk*) {}
void ChunkGenerator::lightChunk(LevelChunk*) {}
10 changes: 6 additions & 4 deletions src/Minecraft.World/level/levelgen/ChunkGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class ChunkGenerator {
public:
ChunkGenerator();
virtual ~ChunkGenerator();
virtual void createChunk(int, int) = 0;
virtual LevelChunk* createChunk(int, int) = 0;
virtual void postProcess(int, int) = 0;
virtual void postProcessLoadedChunk(LevelChunk*, int, int) = 0;
virtual bool postProcessLoadedChunk(LevelChunk*, int, int) = 0;
virtual void getMobsAt(MobCategory*, BlockPos const&) = 0;
virtual void findNearestMapFeature(Level*, std::wstring const&, BlockPos const&, bool) = 0;
virtual void* findNearestMapFeature(Level*, std::wstring const&, BlockPos const&, bool) = 0;
virtual void recreateLogicStructuresForChunk(LevelChunk*, int, int) = 0;
virtual void isPosInFeature(Level*, std::wstring const&, BlockPos const&) = 0;
virtual bool isPosInFeature(Level*, std::wstring const&, BlockPos const&) = 0;
virtual void lightChunk(LevelChunk*);

static bool inPostProcessStep;
};
174 changes: 174 additions & 0 deletions src/Minecraft.World/level/levelgen/NetherFlatLevelSource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include "Minecraft.World/level/levelgen/NetherFlatLevelSource.h"
#include "Minecraft.Client/CMinecraftApp.h"
#include "Minecraft.World/ArrayWithLength.h"
#include "Minecraft.World/Random.h"
#include "Minecraft.World/level/Level.h"
#include "Minecraft.World/level/biome/Biome.h"
#include "Minecraft.World/level/block/Blocks.h"
#include "Minecraft.World/level/chunk/LevelChunk.h"
#include "Minecraft.World/level/levelgen/ChunkPrimer.h"
#include "Minecraft.World/level/levelgen/feature/HellFireFeature.h"
#include "Minecraft.World/level/levelgen/feature/LightGemFeature.h"
#include "Minecraft.World/level/storage/LevelData.h"
#include "windows.h"

// NON_MATCHING: Regswap
NetherFlatLevelSource::NetherFlatLevelSource(Level* level, bool isGenerateMapFeatures, long long seed) {
int xzSize = level->getLevelData()->getXZSize();
float hellScale = level->getLevelData()->getHellScale();
mSize = ceilf(xzSize / hellScale);
mLevel = level;
mIsGenerateMapFeatures = isGenerateMapFeatures;
mSeed = new Random(seed);
mRandom = new Random(seed);
}

NetherFlatLevelSource::~NetherFlatLevelSource() {
delete mSeed;
delete mRandom;
}

void NetherFlatLevelSource::prepareHeights(int x, int z, ChunkPrimer* primer) {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
for (int k = 0; k < 16; k++) {
const BlockState* block = Blocks::AIR->defaultBlockState();
if (k <= 6)
block = Blocks::NETHERRACK->defaultBlockState();
primer->setState((i << 11) | (j << 7) | k, block);
}
}
}
}

// NON_MATCHING
void NetherFlatLevelSource::buildSurfaces(int x, int z, ChunkPrimer* primer) {
const BlockState* bedrock = Blocks::BEDROCK->defaultBlockState();

for (int chunkX = 0; chunkX < 16; ++chunkX) {
for (int chunkZ = 0; chunkZ < 16; ++chunkZ) {
int adjustedSize = (mSize >= 0) ? mSize : mSize + 1;
int halfSize = adjustedSize >> 1;

for (int y = 127; y > 0; --y) {
if (-halfSize < x || (chunkX > mRandom->nextInt(4) && halfSize >= x)) {
primer->setState((y << 11) | (chunkX << 7) | chunkZ, bedrock);
}

if (-halfSize >= z) {
if (y <= mRandom->nextInt(4) || halfSize > z) {
primer->setState((y << 11) | (chunkX << 7) | chunkZ, bedrock);
}
}

if (halfSize - 1 <= x) {
if (mRandom->nextInt(4) + chunkX > 14 || halfSize < x) {
primer->setState((y << 11) | (chunkX << 7) | chunkZ, bedrock);
}
}

if ((adjustedSize / 2 - 1 <= z && (mRandom->nextInt(4) + y > 14 || adjustedSize / 2 < z)) || (y >= (127 - mRandom->nextInt(5)) || y <= mRandom->nextInt(5))) {
primer->setState((y << 11) | (chunkX << 7) | chunkZ, bedrock);
}
}
}
}
}

LevelChunk* NetherFlatLevelSource::createChunk(int x, int z) {
mSeed->setSeed(0x4F9939F508LL * x + 0x1EF1565BD5LL * z);

void* ids = XPhysicalAlloc(0x8000, 0xFFFFFFFFFFFFFFFFLL, 0x1000uLL, 4u);
XMemSet128(ids, 0, 0x8000u);
arrayWithLength<uchar> blockIds((uchar*)ids, 0x8000u);

void* data = XPhysicalAlloc(0x4000, 0xFFFFFFFFFFFFFFFFLL, 0x1000uLL, 4u);
XMemSet128(data, 0, 0x4000u);
arrayWithLength<uchar> blockData((uchar*)data, 0x4000);

ChunkPrimer primer = ChunkPrimer(false, blockIds, blockData);
prepareHeights(x, z, &primer);
buildSurfaces(x, z, &primer);

LevelChunk* chunk = new LevelChunk(mLevel, &primer, x, z);

XPhysicalFree(ids);
XPhysicalFree(data);

return chunk;
}

// NON_MATCHING
void NetherFlatLevelSource::postProcess(int x, int z) {
inPostProcessStep = true;

Random* r = mRandom;
Level* l = mLevel;

int testX = 16 * x;
int testZ = 16 * z;

long long seed = l->getSeed();
r->setSeed(seed);

long seed1 = mRandom->nextLong() | 1;
long seed2 = mRandom->nextLong() | 1;

long long v16 = x * seed1 + z * seed2 ^ seed;

mRandom->setSeed(v16);

int v11 = mRandom->nextInt(10);
int v12 = mRandom->nextInt(v11 + 1) + 1;

if (v12 > 0) {
for (int i = 0; i < v12; ++i) {
int posX = mRandom->nextInt(16) + (testX | 8);
int posY = mRandom->nextInt(120) + 4;
int posZ = mRandom->nextInt(16) + (testZ | 8);

HellFireFeature feature;
feature.place(mLevel, *mRandom, BlockPos(posX, posY, posZ));
}
}

int v31 = mRandom->nextInt(mRandom->nextInt(10) + 1);

for (int i = 0; i < v31; i++) {
int posX = mRandom->nextInt(16) + (testX | 8);
int posY = mRandom->nextInt(120) + 4;
int posZ = mRandom->nextInt(16) + (testZ | 8);

LightGemFeature feature;
feature.place(mLevel, *mRandom, BlockPos(posX, posY, posZ));
}

inPostProcessStep = false;
CConsoleMinecraftApp::instance->processSchematics(mLevel->getChunk(x, z));
}

void NetherFlatLevelSource::lightChunk(LevelChunk* chunk) {
return chunk->recalcHeightmap();
}

bool NetherFlatLevelSource::postProcessLoadedChunk(LevelChunk* chunk, int x, int z) {
return false;
}

void NetherFlatLevelSource::getMobsAt(MobCategory* category, BlockPos const& pos) {
Biome* biome = mLevel->getBiome(pos);
if (biome)
biome->getMobs(category);
}

void* NetherFlatLevelSource::findNearestMapFeature(Level* level, std::wstring const& name, BlockPos const& pos, bool flag) {
return nullptr;
}

void NetherFlatLevelSource::recreateLogicStructuresForChunk(LevelChunk* chunk, int x, int z) {
return;
}

bool NetherFlatLevelSource::isPosInFeature(Level* level, std::wstring const& name, BlockPos const& pos) {
return false;
}
21 changes: 12 additions & 9 deletions src/Minecraft.World/level/levelgen/NetherFlatLevelSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
#include "Minecraft.World/level/levelgen/ChunkGenerator.h"

class Random;
class ChunkPrimer;

class NetherFlatLevelSource : public ChunkGenerator {
public:
NetherFlatLevelSource(Level*, bool, long long);
virtual ~NetherFlatLevelSource();
virtual void createChunk(int, int) override;
virtual LevelChunk* createChunk(int, int) override;
virtual void postProcess(int, int) override;
virtual void postProcessLoadedChunk(LevelChunk*, int, int) override;
virtual bool postProcessLoadedChunk(LevelChunk*, int, int) override;
virtual void getMobsAt(MobCategory*, BlockPos const&) override;
virtual void findNearestMapFeature(Level*, std::wstring const&, BlockPos const&, bool) override;
virtual void* findNearestMapFeature(Level*, std::wstring const&, BlockPos const&, bool) override;
virtual void recreateLogicStructuresForChunk(LevelChunk*, int, int) override;
virtual void isPosInFeature(Level*, std::wstring const&, BlockPos const&) override;
virtual bool isPosInFeature(Level*, std::wstring const&, BlockPos const&) override;
virtual void lightChunk(LevelChunk*) override;
void prepareHeights(int, int, ChunkPrimer*);
void buildSurfaces(int, int, ChunkPrimer*);

int field_8;
int size;
long long seed;
Random* random;
bool isGenerateMapFeatures;
Level* level;
int mSize;
Random* mSeed;
Random* mRandom;
bool mIsGenerateMapFeatures;
Level* mLevel;
};
Loading

0 comments on commit ec940e3

Please sign in to comment.