Sedrowow

FancyStairs

May 1st, 2025 (edited)
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 34.35 KB | None | 0 0
  1. -- Digs a staircase around a quarry
  2. -- Run "stairs help"
  3. -- Or dig a staircase to bedrock
  4. -- Run "stairs"
  5. -- Please email me if you have any
  6. -- bugs or suggestions!
  7.  
  8. -----------------------------------
  9. --  /¯\  || ||  /\  |¯\ |¯\ \\// --
  10. -- | O | ||_|| |  | | / | /  \/  --
  11. --  \_\\  \__| |||| | \ | \  ||  --
  12. -----------------------------------
  13. -- /¯¯\ [¯¯]  /\  [¯¯] |¯\ /¯¯\  --
  14. -- \_¯\  ||  |  |  ][  | / \_¯\  --
  15. -- \__/  ||  |||| [__] | \ \__/  --
  16. -----------------------------------
  17.  
  18. -- Names of tools
  19. local name_torch = {
  20.    "torch", "lantern", "lamp", "light" }
  21. local name_bench = {
  22.    "minecraft:crafting_table",
  23.    "forge:workbench" }
  24. local name_chest = { "chest" }
  25. local name_box = {
  26.    "shulker_box", "travelersbackpack" }
  27.  
  28.  
  29. -- Stair blocks crafting material
  30. local name_cobble = {
  31.   "minecraft:cobblestone",
  32.   "forge:cobblestone" }
  33.  
  34.  
  35. -- Side that swaps with crafting bench
  36. local tool_side = "none"
  37. if not peripheral.find("workbench") then
  38.  tool_side = "left"
  39.  if peripheral.getType("left") == "modem" then
  40.   tool_side = "right"
  41.  end --if
  42. end --if
  43.  
  44.  
  45. -- Load APIs
  46. os.loadAPI("flex.lua")
  47. os.loadAPI("dig.lua")
  48. dig.setFuelSlot(1)
  49. dig.setBlockSlot(2)
  50. dig.setBlockStacks(4)
  51.  
  52.  
  53. function dump()
  54.  local slot = turtle.getSelectedSlot()
  55.  local keepers = { name_cobble, name_box,
  56.     name_torch, name_bench, name_chest,
  57.     "stairs" }
  58.  local x,a = 0,false
  59.  
  60.  for x=1,16 do
  61.   if flex.isItem(name_box,x) then
  62.    turtle.select(x)
  63.    a = turtle.placeUp()
  64.    break
  65.   end --if
  66.  end --for
  67.  
  68.  if not a then
  69.   keepers[#keepers+1] = "diamond"
  70.   keepers[#keepers+1] = "ancient_debris"
  71.  end --if
  72.  
  73.  local blocksPresent = dig.getBlockStacks()
  74.  for x=4,16 do
  75.   if not flex.isItem(keepers,x) then
  76.    if dig.isDumpItem(x) then
  77.     if blocksPresent <= 0 then
  78.      turtle.drop()
  79.     else
  80.      blocksPresent = blocksPresent - 1
  81.     end --if/else
  82.    else
  83.     turtle.select(x)
  84.     if a then
  85.      turtle.dropUp()
  86.     else
  87.      turtle.drop()
  88.     end --if/else
  89.    end --if/else
  90.   end --if
  91.  end --for
  92.  
  93.  turtle.select(slot)
  94.  if a then turtle.digUp() end
  95.  dig.checkBlocks()
  96.  flex.condense()
  97. end --function
  98.  
  99.  
  100.  
  101. -- Program parameter(s)
  102. local args={...}
  103.  
  104. -- Tutorial, kind of
  105. if #args > 0 and args[1] == "help" then
  106.  flex.printColors("Place just to the "..
  107.    "left of a turtle quarrying the same "..
  108.    "dimensions.",colors.lightBlue)
  109.  flex.printColors("Include a crafting "..
  110.    "table and a chest in turtle's "..
  111.    "inventory to auto-craft a staircase",
  112.    colors.yellow)
  113.  flex.printColors("Usage: stairs "..
  114.    "[length] [width] [depth]",colors.pink)
  115.  return
  116. end --if
  117.  
  118.  
  119. -- What Goes Where
  120. flex.printColors("Slot 1: Fuel\n"..
  121.   "Slot 2: Blocks\nSlot 3: Torches\n"..
  122.   "Anywhere: Crafting Bench, Chest\n"..
  123.   "Optional: Shulker Box / Backpack",
  124.   colors.lightBlue)
  125. flex.printColors("Press Enter",
  126.   colors.pink)
  127. while flex.getKey() ~= keys.enter do
  128.  -- Handle remote commands during waiting
  129.  local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  130.  if event == "modem_message" then
  131.      local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  132.      if type(message) == "table" and message.type == "command" then
  133.          if message.command == "status_request" then
  134.              local status = {
  135.                  type = "status",
  136.                  fuel = turtle.getFuelLevel(),
  137.                  inventory = dig.getInventory(),
  138.                  position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  139.                  is_mining = false, -- Not actively building stairs yet
  140.                  estimated_time = "Waiting for user input..." -- Placeholder
  141.              }
  142.              modem.transmit(flex.modem_channel, replyChannel, status)
  143.          elseif message.command == "unload_command" then
  144.              flex.send("Received unload command while waiting, initiating unload...", colors.yellow)
  145.              dig.dropNotFuel()
  146.              flex.send("Unload complete.", colors.lightBlue)
  147.              modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  148.          end
  149.      end
  150.  end
  151.  if event ~= "key" then sleep(0.1) end -- Prevent tight loop if no event
  152. end --while
  153.  
  154.  
  155. -- Convert Inputs
  156. local dx,dy,dz,n,x,y,z
  157. local height = 5
  158. dz = tonumber(args[1]) or 256
  159. dx = tonumber(args[2]) or dz
  160. dy = tonumber(args[3]) or 256
  161. -- -1 to match Quarry depth
  162.  
  163.  
  164. --------------------------------------
  165. -- |¯\ [¯¯] /¯¯] /¯¯][¯¯]|\ || /¯¯] --
  166. -- |  | ][ | [¯|| [¯| ][ | \ || [¯| --
  167. -- |_/ [__] \__| \__|[__]|| \| \__| --
  168. --------------------------------------
  169.  
  170. flex.send("Digging staircase...",
  171.   colors.yellow)
  172.  
  173. -- Staircase Digging Functions
  174.  
  175. local torchNum = 9
  176.  
  177. function placeTorch()
  178.  turtle.select(3)
  179.  if flex.isItem(name_torch) then
  180.  
  181.   if not turtle.place() then
  182.    if not dig.fwd() then return false end
  183.    turtle.select(2)
  184.    dig.place()
  185.    if not dig.back() then return false end
  186.  
  187.    turtle.select(3)
  188.    if not dig.place() then
  189.     if not dig.fwd() then return false end
  190.     turtle.select(2)
  191.     dig.placeUp()
  192.     if not dig.back() then return false end
  193.     turtle.select(3)
  194.     dig.place()
  195.    end --if/else
  196.   end --if
  197.  end --if
  198.  
  199.  turtle.select(2)
  200. end --function
  201.  
  202.  
  203. function stepDown()
  204.  local x
  205.  
  206.  turtle.select(2)
  207.  dig.right()
  208.  for x=1,height-2 do
  209.   dig.blockLava()
  210.   if not dig.up() then return false end
  211.  end --for
  212.  dig.blockLava()
  213.  dig.blockLavaUp()
  214.  
  215.  dig.left()
  216.  dig.blockLava()
  217.  dig.left()
  218.  if not dig.fwd() then return false end
  219.  dig.blockLavaUp()
  220.  dig.blockLava()
  221.  dig.right()
  222.  dig.blockLava()
  223.  dig.left()
  224.  
  225.  if torchNum >= 3 then
  226.   if not dig.back() then return false end
  227.   placeTorch()
  228.   if not dig.down() then return false end
  229.   if not dig.fwd() then return false end
  230.   torchNum = 0
  231.  else
  232.   dig.blockLava()
  233.   if not dig.down() then return false end
  234.   torchNum = torchNum + 1
  235.  end --if/else
  236.  
  237.  for x=1,height-2 do
  238.   dig.blockLava()
  239.   if not dig.down() then return false end
  240.  end --for
  241.  dig.blockLava()
  242.  if not dig.placeDown() then return false end
  243.  
  244.  dig.right(2)
  245.  if not dig.fwd() then return false end
  246.  dig.blockLava()
  247.  if not dig.placeDown() then return false end
  248.  dig.left()
  249.  
  250.  if turtle.getItemCount(16) > 0 then
  251.   dig.left()
  252.   dump()
  253.   dig.right()
  254.  end --if/else
  255.  
  256.  if not dig.fwd() then return false end
  257.  
  258.  return true
  259. end --function
  260.  
  261.  
  262. local function turnRight()
  263.  turtle.select(2)
  264.  dig.right()
  265.  if not dig.up(height-2) then return false end
  266.  dig.blockLavaUp()
  267.  
  268.  dig.left()
  269.  if not dig.down() then return false end
  270.  if not dig.fwd() then return false end
  271.  dig.blockLavaUp()
  272.  for x=1,height-3 do
  273.   dig.blockLava()
  274.   if not dig.down() then return false end
  275.  end --for
  276.  dig.blockLava()
  277.  if not dig.placeDown() then return false end
  278.  
  279.  dig.left()
  280.  if not dig.fwd() then return false end
  281.  for x=1,height-3 do
  282.   dig.blockLava()
  283.   if not dig.up() then return false end
  284.  end --for
  285.  dig.blockLava()
  286.  dig.blockLavaUp()
  287.  
  288.  dig.right()
  289.  for x=1,height-3 do
  290.   dig.blockLava()
  291.   if not dig.down() then return false end
  292.  end --for
  293.  dig.blockLava()
  294.  if not dig.placeDown() then return false end
  295.  
  296.  dig.left(2)
  297.  if not dig.fwd() then return false end
  298.  dig.right()
  299.  if not dig.placeDown() then return false end
  300.  for x=1,height-2 do
  301.   dig.blockLava()
  302.   if not dig.up() then return false end
  303.  end --for
  304.  dig.blockLava()
  305.  dig.blockLavaUp()
  306.  
  307.  dig.right(2)
  308.  if not dig.fwd() then return false end
  309.  if not dig.down(height-1) then return false end
  310.  if not dig.placeDown() then return false end
  311.  dig.left()
  312.  if not dig.fwd() then return false end
  313.  dig.blockLava()
  314.  if not dig.placeDown() then return false end
  315.  if not dig.back() then return false end
  316.  dig.right()
  317.  if not dig.fwd() then return false end
  318.  
  319.  torchNum = torchNum + 1
  320.  return true
  321. end --function
  322.  
  323.  
  324. function endcap(h,stop)
  325.  stop = ( stop ~= nil )
  326.  h = h or 0 -- Height to dig layer
  327.  local x
  328.  
  329.  dig.right()
  330.  if not dig.placeDown() then return false end
  331.  dig.checkBlocks()
  332.  for x=1,height-2-h do
  333.   dig.blockLava()
  334.   if not dig.up() then return false end
  335.  end --for
  336.  dig.blockLava()
  337.  dig.blockLavaUp()
  338.  
  339.  dig.left(2)
  340.  if not dig.fwd() then return false end
  341.  dig.blockLavaUp()
  342.  for x=1,height-2-h do
  343.   dig.blockLava()
  344.   if not dig.down() then return false end
  345.  end --for
  346.  dig.blockLava()
  347.  if not dig.placeDown() then return false end
  348.  dig.checkBlocks()
  349.  if not dig.back() then return false end
  350.  
  351.  dig.right()
  352.  
  353.  if stop then
  354.   dig.blockLava()
  355.   for x=1,height-2-h do
  356.    if not dig.up() then return false end
  357.    dig.blockLava()
  358.   end --for
  359.   dig.blockLavaUp()
  360.   dig.left()
  361.  
  362.   if not dig.fwd() then return false end
  363.   dig.blockLavaUp()
  364.   dig.right()
  365.   dig.blockLava()
  366.   for x=1,height-2-h do
  367.    if not dig.down() then return false end
  368.    dig.blockLava()
  369.   end --for
  370.  
  371.   dig.left()
  372.   if not dig.back() then return false end
  373.   dig.right()
  374.  
  375.  end --if
  376.  
  377.  return true
  378. end --function
  379.  
  380.  
  381.  
  382. local direction
  383.  
  384. function avoidBedrock()
  385.  if dig.isStuck() then
  386.   -- Hit Bedrock/Void
  387.   if dig.getStuckDir() == "fwd" then
  388.    dig.up()
  389.    dig.placeDown()
  390.    dig.checkBlocks()
  391.    dig.setymin(dig.gety())
  392.    dig.fwd()
  393.   elseif dig.getStuckDir() == "down" then
  394.    dig.setymin(dig.gety())
  395.   end --if
  396.  end --if
  397.  
  398.  -- Get X and Z on the inner stair block
  399.  if dig.getx() >= dx+2 then
  400.   dig.gotox(dx+1)
  401.  
  402.  elseif dig.getx() <= -1 then
  403.   dig.gotox(0)
  404.  
  405.  end --if/else
  406.  
  407.  if dig.getz() >= dz+1 then
  408.   dig.gotoz(dz)
  409.  
  410.  elseif dig.getz() <= -2 then
  411.   dig.gotoz(-1)
  412.  
  413.  end --if/else
  414.  
  415.  dig.gotor(direction)
  416.  dig.gotoy(dig.getymin())
  417. end --function
  418.  
  419.  
  420.  
  421. -- Start Digging
  422.  
  423. turtle.select(2)
  424.  
  425. x = 0
  426. direction = dig.getr()
  427. while true do
  428.  -- Handle remote commands during digging
  429.  local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  430.  if event == "modem_message" then
  431.      local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  432.      if type(message) == "table" and message.type == "command" then
  433.          if message.command == "status_request" then
  434.              -- Estimate remaining steps/levels (simple for stairs)
  435.              local total_depth = dy -- Assuming dy is the target depth
  436.              local current_y = dig.gety()
  437.              local estimated_remaining_levels = math.max(0, current_y - dig.getymin()) -- Simplified estimate
  438.  
  439.              local estimated_time_display = "Calculating..."
  440.              if estimated_remaining_levels > 0 then
  441.                  -- Very rough time estimate based on remaining levels and average time per step
  442.                  local avg_time_per_level = 30 -- Calibrate this value based on your turtle's performance
  443.                  local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  444.                  local hours = math.floor(estimated_time_seconds / 3600)
  445.                  local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  446.                  local seconds = math.floor(estimated_time_seconds % 60)
  447.                  estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  448.              end
  449.  
  450.              local status = {
  451.                  type = "status",
  452.                  fuel = turtle.getFuelLevel(),
  453.                  inventory = dig.getInventory(),
  454.                  position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  455.                  is_mining = true, -- Actively building stairs
  456.                  estimated_time = estimated_time_display
  457.              }
  458.              modem.transmit(flex.modem_channel, replyChannel, status)
  459.          elseif message.command == "unload_command" then
  460.              flex.send("Received unload command, initiating unload...", colors.yellow)
  461.              dig.dropNotFuel()
  462.              flex.send("Unload complete.", colors.lightBlue)
  463.              modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  464.          end
  465.      end
  466.  end
  467.  if event ~= nil then sleep(0.1) end -- Prevent tight loop if no event
  468.  
  469.  for n=0,dz-1 do
  470.   if not stepDown() then break end
  471.   x = x + 1
  472.   if x >= dy then break end
  473.   -- Handle remote commands during steps
  474.   local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  475.   if event == "modem_message" then
  476.       local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  477.       if type(message) == "table" and message.type == "command" then
  478.           if message.command == "status_request" then
  479.               local total_depth = dy
  480.               local current_y = dig.gety()
  481.               local estimated_remaining_levels = math.max(0, current_y - dig.getymin())
  482.  
  483.               local estimated_time_display = "Calculating..."
  484.               if estimated_remaining_levels > 0 then
  485.                   local avg_time_per_level = 30
  486.                   local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  487.                   local hours = math.floor(estimated_time_seconds / 3600)
  488.                   local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  489.                   local seconds = math.floor(estimated_time_seconds % 60)
  490.                   estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  491.               end
  492.  
  493.               local status = {
  494.                   type = "status",
  495.                   fuel = turtle.getFuelLevel(),
  496.                   inventory = dig.getInventory(),
  497.                   position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  498.                   is_mining = true,
  499.                   estimated_time = estimated_time_display
  500.               }
  501.               modem.transmit(flex.modem_channel, replyChannel, status)
  502.           elseif message.command == "unload_command" then
  503.               flex.send("Received unload command, initiating unload...", colors.yellow)
  504.               dig.dropNotFuel()
  505.               flex.send("Unload complete.", colors.lightBlue)
  506.               modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  507.           end
  508.       end
  509.   end
  510.  end
  511.  if dig.isStuck() or x >= dy then break end
  512.  if not turnRight() then break end
  513.  x = x + 1
  514.  -- Handle remote commands after turning
  515.  local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  516.  if event == "modem_message" then
  517.      local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  518.      if type(message) == "table" and message.type == "command" then
  519.          if message.command == "status_request" then
  520.              local total_depth = dy
  521.              local current_y = dig.gety()
  522.              local estimated_remaining_levels = math.max(0, current_y - dig.getymin())
  523.  
  524.              local estimated_time_display = "Calculating..."
  525.              if estimated_remaining_levels > 0 then
  526.                  local avg_time_per_level = 30
  527.                  local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  528.                  local hours = math.floor(estimated_time_seconds / 3600)
  529.                  local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  530.                  local seconds = math.floor(estimated_time_seconds % 60)
  531.                  estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  532.              end
  533.  
  534.              local status = {
  535.                  type = "status",
  536.                  fuel = turtle.getFuelLevel(),
  537.                  inventory = dig.getInventory(),
  538.                  position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  539.                  is_mining = true,
  540.                  estimated_time = estimated_time_display
  541.              }
  542.              modem.transmit(flex.modem_channel, replyChannel, status)
  543.          elseif message.command == "unload_command" then
  544.              flex.send("Received unload command, initiating unload...", colors.yellow)
  545.              dig.dropNotFuel()
  546.              flex.send("Unload complete.", colors.lightBlue)
  547.              modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  548.          end
  549.      end
  550.  end
  551.  
  552.  
  553.  direction = dig.getr()
  554.  for n=0,dx-1 do
  555.   if not stepDown() then break end
  556.   x = x + 1
  557.   if x >= dy then break end
  558.   -- Handle remote commands during steps
  559.    local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  560.    if event == "modem_message" then
  561.        local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  562.        if type(message) == "table" and message.type == "command" then
  563.            if message.command == "status_request" then
  564.                local total_depth = dy
  565.                local current_y = dig.gety()
  566.                local estimated_remaining_levels = math.max(0, current_y - dig.getymin())
  567.  
  568.                local estimated_time_display = "Calculating..."
  569.                if estimated_remaining_levels > 0 then
  570.                    local avg_time_per_level = 30
  571.                    local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  572.                    local hours = math.floor(estimated_time_seconds / 3600)
  573.                    local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  574.                    local seconds = math.floor(estimated_time_seconds % 60)
  575.                    estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  576.                end
  577.  
  578.                local status = {
  579.                    type = "status",
  580.                    fuel = turtle.getFuelLevel(),
  581.                    inventory = dig.getInventory(),
  582.                    position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  583.                    is_mining = true,
  584.                    estimated_time = estimated_time_display
  585.                }
  586.                modem.transmit(flex.modem_channel, replyChannel, status)
  587.            elseif message.command == "unload_command" then
  588.                flex.send("Received unload command, initiating unload...", colors.yellow)
  589.                dig.dropNotFuel()
  590.                flex.send("Unload complete.", colors.lightBlue)
  591.                modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  592.            end
  593.        end
  594.    end
  595.  end
  596.  if dig.isStuck() or x >= dy then break end
  597.  if not turnRight() then break end
  598.  x = x + 1
  599.  -- Handle remote commands after turning
  600.   local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  601.   if event == "modem_message" then
  602.       local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  603.       if type(message) == "table" and message.type == "command" then
  604.           if message.command == "status_request" then
  605.               local total_depth = dy
  606.               local current_y = dig.gety()
  607.               local estimated_remaining_levels = math.max(0, current_y - dig.getymin())
  608.  
  609.               local estimated_time_display = "Calculating..."
  610.               if estimated_remaining_levels > 0 then
  611.                   local avg_time_per_level = 30
  612.                   local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  613.                   local hours = math.floor(estimated_time_seconds / 3600)
  614.                   local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  615.                   local seconds = math.floor(estimated_time_seconds % 60)
  616.                   estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  617.               end
  618.  
  619.               local status = {
  620.                   type = "status",
  621.                   fuel = turtle.getFuelLevel(),
  622.                   inventory = dig.getInventory(),
  623.                   position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  624.                   is_mining = true,
  625.                   estimated_time = estimated_time_display
  626.               }
  627.               modem.transmit(flex.modem_channel, replyChannel, status)
  628.           elseif message.command == "unload_command" then
  629.               flex.send("Received unload command, initiating unload...", colors.yellow)
  630.               dig.dropNotFuel()
  631.               flex.send("Unload complete.", colors.lightBlue)
  632.               modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  633.           end
  634.       end
  635.   end
  636.  
  637.  direction = dig.getr()
  638. end --while
  639.  
  640.  
  641. avoidBedrock()
  642. if not dig.fwd() then avoidBedrock() end
  643. if not endcap(1) then avoidBedrock() end
  644. if not dig.fwd() then avoidBedrock() end
  645. if not endcap(1,true) then avoidBedrock() end
  646.  
  647. dig.left(2)
  648. while not turtle.detect() do
  649.  dig.fwd()
  650.  -- Handle remote commands while returning to edge
  651.  local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  652.  if event == "modem_message" then
  653.      local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  654.      if type(message) == "table" and message.type == "command" then
  655.          if message.command == "status_request" then
  656.               local status = {
  657.                   type = "status",
  658.                   fuel = turtle.getFuelLevel(),
  659.                   inventory = dig.getInventory(),
  660.                   position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  661.                   is_mining = true, -- Still working
  662.                   estimated_time = "Returning to edge..." -- Placeholder
  663.               }
  664.               modem.transmit(flex.modem_channel, replyChannel, status)
  665.          elseif message.command == "unload_command" then
  666.              flex.send("Received unload command, initiating unload...", colors.yellow)
  667.              dig.dropNotFuel()
  668.              flex.send("Unload complete.", colors.lightBlue)
  669.              modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  670.          end
  671.      end
  672.  end
  673.  if event ~= nil then sleep(0.1) end
  674. end --while
  675. dig.back()
  676.  
  677. -- This bit compensates for random Bedrock (mostly)
  678. if #dig.getKnownBedrock() > 0 then
  679.  for x=1,4 do
  680.   dig.placeDown()
  681.   dig.right()
  682.   dig.fwd()
  683.  end --for
  684. end --for
  685.  
  686.  
  687.  
  688. ----------------------------------------------
  689. --  /¯] |¯\  /\  |¯¯] [¯¯] [¯¯] |\ ||  /¯¯] --
  690. -- | [  | / |  | | ]   ||   ][  | \ | | [¯| --
  691. --  \_] | \ |||| ||    ||  [__] || \|  \__| --
  692. ----------------------------------------------
  693.  
  694.  
  695. local function placeStairs()
  696.  local x,y,z,slot
  697.  slot = turtle.getSelectedSlot()
  698.  y = turtle.getItemCount()
  699.  z = true
  700.  
  701.  if y < 2 or not flex.isItem("stairs") then
  702.   for x=1,16 do
  703.    turtle.select(x)
  704.    y = turtle.getItemCount()
  705.    if y >= 2 and flex.isItem("stairs") then
  706.     z = false
  707.     break
  708.    end --if
  709.   end --for
  710.  
  711.   if z then
  712.    turtle.select(slot)
  713.    return false
  714.   end --if
  715.  end --if
  716.  
  717.  dig.placeDown()
  718.  dig.right()
  719.  dig.fwd()
  720.  dig.left()
  721.  dig.placeDown()
  722.  dig.left()
  723.  dig.fwd()
  724.  dig.right()
  725. end --function
  726.  
  727.  
  728. flex.send("Returning to surface",
  729.   colors.yellow)
  730.  
  731. function isDone()
  732.  -- Reached Surface
  733.  return dig.gety() >= 0
  734. end
  735.  
  736. -- Follow the Spiral [and place Stairs]
  737. while not isDone() do
  738.  -- Handle remote commands during ascent
  739.  local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  740.  if event == "modem_message" then
  741.      local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  742.      if type(message) == "table" and message.type == "command" then
  743.          if message.command == "status_request" then
  744.               local total_depth = dy
  745.               local current_y = dig.gety()
  746.               local estimated_remaining_levels = math.max(0, current_y - dig.getymin())
  747.  
  748.               local estimated_time_display = "Calculating..."
  749.               if estimated_remaining_levels > 0 then
  750.                   local avg_time_per_level = 30
  751.                   local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  752.                   local hours = math.floor(estimated_time_seconds / 3600)
  753.                   local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  754.                   local seconds = math.floor(estimated_time_seconds % 60)
  755.                   estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  756.               end
  757.  
  758.               local status = {
  759.                   type = "status",
  760.                   fuel = turtle.getFuelLevel(),
  761.                   inventory = dig.getInventory(),
  762.                   position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  763.                   is_mining = true, -- Still working
  764.                   estimated_time = estimated_time_display
  765.               }
  766.               modem.transmit(flex.modem_channel, replyChannel, status)
  767.          elseif message.command == "unload_command" then
  768.              flex.send("Received unload command, initiating unload...", colors.yellow)
  769.              dig.dropNotFuel()
  770.              flex.send("Unload complete.", colors.lightBlue)
  771.              modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  772.          end
  773.      end
  774.  end
  775.  if event ~= nil then sleep(0.1) end
  776.  
  777.  
  778.  if dig.getr()%360 == 0 then
  779.   while dig.getz() < dig.getzmax()-1 do
  780.    dig.fwd()
  781.    dig.up()
  782.    placeStairs()
  783.    if isDone() then break end
  784.    -- Handle remote commands during steps
  785.     local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  786.     if event == "modem_message" then
  787.         local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  788.         if type(message) == "table" and message.type == "command" then
  789.             if message.command == "status_request" then
  790.                  local total_depth = dy
  791.                  local current_y = dig.gety()
  792.                  local estimated_remaining_levels = math.max(0, current_y - dig.getymin())
  793.  
  794.                  local estimated_time_display = "Calculating..."
  795.                  if estimated_remaining_levels > 0 then
  796.                      local avg_time_per_level = 30
  797.                      local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  798.                      local hours = math.floor(estimated_time_seconds / 3600)
  799.                      local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  800.                      local seconds = math.floor(estimated_time_seconds % 60)
  801.                      estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  802.                  end
  803.  
  804.                  local status = {
  805.                      type = "status",
  806.                      fuel = turtle.getFuelLevel(),
  807.                      inventory = dig.getInventory(),
  808.                      position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  809.                      is_mining = true,
  810.                      estimated_time = estimated_time_display
  811.                  }
  812.                  modem.transmit(flex.modem_channel, replyChannel, status)
  813.             elseif message.command == "unload_command" then
  814.                 flex.send("Received unload command, initiating unload...", colors.yellow)
  815.                 dig.dropNotFuel()
  816.                 flex.send("Unload complete.", colors.lightBlue)
  817.                 modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  818.             end
  819.         end
  820.     end
  821.   end
  822.  
  823.  elseif dig.getr()%360 == 90 then
  824.   while dig.getx() < dig.getxmax()-1 do
  825.    dig.fwd()
  826.    dig.up()
  827.    placeStairs()
  828.    if isDone() then break end
  829.    -- Handle remote commands during steps
  830.     local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  831.     if event == "modem_message" then
  832.         local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  833.         if type(message) == "table" and message.type == "command" then
  834.             if message.command == "status_request" then
  835.                  local total_depth = dy
  836.                  local current_y = dig.gety()
  837.                  local estimated_remaining_levels = math.max(0, current_y - dig.getymin())
  838.  
  839.                  local estimated_time_display = "Calculating..."
  840.                  if estimated_remaining_levels > 0 then
  841.                      local avg_time_per_level = 30
  842.                      local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  843.                      local hours = math.floor(estimated_time_seconds / 3600)
  844.                      local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  845.                      local seconds = math.floor(estimated_time_seconds % 60)
  846.                      estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  847.                  end
  848.  
  849.                  local status = {
  850.                      type = "status",
  851.                      fuel = turtle.getFuelLevel(),
  852.                      inventory = dig.getInventory(),
  853.                      position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  854.                      is_mining = true,
  855.                      estimated_time = estimated_time_display
  856.                  }
  857.                  modem.transmit(flex.modem_channel, replyChannel, status)
  858.             elseif message.command == "unload_command" then
  859.                 flex.send("Received unload command, initiating unload...", colors.yellow)
  860.                 dig.dropNotFuel()
  861.                 flex.send("Unload complete.", colors.lightBlue)
  862.                 modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  863.             end
  864.         end
  865.     end
  866.   end
  867.  
  868.  elseif dig.getr()%360 == 180 then
  869.   while dig.getz() > dig.getzmin()+1 do
  870.    dig.fwd()
  871.    dig.up()
  872.    placeStairs()
  873.    if dig.gety() > -4 and dig.getz()
  874.       == dig.getzmin()+1 then
  875.     -- Up at the top
  876.     dig.fwd()
  877.     dig.up()
  878.     placeStairs()
  879.    end --if
  880.    if isDone() then break end
  881.    -- Handle remote commands during steps
  882.     local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  883.     if event == "modem_message" then
  884.         local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  885.         if type(message) == "table" and message.type == "command" then
  886.             if message.command == "status_request" then
  887.                  local total_depth = dy
  888.                  local current_y = dig.gety()
  889.                  local estimated_remaining_levels = math.max(0, current_y - dig.getymin())
  890.  
  891.                  local estimated_time_display = "Calculating..."
  892.                  if estimated_remaining_levels > 0 then
  893.                      local avg_time_per_level = 30
  894.                      local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  895.                      local hours = math.floor(estimated_time_seconds / 3600)
  896.                      local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  897.                      local seconds = math.floor(estimated_time_seconds % 60)
  898.                      estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  899.                  end
  900.  
  901.                  local status = {
  902.                      type = "status",
  903.                      fuel = turtle.getFuelLevel(),
  904.                      inventory = dig.getInventory(),
  905.                      position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  906.                      is_mining = true,
  907.                      estimated_time = estimated_time_display
  908.                  }
  909.                  modem.transmit(flex.modem_channel, replyChannel, status)
  910.             elseif message.command == "unload_command" then
  911.                 flex.send("Received unload command, initiating unload...", colors.yellow)
  912.                 dig.dropNotFuel()
  913.                 flex.send("Unload complete.", colors.lightBlue)
  914.                 modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  915.             end
  916.         end
  917.     end
  918.   end
  919.  
  920.  elseif dig.getr()%360 == 270 then
  921.   while dig.getx() > dig.getxmin()+1 do
  922.    dig.fwd()
  923.    dig.up()
  924.    placeStairs()
  925.    if isDone() then break end
  926.    -- Handle remote commands during steps
  927.     local event, p1, p2, p3, p4, p5, p6 = os.pullEvent("modem_message", 0.1)
  928.     if event == "modem_message" then
  929.         local modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5
  930.         if type(message) == "table" and message.type == "command" then
  931.             if message.command == "status_request" then
  932.                  local total_depth = dy
  933.                  local current_y = dig.gety()
  934.                  local estimated_remaining_levels = math.max(0, current_y - dig.getymin())
  935.  
  936.                  local estimated_time_display = "Calculating..."
  937.                  if estimated_remaining_levels > 0 then
  938.                      local avg_time_per_level = 30
  939.                      local estimated_time_seconds = estimated_remaining_levels * avg_time_per_level
  940.                      local hours = math.floor(estimated_time_seconds / 3600)
  941.                      local minutes = math.floor((estimated_time_seconds % 3600) / 60)
  942.                      local seconds = math.floor(estimated_time_seconds % 60)
  943.                      estimated_time_display = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  944.                  end
  945.  
  946.                  local status = {
  947.                      type = "status",
  948.                      fuel = turtle.getFuelLevel(),
  949.                      inventory = dig.getInventory(),
  950.                      position = { x = dig.getx(), y = dig.gety(), z = dig.getz(), r = dig.getr() },
  951.                      is_mining = true,
  952.                      estimated_time = estimated_time_display
  953.                  }
  954.                  modem.transmit(flex.modem_channel, replyChannel, status)
  955.             elseif message.command == "unload_command" then
  956.                 flex.send("Received unload command, initiating unload...", colors.yellow)
  957.                 dig.dropNotFuel()
  958.                 flex.send("Unload complete.", colors.lightBlue)
  959.                 modem.transmit(flex.modem_channel, replyChannel, { type = "ack", command = "unload_command" })
  960.             end
  961.         end
  962.     end
  963.   end
  964.  
  965.  end --if/else
  966.  
  967.  if not isDone() then dig.left() end
  968.  
  969. end --while
  970.  
  971.  
  972. -- All Done!
  973. turtle.select(1)
  974. dig.goto(0,0,0,0)
  975.  
  976. if success then
  977.  flex.send("Stairway finished!",
  978.    colors.lightBlue)
  979. else
  980.  flex.send("Reached Origin",
  981.    colors.lightBlue)
  982. end --if
  983.  
  984. flex.modemOff()
  985. os.unloadAPI("dig.lua")
  986. os.unloadAPI("flex.lua")
Advertisement
Add Comment
Please, Sign In to add comment