funkd0ct0r

Untitled

Sep 29th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.68 KB | None | 0 0
  1.  
  2.  
  3. -- BeeAnalyzer x
  4. -- maybe i replace the output with simple breeding bee-bee... though it has score and is sorted now
  5.  
  6.  
  7. invmod = peripheral.wrap("right")
  8. targetedSpecies = ""
  9.  
  10. -- the bee graph ---------------------------------------------------------------
  11.  
  12. bees = {}
  13.  
  14. function addParent(parent, offspring)
  15. if not bees[parent] then
  16. bees[parent] = {
  17. --name = parent,
  18. score = nil,
  19. mutateFrom = {}
  20. }
  21. end
  22. end
  23.  
  24. function addOffspring(offspring, parentss)
  25. if bees[offspring] then
  26. for i, parents in ipairs(parentss) do
  27. table.insert(bees[offspring].mutateFrom, parents)
  28. end
  29. else
  30. bees[offspring] = {
  31. score = nil,
  32. mutateFrom = parentss
  33. }
  34. end
  35. for i, parents in ipairs(parentss) do
  36. for i, parent in ipairs(parents) do
  37. addParent(parent, offspring)
  38. end
  39. end
  40. end
  41.  
  42.  
  43.  
  44.  
  45. -- produce combinations from 1 or 2 lists
  46. function choose(list, list2)
  47. local newList = {}
  48. if list2 then
  49. for i = 1, #list2 do
  50. for j = 1, #list do
  51. if list[j] ~= list[i] then
  52. table.insert(newList, {list[j], list2[i]})
  53. end
  54. end
  55. end
  56. else
  57. for i = 1, #list do
  58. for j = i, #list do
  59. if list[i] ~= list[j] then
  60. table.insert(newList, {list[i], list[j]})
  61. end
  62. end
  63. end
  64. end
  65. return newList
  66. end
  67.  
  68. -- Forestry Bees ---------------------------------------------------------------
  69.  
  70. -- Apis Branch
  71. addOffspring("Common", choose({"Forest", "Meadows", "Modest", "Marbled", "Tropical", "Wintry", "Marshy", "Water", "Rocky", "Embittered", "Unusual", "Mystical", "Sorcerous", "Attuned"}))
  72. addOffspring("Cultivated", choose({"Common"}, {"Forest", "Meadows", "Modest", "Marbled", "Tropical", "Wintry", "Marshy", "Water", "Rocky", "Embittered", "Unusual", "Mystical", "Sorcerous", "Attuned"}))
  73. -- Noble Branch
  74. addOffspring("Noble", {{"Cultivated", "Common"}})
  75. addOffspring("Majestic", {{"Noble", "Cultivated"}})
  76. addOffspring("Imperial", {{"Majestic", "Noble"}})
  77. -- Industrious Branch
  78. addOffspring("Diligent", {{"Cultivated", "Common"}})
  79. addOffspring("Unweary", {{"Diligent", "Cultivated"}})
  80. addOffspring("Industrious", {{"Unweary", "Diligent"}})
  81. -- Heroic Branch
  82. addOffspring("Heroic", {{"Steadfast", "Valiant"}})
  83. -- Infernal Branch
  84. addOffspring("Sinister", {{"Cultivated", "Modest"}, {"Cultivated", "Tropical"}})
  85. addOffspring("Fiendish", {{"Sinister", "Cultivated"}, {"Sinister", "Modest"}, {"Sinister", "Tropical"}})
  86. addOffspring("Demonic", {{"Fiendish", "Sinister"}})
  87. -- Austere Branch
  88. addOffspring("Frugal", {{"Sinister", "Modest"}, {"Fiendish", "Modest"}})
  89. addOffspring("Austere", {{"Frugal", "Modest"}})
  90.  
  91. -- Tropical Branch
  92. addOffspring("Exotic", {{"Austere", "Tropical"}})
  93. addOffspring("Edenic", {{"Exotic", "Tropical"}})
  94.  
  95. -- Frozen Branch
  96. addOffspring("Icy", {{"Industrious", "Wintry"}})
  97. addOffspring("Glacial", {{"Icy", "Wintry"}})
  98. addOffspring("Frigid", {{"Diligent", "Wintry"}})
  99. addOffspring("Absolute", {{"Frigid", "Ocean"}})
  100. -- Festive Branch
  101. addOffspring("Celebratory", {{"Austere", "Excited"}})
  102. addOffspring("Leporine", {{"Meadows", "Forest"}})
  103. addOffspring("Merry", {{"Wintry", "Forest"}})
  104. addOffspring("Tipsy", {{"Wintry", "Meadows"}})
  105.  
  106. -- Agrarian Branch
  107. addOffspring("Rural", {{"Diligent", "Meadows"}})
  108. addOffspring("Farmed", {{"Rural", "Cultivated"}})
  109.  
  110. -- Boggy Branch
  111. addOffspring("Swamp", {{"Common", "Marshy"}})
  112. addOffspring("Boggy", {{"Swamp", "Marshy"}})
  113. addOffspring("Fungal", {{"Boggy", "Swamp"}})
  114.  
  115. -- Agricultural Branch
  116. addOffspring("Fermented", {{"Rural", "Fruity"}})
  117. addOffspring("Bovine", {{"Rural", "Water"}})
  118. addOffspring("Caffeine", {{"Rural", "Tropical"}})
  119. addOffspring("Citrus", {{"Farmed", "Modest"}})
  120. addOffspring("Minty", {{"Farmed", "Tropical"}})
  121.  
  122. -- Barren Branch
  123. addOffspring("Arid", {{"Meadows", "Modest"}})
  124. addOffspring("Barren", {{"Arid", "Common"}})
  125. addOffspring("Desolate", {{"Barren", "Arid"}})
  126.  
  127.  
  128. -- Hostile Branch
  129. addOffspring("Skeletal", {{"Desolate", "Frugal"}})
  130. addOffspring("Decaying", {{"Desolate", "Modest"}})
  131. addOffspring("Creepy", {{"Desolate", "Austere"}})
  132. -- Rocky Branch
  133. addOffspring("Tolerant", {{"Diligent", "Rocky"}})
  134. addOffspring("Hardy", {{"Tolerant", "Rocky"}})
  135. addOffspring("Resilient", {{"Hardy", "Tolerant"}})
  136.  
  137. -- Rusty Branch
  138. addOffspring("Rusty", {{"Resilient", "Diligent"}})
  139. addOffspring("Corroded", {{"Resilient", "Diligent"}})
  140. addOffspring("Tarnished", {{"Resilient", "Diligent"}})
  141. addOffspring("Leaden", {{"Resilient", "Unweary"}})
  142.  
  143.  
  144. -- Metallic Branch
  145. addOffspring("Lustered", {{"Resilient", "Unweary"}})
  146. addOffspring("Galvanized", {{"Tarnished", "Cultivated"}})
  147. addOffspring("Invincible", {{"Resilient", "Ender"}})
  148.  
  149. -- Alloyed Branch
  150. addOffspring("Impregnable", {{"Resilient", "Noble"}})
  151. addOffspring("Resolute", {{"Corroded", "Tarnished"}})
  152. addOffspring("Brazen", {{"Corroded", "Galvanized"}})
  153. addOffspring("Fortified", {{"Rusty", "Fossiled"}})
  154. addOffspring("Lustrous", {{"Resilient", "Imperial"}})
  155.  
  156. -- Precious Branch
  157. addOffspring("Shining", {{"Rusty", "Noble"}})
  158. addOffspring("Glittering", {{"Resolute", "Noble"}})
  159. addOffspring("Precious", {{"Glittering", "Shining"}})
  160. addOffspring("Valuable", {{"Glittering", "Ender"}})
  161.  
  162. -- Mineral Branch
  163. addOffspring("Lapis", {{"Resilient", "Water"}})
  164. addOffspring("Sodalite", {{"Lapis", "Diligent"}})
  165. addOffspring("Pyrite", {{"Rusty", "Sinister"}})
  166. addOffspring("Bauxite", {{"Resilient", "Diligent"}})
  167. addOffspring("Cinnabar", {{"Resilient", "Sinister"}})
  168. addOffspring("Sphalerite", {{"Tarnished", "Sinister"}})
  169.  
  170. -- Gemstone Branch
  171. addOffspring("Emerald", {{"Lapis", "Noble"}})
  172. addOffspring("Ruby", {{"Emerald", "Austere"}})
  173. addOffspring("Sapphire", {{"Emerald", "Ocean"}})
  174. addOffspring("Olivine", {{"Emerald", "Ender"}})
  175. addOffspring("Diamond", {{"Sapphire", "Imperial"}})
  176.  
  177. -- Nuclear Branch
  178. addOffspring("Unstable", {{"Austere", "Rocky"}})
  179. addOffspring("Nuclear", {{"Unstable", "Rusty"}})
  180. addOffspring("Radioactive", {{"Nuclear", "Glittering"}})
  181. -- Historic Branch
  182. addOffspring("Ancient", {{"Noble", "Diligent"}})
  183. addOffspring("Primeval", {{"Ancient", "Noble"}})
  184. addOffspring("Prehistoric", {{"Primeval", "Majestic"}})
  185. addOffspring("Relic", {{"Prehistoric", "Imperial"}})
  186.  
  187. -- Fossilized
  188. addOffspring("Fossiled", {{"Primeval", "Forest"}})
  189. addOffspring("Oily", {{"Primeval", "Modest"}})
  190. addOffspring("Preserved", {{"Primeval", "Boggy"}})
  191. addOffspring("Resinous", {{"Primeval", "Marshy"}})
  192.  
  193. -- Refined Branch
  194. addOffspring("Distilled", {{"Oily", "Industrious"}})
  195. addOffspring("Refined", {{"Distilled", "Oily"}})
  196. addOffspring("Elastic", {{"Refined", "Resinous"}})
  197. addOffspring("Tarry", {{"Refined", "Fossiled"}})
  198.  
  199.  
  200. -- Aquatic Branch
  201. addOffspring("River", {{"Common", "Water"}})
  202. addOffspring("Ocean", {{"Common", "Water"}})
  203. addOffspring("Stained", {{"Ocean", "Water"}})
  204.  
  205.  
  206. -- Saccharine Branch
  207. addOffspring("Sweetened", {{"Diligent", "Tropical"}})
  208. addOffspring("Sugary", {{"Sweetened", "Diligent"}})
  209. addOffspring("Fruity", {{"Sugary", "Forest"}})
  210.  
  211.  
  212. -- Volcanic Branch
  213. addOffspring("Angry", {{"Sinister", "Embittered"}})
  214. addOffspring("Furious", {{"Angry", "Embittered"}})
  215. addOffspring("Volcanic", {{"Furious", "Angry"}})
  216. addOffspring("Glowering", {{"Excited", "Angry"}})
  217.  
  218.  
  219. -- Viscous Branch
  220. addOffspring("Viscous", {{"Exotic", "Water"}})
  221. addOffspring("Glutinous", {{"Viscous", "Exotic"}})
  222. addOffspring("Sticky", {{"Glutinous", "Viscous"}})
  223.  
  224. -- Virulent Branch
  225. addOffspring("Malicious", {{"Sinister", "Tropical"}})
  226. addOffspring("Infectious", {{"Malicious", "Tropical"}})
  227. addOffspring("Virulent", {{"Infectious", "Malicious"}})
  228.  
  229. -- Caustic Branch
  230. addOffspring("Corrosive", {{"Virulent", "Sticky"}})
  231. addOffspring("Caustic", {{"Corrosive", "Fiendish"}})
  232. addOffspring("Acidic", {{"Caustic", "Corrosive"}})
  233.  
  234.  
  235. -- Energetic Branch
  236. addOffspring("Excited", {{"Cultivated", "Valiant"}})
  237. addOffspring("Energetic", {{"Excited", "Valiant"}})
  238. addOffspring("Ecstatic", {{"Energetic", "Excited"}})
  239.  
  240.  
  241. -- Shadow Branch
  242. addOffspring("Shadowed", {{"Rocky", "Furious"}})
  243. addOffspring("Darkened", {{"Shadowed", "Embittered"}})
  244. addOffspring("Abyssmal", {{"Darkened", "Shadowed"}})
  245.  
  246. -- Primary Branch
  247. addOffspring("Bleached", {{"Wintry", "Valiant"}})
  248. addOffspring("Ebony", {{"Rocky", "Valiant"}})
  249. addOffspring("Maroon", {{"Forest", "Valiant"}})
  250. addOffspring("Natural", {{"Tropical", "Valiant"}})
  251. addOffspring("Prussian", {{"Water", "Valiant"}})
  252. addOffspring("Saffron", {{"Meadows", "Valiant"}})
  253. addOffspring("Sepia", {{"Marshy", "Valiant"}})
  254. -- Secondary Branch
  255. addOffspring("Amber", {{"Maroon", "Saffron"}})
  256. addOffspring("Azure", {{"Prussian", "Bleached"}})
  257. addOffspring("Indigo", {{"Maroon", "Prussian"}})
  258. addOffspring("Lavender", {{"Maroon", "Bleached"}})
  259. addOffspring("Lime", {{"Natural", "Bleached"}})
  260. addOffspring("Slate", {{"Ebony", "Bleached"}})
  261. addOffspring("Turquoise", {{"Natural", "Prussian"}})
  262. -- Tertiary Branch
  263. addOffspring("Ashen", {{"Slate", "Bleached"}})
  264. addOffspring("Fuchsia", {{"Indigo", "Lavender"}})
  265.  
  266.  
  267. -- hungry,confused,forestry,wooden branch
  268.  
  269. addOffspring("Hungry", {{"Arid", "Rural"}})
  270. addOffspring("Starved", {{"Barren", "Hungry"}})
  271. addOffspring("Ravenous", {{"Hungry", "Starved"}})
  272. addOffspring("Dazed", {{"Arid", "Edenic"}})
  273. addOffspring("Irrational", {{"Barren", "Dazed"}})
  274. addOffspring("Delirious", {{"Dazed", "Irrational"}})
  275. addOffspring("Pulped", {{"Unweary", "Forest"}})
  276. addOffspring("Spoiled", {{"Pulped", "Decaying"}})
  277. addOffspring("Decomposed", {{"Pulped", "Decaying"}})
  278. addOffspring("Wooden", {{"Diligent", "Forest"}})
  279. addOffspring("Lumbered", {{"Diligent", "Wooden"}})
  280. addOffspring("Timbered", {{"Wooden", "Lumbered"}})
  281.  
  282. -- Beastly Branch
  283. addOffspring("Jaded", {{"Ender", "Relic"}})
  284.  
  285.  
  286. -- Magic Bees ------------------------------------------------------------------
  287.  
  288. -- Arcane Branch
  289. addOffspring("Esoteric", {{"Imperial", "Demonic"}, {"Heroic", "Demonic"}})
  290. addOffspring("Mysterious", {{"Ender", "Esoteric"}})
  291. addOffspring("Arcane", {{"Mysterious", "Esoteric"}})
  292.  
  293. -- Supernatural Branch
  294. addOffspring("Charmed", {{"Diligent", "Valiant"}})
  295. addOffspring("Enchanted", {{"Valiant", "Charmed"}})
  296. addOffspring("Supernatural", {{"Enchanted", "Charmed"}})
  297. -- Scholarly Branch
  298. addOffspring("Pupil", {{"Arcane", "Enchanted"}})
  299. addOffspring("Scholarly", {{"Arcane", "Pupil"}})
  300. addOffspring("Savant", {{"Scholarly", "Pupil"}})
  301.  
  302.  
  303. -- Thaumic Branch
  304. addOffspring("Stark", {{"Arcane", "Supernatural"}})
  305.  
  306.  
  307. -- Skulking Branch
  308. addOffspring("Skulking", {{"Mysterious", "Modest"}, {"Mysterious", "Desolate"}})
  309. addOffspring("Brainy", {{"Skulking", "Sinister"}})
  310. addOffspring("Gossamer", {{"Skulking", "Supernatural"}, {"Skulking", "Ancient"}})
  311. addOffspring("Wispy", {{"Gossamer", "Cultivated"}})
  312. addOffspring("Batty", {{"Skulking", "Frugal"}, {"Skulking", "Rocky"}})
  313. addOffspring("Ghastly", {{"Skulking", "Austere"}, {"Skulking", "Creepy"}})
  314.  
  315.  
  316. addOffspring("Aware", {{"Demonic", "Edenic"}})
  317. addOffspring("Vis", {{"Aware", "Arcane"}, {"Aware", "Stark"}})
  318. addOffspring("Pure", {{"Vis", "Edenic"}})
  319. addOffspring("Flux", {{"Vis", "Edenic"}})
  320. addOffspring("Node", {{"Pure", "Flux"}})
  321. addOffspring("Rejuvenating", {{"Vis", "Energetic"}})
  322. addOffspring("Timely", {{"Industrious", "Enchanted"}})
  323. addOffspring("Lordly", {{"Timely", "Imperial"}})
  324. addOffspring("Doctoral", {{"Timely", "Lordly"}})
  325. addOffspring("Spirit", {{"Fiendish", "Enchanted"}, {"Fiendish", "Ender"}})
  326. addOffspring("Soul", {{"Fiendish", "Spirit"}})
  327. addOffspring("Minium", {{"Sinister", "Mysterious"}, {"Frugal", "Pupil"}})
  328. addOffspring("Iron", {{"Industrious", "Diligent"}})
  329. addOffspring("Gold", {{"Minium", "Lead"}})
  330.  
  331.  
  332. addOffspring("Copper", {{"Industrious", "Meadows"}})
  333. addOffspring("Tin", {{"Industrious", "Forest"}})
  334. addOffspring("Silver", {{"Imperial", "Modest"}})
  335. addOffspring("Lead", {{"Industrious", "Copper"}, {"Industrious", "Tin"}, {"Industrious", "Common"}})
  336. addOffspring("Diamond", {{"Austere", "Gold"}})
  337. addOffspring("Emerald", {{"Austere", "Silver"}})
  338. addOffspring("Poultry", {{"Common", "Skulking"}})
  339. addOffspring("Beefy", {{"Common", "Skulking"}})
  340. addOffspring("Porcine", {{"Common", "Skulking"}})
  341.  
  342.  
  343.  
  344. -- logging ---------------------------------------------------------------------
  345.  
  346. local logFile = fs.open("bee.log", "w")
  347. function log(msg)
  348. msg = msg or ""
  349. logFile.write(tostring(msg))
  350. logFile.flush()
  351. io.write(msg)
  352. end
  353. function logLine(msg)
  354. msg = msg or ""
  355. logFile.write(msg.."\n")
  356. logFile.flush()
  357. io.write(msg.."\n")
  358. end
  359.  
  360. -- analyzing functions ---------------------------------------------------------
  361.  
  362. -- Fix for some versions returning bees.species.*
  363. function fixName(name)
  364. return name:gsub("bees%.species%.",""):gsub("^.", string.upper)
  365. end
  366.  
  367. function clearSystem()
  368. -- orient turtle
  369. while true do
  370. local p = peripheral.wrap("front")
  371. if p and p.isBee then
  372. break
  373. end
  374. turtle.turnRight()
  375. end
  376. -- clear out analyzer
  377. turtle.turnLeft()
  378. while turtle.suckUp() do end
  379. -- clear out beealyzer
  380. turtle.turnRight()
  381. turtle.suck()
  382. -- clear out apiary
  383. turtle.turnRight()
  384. while turtle.suck() do end
  385. end
  386.  
  387. function getBees()
  388. -- get bees from apiary
  389. log("waiting for bees.")
  390. turtle.select(1)
  391. while not turtle.suck() do
  392. sleep(10)
  393. log(".")
  394. end
  395. log("*")
  396. while turtle.suck() do
  397. log("*")
  398. end
  399. logLine()
  400. end
  401.  
  402. function countBees()
  403. -- spread dups and fill gaps
  404. local count = 0
  405. for i = 1, 16 do
  406. local slotCount = turtle.getItemCount(i)
  407. if slotCount == 1 then
  408. for j = 1, i-1 do
  409. if turtle.getItemCount(j) == 0 then
  410. turtle.select(i)
  411. turtle.transferTo(j)
  412. break
  413. end
  414. end
  415. count = count + 1
  416. elseif slotCount > 1 then
  417. for j = 2, slotCount do
  418. turtle.select(i)
  419. for k = 1, 16 do
  420. if turtle.getItemCount(k) == 0 then
  421. turtle.transferTo(k, 1)
  422. end
  423. end
  424. end
  425. if turtle.getItemCount(i) > 1 then
  426. turtle.dropDown(turtle.getItemCount(i)-1)
  427. end
  428. end
  429. end
  430. return count
  431. end
  432.  
  433. function breedBees(princessSlot, droneSlot)
  434. turtle.select(princessSlot)
  435. invmod.dropSneaky(1)
  436. turtle.select(droneSlot)
  437. invmod.dropSneaky(0)
  438. end
  439.  
  440. function ditchProduct()
  441. print("ditching product...")
  442. turtle.turnLeft()
  443. m = peripheral.wrap("front")
  444. for i = 1, 16 do
  445. if turtle.getItemCount(i) > 0 then
  446. turtle.select(i)
  447. turtle.drop()
  448. if not m.isBee() then
  449. turtle.suck()
  450. turtle.dropDown()
  451. else
  452. turtle.suck()
  453. end
  454. end
  455. end
  456. turtle.turnRight()
  457. end
  458.  
  459. function scanBees()
  460. log("scanning bees")
  461. turtle.turnLeft()
  462. turtle.turnLeft()
  463. for i = 1, 16 do
  464. if turtle.getItemCount(i) > 0 then
  465. log(".")
  466. turtle.select(i)
  467. invmod.dropSneakyUp(2)
  468. while not turtle.suckUp() do
  469. sleep(1)
  470. end
  471. end
  472. end
  473. logLine()
  474. turtle.turnRight()
  475. turtle.turnRight()
  476. end
  477.  
  478. function swapBee(slot1, slot2, freeSlot)
  479. turtle.select(slot1)
  480. turtle.transferTo(freeSlot)
  481. turtle.select(slot2)
  482. turtle.transferTo(slot1)
  483. turtle.select(freeSlot)
  484. turtle.transferTo(slot2)
  485. end
  486.  
  487. --if princess has no scored offspring (mutated) ... maybewe want to breed with the highest scored drone (droneScore)
  488. --find the highest drone bee we have, find what we need to breed with it
  489. --if we have that drone, breed princess with that drone
  490. --else find the highest we have in that tree, and repeat
  491. --if we dont have any then exit with needbee: bee (tree)
  492.  
  493.  
  494. --returns which drone is highest parent for species, or species if available
  495. function findAvailableParent(species, droneData)
  496. local droneIndex = 0
  497. local droneScore = 99999
  498.  
  499. for i = 2, 16 do
  500. if droneData[i] then
  501. if droneData[i]["speciesPrimary"] == species or droneData[i]["speciesSecondary"] == species then
  502. droneIndex = i
  503. droneScore = bee[species].score
  504. return droneIndex, droneScore
  505. end
  506. end
  507. end
  508.  
  509. for i, parents in ipairs(bee[species].mutateFrom) do
  510. index, score = findAvailableParent(parents[1], droneData)
  511. if score < droneScore then
  512. droneIndex = index
  513. droneScore = score
  514. end
  515. index, score = findAvailableParent(parents[2], droneData)
  516. if score < droneScore then
  517. droneIndex = index
  518. droneScore = score
  519. end
  520. end
  521. if droneScore == 99999 then
  522. logLine("Missing bee species: " .. species)
  523. end
  524. return droneIndex, droneScore
  525. end
  526.  
  527. function analyzeBees()
  528.  
  529. logLine("analyzing bees...")
  530. local freeSlot
  531. local princessSlot
  532. local princessData
  533. local droneData = {}
  534. turtle.turnLeft()
  535. local beealyzer = peripheral.wrap("front")
  536.  
  537. for i = 1, 16 do
  538. if turtle.getItemCount(i) > 0 then
  539. turtle.select(i)
  540. turtle.drop()
  541. local beeData = beealyzer.analyze()
  542. turtle.suck()
  543. if not beeData["speciesPrimary"] then
  544. print("Bee "..i.." not correctly analyzed")
  545. else
  546. beeData["speciesPrimary"] = fixName(beeData["speciesPrimary"])
  547. beeData["speciesSecondary"] = fixName(beeData["speciesSecondary"])
  548. if beeData["type"] == "princess" then
  549. princessData = beeData
  550. princessSlot = i
  551. else
  552. droneData[i] = beeData
  553. end
  554. end
  555. else
  556. freeSlot = i
  557. end
  558. end
  559.  
  560. if princessData then
  561. if princessSlot ~= 1 then
  562. swapBee(1, princessSlot, freeSlot)
  563. droneData[princessSlot] = droneData[1]
  564. droneData[1] = nil
  565. princessSlot = 1
  566. end
  567.  
  568. -- bubble sort drones
  569. print("sorting drones...")
  570. for i = 2, 16 do
  571. if turtle.getItemCount(i) > 0 and droneData[i] then
  572. droneData[i].score = scoreBee(princessData, droneData[i])
  573. for j = i - 1, 2, -1 do
  574. if droneData[j+1].score > droneData[j].score then
  575. swapBee(j+1, j, freeSlot)
  576. droneData[j+1], droneData[j] = droneData[j], droneData[j+1]
  577. end
  578. end
  579. end
  580. end
  581.  
  582. if droneData[2] and droneData[2].score < 1 then
  583. --no breeding combos found for this princess
  584. i, j = findAvailableParent(targetedSpecies, droneData)
  585.  
  586. if j == 99999 then
  587. droneData[2].score = 0
  588. else
  589. swapBee(2, i, freeSlot)
  590. droneData[2], droneData[i] = droneData[i], droneData[2]
  591. end
  592. end
  593.  
  594. printHeader()
  595. princessData.slot = 1
  596. printBee(princessData)
  597. for i = 2, 16 do
  598. if droneData[i] then
  599. droneData[i].slot = i
  600. printBee(droneData[i])
  601. end
  602. end
  603. end
  604. logLine()
  605. turtle.turnRight()
  606. return princessData, droneData
  607. end
  608.  
  609.  
  610.  
  611. --if drone score is 9999 return 9999 or 10k for pure
  612. --find offspring for this combo, return highest scoring offspring ... for each bee, if its cerated from these get its score
  613.  
  614.  
  615. function scoreBee(princessData, droneData)
  616.  
  617. local droneSpecies = {droneData["speciesPrimary"], droneData["speciesSecondary"]}
  618. local princessSpecies = {princessData["speciesPrimary"], princessData["speciesSecondary"]}
  619.  
  620.  
  621. if bee[droneSpecies[1]].score == 9999 and bee[droneSpecies[2]].score == 9999 then
  622. return 10000
  623. if bee[droneSpecies[1]].score == 9999 or bee[droneSpecies[2]].score == 9999 then
  624. return 9999
  625. end
  626.  
  627. local droneScore = 0
  628.  
  629. for name, beeData in pairs(bees) do
  630. if beeData.score > droneScore then
  631. for i, parents in ipairs(beeData.mutateFrom) do
  632. if princessSpecies[1] == parents[1] or princessSpecies[2] == parents[1] then
  633. if droneSpecies[1] == parents[2] and droneSpecies[2] == parents[2] then
  634. droneScore = beeData.score + 1
  635. break
  636. end
  637. if droneSpecies[1] == parents[2] or droneSpecies[2] == parents[2] then
  638. droneScore = beeData.score
  639. break
  640. end
  641. end
  642. if princessSpecies[1] == parents[2] or princessSpecies[2] == parents[2] then
  643. if droneSpecies[1] == parents[1] and droneSpecies[2] == parents[1] then
  644. droneScore = beeData.score + 1
  645. break
  646. end
  647. if droneSpecies[1] == parents[1] or droneSpecies[2] == parents[1] then
  648. droneScore = beeData.score
  649. break
  650. end
  651. end
  652. end
  653. end
  654. end
  655.  
  656. return droneScore
  657. end
  658.  
  659. function printHeader()
  660. logLine()
  661. logLine("typ species f spd d n f c tmp hmd score")
  662. logLine("-|-|-------|-|---|-|-|-|-|---|---|-----")
  663. end
  664.  
  665. toleranceString = {
  666. ["NONE"] = " ",
  667. ["UP_1"] = " +1 ",
  668. ["UP_2"] = " +2 ",
  669. ["UP_3"] = " +3 ",
  670. ["DOWN_1"] = " -1 ",
  671. ["DOWN_2"] = " -2 ",
  672. ["DOWN_3"] = " -3 ",
  673. ["BOTH_1"] = "+-1 ",
  674. ["BOTH_2"] = "+-2 ",
  675. ["BOTH_3"] = "+-3 "
  676. }
  677. function printBee(beeData)
  678. log(beeData["slot"] < 10 and beeData["slot"].." " or beeData["slot"])
  679. if (beeData["type"] == "princess") then
  680. log("P ")
  681. else
  682. log("d ")
  683. end
  684. log(beeData["speciesPrimary"]:gsub("bees%.species%.",""):sub(1,3)..":"..beeData["speciesSecondary"]:gsub("bees%.species%.",""):sub(1,3).." ")
  685. log(tostring(beeData["fertility"]).." ")
  686. log(beeData["speed"] == 1 and "1.0 " or tostring(beeData["speed"]).." ")
  687. if beeData["diurnal"] then
  688. log("d ")
  689. else
  690. log(" ")
  691. end
  692. if beeData["nocturnal"] then
  693. log("n ")
  694. else
  695. log(" ")
  696. end
  697. if beeData["tolerantFlyer"] then
  698. log("f ")
  699. else
  700. log(" ")
  701. end
  702. if beeData["caveDwelling"] then
  703. log("c ")
  704. else
  705. log(" ")
  706. end
  707. log(toleranceString[beeData["toleranceTemperature"]])
  708. log(toleranceString[beeData["toleranceHumidity"]])
  709. if beeData.score then
  710. logLine(string.format("%5.1d", beeData.score).." ")
  711. else
  712. logLine()
  713. end
  714. end
  715.  
  716. function dropExcess(droneData)
  717. print("dropping excess...")
  718. local count = 0
  719. for i = 1, 16 do
  720. if turtle.getItemCount(i) > 0 then
  721. count = count + 1
  722.  
  723. -- drop drones over 9 to clear space for newly bred bees and product
  724. if count > 9 then
  725. turtle.select(i)
  726. turtle.dropDown()
  727. count = count - 1
  728. end
  729. end
  730. end
  731. end
  732.  
  733. function isPurebred(princessData, droneData)
  734. -- check if princess and drone are exactly the same and no chance for mutation
  735. if princessData["speciesPrimary"] ~= princessData["speciesSecondary"] then
  736. return false
  737. end
  738. for key, value in pairs(princessData) do
  739. if value ~= droneData[key] and key ~= "territory" and key ~= "type" and key ~= "slot" then
  740. return false
  741. end
  742. end
  743. return true
  744. end
  745.  
  746. function getUnknown(princessData, droneData)
  747. -- lists species that are not in the bee graph
  748. local unknownSpecies = {}
  749. if not bees[princessData["speciesPrimary"]] then
  750. table.insert(unknownSpecies, princessData["speciesPrimary"])
  751. end
  752. if not bees[princessData["speciesSecondary"]] then
  753. table.insert(unknownSpecies, princessData["speciesSecondary"])
  754. end
  755. for _, beeData in pairs(droneData) do
  756. if not bees[beeData["speciesPrimary"]] then
  757. table.insert(unknownSpecies, beeData["speciesPrimary"])
  758. end
  759. if not bees[beeData["speciesSecondary"]] then
  760. table.insert(unknownSpecies, beeData["speciesSecondary"])
  761. end
  762. end
  763. return unknownSpecies
  764. end
  765.  
  766.  
  767. function scoreBeesIterate(name)
  768.  
  769. for i, parents in ipairs(bees[name].mutateFrom) do
  770. if bees[parents[1]].score < bees[name].score - 2 then
  771. bees[parents[1]].score = bees[name].score - 2
  772. scoreBeesIterate(parents[1])
  773. end
  774. if bees[parents[2]].score < bees[name].score - 2 then
  775. bees[parents[2]].score = bees[name].score - 2
  776. scoreBeesIterate(parents[2])
  777. end
  778. end
  779. end
  780.  
  781. -- program begins, read input---------------------------------------------------
  782.  
  783. tArgs = { ... }
  784. if #tArgs > 0 then
  785.  
  786. logLine("targeting bee species:")
  787. for i, target in ipairs(tArgs) do
  788. targetedSpecies = target
  789. bees[target].score = 9999
  790. for name, data in pairs(bees) do
  791. if data.score > 1 then
  792. logLine(name .. string.rep(" ", 20-#name), data.score)
  793. end
  794. end
  795. end
  796.  
  797. for name, beeData in pairs(bees) do
  798. if beeData.score == 9999 then
  799. scoreBeesIterate(name)
  800. end
  801. end
  802. else
  803. logLine("Usage Bee [species]")
  804. logFile.close()
  805. return false
  806. end
  807.  
  808. -- breeding loop ---------------------------------------------------------------
  809.  
  810. logLine("Clearing system...")
  811. clearSystem()
  812. while true do
  813.  
  814. ditchProduct()
  815. countBees()
  816. scanBees()
  817. princessData, droneData = analyzeBees()
  818.  
  819. if droneData[2].score == 0 then --analyze failed to find a breeding pair
  820. break
  821. end
  822.  
  823. if princessData then
  824.  
  825. if isPurebred(princessData, droneData[2]) then
  826. logLine("Bees are purebred")
  827. turtle.turnRight()
  828. break
  829. end
  830.  
  831. local unknownSpecies = getUnknown(princessData, droneData)
  832. if #unknownSpecies > 0 then
  833. logLine("Please add new species to bee graph:")
  834. for _, species in ipairs(unknownSpecies) do
  835. logLine(" "..species)
  836. end
  837. turtle.turnRight()
  838. break
  839. end
  840.  
  841. breedBees(1, 2)
  842. dropExcess(droneData)
  843. end
  844.  
  845. getBees()
  846. if redstone.getInput("right") then logLine("Execution ended by user.") break end
  847. end
  848. logFile.close()
Advertisement
Add Comment
Please, Sign In to add comment