Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function turn(direction, count)
- if direction ~= "left" and direction ~= "right" then
- error("Invalid direction: " .. direction)
- end
- local count = count or 1
- local success, msg = nil, nil
- for i = 1, count do
- if direction == "left" then
- success, msg = turtle.turnLeft()
- else
- success, msg = turtle.turnRight()
- end
- if not success then
- error(msg)
- end
- end
- end
- local function checkFull()
- for i = 1, 16 do
- if turtle.getItemSpace(i) > 0 then
- return false
- end
- end
- return true
- end
- local function dig(direction, returnFacing)
- while checkFull() do
- print("Inventory full...")
- local event, key, is_held = os.pullEvent("key")
- end
- if returnFacing == nil then
- returnFacing = true
- end
- local success, msg = nil, nil
- if direction == "up" then
- if turtle.detectUp() then
- success, msg = turtle.digUp()
- end
- elseif direction == "down" then
- if turtle.detectDown() then
- success, msg = turtle.digDown()
- end
- elseif direction == "left" then
- turtle.turnLeft()
- if turtle.detect() then
- success, msg = turtle.dig()
- end
- if returnFacing then
- turtle.turnRight()
- end
- elseif direction == "right" then
- turtle.turnRight()
- if turtle.detect() then
- success, msg = turtle.dig()
- end
- if returnFacing then
- turtle.turnLeft()
- end
- elseif turtle.detect() then
- success, msg = turtle.dig()
- end
- if not success and not msg == "Nothing to dig here" then
- error(msg)
- end
- end
- local function go(direction)
- local success, msg = nil, nil
- if direction == "up" then
- dig("up")
- success, msg = turtle.up()
- elseif direction == "down" then
- dig("down")
- success, msg = turtle.down()
- elseif direction == "forward" then
- dig()
- success, msg = turtle.forward()
- elseif direction == "back" then
- turn("right", 2)
- dig()
- success, msg = turtle.forward()
- turn("right", 2)
- elseif direction == "left" then
- turn("left")
- dig()
- success, msg = turtle.forward()
- turn("right")
- elseif direction == "right" then
- turn("right")
- dig()
- success, msg = turtle.forward()
- turn("left")
- else
- error("Invalid direction: " .. direction)
- end
- if not success then
- error(msg)
- end
- end
- return {
- turn = turn,
- dig = dig,
- go = go
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement