Skip to content
This repository was archived by the owner on Feb 9, 2020. It is now read-only.

Commit 2d11db7

Browse files
committed
hopefully these changes are for the best
1 parent 95a4623 commit 2d11db7

2 files changed

Lines changed: 40 additions & 68 deletions

File tree

src/pocketnode/level/chunk/Chunk.js

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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){
@@ -264,14 +237,16 @@ class Chunk {
264237
}
265238

266239
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){
240+
for(let y = 15; y >= 0; y--){
241+
if(!this._subChunks.has(y)){
271242
continue;
272-
}else if(subChunk.isEmpty()){
273-
this._subChunks.set(y, new EmptySubChunk());
274243
}
244+
245+
if(!this._subChunks.get(y).isEmpty()){
246+
return;
247+
}
248+
249+
this._subChunks.delete(y);
275250
}
276251
}
277252

@@ -305,12 +280,11 @@ class Chunk {
305280
let subChunkCount = this.getFilledSubChunks();
306281

307282
stream.writeByte(subChunkCount);
308-
for(let i = 0; i < subChunkCount; i++){
309-
stream.append(this._subChunks.get(i).toBinary());
283+
for(let y = 0; y < subChunkCount; ++y){
284+
stream.append(this._subChunks.get(y).toBinary());
310285
}
311286

312-
this._heightMap.forEach(v => stream.writeShort(v));
313-
287+
this._heightMap.forEach(v => stream.writeLShort(v));
314288
this._biomes.forEach(v => stream.writeByte(v));
315289
stream.writeByte(0);
316290

@@ -319,7 +293,7 @@ class Chunk {
319293
return stream.getBuffer();
320294
}
321295

322-
static getIndex(x, y, z){
296+
static getIdIndex(x, y, z){
323297
return (x << 12) | (z << 8) | y;
324298
}
325299

src/pocketnode/level/chunk/SubChunk.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class SubChunk extends SubChunkInterface {
3939
}
4040

4141
getBlockId(x, y, z){
42-
return this._blockIds[SubChunk.getIndex(x, y, z)];
42+
return this._blockIds[SubChunk.getIdIndex(x, y, z)];
4343
}
4444

4545
setBlockId(x, y, z, id){
46-
this._blockIds[SubChunk.getIndex(x, y, z)] = id;
46+
this._blockIds[SubChunk.getIdIndex(x, y, z)] = id;
4747
return true;
4848
}
4949

@@ -95,10 +95,10 @@ class SubChunk extends SubChunkInterface {
9595
}
9696
}
9797

98-
setBlockSkyLight(x, y, z){
98+
setBlockSkyLight(x, y, z, level){
9999
let i = SubChunk.getLightIndex(x, y, z);
100100
let byte = this._skyLight[i];
101-
if((y & 1) === 0){
101+
if((y & 0x01) === 0){
102102
this._skyLight[i] = (byte & 0xf0) | (level & 0x0f);
103103
}else{
104104
this._skyLight[i] = ((level & 0x0f) << 4) | (byte & 0x0f);
@@ -107,15 +107,13 @@ class SubChunk extends SubChunkInterface {
107107
}
108108

109109
getHighestBlockId(x, z){
110-
let low = (x << 8) | (z << 4);
111-
let i = low | 0x0f;
112-
for(; i >= low; --i){
113-
if(this._blockIds[i] !== 0x00){
114-
return i & 0x0f;
110+
for(let y = 15; y >= 0; y--){
111+
let id = this.getBlockId(x, y, z);
112+
if(id !== 0){
113+
return id;
115114
}
116115
}
117-
118-
return -1;
116+
return 0;
119117
}
120118

121119
getHighestBlockData(x, z){
@@ -124,7 +122,7 @@ class SubChunk extends SubChunkInterface {
124122

125123
getHighestBlock(x, z){
126124
for(let y = 15; y >= 0; y--){
127-
if(this.getBlockData(x, y, z) !== 0){
125+
if(this.getBlockId(x, y, z) !== 0){
128126
return y;
129127
}
130128
}
@@ -133,12 +131,10 @@ class SubChunk extends SubChunkInterface {
133131
}
134132

135133
toBinary(){
136-
let a = Buffer.from(this._blockIds);
137-
let b = Buffer.from(this._blockData);
138-
return Buffer.concat([a, b]);
134+
return Buffer.from([0x00, ...this._blockIds, ...this._blockData]);
139135
}
140136

141-
static getIndex(x, y, z){
137+
static getIdIndex(x, y, z){
142138
return (x << 8) | (z << 4) | y;
143139
}
144140

@@ -151,4 +147,6 @@ class SubChunk extends SubChunkInterface {
151147
}
152148
}
153149

150+
let subchunk = new SubChunk();
151+
154152
module.exports = SubChunk;

0 commit comments

Comments
 (0)