z1haze

mine3.lua

Feb 12th, 2025 (edited)
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.90 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 = -1667
  28.         startY = 63
  29.         startZ = 592
  30.         facing = 3
  31.         minY = 16
  32.         maxY = 24
  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.         while startX == nil do
  85.             print("");
  86.             print("What is the startX of the turtle?")
  87.  
  88.             local input = read();
  89.             startX = tonumber(input)
  90.  
  91.             if startX == nil then
  92.                 print("'" .. input .. "' should be a number")
  93.             end
  94.         end
  95.  
  96.         while startY == nil do
  97.             print("");
  98.             print("What is the startY of the turtle?")
  99.  
  100.             local input = read();
  101.             startY = tonumber(input)
  102.  
  103.             if startY == nil then
  104.                 print("'" .. input .. "' should be a number")
  105.             end
  106.         end
  107.  
  108.         while startZ == nil do
  109.             print("");
  110.             print("What is the startZ of the turtle?")
  111.  
  112.             local input = read();
  113.             startZ = tonumber(input)
  114.  
  115.             if startZ == nil then
  116.                 print("'" .. input .. "' should be a number")
  117.             end
  118.         end
  119.  
  120.         while facing == nil do
  121.             print("");
  122.             print("What is the facing? 1=north, 2=east, 3=south, 4=west")
  123.  
  124.             local input = read();
  125.             facing = tonumber(input)
  126.  
  127.             if facing == nil then
  128.                 print("'" .. input .. "' should be a number")
  129.             end
  130.         end
  131.  
  132.         while minY == nil do
  133.             print("");
  134.             print("What is the minY?")
  135.  
  136.             local input = read();
  137.             minY = tonumber(input)
  138.  
  139.             if minY == nil then
  140.                 print("'" .. input .. "' should be a number")
  141.             end
  142.         end
  143.  
  144.         while maxY == nil do
  145.             print("");
  146.             print("What is the maxY?")
  147.  
  148.             local input = read();
  149.             maxY = tonumber(input)
  150.  
  151.             if maxY == nil then
  152.                 print("'" .. input .. "' should be a number")
  153.             end
  154.         end
  155.     end
  156.  
  157.     miner.aware.state.pos.x = startX
  158.     miner.aware.state.pos.y = startY
  159.     miner.aware.state.pos.z = startZ
  160.     miner.aware.state.pos.f = facing
  161.  
  162.     miner.aware:setHome(miner.aware.state.pos)
  163.  
  164.     miner.aware.state.branchCount = branchCount
  165.     miner.aware.state.branchLength = branchLength
  166.     miner.aware.state.branchGap = branchGap
  167.     miner.aware.state.verticalGap = verticalGap
  168.     miner.aware.state.minY = minY
  169.     miner.aware.state.maxY = maxY
  170.  
  171.     -- set initial target y level
  172.     miner.aware.state.yLevel = minY
  173.  
  174.     -- setup the GUI frame
  175.     miner:guiFrame()
  176.  
  177.     running = true
  178.  
  179.     miner.aware:saveState(miner.aware.state)
  180. end
  181.  
  182. function main()
  183.     if not resume then
  184.         setup()
  185.     end
  186.  
  187.     running = true
  188.  
  189.     miner:useFuel(1000)
  190.  
  191.     if not miner.aware:equip("minecraft:diamond_pickaxe", "right") then
  192.         error()
  193.     end
  194.  
  195.     miner:setCurrentAction("descend")
  196.  
  197.     -- descend to target y level
  198.     miner:moveTo(
  199.             {
  200.                 x = miner.aware.state.pos.x,
  201.                 y = miner.aware.state.yLevel,
  202.                 z = miner.aware.state.pos.z,
  203.                 f = miner.aware.state.pos.f
  204.             },
  205.             true,
  206.             "yxz",
  207.             true
  208.     )
  209.  
  210.     local keepGoing = true
  211.  
  212.     -- as long as we're at or below the max y level, we branch mine
  213.     while keepGoing do
  214.         miner:setCurrentBranch(1)
  215.  
  216.         -- execute each branch mine for the current y level
  217.         for i = miner.aware.state.currentBranch, miner.aware.state.branchCount do
  218.             local isEvenBranch = i % 2 == 0
  219.  
  220.             miner:setCurrentBranch(i)
  221.  
  222.             miner:branchMine({
  223.                 f = isEvenBranch and 4 or 2, -- face right for odd branches, face left for even ones, because we're coming back the other direction
  224.                 l = miner.aware.state.branchLength - 1,
  225.                 b = 1,
  226.                 left = false,
  227.                 right = false,
  228.                 up = false
  229.             })
  230.  
  231.             -- if we just finished an even branch,we can safely unroll the last 2 branches.
  232.             -- we did this in case we need a pitstop midway through an even branch.
  233.             -- this way we can find our path back to the shaft without breaking any new blocks, e.g. we will follow the zag and zig
  234.             -- in the even of odd branches, its a straight unobstructed path back to the shaft
  235.             if isEvenBranch then
  236.                 -- remove 2 branches worth of movements
  237.                 local toBeRemoved = (miner.aware.state.branchLength - 1) * 2;
  238.  
  239.                 -- remove the odd branches additional lateral movement into the current branch
  240.                 toBeRemoved = toBeRemoved + miner.aware.state.branchGap + 1
  241.  
  242.                 miner.aware:removeCheckpoints(toBeRemoved)
  243.             end
  244.  
  245.             -- get in position for the next branch
  246.             if i < miner.aware.state.branchCount then
  247.                 miner:turnTo(1)
  248.  
  249.                 for _ = 1, miner.aware.state.branchGap + 1 do
  250.                     miner:dig()
  251.                     miner:move()
  252.                 end
  253.  
  254.  
  255.             end
  256.         end
  257.  
  258.         -- go back to the vertical shaft
  259.         miner:moveTo(
  260.                 {
  261.                     x = miner.aware.state.home.x,
  262.                     y = miner.aware.state.pos.y,
  263.                     z = miner.aware.state.home.z,
  264.                     f = 1
  265.                 },
  266.                 true,
  267.                 (miner.aware.state.home.f == 1 or miner.aware.state.home.f == 3) and "xzy" or "zxy",
  268.                 true
  269.         )
  270.  
  271.         local nextY = miner.aware.state.pos.y + miner.aware.state.verticalGap + 1
  272.  
  273.         if nextY >= miner.aware.state.home.y or nextY > miner.aware.state.maxY then
  274.             -- we've reached our max ceiling
  275.             keepGoing = false
  276.         else
  277.             -- move up the shaft to the next level to branch mine
  278.             miner.aware.state.yLevel = nextY
  279.             miner.aware:saveState(miner.aware.state)
  280.             miner:moveTo(
  281.                     {
  282.                         x = miner.aware.state.pos.x,
  283.                         y = miner.aware.state.yLevel,
  284.                         z = miner.aware.state.pos.z,
  285.                         f = miner.aware.state.pos.f
  286.                     },
  287.                     false,
  288.                     true
  289.             )
  290.         end
  291.     end
  292.  
  293.     miner:setCurrentAction("home")
  294.     miner.aware:goHome((miner.aware.state.home.f == 1 or miner.aware.state.home.f == 3) and "xzy" or "zxy", true)
  295.     miner:unload("up")
  296.     miner:setCurrentAction("done")
  297.     miner.aware:deleteState()
  298. end
  299.  
  300. function listen()
  301.     while true do
  302.         local event = os.pullEvent()
  303.  
  304.         if event == "stateSaved" and running then
  305.             miner:guiStats()
  306.         end
  307.  
  308.         if event == "moved" then
  309.             miner.aware:addCheckpoint()
  310.         end
  311.     end
  312. end
  313.  
  314. parallel.waitForAny(main, listen)
Advertisement
Add Comment
Please, Sign In to add comment