Skip to content

Commit 0634896

Browse files
fix: (mostly) fixed infdev writing
1 parent 60c70d4 commit 0634896

13 files changed

Lines changed: 228 additions & 133 deletions

File tree

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ BreakFunctionDefinitionParameters: false
116116
BreakInheritanceList: BeforeColon
117117
BreakStringLiterals: true
118118
BreakTemplateDeclarations: MultiLine
119-
ColumnLimit: 80
119+
ColumnLimit: 150
120120
CommentPragmas: "^ IWYU pragma:"
121121
CompactNamespaces: false
122122
ConstructorInitializerIndentWidth: 4

projects/Converters/Minecraft/CMakeLists.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ cmake_minimum_required(VERSION 3.30)
22
project(Lodestone.Minecraft)
33

44
set(NBT_BUILD_TESTS OFF CACHE BOOL "" FORCE)
5-
FetchContent_Declare(
6-
nbt++
7-
GIT_REPOSITORY https://github.com/Team-Lodestone/libnbtplusplus.git
8-
GIT_TAG master
9-
)
10-
FetchContent_MakeAvailable(nbt++)
5+
if (NOT TARGET nbt++)
6+
FetchContent_Declare(
7+
nbt++
8+
GIT_REPOSITORY https://github.com/Team-Lodestone/libnbtplusplus.git
9+
GIT_TAG master
10+
)
11+
FetchContent_MakeAvailable(nbt++)
12+
endif()
1113

1214
# PROJECTS
1315
add_subdirectory(Common)

projects/Converters/Minecraft/Common/include/Lodestone.Minecraft.Common/world/data/LevelData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace lodestone::minecraft::common::world::data {
1414
int64_t lastPlayed;
1515
int64_t time;
1616

17-
LevelData(const int64_t seed = 0, const int64_t lastPlayed = 0, const int64_t time = 0) : seed(seed), lastPlayed(lastPlayed), time(time) {}
17+
explicit LevelData(const int64_t seed = 0, const int64_t lastPlayed = 0, const int64_t time = 0) : seed(seed), lastPlayed(lastPlayed), time(time) {}
1818
};
1919
}
2020

projects/Converters/Minecraft/Java/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION VERSION 3.30)
2-
project(Lodestone.Minecraft.Java VERSION 1.2.4)
2+
project(Lodestone.Minecraft.Java VERSION 1.2.5)
33
set(PROJECT_VERSION_DEV) # used for dev versions, otherwise blank
44

55
set(CMAKE_CXX_STANDARD 20)

projects/Converters/Minecraft/Java/include/Lodestone.Minecraft.Java/conversion/infdev/InfdevChunkIo.h

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
#ifndef LODESTONE_INFDEVCHUNKIO_H
1212
#define LODESTONE_INFDEVCHUNKIO_H
13+
#include <BinaryIO/io/Serializable.h>
14+
1315
#include <memory>
1416

1517
#include <Lodestone.Conversion/io/ObjectIOs.h>
@@ -21,13 +23,52 @@ namespace lodestone::minecraft::java::infdev::chunk {
2123
class InfdevChunkIO : public conversion::io::ChunkIO<&identifiers::INF_624_CHUNK_IO, const common::conversion::io::options::OptionPresets::CommonReadOptions, const common::conversion::io::options::OptionPresets::CommonChunkWriteOptions>
2224
{
2325
public:
24-
static constexpr int CHUNK_WIDTH = 16;
25-
static constexpr int CHUNK_DEPTH = 16;
26-
static constexpr int CHUNK_HEIGHT = 128;
26+
struct Properties {
27+
using Deserializer = bio::io::Deserializable<Properties, void>;
28+
using Serializer = bio::io::Serializable<Properties, void>;
29+
30+
const bool terrainPopulated;
31+
32+
constexpr int64_t serialize() const {
33+
return 0
34+
| (this->terrainPopulated ? 0b0000000000000001 : 0L);
35+
}
36+
37+
constexpr static Properties fromSerialized(const int64_t value) {
38+
return {
39+
.terrainPopulated = (value & 0b0000000000000001) == 1
40+
};
41+
}
42+
};
2743

2844
std::unique_ptr<level::chunk::Chunk> read(const common::conversion::io::options::OptionPresets::CommonReadOptions &options) const override;
2945
void write(level::chunk::Chunk *c, const common::conversion::io::options::OptionPresets::CommonChunkWriteOptions &options) const override;
3046
};
3147
}
3248

