Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- BeeAlverizer 1 fork of BeeAnalyzer 4
- -- Original code by Direwolf20
- -- Hotfix 1 by Mandydax
- -- Hotfix 2 by Mikeyhun/MaaadMike
- -- Major overhaul to make work without turtle by Damaged
- -- JSON lib loaded to debug arrays
- local JSON = (loadfile "JSON.lua")()
- JSON.strictTypes = true
- -- attribute scoring for same species tie-breaking -----------------------------
- local allBeeData = {}
- local princessSlot = 0
- alveary = peripheral.wrap("back")
- analyzer = peripheral.wrap("left")
- memoryChest = peripheral.wrap("top")
- function getCountInMemoryChest(i)
- local inventoryQuantity
- if not memoryChest.getStackInSlot(i) then
- inventoryQuantity = 0
- else
- invenotryQuantity = memoryChest.getStackInSlot(i)["qty"]
- end
- return invenotryQuantity
- end
- scoresFertility = {
- [1] = 0.1,
- [2] = 0.2,
- [3] = 0.3,
- [4] = 0.4
- }
- scoresSpeed = {
- ["0.3"] = 0.01, -- Slowest
- ["0.6"] = 0.02, -- Slower
- ["0.8"] = 0.03, -- Slow
- ["1"] = 0.04, -- Normal
- ["1.2"] = 0.05, -- Fast
- ["1.4"] = 0.06, -- Faster
- ["1.7"] = 0.07 -- Fastest
- }
- scoresAttrib = {
- diurnal = 0.004,
- nocturnal = 0.003,
- tolerantFlyer = 0.002,
- caveDwelling = 0.0001
- }
- scoresTolerance = {
- ["NONE"] = 0.00000,
- ["UP 1"] = 0.00001,
- ["UP 2"] = 0.00002,
- ["UP 3"] = 0.00003,
- ["UP 4"] = 0.00004,
- ["UP 5"] = 0.00005,
- ["DOWN 1"] = 0.00001,
- ["DOWN 2"] = 0.00002,
- ["DOWN 3"] = 0.00003,
- ["DOWN 4"] = 0.00004,
- ["DOWN 5"] = 0.00005,
- ["BOTH 1"] = 0.00002,
- ["BOTH 2"] = 0.00004,
- ["BOTH 3"] = 0.00006,
- ["BOTH 4"] = 0.00008,
- ["BOTH 5"] = 0.00010
- }
- -- the bee graph ---------------------------------------------------------------
- bees = {}
- function addParent(parent, offspring)
- if bees[parent] then
- bees[parent].mutateTo[offspring] = true
- else
- bees[parent] = {
- --name = parent,
- score = nil,
- mutateTo = {[offspring]=true},
- mutateFrom = {}
- }
- end
- end
- function addOffspring(offspring, parentss)
- if bees[offspring] then
- for i, parents in ipairs(parentss) do
- table.insert(bees[offspring].mutateFrom, parents)
- end
- else
- bees[offspring] = {
- score = nil,
- mutateTo = {},
- mutateFrom = parentss
- }
- end
- for i, parents in ipairs(parentss) do
- for i, parent in ipairs(parents) do
- addParent(parent, offspring)
- end
- end
- end
- -- score bees that have no parent combinations as 1
- -- iteratively find the next bee up the line and increase the score
- function scoreBees()
- -- find all bees with no mutateFrom data
- local beeCount = 0
- local beeScore = 1
- for name, beeData in pairs(bees) do
- if #beeData.mutateFrom == 0 then
- beeData.score = beeScore
- else
- beeCount = beeCount + 1
- end
- end
- while beeCount > 0 do
- beeScore = beeScore * 2
- -- find all bees where all parent combos are scored
- for name, beeData in pairs(bees) do
- if not beeData.score then
- local scoreBee = true
- for i, beeParents in ipairs(beeData.mutateFrom) do
- local parent1 = bees[beeParents[1]]
- local parent2 = bees[beeParents[2]]
- if not parent1.score
- or parent1.score == beeScore
- or not parent2.score
- or parent2.score == beeScore then
- scoreBee = false
- break
- end
- end
- if scoreBee then
- beeData.score = beeScore
- beeCount = beeCount - 1
- end
- end
- end
- end
- end
- -- produce combinations from 1 or 2 lists
- function choose(list, list2)
- local newList = {}
- if list2 then
- for i = 1, #list2 do
- for j = 1, #list do
- if list[j] ~= list[i] then
- table.insert(newList, {list[j], list2[i]})
- end
- end
- end
- else
- for i = 1, #list do
- for j = i, #list do
- if list[i] ~= list[j] then
- table.insert(newList, {list[i], list[j]})
- end
- end
- end
- end
- return newList
- end
- -- Fix for some versions returning bees.species.*
- function fixName(name)
- return name:gsub("bees%.species%.",""):gsub("^.", string.upper)
- end
- for key, value in pairs (alveary.getBeeBreedingData()) do
- addOffspring( fixName(value.result), {{ fixName(value.allele1), fixName(value.allele2) }} )
- end
- scoreBees()
- -- logging ---------------------------------------------------------------------
- local logFile = fs.open("bee.log", "w")
- function log(msg)
- msg = msg or ""
- logFile.write(tostring(msg))
- logFile.flush()
- io.write(msg)
- end
- function logLine(msg)
- msg = msg or ""
- logFile.write(msg.."\n")
- logFile.flush()
- io.write(msg.."\n")
- end
- function logTable(table)
- for key, value in pairs (table) do
- logLine(key .. " = " .. tostring(value))
- end
- end
- -- analyzing functions ---------------------------------------------------------
- function clearSystem()
- -- clear out alveary
- for i = 1,9 do
- memoryChest.pullItem("north",i)
- end
- -- clear out analyzer
- for i = 1, 12 do
- analyzer.pushItem("up",i)
- end
- end
- function getBees()
- -- get bees from apiary
- log("waiting for bees.")
- while not not alveary.getStackInSlot(1) do
- sleep(10)
- log(".")
- end
- log("*")
- for i = 3,9 do
- if memoryChest.pullItem("north",i) then
- log("*")
- end
- end
- logLine()
- end
- function analyzeBees()
- princessSlot = 0
- logLine("analyzing bees...")
- memoryChest.condenseItems()
- local highestScore
- for i,data in pairs(memoryChest.getAllStacks()) do
- if not ( data.rawName == "item.beeprincessge" or data.rawName == "item.beequeenge" or data.rawName == "item.beedronege" ) then
- memoryChest.pushItem("east",i)
- print("P")
- else
- print("B")
- --print(JSON:encode(data["beeInfo"]))
- --print(data["beeInfo"]["isAnalyzed"])
- if data["beeInfo"]["isAnalyzed"] == false then
- print("Analyzing: ".. i)
- analyzer.pullItemIntoSlot("up",i,64,3)
- while analyzer.pushItem("up",9,61,i) == 0 do sleep(1) end
- data = memoryChest.getStackInSlot(i)
- end
- -- Check if it's a drone or princess
- allBeeData[i] = {}
- if string.find(data.rawName, "princess") then
- allBeeData[i]["type"] = "princess"
- else
- allBeeData[i]["type"] = "drone"
- end
- -- Active values
- for key, value in pairs (data.beeInfo.active) do
- if key == "species" then
- allBeeData[i]["speciesPrimary"] = value
- else
- allBeeData[i][key] = value
- end
- end
- -- Inactive values
- allBeeData[i]["speciesSecondary"] = data.beeInfo.inactive.species
- allBeeData[i]["speciesPrimary"] = fixName(allBeeData[i]["speciesPrimary"])
- allBeeData[i]["speciesSecondary"] = fixName(allBeeData[i]["speciesSecondary"])
- if allBeeData[i]["type"] == "princess" then
- if i ~= 1 then
- memoryChest.swapStacks(i,1)
- end
- princessSlot = 1
- end
- end
- -- check for untargeted species
- if allBeeData[i] and (not bees[allBeeData[i]["speciesPrimary"]].targeted or not bees[allBeeData[i]["speciesSecondary"]].targeted) then
- memoryChest.pushItem("east",i)
- allBeeData[i] = nil
- end
- end
- if princessSlot > 0 then
- logLine("compacting drones...")
- for i = 2, memoryChest.getInventorySize() do
- if not allBeeData[i] then
- for j = i + 1, memoryChest.getInventorySize() do
- if allBeeData[j] then
- memoryChest.swapStacks(i, j)
- allBeeData[i], allBeeData[j] = allBeeData[j], allBeeData[i]
- break
- end
- end
- end
- end
- -- selection sort drones
- logLine("sorting drones...")
- for i = 2, memoryChest.getInventorySize() do
- highestScore = i
- if allBeeData[i] then
- allBeeData[i].score = scoreBee(allBeeData[princessSlot], allBeeData[i])
- for j = i + 1, memoryChest.getInventorySize() do
- if allBeeData[j] then
- if not allBeeData[j].score then
- allBeeData[j].score = scoreBee(allBeeData[princessSlot], allBeeData[j])
- end
- if allBeeData[j].score > allBeeData[highestScore].score then
- highestScore = j
- end
- end
- end
- -- swap bees
- if highestScore ~= i then
- memoryChest.swapStacks(i, highestScore)
- allBeeData[i], allBeeData[highestScore] = allBeeData[highestScore], allBeeData[i]
- end
- end
- end
- printHeader()
- allBeeData[princessSlot].slot = 1
- printBee(allBeeData[princessSlot])
- for i = 2, memoryChest.getInventorySize() do
- if allBeeData[i] then
- allBeeData[i].slot = i
- printBee(allBeeData[i])
- end
- end
- end
- logLine()
- end
- function scoreBee(princessData, droneData)
- local droneSpecies = {droneData["speciesPrimary"], droneData["speciesSecondary"]}
- -- check for untargeted species
- if not bees[droneSpecies[1]].targeted or not bees[droneSpecies[2]].targeted then
- return 0
- end
- local princessSpecies = {princessData["speciesPrimary"], princessData["speciesSecondary"]}
- local score
- local maxScore = 0
- for _, combo in ipairs({{princessSpecies[1], droneSpecies[1]}
- ,{princessSpecies[1], droneSpecies[2]}
- ,{princessSpecies[2], droneSpecies[1]}
- ,{princessSpecies[2], droneSpecies[2]}}) do
- -- find maximum score for each combo
- score = (bees[combo[1]].score + bees[combo[2]].score) / 2
- for name, beeData in pairs(bees) do
- if beeData.targeted then
- for i, parents in ipairs(beeData.mutateFrom) do
- if combo[1] == parents[1] and combo[2] == parents[2]
- or combo[2] == parents[1] and combo[1] == parents[2] then
- score = (score + beeData.score) / 2
- end
- end
- end
- end
- maxScore = maxScore + score
- end
- -- add one for each combination that results in the maximum score
- score = maxScore
- -- score attributes
- score = score + math.max(scoresFertility[droneData["fertility"]], scoresFertility[princessData["fertility"]])
- score = score + math.min(scoresSpeed[tostring(droneData["speed"])], scoresSpeed[tostring(princessData["speed"])])
- if droneData["nocturnal"] or princessData["nocturnal"] then score = score + scoresAttrib["nocturnal"] end
- if droneData["tolerantFlyer"] or princessData["tolerantFlyer"] then score = score + scoresAttrib["tolerantFlyer"] end
- if droneData["caveDwelling"] or princessData["caveDwelling"] then score = score + scoresAttrib["caveDwelling"] end
- score = score + math.max(scoresTolerance[string.upper(droneData["temperatureTolerance"])], scoresTolerance[string.upper(princessData["temperatureTolerance"])])
- score = score + math.max(scoresTolerance[string.upper(droneData["humidityTolerance"])], scoresTolerance[string.upper(princessData["humidityTolerance"])])
- return score
- end
- function printHeader()
- logLine()
- logLine("sl t species f spd n f c tmp hmd score ")
- logLine("--|-|-------|-|---|-|-|-|---|---|------")
- end
- -- string constants for console output
- toleranceString = {
- ["NONE"] = " - ",
- ["UP 1"] = " +1 ",
- ["UP 2"] = " +2 ",
- ["UP 3"] = " +3 ",
- ["UP 4"] = " +4 ",
- ["UP 5"] = " +5 ",
- ["DOWN 1"] = " -1 ",
- ["DOWN 2"] = " -2 ",
- ["DOWN 3"] = " -3 ",
- ["DOWN 4"] = " -4 ",
- ["DOWN 5"] = " -5 ",
- ["BOTH 1"] = "+-1 ",
- ["BOTH 2"] = "+-2 ",
- ["BOTH 3"] = "+-3 ",
- ["BOTH 4"] = "+-4 ",
- ["BOTH 5"] = "+-5 "
- }
- speedString = {
- ["0.3"] = "0.3", -- Slowest
- ["0.6"] = "0.6", -- Slower
- ["0.8"] = "0.8", -- Slow
- ["1"] = "1.0", -- Normal
- ["1.2"] = "1.2", -- Fast
- ["1.4"] = "1.4", -- Faster
- ["1.7"] = "1.7" -- Fastest
- }
- function printBee(beeData)
- log(beeData["slot"] < 10 and " "..beeData["slot"].." " or beeData["slot"].." ")
- -- type
- log(beeData["type"] == "princess" and "P " or "D ")
- -- species
- log(beeData["speciesPrimary"]:gsub("bees%.species%.",""):sub(1,3)..":"..beeData["speciesSecondary"]:gsub("bees%.species%.",""):sub(1,3).." ")
- -- fertility
- log(tostring(beeData["fertility"]).." ")
- -- speed
- log(speedString[tostring(beeData["speed"])].." ")
- -- nocturnal
- log(beeData["nocturnal"] and "X " or "- ")
- -- flyer
- log(beeData["tolerantFlyer"] and "X " or "- ")
- -- cave dwelling
- log(beeData["caveDwelling"] and "X " or "- ")
- -- temperature tolerance
- log(toleranceString[string.upper(beeData["temperatureTolerance"])])
- -- humidity tolerance
- log(toleranceString[string.upper(beeData["humidityTolerance"])])
- -- score
- log(beeData.score and string.format("%5.1d", beeData.score).." " or " ")
- logLine()
- end
- function dropExcess()
- logLine("dropping excess...")
- for i = ( memoryChest.getInventorySize() - 9 ), memoryChest.getInventorySize() do
- -- drop drones over ( max inv - 9 ) to clear space for newly bred bees and product
- memoryChest.pushItem("east",i)
- allBeeData[i] = nil
- end
- end
- function isPurebred(princessData, droneData)
- -- check if princess and drone are exactly the same and no chance for mutation
- if princessData["speciesPrimary"] ~= princessData["speciesSecondary"] then
- return false
- end
- for key, value in pairs(princessData) do
- if value ~= droneData[key] and key ~= "territory" and key ~= "type" and key ~= "slot" then
- return false
- end
- end
- return true
- end
- function getUnknown(princessData, droneData)
- -- lists species that are not in the bee graph
- local unknownSpecies = {}
- if not bees[princessData["speciesPrimary"]] then
- table.insert(unknownSpecies, princessData["speciesPrimary"])
- end
- if not bees[princessData["speciesSecondary"]] then
- table.insert(unknownSpecies, princessData["speciesSecondary"])
- end
- for _, beeData in pairs(droneData) do
- if not bees[beeData["speciesPrimary"]] then
- table.insert(unknownSpecies, beeData["speciesPrimary"])
- end
- if not bees[beeData["speciesSecondary"]] then
- table.insert(unknownSpecies, beeData["speciesSecondary"])
- end
- end
- return unknownSpecies
- end
- -- targeting -------------------------------------------------------------------
- -- set species and all parents to targeted
- function targetBee(name)
- local bee = bees[name]
- if bee and not bee.targeted then
- bee.targeted = true
- for i, parents in ipairs(bee.mutateFrom) do
- for j, parent in ipairs(parents) do
- targetBee(parent)
- end
- end
- end
- end
- -- set bee graph entry to targeted if species was specified on the command line
- -- otherwise set all entries to targeted
- tArgs = { ... }
- if #tArgs > 0 then
- logLine("targeting bee species:")
- for i, target in ipairs(tArgs) do
- targetBee(target)
- for name, data in pairs(bees) do
- if data.targeted and data.score > 1 then
- logLine(name .. string.rep(" ", 20-#name), data.score)
- end
- end
- end
- else
- for _, beeData in pairs(bees) do
- beeData.targeted = true
- end
- end
- -- breeding loop ---------------------------------------------------------------
- logLine("Clearing system...")
- clearSystem()
- redstone.setOutput("bottom", true)
- while true do
- analyzeBees()
- if allBeeData[princessSlot] then
- if isPurebred(allBeeData[princessSlot], allBeeData[2]) then
- logLine("Bees are purebred")
- redstone.setOutput("bottom", false)
- break
- end
- if true then break end
- memoryChest.pushItemIntoSlot("north",1,1,1)
- memoryChest.pushItemIntoSlot("north",2,1,2)
- allBeeData[1] = nil
- if not memoryChest.getAllStacks()[2] then
- allBeeData[2] = nil
- end
- dropExcess()
- end
- getBees()
- end
- logFile.close()
Advertisement
Add Comment
Please, Sign In to add comment