Jameelo

Simple Staircase Miner

Nov 8th, 2023 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.35 KB | Gaming | 0 0
  1. --[[
  2.     This code will make a turtle dig a downwards staircase with enough headroom to place a staircase in.
  3.     The algorithm I made is periodic repeating at T=2, so a half period will need to be executed for odd values of depth.
  4. ]]
  5.  
  6. ODD_DEPTH = false
  7.  
  8. local function initialize()
  9.     print("Enter staircase depth, minimum value 5.")
  10.     DEPTH = tonumber(read())
  11.  
  12.     while DEPTH < 5 do
  13.         print("Invalid input! The depth must be at least 5.")
  14.         DEPTH = tonumber(read())
  15.     end
  16.  
  17.     if DEPTH%2 == 1 then
  18.         ODD_DEPTH = true
  19.     end
  20.  
  21.     return true
  22. end
  23.  
  24. local function calculateFuelExpenditure()
  25.     -- How the fuck am I gonna calc this
  26.     local startEXP = 5 -- Amount of fuel needed to reach the periodic mining pattern from start
  27.     local loopsEXP = 6 -- Amount of fuel needed to mine 2 steps in the staircase.
  28.  
  29.     local totalConsumption = startEXP
  30.  
  31.     for i = 1,math.floor(DEPTH/2),1 do -- math floor in case DEPTH is odd
  32.         totalConsumption = totalConsumption + loopsEXP
  33.     end
  34.  
  35.     if ODD_DEPTH then
  36.         totalConsumption = totalConsumption + loopsEXP/2 --Gotta mine out that awkward half period
  37.     end
  38.  
  39.     if totalConsumption < turtle.getFuelLevel() then -- If there's enough fuel
  40.         return true
  41.     end
  42.  
  43.     return false
  44. end
  45.  
  46. local function beginStairs() -- Mine out the starting pattern
  47.     -- This code is to run at the start of the mining sequence, to reach a point where the periodic mining pattern can begin.
  48.  
  49.     -- Mine the initial block
  50.     turtle.digDown()
  51.     turtle.down()
  52.  
  53.     -- Next stage is a 2x3 area
  54.     for _ = 1,3,1 do
  55.         turtle.dig()
  56.         turtle.forward()
  57.         turtle.digDown()
  58.     end
  59.  
  60.     -- Move into position to begin periodic staircase mining
  61.     turtle.down()
  62. end
  63.  
  64. local function twoStairs() -- Mine two stair layers
  65.     -- This funciton mines out the actual stairs and ends at a known position
  66.     -- This algorithm isn't exactly efficient or clean. Oh well!
  67.     turtle.digDown()    -- 1
  68.     turtle.down()
  69.     turtle.turnLeft() -- turning around is free anyway so who cares
  70.     turtle.turnLeft()
  71.     turtle.dig()        -- 2
  72.     turtle.digDown()    -- 3
  73.     turtle.turnLeft()
  74.     turtle.turnLeft()
  75.     turtle.dig()        -- 4
  76.     turtle.forward()
  77.     turtle.dig()        -- 5
  78.     turtle.digDown()    -- 6
  79.     turtle.digUp()      -- 7
  80.     turtle.up()
  81.     turtle.digUp()      -- 8
  82.     turtle.dig()        -- 9
  83.     turtle.forward()
  84.     turtle.down()
  85.     turtle.digDown()    -- and 10!
  86.     turtle.down()
  87. end
  88.  
  89. local function finalStep()
  90.     turtle.up()
  91.     turtle.dig()        -- 1
  92.     turtle.down()
  93.     turtle.dig()        -- 2
  94.     turtle.digDown()    -- 3
  95.     turtle.down()
  96.     turtle.dig()
  97.     turtle.turnLeft()
  98.     turtle.turnLeft()
  99.     turtle.dig()
  100.     turtle.turnLeft()
  101.     turtle.turnLeft()
  102. end
  103.  
  104. local function mineStairs()
  105.     beginStairs()
  106.  
  107.     for _ = 1,math.floor((DEPTH-2)/2),1 do -- as this funciton mines two layers, only need to call it n/2 times.
  108.         twoStairs()
  109.     end
  110.  
  111.     if ODD_DEPTH then
  112.         finalStep()
  113.     end
  114. end
  115.  
  116. local function main()
  117.     if calculateFuelExpenditure() == true then
  118.         mineStairs()
  119.     else
  120.         --expand on this bit please future me :3
  121.         print("Not enough fuel.")
  122.     end
  123. end
  124.  
  125. if initialize() then
  126.     main()
  127. end
  128.  
  129. print("Runtime finished, exiting program")
Add Comment
Please, Sign In to add comment