bigtwisty

QuarryBot

Jun 11th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.46 KB | None | 0 0
  1. -- QuarryBot (1.0)
  2.  
  3. -- REQUIRES
  4. local robot = require( "robot" )
  5. local term = require( "term" )
  6. local event = require( "event" )
  7. local component = require( "component" )
  8.  
  9. -- LOCAL VARIABLES
  10.  
  11. local chunksWide = 1
  12. local chunksDeep = 1
  13. local level = 0
  14.  
  15. local shovelSlot = 0
  16. local chestSlot = 0
  17. local inventory = false
  18.  
  19. -- filters are items not to dig from walls
  20. -- shovelFilters are items to shovel if digging down, but not from walls
  21. local firstFilter = 0
  22. local firstShovelFilter = 0
  23. local lastFilter = 0
  24.  
  25. -- INITIALIZATION FUNCTIONS
  26.  
  27. local function loadItem( item, slot )
  28.   print( "Shift-click "..item.." into slot 1..." )
  29.   event.pullFiltered( function( name, slot ) return name == "inventory_changed" and slot == 1 and robot.count( 1 ) > 0 end )
  30.   robot.transferTo( slot )
  31. end
  32.  
  33. local function initItems()
  34.   term.clear()
  35.   term.setCursor( 1, 1 )
  36.   robot.select( 1 )
  37.   print("QuarryBot v1.0")
  38.   if component.isAvailable("inventory_controller") then
  39.     print( "Inventory Manager found!\n" )
  40.     inventory = component.inventory_controller
  41.     shovelSlot = robot.inventorySize()
  42.     chestSlot = shovelSlot - 1
  43.     loadItem( "pickaxe into tool slot then shovel", shovelSlot )
  44.     loadItem( "ender chest", chestSlot )
  45.   else
  46.     print( "Inventory Manager not found." )
  47.     print( "Shovel equip disabled.\n" )
  48.     chestSlot = robot.inventorySize()
  49.     loadItem( "pickaxe into tool slot then ender chest", chestSlot )
  50.   end
  51. end
  52.  
  53. local function initFilters()
  54.   lastFilter = chestSlot - 1
  55.   firstShovelFilter = lastFilter + 1
  56.  
  57.   if inventory ~= false then
  58.     print( "Shift click in all shoveled items (dirt, etc.)" )
  59.     print( "Press space when done..." )
  60.     while true do
  61.       ev, slot, key = event.pull()
  62.       if ev == "inventory_changed" and slot == 1 and robot.count( 1 ) > 0 then
  63.         firstShovelFilter = firstShovelFilter - 1
  64.         robot.transferTo( firstShovelFilter )
  65.       elseif ev == "key_down" and key == string.byte( " " ) then
  66.         break
  67.       end
  68.     end
  69.     print( "Shift click in all other filtered items (stone, etc.)" )
  70.   else
  71.     print( "Shift click in all filtered items (stone, dirt, etc.)" )
  72.   end
  73.   print( "Press space when done..." )
  74.  
  75.   firstFilter = firstShovelFilter
  76.   while true do
  77.     ev, slot, key = event.pull()
  78.     if ev == "inventory_changed" and slot == 1 and robot.count( 1 ) > 0 then
  79.       firstFilter = firstFilter - 1
  80.       robot.transferTo( firstFilter )
  81.     elseif ev == "key_down" and key == string.byte( " " ) then
  82.       break
  83.     end
  84.   end
  85. end
  86.  
  87. local function isFull()
  88.   for i = 1, firstFilter - 1 do
  89.     if robot.count( i ) == 0 then
  90.       return false
  91.     end
  92.   end
  93.   return true
  94. end
  95.  
  96. local placeFunc = { [1] = robot.place, [2] = robot.placeDown }
  97. local swingFunc = { [1] = robot.swing, [2] = robot.swingDown }
  98. local dropFunc  = { [1] = robot.drop,  [2] = robot.dropDown  }
  99.  
  100. local function unload( direction )
  101.   print( "Unloading" )
  102.   robot.select( chestSlot )
  103.   if placeFunc[direction]() then
  104.     for i = 1, firstFilter - 1 do
  105.       robot.select( i )
  106.       dropFunc[direction]()
  107.     end
  108.     for i = firstFilter, lastFilter do
  109.       robot.select( i )
  110.       dropFunc[direction]( robot.count( i ) - 1 )
  111.     end
  112.     robot.select( chestSlot )
  113.     swingFunc[direction]()
  114.   else
  115.     print( "Could not place chest" )
  116.   end
  117.   robot.select( 1 )
  118. end
  119.  
  120. -- Break block if not blacklisted
  121. local function digWall()
  122.   for i = firstFilter, lastFilter do
  123.     robot.select( i )
  124.     if robot.compare() then return end
  125.   end
  126.   robot.select( 1 )
  127.   robot.swing()
  128.   if isFull() then
  129.     unload( 1 )
  130.   end
  131. end
  132.  
  133. local function digDown()
  134.   if robot.detectDown() then
  135.     if inventory ~= false then
  136.       for i = firstShovelFilter, lastFilter do
  137.         robot.select( i )
  138.         if robot.compareDown() then
  139.           robot.select( shovelSlot )
  140.           inventory.equip()
  141.           robot.select( 1 )
  142.           robot.swingDown()
  143.           robot.select( shovelSlot )
  144.           inventory.equip()
  145.           if isFull() then
  146.             unload( 2 )
  147.           end
  148.           return
  149.         end
  150.       end    
  151.     end
  152.     robot.select( 1 )
  153.     if robot.swingDown() and isFull() then
  154.       unload( 2 )
  155.     end
  156.   end
  157. end
  158.  
  159. local function down()
  160.   digDown()
  161.   if not robot.down() then
  162.     return false
  163.   end
  164.   return true
  165. end
  166.  
  167. local function digHole()
  168.   level = 0
  169.   while down() do
  170.     digWall()
  171.     level = level + 1
  172.   end
  173.   robot.turnAround()
  174.   for i = 1, level do
  175.     digWall()
  176.     tries = 0
  177.     while not robot.up() do
  178.       tries = tries + 1
  179.       if tries > 2 then
  180.         print( "Stopping due to 3rd up failure" )
  181.         os.exit()
  182.       end
  183.       print( "Couldn't go up on try: ", tries)
  184.     end
  185.   end
  186.   robot.turnAround()
  187. end
  188.  
  189. -- Initialize
  190. initItems()
  191. initFilters()
  192.  
  193. term.clear()
  194. term.setCursor( 1, 1 )
  195. print( "First Filter Slot:        ", firstFilter )
  196. print( "First Shovel Filter Slot: ", firstShovelFilter )
  197. print( "Last Filter Slot:         ", lastFilter )
  198. print()
  199. print( "Mining!!!" )
  200.  
  201. local turnFunc = { [true] = robot.turnRight, [false] = robot.turnLeft }
  202. local turningRight = true
  203.  
  204. for width = 1, chunksWide * 16 do
  205.   for depth = 1, chunksDeep * 16 / 3 do
  206.     if depth ~= 1 then
  207.       for i = 1, 3 do
  208.         if not robot.forward() then
  209.           robot.swing()
  210.           robot.forward()
  211.         end
  212.       end
  213.     end
  214.     digHole()
  215.   end
  216.   turnFunc[turningRight]()
  217.   robot.forward()
  218.   turnFunc[turningRight]()
  219.   turningRight = not turningRight
  220. end
Advertisement
Add Comment
Please, Sign In to add comment