namagaii

Dig Out

Dec 28th, 2020 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.60 KB | None | 0 0
  1. dofile("Fuel")
  2. dofile("Inventory")
  3. DigOut = {}
  4. local hasSub = false
  5. local fuel = Fuel:new(fuel)
  6. local inventory = Inventory:new(inventory)
  7. local default = "none"
  8. local blacklist
  9. local whiteList
  10. local blackListActive = false
  11. local whiteListActive = false
  12. function DigOut:new(o)
  13.     o = o or {}
  14.     setmetatable(o, self)
  15.     self.__index = self
  16.     return o
  17. end
  18.  
  19. function DigOut.ExcavateDownBlackList(x, y, z, blockList)
  20.     blacklist = blockList
  21.     blacklist = loadTableFromFile(blacklist)
  22.     blackListActive = true
  23.     if x % 2 == 0 then -- Even
  24.         EvenYCycle(x, y, z)
  25.     else -- Odd
  26.         OddYCycle(x, y, z)
  27.     end
  28. end
  29.  
  30. function DigOut.ExcavateDownWhiteList(x, y, z, blockList)
  31.     whiteList = blockList
  32.     whiteList = loadTableFromFile(whiteList)
  33.     whiteListActive = true
  34.     if x % 2 == 0 then -- Even
  35.         EvenYCycle(x, y, z)
  36.     else -- Odd
  37.         OddYCycle(x, y, z)
  38.     end
  39. end
  40.  
  41. function DigOut.ExcavateDownDefault(x, y, z)
  42.     if x % 2 == 0 then -- Even
  43.         EvenYCycle(x, y, z)
  44.     else -- Odd
  45.         OddYCycle(x, y, z)
  46.     end
  47. end
  48.  
  49. function layerOdd(x, y)
  50.     for i = 1, x, 1 do
  51.         if blackListActive then
  52.             mineXDistanceBlackList(y)
  53.         elseif whiteListActive then
  54.             mineXDistanceWhiteList(y)
  55.         else
  56.             mineXDistance(y)
  57.         end
  58.         if i ~= x then          
  59.             if i % 2 ~= 0 then
  60.                 turtle.turnLeft()
  61.                 turtle.dig()
  62.                 turtle.forward()
  63.                 turtle.turnLeft()
  64.             else
  65.                 turtle.turnRight()
  66.                 turtle.dig()
  67.                 turtle.forward()
  68.                 turtle.turnRight()
  69.             end
  70.             if not hasSub then
  71.                 y = y - 1
  72.                 hasSub = true
  73.             end
  74.         end
  75.     end
  76.     return y
  77. end
  78.  
  79. function layerEven(x, y)        
  80.     for i = 1, x, 1 do
  81.         if blackListActive then
  82.             mineXDistanceBlackList(y)
  83.         elseif whileListActive then
  84.             mineXDistanceWhiteList(y)
  85.         else
  86.             mineXDistance(y)
  87.         end
  88.         if i ~= x then                  
  89.             if i % 2 ~= 0 then
  90.                 turtle.turnRight()
  91.                 turtle.dig()
  92.                 turtle.forward()
  93.                 turtle.turnRight()
  94.             else
  95.                 turtle.turnLeft()
  96.                 turtle.dig()
  97.                 turtle.forward()
  98.                 turtle.turnLeft()
  99.             end
  100.             if not hasSub then
  101.                 y = y - 1
  102.                 hasSub = true
  103.             end
  104.         end
  105.     end
  106. end
  107.  
  108. -- Goes down a block and turns around
  109. function layerDown()
  110.     turtle.digDown()
  111.     turtle.down()
  112.     turtle.turnRight()
  113.     turtle.turnRight()
  114. end
  115.  
  116. -- Mine with a blacklist item filter
  117. function mineXDistanceBlackList(x)
  118.     for i = 1, x, 1 do
  119.         -- Check Fuel
  120.         if fuel.isFuelLevelSubOptimal(40) then
  121.             --Refuel
  122.             local fueled = false
  123.             local count = 1
  124.             while not fueled do -- Look through turtle's inventory to refuel
  125.                 turtle.select(count)
  126.                 fueled = turtle.refuel()
  127.                 count = count + 1
  128.                 if count >= 16 and not fueled then -- If the inventory has been check through and nothing has been found break
  129.                     print("Turtle is almost out of fuel and does not have fuel and cannot find any.")
  130.                     break
  131.                 end
  132.             end
  133.         end
  134.         -- Check Inventory
  135.         filterInventoryBlackList()
  136.         -- Dig and move forward
  137.         turtle.dig()
  138.         turtle.forward()
  139.     end
  140. end
  141.  
  142. -- Mine with a whiteList item filter
  143. function mineXDistanceWhiteList(x)
  144.     for i = 1, x, 1 do
  145.         -- Check Fuel
  146.         if fuel.isFuelLevelSubOptimal(40) then
  147.             --Refuel
  148.             local fueled = false
  149.             local count = 1
  150.             while not fueled do -- Look through turtle's inventory to refuel
  151.                 turtle.select(count)
  152.                 fueled = turtle.refuel()
  153.                 count = count + 1
  154.                 if count >= 16 and not fueled then -- If the inventory has been check through and nothing has been found break
  155.                     print("Turtle is almost out of fuel and does not have fuel and cannot find any.")
  156.                     break
  157.                 end
  158.             end
  159.         end
  160.         -- Check Inventory
  161.         filterInventoryWhiteList()
  162.         -- Dig and move forward
  163.         turtle.dig()
  164.         turtle.forward()
  165.     end
  166. end
  167.  
  168. -- Mine WithOut an item filter
  169. function mineXDistance(x)
  170.      for i = 1, x, 1 do
  171.         -- Check Fuel
  172.         if fuel.isFuelLevelSubOptimal(40) then
  173.             --Refuel
  174.             local fueled = false
  175.             local count = 1
  176.             while not fueled do -- Look through turtle's inventory to refuel
  177.                 turtle.select(count)
  178.                 fueled = turtle.refuel()
  179.                 count = count + 1
  180.                 if count >= 16 and not fueled then -- If the inventory has been check through and nothing has been found break
  181.                     print("Turtle is almost out of fuel and does not have fuel and cannot find any.")
  182.                     break
  183.                 end
  184.             end
  185.         end
  186.         -- Dig and move forward
  187.         turtle.dig()
  188.         turtle.forward()
  189.     end
  190. end
  191.  
  192. -- The Odd Y mining cycle
  193. function OddYCycle(x, y, z)
  194.     for s = 1, z, 1 do
  195.         y = layerOdd(x, y)
  196.         if s ~= z then
  197.             layerDown()
  198.         end
  199.     end
  200. end
  201. -- The Even Y mining cycle
  202. function EvenYCycle(x, y, z)
  203.     for s = 1, z, 1 do
  204.         if s % 2 ~= 0 then
  205.             y = layerOdd(x, y)
  206.         else
  207.             layerEven(x, y)
  208.         end
  209.         if s ~= z then
  210.             layerDown()
  211.         end
  212.     end
  213. end
  214.  
  215. function loadTableFromFile(fileName)
  216.     local file = fs.open(fileName, "r")
  217.     local data = file.readAll()
  218.     file.close()
  219.     return textutils.unserialize(data)
  220. end
  221.  
  222. function filterInventoryBlackList()
  223.     if(#blacklist > 0) then
  224.         local itemWasFound, itemsFound = inventory.checkInventoryForItems(blacklist)
  225.         if itemWasFound then
  226.             for i = 1, #blacklist, 1 do
  227.                 local item = itemsFound[blacklist[i]]
  228.                 for s = 1, #item, 1 do
  229.                     if item[s] then
  230.                         turtle.select(s)
  231.                         turtle.drop()
  232.                     end
  233.                 end
  234.             end
  235.         end
  236.     end
  237. end
  238.  
  239. function filterInventoryWhiteList()
  240.     if(#whiteList > 0) then
  241.         local slotsToKeep = {}
  242.         local itemWasFound, itemsFound = inventory.checkInventoryForItems(whiteList)
  243.         -- If one of the items on the whitelist was found then cache all the slots with items from the whitelist in them
  244.         if itemWasFound then
  245.             for i = 1, #whiteList, 1 do
  246.                 local item = itemsFound[whiteList[i]]
  247.                 for s = 1, #item, 1 do
  248.                     if item[s] then
  249.                         slotsToKeep[s] = true
  250.                     else
  251.                         if i ~= 1 then
  252.                             if not slotsToKeep[s] then
  253.                                 slotsToKeep[s] = false
  254.                             end
  255.                         else
  256.                             slotsToKeep[s] = false
  257.                         end
  258.                     end
  259.                 end
  260.             end
  261.         end
  262.         -- Get rid of every item from every slot that doesnt have a whiteList item in its slot
  263.         for i = 1, 16, 1 do
  264.             if itemWasFound then
  265.                 if not slotsToKeep[i] then
  266.                     if turtle.getItemCount(i) > 0 then
  267.                         turtle.select(i)
  268.                         turtle.drop()
  269.                     end
  270.                 end
  271.             else
  272.                 if turtle.getItemCount(i) > 0 then
  273.                     turtle.select(i)
  274.                     turtle.drop()
  275.                 end
  276.             end
  277.         end
  278.     end
  279. end
Add Comment
Please, Sign In to add comment