Advertisement
laifsjo

Turtle mining program

Jan 23rd, 2022 (edited)
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.93 KB | None | 0 0
  1. local xPos = 0
  2. local yPos = 0
  3. local zPos = 0
  4. local xSize = 0
  5. local ySize = 0
  6. local zSize = 0
  7. local direction = 0
  8. local status = "Let's goooooooooooooo"
  9. local fuelTypes = {'minecraft:coal', 'minecraft:charcoal', 'minicoal2:minicoal', 'minicoal2:minicharcoal'}
  10. local finished = false
  11. local inventorySlot = 1
  12.  
  13. --[[ local rednetOutputChannel = 1
  14. local rednetInputChannel = 2
  15. local modem = peripheral.find("modem")
  16. modem.open(rednetOutputChannel) ]]
  17.  
  18. --[[ function rednetOut(message)
  19.  modem.transmit(1, 2, message)
  20. end ]]
  21.  
  22. --[[ function outputData()
  23.  rednetOut({
  24.   "X position: " .. xPos,
  25.   "Y position: " .. yPos,
  26.   "Z position: " .. zPos,
  27.   "Direction : " .. math.fmod(direction, 4),
  28.   "",
  29.   "Fuel level: " .. turtle.getFuelLevel(),
  30.   "",
  31.   "Status    : " .. status
  32.  })
  33. end ]]
  34.  
  35. function mine()
  36.     if turtle.detect() then
  37.         turtle.dig()
  38.         turtle.forward()
  39.     else
  40.         turtle.forward()
  41.     end
  42. end
  43.  
  44. function turn(side)
  45.     if side == "right" then
  46.         turtle.turnRight()
  47.         direction = direction + 1
  48.     elseif side == "left" then
  49.         turtle.turnLeft()
  50.         direction = direction - 1
  51.     else
  52.         error("turn() function called incorrectly. parameter: " .. side .. " needs to be either left or right")
  53.     end
  54. end
  55.  
  56. function enoughFuel(distance)
  57.  local fuelLevel = turtle.getFuelLevel()
  58.  
  59.  if fuelLevel > distance then
  60.   return true
  61.  else
  62.   return false
  63.  end
  64. end
  65.  
  66. function refuel(amount)
  67.  local item = turtle.getItemDetail(16)
  68.  
  69.  for i=1,table.getn(fuelTypes) do
  70.   if item.name == fuelTypes[i] then
  71.    if item.count > amount then
  72.     turtle.refuel(amount)
  73.     status = "Refuelled. " .. item.count - amount .. " " .. item.name .. " left."
  74.     print("Refuelled. "..item.count - amount.." "..item.name.." left.")  
  75.    else
  76.     status = "ERROR: only " .. item.count .. " " .. item.name .. " left. Can't refuel " .. amount .. " items."
  77.     print("ERROR: only "..item.count.." "..item.name.." left. Can't refuel "..amount.." items.")
  78.    end
  79.   end
  80.  end
  81. end
  82.  
  83. function faceDirection(dir)
  84.     while direction > dir do
  85.         turn("left")
  86.     end
  87.  
  88.     while direction < dir do
  89.         turn("right")
  90.     end
  91. end
  92.  
  93. function faceDirectionZero()
  94.     while direction > 0 do
  95.         turn("left")
  96.     end
  97.    
  98.     while direction < 0 do
  99.         turn("right")
  100.     end
  101. end
  102.  
  103. function returnToBase()
  104.     print("Returning to base")
  105.     while zPos > 0 do
  106.         turtle.up()
  107.         zPos = zPos - 1
  108.     end
  109.    
  110.     faceDirectionZero()
  111.    
  112.     if yPos > 0 then
  113.         turn("right")
  114.         while yPos > 0 do
  115.             turtle.forward()
  116.             yPos = yPos - 1
  117.         end
  118.        
  119.         if xPos > 0 then
  120.             turn("right")
  121.             while xPos > 0 do
  122.                 turtle.forward()
  123.                 xPos = xPos - 1
  124.             end
  125.         end
  126.     else
  127.         if xPos > 0 then
  128.             turn("right")
  129.             turn("right")
  130.             while xPos > 0 do
  131.                 turtle.forward()
  132.                 xPos = xPos - 1
  133.             end
  134.         end
  135.     end
  136.    
  137.     turtle.forward()
  138.     faceDirectionZero()
  139. end
  140.  
  141. function moveToPos(x, y, z, dir)
  142.     print("Moving to pos: ["..x..", "..y..", "..z.."]"..".")  
  143.     faceDirectionZero()
  144.  
  145.     while xPos < x do
  146.         turtle.forward()
  147.         xPos = xPos + 1
  148.     end
  149.    
  150.     if yPos < y then
  151.         turn("left")
  152.         while yPos < y do
  153.             turtle.forward()
  154.             yPos = yPos + 1
  155.         end
  156.     end
  157.  
  158.     while zPos < z do
  159.         turtle.down()
  160.         zPos = zPos + 1
  161.     end
  162.     faceDirection(dir)
  163. end
  164.  
  165. function bankItems()
  166.     local success, data = turtle.inspectDown()
  167.     if success and data == 'minecraft:chest' then
  168.         for i=1,16 do
  169.             turtle.select(i)
  170.             turtle.dropDown()
  171.         end
  172.     end
  173. end
  174.  
  175. function miningAction()
  176.     local layer = math.fmod(zPos, 2)
  177.     local row = math.fmod(xPos, 2)
  178.     local column = math.fmod(yPos, 2)
  179.    
  180.     if zPos >= zSize - 1 and ySize <= 0 and xSize <= 0 then
  181.         status = "Finished " .. xSize .. "x" .. ySize .. "x" .. zSize .. " cube. Returning to base..."
  182.         returnToBase()
  183.         finished = true
  184.         return
  185.     end
  186.  
  187.  
  188.     if yPos >= ySize - 1 and xPos <= 0 and layer == 0 then
  189.         turtle.digDown()
  190.         turtle.down()
  191.         turn("left")
  192.         turn("left")
  193.         zPos = zPos + 1
  194.     elseif yPos <= 0 and xPos <= 0 and layer == 1 then
  195.         turtle.digDown()
  196.         turtle.down()
  197.         turn("right")
  198.         turn("right")
  199.         zPos = zPos + 1
  200.     elseif xPos >= xSize - 1 and column == 1 and layer == 1 then
  201.         turn("right")
  202.         mine()
  203.         turn("right")
  204.         yPos = yPos - 1
  205.     elseif xPos <= 0 and column == 0 and layer == 1 then
  206.         turn("left")
  207.         mine()
  208.         turn("left")
  209.         yPos = yPos - 1
  210.     elseif xPos >= xSize - 1 and column == 0 and layer == 0 then
  211.         turn("left")
  212.         mine()
  213.         turn("left")
  214.         yPos = yPos + 1
  215.     elseif xPos <= 0 and column == 1 and layer == 0 then
  216.         turn("right")
  217.         mine()
  218.         turn("right")
  219.         yPos = yPos + 1
  220.     else
  221.         mine()
  222.         if column == 0 then
  223.             if layer == 0 then
  224.                 xPos = xPos + 1
  225.             elseif layer == 1 then
  226.                 xPos = xPos - 1
  227.             end
  228.         elseif column == 1 then
  229.             if layer == 0 then
  230.                 xPos = xPos - 1
  231.             elseif layer == 1 then
  232.                 xPos = xPos + 1
  233.             end
  234.         end
  235.     end
  236. end
  237.  
  238.  
  239. textutils.slowPrint("Starting mining program (Mart)")
  240. --print("\nMake sure to meet the following requirements: ")
  241. --print(" - Put down a fuel chest, with the turtle on top of it")
  242. --print(" - Put down another chest two blocks to the left of the fuel chest")
  243. --print(" - Make sure the turtle has a littlebit of fuel on slot 16 (bottom right)")
  244.  
  245. textutils.slowPrint("\nConfiguring grid size...")
  246. print("How many blocks should i dig forward?")
  247. xSize = tonumber(io.read())
  248. write("\nDigging ")
  249. write(xSize)
  250. write(" blocks forward. How many blocks should i dig sideways?\n")
  251. ySize = tonumber(io.read())
  252. write("Digging a grid of ")
  253. write(xSize)
  254. write("x")
  255. write(ySize)
  256. write("\n")
  257. print("\nHow deep do you want me to dig?")
  258. zSize = tonumber(io.read())
  259. write("Ready to dig ")
  260. write(zSize)
  261. write(" blocks deep.")
  262.  
  263. textutils.slowPrint("\nInitialization complete. Starting program")
  264.  
  265. mine()
  266. turtle.select(inventorySlot)
  267. while enoughFuel(100) and finished == false do
  268.     if turtle.getItemSpace(16) <= 0 then
  269.         print("Inventory full. Banking items.")
  270.         local mineXPos = xPos
  271.         local mineYPos = yPos
  272.         local mineZPos = zPos
  273.         local mineDirection = direction
  274.         returnToBase()
  275.         bankItems()
  276.         turtle.forward()
  277.         moveToPos(mineXPos, mineYPos, mineZPos, mineDirection)
  278.         print("Resuming mining")
  279.     end
  280.  
  281.     miningAction()
  282.     --outputData()
  283. end
  284.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement