Advertisement
hoblin

Maze

Dec 2nd, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. currentSlot = 1
  2. location=vector.new(1,0)
  3. dir=0
  4. tArgs={...}
  5. if #tArgs<1 then
  6. print("Usage: mazegen <width> [length]")
  7. return
  8. end
  9. width=tonumber(tArgs[1])
  10. height=tonumber(tArgs[2]) or width
  11. print("Making maze ",width,"x",height)
  12. cells={}
  13.  
  14. for i=0,width+1 do
  15.  cells[i]={}
  16.  for j=0,height+1 do
  17.   cells[i][j]={visited=true,parent=vector.new(),pos=vector.new(i,j),finish=false}
  18.  end
  19. end
  20.  
  21. for i=1,width do
  22. for j=1,height do
  23.  cells[i][j].visited=false
  24. end
  25. end
  26.  
  27. cells[width][height+1].finish=true
  28. cells[width][height+1].visited=false
  29.  
  30. function go(goto)
  31.  local goDir=goto-location
  32.  local tardir=0
  33.  if goDir.x>0 then
  34.   tardir=1
  35.  elseif goDir.x<0 then
  36.   tardir=3
  37.  end
  38.  if goDir.y>0 then
  39.   tardir=0
  40.  elseif goDir.y<0 then
  41.   tardir=2
  42.  end
  43.  while dir~=tardir do
  44.   turtle.turnRight()
  45.   dir=(dir+1)%4
  46.  end
  47.  -- print(tardir)
  48.  safeMove()
  49.  cells[goto.x][goto.y].visited=true
  50.  location=goto
  51. end
  52. function safeMove()
  53.   while turtle.detect() and not turtle.dig() do
  54.     turtle.attack()
  55.   end
  56.   while not turtle.forward() do
  57.     turtle.dig()
  58.     turtle.attack()
  59.   end
  60.   turtle.digUp()
  61.   while turtle.detectDown() and not turtle.digDown() do
  62.     turtle.attackDown()
  63.   end
  64.   safePlace()
  65.   while turtle.detect() and not turtle.dig() do
  66.     turtle.attack()
  67.   end
  68.   while not turtle.forward() do
  69.     turtle.dig()
  70.     turtle.attack()
  71.   end
  72.   turtle.digUp()
  73.   while turtle.detectDown() and not turtle.digDown() do
  74.     turtle.attackDown()
  75.   end
  76.   safePlace()
  77. end
  78. function selectSlot()
  79.   while turtle.getItemCount(currentSlot) < 2 do
  80.     currentSlot = currentSlot + 1
  81.     if currentSlot == 15 then
  82.       currentSlot = 1
  83.       sleep(1)
  84.     end
  85.   end
  86.   turtle.select(currentSlot)
  87. end
  88. function safePlace()
  89.   selectSlot()
  90.   turtle.placeDown()
  91. end
  92. function mazegen(nextpoint)
  93.  cells[nextpoint.x][nextpoint.y].parent=location
  94.  print("Moving forward")
  95.  go(nextpoint)
  96.  if cells[location.x][location.y].finish==true then
  97.   turtle.placeUp()
  98.   print("Found finish")
  99.  else
  100.   local neighbours={cells[nextpoint.x-1][nextpoint.y],cells[nextpoint.x+1][nextpoint.y],cells[nextpoint.x][nextpoint.y-1],cells[nextpoint.x][nextpoint.y+1]}
  101.   local unvisited={}
  102.   for i,v in ipairs(neighbours) do
  103.    if v.visited==false then
  104.     table.insert(unvisited,v)
  105.    end
  106.   end
  107.   while #unvisited>0 do
  108.    local randomnum=math.random(1,#unvisited)
  109.    if cells[unvisited[randomnum].pos.x][unvisited[randomnum].pos.y].visited==false then
  110.     print("New branch at "..tostring(unvisited[randomnum].pos))
  111.     mazegen(unvisited[randomnum].pos)
  112.    end
  113.    table.remove(unvisited,randomnum)
  114.    print(#unvisited .." left")
  115.   end
  116.  end
  117.  print("Backtracking to "..tostring(cells[location.x][location.y].parent))
  118.  go(cells[location.x][location.y].parent)
  119. end
  120.  
  121. mazegen(vector.new(1,1))
  122. turtle.turnRight()
  123. turtle.turnRight()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement