var TILES_X = 32;
var TILES_Y = 32;
var TILES_PER_REGION = TILES_X * TILES_Y;

var BYTES_PER_TILE = 23;
var BYTES_PER_ROW = BYTES_PER_TILE * TILES_X;
var BYTES_PER_REGION = BYTES_PER_TILE * TILES_PER_REGION;

function Region(data) {
  this.buffer = data.buffer;
  this.entities = data.entities;
  this.tileView = new DataView(data.buffer);
}

Region.prototype.getBackground = function (x, y) {
  var offset = this.getTileOffset(x, y);
  return [
    // Material id
    this.tileView.getInt16(offset + 7),
    // Hue shift
    this.tileView.getUint8(offset + 9),
    // Variant?
    this.tileView.getUint8(offset + 10),
    // Mod
    this.tileView.getInt16(offset + 11),
    // Mod hue shift
    this.tileView.getUint8(offset + 13)
  ];
};

Region.prototype.getForeground = function (x, y) {
  var offset = this.getTileOffset(x, y);
  return [
    // Material id
    this.tileView.getInt16(offset),
    // Hue shift
    this.tileView.getUint8(offset + 2),
    // Variant?
    this.tileView.getUint8(offset + 3),
    // Mod
    this.tileView.getInt16(offset + 4),
    // Mod hue shift
    this.tileView.getUint8(offset + 6)
  ];
};

Region.prototype.getMetadata = function (x, y) {
  var offset = this.getTileOffset(x, y);
  return [
    // Liquid
    this.tileView.getUint8(offset + 14),
    // Liquid pressure
    this.tileView.getUint16(offset + 15),
    // Collision map
    this.tileView.getUint8(offset + 17),
    // ?
    this.tileView.getInt16(offset + 18),
    // Biome?
    this.tileView.getUint8(offset + 20),
    // Biome?
    this.tileView.getUint8(offset + 21),
    // Indestructible
    !!this.tileView.getUint8(offset + 22)
  ];
};

Region.prototype.getTileOffset = function (x, y) {
  return BYTES_PER_REGION - BYTES_PER_ROW * (y + 1) + BYTES_PER_TILE * x;
};

exports.Region = Region;

@@ -336,16 +336,780 @@
 er);%0A%7D%0A%0A
+/**%0A * Returns material ids and material mod ids used within the region.%0A */%0ARegion.prototype.getAssetIds = function () %7B%0A  var materialIds = %5B%5D, matModIds = %5B%5D, id;%0A  for (var offset = 0; offset %3C BYTES_PER_REGION; offset += BYTES_PER_TILE) %7B%0A    id = this.tileView.getInt16(offset);%0A    if (id %3E= 0 && materialIds.indexOf(id) == -1) materialIds.push(id);%0A    id = this.tileView.getInt16(offset + 7);%0A    if (id %3E= 0 && materialIds.indexOf(id) == -1) materialIds.push(id);%0A%0A    id = this.tileView.getInt8(offset + 4);%0A    if (id %3E= 0 && matModIds.indexOf(id) == -1) matModIds.push(id);%0A    id = this.tileView.getInt8(offset + 11);%0A    if (id %3E= 0 && matModIds.indexOf(id) == -1) matModIds.push(id);%0A  %7D%0A%0A  return %7Bmaterials: materialIds, matmods: matModIds%7D;%0A%7D;%0A%0A
 Region.p

Support getting all the asset ids used within a region