Fixed .schem exporter, closes #65

This commit is contained in:
Lucas Dower 2022-10-02 15:49:47 +01:00
parent 27e09c36fb
commit f08c027936
2 changed files with 44 additions and 12 deletions

View File

@ -2,13 +2,13 @@ import { NBT, TagType } from 'prismarine-nbt';
import { BlockMesh } from '../block_mesh';
import { AppConstants } from '../constants';
import { AppUtil } from '../util';
import { LOG } from '../util/log_util';
import { MathUtil } from '../util/math_util';
import { saveNBT } from '../util/nbt_util';
import { Vector3 } from '../vector';
import { IExporter } from './base_exporter';
const varintarray = require('varint-array');
export class SchemExporter extends IExporter {
public override getFormatFilter() {
return {
@ -33,27 +33,41 @@ export class SchemExporter extends IExporter {
// https://github.com/SpongePowered/Schematic-Specification/blob/master/versions/schematic-3.md#paletteObject
// const blockMapping: BlockMapping = {};
const test: {[name: string]: { type: TagType, value: any }} = {
const blockMapping: {[name: string]: { type: TagType, value: any }} = {
'minecraft:air': { type: TagType.Int, value: 0 },
};
let blockIndex = 1;
for (const blockName of blockMesh.getBlockPalette()) {
const namespacedBlockName = 'minecraft:' + blockName; // FIXME: All block names should be namespaced on import
// ASSERT(!(namespacedBlockName in blockMapping));
// blockMapping[namespacedBlockName] = blockIndex;
test[namespacedBlockName] = { type: TagType.Int, value: blockIndex };
const namespacedBlockName = AppUtil.Text.namespaceBlock(blockName);
blockMapping[namespacedBlockName] = { type: TagType.Int, value: blockIndex };
++blockIndex;
}
LOG(test);
LOG(blockMapping);
// const paletteObject = SchemExporter._createBlockStatePalette(blockMapping);
const blockData = new Array<number>(sizeVector.x * sizeVector.y * sizeVector.z).fill(0);
for (const block of blockMesh.getBlocks()) {
const indexVector = Vector3.sub(block.voxel.position, bounds.min);
const bufferIndex = SchemExporter._getBufferIndex(sizeVector, indexVector);
const namespacedBlockName = 'minecraft:' + block.blockInfo.name;
blockData[bufferIndex] = test[namespacedBlockName].value;
const namespacedBlockName = AppUtil.Text.namespaceBlock(block.blockInfo.name);
blockData[bufferIndex] = blockMapping[namespacedBlockName].value;
}
const blockEncoding: number[] = [];
for (let i = 0; i < blockData.length; ++i) {
let id = blockData[i];
while ((id & -128) != 0) {
blockEncoding.push(id & 127 | 128);
id >>>= 7;
}
blockEncoding.push(id);
}
for (let i = 0; i < blockEncoding.length; ++i) {
blockEncoding[i] = MathUtil.int8(blockEncoding[i]);
}
const nbt: NBT = {
@ -66,8 +80,8 @@ export class SchemExporter extends IExporter {
Height: { type: TagType.Short, value: sizeVector.y },
Length: { type: TagType.Short, value: sizeVector.z },
PaletteMax: { type: TagType.Int, value: blockIndex },
Palette: { type: TagType.Compound, value: test },
BlockData: { type: TagType.ByteArray, value: varintarray.encode(blockData) },
Palette: { type: TagType.Compound, value: blockMapping },
BlockData: { type: TagType.ByteArray, value: blockEncoding },
},
};

18
src/util/math_util.ts Normal file
View File

@ -0,0 +1,18 @@
export namespace MathUtil {
export function uint8(x: number) {
return x & 0xFF;
}
export function int8(x: number) {
return uint8(x + 0x80) - 0x80;
}
export function uint32(x: number) {
return x >>> 0;
}
export function int32(x: number) {
return uint32(x + 0x80000000) - 0x80000000;
}
}