Skip to content

Instantly share code, notes, and snippets.

@CertainLach
Created September 5, 2015 21:35
Show Gist options
  • Save CertainLach/adee50c5897f0d1127b8 to your computer and use it in GitHub Desktop.
Save CertainLach/adee50c5897f0d1127b8 to your computer and use it in GitHub Desktop.
Precompiled prismarine-chunk and world
'use strict';
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var BUFFER_SIZE = 16 * 16 * 16 * 16 * 3 + 256;
var _require = require('uint4');
var readUInt4LE = _require.readUInt4LE;
var writeUInt4LE = _require.writeUInt4LE;
module.exports = loader;
function loader(mcVersion) {
Block = require('prismarine-block')(mcVersion);
return Chunk;
}
var Block;
var exists = function exists(val) {
return val !== undefined;
};
var getArrayPosition = function getArrayPosition(pos) {
var n = pos.y >> 4;
var y = pos.y % 16;
return n * 4096 + y * 256 + pos.z * 16 + pos.x;
};
var getBlockCursor = function getBlockCursor(pos) {
return getArrayPosition(pos) * 2.0 + 0;
};
var getBlockLightCursor = function getBlockLightCursor(pos) {
return getArrayPosition(pos) * 0.5 + 131072;
};
var getSkyLightCursor = function getSkyLightCursor(pos) {
return getArrayPosition(pos) * 0.5 + 163840;
};
var getBiomeCursor = function getBiomeCursor(pos) {
return 16 * 16 * 16 * 16 * 3 + pos.z * 16 + pos.x; // X and Z may need to be flipped
};
var Chunk = (function () {
function Chunk() {
_classCallCheck(this, Chunk);
this.data = new Buffer(BUFFER_SIZE);
this.data.fill(0);
}
_createClass(Chunk, [{
key: 'getBlock',
value: function getBlock(pos) {
var block = new Block(this.getBlockType(pos), this.getBiome(pos), this.getBlockData(pos));
block.light = this.getBlockLight(pos);
block.skyLight = this.getSkyLight(pos);
return block;
}
}, {
key: 'setBlock',
value: function setBlock(pos, block) {
if (exists(block.type)) this.setBlockType(pos, block.type);
if (exists(block.metadata)) this.setBlockData(pos, block.metadata);
if (exists(block.biome)) this.setBiome(pos, block.biome.id);
if (exists(block.skyLight)) this.setSkyLight(pos, block.skyLight);
if (exists(block.light)) this.setBlockLight(pos, block.light);
}
}, {
key: 'getBlockType',
value: function getBlockType(pos) {
var cursor = getBlockCursor(pos);
return this.data.readUInt16LE(cursor) >> 4;
}
}, {
key: 'getBlockData',
value: function getBlockData(pos) {
var cursor = getBlockCursor(pos);
return this.data.readUInt16LE(cursor) & 15;
}
}, {
key: 'getBlockLight',
value: function getBlockLight(pos) {
var cursor = getBlockLightCursor(pos);
return readUInt4LE(this.data, cursor);
}
}, {
key: 'getSkyLight',
value: function getSkyLight(pos) {
var cursor = getSkyLightCursor(pos);
return readUInt4LE(this.data, cursor);
}
}, {
key: 'getBiome',
value: function getBiome(pos) {
var cursor = getBiomeCursor(pos);
return this.data.readUInt8(cursor);
}
}, {
key: 'setBlockType',
value: function setBlockType(pos, id) {
var cursor = getBlockCursor(pos);
var data = this.getBlockData(pos);
this.data.writeUInt16LE(id << 4 | data, cursor);
}
}, {
key: 'setBlockData',
value: function setBlockData(pos, data) {
var cursor = getBlockCursor(pos);
var id = this.getBlockType(pos);
this.data.writeUInt16LE(id << 4 | data, cursor);
}
}, {
key: 'setBlockLight',
value: function setBlockLight(pos, light) {
var cursor = getBlockLightCursor(pos);
writeUInt4LE(this.data, light, cursor);
}
}, {
key: 'setSkyLight',
value: function setSkyLight(pos, light) {
var cursor = getSkyLightCursor(pos);
writeUInt4LE(this.data, light, cursor);
}
}, {
key: 'setBiome',
value: function setBiome(pos, biome) {
var cursor = getBiomeCursor(pos);
this.data.writeUInt8(biome, cursor);
}
}, {
key: 'dump',
value: function dump() {
return this.data;
}
}, {
key: 'load',
value: function load(data) {
if (!Buffer.isBuffer(data)) throw new Error('Data must be a buffer');
if (data.length != BUFFER_SIZE) throw new Error('Data buffer not correct size (was ' + data.size() + ', expected ' + BUFFER_SIZE + ')');
this.data = data;
}
}]);
return Chunk;
})();
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Vec3 = require("vec3");
function columnKeyXZ(chunkX, chunkZ) {
return chunkX + ',' + chunkZ;
}
function posInChunk(pos) {
return pos.floored().modulus(new Vec3(16, 256, 16));
}
var World = (function () {
function World() {
_classCallCheck(this, World);
this.columns = {};
this.columnsArray = [];
}
_createClass(World, [{
key: "getColumn",
value: function getColumn(chunkX, chunkZ) {
var key = columnKeyXZ(chunkX, chunkZ);
return this.columns[key];
}
}, {
key: "setColumn",
value: function setColumn(chunkX, chunkZ, chunk) {
var key = columnKeyXZ(chunkX, chunkZ);
this.columnsArray.push({ chunkX: chunkX, chunkZ: chunkZ, column: chunk });
this.columns[key] = chunk;
}
}, {
key: "getColumns",
value: function getColumns() {
return this.columnsArray;
}
}, {
key: "getColumnAt",
value: function getColumnAt(pos) {
var chunkX = Math.floor(pos.x / 16);
var chunkZ = Math.floor(pos.z / 16);
return this.getColumn(chunkX, chunkZ);
}
}, {
key: "setBlock",
value: function setBlock(pos, block) {
this.getColumnAt(pos).setBlock(posInChunk(pos), block);
}
}, {
key: "getBlock",
value: function getBlock(pos) {
return this.getColumnAt(pos).getBlock(posInChunk(pos));
}
}, {
key: "getBlockType",
value: function getBlockType(pos) {
return this.getColumnAt(pos).getBlockType(posInChunk(pos));
}
}, {
key: "getBlockData",
value: function getBlockData(pos) {
return this.getColumnAt(pos).getBlockData(posInChunk(pos));
}
}, {
key: "getBlockLight",
value: function getBlockLight(pos) {
return this.getColumnAt(pos).getBlockLight(posInChunk(pos));
}
}, {
key: "getSkyLight",
value: function getSkyLight(pos) {
return this.getColumnAt(pos).getSkyLight(posInChunk(pos));
}
}, {
key: "getBiome",
value: function getBiome(pos) {
return this.getColumnAt(pos).getBiome(posInChunk(pos));
}
}, {
key: "setBlockType",
value: function setBlockType(pos, blockType) {
this.getColumnAt(pos).setBlockType(posInChunk(pos), blockType);
}
}, {
key: "setBlockData",
value: function setBlockData(pos, data) {
this.getColumnAt(pos).setBlockData(posInChunk(pos), data);
}
}, {
key: "setBlockLight",
value: function setBlockLight(pos, light) {
this.getColumnAt(pos).setBlockLight(posInChunk(pos), light);
}
}, {
key: "setSkyLight",
value: function setSkyLight(pos, light) {
this.getColumnAt(pos).setSkyLight(posInChunk(pos), light);
}
}, {
key: "setBiome",
value: function setBiome(pos, biome) {
this.getColumnAt(pos).setBiome(posInChunk(pos), biome);
}
}]);
return World;
})();
module.exports = World;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment