Skip to content

Instantly share code, notes, and snippets.

@dustinrouillard
Last active May 2, 2022 08:35
Show Gist options
  • Save dustinrouillard/35cbc18564c860b5f529d359aee21125 to your computer and use it in GitHub Desktop.
Save dustinrouillard/35cbc18564c860b5f529d359aee21125 to your computer and use it in GitHub Desktop.
Tunnel digger for ComputerCraft - 3x3 Tunnels, Torch Placer, Auto Refueling - W.I.P

Installing

To install this on your ComputerCraft Turtle run the following command

wget run https://dstn.to/cc-bore

or if you'd rather have the direct url to the install.lua from above instead of the short url

wget run https://gist.githubusercontent.com/dustinrouillard/35cbc18564c860b5f529d359aee21125/raw/install.lua
os.loadAPI("/lib/apis/bore/utils.lua")
os.loadAPI("/lib/apis/bore/constants.lua")
term.clear()
utils.printCenter("Welcome to the tunnel bore 9000")
utils.printCenter("dstn.to", true, true)
utils.refuel()
term.clear()
local currentItteration = 0
local totalTorches = 0
for i = 1, 16 do
local item = turtle.getItemDetail(i)
if item and item.name == "minecraft:torch" then
totalTorches = totalTorches + turtle.getItemCount(i)
end
end
if totalTorches >= 1 then
utils.printTop("Torch mode is enabled as there is "..totalTorches.." in the inventory")
os.sleep(1.5)
term.clear()
end
utils.printTop("Tunnel distance")
utils.printTop("How far we goin?", false)
local distanceEntry = read()
local distance = tonumber(distanceEntry)
term.clear()
if distance == nil then
utils.printTop("Exiting...")
utils.printTop()
utils.printTop("Invalid value, enter a distance in meters (blocks)", false)
error()
end
local torchFirst = true
local torchBlockGap = 7
if totalTorches >= 1 then
utils.printTop("Options")
utils.printTop("Torch on first wall? ([y]/n)", false)
torchFirst = read()
if torchFirst == nil then torchFirst = true end
if torchFirst ~= "y" and torchFirst ~= "n" then
utils.printTop("Invalid value, enter 'y' or 'n'", false)
error()
end
utils.printTop("Torch block gap [7]", false)
local torchBlockGap = read()
if torchBlockGap == "" then torchBlockGap = 7 end
torchBlockGap = tonumber(torchBlockGap)
if torchBlockGap == nil then
utils.printTop("Invalid value, enter a torch block gap in meters (blocks)", false)
error()
end
term.clear()
end
local defaultPosition = "bottom"
if not turtle.detectDown() then defaultPosition = "middle" end
utils.printTop("3x3 Start position")
utils.printTop("Middle or Bottom? ["..defaultPosition.."]:", false)
local middleOrBottom = read()
term.clear()
if string.lower(middleOrBottom) == "middle" then
middleOrBottom = "middle"
elseif string.lower(middleOrBottom) == "bottom" then
middleOrBottom = "bottom"
else
if middleOrBottom == "" then
middleOrBottom = defaultPosition
else
utils.printTop("Exiting...")
utils.printTop()
utils.printTop("Invalid option, valid options [middle, bottom]", false)
error()
end
end
utils.printCenter("Creating a "..distance.."m 3x3 tunnel")
if middleOrBottom == "bottom" then turtle.up() end
function doDigging()
currentItteration = currentItteration + 1
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
turtle.back()
if (currentItteration == 1 and torchFirst) or utils.mod(currentItteration, torchBlockGap) then utils.placeTorch() end
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
turtle.back()
if (currentItteration == 1 and torchFirst) or utils.mod(currentItteration, torchBlockGap) then utils.placeTorch() end
turtle.turnLeft()
end
for i = 1, distance do
local currentFuel = turtle.getFuelLevel()
if currentFuel-constants.fuelNeeded < 0 then
local refueled = utils.refuel()
if refueled == false then
print("Sorry but there is not enough fuel to break down the next wall, please input some coal")
exit()
end
end
doDigging()
end
fuelNeeded = 5
-- 3x3 Tunnel bore
-- Created by Dustin Rouillard
-- on 05/01/22
--
-- Features:
-- Can auto place torches with gaps between
-- Mines in the most efficient way (In, up, down, left, repeat, right, repeat)
-- Will work started from the middle of the 3x3 or the bottom
-- Auto refueling
-- Smart stop (It won't even try the next wall if there is not enough fuel)
--
-- Options:
-- When started the script will prompt you with 4 questions
-- Distance: This is the distance in meters (block) from the first wall
-- Options: You will get 2 questions here about torches (If any exist in the bottom 4 slots)
-- Position: This is the starting position of the turtle (Bottom or Middle of the 3x3 wall)
--
-- Plans
-- . Check the block in front is not on stopBlocks list
-- . Calulate the fuel needed for the distance of tunnel with math instead of hard coded
-- . Show the calulated fuel cost, along with current and if there is not enough to finish the job
-- wait for user prompt to continue, then we will refuel and run
-- . Auto dropoff of items
-- . Auto refuel from point when almost empty with no fuel in inv
local appname = "bore"
local gistId = "35cbc18564c860b5f529d359aee21125"
local apis = {"utils.lua", "constants.lua"}
local contents = http.get("https://api.github.com/gists/"..gistId, { [ "User-Agent" ] = "CC Script Downloader/dstn.to 1.0.0" })
local data = textutils.unserializeJSON(contents.readAll())
for i = 1, table.getn(apis) do
local file = data.files[apis[i]]
if file ~= nil then
print("Downloading to /lib/apis/bore "..apis[i].."...")
shell.run("wget", file.raw_url, "/lib/apis/bore/"..apis[i])
end
end
local scriptUrl = data.files[appname..".lua"].raw_url
print("Downloading to / "..appname..".lua...")
shell.run("wget", scriptUrl, "/"..appname..".lua")
print()
print("Successfully installed "..appname.." to /")
os.loadAPI("/lib/apis/bore/constants.lua")
function printCenter(text, rePlace, bottom)
if rePlace == nil then rePlace = true end
if bottom == nil then bottom = false end
if text == nil then text = "" end
local w, h = term.getSize()
if rePlace then
if bottom then
term.setCursorPos(math.floor(w-string.len(text))/2, h)
else
term.setCursorPos(math.floor(w-string.len(text))/2, 7)
end
else
local x, y = term.getCursorPos()
term.setCursorPos(math.floor(w-string.len(text))/2, y)
end
print(text)
end
function printTop(text, rePlace)
if rePlace == nil then rePlace = true end
if text == nil then text = "" end
if rePlace then term.setCursorPos(1, 1) end
print(text)
end
function placeTorch()
local slotWithTorches = nil
for i = 1, 16 do
if turtle.getItemCount(i) >= 1 then slotWithTorches = i end
end
if slotWithTorches ~= nil then
turtle.select(slotWithTorches)
turtle.place()
end
end
function mod(a, b)
return math.mod(a, b) == 0
end
function has_value (tab, val)
for index, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
function refuel()
local gotFueled = false
for i = 1, 16 do
turtle.select(i)
local fueled = turtle.refuel()
if fueled then
gotFueled = true
end
end
return gotFueled
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment