Advertisement
hevohevo

ComputerCraft Turtorial: HugeWoodCutter0_3

Feb 7th, 2015
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.33 KB | None | 0 0
  1. -- #################################################################
  2. -- Huge wood cutter, version 0.3a
  3. -- hevohevo, License: MIT
  4. -- Twitter: @hevohevo, http://hevohevo.hatenablog.com/
  5.  
  6. -- 説明: 松の巨木(2x2)を2本使った木材伐採場プログラムです。
  7.  
  8. -- 使い方:
  9. --  松の苗木をできるだけ多くインベントリに入れる(ただし丸太用に3スロット程度は空けておく)
  10. --  以下の俯瞰図どおりに苗木とタートルを配置
  11. --  採取した木材はスタート地点に戻ってから真下にドロップします
  12.  
  13. -- 苗: 松の苗木を植える、タ: タートル、上が北向き ***方角は絶対に守ること***
  14. --   苗苗
  15. --   苗苗
  16. -- 苗苗タ
  17. -- 苗苗
  18.  
  19. -- ###############################################################
  20. -- Config
  21. local allow_blocks = {"minecraft:log", "minecraft:leaves"} -- 丸太と葉は伐採してよい
  22. local deny_blocks = {"dirt", "grass"} -- 土(草)ブロックは土台なので破壊不許可
  23. local spruce_sapling = {"minecraft:sapling", 1} -- 松の苗木はダメージ値1
  24. local min_fuel_level = 200
  25. local sleep_time = 60 -- sec
  26.  
  27. -- ###############################################################
  28. -- Funcitons
  29.  
  30. -- 隣接するブロックが、指定ブロック名とマッチしたらtrue
  31. -- 第1引数:ブロック名文字列(部分一致)またはその配列、第2引数:調べる方向の関数
  32. -- 例: findBlock({"dirt", "grass"}, turtle.inspectDown) => true (下に土があった)
  33. function findBlock(names, inspect_func)
  34.   local status, detail = inspect_func()
  35.   if not status then return false end -- ブロックが存在しないときはfalse返す
  36.   if type(names)=="string" then names = {names} end
  37.   for i,name in ipairs(names) do
  38.     if string.match(detail["name"],name) then
  39.       return true -- ブロック名称(複数)のいずれかがマッチしたらtrue
  40.     end
  41.   end
  42.   return false -- いずれもマッチせずfalse
  43. end
  44.  
  45. -- 指定スロットにあるアイテムが、指定アイテム名(damage値はオプション)にマッチしたらtrue
  46. -- ## _matchSlotWithName(slot_num, item_name, [item_damage]) => true/false
  47. local function _matchSlotWithName(slot, name, damage)
  48.   local detail = turtle.getItemDetail(slot) -- アイテムなし: nil、アイテムあり: table
  49.   if detail and string.match(detail["name"],name) then  -- アイテムが存在し、かつアイテム名がマッチする
  50.     if (damage == nil) or (damage == detail["damage"]) then  -- damage値の指定なし、あるいは指定がありそれが一致
  51.       return true -- マッチする
  52.     end
  53.   end
  54.   return false -- マッチしない
  55. end
  56.  
  57. -- ## getAllItemCount(item_name_str, [damage_int]) => インベントリ内アイテム総数
  58. function getAllItemCount(item_name, damage)
  59.   local total = 0
  60.   for i=1,16 do
  61.     if _matchSlotWithName(i, item_name, damage) then
  62.       total = total + turtle.getItemCount(i)
  63.     end
  64.   end
  65.   return total
  66. end
  67.  
  68. -- ## selectItem(item_name, [damage]) => アイテム名とマッチするアイテムスロットを選択
  69. function selectItem(name, damage)
  70.   for i=1,16 do
  71.     if _matchSlotWithName(i , name, damage) then
  72.       turtle.select(i)
  73.       return true -- アイテムを見つけスロット選択できたらtrue
  74.     end
  75.   end
  76.   return false, "No item to match" -- アイテムが見つからないfalse
  77. end
  78.  
  79. -- 伐採してもよいブロックだけ伐採: digFor() / digDownFor() / digUpFor()
  80. function digFor(block_names) -- 正面用
  81.   block_names = block_names or allow_blocks
  82.   if findBlock(block_names, turtle.inspect) then
  83.     turtle.dig()
  84.   end
  85. end
  86. function digDownFor(block_names) -- 真下用
  87.   block_names = block_names or allow_blocks
  88.   if findBlock(block_names, turtle.inspectDown) then
  89.     turtle.digDown()
  90.   end
  91. end
  92. function digUpFor(block_names) -- 真上用
  93.   block_names = block_names or allow_blocks
  94.   if findBlock(block_names, turtle.inspectUp) then
  95.     turtle.digUp()
  96.   end
  97. end
  98.  
  99. -- 上か正面に伐採できるブロックがある限り、ずっと上に伐採しつつ上昇
  100. -- 苗木をできるだけ回収するため回転上昇しながら葉も伐採する。
  101. function revolveUp()
  102.   while turtle.detect() or turtle.detectUp() do
  103.     for i=1,4 do
  104.       digFor()
  105.       turtle.turnRight()
  106.     end
  107.     digUpFor()
  108.     turtle.up()
  109.   end
  110. end
  111.  
  112. -- 下に土(草)ブロックを発見するまで下に回転伐採しつつ降りる
  113. function revolveDown()
  114.   -- 真下が破壊不許可ブロック以外ならwhile回して下に降りる
  115.   while not findBlock(deny_blocks, turtle.inspectDown) do
  116.     digDownFor()
  117.     turtle.down()
  118.     for i=1,4 do
  119.       digFor()
  120.       turtle.turnRight()
  121.     end
  122.   end
  123. end
  124.  
  125. -- 汚いplant()関数とcutHugeWood()関数を見やすくするための小さなサブ関数
  126. local function _digAndFwd()
  127.   digFor()
  128.   turtle.forward()
  129. end
  130. local function _turn180()
  131.   turtle.turnRight()
  132.   turtle.turnRight()
  133. end
  134. local function _getSapling()  -- できるだけ苗木を回収したい
  135.   turtle.select(1)
  136.   turtle.suck();  turtle.suckUp()
  137. end
  138. local function _plant()
  139.   digFor() -- 片方の巨木伐採中にもう片方が成長し、空間を葉で埋めてしまう恐れに備えて
  140.   selectItem(unpack(spruce_sapling));  turtle.place() -- 苗木を選択して植える
  141.   _getSapling()
  142. end
  143.  
  144. -- 苗木を植える。
  145. -- もう片方の巨木が生長して空間を葉で埋めてしまう恐れのため後進は不可。常に伐採&前進
  146. function plant()
  147.   -- 現在位置は2x2巨木の南西/北西、向きは西/北
  148.   turtle.turnRight()
  149.   _digAndFwd()
  150.   _turn180()
  151.   _plant() -- 苗木植える(南西/北西)
  152.   turtle.turnLeft()
  153.   _digAndFwd()
  154.   _turn180()
  155.   _plant() -- 苗木植える(北西/北東)
  156.   turtle.turnLeft()
  157.   _plant() -- 苗木植える(南東/南西)
  158.   turtle.turnLeft()
  159.   _digAndFwd()
  160.   _turn180()
  161.   _plant() -- 苗木植える(北東/南東)
  162. end
  163.  
  164.  
  165. -- 伐採したり、伐採後の地面に苗木を植えたり
  166. function cutHugeWood()
  167.   _digAndFwd()
  168.  
  169.   -- 2x2大木の北東(南東)幹を回転上昇開始
  170.   revolveUp()
  171.  
  172.   -- 南西(北西)幹の位置に移動
  173.   turtle.turnLeft()
  174.   _digAndFwd()
  175.   turtle.turnRight()
  176.   _digAndFwd()
  177.  
  178.   -- 2x2大木の南西(北西)幹を回転下降開始
  179.   revolveDown()
  180.  
  181.   -- 伐採終了、苗木植える
  182.   plant()
  183. end
  184.  
  185. -- ###############################################################
  186. -- main
  187. while true do
  188.   for i=1,4 do -- 四方を見るけれど結局は北と西側にしか巨木は無い
  189.     turtle.select(1)
  190.    
  191.     -- 燃料が min_fuel_level 以下のときはエラー終了
  192.     print("Fuel: ",turtle.getFuelLevel())
  193.     assert(turtle.getFuelLevel()>min_fuel_level, "more fuel!")
  194.  
  195.     -- インベントリ内の松の苗木の総数が4つ未満ならばエラー終了
  196.     assert(getAllItemCount(unpack(spruce_sapling))>=4, "Required 4 spruce-sapling")
  197.  
  198.     -- インベントリ内の丸太を全て下にドロップ
  199.     while selectItem("minecraft:log") do turtle.dropDown() end
  200.  
  201.     -- 巨木が生長することで正面が丸太(log)なら伐採開始
  202.     if findBlock("minecraft:log", turtle.inspect) then
  203.       cutHugeWood()
  204.     end
  205.    
  206.     turtle.turnRight()
  207.     _getSapling()
  208.   end
  209.  
  210.   os.sleep(sleep_time)
  211. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement