Skip to content

Commit

Permalink
Merge pull request #465 from PrismarineJS/pchunk
Browse files Browse the repository at this point in the history
use prismarine-chunk, work on #334
  • Loading branch information
rom1504 committed Jan 8, 2017
2 parents 66dfdff + 0da3b21 commit a4c3259
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 65 deletions.
77 changes: 12 additions & 65 deletions lib/plugins/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Block = require("prismarine-block")(version);
var Painting = require('../painting');
var Location = require('../location');
var ChatMessage = require('../chat_message');
var Chunk = require('prismarine-chunk')(version);

module.exports = inject;

Expand Down Expand Up @@ -42,47 +43,10 @@ function inject(bot) {
return;
}
var column = columns[key];
if(!column) columns[key] = column = new Column();
var chunkIncluded = new Array(16);
var y;
for(y = 0; y < 16; ++y) {
chunkIncluded[y] = args.bitMap & (1 << y);
}

var offset = 0;
if(!column) columns[key] = column = new Chunk();

// block types
var size = 16 * 16 * 16 * 2;
for(y = 0; y < 16; ++y) {
var blockId = args.data.slice(offset, offset + size);

// Column.metadata doesn't exist anymore.
column.blockType[y] = chunkIncluded[y] ? blockId : null;
offset = chunkIncluded[y] ? offset + size : offset;
}
size = 16 * 16 * 16 / 2;
// light
for(y = 0; y < 16; ++y) {
column.light[y] = chunkIncluded[y] ?
args.data.slice(offset, offset + size) : null;
offset = chunkIncluded[y] ? offset + size : offset;
}
// sky light
if(args.skyLightSent) {
for(y = 0; y < 16; ++y) {
column.skyLight[y] = chunkIncluded[y] ?
args.data.slice(offset, offset + size) : null;
offset = chunkIncluded[y] ? offset + size : offset;
}
}
// biome
if(args.groundUp) {
size = 256;
column.biome = args.data.slice(offset, offset + size);
offset += size;
}
column.load(args.data,args.bitMap);

assert.strictEqual(offset, args.data.length);
bot.emit("chunkColumnLoad", columnCorner);
}

Expand Down Expand Up @@ -115,36 +79,25 @@ function inject(bot) {
}
}

function posInChunk(pos)
{
return pos.floored().modulus(new Vec3(16,256,16));
}

function blockAt(absolutePoint) {
var loc = new Location(absolutePoint);
var key = columnKeyXZ(loc.chunkCorner.x, loc.chunkCorner.z);

var column = columns[key];
// null column means chunk not loaded
if(!column) return null;
var blockType = double_bite(column.blockType);
var nibbleIndex = loc.blockIndex >> 1;
var lowNibble = loc.blockIndex % 2 === 1;

var biomeId = column.biome.readUInt8(loc.biomeBlockIndex);

var block = new Block(blockType >> 4, biomeId, blockType & 0x0f);
block.light = nib(column.light);
block.skyLight = nib(column.skyLight);
block=column.getBlock(posInChunk(absolutePoint));
block.position = loc.floored;
block.signText = signs[loc.floored];
block.painting = paintingsByPos[loc.floored];

return block;

function double_bite(array) {
var buf = array[loc.chunkYIndex];
return buf ? buf.readUInt16LE(loc.blockIndex * 2) : 0;
}

function nib(array) {
var buf = array[loc.chunkYIndex];
return buf ? nibble(buf.readUInt8(nibbleIndex), lowNibble) : 0;
}
}

function chunkColumn(x, z) {
Expand All @@ -167,14 +120,8 @@ function inject(bot) {
// sometimes minecraft server sends us block updates before it sends
// us the column that the block is in. ignore this.
if(!column) return;
var blockTypeBuffer = column.blockType[loc.chunkYIndex];
// if it's null, it was all air, but now we're inserting a block.
if(!blockTypeBuffer) {
blockTypeBuffer = new Buffer(16 * 16 * 16 * 2);
blockTypeBuffer.fill(0);
column.blockType[loc.chunkYIndex] = blockTypeBuffer;
}
blockTypeBuffer.writeUInt16LE((type << 4) | metadata, loc.blockIndex * 2);
column.setBlockType(posInChunk(point),type);
column.setBlockData(posInChunk(point),metadata);

delete signs[loc.floored];

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"mojangson": "^0.2.1",
"prismarine-biome": "^1.0.1",
"prismarine-block": "^1.0.1",
"prismarine-chunk": "^1.3.0",
"prismarine-entity": "^0.2.0",
"prismarine-item": "^1.0.1",
"prismarine-recipe": "^1.0.1",
Expand Down

0 comments on commit a4c3259

Please sign in to comment.