Advertisement
Guest User

seed

a guest
Aug 30th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.56 KB | None | 0 0
  1. function go(character)
  2.     if character=="f" then
  3.         turtle.forward()
  4.     elseif character=="b" then
  5.         turtle.back()
  6.     elseif character=="u" then
  7.         turtle.up()
  8.     elseif character=="d" then
  9.         turtle.down()
  10.     elseif character=="r" then
  11.         turtle.turnRight()
  12.     elseif character=="l" then
  13.         turtle.turnLeft()
  14.     end
  15. end
  16.  
  17. function followList(list, inverse)
  18.     --list is a string
  19.     opposites={f="b",b="f",u="d",d="u",l="r",r="l"}
  20.    
  21.     if inverse then
  22.         list=string.reverse(list)
  23.     end
  24.    
  25.     for i=1,string.len(list) do
  26.         local c=list:sub(i,i)
  27.        
  28.         if inverse then
  29.             c=opposites[c]
  30.         end
  31.        
  32.         go(c)
  33.     end
  34. end
  35.  
  36. function checkmeal()
  37.     local meal
  38.     meal=turtle.getItemCount(16)
  39.     if meal >= 4 then
  40.         return true
  41.     else
  42.         return false
  43.     end
  44. end
  45.  
  46. function meal()
  47.     turtle.select(16)
  48.    
  49.     turtle.placeDown()
  50.     turtle.placeDown()
  51.     turtle.placeDown()
  52.     turtle.placeDown()
  53.    
  54.     turtle.select(1)
  55. end
  56.  
  57. function findPlantDirection(SeedAnalyzer)
  58.     --only call this function if there is a plant or double crop next to the sa, otherwise there will be problems
  59.     local function raiseErrorIfNoCrop(Cardinal,saParam)
  60.         saParam.hasPlant(Cardinal)
  61.     end
  62.    
  63.     local listAllDir={"NORTH","EAST","SOUTH","WEST"}
  64.    
  65.     for key,value in pairs(listAllDir) do
  66.         if pcall(raiseErrorIfNoCrop,value,SeedAnalyzer) then
  67.             return value
  68.         end
  69.     end
  70. end
  71.  
  72. function getWaitTime(percent)
  73.     percent=math.floor(percent)
  74.    
  75.     local growthStage={}
  76.     growthStage[0]=0
  77.     growthStage[14]=1
  78.     growthStage[28]=2
  79.     growthStage[42]=3
  80.     growthStage[57]=4
  81.     growthStage[71]=5
  82.     growthStage[85]=6
  83.     growthStage[100]=7
  84.    
  85.     local wTime={[0]=120,[1]=120,[2]=120,[3]=120,[4]=60,[5]=20,[6]=10,[7]=0}
  86.     return wTime[growthStage[percent]]
  87. end
  88.  
  89. function waitForGrowth(SeedAnalyzer)
  90.     local direction=findPlantDirection(SeedAnalyzer)
  91.     local percent
  92.     local t
  93.     while true do
  94.         percent=SeedAnalyzer.getGrowthStage(direction)
  95.         if percent==100 then
  96.             break
  97.         end
  98.         t=getWaitTime(percent)
  99.         os.sleep(t)
  100.     end
  101. end
  102.  
  103. function waitForPlant(SeedAnalyzer)
  104.     local direction=findPlantDirection(SeedAnalyzer)
  105.     while true do
  106.         if SeedAnalyzer.hasPlant(direction) then
  107.             break
  108.         end
  109.         os.sleep(15)
  110.     end
  111. end
  112.  
  113. function updatePeripheral()
  114.     followList("ud")
  115. end
  116.  
  117. function plant()
  118.     turtle.select(2)
  119.     redstone.setOutput("front",true)
  120.     turtle.drop(1)
  121.     os.sleep(1.5)
  122.     redstone.setOutput("front",false)
  123.  
  124.     turtle.select(1)
  125.    
  126.     if checkmeal() then
  127.         followList("ruuflf")
  128.        
  129.         meal()
  130.        
  131.         --go to sa1
  132.         followList("bd")
  133.     else
  134.         --go to sa1
  135.         followList("rufl")
  136.     end
  137. end
  138.  
  139. --bind the sa peripheral between plant and clone funcs
  140.  
  141. function clone(sa)
  142.     waitForGrowth(sa)
  143.    
  144.     --go to sa2
  145.     followList("rfl")
  146.    
  147.     updatePeripheral()
  148.     waitForPlant(sa)
  149.    
  150.     if checkmeal() then
  151.         followList("uf")
  152.        
  153.         meal()
  154.        
  155.         followList("uf",true)
  156.     end
  157.    
  158.     waitForGrowth(sa)
  159.    
  160.     --go to sa3
  161.     followList("ffflfr")
  162.    
  163.     updatePeripheral()
  164.     waitForPlant(sa)
  165.    
  166.     if checkmeal() then
  167.         followList("ub")
  168.        
  169.         meal()
  170.        
  171.         followList("ub",true)
  172.     end
  173.    
  174.     waitForGrowth(sa)
  175. end
  176.  
  177. function breakParent()
  178.     --go to parent
  179.     followList("bb")
  180.    
  181.     --break plant
  182.     turtle.digDown()
  183. end
  184.  
  185. function switchCrops()
  186.     --go to piston
  187.     followList("blfrdd")
  188.    
  189.     --pulse redstone
  190.     redstone.setOutput("back",true)
  191.     os.sleep(2)
  192.     redstone.setOutput("back",false)
  193. end
  194.  
  195. function mutate(sa)
  196.     --go to sa1
  197.     followList("uurfl")
  198.    
  199.     waitForPlant(sa)
  200.    
  201.     --go to sa4
  202.     followList("rflfff")
  203.    
  204.     waitForPlant(sa)
  205. end
  206.  
  207. function breakEachPlant()
  208.     --this function also places the crop stick for the next cycle
  209.     go("b")
  210.    
  211.     turtle.digDown()
  212.     followList("lf")
  213.    
  214.     turtle.digDown()
  215.     followList("lf")
  216.    
  217.     turtle.digDown()
  218.     --place crop
  219.     turtle.placeDown()
  220.     followList("lf")
  221.    
  222.     turtle.digDown()
  223.     followList("lf")
  224. end
  225.  
  226. function getSeedStats(sa)
  227.     turtle.dropDown()
  228.     sa.analyze()
  229.     os.sleep(3.5)
  230.     local stats={sa.getSpecimenStats()}
  231.     turtle.suckDown()
  232.     return stats
  233. end
  234.  
  235. function checkSlot(t,slotNumber)
  236.     if turtle.getItemCount(slotNumber)>=1 then
  237.         table.insert(t,slotNumber)
  238.     end
  239. end
  240.  
  241. function bestSeedSlotNumber(seedProfiles)
  242.     local mGrowth=-1
  243.     local mGain=-1
  244.     local mStrength=-1
  245.    
  246.     local slotNumber=0
  247.    
  248.     for k,v in pairs(seedProfiles) do
  249.         if v[1]>mGrowth then
  250.             mGrowth=v[1]
  251.         end
  252.     end
  253.    
  254.     for k,v in pairs(seedProfiles) do
  255.         if v[1]==mGrowth and v[2]>mGain then
  256.             mGain=v[2]
  257.         end
  258.     end
  259.    
  260.     for k,v in pairs(seedProfiles) do
  261.         if v[1]==mGrowth and v[2]==mGain and v[3]>mStrength then
  262.             mStrength=v[3]
  263.         end
  264.     end
  265.    
  266.     for k,v in pairs(seedProfiles) do
  267.         if v[1]==mGrowth and v[2]==mGain and v[3]==mStrength then
  268.             slotNumber=v.slot
  269.             break
  270.         end
  271.     end
  272.    
  273.     return slotNumber
  274. end
  275.  
  276. function manageInventory(sa)
  277.     --info
  278.     local sticksToDrop=turtle.getItemCount(1)-59
  279.     local fiberToDrop=turtle.getItemCount(3)-52
  280.    
  281.     local seedSlots={}
  282.     checkSlot(seedSlots,2)
  283.     checkSlot(seedSlots,4)
  284.     checkSlot(seedSlots,5)
  285.     checkSlot(seedSlots,6)
  286.    
  287.     local isMaxed=false
  288.    
  289.     --analyze
  290.     go("f")
  291.     local seedProfiles={}
  292.     for key,value in pairs(seedSlots) do
  293.         turtle.select(value)
  294.         local stats=getSeedStats(sa)
  295.         stats.slot=value
  296.         table.insert(seedProfiles,stats)
  297.     end
  298.    
  299.     --choose
  300.     local bestSlot=bestSeedSlotNumber(seedProfiles)
  301.    
  302.     --check if the seed is maxed
  303.     for k,v in pairs(seedProfiles) do
  304.         if v.slot==bestSlot then
  305.             if v[1]==10 and v[2]==10 and v[3]==10 then
  306.                 isMaxed=true
  307.                 break
  308.             end
  309.         end
  310.     end
  311.    
  312.     --delete
  313.     followList("bbblffd")--go to trash can
  314.    
  315.     if sticksToDrop>=1 then
  316.         turtle.select(1)
  317.         turtle.drop(sticksToDrop)
  318.     end
  319.    
  320.     if fiberToDrop>=1 then
  321.         turtle.select(3)
  322.         turtle.drop(fiberToDrop)
  323.     end
  324.    
  325.     --drop bad seeds
  326.     for k,v in pairs(seedSlots) do
  327.         if v~=bestSlot then
  328.             turtle.select(v)
  329.             turtle.drop()
  330.         end
  331.     end
  332.    
  333.     --leave a single seed remaining
  334.     if turtle.getItemCount(bestSlot)>1 then
  335.         turtle.select(bestSlot)
  336.         local numberOfBestSeedsToDrop=turtle.getItemCount(bestSlot)-1
  337.         turtle.drop(numberOfBestSeedsToDrop)
  338.     end
  339.    
  340.     --cleanup
  341.     --move best seed to slot 2
  342.     if bestSlot~=2 then
  343.         turtle.select(bestSlot)
  344.         turtle.transferTo(2)
  345.     end
  346.    
  347.     --select default slot
  348.     turtle.select(1)
  349.    
  350.     --face the aa
  351.     go("r")
  352.    
  353.     return isMaxed
  354. end
  355.  
  356. function maximizeSeed()
  357.     --preqs are that the structure is built, there is a crop in 1, crosscrops in clone spots, crops in slot 1 but not more than 59, any seed in slot 2
  358.     --bone meal goes in slot 16 which is bottom right corner
  359.     local isMaxed=false
  360.    
  361.     while not isMaxed do
  362.         plant()
  363.         local sa=peripheral.wrap("bottom")
  364.         clone(sa)
  365.         breakParent()
  366.         switchCrops()
  367.         mutate(sa)
  368.         breakEachPlant()
  369.         isMaxed=manageInventory(sa)
  370.     end
  371. end
  372.  
  373. --main
  374. maximizeSeed()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement