Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- #############################
- -- cutland
- -- version 0.2
- -- http://hevohevo.hatenablog.com/
- -- #############################
- -- config (default parameters)
- DEPTH = 4 -- okuyuki
- WIDTH = 1 -- haba
- CAVE_HEIGHT = 0 -- expect the hight of cave
- 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
- -- sadly a turtle can only detect neighbor blocks
- -- the turtle go up for touching tha celling of cave
- function goTop()
- repeat
- for i=1,(CAVE_HEIGHT-1) do
- surelyUp()
- end
- local flag_rose= true
- while turtle.detectUp() or turtle.detect() do
- surelyUp()
- flag_rose= false -- execute from beginning, if detects a block
- end
- until flag_rose
- end
- -- down() to the initial height
- -- if argument exists, down() and dig() to facing block
- function goBottom(surelyDig_flag)
- while current_height > 1 do
- surelyDown()
- if surelyDig_flag then surelyDig() end
- 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(current_d, current_w)
- surelyFwd()
- end
- function seichiTwoStep()
- goTop()
- surelyFwd()
- goBottom(false)
- end
- function seichiOneStep()
- goTop()
- goBottom(false)
- 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
- -- two types of put items function:
- -- 'regulary' without investigating inventory and 'as_soon_as' it becomes fill
- function putItems(current_d, current_w)
- -- put items regularly
- if current_w % 4 == 1 and current_d < 4 then
- -- placeDown a chest to put items
- if turtle.getItemCount(2) > 0 and placeItem(CHEST_SLOT, turtle.placeUp) then
- dropAllItems(turtle.dropUp)
- elseif turtle.getItemCount(14) > 0 then
- error("Inventory is full!")
- 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 == 3 or #args == 2 or #args==0 then
- DEPTH = tonumber(args[1]) or DEPTH
- WIDTH = tonumber(args[2]) or WIDTH
- CAVE_HEIGHT = tonumber(args[3]) or CAVE_HEIGHT -- expect the height of cave
- else
- error('seichi <DEPTH> <WIDTH> (CAVE_HEIGHT)')
- end
- print(string.format("D/W = %d/%d, CaveHeight=%d", DEPTH, WIDTH, CAVE_HEIGHT))
- -- 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())
Add Comment
Please, Sign In to add comment