Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- if #args < 1 then
- print("Required parameter: <size>")
- return
- elseif tonumber(args[1]) == nil or tonumber(args[1]) % 2 == 1 then
- print("Size must be an even number")
- return
- end
- local UP = 1
- local LEFT = 2
- local DOWN = 3
- local RIGHT = 4
- local DIRECTIONS = {
- [UP] = {x = 1, y = 0}, -- up
- [LEFT] = {x = 0, y = 1}, -- left
- [DOWN] = {x = -1, y = 0}, -- down
- [RIGHT] = {x = 0, y = -1}, -- right
- }
- local currentDirection = UP
- local currentPosition = {x = 0, y = 0}
- local currentLevel = 0
- local SQUARE_SIZE = tonumber(args[1]) -- has to be even
- local garbageItems = {'minecraft:cobblestone', 'minecraft:gravel', 'minecraft:dirt'}
- -- Lua helpers
- table.contains = function(array, value)
- for _, targetColumn in pairs(array) do
- if targetColumn == value then
- return true
- end
- end
- return false
- end
- -- End of Lua helpers
- local function discardGarbageItems()
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if data and data.name and table.contains(garbageItems, data.name) then
- turtle.select(i)
- turtle.drop()
- end
- end
- turtle.select(1)
- end
- local function mineAndMoveForward()
- turtle.dig()
- success = turtle.forward()
- if success then
- if currentLevel ~= 0 then
- turtle.digUp()
- end
- turtle.digDown()
- currentPosition.x = currentPosition.x + DIRECTIONS[currentDirection].x
- currentPosition.y = currentPosition.y + DIRECTIONS[currentDirection].y
- return true
- end
- return false
- end
- local function turnLeft()
- turtle.turnLeft()
- currentDirection = currentDirection % 4 + 1
- end
- local function turnRight()
- turtle.turnRight()
- currentDirection = (currentDirection - 2) % 4 + 1
- end
- local function digCurrentLevel()
- local success = true
- while success do
- if currentDirection == RIGHT and currentPosition.x == 0 and currentPosition.y == 0 then
- turnLeft()
- break
- elseif currentDirection == UP and currentPosition.x == SQUARE_SIZE - 1 then
- turnLeft()
- success = mineAndMoveForward()
- turnLeft()
- elseif currentDirection == DOWN and currentPosition.x == 1 then
- if currentPosition.y == SQUARE_SIZE - 1 then
- success = mineAndMoveForward()
- turnLeft()
- else
- turnRight()
- success = mineAndMoveForward()
- turnRight()
- end
- else
- success = mineAndMoveForward()
- end
- end
- return success
- end
- local function goDownLevel()
- turtle.digDown()
- turtle.down()
- turtle.digDown()
- turtle.down()
- turtle.digDown()
- turtle.down()
- currentLevel = currentLevel + 1
- end
- local function goUpLevel()
- turtle.digUp()
- turtle.up()
- turtle.digUp()
- turtle.up()
- turtle.digUp()
- turtle.up()
- currentLevel = currentLevel - 1
- end
- local function goBackToStartPosition()
- if currentLevel == 0 then
- return
- end
- while currentLevel > 0 do
- goUpLevel()
- end
- while currentDirection ~= DOWN do
- turnLeft()
- end
- success = true
- while currentPosition.x ~= 0 and success do
- success = mineAndMoveForward()
- end
- if success then
- while currentDirection ~= RIGHT do
- turnLeft()
- end
- while currentPosition.y ~= 0 and success do
- success = mineAndMoveForward()
- end
- end
- return success
- end
- local success = true
- while success do
- success = digCurrentLevel()
- if success then
- discardGarbageItems()
- goDownLevel()
- end
- end
- goBackToStartPosition()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement