NortWind

aStairway

May 3rd, 2024 (edited)
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.36 KB | Gaming | 0 0
  1. --{program="aStairway",version="1.07",date="2024-05-07"}
  2. -- Pastebin "VXFbDSpi"
  3. ---------------------------------------
  4. -- Turtle-automated stairway digger.
  5. -- based on aTreeFarm by Kaikaku
  6. -- v1.04b", date="2018-01-07
  7. -- Pastebin Qn008fPa
  8. ---------------------------------------
  9.  
  10. ---------------------------------------
  11. ---- PARAMETERS -----------------------
  12. ---------------------------------------
  13. local cVer       ="1.00"
  14. local cMaxFuel   = 20000
  15. local cMinFuel   = cMaxFuel * 0.2
  16. local cSleep     = 1         -- os.sleep value used to pace operation
  17. local cSlotStart = 1
  18. local cSlotLast  = 15
  19. local cSlotChest = 16        -- ender chest
  20.  
  21. ---------------------------------------
  22. -- BASIC FUNCTIONS FOR TURTLE CONTROL -
  23. ---------------------------------------
  24.  
  25. -- calling with no parameter moves one block forward
  26. local function safeForward(n)
  27.   if n == nil
  28.   then
  29.     turtle.suck()
  30.     while not turtle.forward() do os.sleep(cSleep) end  
  31.   else
  32.     for i = 1, n do
  33.       turtle.suck()
  34.       while not turtle.forward() do os.sleep(cSleep) end    
  35.     end
  36.   end
  37. end
  38.  
  39. local function safeDig()     -- digs forward
  40.   turtle.suck()
  41.   os.sleep(cSleep)
  42.   while turtle.detect()      -- might be digging into multilayered sand or gravel
  43.   do
  44.     turtle.dig()
  45.     os.sleep(cSleep)         -- allow blocks to fall
  46.   end
  47. end
  48.  
  49. local function safeDigDown()
  50.   turtle.suckDown()          -- might be a sapling or stick
  51.   os.sleep(cSleep)
  52.   turtle.digDown()           -- might not be anything to dig
  53. end
  54.  
  55. local function safeDigUp()
  56.   turtle.suckUp()            -- remove drek on top
  57.   os.sleep(cSleep)
  58.   turtle.digUp()             -- might not be anything to dig
  59. end
  60.  
  61. ---------------------------------------
  62. ---- functions ------------------------
  63. ---------------------------------------
  64. -- wait for enter to be pressed
  65. local function waitEnter()
  66.   write("Press enter to start:")
  67.   read()
  68. end
  69.  
  70. -- check fuel in turtle, attempt to replenish if low
  71. local function checkRefuel()  
  72.   if turtle.getFuelLevel() < cMaxFuel * 0.8
  73.   then
  74.       print("Fuel level is low, "..turtle.getFuelLevel()..".")
  75.     print("Use refuel all command.")
  76.       craftFuel()  
  77.   else
  78.     print("Fuel level ok, "..turtle.getFuelLevel()..".")
  79.   end
  80. end
  81.  
  82. local function emptyInventory()
  83.   -- assume 2 spaces above turtle to transfer
  84.   print("transferring inventory...")
  85.  
  86.   turtle.select(cSlotChest)        -- put chest ontop if turtle
  87.   turtle.placeUp()
  88.   for i = 1, 15 do                 -- scan slots
  89.     turtle.select(i)
  90.     turtle.dropUp()                -- put entire stack in chest
  91.     os.sleep(5)                    -- let ender magic work
  92.     end
  93.   turtle.select(cSlotChest)        -- retrieve chest
  94.   safeDigUp()
  95.   turtle.select(cSlotStart)        -- continue filling from start
  96.  
  97.   print("transfer done.")
  98. end
  99.  
  100. ---------------------------------------
  101. -- dig a row, minimum width is 2
  102. ---------------------------------------
  103. local function digRow(rowWidth)
  104.      -- assume facing stairway direction at start
  105.     turtle.turnRight()
  106.     if rowWidth > 2
  107.     then
  108.       for i = 3, rowWidth
  109.       do
  110.         turtle.dig()
  111.         turtle.forward()
  112.       end
  113.     end
  114.     turtle.dig()             -- free dig with no motion
  115.  
  116.     turtle.turnRight()
  117.     turtle.turnRight()
  118.  
  119.     if rowWidth > 2
  120.     then
  121.       for i = 3, rowWidth
  122.       do
  123.         turtle.forward()  
  124.       end
  125.     end
  126.    
  127.     turtle.turnRight()       -- face next slice
  128. end
  129.  
  130. ---------------------------------------
  131. -- dig a 3 wide 4 high slice
  132. ---------------------------------------
  133. local function digSlice()
  134.     -- assume facing bottom left of slice
  135.     turtle.dig()
  136.     turtle.forward()         -- go into next slice
  137.     turtle.digDown()
  138.     if turtle.detectDown()   -- might be bedrock, so stop
  139.     then
  140.       return false;        -- can't dig, bedrock, all done
  141.     end
  142.     turtle.down()
  143.     for i = 1, 4 do
  144.       if i > 1
  145.       then
  146.         turtle.digUp()
  147.         turtle.up()
  148.       end
  149.       digRow(3)
  150.     end
  151.     for i = 1, 3 do
  152.         turtle.down()                  -- back down to floor
  153.     end
  154.  
  155.     if turtle.getItemCount(cSlotLast) > 0  -- almost full, transfer all to ender chest.
  156.     then
  157.       emptyInventory()
  158.     end
  159.    return true                         -- slice dug
  160. end
  161.  
  162. ---------------------------------------
  163. ---- main -----------------------------
  164. ---------------------------------------
  165. turtle.select(cSlotStart)
  166. term.clear()
  167. term.setCursorPos(1,1)
  168. if turtle.getFuelLevel() < cMinFuel
  169. then
  170.       print("Fuel level "..turtle.getFuelLevel().." is too low.")
  171.     print("Use command \"refuel all\" with 3 stacks of planks.")     -- need to add fuel
  172.     return
  173. end
  174.  
  175. print("+-------------------------------------+")
  176. print("| aStairway "..cVer..", by NortWind        |")
  177. print("+-------------------------------------+")
  178. print("| Digging a stairway:                 |")
  179. print("|   Mining turtle sits facing the     |")
  180. print("|   first step into the ground        |")
  181. print("|   Put ender chest in slot 16        |")
  182. print("+-------------------------------------+")
  183. waitEnter()  
  184. while digSlice()
  185. do
  186.   os.sleep(cSleep)
  187. end
  188.  
  189. turtle.turnRight()           -- climb back to top of stairs
  190. turtle.turnRight()
  191. turtle.forward()
  192. while turtle.detect()
  193. do
  194.     turtle.up()
  195.     turtle.forward()
  196. end    
  197. emptyInventory()              -- clean leftover inventory
  198.  
Tags: minecraft
Add Comment
Please, Sign In to add comment