Advertisement
dan330

Untitled

May 7th, 2025 (edited)
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.57 KB | None | 0 0
  1. -- Island Miner v0.2.0
  2. -- A turtle program that mines gravel and andesite around an island
  3. -- This program requires a turtle with a pickaxe
  4.  
  5. -- Constants
  6. local VERSION = "0.2.0"
  7. local STARTING_POSITION = {x = -279, y = 64, z = -182}
  8. local MAX_Y_LEVEL = 70
  9. local REFUEL_THRESHOLD = 50 -- Refuel when fuel level drops below this value
  10. local TARGET_BLOCKS = {
  11.     ["minecraft:gravel"] = true,
  12.     ["minecraft:andesite"] = true
  13. }
  14.  
  15. -- Debug logging function
  16. local function log(message)
  17.     print("[DEBUG] " .. message)
  18. end
  19.  
  20. -- Current position and orientation
  21. local position = {
  22.     x = STARTING_POSITION.x,
  23.     y = STARTING_POSITION.y,
  24.     z = STARTING_POSITION.z
  25. }
  26. local facing = 0 -- 0 = north, 1 = east, 2 = south, 3 = west
  27.  
  28. -- Function to check if a block is a target block
  29. local function isTargetBlock()
  30.     local success, data = turtle.inspect()
  31.     if success then
  32.         -- Log the block type we found
  33.         log("Found block: " .. data.name)
  34.        
  35.         -- Check if it's in our target list
  36.         local isTarget = TARGET_BLOCKS[data.name] == true
  37.        
  38.         if not isTarget then
  39.             log("Block is not a target block, skipping")
  40.         end
  41.        
  42.         return isTarget
  43.     end
  44.     return false
  45. end
  46.  
  47. -- Function to check if a block is exposed to air
  48. local function isExposedToAir()
  49.     -- Check if we can move up first before trying to move up
  50.     if turtle.detectUp() then
  51.         log("Block above is not air, can't check if target is exposed to air")
  52.         return false
  53.     end
  54.    
  55.     -- Move up to check if the block above is air
  56.     turtle.up()
  57.     position.y = position.y + 1
  58.    
  59.     -- Look down to check the block below
  60.     turtle.turnLeft()
  61.     turtle.turnLeft()
  62.     facing = (facing + 2) % 4
  63.    
  64.     local success, data = turtle.inspect()
  65.     local isAir = not success
  66.    
  67.     if success then
  68.         isAir = data.name == "minecraft:air"
  69.         log("Block above target is: " .. data.name .. ", isAir: " .. tostring(isAir))
  70.     else
  71.         log("No block detected above target, treating as air")
  72.     end
  73.    
  74.     -- Return to original position and orientation
  75.     turtle.turnLeft()
  76.     turtle.turnLeft()
  77.     facing = (facing + 2) % 4
  78.     turtle.down()
  79.     position.y = position.y - 1
  80.    
  81.     return isAir
  82. end
  83.  
  84. -- Function to refuel if needed
  85. local function refuelIfNeeded()
  86.     local fuelLevel = turtle.getFuelLevel()
  87.    
  88.     if fuelLevel < REFUEL_THRESHOLD then
  89.         print("Fuel low (" .. fuelLevel .. "), attempting to refuel...")
  90.        
  91.         -- Try each inventory slot for fuel
  92.         for i = 1, 16 do
  93.             turtle.select(i)
  94.             if turtle.refuel(0) then
  95.                 local before = turtle.getFuelLevel()
  96.                 turtle.refuel(1)
  97.                 local after = turtle.getFuelLevel()
  98.                 print("Refueled with " .. (after - before) .. " fuel units")
  99.                 if turtle.getFuelLevel() >= REFUEL_THRESHOLD then
  100.                     break
  101.                 end
  102.             end
  103.         end
  104.        
  105.         -- Check if refueling was successful
  106.         if turtle.getFuelLevel() < REFUEL_THRESHOLD then
  107.             print("WARNING: Unable to refuel to threshold. Current fuel: " .. turtle.getFuelLevel())
  108.         end
  109.     end
  110. end
  111.  
  112. -- Movement functions that update position
  113. local function forward()
  114.     refuelIfNeeded()
  115.     if turtle.forward() then
  116.         if facing == 0 then position.z = position.z - 1      -- North
  117.         elseif facing == 1 then position.x = position.x + 1  -- East
  118.         elseif facing == 2 then position.z = position.z + 1  -- South
  119.         elseif facing == 3 then position.x = position.x - 1  -- West
  120.         end
  121.         return true
  122.     end
  123.     return false
  124. end
  125.  
  126. local function up()
  127.     refuelIfNeeded()
  128.     if position.y < MAX_Y_LEVEL and turtle.up() then
  129.         position.y = position.y + 1
  130.         return true
  131.     end
  132.     return false
  133. end
  134.  
  135. local function down()
  136.     refuelIfNeeded()
  137.     if turtle.down() then
  138.         position.y = position.y - 1
  139.         return true
  140.     end
  141.     return false
  142. end
  143.  
  144. local function turnLeft()
  145.     turtle.turnLeft()
  146.     facing = (facing - 1) % 4
  147. end
  148.  
  149. local function turnRight()
  150.     turtle.turnRight()
  151.     facing = (facing + 1) % 4
  152. end
  153.  
  154. -- Function to check and mine target blocks
  155. local function checkAndMine()
  156.     -- Only check if it's exposed to air if it's a target block
  157.     if isTargetBlock() then
  158.         if isExposedToAir() then
  159.             print("Mining " .. position.x .. "," .. position.y .. "," .. position.z)
  160.             turtle.dig()
  161.             return true
  162.         else
  163.             log("Block not exposed to air, skipping")
  164.         end
  165.     end
  166.     return false
  167. end
  168.  
  169. -- Spiral search pattern to find and mine blocks
  170. local function spiralSearch(maxRadius)
  171.     local radius = 1
  172.     local direction = 0  -- 0 = north, 1 = east, 2 = south, 3 = west
  173.     local steps = 1
  174.    
  175.     while radius <= maxRadius do
  176.         for i = 1, 2 do
  177.             for j = 1, steps do
  178.                 checkAndMine()
  179.                
  180.                 -- Check blocks in other directions
  181.                 turnLeft()
  182.                 checkAndMine()
  183.                 turnRight()
  184.                
  185.                 turnRight()
  186.                 checkAndMine()
  187.                 turnLeft()
  188.                
  189.                 -- Move forward, but try not to dig if it's not a target block
  190.                 if not forward() then
  191.                     -- Check if it's a target block before digging
  192.                     if isTargetBlock() then
  193.                         log("Target block in path, digging to move forward")
  194.                         turtle.dig()
  195.                     else
  196.                         -- Try to go around the obstacle
  197.                         log("Non-target block in path, trying to go around")
  198.                         -- Try going up and over
  199.                         if position.y < MAX_Y_LEVEL - 1 and up() then
  200.                             forward()
  201.                             down()
  202.                         else
  203.                             -- If we can't go over, try going around
  204.                             turnLeft()
  205.                             if forward() then
  206.                                 turnRight()
  207.                                 forward()
  208.                                 turnRight()
  209.                                 forward()
  210.                                 turnLeft()
  211.                             else
  212.                                 turnRight()
  213.                                 turnRight()
  214.                                 if forward() then
  215.                                     turnLeft()
  216.                                     forward()
  217.                                     turnLeft()
  218.                                     forward()
  219.                                     turnRight()
  220.                                 else
  221.                                     -- If we can't go around, just dig through as a last resort
  222.                                     turnRight()
  223.                                     log("Unable to navigate around obstacle, digging through")
  224.                                     turtle.dig()
  225.                                     if not forward() then
  226.                                         print("Path completely blocked at " .. position.x .. "," .. position.y .. "," .. position.z)
  227.                                     end
  228.                                 end
  229.                             end
  230.                         end
  231.                     end
  232.                 end
  233.             end
  234.            
  235.             -- Change direction
  236.             turnLeft()
  237.             direction = (direction + 1) % 4
  238.         end
  239.        
  240.         steps = steps + 1
  241.         radius = radius + 1
  242.     end
  243. end
  244.  
  245. -- Main function to search at different y levels
  246. local function searchArea()
  247.     print("Island Miner v" .. VERSION)
  248.     print("Starting at position: " .. position.x .. "," .. position.y .. "," .. position.z)
  249.    
  250.     -- Start at the initial position
  251.     for y = position.y, MAX_Y_LEVEL - 1 do
  252.         print("Searching at y level: " .. position.y)
  253.         spiralSearch(20)  -- Search in a spiral pattern with max radius of 20 blocks
  254.        
  255.         -- Move up to the next level if not at max height
  256.         if position.y < MAX_Y_LEVEL - 1 then
  257.             up()
  258.         end
  259.     end
  260.    
  261.     -- Return to starting position
  262.     print("Mining complete, returning to start position")
  263.    
  264.     -- Navigate back to starting coordinates
  265.     while position.y > STARTING_POSITION.y do
  266.         down()
  267.     end
  268.    
  269.     print("Mining operation completed")
  270. end
  271.  
  272. -- Run the program
  273. searchArea()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement