Advertisement
Mkalo

Untitled

Jan 17th, 2022
671
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 or tonumber(args[1]) % 2 == 1 then
  6.   print("Size must be an even 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 currentLevel = 0
  24. local SQUARE_SIZE = tonumber(args[1]) -- has to be even
  25.  
  26. local garbageItems = {'minecraft:cobblestone', 'minecraft:gravel', 'minecraft:dirt'}
  27.  
  28. -- Lua helpers
  29.  
  30. table.contains = function(array, value)
  31.     for _, targetColumn in pairs(array) do
  32.         if targetColumn == value then
  33.             return true
  34.         end
  35.     end
  36.     return false
  37. end
  38.  
  39. -- End of Lua helpers
  40.  
  41. local function discardGarbageItems()
  42.   for i = 1, 16 do
  43.     local data = turtle.getItemDetail(i)
  44.     if data and data.name and table.contains(garbageItems, data.name) then
  45.       turtle.select(i)
  46.       turtle.drop()
  47.     end
  48.   end
  49.   turtle.select(1)
  50. end
  51.  
  52. local function mineAndMoveForward()
  53.   turtle.dig()
  54.   success = turtle.forward()
  55.   if success then
  56.     if currentLevel ~= 0 then
  57.       turtle.digUp()
  58.     end
  59.     turtle.digDown()
  60.     currentPosition.x = currentPosition.x + DIRECTIONS[currentDirection].x
  61.     currentPosition.y = currentPosition.y + DIRECTIONS[currentDirection].y
  62.     return true
  63.   end
  64.   return false
  65. end
  66.  
  67. local function turnLeft()
  68.   turtle.turnLeft()
  69.   currentDirection = currentDirection % 4 + 1
  70. end
  71.  
  72. local function turnRight()
  73.   turtle.turnRight()
  74.   currentDirection = (currentDirection - 2) % 4 + 1
  75. end
  76.  
  77.  
  78. local function digCurrentLevel()
  79.   local success = true
  80.   while success do
  81.     if currentDirection == RIGHT and currentPosition.x == 0 and currentPosition.y == 0 then
  82.       turnLeft()
  83.       break
  84.     elseif currentDirection == UP and currentPosition.x == SQUARE_SIZE - 1 then
  85.       turnLeft()
  86.       success = mineAndMoveForward()
  87.       turnLeft()
  88.     elseif currentDirection == DOWN and currentPosition.x == 1 then
  89.       if currentPosition.y == SQUARE_SIZE - 1 then
  90.         success = mineAndMoveForward()
  91.         turnLeft()
  92.       else
  93.         turnRight()
  94.         success = mineAndMoveForward()
  95.         turnRight()
  96.       end
  97.     else
  98.       success = mineAndMoveForward()
  99.     end
  100.   end
  101.   return success
  102. end
  103.  
  104. local function goDownLevel()
  105.   turtle.digDown()
  106.   turtle.down()
  107.   turtle.digDown()
  108.   turtle.down()
  109.   turtle.digDown()
  110.   turtle.down()
  111.   currentLevel = currentLevel + 1
  112. end
  113.  
  114. local function goUpLevel()
  115.   turtle.digUp()
  116.   turtle.up()
  117.   turtle.digUp()
  118.   turtle.up()
  119.   turtle.digUp()
  120.   turtle.up()
  121.   currentLevel = currentLevel - 1
  122. end
  123.  
  124. local function goBackToStartPosition()
  125.   if currentLevel == 0 then
  126.     return
  127.   end
  128.  
  129.   while currentLevel > 0 do
  130.     goUpLevel()
  131.   end
  132.  
  133.   while currentDirection ~= DOWN do
  134.     turnLeft()
  135.   end
  136.   success = true
  137.   while currentPosition.x ~= 0 and success do
  138.     success = mineAndMoveForward()
  139.   end
  140.  
  141.   if success then
  142.     while currentDirection ~= RIGHT do
  143.       turnLeft()
  144.     end
  145.     while currentPosition.y ~= 0 and success do
  146.       success = mineAndMoveForward()
  147.     end
  148.   end
  149.  
  150.   return success
  151. end
  152.  
  153. local success = true
  154. while success do
  155.   success = digCurrentLevel()
  156.   if success then
  157.     discardGarbageItems()
  158.     goDownLevel()
  159.   end
  160. end
  161.  
  162. goBackToStartPosition()
  163.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement