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
12 changes: 8 additions & 4 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 @@ -139,7 +141,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 @@ -235,11 +239,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 @@ -257,6 +260,7 @@ module.exports = (Block, mcData) => {
data: dataArray,
palette,
blockLight,
maxBitsPerBlock: this.maxBitsPerBlock,
...(skyLightSent ? { skyLight } : { skyLight: null })
})
this.sections[y] = section
Expand Down
8 changes: 4 additions & 4 deletions src/pc/1.13/ChunkSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const BitArray = require('../common/BitArray')
const neededBits = require('../common/neededBits')
const constants = require('../common/constants')
const varInt = require('../common/varInt')
const GLOBAL_BITS_PER_BLOCK = 13

function getBlockIndex (pos) {
return (pos.y << 8) | (pos.z << 4) | pos.x
Expand Down Expand Up @@ -55,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 @@ -116,7 +118,7 @@ class ChunkSection {
} 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 @@ -177,10 +179,8 @@ class ChunkSection {
})
}

// 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
14 changes: 9 additions & 5 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 @@ -150,7 +152,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 @@ -257,16 +261,16 @@ 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,
palette,
solidBlockCount
solidBlockCount,
maxBitsPerBlock: this.maxBitsPerBlock
})
this.sections[y] = section
}
Expand Down
14 changes: 9 additions & 5 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 @@ -144,7 +146,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 @@ -254,16 +258,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)

const section = new ChunkSection({
data: dataArray,
palette,
solidBlockCount
solidBlockCount,
maxBitsPerBlock: this.maxBitsPerBlock
})
this.sections[y] = section
}
Expand Down
14 changes: 9 additions & 5 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 @@ -144,7 +146,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 @@ -254,16 +258,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)

const section = new ChunkSection({
data: dataArray,
palette,
solidBlockCount
solidBlockCount,
maxBitsPerBlock: this.maxBitsPerBlock
})
this.sections[y] = section
}
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
Loading
Loading