Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- currentSlot = 1
- location=vector.new(1,0)
- dir=0
- tArgs={...}
- if #tArgs<1 then
- print("Usage: mazegen <width> [length]")
- return
- end
- width=tonumber(tArgs[1])
- height=tonumber(tArgs[2]) or width
- print("Making maze ",width,"x",height)
- cells={}
- for i=0,width+1 do
- cells[i]={}
- for j=0,height+1 do
- cells[i][j]={visited=true,parent=vector.new(),pos=vector.new(i,j),finish=false}
- end
- end
- for i=1,width do
- for j=1,height do
- cells[i][j].visited=false
- end
- end
- cells[width][height+1].finish=true
- cells[width][height+1].visited=false
- function go(goto)
- local goDir=goto-location
- local tardir=0
- if goDir.x>0 then
- tardir=1
- elseif goDir.x<0 then
- tardir=3
- end
- if goDir.y>0 then
- tardir=0
- elseif goDir.y<0 then
- tardir=2
- end
- while dir~=tardir do
- turtle.turnRight()
- dir=(dir+1)%4
- end
- -- print(tardir)
- safeMove()
- cells[goto.x][goto.y].visited=true
- location=goto
- end
- function safeMove()
- while turtle.detect() and not turtle.dig() do
- turtle.attack()
- end
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- turtle.digUp()
- while turtle.detectDown() and not turtle.digDown() do
- turtle.attackDown()
- end
- safePlace()
- while turtle.detect() and not turtle.dig() do
- turtle.attack()
- end
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- turtle.digUp()
- while turtle.detectDown() and not turtle.digDown() do
- turtle.attackDown()
- end
- safePlace()
- end
- function selectSlot()
- while turtle.getItemCount(currentSlot) < 2 do
- currentSlot = currentSlot + 1
- if currentSlot == 15 then
- currentSlot = 1
- sleep(1)
- end
- end
- turtle.select(currentSlot)
- end
- function safePlace()
- selectSlot()
- turtle.placeDown()
- end
- function mazegen(nextpoint)
- cells[nextpoint.x][nextpoint.y].parent=location
- print("Moving forward")
- go(nextpoint)
- if cells[location.x][location.y].finish==true then
- turtle.placeUp()
- print("Found finish")
- else
- 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]}
- local unvisited={}
- for i,v in ipairs(neighbours) do
- if v.visited==false then
- table.insert(unvisited,v)
- end
- end
- while #unvisited>0 do
- local randomnum=math.random(1,#unvisited)
- if cells[unvisited[randomnum].pos.x][unvisited[randomnum].pos.y].visited==false then
- print("New branch at "..tostring(unvisited[randomnum].pos))
- mazegen(unvisited[randomnum].pos)
- end
- table.remove(unvisited,randomnum)
- print(#unvisited .." left")
- end
- end
- print("Backtracking to "..tostring(cells[location.x][location.y].parent))
- go(cells[location.x][location.y].parent)
- end
- mazegen(vector.new(1,1))
- turtle.turnRight()
- turtle.turnRight()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement