DrFair

Beekeeper

Mar 9th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. function startup()
  2. dir = { ["canalyzer"]=1, ["apiary"]=2, ["analyzer"]=3 }
  3. nDir = 2
  4. inv = {}
  5. rednet.open("right")
  6. status = "active"
  7. species1 = {}
  8. species2 = {}
  9. types = {}
  10. target = { [1]="bees.species.common", [2]="bees.species.meadows", [3]="bees.species.marbled"}
  11. invmod = peripheral.wrap("left")
  12. fail = false
  13. for i=1,16 do
  14. inv[i] = "n"
  15. end
  16. end
  17.  
  18. function turnLeft()
  19. if nDir > 1 then
  20. turtle.turnLeft()
  21. nDir = nDir - 1
  22. end
  23. end
  24.  
  25. function turnRight()
  26. if nDir < 3 then
  27. turtle.turnRight()
  28. nDir = nDir + 1
  29. end
  30. end
  31.  
  32. function turnTo(str)
  33. while nDir ~= dir[str] do
  34. if nDir < dir[str] then
  35. turnRight()
  36. else
  37. turnLeft()
  38. end
  39. end
  40. end
  41.  
  42. function extract()
  43. turnTo("apiary")
  44. out = "empty"
  45. print("Waiting for apiary.")
  46. countBees = 0
  47. while out == "empty" do
  48. turtle.suck()
  49. if turtle.getItemCount(1) > 0 then
  50. for i=1,4 do
  51. turtle.suck()
  52. end
  53. if turtle.getItemCount(2) > 0 then
  54. out = "b"
  55. print("Found bees in apiary.")
  56. else
  57. out = "c"
  58. print("Found comb in apiary.")
  59. end
  60. end
  61. end
  62. end
  63.  
  64. function dumbComb()
  65. for i=1,16 do
  66. if inv[i] == "c" then
  67. turtle.select(i)
  68. turtle.dropDown()
  69. inv[i] = "n"
  70. end
  71. end
  72. turtle.select(1)
  73. end
  74.  
  75. function sortComb()
  76. turnTo("canalyzer")
  77. print("Sorting out the comb.")
  78. p = peripheral.wrap("front")
  79. for i=1,16 do
  80. turtle.select(i)
  81. if turtle.getItemCount(i) > 0 then
  82. turtle.drop()
  83. if p.isBee() then
  84. inv[i] = "b"
  85. else
  86. inv[i] = "c"
  87. end
  88. turtle.suck()
  89. end
  90. end
  91. dumbComb()
  92. turtle.select(1)
  93. end
  94.  
  95. function countItems()
  96. numbees = 0
  97. for i=1,16 do
  98. if turtle.getItemCount(i) > 0 then
  99. numbees = numbees + 1
  100. end
  101. end
  102. end
  103.  
  104. function countStacks()
  105. stacks = {}
  106. for i=1,16 do
  107. stacks[i] = turtle.getItemCount(i)
  108. end
  109. end
  110.  
  111. function findBestDrone()
  112. bestScore = 0
  113. bestDrone = 0
  114. countItems()
  115. for i=1,numbees do
  116. score = 0
  117. if types[i] == "drone" then
  118. if species1[i] == target[1] then
  119. score = score + 2
  120. elseif species1[i] == target[2] or species1[i] == target[3] then
  121. score = score + 1
  122. end
  123. if species2[i] == target[1] then
  124. score = score + 2
  125. elseif species2[i] == target[2] or species2[i] == target[3] then
  126. score = score + 1
  127. end
  128. if score > bestScore then
  129. bestSpecies = string.sub(species1[i],14,#species1[i]).." - "..string.sub(species2[i],14,#species2[i])
  130. bestScore = score
  131. bestDrone = i
  132. end
  133. end
  134. end
  135. print("Found best drone in slot "..bestDrone..":")
  136. print(bestSpecies)
  137. end
  138.  
  139. function checkPrincess()
  140. countItems()
  141. for i=1,numbees do
  142. if types[i] == "princess" then
  143. if species1[i] == target[1] and species2[i] == target[1] then
  144. return true
  145. else
  146. return false
  147. end
  148. end
  149. end
  150. fail = true
  151. end
  152.  
  153. function analyzeBees()
  154. turnTo("analyzer")
  155. print("Analyzing bees.")
  156. countItems()
  157. countStacks()
  158. countBees = 0
  159. for i=1,16 do
  160. if inv[i] == "b" then
  161. turtle.select(i)
  162. if turtle.drop() then countBees = countBees + 1 end
  163. end
  164. end
  165. turtle.select(1)
  166. for i=1,16 do
  167. if inv[i] == "b" then
  168. os.sleep(26)
  169. turtle.select(i)
  170. invmod.suckSneaky(0,stacks[i])
  171. end
  172. end
  173. turnTo("canalyzer")
  174. p = peripheral.wrap("front")
  175. countItems()
  176. species1 = {}
  177. species2 = {}
  178. types = {}
  179. for i=1,numbees do
  180. turtle.select(i)
  181. turtle.drop()
  182. data = p.analyze()
  183. species1[i] = data["speciesPrimary"]
  184. species2[i] = data["speciesSecondary"]
  185. types[i] = data["type"]
  186. turtle.suck()
  187. end
  188. end
  189.  
  190. function dropExtra()
  191. print("Dropping extra drones.")
  192. for i=1,16 do
  193. turtle.select(i)
  194. turtle.dropDown()
  195. inv[i] = "n"
  196. end
  197. turtle.select(1)
  198. end
  199.  
  200. function dropBees()
  201. turnTo("apiary")
  202. print("Dropping off best couple.")
  203. countItems()
  204. for i=1,numbees do
  205. if types[i] == "princess" then
  206. turtle.select(i)
  207. invmod.dropSneaky(1,1)
  208. end
  209. if bestDrone == i then
  210. turtle.select(i)
  211. invmod.dropSneaky(0,1)
  212. end
  213. end
  214. turtle.select(1)
  215. dropExtra()
  216. end
  217.  
  218. startup()
  219.  
  220. while true do
  221. event,pr1,pr2,pr3 = os.pullEvent()
  222. if status == "active" then
  223. extract()
  224. if out == "b" then
  225. sortComb()
  226. analyzeBees()
  227. findBestDrone()
  228. if checkPrincess() and bestScore == 4 then
  229. print("Job finished.")
  230. turnTo("apiary")
  231. break
  232. else
  233. if fail then
  234. print("Something went wrong!")
  235. break
  236. end
  237. dropBees()
  238. end
  239. elseif out == "c" then
  240. sortComb()
  241. end
  242. else
  243. os.sleep(5)
  244. end
  245. end
Advertisement
Add Comment
Please, Sign In to add comment