vvenaya

Untitled

Jan 20th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. --[[
  2.  Build simple bridge
  3.  Slot 1-15: Building block
  4.  Slot 16: fuel
  5. --]]
  6.  
  7. -- Parameters
  8.  
  9.  
  10. -- Variables
  11.  
  12. local itemBlockSlot = 0
  13. local fuelSlot = 16
  14. local collected = 0
  15.  
  16. -- Process commandline
  17.  
  18. local tArgs = { ... }
  19. if #tArgs < 0 then
  20.     print( "Usage: SimpleBridge <length>" )
  21.     print( "======================================" )
  22.     print( "")
  23.     print( "Expected content in inventory of turtle:" )
  24.     print( "Slot 1 to 15: Block to us" )
  25.     print( "Slot 16: Fuel")
  26.     return
  27. end
  28.  
  29. local length = tonumber( tArgs[1] )
  30. if length < 1 then
  31.     print( "length must be 1+" )
  32.     return
  33. end
  34.  
  35. local function checkSlot(slotNr,description)
  36.    if turtle.getItemCount(slotNr)==0 then
  37.       print("Expected item '"..description.."' in slot "..slotNr..".")
  38.       return false
  39.    end
  40.    return true
  41. end
  42.  
  43. -- Check slots
  44.  
  45. if not checkSlot(1,"BuildingBlocks") then return end
  46. if not checkSlot(fuelSlot,"fuel") then return end
  47.  
  48. -- Main Code
  49.  
  50. local function collect()
  51.     collected = collected + 1
  52.     if math.fmod(collected, 100) == 0 then
  53.         print( "Mined "..collected.." items." )
  54.     end
  55. end
  56. local function refuel()
  57.     local fuelLevel = turtle.getFuelLevel()
  58.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  59.         return
  60.     end
  61.    
  62.     local function tryRefuel()
  63.         if turtle.getItemCount(fuelSlot) > 0 then
  64.             turtle.select(fuelSlot)
  65.             if turtle.refuel(1) then
  66.                 return true
  67.             end
  68.         end
  69.         turtle.select(1)
  70.         return false
  71.     end
  72.     if not tryRefuel() then
  73.         print( "Add more fuel in slot "..fuelSlot.." to continue." )
  74.         while not tryRefuel() do
  75.             sleep(1)
  76.         end
  77.         print( "Resuming Tunnel." )
  78.     end
  79. end
  80. local function tryDig()
  81.     while turtle.detect() do
  82.         if turtle.dig() then
  83.             collect()
  84.             sleep(0.5)
  85.         else
  86.             return false
  87.         end
  88.     end
  89.     return true
  90. end
  91. local function tryDigUp()
  92.     while turtle.detectUp() do
  93.         if turtle.digUp() then
  94.             collect()
  95.             sleep(0.5)
  96.         else
  97.             return false
  98.         end
  99.     end
  100.     return true
  101. end
  102. local function tryDigDown()
  103.  while turtle.detectDown() do
  104.   if turtle.digDown() then
  105.     collect()
  106.     sleep(0.5)
  107.  else
  108.    return false
  109.   end
  110.  end
  111.  return true
  112. end
  113. local function tryUp()
  114.     refuel()
  115.     while not turtle.up() do
  116.         if turtle.detectUp() then
  117.             if not tryDigUp() then
  118.                 return false
  119.             end
  120.         elseif turtle.attackUp() then
  121.             collect()
  122.         else
  123.             sleep( 0.5 )
  124.         end
  125.     end
  126.     return true
  127. end
  128. local function tryDown()
  129.     refuel()
  130.     while not turtle.down() do
  131.         if turtle.detectDown() then
  132.             if not tryDigDown() then
  133.                 return false
  134.             end
  135.         elseif turtle.attackDown() then
  136.             collect()
  137.         else
  138.             sleep( 0.5 )
  139.         end
  140.     end
  141.     return true
  142. end
  143. local function tryForward()
  144.     refuel()
  145.     while not turtle.forward() do
  146.         if turtle.detect() then
  147.             if not tryDig() then
  148.                 return false
  149.             end
  150.         elseif turtle.attack() then
  151.             collect()
  152.         else
  153.             sleep( 0.5 )
  154.         end
  155.     end
  156.     return true
  157. end
  158. local function DumpStatistics()
  159.     print( "Statistics:")
  160.     print( string.rep("=",20))
  161.     print( "Mined "..collected.." blocks total." )
  162.     print( string.rep("=",20))
  163. end
  164.  
  165. local function checkOrSwapItemBlockSlot()
  166.     while (turtle.getItemCount(itemBlockSlot)==0) do
  167.         itemBlockSlot=itemBlockSlot+1
  168.         if itemBlockSlot==16 then return false end
  169.     end
  170.     turtle.select(itemBlockSlot)
  171.     return true
  172. end
  173.  
  174. local function buildBridgeSection()
  175.     if not checkOrSwapItemBlockSlot() then return false end
  176.     if not tryForward() then return false end
  177.     turtle.digDown()
  178.     turtle.digUp()
  179.     turtle.placeDown()
  180.     turtle.turnLeft()
  181.     turtle.dig()
  182.     turtle.place()
  183.     turtle.turnRight()
  184.     turtle.turnRight()
  185.     turtle.dig()
  186.     turtle.place()
  187.     turtle.turnLeft()
  188.     return true
  189. end
  190.  
  191. local function mine()
  192.     for n=1,length do
  193.         if not buildBridgeSection() then return false end
  194.     end    
  195.     return true
  196. end
  197.  
  198. if mine() then
  199.     print( "Job complete." )
  200. else
  201.     print( "Job aborted prematurely.")
  202. end
  203. DumpStatistics()
Advertisement
Add Comment
Please, Sign In to add comment