makeclass('player')
makeclass('buff')

function player.interface(actor,place)
  gui.menu_item("cb_north","Go North")
  gui.menu_item("cb_south","Go South")
  gui.menu_item("cb_west" ,"Go West")
  gui.menu_item("cb_east" ,"Go East")
  gui.menu_item("cb_emit" ,"Emit Buff")
  gui.menu_item("cb_map"  ,"Where Am I?")
  end

function player.cb_where(actor,place)
  pprint(tangible.xyz(actor))
  end
  
function player.cb_north(actor,place) tangible.animate(actor,{action="walk",dy= 1}) end
function player.cb_south(actor,place) tangible.animate(actor,{action="walk",dy=-1}) end
function player.cb_west (actor,place) tangible.animate(actor,{action="walk",dx=-1}) end
function player.cb_east (actor,place) tangible.animate(actor,{action="walk",dx= 1}) end

function player.cb_emit(actor,place)
  tangible.build{class='buff',x=0,y=0,z=0,plane='main',graphic='Ring'}
  end

function seq(a,b,c) return a<=b and b<=c or false end

function player.cb_map(actor,place)
  local rad=2
  local map={}
  local lis=tangible.near(actor,rad,true,false)
  local ax,ay=tangible.xyz(actor)
  for _,t in ipairs(lis) do
    local x,y=tangible.xyz(t)
    local dx=ax-x
    local dy=ay-y
    local class=tangible.getclass(t)
    if seq(-rad,dx,rad) and seq(-rad,dy,rad) then
      local offset=(dy+rad)*(rad*2+1)+dx+rad
      if not map[offset] then map[offset]={} end
      if class=='buff'         then map[offset].buff=true
        elseif class=='player' then map[offset].player=true
	end
      end
    end
  for dy=-rad,rad do for line=1,3 do
    local str=""
    for dx=-rad,rad do
      local offset=(dy+rad)*(rad*2+1)+dx+rad
      str=str.."---"
      end
    print(str)
    end end
  end

