Advertisement
Hammi

Intelligent Stripmine Turtle

Apr 29th, 2019 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.43 KB | Gaming | 0 0
  1. --General Settings
  2. lengthOfRows = 5
  3. rowSpacing = 3
  4. numberOfRows = 5
  5. maxTorchDistance = 10
  6. runningLowWarning = 16
  7.  
  8. -- User Input Settings
  9. useTorches = false
  10. useChest = false
  11. useCoal = false
  12. useLava = false
  13. clearInv = false
  14. fastMode = false
  15. waitForTorches = false
  16. allowMultipleStacks = false
  17. returnToStart = false
  18.  
  19. -- Work Settings (DONT CHANGE...Changed by Program)
  20. Place_Blocks = {"minecraft:cobblestone","minecraft:cobbled_deepslate"}
  21. torchPlacement = 0
  22. localStorageNames = {"minecraft:chest","minecraft:trapped_chest","appliedenergistics2:sky_stone_chest","appliedenergistics2:smooth_sky_stone_chest","ironchest:iron_chest","ironchest:gold_chest","ironchest:diamond_chest","ironchest:copper_chest","ironchest:silver_chest","ironchest:crystal_chest","ironchest:obsidian_chest","quark:oak_chest","quark:spruce_chest","quark:birch_chest","quark:jungle_chest","quark:acacia_chest","quark:dark_oak_chest","quark:crimson_chest","quark:warped_chest","quark:nether_brick_chest","quark:purpur_chest","quark:prismarine_chest","quark:mushroom_chest", "charm:oak_chest", "charm:acacia_chest", "charm:spruce_chest"}
  23. remoteStorage = false
  24. remoteStorageNames = {"enderstorage:ender_storage","enderchests:ender_chest","dimstorage:dimensional_chest"}
  25. coal_names = {"minecraft:coal","minecraft:charcoal","modern_industrialization:lignite_coal"}
  26. storageName = ""
  27. firstOpenSlot = 2
  28. torchSlot = 0
  29. chestSlot = 0
  30. bucketSlot = 0
  31.  
  32. function itemInSlot(slot,name)
  33.   details = turtle.getItemDetail(slot)
  34.   if details ~= nil and details.name == name then
  35.       return true
  36.   end
  37.   return false
  38. end
  39.  
  40. function itemInInventory (name,deadSlot)
  41.   local outputarray = {}
  42.   for i=1,16 do
  43.     if i ~= deadSlot then
  44.       if itemInSlot(i,name) then
  45.         outputarray[#outputarray+1]= i
  46.       end
  47.     end
  48.   end
  49.   return outputarray
  50. end
  51.  
  52. function missingItem (name)
  53.   return (next(itemInInventory(name)) == nil) and true or false
  54. end
  55.  
  56. function waitForItem (name)
  57.   if missingItem(name)  then
  58.     print("Please insert: "..name)
  59.     while missingItem(name) do
  60.       sleep(0.5)
  61.     end
  62.   end
  63. end
  64.  
  65. function freeUpSlot(slot) -- can return slot number
  66.   if turtle.getItemCount(slot)==0 then
  67.     return 0
  68.   end
  69.   for i=1,16 do
  70.     if i~=slot then
  71.       if turtle.getItemCount(i)==0 then
  72.         turtle.select(slot)
  73.         turtle.transferTo(i)
  74.         return i
  75.       end
  76.     end
  77.   end
  78.   turtle.select(slot)
  79.   turtle.dropUp()
  80.   return slot
  81. end
  82.  
  83. function sortItems(slot,name)
  84.   waitForItem(name)
  85.   if not itemInSlot(slot,name) then
  86.     freeUpSlot(slot)
  87.   end
  88.   for i,v in ipairs(itemInInventory(name,slot)) do
  89.     turtle.select(v)
  90.     turtle.transferTo(slot)
  91.     if turtle.getItemCount(slot)>=64 then
  92.       turtle.select(1)
  93.       break
  94.     end
  95.   end
  96. end
  97.  
  98. function __checkForBlockName()
  99.   for k,v in pairs(Place_Blocks) do
  100.     if not missingItem(v) then
  101.       return v
  102.     end
  103.   end
  104.   return ""
  105. end
  106.  
  107. function checkForBlockName()
  108.   blockName=__checkForBlockName()
  109.   if blockName=="" then
  110.     print("Please insert a block to place")
  111.     while blockName=="" do
  112.       blockName=__checkForBlockName()
  113.       sleep(0.5)
  114.     end
  115.   end
  116.   return blockName
  117. end
  118.  
  119. function __checkForStorageName()
  120.   for k,v in pairs(localStorageNames) do
  121.     if not missingItem(v) then
  122.       return v
  123.     end
  124.   end
  125.   for k,v in pairs(remoteStorageNames) do
  126.     if not missingItem(v) then
  127.       remoteStorage = true
  128.       return v
  129.     end
  130.   end
  131.   return ""
  132. end
  133.  
  134. function checkForStorageName()
  135.   storageName=__checkForStorageName()
  136.   if storageName=="" then
  137.     print("Please insert a storage medium")
  138.     while storageName=="" do
  139.       storageName=__checkForStorageName()
  140.       sleep(0.5)
  141.     end
  142.   end
  143.   return storageName
  144. end
  145.  
  146. function dropInventory (direction)
  147.   for i=firstOpenSlot,16 do
  148.     if (allowMultipleStacks and not (itemInSlot(i,storageName) or itemInSlot(i,"minecraft:torches"))) or not allowMultipleStacks then
  149.       turtle.select(i)
  150.       if direction == "up" then
  151.         drop = turtle.dropUp
  152.       elseif direction == "down" then
  153.         drop = turtle.dropDown
  154.       else
  155.         error("Unknown direction to drop to, error in program")
  156.       end
  157.       while true do
  158.         drop()
  159.         if turtle.getItemCount(i)==0 then
  160.           break
  161.         end
  162.         print("Couldn't drop items... trying again")
  163.         sleep(2)
  164.       end
  165.     end
  166.   end
  167. end
  168.  
  169. function clearInventory ()
  170.   sortItems(chestSlot,storageName)
  171.   if remoteStorage then
  172.     turtle.select(chestSlot)
  173.     turtle.placeUp()
  174.     dropInventory("up")
  175.     turtle.select(chestSlot)
  176.     turtle.digUp()
  177.   else
  178.     turtle.digDown()
  179.     turtle.select(chestSlot)
  180.     turtle.placeDown()
  181.     dropInventory("down")
  182.   end
  183.   turtle.select(1)
  184. end
  185.  
  186. function checkInventoryFull()
  187.   if turtle.getItemCount(16)>0 then
  188.     clearInventory()
  189.   end
  190. end
  191.  
  192. function calculateFuelNeed()
  193.   fuelneeded = 0
  194.   if newMine then
  195.     fuelneeded = fuelneeded + rowSpacing
  196.   end
  197.   fuelneeded = fuelneeded + lengthOfRows*4*numberOfRows
  198.   fuelneeded = fuelneeded + rowSpacing*2*numberOfRows
  199.   if returnToStart then
  200.     fuelneeded = fuelneeded + numberOfRows*rowSpacing*2
  201.   end
  202.   return fuelneeded
  203. end
  204.  
  205. function burnCoal()
  206.   for i,v in ipairs(itemInInventory("minecraft:coal")) do
  207.     turtle.select(v)
  208.     turtle.refuel()
  209.   end
  210. end
  211.  
  212. function oneBucket()
  213.   if itemInSlot(bucketSlot,"minecraft:bucket") then
  214.     details = turtle.getItemDetail(bucketSlot)
  215.     if details.count >1 then
  216.       turtle.select(bucketSlot)
  217.       turtle.dropUp(details.count - 1)
  218.       turtle.select(1)
  219.     end
  220.     return true
  221.   end
  222.   return false
  223. end
  224.  
  225. function checkForLava()
  226.   bool,details = turtle.inspectDown()
  227.   if details.name == "minecraft:lava" and details.state.level == 0 and oneBucket() then
  228.     turtle.select(bucketSlot)
  229.     turtle.placeDown()
  230.     turtle.refuel()
  231.     turtle.select(1)
  232.   end
  233. end
  234.  
  235. function placeTorch()
  236.   if torchPlacement>=10 then
  237.     if not itemInSlot(torchSlot,"minecraft:torch") then
  238.       if not missingItem("minecraft:torch") then
  239.         sortItems(torchSlot,"minecraft:torch")
  240.       else
  241.         if waitForTorches then
  242.           sortItems(torchSlot,"minecraft:torch")
  243.         else
  244.           print("Out of torches!")
  245.         end
  246.       end
  247.     end
  248.     if itemInSlot(torchSlot,"minecraft:torch") then
  249.       turtle.turnLeft()
  250.       turtle.turnLeft()
  251.       turtle.select(torchSlot)
  252.       while not turtle.place() do
  253.         turtle.dig()
  254.         sleep(0.5)
  255.       end
  256.       turtle.turnLeft()
  257.       turtle.turnLeft()
  258.       turtle.select(1)
  259.       if turtle.getItemCount(torchSlot) <= runningLowWarning then
  260.         print("Running low on torches")
  261.       end
  262.       torchPlacement = 0
  263.     end
  264.   end
  265.   torchPlacement = torchPlacement + 1
  266. end
  267.  
  268. function forward(times)
  269.   times = times or 1
  270.   for i=1,times do
  271.     while (not turtle.forward()) do
  272.       turtle.dig()
  273.       sleep(0.4)
  274.     end
  275.     while (turtle.detectUp()) do
  276.       turtle.digUp()
  277.       sleep(0.4)
  278.     end
  279.     if not fastMode then
  280.       if useLava then
  281.         checkForLava()
  282.       end
  283.       if turtle.getItemCount(1) < 16 then
  284.         -- Check what is currently used for placing
  285.         for i,v in ipairs(Place_Blocks) do
  286.           if turtle.getItemDetail(1).name == Place_Blocks[0] then
  287.             sortItems(1,Place_Blocks[0])
  288.             break
  289.           end
  290.         end
  291.       end
  292.       turtle.select(1)
  293.       turtle.placeDown()
  294.       if useChest then
  295.         checkInventoryFull()
  296.       end
  297.       if useTorches then
  298.         placeTorch()
  299.       end
  300.     else
  301.       turtle.select(1)
  302.       turtle.placeDown()
  303.     end
  304.   end
  305. end
  306.  
  307. -- User Inputs ------------------------------------
  308. term.clear()
  309. term.setCursorPos(1,1)
  310.  
  311. confirmationScreen = "Starting to mine with:\n"
  312. itemsNeeded = "Order of Items in Inventory does not matter. \nPlease add: 1x64 Placement Block"
  313. local termInput = ""
  314.  
  315. print("Start new Mine? (y for yes):")
  316. newMine = read()=="y" and true or false
  317. confirmationScreen = newMine and confirmationScreen.."New mine: true\n" or confirmationScreen
  318. print("Length of rows? (Default: "..tostring(lengthOfRows).."):")
  319. termInput = read()
  320. lengthOfRows = termInput=="" and lengthOfRows or tonumber(termInput)
  321. confirmationScreen = confirmationScreen.."Row Length: "..tostring(lengthOfRows).."\n"
  322. print("Number of rows? (Default: "..numberOfRows.."): ")
  323. termInput = read()
  324. numberOfRows =  termInput=="" and numberOfRows or tonumber(termInput)
  325. confirmationScreen = confirmationScreen.."Row Length: "..tostring(lengthOfRows).."\n"
  326. print("Following y for yes or anything else for no:")
  327. print("Fast Mode?:")
  328. fastMode = read()=="y" and true
  329. confirmationScreen = confirmationScreen.."Fast Mode: "..tostring(fastMode).."\n"
  330. if not fastMode then
  331.   print("Use Torches?:")
  332.   useTorches = read()=="y" and true
  333.   if useTorches then
  334.     confirmationScreen = confirmationScreen.."useTorches: true\n"
  335.     itemsNeeded = itemsNeeded..", 1x64 Torches"
  336.     print("Wait for Torches?:")
  337.     waitForTorches = read()=="y" and true
  338.     confirmationScreen = waitForTorches and confirmationScreen.."waitForTorches: true\n" or confirmationScreen
  339.   end
  340.   print("Use Chest?:")
  341.   useChest = read()=="y" and true
  342.   if useChest then
  343.     confirmationScreen = confirmationScreen.."useChest: true\n"
  344.     itemsNeeded = itemsNeeded..", 1x64 Chests"
  345.     print("Clear Inventory at the end?:")
  346.     clearInv = read()=="y" and true
  347.     confirmationScreen = clearInv and confirmationScreen.."clearInv: true\n" or confirmationScreen
  348.   end
  349.   if useChest or useTorches then
  350.     print("Allow MultipleStacks?:")
  351.     allowMultipleStacks = read()=="y" and true
  352.     confirmationScreen = allowMultipleStacks and confirmationScreen.."allowMultipleStacks: true\n" or confirmationScreen
  353.   end
  354.   print("Burn Coal: ")
  355.   useCoal = read()=="y" and true
  356.   confirmationScreen = useCoal and confirmationScreen.."useCoal: true\n" or confirmationScreen
  357.   print("Warning: Lava only useful on height 11")
  358.   print("Burn Lava: ")
  359.   useLava = read()=="y" and true
  360.   if useLava then
  361.     confirmationScreen = confirmationScreen.."useLava: true\n"
  362.     itemsNeeded = itemsNeeded..", 1x Bucket"
  363.   end
  364. end
  365. print("Return to start Point?: ")
  366. returnToStart = read()=="y" and true
  367. confirmationScreen = returnToStart and confirmationScreen.."returnToStart: true\n" or confirmationScreen
  368.  
  369. term.clear()
  370. term.setCursorPos(1,1)
  371. write(confirmationScreen)
  372. read()
  373.  
  374. term.clear()
  375. term.setCursorPos(1,1)
  376. confirmationScreen = ""
  377. if (turtle.getFuelLevel()-calculateFuelNeed())<=0 then
  378.   confirmationScreen = confirmationScreen.."Not Enough Fuel\n"
  379.   if useCoal or useLava then
  380.     confirmationScreen = confirmationScreen.."Burn Coal is active could be enough\n"
  381.   end
  382. else
  383.   confirmationScreen = confirmationScreen.."Enough Fuel\n"
  384. end
  385. confirmationScreen = confirmationScreen..itemsNeeded..".\n"
  386. if allowMultipleStacks then
  387.   confirmationScreen = confirmationScreen.."You can also add more Torches or Chests\n"
  388. end
  389. confirmationScreen = confirmationScreen.."Start? (else CTRL+R)"
  390.   print(confirmationScreen)
  391.   read()
  392.  
  393. term.clear()
  394. term.setCursorPos(1,1)
  395.  
  396.  
  397. sortItems(1,checkForBlockName())
  398.  
  399. if useTorches then
  400.   torchSlot = firstOpenSlot
  401.   firstOpenSlot = firstOpenSlot + 1
  402.   sortItems(torchSlot,"minecraft:torch")
  403. end
  404. if useChest then
  405.   chestSlot = firstOpenSlot
  406.   firstOpenSlot = firstOpenSlot + 1
  407.   sortItems(chestSlot,checkForStorageName())
  408. end
  409. if useLava then
  410.   bucketSlot = firstOpenSlot
  411.   firstOpenSlot = firstOpenSlot + 1
  412.   sortItems(bucketSlot, "minecraft:bucket")
  413.   oneBucket()
  414. end
  415.  
  416. -- Main Programm --
  417. turtle.select(1)
  418. if(newMine) then
  419.   forward(rowSpacing)
  420. end
  421. for i = 1,tonumber(numberOfRows) do
  422.   print("starting row: "..tostring(i) .. " " .. turtle.getFuelLevel())
  423.   turtle.dig()
  424.   turtle.turnLeft()
  425.   forward(lengthOfRows)
  426.   turtle.turnRight()
  427.   -- Turnaround
  428.   forward(rowSpacing)
  429.   turtle.turnRight()
  430.   forward(lengthOfRows)
  431.   turtle.turnRight()
  432.   turtle.dig()
  433.   -- In middle
  434.   turtle.turnLeft()
  435.   turtle.turnLeft()
  436.   turtle.dig()
  437.   turtle.turnRight()
  438.   forward(lengthOfRows)
  439.   turtle.turnLeft()
  440.   -- Turnaround
  441.   forward(rowSpacing)
  442.   turtle.turnLeft()
  443.   forward(lengthOfRows)
  444.   turtle.turnLeft()
  445.   turtle.dig()
  446.   turtle.turnRight()
  447.   turtle.turnRight()
  448.   if useCoal then
  449.     burnCoal()
  450.   end
  451. end
  452. if returnToStart then
  453.   turtle.turnLeft()
  454.   turtle.turnLeft()
  455.   forward(numberOfRows*rowSpacing*2)
  456.   turtle.turnLeft()
  457.   turtle.turnLeft()
  458. end
  459. if clearInv then
  460.   print("Cleaning inventory!")
  461.   clearInventory()
  462. end
  463. print("Finished Mine")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement