Advertisement
jille_Jr

Untitled

Jun 19th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1.  
  2. local tArgs = {...}
  3.  
  4. local LEFT = 0
  5. local FORWARD = 1
  6. local RIGHT = 2
  7.  
  8. local nCurrentDir = FORWARD
  9.  
  10. local nCurrentX = 0
  11. local nCurrentY = 0
  12.  
  13. --(( Functions ))--
  14.  
  15. local function look(nDir)
  16.     while nDir < nCurrentDir do
  17.         turtle.turnLeft()
  18.         nCurrentDir = nCurrentDir - 1
  19.     end
  20.     while nDir > nCurrentDir do
  21.         turtle.turnRight()
  22.         nCurrentDir = nCurrentDir + 1
  23.     end
  24. end
  25.  
  26. local function _go(move, suffix)
  27.     while not turtle[move]() do
  28.         if not turtle["dig"..suffix]() then
  29.             if turtle["detect"..suffix]() then
  30.                 error("Bedrock while trying to move " .. move, 0)
  31.             end
  32.             turtle["attack"..suffix]()
  33.         end
  34.     end
  35. end
  36.  
  37. local function goForward()
  38.     _go("forward", "")
  39.     if nCurrentDir == RIGHT then
  40.         nCurrentX = nCurrentX + 1
  41.     elseif nCurrentDir == LEFT then
  42.         nCurrentX = nCurrentX - 1
  43.     end
  44. end
  45.  
  46. local function goUp()
  47.     _go("up", "Up")
  48.     nCurrentY = nCurrentY + 1
  49. end
  50.  
  51. local function goDown()
  52.     _go("down", "Down")
  53.     nCurrentY = nCurrentY - 1
  54. end
  55.  
  56. local function _digTilTop(nHeight)
  57.     -- dig til top-1
  58.     while nCurrentY < nHeight - 2 do
  59.         goUp()
  60.     end
  61.     if nCurrentY <= nHeight - 2 then
  62.         repeat until not turtle.digUp()
  63.     end
  64. end
  65.  
  66. local function _digTilBottom(nHeight)
  67.     -- dig til bottom+1
  68.     while nCurrentY > 1 do
  69.         goDown()
  70.     end
  71.     if nCurrentY >= 1 then
  72.         repeat until not turtle.digDown()
  73.     end
  74. end
  75.  
  76. local function digColumn(nHeight)
  77.     if nCurrentY < nHeight / 2 then
  78.         -- bot to top
  79.         _digTilBottom(nHeight)
  80.         _digTilTop(nHeight)
  81.     else
  82.         -- top to bot
  83.         _digTilTop(nHeight)
  84.         _digTilBottom(nHeight)
  85.     end
  86. end
  87.  
  88. local function digFrame(nHeight, nWidth)
  89.     digColumn(nHeight)
  90.  
  91.     if nWidth == 1 then
  92.         return
  93.     end
  94.  
  95.     if nCurrentX > nWidth / 2 then
  96.         look(LEFT)
  97.     else
  98.         look(RIGHT)
  99.     end
  100.  
  101.     for i = 1,nWidth-1 do
  102.         goForward()
  103.         digColumn(nHeight)
  104.     end
  105. end
  106.  
  107. local function tunnel(nLength, nHeight, nWidth)
  108.     for i=1,nLength do
  109.         look(FORWARD)
  110.         goForward()
  111.         digFrame(nHeight, nWidth)
  112.     end
  113.  
  114.     if nCurrentX > 0 then
  115.         look(LEFT)
  116.         repeat
  117.             goForward()
  118.         until nCurrentX <= 0
  119.     end
  120.  
  121.     look(FORWARD)
  122.     while nCurrentY > 0 do
  123.         goDown()
  124.     end
  125. end
  126.  
  127. --(( Error checking ))--
  128.  
  129. local nArgLength = tonumber(tArgs[1])
  130. local nArgHeight = tonumber(tArgs[2] or 2)
  131. local nArgWidth = tonumber(tArgs[3] or 1)
  132.  
  133. local function usage(sMsg)
  134.     sMsg = sMsg and (sMsg .. "\n") or ""
  135.     error(sMsg .. "Usage: tunnel <length> [height=2] [width=1]", 0)
  136. end
  137.  
  138. if #tArgs==0 then usage("Missing length.") end
  139. if not nArgLength then usage("Invalid length, expected number.") end
  140. if not nArgHeight then usage("Invalid height, expected number.") end
  141. if not nArgWidth then usage("Invalid width, expected number.") end
  142.  
  143. if nArgLength <= 0 then usage("Invalid length, negative not supported.") end
  144. if nArgHeight <= 0 then usage("Invalid height, negative not supported.") end
  145. if nArgWidth <= 0 then usage("Invalid width, negative not supported.") end
  146.  
  147. if nArgLength % 1 ~= 0 then usage("Invalid length, fractions not supported.") end
  148. if nArgHeight % 1 ~= 0 then usage("Invalid height, fractions not supported.") end
  149. if nArgWidth % 1 ~= 0 then usage("Invalid width, fractions not supported.") end
  150.  
  151. --(( Main program ))--
  152.  
  153. tunnel(nArgLength, nArgHeight, nArgWidth)
  154.  
  155. --(( EOF ))---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement