Skip to content

Instantly share code, notes, and snippets.

@forlornhedgehog
Last active December 12, 2015 09:18
Show Gist options
  • Save forlornhedgehog/4749974 to your computer and use it in GitHub Desktop.
Save forlornhedgehog/4749974 to your computer and use it in GitHub Desktop.
A lua script for the ComputerCraft Minecraft mod, that will mine in a spiral pattern, exposing the most blocks, with the least fuel consumed.
-- This work is licensed under a CC BY-NC-SA 3.0 license.
-- http://creativecommons.org/licenses/by-nc-sa/3.0/
--
-- by the Forlorn Hedgehog
--
-- Fuel goes in slot 1. Torches go in slot 2. Floor blocks (usually cobblestone) go in slot 3.
-- If fuel level is less than 10, refuel
function fuel()
if turtle.getFuelLevel() < 10 then
turtle.select(1)
if turtle.refuel(1) then
return true
end
print("Refuelling failed.")
return false
end
end
-- Place floor block, if there isn't one.
function placeFloorBlock()
if not turtle.detectDown() then
turtle.select(3)
if turtle.placeDown() then
return true
end
print("Placing floor block failed.")
return false
end
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
placeFloorBlock()
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
-- 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: spiralMine <diameter>")
return
end
local diameter = tonumber(tArgs[1])
local blocksMoved = 0
function doThings(num_of_things)
fuel()
for i=1,num_of_things do
basicDigAndMove()
blocksMoved = blocksMoved + 1
-- Add torch (on floor) every 8 blocks
if (blocksMoved % 8) == 0 then
turnAround()
placeTorch()
turnAround()
end
end
turtle.turnRight()
local otherNum = num_of_things - 1
if otherNum > 0 then
fuel()
for i=1,otherNum do
basicDigAndMove()
blocksMoved = blocksMoved + 1
-- Add torch (on floor) every 8 blocks
if (blocksMoved % 8) == 0 then
turnAround()
placeTorch()
turnAround()
end
end
turtle.turnRight()
local finalNum = otherNum - 2
if finalNum > 0 then
doThings(finalNum)
end
end
end
doThings(diameter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment