Advertisement
HarvDad

pattern

Jun 8th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1.  
  2. args = {...}
  3. nArgs = #args
  4.  
  5. version = "pattern: Rev 1.2"
  6. mission = "Lay down the specified pattern."
  7. instructions = "Place stack(s) of material starting in first slot."
  8.  
  9. usage = "usage: pattern <pattern name>"
  10.  
  11. funnel12 = {
  12. "============",
  13. "============",
  14. "============",
  15. "============",
  16. "============",
  17. "===== =====",
  18. "===== =====",
  19. "============",
  20. "============",
  21. "============",
  22. "============",
  23. "============",
  24. }
  25.  
  26. fullShape = {
  27. " = ",
  28. " === ",
  29. " ===== ",
  30. " ======= ",
  31. " ========= ",
  32. " =========== ",
  33. " ============= ",
  34. " =============== ",
  35. "=================",
  36. " =============== ",
  37. " ============= ",
  38. " =========== ",
  39. " ========= ",
  40. " ======= ",
  41. " ===== ",
  42. " === ",
  43. " = ",
  44. }
  45.  
  46. reducedShape = {
  47. " ",
  48. " = ",
  49. " === ",
  50. " ===== ",
  51. " ======= ",
  52. " ========= ",
  53. " =========== ",
  54. " ============= ",
  55. " =============== ",
  56. " ============= ",
  57. " =========== ",
  58. " ========= ",
  59. " ======= ",
  60. " ===== ",
  61. " === ",
  62. " = ",
  63. " ",
  64. }
  65.  
  66. walls = {
  67. " = ",
  68. " = = ",
  69. " = = ",
  70. " = = ",
  71. " = = ",
  72. " = = ",
  73. " = = ",
  74. " = = ",
  75. "= =",
  76. " = = ",
  77. " = = ",
  78. " = = ",
  79. " = = ",
  80. " = = ",
  81. " = = ",
  82. " = = ",
  83. " = ",
  84. }
  85.  
  86. x = 0
  87. y = 0
  88. z = 0
  89.  
  90. -- The following 'face' directions are relative to the starting position of the turtle in this program
  91. north = 0
  92. west = 1
  93. south = 2
  94. east = 3
  95.  
  96. materialSlot = 1
  97. face = north
  98. nextTurn = "right"
  99.  
  100. function ensureMaterial()
  101. if turtle.getItemCount(materialSlot) < 3 then
  102. organizeMaterial()
  103. end
  104. if turtle.getItemCount(materialSlot) < 3 then
  105. print("No more material")
  106. return false
  107. end
  108. return true
  109. end
  110.  
  111. function organizeMaterial()
  112. local i
  113. materialCount = turtle.getItemCount(materialSlot)
  114.  
  115. if materialCount < 3 then
  116. -- print("Attempting to refill slot ", materialSlot)
  117. for i=2,16 do
  118. turtle.select(i)
  119. if turtle.compareTo(materialSlot) then
  120. turtle.transferTo(materialSlot)
  121. end
  122. end
  123. end
  124. turtle.select(materialSlot)
  125. end
  126.  
  127. function left()
  128. if face == 0 then face = 1 turtle.turnLeft() return end
  129. if face == 1 then face = 2 turtle.turnLeft() return end
  130. if face == 2 then face = 3 turtle.turnLeft() return end
  131. if face == 3 then face = 0 turtle.turnLeft() return end
  132. print("function left\(\): Bad face value: ", face)
  133. end
  134.  
  135. function right()
  136. if face == 0 then face = 3 turtle.turnRight() return end
  137. if face == 1 then face = 0 turtle.turnRight() return end
  138. if face == 2 then face = 1 turtle.turnRight() return end
  139. if face == 3 then face = 2 turtle.turnRight() return end
  140. print("function right\(\): Bad face value: ", face)
  141. end
  142.  
  143. function turn()
  144. if nextTurn == "left" then
  145. left()
  146. else
  147. right()
  148. end
  149. forward()
  150. if nextTurn == "left" then
  151. left()
  152. nextTurn = "right"
  153. else
  154. right()
  155. nextTurn = "left"
  156. end
  157. end
  158.  
  159. function forward()
  160. for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
  161. if turtle.forward() then
  162. break
  163. end
  164. turtle.attack()
  165. sleep(2)
  166. end
  167.  
  168. if face == north then z = z+1 return end
  169. if face == west then x = x-1 return end
  170. if face == south then z = z-1 return end
  171. if face == east then x = x+1 return end
  172. end
  173.  
  174. function setFace(f)
  175. if f == 0 then
  176. if face == 0 then return end
  177. if face == 1 then right() return end
  178. if face == 2 then right() right() return end
  179. if face == 3 then left() return end
  180. end
  181.  
  182. if f == 1 then
  183. if face == 0 then left() return end
  184. if face == 1 then return end
  185. if face == 2 then right() return end
  186. if face == 3 then right() right() return end
  187. end
  188.  
  189. if f == 2 then
  190. if face == 0 then left() left() return end
  191. if face == 1 then left() return end
  192. if face == 2 then return end
  193. if face == 3 then right() return end
  194. end
  195.  
  196. if f == 3 then
  197. if face == 0 then right() return end
  198. if face == 1 then left() left() return end
  199. if face == 2 then left() return end
  200. if face == 3 then return end
  201. end
  202. end
  203.  
  204. function home(targetY)
  205. -- print("home:face ", face, ", x = ", x, ", z = ", z)
  206. if x < 0 then
  207. setFace(east)
  208. while x < 0 do
  209. forward()
  210. end
  211. else
  212. if x > 0 then
  213. setFace(west)
  214. while x > 0 do
  215. forward()
  216. end
  217. end
  218. end
  219.  
  220. if z < 0 then
  221. setFace(north)
  222. while z < 0 do
  223. forward()
  224. end
  225. else
  226. if z > 0 then
  227. setFace(south)
  228. while z > 0 do
  229. forward()
  230. end
  231. end
  232. end
  233.  
  234. if y > 0 then
  235. while y > 0 do
  236. down()
  237. end
  238. end
  239.  
  240. setFace(0)
  241. end
  242.  
  243.  
  244. function layPattern(pattern)
  245. for i=1,#pattern do
  246. row = pattern[i]
  247. length = string.len(row)
  248. for j=1,string.len(row) do
  249. char = string.sub(row, j, j)
  250. if char ~= " " then
  251. ensureMaterial()
  252. turtle.select(materialSlot)
  253. turtle.placeDown()
  254. end
  255. if j < length then
  256. forward()
  257. end
  258. end
  259. turn()
  260. end
  261. end
  262.  
  263.  
  264. if nArgs < 1 then
  265. print(usage)
  266. return
  267. end
  268.  
  269. if args[1] == "funnel12" then
  270. layPattern(funnel12)
  271. elseif args[1] == "full" then
  272. layPattern(fullShape)
  273. elseif args[1] == "reduced" then
  274. layPattern(reducedShape)
  275. elseif args[1] == "walls" then
  276. layPattern(walls)
  277. else
  278. print("Unknown pattern: \"", args[1], "\"")
  279. return
  280. end
  281.  
  282. home()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement