Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Minecraft Turtle script to make a ocean area usable for a player by placing blocks under itself and therefore creating a landmass.
- --[[
- The Script takes the following arguments:
- 1. The length of the area
- 2. The width of the area
- 3. The depth how much the turtle should dig down
- The turtle will go down (Without digging because there should be water) the specified depth and then place blocks under itself to create a landmass.
- Therefore it will go back and forth and turn left and right to create a new layer of blocks.
- If the turtle is done with the first layer it will go up by a block and place blocks there to create a new layer.
- This will be repeated until the turtle is at the same level as it started.
- Behind the start position of the turtle will be a chest with the blocks that the turtle needs to place.
- If there are blocks in the way the turtle will dig through them to get to its desired position.
- The turtle will also check if it has enough fuel and refuel if necessary.
- The turtle will also check if there are enough blocks in the chest and if not it will pause the program.
- ]]
- -- Function to check and refuel if necessary
- function checkFuel(requiredFuel)
- local currentFuel = turtle.getFuelLevel()
- -- If fuel is unlimited, return true
- if currentFuel == "unlimited" then
- return true
- end
- -- Check if we have enough fuel
- if currentFuel < requiredFuel then
- print("Low fuel. Attempting to refuel...")
- -- Try to refuel from inventory
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then
- local amountToRefuel = math.min(64, math.ceil((requiredFuel - currentFuel) / 80))
- if turtle.refuel(amountToRefuel) then
- print("Refueled successfully.")
- -- Check the new fuel level again
- if turtle.getFuelLevel() >= requiredFuel then
- return true
- end
- end
- end
- end
- print("WARNING: Not enough fuel. Need at least " .. requiredFuel .. " fuel points.")
- return false
- end
- return true
- end
- -- Function to ensure a block is selected
- function ensureBlockSelected()
- if selectBlockSlot() then
- return true
- end
- print("Out of blocks! Attempting to get more...")
- if not getBlocks() then
- print("Please add more blocks to the chest and press Enter to continue.")
- read()
- if not getBlocks() then
- print("Still no blocks available. Exiting.")
- return false
- end
- end
- return selectBlockSlot()
- end
- -- Function to safely place a block downward
- function safelyPlaceBlockDown()
- if not ensureBlockSelected() then
- return false
- end
- -- If there's already a block down, consider it success
- if turtle.detectDown() then
- return true
- end
- -- Try to place block
- local success = turtle.placeDown()
- -- Try one more time if failed
- if not success then
- if not ensureBlockSelected() then
- return false
- end
- success = turtle.placeDown()
- end
- return success
- end
- -- Function to get blocks from the chest behind
- function getBlocks()
- -- Save current selected slot
- local currentSlot = turtle.getSelectedSlot()
- -- Turn around to face the chest
- turtle.turnRight()
- turtle.turnRight()
- -- Check if chest exists
- if not turtle.detect() then
- print("No chest found behind the turtle!")
- -- Return to original orientation
- turtle.turnRight()
- turtle.turnRight()
- turtle.select(currentSlot)
- return false
- end
- -- Count current blocks
- local totalBlocks = 0
- for i = 1, 16 do
- totalBlocks = totalBlocks + turtle.getItemCount(i)
- end
- -- Try to get more blocks if we have empty slots
- local slotsAvailable = false
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- slotsAvailable = true
- break
- end
- end
- if slotsAvailable then
- -- Try to get blocks until inventory is full or chest is empty
- local attemptCount = 0
- while slotsAvailable and attemptCount < 16 do
- turtle.select(1)
- if not turtle.suck() then
- break
- end
- print("Collected blocks from chest.")
- -- Check if we still have empty slots
- slotsAvailable = false
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- slotsAvailable = true
- break
- end
- end
- attemptCount = attemptCount + 1
- end
- end
- -- Turn back around
- turtle.turnRight()
- turtle.turnRight()
- -- Count blocks again
- local newTotalBlocks = 0
- for i = 1, 16 do
- newTotalBlocks = newTotalBlocks + turtle.getItemCount(i)
- end
- -- Restore selected slot
- turtle.select(currentSlot)
- return newTotalBlocks > 0
- end
- -- Function to select a slot with blocks
- function selectBlockSlot()
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- return true
- end
- end
- return false
- end
- -- Function to move to the starting position
- function moveToStartPosition(depth)
- -- Subtract 1 from depth to fix the "one block too deep" issue
- for i = 1, depth - 1 do
- -- Dig if there's a block in the way
- if turtle.detectDown() then
- turtle.digDown()
- end
- if not turtle.down() then
- -- Try digging again in case something fell in the way
- if turtle.detectDown() then
- turtle.digDown()
- if not turtle.down() then
- print("Couldn't move down at depth " .. i .. " even after digging.")
- return false
- end
- else
- print("Couldn't move down at depth " .. i)
- return false
- end
- end
- end
- return true
- end
- -- Function to try moving forward, digging if necessary
- function moveForwardSafely()
- if turtle.detect() then
- turtle.dig()
- os.sleep(0.5) -- Wait for falling blocks/sand
- end
- if not turtle.forward() then
- -- Try digging again
- if turtle.detect() then
- turtle.dig()
- os.sleep(0.5)
- return turtle.forward()
- else
- print("Couldn't move forward - path might be blocked")
- return false
- end
- end
- return true
- end
- -- Function to place a single layer of blocks
- function placeLayerOfBlocks(length, width)
- local turnRight = true
- local posX, posZ = 0, 0 -- Track position relative to start
- for w = 1, width do
- -- Place block under current position
- if not safelyPlaceBlockDown() then
- print("Failed to place block at position X=" .. posX .. ", Z=" .. posZ)
- return false
- end
- -- Move forward placing blocks for the entire row (except for the last row's last position)
- if w < width or posX < length - 1 then
- for l = 1, length-1 do
- -- Move forward, digging if necessary
- if not moveForwardSafely() then
- print("Failed to move forward in row " .. w .. " at position X=" .. posX .. ", Z=" .. posZ)
- return false
- end
- -- Update position
- if turnRight then
- posX = posX + 1
- else
- posX = posX - 1
- end
- -- Place block below
- if not safelyPlaceBlockDown() then
- print("Failed to place block at position X=" .. posX .. ", Z=" .. posZ)
- return false
- end
- end
- -- Don't turn or move forward if this is the last row
- if w < width then
- -- Turn for the next row
- if turnRight then
- turtle.turnRight()
- if not moveForwardSafely() then
- print("Failed to move forward when turning for next row")
- return false
- end
- posZ = posZ + 1
- turtle.turnRight()
- else
- turtle.turnLeft()
- if not moveForwardSafely() then
- print("Failed to move forward when turning for next row")
- return false
- end
- posZ = posZ + 1
- turtle.turnLeft()
- end
- turnRight = not turnRight
- end
- end
- end
- -- Return to starting position
- print("Returning to starting position. Current position: X=" .. posX .. ", Z=" .. posZ)
- -- First, orient correctly based on final row direction
- if (width % 2) == 0 then
- -- If width is even, we ended facing the opposite direction
- turtle.turnRight()
- turtle.turnRight()
- end
- -- Move back to X=0 (start of row)
- for i = 1, math.abs(posX) do
- if not moveForwardSafely() then
- print("Couldn't return to starting position (X)")
- return false
- end
- end
- -- Turn to face the Z direction (back toward origin)
- if posX > 0 then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- -- Move back to Z=0 (first row)
- for i = 1, posZ do
- if not moveForwardSafely() then
- print("Couldn't return to starting position (Z)")
- return false
- end
- end
- -- Turn to face the original direction (positive X)
- turtle.turnRight()
- print("Successfully returned to starting position")
- return true
- end
- -- Main function to build all layers
- function buildLayers(length, width, depth)
- -- Calculate required fuel (more accurate approximation)
- local movesPerLayer = (length * width) + (width * 2) + length + depth
- local totalMoves = movesPerLayer * depth + (depth * 2) + 2
- local requiredFuel = totalMoves + 10 -- Adding a safety margin
- print("Estimated fuel required: " .. requiredFuel)
- -- Check if we have enough fuel
- if not checkFuel(requiredFuel) then
- return false
- end
- -- Get initial blocks
- if not getBlocks() then
- print("No blocks in chest! Please add blocks and press Enter to continue.")
- read()
- if not getBlocks() then
- print("Still no blocks available. Exiting.")
- return false
- end
- end
- -- Move forward one block before starting
- print("Moving forward one block to start building")
- if not moveForwardSafely() then
- print("Couldn't move forward to start building")
- return false
- end
- -- Move to starting position (one block less to avoid being too deep)
- print("Going down to depth " .. depth)
- if not moveToStartPosition(depth) then
- return false
- end
- -- Reset orientation for consistent coordinate tracking
- local facing = 0 -- 0 = positive X, 1 = positive Z, 2 = negative X, 3 = negative Z
- -- Place layers from bottom to top
- for layer = 1, depth do
- print("Building layer " .. layer .. " of " .. depth)
- -- Place block under the starting position is now handled in placeLayerOfBlocks
- if not placeLayerOfBlocks(length, width) then
- print("Failed to complete layer " .. layer)
- return false
- end
- -- Move up one level if not the final layer
- if layer < depth then
- print("Moving up one layer")
- if not turtle.up() then
- print("Couldn't move up to the next layer")
- return false
- end
- end
- -- Get more blocks if needed for the next layer
- if layer < depth and not ensureBlockSelected() then
- print("Unable to get more blocks for the next layer")
- return false
- end
- end
- -- Return to the surface - we need to go up exactly the right number of blocks
- print("Returning to the surface")
- -- We went down (depth-1) blocks and then up (depth-1) blocks during layering
- -- So we should be at the initial depth already, no need for extra ascent
- -- But let's check in case we need to go up
- local current_depth = 0
- while true do
- if turtle.detectUp() then
- -- There's a block above, try to dig it
- turtle.digUp()
- if not turtle.up() then
- print("Warning: Couldn't return to the surface, path blocked")
- break
- end
- current_depth = current_depth + 1
- elseif not turtle.up() then
- -- We've hit the surface (likely water)
- break
- else
- current_depth = current_depth + 1
- end
- -- Safety check to prevent infinite loop
- if current_depth >= 10 then
- print("Warning: Ascent limit reached, stopping")
- break
- end
- end
- -- Return to original position (behind the chest)
- print("Moving back to original position")
- if not turtle.back() then
- print("Warning: Couldn't move back to original position")
- -- Try digging if necessary
- turtle.turnRight()
- turtle.turnRight()
- if turtle.detect() then
- turtle.dig()
- if not turtle.forward() then
- print("Failed to return to original position")
- return true -- Still consider the build successful
- end
- else
- print("Failed to return to original position")
- return true -- Still consider the build successful
- end
- -- Turn back to original orientation
- turtle.turnRight()
- turtle.turnRight()
- end
- print("Successfully built all layers!")
- return true
- end
- -- Parse command line arguments
- local args = {...}
- if #args < 3 then
- print("Usage: TurtleMakeUsableRoom <length> <width> <depth>")
- return
- end
- local length = tonumber(args[1])
- local width = tonumber(args[2])
- local depth = tonumber(args[3])
- if not length or not width or not depth then
- print("All arguments must be numbers!")
- return
- end
- if length < 1 or width < 1 or depth < 1 then
- print("All dimensions must be positive numbers!")
- return
- end
- print("Building an area of " .. length .. "x" .. width .. " blocks to a depth of " .. depth)
- local success = buildLayers(length, width, depth)
- if success then
- print("Build completed successfully!")
- else
- print("Build failed. Please check the errors above.")
- end
Add Comment
Please, Sign In to add comment