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

Commit 434848c

Browse files
committed
CHUNKS!
1 parent 91061b3 commit 434848c

5 files changed

Lines changed: 56 additions & 35 deletions

File tree

src/pocketnode/level/chunk/Chunk.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,22 @@ class Chunk {
228228
}
229229

230230
getHighestBlock(x, z){
231-
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;
232244
}
233245

234-
getFilledSubChunks(){
235-
//this.pruneEmptySubChunks();
236-
//return this._subChunks.size;
246+
getHighestSubChunkIndex(){
237247
let y;
238248
for(y = this._subChunks.size - 1; y >= 0; --y){
239249
if(this._subChunks.get(y) instanceof EmptySubChunk){
@@ -242,7 +252,13 @@ class Chunk {
242252
break;
243253
}
244254

245-
return y + 1;
255+
return y;
256+
}
257+
258+
getFilledSubChunks(){
259+
//this.pruneEmptySubChunks();
260+
//return this._subChunks.size;
261+
return this.getHighestSubChunkIndex() + 1;
246262
}
247263

248264
pruneEmptySubChunks(){

src/pocketnode/network/PlayerSessionAdapter.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,35 +123,37 @@ class PlayerSessionAdapter {
123123
handleRequestChunkRadius(packet){
124124
this.player.setViewDistance(packet.radius);
125125

126-
let distance = this.player.getViewDistance();
127-
for(let chunkX = -distance; chunkX <= distance; chunkX++) {
128-
for(let chunkZ = -distance; chunkZ <= distance; chunkZ++) {
129-
let chunk = new Chunk(chunkX, chunkZ);
130-
131-
for(let x = 0; x < 16; x++){
132-
for(let z = 0; z < 16; z++){
133-
let y = 0;
134-
chunk.setBlockId(x, y++, z, 7);
135-
chunk.setBlockId(x, y++, z, 3);
136-
chunk.setBlockId(x, y++, z, 3);
137-
chunk.setBlockId(x, y, z, 2);
138-
139-
/*for (let i = y - 1; i >= 0; i--) {
140-
chunk.setBlockSkyLight(x, y, z, 0);
141-
}*/
126+
Async(function() {
127+
let distance = this.player.getViewDistance();
128+
for (let chunkX = -distance; chunkX <= distance; chunkX++) {
129+
for (let chunkZ = -distance; chunkZ <= distance; chunkZ++) {
130+
let chunk = new Chunk(chunkX, chunkZ);
131+
132+
for (let x = 0; x < 16; x++) {
133+
for (let z = 0; z < 16; z++) {
134+
let y = 0;
135+
chunk.setBlockId(x, y++, z, 7);
136+
chunk.setBlockId(x, y++, z, 3);
137+
chunk.setBlockId(x, y++, z, 3);
138+
chunk.setBlockId(x, y, z, 2);
139+
140+
/*for (let i = y - 1; i >= 0; i--) {
141+
chunk.setBlockSkyLight(x, y, z, 0);
142+
}*/
143+
}
142144
}
143-
}
144145

145-
//chunk.recalculateHeightMap();
146-
//if(chunkX === -distance && chunkZ === -distance) console.log(`${chunk.toBinary().length}`);// > ${chunk.toBinary().toString("hex")}`);
146+
chunk.recalculateHeightMap();
147+
if (chunkX === -distance && chunkZ === -distance) console.log(`${chunk.toBinary().length} > ${chunk.toBinary().toString("hex")}`);
147148

148-
this.player.sendChunk(chunk);
149+
this.player.sendChunk(chunk);
150+
}
149151
}
150-
}
151-
152-
console.log("done sending chunks");
153-
this.player.sendPlayStatus(PlayStatusPacket.PLAYER_SPAWN);
154-
152+
}.bind(this))
153+
.then(function(){
154+
console.log("done sending chunks");
155+
this.player.sendPlayStatus(PlayStatusPacket.PLAYER_SPAWN);
156+
}.bind(this));
155157
return true;
156158
}
157159

src/pocketnode/network/minecraft/NetworkBinaryStream.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const UUID = pocketnode("utils/UUID");
22

3-
class NetworkBinaryStream extends (pocketnode.RUNNING_LOCALLY ? require("../../../../../PocketNode-BinaryStream") : require("pocketnode-binarystream")) {
3+
class NetworkBinaryStream extends require("pocketnode-binarystream") {
44
/**
55
* @return {string}
66
*/
@@ -13,9 +13,9 @@ class NetworkBinaryStream extends (pocketnode.RUNNING_LOCALLY ? require("../../.
1313
* @return {NetworkBinaryStream}
1414
*/
1515
writeString(v){
16-
this.writeUnsignedVarInt(v.length);
16+
this.writeUnsignedVarInt(Buffer.byteLength(v));
1717
if(v.length === 0) return this;
18-
this.append(Buffer.from(v));
18+
this.append(Buffer.from(v, "utf8"));
1919
return this;
2020
}
2121

src/pocketnode/network/minecraft/protocol/FullChunkDataPacket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class FullChunkDataPacket extends DataPacket {
2020
_decodePayload(){
2121
this.chunkX = this.readVarInt();
2222
this.chunkZ = this.readVarInt();
23-
this.data = this.readString(true);
23+
this.data = this.read(this.readUnsignedVarInt());
2424
}
2525

2626
_encodePayload(){

src/pocketnode/utils/methods/Globals.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,7 @@ global.createInterval = function(fn, interval){
229229

230230
this.stop = () => clearTimeout(this.timer);
231231
});
232-
};
232+
};
233+
234+
global.TRAVIS_BUILD = process.argv.indexOf("--travis-build") !== -1;
235+
global.RUNNING_LOCALLY = (process.argv.indexOf("--local") !== -1 || process.argv.indexOf("-l") !== -1);

0 commit comments

Comments
 (0)