Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.31 KB | None | 0 0
  1. -- mining01.lua
  2. -- 入っている燃料で行ける分だけ前に掘っていって、戻ってくるプログラム
  3.  
  4. -- ユーザ定義
  5. PROGRAM_NAME = "mining"
  6. FUEL_SLOT = 16
  7. MINFUEL = 5
  8. MAX_LEN = 100
  9. MAX_RETRY = 15
  10.  
  11. -- for system
  12. local ARGS = {...}
  13.  
  14. -- 関数
  15.  
  16. -- インベントリを調べて丸石捨てる
  17. -- onlyOnece : bool 1スタック捨てたらそれでやめるか
  18. -- return (true/false) 捨てるのに成功したかどうか
  19. local function dropStones(onlyOnce)
  20.     local droped = false --捨てるのに成功したかのフラグ
  21.     for i=1,16 do
  22.         turtle.select(i)
  23.         local item = turtle.getItemDetail()
  24.         if item ~= nil and item.name == "minecraft:cobblestone" then
  25.             turtle.dropDown()
  26.             droped = true
  27.             if onlyOnce == true then
  28.                 turtle.select(1)
  29.                 return true
  30.             end
  31.         end
  32.     end
  33.     turtle.select(1)
  34.     return droped
  35. end
  36.  
  37. local function writeResumeFile()
  38.     if not fs.exists("working") then
  39.         return
  40.     end
  41.     local fh = fs.open("working/"..PROGRAM_NAME, "w")
  42.     fh.writeLine(PROGRAM_NAME.." -r")
  43.     fh.close()
  44. end
  45. local function delResumeFile()
  46.     fs.delete("working/"..PROGRAM_NAME)
  47. end
  48.  
  49. local function digHere()
  50.     turtle.turnRight()
  51.     turtle.dig()
  52.     turtle.turnLeft()
  53.     turtle.turnLeft()
  54.     turtle.dig()
  55.     turtle.turnRight()
  56.     turtle.digUp()
  57.     turtle.digDown()
  58. end
  59.  
  60. local function writeCurrentData(data)
  61.     if not fs.exists("memory") then
  62.         return
  63.     end
  64.     local fh = fs.open("memory/"..PROGRAM_NAME, "w")
  65.     local tempStr = textutils.serialise(data)
  66.     fh.write(tempStr)
  67.     fh.close()
  68. end
  69. local function delCurrentData()
  70.     fs.delete("memory/"..PROGRAM_NAME)
  71. end
  72.  
  73. -- init
  74. local data = {state = "fwd", fwdCount = 0, backCount = 0}
  75. local resumed = false
  76. -- 復帰用ファイルの書き込み
  77. writeResumeFile()
  78.  
  79. for i=1, table.maxn(ARGS) do
  80.     local temp = ARGS[i]
  81.     if temp == "-r" then
  82.         resumed = true
  83.         if fs.exists("memory/"..PROGRAM_NAME) then
  84.             local fh = fs.open("memory/"..PROGRAM_NAME, "r")
  85.             data = textutils.unserialise(fh.readAll())
  86.             fh.close()
  87.             if type(data.fwdCount) ~= "number" then
  88.                 data.fwdCount = 0
  89.             end
  90.             if type(data.backCount) ~= "number" then
  91.                 data.backCount = 0
  92.             end
  93.         end
  94.     elseif temp == "--max-len" then
  95.         MAX_LEN = tonumber(ARGS[i+1])
  96.         i = i + 1
  97.     end
  98. end
  99.  
  100. if not resumed then
  101.     turtle.select(FUEL_SLOT)
  102.     turtle.refuel()
  103.     if turtle.getFuelLevel() <= MINFUEL then
  104.         print("can not refuel enogh fuel!")
  105.         return
  106.     end
  107.     turtle.select(1)
  108. end
  109.  
  110. -- 燃料がなくなるか最大の長さになるまで掘り進む
  111. local retryCount = 0
  112. while data.state == "fwd" and turtle.getFuelLevel() > data.fwdCount+MINFUEL and MAX_LEN > data.fwdCount do
  113.     turtle.dig()
  114.     if turtle.forward() then
  115.         data.fwdCount = data.fwdCount + 1
  116.         retryCount = 0
  117.         writeCurrentData(data)
  118.     else
  119.         if retryCount >= MAX_RETRY then
  120.             break
  121.         end
  122.         print("susumenai")
  123.         turtle.dig()
  124.         turtle.attack()
  125.         retryCount = retryCount + 1
  126.     end
  127.     digHere()
  128.  
  129.     turtle.select(16)
  130.     if turtle.getItemDetail() ~= nil then
  131.         if not dropStones(true) then
  132.             print("Go back. because of full inventory")
  133.             break
  134.         end
  135.     end
  136.     turtle.select(1)
  137. end
  138. -- 元の位置に戻る
  139. if data.state == "fwd" then
  140.     print("no enough fuel. I will go back start position.")
  141.     turtle.turnRight()
  142.     turtle.turnRight()
  143.     data.state = "rtn"
  144. end
  145. retryCount = 0
  146. while data.state == "rtn" and data.fwdCount > data.backCount do
  147.     if turtle.forward() then
  148.         data.backCount = data.backCount + 1
  149.         retryCount = 0
  150.         writeCurrentData(data)
  151.     else
  152.         if retryCount >= MAX_RETRY then
  153.             data.state = "err"
  154.             break
  155.         end
  156.         print("susumenai")
  157.         turtle.dig()
  158.         turtle.attack()
  159.         retryCount = retryCount + 1
  160.     end
  161. end
  162. if data.state == "rtn" then
  163.     turtle.turnRight()
  164.     turtle.turnRight()
  165.     data.state = "done"
  166. end
  167.  
  168. delResumeFile()
  169. delCurrentData()
  170. print("modotta!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement