Advertisement
daFARKA

Minecraft MiningTurtle Excavation Program

Jun 4th, 2022 (edited)
1,370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.32 KB | None | 0 0
  1. ---@diagnostic disable: undefined-global
  2. local input = { ... }
  3.  
  4. local size = 5
  5. local depth = 0
  6. local chestName = 'minecraft:chest'
  7. local coalName = 'minecraft:coal'
  8. local bedrockName = 'minecraft:bedrock'
  9. local chestSlot = 15
  10. local coalSlot = 16
  11.  
  12. -- start of input logic
  13.  
  14. if input[1] == nil then
  15.     print("Usage: 'mine' 'size' ['coalslot'] ['chestslot']")
  16.     print("The Coal has to be in the last slot (16) and at least 2 chests should be in the second last slot (15) or one in the slot and one already be placed on top of the turtle.")
  17. end
  18.  
  19. if input[1] ~= nil and tonumber(input[1]) <= 0 then
  20.     print("Size is 0 or negative!")
  21.     return
  22. end
  23.  
  24. if input[1] ~= nil and tonumber(input[1]) > 0 then
  25.     size = tonumber(input[1])
  26. end
  27.  
  28. if size == nil or size <= 0 then
  29.     print("Size: " .. size .. "\n" .. "Is simply invalid!")
  30.     return
  31. end
  32.  
  33. if input[2] ~= nil then
  34.     if tonumber(input[2]) >= 2 and tonumber(input[2]) <= 16 then
  35.         coalSlot = tonumber(input[2])
  36.     end
  37. end
  38.  
  39. if input[3] ~= nil then
  40.     if tonumber(input[3]) >= 2 and tonumber(input[3]) <= 16 then
  41.         if tonumber(input[3]) ~= coalSlot then
  42.             chestSlot = tonumber(input[3])
  43.         end
  44.     end
  45. end
  46.  
  47. -- end of input logic
  48.  
  49. -- Stacks all of the coal found in the local inventory to slot 16
  50. local function coalSetup()
  51.     for slot = 1, 16, 1 do
  52.         local slotData = turtle.getItemDetail(slot)
  53.         if slotData then
  54.             if slotData.name == coalName then
  55.                 turtle.select(slot)
  56.                 turtle.transferTo(coalSlot)
  57.             end
  58.         end
  59.     end
  60.     turtle.select(1)
  61. end
  62.  
  63. -- Dumps the local inventory into the starting chest
  64. local function dumpInv()
  65.     local success, data = turtle.inspectUp()
  66.     if data.name == chestName then
  67.         for slot = 1, 16, 1 do
  68.             turtle.select(slot)
  69.             local slotData = turtle.getItemDetail(slot)
  70.             if slotData then
  71.                 if slotData.name ~= chestName and slotData.name ~= coalName then
  72.                     if turtle.dropUp() then
  73.                         -- HIER: turtle.dropUp() only checks if dropUp() worked NOT if the chest is full.
  74.                         -- the chest is not full!
  75.                         -- else the chest is full.
  76.                     else
  77.                         print("Chest above is full!")
  78.                         os.shutdown()
  79.                     end
  80.                 end
  81.             end
  82.         end
  83.     else
  84.         local chestInInv = false
  85.         local validSlot
  86.         for slot = 1, 16, 1 do
  87.             local slotData = turtle.getItemDetail(slot)
  88.             if slotData then
  89.                 if slotData.name == chestName then
  90.                     chestInInv = true
  91.                     validSlot = slot
  92.                 end
  93.             end
  94.         end
  95.         if chestInInv then
  96.             turtle.select(validSlot)
  97.             turtle.placeUp()
  98.             dumpInv()
  99.         else
  100.             print("No Chest either above the turtle or in the turtle inventory. Aborted!")
  101.             return
  102.         end
  103.     end
  104.     turtle.select(1)
  105. end
  106.  
  107. -- After an execution of the function 'excavate()' the turtle goes to the origin of the coordinates (aka where the chest is / supposed to be)
  108. local function goToOrigin()
  109.     if size % 2 == 1 then
  110.         turtle.turnLeft()
  111.         for i = 1, size-1, 1 do
  112.             turtle.forward()
  113.         end
  114.         turtle.turnLeft()
  115.         for i = 1, size-1, 1 do
  116.             turtle.forward()
  117.         end
  118.         turtle.turnLeft()
  119.         turtle.turnLeft()
  120.     else
  121.         turtle.turnRight()
  122.         for i = 1, size-1, 1 do
  123.             turtle.forward()
  124.         end
  125.         turtle.turnRight()
  126.     end
  127. end
  128.  
  129. -- The turtle goes back up to the top, where (ideally) the chest is located.
  130. local function goBackUp()
  131.     for i = 1, depth, 1 do
  132.         turtle.up()
  133.     end
  134. end
  135.  
  136. -- The turtle goes bakc down, where (ideally) the turtle can resume excavating.
  137. local function goBackDown()
  138.     for i = 1, depth, 1 do
  139.         turtle.down()
  140.     end
  141. end
  142.  
  143. -- excavates an area the size of 'size' (default: 5) and immediately after runs goToOrigin() and goes 1 deeper
  144. local function excavate()
  145.     for i = 1, size, 1 do
  146.         for j = 1, size-1, 1 do
  147.             turtle.digDown()
  148.             turtle.forward()
  149.         end
  150.         turtle.digDown()
  151.         if i ~= size then
  152.             if i % 2 == 1 then
  153.                 turtle.turnRight()
  154.                 turtle.forward()
  155.                 turtle.turnRight()
  156.             else
  157.                 turtle.turnLeft()
  158.                 turtle.forward()
  159.                 turtle.turnLeft()
  160.             end
  161.         end
  162.     end
  163.     goToOrigin()
  164.     depth = depth + 1
  165.     turtle.down()
  166. end
  167.  
  168. -- This function is needed for the function 'searchForCoal()'.
  169. -- It either directly drops the items in slot 1 into a second chest placed under the turtle or finds a chest in inventory and places it and then drops the items.
  170. -- If no chest is found abort.
  171. local function chestUnderTurtle()
  172.     local success, data = turtle.inspectDown()
  173.     if data.name == chestName then
  174.         turtle.dropDown()
  175.     -- if the chest is not already placed, search for a chest in the inventory of the turtle
  176.     -- if no chest is found abort.
  177.     else
  178.         for slot = 1, 16, 1 do
  179.             local slotData1 = turtle.getItemDetail(slot)
  180.             if slotData1 then
  181.                 if slotData1.name == chestName then
  182.                     chestInInv = true
  183.                     validSlot = slot
  184.                 end
  185.             end
  186.         end
  187.         if chestInInv then
  188.             turtle.select(validSlot)
  189.             turtle.placeDown()
  190.             turtle.select(1)
  191.             turtle.dropDown()
  192.         else
  193.             print("No Chest either under the turtle or in the turtle inventory. Aborted!")
  194.             return
  195.         end
  196.     end
  197. end
  198.  
  199. -- Cycles all of the items of the chest under the turtle back up to the chest above the turtle.
  200. local function cycleItemsUp()
  201.     while turtle.suckDown() do
  202.         turtle.select(1)
  203.         turtle.dropUp()
  204.     end
  205.     turtle.digDown()
  206.     local chestInInv = false
  207.     local validSlot
  208.     for slot = 1, 16, 1 do
  209.         local slotData = turtle.getItemDetail(slot)
  210.         if slotData then
  211.             if slotData.name == chestName then
  212.                 chestInInv = true
  213.                 validSlot = slot
  214.             end
  215.         end
  216.     end
  217.     if chestInInv then
  218.         turtle.transferTo(validSlot)
  219.     else
  220.         turtle.transferTo(chestSlot)
  221.     end
  222. end
  223.  
  224. -- Searches for Coal in an above chest
  225. local function searchForCoal()
  226.     local foundNoCoal = true
  227.     while foundNoCoal and turtle.suckUp() do
  228.         turtle.select(1)
  229.         local slotData = turtle.getItemDetail()
  230.         if slotData then
  231.             if slotData.name == coalName then
  232.                 turtle.transferTo(14)
  233.                 if turtle.getItemCount(14) > 32 then
  234.                     foundNoCoal = false
  235.                 end
  236.             -- else the slot does not contain coal
  237.             else
  238.                 chestUnderTurtle()
  239.             end
  240.         end
  241.     end
  242.     coalSetup()
  243.     cycleItemsUp()
  244. end
  245.  
  246. -- Looks at the fuel level and decides wether to continue, just refuel or needs to gobackUp() to suck fuel out of the chest or (worst case) terminate if no fuel is available.
  247. local function refuel()
  248.     local safetyLvl = 20
  249.     -- if fuelLvl is in critical range
  250.     if turtle.getFuelLevel() < size*size + depth + safetyLvl then
  251.         coalSetup()
  252.         turtle.select(coalSlot)
  253.         turtle.refuel()
  254.         turtle.select(1)
  255.         -- if after setting up fuel the fuel is still critical gobackUp() and search for fuel in chest
  256.         if turtle.getFuelLevel() < size*size + depth + safetyLvl then
  257.             goBackUp()
  258.             dumpInv()
  259.             searchForCoal()
  260.             turtle.select(coalSlot)
  261.             turtle.refuel()
  262.             turtle.select(1)
  263.             -- if even after searching for fuel in the chest the fuel is still at critical Level, need to abort.
  264.             if turtle.getFuelLevel() < size*size + depth + safetyLvl then
  265.                 print("FuelLevel: " .. turtle.getFuelLevel() .. " ist still critically low.\nNeed to abort!" )
  266.                 os.shutdown()
  267.             -- else need to goBackDown() and resume mining
  268.             else
  269.                 goBackDown()
  270.             end
  271.         end
  272.     end
  273. end
  274.  
  275. -- Detects bedrock under the turtle, meaning the turtle is finished.
  276. local function reachedBedrock()
  277.     local success, data = turtle.inspectDown()
  278.     if success then
  279.         if data.name == bedrockName then
  280.             return true
  281.         end
  282.     end
  283.     return false
  284. end
  285.  
  286. -- This function does all the work if the turtle is full.
  287. local function turtleIsFullHandler()
  288.     local numbOfNonEmptySlots = 0
  289.     for slot = 1, 16, 1 do
  290.         if turtle.getItemCount(slot) ~= 0 then
  291.             numbOfNonEmptySlots = numbOfNonEmptySlots + 1
  292.         end
  293.     end
  294.     if numbOfNonEmptySlots > 14 then
  295.         goBackUp()
  296.         dumpInv()
  297.         refuel()
  298.         goBackDown()
  299.     end
  300. end
  301.  
  302. -- main program
  303. local function main()
  304.     while reachedBedrock() == false do
  305.         refuel()
  306.         turtleIsFullHandler()
  307.         excavate()
  308.     end
  309.     goBackUp()
  310.     dumpInv()
  311.     coalSetup()
  312. end
  313.  
  314. -- testing area
  315. main()
  316. print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement