Skip to content

Instantly share code, notes, and snippets.

@gwillen
Forked from forlornhedgehog/branchMine.lua
Last active December 17, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwillen/5622200 to your computer and use it in GitHub Desktop.
Save gwillen/5622200 to your computer and use it in GitHub Desktop.
-- This work is licensed under a CC BY-NC-SA 3.0 license.
-- http://creativecommons.org/licenses/by-nc-sa/3.0/
--
-- by Spike Padley
--
-- Usage: branchMine <length>
-- Fuel goes in slot 1. Torches go in slot 2. Floor blocks (usually cobblestone) go in slot 3.
-- Mines a trunk <length> blocks long, with an 8 block long branch, on each side, every 3 blocks, starting 2 blocks in (n=3n-1)
local blocksMovedForward = 0
local blocksMovedSideways = 0
local kBoring = 2
-- If fuel level is less than needed to get home, refuel
function fuel()
if turtle.getFuelLevel() < blocksMovedForward + blocksMovedSideways + 50 then
print("Refuelling")
turtle.select(1)
if turtle.refuel(1) then
return true
end
print("Refuelling failed.")
return false
end
return true
end
-- Place floor block, if there is not already one.
function placeFloorBlock()
if not turtle.detectDown() then
print("Placing floor block")
if turtle.getItemCount(3) <= 1 then
print ("Out of floor blocks")
return false
end
turtle.select(3)
if turtle.placeDown() then
return true
end
print("Placing floor block failed.")
return false
end
return true
end
function mineMaybe()
for i = 16 - kBoring + 1, 16 do
turtle.select(i)
if turtle.compare() then
return false
end
end
for i = 2, 3 do
turtle.select(i)
if turtle.compare() then
return false
end
end
return turtle.dig()
end
function mineDownMaybe()
for i = 16 - kBoring + 1, 16 do
turtle.select(i)
if turtle.compareDown() then
return false
end
end
for i = 2, 3 do
turtle.select(i)
if turtle.compareDown() then
return false
end
end
return turtle.digDown()
end
function mineUpMaybe()
for i = 16 - kBoring + 1, 16 do
turtle.select(i)
if turtle.compareUp() then
return false
end
end
for i = 2, 3 do
turtle.select(i)
if turtle.compareUp() then
return false
end
end
return turtle.digUp()
end
-- Digs and moves into a 1x2x1 (xyz) area.
function basicDigAndMove()
while not turtle.forward() do
if not turtle.dig() then
turtle.attack()
end
end
while turtle.detectUp() do
turtle.digUp()
sleep(0.5)
end
mineDownMaybe()
turtle.turnLeft()
mineMaybe()
turnAround()
mineMaybe()
turtle.turnLeft()
if not placeFloorBlock() then
return false
end
return true
end
function basicMoveBack()
while not turtle.forward() do
if not turtle.dig() then
turtle.attack()
end
end
mineUpMaybe()
turtle.turnLeft()
mineMaybe()
turnAround()
mineMaybe()
turtle.turnLeft()
end
-- Turn turtle 180 degrees
function turnAround()
turtle.turnLeft()
turtle.turnLeft()
end
-- Place torch on floor, in front of the turtle
function placeTorch()
turtle.select(2)
if turtle.place() then
return true
end
print("Placing torch failed.")
return false
end
-- This function mines a 1x2x8 (xyz) tunnel directly infront of the turtle, placing a torch near the end, and returning to start point (but in opposite orientation)
-- pre/post condition: blocksMovedSideways == 0
function branch()
print("Doing a branch")
while blocksMovedSideways < 8 do
if not fuel() then
break
end
if not basicDigAndMove() then break end
blocksMovedSideways = blocksMovedSideways + 1
if blocksMovedSideways == 8 then
turnAround()
placeTorch()
end
end
print("Backing out of branch")
-- Back to start point (goes up to avoid the torch on floor)
while not turtle.up() do end
while blocksMovedSideways > 0 do
basicMoveBack()
blocksMovedSideways = blocksMovedSideways - 1
end
while not turtle.down() do end
end
function inventoryFull()
-- recover space by dumping cobblestone on the floor
for i = 4, 16 - kBoring do
if turtle.getItemCount(i) == 0 then
return false
end
end
madeSpace = false
for i = 4, 16 - kBoring do
turtle.select(i)
if turtle.compareTo(3) == true then
turtle.drop()
madeSpace = true
end
end
return not madeSpace
end
function outOfSupplies()
for i = 1,3 do
-- We need at least one cobblestone, to keep it in the right slot.
-- The others we could wait for zero if we wanted.
if turtle.getItemCount(i) <= 1 then
return true
end
end
return false
end
-- Arguments
local tArgs = {...}
if #tArgs ~= 1 or tonumber(tArgs[1]) == nil or math.floor(tonumber(tArgs[1])) ~= tonumber(tArgs[1]) or tonumber(tArgs[1]) < 1 then
print("Usage: branchMine <length>")
return
end
local length = tonumber(tArgs[1])
print("Starting branchMine...")
while blocksMovedForward < length do
if not fuel() then
break
end
if inventoryFull() then
break
end
if outOfSupplies() then
break
end
if not basicDigAndMove() then break end
blocksMovedForward = blocksMovedForward + 1
-- Add torch (on floor) every 8 blocks
if (blocksMovedForward % 8) == 0 then
turnAround()
placeTorch()
turnAround()
end
-- n=3n-1
if ((blocksMovedForward + 1) % 3) == 0 then
turtle.turnLeft()
branch()
branch()
turtle.turnRight()
end
end
print("Done mining, backing out...")
turnAround()
while not turtle.up() do end
while blocksMovedForward > 0 do
basicMoveBack()
blocksMovedForward = blocksMovedForward - 1
end
while not turtle.down() do end
for i = 4, 16 - kBoring do
turtle.select(i)
turtle.drop()
end
turtle.select(3)
turtle.drop(turtle.getItemCount(3) - 1)
turnAround()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment