Advertisement
z1haze

mine3.lua

Feb 12th, 2025 (edited)
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.58 KB | None | 0 0
  1. if not turtle then
  2.     error("Turtle required!")
  3. end
  4.  
  5. write("Miner initializing")
  6. textutils.slowPrint("...", 5)
  7.  
  8. local DEBUG = true
  9.  
  10. local resume = not DEBUG and fs.exists(fs.combine("database", shell.getRunningProgram(), "state"))
  11. local running = false
  12.  
  13. local Aware2 = require("Aware2")
  14. local aware = Aware2.new()
  15.  
  16. local Miner2 = require("Miner2")
  17. local miner = Miner2.new(aware)
  18.  
  19. function setup()
  20.     local branchCount, branchLength, branchGap, verticalGap, startX, startY, startZ, facing, minY, maxY
  21.  
  22.     if DEBUG then
  23.         branchCount = 16
  24.         branchLength = 16
  25.         branchGap = 0
  26.         verticalGap = 1
  27.         startX = 1071
  28.         startY = 65
  29.         startZ = -33
  30.         facing = 4
  31.         minY = 16
  32.         maxY = 20
  33.     else
  34.         while branchCount == nil do
  35.             print("");
  36.             print("How many branches should be mined?")
  37.  
  38.             local input = read();
  39.             branchCount = tonumber(input)
  40.  
  41.             if branchCount == nil then
  42.                 print("'" .. input .. "' should be a number")
  43.             end
  44.         end
  45.  
  46.         while branchLength == nil do
  47.             print("");
  48.             print("How long should each branch be?")
  49.  
  50.             local input = read();
  51.             branchLength = tonumber(input)
  52.  
  53.             if branchLength == nil then
  54.                 print("'" .. input .. "' should be a number")
  55.             end
  56.         end
  57.  
  58.         if branchCount > 1 then
  59.             while branchGap == nil do
  60.                 print("");
  61.                 print("How many block gap should there be between branches?")
  62.  
  63.                 local input = read();
  64.                 branchGap = tonumber(input)
  65.  
  66.                 if branchGap == nil then
  67.                     print("'" .. input .. "' should be a number")
  68.                 end
  69.             end
  70.         end
  71.  
  72.         while verticalGap == nil do
  73.             print("");
  74.             print("How much space between floors?")
  75.  
  76.             local input = read();
  77.             verticalGap = tonumber(input)
  78.  
  79.             if verticalGap == nil then
  80.                 print("'" .. input .. "' should be a number")
  81.             end
  82.         end
  83.  
  84.         if not miner.aware.state.hasGPS then
  85.             while startX == nil do
  86.                 print("");
  87.                 print("What is the startX of the turtle?")
  88.  
  89.                 local input = read();
  90.                 startX = tonumber(input)
  91.  
  92.                 if startX == nil then
  93.                     print("'" .. input .. "' should be a number")
  94.                 end
  95.             end
  96.  
  97.             while startY == nil do
  98.                 print("");
  99.                 print("What is the startY of the turtle?")
  100.  
  101.                 local input = read();
  102.                 startY = tonumber(input)
  103.  
  104.                 if startY == nil then
  105.                     print("'" .. input .. "' should be a number")
  106.                 end
  107.             end
  108.  
  109.             while startZ == nil do
  110.                 print("");
  111.                 print("What is the startZ of the turtle?")
  112.  
  113.                 local input = read();
  114.                 startZ = tonumber(input)
  115.  
  116.                 if startZ == nil then
  117.                     print("'" .. input .. "' should be a number")
  118.                 end
  119.             end
  120.         end
  121.  
  122.         while facing == nil do
  123.             print("");
  124.             print("What is the facing? 1=north, 2=east, 3=south, 4=west")
  125.  
  126.             local input = read();
  127.             facing = tonumber(input)
  128.  
  129.             if facing == nil then
  130.                 print("'" .. input .. "' should be a number")
  131.             end
  132.         end
  133.  
  134.         while minY == nil do
  135.             print("");
  136.             print("What is the minY?")
  137.  
  138.             local input = read();
  139.             minY = tonumber(input)
  140.  
  141.             if minY == nil then
  142.                 print("'" .. input .. "' should be a number")
  143.             end
  144.         end
  145.  
  146.         while maxY == nil do
  147.             print("");
  148.             print("What is the maxY?")
  149.  
  150.             local input = read();
  151.             maxY = tonumber(input)
  152.  
  153.             if maxY == nil then
  154.                 print("'" .. input .. "' should be a number")
  155.             end
  156.         end
  157.     end
  158.  
  159.     miner.aware.state.pos.x = startX
  160.     miner.aware.state.pos.y = startY
  161.     miner.aware.state.pos.z = startZ
  162.     miner.aware.state.pos.f = facing
  163.  
  164.     miner.aware:setHome(miner.aware.state.pos)
  165.  
  166.     miner.aware.state.branchCount = branchCount
  167.     miner.aware.state.branchLength = branchLength
  168.     miner.aware.state.branchGap = branchGap
  169.     miner.aware.state.verticalGap = verticalGap
  170.     miner.aware.state.minY = minY
  171.     miner.aware.state.maxY = maxY
  172.  
  173.     -- set initial target y level
  174.     miner.aware.state.yLevel = minY
  175.  
  176.     -- setup the GUI frame
  177.     miner:guiFrame()
  178.  
  179.     running = true
  180.  
  181.     miner.aware:saveState(miner.aware.state)
  182. end
  183.  
  184. function main()
  185.     if not resume then
  186.         setup()
  187.     end
  188.  
  189.     running = true
  190.  
  191.     miner:useFuel(1000)
  192.  
  193.     if not miner.aware:equip("minecraft:diamond_pickaxe", "right") then
  194.         error()
  195.     end
  196.  
  197.     miner:setCurrentAction("descend")
  198.  
  199.     -- descend to target y level
  200.     miner.aware:moveToY(miner.aware.state.yLevel, true)
  201.  
  202.     local keepGoing = true
  203.  
  204.     -- as long as we're at or below the max y level, we branch mine
  205.     while keepGoing do
  206.         miner:setCurrentBranch(1)
  207.  
  208.         -- execute each branch mine for the current y level
  209.         for i = (miner.aware.state.currentBranch), miner.aware.state.branchCount do
  210.             local isEvenBranch = i % 2 == 0
  211.  
  212.             miner:setCurrentBranch(i)
  213.  
  214.             miner:branchMine({
  215.                 f = isEvenBranch and 4 or 2, -- face right for odd branches, face left for even ones, because we're coming back the other direction
  216.                 l = miner.aware.state.branchLength - 1,
  217.                 b = 1,
  218.                 left = false,
  219.                 right = false,
  220.                 up = false
  221.             })
  222.  
  223.             -- get in position for the next branch
  224.             if i < miner.aware.state.branchCount then
  225.                 miner:turnTo(1)
  226.  
  227.                 for _ = 1, miner.aware.state.branchGap + 1 do
  228.                     miner:dig()
  229.                     miner:move()
  230.                 end
  231.             end
  232.         end
  233.  
  234.         -- go back to the vertical shaft
  235.         miner:goTo(
  236.                 {
  237.                     x = miner.aware.state.home.x,
  238.                     y = miner.aware.state.pos.y,
  239.                     z = miner.aware.state.home.z,
  240.                     f = 1
  241.                 },
  242.                 true,
  243.                 (miner.aware.state.home.f == 1 or miner.aware.state.home.f == 3) and "xzy" or "zxy"
  244.         )
  245.  
  246.         local nextY = miner.aware.state.pos.y + miner.aware.state.verticalGap + 1
  247.  
  248.         if nextY >= miner.aware.state.home.y or nextY > miner.aware.state.maxY then -- no more floors to mine out
  249.             keepGoing = false
  250.         else -- move up the shaft to the next level to branch mine
  251.             miner.aware.state.yLevel = nextY
  252.             miner.aware:saveState(miner.aware.state)
  253.             miner.aware:moveToY(miner.aware.state.yLevel)
  254.         end
  255.     end
  256.  
  257.     miner:setCurrentAction("home")
  258.     miner.aware:goHome((miner.aware.state.home.f == 1 or miner.aware.state.home.f == 3) and "xzy" or "zxy", true)
  259.     miner:unload("up")
  260.     miner:setCurrentAction("done")
  261.     miner.aware:deleteState()
  262. end
  263.  
  264. function listen()
  265.     while true do
  266.         local event = os.pullEvent()
  267.  
  268.         if event == "stateSaved" and running then
  269.             miner:guiStats()
  270.         end
  271.     end
  272. end
  273.  
  274. parallel.waitForAny(main, listen)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement