Advertisement
asweigart

harev6

Jan 18th, 2017
1,900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. --[[Function Module program by Al Sweigart
  2. Provides useful utility functions.]]
  3.  
  4. -- selectItem() selects the inventory
  5. -- slot with the named item, returns
  6. -- true if found and false if not
  7. function selectItem(name)
  8. -- check all inventory slots
  9. local item
  10. for slot = 1, 16 do
  11. item = turtle.getItemDetail(slot)
  12. if item ~= nil and item['name'] == name then
  13. turtle.select(slot)
  14. return true
  15. end
  16. end
  17.  
  18. return false -- couldn't find item
  19. end
  20.  
  21.  
  22. -- selectEmptySlot() selects inventory
  23. -- slot that is empty, returns true if
  24. -- found, false if no empty spaces
  25. function selectEmptySlot()
  26.  
  27. -- loop through all slots
  28. for slot = 1, 16 do
  29. if turtle.getItemCount(slot) == 0 then
  30. turtle.select(slot)
  31. return true
  32. end
  33. end
  34. return false -- couldn't find empty space
  35. end
  36.  
  37.  
  38. -- countInventory() returns the total
  39. -- number of items in the inventory
  40. function countInventory()
  41. local total = 0
  42.  
  43. for slot = 1, 16 do
  44. total = total + turtle.getItemCount(slot)
  45. end
  46. return total
  47. end
  48.  
  49.  
  50. -- selectAndPlaceDown() selects a nonempty slot
  51. -- and places a block from it under the turtle
  52. function selectAndPlaceDown()
  53.  
  54. for slot = 1, 16 do
  55. if turtle.getItemCount(slot) > 0 then
  56. turtle.select(slot)
  57. turtle.placeDown()
  58. return
  59. end
  60. end
  61. end
  62.  
  63.  
  64. -- buildWall() creates a wall stretching
  65. -- in front of the turtle
  66. function buildWall(length, height)
  67. if hare.countInventory() < length * height then
  68. return false -- not enough blocks
  69. end
  70.  
  71. turtle.up()
  72.  
  73. local movingForward = true
  74.  
  75. for currentHeight = 1, height do
  76. for currentLength = 1, length do
  77. selectAndPlaceDown() -- place the block
  78. if movingForward and currentLength ~= length then
  79. turtle.forward()
  80. elseif not movingForward and currentLength ~= length then
  81. turtle.back()
  82. end
  83. end
  84. if currentHeight ~= height then
  85. turtle.up()
  86. end
  87. movingForward = not movingForward
  88. end
  89.  
  90. -- done building wall; move to end position
  91. if movingForward then
  92. -- turtle is near the start position
  93. for currentLength = 1, length do
  94. turtle.forward()
  95. end
  96. else
  97. -- turtle is near the end position
  98. turtle.forward()
  99. end
  100.  
  101. -- move down to the ground
  102. for currentHeight = 1, height do
  103. turtle.down()
  104. end
  105.  
  106. return true
  107. end
  108.  
  109.  
  110. -- buildRoom() constructs four walls
  111. -- and a ceiling
  112. function buildRoom(length, width, height)
  113. if hare.countInventory() < (((length - 1) * height * 2) + ((width - 1) * height * 2)) then
  114. return false -- not enough blocks
  115. end
  116.  
  117. -- build the four walls
  118. buildWall(length - 1, height)
  119. turtle.turnRight()
  120.  
  121. buildWall(width - 1, height)
  122. turtle.turnRight()
  123.  
  124. buildWall(length - 1, height)
  125. turtle.turnRight()
  126.  
  127. buildWall(width - 1, height)
  128. turtle.turnRight()
  129.  
  130. return true
  131. end
  132.  
  133.  
  134. -- sweepField() moves across the rows
  135. -- and columns of an area in front and
  136. -- to the right of the turtle, calling
  137. -- the provided sweepFunc at each space
  138. function sweepField(length, width, sweepFunc)
  139. local turnRightNext = true
  140.  
  141. for x = 1, width do
  142. for y = 1, length do
  143. sweepFunc()
  144.  
  145. -- don't move forward on the last row
  146. if y ~= length then
  147. turtle.forward()
  148. end
  149. end
  150.  
  151. -- don't turn on the last column
  152. if x ~= width then
  153. -- turn to the next column
  154. if turnRightNext then
  155. turtle.turnRight()
  156. turtle.forward()
  157. turtle.turnRight()
  158. else
  159. turtle.turnLeft()
  160. turtle.forward()
  161. turtle.turnLeft()
  162. end
  163.  
  164. turnRightNext = not turnRightNext
  165. end
  166. end
  167.  
  168. -- move back to the start position
  169. if width % 2 == 0 then
  170. turtle.turnRight()
  171. else
  172. for y = 1, length - 1 do
  173. turtle.back()
  174. end
  175. turtle.turnLeft()
  176. end
  177.  
  178. for x = 1, width - 1 do
  179. turtle.forward()
  180. end
  181. turtle.turnRight()
  182.  
  183. return true
  184. end
  185.  
  186.  
  187. -- buildFloor() builds a rectangular
  188. -- floor out of the blocks in the
  189. -- inventory
  190. function buildFloor(length, width)
  191. if countInventory() < length * width then
  192. return false -- not enough blocks
  193. end
  194.  
  195. turtle.up()
  196. sweepField(length, width, selectAndPlaceDown)
  197. end
  198.  
  199.  
  200. -- findBlock() spins around searching
  201. -- for the named block next to the turtle
  202. function findBlock(name)
  203. local result, block
  204.  
  205. for i = 1, 4 do
  206. result, block = turtle.inspect()
  207. if block ~= nil and block['name'] == name then
  208. return true
  209. end
  210. turtle.turnRight()
  211. end
  212. return false
  213. end
  214.  
  215.  
  216. -- digUntilClear() keeps digging until
  217. -- there are no more blocks (used when
  218. -- sand or gravel can fall into the path)
  219. function digUntilClear()
  220. while turtle.detect() do
  221. if not turtle.dig() then
  222. return false
  223. end
  224. end
  225. return true
  226. end
  227.  
  228. -- digUpUntilClear() keeps digging up until
  229. -- there are no more blocks (used when
  230. -- sand or gravel can fall into the path)
  231. function digUpUntilClear()
  232. while turtle.detectUp() do
  233. if not turtle.digUp() then
  234. return false
  235. end
  236. end
  237. return true
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement