Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Maze building script for MC computercraft turtles
- -- Turtle requires the proper amount of fuel to build before it will start, does not attempt to refuel,
- -- this must be done manually
- -- Builds the maze to the front and left of the turtle.
- -- Can specify size with command line arguments. Example ( to make a 21x9 maze) : Maze 21 9
- local maze = {}
- local args = { ... }
- local xMax = tonumber(args[1])
- local yMax = tonumber(args[2])
- local selection = 1
- turtle.select(1)
- if #args < 2 then
- xMax = 7
- yMax = 7
- end
- if ( xMax < 7 ) then xMax = 7 end
- if ( yMax < 7 ) then yMax = 7 end
- if ( xMax % 2 == 0 ) then xMax = xMax + 1 end
- if ( yMax % 2 == 0 ) then yMax = yMax + 1 end
- local fuelNeeded = yMax * xMax * 3 + yMax * 2 + xMax * 2
- if ( turtle.getFuelLevel() < fuelNeeded ) then
- print("Not enough fuel, need at least " .. fuelNeeded .. " to make a maze of that size")
- return
- end
- function tryForward()
- turtle.dig()
- if ( turtle.forward() == false ) then
- print("Get out of my way!!")
- while ( turtle.forward() == false ) do
- os.sleep(2)
- end
- end
- end
- function initilize(x, y)
- local i = 1
- while i <= x do
- maze[i] = {}
- local j = 1
- while j <= y do
- if ( i % 2 == 0 ) and ( j % 2 == 0 ) then
- maze[i][j] = 2
- else
- maze[i][j] = 1
- end
- j = j + 1
- end
- i = i + 1
- end
- end
- function arrayToCell(A)
- return ( A - 1 ) / 2
- end
- function cellToArray(C)
- return C * 2
- end
- function isOpenCell( cell )
- local x = cell[1]
- local y = cell[2]
- if ( x < 1 or y < 1 ) then return false end
- local cellXMax = arrayToCell( xMax )
- local cellYMax = arrayToCell( yMax )
- if ( x > cellXMax or y > cellYMax ) then return false end
- local arrayX = cellToArray(x)
- local arrayY = cellToArray(y)
- if ( maze[arrayX][arrayY] == 2 ) then
- return true
- else
- return false
- end
- end
- function getNeighbors ( cell )
- local x = cell[1]
- local y = cell[2]
- local neighbors = {}
- local testCell
- testCell = { x , y + 1 }
- if ( isOpenCell(testCell) ) then table.insert(neighbors,testCell) end
- testCell = { x , y - 1 }
- if ( isOpenCell(testCell) ) then table.insert(neighbors,testCell) end
- testCell = { x + 1 , y }
- if ( isOpenCell(testCell) ) then table.insert(neighbors,testCell) end
- testCell = { x - 1, y }
- if ( isOpenCell(testCell) ) then table.insert(neighbors,testCell) end
- return neighbors
- end
- function addToMaze( cell )
- local x = cellToArray( cell[1] )
- local y = cellToArray( cell[2] )
- maze[x][y] = 0
- end
- function addToOpenCells( cell, openCells )
- local newCell = { cell[1] , cell[2] }
- openCells = table.insert(openCells,newCell)
- end
- function openWall( cell, cell2 )
- local x1 = cellToArray( cell[1] )
- local x2 = cellToArray( cell2[1] )
- local y1 = cellToArray( cell[2] )
- local y2 = cellToArray( cell2[2] )
- local midX = ( x1 + x2 ) / 2
- local midY = ( y1 + y2 ) / 2
- maze[midX][midY] = 0
- end
- function makeMaze()
- local openCells = { {math.random(arrayToCell(xMax)),math.random(arrayToCell(yMax))} }
- while #openCells >= 1 do
- local pickedCellNumber = math.random(#openCells)
- local currentCell = openCells[pickedCellNumber]
- addToMaze( currentCell )
- local neighborCells = getNeighbors( currentCell )
- if ( #neighborCells >= 1 ) then
- local neighbor = 1
- --while neighbor <= #neighborCells do
- -- addToOpenCells( neighborCells[neighbor], openCells )
- -- neighbor = neighbor + 1
- --end
- neighbor = math.random(#neighborCells)
- openWall(currentCell, neighborCells[neighbor])
- addToOpenCells(neighborCells[neighbor],openCells)
- addToMaze( neighborCells[neighbor] )
- else
- table.remove(openCells,pickedCellNumber)
- end
- end
- maze[1][2] = 0
- maze[xMax][yMax-1] = 0
- end
- function dropBlock(x,y)
- if ( maze[x][y] == 1 ) then
- while ( turtle.getItemCount(selection) <= 0 ) do
- selection = selection + 1
- if ( selection > 16 ) then
- selection = 1
- if ( turtle.getItemCount(selection) <= 0 ) then
- print ("Need more blocks, please put more in!")
- while ( turtle.getItemCount(selection) <= 0 ) do
- os.sleep(10)
- end
- end
- end
- turtle.select(selection)
- end
- turtle.placeDown()
- else
- turtle.digDown()
- end
- end
- function drawMaze(addFloor)
- local x = 1
- while ( x <= xMax ) do
- local goForward = ( x % 2 == 1 )
- local y
- if ( goForward ) then
- y = 1
- else
- y = yMax
- end
- while ( goForward and y <= yMax ) or ( not goForward and y >= 1 ) do
- dropBlock(x,y)
- tryForward()
- if ( goForward ) then
- y = y + 1
- else
- y = y - 1
- end
- end
- dropBlock(x,y)
- if ( goForward ) then
- turtle.turnLeft()
- tryForward()
- turtle.turnLeft()
- else
- turtle.turnRight()
- tryForward()
- turtle.turnRight()
- end
- tryForward()
- x = x + 1
- end
- end
- function goHome()
- local i = 1
- while i < yMax do
- tryForward()
- i = i + 1
- end
- i = 1
- turtle.turnLeft()
- while i <= xMax do
- tryForward()
- i = i + 1
- end
- turtle.turnLeft()
- turtle.digUp()
- turtle.up()
- end
- function coverMaze()
- turtle.turnLeft()
- tryForward()
- turtle.up()
- local y = 1
- while y <= yMax do
- local x = 1
- while x < xMax do
- dropBlock(1,1)
- tryForward()
- x = x + 1
- end
- dropBlock(1,1)
- if ( y % 2 == 0 ) then
- turtle.turnLeft()
- tryForward()
- turtle.turnLeft()
- else
- turtle.turnRight()
- tryForward()
- turtle.turnRight()
- end
- y = y + 1
- end
- end
- if ( turtle.detectDown() ) then turtle.up() end
- initilize(xMax,yMax)
- makeMaze()
- drawMaze()
- goHome()
- drawMaze()
- if ( #args < 3 or args[3] ~= "-r" ) then
- coverMaze()
- end
- turtle.down()
- turtle.down()
- turtle.down()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement