Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local startX, startY, startZ, startDir = 0, 0, 0, "north"
- local currX, currY, currZ, currDir = startX, startY, startZ, startDir
- -- Initialize the unmined table
- unmined = {}
- for x = -26, 26 do
- unmined[x] = {}
- for y = 0, 4 do
- unmined[x][y] = {}
- for z = -26, 26 do
- unmined[x][y][z] = true
- end
- end
- end
- function move(dir, calledByReturnHome)
- -- If the function was not called by returnHome, check if there is an item in slot 16
- if not calledByReturnHome and turtle.getItemCount(16) > 0 then
- -- If there is, run the returnHome function
- returnHome(true)
- end
- if dir == "up" or dir == "down" then
- -- Check if we are at y0 and trying to move down, or at y4 and trying to move up
- if (currY == 0 and dir == "down") or (currY == 4 and dir == "up") then
- -- Do not move in the specified direction if we are at y0 and trying to move down, or at y4 and trying to move up
- return
- end
- if dir == "up" then
- if turtle.detectUp() then
- turtle.digUp()
- turtle.up()
- updatePosition(dir)
- else
- turtle.up()
- updatePosition(dir)
- end
- elseif dir == "down" then
- if turtle.detectDown() then
- turtle.digDown()
- turtle.down()
- updatePosition(dir)
- else
- turtle.down()
- updatePosition(dir)
- end
- end
- else
- if turtle.detect() then
- turtle.dig()
- if dir == "forward" then
- turtle.forward()
- updatePosition(dir)
- else
- turtle.back()
- updatePosition(dir)
- end
- else
- if dir == "forward" then
- turtle.forward()
- updatePosition(dir)
- else
- turtle.back()
- updatePosition(dir)
- end
- end
- end
- -- Check if we are at the outer bounds of the unmined table on the x or z axis
- if currX == -25 or currX == 25 or currZ == -25 or currZ == 25 then
- -- If we are, move to the unmined block closest to 0, 0, 0
- moveToUnmined()
- end
- end
- function updatePosition(dir)
- if dir == "forward" then
- currZ = currZ + (currDir == "north" and 1 or (currDir == "south" and -1 or 0))
- currX = currX + (currDir == "east" and 1 or (currDir == "west" and -1 or 0))
- elseif dir == "backward" then
- currZ = currZ + (currDir == "south" and 1 or (currDir == "north" and -1 or 0))
- currX = currX + (currDir == "west" and 1 or (currDir == "east" and -1 or 0))
- elseif dir == "up" then
- currY = currY + 1
- elseif dir == "down" then
- currY = currY - 1
- end
- -- Check if unmined[currX][currY][currZ] is nil before setting it to false
- if unmined[currX][currY][currZ] == nil then
- -- Print an error and call moveToUnmined()
- print("Error: unmined[" .. currX .. "][" .. currY .. "][" .. currZ .. "] is nil")
- moveToUnmined()
- else
- unmined[currX][currY][currZ] = false
- end
- end
- function returnHome()
- local atHome = false
- while not atHome do
- if currX > startX then
- turnTo("west")
- move("forward", true)
- elseif currX < startX then
- turnTo("east")
- move("forward", true)
- elseif currY > startY then
- move("down", true)
- elseif currY < startY then
- move("up", true)
- elseif currZ > startZ then
- turnTo("south")
- move("forward", true)
- elseif currZ < startZ then
- turnTo("north")
- move("forward", true)
- else
- atHome = true
- end
- end
- end
- function turnTo(dir)
- if currDir == "north" then
- if dir == "east" then
- turn("right")
- elseif dir == "south" then
- turn("left")
- turn("left")
- elseif dir == "west" then
- turn("left")
- end
- elseif currDir == "east" then
- if dir == "south" then
- turn("right")
- elseif dir == "west" then
- turn("left")
- turn("left")
- elseif dir == "north" then
- turn("left")
- end
- elseif currDir == "south" then
- if dir == "west" then
- turn("right")
- elseif dir == "north" then
- turn("left")
- turn("left")
- elseif dir == "east" then
- turn("left")
- end
- elseif currDir == "west" then
- if dir == "north" then
- turn("right")
- elseif dir == "east" then
- turn("left")
- turn("left")
- elseif dir == "south" then
- turn("left")
- end
- end
- currDir = dir
- end
- function turn(dir)
- if dir == "left" then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- if currDir == "north" then
- currDir = (dir == "left") and "west" or "east"
- elseif currDir == "east" then
- currDir = (dir == "left") and "north" or "south"
- elseif currDir == "south" then
- currDir = (dir == "left") and "east" or "west"
- elseif currDir == "west" then
- currDir = (dir == "left") and "south" or "north"
- end
- end
- function refuel()
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(1) then
- return
- end
- end
- returnHome()
- turtle.suckUp()
- refuel()
- end
- function mine()
- local blocksMined = 0
- local consecutiveNoBlocks = 0
- while blocksMined < 256 do
- if turtle.getFuelLevel() < 1000 then
- returnHome()
- refuel()
- end
- if turtle.getItemCount(16) > 0 then
- returnHome()
- for i = 1, 16 do
- turtle.select(i)
- turtle.dropDown()
- turtle.select(1)
- end
- end
- if currY > 4 then
- move("down")
- elseif currY < 0 then
- move("up")
- elseif math.random(1,5) == 1 then
- turn(math.random(1,2) == 1 and "left" or "right")
- elseif math.random(1,20) == 1 then
- if math.random(1,2) == 1 then
- move("up")
- else
- move("down")
- end
- else
- local moved = false
- for i = 1, 10 do
- if turtle.detect() then
- move("forward")
- blocksMined = blocksMined + 1
- consecutiveNoBlocks = 0
- moved = true
- break
- else
- move("forward")
- consecutiveNoBlocks = consecutiveNoBlocks + 1
- if consecutiveNoBlocks >= 10 then
- moveToUnmined()
- consecutiveNoBlocks = 0
- break
- end
- end
- end
- if not moved then
- consecutiveNoBlocks = 0
- end
- end
- end
- returnHome()
- end
- function moveToUnmined()
- -- Find the unmined block closest to the starting position (0, 0, 0)
- local closestX, closestY, closestZ = 0, 0, 0
- local closestDistance = math.huge
- for x = -26, 26 do
- for y = 0, 4 do
- for z = -26, 26 do
- if unmined[x][y][z] then
- -- Calculate the distance from the starting position to the unmined block
- local distance = math.sqrt(x^2 + y^2 + z^2)
- if distance < closestDistance then
- -- Update the closest unmined block and distance
- closestX, closestY, closestZ = x, y, z
- closestDistance = distance
- end
- end
- end
- end
- end
- -- Move to the closest unmined block and mine it
- local xDiff = currX - closestX
- local yDiff = currY - closestY
- local zDiff = currZ - closestZ
- -- Move horizontally towards the closest unmined block
- while xDiff ~= 0 or zDiff ~= 0 do
- if xDiff > 0 then
- move("left")
- xDiff = xDiff - 1
- elseif xDiff < 0 then
- move("right")
- xDiff = xDiff + 1
- elseif zDiff > 0 then
- move("forward")
- zDiff = zDiff - 1
- elseif zDiff < 0 then
- move("back")
- zDiff = zDiff + 1
- end
- end
- -- Move vertically towards the closest unmined block
- while yDiff ~= 0 do
- if yDiff > 0 then
- move("down")
- yDiff = yDiff - 1
- elseif yDiff < 0 then
- move("up")
- yDiff = yDiff + 1
- end
- end
- -- Mine the closest unmined block
- turtle.dig()
- -- Update the unmined table
- unmined[closestX][closestY][closestZ] = false
- end
- mine()
Advertisement
Add Comment
Please, Sign In to add comment