Advertisement
squidingtin

CC turtle

Jun 9th, 2025 (edited)
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.70 KB | None | 0 0
  1. -- 3D Miner and Mapper with Fuel Check, Junk Dropping, and Disk Safety
  2.  
  3. -- Config: target ores to mine
  4. local targetBlocks = {
  5.     ["minecraft:coal_ore"] = true,
  6.     ["minecraft:iron_ore"] = true,
  7.     ["minecraft:copper_ore"] = true
  8. }
  9.  
  10. -- Config: junk blocks to auto-drop
  11. local junkBlocks = {
  12.     ["minecraft:cobblestone"] = true,
  13.     ["minecraft:dirt"] = true,
  14.     ["minecraft:granite"] = true,
  15.     ["minecraft:diorite"] = true,
  16.     ["minecraft:andesite"] = true,
  17.     ["minecraft:gravel"] = true,
  18.     ["minecraft:sand"] = true,
  19.     ["minecraft:tuff"] = true
  20. }
  21.  
  22. local fuelNames = {
  23.     ["minecraft:coal"] = true,
  24.     ["minecraft:charcoal"] = true,
  25.     ["minecraft:lava_bucket"] = true
  26. }
  27. -- Position and direction tracking
  28. local pos = {x = 0, y = 0, z = 0}
  29. local dir = 0 -- 0=N,1=E,2=S,3=W
  30.  
  31. local map = {}
  32. local visited = {}
  33.  
  34. local function key(x,y,z) return x..","..y..","..z end
  35.  
  36. local function saveMap(x, y, z, face, id)
  37.     map[x] = map[x] or {}
  38.     map[x][y] = map[x][y] or {}
  39.     map[x][y][z] = map[x][y][z] or {}
  40.     map[x][y][z][face] = id
  41. end
  42.  
  43. -- Turn helpers
  44. local function turnLeft()
  45.     dir = (dir + 3) % 4
  46.     turtle.turnLeft()
  47. end
  48.  
  49. local function turnRight()
  50.     dir = (dir + 1) % 4
  51.     turtle.turnRight()
  52. end
  53.  
  54. local function faceName()
  55.     if dir == 0 then return "north"
  56.     elseif dir == 1 then return "east"
  57.     elseif dir == 2 then return "south"
  58.     else return "west" end
  59. end
  60.  
  61. -- Dump junk blocks from inventory
  62. local function dumpJunk()
  63.     for i = 1, 16 do
  64.         local item = turtle.getItemDetail(i)
  65.         if item and junkBlocks[item.name] then
  66.             turtle.select(i)
  67.             turtle.drop() -- drops forward
  68.         end
  69.     end
  70.     turtle.select(1)
  71. end
  72.  
  73. -- Dig forward if target block is there
  74. local function tryDigForward()
  75.     local success, data = turtle.inspect()
  76.     if success and targetBlocks[data.name] then
  77.         turtle.dig()
  78.         dumpJunk()
  79.         return true
  80.     end
  81.     return false
  82. end
  83.  
  84. -- Move forward, update position, mark old block as empty
  85. local function tryForward()
  86.     local px, py, pz = pos.x, pos.y, pos.z
  87.  
  88.     if tryDigForward() then sleep(0.4) end
  89.  
  90.     if turtle.forward() then
  91.         -- Update pos
  92.         if dir == 0 then pos.z = pos.z - 1
  93.         elseif dir == 1 then pos.x = pos.x + 1
  94.         elseif dir == 2 then pos.z = pos.z + 1
  95.         elseif dir == 3 then pos.x = pos.x - 1 end
  96.  
  97.         -- Mark new position as empty (center)
  98.         saveMap(pos.x, pos.y, pos.z, "center", "empty")
  99.  
  100.         return true
  101.     end
  102.     return false
  103. end
  104.  
  105. -- Dig up if target block is there
  106. local function tryUp()
  107.     local success, data = turtle.inspectUp()
  108.     if success and targetBlocks[data.name] then
  109.         turtle.digUp()
  110.         dumpJunk()
  111.     end
  112.     if turtle.up() then
  113.         pos.y = pos.y + 1
  114.         saveMap(pos.x, pos.y, pos.z, "center", "empty")
  115.         return true
  116.     end
  117.     return false
  118. end
  119.  
  120. -- Dig down if target block is there
  121. local function tryDown()
  122.     local success, data = turtle.inspectDown()
  123.     if success and targetBlocks[data.name] then
  124.         turtle.digDown()
  125.         dumpJunk()
  126.     end
  127.     if turtle.down() then
  128.         pos.y = pos.y - 1
  129.         saveMap(pos.x, pos.y, pos.z, "center", "empty")
  130.         return true
  131.     end
  132.     return false
  133. end
  134.  
  135. -- Inspect all surroundings once per position
  136. local function inspectAll(force)
  137.     local blockKey = key(pos.x, pos.y, pos.z)
  138.     if visited[blockKey] and not force then return end
  139.     visited[blockKey] = true
  140.  
  141.     local successF, dataF = turtle.inspect()
  142.     if successF then saveMap(pos.x, pos.y, pos.z, "front", dataF.name) end
  143.  
  144.     local successUp, dataUp = turtle.inspectUp()
  145.     if successUp then saveMap(pos.x, pos.y, pos.z, "up", dataUp.name) end
  146.  
  147.     local successDown, dataDown = turtle.inspectDown()
  148.     if successDown then saveMap(pos.x, pos.y, pos.z, "down", dataDown.name) end
  149.  
  150.     for i = 1, 4 do
  151.         turnRight()
  152.         local successS, dataS = turtle.inspect()
  153.         if successS then
  154.             saveMap(pos.x, pos.y, pos.z, faceName(), dataS.name)
  155.         end
  156.     end
  157. end
  158.  
  159.  
  160. local fuelNames = {
  161.     ["minecraft:coal"] = true,
  162.     ["minecraft:charcoal"] = true,
  163.     ["minecraft:lava_bucket"] = true
  164. }
  165.  
  166. -- Then: define tryRefuel BEFORE any usage
  167. local function tryRefuel()
  168.     for i = 1, 16 do
  169.         local item = turtle.getItemDetail(i)
  170.         if item and fuelNames[item.name] then
  171.             local count = item.count
  172.             local useAmount = 1
  173.             if count >= 16 then
  174.                 useAmount = math.floor(count * 0.25)
  175.             end
  176.  
  177.             turtle.select(i)
  178.             local success = turtle.refuel(useAmount)
  179.  
  180.             if success then
  181.                 print("Refueled using "..useAmount.." of "..item.name)
  182.                 return true
  183.             else
  184.                 print("Failed to refuel with "..item.name)
  185.                 return false
  186.             end
  187.         end
  188.     end
  189.     print("No fuel in inventory to refuel")
  190.     return false
  191. end
  192.  
  193. -- Check fuel level; return true if below 25%
  194. local function checkFuel()
  195.     local level = turtle.getFuelLevel()
  196.     local limit = turtle.getFuelLimit()
  197.  
  198.     if level / limit < 0.25 then
  199.         print("Fuel below 25%, attempting to refuel...")
  200.         if not tryRefuel() then
  201.             print("Refuel failed, returning home...")
  202.             --goHome()
  203.             return true
  204.         end
  205.     end
  206.     return false
  207. end
  208.  
  209.  
  210. -- Turn turtle to face given direction (0-3)
  211. local function faceDirection(targetDir)
  212.     while dir ~= targetDir do
  213.         turnRight()
  214.     end
  215. end
  216.  
  217. -- Move backward and update position
  218. local function tryBack()
  219.     if turtle.back() then
  220.         if dir == 0 then pos.z = pos.z + 1
  221.         elseif dir == 1 then pos.x = pos.x - 1
  222.         elseif dir == 2 then pos.z = pos.z - 1
  223.         elseif dir == 3 then pos.x = pos.x + 1 end
  224.         return true
  225.     end
  226.     return false
  227. end
  228.  
  229. -- Return safely to spawn (0,0,0), avoiding (0,0,-1)
  230. local function goHome()
  231.     -- Move vertically to y=0
  232.     while pos.y > 0 do tryDown() end
  233.     while pos.y < 0 do tryUp() end
  234.  
  235.     -- Move horizontally x=0
  236.     if pos.x > 0 then faceDirection(3) -- west
  237.     elseif pos.x < 0 then faceDirection(1) -- east
  238.     end
  239.     while pos.x ~= 0 do tryForward() end
  240.  
  241.     -- Move horizontally z=0, avoid (0,0,-1)
  242.     if pos.z > 0 then faceDirection(0) -- north
  243.     elseif pos.z < 0 then faceDirection(2) -- south
  244.     end
  245.     while pos.z ~= 0 do
  246.         if pos.x == 0 and pos.y == 0 and pos.z == -1 then
  247.             print("Avoiding disk drive at (0,0,-1)")
  248.             break
  249.         end
  250.         tryForward()
  251.     end
  252. end
  253.  
  254. -- dir: 0 = north, 1 = east, 2 = south, 3 = west
  255. local function getDirectionOffset(dir)
  256.     if dir == 0 then return 0, -1  -- north: +z goes negative
  257.     elseif dir == 1 then return 1, 0  -- east
  258.     elseif dir == 2 then return 0, 1  -- south
  259.     elseif dir == 3 then return -1, 0  -- west
  260.     end
  261. end
  262.  
  263.  
  264. -- Recursive exploration of area
  265. local function explore()
  266.     if checkFuel() then
  267.         goHome()
  268.         writeMap()
  269.         print("Stopped: Fuel below 25%")
  270.         return
  271.     end
  272.  
  273.     inspectAll()
  274.  
  275.     -- Explore vertically down first
  276.     if tryDown() then
  277.         explore()
  278.         tryUp()
  279.     end
  280.  
  281.     -- Explore horizontally in 4 directions
  282.     for i = 1, 4 do
  283.         local dx, dz = getDirectionOffset(dir) -- based on current direction
  284.         local tx = pos.x + dx
  285.         local ty = pos.y
  286.         local tz = pos.z + dz
  287.  
  288.         if not map[tx] or not map[tx][ty] or not map[tx][ty][tz] then
  289.             if tryForward() then
  290.                 explore()
  291.                 tryBack()
  292.             end
  293.         end
  294.  
  295.         turnRight()
  296.     end
  297.  
  298.     -- Optional: Explore up after all else
  299.     if tryUp() then
  300.         explore()
  301.         tryDown()
  302.     end
  303. end
  304.  
  305.  
  306. -- Save map to disk under turtle
  307. function writeMap()
  308.     if not fs.exists("disk") then
  309.         print("No disk under turtle to save map!")
  310.         return
  311.     end
  312.     local f = fs.open("disk/map.json", "w")
  313.     f.write(textutils.serializeJSON(map))
  314.     f.close()
  315.     print("Map saved to disk/map.json")
  316. end
  317.  
  318. -- Start script
  319. print("Starting 3D mining and mapping...")
  320. explore()
  321. goHome()
  322. writeMap()
  323. print("Done.")
  324.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement