Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Desc: Attempts to flatten a square area defined by the user
- -- By: hornedcommando
- print("y set to 0")
- y = 0
- local function input()
- while true do
- write("How big me dig?\n")
- size = tonumber(read())
- break
- end
- end
- --TODO: it's hard coded to use dirt, should update to use a table of blocks
- local blocks = {
- {name = "minecraft:dirt"},
- {name = "minecraft:cobblestone"}
- }
- -- Function to smartly refuel the turtle
- local function smartRefuel()
- -- Check if fuel level is below 200
- while turtle.getFuelLevel() < 200 do
- for slot = 1, 16 do
- turtle.select(slot) -- Select the slot
- if turtle.refuel(0) then -- Check if the selected item is a fuel
- turtle.refuel(1) -- Refuel with the selected item
- break -- Stop searching for fuel after refueling
- end
- end
- end
- end
- local function searchInventory(name)
- for slot = 1, 16 do
- turtle.select(slot)
- local slotDetail = turtle.getItemDetail()
- if slotDetail and slotDetail.name:find(name) then
- return slot -- Return the slot number if item found
- end
- end
- return nil -- Return nil if item not found
- end
- -- Function to make the turtle go up
- function gu()
- print("Going up")
- while not turtle.up() do
- print("Digging up")
- turtle.digUp()
- end
- y = y + 1
- print("y is now " .. y)
- end
- local function fill(name)
- local found, slot = searchInventory(name)
- if found then
- turtle.placeDown() -- Place the block
- print("Placed block:", name)
- else
- print("Block not found:", name)
- end
- end
- -- Function to make the turtle go down
- function gd()
- print("Going down")
- while not turtle.down() do
- print("Digging down")
- turtle.digDown()
- if not turtle.inspectDown() and y == 0 then
- fill("minecraft:dirt")
- end
- end
- y = y - 1
- print("y is now " .. y)
- end
- -- Function to make the turtle go forward
- function gf()
- print("Checking if a block is in front of me")
- if not turtle.inspectDown() then
- fill("minecraft:dirt")
- end
- while turtle.inspect() or turtle.inspectUp() do
- gu()
- end
- print("Moving forward")
- turtle.forward()
- while y > 0 do
- gd()
- end
- if not turtle.inspect() then
- fill("minecraft:dirt")
- end
- smartRefuel()
- end
- -- Function to make the turtle go back along the defined area
- function lTurn()
- turtle.turnLeft() -- Turn left
- gf() -- Move forward
- turtle.turnLeft() -- Turn left
- end
- -- Function to make the turtle go back along the defined area (but to the right)
- function rTurn()
- turtle.turnRight() -- Turn right
- gf() -- Move forward
- turtle.turnRight() -- Turn right
- end
- -- Function to make the turtle travel the length of the defined area
- function fEnd(size)
- for i = 1, size do
- gf() -- Move forward
- end
- end
- -- Combines movement functions to form a sweeping square pattern
- local function sweep(size)
- for i = 1, size do
- fEnd(size - 1) -- Move to the end of the row
- if i < size then
- if i % 2 == 1 then
- rTurn() -- Turn right at the end of the row
- else
- lTurn() -- Turn left at the end of the row
- end
- end
- end
- end
- smartRefuel()
- input()
- sweep(size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement