Advertisement
hevohevo

ComputerCraft Turtorial: HugeWoodCutter0_2

Feb 5th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. -- #################################################################
  2. -- Huge wood cutter
  3. -- version 0.2
  4. -- hevohevo, License: MIT
  5. -- Twitter: @hevohevo, http://hevohevo.hatenablog.com/
  6.  
  7. -- 説明
  8. -- このプログラムは、松の巨木(2x2)2本を伐採するプログラムです。
  9.  
  10. -- 使い方
  11. -- スロット1に松の苗木を64本入れる
  12. -- 以下の形に苗木とタートルを配置
  13. -- 採取した木材はスタート地点に戻ってから真下にドロップします
  14.  
  15. -- m: 松の苗木, T: タートル, 上が北向き(方角がとても重要)
  16.  
  17. --   mm
  18. --   mm
  19. --  mmT
  20. --  mm
  21.  
  22.  
  23. -- ###############################################
  24. -- サブ関数
  25.  
  26. -- 上か正面に伐採できるブロックがある限り、ずっと上に伐採しつつ上昇
  27. -- 2x2の北西部分だけ1段高く幹が成長するとかなにそのめんどくさい仕様。
  28. function cut2Up()
  29.   while turtle.detect() or turtle.detectUp() do
  30.     turtle.digUp()
  31.     turtle.dig()
  32.     turtle.up()
  33.   end
  34.   turtle.digUp() -- 最後に正面を伐採して上昇終了
  35. end
  36.  
  37. -- 隣接する場所にあるブロックの名称を調べてマッチしたらtrue
  38. -- 第1引数:ブロック名称(部分一致、複数可)テーブル、第2引数:調べる方角の関数
  39. -- 例: findBlock({"dirt", "grass"}, turtle.inspect) => true (正面に土があった)
  40. function findBlock(names, inspect_func)
  41.   inspect_func = inspect_func or turtle.inspect
  42.   local status, detail = inspect_func()
  43.   if not status then return false end
  44.  
  45.   for i,name in ipairs(names) do
  46.     if string.match(detail["name"],name) then
  47.       return true
  48.     end
  49.   end
  50.   return false
  51. end
  52.  
  53. -- 下に土(草)ブロックを発見するまで下に伐採しつつ降りる
  54. function cut2Down()
  55.   while not findBlock({"dirt","grass"},turtle.inspectDown) do -- 下が土・草じゃなかったら回す
  56.     turtle.dig()
  57.     turtle.digDown()
  58.     turtle.down()
  59.   end
  60.   turtle.dig() -- 最後に正面伐採して下降終了
  61. end
  62.  
  63. -- スロット2から16のブロックを下に落とす
  64. function drop2_16()
  65.   for i=2,16 do
  66.     turtle.select(i)
  67.     turtle.dropDown()
  68.   end
  69.   turtle.select(1)
  70. end
  71.  
  72. -- スロット1に松の苗木が4つ以上あることを確認。
  73. -- それ以外のスロットは空にする
  74. function prepare_inventory()
  75.   turtle.select(1)
  76.   local detail = turtle.getItemDetail()
  77.   assert(detail and detail["count"]>3 and detail["name"]=="minecraft:sapling" and detail["damage"]==1,
  78.     "Required 4 spruce-sapling")
  79.  
  80.   for i=2,16 do
  81.     turtle.select(i)
  82.     turtle.dropDown()
  83.   end
  84.   turtle.select(1)
  85.   print("Fuel: ",turtle.getFuelLevel())
  86. end
  87.  
  88.  
  89.  
  90. -- 汚いメイン関数、伐採したり、伐採後の地面に苗木を植えたり
  91. -- turtle.suck()を連発しているのは、少しでも落ちてくる苗木を回収するため
  92. function main()
  93.   local _dig_fwd = function()
  94.     turtle.dig()
  95.     turtle.forward()
  96.   end
  97.   local _suck_place = function()
  98.     turtle.suck() -- できるだけ落ちてきた苗木を回収したい
  99.     turtle.place()
  100.   end
  101.  
  102.   -- 2x2大木の右半分伐採(上昇)開始
  103.   _dig_fwd()
  104.   cut2Up()
  105.   turtle.turnLeft()
  106.   _dig_fwd()
  107.   turtle.turnRight()
  108.   -- 2x2大木の左半分伐採(下降)開始
  109.   cut2Down()
  110.   -- 伐採終了、苗木植える
  111.   _suck_place()
  112.   turtle.turnLeft()
  113.   turtle.back()
  114.   _suck_place()
  115.   turtle.turnRight()
  116.   _suck_place()
  117.   turtle.back()
  118.   _suck_place()
  119.   -- スタート位置に戻った
  120. end
  121.  
  122.  
  123.  
  124. while true do
  125.   prepare_inventory() -- インベントリの準備
  126.  
  127.   for i=1,4 do -- 四方を見るけれど結局は北と西だけ。
  128.     -- 燃料が200以下のときはエラー終了
  129.     assert(turtle.getFuelLevel()>200, "more fuel!")
  130.    
  131.     local status, detail = turtle.inspect()
  132.     if status and string.match(detail["name"],"minecraft:log") then -- 正面が"log"なら開始
  133.       main() -- 伐採作業
  134.       prepare_inventory() -- インベントリの準備
  135.     end
  136.     turtle.turnRight()
  137.   end
  138.  
  139.   os.sleep(120)
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement