jille_Jr icon

Untitled

jille_Jr | PRO | 06/19/18 07:40:15 PM UTC | 0 ⭐ | 381 👁️ | Never ⏰ | []
Lua |

3.53 KB

|

None

|

0 👍

/

0 👎

 
local tArgs = {...}
 
local LEFT = 0
local FORWARD = 1
local RIGHT = 2
 
local nCurrentDir = FORWARD
 
local nCurrentX = 0
local nCurrentY = 0
 
--(( Functions ))--
 
local function look(nDir)
    while nDir < nCurrentDir do
        turtle.turnLeft()
        nCurrentDir = nCurrentDir - 1
    end
    while nDir > nCurrentDir do
        turtle.turnRight()
        nCurrentDir = nCurrentDir + 1
    end
end
 
local function _go(move, suffix)
    while not turtle[move]() do
        if not turtle["dig"..suffix]() then
            if turtle["detect"..suffix]() then
                error("Bedrock while trying to move " .. move, 0)
            end
            turtle["attack"..suffix]()
        end
    end
end
 
local function goForward()
    _go("forward", "")
    if nCurrentDir == RIGHT then
        nCurrentX = nCurrentX + 1
    elseif nCurrentDir == LEFT then
        nCurrentX = nCurrentX - 1
    end
end
 
local function goUp()
    _go("up", "Up")
    nCurrentY = nCurrentY + 1
end
 
local function goDown()
    _go("down", "Down")
    nCurrentY = nCurrentY - 1
end
 
local function _digTilTop(nHeight)
    -- dig til top-1
    while nCurrentY < nHeight - 2 do
        goUp()
    end
    if nCurrentY <= nHeight - 2 then
        repeat until not turtle.digUp()
    end
end
 
local function _digTilBottom(nHeight)
    -- dig til bottom+1
    while nCurrentY > 1 do
        goDown()
    end
    if nCurrentY >= 1 then
        repeat until not turtle.digDown()
    end
end
 
local function digColumn(nHeight)
    if nCurrentY < nHeight / 2 then
        -- bot to top
        _digTilBottom(nHeight)
        _digTilTop(nHeight)
    else
        -- top to bot
        _digTilTop(nHeight)
        _digTilBottom(nHeight)
    end
end
 
local function digFrame(nHeight, nWidth)
    digColumn(nHeight)
 
    if nWidth == 1 then
        return
    end
 
    if nCurrentX > nWidth / 2 then
        look(LEFT)
    else
        look(RIGHT)
    end
 
    for i = 1,nWidth-1 do
        goForward()
        digColumn(nHeight)
    end
end
 
local function tunnel(nLength, nHeight, nWidth)
    for i=1,nLength do
        look(FORWARD)
        goForward()
        digFrame(nHeight, nWidth)
    end
 
    if nCurrentX > 0 then
        look(LEFT)
        repeat
            goForward()
        until nCurrentX <= 0
    end
 
    look(FORWARD)
    while nCurrentY > 0 do
        goDown()
    end
end
 
--(( Error checking ))--
 
local nArgLength = tonumber(tArgs[1])
local nArgHeight = tonumber(tArgs[2] or 2)
local nArgWidth = tonumber(tArgs[3] or 1)
 
local function usage(sMsg)
    sMsg = sMsg and (sMsg .. "\n") or ""
    error(sMsg .. "Usage: tunnel <length> [height=2] [width=1]", 0)
end
 
if #tArgs==0 then usage("Missing length.") end
if not nArgLength then usage("Invalid length, expected number.") end
if not nArgHeight then usage("Invalid height, expected number.") end
if not nArgWidth then usage("Invalid width, expected number.") end
 
if nArgLength <= 0 then usage("Invalid length, negative not supported.") end
if nArgHeight <= 0 then usage("Invalid height, negative not supported.") end
if nArgWidth <= 0 then usage("Invalid width, negative not supported.") end
 
if nArgLength % 1 ~= 0 then usage("Invalid length, fractions not supported.") end
if nArgHeight % 1 ~= 0 then usage("Invalid height, fractions not supported.") end
if nArgWidth % 1 ~= 0 then usage("Invalid width, fractions not supported.") end
 
--(( Main program ))--
 
tunnel(nArgLength, nArgHeight, nArgWidth)
 
--(( EOF ))---

Comments