Advertisement
asweigart

cultivateseeds

Dec 19th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. os.loadAPI('hare')
  2. os.loadAPI('act')
  3.  
  4. local PLANT_CHECK_INTERVAL = 20 -- wait 2 mins in between plant growth checks
  5.  
  6. function activateAA() -- AA is "autonomous activator"
  7. os.sleep(1) -- wait for items to go through hopper into AA
  8. redstone.setOutput('bottom', true)
  9. os.sleep(0.85) -- this is the tricky part, server lag might mess up this step, causing the AA to break the crop sticks
  10. redstone.setOutput('bottom', false)
  11. end
  12.  
  13.  
  14. function checkFacingHopper()
  15. local result, block
  16. result, block = turtle.inspect()
  17. assert(result == true and block['name'] == 'minecraft:hopper', 'Turtle is not facing a hopper')
  18. end
  19.  
  20.  
  21. function checkOverChest()
  22. local result, block
  23. result, block = turtle.inspectDown()
  24. assert(result == true and block['name'] == 'minecraft:chest', 'Turtle is not over a chest')
  25. end
  26.  
  27.  
  28. function dropSeedIntoHopper()
  29. local didDropSeed = false
  30. local slot, result
  31. for slot = 1, 16 do
  32. result = turtle.getItemDetail(slot)
  33. -- NOTE: This relies on "seed" being in the name of the seed item
  34. if result ~= nil and string.find(string.lower(result['name']), 'seed') then
  35. turtle.select(slot)
  36. act.doActions('dr')
  37. didDropSeed = true
  38. end
  39. end
  40. assert(didDropSeed, 'No seed to drop into seed analyzer hopper')
  41. end
  42.  
  43.  
  44. function condense()
  45. -- condense similar inventory items into fewer slots
  46. local slot1, slot2
  47. local itemData1, itemData2
  48. for slot1 = 1, 15 do
  49. turtle.select(slot1)
  50. for slot2 = slot1 + 1, 16 do
  51. if turtle.getItemCount() > 0 then
  52. itemData1 = turtle.getItemDetail(slot1)
  53. itemData2 = turtle.getItemDetail(slot2)
  54. if itemData1 ~= nil and itemData2 ~= nil and itemData1['name'] == itemData2['name'] then
  55. turtle.transferTo(slot2)
  56. end
  57. end
  58. end
  59. end
  60. end
  61.  
  62.  
  63. -- turtle start up checks
  64. local result, block
  65. result, block = turtle.inspectDown()
  66. assert(result == true and block['name'] == 'ExtraUtilities:trashcan', 'Start turtle on top of trash can')
  67.  
  68. result, block = turtle.inspect()
  69. assert(result == true and block['name'] == 'minecraft:hopper', 'Start turtle facing the hopper')
  70.  
  71. assert(turtle.getFuelLevel() > 1000, 'Start turtle w/ >= 1000 fuel')
  72.  
  73. --assert(hare.selectItem('AgriCraft:cropsItem'), 'Start turtle with >= 6 crop sticks')
  74. --assert(turtle.getItemCount() >= 6, 'Start turtle with >= 6 crop sticks')
  75.  
  76. local slot
  77. local numEmptySlots = 0
  78. for slot = 1, 16 do
  79. if turtle.getItemCount(slot) == 0 then
  80. numEmptySlots = numEmptySlots + 1
  81. end
  82. end
  83. -- 2 empty spaces needed for when picking up seeds and possibly essence
  84. assert(numEmptySlots >= 2, 'Start turtle with >= 2 empty slots')
  85.  
  86. print('Passed startup checks.')
  87.  
  88. while true do
  89. assert(hare.selectItem('AgriCraft:cropsItem'), 'Not enough crop sticks')
  90. assert(turtle.getItemCount() >= 6, 'Not enough crop sticks')
  91.  
  92. -- place crop stick in P1 & P2
  93. act.doActions('b pd b r f pd') -- turtle ends over P2, facing P2AA
  94. print('Placed cropstick in P2.')
  95.  
  96. -- get seeds from seed analyzer output chest
  97. assert(hare.selectEmptySlot(), 'Did not have empty space for seed pick up')
  98. act.doActions('l f 2')
  99.  
  100. --act.doActions('sd') -- DON'T DO THIS. If the seeds are two different levels, only 1 gets picked up
  101. assert(turtle.suckDown(1), 'Could not get first seed')
  102. assert(turtle.suckDown(1), 'Could not get second seed')
  103.  
  104. -- because non-magical crop seeds could have different names, we can't verify that seeds were taken from the chest here.
  105.  
  106.  
  107. print('Got seeds from chest.')
  108.  
  109. -- put seed into P2 AA
  110. act.doActions('b 2 r f')
  111. checkFacingHopper()
  112. assert(hare.selectItem('ee'), 'Could not select P2 seed') -- hopefully 'ee' is enough to match 'seed' in the seed name
  113. turtle.drop(1)
  114. print('Put seed into P2 AA.')
  115.  
  116. -- activate P2 AA to plant seed
  117. activateAA()
  118. print('Activated P2 AA.')
  119.  
  120. -- put seed into P1 AA
  121. act.doActions('b 3 l f l')
  122. checkFacingHopper()
  123. assert(hare.selectItem('ee'), 'Could not select P1 seed') -- hopefully 'ee' is enough to match 'seed' in the seed name
  124. turtle.drop(1)
  125. print('Put seed into P1 AA.')
  126.  
  127. -- activate P1 AA to plant seed
  128. activateAA()
  129. print('Activated P1 AA.')
  130.  
  131. -- put cropsticks in C1
  132. assert(hare.selectItem('AgriCraft:cropsItem'), 'Turtle somehow lost a cropstick')
  133. act.doActions('b 2 pd')
  134. print('Placed cropstick in C1.')
  135.  
  136. -- put cropsticks in C1 AA
  137. act.doActions('l 2 f')
  138. checkFacingHopper()
  139. turtle.drop(1)
  140. print('Put cropstick in C1 AA.')
  141.  
  142. -- activate C1 AA to place 2nd crop stick
  143. activateAA()
  144. print('Activated C1 AA.')
  145.  
  146. -- put cropsticks in C2
  147. assert(hare.selectItem('AgriCraft:cropsItem'), 'Turtle somehow lost a cropstick')
  148. act.doActions('b 2 l b pd')
  149. print('Placed cropstick in C2.')
  150.  
  151. -- put cropsticks in C2 AA
  152. act.doActions('l f')
  153. checkFacingHopper()
  154. turtle.drop(1)
  155. print('Put cropstick in C1 AA.')
  156.  
  157. -- activate C2 AA to plant seed
  158. activateAA()
  159. print('Activated C2 AA.')
  160.  
  161.  
  162. -- NOTE: The plants can't grow while the turtle is hovering over them.
  163.  
  164. -- check if C2 plant has grown
  165. print('Waiting for C2 to grow...')
  166. while true do
  167. os.sleep(PLANT_CHECK_INTERVAL)
  168. act.doActions('b')
  169. result, block = turtle.inspectDown()
  170. assert(result and block['name'] == 'AgriCraft:crops', 'Turtle somehow not over crops')
  171. if block['metadata'] > 0 then
  172. print('C2 has grown!')
  173. break
  174. else
  175. act.doActions('f')
  176. end
  177. end
  178.  
  179. -- harvest C2 plant
  180. act.doActions('dd')
  181.  
  182. -- put seed into seed analyzer hopper
  183. act.doActions('r f 2')
  184. checkFacingHopper()
  185. dropSeedIntoHopper()
  186.  
  187. -- move over the chest
  188. act.doActions('r f r')
  189. checkOverChest()
  190.  
  191. -- check if C1 plant has grown
  192. print('Waiting for C1 to grow...')
  193. while true do
  194. act.doActions('f')
  195. result, block = turtle.inspectDown()
  196. assert(result and block['name'] == 'AgriCraft:crops', 'Turtle somehow not over crops')
  197. if block['metadata'] > 0 then
  198. print('C1 has grown!')
  199. break
  200. else
  201. act.doActions('b')
  202. end
  203. os.sleep(PLANT_CHECK_INTERVAL) -- wait comes after for C1 because if C2 has grown, so has this one probably
  204. end
  205.  
  206. -- harvest C1 plant
  207. act.doActions('dd')
  208.  
  209. -- put seed into seed analyzer hopper
  210. act.doActions('b r f r')
  211. checkFacingHopper()
  212. dropSeedIntoHopper()
  213.  
  214. print('Collecting old plants to throw away...')
  215. -- pick up P1 plant
  216. act.doActions('b dd')
  217.  
  218. -- pick up P2 plant
  219. act.doActions('b r f dd')
  220.  
  221. -- trash the old seeds
  222. act.doActions('b l f 2')
  223. result, block = turtle.inspectDown()
  224. assert(result == true and block['name'] == 'ExtraUtilities:trashcan', 'Turtle is not over trash can')
  225.  
  226. local didDropSeed = false
  227. for slot = 1, 16 do
  228. result = turtle.getItemDetail(slot)
  229. -- NOTE: This relies on "seed" being in the name of the seed item
  230. if result ~= nil and string.find(string.lower(result['name']), 'seed') then
  231. turtle.select(slot)
  232. act.doActions('drd')
  233. didDropSeed = true
  234. end
  235. if result ~= nil and string.find(string.lower(result['name']), 'essence') then
  236. turtle.select(slot)
  237. act.doActions('drd')
  238. didDropSeed = true
  239. end
  240. end
  241. assert(didDropSeed, 'No seed to drop into trash can')
  242.  
  243.  
  244. -- At this point, the turtle will have 2 fewer cropsticks
  245. -- than at the start, but they're in multiple slots.
  246. -- Condense them all into as few slots as possible.
  247. condense()
  248.  
  249. os.sleep(10) -- just some extra time in case the seed analyzer is taking a while
  250.  
  251. print('Cycle complete. Repeating...')
  252. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement