Advertisement
Guest User

SeedBreeder.lua

a guest
Aug 17th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.02 KB | None | 0 0
  1.  
  2. ----------------------------------------------------
  3. ------------------SOME VARIABLES--------------------
  4. ----------------------------------------------------
  5.  
  6. t = turtle
  7. dataF = "sbdata/data"
  8. redstoneSleep = 0.9
  9.  
  10. seed = {g = 0, y = 0, s = 0, name=""}
  11.  
  12. slot = {fuel = 1, sticks1 = 2, sticks2 = 3, seed = 4, extra = 5}
  13. pos={x = 1, z = 0, seed={} , actv={}}
  14. pos.seed[0] = {x = -1, z = 0}
  15. pos.actv[0] = {x = -1, z = -1}
  16.  
  17. pos.seed[1] = {x = 0, z = 1}
  18. pos.actv[1] = {x = 1, z = 1}
  19.  
  20. pos.seed[2] = {x = 0, z = 0}
  21. pos.actv[2] = {x = 0, z = -1}
  22.  
  23. pos.anlzer = {x=1,z=0}
  24. pos.chest = {x=-1,z=1}
  25. pos.bin = {x=1,z=-1}
  26.  
  27. seedSlot = 4
  28.  
  29. data={}
  30.  
  31.  
  32. if not fs.exists("UpdatedSB1") then
  33.     print("Updating sb program")
  34.     fs.delete("sb")
  35.     shell.run("pastebin get gF0Tsbhg sb")
  36.    
  37.     local file = fs.open("UpdatedSB1","w")
  38.     file.close()
  39. end
  40.  
  41.  
  42. ----------------------------------------------------
  43. -----------------LANG VARIABLES---------------------
  44. ----------------------------------------------------
  45. lang_noFuel = "Please insert a valid fuel in slot "..slot.fuel.."!"
  46. lang_noSticks = "Please insert Crop Sticks in slot "..slot.sticks1.." or "..slot.sticks2.."!"
  47. --lang_noRake = "Please insert a Hand Rake in slot "..slot.rake.."!"
  48. lang_noSeed = "Please insert a valid seeds in slot "..slot.seed.."!"
  49. lang_manySeeds = "There are too many seeds! Use 1 or 2 only."
  50. lang_maxedOut = "This seed is now 10/10/10!"
  51. lang_line = "---------------------------------------"
  52. lang_placingStick = "Placing sticks"
  53. lang_placingSeedsInAcv = "Placing seeds in Autonomous Activator"
  54. lang_placingSticksInAcv = "Placing sticks in Autonomous Activator"
  55.  
  56.  
  57. CCSA = "Computer Controlled Seed Analyzer"
  58. TRSHC = "Trash Can"
  59. CRPSTCK = "Crop Sticks without seeds"
  60. ----------------------------------------------------
  61. ----------------------------------------------------
  62. ----------------------------------------------------
  63.  
  64. function loadData()
  65.    if fs.exists(dataF) then
  66.     local file = fs.open(dataF,"r")
  67.     local data = file.readAll()
  68.     file.close()
  69.     return textutils.unserialize(data)
  70.    else
  71.     data = {x = 0, z = 0, state = 0, weedState = 0, seed = "",Gm = 0, Ym = 0, Sm = 0, seedRepl = 0,}
  72.     fs.makeDir("sbdata")
  73.     local file = fs.open(dataF,"w")
  74.     file.write(textutils.serialize(data))
  75.     file.close()
  76.     return data
  77.    end
  78. end
  79. function saveData()
  80.    local file = fs.open(dataF,"w")
  81.    file.write(textutils.serialize(data))
  82.    file.close()
  83. end
  84. function setState(id)
  85.    data.state = id
  86.     saveData()
  87. end
  88. function setWState(id)
  89.     data.weedState = id
  90.     saveData()
  91. end
  92. function stateRange(min,max)
  93.     if data.state >= min and data.state < max then
  94.         return true
  95.     else
  96.         return false
  97.     end
  98. end
  99.  
  100. ----------------------------------------------------
  101. ---------------BASIC TURTLE COMMANDS----------------
  102. ----------------------------------------------------
  103. function resetScreen()
  104.    term.clear()
  105.    term.setCursorPos(1,1)
  106. end
  107.  
  108. function putInAnlzer()
  109.   if checkCount(slot.seed,1) then
  110.     lastSl = select(slot.seed)
  111.   elseif checkCount(slot.sticks2,1) then
  112.     lastSl = select(slot.sticks2)
  113.   end
  114.   succes = t.dropDown()
  115.   select(lastSl)
  116.   return succes
  117. end
  118. function wrapAnlzer()
  119.   return peripheral.wrap("bottom")
  120. end
  121. function takeFromAnlzer()
  122.   lastSl = select(slot.seed)
  123.   succes = suckDown(64)
  124.   select(lastSl)
  125.   return succes
  126. end
  127. function isMaxedOut()
  128.   if seed.g == 10 and seed.y == 10 and seed.s == 10 then
  129.     return true
  130.   end
  131.   return false
  132. end
  133. function updateSeedMaxData(letEqualUpdate)
  134.   local anl = wrapAnlzer()
  135.   local g, y, s = anl.getSpecimenStats()
  136.     if (seed.g+seed.s+seed.y)/3 < (g+y+s)/3 and not letEqualUpdate then
  137.       seed.g = g
  138.       seed.y = y
  139.       seed.s = s
  140.       return true
  141.     elseif (seed.g+seed.s+seed.y)/3 <= (g+y+s)/3 and letEqualUpdate then
  142.       seed.g = g
  143.       seed.y = y
  144.       seed.s = s
  145.       return true
  146.     end
  147.   return false
  148. end
  149. function analyze(letEqualUpdate)
  150.   move(pos.anlzer)
  151.   print("Analyzing")
  152.   if putInAnlzer() then
  153.     local anl = wrapAnlzer()
  154.     anl.analyze()
  155.     while not anl.isAnalyzed() do
  156.         os.sleep(0.2)
  157.     end
  158.     isUpdated = updateSeedMaxData(letEqualUpdate)
  159.     if isMaxedOut()then
  160.       takeFromAnlzer()
  161.       seedMaxedOut()
  162.     end
  163.     takeFromAnlzer()
  164.     return isUpdated
  165.   end
  166.   return false
  167. end
  168. function redstoneOn()
  169.   return redstone.setOutput("bottom",true)
  170. end
  171. function redstoneOff()
  172.   return redstone.setOutput("bottom",false)
  173. end
  174.  
  175.  
  176. function placeSticks(tablePos)
  177.   move(tablePos)
  178.   print("Placing sticks")
  179.   if sticks() then
  180.       print(lang_placingStick)
  181.       lastSelected = select(slot.sticks1)
  182.       t.placeDown()
  183.       select(lastSelected)
  184.   end
  185. end
  186. function breakStick(tablePos)
  187.   move(tablePos)
  188.   print("Breaking sticks")
  189.   t.digDown()
  190. end
  191. function placeSticksInActv(tablePos,doubleSticks)
  192.   print("Placing sticks in right clicky machine")
  193.   move(tablePos)
  194.     if doubleSticks then
  195.       sticks()
  196.       dropDownFromSlot(slot.sticks1,1)
  197.     end
  198.     sticks()
  199.     dropDownFromSlot(slot.sticks1,1)
  200.     redstoneOn()
  201.     sleep(redstoneSleep)
  202.   redstoneOff()
  203. end
  204.  
  205. function dropSeedsInActv(tablePos)
  206.   move(tablePos)
  207.   print("Placing seeds in right clicky machine")
  208.   redstoneOn()
  209.     --------dropDownFromSlot(slot.rake,64)
  210.     --------sleep(redstoneSleep)
  211.     --------suckDownInSlot(slot.rake,64)
  212.     dropDownFromSlot(slot.seed,1)
  213.     sleep(redstoneSleep)
  214.   redstoneOff()
  215.   suckDownInSlot(12,64)
  216.   if checkCount(12,1) then
  217.     transferItem(12,slot.seed)
  218.     print("Weeds detected ")
  219.     return false
  220.   end
  221.   return true
  222. end
  223.  
  224. function placeSeeds(seedPosTbl,actvPosTbl)
  225.   placeSticks(seedPosTbl,false)
  226.   while not dropSeedsInActv(actvPosTbl) do
  227.     breakStick(seedPosTbl)
  228.     placeSticks(seedPosTbl,false)
  229.   end
  230. end
  231.  
  232. function waitForSeedToGrow()
  233.   move(pos.anlzer)
  234.   print("Waiting for seeds to grow")
  235.   local anl = wrapAnlzer()
  236.   while not anl.hasPlant("WEST") do
  237.     if anl.hasWeeds("WEST") then
  238.       print("Removing weeds")
  239.       breakStick(pos.seed[2])
  240.       placeSticksInActv(pos.actv[2],true)
  241.       move(pos.anlzer)
  242.     end
  243.     sleep(1)
  244.   end
  245.   breakStick(pos.seed[2])
  246.   return true
  247. end
  248.  
  249. function trashSeed()
  250.   move(pos.bin)
  251.   print("Trashing seeds")
  252.   --print(lang_)
  253.   if compareItemInSlot(seed.name,slot.extra) then
  254.     dropDownFromSlot(slot.extra,64)
  255.   elseif compareItemInSlot(seed.name,slot.sticks2) then
  256.     dropDownFromSlot(slot.sticks2,64)
  257.   else
  258.     dropDownFromSlot(slot.seed,64)
  259.   end
  260. end
  261. function trashItem(slot)
  262.   move(pos.bin)
  263.   print("Trashing item")
  264.   dropDownFromSlot(slot,64)
  265. end
  266. function storeYeld()
  267.   move(pos.chest)
  268.   print("Storing yeld")
  269.   if not compareItemInSlot(seed.name,slot.extra) then
  270.     dropDownFromSlot(slot.extra,64)
  271.   elseif not compareItemInSlot("AgriCraft:cropsItem",slot.sticks2) then
  272.     dropDownFromSlot(slot.sticks2,64)
  273.   else
  274.     dropDownFromSlot(slot.extra,64)
  275.   end
  276.     dropDownFromSlot(6,64)
  277.     dropDownFromSlot(7,64)
  278.     dropDownFromSlot(8,64)
  279. end
  280.  
  281. ----------------------------------------------------
  282. --------------INVENTORY CONTROLLLER-----------------
  283. ----------------------------------------------------
  284. function select(slot)
  285.   lastSl = t.getSelectedSlot()
  286.   t.select(slot)
  287.   return lastSl
  288. end
  289. function count(slot)
  290.     return t.getItemCount(slot)
  291. end
  292. function checkCount(slot,number)
  293.   if count(slot) >= number then
  294.     return true
  295.   end
  296.   return false
  297. end
  298. function dropDown(number)
  299.     return t.dropDown(number)
  300. end
  301. function dropDownFromSlot(slot,number)
  302.     lastSelected = select(slot)
  303.     if dropDown(number) then
  304.         select(lastSelected)
  305.         return true
  306.     end
  307.     select(lastSelected)
  308.     return false
  309. end
  310. function suckDown(number)
  311.     return t.suckDown(number)
  312. end
  313. function suckDownInSlot(slot,number)
  314.     lastSelected = select(slot)
  315.     if suckDown(number) then
  316.         t.select(lastSelected)
  317.         return true
  318.     end
  319.     select(lastSelected)
  320.     return false
  321. end
  322. -----------------------------------------------
  323. function transferItem(fromSlot,toSlot)
  324.   lastSl = select(fromSlot)
  325.   t.transferTo(toSlot,64)
  326.   select(lastSl)
  327. end
  328. function compareItemInSlot(item,slot)
  329.   local itemInfo = t.getItemDetail(slot)
  330.   if itemInfo ~= nil then
  331.     --print("Comparing: "..item.." AND: "..itemInfo.name)
  332.     if item == itemInfo.name then
  333.       return true
  334.     end
  335.   end
  336.   return false
  337. end
  338. function matchItemInSlot(item,slot)
  339.   itemInfo = turtle.getItemDetail(slot)
  340.   if itemInfo.name == item then
  341.    return true
  342.   end
  343.   return false
  344. end
  345. ------------------------------------------------------
  346. ------------------TURTLE CHECKS-----------------------
  347. ------------------------------------------------------
  348.  
  349. function fuel()
  350.    lastSelected =   t.getSelectedSlot()
  351.    if t.getFuelLevel() < 70 then
  352.         lastSelected = select(slot.fuel)
  353.        if t.refuel(slot.fuel) then
  354.         select(lastSelected)
  355.         return true
  356.        else
  357.         noFuel()
  358.         t.select(lastSelected)
  359.        end
  360.    end
  361.     return true
  362. end
  363.  
  364. function tidySticks()
  365.   if compareItemInSlot("AgriCraft:cropsItem",slot.sticks1) then
  366.     return true
  367.   else
  368.     if compareItemInSlot("AgriCraft:cropsItem",slot.sticks2) then
  369.       transferItem(slot.sticks2,slot.sticks1)
  370.       return true
  371.     end
  372.   end
  373.   return false
  374. end
  375. function sticks()
  376.   if tidySticks() then
  377.     return true
  378.   end
  379.   noSticks()
  380.   return true
  381. end
  382. function seeds()
  383.   local seedCount = count(slot.seed)
  384.   if seedCount > 2 then
  385.     tooManySeeds()
  386.   elseif seedCount == 2 or seedCount == 1 then
  387.     if analyze() then
  388.       local lastSelected = select(slot.seed)
  389.       local seedInfo = t.getItemDetail()
  390.       select(lastSelected)
  391.       seed.name = seedInfo.name
  392.       print("Seed set to: "..seed.name)
  393.       return seedCount
  394.     end
  395.   end
  396.   return 0
  397. end
  398. function rake()
  399.   if compareItemInSlot("AgriCraft:handRake",slot.rake) then
  400.     return true
  401.   end
  402.   noRake()
  403.   return true
  404. end
  405. ------------------------------------------------
  406. -------------------MESSAGES---------------------
  407. ------------------------------------------------
  408. function noFuel()
  409.     while not t.refuel(slot.fuel) do
  410.       resetScreen()
  411.       print (lang_noFuel)
  412.       sleep(1)
  413.     end
  414. end
  415. function noSticks()
  416.     while not tidySticks() do
  417.       resetScreen()
  418.       print (lang_noSticks)
  419.       sleep(1)
  420.     end
  421. end
  422. function noRake()
  423.   while not compareItemInSlot("AgriCraft:handRake",slot.rake) do
  424.     resetScreen()
  425.     print(lang_noRake)
  426.     sleep(1)
  427.   end
  428.   return true
  429. end
  430. function noSeeds()
  431.   while not checkCount(slot.seed,1) do
  432.     resetScreen()
  433.     print(lang_noSeed)
  434.     sleep(1)
  435.   end
  436.   if seeds() >= 1 then
  437.     resetScreen()
  438.     return count(slot.seed)
  439.   else
  440.     noSeeds()
  441.   end
  442. end
  443. function tooManySeeds()
  444.     while checkCount(slot.seed,3) do
  445.       resetScreen()
  446.       print (lang_manySeeds)
  447.       sleep(1)
  448.     end
  449.     return true
  450. end
  451.  
  452. function seedMaxedOut()
  453.   print("Seed is 10/10/10. Clearing crop sticks")
  454.   breakStick(pos.seed[0])
  455.   if compareItemInSlot(seed.name,slot.sticks2) then
  456.     trashItem(slot.sticks2)
  457.   else
  458.     trashItem(slot.extra)
  459.   end
  460.   storeYeld()
  461.   breakStick(pos.seed[1])
  462.   if compareItemInSlot(seed.name,slot.sticks2) then
  463.     trashItem(slot.sticks2)
  464.   else
  465.     trashItem(slot.extra)
  466.   end
  467.   storeYeld()
  468.   placeSeeds(pos.seed[1],pos.actv[1])
  469.   move(pos.anlzer)
  470.   error(lang_maxedOut)
  471. end
  472.  
  473. ------------------------------------------------------
  474. -----------------TURTLE MOVEMENT----------------------
  475. ------------------------------------------------------
  476. function forward(n)
  477.   for i = 1,n do
  478.    fuel()
  479.    t.forward()
  480.   end
  481. end
  482. function back(n)
  483.   for i = 1,n do
  484.    fuel()
  485.    t.back()
  486.   end
  487. end
  488. function left(n)
  489.    fuel()
  490.    t.turnLeft()
  491.   for i = 1,n do
  492.    fuel()
  493.    t.forward()
  494.   end
  495.    t.turnRight()
  496. end
  497. function right(n)
  498.    fuel()
  499.    t.turnRight()
  500.   for i = 1,n do
  501.    fuel()
  502.    t.forward()
  503.   end
  504.    t.turnLeft()
  505. end
  506.  
  507. function move(x,z)
  508.   if z == nil then
  509.     tbl = x
  510.     --print("tbl "..tbl.x.." "..tbl.y.." "..tbl.z.." " )
  511.     --print("tbl pos "..pos.x.." "..pos.y.." "..pos.z.." " )
  512.     x = tbl.x - pos.x
  513.     z = tbl.z - pos.z
  514.     --print("tbl AFTER "..tbl.x.." "..tbl.y.." "..tbl.z.." " )
  515.   else
  516.     --print("not tbl "..x.." "..y.." "..z.." " )
  517.     --print("not tbl pos "..pos.x.." "..pos.y.." "..pos.z.." " )
  518.     x = x - pos.x
  519.     z = z - pos.z
  520.     --print("not tbl AFTER "..x.." "..y.." "..z.." " )
  521.     --print("------------------------------------" )
  522.   end
  523.   if x > 0 then
  524.     right(x)
  525.     sleep(0.1)
  526.   end
  527.   if x < 0 then
  528.     left(math.abs(x))
  529.     sleep(0.1)
  530.   end
  531.   if z > 0 then
  532.     back(z)
  533.     sleep(0.1)
  534.   end
  535.   if z < 0 then
  536.     forward(math.abs(z))
  537.     sleep(0.1)
  538.   end
  539.   pos.x = pos.x + x
  540.   pos.z = pos.z + z
  541.   --savePos()
  542. end
  543. ------------------------------------------------------
  544. ------------------------------------------------------
  545. ------------------------------------------------------
  546.  
  547.  
  548. resetScreen()
  549. seedRepl = 0
  550. select(1)
  551. function main()
  552.   resetScreen()
  553.   if fuel() and sticks() then
  554.     numOfSeeds = seeds()
  555.     --seedName = getSeedName()
  556.     --print("Seed set to: "..seedName)
  557.     if numOfSeeds == 0 then
  558.       numOfSeeds = noSeeds()
  559.     end
  560.     if numOfSeeds == 1 then
  561.       placeSeeds(pos.seed[0],pos.actv[0])
  562.       placeSticksInActv(pos.actv[2],true)
  563.       waitForSeedToGrow()
  564.       analyze(true)
  565.       placeSeeds(pos.seed[1],pos.actv[1])
  566.     end
  567.     if numOfSeeds == 2 then
  568.       placeSeeds(pos.seed[0],pos.actv[0])
  569.       placeSeeds(pos.seed[1],pos.actv[1])
  570.     end
  571.     while not isMaxedOut() do
  572.       --removeWeedsLoop(posTable)
  573.       placeSticksInActv(pos.actv[2],true)
  574.       waitForSeedToGrow()
  575.       if analyze() then
  576.         if seedRepl > 1 then
  577.           seedRepl = 0
  578.         end
  579.         breakStick(pos.seed[seedRepl])
  580.         placeSeeds(pos.seed[seedRepl],pos.actv[seedRepl])
  581.         trashSeed()
  582.         storeYeld()
  583.         seedRepl = seedRepl + 1
  584.       else
  585.         trashSeed()
  586.       end
  587.     end
  588.   end
  589. end --MAIN END
  590. --replaceSticks(pos.seed[2])
  591. --trashSeed()
  592. --storeYeld()
  593. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement