Advertisement
Sedrowow

FancyStairs

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