49+
using namespace lodestone::minecraft::java::infdev::chunk;
50+
51+
template <>
52+
class bio::io::Deserializable<InfdevChunkIO::Properties, void> {
53+
public:
54+
using Type = InfdevChunkIO::Properties;
55+
using Options = void;
56+
57+
static std::unique_ptr<InfdevChunkIO::Properties> deserialize(ReadableBufferLike auto &readable) {
58+
const int64_t props = readable.template readBE<int64_t>();
59+
return std::make_unique<InfdevChunkIO::Properties>(InfdevChunkIO::Properties::fromSerialized(props));
60+
}
61+
};
62+
63+
template<>
64+
class bio::io::Serializable<InfdevChunkIO::Properties, void> {
65+
public:
66+
using Type = InfdevChunkIO::Properties;
67+
using Options = void; // allows us to call serialize without an options arg.
68+
69+
static void serialize(const InfdevChunkIO::Properties& properties, WritableBufferLike auto &writable) {
70+
writable.template writeBE<int64_t>(properties.serialize());
71+
}
72+
};
73+
3374
#endif //LODESTONE_INFDEVCHUNKIO_H

projects/Converters/Minecraft/Java/include/Lodestone.Minecraft.Java/infdev/InfdevChunk.h

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,59 @@
1010
*/
1111
#ifndef LODESTONE_INFDEVCHUNK_H
1212
#define LODESTONE_INFDEVCHUNK_H
13+
#include "Lodestone.Minecraft.Java/infdev/InfdevZone.h"
14+
1315
#include <Lodestone.Level/chunk/LevelChunk.h>
1416

1517
namespace lodestone::minecraft::java::infdev::chunk {
1618

1719
class InfdevChunk final : public level::chunk::LevelChunk {
1820
public:
19-
explicit InfdevChunk(const int64_t lastUpdate = 0)
20-
: LevelChunk(128), m_lastUpdate(lastUpdate) {}
21+
static constexpr int CHUNK_WIDTH = 16;
22+
static constexpr int CHUNK_DEPTH = 16;
23+
static constexpr int CHUNK_HEIGHT = 128;
24+
25+
/** Size of the chunk header in bytes */
26+
static constexpr int CHUNK_HEADER_SIZE = sizeof(int32_t) // chunk x coord
27+
+ sizeof(int32_t) // chunk z coord
28+
+ sizeof(int64_t) // inhabited time
29+
+ sizeof(int64_t) // properties
30+
+ 232; // extra padding/reserved
31+
32+
/** Size of the chunk heightmap in bytes */
33+
static constexpr int CHUNK_HEIGHTMAP_SIZE = CHUNK_WIDTH * CHUNK_DEPTH;
34+
/** Size of the chunk block array in bytes */
35+
static constexpr int CHUNK_TOTAL_BLOCKS = CHUNK_WIDTH * CHUNK_DEPTH * CHUNK_HEIGHT;
36+
/** Size of the chunk layer in bytes (used for metadata, skylight, blocklight) */
37+
static constexpr int CHUNK_LAYER_SIZE = CHUNK_TOTAL_BLOCKS / 2;
38+
39+
/** Amount of layers stored in the chunk (DataLayer)
40+
*
41+
* - Metadata
42+
* - Sky light
43+
* - Block light
44+
*/
45+
static constexpr int CHUNK_LAYERS = 3;
46+
47+
/** Incorrect chunk size in bytes, used for getting the zone offset
48+
*
49+
* The calculation is incorrect, please use CHUNK_SIZE_BYTES for the actual size in bytes
50+
*/
51+
static constexpr int CHUNK_SIZE_SLOT_OFFSET = CHUNK_TOTAL_BLOCKS * CHUNK_LAYERS + CHUNK_HEADER_SIZE; // not the true size tho??? I mean technically it works since it will be bigger than the real size...
52+
/** Chunk size in bytes */
53+
static constexpr int CHUNK_BYTES = /*header*/CHUNK_HEADER_SIZE + /*block array*/CHUNK_TOTAL_BLOCKS + /*metadata, skylight, blocklight*/(CHUNK_LAYER_SIZE * CHUNK_LAYERS) + /*heightmap*/CHUNK_HEIGHTMAP_SIZE;
54+
55+
explicit InfdevChunk(const int64_t lastUpdate = 0, const bool isPopulated = true)
56+
: LevelChunk(128), m_lastUpdate(lastUpdate), m_terrainPopulated(isPopulated) {}
2157

2258
explicit InfdevChunk(const level::types::Vec2i &coords,
23-
const int64_t lastUpdate = 0)
24-
: LevelChunk(128, coords), m_lastUpdate(lastUpdate) {}
59+
const int64_t lastUpdate = 0, const bool isPopulated = true)
60+
: LevelChunk(128, coords), m_lastUpdate(lastUpdate), m_terrainPopulated(isPopulated) {}
2561

2662
InfdevChunk(level::chunk::ChunkContainer *container,
2763
const level::types::Vec2i &coords,
28-
const int64_t lastUpdate = 0)
29-
: LevelChunk(128, container, coords), m_lastUpdate(lastUpdate) {}
64+
const int64_t lastUpdate = 0, const bool isPopulated = true)
65+
: LevelChunk(128, container, coords), m_lastUpdate(lastUpdate), m_terrainPopulated(isPopulated) {}
3066

