TangentFox icon

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

TangentFox | PRO | 05/24/22 10:22:41 PM UTC (Edited) | 0 ⭐ | 361 👁️ | Never ⏰ | []
Lua |

3.37 KB

|

None

|

0 👍

/

0 👎

-- version 4.4
 
local slots = {
  first_free = 4,
  building_material = 3,
  torch = 2,
  fuel = 1,
}
 
local server_render_distance = 9
local options = {
  mine_up = true,
  mine_down = true,
  place_torches = true,
  place_floor = false,
  torch_frequency = 13,
  use_any_fuel = true,
}
 
local function refuel(goal)
  if turtle.getFuelLevel() >= goal then
    return true
  end
  local function refuel_loop()
    if turtle.refuel(0) then
      while turtle.refuel(1) do
        if turtle.getFuelLevel() >= goal then
          return true
        end
      end
    end
  end
  if options.use_any_fuel then
    for i = 1, 16 do
      turtle.select(i)
      if refuel_loop() then
        return true
      end
    end
  end
  turtle.select(slots.fuel)
  return refuel_loop()
end
 
local function forward(options)
  while turtle.detect() do
    if not turtle.dig() then
      print("Could not dig forward!")
      break
    end
  end
  local success = turtle.forward()
  if options.mine_up then
    while turtle.detectUp() do
      if not turtle.digUp() then
        print("Could not dig above!")
        break
      end
    end
  end
  if options.mine_down then
    if turtle.detectDown() then
      if not turtle.digDown() then
        print("Could not dig down!")
      end
    end
  end
  if options.place_floor then
    if not turtle.detectDown() then
      turtle.select(slots.building_material)
      if not turtle.placeDown() then
        print("Could not place below!")
      end
    end
  end
  return success
end
 
local function place_torch()
  assert(turtle.turnRight(), "Can't turn right! D:")
  turtle.select(slots.torch)
  local success = turtle.placeUp()
  assert(turtle.turnLeft(), "Can't turn left! D:")
end
 
local function turn_around()
  return turtle.turnLeft() and turtle.turnLeft()
end
 
local function mine()
  local success = true
  local shaft_distance = (1 + server_render_distance) * 16 / 2
  local fuel_required = shaft_distance * 2
 
  while not refuel(fuel_required) do
    local append = "Add fuel to any slot."
    if not options.use_any_fuel then
      append = "Add fuel to slot " .. slots.fuel .. "."
    end
    print("Turtle has " .. turtle.getFuelLevel() .. "/" .. fuel_required .. " fuel. " .. append)
    sleep(1)
  end
 
  local distance_remaining = shaft_distance
  while distance_remaining > 0 do
    if forward(options) then
      distance_remaining = distance_remaining - 1
      if options.place_torches and (distance_remaining % options.torch_frequency == options.torch_frequency - 1) then
        if not place_torch() then
          print("Can't place torch!")
        end
      end
      if turtle.getItemCount(16) > 0 then
        print("Inventory full, returning!")
        success = false
        break
      end
    else
      print("Couldn't move forward, returning!")
      success = false
      break
    end
  end
 
  assert(turn_around(), "Can't turn around! D:")
  while distance_remaining < shaft_distance do
    assert(forward({}), "Can't move forward! D:")
    distance_remaining = distance_remaining + 1
  end
  for i = slots.first_free, 16 do
    turtle.select(i)
    if not turtle.drop() then
      print("Can't drop items!")
    end
  end
  turtle.select(1)
  assert(turn_around(), "Can't turn around! D:")
  return success
end
 
while not mine() do end
 

Comments