SHARE
TWEET

ComputerCraft Turtorial: BuildFrameAndFloor0_1

hevohevo Feb 14th, 2015 (edited) 181 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ##################################
  2. -- yuka (build frame and floor)
  3.  
  4. -- API install and load API
  5. local api_name = "h2turtle"
  6. if fs.exists(api_name) then
  7.   print("Found API: "..api_name)
  8. else
  9.   print("Download API: "..api_name)
  10.   assert(shell.run("pastebin","get","NCKmS1qx",api_name), "Failed!: download API")
  11. end
  12.  
  13. os.loadAPI(api_name)
  14.  
  15.  
  16. -- ##################################
  17. -- 使い方(奥行き3、幅4の床を作る)
  18. -- > yuka 3 4
  19.  
  20. -- 枠として使うブロック名を聞いてくるので入力
  21. -- 枠の中として使うブロック名を聞いてくるので入力
  22. -- 必要なアイテム数を渡すと、プログラム開始
  23. -- 枠作成→中を埋めるという順番で床を作成する
  24.  
  25. -- FFFF
  26. -- FBBF
  27. -- FFFF
  28. -- T
  29.  
  30. -- F:枠として使うブロック、B:枠の中のブロック、T:タートル
  31.  
  32. -- ##################################
  33. -- Config
  34.  
  35. -- 床の奥行き、幅。デフォルト値
  36. local depth = 4
  37. local width = 4
  38.  
  39. -- 床作成に使うブロック名
  40. -- {アイテム名, ダメージ値}
  41. -- アイテム名は部分一致、ダメージ値は省略可能
  42. local frame_name = {"stonebrick", 0}
  43. local floor_name = {"cobblestone", 0}
  44.  
  45.  
  46. -- ##################################
  47. -- functions
  48. function buildFrameOneLine(length)
  49.   for i=1,length do
  50.     if h2turtle.isDownBlock(frame_name) then
  51.       -- do nothing
  52.     else
  53.       turtle.digDown()
  54.       h2turtle.placeDownItem(unpack(frame_name))
  55.     end
  56.     if i ~= length then turtle.forward() end
  57.   end
  58. end
  59.  
  60.  
  61. function buildFrame()
  62.   turtle.forward()
  63.   for i=1,2 do
  64.     buildFrameOneLine(depth)
  65.    
  66.     turtle.turnRight()
  67.    
  68.     buildFrameOneLine(width)
  69.    
  70.     turtle.turnRight()
  71.   end
  72. end
  73.  
  74. function buildFloor()
  75.   while true do
  76.     while not turtle.detectDown() do
  77.       turtle.digDown()
  78.       h2turtle.placeDownItem(unpack(floor_name))
  79.       turtle.forward()
  80.     end
  81.     turtle.back()
  82.     turtle.turnRight()
  83.     turtle.forward()
  84.     if turtle.detectDown() then break end
  85.   end
  86. end
  87.  
  88. -- ##################################
  89. -- main
  90.  
  91. -- 床のサイズを確認
  92. local args = {...}
  93. depth = args[1] and tonumber(args[1]) or depth
  94. width = args[2] and tonumber(args[2]) or width
  95.  
  96. print()
  97. local depth = tonumber(h2turtle.promptValue("depth?", depth))
  98. local width = tonumber(h2turtle.promptValue("width?", width))
  99.  
  100. print()
  101. print(string.format("depth: %d, width: %d", depth, width))
  102. print()
  103.  
  104. -- 床の材料を確認
  105. local _frame_n = h2turtle.promptValue("frame block name?", frame_name[1])
  106. local _frame_d = h2turtle.promptValue("frame block damage?", frame_name[2])
  107. frame_name = {_frame_n, tonumber(_frame_d)}
  108.  
  109. local _floor_n = h2turtle.promptValue("floor block name?", floor_name[1])
  110. local _floor_d = h2turtle.promptValue("floor block damage?", floor_name[2])
  111. floor_name = {_floor_n, tonumber(_floor_d)}
  112.  
  113. -- ここまでに入力された値を確認表示
  114. print()
  115. print("Build frame and floor:")
  116. print(string.format(" depth: %d, width: %d", depth, width))
  117. print(string.format(" frame block: %s (%d)", frame_name[1], frame_name[2]))
  118. print(string.format(" floor block: %s (%d)", floor_name[1], floor_name[2]))
  119. print()
  120.  
  121. -- 必要アイテム数を確保
  122. print("Please give me items.")
  123. local required_frame_qty = (depth+width)*2
  124. h2turtle.waitForEnoughItems(required_frame_qty, unpack(frame_name))
  125.  
  126. local required_floor_qty = (depth-2)*(width-2)
  127. h2turtle.waitForEnoughItems(required_floor_qty, unpack(floor_name))
  128.  
  129. -- スタートしてよいか最終確認
  130. print()
  131. assert(h2turtle.promptYN(),"terminated!")
  132.  
  133. -- build frame
  134. print()
  135. print("Build frame and floor: frame..")
  136.  
  137. buildFrame()
  138.  
  139. --  中心の床部分を埋めるために、最初の位置に移動
  140. turtle.turnRight()
  141. turtle.forward()
  142. turtle.turnLeft()
  143. turtle.forward()
  144.  
  145. -- build floor
  146. print()
  147. print("Build frame and floor: floor..")
  148. buildFloor()
  149.  
  150. print("Build frame and floor: finished")
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top