1- const BinaryStream = pocketnode ( "utils/BinaryStream " ) ;
1+ const BinaryStream = pocketnode ( "network/minecraft/NetworkBinaryStream " ) ;
22const SubChunk = pocketnode ( "level/chunk/SubChunk" ) ;
33const EmptySubChunk = pocketnode ( "level/chunk/EmptySubChunk" ) ;
44
@@ -7,7 +7,7 @@ class Chunk {
77 this . _x = 0 ;
88 this . _z = 0 ;
99
10- this . _height = 256 ;
10+ this . _height = 16 ;
1111
1212 /**
1313 * @type {Map<number, SubChunk> }
@@ -40,24 +40,8 @@ class Chunk {
4040 this . _x = x ;
4141 this . _z = z ;
4242
43- if ( subChunks . size !== 0 ) {
44- for ( let [ y , chunk ] of subChunks ) {
45- if ( y < 0 || y >= this . _height ) {
46- throw new Error ( "Invalid subchunk index " + y ) ;
47- }
48-
49- if ( chunk . isEmpty ( ) ) {
50- this . _subChunks . set ( y , new EmptySubChunk ( ) ) ;
51- } else {
52- this . _subChunks . set ( y , chunk ) ;
53- }
54- }
55- }
56-
57- for ( let i = 0 ; i < this . _height ; ++ i ) {
58- if ( ! this . _subChunks . has ( i ) ) {
59- this . _subChunks . set ( i , new EmptySubChunk ( ) ) ;
60- }
43+ for ( let y = 0 ; y < this . _height ; y ++ ) {
44+ this . _subChunks . set ( y , subChunks . has ( y ) ? subChunks . get ( y ) : new EmptySubChunk ( ) ) ;
6145 }
6246
6347 if ( heightMap . length === 256 ) {
@@ -76,7 +60,7 @@ class Chunk {
7660 if ( biomes . length !== 0 ) {
7761 throw new Error ( "Wrong Biomes value count, expected 256, got " + biomes . length ) ;
7862 } else {
79- this . _biomes = new Array ( 256 ) . fill ( 0 ) ;
63+ this . _biomes = new Array ( 256 ) . fill ( 0x00 ) ;
8064 }
8165 }
8266 }
@@ -162,7 +146,7 @@ class Chunk {
162146 }
163147
164148 setBlockData ( x , y , z , data ) {
165- return this . getSubChunk ( y >> 4 ) . setBlockData ( x , y & 0x0f , z , data ) ;
149+ return this . getSubChunk ( y >> 4 , true ) . setBlockData ( x , y & 0x0f , z , data ) ;
166150 }
167151
168152 getBlockLight ( x , y , z ) {
@@ -181,17 +165,10 @@ class Chunk {
181165 return this . getSubChunk ( y >> 4 , true ) . setBlockSkyLight ( x , y & 0x0f , z , level ) ;
182166 }
183167
184- getSubChunk ( y , generateNew = false ) {
185- if ( y < 0 || y >= this . _height ) {
186- return new EmptySubChunk ( ) ;
187- } else if ( generateNew && this . _subChunks . has ( y ) instanceof EmptySubChunk ) {
188- this . _subChunks . set ( y , new SubChunk ( ) ) ;
189- }
190-
191- if ( this . _subChunks . get ( y ) === null ) {
192- throw new Error ( "something broke.." ) ;
168+ getSubChunk ( y , genNew = false ) {
169+ if ( genNew && this . _subChunks . get ( y ) instanceof EmptySubChunk ) {
170+ return this . _subChunks . set ( y , new SubChunk ( ) ) . get ( y ) ;
193171 }
194-
195172 return this . _subChunks . get ( y ) ;
196173 }
197174
@@ -222,28 +199,24 @@ class Chunk {
222199 }
223200
224201 recalculateHeightMap ( ) {
225- for ( let z = 0 ; z < 16 ; ++ z ) {
226- for ( let x = 0 ; x < 16 ; ++ x ) {
227- let id = this . getHighestBlockId ( x , z ) ;
228-
202+ for ( let x = 0 ; x < 16 ; x ++ ) {
203+ for ( let z = 0 ; z < 16 ; z ++ ) {
229204 this . setHeightMap ( x , z , this . getHighestBlock ( x , z ) + 1 ) ;
230205 }
231206 }
232207 }
233208
234209 getHighestSubChunk ( ) {
235- let highest = new EmptySubChunk ( ) ;
236- for ( let y = 16 ; y > 0 ; -- y ) {
237- if ( this . _subChunks . has ( y ) ) {
210+ for ( let y = 15 ; y >= 0 ; y -- ) {
211+ if ( ! this . _subChunks . has ( y ) ) {
238212 continue ;
239213 }
240214 if ( this . _subChunks . get ( y ) . isEmpty ( ) ) {
241215 continue ;
242216 }
243- highest = this . _subChunks . get ( y ) ;
244- break ;
217+ return this . _subChunks . get ( y ) ;
245218 }
246- return highest ;
219+ return new EmptySubChunk ( ) ;
247220 }
248221
249222 getHighestBlockId ( x , z ) {
@@ -255,23 +228,50 @@ class Chunk {
255228 }
256229
257230 getHighestBlock ( x , z ) {
258- return this . getHighestSubChunk ( ) . getHighestBlock ( x , z ) ;
231+ let index = this . getHighestSubChunkIndex ( ) ;
232+ if ( index === - 1 ) {
233+ return - 1 ;
234+ }
235+
236+ for ( let y = index ; y >= 0 ; -- y ) {
237+ let height = this . getSubChunk ( y ) . getHighestBlock ( x , z ) | ( y << 4 ) ;
238+ if ( height !== - 1 ) {
239+ return height ;
240+ }
241+ }
242+
243+ return - 1 ;
244+ }
245+
246+ getHighestSubChunkIndex ( ) {
247+ let y ;
248+ for ( y = this . _subChunks . size - 1 ; y >= 0 ; -- y ) {
249+ if ( this . _subChunks . get ( y ) instanceof EmptySubChunk ) {
250+ continue ;
251+ }
252+ break ;
253+ }
254+
255+ return y ;
259256 }
260257
261258 getFilledSubChunks ( ) {
262- this . pruneEmptySubChunks ( ) ;
263- return this . _subChunks . size ;
259+ //this.pruneEmptySubChunks();
260+ //return this._subChunks.size;
261+ return this . getHighestSubChunkIndex ( ) + 1 ;
264262 }
265263
266264 pruneEmptySubChunks ( ) {
267- for ( let [ y , subChunk ] of this . _subChunks ) {
268- if ( y < 0 || y >= this . _height ) {
269- this . _subChunks . delete ( y ) ;
270- } else if ( subChunk instanceof EmptySubChunk ) {
265+ for ( let y = 15 ; y >= 0 ; y -- ) {
266+ if ( ! this . _subChunks . has ( y ) ) {
271267 continue ;
272- } else if ( subChunk . isEmpty ( ) ) {
273- this . _subChunks . set ( y , new EmptySubChunk ( ) ) ;
274268 }
269+
270+ if ( ! this . _subChunks . get ( y ) . isEmpty ( ) ) {
271+ return ;
272+ }
273+
274+ this . _subChunks . delete ( y ) ;
275275 }
276276 }
277277
@@ -305,12 +305,11 @@ class Chunk {
305305 let subChunkCount = this . getFilledSubChunks ( ) ;
306306
307307 stream . writeByte ( subChunkCount ) ;
308- for ( let i = 0 ; i < subChunkCount ; i ++ ) {
309- stream . append ( this . _subChunks . get ( i ) . toBinary ( ) ) ;
308+ for ( let y = 0 ; y < subChunkCount ; ++ y ) {
309+ stream . append ( this . _subChunks . get ( y ) . toBinary ( ) ) ;
310310 }
311311
312- this . _heightMap . forEach ( v => stream . writeShort ( v ) ) ;
313-
312+ this . _heightMap . forEach ( v => stream . writeLShort ( v ) ) ;
314313 this . _biomes . forEach ( v => stream . writeByte ( v ) ) ;
315314 stream . writeByte ( 0 ) ;
316315
@@ -319,7 +318,7 @@ class Chunk {
319318 return stream . getBuffer ( ) ;
320319 }
321320
322- static getIndex ( x , y , z ) {
321+ static getIdIndex ( x , y , z ) {
323322 return ( x << 12 ) | ( z << 8 ) | y ;
324323 }
325324
0 commit comments