lucas_csantos

CactusFarm

Nov 25th, 2012
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. local farmY
  2. local initialX
  3. local initialZ
  4. local finalX
  5. local finalZ
  6. local farmDelay
  7. local state = "notDefined"
  8. local chestPosition
  9. local checkInterval = 60
  10. local tempVar = {}
  11.  
  12. function loadFarmInfo()
  13. if fs.exists("FarmInfo") then
  14. local file = fs.open("FarmInfo","r")
  15. for i=0,6 do
  16. tempVar[i] = tonumber(file.readLine())
  17. end
  18. file.close()
  19. farmY = tempVar[0]
  20. initialX = tempVar[1]
  21. initialZ = tempVar[2]
  22. finalX = tempVar[3]
  23. finalZ = tempVar[4]
  24. farmDelay = tempVar[5]
  25. chestPosition = tempVar[6]
  26.  
  27. else
  28. requestFarmInfo()
  29. end
  30. end
  31.  
  32. --Defines the farm information
  33. function requestFarmInfo()
  34. write("Farm Y level: ")
  35. farmY = read()
  36. write("Initial X: ")
  37. initialX = read()
  38. write("Initial Z: ")
  39. initialZ = read()
  40. write("Final X: ")
  41. finalX = read()
  42. write("Final Z: ")
  43. finalZ = read()
  44. write("How many seconds to wait between each cycle: ")
  45. farmDelay = read()
  46. write("Chest position (number) relative to turtle home: ")
  47. chestPosition = read()
  48.  
  49. local file = fs.open("FarmInfo","w")
  50. file.writeLine(farmY)
  51. file.writeLine(initialX)
  52. file.writeLine(initialZ)
  53. file.writeLine(finalX)
  54. file.writeLine(finalZ)
  55. file.writeLine(farmDelay)
  56. file.writeLine(chestPosition)
  57. file.close()
  58. end
  59.  
  60.  
  61. --Performs an action in a line
  62. function doLine(towardsFinal,action)
  63. if towardsFinal then
  64. while x ~= finalX do
  65. performAction(action)
  66. if initialX > finalX then
  67. shell.run("goto",x-1,y,z)
  68. else
  69. shell.run("goto",x+1,y,z)
  70. end
  71. end
  72.  
  73. else
  74. while x ~= initialX do
  75. performAction(action)
  76. if initialX > finalX then
  77. shell.run("goto",x+1,y,z)
  78. else
  79. shell.run("goto",x-1,y,z)
  80. end
  81. end
  82. end
  83. performAction(action)
  84. end
  85.  
  86. --Performs an action
  87. function performAction(action)
  88. if action == "collect" then
  89. suckAround()
  90. elseif action == "plant" then
  91. selectCactus()
  92. turtle.placeDown()
  93. end
  94. end
  95.  
  96. --Suck every direction to collect everything
  97. function suckAround()
  98. while turtle.suckUp() do end
  99. while turtle.suckDown() do end
  100. for i = 0,3 do
  101. while turtle.suck() do end
  102. shell.run("turnRight")
  103. end
  104. end
  105.  
  106. --Performs an action on the whole farm
  107. function doFarm(action)
  108. --Boolean variable that stores the direction the turtle has to go
  109. local towardsFinal
  110.  
  111. --Decide which direction the turtle has to go
  112. if (finalZ - initialZ)%2 == 0 then
  113. if (finalZ - z)%2 == 0 then
  114. towardsFinal = true
  115. else
  116. towardsFinal = false
  117. end
  118. else
  119. if(finalZ - z)%2 == 0 then
  120. towardsFinal = false
  121. else
  122. towardsFinal = true
  123. end
  124. end
  125.  
  126. --Call doLine for each line, until it reaches the final position
  127. while z ~= finalZ do
  128. doLine(towardsFinal,action)
  129. if initialZ > finalZ then
  130. shell.run("goto",x,y,z-1)
  131. else
  132. shell.run("goto",x,y,z+1)
  133. end
  134. --switch the direction the turtle has to go
  135. towardsFinal = not towardsFinal
  136. end
  137. doLine(towardsFinal,action)
  138. end
  139.  
  140. --Select the cactus to be planted
  141. function selectCactus()
  142. for i=1,15 do
  143. turtle.select(i)
  144. if turtle.compareTo(16) then
  145. break
  146. end
  147. end
  148. end
  149.  
  150. --Stores what the turtle just collected
  151. function dropOff()
  152. for i=1,15 do
  153. turtle.select(i)
  154. turtle.drop()
  155. end
  156. turtle.select(16)
  157. local toDrop = turtle.getItemCount(16) - 1
  158. turtle.drop(toDrop)
  159. end
  160.  
  161. --Saves the state of the turtle in a file, in case of reboot
  162. function registerState(newState)
  163. state = newState
  164. file = io.open("StateFile","w")
  165. file:write(state)
  166. file:close()
  167. displayInfo()
  168. end
  169.  
  170. --In case of reboot, this function will be called
  171. function loadState()
  172. if fs.exists("StateFile") then
  173. print("Loading State")
  174. file = io.open("StateFile","r")
  175. state = file:read()
  176. file:close()
  177. displayInfo()
  178. else
  179. print("No state found, defining goHome")
  180. state = "goHome"
  181. end
  182. end
  183.  
  184. function displayInfo()
  185. term.clear()
  186. term.setCursorPos(1,1)
  187. write("State: "..state)
  188. term.setCursorPos(1,2)
  189. write("Fuel Level: "..turtle.getFuelLevel())
  190. term.setCursorPos(1,3)
  191. write("Order: ")
  192. end
  193.  
  194. -- Main Code --
  195. function main()
  196. while(true) do
  197.  
  198. --Check for fuel
  199. if turtle.getFuelLevel() < 100 then
  200. write("Out of Fuel \n")
  201. break
  202. end
  203.  
  204. if state == "notDefined" then
  205. loadState()
  206. end
  207.  
  208. if state == "collect" then
  209. doFarm("collect")
  210. registerState("goOriginUp")
  211. end
  212.  
  213. if state == "goOriginUp" then
  214. shell.run("goto",initialX,farmY+1,initialZ)
  215. registerState("plant")
  216. end
  217.  
  218. if state == "plant" then
  219. doFarm("plant")
  220. registerState("goHome")
  221. end
  222.  
  223. if state == "goHome" then
  224. shell.run("gohome")
  225. shell.run("setFacing",chestPosition)
  226. dropOff()
  227. registerState("wait"..farmDelay)
  228. end
  229.  
  230. if string.match(state,"wait") == "wait" then
  231. time = string.match(state,"%p*%d+")
  232. time = tonumber(time)
  233. if time <= 0 then
  234. registerState("goOrigin")
  235. else
  236. sleep(checkInterval)
  237. time = time - checkInterval
  238. registerState("wait"..time)
  239. end
  240. end
  241.  
  242. if state == "goOrigin" then
  243. turtle.select(1)
  244. shell.run("goto",initialX,farmY,initialZ)
  245. registerState("collect")
  246. end
  247.  
  248. end
  249.  
  250. end
  251.  
  252. function readInput()
  253. while (true) do
  254. term.setCursorPos(8,3)
  255. local input = read()
  256. if input == "quit" then
  257. term.clear()
  258. return
  259. end
  260. if input == "resetFarm" then
  261. term.clear()
  262. fs.delete("FarmInfo")
  263. return
  264. end
  265. end
  266. end
  267.  
  268. loadFarmInfo()
  269. --Run the farm program parallel to the readInput function, so you can give orders while the turtle works
  270. parallel.waitForAny(readInput, main)
Advertisement
Add Comment
Please, Sign In to add comment