Skip to content

Commit

Permalink
Finish ChunkPrimer
Browse files Browse the repository at this point in the history
  • Loading branch information
GRAnimated committed Sep 15, 2024
1 parent 4e13048 commit 86f1935
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 19 deletions.
12 changes: 6 additions & 6 deletions data/mcswitch_functions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1724,7 +1724,7 @@ Address,Quality,Size,Name
0x0000007100042cac,U,000116,sub_7100042CAC
0x0000007100042d20,U,000012,sub_7100042D20
0x0000007100042d2c,U,000852,_ZN5Biome21buildSurfaceAtDefaultEP5LevelR6RandomP11ChunkPrimeriid
0x0000007100043080,U,000124,_ZN11ChunkPrimer15setBlockAndDataEiii
0x0000007100043080,M,000124,_ZN11ChunkPrimer15setBlockAndDataEiii
0x00000071000430fc,O,000024,_ZN11ChunkPrimer10getBlockIdEi
0x0000007100043114,U,000060,sub_7100043114
0x0000007100043150,U,000012,_ZN5Biome9getBiomesEv
Expand Down Expand Up @@ -3009,7 +3009,7 @@ Address,Quality,Size,Name
0x000000710009d120,O,000024,_ZN8Calendar13GetDayOfMonthEv
0x000000710009d138,O,000024,_ZN8Calendar8GetMonthEv
0x000000710009d150,U,002676,sub_710009D150
0x000000710009dbc4,U,000060,sub_710009DBC4
0x000000710009dbc4,O,000060,_ZN11ChunkPrimer8getStateEiii
0x000000710009dc00,U,000416,sub_710009DC00
0x000000710009dda0,U,000016,sub_710009DDA0
0x000000710009ddb0,U,000016,sub_710009DDB0
Expand Down Expand Up @@ -3337,13 +3337,13 @@ Address,Quality,Size,Name
0x00000071000a9b54,U,000016,sub_71000A9B54
0x00000071000a9b64,U,000056,sub_71000A9B64
0x00000071000a9bc8,O,000116,_ZN11ChunkPrimerC1Eb15arrayWithLengthIhES1_
0x00000071000a9c3c,U,000068,_ZN11ChunkPrimerD2Ev
0x00000071000a9c80,M,000088,_ZN11ChunkPrimer8getStateEiii
0x00000071000a9c3c,M,000068,_ZN11ChunkPrimerD1Ev
0x00000071000a9c80,M,000088,_ZN11ChunkPrimer8getStateEi
0x00000071000a9cd8,O,000064,_ZN11ChunkPrimer8setStateEiiiPK10BlockState
0x00000071000a9d18,U,000184,_ZN11ChunkPrimer8setStateEiPK10BlockState
0x00000071000a9d18,M,000184,_ZN11ChunkPrimer8setStateEiPK10BlockState
0x00000071000a9dd0,O,000012,_ZN11ChunkPrimer11getBlockIdsEv
0x00000071000a9ddc,O,000012,_ZN11ChunkPrimer12getBlockDataEv
0x00000071000a9de8,M,000116,_ZN11ChunkPrimer19getHighestNonAirPosEii
0x00000071000a9de8,O,000116,_ZN11ChunkPrimer19getHighestNonAirPosEii
0x00000071000a9e5c,O,000020,_ZN12ChunkStorageC2Ev
0x00000071000a9e70,U,000068,_ZN25ChunkVisibilityAreaPacketC1Ev
0x00000071000a9eb4,U,000124,sub_71000A9EB4
Expand Down
3 changes: 3 additions & 0 deletions src/Minecraft.Core/IdMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ class IdMapper : public IdMap<T> {
int getId(const BlockState* state) override;
T byId(int id) override;
};

// TODO: Unknown location for this variable
extern IdMapper<const BlockState*>* sBlockStateMapper;
78 changes: 65 additions & 13 deletions src/Minecraft.World/level/levelgen/ChunkPrimer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "Minecraft.World/level/levelgen/ChunkPrimer.h"

#include "Minecraft.Core/IdMapper.h"
#include "Minecraft.World/level/block/Blocks.h"

ChunkPrimer::ChunkPrimer(bool unk, arrayWithLength<uchar> blockIds, arrayWithLength<uchar> blockData) {
Expand All @@ -8,18 +10,57 @@ ChunkPrimer::ChunkPrimer(bool unk, arrayWithLength<uchar> blockIds, arrayWithLen
byte_28 = unk;
}

// NON_MATCHING
ChunkPrimer::~ChunkPrimer() {
if (byte_28) {
if (mBlockIds.data)
delete[] & mBlockIds.data;
if (mBlockData.data)
delete[] & mBlockData.data;
}
}

// NON_MATCHING: CMP 0x1 vs CMP 0x0
const BlockState* ChunkPrimer::getState(int x, int y, int z) {
int packedPos = x <= 0 ? x + 1 : x;
const BlockState* ChunkPrimer::getState(int a2) {
int packedPos = a2 <= 0 ? a2 + 1 : a2;

int blockDataIndex = packedPos >> 1;

int bitShift = 4 * (x & 1);
int bitShift = 4 * (a2 & 1);

return Block::getStateByIdAndData(mBlockIds.get(x), (mBlockData.get(blockDataIndex) >> bitShift) & 0xF);
return Block::getStateByIdAndData(mBlockIds.get(a2), (mBlockData.get(blockDataIndex) >> bitShift) & 0xF);
}

// void ChunkPrimer::setState(int a, BlockState const* state);
const BlockState* ChunkPrimer::getState(int x, int y, int z) {
int v5 = 0;
if (mBlockIds.length > 0x8000u && y >= 128) {
y -= 128;
v5 = 0x8000;
}
int packedPos = v5 + ((x << 11) | (z << 7) | y);
return getState(packedPos);
}

// NON_MATCHING
void ChunkPrimer::setState(int packedPos, const BlockState* state) {
int id = 0;
int data = 0;

if (state) {
int stateId = sBlockStateMapper->getId(state);
id = stateId >> 0x4;
data = stateId & 0xF;
}

mBlockIds.get(packedPos) = id;

int test = packedPos / 2;

if (packedPos & 1)
mBlockData.get(test) = (mBlockData.get(test) & 0xF) | (16 * data);
else
mBlockData.get(test) = (mBlockData.get(test) & 0xF0) | data;
}

void ChunkPrimer::setState(int x, int y, int z, BlockState const* state) {
int v5 = 0;
Expand All @@ -31,29 +72,40 @@ void ChunkPrimer::setState(int x, int y, int z, BlockState const* state) {
setState(packedPos, state);
}

// void setBlockAndData(int, int, int);
// NON_MATCHING: CMP 0x1 vs CMP 0x0
void ChunkPrimer::setBlockAndData(int packedPos, int id, int data) {
mBlockIds.get(packedPos) = id;

int v9 = (packedPos <= 0) ? (packedPos + 1) : packedPos;

int ChunkPrimer::getBlockId(int a) {
return mBlockIds.get(a);
int test = v9 >> 1;

if (packedPos & 1)
mBlockData.get(test) = (mBlockData.get(test) & 0xF) | (16 * data);
else
mBlockData.get(test) = (mBlockData.get(test) & 0xF0) | data;
}

arrayWithLength<uchar> ChunkPrimer::getBlockData() {
return mBlockData;
int ChunkPrimer::getBlockId(int a) {
return mBlockIds.get(a);
}

arrayWithLength<uchar> ChunkPrimer::getBlockIds() {
return mBlockIds;
}

arrayWithLength<uchar> ChunkPrimer::getBlockData() {
return mBlockData;
}

// TODO: Find out what this function is
Block* sub_7100029D84(unsigned char);

// NON_MATCHING: packedXZ regswap
int ChunkPrimer::getHighestNonAirPos(int x, int z) {
int packedXZ = (x << 11) | (z << 7);
int packedXZ = (x << 11) | (z << 7) | 1;

for (int y = 127; y >= 0; y--) {
if (sub_7100029D84(mBlockIds.get(packedXZ + y + 127)) != Blocks::AIR) {
if (sub_7100029D84(mBlockIds.get(packedXZ + y + 126)) != Blocks::AIR) {
return y;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Minecraft.World/level/levelgen/ChunkPrimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ChunkPrimer {
public:
ChunkPrimer(bool, arrayWithLength<uchar>, arrayWithLength<uchar>);
~ChunkPrimer();
const BlockState* getState(int);
const BlockState* getState(int, int, int);
void setState(int, BlockState const*);
void setState(int, int, int, BlockState const*);
Expand Down

0 comments on commit 86f1935

Please sign in to comment.