faintvirus

mine

Dec 13th, 2020 (edited)
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.38 KB | None | 0 0
  1. local SLOT_COUNT = 16
  2. local args = {...}
  3.  
  4. if #args == 0 then
  5.     print("Usage: mine <x> [z] [depth]")
  6.     return
  7. end --if
  8.  
  9. local xmax = tonumber(args[1]) -- width
  10. local zmax = tonumber(args[2]) -- depth
  11. local ymax = tonumber(args[3]) -- height
  12.  
  13. if xmax == nil or xmax < 0 or zmax == nil or zmax < 0 or ymax == nil or ymax < 0 then
  14.     print("Invalid dimensions")
  15.     return
  16. end
  17.  
  18. local xDir, zDir = 0,1 -- xDir = 0 to start in z axis direction
  19. --local direction = 0
  20. local xPos, zPos, yPos = 0,0,0
  21.  
  22. DROPPED_ITEMS = {
  23.     "minecraft:stone",
  24.     "minecraft:cobblestone",
  25.     "minecraft:grass",
  26.     "minecraft:dirt",
  27.     "minecraft:gravel",
  28.     "minecraft:flint",
  29.     "minecraft:sand",
  30.     "minecraft:redstone",
  31.     "minecraft:dye"
  32. }
  33.  
  34. local function condense()
  35.     print("Condensing inventory...")
  36.     local x, y, slot
  37.     local condensed_items = 0
  38.     slot = turtle.getSelectedSlot()
  39.     for x = 2, SLOT_COUNT, 1 do
  40.         turtle.select(x)
  41.         for y = 1, x-1, 1 do
  42.             if turtle.getItemCount(x) == 0 then
  43.                 break
  44.             end --if
  45.             turtle.transferTo(y)
  46.         end --for
  47.     end --for
  48.     print("Condensing - finished.")
  49.     turtle.select(slot)
  50. end --function
  51.  
  52.  
  53. --condense()
  54.  
  55.  
  56. local function dropItems()
  57.     condense()
  58.     print("Dropping items...")
  59.     for slot = 1, SLOT_COUNT, 1 do -- numeric exp, generic exp, increment exp
  60.         local item = turtle.getItemDetail(slot)
  61.         if item ~= nil then
  62.             for filterIndex = 1, #DROPPED_ITEMS, 1 do
  63.                 if item["name"] == DROPPED_ITEMS[filterIndex] then
  64.                     --print("Dropping " .. item["name"])
  65.                     turtle.select(slot)
  66.                     turtle.drop()
  67.                 end --if/else
  68.             end --for
  69.         end --if
  70.     end --for
  71. end --function
  72.  
  73.  
  74. --dropItems()
  75.  
  76.  
  77. local function getItemIndex(itemName)
  78.     for slot = 1, SLOT_COUNT, 1 do
  79.         local item = turtle.getItemDetail(slot)
  80.         if item ~= nil then
  81.             if item.name == itemName then
  82.                 return slot
  83.             end --if
  84.         end --if
  85.     end --for
  86. end --function
  87.  
  88. local function storeItems()
  89.     turtle.select(1)
  90.     turtle.digUp()
  91.     turtle.placeUp()
  92.     -- Chest has been deployed
  93.     for slot = 1, SLOT_COUNT, 1 do
  94.         local item = turtle.getItemDetail(slot)
  95.         if item ~= nil then
  96.             if item.name ~= "minecraft:coal_block" and item.name ~= "minecraft:coal" and item.name ~= "enderstorage:ender_storage" then
  97.                 turtle.select(slot)
  98.                 turtle.dropUp()
  99.             end --if
  100.         end --if
  101.     end --for
  102.     -- Items have been stored
  103.     turtle.digUp()
  104.     print("Items stored -- condensing...")
  105.     condense()
  106. end --function
  107.  
  108.  
  109. --storeItems()
  110.  
  111.  
  112. local function checkInventory()
  113.     local invFull = true
  114.     local nTotalItems = 0
  115.  
  116.     for n = 1, SLOT_COUNT, 1 do
  117.         local nCount = turtle.getItemCount(n)
  118.         if nCount == 0 then
  119.             invFull = false
  120.         end --if
  121.     end --for
  122.  
  123.     if invFull then
  124.         print("No empty slots left.")
  125.         return false
  126.     else
  127.         print("There is still room in the inventory.")
  128.     end --if/else
  129.     return true
  130. end --function
  131.  
  132.  
  133. --checkInventory()
  134.  
  135.  
  136. local function refuel()
  137.     while turtle.getFuelLevel() == 0 do
  138.         local bucketIndex
  139.         turtle.select(2)
  140.         turtle.digUp()
  141.         turtle.placeUp()
  142.         -- fuel chest is deployed
  143.        
  144.         turtle.suckUp()
  145.    
  146.         bucketIndex = getItemIndex("minecraft:lava_bucket")
  147.         if bucketIndex ~= nil then
  148.             turtle.select(bucketIndex)
  149.             turtle.refuel()
  150.             storeItems()
  151.             break
  152.         end
  153.     end
  154.     turtle.select(1)
  155. end --function
  156.  
  157. refuel()
  158.  
  159.  
  160. --[[
  161. ========== MOVEMENT FUNCTIONS ==========
  162. ]]
  163. local stuck = false
  164. local function isStuck()
  165.     return stuck
  166. end --function
  167.  
  168.  
  169. local function forward()
  170.     refuel()
  171.     stuck = false
  172.     while not turtle.forward() do
  173.         if turtle.detect() then
  174.             if not turtle.dig() then
  175.                 stuck = true
  176.                 return
  177.             end --if
  178.         end --if
  179.     end --while
  180.     xPos = xPos + xDir
  181.     zPos = zPos + zDir
  182.     print("xPos: "..xPos)
  183.     print("zPos: "..zPos)
  184. end --function
  185.  
  186.  
  187. local function up()
  188.     refuel()
  189.     stuck = false
  190.     while not turtle.up() do
  191.         if turtle.detectUp() then
  192.             if not turtle.digUp() then
  193.                 stuck = true
  194.                 return
  195.             end --if
  196.         end --if
  197.     end --while
  198.     print("Going up...")
  199.     yPos = yPos + 1
  200. end --function
  201.  
  202.  
  203. local function down()
  204.     refuel()
  205.     stuck = false
  206.     while not turtle.down() do
  207.         if turtle.detectDown() then
  208.             if not turtle.digDown() then
  209.                 stuck = true
  210.                 return
  211.             end --if
  212.         end --if
  213.     end --while
  214.     print("Going down...")
  215.     yPos = yPos - 1
  216. end --function
  217.  
  218.  
  219. local function left(n)
  220.     local x
  221.     for x = 1, n or 1, 1 do
  222.         print("Turning left...")
  223.         turtle.turnLeft()
  224.         xDir, zDir = -zDir, xDir
  225.     end --for
  226. end --function
  227.  
  228. local function right(n)
  229.     local x
  230.     for x = 1, n or 1, 1 do
  231.         print("Turning right...")
  232.         turtle.turnRight()
  233.         xDir, zDir = zDir, -xDir
  234.     end --for
  235. end --function
  236.  
  237.  
  238. local function turnAround()
  239.     if zDir == 1 then
  240.         right(2)
  241.     else
  242.         left(2)
  243.     end --if/else
  244. end --function
  245.  
  246.  
  247. --[[
  248. ========== GOTO FUNCTIONS ==========
  249. ]]
  250. local function goTo(x,y,z,xd,zd)
  251.     print("Going...")
  252.     while yPos > y do down() end
  253.  
  254.     if xPos > x then -- if to the right of x
  255.         while xDir ~= -1 do left() end -- if xDir == 1 (facing right) then left(2)
  256.         while xPos > x do forward() end -- forward until xPos becomes x
  257.    
  258.     elseif xPos < x then -- if to the left of x
  259.         while xDir ~= 1 do left() end -- while xDir == -1 (facing left) then left(2)
  260.         while xPos < x do forward() end
  261.     end --if/else
  262.  
  263.     if zPos > z then -- if past z
  264.         while zDir ~= -1 do left() end -- while zDir == 1 (facing forwards) then left(2)
  265.         while zPos > z do forward() end -- forward until zPos becomes z
  266.    
  267.     elseif zPos < z then -- if before z
  268.         while zDir ~= 1 do left() end -- while zDir == -1 (facing backwards) then left(2)
  269.         while zPos < z do forward() end
  270.     end --if/else
  271.  
  272.     while yPos < y do up() end
  273.     while zDir ~= zd or xDir ~= xd do left() end
  274. end --function
  275.  
  276.  
  277. local function returnToOrigin()
  278.     local x, y, z, xd, zd = xPos, yPos, zPos, xDir, zDir
  279.     print("Returning to surface...")
  280.     goTo(0,0,0,0,-1)
  281.  
  282.     local fuelNeeded = 2*(x + y + z) + 1
  283.     local currentFuelLevel = turtle.getFuelLevel()
  284.     if currentFuelLevel < fuelNeeded then
  285.         refuel()
  286.         storeItems()
  287.     else
  288.         storeItems()
  289.     end --if/else
  290.  
  291.     print("Resuming mining...")
  292.     goTo(x,y,z,xd,zd)
  293. end --function
  294.  
  295. --returnToOrigin()
  296.  
  297. --[[
  298. ========== MAIN ==========
  299. ]]
  300.  
  301. print("Mining process - initiated.")
  302.  
  303. local currFuel
  304. local done = false
  305.  
  306. while not done and not isStuck() do
  307.  
  308.     currFuel = turtle.getFuelLevel()
  309.     if currFuel == 0 then
  310.         refuel()
  311.     end --if
  312.  
  313.     turtle.select(1)
  314.  
  315.     print("Z Direction: "..zDir)
  316.     print("X Direction: "..xDir)
  317.  
  318.     if zDir == 1 then
  319.         while zPos < zmax - 1 do
  320.             forward()
  321.             if isStuck() then
  322.                 done = true
  323.                 break
  324.             end --if
  325.         end --while
  326.     elseif zDir == -1 then
  327.         while zPos > 0 do
  328.             forward()
  329.             if isStuck() then
  330.                 done = true
  331.                 break
  332.             end --if
  333.         end --while
  334.     end --if/else
  335.     -- reached end of z axis
  336.    
  337.     if done then break end
  338.  
  339.     if xPos == 0 and zDir == -1 then
  340.         down()
  341.         turnAround()
  342.     elseif xPos == xmax - 1 and zDir == 1 then
  343.         down()
  344.         turnAround()
  345.     end --if/else
  346.  
  347.     if zPos == zmax - 1 and zDir == 1 then
  348.         right()
  349.         forward()
  350.         right()
  351.     elseif zPos == 0 and zDir == -1 then
  352.         left()
  353.         forward()
  354.         left()
  355.     end --if/else
  356.  
  357.     if turtle.getItemCount(15) > 0 then
  358.         storeItems()
  359.     end --if
  360.  
  361. end --while/MAIN
  362.  
  363. goTo(0,0,0,0,-1)
  364. storeItems()
  365. goTo(0,0,0,0,1)
  366.  
  367. turtle.select(1)
  368.  
  369. print("Mining process - completed.")
Add Comment
Please, Sign In to add comment