Mr_Chou

Ore Miner

Dec 30th, 2024 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | Gaming | 0 0
  1. -- Turtle Mining Script
  2. -- Ensure your turtle has a pickaxe and enough fuel.
  3.  
  4. -- Variables
  5. local targetOre
  6. local requiredAmount = 10 -- Amount of ore to mine
  7. local homePosition = {x = 0, y = 0, z = 0} -- Starting coordinates
  8. local chunkBoundary = {xMin = 0, xMax = 15, zMin = 0, zMax = 15} -- Current chunk boundaries
  9.  
  10. local minedAmount = 0
  11. local position = {x = 0, y = 0, z = 0} -- Turtle's current position
  12. local direction = 0 -- 0: forward, 1: right, 2: back, 3: left
  13. local spiralStep = 1
  14. local spiralDirection = 0
  15.  
  16. -- Helper Functions
  17. local function turnRight()
  18.     turtle.turnRight()
  19.     direction = (direction + 1) % 4
  20. end
  21.  
  22. local function turnLeft()
  23.     turtle.turnLeft()
  24.     direction = (direction - 1) % 4
  25. end
  26.  
  27. local function faceDirection(targetDirection)
  28.     while direction ~= targetDirection do
  29.         turnRight()
  30.     end
  31. end
  32.  
  33. local function moveForward()
  34.     while not turtle.forward() do
  35.         if turtle.detect() then
  36.             turtle.dig()
  37.         else
  38.             return false
  39.         end
  40.     end
  41.     if direction == 0 then position.z = position.z + 1
  42.     elseif direction == 1 then position.x = position.x + 1
  43.     elseif direction == 2 then position.z = position.z - 1
  44.     elseif direction == 3 then position.x = position.x - 1 end
  45.     return true
  46. end
  47.  
  48. local function moveUp()
  49.     while not turtle.up() do
  50.         if turtle.detectUp() then
  51.             turtle.digUp()
  52.         else
  53.             return false
  54.         end
  55.     end
  56.     position.y = position.y + 1
  57.     return true
  58. end
  59.  
  60. local function moveDown()
  61.     while not turtle.down() do
  62.         if turtle.detectDown() then
  63.             turtle.digDown()
  64.         else
  65.             return false
  66.         end
  67.     end
  68.     position.y = position.y - 1
  69.     return true
  70. end
  71.  
  72. local function detectOre()
  73.     local success, data = turtle.inspect()
  74.     if success then
  75.         if data.name == targetOre then
  76.             return true
  77.         end
  78.     end
  79.     return false
  80. end
  81.  
  82. local function mineOre()
  83.     if turtle.dig() then
  84.         minedAmount = minedAmount + 1
  85.     end
  86. end
  87.  
  88. local function isWithinChunkBounds()
  89.     return position.x >= chunkBoundary.xMin and position.x <= chunkBoundary.xMax and
  90.            position.z >= chunkBoundary.zMin and position.z <= chunkBoundary.zMax
  91. end
  92.  
  93. local function returnHome()
  94.     -- Move back to the original Y position
  95.     while position.y > homePosition.y do
  96.         moveDown()
  97.     end
  98.     while position.y < homePosition.y do
  99.         moveUp()
  100.     end
  101.  
  102.     -- Move back to the original Z position
  103.     if position.z > homePosition.z then
  104.         faceDirection(2) -- Face back
  105.         while position.z > homePosition.z do
  106.             moveForward()
  107.         end
  108.     elseif position.z < homePosition.z then
  109.         faceDirection(0) -- Face forward
  110.         while position.z < homePosition.z do
  111.             moveForward()
  112.         end
  113.     end
  114.  
  115.     -- Move back to the original X position
  116.     if position.x > homePosition.x then
  117.         faceDirection(3) -- Face left
  118.         while position.x > homePosition.x do
  119.             moveForward()
  120.         end
  121.     elseif position.x < homePosition.x then
  122.         faceDirection(1) -- Face right
  123.         while position.x < homePosition.x do
  124.             moveForward()
  125.         end
  126.     end
  127. end
  128.  
  129. local function depositOre()
  130.     for i = 1, 16 do
  131.         turtle.select(i)
  132.         turtle.drop()
  133.     end
  134.     turtle.select(1) -- Reset to slot 1
  135. end
  136.  
  137. local function spiralMove()
  138.     if spiralDirection % 2 == 0 then
  139.         for _ = 1, spiralStep do
  140.             if not moveForward() then
  141.                 return false
  142.             end
  143.         end
  144.     else
  145.         for _ = 1, spiralStep do
  146.             if not moveForward() then
  147.                 return false
  148.             end
  149.         end
  150.         spiralStep = spiralStep + 1
  151.     end
  152.     turnRight()
  153.     spiralDirection = (spiralDirection + 1) % 4
  154.     return true
  155. end
  156.  
  157. -- Setup
  158. print("Enter the ore to mine (e.g., minecraft:iron_ore):")
  159. targetOre = read()
  160.  
  161. print("Enter the turtle's starting X coordinate:")
  162. homePosition.x = tonumber(read())
  163.  
  164. print("Enter the turtle's starting Y coordinate:")
  165. homePosition.y = tonumber(read())
  166.  
  167. print("Enter the turtle's starting Z coordinate:")
  168. homePosition.z = tonumber(read())
  169.  
  170. -- Calculate chunk boundaries based on home position
  171. chunkBoundary.xMin = homePosition.x - (homePosition.x % 16)
  172. chunkBoundary.xMax = chunkBoundary.xMin + 15
  173. chunkBoundary.zMin = homePosition.z - (homePosition.z % 16)
  174. chunkBoundary.zMax = chunkBoundary.zMin + 15
  175.  
  176. position.x, position.y, position.z = homePosition.x, homePosition.y, homePosition.z
  177.  
  178. -- Main Loop
  179. while minedAmount < requiredAmount do
  180.     if not isWithinChunkBounds() then
  181.         print("Out of chunk bounds, returning home.")
  182.         break
  183.     end
  184.  
  185.     if detectOre() then
  186.         mineOre()
  187.     else
  188.         if not spiralMove() then
  189.             print("Obstacle encountered, returning home.")
  190.             break
  191.         end
  192.     end
  193. end
  194.  
  195. -- Return and Deposit
  196. returnHome()
  197. depositOre()
  198. print("Mining complete! Ores deposited.")
  199.  
Tags: #minecraft
Advertisement
Add Comment
Please, Sign In to add comment