Te-ki

ccStairs

Mar 27th, 2025 (edited)
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.65 KB | Gaming | 0 0
  1. local tArgs = { ... }
  2. local monitor = term.current()
  3. local sizeX,sizeY = monitor.getSize()
  4.  
  5. -- Logging
  6.  
  7. local lastMsg = ""
  8. local lastMsgCount = 0
  9. local function logMsg(msg)
  10.     monitor.setCursorPos(1,13)
  11.     monitor.clearLine()
  12.     if lastMsg == msg then
  13.         lastMsgCount = lastMsgCount + 1
  14.         msg = msg .. " " .. lastMsgCount
  15.     else
  16.         lastMsg = msg
  17.         lastMsgCount = 0
  18.         monitor.scroll(1)
  19.     end
  20.     monitor.setCursorPos(1,12)
  21.     monitor.clearLine()
  22.     monitor.write(msg)
  23.     monitor.setCursorPos(1,13)
  24. end
  25. local function msg(msg)
  26.     monitor.setCursorPos(1,13)
  27.     monitor.clearLine()
  28.     monitor.write(msg)
  29. end
  30.  
  31. -- Turtle Inventory
  32.  
  33. local function getItemList()
  34.     local itemList = {}
  35.     for slot=1, 16 do
  36.         itemList[slot] = turtle.getItemDetail(slot)
  37.     end
  38.     return itemList
  39. end
  40.  
  41. local function findEmptySlot(itemList, size)
  42.     for slot=1, size do
  43.         if not itemList[slot] then
  44.             return true, slot
  45.         end
  46.     end
  47.     return false, nil
  48. end
  49.  
  50. local function isStairs(itemName)
  51.     return string.match(itemName, "_stairs$") ~= nil
  52. end
  53.  
  54. local function isTorch(itemName)
  55.     return string.match(itemName, "minecraft:torch") ~= nil
  56. end
  57.  
  58. local function selectStairSlot()
  59.     for slot = 1, 16 do
  60.         local itemDetail = turtle.getItemDetail(slot)
  61.         if itemDetail ~= nil and isStairs(itemDetail.name) then
  62.             turtle.select(slot)
  63.             return true
  64.         end
  65.     end
  66.     return false
  67. end
  68.  
  69. local function selectTorchSlot()
  70.     for slot = 1, 16 do
  71.         local itemDetail = turtle.getItemDetail(slot)
  72.         if itemDetail ~= nil and isTorch(itemDetail.name) then
  73.             turtle.select(slot)
  74.             return true
  75.         end
  76.     end
  77.     return false
  78. end
  79.  
  80. local function refuel()
  81.     for slot = 1, 16 do
  82.         local itemDetail = turtle.getItemDetail(slot)
  83.         turtle.select(slot)
  84.         if itemDetail ~= nil and turtle.refuel(1) then
  85.             return true
  86.         end
  87.     end
  88.     return false
  89. end
  90.  
  91. -- Setup
  92.  
  93. if #tArgs == 0 then
  94.     logMsg("To start digging a stair type:")
  95.     logMsg("ccstairs <1>")
  96.     logMsg("<1>: the number of floors to dig.")
  97.     return 0
  98. end
  99.  
  100. local floorsToMine = tArgs[1] + 0
  101.  
  102. local function startup()
  103.     local startupSuccess = true
  104.  
  105.     local items = getItemList()
  106.     local freeSlotCount = 0
  107.     local stairsCount = 0
  108.     local torchesCount = 0
  109.     for slot = 1, 16 do
  110.         if not items[slot] then
  111.             freeSlotCount = freeSlotCount + 1
  112.         elseif isStairs(items[slot].name) then
  113.             stairsCount = stairsCount + items[slot].count
  114.         elseif isTorch(items[slot].name) then
  115.             torchesCount = torchesCount + items[slot].count
  116.         end
  117.     end
  118.     local slotsNeeded = 0
  119.     if floorsToMine <= 2 then
  120.         slotsNeeded = floorsToMine * 4
  121.     else
  122.         slotsNeeded = 8 + ((floorsToMine - 2) * 5)
  123.     end
  124.     slotsNeeded = math.ceil(slotsNeeded / 64)
  125.  
  126.     local fuelNeeded = 0
  127.     if floorsToMine == 1 then
  128.         fuelNeeded = 1
  129.     elseif floorsToMine == 2 then
  130.         fuelNeeded = 3
  131.     else
  132.         fuelNeeded = 3 + ((floorsToMine - 2) * 4)
  133.     end
  134.     logMsg("There is " .. freeSlotCount .. " free slots.")
  135.     logMsg("There is " .. stairsCount .. " stairs.")
  136.     logMsg("There is " .. torchesCount .. " torches.")
  137.     logMsg("There is " .. turtle.getFuelLevel() .. " fuel left.")
  138.  
  139.     if freeSlotCount < slotsNeeded then
  140.         startupSuccess = false
  141.         logMsg("Not enough free slots ! (".. freeSlotCount .. "/" .. slotsNeeded .. ")")
  142.     end
  143.     if stairsCount < floorsToMine then
  144.         startupSuccess = false
  145.         logMsg("Not enough stairs ! (".. stairsCount .. "/" .. floorsToMine .. ")")
  146.     end
  147.     if floorsToMine > 2 and torchesCount < floorsToMine / 2 then
  148.         startupSuccess = false
  149.         logMsg("Not enough torches ! (".. torchesCount .. "/" .. (floorsToMine / 2) .. ")")
  150.     end
  151.  
  152.     while turtle.getFuelLevel() < fuelNeeded and refuel() do
  153.     end
  154.     if turtle.getFuelLevel() < fuelNeeded then
  155.         startupSuccess = false
  156.         logMsg("Not enough fuel ! (".. fuelLevel .. "/" .. fuelNeeded .. ")")
  157.     end
  158.  
  159.     return startupSuccess
  160. end
  161. if not startup() then
  162.     return 0
  163. end
  164.  
  165. -- Digging
  166.  
  167. local function move(direction)
  168.     local movement = nil
  169.     if direction == "forward" then
  170.         movement = turtle.forward
  171.     elseif direction == "up" then
  172.         movement = turtle.up
  173.     elseif direction == "down" then
  174.         movement = turtle.down
  175.     else
  176.         error("Error: Bad direction")
  177.     end
  178.     while not movement() do
  179.         -- fuel check
  180.         if turtle.getFuelLevel() < 1 then
  181.             refuel()
  182.         else
  183.             msg("Can't move " .. direction)
  184.         end
  185.     end
  186. end
  187.  
  188. local function dig(direction)
  189.     local detect = nil
  190.     local dig = nil
  191.     local inspect = nil
  192.     if direction == "forward" then
  193.         detect = turtle.detect
  194.         dig = turtle.dig
  195.         inspect = turtle.inspect
  196.     elseif direction == "up" then
  197.         detect = turtle.detectUp
  198.         dig = turtle.digUp
  199.         inspect = turtle.inspectUp
  200.     elseif direction == "down" then
  201.         detect = turtle.detectDown
  202.         dig = turtle.digDown
  203.         inspect = turtle.inspectDown
  204.     else
  205.         error("Error: Bad direction")
  206.     end
  207.     while detect() do
  208.         if not dig() then
  209.             local block = inspect()
  210.             if block == nil then
  211.                 block = {name = "unknown"}
  212.             end
  213.             msg("Can't dig " .. direction .. " " .. block.name)
  214.         end
  215.     end
  216. end
  217.  
  218. local function placeStair()
  219.     if selectStairSlot() then
  220.         while not turtle.turnRight() do
  221.         end
  222.         while not turtle.turnRight() do
  223.         end
  224.         while not turtle.placeDown() do
  225.         end
  226.         while not turtle.turnRight() do
  227.         end
  228.         while not turtle.turnRight() do
  229.         end
  230.         return true
  231.     end
  232.     return false
  233. end
  234.  
  235. local function digFirstLevel()
  236.     dig("up")
  237.     dig("forward")
  238.     move("forward")
  239.     dig("up")
  240.     dig("down")
  241.     return placeStair()
  242. end
  243.  
  244. local function digSecondLevel()
  245.     dig("forward")
  246.     move("forward")
  247.     dig("up")
  248.     dig("down")
  249.     move("down")
  250.     dig("down")
  251.     return placeStair()
  252. end
  253.  
  254. local function digRemainingLevels(floorNumber)
  255.     move("up")
  256.     dig("forward")
  257.     move("forward")
  258.     dig("up")
  259.     if floorNumber % 2 == 0 then
  260.         if selectTorchSlot() then
  261.             turtle.placeUp()
  262.         end
  263.     end
  264.     dig("down")
  265.     move("down")
  266.     dig("down")
  267.     move("down")
  268.     dig("down")
  269.     return placeStair()
  270. end
  271.  
  272. for i = 1, floorsToMine do
  273.     if i == 1 then
  274.         digFirstLevel()
  275.     elseif i == 2 then
  276.         digSecondLevel()
  277.     else
  278.         digRemainingLevels(i)
  279.     end
  280. end
  281.  
  282. monitor.scroll(1)
  283. monitor.setCursorPos(1,13)
  284.  
Add Comment
Please, Sign In to add comment