Advertisement
mrWhiskasss

пчелы

Dec 21st, 2021 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.54 KB | None | 0 0
  1. -- (c) Max K https://github.com/asgmax
  2.  
  3. local component = require("component")
  4. local computer = require("computer")
  5. local term = require("term")
  6. local event = require("event")
  7. local gpu = component.gpu
  8. local serialization = require("serialization")
  9. local unicode = require("unicode")
  10.  
  11. local const = {
  12. strongPrincessCount = 3, -- >=
  13. strongCount = 10,
  14. strongCountRoof = 15,
  15. trashInterface = component.proxy("a972b1ec-bf18-418c-bf26-7e6bb5740871"),
  16. analyzerInterface = component.proxy("5df617f8-4b45-4cf1-ab82-0c3cdd90b00a"),
  17. analyzerChestSide = "SOUTH", -- relative to analyzerInterface
  18. exportSide = "WEST", -- side to export for test purposes
  19. houseSide = "WEST", -- side of apiary (relative to interface)
  20. trashSide = "SOUTH", -- side of chest (relative to intTrash (trashing interface)) to drop notNatural princesses
  21. }
  22.  
  23. local int = {
  24. [1] = component.proxy("ab5bca03-1ef4-4fca-bcc5-8a3a5074e652"),
  25. }
  26.  
  27. local apiary = {
  28. [1] = component.proxy("35677c94-0f77-4da6-b19e-8a74e24d586b"),
  29. }
  30.  
  31. function isBee(table) if table.individual then return true else return false end end
  32. function isNatural(table) if table.individual.isNatural == true then return true else return false end end
  33. function isAnalyzed(table) if table.individual.isAnalyzed == true then return true else return false end end
  34. function isDrone(table) if string.find(table.label,"Drone") then return true else return false end end
  35. function isPrincess(table) if string.find(table.label,"Princess") then return true else return false end end
  36. function isQueen(table) if string.find(table.label,"Queen") then return true else return false end end
  37. function isPure(table) if table.individual.isAnalyzed and table.individual.active.species == table.individual.inactive.species then return true else return false end end
  38. function isUnpure(table) if table.individual.isAnalyzed and table.individual.active.species ~= table.individual.inactive.species then return true else return false end end
  39. function isSpecie(table,specie) if string.lower(specie) == string.lower(table.individual.displayName) then return true else return false end end
  40. function isAl1(table,specie) if string.lower(specie) == string.lower(table.individual.active.species) then return true else return false end end
  41. function isAl2(table,specie) if string.lower(specie) == string.lower(table.individual.inactive.species) then return true else return false end end
  42.  
  43. function isAlx(table,specie) if string.lower(specie) == string.lower(table.individual.active.species) or string.lower(specie) == string.lower(table.individual.inactive.species) then return true else return false end end
  44. function isPureSpec(table,specie) if string.lower(specie) == string.lower(table.individual.active.species) and string.lower(specie) == string.lower(table.individual.inactive.species) then return true else return false end end
  45.  
  46. function isVanilla(table) if isSpecie(table,"meadows") or isSpecie(table,"forest") then return true else return false end end
  47. function canFind(string1,string2) if string.find(string1,string2) then return true else return false end end
  48.  
  49. function totalBeesCount()
  50. local ti = int[1].getItemsInNetwork()
  51. local ta = int[1].getAvailableItems()
  52. local count = 0
  53. for i in ipairs(ti) do
  54. if isBee(ti[i]) then
  55. count = count + ti[i].size
  56. end
  57. end
  58. return count
  59. end
  60.  
  61.  
  62. function clearNetwork()
  63. local ti = int[1].getItemsInNetwork()
  64. local ta = int[1].getAvailableItems()
  65. local totalExported = 0
  66. for i in ipairs(ti) do
  67. if isBee(ti[i]) then
  68. local toexport = ti[i].size
  69. repeat
  70. local exported = const.trashInterface.exportItem(ta[i].fingerprint,const.trashSide,toexport)
  71. toexport = toexport - exported.size
  72. totalExported = totalExported + exported.size
  73. until toexport == 0
  74. end
  75. end
  76. io.write("clearNetwork() - removed " .. totalExported .. " \n")
  77. end
  78.  
  79. function isApiaryFree(num)
  80. for i = 1,9 do
  81. if apiary[num].getStackInSlot(i) then
  82. return false
  83. end
  84. end
  85. return true
  86. end
  87.  
  88. function waitForApiary(num)
  89.  
  90. io.write("waitForApiary(" .. num .. ") ")
  91. if isApiaryFree(num) ~= true then
  92. repeat
  93. io.write(". ")
  94. os.sleep(3)
  95. until isApiaryFree(num) == true
  96. io.write(" \n")
  97. else
  98. io.write("waitForApiary(" .. num .. ") - is free \n" )
  99. end
  100. end
  101.  
  102. function waitForAnalyze()
  103. local count1 = totalBeesCount()
  104. local a = analyzeAll()
  105. if a ~= false then
  106. io.write("waitForAnalyze() - analyzing ")
  107. repeat
  108. io.write(" .")
  109. os.sleep(5)
  110. until totalBeesCount() == count1
  111. io.write("\nwaitForAnalyze() - analysis finished\n")
  112. else
  113. io.write("waitForAnalyze() - nothing to analyze\n")
  114. end
  115. end
  116.  
  117. function analyze(table) -- ta[i]
  118. local toexport = const.analyzerInterface.getItemDetail(table.fingerprint).basic().qty
  119. local sent = 0
  120. repeat
  121. local exported = const.analyzerInterface.exportItem(table.fingerprint,const.analyzerChestSide,toexport)
  122. toexport = toexport - exported.size
  123. sent = sent + exported.size
  124. until toexport == 0
  125. return sent
  126. end
  127.  
  128. function analyzeAll()
  129. local ti = int[1].getItemsInNetwork()
  130. local ta = int[1].getAvailableItems()
  131. local unknownStacks = 0
  132. local knownStacks = 0
  133. local beesSent = 0
  134. local knownBees = 0
  135. for i in ipairs(ti) do
  136. if isBee(ti[i]) then
  137. if not isAnalyzed(ti[i]) then
  138. unknownStacks = unknownStacks + 1
  139. beesSent = beesSent + analyze(ta[i])
  140. else
  141. knownStacks = knownStacks + 1
  142. knownBees = knownBees + ti[i].size
  143. end
  144. end
  145. end
  146. io.write("analyzeAll() - known: " .. knownBees .. " bees (" .. knownStacks .. " stacks), being analyzed: " .. beesSent .. " bees (" .. unknownStacks .. " stacks) \n")
  147. if unknownStacks == 0 then
  148. return false
  149. end
  150. end
  151.  
  152. function isType(table,type) -- "drone" or "princess" or "queen"
  153. if string.lower(type) == "drone" then
  154. return isDrone(table)
  155. elseif string.lower(type) == "princess" then
  156. return isPrincess(table)
  157. elseif string.lower(type) == "queen" then
  158. return isQueen(table)
  159. else
  160. io.write("isType() - incorrect argument " .. tostring(type) .. " \n")
  161. end
  162. end
  163.  
  164. function isPurity(table,purity)
  165. if string.lower(purity) == "pure" then
  166. return isPure(table)
  167. elseif string.lower(purity) == "unpure" then
  168. return isUnpure(table)
  169. else
  170. io.write("isPurity() - incorrect argument " .. tostring(purity) .. " \n")
  171. end
  172. end
  173.  
  174. function countPrincess(al1,al2)
  175. local ti = int[1].getItemsInNetwork()
  176. local ta = int[1].getAvailableItems()
  177. local count = 0
  178. for i in ipairs(ti) do
  179. if isBee(ti[i]) and isAnalyzed(ti[i]) and isPrincess(ti[i]) then
  180. if al1check(ti[i],al1) and al2check(ti[i],al2) then
  181. count = count + ti[i].size
  182. end
  183. end
  184. end
  185. io.write("countPrincess() - " .. tostring(al1) .. "/" .. tostring(al2) .. " - " .. count .. "\n")
  186. return count
  187. end
  188.  
  189. function countDrone(al1,al2)
  190. local ti = int[1].getItemsInNetwork()
  191. local ta = int[1].getAvailableItems()
  192. local count = 0
  193. for i in ipairs(ti) do
  194. if isBee(ti[i]) and isAnalyzed(ti[i]) and isDrone(ti[i]) then
  195. if al1check(ti[i],al1) and al2check(ti[i],al2) then
  196. count = count + ti[i].size
  197. end
  198. end
  199. end
  200. io.write("countDrone() - " .. tostring(al1) .. "/" .. tostring(al2) .. " - " .. count .. "\n")
  201. return count
  202. end
  203.  
  204.  
  205.  
  206. function al1check(table,al1)
  207. if al1 then
  208. if string.lower(al1) == string.lower(table.individual.active.species) then
  209. return true
  210. else
  211. return false
  212. end
  213. else
  214. return true
  215. end
  216. end
  217.  
  218. function al2check(table,al2)
  219. if al2 then
  220. if string.lower(al2) == string.lower(table.individual.inactive.species) then
  221. return true
  222. else
  223. return false
  224. end
  225. else
  226. return true
  227. end
  228. end
  229.  
  230.  
  231. function move(interface,side,table,number) -- table = ta[i] ONLY
  232. local toexport = math.min(number or 1,table.size)
  233. local totalExported = 0
  234. repeat
  235. local exported = int[interface].exportItem(table.fingerprint,side,toexport)
  236. toexport = toexport - exported.size
  237. totalExported = totalExported + exported.size
  238. until toexport == 0
  239. io.write("move() - moved:" .. totalExported .. ", int:" .. interface .. ", side:" .. side .. ", " .. table.fingerprint.id .. ":" .. table.fingerprint.dmg .. " \n")
  240. return totalExported
  241. end
  242.  
  243. function house1(table)
  244. move(1,const.houseSide,table,1)
  245. end
  246.  
  247. function getParents(spec)
  248. if string.lower(spec) == "common" then
  249. return "forest","meadows"
  250. elseif string.lower(spec) == "cultivated" then
  251. return "common","forest"
  252. else
  253. local t = apiary[1].getBeeParents(spec)
  254. local par1 = t[1].allele1.name
  255. local par2 = t[1].allele2.name
  256. return par1, par2
  257. end
  258. end
  259.  
  260. function GET(purity,type,spec) -- RETURNS ta[i], ("pure","princess","common") or ("unpure","drone","meadows"), pure means al1 == al2
  261. local ti = int[1].getItemsInNetwork()
  262. local ta = int[1].getAvailableItems()
  263. local count = 0
  264. local tai
  265. local speed = 0
  266. for i in ipairs(ti) do
  267. if isBee(ti[i]) and isAnalyzed(ti[i]) then
  268. if isType(ti[i],type) and isPurity(ti[i],purity) and isAlx(ti[i],spec) then
  269. count = count + ti[i].size
  270. if ti[i].individual.active.speed + ti[i].individual.inactive.speed > speed then
  271. speed = ti[i].individual.active.speed + ti[i].individual.inactive.speed
  272. tai = ta[i]
  273. end
  274. end
  275. end
  276. end
  277. io.write("GET() - " .. purity .. " " .. type .. " " .. spec .. " - found " .. count .. " (returned avg speed: " .. speed/2 .. ") \n")
  278. return count, tai
  279. end
  280.  
  281. function GET1(type,spec1,spec2) -- RETURNS ta[i], gets only bees with BOTH spec genes present
  282. local ti = int[1].getItemsInNetwork()
  283. local ta = int[1].getAvailableItems()
  284. local count = 0
  285. local tai
  286. local speed = 0
  287. for i in ipairs(ti) do
  288. if isBee(ti[i]) and isAnalyzed(ti[i]) then
  289. if isType(ti[i],type) and isAlx(ti[i],spec1) and isAlx(ti[i],spec2) then
  290. count = count + ti[i].size
  291. if ti[i].individual.active.speed + ti[i].individual.inactive.speed > speed then
  292. speed = ti[i].individual.active.speed + ti[i].individual.inactive.speed
  293. tai = ta[i]
  294. end
  295. end
  296. end
  297. end
  298. io.write("GET1() - " .. type .. " " .. spec1 .. "+" .. spec2 .. " - found " .. count .. " (returned avg speed: " .. speed/2 .. ") \n")
  299. return count, tai
  300. end
  301.  
  302. function GET3(type,spec) -- RETURNS ta[i], counts bees of TYPE and at least 1 of genes SPEC
  303. local ti = int[1].getItemsInNetwork()
  304. local ta = int[1].getAvailableItems()
  305. local count = 0
  306. local tai
  307. local speed = 0
  308. for i in ipairs(ti) do
  309. if isBee(ti[i]) and isAnalyzed(ti[i]) then
  310. if isType(ti[i],type) and isAlx(ti[i],spec) then
  311. count = count + ti[i].size
  312. if ti[i].individual.active.speed + ti[i].individual.inactive.speed > speed then
  313. speed = ti[i].individual.active.speed + ti[i].individual.inactive.speed
  314. tai = ta[i]
  315. end
  316. end
  317. end
  318. end
  319. io.write("GET3() - found " .. count .. " " .. spec .. " " .. type .. " \n")
  320. return count, tai
  321. end
  322.  
  323. function GET4(purity,type) -- RETURNS ta[i],
  324. local ti = int[1].getItemsInNetwork()
  325. local ta = int[1].getAvailableItems()
  326. local count = 0
  327. local tai
  328. local speed = 0
  329. local reportedSpecs
  330. for i in ipairs(ti) do
  331. if isBee(ti[i]) and isAnalyzed(ti[i]) then
  332. if isType(ti[i],type) and isPurity(ti[i],purity) then
  333. count = count + ti[i].size
  334. if ti[i].individual.active.speed + ti[i].individual.inactive.speed > speed then
  335. speed = ti[i].individual.active.speed + ti[i].individual.inactive.speed
  336. reportedSpecs = ti[i].individual.active.species .. "/" .. ti[i].individual.inactive.species
  337. tai = ta[i]
  338. end
  339. end
  340. end
  341. end
  342. io.write("GET4() - " .. purity .. " " .. type .. " - found " .. tostring(reportedSpecs) .. " " .. count .. " (returned avg speed: " .. speed/2 .. ") \n")
  343. return count, tai
  344. end
  345.  
  346. function breed(spec)
  347. local par1, par2 = getParents(spec)
  348. local princess = nil
  349. local drone = nil
  350. local function getDrone(num) -- num is 1 for par1, 2 for par2, 3 for par1 AND par2
  351. if num == 2 then
  352. if GET("pure","drone",par2) > 0 then
  353. _,drone = GET("pure","drone",par2)
  354. elseif GET1("drone",par1,par2) > 0 then
  355. _,drone = GET1("drone",par1,par2)
  356. elseif GET("unpure","drone",par2) > 0 then
  357. _,drone = GET("unpure","drone",par2)
  358. end
  359. elseif num == 1 then
  360. if GET("pure","drone",par1) > 0 then
  361. _,drone = GET("pure","drone",par1)
  362. elseif GET1("drone",par1,par2) > 0 then
  363. _,drone = GET1("drone",par1,par2)
  364. elseif GET("unpure","drone",par1) > 0 then
  365. _,drone = GET("unpure","drone",par1)
  366. end
  367. elseif num == 3 then
  368. if GET1("drone",par1,par2) > 0 then
  369. _,drone = GET1("drone",par1,par2)
  370. elseif GET("pure","drone",par1) > 0 then
  371. _,drone = GET("pure","drone",par1)
  372. elseif GET("pure","drone",par2) > 0 then
  373. _,drone = GET("pure","drone",par2)
  374. elseif GET("unpure","drone",par1) > 0 then
  375. _,drone = GET("unpure","drone",par1)
  376. elseif GET("unpure","drone",par2) > 0 then
  377. _,drone = GET("unpure","drone",par2)
  378. end
  379. end
  380. end
  381. if not isStrong(par1) and par1 ~= "forest" and par1 ~= "meadows" then
  382. io.write("breed() - " .. par1 .. " is not Strong, calling multiply() for ".. par1 .. " \n")
  383. multiply(par1)
  384. return true
  385. elseif not isStrong(par2) and par1 ~= "forest" and par1 ~= "meadows" then
  386. io.write("breed() - " .. par2 .. " is not Strong, calling multiply() for ".. par2 .. " \n")
  387. multiply(par2)
  388. return true
  389. end
  390. if GET("pure","princess",par1) > 2 then
  391. _,princess = GET("pure","princess",par1)
  392. getDrone(2)
  393. end
  394.  
  395. if drone == nil then
  396. if GET("pure","princess",par2) > 2 then
  397. _,princess = GET("pure","princess",par2)
  398. getDrone(1)
  399. end
  400. end
  401.  
  402. if drone == nil then
  403. if GET1("princess",par1,par2) > 0 then
  404. _,princess = GET1("princess",par1,par2)
  405. getDrone(3)
  406. end
  407. end
  408.  
  409. if drone == nil then
  410. if GET("unpure","princess",par1) > 0 then
  411. _,princess = GET("unpure","princess",par1)
  412. getDrone(2)
  413. end
  414. end
  415.  
  416. if drone == nil then
  417. if GET("unpure","princess",par2) > 0 then
  418. _,princess = GET("unpure","princess",par2)
  419. getDrone(1)
  420. end
  421. end
  422.  
  423. if princess and drone then
  424. io.write("breed(".. spec ..") - housing \n")
  425. house1(princess)
  426. house1(drone)
  427. else
  428. local missingSpecGene = nil
  429. local isPrincessMissing = nil
  430. local isDroneMissing = nil
  431. io.write("breed() - cant find (enough) princesses OR a drone to breed " .. spec .. " \n")
  432. if GET3("drone",par1) < 1 then
  433. io.write("breed(".. spec ..") - missing drone with at least 1 gene of spec " .. par1 .. " \n")
  434. missingSpecGene = par1
  435. isDroneMissing = true
  436. end
  437. if GET3("drone",par2) < 1 then
  438. io.write("breed(".. spec ..") - missing drone with at least 1 gene of spec " .. par2 .. " \n")
  439. if missingSpecGene == nil then
  440. missingSpecGene = par2
  441. end
  442. isDroneMissing = true
  443. end
  444. if GET3("princess",par1) < 3 then
  445. io.write("breed(".. spec ..") - missing princess (or not enough pures) with at least 1 gene of spec " .. par1 .. " \n")
  446. if missingSpecGene == nil then
  447. missingSpecGene = par1
  448. end
  449. isPrincessMissing = true
  450. end
  451. if GET3("drone",par2) < 3 then
  452. io.write("breed(".. spec ..") - missing princess (or not enough pures) with at least 1 gene of spec " .. par2 .. " \n")
  453. if missingSpecGene == nil then
  454. missingSpecGene = par2
  455. end
  456. isPrincessMissing = true
  457. end
  458. gpu.setForeground(0x00FF00)
  459. io.write("breed(".. spec ..") - calling multiply for species " .. missingSpecGene .. " \n")
  460. gpu.setForeground(0xFFFFFF)
  461. multiply(missingSpecGene)
  462. end
  463. end
  464.  
  465.  
  466.  
  467. function isStrong(spec)
  468. if GET("pure","princess",spec) >= const.strongPrincessCount and GET("pure","drone",spec) >= const.strongCount then
  469. return true
  470. else
  471. return false
  472. end
  473. end
  474.  
  475.  
  476.  
  477. function multiply(spec)
  478. local princess = nil
  479. local drone = nil
  480.  
  481. local function getPrincessAndDrone()
  482. if GET("pure","princess",spec) > 0 then
  483. _,princess = GET("pure","princess",spec)
  484. else
  485. if
  486. GET("unpure","princess",spec) > 0 then
  487. _,princess = GET("unpure","princess",spec)
  488. end
  489. end
  490.  
  491. if GET("pure","drone",spec) > 0 then
  492. _,drone = GET("pure","drone",spec)
  493. else
  494. if
  495. GET("unpure","drone",spec) > 0 then
  496. _,drone = GET("unpure","drone",spec)
  497. end
  498. end
  499. end
  500.  
  501. local function makePrincess()
  502. local princess1 = nil
  503. local drone1 = nil
  504. if GET("unpure","princess",spec) > 0 then
  505. _,princess1 = GET("unpure","princess",spec)
  506. elseif GET4("unpure","princess") > 0 then
  507. _,princess1 = GET4("unpure","princess")
  508. elseif GET("pure","princess","rocky") > 0 then
  509. _,princess1 = GET("pure","princess","rocky")
  510. else
  511. gpu.setForeground(0x00FF00)
  512. io.write("makePrincess() - ERROR#2 - no princesses with Rocky gene available \n")
  513. gpu.setForeground(0xFFFFFF)
  514. error("ERROR#2")
  515. end
  516. _,drone1 = GET("pure","drone",spec)
  517. house1(princess1)
  518. house1(drone1)
  519. end
  520. if isStrong(spec) then
  521. gpu.setForeground(0x00FF00)
  522. io.write("multiply() - called to multiply " .. spec .. " - strong species (" .. const.strongCount .. " drones and ".. const.strongPrincessCount .. " pure princesses) achieved! \n")
  523. gpu.setForeground(0xFFFFFF)
  524. else
  525. getPrincessAndDrone()
  526. if princess and drone then
  527. if GET("pure","princess",spec) < const.strongPrincessCount and GET("pure","drone",spec) >= const.strongCount then
  528. gpu.setForeground(0x00FF00)
  529. io.write("multiply() - found <" .. const.strongPrincessCount .. " pure princesses of " .. spec .. " and >=" .. const.strongCount .." drones, calling makePrincess() " .. spec .. " \n")
  530. gpu.setForeground(0xFFFFFF)
  531. makePrincess()
  532. elseif GET("pure","princess",spec) > 0 and GET("pure","drone",spec) < const.strongCount then
  533. gpu.setForeground(0xff9933)
  534. io.write("multiply() - forming strong specie of " .. spec .. " \n")
  535. gpu.setForeground(0xFFFFFF)
  536.  
  537. house1(princess)
  538. house1(drone)
  539. else
  540. gpu.setForeground(0x00FF00)
  541. io.write("multiply() - attempting to multiply specie of " .. spec .. " \n")
  542. gpu.setForeground(0xFFFFFF)
  543.  
  544. house1(princess)
  545. house1(drone)
  546. end
  547. elseif not princess and not drone then
  548. io.write("multiply() - cant find princess NOR drone of species " .. spec .. " \n")
  549. gpu.setForeground(0x00FF00)
  550. io.write("multiply() - calling breed() " .. spec .. " \n")
  551. gpu.setForeground(0xFFFFFF)
  552. breed(spec)
  553. elseif princess == nil and GET("pure","drone",spec) > 0 then
  554. io.write("multiply() - cant find princess (have pure drone) of species " .. spec .. " \n")
  555. gpu.setForeground(0x00FF00)
  556. io.write("multiply() - calling makePrincess() " .. spec .. " \n")
  557. gpu.setForeground(0xFFFFFF)
  558. makePrincess()
  559. else
  560. io.write("multiply() - cant find princess AND drone of species " .. spec .. " \n")
  561. gpu.setForeground(0x00FF00)
  562. io.write("multiply() - calling breed() " .. spec .. " \n")
  563. gpu.setForeground(0xFFFFFF)
  564. breed(spec)
  565. end
  566. end
  567. end
  568.  
  569.  
  570. function houseBee(interface,type,al1,al2)
  571. local ti = int[interface].getItemsInNetwork()
  572. local ta = int[interface].getAvailableItems()
  573. for i in ipairs(ti) do
  574. if isBee(ti[i]) and isAnalyzed(ti[i]) and isType(ti[i],type) then
  575. if al1check(ti[i],al1) and al2check(ti[i],al2) then
  576. house1(ta[i])
  577. io.write("houseBee() - " .. tostring(al1) .. "/" .. tostring(al2) .. " " .. tostring(type) .. " housed \n")
  578. return true
  579. end
  580. end
  581. end
  582. io.write("houseBee() - nothing found \n")
  583. end
  584.  
  585. function console()
  586. while true do
  587. gpu.setForeground(0x00FF00)
  588. io.write("Console >> ")
  589. gpu.setForeground(0x99ccff)
  590. local command = io.read()
  591. gpu.setForeground(0xFFFFFF)
  592. if command == "close" then
  593. return true
  594. else
  595. local a,err = pcall((load(command)))
  596. if a == false then
  597. gpu.setForeground(0xFF0000)
  598. io.write("failed: ")
  599. gpu.setForeground(0xFFFFFF)
  600. io.write(tostring(err))
  601. else
  602. io.write("success ")
  603. end
  604. io.write("\n")
  605. end
  606. end
  607. end
  608.  
  609.  
  610. --
  611. term.clear()
  612. console()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement