HAJ523

ladder

Dec 3rd, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.82 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. --Check for Valid Args
  4. if (#tArgs < 1) or (#tArgs > 2) then
  5.     print("Usage: ladder <depth> <up?>")
  6.     print("    Make sure that you provide enough fuel to go up and down <depth> + 1")
  7.     print("    Max Depth 64")
  8.     print("    Slot 1: Fuel")
  9.     print("    Slot 2: Wall Blocks")
  10.     print("    Slot 3: Ladder Blocks")
  11.     return
  12. end
  13.  
  14. local depth=tonumber(tArgs[1])
  15. local dir=0
  16.  
  17. --Check Depth
  18. if depth>64 then
  19.     depth=64
  20. elseif depth<1 then
  21.     return
  22. end
  23.  
  24. if (#tArgs > 1) then
  25.     dir=tonumber(tArgs[2])
  26.     if (dir >= 1) then
  27.         dir=1
  28.     else
  29.         dir=-1
  30.     end
  31. end
  32.  
  33. local function refuel(slot)
  34.     local fuelLevel = turtle.getFuelLevel()
  35.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  36.         return
  37.     end
  38.    
  39.     local function tryRefuel()
  40.         if turtle.getItemCount(1) > 0 then
  41.             turtle.select(1)
  42.             if turtle.refuel(1) then
  43.                 return true
  44.             end
  45.         end
  46.         return false
  47.     end
  48.    
  49.     if not tryRefuel() then
  50.         print( "Add more fuel to continue." )
  51.         while not tryRefuel() do
  52.             sleep(5)
  53.         end
  54.         print( "Resuming..." )
  55.         turtle.select(slot)
  56.     end
  57. end
  58.  
  59. local function checkBlock(slot,first,last,block)
  60.     while turtle.getItemCount(slot) == 0 do
  61.         slot = slot + 1
  62.         if slot > last then
  63.             print("Missing: " .. block)
  64.             print("Slots: " .. first .. "-" .. last)
  65.             local line=""
  66.             repeat
  67.                 print("Enter 'Y' to continue: ")
  68.                 line = io.read()
  69.             until string.lower(line) ~= "y"
  70.             print("Resuming...")
  71.             slot = first --Reset slot to first
  72.         end
  73.     end
  74.     return slot
  75. end
  76.  
  77. local function checkStorage()
  78.     if turtle.getItemCount(16) > 0 then
  79.         print("Clean inventory...")
  80.         while turtle.getItemCount(16) > 0 do
  81.             sleep(5)
  82.         end
  83.         print( "Resuming..." )
  84.     end
  85. end
  86.  
  87. --slot  - current slot of blocks (returned)
  88. --inv   - start of inventory slots
  89. --first - start of blocks slots
  90. --last  - end of blocks slots
  91. --block - block name
  92. --dir   - place direction
  93. local function tryBlock(slot,inv,first,last,block,dir)
  94.     slot=checkBlock(slot,first,last,block)
  95.     turtle.select(slot)
  96.     if (dir == 0) then
  97.         turtle.place()
  98.     elseif (dir > 0) then
  99.         turtle.placeUp()
  100.     else
  101.         turtle.placeDown()
  102.     end
  103.     turtle.select(inv)
  104.     return checkBlock(slot,first,last,block)
  105. end
  106.  
  107. local function tryDig()
  108.     while turtle.detect() do
  109.         if turtle.dig() then
  110.             checkStorage()
  111.             sleep(0.5)
  112.         else
  113.             return false
  114.         end
  115.     end
  116.     return true
  117. end
  118.  
  119. local function tryDigDown()
  120.     if turtle.digDown() then
  121.         checkStorage()
  122.         return true
  123.     end
  124.     return false
  125. end
  126.  
  127. local function tryDigUp()
  128.     while turtle.detectUp() do
  129.         if turtle.digUp() then
  130.             checkStorage()
  131.             sleep(0.5)
  132.         else
  133.             return false
  134.         end
  135.     end
  136.     return true
  137. end
  138.  
  139. local function tryUp(slot)
  140.     refuel(slot)
  141.     while not turtle.up() do
  142.         if turtle.detectUp() then
  143.             if not tryDigUp() then
  144.                 return false
  145.             end
  146.         elseif turtle.attackUp() then
  147.             checkStorage()
  148.         else
  149.             sleep( 0.5 )
  150.         end
  151.     end
  152.     return true
  153. end
  154.  
  155. local function tryDown(slot)
  156.     refuel(slot)
  157.     while not turtle.down() do
  158.         if turtle.detectDown() then
  159.             if not tryDigDown() then
  160.                 return false
  161.             end
  162.         elseif turtle.attackDown() then
  163.             checkStorage()
  164.         else
  165.             sleep( 0.5 )
  166.         end
  167.     end
  168.     return true
  169. end
  170.  
  171. local function tryForward(slot)
  172.     refuel(slot)
  173.     while not turtle.forward() do
  174.         if turtle.detect() then
  175.             if not tryDig() then
  176.                 return false
  177.             end
  178.         elseif turtle.attack() then
  179.             checkStorage()
  180.         else
  181.             sleep(0.5)
  182.         end
  183.     end
  184.     return true
  185. end
  186.  
  187. local i=depth
  188.  
  189. while i > 0 do
  190.     if (dir < 0) then
  191.         tryDigDown()
  192.         tryDown()
  193.     else
  194.         tryDigUp()
  195.         tryUp()
  196.     end
  197.     tryDig()
  198.     tryBlock(2,4,2,2,"Wall",0)
  199.     i=i-1
  200. end
  201.  
  202. i=depth
  203. turtle.turnRight()
  204. turtle.turnRight()
  205. tryDig()
  206. tryForward()
  207. turtle.turnRight()
  208. turtle.turnRight()
  209.  
  210. while i > 0 do
  211.     tryBlock(3,4,3,3,"Ladder",0)
  212.     if (dir > 0) then
  213.         tryDigDown()
  214.         tryDown()
  215.     else
  216.         tryDigUp()
  217.         tryUp()
  218.     end
  219.     i=i-1
  220. end
Advertisement
Add Comment
Please, Sign In to add comment