Guest User

Untitled

a guest
Aug 26th, 2013
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.40 KB | None | 0 0
  1. -- BeeAnalyzer 4.3
  2. -- Original code by Direwolf20
  3. -- Hotfix 1 by Mandydax
  4. -- Hotfix 2 by Mikeyhun/MaaadMike
  5. -- 4.0 Major overhaul by MacLeopold
  6. -- Breeds bees with best attributes in this order:
  7. -- fertility, speed, nocturnal, flyer, cave, temperature tolerance, humidity tolerance
  8. -- other attributes are ignored (lifespawn, flowers, effect and territory)
  9. -- Can specify multiple target species to help with keeping to the correct line
  10. -- 4.1 Minor fix for FTB Unleashed, no more inventory module or suckSneaky needed
  11. -- 4.2 Major overhaul 2
  12. -- Added magic bees, removed old bees
  13. -- Added species graph
  14. -- Changed targeting to target parent species
  15. -- Changed breeding to keep stock of good bees to prevent losing attributes
  16. -- Added logging
  17. -- Changed scoring to look for best combination of princess and drone
  18. -- 4.3 Updated targeting
  19.  
  20. -- attribute scoring for same species tie-breaking -----------------------------
  21.  
  22. scoresFertility = {
  23. [1] = 0.1,
  24. [2] = 0.2,
  25. [3] = 0.3,
  26. [4] = 0.4
  27. }
  28. scoresSpeed = {
  29. ["0.3"] = 0.01,
  30. ["0.6"] = 0.02,
  31. ["0.8"] = 0.03,
  32. ["1"] = 0.04,
  33. ["1.2"] = 0.05,
  34. ["1.4"] = 0.06,
  35. ["1.7"] = 0.07
  36. }
  37. scoresAttrib = {
  38. diurnal =0.004,
  39. nocturnal =0.002,
  40. tolerantFlyer=0.001,
  41. caveDwelling =0.0001
  42. }
  43. scoresTolerance = {
  44. ["NONE"] = 0.00000,
  45. ["UP_1"] = 0.00001,
  46. ["UP_2"] = 0.00002,
  47. ["UP_3"] = 0.00003,
  48. ["DOWN_1"] = 0.00001,
  49. ["DOWN_2"] = 0.00002,
  50. ["DOWN_3"] = 0.00003,
  51. ["BOTH_1"] = 0.00002,
  52. ["BOTH_2"] = 0.00004,
  53. ["BOTH_3"] = 0.00006
  54. }
  55.  
  56. -- the bee graph ---------------------------------------------------------------
  57.  
  58. bees = {}
  59.  
  60. function addParent(parent, offspring)
  61. if bees[parent] then
  62. bees[parent].mutateTo[offspring] = true
  63. else
  64. bees[parent] = {
  65. --name = parent,
  66. score = nil,
  67. mutateTo = {[offspring]=true},
  68. mutateFrom = {}
  69. }
  70. end
  71. end
  72.  
  73. function addOffspring(offspring, parentss)
  74. if bees[offspring] then
  75. for i, parents in ipairs(parentss) do
  76. table.insert(bees[offspring].mutateFrom, parents)
  77. end
  78. else
  79. bees[offspring] = {
  80. score = nil,
  81. mutateTo = {},
  82. mutateFrom = parentss
  83. }
  84. end
  85. for i, parents in ipairs(parentss) do
  86. for i, parent in ipairs(parents) do
  87. addParent(parent, offspring)
  88. end
  89. end
  90. end
  91.  
  92. -- score bees that have no parent combinations as 1
  93. -- iteratively find the next bee up the line and increase the score
  94. function scoreBees()
  95. -- find all bees with no mutateFrom data
  96. local beeCount = 0
  97. local beeScore = 1
  98. for name, beeData in pairs(bees) do
  99. if #beeData.mutateFrom == 0 then
  100. beeData.score = beeScore
  101. else
  102. beeCount = beeCount + 1
  103. end
  104. end
  105. while beeCount > 0 do
  106. beeScore = beeScore * 4
  107. -- find all bees where all parent combos are scored
  108. for name, beeData in pairs(bees) do
  109. if not beeData.score then
  110. local scoreBee = true
  111. for i, beeParents in ipairs(beeData.mutateFrom) do
  112. local parent1 = bees[beeParents[1]]
  113. local parent2 = bees[beeParents[2]]
  114.  
  115. if not parent1.score
  116. or parent1.score == beeScore
  117. or not parent2.score
  118. or parent2.score == beeScore then
  119. scoreBee = false
  120. break
  121. end
  122. end
  123. if scoreBee then
  124. beeData.score = beeScore
  125. beeCount = beeCount - 1
  126. end
  127. end
  128. end
  129. end
  130. end
  131.  
  132. -- produce combinations from 1 or 2 lists
  133. function choose(list, list2)
  134. local newList = {}
  135. if list2 then
  136. for i = 1, #list2 do
  137. for j = 1, #list do
  138. if list[j] ~= list[i] then
  139. table.insert(newList, {list[j], list2[i]})
  140. end
  141. end
  142. end
  143. else
  144. for i = 1, #list do
  145. for j = i, #list do
  146. if list[i] ~= list[j] then
  147. table.insert(newList, {list[i], list[j]})
  148. end
  149. end
  150. end
  151. end
  152. return newList
  153. end
  154.  
  155. -- Forestry Bees ---------------------------------------------------------------
  156.  
  157. -- Agrarian Branch
  158. addOffspring("Rural", {{"Diligent", "Meadows"}})
  159. addOffspring("Farmed", {{"Rural", "Cultivated"}})
  160. -- Apis Branch
  161. addOffspring("Common", choose({"Forest", "Meadows", "Modest", "Marbled", "Tropical", "Wintry", "Marshy", "Water", "Rocky", "Embittered", "Unusual", "Mystical", "Sorcerous", "Attuned"}))
  162. addOffspring("Cultivated", choose({"Common"}, {"Forest", "Meadows", "Modest", "Marbled", "Tropical", "Wintry", "Marshy", "Water", "Rocky", "Embittered", "Unusual", "Mystical", "Sorcerous", "Attuned"}))
  163. -- Austere Branch
  164. addOffspring("Frugal", {{"Sinister", "Modest"}, {"Fiendish", "Modest"}})
  165. addOffspring("Austere", {{"Frugal", "Modest"}})
  166. -- Beastly Branch
  167. addOffspring("Jaded", {{"Ender", "Relic"}})
  168. -- End Branch
  169. addOffspring("Spectral", {{"Hermitic", "Ender"}})
  170. addOffspring("Phantasmal", {{"Spectral", "Ender"}})
  171. -- Festive Branch
  172. addOffspring("Celebratory", {{"Austere", "Excited"}})
  173. addOffspring("Hazardous", {{"Austere", "Desolate"}})
  174. addOffspring("Leporine", {{"Meadows", "Forest"}})
  175. addOffspring("Merry", {{"Wintry", "Forest"}})
  176. addOffspring("Tipsy", {{"Wintry", "Meadows"}})
  177. -- Frozen Branch
  178. addOffspring("Icy", {{"Industrious", "Wintry"}})
  179. addOffspring("Glacial", {{"Icy", "Wintry"}})
  180. addOffspring("Frigid", {{"Diligent", "Wintry"}})
  181. addOffspring("Absolute", {{"Frigid", "Ocean"}})
  182. -- Heroic Branch
  183. addOffspring("Heroic", {{"Steadfast", "Valiant"}})
  184. -- Industrious Branch
  185. addOffspring("Diligent", {{"Cultivated", "Common"}})
  186. addOffspring("Unweary", {{"Diligent", "Cultivated"}})
  187. addOffspring("Industrious", {{"Unweary", "Diligent"}})
  188. -- Infernal Branch
  189. addOffspring("Sinister", {{"Cultivated", "Modest"}, {"Cultivated", "Tropical"}})
  190. addOffspring("Fiendish", {{"Sinister", "Cultivated"}, {"Sinister", "Modest"}, {"Sinister", "Tropical"}})
  191. addOffspring("Demonic", {{"Fiendish", "Sinister"}})
  192. -- Monastic Branch
  193. addOffspring("Secluded", {{"Monastic", "Austere"}})
  194. addOffspring("Hermitic", {{"Secluded", "Monastic"}})
  195. -- Noble Branch
  196. addOffspring("Noble", {{"Cultivated", "Common"}})
  197. addOffspring("Majestic", {{"Noble", "Cultivated"}})
  198. addOffspring("Imperial", {{"Majestic", "Noble"}})
  199. -- Tropical Branch
  200. addOffspring("Exotic", {{"Austere", "Tropical"}})
  201. addOffspring("Edenic", {{"Exotic", "Tropical"}})
  202. -- Vengeful Branch
  203. addOffspring("Vindictive", {{"Monastic", "Demonic"}})
  204. addOffspring("Vengeful", {{"Vindictive", "Demonic"}, {"Vindictive", "Monastic"}})
  205. addOffspring("Avenging", {{"Vengeful", "Vindictive"}})
  206.  
  207. -- Extra Bees ------------------------------------------------------------------
  208.  
  209. -- Agricultural Branch
  210. addOffspring("Bovine", {{"Rural", "Water"}})
  211. addOffspring("Caffeinated", {{"Rural", "Tropical"}})
  212. addOffspring("Citrus", {{"Farmed", "Modest"}})
  213. addOffspring("Fermented", {{"Rural", "Fruity"}})
  214. addOffspring("Minty", {{"Farmed", "Tropical"}})
  215. -- Alloyed Branch
  216. addOffspring("Impregnable", {{"Resilient", "Noble"}})
  217. -- Aquatic Branch
  218. addOffspring("River", {{"Common", "Water"}})
  219. addOffspring("Ocean", {{"Diligent", "Water"}})
  220. addOffspring("Stained", {{"Ocean", "Ebony"}})
  221. -- Barren Branch
  222. addOffspring("Arid", {{"Meadows", "Modest"}})
  223. addOffspring("Barren", {{"Arid", "Common"}})
  224. addOffspring("Desolate", {{"Barren", "Arid"}})
  225. -- Boggy Branch
  226. addOffspring("Damp", {{"Common", "Marshy"}})
  227. addOffspring("Boggy", {{"Damp", "Marshy"}})
  228. addOffspring("Fungal", {{"Boggy", "Damp"}})
  229. -- Caustic Branch
  230. addOffspring("Corrosive", {{"Virulent", "Sticky"}})
  231. addOffspring("Caustic", {{"Corrosive", "Fiendish"}})
  232. addOffspring("Acidic", {{"Caustic", "Corrosive"}})
  233. -- Energetic Branch
  234. addOffspring("Excited", {{"Cultivated", "Valiant"}})
  235. addOffspring("Energetic", {{"Excited", "Valiant"}})
  236. addOffspring("Ecstatic", {{"Energetic", "Excited"}})
  237. -- Fossilized
  238. addOffspring("Fossiled", {{"Primeval", "Growing"}})
  239. addOffspring("Oily", {{"Primeval", "Ocean"}})
  240. addOffspring("Preserved", {{"Primeval", "Boggy"}})
  241. addOffspring("Resinous", {{"Primeval", "Fungal"}})
  242. -- Gemstone Branch
  243. addOffspring("Diamond", {{"Lapis", "Imperial"}})
  244. addOffspring("Emerald", {{"Lapis", "Noble"}})
  245. addOffspring("Ruby", {{"Emerald", "Austere"}})
  246. addOffspring("Sapphire", {{"Emerald", "Ocean"}})
  247. -- Historic Branch
  248. addOffspring("Ancient", {{"Noble", "Diligent"}})
  249. addOffspring("Primeval", {{"Ancient", "Noble"}})
  250. addOffspring("Prehistoric", {{"Primeval", "Majestic"}})
  251. addOffspring("Relic", {{"Prehistoric", "Imperial"}})
  252. -- Hostile Branch
  253. addOffspring("Skeletal", {{"Desolate", "Frugal"}})
  254. addOffspring("Decaying", {{"Desolate", "Modest"}})
  255. addOffspring("Creepy", {{"Desolate", "Austere"}})
  256. -- Metallic Branch
  257. addOffspring("Galvanized", {{"Tarnished", "Cultivated"}})
  258. addOffspring("Invincible", {{"Resilient", "Ender"}})
  259. addOffspring("Lustered", {{"Resilient", "Unweary"}})
  260. -- Nuclear Branch
  261. addOffspring("Unstable", {{"Austere", "Rocky"}})
  262. addOffspring("Nuclear", {{"Unstable", "Rusty"}})
  263. addOffspring("Radioactive", {{"Nuclear", "Glittering"}})
  264. -- Precious Branch
  265. addOffspring("Lapis", {{"Resilient", "Water"}})
  266. addOffspring("Glittering", {{"Corroded", "Imperial"}})
  267. addOffspring("Shining", {{"Rusty", "Imperial"}})
  268. addOffspring("Valuable", {{"Glittering", "Shining"}})
  269. -- Refined Branch
  270. addOffspring("Distilled", {{"Oily", "Industrious"}})
  271. addOffspring("Refined", {{"Distilled", "Oily"}})
  272. addOffspring("Elastic", {{"Refined", "Resinous"}})
  273. addOffspring("Tarry", {{"Refined", "Fossiled"}})
  274. -- Rocky Branch
  275. addOffspring("Tolerant", {{"Diligent", "Rocky"}})
  276. addOffspring("Robust", {{"Tolerant", "Rocky"}})
  277. addOffspring("Resilient", {{"Robust", "Imperial"}})
  278. -- Rusty Branch
  279. addOffspring("Corroded", {{"Resilient", "Forest"}})
  280. addOffspring("Leaden", {{"Resilient", "Unweary"}})
  281. addOffspring("Rusty", {{"Resilient", "Meadows"}})
  282. addOffspring("Tarnished", {{"Resilient", "Marshy"}})
  283. -- Saccharine Branch
  284. addOffspring("Sweetened", {{"Diligent", "Valiant"}})
  285. addOffspring("Sugary", {{"Sweetened", "Diligent"}})
  286. addOffspring("Fruity", {{"Ripening", "Rural"}})
  287. -- Shadow Branch
  288. addOffspring("Shadowed", {{"Tolerant", "Sinister"}})
  289. addOffspring("Darkened", {{"Shadowed", "Embittered"}})
  290. addOffspring("Abyssmal", {{"Darkened", "Shadowed"}})
  291. -- Virulent Branch
  292. addOffspring("Malicious", {{"Sinister", "Tropical"}})
  293. addOffspring("Infectious", {{"Malicious", "Tropical"}})
  294. addOffspring("Virulent", {{"Infectious", "Malicious"}})
  295. -- Viscous Branch
  296. addOffspring("Viscous", {{"Exotic", "Water"}})
  297. addOffspring("Glutinous", {{"Viscous", "Exotic"}})
  298. addOffspring("Sticky", {{"Glutinous", "Viscous"}})
  299. -- Volcanic Branch
  300. addOffspring("Furious", {{"Sinister", "Embittered"}})
  301. addOffspring("Volcanic", {{"Furious", "Embittered"}})
  302. addOffspring("Glowering", {{"Excited", "Furious"}})
  303.  
  304. -- Primary Branch
  305. addOffspring("Bleached", {{"Wintry", "Valiant"}})
  306. addOffspring("Ebony", {{"Rocky", "Valiant"}})
  307. addOffspring("Maroon", {{"Forest", "Valiant"}})
  308. addOffspring("Natural", {{"Tropical", "Valiant"}})
  309. addOffspring("Prussian", {{"Water", "Valiant"}})
  310. addOffspring("Saffron", {{"Meadows", "Valiant"}})
  311. addOffspring("Sepia", {{"Marshy", "Valiant"}})
  312. -- Secondary Branch
  313. addOffspring("Amber", {{"Maroon", "Saffron"}})
  314. addOffspring("Azure", {{"Prussian", "Bleached"}})
  315. addOffspring("Indigo", {{"Maroon", "Prussian"}})
  316. addOffspring("Lavender", {{"Maroon", "Bleached"}})
  317. addOffspring("Lime", {{"Natural", "Bleached"}})
  318. addOffspring("Slate", {{"Ebony", "Bleached"}})
  319. addOffspring("Turquoise", {{"Natural", "Prussian"}})
  320. -- Tertiary Branch
  321. addOffspring("Ashen", {{"Slate", "Bleached"}})
  322. addOffspring("Fuchsia", {{"Indigo", "Lavender"}})
  323.  
  324. -- no branch
  325. addOffspring("Gnawing", {{"Barren", "Forest"}})
  326. addOffspring("Decoposing", {{"Arid", "Common"}, {"Gnawing", "Common"}})
  327. addOffspring("Growing", {{"Diligent", "Forest"}})
  328. addOffspring("Thriving", {{"Growing", "Rural"}})
  329. addOffspring("Blooming", {{"Growing", "Thriving"}})
  330. addOffspring("Ripening", {{"Sugary", "Forest"}})
  331.  
  332. -- Magic Bees ------------------------------------------------------------------
  333.  
  334. -- Abominable Branch
  335. addOffspring("Hateful", {{"Eldritch", "Infernal"}})
  336. addOffspring("Spiteful", {{"Hateful", "Infernal"}})
  337. addOffspring("Withering", {{"Demonic", "Spiteful"}})
  338. -- Alchemical Branch
  339. addOffspring("Minium", {{"Eldritch", "Frugal"}})
  340. -- Arcane Branch
  341. addOffspring("Esoteric", {{"Eldritch", "Cultivated"}})
  342. addOffspring("Mysterious", {{"Eldritch", "Esoteric"}})
  343. addOffspring("Arcane", {{"Mysterious", "Esoteric"}})
  344. -- Aware Branch
  345. addOffspring("Ethereal", {{"Arcane", "Supernatural"}})
  346. addOffspring("Aware", {{"Ethereal", "Attuned"}})
  347. addOffspring("Watery", {{"Ethereal", "Supernatural"}})
  348. addOffspring("Windy", {{"Ethereal", "Supernatural"}})
  349. addOffspring("Firey", {{"Ethereal", "Supernatural"}})
  350. addOffspring("Earthen", {{"Ethereal", "Supernatural"}})
  351. -- Essential Branch
  352. addOffspring("Essence", {{"Arcane", "Ethereal"}})
  353. addOffspring("Arkanen", {{"Essence", "Ethereal"}})
  354. addOffspring("Quintessential", {{"Essence", "Arcane"}})
  355. addOffspring("Vortex", {{"Essence", "Skulking"}})
  356. addOffspring("Wight", {{"Ghastly", "Skulking"}})
  357. addOffspring("Luft", {{"Essence", "Windy"}})
  358. addOffspring("Blitz", {{"Luft", "Windy"}})
  359. addOffspring("Wasser", {{"Essence", "Watery"}})
  360. addOffspring("Eis", {{"Wasser", "Watery"}})
  361. addOffspring("Erde", {{"Essence", "Earthen"}})
  362. addOffspring("Staude", {{"Erde", "Earthen"}})
  363. addOffspring("Feuer", {{"Essence", "Firey"}})
  364. addOffspring("Magma", {{"Feuer", "Firey"}})
  365. -- Extrinsic
  366. addOffspring("Nameless", {{"Oblivion", "Ethereal"}})
  367. addOffspring("Abandoned", {{"Nameless", "Oblivion"}})
  368. addOffspring("Forlorn", {{"Abandoned", "Nameless"}})
  369. addOffspring("Draconic", {{"Abandoned", "Imperial"}})
  370. -- Fleshly Branch
  371. addOffspring("Poultry", {{"Skulking", "Common"}})
  372. addOffspring("Beefy", {{"Skulking", "Common"}})
  373. addOffspring("Porcine", {{"Skulking", "Common"}})
  374. -- Gem Branch
  375. addOffspring("Apatine", {{"Rural", "Cuprum"}})
  376. addOffspring("Diamandi", {{"Austere", "Auric"}})
  377. addOffspring("Esmeraldi", {{"Austere", "Argentum"}})
  378. -- Metallic2 Branch
  379. addOffspring("Cuprum", {{"Industrious", "Meadows"}})
  380. addOffspring("Stannum", {{"Industrious", "Forest"}})
  381. addOffspring("Aluminum", {{"Industrious", "Cultivated"}})
  382. addOffspring("Ardite", {{"Industrious", "Infernal"}})
  383. addOffspring("Argentum", {{"Imperial", "Modest"}})
  384. addOffspring("Cobalt", {{"Imperial", "Infernal"}})
  385. addOffspring("Ferrous", {{"Common", "Industrious"}})
  386. addOffspring("Plumbum", {{"Stannum", "Common"}})
  387. addOffspring("Auric", {{"Minium", "Plumbum"}})
  388. addOffspring("Manyullyn", {{"Ardite", "Cobalt"}})
  389. -- Scholarly Branch
  390. addOffspring("Pupil", {{"Arcane", "Monastic"}})
  391. addOffspring("Scholarly", {{"Arcane", "Pupil"}})
  392. addOffspring("Savant", {{"Scholarly", "Pupil"}})
  393. -- Skulking Branch
  394. addOffspring("Skulking", {{"Eldritch", "Modest"}})
  395. addOffspring("Ghastly", {{"Skulking", "Ethereal"}})
  396. addOffspring("Smouldering", {{"Skulking", "Hateful"}})
  397. addOffspring("Spidery", {{"Skulking", "Tropical"}})
  398. -- Soulful Branch
  399. addOffspring("Spirit", {{"Ethereal", "Aware"}, {"Attuned", "Aware"}})
  400. addOffspring("Soul", {{"Spirit", "Aware"}})
  401. -- Supernatural Branch
  402. addOffspring("Charmed", {{"Eldritch", "Cultivated"}})
  403. addOffspring("Enchanted", {{"Eldritch", "Charmed"}})
  404. addOffspring("Supernatural", {{"Enchanted", "Charmed"}})
  405. -- Thaumic Branch
  406. addOffspring("Aqua", {{"Watery", "Watery"}})
  407. addOffspring("Aura", {{"Windy", "Windy"}})
  408. addOffspring("Ignis", {{"Firey", "Firey"}})
  409. addOffspring("Praecantatio", {{"Ethereal", "Ethereal"}})
  410. addOffspring("Solum", {{"Earthen", "Earthen"}})
  411. addOffspring("Stark", choose({"Earthen", "Firey", "Watery", "Windy"}))
  412. addOffspring("Vis", {{"Eldritch", "Ethereal"}})
  413. addOffspring("Flux", {{"Vis", "Demonic"}})
  414. addOffspring("Attractive", {{"Vis", "Flux"}})
  415. addOffspring("Rejuvenating", {{"Vis", "Imperial"}})
  416. addOffspring("Pure", {{"Vis", "Rejuvenating"}})
  417. addOffspring("Batty", {{"Skulking", "Windy"}})
  418. addOffspring("Brainy", {{"Skulking", "Pupil"}})
  419. addOffspring("Wispy", {{"Ethereal", "Ghastly"}})
  420. -- Time Branch
  421. addOffspring("Timely", {{"Ethereal", "Imperial"}})
  422. addOffspring("Lordly", {{"Timely", "Imperial"}})
  423. addOffspring("Doctoral", {{"Lordly", "Timely"}})
  424. -- Veiled Branch
  425. addOffspring("Eldritch", {{"Mystical", "Cultivated"}, {"Sorcerous", "Cultivated"}, {"Unusual", "Cultivated"}, {"Attuned", "Cultivated"}})
  426.  
  427. scoreBees()
  428.  
  429. -- logging ---------------------------------------------------------------------
  430.  
  431. local logFile = fs.open("bee.log", "w")
  432. function log(msg)
  433. msg = msg or ""
  434. logFile.write(tostring(msg))
  435. logFile.flush()
  436. io.write(msg)
  437. end
  438. function logLine(msg)
  439. msg = msg or ""
  440. logFile.write(msg.."\n")
  441. logFile.flush()
  442. io.write(msg.."\n")
  443. end
  444.  
  445. -- analyzing functions ---------------------------------------------------------
  446.  
  447. -- Fix for some versions returning bees.species.*
  448. function fixName(name)
  449. return name:gsub("bees%.species%.",""):gsub("^.", string.upper)
  450. end
  451.  
  452. function clearSystem()
  453. -- orient turtle
  454. while true do
  455. local p = peripheral.wrap("front")
  456. if p and p.isMember then
  457. break
  458. end
  459. turtle.turnRight()
  460. end
  461. -- clear out analyzer
  462. turtle.turnLeft()
  463. while turtle.suck() do end
  464. -- clear out beealyzer
  465. turtle.turnRight()
  466. turtle.suck()
  467. -- clear out apiary
  468. turtle.turnRight()
  469. while turtle.suck() do end
  470. end
  471.  
  472. function getBees()
  473. -- get bees from apiary
  474. log("waiting for bees.")
  475. turtle.select(1)
  476. while not turtle.suck() do
  477. sleep(10)
  478. log(".")
  479. end
  480. log("*")
  481. while turtle.suck() do
  482. log("*")
  483. end
  484. logLine()
  485. end
  486.  
  487. function countBees()
  488. -- spread dups and fill gaps
  489. local count = 0
  490. for i = 1, 16 do
  491. local slotCount = turtle.getItemCount(i)
  492. if slotCount == 1 then
  493. for j = 1, i-1 do
  494. if turtle.getItemCount(j) == 0 then
  495. turtle.select(i)
  496. turtle.transferTo(j)
  497. break
  498. end
  499. end
  500. count = count + 1
  501. elseif slotCount > 1 then
  502. for j = 2, slotCount do
  503. turtle.select(i)
  504. for k = 1, 16 do
  505. if turtle.getItemCount(k) == 0 then
  506. turtle.transferTo(k, 1)
  507. end
  508. end
  509. end
  510. if turtle.getItemCount(i) > 1 then
  511. turtle.dropDown(turtle.getItemCount(i)-1)
  512. end
  513. end
  514. end
  515. return count
  516. end
  517.  
  518. function breedBees(princessSlot, droneSlot)
  519. turtle.select(princessSlot)
  520. turtle.drop()
  521. turtle.select(droneSlot)
  522. turtle.drop()
  523. end
  524.  
  525. function ditchProduct()
  526. print("ditching product...")
  527. turtle.turnLeft()
  528. m = peripheral.wrap("front")
  529. for i = 1, 16 do
  530. if turtle.getItemCount(i) > 0 then
  531. turtle.select(i)
  532. turtle.drop()
  533. if not m.isMember() then
  534. turtle.suck()
  535. turtle.dropDown()
  536. else
  537. turtle.suck()
  538. end
  539. end
  540. end
  541. turtle.turnRight()
  542. end
  543.  
  544. function scanBees()
  545. log("scanning bees")
  546. turtle.turnLeft()
  547. turtle.turnLeft()
  548. for i = 1, 16 do
  549. if turtle.getItemCount(i) > 0 then
  550. log(".")
  551. turtle.select(i)
  552. turtle.drop()
  553. while not turtle.suck() do
  554. sleep(1)
  555. end
  556. end
  557. end
  558. logLine()
  559. turtle.turnRight()
  560. turtle.turnRight()
  561. end
  562.  
  563. function swapBee(slot1, slot2, freeSlot)
  564. turtle.select(slot1)
  565. turtle.transferTo(freeSlot)
  566. turtle.select(slot2)
  567. turtle.transferTo(slot1)
  568. turtle.select(freeSlot)
  569. turtle.transferTo(slot2)
  570. end
  571.  
  572. function analyzeBees()
  573. logLine("analyzing bees...")
  574. local freeSlot
  575. local princessSlot
  576. local princessData
  577. local droneData = {}
  578. turtle.turnLeft()
  579. local beealyzer = peripheral.wrap("front")
  580. for i = 1, 16 do
  581. if turtle.getItemCount(i) > 0 then
  582. turtle.select(i)
  583. turtle.drop()
  584. local beeData = beealyzer.analyze()
  585. turtle.suck()
  586. if not beeData["speciesPrimary"] then
  587. print("Bee "..i.." not correctly analyzed")
  588. else
  589. beeData["speciesPrimary"] = fixName(beeData["speciesPrimary"])
  590. beeData["speciesSecondary"] = fixName(beeData["speciesSecondary"])
  591. if beeData["type"] == "princess" then
  592. princessData = beeData
  593. princessSlot = i
  594. else
  595. droneData[i] = beeData
  596. end
  597. end
  598. else
  599. freeSlot = i
  600. end
  601. end
  602. if princessData then
  603. if princessSlot ~= 1 then
  604. swapBee(1, princessSlot, freeSlot)
  605. droneData[princessSlot] = droneData[1]
  606. droneData[1] = nil
  607. princessSlot = 1
  608. end
  609. -- bubble sort drones
  610. print("sorting drones...")
  611. for i = 2, 16 do
  612. if turtle.getItemCount(i) > 0 and droneData[i] then
  613. droneData[i].score = scoreBee(princessData, droneData[i])
  614. for j = i - 1, 2, -1 do
  615. if droneData[j+1].score > droneData[j].score then
  616. swapBee(j+1, j, freeSlot)
  617. droneData[j+1], droneData[j] = droneData[j], droneData[j+1]
  618. end
  619. end
  620. end
  621. end
  622.  
  623. princessData.slot = 1
  624. for i = 16, 2, -1 do
  625. if droneData[i] then
  626. droneData[i].slot = i
  627. printBee(droneData[i])
  628. end
  629. end
  630. printBee(princessData)
  631. printHeader()
  632. end
  633. logLine()
  634. turtle.turnRight()
  635. return princessData, droneData
  636. end
  637.  
  638. function scoreBee(princessData, droneData)
  639. local droneSpecies = {droneData["speciesPrimary"], droneData["speciesSecondary"]}
  640. -- check for untargeted species
  641. if not bees[droneSpecies[1]].targeted or not bees[droneSpecies[2]].targeted then
  642. return 0
  643. end
  644. local princessSpecies = {princessData["speciesPrimary"], princessData["speciesSecondary"]}
  645. local max = math.max
  646. local score
  647. local maxScore = 0
  648. for _, combo in ipairs({{princessSpecies[1], droneSpecies[1]}
  649. ,{princessSpecies[1], droneSpecies[2]}
  650. ,{princessSpecies[2], droneSpecies[1]}
  651. ,{princessSpecies[2], droneSpecies[2]}}) do
  652.  
  653. -- find maximum score for each combo
  654. local s1
  655. if bees[combo[1]].targeted then
  656. s1 = bees[combo[1]].score
  657. else
  658. s1 = 0
  659. end
  660. local s2
  661. if bees[combo[2]].targeted then
  662. s2 = bees[combo[2]].score
  663. else
  664. s2 = 0
  665. end
  666. score = max(s1, s2)
  667.  
  668. -- pure drone boost in score
  669. if combo[1] == combo[2] then
  670. score = score + 1
  671. end
  672.  
  673. for name, beeData in pairs(bees) do
  674. if beeData.targeted then
  675. for i, parents in ipairs(beeData.mutateFrom) do
  676. if combo[1] == parents[1] and combo[2] == parents[2]
  677. or combo[2] == parents[1] and combo[1] == parents[2] then
  678. if beeData.score > score then
  679. --log(" "..name:sub(1,3).."="..tostring(beeData.score))
  680. end
  681. score = max(score, beeData.score)
  682. end
  683. end
  684. end
  685. end
  686. maxScore = maxScore + score
  687. end
  688. -- add one for each combination that results in the maximum score
  689. score = maxScore
  690. -- score attributes
  691. score = score + max(scoresFertility[droneData["fertility"]], scoresFertility[princessData["fertility"]])
  692. score = score + math.min(scoresSpeed[tostring(droneData["speed"])], scoresSpeed[tostring(princessData["speed"])])
  693. if droneData["diurnal"] or princessData["diurnal"] then score = score + scoresAttrib["diurnal"] end
  694. if droneData["nocturnal"] or princessData["nocturnal"] then score = score + scoresAttrib["nocturnal"] end
  695. if droneData["tolerantFlyer"] or princessData["tolerantFlyer"] then score = score + scoresAttrib["tolerantFlyer"] end
  696. if droneData["caveDwelling"] or princessData["caveDwelling"] then score = score + scoresAttrib["caveDwelling"] end
  697. score = score + max(scoresTolerance[droneData["toleranceTemperature"]], scoresTolerance[princessData["toleranceTemperature"]])
  698. score = score + max(scoresTolerance[droneData["toleranceHumidity"]], scoresTolerance[princessData["toleranceHumidity"]])
  699. return score
  700. end
  701.  
  702. function printHeader()
  703. logLine()
  704. logLine("-|-|-------|-|---|-|-|-|-|---|---|-----")
  705. logLine("typ species f spd d n f c tmp hmd score")
  706. end
  707.  
  708. toleranceString = {
  709. ["NONE"] = " ",
  710. ["UP_1"] = " +1 ",
  711. ["UP_2"] = " +2 ",
  712. ["UP_3"] = " +3 ",
  713. ["DOWN_1"] = " -1 ",
  714. ["DOWN_2"] = " -2 ",
  715. ["DOWN_3"] = " -3 ",
  716. ["BOTH_1"] = "+-1 ",
  717. ["BOTH_2"] = "+-2 ",
  718. ["BOTH_3"] = "+-3 "
  719. }
  720. function printBee(beeData)
  721. log(beeData["slot"] < 10 and beeData["slot"].." " or beeData["slot"])
  722. if (beeData["type"] == "princess") then
  723. log("P ")
  724. else
  725. log("d ")
  726. end
  727. log(beeData["speciesPrimary"]:gsub("bees%.species%.",""):sub(1,3)..":"..beeData["speciesSecondary"]:gsub("bees%.species%.",""):sub(1,3).." ")
  728. log(tostring(beeData["fertility"]).." ")
  729. log(beeData["speed"] == 1 and "1.0 " or tostring(beeData["speed"]).." ")
  730. if beeData["diurnal"] then
  731. log("d ")
  732. else
  733. log(" ")
  734. end
  735. if beeData["nocturnal"] then
  736. log("n ")
  737. else
  738. log(" ")
  739. end
  740. if beeData["tolerantFlyer"] then
  741. log("f ")
  742. else
  743. log(" ")
  744. end
  745. if beeData["caveDwelling"] then
  746. log("c ")
  747. else
  748. log(" ")
  749. end
  750. log(toleranceString[beeData["toleranceTemperature"]])
  751. log(toleranceString[beeData["toleranceHumidity"]])
  752. if beeData.score then
  753. logLine(string.format("%5.1d", beeData.score).." ")
  754. else
  755. logLine()
  756. end
  757. end
  758.  
  759. function dropExcess(droneData)
  760. print("dropping excess...")
  761. local count = 0
  762. for i = 1, 16 do
  763. if turtle.getItemCount(i) > 0 then
  764. -- check for untargeted species
  765. if droneData[i] and (not bees[droneData[i]["speciesPrimary"]].targeted
  766. or not bees[droneData[i]["speciesSecondary"]].targeted) then
  767. turtle.select(i)
  768. turtle.dropDown()
  769. else
  770. count = count + 1
  771. end
  772. -- drop drones over 9 to clear space for newly bred bees and product
  773. if count > 9 then
  774. turtle.select(i)
  775. turtle.dropDown()
  776. count = count - 1
  777. end
  778. end
  779. end
  780. end
  781.  
  782. function isPurebred(princessData, droneData)
  783. -- check if princess and drone are exactly the same and no chance for mutation
  784. if princessData["speciesPrimary"] ~= princessData["speciesSecondary"] then
  785. return false
  786. end
  787. for key, value in pairs(princessData) do
  788. logLine(key .. " : " .. tostring(value) .. " : " .. tostring(droneData[key]))
  789. if value ~= droneData[key] and key ~= "territory" and key ~= "type" and key ~= "slot" then
  790. return false
  791. end
  792. end
  793. return true
  794. end
  795.  
  796. function getUnknown(princessData, droneData)
  797. -- lists species that are not in the bee graph
  798. local unknownSpecies = {}
  799. if not bees[princessData["speciesPrimary"]] then
  800. table.insert(unknownSpecies, princessData["speciesPrimary"])
  801. end
  802. if not bees[princessData["speciesSecondary"]] then
  803. table.insert(unknownSpecies, princessData["speciesSecondary"])
  804. end
  805. for _, beeData in pairs(droneData) do
  806. if not bees[beeData["speciesPrimary"]] then
  807. table.insert(unknownSpecies, beeData["speciesPrimary"])
  808. end
  809. if not bees[beeData["speciesSecondary"]] then
  810. table.insert(unknownSpecies, beeData["speciesSecondary"])
  811. end
  812. end
  813. return unknownSpecies
  814. end
  815.  
  816. -- targeting -------------------------------------------------------------------
  817.  
  818. -- set species and all parents to targeted
  819. function targetBee(name)
  820. local bee = bees[name]
  821. if bee and not bee.targeted then
  822. bee.targeted = true
  823. for i, parents in ipairs(bee.mutateFrom) do
  824. for j, parent in ipairs(parents) do
  825. targetBee(parent)
  826. end
  827. end
  828. end
  829. end
  830.  
  831. -- set bee graph entry to targeted if species was specified on the command line
  832. -- otherwise set all entries to targeted
  833. tArgs = { ... }
  834. if #tArgs > 0 then
  835. logLine("targeting bee species:")
  836. for i, target in ipairs(tArgs) do
  837. targetBee(target)
  838. for name, data in pairs(bees) do
  839. if data.targeted and data.score > 1 then
  840. logLine(name .. string.rep(" ", 20-#name), data.score)
  841. end
  842. end
  843. end
  844. else
  845. for _, beeData in pairs(bees) do
  846. beeData.targeted = true
  847. end
  848. end
  849.  
  850. -- breeding loop ---------------------------------------------------------------
  851.  
  852. logLine("Clearing system...")
  853. clearSystem()
  854. while true do
  855. ditchProduct()
  856. countBees()
  857. scanBees()
  858. princessData, droneData = analyzeBees()
  859. if princessData then
  860. if isPurebred(princessData, droneData[2]) then
  861. logLine("Bees are purebred")
  862. turtle.turnRight()
  863. break
  864. end
  865. local unknownSpecies = getUnknown(princessData, droneData)
  866. if #unknownSpecies > 0 then
  867. logLine("Please add new species to bee graph:")
  868. for _, species in ipairs(unknownSpecies) do
  869. logLine(" "..species)
  870. end
  871. turtle.turnRight()
  872. break
  873. end
  874. breedBees(1, 2)
  875. dropExcess(droneData)
  876. end
  877. getBees()
  878. end
  879. logFile.close()
Advertisement
Add Comment
Please, Sign In to add comment