-- #############################
-- cutland
-- version 0.1
-- http://hevohevo.hatenablog.com/
-- #############################
-- config (default parameters)
DEPTH = 4 -- okuyuki
WIDTH = 4 -- haba
TOARCH_SLOT = 15
CHEST_SLOT = 16
-- #############################
-- functions
local current_height = 1
function surelyDigUp()
while turtle.digUp() do
os.sleep(0.4)
end
end
function surelyUp()
surelyDigUp()
local status, error_msg = turtle.up()
if status then current_height = current_height + 1 end
return status, error_msg
end
function surelyDown()
turtle.digDown()
local status, error_msg = turtle.down()
if status then current_height = current_height - 1 end
return status, error_msg
end
function surelyDig()
while turtle.dig() do end
end
function surelyFwd()
for i = 1, 4 do
local status, err = turtle.forward()
if status then
return true -- success!
elseif err == "Out of fuel" then
return status, err
end
surelyDig() -- face to a normal block or a sand(gravel) hill
if turtle.detect() and not turtle.dig() then
return false, "bedrock!"
end
if turtle.forward() then return true end -- success!
if turtle.attack() then
-- face to monster-mob
while turtle.attack() do end
else
-- face to sand-blocks which is dropping long time
os.sleep(5) -- probably, adjustment is required
end
end
return turtle.up()
end
-- go to the top of a facing block tower
function goTop()
while turtle.detectUp() or turtle.detect() do
surelyUp()
end
end
-- down() to the initial height
-- if argument exists, down() and dig() to facing block
function goBottom(func)
while current_height > 1 do
if func then func() end -- if func isn\'t nil, dig foward
surelyDown()
end
end
function seichiThreeStep(current_d, current_w)
goTop()
surelyFwd()
goTop()
goBottom(surelyDig)
-- place toarch
placeToarch(current_d, current_w)
-- place chest and put items
putItems()
surelyFwd()
end
function seichiTwoStep()
goTop()
surelyFwd()
goBottom()
end
function seichiOneStep()
goTop()
goBottom()
end
-- use seichiThreeStep() as much as possible
function seichiOneLine(current_w)
local div3 = math.floor(DEPTH / 3)
local mod3 = DEPTH % 3
local current_d = 1
for i = 1, div3 do
seichiThreeStep(current_d, current_w)
current_d = current_d + 2
if current_d < DEPTH then
surelyFwd()
current_d = current_d + 1
end
end
if mod3 == 1 then
seichiOneStep()
elseif mod3 == 2 then
seichiTwoStep()
end
end
-- placeItem(TOARCH_SLOT): place() TOARCH_SLOT\'s item
-- placeItem(CHEST_SLOT, turtle.placeUp): use placeUp()
function placeItem(slot, place_func)
local place_func = place_func or turtle.place
if turtle.getItemCount(slot) == 0 then return false end
turtle.select(slot)
local status, error_msg = place_func()
turtle.select(1)
return status, error_msg
end
-- dropAllItems(): dropDown items within 1-14 slots
-- dropAllItems(turtle.dropUp): use dropUp() function
function dropAllItems(drop_func)
local drop_func = drop_func or turtle.dropDown
for i = 1, 14 do
if turtle.getItemCount(i) > 0 then
turtle.select(i)
drop_func()
end
end
turtle.select(1)
end
-- put items as soon as inventory becomes fill (slot 12)
function putItems()
if turtle.getItemCount(12)>0 then
if placeItem(CHEST_SLOT, turtle.placeUp) then
dropAllItems(turtle.dropUp)
elseif turtle.getItemCount(14)>0 then
error("Inventory is full!") -- terminate if can\'t put
end
end
end
-- turn around and place toarch at even intervals (5 spaces)
function placeToarch(current_d, current_w)
if current_d % 2 == 0 and current_w % 6 == 1 then
print(string.format("D/W = %d/%d", current_d, current_w))
turtle.turnRight(); turtle.turnRight()
print(\'place a toarch: \', placeItem(TOARCH_SLOT))
turtle.turnRight(); turtle.turnRight()
end
end
-- #############################
-- main
-- arguments setting
local args = { ... }
if #args == 2 or #args == 0 then
DEPTH = tonumber(args[1]) or DEPTH
WIDTH = tonumber(args[2]) or WIDTH
else
error(\'seichi <DEPTH> <WIDTH>\')
end
print(string.format("D/W = %d/%d", DEPTH, WIDTH))
-- start
surelyFwd()
for w = 1, WIDTH do
seichiOneLine(w)
if WIDTH > w and w % 2 == 1 then -- current width pos is odd number
turtle.turnRight()
surelyFwd()
turtle.turnRight()
elseif WIDTH > w and w % 2 == 0 then -- current width pos is even number
turtle.turnLeft()
surelyFwd()
turtle.turnLeft()
end
end
print("current fuel: ", turtle.getFuelLevel())