Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix direct palettes #232

Merged
merged 13 commits into from
Aug 26, 2023
7 changes: 4 additions & 3 deletions src/pc/1.13/ChunkColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const constants = require('../common/constants')
const BitArray = require('../common/BitArray')
const varInt = require('../common/varInt')
const CommonChunkColumn = require('../common/CommonChunkColumn')
const neededBits = require('../common/neededBits')

// wrap with func to provide version specific Block
module.exports = (Block, mcData) => {
Expand All @@ -17,6 +18,7 @@ module.exports = (Block, mcData) => {
this.biomes = Array(
constants.SECTION_WIDTH * constants.SECTION_WIDTH
).fill(1)
this.maxBitsPerBlock = neededBits(Object.values(mcData.blocks).reduce((high, block) => Math.max(high, block.maxStateId), 0))
}

toJson () {
Expand Down Expand Up @@ -235,11 +237,10 @@ module.exports = (Block, mcData) => {
}

// number of items in data array
const numLongs = varInt.read(reader)
const dataArray = new BitArray({
bitsPerValue: Math.ceil((numLongs * 64) / 4096),
bitsPerValue: bitsPerBlock > constants.MAX_BITS_PER_BLOCK ? this.maxBitsPerBlock : bitsPerBlock,
capacity: 4096
}).readBuffer(reader)
}).readBuffer(reader, varInt.read(reader) * 2)

