giwdul

geoscan_V1

Jan 16th, 2024 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.43 KB | Source Code | 0 0
  1. -- Function to check if the desired peripheral is equipped
  2. local function isPeripheralEquipped(peripheralType)
  3.   local equipped = peripheral.getType("left")
  4.   return equipped == peripheralType
  5. end
  6.  
  7. -- Function to swap peripheral with the one in slot 16
  8. local function swapPeripheral()
  9.   turtle.select(16)
  10.   if not turtle.equipLeft() then
  11.       print("Failed to swap peripheral.")
  12.   else
  13.       print("Peripheral swapped.")
  14.   end
  15. end
  16.  
  17. -- Function to mine in front if needed
  18. local function mineIfNeeded()
  19. if turtle.detect() then
  20.   turtle.dig()
  21. end
  22. end
  23.  
  24. -- Function to mine above if needed
  25. local function mineUpIfNeeded()
  26. if turtle.detectUp() then
  27.   turtle.digUp()
  28. end
  29. end
  30.  
  31. -- Function to mine below if needed
  32. local function mineDownIfNeeded()
  33. if turtle.detectDown() then
  34.   turtle.digDown()
  35. end
  36. end
  37.  
  38. -- Function to check and mine ores around the turtle without moving
  39. local function checkAndMineAround()
  40. mineIfNeeded() -- Front
  41. turtle.turnRight()
  42. mineIfNeeded() -- Right
  43. turtle.turnRight()
  44. mineIfNeeded() -- Back
  45. turtle.turnRight()
  46. mineIfNeeded() -- Left
  47. turtle.turnLeft() -- Reset to original orientation
  48. mineUpIfNeeded() -- Up
  49. mineDownIfNeeded() -- Down
  50. turtle.turnLeft() -- Reset to original orientation
  51. turtle.turnLeft() -- Reset to original orientation
  52.  
  53. end
  54.  
  55. -- Function to move to a specific coordinate
  56. local function moveTo(x, y, z)
  57. -- Move in x direction
  58. for i = 1, math.abs(x) do
  59.   mineIfNeeded()
  60.   if x < 0 then
  61.     turtle.forward()
  62.   else
  63.     turtle.back()
  64.   end
  65. end
  66.  
  67. -- Move in y direction
  68. for i = 1, math.abs(y) do
  69.   if y > 0 then
  70.     turtle.up()
  71.   else
  72.     mineDownIfNeeded()
  73.     turtle.down()
  74.   end
  75. end
  76.  
  77. -- Move in z direction
  78. if z ~= 0 then
  79.   local turnFunc = z < 0 and turtle.turnRight or turtle.turnLeft
  80.   turnFunc()
  81.   for i = 1, math.abs(z) do
  82.     mineIfNeeded()
  83.     turtle.forward()
  84.   end
  85.   -- Return to the original facing direction
  86.   if z < 0 then
  87.     turtle.turnLeft()
  88.   else
  89.     turtle.turnRight()
  90.   end
  91. end
  92. end
  93.  
  94. -- Function to check and refuel the turtle
  95. local function refuelIfNeeded()
  96.   local fuelLevel = turtle.getFuelLevel()
  97.   local fuelThreshold = 1000 -- Set your desired fuel threshold
  98.  
  99.   if fuelLevel < fuelThreshold then
  100.       turtle.select(13) -- Select the Ender Chest with fuel in slot 13
  101.       turtle.place() -- Place the Ender Chest
  102.  
  103.       -- Assuming fuel is stored in the Ender Chest at a known slot (e.g., slot 12)
  104.       turtle.select(12) -- Select the slot with fuel
  105.       turtle.suck() -- Take some fuel
  106.       turtle.refuel() -- Refuel with the taken fuel
  107.  
  108.       turtle.select(13) -- Re-select the Ender Chest slot
  109.       turtle.dig() -- Pick up the Ender Chest
  110.   end
  111. end
  112.  
  113. -- Function to empty inventory into Ender Chest
  114. local function emptyInventoryIfNeeded()
  115.   if turtle.getItemCount(10) > 0 then -- Check if slot 10 has items
  116.       turtle.select(14) -- Select the Ender Chest for items in slot 14
  117.       turtle.place() -- Place the Ender Chest
  118.  
  119.       for i = 1, 10 do -- Loop through slots 1 to 10 to empty them
  120.           turtle.select(i)
  121.           turtle.drop() -- Drop items into the Ender Chest
  122.       end
  123.  
  124.       turtle.select(14) -- Re-select the Ender Chest slot
  125.       turtle.dig() -- Pick up the Ender Chest
  126.   end
  127. end
  128.  
  129.  
  130. -- Main Script
  131. while true do
  132.   local radius = 8 -- Scan radius set to 5
  133.  
  134.   -- Equip geoScanner if not already equipped
  135.   if not isPeripheralEquipped("geoScanner") then
  136.     swapPeripheral()
  137.   end
  138.  
  139.   -- Scan for ores
  140.   local geoScanner = peripheral.find("geoScanner")
  141.   if not geoScanner then
  142.     error("Geo Scanner not found")
  143.   end
  144.  
  145.   local data, reason = geoScanner.scan(radius)
  146.   if not data then
  147.     print("Scan failed: " .. reason)
  148.     return
  149.   end
  150.  
  151.   print("Scan successful. Searching for ores...")
  152.   for _, block in ipairs(data) do
  153.     if string.match(block.name, "_ore") then
  154.         local x, y, z = block.x, block.y, block.z
  155.         print("Ore found at: x=" .. x .. ", y=" .. y .. ", z=" .. z)
  156.  
  157.         -- Equip pickaxe if not already equipped
  158.         if not isPeripheralEquipped("pickaxe") then
  159.             swapPeripheral()
  160.         end
  161.  
  162.         -- Move to the ore and mine it
  163.         moveTo(x, y, z)
  164.         print("Mining ore: " .. block.name)
  165.         mineIfNeeded()
  166.  
  167.         -- Check and mine ores around the current position
  168.         checkAndMineAround()
  169.     refuelIfNeeded()
  170.       emptyInventoryIfNeeded()
  171.  
  172.         break -- Stop after handling the first ore
  173.     end
  174.   end
  175.   os.sleep(5)
  176. end
Tags: source
Add Comment
Please, Sign In to add comment