Advertisement
Rnen10

Agricraft Seed Breeder

Jun 4th, 2016 (edited)
4,894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.83 KB | Gaming | 0 0
  1. -- Made by Rnen10
  2. -- If problems/questions feel free to contact me
  3. -- https://twitter.com/Rnen10
  4. -- rnen. (Evan#7181)  on discord
  5.  
  6. --Here is a detailed view
  7. --https://imgur.com/a/FMfTi
  8.  
  9. --Here's a flowchart showing how it works
  10. --https://sketchboard.me/NAZDv63LNgBd#/
  11.  
  12.  
  13. --Use this for automatic updates->  pastebin get fehTMGPp startup
  14.  
  15. -- =============VARIABLES===================
  16. analyzer = peripheral.wrap("bottom")
  17.  
  18. --Plant 1 & 2
  19. local p1 = nil
  20. local p2 = nil
  21. --Seeds in analyzer
  22. local p3 = nil
  23. --Variable for when job is done
  24. local done = false
  25.  
  26. local plantName = nil
  27.  
  28. function DEBUG()
  29.     return fs.exists("seeddebug")
  30. end
  31.  
  32. local posData = { ["x"] = 0, ["y"] = 0, ["rot"] = 1 }
  33. -- ============VARIABLES END================
  34.  
  35. -- ============FUNCTIONS====================
  36. function NewLine(Str)
  37.     local barLength = 40
  38.     local tempPercentage = 0
  39.     term.clear()
  40.     term.setCursorPos(1,2)
  41.     print("-------------[SeedBot v3.8]------------")
  42.     term.setCursorPos(11,5)
  43.     write("v {Current action} v")
  44.     term.setCursorPos(21 - (math.floor(string.len(Str) / 2)),8)
  45.     print(Str)
  46.     if not plantName == nil then
  47.         plantString = "Current Type: "..plantName
  48.         term.setCursorPos(20 - (math.floor(string.len(plantString) / 2)),9)
  49.         print(plantString)
  50.     end
  51.     if DEBUG() then
  52.         term.setCursorPos(1,11)
  53.         print("X: "..posData["x"].." Y: "..posData["y"].." Rot: "..posData["rot"])
  54.     end
  55.     term.setCursorPos(12,12)
  56.     write("[Current Fuel %"..(math.floor(turtle.getFuelLevel())))
  57.     write("]")
  58.     term.setCursorPos(1,13)
  59.     barLength = turtle.getFuelLevel() / 100 * 40
  60.     if barLength > 40 then barLength = 40 end
  61.     if term.isColor() then
  62.       term.setBackgroundColor(colors.green)
  63.     else
  64.       term.setBackgroundColor(colors.white)
  65.     end
  66.     for i =1,(barLength) do
  67.       write(" ")
  68.     end
  69.     if barLength < 40 then
  70.         if term.isColor() then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.gray) end
  71.         local remainingBarLength = (40 - barLength)
  72.         for i =1,(remainingBarLength) do
  73.           write(" ")
  74.         end
  75.     end
  76.     term.setBackgroundColor(colors.black)
  77. end
  78.  
  79. function ClearSeeds()
  80.     analyzer = peripheral.wrap("bottom")
  81.     if analyzer.hasPlant("NORTH") then
  82.         Left()
  83.         Forward()
  84.         turtle.select(1)
  85.         turtle.digDown()
  86.         turtle.suckDown()
  87.         turtle.select(1)
  88.         turtle.placeDown()
  89.         Back()
  90.         Right()
  91.     end
  92.     if analyzer.hasPlant("EAST") then
  93.         Forward()
  94.         turtle.select(1)
  95.         turtle.digDown()
  96.         turtle.suckDown()
  97.         turtle.select(1)
  98.         turtle.placeDown()
  99.         Back()
  100.     end
  101.     Empty()
  102. end
  103.  
  104.  
  105. function Empty()
  106.     if not done then NewLine("Emptying Inventory") end
  107.     for i = 1,16 do
  108.       turtle.select(i)
  109.       if not turtle.dropUp() and (turtle.getItemCount()>0) then
  110.         NewLine("Chest is full!")
  111.         done = true
  112.         break
  113.       end
  114.     end
  115.     turtle.select(1)
  116. end
  117.  
  118. function CheckSeeds()
  119.   analyzer = peripheral.wrap("bottom")
  120.   if analyzer.hasPlant("NORTH") == true and analyzer.hasPlant("EAST") == true then
  121.     local s1, s2, s3 = analyzer.getSpecimenStats("NORTH")
  122.     p1 = s1 + s2 + s3
  123.     local s1, s2, s3 = analyzer.getSpecimenStats("EAST")
  124.     p2 = s1 + s2 + s3
  125.     if p1==30 or p2==30 then
  126.       GetNewSeeds()
  127.     end
  128.   else
  129.     GetNewSeeds()
  130.     return
  131.   end
  132. end
  133.  
  134. function DuplicateSingleSeed()
  135.     if analyzer.hasPlant("NORTH") or analyzer.hasPlant("EAST") then
  136.         Empty()
  137.         os.reboot()
  138.     else
  139.         NewLine("Analyzing single Seed")
  140.         turtle.select(1)
  141.         analyzer = peripheral.wrap("bottom")
  142.         turtle.dropDown(turtle.getItemCount())
  143.         analyzer.analyze()
  144.         while not analyzer.isAnalyzed() do
  145.             sleep(1)
  146.         end
  147.         plantName = analyzer.getSpecimen()
  148.         NewLine("Planting single seed")
  149.         turtle.suckDown()
  150.         Forward()
  151.         Right()
  152.         Forward()
  153.         redstone.setOutput("bottom", true)
  154.         while turtle.getItemCount() > 0 do turtle.dropDown(1) end
  155.         sleep(2)
  156.         redstone.setOutput("bottom", false)
  157.         Back()
  158.         Back()
  159.         Left()
  160.         Forward()
  161.         redstone.setOutput("bottom", true)
  162.         sleep(0.9)
  163.         redstone.setOutput("bottom", false)
  164.         Back()
  165.         Right()
  166.         Back()
  167.         NewLine("Waiting for seed to duplicate...")
  168.         while analyzer.isCrossCrop("SOUTH") do
  169.             sleep(5)
  170.         end
  171.         NewLine("Picking up new seed")
  172.         Forward()
  173.         turtle.select(1)
  174.         turtle.digDown()
  175.         turtle.suckDown()
  176.         turtle.placeDown()
  177.         Forward()
  178.         Left()
  179.         Back()
  180.         turtle.select(2)
  181.         analyzer = peripheral.wrap("bottom")
  182.         turtle.dropDown(turtle.getItemCount())
  183.         analyzer.analyze()
  184.         sleep(3.5)
  185.         turtle.suckDown()
  186.         Left()
  187.         Forward()
  188.         Forward()
  189.         redstone.setOutput("bottom", true)
  190.         turtle.dropDown(1)
  191.         sleep(2)
  192.         redstone.setOutput("bottom", false)
  193.         Back()
  194.         Back()
  195.         Right()
  196.         Empty()
  197.         os.reboot()
  198.     end
  199. end
  200.  
  201. function GetNewSeeds()
  202.     plantName = nil
  203.     NewLine("Changing out seeds")
  204.     if p1 == nil or p2 == nil then
  205.         p1 = 1
  206.         p2 = 1
  207.     elseif p1>p2 then
  208.         Left()
  209.         Forward()
  210.         turtle.select(1)
  211.         turtle.digDown()
  212.         turtle.suckDown()
  213.         turtle.select(1)
  214.         turtle.placeDown()
  215.         Back()
  216.         Right()
  217.         Forward()
  218.         Empty()
  219.         turtle.select(1)
  220.         turtle.digDown()
  221.         turtle.suckDown()
  222.         turtle.select(1)
  223.         turtle.placeDown()
  224.         Back()
  225.         Empty()
  226.     elseif p1<p2 then
  227.         Left()
  228.         Forward()
  229.         turtle.select(1)
  230.         turtle.digDown()
  231.         turtle.suckDown()
  232.         turtle.select(1)
  233.         turtle.placeDown()
  234.         Back()
  235.         Right()
  236.         Empty()
  237.         Forward()
  238.         turtle.select(1)
  239.         turtle.digDown()
  240.         turtle.suckDown()
  241.         turtle.select(1)
  242.         turtle.placeDown()
  243.         Empty()
  244.         Back()
  245.     else
  246.         ClearSeeds()
  247.         Empty()
  248.     end
  249.     Right()
  250.     if turtle.suck() then
  251.       Left()
  252.       if turtle.getItemCount() == 1 then
  253.         --Empty()
  254.         --os.reboot()
  255.         DuplicateSingleSeed()
  256.       end
  257.       analyzer = peripheral.wrap("bottom")
  258.       turtle.dropDown(turtle.getItemCount())    
  259.       analyzer.analyze()
  260.       NewLine("Analyzing new seeds")
  261.       while not analyzer.isAnalyzed() do
  262.         sleep(1)
  263.       end
  264.       plantName = analyzer.getSpecimen()
  265.       turtle.select(2)
  266.       turtle.suckDown()
  267.  
  268.       NewLine("Planting new seeds")
  269.  
  270.       Forward()
  271.       Right()
  272.       Forward()
  273.       redstone.setOutput("bottom", true)
  274.       turtle.dropDown(1)
  275.       sleep(2)
  276.       redstone.setOutput("bottom", false)
  277.       Back()
  278.       Left()
  279.       Back()
  280.       Left()
  281.       Forward()
  282.       Forward()
  283.       redstone.setOutput("bottom", true)
  284.       turtle.dropDown(1)
  285.       sleep(2)
  286.       redstone.setOutput("bottom", false)
  287.       Back()
  288.       Back()
  289.       Right()
  290.       Empty()
  291.       CheckSeeds()
  292.     else
  293.       Left()
  294.       NewLine("Done!")
  295.       done = true
  296.       ClearSeeds()
  297.       return
  298.     end
  299.     CheckSeeds()
  300. end
  301.  
  302. function CheckFuel(bool)
  303.   local fuel = turtle.getFuelLevel()
  304.   --Checking fuel level & fueling if low
  305.   if turtle.getFuelLevel() < 25 then
  306.     GetFuel()
  307.     return true
  308.   else
  309.   return false
  310.   end
  311. end
  312.  
  313. function GetFuel()
  314.   if not done then NewLine("Getting fuel") end
  315.   --print("Current Fuel Level: "..turtle.getFuelLevel())
  316.   Left()
  317.   Left()
  318.   turtle.select(16)
  319.   while turtle.getFuelLevel() <= 100 do
  320.     if not turtle.suck(1) and (turtle.getFuelLevel() <= 100) then
  321.       NewLine("No fuel in fuel chest!")
  322.       turtle.select(1)
  323.       done = true
  324.       break
  325.     end
  326.     turtle.refuel(1)
  327.     turtle.drop(turtle.getItemCount(16))
  328.     NewLine("Getting fuel")
  329.     --print("Current Fuel Level: "..turtle.getFuelLevel())
  330.   end
  331.   turtle.select(1)
  332.   Right()
  333.   Right()
  334. end
  335.  
  336. function CheckEmpty()
  337.   local invItemNumber = 0
  338.   NewLine("Counting Items...")
  339.   for i = 1,16 do
  340.     turtle.select(i)
  341.     invItemNumber = invItemNumber + turtle.getItemCount(i)
  342.   end
  343.   if invItemNumber > 0 then
  344.     NewLine("Not empty! Resetting")
  345.     turtle.select(1)
  346.     return false
  347.   else
  348.     turtle.select(1)
  349.     return true
  350.   end
  351. end
  352.  
  353. function CheckPosition()
  354.     if fs.exists("seedPos") then
  355.         local file = fs.open("seedPos","r")
  356.         local data = file.readAll()
  357.         file.close()
  358.         posData = textutils.unserialize(data)
  359.     else
  360.         local file = fs.open("seedPos","w")
  361.         file.write(textutils.serialize(posData))
  362.         file.close()
  363.     end
  364. end
  365.  
  366. function SavePosition()
  367.     local file = fs.open("seedPos","w")
  368.     file.write(textutils.serialize(posData))
  369.     file.close()
  370. end
  371.  
  372. function Forward()
  373.     turtle.forward()
  374.     if posData["rot"] == 1 then
  375.         posData["x"] = posData["x"] + 1
  376.     elseif posData["rot"] == 3 then
  377.         posData["x"] = posData["x"] - 1
  378.     elseif posData["rot"] == 0 then
  379.         posData["y"] = posData["y"] - 1
  380.     elseif posData["rot"] == 2 then
  381.         posData["y"] = posData["y"] + 1
  382.     end
  383.     SavePosition()
  384. end
  385.  
  386. function Right()
  387.     turtle.turnRight()
  388.     posData["rot"] = posData["rot"] + 1
  389.     if posData["rot"] > 4 then posData["rot"] = 0 end
  390.     SavePosition()
  391. end
  392.  
  393. function Left()
  394.     turtle.turnLeft()
  395.     posData["rot"] = posData["rot"] - 1
  396.     if posData["rot"] < 0 then posData["rot"] = 4 end
  397.     SavePosition()
  398. end
  399.  
  400. function Back()
  401.     turtle.back()
  402.     if posData["rot"] == 1 then
  403.         posData["x"] = posData["x"] - 1
  404.     elseif posData["rot"] == 3 then
  405.         posData["x"] = posData["x"] + 1
  406.     elseif posData["rot"] == 0 then
  407.         posData["y"] = posData["y"] + 1
  408.     elseif posData["rot"] == 2 then
  409.         posData["y"] = posData["y"] - 1
  410.     end
  411.     SavePosition()
  412. end
  413.  
  414. -- ===========STARTUP SEQUENCE==========
  415. if turtle.getFuelLevel() > 4 then CheckPosition() end
  416. CheckFuel()
  417. -- ===========STARTUP SEQUENCE END======
  418.  
  419. -- ===========MAIN LOOP=================
  420. while not done do
  421.   --Sleep to prevent timeout
  422.   --sleep(1)
  423.  
  424.   p1Mature = analyzer.isMature("EAST")
  425.   p2Mature = analyzer.isMature("NORTH")
  426.  
  427.   if p1Mature and p2Mature then
  428.     NewLine("Going to cross-crop activator")
  429.  
  430.     --Place 2nd crop stick for crossbreeding
  431.     Forward()
  432.     Left()
  433.     Forward()
  434.     Right()
  435.     Forward()
  436.     redstone.setOutput("bottom", true)
  437.     sleep(0.8)
  438.     redstone.setOutput("bottom", false)
  439.    
  440.     NewLine("Moving to second Analyzer")
  441.  
  442.     Back()
  443.     Right()
  444.     if turtle.suckDown() then
  445.       Forward()
  446.       Left()
  447.       Back()
  448.       Empty()
  449.       os.reboot()
  450.     else
  451.       Back()
  452.     end
  453.  
  454.    
  455.     NewLine("Waiting for new seed")
  456.  
  457.     analyzer = peripheral.wrap("bottom")
  458.     if not analyzer.hasPlant("SOUTH") and not analyzer.isCrossCrop("SOUTH") then
  459.       NewLine("No cross crop sticks, resetting")
  460.       Forward()
  461.       Forward()
  462.       Left()
  463.       Back()
  464.       os.reboot()
  465.     else
  466.       while analyzer.isCrossCrop("SOUTH") do
  467.         sleep(5)
  468.       end
  469.     end
  470.     NewLine("Picking up new seed") 
  471.  
  472.     Forward()
  473.     turtle.select(1)
  474.     turtle.digDown()
  475.     turtle.suckDown()
  476.     turtle.placeDown()
  477.  
  478.     NewLine("Going to Analyzer 1")
  479.  
  480.     turtle.select(2)
  481.     Forward()
  482.     Left()
  483.     Back()
  484.     sleep(0.1)
  485.  
  486.     if CheckEmpty() then os.reboot() else turtle.select(2) end
  487.     NewLine("Analyzing new seed")
  488.  
  489.     analyzer = peripheral.wrap("bottom")
  490.     turtle.dropDown(turtle.getItemCount())    
  491.     analyzer.analyze()
  492.     while not analyzer.isAnalyzed() do
  493.         sleep(3.5)
  494.     end
  495.     local s1, s2, s3 = analyzer.getSpecimenStats()
  496.     p3 = s1 + s2 + s3
  497.     if p3 == 30 then
  498.       turtle.suckDown()
  499.       Forward()
  500.       Empty()
  501.       Back()
  502.       p1=nil
  503.       p2=nil
  504.       ClearSeeds()
  505.       --return
  506.     else
  507.       turtle.select(2)
  508.       turtle.suckDown()
  509.  
  510.       NewLine("Removing weakest seed")
  511.  
  512.       CheckSeeds()
  513.       if p1<=p2 then
  514.         Left()
  515.         Forward()
  516.         turtle.select(1)
  517.         turtle.digDown()
  518.         turtle.suckDown()
  519.         turtle.select(1)
  520.         turtle.placeDown()
  521.         Forward()
  522.         redstone.setOutput("bottom", true)
  523.         turtle.select(2)
  524.         turtle.dropDown(1)
  525.         sleep(2)
  526.         redstone.setOutput("bottom", false)
  527.         Back()
  528.         Back()
  529.         Right()
  530.         Empty()
  531.       else
  532.         Forward()
  533.         turtle.select(1)
  534.         turtle.digDown()
  535.         turtle.suckDown()
  536.         Right()
  537.         turtle.select(1)
  538.         turtle.placeDown()
  539.         Forward()
  540.         redstone.setOutput("bottom", true)
  541.         turtle.select(2)
  542.         turtle.dropDown(1)
  543.         sleep(2)
  544.         redstone.setOutput("bottom", false)
  545.         Back()
  546.         Left()
  547.         Back()
  548.         Empty()
  549.       end
  550.     end
  551.   end
  552.   CheckSeeds()
  553.   if not done then
  554.  NewLine("Waiting for growth")
  555.   if not CheckFuel() then sleep(5) end
  556.  end
  557. end
  558. -- ================Main Loop End=========================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement