ame824

CC:Tweaked Turtle Breeder for AgriCraft

Mar 7th, 2026 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.66 KB | None | 0 0
  1. -- Turtle Seed Breeder Program for AgriCraft
  2. -- Created by ame824
  3. -- This program automates breeding seeds in AgriCraft using a turtle.
  4. -- It scans and compares plant genes, replaces lower-scoring parents with better offspring,
  5. -- and continues until a perfect seed (score 120) is achieved.
  6. -- Please respect the original author and do not republish this as your own!
  7.  
  8. -- Peripheral setup: Monitor is wrapped on the back for displaying information.
  9. mon = peripheral.wrap("back")
  10.  
  11. -- Constants for item names used in the program.
  12. local cropsticks = "agricraft:wooden_crop_sticks"
  13. local seed = "agricraft:seed"
  14.  
  15. -- Variables to store plant data and scores.
  16. local parent0, parent1, child, score0, score1, scoreChild, cropstickSlot, seedSlot
  17.  
  18. -- Flags for program state.
  19. local initialized = false
  20. local notFinished = true
  21.  
  22. -- Reset the monitor and set initial cursor position.
  23. mon.clear()
  24. mon.setCursorPos(1,1)
  25. local line = 1
  26.  
  27. -- Function to write a line of text to the monitor and increment the line counter.
  28. function writeLine(text)
  29.     mon.setCursorPos(1, line)
  30.     mon.write(text)
  31.     line = line + 1
  32. end
  33.  
  34. -- Function to print a table recursively to the monitor with indentation for nested structures.
  35. function printTable(t, indent)
  36.     indent = indent or 0
  37.     local prefix = string.rep(" ", indent)
  38.     for k, v in pairs(t) do
  39.         if type(v) == "table" then
  40.             writeLine(prefix .. tostring(k) .. " = {")
  41.             printTable(v, indent + 1)
  42.             writeLine(prefix .. "}")
  43.         else
  44.             writeLine(prefix .. tostring(k) .. " = " .. tostring(v))
  45.         end
  46.     end
  47. end
  48.  
  49. function scan(direction)
  50.     if direction == "left" then
  51.         turtle.turnLeft()
  52.         sleep(0.5)
  53.         turtle.forward()
  54.         sleep(0.5)
  55.         local crop = peripheral.wrap("bottom")
  56.         local data = crop.getBlockData()
  57.         turtle.back()
  58.         sleep(0.5)
  59.         turtle.turnRight()
  60.         sleep(0.5)
  61.         return data
  62.     elseif direction == "right" then
  63.         turtle.turnRight()
  64.         sleep(0.5)
  65.         turtle.forward()
  66.         sleep(0.5)
  67.         local crop = peripheral.wrap("bottom")
  68.         local data = crop.getBlockData()
  69.         turtle.back()
  70.         sleep(0.5)
  71.         turtle.turnLeft()
  72.         sleep(0.5)
  73.         return data    
  74.     elseif direction == "mid" then
  75.         local crop = peripheral.wrap("bottom")
  76.         if not crop then
  77.             for i=1,16 do
  78.                 local scan = turtle.getItemDetail(i)
  79.                 if scan ~= nil then
  80.                     if scan.name == cropsticks then
  81.                         cropstickSlot = i
  82.                     end
  83.                 end
  84.             end
  85.             turtle.select(cropstickSlot)
  86.             turtle.placeDown()
  87.             sleep(1)
  88.             turtle.placeDown()
  89.             sleep(1)
  90.         end
  91.         sleep(0.5)
  92.         crop = peripheral.wrap("bottom")
  93.         return crop.getBlockData()
  94.     else
  95.         error("Wrong direction!")
  96.     end
  97. end
  98.  
  99. function replace(direction)
  100.     if direction == "left" then
  101.         turtle.digDown()
  102.         sleep(1)
  103.         for i=1,16 do
  104.             local scan = turtle.getItemDetail(i)
  105.             if scan ~= nil then
  106.                 if scan.name == cropsticks then cropstickSlot = i end
  107.                 if scan.name == seed then seedSlot = i end
  108.             end
  109.         end
  110.         turtle.turnLeft()
  111.         sleep(0.5)
  112.         turtle.forward()
  113.         sleep(0.5)
  114.         turtle.digDown()
  115.         turtle.select(cropstickSlot)
  116.         turtle.placeDown()
  117.         sleep(0.5)
  118.         turtle.select(seedSlot)
  119.         turtle.placeDown()
  120.         sleep(0.5)
  121.         turtle.back()
  122.         sleep(0.5)
  123.         turtle.turnRight()
  124.         sleep(0.5)
  125.         for i=1,16 do
  126.             local scan = turtle.getItemDetail(i)
  127.             if scan ~= nil then
  128.                 if scan.name == seed then seedSlot = i end
  129.             end
  130.         end
  131.         turtle.select(seedSlot)
  132.         turtle.drop()
  133.     elseif direction == "right" then
  134.         turtle.digDown()
  135.         sleep(1)
  136.         for i=1,16 do
  137.             local scan = turtle.getItemDetail(i)
  138.             if scan ~= nil then
  139.                 if scan.name == cropsticks then cropstickSlot = i end
  140.                 if scan.name == seed then seedSlot = i end
  141.             end
  142.         end
  143.         turtle.turnRight()
  144.         sleep(0.5)
  145.         turtle.forward()
  146.         sleep(0.5)
  147.         turtle.digDown()
  148.         sleep(0.5)
  149.         turtle.select(cropstickSlot)
  150.         turtle.placeDown()
  151.         sleep(0.5)
  152.         turtle.select(seedSlot)
  153.         turtle.placeDown()
  154.         sleep(0.5)
  155.         turtle.back()
  156.         sleep(0.5)
  157.         turtle.turnLeft()
  158.         sleep(0.5)
  159.         for i=1,16 do
  160.             local scan = turtle.getItemDetail(i)
  161.             if scan ~= nil then
  162.                 if scan.name == seed then seedSlot = i end
  163.             end
  164.         end
  165.         turtle.select(seedSlot)
  166.         turtle.drop()
  167.     elseif direction == "mid" then
  168.         turtle.digDown()
  169.         sleep(1)
  170.         for i=1,16 do
  171.             local scan = turtle.getItemDetail(i)
  172.             if scan ~= nil then
  173.                 if scan.name == seed then seedSlot = i end
  174.             end
  175.         end
  176.         turtle.select(seedSlot)
  177.         turtle.drop()
  178.     else
  179.         error("wrong direction")
  180.     end
  181. end
  182.  
  183.  
  184. -- Function to calculate the gene score of a plant by summing recessive and dominant values for all genes.
  185. function geneScore(data)
  186.     local g = data.genes
  187.     return
  188.         g.gain.rec + g.gain.dom +
  189.         g.fertility.rec + g.fertility.dom +
  190.         g.resistance.rec + g.resistance.dom +
  191.         g.growth.rec + g.growth.dom +
  192.         g.strength.rec + g.strength.dom +
  193.         g.mutativity.rec + g.mutativity.dom
  194. end
  195.  
  196. -- Function to compare parent and child scores, replace the lower-scoring parent, and check for completion.
  197. function compareCrops()
  198.     score0 = geneScore(parent0)
  199.     score1 = geneScore(parent1)
  200.     scoreChild = geneScore(child)
  201.  
  202.     if score0 <= score1 then
  203.         if score0 < scoreChild then
  204.             print("Replacing left...")
  205.             replace("left")
  206.             parent0 = child
  207.         else            
  208.             print("Replacing mid...")
  209.             replace("mid")
  210.         end
  211.     elseif score0 > score1 then
  212.         if score1 < scoreChild then
  213.             print("Replacing right...")
  214.             replace("right")
  215.             parent1 = child
  216.         else            
  217.             print("Replacing mid...")
  218.             replace("mid")
  219.         end
  220.     else
  221.         print("Replacing mid...")
  222.         replace("mid")
  223.     end
  224.  
  225.     score0 = geneScore(parent0)
  226.     score1 = geneScore(parent1)
  227.  
  228.     if score0 == 120 then notFinished=false end
  229.     if score1 == 120 then notFinished=false end
  230. end
  231.  
  232. -- Initialization function: Scans initial parents and middle, calculates scores.
  233. function init()
  234.     parent0 = scan("left")
  235.     parent1 = scan("right")
  236.     child = scan("mid")
  237.     score0 = geneScore(parent0)
  238.     score1 = geneScore(parent1)
  239.     initialized = true
  240. end
  241.  
  242. -- Function to display parent gene information on the monitor.
  243. function printParents()
  244.     mon.clear()
  245.     mon.setCursorPos(1,1)
  246.     line = 1 -- Reset the line counter.
  247.    
  248.     local genes0 = parent0.genes
  249.     writeLine("Parent 0:")
  250.     writeLine("1. Gain: " .. genes0.gain.rec .. " - " .. genes0.gain.dom)
  251.     writeLine("2. Fertility: " .. genes0.fertility.rec .. " - " .. genes0.fertility.dom)
  252.     writeLine("3. Resistance: " .. genes0.resistance.rec .. " - " .. genes0.resistance.dom)
  253.     writeLine("4. Growth: " .. genes0.growth.rec .. " - " .. genes0.growth.dom)
  254.     writeLine("5. Strength: " .. genes0.strength.rec .. " - " .. genes0.strength.dom)
  255.     writeLine("6. Mutativity: " .. genes0.mutativity.rec .. " - " .. genes0.mutativity.dom)
  256.     writeLine("Score: " .. score0)
  257.     writeLine("") -- Empty line for spacing.
  258.    
  259.     local genes1 = parent1.genes
  260.     writeLine("Parent 1:")
  261.     writeLine("1. Gain: " .. genes1.gain.rec .. " - " .. genes1.gain.dom)
  262.     writeLine("2. Fertility: " .. genes1.fertility.rec .. " - " .. genes1.fertility.dom)
  263.     writeLine("3. Resistance: " .. genes1.resistance.rec .. " - " .. genes1.resistance.dom)
  264.     writeLine("4. Growth: " .. genes1.growth.rec .. " - " .. genes1.growth.dom)
  265.     writeLine("5. Strength: " .. genes1.strength.rec .. " - " .. genes1.strength.dom)
  266.     writeLine("6. Mutativity: " .. genes1.mutativity.rec .. " - " .. genes1.mutativity.dom)
  267.     writeLine("Score: " .. score1)
  268. end
  269.  
  270. -- Main loop: Runs until a perfect seed is bred.
  271. while notFinished do
  272.     if not initialized then
  273.         print("First initializing...")
  274.         init()
  275.     end
  276.     sleep(0.5)
  277.     print("Bring Parent Infos on Monitor...")
  278.     printParents()
  279.     sleep(0.5)
  280.     print("Waiting for Crop...")
  281.     child = scan("mid")
  282.     -- Waiting loop until a plant grows in the middle.
  283.     while child.hasPlant == 0 do
  284.         sleep(0.5)
  285.         child = scan("mid")
  286.         sleep(0.5)
  287.     end
  288.     sleep(0.5)
  289.     print("Scanning...")
  290.     scan("mid")
  291.     sleep(0.5)
  292.     print("Comparing...")
  293.     compareCrops()
  294.     sleep(0.5)
  295. end
  296.  
  297. -- Display end message on the monitor when breeding is complete.
  298. mon.clear()
  299. mon.setCursorPos(1,1)
  300. line = 1
  301. writeLine("####################################")
  302. writeLine("##                                ##")
  303. writeLine("##  ######  ####   ##  ######  ## ##")
  304. writeLine("##  ##      #####  ##  ##   ## ## ##")
  305. writeLine("##  ######  ## ### ##  ##   ## ## ##")
  306. writeLine("##  ##      ##  #####  ##   ##    ##")
  307. writeLine("##  ######  ##   ####  ######  ## ##")
  308. writeLine("##                                ##")
  309. writeLine("####################################")
  310. print("seed is finished")
Advertisement
Add Comment
Please, Sign In to add comment