Advertisement
Henning_B

Untitled

Nov 15th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.45 KB | None | 0 0
  1. local fuelSlot = 1
  2. local torchSlot = 2
  3. local firstItem,lastItem = 3,16
  4.  
  5. local currentY = 1
  6. local currentX = 1
  7. local currentZ = 2
  8.  
  9. local startY = 1
  10. local startX = 1
  11. local startZ = 2
  12.  
  13.  
  14. local setting = {}
  15. setting["tunnelLength"] = 0
  16. setting["lastX"] = 1
  17. setting["lastY"] = 2
  18. setting["lastZ"] = 1
  19. setting["currentCaveMiddle"] = 2
  20. setting["lastTorching"] = 0
  21. setting["currentTunnel"] = 0
  22. setting["WallEntryWidth"] = 1
  23. setting["WallScore"] = 0
  24.  
  25.  
  26.  
  27.  
  28. local fuelBarrel = nil
  29. local torchBarrel = nil
  30. local cobbleBarrel = nil
  31.  
  32. local CONFIG_FILE = "miner.json"
  33. local loadedConfig = nil
  34.  
  35.  
  36. local NORTH = 1
  37. local EAST = 2
  38. local SOUTH = 3
  39. local WEST = 4
  40. local currentFace = NORTH
  41.  
  42.  
  43.  
  44. local currentStage = 1
  45.  
  46.  
  47.  
  48. function placeAll(all)
  49.     local start = firstItem
  50.     if(all) then
  51.         start = 1
  52.     end
  53.     local i = firstItem
  54.     while i <= lastItem do
  55.         turtle.select(i)
  56.         turtle.drop()
  57.         if not turtle.getItemDetail() then
  58.             i = i + 1
  59.         else
  60.             print("Chest is full, stopping")
  61.             exit()
  62.         end
  63.     end
  64. end
  65.  
  66. function checkAndReset()
  67.     local lookingAt = nil
  68.     local itemData = nil
  69.     lookingAt, itemData = turtle.inspect()
  70.     if lookingAt then
  71.         if string.match(itemData.name, "chest") then
  72.             print("Found Chest")
  73.             placeAll(true)
  74.         else
  75.             print("Missing loot Chest")
  76.             return false
  77.         end
  78.     else
  79.         print("Missing loot Chest")
  80.         return false
  81.     end
  82.     setDirection(WEST)
  83.     turtle.forward()
  84.     setDirection(NORTH)
  85.     lookingAt, itemData = turtle.inspect()
  86.     if lookingAt then
  87.         if string.match(itemData.name, "barrel") then
  88.             print("found barrel")
  89.             turtle.select(1)
  90.             turtle.suck()
  91.             item = turtle.getItemDetail()
  92.             turtle.drop()
  93.             if item then
  94.                 if string.match(item.name, "coal") then
  95.                     fuelBarrel = 1
  96.                 elseif string.match(item.name, "torch") then
  97.                     torchBarrel = 1
  98.                 elseif string.match(item.name, "stone") then
  99.                     cobbleBarrel = 1
  100.                 else
  101.                     print("Missing coal, torch or stone in barrel")
  102.                     return false
  103.                 end
  104.             else
  105.                 print("Missing coal, torch or stone in barrel")
  106.                 return false
  107.             end
  108.            
  109.         else
  110.             print("Missing coal, torch or stone in barrel")
  111.             return false
  112.         end
  113.     else
  114.         print("Missing coal, torch or stone in barrel")
  115.         return false
  116.     end
  117.  
  118.     turtle.up()
  119.     lookingAt, itemData = turtle.inspect()
  120.  
  121.     if lookingAt then
  122.         if string.match(itemData.name, "barrel") then
  123.             print("found barrel")
  124.             turtle.select(1)
  125.             turtle.suck()
  126.             item = turtle.getItemDetail()
  127.             turtle.drop()
  128.             if item then
  129.                 if string.match(item.name, "coal") then
  130.                     fuelBarrel = 2
  131.                 elseif string.match(item.name, "torch") then
  132.                     torchBarrel = 2
  133.                 elseif string.match(item.name, "stone") then
  134.                     cobbleBarrel = 2
  135.                 else
  136.                     print("Missing coal, torch or stone in barrel")
  137.                     return false
  138.                 end
  139.             else
  140.                 print("Missing coal, torch or stone in barrel")
  141.                 return false
  142.             end
  143.            
  144.         else
  145.             print("Missing coal, torch or stone in barrel")
  146.             return false
  147.         end
  148.     else
  149.         print("Missing coal, torch or stone in barrel")
  150.         return false
  151.     end
  152.  
  153.     turtle.up()
  154.     lookingAt, itemData = turtle.inspect()
  155.  
  156.     if lookingAt then
  157.         if string.match(itemData.name, "barrel") then
  158.             print("found barrel")
  159.             turtle.select(1)
  160.             turtle.suck()
  161.             item = turtle.getItemDetail()
  162.             turtle.drop()
  163.             if item then
  164.                 if string.match(item.name, "coal") then
  165.                     fuelBarrel = 3
  166.                 elseif string.match(item.name, "torch") then
  167.                     torchBarrel = 3
  168.                 elseif string.match(item.name, "stone") then
  169.                     cobbleBarrel = 3
  170.                 else
  171.                     print("Missing coal, torch or stone in barrel")
  172.                     return false
  173.                 end
  174.             else
  175.                 print("Missing coal, torch or stone in barrel")
  176.                 return false
  177.             end
  178.            
  179.         else
  180.             print("Missing coal, torch or stone in barrel")
  181.             return false
  182.         end
  183.     else
  184.         print("Missing coal, torch or stone in barrel")
  185.         return false
  186.     end
  187.  
  188.     turtle.down()
  189.     turtle.down()
  190.     setDirection(EAST)
  191.     turtle.forward()
  192.     setDirection(NORTH)
  193.     return true
  194.  
  195. end
  196.  
  197. function setDirection(direction)
  198.     local turnAmount = currentFace - direction
  199.     if turnAmount == -1 or turnAmount == 3 then
  200.         turtle.turnRight()
  201.     end
  202.     if turnAmount == 2 or turnAmount == -2 then
  203.         turtle.turnRight()
  204.         turtle.turnRight()
  205.     end
  206.     if turnAmount == 1 or turnAmount == -3 then
  207.         turtle.turnLeft()
  208.     end
  209.     currentFace = direction
  210. end
  211.  
  212. function walkToStart()
  213.     setting.lastY = currentY
  214.     setting.lastX = currentX
  215.     setting.lastZ = currentZ
  216.     WalkToPoint(startY, startX, startZ)
  217.     setDirection(NORTH)
  218. end
  219.  
  220. function startNewCave()
  221.     WalkToPoint(2, startX, setting.currentCaveMiddle)
  222.     setDirection(WEST)
  223.     WalkDirectionForce(WEST)
  224.     WalkDirectionForce(WEST)
  225.     turtle.digUp()
  226.     turtle.digDown()
  227.     WalkDirectionForce(WEST)
  228.     turtle.digUp()
  229.     turtle.digDown()
  230.     WalkDirectionForce(WEST)
  231.     turtle.digUp()
  232.     turtle.digDown()
  233.  
  234.     WalkDirectionForce(WEST)
  235.     turtle.digUp()
  236.     turtle.digDown()
  237.     WalkDirectionForce(EAST)
  238.     setting.currentCaveMiddle = currentZ
  239.     print(setting.currentCaveMiddle)
  240.  
  241.     walkToStart()
  242.     setting.lastY = 2
  243.     setting.lastX = startX
  244.     setting.lastZ = setting.currentCaveMiddle
  245.     setting.tunnelLength = 0
  246.     setting.lastTorching = 0
  247.     setting.WallEntryWidth = 3
  248. end
  249.  
  250. function WalkToPoint(height, length, width)
  251.     print("walking to:", height, length, width)
  252.     print("walking from:", currentY, currentX, currentZ)
  253.  
  254.     if currentX > length then
  255.         while currentX ~= length do
  256.             print("solving lenght")
  257.    
  258.             if currentX < length then
  259.                 setDirection(SOUTH)
  260.                 turtle.forward()
  261.                 currentX = currentX + 1
  262.             else
  263.                 setDirection(NORTH)
  264.                 turtle.forward()
  265.                 currentX = currentX - 1
  266.             end
  267.            
  268.         end
  269.         while currentZ ~= width do
  270.             print("solving width")
  271.             if currentZ < width then
  272.                 setDirection(WEST)
  273.                 turtle.forward()
  274.                 currentZ = currentZ + 1
  275.             else
  276.                 setDirection(EAST)
  277.                 turtle.forward()
  278.                 currentZ = currentZ - 1
  279.             end
  280.            
  281.         end
  282.  
  283.         while currentY ~= height do
  284.             print("solving height")
  285.  
  286.             if currentY < height then
  287.                 turtle.up()
  288.                 currentY = currentY + 1
  289.             else
  290.                 turtle.down()
  291.                 currentY = currentY - 1
  292.             end
  293.            
  294.         end
  295.     else
  296.         while currentZ ~= width do
  297.             print("solving width")
  298.             if currentZ < width then
  299.                 setDirection(WEST)
  300.                 turtle.forward()
  301.                 currentZ = currentZ + 1
  302.             else
  303.                 setDirection(EAST)
  304.                 turtle.forward()
  305.                 currentZ = currentZ - 1
  306.             end
  307.            
  308.         end
  309.         while currentY ~= height do
  310.             print("solving height")
  311.  
  312.             if currentY < height then
  313.                 turtle.up()
  314.                 currentY = currentY + 1
  315.             else
  316.                 turtle.down()
  317.                 currentY = currentY - 1
  318.             end
  319.            
  320.         end
  321.         while currentX ~= length do
  322.             print("solving lenght")
  323.    
  324.             if currentX < length then
  325.                 setDirection(SOUTH)
  326.                 turtle.forward()
  327.                 currentX = currentX + 1
  328.             else
  329.                 setDirection(NORTH)
  330.                 turtle.forward()
  331.                 currentX = currentX - 1
  332.             end
  333.            
  334.         end
  335.  
  336.  
  337.  
  338.     end
  339.  
  340.  
  341.  
  342. end
  343.  
  344. function setupInventory()
  345.     WalkToPoint(torchBarrel, startX, 3)
  346.     setDirection(NORTH)
  347.     turtle.select(1)
  348.     turtle.suck()
  349.     turtle.select(firstItem)
  350.     WalkToPoint(cobbleBarrel, startX, 3)
  351.     turtle.select(2)
  352.     turtle.suck()
  353.     turtle.select(firstItem)
  354.     WalkToPoint(startZ, startX, startZ)
  355.     setDirection(NORTH)
  356. end
  357.  
  358. function IsBlockInFront()
  359.     return turtle.detect()
  360. end
  361.  
  362. function IsBlockOver()
  363.     return turtle.detectUp()
  364. end
  365. function IsBlockUnder()
  366.     return turtle.detectDown()
  367. end
  368.  
  369. function isInventoryFull()
  370.     turtle.select(lastItem)
  371.     if turtle.getItemCount() ~= 0 then
  372.         turtle.select(firstItem)
  373.         return true
  374.     end
  375.     turtle.select(firstItem)
  376.     return false
  377. end
  378.  
  379. function WalkDirectionForce(direction)
  380.     setDirection(direction)
  381.     while IsBlockInFront() do
  382.         turtle.dig()
  383.     end
  384.     turtle.forward()
  385.    
  386.     if direction == NORTH then
  387.         currentX = currentX - 1
  388.     end
  389.     if direction == SOUTH then
  390.         currentX = currentX + 1
  391.     end
  392.     if direction == EAST then
  393.         currentZ = currentZ - 1
  394.     end
  395.     if direction == WEST then
  396.         currentZ = currentZ + 1
  397.     end
  398.    
  399. end
  400.  
  401. function mine()
  402.     if setting.tunnelLength > 6 then
  403.         walkToStart()
  404.         currentStage = 5
  405.     end
  406.     if isInventoryFull() then
  407.         walkToStart()
  408.         currentStage = 4
  409.     end
  410.  
  411.     print("WallScore:", setting.WallScore)
  412.     local walkTo = 1
  413.     if setting.WallEntryWidth == 3 then
  414.         local walkTo = 1
  415.     else
  416.         walkTo = 3
  417.     end
  418.  
  419.     if setting.WallScore >= 7 then
  420.         if IsBlockOver() then
  421.             turtle.digUp()
  422.         end
  423.         setting.WallScore = setting.WallScore + 1
  424.    
  425.         if IsBlockUnder() then
  426.             turtle.digDown()
  427.         end
  428.         setting.WallScore = setting.WallScore + 1
  429.         WalkDirectionForce(SOUTH)
  430.         setting.WallScore = 1
  431.         setting.tunnelLength = setting.tunnelLength + 1
  432.         setting.WallEntryWidth = walkTo
  433.         return
  434.     end
  435.  
  436.  
  437.  
  438.  
  439.     if IsBlockOver() then
  440.         turtle.digUp()
  441.     end
  442.     setting.WallScore = setting.WallScore + 1
  443.  
  444.     if IsBlockUnder() then
  445.         turtle.digDown()
  446.     end
  447.     setting.WallScore = setting.WallScore + 1
  448.     if currentZ - (setting.currentCaveMiddle - 2) == 2 and setting.lastTorching + 5 < setting.tunnelLength then
  449.         turtle.down()
  450.         if not turtle.detectDown() then
  451.             select(2)
  452.             turtle.placeDown()
  453.         end
  454.         turtle.up()
  455.         turtle.select(1)
  456.         turtle.placeDown()
  457.         setting.lastTorching = setting.tunnelLength
  458.         turtle.select(firstItem)
  459.  
  460.     end
  461.     if(walkTo == 1) then
  462.         WalkDirectionForce(EAST)
  463.     else
  464.         WalkDirectionForce(WEST)
  465.     end
  466.  
  467.     setting.WallScore = setting.WallScore + 1
  468.  
  469.  
  470. end
  471.  
  472. function refuel()
  473.     walkToStart()
  474.     WalkToPoint(fuelBarrel, startX, 3)
  475.     turtle.select(1)
  476.     setDirection(NORTH)
  477.     local hasNotRefueled = true
  478.  
  479.     while hasNotRefueled do
  480.         turtle.suck()
  481.         if turtle.getItemDetail() then
  482.             turtle.refuel()
  483.             hasNotRefueled = false
  484.         else
  485.             if not turtle.getItemDetail() then
  486.                 print("No fuel found")
  487.                 sleep(20)
  488.             end
  489.         end
  490.     end
  491.     currentStage = 4
  492.  
  493.  
  494. end
  495.  
  496. function updateConfig()
  497.     --loadedConfig = fs.open(CONFIG_FILE, "w")
  498.     --loadedConfig.write(textutils.serialize(setting))
  499.     --loadedConfig.close()
  500. end
  501.  
  502. function Main()
  503.  
  504.     if fs.exists(CONFIG_FILE) then
  505.         --loadedConfig = fs.open(CONFIG_FILE, "r")
  506.         --setting = textutils.unserialize(loadedConfig.readAll())
  507.         --loadedConfig.close()
  508.     else
  509.         updateConfig()
  510.     end
  511.     print(textutils.serialize(setting))
  512.     if checkAndReset() then
  513.         print("All good")
  514.     else
  515.         print("Fix the setup and restart")
  516.         exit()
  517.     end
  518.  
  519.  
  520.  
  521.     while true do
  522.         if turtle.getFuelLevel() <= currentX * 2 then
  523.             refuel()
  524.         end
  525.         updateConfig()
  526.  
  527.         if currentStage == 1 then
  528.             setupInventory()
  529.             currentStage = currentStage + 1
  530.         end
  531.  
  532.         if currentStage == 2 then
  533.             WalkToPoint(setting.lastY, setting.lastX, setting.lastZ)
  534.             currentStage = currentStage + 1
  535.         end
  536.  
  537.         if currentStage == 3 then -- MINING
  538.             mine()
  539.         end
  540.  
  541.         if currentStage == 4 then -- BANKING
  542.             placeAll()
  543.             WalkToPoint(setting.lastY, setting.lastX, setting.lastZ)
  544.             currentStage = 3
  545.         end
  546.         if currentStage == 5 then -- Create new tunnel
  547.             startNewCave()
  548.             currentStage = 4
  549.         end
  550.  
  551.     end
  552.  
  553. end
  554.  
  555.  
  556. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement