Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. -- Config:
  2. WIDTH, HEIGHT = CHANGE_ME, CHANGE_ME
  3. HOE, PICKAXE = 'left', 'right'
  4. HARVEST_TOOL = PICKAXE -- change to PICKAXE if available
  5. MIN_FUEL = WIDTH * HEIGHT * 3
  6. MIN_SLOTS = 0 -- 0 = ignore inv check
  7. FIELD_CYCLE_DELAY = 3*60 -- Delay after moving over complete field
  8. SOIL_BLOCKS = { 'minecraft:dirt', 'minecraft:grass', 'minecraft:end_stone' }
  9.  
  10. -- Rice:
  11. --CROP_ITEM = 'harvestcraft:riceitem'
  12. --CROP_BLOCK = 'harvestcraft:pamricecrop'
  13. --CROP_BLOCK_SEASONED_METADATA = 3
  14. -- Carrots:
  15. --CROP_ITEM = 'minecraft:carrot'
  16. --CROP_BLOCK = 'minecraft:carrots'
  17. --CROP_BLOCK_SEASONED_METADATA = 7
  18. -- Beetroot:
  19. --CROP_ITEM = 'minecraft:beetroot_seeds'
  20. --CROP_BLOCK = 'minecraft:beetroots'
  21. --CROP_OTHER_ITEMS = { 'minecraft:beetroot' }
  22. --CROP_BLOCK_SEASONED_METADATA = 3
  23. -- Ender Lilly:
  24. CROP_ITEM = 'extrautils2:enderlilly'
  25. CROP_BLOCK = 'extrautils2:enderlilly'
  26. CROP_OTHER_ITEMS = { 'minecraft:ender_pearl' }
  27. CROP_BLOCK_SEASONED_METADATA = 7
  28. ------------
  29.  
  30. t = turtle
  31. missingFields, seasonedFields = 0, 0
  32.  
  33. function contains(list, element)
  34. i = 0
  35. while i < #list do
  36. i = i + 1
  37. if list[i] == element then
  38. return true
  39. end
  40. end
  41. return false
  42. end
  43.  
  44. function countFreeSlots()
  45. freeSlots = 0
  46. slot = 1
  47. while slot <= 16 do
  48. if t.getItemCount(slot) == 0 then
  49. freeSlots = freeSlots + 1
  50. end
  51. slot = slot + 1
  52. end
  53. return freeSlots
  54. end
  55.  
  56. function countCropItem()
  57. cropItems = 0
  58. slot = 1
  59. while slot <= 16 do
  60. if t.getItemCount(slot) > 0 and t.getItemDetail(slot)['name'] == CROP_ITEM then
  61. cropItems = cropItems + t.getItemCount(slot)
  62. end
  63. slot = slot + 1
  64. end
  65. return cropItems
  66. end
  67.  
  68.  
  69. function fuelCheck()
  70. if not refuel() then
  71. print('Waiting for fuel...')
  72. while not refuel() do
  73. sleep(3)
  74. end
  75. end
  76. end
  77.  
  78. function invCheck()
  79. if MIN_SLOTS > 0 and countFreeSlots() < MIN_SLOTS then
  80. print('Waiting for inv to be cleared.')
  81. while countFreeSlots() < MIN_SLOTS do
  82. sleep(3)
  83. end
  84. end
  85. end
  86.  
  87. function attemptDeposit()
  88. keepAttempting, depositSucceeded = true, false
  89. while keepAttempting do
  90. i = 0
  91. while i < 4 do
  92. if not depositSucceeded then
  93. depositSucceeded = depositCropItems()
  94. end
  95. i = i + 1
  96. t.turnRight()
  97. end
  98. if depositSucceeded then
  99. keepAttempting = false
  100. elseif not detectDown() then
  101. t.down()
  102. elseif not depositSucceeded and detectDown() then
  103. keepAttempting = false
  104. end
  105. end
  106.  
  107. end
  108.  
  109. function depositCropItems()
  110. found, data = t.inspect()
  111. if not found then
  112. return false
  113. end
  114. if string.find(data['name'], 'chest') == nil then
  115. return false
  116. end
  117.  
  118. cropItems = countCropItem()
  119. slot = 1
  120. while slot <= 16 do
  121. if t.getItemCount(slot) > 0 and t.getItemDetail(slot)['name'] == CROP_ITEM then
  122. t.select(slot)
  123. if cropItems >= missingFields then
  124. count = math.min(cropItems - missingFields, t.getItemCount())
  125. if t.drop(count) then
  126. cropItems = cropItems - count
  127. else
  128. return false
  129. end
  130. end
  131. elseif not(CROP_OTHER_ITEMS == nil) and t.getItemCount(slot) > 0 and contains(CROP_OTHER_ITEMS, t.getItemDetail(slot)['name']) then
  132. t.select(slot)
  133. if not t.drop(t.getItemCount(slot)) then
  134. return false
  135. end
  136. end
  137. slot = slot + 1
  138. end
  139. return true
  140. end
  141.  
  142.  
  143. function refuel()
  144. if t.getFuelLevel() > MIN_FUEL then
  145. return true
  146. end
  147. slot = 1
  148. while slot <= 16 do
  149. t.select(slot)
  150. t.refuel()
  151. slot = slot + 1
  152. end
  153. return t.getFuelLevel() > MIN_FUEL
  154. end
  155.  
  156. function detectDown()
  157. -- Also detects fluids
  158. found, _ = t.inspectDown()
  159. return found
  160. end
  161.  
  162. function selectCropItem()
  163. slot = 16
  164. while slot >= 1 do
  165. if t.getItemCount(slot) > 0 and t.getItemDetail(slot)['name'] == CROP_ITEM then
  166. t.select(slot)
  167. return true
  168. end
  169. slot = slot - 1
  170. end
  171. --print('NO CROP ITEMS FOUND!')
  172. return false
  173. end
  174.  
  175. function countAvailableCropItemSpace()
  176. availableSpace = 0
  177. slot = 1
  178. while slot <= 16 do
  179. if t.getItemCount(slot) == 0 then
  180. availableSpace = availableSpace + 64
  181. elseif t.getItemDetail(slot)['name'] == CROP_ITEM then
  182. availableSpace = availableSpace + t.getItemSpace(slot)
  183. end
  184. slot = slot + 1
  185. end
  186. return availableSpace
  187. end
  188.  
  189. function harvestAndPlant(tool)
  190. t.select(1) -- Add correctly to inv
  191. plantCropItem = false
  192.  
  193. if tool == PICKAXE then
  194. t.digDown(PICKAXE)
  195. -- Automatically picks up items
  196. plantCropItem = true
  197. elseif tool == HOE then
  198. if selectCropItem() then
  199. t.placeDown()
  200. else
  201. t.digDown(HOE)
  202. plantCropItem = true
  203. end
  204. -- Doesn't pick up items
  205. t.suckDown()
  206. sleep(0.25)
  207. t.suckDown()
  208. end
  209.  
  210. if plantCropItem and selectCropItem() then
  211. t.placeDown()
  212. elseif plantCropItem then
  213. missingFields = missingFields + 1
  214. end
  215. end
  216.  
  217. function careRow(length)
  218. pos = 0
  219. while pos < length do
  220. -- Get to ground:
  221. while not detectDown() do
  222. t.down()
  223. end
  224.  
  225. -- Field:
  226. _, data = t.inspectDown()
  227. name = data['name']
  228. -- Place crop item on empty field:
  229. if contains(SOIL_BLOCKS, name) then
  230. if selectCropItem() then
  231. t.up()
  232. t.digDown(HOE)
  233. t.placeDown()
  234. else
  235. missingFields = missingFields + 1
  236. end
  237. end
  238. -- Check crop item field state:
  239. if name == CROP_BLOCK and data['metadata'] == CROP_BLOCK_SEASONED_METADATA then
  240. if (CROP_OTHER_ITEMS == nil and countAvailableCropItemSpace() >= 3) or countFreeSlots() > 0 then
  241. harvestAndPlant(HARVEST_TOOL)
  242. else
  243. seasonedFields = seasonedFields + 1
  244. end
  245. end
  246.  
  247. -- Climb:
  248. while t.detect() and not t.detectUp() do
  249. t.up()
  250. end
  251.  
  252. if not (pos == (length-1)) then
  253. while not t.forward() do
  254. sleep(1)
  255. end
  256. end
  257. pos = pos + 1
  258. end
  259. end
  260.  
  261. f, belowData = t.inspectDown()
  262. if f and not (string.find(belowData['name'], 'chest') == nil) then
  263. t.forward()
  264. end
  265.  
  266. xDir, yDir = 1, 1 -- 1 = forwards ; -1 = back
  267. col = 0
  268. x, y = 0, 0
  269. fuelCheck()
  270. invCheck()
  271. while true do
  272. careRow(HEIGHT)
  273. x = x + xDir
  274. y = y + yDir*(HEIGHT-1)
  275. yDir = yDir * -1
  276. --print('X: ' .. x .. ' ; Y: ' .. y)
  277. if x == WIDTH or x == 0 then
  278. attemptDeposit()
  279. t.turnRight()
  280. t.turnRight()
  281. xDir = xDir * -1
  282. if missingFields > 0 then
  283. print(missingFields .. ' crop items are needed for next cycle.')
  284. end
  285. if seasonedFields > 0 or (missingFields > 0 and selectCropItem()) then
  286. print('Work is not done, yet.')
  287. else
  288. print('Sleeping ' .. FIELD_CYCLE_DELAY .. ' seconds...')
  289. sleep(FIELD_CYCLE_DELAY)
  290. end
  291. seasonedFields = 0
  292. missingFields = 0
  293. fuelCheck()
  294. invCheck()
  295.  
  296. elseif (xDir == 1 and x % 2 == 1 and not (y == 0)) or (xDir == -1 and x % 2 == 0 and y == 0) then
  297. -- Next row is the the right:
  298. t.turnRight()
  299.  
  300. -- Climb:
  301. while t.detect() do
  302. t.up()
  303. end
  304.  
  305. while not t.forward() do
  306. sleep(1)
  307. end
  308. t.turnRight()
  309.  
  310. elseif (xDir == 1 and x % 2 == 0 and y == 0) or (yDir == -1 and x % 2 == 1 and not (y == 0)) then
  311. -- Next row is to the left:
  312. t.turnLeft()
  313.  
  314. -- Climb:
  315. while t.detect() do
  316. t.up()
  317. end
  318. while not t.forward() do
  319. sleep(1)
  320. end
  321.  
  322. t.turnLeft()
  323. else
  324. print('PANIC: UNEXPECTED CASE!!!')
  325. break
  326. end
  327.  
  328. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement