Advertisement
HarvDad

cactus

Apr 10th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. -- cactus
  2. -- Harvests the top 2 blocks of cacti from the top
  3. -- Turtle returns to its starting point when mission is completed or fuel runs low
  4. -- Written by HarvDad, April 2014
  5.  
  6. args = {...}
  7. nArgs = #args
  8.  
  9. print("cactus: Rev 1.2")
  10.  
  11. x = 0
  12. y = 0
  13. z = 0
  14. face = 0
  15. cactusSlot = 1
  16. minimumFuel = 100
  17. missionMessage = "Mission complete."
  18. abort = false
  19. local currentFuelLevel = turtle.getFuelLevel()
  20.  
  21. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  22. print("length, width, and height")
  23. print("Usage: cactus <length> <width>")
  24. return
  25. end
  26.  
  27. if nArgs ~= 2 then
  28. print("Usage: clear <length> <width>")
  29. return
  30. end
  31.  
  32. length = tonumber(args[1])
  33. if length == nil then
  34. print("\"", args[1], "\" is not a valid length")
  35. return
  36. end
  37. if length < 1 then
  38. print("length must be a positive number greater than zero")
  39. end
  40.  
  41. width = tonumber(args[2])
  42. if width == nil then
  43. print("\"", args[2], "\" is not a valid width")
  44. return
  45. end
  46. if width < 1 then
  47. print("width must be a positive number greater than zero")
  48. end
  49.  
  50. targetArea = length * width
  51. areaCovered = 1;
  52.  
  53. function chomp()
  54. turtle.select(cactusSlot)
  55. if turtle.compareDown() then
  56. turtle.digDown()
  57. end
  58. areaCovered = areaCovered+1
  59. end
  60.  
  61. direction = "left"
  62.  
  63. local clock = os.clock
  64. function sleep(n) -- seconds
  65. local t0 = clock()
  66. while clock() - t0 <= n do end
  67. end
  68.  
  69. function left()
  70. if face == 0 then face = 1 turtle.turnLeft() return end
  71. if face == 1 then face = 2 turtle.turnLeft() return end
  72. if face == 2 then face = 3 turtle.turnLeft() return end
  73. if face == 3 then face = 0 turtle.turnLeft() return end
  74. print("function left\(\): Bad face value: ", face)
  75. end
  76.  
  77. function right()
  78. if face == 0 then face = 3 turtle.turnRight() return end
  79. if face == 1 then face = 0 turtle.turnRight() return end
  80. if face == 2 then face = 1 turtle.turnRight() return end
  81. if face == 3 then face = 2 turtle.turnRight() return end
  82. print("function right\(\): Bad face value: ", face)
  83. end
  84.  
  85. function up()
  86. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  87. turtle.digUp()
  88. end
  89. turtle.up()
  90. y = y+1
  91. end
  92.  
  93. function rise(nBlocks)
  94. local i
  95. for i=1,nBlocks do
  96. up()
  97. end
  98. end
  99.  
  100. function down()
  101. if turtle.detectDown() then
  102. turtle.digDown()
  103. end
  104. turtle.down()
  105. y = y-1
  106. end
  107.  
  108. function turn()
  109. if direction == "left" then
  110. left()
  111. else
  112. right()
  113. end
  114. chomp()
  115. forward()
  116. if direction == "left" then
  117. left()
  118. direction = "right"
  119. else
  120. right()
  121. direction = "left"
  122. end
  123. end
  124.  
  125. function forward()
  126. while turtle.detect() do -- This loop added in case of falling sand or whatever
  127. turtle.dig()
  128. end
  129. for i=1,10 do
  130. if turtle.forward() then
  131. break
  132. end
  133. turtle.attack()
  134. sleep(2)
  135. end
  136. if face == 0 then z = z+1 return end
  137. if face == 1 then x = x-1 return end
  138. if face == 2 then z = z-1 return end
  139. if face == 3 then x = x+1 return end
  140. end
  141.  
  142. function setFace(f)
  143. if f == 0 then
  144. if face == 0 then return end
  145. if face == 1 then right() return end
  146. if face == 2 then right() right() return end
  147. if face == 3 then left() return end
  148. end
  149.  
  150. if f == 1 then
  151. if face == 0 then left() return end
  152. if face == 1 then return end
  153. if face == 2 then right() return end
  154. if face == 3 then right() right() return end
  155. end
  156.  
  157. if f == 2 then
  158. if face == 0 then left() left() return end
  159. if face == 1 then left() return end
  160. if face == 2 then return end
  161. if face == 3 then right() return end
  162. end
  163.  
  164. if f == 3 then
  165. if face == 0 then right() return end
  166. if face == 1 then left() left() return end
  167. if face == 2 then left() return end
  168. if face == 3 then return end
  169. end
  170. end
  171.  
  172. function home(targetY)
  173. -- print("home:face ", face, ", x = ", x, ", z = ", z)
  174. if x < 0 then
  175. setFace(3)
  176. while x < 0 do
  177. forward()
  178. end
  179. else
  180. if x > 0 then
  181. setFace(1)
  182. while x > 0 do
  183. forward()
  184. end
  185. end
  186. end
  187.  
  188. if z < 0 then
  189. setFace(0)
  190. while z < 0 do
  191. forward()
  192. end
  193. else
  194. if z > 0 then
  195. setFace(2)
  196. while z > 0 do
  197. forward()
  198. end
  199. end
  200. end
  201. setFace(0)
  202.  
  203. if (targetY == 0) then
  204. while y < 0 do
  205. up()
  206. end
  207. end
  208. end
  209.  
  210. function harvest(nBlocks)
  211. local i
  212. for i=1,nBlocks do
  213. chomp()
  214. checkFuel()
  215. if abort then
  216. areaCovered = targetArea
  217. end
  218.  
  219. if areaCovered < targetArea then
  220. forward()
  221. end
  222. end
  223. end
  224.  
  225.  
  226. function checkFuel()
  227. if currentFuelLevel == "unlimited" then
  228. return true
  229. end
  230.  
  231. if currentFuelLevel < minimumFuel then
  232. if not turtle.refuel() then
  233. areaCovered = targetArea
  234. missionMessage = "Mission aborted due to low fuel."
  235. abort = true
  236. return false
  237. end
  238. end
  239. return true
  240. end
  241.  
  242. -- MAIN PROGRAM
  243.  
  244. turtle.select(1)
  245.  
  246. print("fuelLevel = ", currentFuelLevel)
  247. print("length = ", length)
  248. print("width = ", width)
  249. up()
  250. up()
  251. up()
  252.  
  253. height = 2
  254.  
  255. for d=1,height do
  256. for w=1,width do
  257. harvest(length-1)
  258. if areaCovered < targetArea then
  259. turn()
  260. end
  261. if abort then
  262. home(0)
  263. end
  264. end
  265. home(99)
  266. if d < height then
  267. down()
  268. direction = "left"
  269. areaCovered = 1;
  270. end
  271. end
  272.  
  273. print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
  274. home(0)
  275. down()
  276. down()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement