Advertisement
HarvDad

belts

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