Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 3D Miner and Mapper with Fuel Check, Junk Dropping, and Disk Safety
- -- Config: target ores to mine
- local targetBlocks = {
- ["minecraft:coal_ore"] = true,
- ["minecraft:iron_ore"] = true,
- ["minecraft:copper_ore"] = true
- }
- -- Config: junk blocks to auto-drop
- local junkBlocks = {
- ["minecraft:cobblestone"] = true,
- ["minecraft:dirt"] = true,
- ["minecraft:granite"] = true,
- ["minecraft:diorite"] = true,
- ["minecraft:andesite"] = true,
- ["minecraft:gravel"] = true,
- ["minecraft:sand"] = true,
- ["minecraft:tuff"] = true
- }
- local fuelNames = {
- ["minecraft:coal"] = true,
- ["minecraft:charcoal"] = true,
- ["minecraft:lava_bucket"] = true
- }
- -- Position and direction tracking
- local pos = {x = 0, y = 0, z = 0}
- local dir = 0 -- 0=N,1=E,2=S,3=W
- local map = {}
- local visited = {}
- local function key(x,y,z) return x..","..y..","..z end
- local function saveMap(x, y, z, face, id)
- map[x] = map[x] or {}
- map[x][y] = map[x][y] or {}
- map[x][y][z] = map[x][y][z] or {}
- map[x][y][z][face] = id
- end
- -- Turn helpers
- local function turnLeft()
- dir = (dir + 3) % 4
- turtle.turnLeft()
- end
- local function turnRight()
- dir = (dir + 1) % 4
- turtle.turnRight()
- end
- local function faceName()
- if dir == 0 then return "north"
- elseif dir == 1 then return "east"
- elseif dir == 2 then return "south"
- else return "west" end
- end
- -- Dump junk blocks from inventory
- local function dumpJunk()
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item and junkBlocks[item.name] then
- turtle.select(i)
- turtle.drop() -- drops forward
- end
- end
- turtle.select(1)
- end
- -- Dig forward if target block is there
- local function tryDigForward()
- local success, data = turtle.inspect()
- if success and targetBlocks[data.name] then
- turtle.dig()
- dumpJunk()
- return true
- end
- return false
- end
- -- Move forward, update position, mark old block as empty
- local function tryForward()
- local px, py, pz = pos.x, pos.y, pos.z
- if tryDigForward() then sleep(0.4) end
- if turtle.forward() then
- -- Update pos
- if dir == 0 then pos.z = pos.z - 1
- elseif dir == 1 then pos.x = pos.x + 1
- elseif dir == 2 then pos.z = pos.z + 1
- elseif dir == 3 then pos.x = pos.x - 1 end
- -- Mark new position as empty (center)
- saveMap(pos.x, pos.y, pos.z, "center", "empty")
- return true
- end
- return false
- end
- -- Dig up if target block is there
- local function tryUp()
- local success, data = turtle.inspectUp()
- if success and targetBlocks[data.name] then
- turtle.digUp()
- dumpJunk()
- end
- if turtle.up() then
- pos.y = pos.y + 1
- saveMap(pos.x, pos.y, pos.z, "center", "empty")
- return true
- end
- return false
- end
- -- Dig down if target block is there
- local function tryDown()
- local success, data = turtle.inspectDown()
- if success and targetBlocks[data.name] then
- turtle.digDown()
- dumpJunk()
- end
- if turtle.down() then
- pos.y = pos.y - 1
- saveMap(pos.x, pos.y, pos.z, "center", "empty")
- return true
- end
- return false
- end
- -- Inspect all surroundings once per position
- local function inspectAll(force)
- local blockKey = key(pos.x, pos.y, pos.z)
- if visited[blockKey] and not force then return end
- visited[blockKey] = true
- local successF, dataF = turtle.inspect()
- if successF then saveMap(pos.x, pos.y, pos.z, "front", dataF.name) end
- local successUp, dataUp = turtle.inspectUp()
- if successUp then saveMap(pos.x, pos.y, pos.z, "up", dataUp.name) end
- local successDown, dataDown = turtle.inspectDown()
- if successDown then saveMap(pos.x, pos.y, pos.z, "down", dataDown.name) end
- for i = 1, 4 do
- turnRight()
- local successS, dataS = turtle.inspect()
- if successS then
- saveMap(pos.x, pos.y, pos.z, faceName(), dataS.name)
- end
- end
- end
- local fuelNames = {
- ["minecraft:coal"] = true,
- ["minecraft:charcoal"] = true,
- ["minecraft:lava_bucket"] = true
- }
- -- Then: define tryRefuel BEFORE any usage
- local function tryRefuel()
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item and fuelNames[item.name] then
- local count = item.count
- local useAmount = 1
- if count >= 16 then
- useAmount = math.floor(count * 0.25)
- end
- turtle.select(i)
- local success = turtle.refuel(useAmount)
- if success then
- print("Refueled using "..useAmount.." of "..item.name)
- return true
- else
- print("Failed to refuel with "..item.name)
- return false
- end
- end
- end
- print("No fuel in inventory to refuel")
- return false
- end
- -- Check fuel level; return true if below 25%
- local function checkFuel()
- local level = turtle.getFuelLevel()
- local limit = turtle.getFuelLimit()
- if level / limit < 0.25 then
- print("Fuel below 25%, attempting to refuel...")
- if not tryRefuel() then
- print("Refuel failed, returning home...")
- --goHome()
- return true
- end
- end
- return false
- end
- -- Turn turtle to face given direction (0-3)
- local function faceDirection(targetDir)
- while dir ~= targetDir do
- turnRight()
- end
- end
- -- Move backward and update position
- local function tryBack()
- if turtle.back() then
- if dir == 0 then pos.z = pos.z + 1
- elseif dir == 1 then pos.x = pos.x - 1
- elseif dir == 2 then pos.z = pos.z - 1
- elseif dir == 3 then pos.x = pos.x + 1 end
- return true
- end
- return false
- end
- -- Return safely to spawn (0,0,0), avoiding (0,0,-1)
- local function goHome()
- -- Move vertically to y=0
- while pos.y > 0 do tryDown() end
- while pos.y < 0 do tryUp() end
- -- Move horizontally x=0
- if pos.x > 0 then faceDirection(3) -- west
- elseif pos.x < 0 then faceDirection(1) -- east
- end
- while pos.x ~= 0 do tryForward() end
- -- Move horizontally z=0, avoid (0,0,-1)
- if pos.z > 0 then faceDirection(0) -- north
- elseif pos.z < 0 then faceDirection(2) -- south
- end
- while pos.z ~= 0 do
- if pos.x == 0 and pos.y == 0 and pos.z == -1 then
- print("Avoiding disk drive at (0,0,-1)")
- break
- end
- tryForward()
- end
- end
- -- dir: 0 = north, 1 = east, 2 = south, 3 = west
- local function getDirectionOffset(dir)
- if dir == 0 then return 0, -1 -- north: +z goes negative
- elseif dir == 1 then return 1, 0 -- east
- elseif dir == 2 then return 0, 1 -- south
- elseif dir == 3 then return -1, 0 -- west
- end
- end
- -- Recursive exploration of area
- local function explore()
- if checkFuel() then
- goHome()
- writeMap()
- print("Stopped: Fuel below 25%")
- return
- end
- inspectAll()
- -- Explore vertically down first
- if tryDown() then
- explore()
- tryUp()
- end
- -- Explore horizontally in 4 directions
- for i = 1, 4 do
- local dx, dz = getDirectionOffset(dir) -- based on current direction
- local tx = pos.x + dx
- local ty = pos.y
- local tz = pos.z + dz
- if not map[tx] or not map[tx][ty] or not map[tx][ty][tz] then
- if tryForward() then
- explore()
- tryBack()
- end
- end
- turnRight()
- end
- -- Optional: Explore up after all else
- if tryUp() then
- explore()
- tryDown()
- end
- end
- -- Save map to disk under turtle
- function writeMap()
- if not fs.exists("disk") then
- print("No disk under turtle to save map!")
- return
- end
- local f = fs.open("disk/map.json", "w")
- f.write(textutils.serializeJSON(map))
- f.close()
- print("Map saved to disk/map.json")
- end
- -- Start script
- print("Starting 3D mining and mapping...")
- explore()
- goHome()
- writeMap()
- print("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement