Advertisement
FurtherV

Turtle Stripmine

Oct 29th, 2020 (edited)
2,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.89 KB | None | 0 0
  1. --What should this program accomplish?
  2. --Create a 2x1 Tunnel with user given length
  3. --Create roof against lava and depending on user input check also walls
  4. --Place torches
  5. --Automatically Refuel
  6. --Broadcast status on rednet
  7. --Automatically return to start location
  8. --Sand / Gravel support
  9.  
  10. local DISTANCE_TO_MINE = 0
  11. local SHOULD_CHECK_WALLS = false
  12. local TORCH_SLOT = 1
  13. local FUEL_SLOT = 2
  14. local BLOCK_SLOT = 3
  15. local USES_FUEL = (turtle.getFuelLevel() == "unlimited")
  16. local DISTANCE_BETWEEN_TORCHES = 10
  17. local REDNET_ACCESS = false
  18. local MODEM_SIDE = ""
  19. local MINING_START_TIME = 0
  20. local MINING_FINISH_TIME = 0
  21. local MINING_BLOCK_COUNT = 0
  22.  
  23. local function DigForward()
  24.     if turtle.detect() then
  25.         if turtle.dig() then
  26.             MINING_BLOCK_COUNT = MINING_BLOCK_COUNT + 1
  27.         end
  28.     end
  29. end
  30.  
  31. local function DigUp()
  32.     if turtle.detectUp() then
  33.         if turtle.digUp() then
  34.             MINING_BLOCK_COUNT = MINING_BLOCK_COUNT + 1
  35.         end
  36.     end
  37. end
  38.  
  39. local function DigDown()
  40.     if turtle.detectDown() then
  41.         if turtle.digDown() then
  42.             MINING_BLOCK_COUNT = MINING_BLOCK_COUNT + 1
  43.         end
  44.     end
  45. end
  46.  
  47. local function BroadcastMessage(text)
  48.     if REDNET_ACCESS then
  49.         rednet.broadcast(((os.getComputerLabel() or "nil") .. text), "miningturtle")
  50.     end
  51. end
  52.  
  53. --Returns true if a torch should be placed. False if not.
  54. local function ShouldPlaceTorch(distance)
  55.     if (distance % DISTANCE_BETWEEN_TORCHES == 0) then
  56.         return not (turtle.detectDown())
  57.     end
  58.     return false
  59. end
  60.  
  61. --Places a torch
  62. local function PlaceTorch()
  63.     turtle.select(TORCH_SLOT)
  64.     if (turtle.getItemCount(TORCH_SLOT) > 0) then
  65.         turtle.placeDown()
  66.     end
  67. end
  68.  
  69. --Checks if the turtle needs refuel
  70. local function NeedsRefuel()
  71.     if USES_FUEL then
  72.         return false
  73.     else
  74.         return (turtle.getFuelLevel() < 100)
  75.     end
  76. end
  77.  
  78. --Refuels the turtle
  79. local function Refuel()
  80.     turtle.select(FUEL_SLOT)
  81.     if (turtle.getItemCount(FUEL_SLOT) > 0) then
  82.         turtle.refuel(1)
  83.     else
  84.         BroadcastMessage("nofuel")
  85.     end
  86. end
  87.  
  88. --Checks floor for existance.
  89. local function ShouldPlaceFloor()
  90.     return not (turtle.detectDown())
  91. end
  92.  
  93. --Places a block down
  94. local function PlaceFloor()
  95.     turtle.select(BLOCK_SLOT)
  96.     if (turtle.getItemCount(BLOCK_SLOT) > 0) then
  97.         turtle.placeDown()
  98.     end
  99. end
  100.  
  101. --Checks the roof for existance.
  102. local function ShouldPlaceRoof()
  103.     return not (turtle.detectUp())
  104. end
  105.  
  106. --Places a block up
  107. local function PlaceRoof()
  108.     turtle.select(BLOCK_SLOT)
  109.     if (turtle.getItemCount(BLOCK_SLOT) > 0) then
  110.         turtle.placeUp()
  111.     end
  112. end
  113.  
  114. --Checks walls left and right for existance. IF not, a block is placed.
  115. local function CheckWalls()
  116.     turtle.turnLeft()
  117.     turtle.select(BLOCK_SLOT)
  118.     if not (turtle.detect()) then
  119.         turtle.place()
  120.     end
  121.     turtle.turnRight()
  122.     turtle.turnRight()
  123.     if not (turtle.detect()) then
  124.         turtle.place()
  125.     end
  126.     turtle.turnLeft()
  127. end
  128.  
  129. --Moves forward, mines forward and up.
  130. local function MineForward(maxDistance)
  131.     local currentDistance = 0
  132.     repeat
  133.         DigForward()
  134.         DigUp()
  135.        
  136.         if ShouldPlaceFloor() then
  137.             PlaceFloor()
  138.         end
  139.        
  140.         if NeedsRefuel() then
  141.             Refuel()
  142.         end
  143.        
  144.         if turtle.forward() then
  145.             currentDistance = currentDistance + 1
  146.         end
  147.        
  148.         if SHOULD_CHECK_WALLS then
  149.             CheckWalls()
  150.         end
  151.        
  152.     until (currentDistance >= maxDistance)
  153. end
  154.  
  155. --Turns the turtle 180° and makes it mine forward and move forward. Also moves the turtle up once at the beginning.
  156. local function ReturnToStart(distanceToStart)
  157.     DigUp()
  158.     turtle.up()
  159.     turtle.turnLeft()
  160.     turtle.turnLeft()
  161.    
  162.     local distanceLeft = distanceToStart
  163.     repeat
  164.         DigForward()
  165.         DigDown()
  166.        
  167.         if ShouldPlaceRoof() then
  168.             PlaceRoof()
  169.         end
  170.        
  171.         if ShouldPlaceTorch(distanceLeft) then
  172.             PlaceTorch()
  173.         end
  174.        
  175.         if SHOULD_CHECK_WALLS then
  176.             CheckWalls()
  177.         end
  178.        
  179.         if NeedsRefuel() then
  180.             Refuel()
  181.         end
  182.        
  183.         if turtle.forward() then
  184.             distanceLeft = distanceLeft - 1
  185.         end
  186.        
  187.     until (distanceLeft <= 0)
  188.    
  189.     turtle.turnLeft()
  190.     turtle.turnLeft()
  191.     DigDown()
  192.     turtle.down()
  193. end
  194.  
  195. --Tries to open a modem on all sides and checks if a modem is open then, therefor returning whether the turtle has a modem or not.
  196. local function CheckIfModemExists()
  197.     for n,m in ipairs(rs.getSides()) do
  198.         if peripheral.isPresent(m) and peripheral.getType(m) == "modem" then
  199.             rednet.open(m)
  200.             MODEM_SIDE = m
  201.             return true
  202.         end
  203.     end
  204.     return false
  205. end
  206.  
  207. --Converts a given user string (true/false OR y/n) to a boolean.
  208. local function UserStringToBoolean(text)
  209.     local input = string.lower(text)
  210.     if (input == "true") then
  211.         return true
  212.     end
  213.     if (input == "y") then
  214.         return true
  215.     end
  216.     return false
  217. end
  218.  
  219. --A delayed calls of CheckInventory()
  220. local function DelayedRecheck(delay)
  221.     local counter = 10
  222.     repeat
  223.         shell.run("clear")
  224.         print("Please fill inventory with the required items.")
  225.         print("Will recheck in " .. tostring(counter) .. " seconds.")
  226.         sleep(1)
  227.         counter = counter - 1
  228.     until (counter == 0)
  229. end
  230.  
  231. --Checks the inventory and the modem
  232. local function CheckInventory()
  233.     local torchCount = turtle.getItemCount(TORCH_SLOT)
  234.     local fuelCount = turtle.getItemCount(FUEL_SLOT)
  235.     local blockCount = turtle.getItemCount(BLOCK_SLOT)
  236.     local invError = false
  237.    
  238.     if (torchCount == 0) then
  239.         print("There are no torches in the inventory! (Slot 1)")
  240.         invError = true
  241.     end
  242.    
  243.     if (fuelCount == 0) then
  244.         print("There is no fuel in the inventory! (Slot 2)")
  245.         invError = true
  246.     end
  247.  
  248.     if (blockCount == 0) then
  249.         print("There are no blocks in the inventory! (Slot 3)")
  250.         invError = true
  251.     end
  252.    
  253.     REDNET_ACCESS = CheckIfModemExists()
  254.    
  255.     if not (REDNET_ACCESS) then
  256.         print("There is no connected modem!")
  257.         invError = true
  258.     end
  259.    
  260.     if (invError) then
  261.         print("Continue with current status? y/n")
  262.         local input = io.read()
  263.         local continue = UserStringToBoolean(input)
  264.         if (not continue) then
  265.             DelayedRecheck()
  266.             CheckInventory()
  267.             return nil
  268.         end
  269.     end
  270. end
  271.  
  272. --Called upon program start
  273. local function Startup()
  274.     shell.run("clear")
  275.     print("<== Stripmine Program ==>")
  276.     print("Please enter tunnel length:")
  277.     local input = io.read()
  278.     DISTANCE_TO_MINE = tonumber(input)
  279.     print("Should the turtle check the walls for holes? y/n")
  280.     input = io.read()
  281.     SHOULD_CHECK_WALLS = UserStringToBoolean(input)
  282.     shell.run("clear")
  283.     print("Tunnel length: " .. tostring(DISTANCE_TO_MINE))
  284.     print("Check walls: " .. tostring(SHOULD_CHECK_WALLS))
  285.     print("Continue with these settings? y/n")
  286.     input = io.read()
  287.     local continue = UserStringToBoolean(input)
  288.     if (not continue) then
  289.         Startup()
  290.         return nil
  291.     end
  292.    
  293.     CheckInventory()
  294.     shell.run("clear")
  295.     MINING_START_TIME = os.epoch("utc")
  296.     BroadcastMessage("startmining")
  297.     MineForward(DISTANCE_TO_MINE)
  298.     ReturnToStart(DISTANCE_TO_MINE)
  299.     MINING_FINISH_TIME = os.epoch("utc")
  300.     local timeSpent = (MINING_FINISH_TIME - MINING_START_TIME) / 1000
  301.     print("Time spent mining: " .. tostring(timeSpent) .. " seconds")
  302.     print("Blocks mined: " .. tostring(MINING_BLOCK_COUNT))
  303.     BroadcastMessage("endmining:" .. (tostring(timeSpent)) .. ":" .. (MINING_BLOCK_COUNT))
  304. end
  305.  
  306. Startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement