Skip to content

Instantly share code, notes, and snippets.

@atomgiant
Last active March 20, 2021 01:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomgiant/4982440 to your computer and use it in GitHub Desktop.
Save atomgiant/4982440 to your computer and use it in GitHub Desktop.
MineCraft Lua scripts
h=fs.open("/gfs","w");h.write(http.get("https://gist.github.com/ntalbott/4719172/raw/gfs.lua").readAll());h.close();print "Done";
-- tunnel <width>, <depth>, <height>
-- height may be negative to go down or positive to go up
-- place fuel in any cell
-- optionally place torches in cell 16 (bottom right)
-- digs a an x,y,z box ending at the same point as the start
-- the Turtle starts mining immediately so depth 1 or -1 count as the current height
function tunnel(width, depth, height)
print("digging tunnel of width: " .. width .. ", depth: " .. depth .. ", height: " .. height)
width = tonumber(width)
depth = tonumber(depth)
height = tonumber(height)
local goDown = false
if height < 0 then
goDown = true
height = height * -1
end
for z = 1,height do
local useTorch = z == 2
plane(width, depth, useTorch)
-- don't go up or down on the last row
if z < height then
if z % 10 == 0 then
placeTorch()
end
if goDown then
down()
else
up()
end
end
end
-- return to surface
for z = 2,height do
if goDown then
up()
else
down()
end
end
end
-- creates num mine shafts of a given depth
function mineShafts(num, depth)
depth = depth or 50
print("mining " .. num .. " shafts of depth: " .. depth)
num = tonumber(num)
depth = tonumber(depth)
for i = 1,num do
path(depth)
local turn = 'turnLeft'
if i % 2 == 0 then
-- even rows turn right
turn = 'turnRight'
end
-- dig side path to next tunnel
turtle[turn]()
path(3)
turtle[turn]()
end
end
-- digs a 2 block high path of a certain depth, planting torches along the way
--
-- the path is dug in such as way as to allow a player to walk behind it
function path(depth)
print("digging path of depth: " .. depth)
depth = tonumber(depth)
for i = 1,depth do
forward()
up()
if i % 10 == 0 then
placeTorch()
end
down()
end
end
function plane(width, depth, useTorch)
for x = 1,width do
for y = 2,depth do
if useTorch and x == 1 and y % 10 == 0 then
placeTorch()
end
forward()
end
-- don't turn if we are at the end
if x < width then
local whichTurn = (x % 2 == 0) and "turnLeft" or "turnRight"
turtle[whichTurn]()
forward()
turtle[whichTurn]()
end
end
-- return to 0,0
if width % 2 == 0 then
turtle.turnRight()
else
turtle.turnLeft()
turtle.turnLeft()
for y = 2,depth do
forward()
end
turtle.turnRight()
end
for x = 2,width do
forward()
end
-- turn back the original way we were facing
turtle.turnRight()
end
function forward()
digForward()
turtle.forward()
end
-- will dig forward until there is nothing infront (to handle gravel / sand that falls)
function digForward()
refuel()
-- dig until there is nothing in front of us
while turtle.detect() do
turtle.dig()
end
end
function up()
digUp()
turtle.up()
end
function digUp()
refuel()
while turtle.detectUp() do
turtle.digUp()
end
end
function down()
digDown()
turtle.down()
end
function digDown()
refuel()
while turtle.detectDown() do
turtle.digDown()
end
end
function refuel()
while not doRefuel() do
print "waiting for fuel - place in any slot"
os.sleep(1)
end
end
function doRefuel()
if turtle.getFuelLevel() >= 10 then
-- skip refuel
return true
end
for i = 1,16 do
turtle.select(i)
if turtle.refuel(1) then
return true
end
end
return false
end
function placeTorch()
if turtle.getItemCount(16) < 1 then
print "uh oh, no torches in slot 16.. skipping"
else
turtle.turnLeft()
digForward()
turtle.select(16)
turtle.place()
turtle.turnRight()
end
end
-- refresh all gfs loaded files
shell.run("/gfs", "refresh", string.match(shell.dir(), "%w+$"))
os.loadAPI(shell.dir() .. "/dig")
dig.mineShafts(...)
@atomgiant
Copy link
Author

@ntalbott - You can paste this into MineCraft in one line now with Ctrl-V. No floppy needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment