Advertisement
jille_Jr

CC: Tunnel program 2.0

Jun 19th, 2018 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.85 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. local LEFT = 0
  4. local FORWARD = 1
  5. local RIGHT = 2
  6.  
  7. local nCurrentDir = FORWARD
  8.  
  9. local nCurrentX = 0
  10. local nCurrentY = 0
  11.  
  12. --(( Functions ))--
  13.  
  14. local function look(nDir)
  15.   while nDir < nCurrentDir do
  16.     turtle.turnLeft()
  17.     nCurrentDir = nCurrentDir - 1
  18.   end
  19.   while nDir > nCurrentDir do
  20.     turtle.turnRight()
  21.     nCurrentDir = nCurrentDir + 1
  22.   end
  23. end
  24.  
  25. local function _go(move, suffix)
  26.   while not turtle[move]() do
  27.     if not turtle["dig"..suffix]() then
  28.       if turtle["detect"..suffix]() then
  29.         error("Bedrock while trying to move " .. move, 0)
  30.       end
  31.       turtle["attack"..suffix]()
  32.     end
  33.   end
  34. end
  35.  
  36. local function goForward()
  37.   _go("forward", "")
  38.   if nCurrentDir == RIGHT then
  39.     nCurrentX = nCurrentX + 1
  40.   elseif nCurrentDir == LEFT then
  41.     nCurrentX = nCurrentX - 1
  42.   end
  43. end
  44.  
  45. local function goUp()
  46.   _go("up", "Up")
  47.   nCurrentY = nCurrentY + 1
  48. end
  49.  
  50. local function goDown()
  51.   _go("down", "Down")
  52.   nCurrentY = nCurrentY - 1
  53. end
  54.  
  55. local function goToY(y)
  56.   if nCurrentY < y then
  57.     -- +1 because for loops are inclusive
  58.     for _ = nCurrentY + 1, y, 1 do
  59.       goUp()
  60.     end
  61.   else
  62.     -- -1 because for loops are inclusive
  63.     for _ = nCurrentY - 1, y, -1 do
  64.       goDown()
  65.     end
  66.   end
  67. end
  68.  
  69. ---If current `Y` position is higher than the given `Y` coordinate
  70. ---then move to said `Y` coordinate.
  71. local function goToYIfHigher(y)
  72.   if nCurrentY > y then
  73.     goToY(y)
  74.   end
  75. end
  76.  
  77. ---If current `Y` position is lower than the given `Y` coordinate
  78. ---then move to said `Y` coordinate.
  79. local function goToYIfLower(y)
  80.   if nCurrentY < y then
  81.     goToY(y)
  82.   end
  83. end
  84.  
  85. local function goToX(x)
  86.   if nCurrentX < x then
  87.     look(RIGHT)
  88.     -- +1 because for loops are inclusive
  89.     for _ = nCurrentX + 1, x do
  90.       goForward()
  91.     end
  92.   elseif nCurrentX > x then
  93.     look(LEFT)
  94.     -- -1 because for loops are inclusive
  95.     for _ = nCurrentX - 1, x, -1 do
  96.       goForward()
  97.     end
  98.   end
  99. end
  100.  
  101. local function _digTilTop(nHeight)
  102.   -- dig til top-1
  103.   while nCurrentY < nHeight - 2 do
  104.     goUp()
  105.   end
  106.   if nCurrentY <= nHeight - 2 then
  107.     repeat until not turtle.digUp()
  108.   end
  109. end
  110.  
  111. local function _digTilBottom()
  112.   -- dig til bottom+1
  113.   while nCurrentY > 1 do
  114.     goDown()
  115.   end
  116.   if nCurrentY >= 1 then
  117.     repeat until not turtle.digDown()
  118.   end
  119. end
  120.  
  121. local function digColumn(nHeight)
  122.   if nCurrentY < nHeight / 2 then
  123.     -- bot to top
  124.     _digTilBottom()
  125.     _digTilTop(nHeight)
  126.   else
  127.     -- top to bot
  128.     _digTilTop(nHeight)
  129.     _digTilBottom()
  130.   end
  131. end
  132.  
  133. local function digFrame(nHeight, nWidth)
  134.   digColumn(nHeight)
  135.  
  136.   if nWidth == 1 or nWidth == -1 then
  137.     return
  138.   end
  139.  
  140.   if nCurrentX > nWidth / 2 then
  141.     look(LEFT)
  142.   else
  143.     look(RIGHT)
  144.   end
  145.  
  146.   for _ = 1, math.abs(nWidth) - 1 do
  147.     goForward()
  148.     digColumn(nHeight)
  149.   end
  150. end
  151.  
  152. local function adjustForYDiff(nHeight, nYDiff)
  153.   if nYDiff == 0 then
  154.     return
  155.   end
  156.  
  157.   if nYDiff > 0 then
  158.     local lowestY = nYDiff
  159.     goToYIfLower(lowestY)
  160.   else
  161.     local highestY = nHeight + nYDiff - 1
  162.     goToYIfHigher(highestY)
  163.   end
  164.  
  165.   nCurrentY = nCurrentY - nYDiff
  166. end
  167.  
  168. local function goHome()
  169.   goToX(0)
  170.   look(FORWARD)
  171.   goToY(0)
  172. end
  173.  
  174. local function tunnel(nLength, nHeight, nWidth, nYDiff)
  175.   for _ = 1, nLength do
  176.     look(FORWARD)
  177.     goForward()
  178.     adjustForYDiff(nHeight, nYDiff)
  179.     digFrame(nHeight, nWidth)
  180.   end
  181.  
  182.   goHome()
  183. end
  184.  
  185. --(( Error checking ))--
  186.  
  187. local nArgLength = tonumber(tArgs[1])
  188. local nArgHeight = tonumber(tArgs[2] or 2)
  189. local nArgWidth = tonumber(tArgs[3] or 1)
  190. local nArgYDiff = tonumber(tArgs[4] or 0)
  191.  
  192. local function usage(sMsg)
  193.   sMsg = sMsg and (sMsg .. "\n") or ""
  194.   error(sMsg .. "Usage: tunnel <length> [height=2] [width=1] [y-diff=0]", 0)
  195. end
  196.  
  197. if #tArgs==0 then usage("Missing length.") end
  198. if not nArgLength then usage("Invalid length, expected number.") end
  199. if not nArgHeight then usage("Invalid height, expected number.") end
  200. if not nArgWidth then usage("Invalid width, expected number.") end
  201. if not nArgYDiff then usage("Invalid y-diff, expected number.") end
  202.  
  203. if nArgLength <= 0 then usage("Invalid length, negative not supported.") end
  204. if nArgHeight <= 0 then usage("Invalid height, negative not supported.") end
  205. if nArgWidth == 0 then usage("Invalid width, zero not supported.") end
  206. if math.abs(nArgYDiff) > nArgHeight - 1 then usage("Invalid y-diff, cannot be longer than height - 1.") end
  207.  
  208. if nArgLength % 1 ~= 0 then usage("Invalid length, fractions not supported.") end
  209. if nArgHeight % 1 ~= 0 then usage("Invalid height, fractions not supported.") end
  210. if nArgWidth % 1 ~= 0 then usage("Invalid width, fractions not supported.") end
  211. if nArgYDiff % 1 ~= 0 then usage("Invalid y-diff, fractions not supported.") end
  212.  
  213. --(( Main program ))--
  214.  
  215. tunnel(nArgLength, nArgHeight, nArgWidth, nArgYDiff)
  216.  
  217. --(( EOF ))---
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement