Advertisement
asweigart

harev3

Jan 9th, 2017
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. --[[ Hare module 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 slot, 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. -- loop through all slots
  27. local slot
  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. local slot
  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
  51. -- slot and places it under the turtle
  52. function selectAndPlaceDown()
  53. local slot
  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. local currentHeight, currentLength
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement