Advertisement
SiliconUnicorn

stairs2

May 28th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. local width  = tonumber(tArgs[1])
  4. local length = tonumber(tArgs[2])
  5. local depth  = tonumber(tArgs[3])
  6.  
  7.  
  8. local dir = 0
  9. local NORTH,EAST,SOUTH,WEST = 0,1,2,3
  10. local INPUT_PIPE = 1
  11. local TRANSPORT_PIPE = 2
  12.  
  13.  
  14.  
  15. if width > 0 and length > 0 and depth > 0 then
  16.   print ("Digging a "..tostring(width)
  17.          .." x "..tostring(length)
  18.          .." x "..tostring(depth)
  19.          .." square.")
  20. end
  21.  
  22. local function refuel()
  23.   if turtle.getFuelLevel() < 20 then
  24.     local i=1
  25.     while not turtle.refuel(i) do
  26.       turtle.select(i)  
  27.       i = i + 1
  28.       if i > 16 then
  29.         print("Need fuel to continue...")
  30.       break
  31.       end
  32.     end
  33.   end
  34. end
  35.  
  36.  
  37. local function forward()
  38.   while turtle.detect() do
  39.     turtle.dig()
  40.   end
  41.   refuel()
  42.   turtle.forward()
  43. end
  44.  
  45. local function turnRight()
  46.   turtle.turnRight()
  47.   dir = (dir + 1) % 4
  48. end
  49.  
  50. local function turnLeft()
  51.   turtle.turnLeft()
  52.   dir = (dir - 1) % 4
  53. end
  54.  
  55. local function face(d)
  56.  
  57.  local delta = (d - dir) % 4
  58.  
  59.  if delta == 0 then
  60.    return
  61.  elseif delta == 1 then
  62.    turnRight()  
  63.  elseif delta == 2 then
  64.    turnRight()
  65.    turnRight()
  66.  elseif delta == 3 then
  67.    turnLeft()
  68.  end
  69. end
  70.  
  71. local function placePipe(n)
  72.   turtle.select(n)
  73.   turtle.place()
  74. end
  75.  
  76. local function placeInputPipe()
  77.   placePipe(INPUT_PIPE)
  78. end
  79.  
  80. local function placeTransportPipe()
  81.   placePipe(TRANSPORT_PIPE)
  82. end
  83.  
  84. local function minePipe()
  85.   turtle.select(1)
  86.   turtle.dig()
  87. end
  88.  
  89. local function unload()
  90.   face(WEST)
  91.   placeInputPipe()
  92.   turtle.up()
  93.   turtle.up()
  94.   forward()
  95.  
  96.   for i=3,16 do
  97.     turtle.select(i)
  98.     turtle.dropDown()
  99.   end
  100.  
  101.   turtle.back()
  102.   turtle.down()
  103.   placeTransportPipe()
  104.   turtle.down()
  105.   minePipe()
  106.   placeTransportPipe()
  107. end
  108.  
  109. local function digRow(n)
  110.   for i=1,n-1 do
  111.     turtle.dig()
  112.     forward()
  113.   end
  114. end
  115.        
  116. local function advanceRowOdd()
  117.   face(EAST)
  118.   turtle.dig()
  119.   forward()
  120.   face(NORTH)
  121. end
  122.  
  123. local function advanceRowEven()
  124.  face(EAST)
  125.  turtle.dig()
  126.  forward()
  127.  face(SOUTH)
  128. end
  129.  
  130. local function advanceLevel()
  131.  unload()
  132.  turtle.digDown()
  133.  turtle.down()
  134.  forward()
  135.  face(NORTH)
  136.  
  137. end
  138.  
  139. local function startingPoint(x,y)
  140.   face(WEST)
  141.   digRow(x-1)
  142.   if width % 2 == 1 then
  143.     face(SOUTH)
  144.     digRow(y)
  145.   end    
  146. end
  147.  
  148. local function digSquare(x,y)
  149.   for i=1,x do
  150.     digRow(y)
  151.   if i ~= x then
  152.     if i % 2 == 0 then
  153.       advanceRowOdd()
  154.     else
  155.       advanceRowEven()
  156.     end
  157.   end
  158.   end
  159.   startingPoint(x,y)
  160. end
  161.        
  162. local function digStairs(x,y,z)
  163.   for i=1, z do
  164.     digSquare(x,y)
  165.     advanceLevel()
  166.   end
  167. end
  168.  
  169. function main()
  170.   digStairs(width,length,depth)
  171.   print("Mine Complete")
  172. end
  173.  
  174. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement