Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Seed Breeder Program for AgriCraft
- -- Created by ame824
- -- This program automates breeding seeds in AgriCraft using a turtle.
- -- It scans and compares plant genes, replaces lower-scoring parents with better offspring,
- -- and continues until a perfect seed (score 120) is achieved.
- -- Please respect the original author and do not republish this as your own!
- -- Peripheral setup: Monitor is wrapped on the back for displaying information.
- mon = peripheral.wrap("back")
- -- Constants for item names used in the program.
- local cropsticks = "agricraft:wooden_crop_sticks"
- local seed = "agricraft:seed"
- -- Variables to store plant data and scores.
- local parent0, parent1, child, score0, score1, scoreChild, cropstickSlot, seedSlot
- -- Flags for program state.
- local initialized = false
- local notFinished = true
- -- Reset the monitor and set initial cursor position.
- mon.clear()
- mon.setCursorPos(1,1)
- local line = 1
- -- Function to write a line of text to the monitor and increment the line counter.
- function writeLine(text)
- mon.setCursorPos(1, line)
- mon.write(text)
- line = line + 1
- end
- -- Function to print a table recursively to the monitor with indentation for nested structures.
- function printTable(t, indent)
- indent = indent or 0
- local prefix = string.rep(" ", indent)
- for k, v in pairs(t) do
- if type(v) == "table" then
- writeLine(prefix .. tostring(k) .. " = {")
- printTable(v, indent + 1)
- writeLine(prefix .. "}")
- else
- writeLine(prefix .. tostring(k) .. " = " .. tostring(v))
- end
- end
- end
- function scan(direction)
- if direction == "left" then
- turtle.turnLeft()
- sleep(0.5)
- turtle.forward()
- sleep(0.5)
- local crop = peripheral.wrap("bottom")
- local data = crop.getBlockData()
- turtle.back()
- sleep(0.5)
- turtle.turnRight()
- sleep(0.5)
- return data
- elseif direction == "right" then
- turtle.turnRight()
- sleep(0.5)
- turtle.forward()
- sleep(0.5)
- local crop = peripheral.wrap("bottom")
- local data = crop.getBlockData()
- turtle.back()
- sleep(0.5)
- turtle.turnLeft()
- sleep(0.5)
- return data
- elseif direction == "mid" then
- local crop = peripheral.wrap("bottom")
- if not crop then
- for i=1,16 do
- local scan = turtle.getItemDetail(i)
- if scan ~= nil then
- if scan.name == cropsticks then
- cropstickSlot = i
- end
- end
- end
- turtle.select(cropstickSlot)
- turtle.placeDown()
- sleep(1)
- turtle.placeDown()
- sleep(1)
- end
- sleep(0.5)
- crop = peripheral.wrap("bottom")
- return crop.getBlockData()
- else
- error("Wrong direction!")
- end
- end
- function replace(direction)
- if direction == "left" then
- turtle.digDown()
- sleep(1)
- for i=1,16 do
- local scan = turtle.getItemDetail(i)
- if scan ~= nil then
- if scan.name == cropsticks then cropstickSlot = i end
- if scan.name == seed then seedSlot = i end
- end
- end
- turtle.turnLeft()
- sleep(0.5)
- turtle.forward()
- sleep(0.5)
- turtle.digDown()
- turtle.select(cropstickSlot)
- turtle.placeDown()
- sleep(0.5)
- turtle.select(seedSlot)
- turtle.placeDown()
- sleep(0.5)
- turtle.back()
- sleep(0.5)
- turtle.turnRight()
- sleep(0.5)
- for i=1,16 do
- local scan = turtle.getItemDetail(i)
- if scan ~= nil then
- if scan.name == seed then seedSlot = i end
- end
- end
- turtle.select(seedSlot)
- turtle.drop()
- elseif direction == "right" then
- turtle.digDown()
- sleep(1)
- for i=1,16 do
- local scan = turtle.getItemDetail(i)
- if scan ~= nil then
- if scan.name == cropsticks then cropstickSlot = i end
- if scan.name == seed then seedSlot = i end
- end
- end
- turtle.turnRight()
- sleep(0.5)
- turtle.forward()
- sleep(0.5)
- turtle.digDown()
- sleep(0.5)
- turtle.select(cropstickSlot)
- turtle.placeDown()
- sleep(0.5)
- turtle.select(seedSlot)
- turtle.placeDown()
- sleep(0.5)
- turtle.back()
- sleep(0.5)
- turtle.turnLeft()
- sleep(0.5)
- for i=1,16 do
- local scan = turtle.getItemDetail(i)
- if scan ~= nil then
- if scan.name == seed then seedSlot = i end
- end
- end
- turtle.select(seedSlot)
- turtle.drop()
- elseif direction == "mid" then
- turtle.digDown()
- sleep(1)
- for i=1,16 do
- local scan = turtle.getItemDetail(i)
- if scan ~= nil then
- if scan.name == seed then seedSlot = i end
- end
- end
- turtle.select(seedSlot)
- turtle.drop()
- else
- error("wrong direction")
- end
- end
- -- Function to calculate the gene score of a plant by summing recessive and dominant values for all genes.
- function geneScore(data)
- local g = data.genes
- return
- g.gain.rec + g.gain.dom +
- g.fertility.rec + g.fertility.dom +
- g.resistance.rec + g.resistance.dom +
- g.growth.rec + g.growth.dom +
- g.strength.rec + g.strength.dom +
- g.mutativity.rec + g.mutativity.dom
- end
- -- Function to compare parent and child scores, replace the lower-scoring parent, and check for completion.
- function compareCrops()
- score0 = geneScore(parent0)
- score1 = geneScore(parent1)
- scoreChild = geneScore(child)
- if score0 <= score1 then
- if score0 < scoreChild then
- print("Replacing left...")
- replace("left")
- parent0 = child
- else
- print("Replacing mid...")
- replace("mid")
- end
- elseif score0 > score1 then
- if score1 < scoreChild then
- print("Replacing right...")
- replace("right")
- parent1 = child
- else
- print("Replacing mid...")
- replace("mid")
- end
- else
- print("Replacing mid...")
- replace("mid")
- end
- score0 = geneScore(parent0)
- score1 = geneScore(parent1)
- if score0 == 120 then notFinished=false end
- if score1 == 120 then notFinished=false end
- end
- -- Initialization function: Scans initial parents and middle, calculates scores.
- function init()
- parent0 = scan("left")
- parent1 = scan("right")
- child = scan("mid")
- score0 = geneScore(parent0)
- score1 = geneScore(parent1)
- initialized = true
- end
- -- Function to display parent gene information on the monitor.
- function printParents()
- mon.clear()
- mon.setCursorPos(1,1)
- line = 1 -- Reset the line counter.
- local genes0 = parent0.genes
- writeLine("Parent 0:")
- writeLine("1. Gain: " .. genes0.gain.rec .. " - " .. genes0.gain.dom)
- writeLine("2. Fertility: " .. genes0.fertility.rec .. " - " .. genes0.fertility.dom)
- writeLine("3. Resistance: " .. genes0.resistance.rec .. " - " .. genes0.resistance.dom)
- writeLine("4. Growth: " .. genes0.growth.rec .. " - " .. genes0.growth.dom)
- writeLine("5. Strength: " .. genes0.strength.rec .. " - " .. genes0.strength.dom)
- writeLine("6. Mutativity: " .. genes0.mutativity.rec .. " - " .. genes0.mutativity.dom)
- writeLine("Score: " .. score0)
- writeLine("") -- Empty line for spacing.
- local genes1 = parent1.genes
- writeLine("Parent 1:")
- writeLine("1. Gain: " .. genes1.gain.rec .. " - " .. genes1.gain.dom)
- writeLine("2. Fertility: " .. genes1.fertility.rec .. " - " .. genes1.fertility.dom)
- writeLine("3. Resistance: " .. genes1.resistance.rec .. " - " .. genes1.resistance.dom)
- writeLine("4. Growth: " .. genes1.growth.rec .. " - " .. genes1.growth.dom)
- writeLine("5. Strength: " .. genes1.strength.rec .. " - " .. genes1.strength.dom)
- writeLine("6. Mutativity: " .. genes1.mutativity.rec .. " - " .. genes1.mutativity.dom)
- writeLine("Score: " .. score1)
- end
- -- Main loop: Runs until a perfect seed is bred.
- while notFinished do
- if not initialized then
- print("First initializing...")
- init()
- end
- sleep(0.5)
- print("Bring Parent Infos on Monitor...")
- printParents()
- sleep(0.5)
- print("Waiting for Crop...")
- child = scan("mid")
- -- Waiting loop until a plant grows in the middle.
- while child.hasPlant == 0 do
- sleep(0.5)
- child = scan("mid")
- sleep(0.5)
- end
- sleep(0.5)
- print("Scanning...")
- scan("mid")
- sleep(0.5)
- print("Comparing...")
- compareCrops()
- sleep(0.5)
- end
- -- Display end message on the monitor when breeding is complete.
- mon.clear()
- mon.setCursorPos(1,1)
- line = 1
- writeLine("####################################")
- writeLine("## ##")
- writeLine("## ###### #### ## ###### ## ##")
- writeLine("## ## ##### ## ## ## ## ##")
- writeLine("## ###### ## ### ## ## ## ## ##")
- writeLine("## ## ## ##### ## ## ##")
- writeLine("## ###### ## #### ###### ## ##")
- writeLine("## ##")
- writeLine("####################################")
- print("seed is finished")
Advertisement
Add Comment
Please, Sign In to add comment