Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------
- -- AgriCraftCropBreeder by ExaByte --
- -------------------------------------
- fuelSlot = 1
- bonemealSlot = 2
- analyzerSlot = 3
- cropStickSlot = 4
- fruitSlot = 5
- bestSeedSlot = 16
- analyzerSide = nil -- where the analyzer can find crops
- useBonemealOnParents = true
- useBonemealOnCrossCrops = false
- bestSeedStats = {}
- nextBestSeedStats = {}
- -- movement
- function u()
- while not turtle.up() do refuel()sleep(1) end
- end
- function f()
- while not turtle.forward() do refuel()sleep(1) end
- end
- function d()
- while not turtle.down() do refuel()sleep(1) end
- end
- function b()
- while not turtle.back() do refuel()sleep(1) end
- end
- function l()
- turtle.turnLeft()
- end
- function r()
- turtle.turnRight()
- end
- function place(slotOrName)
- select(slotOrName)
- while not turtle.place() do sleep(1) end
- end
- function placeDown(slotOrName)
- select(slotOrName)
- while not turtle.placeDown() do sleep(1) end
- end
- function placeUp(slotOrName)
- select(slotOrName)
- while not turtle.placeUp() do sleep(1) end
- end
- -- inventory management
- -- count item
- function countItem(itemName)
- local count = 0
- for i = 1,16 do
- local data = turtle.getItemDetail(i)
- if data then
- if data.name == itemName then
- count = count + data.count
- end
- end
- end
- return count
- end
- -- time efficient slot selection
- function selectSlot(num)
- if (turtle.getSelectedSlot() ~= num) then
- turtle.select(num)
- end
- end
- function select(itemNameOrSlot)
- if type(itemNameOrSlot) == "number" then
- if not (turtle.getSelectedSlot() == itemNameOrSlot) then
- turtle.select(itemNameOrSlot)
- end
- elseif itemNameOrSlot == "emptySlot" then
- local found = false
- for i = 1,16 do
- if turtle.getItemCount(i) == 0 then
- turtle.select(i)
- found = true
- break
- end
- end
- if not found then
- error("select(\"emptySlot\"): no empty slot found!")
- end
- else
- local data = turtle.getItemDetail()
- if data then
- if data.name == itemNameOrSlot then
- return
- end
- end
- local found = false
- for i = 1, 16, 1 do
- data = turtle.getItemDetail(i)
- if data then
- if data.name == itemNameOrSlot then
- turtle.select(i)
- found = true
- break
- end
- end
- end
- if not found then
- print("could not find item of type \"" .. itemNameOrSlot .. "\" - please insert!")
- while found == false do
- local data
- -- local found = false
- for i = 1, 16, 1 do
- data = turtle.getItemDetail(i)
- if data then
- if data.name == itemNameOrSlot then
- turtle.select(i)
- found = true
- break
- end
- end
- end
- sleep (2)
- end
- print("thanks for the supplies!")
- end
- end
- end
- -- fuel management
- -- on the go refuel
- function refuel()
- local wasNoFuel = false
- while (turtle.getFuelLevel() < 1) do
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.refuel(0) then
- turtle.refuel()
- end
- end
- end
- if (turtle.getFuelLevel() < 1) then
- print("no fuel! please insert some!")
- wasNoFuel = true
- end
- end
- if (wasNoFuel) then
- print("refueled")
- end
- end
- -- check for fuel
- function checkGetFuel()
- local fuelLevel = turtle.getFuelLevel()
- if (fuelLevel <= 512) then
- refuel() -- refuel from inventory
- if (fuelLevel <= 512) then
- print("low on fuel (less than 512): taking some from chest...")
- l()
- select("emptySlot")
- local written = false
- while (turtle.suck() == false) do
- if not written then
- written = true
- print("fuel chest is now empty, please refill")
- end
- sleep(1)
- end
- turtle.refuel()
- r()
- end
- end
- end
- -- find analyzer in inventory
- function findAnalyzer()
- select("AgriCraft:peripheral")
- end
- -- wrap computer controlled seed analyzer on bottom
- function connectAnalyzer(dir)
- analyzer = peripheral.wrap(dir)
- while (not analyzer) do
- analyzer = peripheral.wrap(dir)
- if (not analyzer) then
- print("Could not find computer controlled seed analyzer on side <" .. dir .. ">! Please place it there!")
- sleep(1)
- end
- end
- end
- -- get bonemeal from chest to the right
- function getBonemeal()
- local toGet = 64 - turtle.getItemCount(bonemealSlot)
- r()
- selectSlot(bonemealSlot)
- local written = false
- while (not turtle.suck(toGet)) or (turtle.getItemCount(bonemealSlot) <= 1) do
- if not written then
- written = true
- print("bonemeal chest is empty, please refill")
- end
- sleep(1)
- end
- l()
- end
- -- determine analyzerSide
- function determineAnalyzerSide()
- b()
- place("AgriCraft:peripheral")
- connectAnalyzer("front")
- while (analyzerSide == nil) do
- if pcall(function ()analyzer.hasPlant("NORTH")end) then
- analyzerSide = "NORTH"
- elseif pcall(function ()analyzer.hasPlant("EAST")end) then
- analyzerSide = "EAST"
- elseif pcall(function ()analyzer.hasPlant("SOUTH")end) then
- analyzerSide = "SOUTH"
- elseif pcall(function ()analyzer.hasPlant("WEST")end) then
- analyzerSide = "WEST"
- else
- print("Error while determining analyzerSide, there must be crop sticks next to the seed analyzer")
- sleep (1)
- end
- end
- turtle.dig()
- f()
- end
- -- use the seed analyzer to check whether the seed grew
- function checkSeedGrew()
- b()
- place("AgriCraft:peripheral")
- connectAnalyzer("front")
- --[[if analyzerSide == nil then
- determineAnalyzerSide()
- end]]
- local seedGrew = analyzer.hasPlant(analyzerSide)
- turtle.dig()
- f()
- return seedGrew
- end
- -- make sure turtle has at least 2 crop sticks to start with
- function checkCropSticks()
- local written = false
- local data = turtle.getItemDetail(4)
- while true do
- data = turtle.getItemDetail(4)
- if data then
- -- if crop sticks present, break
- if (data.name == "AgriCraft:cropsItem" and data.count >= 2) then
- break
- end
- end
- if not written then
- print("please insert initial 3 (or more) crop sticks in slot 4")
- written = true
- end
- sleep(1)
- end
- end
- -- bonemeal parent crop until fully grown
- function bonemealParent()
- -- only do this if useBonemealOnParents flag is set to true
- if useBonemealOnParents then
- local mature = false
- while not mature do
- local success, data = turtle.inspectDown()
- if success then
- if data.metadata == 7 then
- mature = true
- else
- placeDown(bonemealSlot)
- end
- end
- end
- end
- end
- -- analyzes farmed seeds and throws bad ones away
- function analyzeSeeds(dir)
- placeUp("AgriCraft:peripheral")
- connectAnalyzer("top")
- local aSlot = turtle.getSelectedSlot()
- local data
- for i = 1,16 do
- data = turtle.getItemDetail(i)
- if data then
- if string.find(data.name, "seed") then
- -- case: this item name contains "AgriCraft:seed"
- selectSlot(i)
- turtle.dropUp()
- -- analyze seeds
- while not analyzer.isAnalyzed() do
- analyzer.analyze()
- sleep(0.2)
- end
- local stats = { analyzer.getSpecimenStats() }
- if ( ( stats[1] == 10 or stats[1] >= bestSeedStats[1] + 1 )
- and ( stats[2] == 10 or stats[2] >= bestSeedStats[2] + 1 )
- and ( stats[3] == 10 or stats[3] >= bestSeedStats[3] + 1 )) then
- -- set nextBestSeedStats to this
- nextBestSeedStats = { stats[1], stats[2], stats[3] }
- -- best possible crop for this iteration reached, transfer to bestSeedSlot
- selectSlot(bestSeedSlot)
- turtle.suckUp()
- -- if there are more than 2 seeds in slot, transfer exceeding to old slot
- if (turtle.getItemCount(bestSeedSlot) > 2) then
- turtle.transferTo(i, turtle.getItemCount(bestSeedSlot)-2)
- end
- else
- -- suck this damn unworthy seed
- turtle.suckUp()
- end
- end
- end
- end
- -- break analyzer
- select(aSlot)
- turtle.digUp()
- return stats
- end
- -- dumps unworthy seeds to an inventory below the starting place
- function dumpSeeds()
- local data
- for i = 1,15 do
- data = turtle.getItemDetail(i)
- if data then
- if string.find(data.name, "seed") then
- -- case: this item name contains "AgriCraft:seed"
- selectSlot(i)
- turtle.dropDown()
- end
- end
- end
- end
- -- place analyzer down and use it to watch growth stage
- function checkParentGrowthWithAnalyzer()
- if not useBonemealOnParents then
- -- local table for analyzer directions for plant stats
- local dir1, dir2
- if analyzerSide == "WEST" then dir1 = "NORTH"; dir2 = "EAST"
- elseif analyzerSide == "NORTH" then dir1 = "EAST"; dir2 = "SOUTH"
- elseif analyzerSide == "EAST" then dir1 = "SOUTH"; dir2 = "WEST"
- elseif analyzerSide == "SOUTH" then dir1 = "WEST"; dir2 = "NORTH"
- end
- placeDown("AgriCraft:peripheral")
- connectAnalyzer("bottom")
- print("waiting for parent plants to grow fully")
- while not (analyzer.isMature(dir1) and analyzer.isMature(dir2)) do
- sleep(2)
- end
- print("parent plants fully grown...")
- turtle.digDown()
- end
- end
- -- grow new seeds with bonemeal and break them
- function growBreakSeeds()
- local success, data = turtle.inspect()
- -- wait until there are crop sticks placed
- local msec = 1
- while not success do
- success, data = turtle.inspect()
- -- check interval increments up to 2s
- sleep(math.min(msec/1000, 2000))
- msec = msec*2
- end
- if success then
- if useBonemealOnCrossCrops then
- repeat
- -- get bonemeal when needed
- if turtle.getItemCount(bonemealSlot) <= 1 then
- getBonemeal()
- end
- selectSlot(bonemealSlot)
- turtle.place()
- until checkSeedGrew()
- else
- -- use analyzer to wait for new seeds to grow
- b()
- place("AgriCraft:peripheral")
- connectAnalyzer("front")
- while not pcall (function() analyzer.hasPlant(analyzerSide) end) do
- print("the autonomous activator might be out of crop sticks, please refill")
- sleep(2)
- end
- local written = false
- while not analyzer.hasPlant(analyzerSide) do
- if not written then
- print("waiting for seed to grow")
- written = true
- end
- sleep(1)
- end
- -- get analyzer
- turtle.dig()
- f()
- end
- -- dig seed up
- select(cropStickSlot)
- turtle.dig()
- end
- end
- -- uses analyzer to get stats of already planted parent seeds
- function getParentStats()
- -- local table for analyzer directions for plant stats
- local dir1, dir2
- if analyzerSide == "WEST" then dir1 = "NORTH"; dir2 = "EAST"
- elseif analyzerSide == "NORTH" then dir1 = "EAST"; dir2 = "SOUTH"
- elseif analyzerSide == "EAST" then dir1 = "SOUTH"; dir2 = "WEST"
- elseif analyzerSide == "SOUTH" then dir1 = "WEST"; dir2 = "NORTH"
- end
- placeDown("AgriCraft:peripheral")
- connectAnalyzer("bottom")
- local stats = { analyzer.getSpecimenStats(dir2) }
- turtle.digDown()
- return stats
- end
- -- place analyzer in direction <dir> and get the seed stats from selected slot, put these in <stats> (table) [usage: local stats = getSeedStats(dir, slot);
- function getSeedStats(dir, slot)
- -- place analyzer
- if dir == "top" then
- placeUp("AgriCraft:peripheral")
- elseif dir == "front" then
- place("AgriCraft:peripheral")
- elseif dir == "bottom" then
- placeDown("AgriCraft:peripheral")
- else
- print("Error: getSeedStats(dir, stats) doesn't accept <" .. dir .. "> as dir!") return
- end
- local lyzerSlot = turtle.getSelectedSlot()
- connectAnalyzer(dir)
- selectSlot(slot)
- if dir == "top" then
- turtle.dropUp()
- elseif dir == "front" then
- turtle.drop()
- elseif dir == "bottom" then
- turtle.dropDown()
- end
- while not analyzer.isAnalyzed() do
- analyzer.analyze()
- sleep(0.2)
- end
- local s1,s2,s3 = analyzer.getSpecimenStats()
- local stats = { s1,s2,s3 }
- -- retrieve seeds from analyzer, then get the analyzer itself
- if dir == "top" then
- turtle.suckUp()
- elseif dir == "front" then
- turtle.suck()
- elseif dir == "bottom" then
- turtle.suckDown()
- end
- select(lyzerSlot)
- if dir == "top" then
- turtle.digUp()
- elseif dir == "front" then
- turtle.dig()
- elseif dir == "bottom" then
- turtle.digDown()
- end
- return stats
- end
- -- unloadsFruits
- function checkUnoadFruits()
- local limitReached = false
- for i = 1,16 do
- local data = turtle.getItemDetail(i)
- if data then
- if not ( data.name == "AgriCraft:peripheral" or
- data.name == "AgriCraft:cropsItem" or
- (data.name == "minecraft:dye" and data.damage == 15) or
- string.find(data.name, "seed")) then
- -- all that is in turtle inventory and not covered by this list above is considered a fruit
- --if turtle.getItemSpace(i) < 3 then -- removed for clearing every time
- limitReached = true
- break
- --end
- end
- end
- end
- if limitReached then
- b()
- for i = 1,16 do
- -- if one is to much dump all
- if limitReached then
- data = turtle.getItemDetail(i)
- if data then
- if not ( data.name == "AgriCraft:peripheral" or
- data.name == "AgriCraft:cropsItem" or
- (data.name == "minecraft:dye" and data.damage == 15) or
- string.find(data.name, "seed")) then
- -- all that is in turtle inventory and not covered by this list above is considered a fruit
- select(i)
- while not turtle.dropDown() do sleep(1) end
- end
- end
- end
- end
- f()
- end
- end
- -- check unload cropSticks
- function checkUnloadCropSticks()
- local count = countItem("AgriCraft:cropsItem")
- if (count >= 52) then
- -- goto unload point1
- u()f()r()f()
- -- select last slot with crop sticks in it
- local data
- local continue = true
- local goToNextUnload = false
- while countItem("AgriCraft:cropsItem") > 3 and continue do
- for i = 16,1,-1 do
- data = turtle.getItemDetail(i)
- if data then
- if data.name == "AgriCraft:cropsItem" then
- selectSlot(i)
- if not (turtle.dropDown(math.max(math.min(64, count-3)), 0)) then
- -- turtle couldn't unlaod stuff anymore ( autonomous activator inventory is full )
- goToNextUnload = true
- continue = false
- break
- end
- count = countItem("AgriCraft:cropsItem")
- end
- end
- end
- end
- if goToNextUnload then
- -- turtle must unload at unload spot 2:
- -- goto unload point2
- l()f()
- while countItem("AgriCraft:cropsItem") > 3 do
- for i = 16,1,-1 do
- data = turtle.getItemDetail(i)
- if data then
- if data.name == "AgriCraft:cropsItem" then
- selectSlot(i)
- while not (turtle.dropDown(math.max(math.min(64, count-3)), 0)) do
- -- turtle couldn't unlaod stuff anymore ( unload spot 2 is full )
- sleep(1)
- end
- count = countItem("AgriCraft:cropsItem")
- end
- end
- end
- end
- b()r()
- end
- -- go back
- b()l()b()d()
- end
- end
- -- selects a slot that contains a seed with the specified stats by using a on top placed analyzer
- function selectSeedWithStats(dstats)
- placeUp("AgriCraft:peripheral")
- connectAnalyzer("top")
- local aSlot = turtle.getSelectedSlot()
- local data
- local sSlot
- for i = 1,16 do
- data = turtle.getItemDetail(i)
- if data then
- if string.find(data.name, "seed") then
- -- case: this item name contains "AgriCraft:seed"
- selectSlot(i)
- turtle.dropUp()
- -- analyze seeds
- while not analyzer.isAnalyzed() do
- analyzer.analyze()
- sleep(0.2)
- end
- local stats = { analyzer.getSpecimenStats() }
- if (stats[1] == dstats[1] and stats[2] == dstats[2] and stats[3] == dstats[3]) then
- turtle.suckUp()
- sSlot = i
- break
- else
- -- suck this damn unworthy seed
- turtle.suckUp()
- end
- end
- end
- end
- -- break analyzer
- select(aSlot)
- turtle.digUp()
- if sSlot ~= nil then
- select(sSlot)
- end
- end
- -- replaces old parents with better new ones
- function replaceParents()
- getBonemeal()
- u()f()l()f()
- -- replace old first parent with fresh crop sticks
- select(cropStickSlot)
- turtle.digDown()
- placeDown(cropStickSlot)
- b()r()f()
- -- replace old first parent with fresh crop sticks
- turtle.digDown()
- placeDown(cropStickSlot)
- b()
- -- break old middle plant
- select(cropStickSlot)
- turtle.digDown()
- l()f()f()
- select(bestSeedSlot)
- -- plant the first parent
- turtle.dropDown(1)
- redstone.setOutput("bottom", true)
- sleep(1)
- redstone.setOutput("bottom", false)
- b()
- bonemealParent()
- -- grow second parent from first
- b()r()b()d()
- -- this should give 1 seed
- growBreakSeeds()
- -- this puts the 2nd parent into slot 16
- selectSeedWithStats(bestSeedStats)
- u()f()f()f()
- -- plant the 2nd parent
- turtle.dropDown(1)
- redstone.setOutput("bottom", true)
- sleep(1)
- redstone.setOutput("bottom", false)
- b()
- bonemealParent()
- if not useBonemealOnParents then
- l()f()
- checkParentGrowthWithAnalyzer()
- b()r()
- end
- b()b()d()
- end
- -- check initial plants and place and grow them when there are none, also use analyzer on them to get stats
- function checkInitialPlants()
- u()f()l()f()
- local numCropSticksNeeded = 0
- -- check if theres already a crop planted
- if not turtle.detectDown() then
- -- case: no plant present
- if countItem("AgriCraft:cropsItem") >= 1 then
- r()
- -- get best seed stats from analyzer
- bestSeedStats = getSeedStats("front", bestSeedSlot)
- l()b()r()b()d()
- replaceParents()
- else
- print("now broken: needed crop sticks, had none")
- end
- else
- -- case: plant present
- r()f()l()
- local stats = getParentStats()
- bestSeedStats = { stats[1], stats[2], stats[3] }
- b()r()b()b()d()
- end
- end
- -- breaks remaining plants after 10 10 10 crop reached and dumps them into chest
- function clearFieldAndInventory()
- checkUnloadCropSticks()
- u()f()l()f()
- selectSlot(cropStickSlot)
- turtle.digDown()
- b()r()f()
- turtle.digDown()
- b()
- turtle.digDown()
- b()d()
- dumpSeeds()
- checkUnoadFruits()
- end
- -- unloads 10 10 10 seed in inventory 1 back and 1 left from starting point
- function unloadBestSeed()
- u()l()
- selectSlot(bestSeedSlot)
- local written = false
- while not turtle.drop() do
- if not written then
- print("inventory on front is full... waiting")
- end
- sleep(1)
- end
- r()d()
- end
- -- gets a new seed to enhance from inventory 1 back and 1 left from starting point
- function pickUpNewSeed()
- u()r()
- selectSlot(bestSeedSlot)
- local written = false
- print("turtle.suck()")
- while not turtle.suck() do
- if not written then
- print("waiting for new seed in inventory to the front")
- end
- sleep(1)
- end
- l()d()
- end
- ----------
- -- main --
- ----------
- checkGetFuel()
- checkCropSticks()
- findAnalyzer()
- determineAnalyzerSide()
- checkInitialPlants()
- analyzeSeeds()
- dumpSeeds()
- while true do
- while not (nextBestSeedStats[1] == 10 and nextBestSeedStats[2] == 10 and nextBestSeedStats[3] == 10 and turtle.getItemCount(bestSeedSlot) == 1) do
- while (not (turtle.getItemCount(bestSeedSlot) == 1)) do
- growBreakSeeds()
- analyzeSeeds()
- dumpSeeds()
- end
- checkUnoadFruits()
- checkUnloadCropSticks()
- checkGetFuel()
- if not (nextBestSeedStats[1] == 10 and nextBestSeedStats[2] == 10 and nextBestSeedStats[3] == 10 and turtle.getItemCount(bestSeedSlot) == 1) then
- bestSeedStats = nextBestSeedStats
- getBonemeal()
- replaceParents()
- end
- end
- clearFieldAndInventory()
- unloadBestSeed()
- print("finished: got 10 10 10 seed in chest")
- pickUpNewSeed()
- bestSeedStats = getSeedStats("top", bestSeedSlot)
- nextBestSeedStats = { math.min(bestSeedStats[1]+1, 10), math.min(bestSeedStats[2]+1, 10), math.min(bestSeedStats[3]+1, 10) }
- print("bestSeedStats = "..bestSeedStats[1]..bestSeedStats[2]..bestSeedStats[3])
- print("nextBestSeedStats = "..nextBestSeedStats[1]..nextBestSeedStats[2]..nextBestSeedStats[3])
- replaceParents()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement