Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

0.1.0

  • Loading branch information...
commit ffb6b7378b01d35bd92950d3bbbf2612d90a09e0 1 parent 5f4d947
@maxogden authored
Showing with 54 additions and 33 deletions.
  1. +7 −7 demo.js
  2. +46 −25 index.js
  3. +1 −1  package.json
View
14 demo.js
@@ -9,16 +9,16 @@ function handleFileSelect(evt) {
if (parts[0] !== 'r' && parts[3] !== 'mca') return
var reader = new FileReader()
reader.onloadend = function() {
- var converter = mca2json()
- var count = 1022
- console.log(console.time('load'))
+ var converter = mca2json({ distance: 1 })
+ console.time('load')
converter.on('data', function(chunk) {
- // var rle = crunch.encode(chunk.voxels)
- chunks.push(chunk)
- console.log(count--)
- if (count === 0) console.timeEnd('load')
+ var rle = crunch.encode(chunk.voxels)
+ chunks.push(rle)
// crunch.decode(rle, new Uint32Array(chunk.voxels.length))
})
+ converter.on('end', function(){
+ console.timeEnd('load')
+ })
converter.convert(reader.result, parts[1], parts[2])
}
reader.readAsArrayBuffer(file)
View
71 index.js
@@ -2,8 +2,12 @@ var mcRegion = require('minecraft-region')
var mca = require('minecraft-mca')
var stream = require('stream')
var util = require('util')
+
var width = 16
var height = 256
+var size = 16
+var length = size * size * size
+var bufferType = Uint32Array
module.exports = MCA2JSON
@@ -11,6 +15,7 @@ function MCA2JSON(opts) {
if (!(this instanceof MCA2JSON)) return new MCA2JSON(opts)
if (!opts) opts = {}
this.opts = opts
+ this.chunks = {}
var self = this
stream.Stream.call(self)
self.readable = true
@@ -18,41 +23,57 @@ function MCA2JSON(opts) {
util.inherits(MCA2JSON, stream.Stream)
+MCA2JSON.prototype.chunksInChunk = function(chunkX, chunkZ) {
+ var chunks = []
+ for (var i = 0; i < 256/size; i++) {
+ chunks.push([chunkX * size, i * size, chunkZ * size])
+ }
+ return chunks
+}
+
MCA2JSON.prototype.convert = function(buf, regionX, regionZ) {
var self = this
var region = mcRegion(buf, regionX, regionZ)
- var chunk, lastChunk
- var voxels = newEmptyChunk()
+ var lastChunk
var opts = {ymin: 0, onVoxel: function(x, y, z, block, chunkX, chunkZ) {
- if (!chunk) chunk = {
- dimensions: [16, 256, 16],
- position: [chunkX, 0, chunkZ]
- }
var currentChunk = chunkX + ',' + chunkZ
if (!lastChunk) lastChunk = currentChunk
if (currentChunk !== lastChunk) {
- chunk.voxels = voxels
- self.emit('data', chunk)
- chunk = {
- dimensions: [16, 256, 16],
- position: [chunkX, 0, chunkZ]
- }
- voxels = newEmptyChunk()
+ var lc = lastChunk.split(',').map(function(n) { return +n })
+ self.chunksInChunk(lc[0], lc[1]).map(function(c) {
+ var cidx = c.join('|')
+ var finishedChunk = self.chunks[cidx]
+ if (!finishedChunk) return
+ self.emit('data', finishedChunk)
+ })
lastChunk = currentChunk
}
- x = Math.abs((width + x % width) % width)
- y = Math.abs((height + y % height) % height)
- z = Math.abs((width + z % width) % width)
- var idx = x + y*height + z*width*height
- voxels[idx] = block.id
+ var chunkY = Math.floor(y / size)
+ y = y % size
+ var chunkPos = [chunkX * size, chunkY * size, chunkZ * size]
+ var chunkIDX = chunkPos.join('|')
+ x = Math.abs((size + x % size) % size)
+ y = Math.abs((size + y % size) % size)
+ z = Math.abs((size + z % size) % size)
+ var idx = x + (y * size) + (z * size * size)
+ if (!self.chunks[chunkIDX]) {
+ var chunk = {
+ dimensions: [size, size, size],
+ position: chunkPos,
+ voxels: new bufferType(length)
+ }
+ self.chunks[chunkIDX] = chunk
+ }
+ self.chunks[chunkIDX].voxels[idx] = block.id
}}
var mcaReader = mca(region, opts)
var distance = this.opts.distance || 1
- if (this.opts.start) return mcaReader.loadNearby(this.opts.start, distance)
- if (this.opts.distance) return mcaReader.loadNearby(mcaReader.positionBounds()[0], distance)
- mcaReader.loadAll()
+ if (this.opts.start) {
+ mcaReader.loadNearby(this.opts.start, distance)
+ } else if (this.opts.distance) {
+ mcaReader.loadNearby(mcaReader.positionBounds()[0], distance)
+ } else {
+ mcaReader.loadAll()
+ }
+ self.emit('end')
}
-
-function newEmptyChunk() {
- return new Uint32Array(width * height * width)
-}
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "mca2js",
- "version": "0.0.1",
+ "version": "0.1.0",
"description": "convert minecraft(tm) .mca region files to JS typed arrays",
"main": "index.js",
"scripts": {
Please sign in to comment.
Something went wrong with that request. Please try again.