Advertisement
Sedrowow

FancyQuarry

May 1st, 2025 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.84 KB | None | 0 0
  1. -- This is a replacement for the
  2. -- 'excavate' program, as it can re-
  3. -- cover from a reboot/unload event.
  4. -- Also avoids destroying spawners!
  5.  
  6. -----------------------------------
  7. -- [¯¯] || || |¯\ [¯¯] ||   |¯¯] --
  8. --  ||  ||_|| | /  ||  ||_  | ]  --
  9. --  ||   \__| | \  ||  |__] |__] --
  10. -----------------------------------
  11. --  /¯\  || ||  /\  |¯\ |¯\ \\// --
  12. -- | O | ||_|| |  | | / | /  \/  --
  13. --  \_\\  \__| |||| | \ | \  ||  --
  14. -----------------------------------
  15.  
  16. os.loadAPI("flex.lua")
  17. os.loadAPI("dig.lua")
  18. dig.doBlacklist() -- Avoid Protected Blocks
  19. dig.doAttack() -- Attack entities that block the way
  20. dig.setFuelSlot(1)
  21. dig.setBlockSlot(2)
  22. local world_height = 384
  23.  
  24.  
  25. local args = {...}
  26. if #args == 0 then
  27.  flex.printColors(
  28.    "quarry <length> [width] [depth]\n"..
  29.    "[skip <layers>] [dump] [nolava] [nether]",
  30.    colors.lightBlue)
  31.  return
  32. end --if
  33.  
  34.  
  35. local reloaded = false
  36. if dig.saveExists() then
  37.  reloaded = true
  38.  dig.loadCoords()
  39. end --if
  40. dig.makeStartup("quarry",args)
  41.  
  42.  
  43. local zmax = tonumber(args[1])
  44. local xmax = tonumber(args[2]) or zmax
  45. local depth = world_height-1
  46. if tonumber(args[2]) ~= nil then
  47.  depth = tonumber(args[3]) or depth
  48. end --if
  49. local ymin = -depth --(1-depth)
  50.  
  51. if xmax == nil or zmax == nil then
  52.  flex.send("Invalid dimensions,",colors.red)
  53.  shell.run("rm startup.lua")
  54.  return
  55. end --if
  56.  
  57.  
  58. local x
  59. local skip = 0
  60. local lava = true
  61. local dodumps = false
  62.  
  63. for x=1,#args do
  64.  
  65.  if args[x] == "dump" then
  66.   dodumps = true
  67.  elseif args[x] == "nolava" then
  68.   lava = false
  69.  elseif args[x] == "nether" then
  70.   dig.setBlockStacks(4)
  71.  end --if
  72.  
  73.  if args[x] == "skip" then
  74.   skip = tonumber(args[x+1])
  75.   if skip == nil then
  76.    flex.printColors("Please specify skip depth",
  77.      colors.red)
  78.    dig.saveClear()
  79.    return
  80.   end --if
  81.   if dig.getymin() > -skip then
  82.    dig.setymin(-skip)
  83.   end --if
  84.  end --if
  85.  
  86. end --for
  87.  
  88.  
  89. if not lava then -- Block lava around edges of quarry
  90.  dig.setBlockSlot(0)
  91.  -- Always keep a stack of blocks
  92. end --if
  93.  
  94.  
  95.  
  96.  
  97. ----------------------------------------------
  98. -- |¯¯]|| |||\ || /¯][¯¯][¯¯] /¯\ |\ ||/¯¯\ --
  99. -- | ] ||_||| \ || [  ||  ][ | O || \ |\_¯\ --
  100. -- ||   \__||| \| \_] || [__] \_/ || \|\__/ --
  101. ----------------------------------------------
  102.  
  103. local location
  104. local function gotoBase()
  105.  local x = dig.getxlast()
  106.  location = dig.location()
  107.  if dig.gety() < -skip then dig.up() end
  108.  dig.gotox(0)
  109.  dig.gotoz(0)
  110.  dig.gotor(180)
  111.  dig.gotoy(0)
  112.  dig.gotox(0)
  113.  dig.setxlast(x)
  114.  dig.gotoz(0)
  115.  dig.gotor(180)
  116.  return location
  117. end --function
  118.  
  119. local function returnFromBase(loc)
  120.  local loc = loc or location
  121.  local x = dig.getxlast()
  122.  dig.gotor(0)
  123.  checkFuel()
  124.  dig.gotoy(math.min(loc[2]+1,-skip))
  125.  checkFuel()
  126.  dig.gotoz(loc[3])
  127.  checkFuel()
  128.  dig.gotox(loc[1])
  129.  dig.setxlast(x) -- Important for restoring
  130.  checkFuel()
  131.  dig.gotor(loc[4])
  132.  checkFuel()
  133.  dig.gotoy(loc[2])
  134. end --function
  135.  
  136.  
  137. -- print("checkhalt") -- Debug print removed
  138. local function checkHalt()
  139.  -- Remote control halt via modem is removed
  140.  -- Check for redstone signal from above
  141.  if not rs.getInput("top") then
  142.   return
  143.  end --if
  144.  if dig.gety() == 0 then
  145.   return
  146.  end --if
  147.  
  148.  local loc,x
  149.  -- Manual halt; redstone signal from above
  150.  flex.send("Manual halt initiated (Redstone)", colors.orange)
  151.  flex.printColors("Press ENTER to resume mining\n"
  152.    .."or SPACE to return to base",
  153.    colors.pink)
  154.  
  155.  while true do
  156.   x = flex.getKey()
  157.   if x == keys.enter then return end
  158.   if x == keys.space then break end
  159.  end --while
  160.  
  161.  flex.send("Returning to base", colors.yellow)
  162.  loc = gotoBase()
  163.  print(" ")
  164.  flex.printColors("Press ENTER to resume mining",
  165.    colors.pink)
  166.  while flex.getKey() ~= keys.enter do
  167.   sleep(1)
  168.  end --while
  169.  
  170.  if dodumps then dig.doDumpDown() end
  171.  dig.dropNotFuel()
  172.  flex.send("Resuming quarry",colors.yellow)
  173.  returnFromBase(loc)
  174.  
  175. end --function
  176.  
  177.  
  178. -- print("checkinv") -- Debug print removed
  179. local function checkInv()
  180.  if turtle.getItemCount(16) > 0 then
  181.  
  182.   if dodumps then
  183.    dig.right(2)
  184.    dig.doDump()
  185.    dig.left(2)
  186.   end --if
  187.  
  188.   if turtle.getItemCount(14) > 0 then
  189.    local loc = gotoBase()
  190.    dig.dropNotFuel()
  191.    returnFromBase(loc)
  192.   end --if
  193.  
  194.  end --if
  195. end --function
  196.  
  197.  
  198. -- print("checkfuel") -- Debug print removed
  199. function checkFuel()
  200.  local a = turtle.getFuelLevel()
  201.  local b = ( zmax + xmax + depth + 1 )*2 -- Original basic fuel estimate
  202.  local c = true
  203.  
  204.  -- More detailed fuel estimate based on remaining blocks
  205.  local total_quarry_blocks = xmax * zmax * (dig.getymax() - dig.getymin() + 1)
  206.  local current_dug_blocks = dig.getdug()
  207.  local estimated_remaining_blocks = total_quarry_blocks - current_dug_blocks
  208.  -- Rough estimate of fuel needed per block (adjust based on your setup)
  209.  local fuel_per_block = 0.05 -- Example: needs calibration
  210.  local estimated_fuel_needed = estimated_remaining_blocks * fuel_per_block
  211.  
  212.  -- Use a more robust fuel check based on estimated needs
  213.  if a < estimated_fuel_needed * 1.2 then -- Check if current fuel is less than 120% of estimated needed
  214.      flex.send("Fuel low (Estimated needed: "..tostring(math.ceil(estimated_fuel_needed)).."), returning to surface", colors.yellow)
  215.      local loc = gotoBase()
  216.      turtle.select(1)
  217.      if dodumps then dig.doDumpDown() end
  218.      while turtle.suckUp() do sleep(0) end
  219.      dig.dropNotFuel()
  220.      dig.refuel(estimated_fuel_needed * 1.5) -- Refuel to 150% of estimated needed
  221.      flex.send("Fuel acquired! ("..tostring(turtle.getFuelLevel()).." fuel)", colors.lightBlue)
  222.      returnFromBase(loc)
  223.  end
  224.  
  225.  
  226.  -- Original fuel check logic (can keep or replace with the above)
  227.  --while a < b and c do
  228.  -- for x=1,16 do
  229.  --  turtle.select(x)
  230.  --  if turtle.refuel(1) then
  231.  --   break
  232.  --  end --if
  233.  --  if x == 16 then
  234.  --   c = false
  235.  --  end --if
  236.  -- end --for
  237.  -- a = turtle.getFuelLevel()
  238.  --end --while
  239.  
  240.  --if a < b then
  241.  -- flex.send("Fuel low, returning to surface",
  242.  --   colors.yellow)
  243.  -- local loc = gotoBase()
  244.  -- turtle.select(1)
  245.  -- if dodumps then dig.doDumpDown() end
  246.  -- while turtle.suckUp() do sleep(0) end
  247.  -- dig.dropNotFuel()
  248.  -- dig.refuel(b)
  249.  -- flex.send("Fuel aquired!",colors.lightBlue)
  250.  -- returnFromBase(loc)
  251.  --end --if
  252. end --function
  253.  
  254.  
  255.  
  256. local dug = dig.getdug()
  257. local ydeep = dig.getymin()
  258. local function checkProgress()
  259.  local a = 1000 --report every <a> blocks dug
  260.  local b = 5 --report every <b> meters descended
  261.  
  262.  -- Print detailed progress information
  263.  term.setCursorPos(1,1)
  264.  term.clearLine()
  265.  flex.printColors("Pos: X="..tostring(dig.getx())..
  266.                     ", Y="..tostring(dig.gety())..
  267.                     ", Z="..tostring(dig.getz())..
  268.                     ", Rot="..tostring(dig.getr()%360), colors.white)
  269.  
  270.  term.setCursorPos(1,2)
  271.  term.clearLine()
  272.  flex.printColors("Fuel: "..tostring(turtle.getFuelLevel()), colors.orange)
  273.  
  274.  term.setCursorPos(1,3)
  275.  term.clearLine()
  276.  flex.printColors("Dug: "..tostring(dig.getdug()).." blocks", colors.lightBlue)
  277.  
  278.  term.setCursorPos(1,4)
  279.  term.clearLine()
  280.  flex.printColors("Depth: "..tostring(-dig.gety()).."m / "..tostring(-ymin).."m", colors.green)
  281.  
  282.  
  283.  -- Original progress reports (can keep or remove if detailed prints are sufficient)
  284.  --if math.floor(dug/a) < math.floor(dig.getdug()/a) then
  285.  -- flex.send("Dug "..tostring(dig.getdug())..
  286.  --   " blocks",colors.lightBlue)
  287.  --end --if
  288.  --if math.floor(-ydeep/b) < math.floor(-dig.gety()/b) then
  289.  -- flex.send("Descended "..tostring(-dig.gety())..
  290.  --   "m",colors.green)
  291.  --end --if
  292.  
  293.  dug = dig.getdug()
  294.  ydeep = dig.gety()
  295.  -- checkReceivedCommand() -- Removed remote check
  296. end --function
  297.  
  298.  
  299.  
  300. local newlayer = false
  301. function checkNewLayer()
  302.  if newlayer then
  303.   -- This encodes whether or not the turtle has
  304.   --  started a new layer if at the edge
  305.   dig.setr(dig.getr() % 360 + 360)
  306.  else
  307.   dig.setr(dig.getr() % 360)
  308.  end --if
  309. end --function
  310.  
  311.  
  312.  
  313. function lavax()
  314.   if dig.getx() == 0 then
  315.    dig.gotor(270)
  316.    checkNewLayer()
  317.    dig.blockLava()
  318.   elseif dig.getx() == xmax-1 then
  319.    dig.gotor(90)
  320.    checkNewLayer()
  321.    dig.blockLava()
  322.   end --if/else
  323. end --function
  324.  
  325. function lavaz()
  326.   if dig.getz() == 0 then
  327.    dig.gotor(180)
  328.    checkNewLayer()
  329.    dig.blockLava()
  330.   elseif dig.getz() == zmax-1 then
  331.    dig.gotor(0)
  332.    checkNewLayer()
  333.    dig.blockLava()
  334.   end --if/else
  335. end --function
  336.  
  337. function checkLava(n)
  338.  if lava then
  339.   local x
  340.   local r = dig.getr() % 360
  341.  
  342.   if r == 0 or r == 180 then
  343.    lavaz()
  344.    lavax()
  345.   else
  346.    lavax()
  347.    lavaz()
  348.   end --if/else
  349.  
  350.   if dig.gety() == -skip then
  351.    dig.blockLavaUp()
  352.   end --if
  353.  
  354.   if dig.getx() == 0 and dig.getz() == 0
  355.      and dig.gety() > -skip then
  356.    for x=1,4 do
  357.     dig.blockLava()
  358.     dig.left()
  359.     checkNewLayer()
  360.    end --for
  361.   end --if
  362.  
  363.   if n ~= 0 then
  364.    dig.gotor(r)
  365.    checkNewLayer()
  366.   end --if
  367.  
  368.  end --if
  369. end --function
  370.  
  371.  
  372. local function checkAll(n)
  373.  checkNewLayer()
  374.  checkProgress() -- checkProgress now includes detailed prints
  375.  checkFuel()
  376.  checkInv()
  377.  checkHalt()
  378.  checkLava(n)
  379.  dig.checkBlocks()
  380.  checkNewLayer()
  381. end --function
  382.  
  383.  
  384.  
  385. ---------------------------------------
  386. --      |\/|  /\  [¯¯] |\ ||         --
  387. --      |  | |  |  ][  | \ |         --
  388. --      |||| |||| [__] || \|         --
  389. ---------------------------------------
  390. -- |¯\ |¯\  /¯\   /¯¯] |¯\  /\  |\/| --
  391. -- | / | / | O | | [¯| | / |  | |  | --
  392. -- ||  | \  \_/   \__| | \ |||| |||| --
  393. ---------------------------------------
  394.  
  395. local a,b,c,x,y,z,r,loc
  396. local xdir, zdir = 1, 1
  397.  
  398. turtle.select(1)
  399. if reloaded then
  400.  
  401.  flex.send("Resuming "..tostring(zmax).."x"
  402.    ..tostring(xmax).." quarry",colors.yellow)
  403.  
  404.  if dig.gety()==dig.getymin() and dig.gety()~=0 then
  405.   zdir = dig.getzlast()
  406.   if zdir == 0 then zdir = 1 end
  407.   xdir = dig.getxlast()
  408.   if xdir == 0 then xdir = 1 end
  409.  
  410.   if dig.getr() >= 360 then
  411.    -- This encodes whether or not the turtle has
  412.    --  started a new layer if at the edge
  413.    xdir = -xdir
  414.    newlayer = true
  415.   end --if
  416.  
  417.  else
  418.   gotoBase()
  419.   if dodumps then dig.doDumpDown() end
  420.   dig.dropNotFuel()
  421.   dig.gotor(0)
  422.   checkFuel()
  423.   dig.gotoy(math.min(dig.getymin(),-skip)) -- Corrected: go to min y or skip depth
  424.  end --if
  425.  
  426. else
  427.  
  428.  flex.send("Starting "..tostring(zmax).."x"
  429.    ..tostring(xmax).." quarry",colors.yellow)
  430.  
  431.  if skip > 0 then
  432.   flex.send("Skipping "..tostring(skip)
  433.     .."m", colors.lightGray)
  434.  end --if
  435.  
  436.  if depth < world_height-1 then
  437.   flex.send("Going "..tostring(-ymin)
  438.     .."m deep", colors.lightGray)
  439.  else
  440.   flex.send("To bedrock!",colors.lightGray)
  441.  end --if/else
  442.  
  443. end --if/else
  444.  
  445.  
  446. -- Immediately before the descent loop
  447. print("DEBUG: Before descent loop. dig.gety(): " .. tostring(dig.gety()) .. ", -skip: " .. tostring(-skip)) -- Debug print kept
  448. while dig.gety() > -skip do
  449.  checkFuel()
  450.  dig.down()
  451.  
  452.  if dig.isStuck() then
  453.   flex.send("Co-ordinates lost! Shutting down",
  454.     colors.red)
  455.   --rs.delete("startup.lua")
  456.   return
  457.  end --if
  458.  -- No os.pullEvent here anymore
  459.  -- checkReceivedCommand() -- Removed remote check
  460. end --while
  461. print("DEBUG: After descent loop.") -- Debug print kept
  462.  
  463.  
  464. --------------------------
  465. -- |\/|  /\  [¯¯] |\ || --
  466. -- |  | |  |  ][  | \ | --
  467. -- |||| |||| [__] || \| --
  468. --------------------------
  469. -- ||    /¯\   /¯\  |¯\ --
  470. -- ||_  | O | | O | | / --
  471. -- |__]  \_/   \_/  ||  --
  472. --------------------------
  473.  
  474. local done = false
  475. -- Removed DEBUG print before main loop
  476. while not done and not dig.isStuck() do
  477.  -- No os.pullEvent here anymore
  478.  -- checkReceivedCommand() -- Removed remote check
  479.  turtle.select(1)
  480.  -- Removed DEBUG print after turtle.select(1)
  481.  
  482.  -- Removed DEBUG print before inner loop
  483.  while not done do
  484.   -- No os.pullEvent here anymore
  485.   -- checkReceivedCommand() -- Removed remote check
  486.   -- Removed DEBUG print after modem check
  487.  
  488.   checkAll(0) -- checkAll now includes detailed prints
  489.   -- Removed DEBUG print after checkAll(0)
  490.  
  491.   if dig.getz()<=0 and zdir==-1 then break end
  492.   if dig.getz()>=zmax-1 and zdir==1 then break end
  493.  
  494.   if zdir == 1 then dig.gotor(0)
  495.   elseif zdir == -1 then dig.gotor(180)
  496.   end --if/else
  497.   checkNewLayer()
  498.  
  499.   dig.fwd()
  500.  
  501.   if dig.isStuck() then
  502.    done = true
  503.   end --if
  504.  
  505.  end --while (z loop)
  506.  
  507.  if done then break end
  508.  
  509.  zdir = -zdir
  510.  newlayer = false
  511.  
  512.  -- Add print at the start of a new row
  513.  flex.printColors("Starting new row at X="..tostring(dig.getx()).." Z="..tostring(dig.getz()).." Layer="..tostring(-dig.gety()), colors.gray)
  514.  
  515.  if dig.getx()<=0 and xdir==-1 then
  516.   newlayer = true
  517.  elseif dig.getx()>=xmax-1 and xdir==1 then
  518.   newlayer = true
  519.  else
  520.   checkAll(0) -- checkAll now includes detailed prints
  521.   dig.gotox(dig.getx()+xdir)
  522.  end --if/else
  523.  
  524.  if newlayer and not dig.isStuck() then
  525.   xdir = -xdir
  526.   if dig.getymin() <= ymin then break end
  527.   checkAll(0) -- checkAll now includes detailed prints
  528.   dig.down()
  529.   -- Add print at the start of a new layer
  530.   flex.printColors("Starting new layer at Y="..tostring(dig.gety()), colors.purple)
  531.  end --if
  532.  
  533. end --while (cuboid dig loop)
  534.  
  535. -- Removed DEBUG print after main loop
  536.  
  537.  
  538. flex.send("Digging completed, returning to surface",
  539.   colors.yellow)
  540. gotoBase()
  541.  
  542. flex.send("Descended "..tostring(-dig.getymin())..
  543.     "m total",colors.green)
  544. flex.send("Dug "..tostring(dig.getdug())..
  545.     " blocks total",colors.lightBlue)
  546.  
  547. for x=1,16 do
  548.  if dig.isBuildingBlock(x) then
  549.   turtle.select(x)
  550.   dig.placeDown()
  551.   break
  552.  end --if
  553. end --for
  554. turtle.select(1)
  555.  
  556. if dodumps then
  557.  dig.gotor(0)
  558.  dig.doDump()
  559.  dig.gotor(180)
  560. end
  561. dig.dropNotFuel()
  562. dig.gotor(0)
  563.  
  564. dig.clearSave()
  565. flex.modemOff() -- Keep this to close the modem even if not used for remote control
  566. os.unloadAPI("dig.lua")
  567. os.unloadAPI("flex.lua")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement