Advertisement
HarvDad

sprinkle

May 12th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1.  
  2. itemSlot = 16
  3.  
  4. x = 0
  5. y = 0
  6. z = 0
  7.  
  8. -- The following 'face' directions are relative to the starting position of the turtle in this program
  9.  
  10. north = 0
  11. west = 1
  12. south = 2
  13. east = 3
  14.  
  15. face = north
  16.  
  17. length = 9
  18. width = 9
  19.  
  20. interval = 4
  21.  
  22. args = {...}
  23. nArgs = #args
  24.  
  25. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  26. print("Lays down a pattern of items of length and width.")
  27. print("Usage: sprinkle <length> <width>")
  28. return
  29. end
  30.  
  31. if nArgs ~= 2 then
  32. print("Usage: sprinkle <distance>")
  33. return
  34. end
  35.  
  36. length = tonumber(args[1])
  37. if length == nil then
  38. print("\"", args[1], "\" is not a valid length")
  39. return
  40. end
  41. if length < 1 then
  42. print("length must be a positive number greater than zero")
  43. return
  44. end
  45.  
  46. width = tonumber(args[2])
  47. if width == nil then
  48. print("\"", args[1], "\" is not a valid width")
  49. return
  50. end
  51. if width < 1 then
  52. print("width must be a positive number greater than zero")
  53. return
  54. end
  55.  
  56. function left()
  57. if face == 0 then face = 1 turtle.turnLeft() return end
  58. if face == 1 then face = 2 turtle.turnLeft() return end
  59. if face == 2 then face = 3 turtle.turnLeft() return end
  60. if face == 3 then face = 0 turtle.turnLeft() return end
  61. print("function left\(\): Bad face value: ", face)
  62. end
  63.  
  64. function right()
  65. if face == 0 then face = 3 turtle.turnRight() return end
  66. if face == 1 then face = 0 turtle.turnRight() return end
  67. if face == 2 then face = 1 turtle.turnRight() return end
  68. if face == 3 then face = 2 turtle.turnRight() return end
  69. print("function right\(\): Bad face value: ", face)
  70. end
  71.  
  72. function setFace(f)
  73. if f == 0 then
  74. if face == 0 then return end
  75. if face == 1 then right() return end
  76. if face == 2 then right() right() return end
  77. if face == 3 then left() return end
  78. end
  79.  
  80. if f == 1 then
  81. if face == 0 then left() return end
  82. if face == 1 then return end
  83. if face == 2 then right() return end
  84. if face == 3 then right() right() return end
  85. end
  86.  
  87. if f == 2 then
  88. if face == 0 then left() left() return end
  89. if face == 1 then left() return end
  90. if face == 2 then return end
  91. if face == 3 then right() return end
  92. end
  93.  
  94. if f == 3 then
  95. if face == 0 then right() return end
  96. if face == 1 then left() left() return end
  97. if face == 2 then left() return end
  98. if face == 3 then return end
  99. end
  100. end
  101.  
  102. function forward(nBlocks)
  103. for i=1,nBlocks do
  104. turtle.forward()
  105. end
  106.  
  107. if face == north then z = z+nBlocks return end
  108. if face == west then x = x-nBlocks return end
  109. if face == south then z = z-nBlocks return end
  110. if face == east then x = x+nBlocks return end
  111.  
  112. end
  113.  
  114. function layRow(nBlocks)
  115. turtle.select(itemSlot)
  116.  
  117. for i=0,nBlocks do
  118. if (i % interval) == 0 then
  119. turtle.placeDown()
  120. end
  121. if i < nBlocks then
  122. forward(1)
  123. end
  124. end
  125. end
  126.  
  127. function layPattern(l, w)
  128.  
  129. -- setFace(north)
  130. layRow(w - 1)
  131.  
  132. setFace(west)
  133. layRow(l - 1)
  134.  
  135. setFace(south)
  136. layRow(w - 1)
  137.  
  138. setFace(east)
  139. layRow(l - 1)
  140.  
  141. setFace(north)
  142. end
  143.  
  144. radius = interval
  145.  
  146. -- Let's let the parent program get to start position
  147.  
  148. --[[
  149. turtle.up()
  150.  
  151. turtle.select(itemSlot)
  152. turtle.placeDown()
  153.  
  154. -- Move to starting position
  155.  
  156. setFace(north)
  157. forward(radius)
  158. setFace(west)
  159. forward(radius)
  160. --]]
  161.  
  162. layPattern(length, width)
  163.  
  164. setFace(north)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement