Machuga14

Strip Mining Turtle Assistant

Apr 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.22 KB | None | 0 0
  1. ----------------------------------------------------------------------------------------------------------
  2. --  04.22.2017 - Started scripting a basic Turtle AI to help out with strip mining.
  3. --      This turtle is not really very smart; it will just mine in a straight line until it runs out of fuel,
  4. --          A pathway 3 blocks tall, placing torches every 5 blocks along the ground (assuming there is valid ground to do so)
  5. --              -- Note: If the turtle runs out of torches, it will also stop.
  6. --
  7. ----------------------------------------------------------------------------------------------------------
  8.  
  9. ----------------------------------------------------------------------------------------------------------
  10. -- Settings
  11. ----------------------------------------------------------------------------------------------------------
  12. local itemNameFuel = "minecraft:coal" -- The name of the fuel the turtle will be using (Can only accept 1 type of fuel.)
  13. local itemNameTorch = "minecraft:torch" -- The name of the torches we are using.
  14. local distanceBetweenTorches = 7 -- The number of squares between the torches.
  15. local waitForTorchesOnPlaceTorch = true -- If we are set to place a torch right now, and we don't have torches, let's wait for the torch to be put in our inventory.
  16.  
  17.  
  18. ----------------------------------------------------------------------------------------------------------
  19. --  Function Pointers / Documentation (Helper Functions)
  20. ----------------------------------------------------------------------------------------------------------
  21.  
  22. -- Returns the index of the item in the inventory by the name provided. Returns nil if it can't be found.
  23. -- Note: If you are searching for a nil name, will return the first nil position.
  24. local ScanForItemInInventoryByName
  25.  
  26. -- If the turtle is less than 90% fuel, will search for fuel in the turtle's inventory to refuel itself.
  27. local HandleFuelLevel
  28.  
  29. -- If the turtle has a torch, place it. Sits and waits for torches to be added if it has no torches (so no mobs can spawn), if the setting is set to do so.
  30. local PlaceTorch
  31.  
  32. -- StripMines; main function.
  33. local StripMine
  34.  
  35. -- Infinite retries moving up.
  36. local RobustUp
  37.  
  38. -- Infinitely retries moving down.
  39. local RobustDown
  40.  
  41. -- Infinitely retries moving forward.
  42. local RobustForward
  43.  
  44. ----------------------------------------------------------------------------------------------------------
  45. --  Function Definitions (Helper Functions)
  46. ----------------------------------------------------------------------------------------------------------
  47.  
  48. -- Returns the index of the item in the inventory by the name provided. Returns nil if it can't be found.
  49. -- Note: If you are searching for a nil name, will return the first nil position.
  50. ScanForItemInInventoryByName = function(name, caseInsensitive)
  51.     for i = 1, 16, 1 do
  52.         local item = turtle.getItemDetail(i)
  53.        
  54.         if item then
  55.             local itemName = item.name
  56.            
  57.             if caseInsensitive then
  58.                 itemName = string.lower(itemName)
  59.                 name = string.lower(name)
  60.             end
  61.            
  62.             if name and string.match(itemName, name) and (name ~= itemNameBoneMeal or (name == itemNameBoneMeal and item.damage == itemMetaDataBoneMeal)) then
  63.                 return i
  64.             end
  65.         end
  66.        
  67.         if item == nil and name == nil then
  68.             return i
  69.         end
  70.     end
  71.  
  72.     return nil
  73. end
  74.  
  75. -- If the turtle is less than 90% fuel, will search for fuel in the turtle's inventory to refuel itself.
  76. HandleFuelLevel = function()
  77.     if turtle.getFuelLevel() / turtle.getFuelLimit() < .9 then
  78.         local archivedIndex = turtle.getSelectedSlot()
  79.         local index = ScanForItemInInventoryByName(itemNameFuel)
  80.        
  81.         if index then
  82.             print("Fuel is below 90%; Detected fuel in inventory. Refueling.")
  83.             local currItem = turtle.getItemDetail(index)
  84.            
  85.             while turtle.getFuelLevel() < turtle.getFuelLimit() and currItem and currItem.name == itemNameFuel do
  86.                 turtle.select(index)
  87.                 turtle.refuel(1)
  88.                 currItem = turtle.getItemDetail(index)
  89.             end
  90.            
  91.             print("Fuel: "..turtle.getFuelLevel().." / "..turtle.getFuelLimit().." ("..(100 * (turtle.getFuelLevel() / turtle.getFuelLimit())).."%)")
  92.             turtle.select(archivedIndex)
  93.         else
  94.             if turtle.getFuelLevel() == 0 then
  95.                 print("\r\n\r\nALERT! We are out of fuel!!! Weird stuff may happen (We may even crash!) Please give me fuel (Coal or charcoal)\r\n\r\n")
  96.             end
  97.         end
  98.     end
  99. end
  100.  
  101. -- If the turtle has a torch, place it. Sits and waits for torches to be added if it has no torches (so no mobs can spawn), if the setting is set to do so.
  102. PlaceTorch = function()
  103.     local archivedIndex = turtle.getSelectedSlot()
  104.     local torchIndex = ScanForItemInInventoryByName(itemNameTorch)
  105.     local success = false
  106.    
  107.     if not torchIndex and waitForTorchesOnPlaceTorch then
  108.         while not torchIndex do
  109.             torchIndex = ScanForItemInInventoryByName(itemNameTorch)
  110.             print("Waiting for item by name of "..itemNameTorch.." to be placed in inventory.")
  111.             sleep(5)
  112.         end
  113.     elseif not torchIndex then
  114.         return
  115.     end
  116.    
  117.     if torchIndex then
  118.         turtle.select(torchIndex)
  119.        
  120.         if turtle.placeDown() then
  121.             print("Torch Placed!")
  122.             success = true
  123.         else
  124.             print("Failed to place torch; is there air below?")
  125.             success = false
  126.         end
  127.     end
  128.    
  129.     turtle.select(archivedIndex)
  130.     return success
  131. end
  132.  
  133. -- StripMines; main function.
  134. StripMine = function()
  135.     HandleFuelLevel()
  136.    
  137.     if turtle.getFuelLevel() == 0 then
  138.         print("Terminating Early; turtle started up without fuel. Place fuel in turtle and run program again.")
  139.         return
  140.     end
  141.    
  142.     local placesMovedSinceLastTorch = distanceBetweenTorches
  143.     while true do
  144.         placesMovedSinceLastTorch = placesMovedSinceLastTorch + 1
  145.        
  146.         -- Dig up, place a torch if necesary.
  147.         for i = 1, 3, 1 do
  148.             if placesMovedSinceLastTorch >= distanceBetweenTorches and i == 2 then
  149.                 if (PlaceTorch()) then
  150.                     placesMovedSinceLastTorch = 0
  151.                 end
  152.             end
  153.            
  154.             turtle.dig()
  155.            
  156.             if turtle.inspectUp() then
  157.                 turtle.digUp()
  158.             end
  159.            
  160.             RobustUp()
  161.         end
  162.        
  163.         turtle.dig()
  164.         RobustForward()
  165.        
  166.         for i = 1, 3, 1 do
  167.             if turtle.inspectDown() then
  168.                 turtle.digDown()
  169.             end
  170.            
  171.             RobustDown()
  172.         end
  173.        
  174.         HandleFuelLevel()
  175.     end
  176. end
  177.  
  178. -- Infinite retries moving up.
  179. RobustUp = function()
  180.     local success = false
  181.    
  182.     while not success do
  183.         success = turtle.up()
  184.     end
  185. end
  186.  
  187. -- Infinitely retries moving down.
  188. RobustDown = function()
  189.     local success = false
  190.    
  191.     while not success do
  192.         success = turtle.down()
  193.     end
  194. end
  195.  
  196. -- Infinitely retries moving forward.
  197. RobustForward = function()
  198.     local success = false
  199.    
  200.     while not success do
  201.         success = turtle.forward()
  202.     end
  203. end
  204.  
  205. ----------------------------------------------------------------------------------------------------------
  206. -- TESTING!!!
  207. ----------------------------------------------------------------------------------------------------------
  208.  
  209. --HandleFuelLevel()
  210. --PlaceTorch()
  211.  
  212.  
  213. ----------------------------------------------------------------------------------------------------------
  214. --      Main EntryPoint
  215. ----------------------------------------------------------------------------------------------------------
  216. print("Executing StripMine")
  217. StripMine()
  218. print("StripMine Terminated.")
Add Comment
Please, Sign In to add comment