TangentFox

mine.lua v4.4 (Basic ComputerCraft shaft miner)

May 7th, 2022 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1. -- version 4.4
  2.  
  3. local slots = {
  4.   first_free = 4,
  5.   building_material = 3,
  6.   torch = 2,
  7.   fuel = 1,
  8. }
  9.  
  10. local server_render_distance = 9
  11. local options = {
  12.   mine_up = true,
  13.   mine_down = true,
  14.   place_torches = true,
  15.   place_floor = false,
  16.   torch_frequency = 13,
  17.   use_any_fuel = true,
  18. }
  19.  
  20. local function refuel(goal)
  21.   if turtle.getFuelLevel() >= goal then
  22.     return true
  23.   end
  24.   local function refuel_loop()
  25.     if turtle.refuel(0) then
  26.       while turtle.refuel(1) do
  27.         if turtle.getFuelLevel() >= goal then
  28.           return true
  29.         end
  30.       end
  31.     end
  32.   end
  33.   if options.use_any_fuel then
  34.     for i = 1, 16 do
  35.       turtle.select(i)
  36.       if refuel_loop() then
  37.         return true
  38.       end
  39.     end
  40.   end
  41.   turtle.select(slots.fuel)
  42.   return refuel_loop()
  43. end
  44.  
  45. local function forward(options)
  46.   while turtle.detect() do
  47.     if not turtle.dig() then
  48.       print("Could not dig forward!")
  49.       break
  50.     end
  51.   end
  52.   local success = turtle.forward()
  53.   if options.mine_up then
  54.     while turtle.detectUp() do
  55.       if not turtle.digUp() then
  56.         print("Could not dig above!")
  57.         break
  58.       end
  59.     end
  60.   end
  61.   if options.mine_down then
  62.     if turtle.detectDown() then
  63.       if not turtle.digDown() then
  64.         print("Could not dig down!")
  65.       end
  66.     end
  67.   end
  68.   if options.place_floor then
  69.     if not turtle.detectDown() then
  70.       turtle.select(slots.building_material)
  71.       if not turtle.placeDown() then
  72.         print("Could not place below!")
  73.       end
  74.     end
  75.   end
  76.   return success
  77. end
  78.  
  79. local function place_torch()
  80.   assert(turtle.turnRight(), "Can't turn right! D:")
  81.   turtle.select(slots.torch)
  82.   local success = turtle.placeUp()
  83.   assert(turtle.turnLeft(), "Can't turn left! D:")
  84. end
  85.  
  86. local function turn_around()
  87.   return turtle.turnLeft() and turtle.turnLeft()
  88. end
  89.  
  90. local function mine()
  91.   local success = true
  92.   local shaft_distance = (1 + server_render_distance) * 16 / 2
  93.   local fuel_required = shaft_distance * 2
  94.  
  95.   while not refuel(fuel_required) do
  96.     local append = "Add fuel to any slot."
  97.     if not options.use_any_fuel then
  98.       append = "Add fuel to slot " .. slots.fuel .. "."
  99.     end
  100.     print("Turtle has " .. turtle.getFuelLevel() .. "/" .. fuel_required .. " fuel. " .. append)
  101.     sleep(1)
  102.   end
  103.  
  104.   local distance_remaining = shaft_distance
  105.   while distance_remaining > 0 do
  106.     if forward(options) then
  107.       distance_remaining = distance_remaining - 1
  108.       if options.place_torches and (distance_remaining % options.torch_frequency == options.torch_frequency - 1) then
  109.         if not place_torch() then
  110.           print("Can't place torch!")
  111.         end
  112.       end
  113.       if turtle.getItemCount(16) > 0 then
  114.         print("Inventory full, returning!")
  115.         success = false
  116.         break
  117.       end
  118.     else
  119.       print("Couldn't move forward, returning!")
  120.       success = false
  121.       break
  122.     end
  123.   end
  124.  
  125.   assert(turn_around(), "Can't turn around! D:")
  126.   while distance_remaining < shaft_distance do
  127.     assert(forward({}), "Can't move forward! D:")
  128.     distance_remaining = distance_remaining + 1
  129.   end
  130.   for i = slots.first_free, 16 do
  131.     turtle.select(i)
  132.     if not turtle.drop() then
  133.       print("Can't drop items!")
  134.     end
  135.   end
  136.   turtle.select(1)
  137.   assert(turn_around(), "Can't turn around! D:")
  138.   return success
  139. end
  140.  
  141. while not mine() do end
  142.  
Add Comment
Please, Sign In to add comment