139 lines
3.3 KiB
Lua
139 lines
3.3 KiB
Lua
args = {...}
|
|
|
|
function noop() end
|
|
|
|
function pair(x, y)
|
|
return math.floor((x + y) * (x + y + 1)/2 + y)
|
|
end
|
|
|
|
function unpairX(z)
|
|
local j = math.floor(math.sqrt(0.25 + 2*z) - 0.5)
|
|
return j - (z - j*(j+1)/2)
|
|
end
|
|
|
|
|
|
function unpairZ(z)
|
|
local j = math.floor(math.sqrt(0.25 + 2*z) - 0.5)
|
|
return z - j * (j+1)/2;
|
|
end
|
|
|
|
lookup = {4, 1, 3, 0, 2}
|
|
local function isDiggingSpot(x, y)
|
|
local val = lookup[(y % 5) + 1]
|
|
return (x % 5) == val
|
|
end
|
|
|
|
offset = 10
|
|
function ithSpot(i)
|
|
j = offset
|
|
while true do
|
|
x, z = unpairX(j), unpairZ(j)
|
|
if isDiggingSpot(x, z) then
|
|
if i <= 1 then
|
|
return x, z
|
|
else
|
|
i = i - 1
|
|
end
|
|
end
|
|
j = j + 1
|
|
end
|
|
end
|
|
|
|
function digTo(x, z)
|
|
print("Digging to rel. coord ("..tostring(x)..", "..tostring(z)..")")
|
|
while z > 0 do
|
|
turtle.dig()
|
|
turtle.forward()
|
|
turtle.digUp()
|
|
z = z - 1
|
|
end
|
|
turtle.turnRight()
|
|
while x > 0 do
|
|
turtle.dig()
|
|
turtle.forward()
|
|
turtle.digUp()
|
|
x = x - 1
|
|
end
|
|
end
|
|
|
|
-- If the block in front of the turtle is desired, take it.
|
|
function grabOres()
|
|
local status, block = turtle.inspect()
|
|
if status and block["name"]:lower():find("ore") then
|
|
turtle.dig()
|
|
end
|
|
end
|
|
|
|
-- dig all the way down to bedrock, collect ores
|
|
-- Parameters:
|
|
-- * moveAction: the **vertical** move action, eg turtle.down
|
|
-- * digAction: the **vertical** dig action, eg turtle.digDown
|
|
-- * inpectAction: the **vertical** inspect action, eg turtle.inspectDown
|
|
-- * limit: if >= 0 * the number of blocks to dig
|
|
-- * dig until bedrock is hit
|
|
--
|
|
--
|
|
-- Returns: the depth dug
|
|
function digDeep(moveAction, digAction, inspectAction, limit)
|
|
local depth = 0
|
|
while true do
|
|
-- termination condition:
|
|
-- * if limit != -1: limit > 0
|
|
-- * otherwise : bedrock beneath
|
|
if limit < 0 then
|
|
local status, nextblock = inspectAction()
|
|
if status and nextblock["name"] == "minecraft:bedrock" then
|
|
return depth
|
|
end
|
|
else
|
|
if limit == 0 then
|
|
return depth
|
|
end
|
|
end
|
|
-- Limit not reached: keep digging
|
|
depth = depth + 1
|
|
limit = limit - 1
|
|
digAction()
|
|
moveAction()
|
|
grabOres() -- inspect block in front of turtle, take if neccessary
|
|
end
|
|
end
|
|
|
|
function selectCobble()
|
|
for i = 1,16 do
|
|
turtle.select(i)
|
|
item = turtle.getItemDetail()
|
|
if item and item["name"] == "minecraft:cobblestone" then
|
|
break
|
|
end
|
|
end
|
|
end
|
|
|
|
function digShaft()
|
|
turtle.digDown()
|
|
turtle.down()
|
|
selectCobble()
|
|
turtle.placeUp()
|
|
print("Starting to dig shaft")
|
|
depth = digDeep(turtle.down, turtle.digDown, turtle.inspectDown, -1)
|
|
print("Dug the first shaft (depth = "..tostring(depth)..")")
|
|
turtle.turnLeft()
|
|
digDeep(turtle.up, noop, noop, depth)
|
|
turtle.turnLeft()
|
|
digDeep(turtle.down, noop, noop, depth)
|
|
turtle.turnLeft()
|
|
digDeep(turtle.up, noop, noop, depth)
|
|
turtle.turnLeft()
|
|
turtle.up()
|
|
turtle.up()
|
|
selectCobble()
|
|
turtle.placeDown()
|
|
end
|
|
|
|
spotNum = tonumber(args[1])
|
|
if spotNum then
|
|
x, z = ithSpot(spotNum)
|
|
digTo(x,z)
|
|
digShaft()
|
|
end
|