Advertisement
hevohevo

ComputerCraft: Tofu5

Jun 11th, 2016
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. -- tofu5
  2. --  豆腐建築を行うプログラムです
  3. --  アイテムインベントリに適当にアイテム詰めて
  4. --  燃料はもちろんたっぷり与えておくこと
  5.  
  6. -- ####### Config ###########
  7. local haba = 7
  8. local okuyuki = 6
  9. local takasa = 3
  10. local togariyane = true -- true or false
  11.  
  12.  
  13. -- ####### Functions ########
  14.  
  15. -- #####
  16. -- アイテムインベントリ用の関数群
  17.  
  18. -- 次のアイテムスロットへ選択スロットを移動
  19. function selectNextSlot()
  20.   local current = turtle.getSelectedSlot()
  21.   if current == 16 then
  22.     return false
  23.   else
  24.     turtle.select(current+1)
  25.     return true    
  26.   end
  27. end
  28.  
  29. -- 選択スロットを次のアイテムのあるスロットまで移動
  30. function selectNextItemSlot()
  31.   while (turtle.getItemCount()==0) and selectNextSlot() do
  32.   end
  33. end
  34.  
  35. -- インベントリを先頭から走査し、確実にアイテムを真下設置
  36. function myPlaceDown()
  37.   while (turtle.getItemCount()==0) and selectNextItemSlot() do
  38.     print("Current slot:",turtle.getSelectedSlot())
  39.   end
  40.  
  41.   return turtle.placeDown()
  42. end
  43.  
  44. -- #####
  45. -- 建築用の関数
  46. local usedBlocks = 0
  47.  
  48. -- 前進しつつn個のブロックを下に設置
  49. function placeSomeBlocks(n)
  50.   if n<1 then -- 最後の真下の穴を埋める
  51.     if myPlaceDown() then
  52.       usedBlocks = usedBlocks+1
  53.     end
  54.     return
  55.   end
  56.  
  57.   for i=1,n do -- n個並べる
  58.     turtle.forward()
  59.     if myPlaceDown() then
  60.       usedBlocks = usedBlocks+1
  61.     end
  62.   end
  63. end
  64.  
  65. -- ロの字にブロックを設置
  66. function buildRonoji(okuyuki, haba)
  67.   for i=1,2 do
  68.     placeSomeBlocks(okuyuki - 1)
  69.     turtle.turnLeft()
  70.     placeSomeBlocks(haba - 1)
  71.     turtle.turnLeft()
  72.   end
  73. end
  74.  
  75. -- 屋根の次の位置に移動
  76. function goToRightForward()
  77.   turtle.turnLeft()
  78.   turtle.forward()
  79.   turtle.turnLeft()
  80.   turtle.forward()
  81. end
  82.  
  83. function goToLeftBack()
  84.   turtle.turnRight()
  85.   turtle.forward()
  86.   turtle.turnLeft()
  87.   turtle.back()
  88. end
  89.  
  90. function goUp()
  91.   if togariyane then
  92.     turtle.up()
  93.   end
  94. end
  95.  
  96.  
  97. -- ###### Main ########
  98. turtle.select(16)
  99. turtle.refuel()
  100.  
  101. turtle.select(1)
  102.  
  103. -- 無線通信を受けて建設開始
  104. rednet.open("right")
  105.  
  106. while true do
  107.   local id, msg = rednet.receive()
  108.   print(id, msg)
  109.  
  110.   if msg == "start_build" then
  111.     break
  112.   elseif msg == "calling_turtle" then
  113.     turtle.digUp()
  114.     turtle.up()
  115.     turtle.up()
  116.   end
  117. end
  118.  
  119.  
  120.  
  121. turtle.select(1) -- 最初の決まり文句
  122.  
  123. turtle.up()  -- 一歩上昇して、設置位置につく
  124.  
  125. -- ロの字を1段ずつ takasa まで積み上げる
  126. for ronoji=1,takasa do
  127.   buildRonoji(okuyuki, haba)
  128.   turtle.up()  -- move up
  129. end
  130.  
  131. -- とがり屋根だと、ひさしを作る
  132. -- 一回り大きなロの字を作る
  133. if togariyane then
  134.   goToLeftBack()
  135.   buildRonoji(okuyuki+2, haba+2)
  136.   goToRightForward()
  137.   buildRonoji(okuyuki, haba)
  138.   turtle.up()
  139. end
  140.  
  141. -- 屋根作り
  142. -- 少しずつ小さなロの字を敷き詰める。
  143. while (okuyuki > 0) and (haba > 0) do
  144.   buildRonoji(okuyuki, haba)
  145.   goToRightForward()
  146.   goUp() -- とがり屋根だと1歩上昇
  147.   okuyuki = okuyuki - 2
  148.   haba = haba - 2
  149. end
  150.  
  151. print("Used Blocks:", usedBlocks)
  152. -- redstone.setOutput("bottom",true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement