Advertisement
HarvDad

fabdrain

May 2nd, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1.  
  2.  
  3. print("fabdrain: Rev 1.0")
  4.  
  5. beltSlot = 1
  6. currentSlot = beltSlot
  7. goForward = 0
  8. goBackward = 1
  9. direction = goForward
  10. holeWidth = 3
  11.  
  12. -- The following 'face' directions are relative to the starting position of the turtle in this program
  13. north = 0
  14. west = 1
  15. south = 2
  16. east = 3
  17.  
  18. x = 0
  19. y = 0
  20. z = 0
  21.  
  22. currentFace = north
  23.  
  24. function setFace(f)
  25. if f == 0 then
  26. if face == 0 then return end
  27. if face == 1 then right() return end
  28. if face == 2 then right() right() return end
  29. if face == 3 then left() return end
  30. end
  31.  
  32. if f == 1 then
  33. if face == 0 then left() return end
  34. if face == 1 then return end
  35. if face == 2 then right() return end
  36. if face == 3 then right() right() return end
  37. end
  38.  
  39. if f == 2 then
  40. if face == 0 then left() left() return end
  41. if face == 1 then left() return end
  42. if face == 2 then return end
  43. if face == 3 then right() return end
  44. end
  45.  
  46. if f == 3 then
  47. if face == 0 then right() return end
  48. if face == 1 then left() left() return end
  49. if face == 2 then left() return end
  50. if face == 3 then return end
  51. end
  52. end
  53.  
  54. function left()
  55. if face == 0 then face = 1 turtle.turnLeft() return end
  56. if face == 1 then face = 2 turtle.turnLeft() return end
  57. if face == 2 then face = 3 turtle.turnLeft() return end
  58. if face == 3 then face = 0 turtle.turnLeft() return end
  59. print("function left\(\): Bad face value: ", face)
  60. end
  61.  
  62. function right()
  63. if face == 0 then face = 3 turtle.turnRight() return end
  64. if face == 1 then face = 0 turtle.turnRight() return end
  65. if face == 2 then face = 1 turtle.turnRight() return end
  66. if face == 3 then face = 2 turtle.turnRight() return end
  67. print("function right\(\): Bad face value: ", face)
  68. end
  69.  
  70. function aboutFace()
  71. if face == north then setFace(south) return end
  72. if face == south then setFace(north) return end
  73. if face == east then setFace(west) return end
  74. if face == west then setFace(east) return end
  75. end
  76.  
  77. function reverseDirection()
  78. if direction == goForward then
  79. direction = goBackward
  80. else
  81. direction = goForward
  82. end
  83. end
  84.  
  85. function moveForward(nBlocks)
  86. for i = 1, nBlocks do
  87. turtle.forward()
  88. if face == north then
  89. z = z + 1
  90. elseif face == south then
  91. z = z - 1
  92. elseif face == east then
  93. x = x + 1
  94. elseif face == west then
  95. x = x - 1
  96. end
  97. end
  98.  
  99. end
  100.  
  101. function moveBackward(nBlocks)
  102. for i = 1, nBlocks do
  103. turtle.back()
  104. if face == north then
  105. z = z - 1
  106. elseif face == south then
  107. z = z + 1
  108. elseif face == east then
  109. x = x - 1
  110. elseif face == west then
  111. x = x + 1
  112. end
  113. end
  114. end
  115.  
  116. function home()
  117. end
  118.  
  119. function xzHome()
  120. local X = gX
  121. local Z = gZ
  122.  
  123. if x ~= X then
  124. if x < X then
  125. setFace(east)
  126. while x < X do
  127. turtle.forward()
  128. x = x + 1
  129. end
  130. else
  131. setFace(west)
  132. while x > X do
  133. turtle.forward()
  134. x = x - 1
  135. end
  136. end
  137. end
  138.  
  139. if z ~= Z then
  140. if z < Z then
  141. setFace(north)
  142. while z < Z do
  143. turtle.forward()
  144. z = z + 1
  145. end
  146. else
  147. setFace(south)
  148. while z > Z do
  149. turtle.forward()
  150. z = z - 1
  151. end
  152. end
  153. end
  154. end
  155.  
  156. function layBeltRow(direction, length)
  157. for j=1,5 do
  158. if turtle.getItemCount(currentSlot) < 1 then
  159. currentSlot = currentSlot + 1
  160. if turtle.getItemCount(currentSlot) < 1 then
  161. abort("No more belts in inventory.")
  162. end
  163. turtle.select(currentSlot)
  164. end
  165. turtle.placeDown()
  166. if j < 5 then
  167. if direction == goForward then
  168. moveForward(1)
  169. else
  170. moveBackward(1)
  171. end
  172. end
  173. end
  174. end
  175.  
  176. rowHeading = west
  177.  
  178. function goToNextRow()
  179. currentFace = face
  180. if face == north or face == south then
  181. setFace(rowHeading)
  182. moveForward(1)
  183. setFace(currentFace)
  184. else
  185. setFace(rowHeading)
  186. moveForward(1)
  187. setFace(currentFace)
  188. end
  189. end
  190.  
  191. function layBeltSection(length, width)
  192. moveBackward(length - 1)
  193. for i = 1,width do
  194. direction = goForward
  195. layBeltRow(direction, length)
  196. moveForward(holeWidth+1)
  197. aboutFace()
  198. direction = goBackward
  199. layBeltRow(direction, length)
  200. goToNextRow()
  201. end
  202.  
  203. setFace(east)
  204. rowHeading = south
  205. direction = goBackwards
  206. segLength = length + 3 + length
  207. for i = 1, segLength do
  208. layBeltRow(direction, length)
  209. if i < segLength then
  210. goToNextRow()
  211. reverseDirection()
  212. end
  213. end
  214.  
  215. setFace(east)
  216. moveForward(length + holeWidth)
  217. aboutFace()
  218.  
  219. rowHeading = north
  220. direction = goBackwards
  221. for i = 1, segLength do
  222. layBeltRow(direction, length)
  223. if i < segLength then
  224. goToNextRow()
  225. reverseDirection()
  226. end
  227. end
  228. end
  229.  
  230. function abort(message)
  231. print(message)
  232. error()
  233. end
  234.  
  235. -- Main Program
  236.  
  237. turtle.up()
  238. y = y + 1
  239.  
  240. turtle.select(1)
  241. layBeltSection(5, holeWidth)
  242.  
  243. xzHome()
  244. yHome()
  245. setFace(north)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement