Advertisement
hevohevo

ComputerCraft Turtorial: HugeWoodCutter0_1

Feb 4th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. -- #################################################################
  2. -- Huge wood cutter
  3. -- version 0.1
  4. -- hevohevo, License: MIT
  5. -- Twitter: @hevohevo, http://hevohevo.hatenablog.com/
  6.  
  7. -- 説明
  8. -- このプログラムは、松の巨木(2x2)を2本を伐採するプログラムです。
  9.  
  10. -- 使い方
  11. -- スロット1に松の苗木を64本入れる
  12. -- 以下の形に苗木とタートルを配置
  13.  
  14. -- M: 松の苗木, T: タートル, 上が北向き(方角がとても重要)
  15.  
  16. --   MM
  17. --   MM
  18. --  MMT
  19. --  MM
  20.  
  21. -- サブ関数
  22.  
  23. -- 上に伐採できるものがある限り、ずっと上に伐採しつつ上昇
  24. function cut2Up()
  25.   repeat
  26.     turtle.dig()
  27.     turtle.up()
  28.   until not turtle.digUp()
  29. end
  30.  
  31. -- 隣接する場所にあるブロックの名称を調べてマッチしたらtrue
  32. -- 第1引数:ブロック名称(部分一致、複数可)テーブル、第2引数:調べる方角の関数
  33. -- 例: findBlock({"dirt", "grass"}, turtle.inspect) => true (正面に土があった)
  34. function findBlock(names, inspect_func)
  35.   inspect_func = inspect_func or turtle.inspect
  36.   local status, detail = inspect_func()
  37.   if not status then return false end
  38.  
  39.   for i,name in ipairs(names) do
  40.     if string.match(detail["name"],name) then
  41.       return true
  42.     end
  43.   end
  44.   return false
  45. end
  46.  
  47. -- 下に土(草)ブロックを発見するまで下に伐採しつつ降りる
  48. function cut2Down()
  49.   repeat
  50.     turtle.dig()
  51.     turtle.down()
  52.     if findBlock({"dirt","grass"},turtle.inspectDown) then
  53.       return
  54.     end
  55.   until not turtle.digDown()
  56. end
  57.  
  58. -- スロット2から16のブロックを下に落とす
  59. function drop2_16()
  60.   for i=2,16 do
  61.     turtle.select(i)
  62.     turtle.dropDown()
  63.   end
  64.   turtle.select(1)
  65. end
  66.  
  67. -- 汚いメイン関数、伐採したり、伐採後の地面に苗木を植えたり
  68. -- turtle.suck()を連発しているのは、少しでも落ちてくる苗木を回収するため
  69. function main()
  70.   turtle.dig(); turtle.suck()
  71.   turtle.forward()
  72.   cut2Up()
  73.   turtle.turnLeft()
  74.   turtle.dig(); turtle.suck()
  75.   turtle.forward()
  76.   turtle.turnRight()
  77.   cut2Up()
  78.   cut2Down()
  79.   turtle.dig(); turtle.suck()
  80.   -- 伐採終了、苗木植える
  81.   turtle.place(); turtle.suck()
  82.   turtle.turnLeft()
  83.   turtle.back()
  84.   turtle.place(); turtle.suck()
  85.   turtle.turnRight()
  86.   turtle.place(); turtle.suck()
  87.   turtle.back()
  88.   turtle.place(); turtle.suck()
  89. end
  90.  
  91.  
  92. while true do
  93.   -- 燃料が200未満だったり、スロット1のアイテムが4個未満ならエラー終了。
  94.   assert(turtle.getFuelLevel()>200)
  95.   assert(turtle.getItemCount(1)>4)
  96.  
  97.   drop2_16()
  98.   for i=1,4 do
  99.     local status, detail = turtle.inspect()
  100.     if status and string.match(detail["name"],"minecraft:log") then
  101.       main()
  102.       drop2_16()
  103.     end
  104.     turtle.turnRight()
  105.   end
  106.  
  107.   os.sleep(120)
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement