Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- mining01.lua
- -- 入っている燃料で行ける分だけ前に掘っていって、戻ってくるプログラム
- -- ユーザ定義
- PROGRAM_NAME = "mining"
- FUEL_SLOT = 16
- MINFUEL = 5
- MAX_LEN = 100
- MAX_RETRY = 15
- -- for system
- local ARGS = {...}
- -- 関数
- -- インベントリを調べて丸石捨てる
- -- onlyOnece : bool 1スタック捨てたらそれでやめるか
- -- return (true/false) 捨てるのに成功したかどうか
- local function dropStones(onlyOnce)
- local droped = false --捨てるのに成功したかのフラグ
- for i=1,16 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if item ~= nil and item.name == "minecraft:cobblestone" then
- turtle.dropDown()
- droped = true
- if onlyOnce == true then
- turtle.select(1)
- return true
- end
- end
- end
- turtle.select(1)
- return droped
- end
- local function writeResumeFile()
- if not fs.exists("working") then
- return
- end
- local fh = fs.open("working/"..PROGRAM_NAME, "w")
- fh.writeLine(PROGRAM_NAME.." -r")
- fh.close()
- end
- local function delResumeFile()
- fs.delete("working/"..PROGRAM_NAME)
- end
- local function digHere()
- turtle.turnRight()
- turtle.dig()
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.dig()
- turtle.turnRight()
- turtle.digUp()
- turtle.digDown()
- end
- local function writeCurrentData(data)
- if not fs.exists("memory") then
- return
- end
- local fh = fs.open("memory/"..PROGRAM_NAME, "w")
- local tempStr = textutils.serialise(data)
- fh.write(tempStr)
- fh.close()
- end
- local function delCurrentData()
- fs.delete("memory/"..PROGRAM_NAME)
- end
- -- init
- local data = {state = "fwd", fwdCount = 0, backCount = 0}
- local resumed = false
- -- 復帰用ファイルの書き込み
- writeResumeFile()
- for i=1, table.maxn(ARGS) do
- local temp = ARGS[i]
- if temp == "-r" then
- resumed = true
- if fs.exists("memory/"..PROGRAM_NAME) then
- local fh = fs.open("memory/"..PROGRAM_NAME, "r")
- data = textutils.unserialise(fh.readAll())
- fh.close()
- if type(data.fwdCount) ~= "number" then
- data.fwdCount = 0
- end
- if type(data.backCount) ~= "number" then
- data.backCount = 0
- end
- end
- elseif temp == "--max-len" then
- MAX_LEN = tonumber(ARGS[i+1])
- i = i + 1
- end
- end
- if not resumed then
- turtle.select(FUEL_SLOT)
- turtle.refuel()
- if turtle.getFuelLevel() <= MINFUEL then
- print("can not refuel enogh fuel!")
- return
- end
- turtle.select(1)
- end
- -- 燃料がなくなるか最大の長さになるまで掘り進む
- local retryCount = 0
- while data.state == "fwd" and turtle.getFuelLevel() > data.fwdCount+MINFUEL and MAX_LEN > data.fwdCount do
- turtle.dig()
- if turtle.forward() then
- data.fwdCount = data.fwdCount + 1
- retryCount = 0
- writeCurrentData(data)
- else
- if retryCount >= MAX_RETRY then
- break
- end
- print("susumenai")
- turtle.dig()
- turtle.attack()
- retryCount = retryCount + 1
- end
- digHere()
- turtle.select(16)
- if turtle.getItemDetail() ~= nil then
- if not dropStones(true) then
- print("Go back. because of full inventory")
- break
- end
- end
- turtle.select(1)
- end
- -- 元の位置に戻る
- if data.state == "fwd" then
- print("no enough fuel. I will go back start position.")
- turtle.turnRight()
- turtle.turnRight()
- data.state = "rtn"
- end
- retryCount = 0
- while data.state == "rtn" and data.fwdCount > data.backCount do
- if turtle.forward() then
- data.backCount = data.backCount + 1
- retryCount = 0
- writeCurrentData(data)
- else
- if retryCount >= MAX_RETRY then
- data.state = "err"
- break
- end
- print("susumenai")
- turtle.dig()
- turtle.attack()
- retryCount = retryCount + 1
- end
- end
- if data.state == "rtn" then
- turtle.turnRight()
- turtle.turnRight()
- data.state = "done"
- end
- delResumeFile()
- delCurrentData()
- print("modotta!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement