Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Stairwell program by Josh Swan
- -- Version 1.3
- -- Instructions: Copy file into 'ComputerCraft/lua/rom/programs/turtle' and rename to 'stairs' (no file suffix).
- local tArgs = { ... }
- if #tArgs ~= 5 then
- print( "Usage: stairs <width> <height> <depth> <railingWidth> <centerColumnWidth>" )
- print( "Start turtle on the right side of the stairs on the floor. Railings of width 'railingWidth' will be added to both sides of the stairs. The stairs will wrap clockwise around a center column of width 'centerColumnWidth'. Turtle will not stop if its inventory is full." )
- return
- end
- -- Mine a stairway to the specified depth and width
- local width = tonumber( tArgs[1] )
- local height = tonumber( tArgs[2] )
- local depth = tonumber( tArgs[3] )
- local railingWidth = tonumber(tArgs[4])
- local columnWidth = tonumber(tArgs[5])
- if columnWidth < 0 then
- columnWidth = 0
- end
- if width < 1 then
- print( "Stair width must be at least 1" )
- return
- end
- if height < 2 then
- print( "Stair height must be at least 2" )
- return
- end
- if depth < 1 then
- print( "Stair depth must be at least 1" )
- return
- end
- if railingWidth < 0 then
- print( "Railing width must be a positive number or zero for no railings" )
- return
- end
- if columnWidth > 0 then
- if columnWidth < 3 and railingWidth > 0 then
- print( "Center column width must be zero for a straight flight of stairs or at least 3 for a stairwell with railings" )
- return
- elseif columnWidth < 2 and railingWidth == 0 then
- print( "Center column width must be zero for a straight flight of stairs or at least 2 for a stairwell without railings" )
- return
- end
- end
- -- Digs stairs/stairwell
- function createStairs(width, height, depth, railingWidth, columnWidth)
- local originalDepth = depth
- local totalDepth = 0
- while totalDepth < originalDepth do
- if columnWidth > 0 then
- depth = columnWidth-1
- end
- local atDepth = false
- for d=1, depth+1, 1 do
- for h=1, height-1, 1 do turtle.up() end
- digForward()
- if d == 1 then
- digStair(width, height - 1, railingWidth)
- elseif d == depth+1 and railingWidth > 0 then
- digStair(width, height - 1, railingWidth)
- else
- digStair(width, height, railingWidth)
- totalDepth = totalDepth + 1
- end
- if totalDepth == originalDepth then
- atDepth = true
- break
- end
- end
- -- Position at top
- for h=1, height-1, 1 do turtle.up() end
- digForward()
- railingOffset(railingWidth)
- if atDepth then
- -- Make space at bottom
- digStair(width+(railingWidth*2), height-1, 0)
- else
- -- Dig landing
- digSquare(width+(railingWidth*2), height)
- -- Reposition for next flight
- for w=1, width-1+(railingWidth), 1 do
- turtle.back()
- end
- turtle.turnRight()
- end
- end
- return true
- end
- -- Helper function for digging a stair
- function digStair(width, height, railingWidth)
- railingOffset(railingWidth)
- -- Dig stair too if there is no railing
- local digHeight = height + 1
- if railingWidth > 0 then
- digHeight = digHeight - 1
- end
- for h=1, digHeight, 1 do
- -- Mine width
- turtle.turnLeft()
- digLine(width-1+(railingWidth*2))
- turtle.turnRight()
- -- Dig down if we haven't reach the stair or railing level yet
- if h <= digHeight-1 then
- turtle.digDown()
- turtle.down()
- end
- end
- -- Cut stair out separately if there is a railing
- if railingWidth > 0 then
- -- Reposition turtle
- turtle.turnLeft()
- for r=1, railingWidth, 1 do
- turtle.forward()
- end
- turtle.digDown()
- turtle.down()
- -- Mine width
- for w=1, width-1, 1 do
- digForward()
- end
- -- Backup
- for w=1, width-1, 1 do
- turtle.back()
- end
- turtle.turnRight()
- end
- end
- -- Handles digging into falling blocks
- function digForward()
- while not turtle.forward() do
- turtle.dig()
- sleep(0.3)
- end
- end
- -- Dig the specified number of blocks in the direction the turtle is facing
- function digLine(length)
- for l=1, length, 1 do
- digForward()
- end
- -- Backup
- for l=1, length, 1 do
- turtle.back()
- end
- end
- -- Offset for right railing
- function railingOffset(railingWidth)
- if railingWidth > 0 then
- turtle.turnRight()
- for r=1, railingWidth, 1 do
- digForward()
- end
- turtle.turnLeft()
- end
- end
- -- Digs square with specified width and height
- function digSquare(width, height)
- for l=1, width, 1 do
- for h=1, height, 1 do
- -- Mine width
- turtle.turnLeft()
- digLine(width-1)
- turtle.turnRight()
- -- Dig down if we haven't reach the floor level
- if h < height then
- turtle.digDown()
- turtle.down()
- end
- end
- if l < width then
- for h=1, height-1, 1 do turtle.up() end
- digForward()
- end
- end
- end
- -- Execute stair creation --
- ----------------------------
- print ("Digging stairs...")
- createStairs(width, height, depth, railingWidth, columnWidth)
Add Comment
Please, Sign In to add comment