const blockLight = new BitArray({
bitsPerValue: 4,
Expand Down
1 change: 1 addition & 0 deletions src/pc/1.13/ChunkSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class ChunkSection {
varInt.write(smartBuffer, this.data.length())

// write block data
varInt.write(smartBuffer, this.data.length())
this.data.writeBuffer(smartBuffer)

// write block light data
Expand Down
7 changes: 4 additions & 3 deletions src/pc/1.14/ChunkColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ChunkSection = require('../common/CommonChunkSection')(BitArray)
const CommonChunkColumn = require('../common/CommonChunkColumn')
const constants = require('../common/constants')
const varInt = require('../common/varInt')
const neededBits = require('../common/neededBits')

// wrap with func to provide version specific Block
module.exports = (Block, mcData) => {
Expand All @@ -20,6 +21,7 @@ module.exports = (Block, mcData) => {
this.blockLightMask = 0
this.skyLightSections = Array(constants.NUM_SECTIONS + 2).fill(null)
this.blockLightSections = Array(constants.NUM_SECTIONS + 2).fill(null)
this.maxBitsPerBlock = neededBits(Object.values(mcData.blocks).reduce((high, block) => Math.max(high, block.maxStateId), 0))
}

toJson () {
Expand Down Expand Up @@ -257,11 +259,10 @@ module.exports = (Block, mcData) => {
}

// number of items in data array
const numLongs = varInt.read(reader)
const dataArray = new BitArray({
bitsPerValue: Math.ceil((numLongs * 64) / 4096),
bitsPerValue: bitsPerBlock > constants.MAX_BITS_PER_BLOCK ? this.maxBitsPerBlock : bitsPerBlock,
capacity: 4096
}).readBuffer(reader)
}).readBuffer(reader, varInt.read(reader) * 2)

const section = new ChunkSection({
data: dataArray,
Expand Down
7 changes: 4 additions & 3 deletions src/pc/1.15/ChunkColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ChunkSection = require('../common/CommonChunkSection')(BitArray)
const CommonChunkColumn = require('../common/CommonChunkColumn')
const constants = require('../common/constants')
const varInt = require('../common/varInt')
const neededBits = require('../common/neededBits')

// wrap with func to provide version specific Block
module.exports = (Block, mcData) => {
Expand All @@ -18,6 +19,7 @@ module.exports = (Block, mcData) => {
this.blockLightMask = 0
this.skyLightSections = Array(constants.NUM_SECTIONS + 2).fill(null)
this.blockLightSections = Array(constants.NUM_SECTIONS + 2).fill(null)
this.maxBitsPerBlock = neededBits(Object.values(mcData.blocks).reduce((high, block) => Math.max(high, block.maxStateId), 0))
}

toJson () {
Expand Down Expand Up @@ -254,11 +256,10 @@ module.exports = (Block, mcData) => {
}

// number of items in data array
varInt.read(reader) // numLongs
const dataArray = new BitArray({
bitsPerValue: bitsPerBlock,
bitsPerValue: bitsPerBlock > constants.MAX_BITS_PER_BLOCK ? this.maxBitsPerBlock : bitsPerBlock,
capacity: 4096
}).readBuffer(reader)
}).readBuffer(reader, varInt.read(reader) * 2)

const section = new ChunkSection({
data: dataArray,
Expand Down
7 changes: 4 additions & 3 deletions src/pc/1.16/ChunkColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ChunkSection = require('../common/CommonChunkSection')(BitArray)
const CommonChunkColumn = require('../common/CommonChunkColumn')
const constants = require('../common/constants')
const varInt = require('../common/varInt')
const neededBits = require('../common/neededBits')

// wrap with func to provide version specific Block
module.exports = (Block, mcData) => {
Expand All @@ -18,6 +19,7 @@ module.exports = (Block, mcData) => {
this.blockLightMask = 0
this.skyLightSections = Array(constants.NUM_SECTIONS + 2).fill(null)
this.blockLightSections = Array(constants.NUM_SECTIONS + 2).fill(null)
this.maxBitsPerBlock = neededBits(Object.values(mcData.blocks).reduce((high, block) => Math.max(high, block.maxStateId), 0))
}

toJson () {
Expand Down Expand Up @@ -254,11 +256,10 @@ module.exports = (Block, mcData) => {
}

// number of items in data array
varInt.read(reader) // numLongs
const dataArray = new BitArray({
bitsPerValue: bitsPerBlock,
bitsPerValue: bitsPerBlock > constants.MAX_BITS_PER_BLOCK ? this.maxBitsPerBlock : bitsPerBlock,
capacity: 4096
}).readBuffer(reader)
}).readBuffer(reader, varInt.read(reader) * 2)

const section = new ChunkSection({
data: dataArray,
Expand Down
12 changes: 7 additions & 5 deletions src/pc/1.17/ChunkColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ChunkSection = require('../common/CommonChunkSection')(BitArray)
const CommonChunkColumn = require('../common/CommonChunkColumn')
const constants = require('../common/constants')
const varInt = require('../common/varInt')
const neededBits = require('../common/neededBits')

// wrap with func to provide version specific Block
module.exports = (Block, mcData) => {
Expand All @@ -14,6 +15,7 @@ module.exports = (Block, mcData) => {
this.minY = options?.minY ?? 0
this.worldHeight = options?.worldHeight ?? constants.CHUNK_HEIGHT
this.numSections = this.worldHeight >> 4
this.maxBitsPerBlock = neededBits(Object.values(mcData.blocks).reduce((high, block) => Math.max(high, block.maxStateId), 0))

this.sectionMask = new BitArray({
bitsPerValue: 1,
Expand Down Expand Up @@ -181,7 +183,7 @@ module.exports = (Block, mcData) => {
if (stateId === 0) {
return
}
section = new ChunkSection()
section = new ChunkSection({ maxBitsPerBlock: this.maxBitsPerBlock })
if (sectionIndex > this.sectionMask.capacity) {
this.sectionMask = this.sectionMask.resize(sectionIndex)
}
Expand Down Expand Up @@ -296,16 +298,16 @@ module.exports = (Block, mcData) => {
}

// number of items in data array
varInt.read(reader) // numLongs
const dataArray = new BitArray({
bitsPerValue: bitsPerBlock,
bitsPerValue: bitsPerBlock > constants.MAX_BITS_PER_BLOCK ? this.maxBitsPerBlock : bitsPerBlock,
capacity: 4096
}).readBuffer(reader)
}).readBuffer(reader, varInt.read(reader) * 2)

this.sections[y] = new ChunkSection({
data: dataArray,
palette,
solidBlockCount
solidBlockCount,
maxBitsPerBlock: this.maxBitsPerBlock
})
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/pc/1.18/ChunkColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ChunkSection = require('../common/PaletteChunkSection')
const BiomeSection = require('../common/PaletteBiome')
const CommonChunkColumn = require('../common/CommonChunkColumn')
const constants = require('../common/constants')
const neededBits = require('../common/neededBits')

// wrap with func to provide version specific Block
module.exports = (Block, mcData) => {
Expand All @@ -14,9 +15,11 @@ module.exports = (Block, mcData) => {
this.minY = options?.minY ?? 0
this.worldHeight = options?.worldHeight ?? constants.CHUNK_HEIGHT
this.numSections = this.worldHeight >> 4
this.maxBitsPerBlock = neededBits(Object.values(mcData.blocks).reduce((high, block) => Math.max(high, block.maxStateId), 0))
this.maxBitsPerBiome = neededBits(Object.values(mcData.biomes).length)
frej4189 marked this conversation as resolved.
Show resolved Hide resolved

this.sections = options?.sections ?? Array.from(
{ length: this.numSections }, _ => new ChunkSection()
{ length: this.numSections }, _ => new ChunkSection({ maxBitsPerBlock: this.maxBitsPerBlock })
)
this.biomes = options?.biomes ?? Array.from(
{ length: this.numSections }, _ => new BiomeSection()
Expand Down Expand Up @@ -243,8 +246,8 @@ module.exports = (Block, mcData) => {
load (data) {
const reader = SmartBuffer.fromBuffer(data)
for (let i = 0; i < this.numSections; ++i) {
this.sections[i] = ChunkSection.read(reader)
this.biomes[i] = BiomeSection.read(reader)
this.sections[i] = ChunkSection.read(reader, this.maxBitsPerBlock)
this.biomes[i] = BiomeSection.read(reader, this.maxBitsPerBiome)
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/pc/1.9/ChunkColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const constants = require('../common/constants')
const BitArray = require('../common/BitArray')
const varInt = require('../common/varInt')
const CommonChunkColumn = require('../common/CommonChunkColumn')
const neededBits = require('../common/neededBits')

const exists = val => val !== undefined

Expand All @@ -19,6 +20,7 @@ module.exports = (Block, mcData) => {
this.biomes = Array(
constants.SECTION_WIDTH * constants.SECTION_WIDTH
).fill(1)
this.maxBitsPerBlock = neededBits(Object.values(mcData.blocks).reduce((high, block) => Math.max(high, block.maxStateId), 0))
}

toJson () {
Expand Down Expand Up @@ -123,7 +125,9 @@ module.exports = (Block, mcData) => {
if (stateId === 0) {
return
}
section = new ChunkSection()
section = new ChunkSection({
maxBitsPerBlock: this.maxBitsPerBlock
})
this.sectionMask |= 1 << sectionIndex
this.sections[sectionIndex] = section
}
Expand Down Expand Up @@ -221,11 +225,10 @@ module.exports = (Block, mcData) => {
}

// number of items in data array
const numLongs = varInt.read(reader)
const dataArray = new BitArray({
bitsPerValue: Math.ceil((numLongs * 64) / 4096),
bitsPerValue: bitsPerBlock > constants.MAX_BITS_PER_BLOCK ? this.maxBitsPerBlock : bitsPerBlock,
capacity: 4096
}).readBuffer(reader)
}).readBuffer(reader, varInt.read(reader) * 2)

const blockLight = new BitArray({
bitsPerValue: 4,
Expand All @@ -243,7 +246,8 @@ module.exports = (Block, mcData) => {
data: dataArray,
palette,
blockLight,
...(skyLightSent ? { skyLight } : { skyLight: null })
...(skyLightSent ? { skyLight } : { skyLight: null }),
maxBitsPerBlock: this.maxBitsPerBlock
})
this.sections[y] = section
}
Expand Down
7 changes: 3 additions & 4 deletions src/pc/1.9/ChunkSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ChunkSection {
this.blockLight = options.blockLight
this.skyLight = options.skyLight
this.solidBlockCount = options.solidBlockCount
this.maxBitsPerBlock = options.maxBitsPerBlock || GLOBAL_BITS_PER_BLOCK
}

toJson () {
Expand Down Expand Up @@ -117,7 +118,7 @@ class ChunkSection {
} else {
// switches to the global palette
const newData = new BitArray({
bitsPerValue: GLOBAL_BITS_PER_BLOCK,
bitsPerValue: this.maxBitsPerBlock,
capacity: constants.BLOCK_SECTION_VOLUME
})
for (let i = 0; i < constants.BLOCK_SECTION_VOLUME; i++) {
Expand Down Expand Up @@ -181,10 +182,8 @@ class ChunkSection {
varInt.write(smartBuffer, 0)
}

// write the number of longs to be written
varInt.write(smartBuffer, this.data.length())

// write block data
varInt.write(smartBuffer, this.data.length())
this.data.writeBuffer(smartBuffer)

// write block light data
Expand Down
7 changes: 6 additions & 1 deletion src/pc/common/BitArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ class BitArray {
return this.data.length / 2
}

readBuffer (smartBuffer) {
readBuffer (smartBuffer, size = this.data.length) {
if (size !== this.data.length) {
this.data = new Uint32Array(size)
return
}

for (let i = 0; i < this.data.length; i += 2) {
this.data[i + 1] = smartBuffer.readUInt32BE()
this.data[i] = smartBuffer.readUInt32BE()
Expand Down
7 changes: 6 additions & 1 deletion src/pc/common/BitArrayNoSpan.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ class BitArray {
return this.data.length / 2
}

readBuffer (smartBuffer) {
readBuffer (smartBuffer, size = this.data.length) {
if (size !== this.data.length) {
this.data = new Uint32Array(size)
return
}

for (let i = 0; i < this.data.length; i += 2) {
this.data[i + 1] = smartBuffer.readUInt32BE()
this.data[i] = smartBuffer.readUInt32BE()
Expand Down
7 changes: 3 additions & 4 deletions src/pc/common/CommonChunkSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = BitArray => {
this.palette = options.palette
this.isDirty = false
this.solidBlockCount = options.solidBlockCount
this.maxBitsPerBlock = options.maxBitsPerBlock ?? constants.GLOBAL_BITS_PER_BLOCK
}

toJson () {
Expand Down Expand Up @@ -96,7 +97,7 @@ module.exports = BitArray => {
} else {
// switches to the global palette
const newData = new BitArray({
bitsPerValue: constants.GLOBAL_BITS_PER_BLOCK,
bitsPerValue: this.maxBitsPerBlock,
capacity: constants.BLOCK_SECTION_VOLUME
})
for (let i = 0; i < constants.BLOCK_SECTION_VOLUME; i++) {
Expand Down Expand Up @@ -145,10 +146,8 @@ module.exports = BitArray => {
})
}

// write the number of longs to be written
varInt.write(smartBuffer, this.data.length())

// write block data
varInt.write(smartBuffer, this.data.length())
this.data.writeBuffer(smartBuffer)
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/pc/common/PaletteBiome.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class BiomeSection {
this.data.write(smartBuffer)
}

static read (smartBuffer) {
static read (smartBuffer, maxBitsPerBiome = constants.GLOBAL_BITS_PER_BIOME) {
const bitsPerBlock = smartBuffer.readUInt8()
if (!bitsPerBlock) {
const section = new BiomeSection({
Expand All @@ -68,10 +68,9 @@ class BiomeSection {
}

if (bitsPerBlock > constants.MAX_BITS_PER_BIOME) {
varInt.read(smartBuffer)
return new BiomeSection({
data: new DirectPaletteContainer({
bitsPerValue: bitsPerBlock,
bitsPerValue: maxBitsPerBiome,
capacity: constants.BIOME_SECTION_VOLUME
}).readBuffer(smartBuffer)
})
Expand All @@ -83,7 +82,6 @@ class BiomeSection {
palette.push(varInt.read(smartBuffer))
}

varInt.read(smartBuffer)
frej4189 marked this conversation as resolved.
Show resolved Hide resolved
return new BiomeSection({
data: new IndirectPaletteContainer({
bitsPerValue: bitsPerBlock,
Expand Down
Loading
Loading