3167
std::string toString() const override {
3268
if (this->m_coords.has_value())
@@ -36,8 +72,22 @@ namespace lodestone::minecraft::java::infdev::chunk {
3672
return std::format("InfdevChunk");
3773
};
3874

75+
constexpr static int getSlotIndex(const int chunkX, const int chunkZ) {
76+
const int zoneX = chunkX >> zone::InfdevZone::CHUNKS_PER_ZONE_BITS;
77+
const int zoneZ = chunkZ >> zone::InfdevZone::CHUNKS_PER_ZONE_BITS;
78+
79+
const int offsetX = chunkX - (zoneX << zone::InfdevZone::CHUNKS_PER_ZONE_BITS);
80+
const int offsetZ = chunkZ - (zoneZ << zone::InfdevZone::CHUNKS_PER_ZONE_BITS);
81+
82+
return offsetX + offsetZ * zone::InfdevZone::CHUNKS_PER_ZONE;
83+
}
84+
85+
constexpr static int getSlotOffset(int slotIndex) {
86+
return (slotIndex - 1) * CHUNK_SIZE_SLOT_OFFSET + zone::InfdevZone::HEADER_SIZE;
87+
}
3988
private:
4089
int64_t m_lastUpdate = 0;
90+
bool m_terrainPopulated = true;
4191
};
4292

4393
}

projects/Converters/Minecraft/Java/include/Lodestone.Minecraft.Java/infdev/InfdevZone.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616
namespace lodestone::minecraft::java::infdev::zone {
1717
class InfdevZone : public level::Level {
1818
public:
19+
static constexpr int HEADER_SIZE = 4096;
20+
static constexpr int MAGIC_NUMBER = 0x13737000;
21+
static constexpr short VERSION = 0;
22+
23+
static constexpr int CHUNKS_PER_ZONE_BITS = 5;
24+
static constexpr int CHUNKS_PER_ZONE = 1 << CHUNKS_PER_ZONE_BITS;
25+
1926
explicit InfdevZone(const level::types::Vec2i &coords) : m_coords(coords) {}
2027

2128
std::string getFilename() const;
2229
static level::types::Vec2i
2330
getCoordsFromFilename(const std::string &name);
31+
32+
static signed int getSlot(int cx, int cz);
2433
private:
2534
const level::types::Vec2i m_coords;
2635
};

projects/Converters/Minecraft/Java/src/infdev/chunk/InfdevChunk.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
*/
1111
#include "Lodestone.Minecraft.Java/infdev/InfdevChunk.h"
1212

13+
#include "Lodestone.Minecraft.Java/infdev/InfdevZone.h"
14+
1315
namespace lodestone::minecraft::java::infdev::chunk {
16+
1417
}

0 commit comments

Comments
 (0)