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;

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);
}

/**
 * Returns material ids and material mod ids used within the region.
 */
Region.prototype.getAssetIds = function () {
  var materialIds = [], matModIds = [], id;
  for (var offset = 0; offset < BYTES_PER_REGION; offset += BYTES_PER_TILE) {
    id = this.tileView.getInt16(offset);
    if (id >= 0 && materialIds.indexOf(id) == -1) materialIds.push(id);
    id = this.tileView.getInt16(offset + 7);
    if (id >= 0 && materialIds.indexOf(id) == -1) materialIds.push(id);

    id = this.tileView.getInt8(offset + 4);
    if (id >= 0 && matModIds.indexOf(id) == -1) matModIds.push(id);
    id = this.tileView.getInt8(offset + 11);
    if (id >= 0 && matModIds.indexOf(id) == -1) matModIds.push(id);
  }

  return {materials: materialIds, matmods: matModIds};
};

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;

Support getting all the asset ids used within a region