Advertisement
Mkalo

Untitled

Jan 17th, 2022
748
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local args = {...}
  2. if #args < 1 then
  3.   print("Required parameter: <size>")
  4.   return
  5. elseif tonumber(args[1]) == nil then
  6.   print("Size must be a number")
  7.   return
  8. end
  9.  
  10. local UP = 1
  11. local LEFT = 2
  12. local DOWN = 3
  13. local RIGHT = 4
  14.  
  15. local DIRECTIONS = {
  16.   [UP] = {x = 1, y = 0}, -- up
  17.   [LEFT] = {x = 0, y = 1}, -- left
  18.   [DOWN] = {x = -1, y = 0}, -- down
  19.   [RIGHT] = {x = 0, y = -1}, -- right
  20. }
  21. local currentDirection = UP
  22. local currentPosition = {x = 0, y = 0}
  23. local TUNNEL_SIZE = tonumber(args[1])
  24. local lastMiningDirection = LEFT
  25.  
  26. local function mineAndMoveForward()
  27.   turtle.dig()
  28.   success = turtle.forward()
  29.   if success then
  30.     if currentLevel ~= 0 then
  31.       turtle.digUp()
  32.     end
  33.     turtle.digDown()
  34.     currentPosition.x = currentPosition.x + DIRECTIONS[currentDirection].x
  35.     currentPosition.y = currentPosition.y + DIRECTIONS[currentDirection].y
  36.     return true
  37.   end
  38.   return false
  39. end
  40.  
  41. local function turnLeft()
  42.   turtle.turnLeft()
  43.   currentDirection = currentDirection % 4 + 1
  44. end
  45.  
  46. local function turnRight()
  47.   turtle.turnRight()
  48.   currentDirection = (currentDirection - 2) % 4 + 1
  49. end
  50.  
  51. while turtle.getFuelLevel() > 0 do
  52.   if currentDirection == UP then
  53.     mineAndMoveForward()
  54.     if lastMiningDirection == LEFT then
  55.       turnRight()
  56.     else
  57.       turnLeft()
  58.     end
  59.     for i = 1, TUNNEL_SIZE do
  60.       mineAndMoveForward()
  61.     end
  62.     if lastMiningDirection == LEFT then
  63.       turnLeft()
  64.       lastMiningDirection = RIGHT
  65.     else
  66.       turnRight()
  67.       lastMiningDirection = LEFT
  68.     end
  69.     local coalCount = turtle.getItemCount()
  70.     if coalCount > 1 then
  71.       turtle.refuel(coalCount - 1)
  72.     end
  73.   end
  74. end
  75.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement