Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Program
- local modem = peripheral.find("modem") or error("No modem found")
- modem.open(10000)
- local scanData = {}
- local miningTargets = {}
- -- Function to update position based on GPS
- local function updatePosition()
- local x, y, z = gps.locate()
- if not x then error("Could not get GPS signal") end
- return math.floor(x + 0.5), math.floor(y + 0.5), math.floor(z + 0.5)
- end
- -- Function to determine facing direction
- local function getFacing()
- local x, y, z = updatePosition()
- for i = 0, 3 do
- if turtle.forward() then
- local newX, newY, newZ = updatePosition()
- turtle.back()
- if newX > x then return 1 -- East
- elseif newX < x then return 3 -- West
- elseif newZ > z then return 2 -- South
- elseif newZ < z then return 0 -- North
- end
- end
- turtle.turnRight()
- end
- error("Could not determine facing direction")
- end
- -- Initialize position and facing
- local x, y, z = updatePosition()
- local facing = getFacing()
- print("Turtle initialized at:", x, y, z, "facing", facing)
- -- Function to turn to a specific direction
- local function turnTo(targetFacing)
- while facing ~= targetFacing do
- turtle.turnRight()
- facing = (facing + 1) % 4
- end
- end
- -- Function to move to adjacent block
- local function moveTo(tx, ty, tz)
- if ty > y then
- while not turtle.up() do turtle.digUp() end
- y = y + 1
- elseif ty < y then
- while not turtle.down() do turtle.digDown() end
- y = y - 1
- else
- local dx, dz = tx - x, tz - z
- if dx ~= 0 then
- turnTo(dx > 0 and 1 or 3)
- while not turtle.forward() do turtle.dig() end
- x = tx
- elseif dz ~= 0 then
- turnTo(dz > 0 and 2 or 0)
- while not turtle.forward() do turtle.dig() end
- z = tz
- end
- end
- end
- -- A* pathfinding algorithm
- local function findPath(startX, startY, startZ, endX, endY, endZ)
- local function heuristic(x, y, z)
- return math.abs(x - endX) + math.abs(y - endY) + math.abs(z - endZ)
- end
- local function getNeighbors(x, y, z)
- local neighbors = {}
- for _, offset in ipairs({{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}}) do
- local nx, ny, nz = x + offset[1], y + offset[2], z + offset[3]
- if scanData[nx..","..ny..","..nz] ~= "minecraft:bedrock" then
- table.insert(neighbors, {nx, ny, nz})
- end
- end
- return neighbors
- end
- local openSet = {{x = startX, y = startY, z = startZ, g = 0, h = heuristic(startX, startY, startZ)}}
- local closedSet = {}
- local cameFrom = {}
- while #openSet > 0 do
- table.sort(openSet, function(a, b) return (a.g + a.h) < (b.g + b.h) end)
- local current = table.remove(openSet, 1)
- if current.x == endX and current.y == endY and current.z == endZ then
- local path = {}
- while current do
- table.insert(path, 1, {x = current.x, y = current.y, z = current.z})
- current = cameFrom[current.x .. "," .. current.y .. "," .. current.z]
- end
- return path
- end
- closedSet[current.x .. "," .. current.y .. "," .. current.z] = true
- for _, neighbor in ipairs(getNeighbors(current.x, current.y, current.z)) do
- local nx, ny, nz = table.unpack(neighbor)
- if not closedSet[nx .. "," .. ny .. "," .. nz] then
- local tentative_g = current.g + 1
- local node = {x = nx, y = ny, z = nz, g = tentative_g, h = heuristic(nx, ny, nz)}
- local idx = nil
- for i, v in ipairs(openSet) do
- if v.x == nx and v.y == ny and v.z == nz then
- idx = i
- break
- end
- end
- if idx then
- if tentative_g < openSet[idx].g then
- openSet[idx] = node
- cameFrom[nx .. "," .. ny .. "," .. nz] = current
- end
- else
- table.insert(openSet, node)
- cameFrom[nx .. "," .. ny .. "," .. nz] = current
- end
- end
- end
- end
- return nil -- No path found
- end
- -- Function to mine to specific coordinates
- local function mineTo(tx, ty, tz)
- local path = findPath(x, y, z, tx, ty, tz)
- if not path then
- print("No path found to", tx, ty, tz)
- return false
- end
- for i = 2, #path do -- Start from 2 because 1 is the current position
- local node = path[i]
- moveTo(node.x, node.y, node.z)
- end
- return true
- end
- -- Function to mine all targets
- local function mineAllTargets()
- for _, target in ipairs(miningTargets) do
- if mineTo(target.x, target.y, target.z) then
- turtle.dig()
- modem.transmit(10000, 10000, {cmd = "ore_mined", x = target.x, y = target.y, z = target.z})
- else
- modem.transmit(10000, 10000, {cmd = "error", msg = "Could not reach target"})
- end
- end
- modem.transmit(10000, 10000, {cmd = "mining_complete"})
- end
- -- Main program loop
- while true do
- local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
- if channel == 10000 then
- if message.cmd == "scan_data" then
- scanData = message.data
- print("Received scan data")
- elseif message.cmd == "mine_targets" then
- miningTargets = message.data
- print("Received mining targets. Starting mining operation.")
- mineAllTargets()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment