Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. -- Crop Breeder from hoblin
  2. -- pastebin get fVj7fU52 cb
  3. -- Version 0.2.3
  4.  
  5. --
  6. -- LAYOUT
  7. --
  8. -- Layer 1
  9. -- - - -
  10. -- D D -
  11. -- W D -
  12. --
  13. -- Layer 2
  14. -- P P T C
  15. -- - - N C
  16. -- C - P C
  17. --
  18. -- W - water block
  19. -- P - Autonomous Activator
  20. -- T - Trashcan
  21. -- N - Crop Analyser
  22. -- C - Chest
  23. -- D - Soil
  24.  
  25.  
  26. --
  27. -- System functions
  28. --
  29. function debug(message)
  30. if debugging then
  31. print(message)
  32. end
  33. end
  34.  
  35. function resetScreen()
  36. if not debugging then
  37. term.clear()
  38. term.setCursorPos(1,1)
  39. end
  40. end
  41.  
  42.  
  43. --
  44. -- Breeder
  45. --
  46.  
  47. Breeder = {}
  48.  
  49. function Breeder:new()
  50. local obj = {
  51. redstone_pulse_length = 0.9,
  52. replace_seed_index = 0,
  53. seed = {name = '', gr = 0, ga = 0, st = 0},
  54. slot = {
  55. fuel = 1,
  56. sticks = 2,
  57. seed = 3,
  58. extra = 4
  59. },
  60. positions = {
  61. x = 2,
  62. y = 1,
  63. chest = {x = 0, y = 0},
  64. analyser = {x = 2, y = 1},
  65. trashcan = {x = 2, y = 2},
  66. seed = {},
  67. activator = {}
  68. }
  69. }
  70. obj.positions.seed[0] = {x = 1, y = 0}
  71. obj.positions.seed[1] = {x = 0, y = 1}
  72. obj.positions.seed[2] = {x = 1, y = 1}
  73. obj.positions.activator[0] = {x = 2, y = 0}
  74. obj.positions.activator[1] = {x = 0, y = 2}
  75. obj.positions.activator[2] = {x = 1, y = 2}
  76.  
  77. function obj:getFuel()
  78. local current_amount = turtle.getItemCount(self.slot.fuel)
  79. if current_amount < 2 then
  80. self:move_to(self.positions.trashcan)
  81. turtle.select(self.slot.fuel)
  82. turtle.turnRight()
  83. turtle.suck(64 - current_amount)
  84. turtle.turnLeft()
  85. if turtle.getItemCount() == 0 then
  86. error('Out of fuel')
  87. end
  88. end
  89. end
  90.  
  91. function obj:getSticks()
  92. local current_amount = turtle.getItemCount(self.slot.sticks)
  93. if current_amount < 60 then
  94. self:move_to(self.positions.activator[0])
  95. turtle.select(self.slot.sticks)
  96. turtle.turnRight()
  97. turtle.suck(60 - current_amount)
  98. turtle.turnLeft()
  99. if turtle.getItemCount() == 0 then
  100. error('Out of sticks')
  101. end
  102. end
  103. end
  104.  
  105. function obj:getSeed()
  106. self:move_to(self.positions.analyser)
  107. turtle.select(self.slot.seed)
  108. local current_amount = turtle.getItemCount()
  109. if current_amount == 0 then
  110. turtle.turnRight()
  111. turtle.suck(2)
  112. turtle.turnLeft()
  113. end
  114. if turtle.getItemCount() == 0 then
  115. error('Out of seeds')
  116. end
  117. self:updateSeedStats(self:analyse())
  118. seed_info = turtle.getItemDetail()
  119. self.seed.name = seed_info.name
  120. end
  121.  
  122. function obj:updateSeedStats(stats)
  123. self.seed.gr = stats[1]
  124. self.seed.ga = stats[2]
  125. self.seed.st = stats[3]
  126. end
  127.  
  128. function obj:analyse()
  129. self:move_to(self.positions.analyser)
  130. analyser = peripheral.wrap("bottom")
  131. turtle.select(self.slot.seed)
  132. turtle.dropDown()
  133. analyser.analyze()
  134. while not analyser.isAnalyzed() do
  135. os.sleep(0.2)
  136. end
  137. gr, ga, st = analyser.getSpecimenStats()
  138. turtle.suckDown()
  139. return {gr, ga, st}
  140. end
  141.  
  142. function obj:move_to(move_to)
  143. x_offset = self.positions.x - move_to.x
  144. y_offset = self.positions.y - move_to.y
  145. if x_offset > 0 then
  146. turtle.turnLeft()
  147. for i = 1, x_offset do
  148. turtle.forward()
  149. end
  150. turtle.turnRight()
  151. elseif x_offset < 0 then
  152. turtle.turnRight()
  153. for i = 1, math.abs(x_offset) do
  154. turtle.forward()
  155. end
  156. turtle.turnLeft()
  157. end
  158. if y_offset > 0 then
  159. for i = 1, y_offset do
  160. turtle.back()
  161. end
  162. elseif y_offset < 0 then
  163. for i = 1, math.abs(y_offset) do
  164. turtle.forward()
  165. end
  166. end
  167. self.positions.x = move_to.x
  168. self.positions.y = move_to.y
  169. return true
  170. end
  171.  
  172. function obj:redstonePulse()
  173. redstone.setOutput("bottom", true)
  174. os.sleep(self.redstone_pulse_length)
  175. redstone.setOutput("bottom", false)
  176. end
  177.  
  178. function obj:sow(index)
  179. self:move_to(self.positions.seed[index])
  180. turtle.select(self.slot.sticks)
  181. turtle.placeDown(1)
  182. self:move_to(self.positions.activator[index])
  183. turtle.select(self.slot.seed)
  184. turtle.dropDown(1)
  185. self:redstonePulse()
  186. end
  187.  
  188. function obj:breed()
  189. self:move_to(self.positions.seed[2])
  190. turtle.select(self.slot.sticks)
  191. turtle.placeDown(1)
  192. self:move_to(self.positions.activator[2])
  193. turtle.dropDown(1)
  194. self:redstonePulse()
  195. end
  196.  
  197. function obj:fixCrossCropSticks()
  198. self:checkFuel()
  199. self:checkSticks()
  200. self:move_to(self.positions.activator[2])
  201. turtle.select(self.slot.sticks)
  202. turtle.suckDown(1)
  203. turtle.dropDown(1)
  204. self:redstonePulse()
  205. self:move_to(self.positions.analyser)
  206. end
  207.  
  208. function obj:harvest(index)
  209. self:move_to(self.positions.seed[index])
  210. turtle.select(self.slot.sticks)
  211. turtle.digDown()
  212. end
  213.  
  214. function obj:waitForBreeding()
  215. self:move_to(self.positions.analyser)
  216. analyser = peripheral.wrap("bottom")
  217. while not analyser.hasPlant("SOUTH") do
  218. if not analyser.isCrossCrop("SOUTH") then
  219. self:fixCrossCropSticks()
  220. end
  221. if analyser.hasWeeds("SOUTH") then
  222. self:harvest(2)
  223. self:breed()
  224. self:move_to(self.positions.analyser)
  225. end
  226. sleep(1)
  227. end
  228. end
  229.  
  230. function obj:setupField()
  231. if turtle.getItemCount(self.slot.seed) == 2 then
  232. self:sow(0)
  233. self:breed()
  234. self:sow(1)
  235. elseif turtle.getItemCount(self.slot.seed) == 1 then
  236. self:sow(0)
  237. self:breed()
  238. self:waitForBreeding()
  239. self:harvest(2)
  240. self:analyse()
  241. self:sow(1)
  242. self:breed()
  243. else
  244. error('Wrong seeds amount')
  245. end
  246. end
  247.  
  248. function obj:cleanField()
  249. self:harvest(0)
  250. self:cleanInventory(self.slot.seed)
  251. self:cleanInventory(self.slot.extra)
  252. self:harvest(1)
  253. self:cleanInventory(self.slot.seed)
  254. self:cleanInventory(self.slot.extra)
  255. end
  256.  
  257. function obj:isSeedBreeded()
  258. return (self.seed.gr + self.seed.ga + self.seed.st == 30)
  259. end
  260.  
  261. function obj:cleanInventory(index)
  262. items_to_drop_amount = 0
  263. for i=index, self.slot.extra + 1 do
  264. items_to_drop_amount = items_to_drop_amount + turtle.getItemCount(i)
  265. end
  266. if items_to_drop_amount > 0 then
  267. self:move_to(self.positions.trashcan)
  268. for i=index, self.slot.extra + 1 do
  269. turtle.select(i)
  270. turtle.dropDown()
  271. end
  272. end
  273. end
  274.  
  275. function obj:checkFuel()
  276. while turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 50 do
  277. turtle.select(self.slot.fuel)
  278. turtle.refuel(1)
  279. self:getFuel()
  280. end
  281. end
  282.  
  283. function obj:checkSticks()
  284. if turtle.getItemCount(self.slot.sticks) < 10 then
  285. self:getSticks()
  286. end
  287. end
  288.  
  289. function obj:isSameSpecies()
  290. seed_info = turtle.getItemDetail(self.slot.seed)
  291. return self.seed.name == seed_info.name
  292. end
  293.  
  294. function obj:work()
  295. self:checkFuel()
  296. self:getFuel()
  297. self:getSticks()
  298. self:getSeed()
  299. self:setupField()
  300. while not self:isSeedBreeded() do
  301. self:checkFuel()
  302. self:checkSticks()
  303. self:waitForBreeding()
  304. self:harvest(2)
  305. breeded_stats = self:analyse()
  306. current_stats = {self.seed.gr, self.seed.ga, self.seed.st}
  307. self:updateSeedStats(breeded_stats)
  308. if self:isSeedBreeded() then
  309. -- Store breeded seed
  310. self:move_to(self.positions.chest)
  311. turtle.select(self.slot.seed)
  312. turtle.dropDown()
  313. self:cleanInventory(self.slot.extra)
  314. self:cleanField()
  315. else
  316. if ((current_stats[1] + current_stats[2] + current_stats[3]) < (breeded_stats[1] + breeded_stats[2] + breeded_stats[3])) and self:isSameSpecies() then
  317. -- replace parent with breeded
  318. self:harvest(self.replace_seed_index)
  319. self:sow(self.replace_seed_index)
  320. self:breed()
  321. self:cleanInventory(self.slot.extra)
  322. self.replace_seed_index = self.replace_seed_index + 1
  323. if self.replace_seed_index > 1 then
  324. self.replace_seed_index = 0
  325. end
  326. else
  327. -- trash duplicate
  328. self:breed()
  329. self:cleanInventory(self.slot.seed)
  330. end
  331. end
  332. end
  333. end
  334.  
  335. setmetatable(obj, self)
  336. self.__index = self; return obj
  337. end
  338.  
  339. worker = Breeder:new()
  340.  
  341. while true do
  342. worker:work()
  343. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement