-
Notifications
You must be signed in to change notification settings - Fork 55
/
ChunkColumn.js
288 lines (242 loc) · 7.98 KB
/
ChunkColumn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const SmartBuffer = require('smart-buffer').SmartBuffer
const ChunkSection = require('./ChunkSection')
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) => {
return class ChunkColumn extends CommonChunkColumn {
static get section () { return ChunkSection }
constructor () {
super(mcData)
this.sectionMask = 0
this.skyLightSent = true
this.sections = Array(constants.NUM_SECTIONS).fill(null)
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 () {
return JSON.stringify({
biomes: this.biomes,
blockEntities: this.blockEntities,
sectionMask: this.sectionMask,
sections: this.sections.map(section => section === null ? null : section.toJson())
})
}
static fromJson (j) {
const parsed = JSON.parse(j)
const chunk = new ChunkColumn()
chunk.biomes = parsed.biomes
chunk.blockEntities = parsed.blockEntities
chunk.sectionMask = parsed.sectionMask
chunk.sections = parsed.sections.map(s => s === null ? null : ChunkSection.fromJson(s))
return chunk
}
initialize (func) {
const p = { x: 0, y: 0, z: 0 }
for (p.y = 0; p.y < constants.CHUNK_HEIGHT; p.y++) {
for (p.z = 0; p.z < constants.SECTION_WIDTH; p.z++) {
for (p.x = 0; p.x < constants.SECTION_WIDTH; p.x++) {
const block = func(p.x, p.y, p.z)
if (block === null) {
continue
}
this.setBlock(p, block)
}
}
}
}
getBlock (pos) {
const section = this.sections[pos.y >> 4]
const biome = this.getBiome(pos)
if (!section) {
return Block.fromStateId(0, biome)
}
const stateId = section.getBlock(toSectionPos(pos))
const block = Block.fromStateId(stateId, biome)
block.light = this.getBlockLight(pos)
block.skyLight = this.getSkyLight(pos)
block.entity = this.getBlockEntity(pos)
return block
}
setBlock (pos, block) {
if (typeof block.stateId !== 'undefined') {
this.setBlockStateId(pos, block.stateId)
}
if (typeof block.biome !== 'undefined') {
this.setBiome(pos, block.biome.id)
}
if (typeof block.skyLight !== 'undefined' && this.skyLightSent) {
this.setSkyLight(pos, block.skyLight)
}
if (typeof block.light !== 'undefined') {
this.setBlockLight(pos, block.light)
}
if (block.entity) {
this.setBlockEntity(pos, block.entity)
} else {
this.removeBlockEntity(pos)
}
}
getBlockType (pos) {
const blockStateId = this.getBlockStateId(pos)
return mcData.blocksByStateId[blockStateId].id
}
getBlockData (pos) {
const blockStateId = this.getBlockStateId(pos)
return mcData.blocksByStateId[blockStateId].metadata
}
getBlockStateId (pos) {
const section = this.sections[pos.y >> 4]
return section ? section.getBlock(toSectionPos(pos)) : 0
}
getBlockLight (pos) {
const section = this.sections[pos.y >> 4]
return section ? section.getBlockLight(toSectionPos(pos)) : 15
}
getSkyLight (pos) {
const section = this.sections[pos.y >> 4]
return section ? section.getSkyLight(toSectionPos(pos)) : 15
}
getBiome (pos) {
return this.biomes[getBiomeIndex(pos)]
}
getBiomeColor (pos) {
// TODO
return { r: 0, g: 0, b: 0 }
}
setBlockType (pos, id) {
this.setBlockStateId(pos, mcData.blocks[id].minStateId)
}
setBlockData (pos, data) {
this.setBlockStateId(pos, mcData.blocksByStateId[this.getBlockStateId(pos)].minStateId + data)
}
setBlockStateId (pos, stateId) {
const sectionIndex = pos.y >> 4
if (sectionIndex < 0 || sectionIndex >= 16) return
let section = this.sections[sectionIndex]
if (!section) {
// if it's air
if (stateId === 0) {
return
}
section = new ChunkSection({
maxBitsPerBlock: this.maxBitsPerBlock
})
this.sectionMask |= 1 << sectionIndex
this.sections[sectionIndex] = section
}
section.setBlock(toSectionPos(pos), stateId)
}
setBlockLight (pos, light) {
const section = this.sections[pos.y >> 4]
return section && section.setBlockLight(toSectionPos(pos), light)
}
setSkyLight (pos, light) {
const section = this.sections[pos.y >> 4]
return section && section.setSkyLight(toSectionPos(pos), light)
}
setBiome (pos, biome) {
this.biomes[getBiomeIndex(pos)] = biome
}
getMask () {
return this.sectionMask
}
// These methods do nothing, and are present only for API compatibility
dumpBiomes () {
}
dumpLight () {
}
loadLight () {
}
loadBiomes () {
}
dump () {
const smartBuffer = new SmartBuffer()
this.sections.forEach((section, i) => {
if (section !== null && !section.isEmpty()) {
section.write(smartBuffer)
}
})
// write biome data
this.biomes.forEach(biome => {
smartBuffer.writeInt32BE(biome)
})
return smartBuffer.toBuffer()
}
load (data, bitMap = 0xffff, skyLightSent = true, fullChunk = true) {
// make smartbuffer from node buffer
// so that we doesn't need to maintain a cursor
const reader = SmartBuffer.fromBuffer(data)
this.skyLightSent = skyLightSent
this.sectionMask |= bitMap
for (let y = 0; y < constants.NUM_SECTIONS; ++y) {
// does `data` contain this chunk?
if (!((bitMap >> y) & 1)) {
// we can skip write a section if it isn't requested
continue
}
// keep temporary palette
let palette
let skyLight
// get number of bits a palette item use
const bitsPerBlock = reader.readUInt8()
// check if the section uses a section palette
if (bitsPerBlock <= constants.MAX_BITS_PER_BLOCK) {
palette = []
// get number of palette items
const numPaletteItems = varInt.read(reader)
// save each palette item
for (let i = 0; i < numPaletteItems; ++i) {
palette.push(varInt.read(reader))
}
} else {
// global palette is used
palette = null
}
// number of items in data array
const dataArray = new BitArray({
bitsPerValue: bitsPerBlock > constants.MAX_BITS_PER_BLOCK ? this.maxBitsPerBlock : bitsPerBlock,
capacity: 4096
}).readBuffer(reader, varInt.read(reader) * 2)
const blockLight = new BitArray({
bitsPerValue: 4,
capacity: 4096
}).readBuffer(reader)
if (skyLightSent) {
skyLight = new BitArray({
bitsPerValue: 4,
capacity: 4096
}).readBuffer(reader)
}
const section = new ChunkSection({
data: dataArray,
palette,
blockLight,
maxBitsPerBlock: this.maxBitsPerBlock,
...(skyLightSent ? { skyLight } : { skyLight: null })
})
this.sections[y] = section
}
// read biomes
if (fullChunk) {
const p = { x: 0, y: 0, z: 0 }
for (p.z = 0; p.z < constants.SECTION_WIDTH; p.z++) {
for (p.x = 0; p.x < constants.SECTION_WIDTH; p.x++) {
this.setBiome(p, reader.readInt32BE())
}
}
}
}
}
}
function getBiomeIndex (pos) {
return (pos.z * 16) | pos.x
}
function toSectionPos (pos) {
return { x: pos.x, y: pos.y & 15, z: pos.z }
}