|
13 | 13 | #include <BinaryIO/stream/BinaryInputStream.h> |
14 | 14 | #include <Lodestone.Common/Indexing.h> |
15 | 15 | #include <Lodestone.Conversion/block/data/NumericBlockData.h> |
| 16 | + |
| 17 | +#include <BinaryIO/stream/BinaryOutputStream.h> |
16 | 18 | #include "Lodestone.Minecraft.Java/LodestoneJava.h" |
17 | 19 | #include "Lodestone.Minecraft.Java/infdev/InfdevChunk.h" |
18 | 20 |
|
19 | | -#include "Lodestone.Minecraft.Java/Identifiers.h" |
20 | | - |
21 | 21 | namespace lodestone::minecraft::java::infdev::chunk { |
22 | 22 | std::unique_ptr<level::chunk::Chunk> InfdevChunkIO::read( |
23 | 23 | const common::conversion::io::options::OptionPresets::CommonReadOptions &options) const { |
@@ -87,6 +87,86 @@ bio::stream::BinaryInputStream bis(options.input); |
87 | 87 | void InfdevChunkIO::write(level::chunk::Chunk *c, |
88 | 88 | const common::conversion::io::options::OptionPresets::CommonChunkWriteOptions &options) |
89 | 89 | const { |
90 | | - throw std::runtime_error("Not implemented"); |
| 90 | + bio::stream::BinaryOutputStream bos(options.output); |
| 91 | + |
| 92 | + const std::unique_ptr<conversion::block::version::BlockIO> |
| 93 | + bio = LodestoneJava::getInstance()->io.getIo(options.version); |
| 94 | + |
| 95 | + const level::types::Vec2i coords = c->getCoords().value(); |
| 96 | + bos.writeBE<int32_t>(coords.x); |
| 97 | + bos.writeBE<int32_t>(coords.y); // Z |
| 98 | + |
| 99 | + // Write padding |
| 100 | + for (int i = 0; i < 232; i++) { |
| 101 | + bos.writeBE<int8_t>(0); |
| 102 | + } |
| 103 | + |
| 104 | + std::vector<int8_t> blocks( |
| 105 | + CHUNK_WIDTH * CHUNK_HEIGHT * |
| 106 | + CHUNK_DEPTH); |
| 107 | + std::vector<int8_t> data((CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) / |
| 108 | + 2); |
| 109 | + std::vector<int8_t> skyLight( |
| 110 | + (CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) / 2); |
| 111 | + std::vector<int8_t> blockLight( |
| 112 | + (CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) / 2); |
| 113 | + std::vector<int8_t> heightMap(CHUNK_WIDTH * CHUNK_DEPTH); |
| 114 | + // todo wiki says there's tileticks which is gonna be pain |
| 115 | + |
| 116 | + int8_t *blockData = blocks.data(); // avoid the annoying bounds checks |
| 117 | + // and just write directly |
| 118 | + int8_t *dataData = data.data(); // welcome back 4J |
| 119 | + int8_t *skyLightData = skyLight.data(); |
| 120 | + int8_t *blockLightData = blockLight.data(); |
| 121 | + int8_t *heightMapData = heightMap.data(); |
| 122 | + |
| 123 | + for (int cx = 0; cx < CHUNK_WIDTH; cx++) { |
| 124 | + for (int cz = 0; cz < CHUNK_DEPTH; cz++) { |
| 125 | + for (int cy = 0; cy < CHUNK_HEIGHT; cy++) { |
| 126 | + const size_t idx = |
| 127 | + INDEX_XZY(cx, cy, cz, InfdevChunkIO::CHUNK_HEIGHT, InfdevChunkIO::CHUNK_DEPTH); |
| 128 | + const level::block::instance::BlockInstance &b = |
| 129 | + c->getBlock(cx, cy, cz); |
| 130 | +#ifdef USE_RISKY_OPTIMIZATIONS |
| 131 | + if (b.getBlock() != |
| 132 | + level::block::BlockRegistry::s_defaultBlock) { |
| 133 | +#endif |
| 134 | + uint8_t id = 0; |
| 135 | + uint8_t dat = 0; |
| 136 | + |
| 137 | + if (const conversion::block::data::NumericBlockData *bl = |
| 138 | + bio->convertBlockFromInternal(&b)->as<conversion::block::data::NumericBlockData>()) { |
| 139 | + id = bl->getId(); |
| 140 | + dat = bl->getData(); |
| 141 | + } |
| 142 | + |
| 143 | + blockData[idx] = id; |
| 144 | + |
| 145 | + SET_NIBBLE(dataData, idx, dat); |
| 146 | +#ifdef USE_RISKY_OPTIMIZATIONS |
| 147 | + } |
| 148 | +#endif |
| 149 | + |
| 150 | + level::chunk::section::Section *s = c->getSection(cy >> 4); |
| 151 | + |
| 152 | + SET_NIBBLE(skyLightData, idx, |
| 153 | + s ? s->getSkyLight()->getNibble(cx, cy & 15, cz) |
| 154 | + : 15); |
| 155 | + SET_NIBBLE( |
| 156 | + blockLightData, idx, |
| 157 | + s ? s->getBlockLight()->getNibble(cx, cy & 15, cz) |
| 158 | + : 15); |
| 159 | + } |
| 160 | + |
| 161 | + heightMapData[INDEX_YX(cx, cz, CHUNK_WIDTH)] = |
| 162 | + c->getHeightAt(cx, cz); |
| 163 | + } |
| 164 | + |
| 165 | + bos.writeBytes(reinterpret_cast<uint8_t*>(blockData), 32768); |
| 166 | + bos.writeBytes(reinterpret_cast<uint8_t*>(dataData), 16384); |
| 167 | + bos.writeBytes(reinterpret_cast<uint8_t*>(skyLightData), 16384); |
| 168 | + bos.writeBytes(reinterpret_cast<uint8_t*>(blockLightData), 16384); |
| 169 | + bos.writeBytes(reinterpret_cast<uint8_t*>(heightMapData), 256); |
| 170 | + } |
91 | 171 | } |
92 | 172 | } |
0 commit comments