local op = peripheral.wrap("top")

local c = {x=198, y=99, z=369}
local w = op.getWorld(0)

--for k,v in pairs(w) do
--  print(k)
--end

local blocks = {
    ["unknown"] = {
      char="?",
      color={f=colors.white, b=colors.green}
    },
    ["0:0"] = {
      char=".",
      color={f=colors.white, b=colors.black}
    },
    [1] = {
      char="s",
      color={f=colors.white, b=colors.gray}
    },
    [2] = {
      char="g",
      color={f=colors.green, b=colors.brown}
    },
    [3] = {
      char="d",
      color={f=colors.white, b=colors.brown}
    },
    [5] = {
      char="W",
      color={f=colors.white, b=colors.brown}
    },
    [16] = {
      char="c",
      color={f=colors.black, b=colors.gray}
    },
    ["20:0"] = {
      char="G",
      color={f=colors.black, b=colors.white}
    },
    ["31:1"] = {
      char="g",
      color={f=colors.green, b=colors.black}
    },
    [50] = {
      char="t",
      color={f=colors.yellow, b=colors.black}
    },
    [54] = {
      char="c",
      color={f=colors.white, b=colors.brown}
    },
    [68] = {
      char="S",
      color={f=colors.black, b=colors.brown}
    },
    [1287] = {
      char="*",
      color={f=colors.white, b=colors.black}
    },
    ["1947:1"] = { -- BOP Wood Cherry
      char="W",
      color={f=colors.white, b=colors.red}
    },
    [1980] = { -- Puddle
      char="d",
      color={f=colors.blue, b=colors.brown}
    },
    ["2242:2"] = {
      char="P",
      color={f=colors.black, b=colors.white}
    },
    [2249] = {
      char="A",
      color={f=colors.white, b=colors.gray}
    },
    [2250] = { -- Book case
      char="#",
      color={f=colors.blue, b=colors.red}
    },
    [2254] = { -- Shelf
      char="s",
      color={f=colors.brown, b=colors.black}
    },
    [2291] = { -- Seat (Chair)
      char="C",
      color={f=colors.white, b=colors.black}
    },
    [2403] = {
      char="O",
      color={f=colors.cyan, b=colors.gray}
    },
}

local unknown = {}

local r = 8

for y=c.y-2,c.y+4 do
  term.setCursorPos(1,1)
  term.clear()
  print(tostring(c.y) .. " - " .. tostring(y))
  for x=c.x-r,c.x+r do
    for z=c.z-r,c.z+r do
      local id = w.getBlockID(x, y, z)
      local meta = w.getMetadata(x, y, z)
      local b = tostring(id)
      if meta ~=  nil then
        b = b .. ":" .. tostring(meta)
      end
    
      local bl = blocks[b]
      if not bl then
        bl = blocks[id]
      end

      if not bl then
        bl = blocks["unknown"]
        table.insert(unknown, b)
      end

      if bl then
        term.setTextColor(bl.color.f)
        term.setBackgroundColor(bl.color.b)
        write(bl.char)
      else
--        write(b .. "!")
      end
    end

    term.setTextColor(colors.white)
    term.setBackgroundColor(colors.black)
    print()
  end
  sleep(10)
end

--for k,v in pairs(unknown) do
--  print(v)
--end
