Miner8149

Agricraft Opencomputers Robot Side 10/10/10

Dec 8th, 2021 (edited)
643
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Miner8149
  2. --12/7/2021
  3.  
  4. -- Visit my github for a more detailed set of instructions https://github.com/Miner8149/Agricraft-10-10-10-seed-breeder
  5. -- https://pastebin.com/uSjibHSi
  6. -- Copyright 2020 Miner8149
  7. --
  8. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  9. -- of this software and associated documentation files (the "Software"), to deal
  10. -- in the Software without restriction, including without limitation the rights
  11. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. -- copies of the Software, and to permit persons to whom the Software is
  13. -- furnished to do so, subject to the following conditions:
  14. --
  15. -- The above copyright notice and this permission notice shall be included in all
  16. -- copies or substantial portions of the Software.
  17. --
  18. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. -- SOFTWARE.
  25. --
  26. -- I am not responible for exploding robots, or missing limbs. Do note that if the
  27. -- robot gains sentience, that is would be in your best interest to run
  28.  
  29. --This is for the ROBOT
  30. local com = require("component")
  31. local ic = com.inventory_controller
  32. local m = com.modem
  33. local r = require("robot")
  34. local s = require("sides")
  35. local event = require("event")
  36.  
  37.  
  38. local numCropsticksPlaced = nil
  39. local childSeedPresent = false
  40. local actionSlot = 0
  41. local numParentSeedsObtained = 0
  42. local mode = "clipping"  --Availiable modes are clipping and replacing
  43.  
  44. -- clipping mode will take a clipping of each new crop to replace both parents at once,
  45. -- replacing will replace the lowest parent
  46. -- it will swap modes automatically to accomidate the presence of clippers.
  47. -- replacing mode: avg time for 1.1.1 seeds to 10.10.10 seeds is 67 mins
  48. -- clipping mode: avg time for 1.1.1 seeds to 10.10.10 seeds is not calulated yet
  49.  
  50. local pos = 0 --  -1 is left, 0 is center, 1 is right
  51. local rot = 0 --  0-front, 1-right, 2-back, 3-left
  52.  
  53. --Slot Config
  54. local newParentSeedSlot = 10
  55. local brokenParentSeedSlot = 6
  56. local childSeedSlot = 2
  57. local clippingSeedSlot = 14
  58. local cropStickSlot = 1
  59. local wateringCanSlot = 16
  60. local clippersSlot = 15
  61.  
  62.  
  63. function goTo(posL, rotL)
  64.   --print("Going to Position: ", posL, ", Rotation: ", rotL)
  65.   local rotC = rotL - rot
  66.   local posC = posL - pos
  67.   local forwardMoved = false
  68.   if (posC == 0) then
  69.     if ((rotC == 3) or (rotC == -1)) then
  70.       r.turnLeft()
  71.       rot = (rot+3)%4
  72.     elseif ((rotC == 2) or (rotC == -2)) then
  73.       r.turnAround()
  74.       rot = (rot+2)%4
  75.     elseif ((rotC == -3) or (rotC == 1)) then
  76.       r.turnRight()
  77.       rot = (rot+1)%4
  78.     end
  79.   elseif (posC < 0) then
  80.     goTo(pos, 3)
  81.     if (posC == -1) then
  82.       moveForward()
  83.       pos = pos - 1
  84.     elseif (posC == -2) then
  85.       moveForward()
  86.       pos = pos - 1
  87.       moveForward()
  88.       pos = pos - 1
  89.     end
  90.     goTo(posL, rotL)
  91.   elseif (posC > 0) then
  92.     goTo(pos, 1)
  93.     if (posC == 1) then
  94.       moveForward()
  95.       pos = pos + 1
  96.     elseif (posC == 2) then
  97.       moveForward()
  98.       pos = pos + 1
  99.       moveForward()
  100.       pos = pos + 1
  101.     end
  102.     goTo(posL, rotL)
  103.   end
  104. end
  105.  
  106. function moveForward()
  107.   local hasMovedForward = nil
  108.   while (hasMovedForward == nil) do
  109.     hasMovedForward = r.forward()
  110.     if (hasMovedForward == nil) then
  111.       print("I've bumped something")
  112.       os.sleep(1)
  113.     end
  114.   end
  115. end
  116.  
  117. function getCropSticks() --keep 48 on the robot max, 8 min
  118.   --print("Fetching Cropsticks")
  119.   local cropstickcount = ic.getStackInInternalSlot(cropStickSlot)
  120.   if ((cropstickcount ~= nil) and (string.match(cropstickcount["name"], "crop") ~= "crop") and (string.match(cropstickcount["name"], "stick") ~= "stick"))  then
  121.     local cropStickSlotFree = false
  122.     for index=1, r.inventorySize() do
  123.       r.select(index)
  124.       cropstickcount = ic.getStackInInternalSlot(index)
  125.       if ((cropstickcount == nil) and ( (index ~= newParentSeedSlot) and (index ~= brokenParentSeedSlot) and (index ~= childSeedSlot) and (index ~= clippingSeedSlot) and (index ~= cropStickSlot) and (index ~= wateringCanSlot) and (index ~= clippersSlot) ) ) then
  126.         r.select(cropStickSlot)
  127.         r.transferTo(index)
  128.         cropStickSlotFree = true
  129.         index = r.inventorySize()
  130.       end
  131.     end
  132.     if not cropStickSlotFree then
  133.       storeOther(cropStickSlot)
  134.     end
  135.   end
  136.   local getNumber = 0
  137.   cropstickcount = ic.getStackInInternalSlot(cropStickSlot)
  138.   if(cropstickcount == nil) then
  139.     getNumber = 48
  140.   elseif (cropstickcount["size"] < 16) then
  141.     getNumber = 48 - cropstickcount["size"]
  142.   else
  143.     --print("Nevermind")
  144.     return true
  145.   end
  146.   goTo(0, 0)
  147.   local chestSlot = nil
  148.   r.select(cropStickSlot)
  149.   for index=1,ic.getInventorySize(s.top) do
  150.     chestSlot = ic.getStackInSlot(s.top,index)
  151.     if ((chestSlot ~= nil) and ((string.match(chestSlot["name"], "crop") == "crop") and (string.match(chestSlot["name"], "stick") == "stick"))) then
  152.       if (chestSlot["size"] < getNumber) then
  153.         ic.suckFromSlot(s.top,index,chestSlot["size"])
  154.         getNumber = getNumber - chestSlot["size"]
  155.         print("Getting ", chestSlot["size"])
  156.       else
  157.         ic.suckFromSlot(s.top,index,getNumber)
  158.         print("Getting ", getNumber)
  159.         return true
  160.       end
  161.     end
  162.   end
  163.   return false
  164. end
  165.  
  166. function destroy(slot) --void item in "slot" to trashcan
  167.   print("Destroying Slot ", slot)
  168.   goTo(-1, 3)
  169.   r.select(slot)
  170.   r.drop()
  171.   --r.select(1)
  172. end
  173.  
  174. function storeFinished(slot)
  175.   print("Storing Finished Seed")
  176.   goTo(-1, 2)
  177.   r.select(slot)
  178.   r.drop()
  179. end
  180.  
  181. function storeOther(slot)
  182.   print("Storing Other: ", slot)
  183.   goTo(0, 2)
  184.   r.select(slot)
  185.   r.drop()
  186.   --r.select(1)
  187. end
  188.  
  189. function placeAnalyzeSeed(slot)
  190.   print("Giving seed to Analyzer")
  191.   goTo(1, 1)
  192.   r.select(slot)
  193.   r.drop()
  194.   os.sleep(1)
  195. end
  196.  
  197. function getAnalyzedSeed(slot)
  198.   print("Retrieving Seed from Analyzer")
  199.   goTo(1, 1)
  200.   r.select(slot)
  201.   os.sleep(1)
  202.   r.suck()
  203. end
  204.  
  205. function placeSingleCropstick(posL, index)
  206.   print("Placing single cropsticks at ", posL)
  207.   getCropSticks()
  208.   goTo(posL, 0)
  209.   r.select(cropStickSlot)
  210.   r.place(s.front, false)
  211.   local confirmSticks = 1 --requestData(2, posL)
  212.   if (confirmSticks ~= 1) or (confirmSticks == nil) then
  213.     if (index >= 3) then
  214.       print("Unable to place Single Cropstick")
  215.       return false
  216.     end
  217.     print("trying again in 5")
  218.     os.sleep(5)
  219.     placeSingleCropstick(posL, index+1)
  220.   end
  221.   return true
  222. end
  223.  
  224. function placeDoubleCropstick(posL, index)
  225.   print("Placing double cropsticks at ", posL)
  226.   getCropSticks()
  227.   goTo(posL, 0)
  228.   r.select(cropStickSlot)
  229.   r.place(s.front, true)
  230.   local confirmSticks = 2 --requestData(2, posL)
  231.   if (confirmSticks ~= 2) or (confirmSticks == nil) then
  232.     if (index >= 3) then
  233.       print("Unable to place Double Cropstick")
  234.       return false
  235.     end
  236.     print("trying again in 5")
  237.     os.sleep(5)
  238.     placeDoubleCropstick(posL, index+1)
  239.   end
  240.   return true
  241. end
  242.  
  243. function waterCrops() --watering can in slot 16
  244.   --This is a work in progress, but the watering can isnt working... yes, I changed the config
  245.   print("Watering...")
  246.   goTo(0, 0)
  247.   r.select(wateringCanSlot)
  248.   --ic.equip()
  249.   --r.use(s.all, true, 10)
  250.   --ic.equip()
  251.   os.sleep(10)
  252. --  r.useDown(s.all, false, 10)
  253. end
  254.  
  255. function breakChildSeed()
  256.   print("Breaking Child Seed")
  257.   local slotForChildSeed = ic.getStackInInternalSlot(childSeedSlot)
  258.   while (slotForChildSeed ~= nil) do
  259.     storeOther(childSeedSlot)
  260.   end
  261.   r.select(childSeedSlot)
  262.   goTo(0, 0)
  263.   r.swing()
  264. end
  265.  
  266. function breakParentSeed(location) -- -1 or 1
  267.   print("Breaking Parent at ", location)
  268.   local slotForBrokenParentSeed = ic.getStackInInternalSlot(brokenParentSeedSlot)
  269.   while (slotForBrokenParentSeed ~= nil) do
  270.     storeOther(brokenParentSeedSlot)
  271.   end
  272.   r.select(brokenParentSeedSlot)
  273.   goTo(location, 0)
  274.   r.swing()
  275. end
  276.  
  277. function plantSeed(slot, location, index)
  278.   print("Planting Seed at ", location)
  279.   r.select(slot)
  280.   goTo(location, 0)
  281.   ic.equip()  
  282.   r.use()
  283.   ic.equip()
  284.   local confirmSeeds = true --requestData(1, location)
  285.   if (confirmSeeds ~= true) then
  286.     if (index >= 3) then
  287.       print("Unable to place Seed")
  288.       return false
  289.     end
  290.     print("trying again in 5")
  291.     os.sleep(5)
  292.     plantSeed(slot, location, index+1)
  293.   end
  294.   return true
  295. end
  296.  
  297. function getNewSeeds()  --returns true on sucess, false otherwise
  298.   print("Getting new Seeds")
  299.   local checkClipperSlot = ic.getStackInInternalSlot(clippersSlot)
  300.   if ((checkClipperSlot ~= nil) and (string.match(checkClipperSlot["name"], "clipper") == "clipper")) then
  301.     mode = "clipping"
  302.   else
  303.     mode = "replacing"
  304.   end
  305.   goTo(1, 2)
  306.   local chestSlot = nil
  307.   local numSeeds = 0
  308.   r.select(newParentSeedSlot)
  309.   for index=1,ic.getInventorySize(s.front) do
  310.     chestSlot = ic.getStackInSlot(s.front,index)
  311.     if ((chestSlot ~= nil) and (((string.match(chestSlot["name"], "Seed") == "Seed") or (string.match(chestSlot["name"], "seed") == "seed")) or (string.match(chestSlot["name"], "clipping") == "clipping")))then
  312.       numSeeds = chestSlot["size"]
  313.       if (numSeeds == 2) then
  314.         ic.suckFromSlot(s.front,index,numSeeds)
  315.         print("Found 2 Seeds/Clippings")
  316.         numParentSeedsObtained = numSeeds
  317.         initParents(newParentSeedSlot)
  318.         return true
  319.       elseif ((numSeeds == 1) and (mode == "clipping")) then
  320.         ic.suckFromSlot(s.front,index,numSeeds)
  321.         print("Found 1 Seeds/Clippings")
  322.         numParentSeedsObtained = numSeeds
  323.         initParents(newParentSeedSlot)
  324.         return true
  325.       else
  326.         print("Not 2 or 1 Seeds/Clippings!")
  327.         --return false
  328.       end
  329.     end
  330.   end
  331.   print("No Seeds Found")
  332.   return false
  333. end
  334.  
  335. function initParents(slot)
  336.   print("initializing Parents from slot ", slot)
  337.   goTo(1, 1)
  338.   r.select(slot)
  339.   r.drop()
  340.   requestData(0, 0)
  341.   r.suck()
  342. end
  343.  
  344. function clearInv()
  345.   print("Clearing Inventory")
  346.   local invSlot = nil
  347.   for index=1,r.inventorySize() do
  348.     r.select(index)
  349.     invSlot = ic.getStackInInternalSlot(index)
  350.     if (invSlot == nil) then
  351.       --do nothing
  352.     elseif ((string.match(invSlot["name"], "crop") == "crop") and (string.match(invSlot["name"], "stick") == "stick"))  then
  353.       if (index ~= cropStickSlot) then
  354.         storeOther(index)
  355.       end
  356.     elseif (string.match(invSlot["name"], "clipper") == "clipper") then
  357.       if (index ~= clippersSlot) then
  358.         local subCheckInv = ic.getStackInInternalSlot(clippersSlot)
  359.         if ((subCheckInv ~= nil) and (string.match(subCheckInv["name"], "clipper") == "clipper")) then
  360.           storeOther(index)
  361.         else
  362.           r.transferTo(clippersSlot)
  363.           index = index - 1
  364.         end
  365.       end
  366.     elseif ((string.match(invSlot["name"], "Seed") == "Seed") or (string.match(invSlot["name"], "seed") == "seed")) then
  367.       destroy(index)
  368.     elseif (string.match(invSlot["name"], "clipping") == "clipping") then
  369.       destroy(index)
  370.     elseif ((string.match(invSlot["name"], "water") == "water") and (string.match(invSlot["name"], "can") == "can")) then
  371.       if (index ~= wateringCanSlot) then
  372.         local subCheckInv = ic.getStackInInternalSlot(wateringCanSlot)
  373.         if ((string.match(invSlot["name"], "water") == "water") and (string.match(invSlot["name"], "can") == "can")) then
  374.           storeOther(index)
  375.         else
  376.           r.transferTo(wateringCanSlot)
  377.           index = index - 1
  378.         end
  379.       end
  380.     else
  381.       storeOther(index)
  382.     end
  383.   end
  384. end
  385.  
  386. function breakAll()
  387.   print("Breaking all")
  388.   goTo(1, 0)
  389.   r.swing()
  390.   goTo(0, 0)
  391.   r.swing()
  392.   goTo(-1, 0)
  393.   r.swing()
  394.   --goTo(-1, 3)
  395.   --for index=2,15 do
  396.   --  r.select(index)
  397.   --  r.drop()
  398.   --end
  399.   --r.select(1)
  400. end
  401.  
  402. function requestData(funct, slot) --can request init (0), isplant(1, slot), is cropstick(2, slot), analyze(3), decide analyze location(4)
  403.   local returnVal = nil
  404.   print("Requesting data: ", funct, ", ", slot)
  405.   if (funct == 0) then  --initialize parent varibales, expect bool return
  406.     m.broadcast(111, 0, 0)
  407.     _, _, _, _, _, returnVal = event.pull("modem_message")
  408.     event.pull(2, "modem_message")
  409.   elseif (funct == 1) then --check for plant at slot, expect bool return
  410.     m.broadcast(111, 1, slot)
  411.     _, _, _, _, _, returnVal = event.pull("modem_message")
  412.     event.pull(2, "modem_message")
  413.   elseif (funct == 2) then --check for cropsticks, expect number of cropsticks 0, 1, 2
  414.     m.broadcast(111, 2, slot)
  415.     _, _, _, _, _, returnVal = event.pull("modem_message")
  416.     event.pull(2, "modem_message")
  417.   elseif (funct == 3) then --analyze crop, expect bool return
  418.     m.broadcast(111, 3, 0)
  419.     _, _, _, _, _, returnVal = event.pull("modem_message")
  420.     event.pull(2, "modem_message")
  421.   elseif (funct == 4) then --decide location, expect slot -1, 0, 1
  422.     m.broadcast(111, 4, 0)
  423.     _, _, _, _, _, returnVal = event.pull("modem_message")
  424.     event.pull(2, "modem_message")
  425.   else
  426.     return nil
  427.   end
  428.   print("Recieved: ", returnVal)
  429.   return returnVal
  430. end
  431.  
  432. function obtainClipping(location)
  433.   local itemInClippersSlot = ic.getStackInInternalSlot(clippersSlot)
  434.   local hasClippers = false
  435.   if ((itemInClippersSlot ~= nil) and (string.match(itemInClippersSlot["name"], "clipper") == "clipper")) then
  436.     hasClippers = true
  437.   else    --look for clippers
  438.     r.select(clippersSlot)
  439.     ic.equip()
  440.     itemInClippersSlot = ic.getStackInInternalSlot(clippersSlot)
  441.     if ((itemInClippersSlot ~= nil) and (string.match(itemInClippersSlot["name"], "clipper") == "clipper")) then
  442.       hasClippers = true
  443.     else
  444.       for index=1,r.inventorySize() do
  445.         r.select(index)
  446.         itemInClippersSlot = ic.getStackInInternalSlot(index)
  447.         if ((itemInClippersSlot ~= nil) and (string.match(itemInClippersSlot["name"], "clipper") == "clipper")) then
  448.           r.transferTo(clippersSlot)
  449.           hasClippers = true
  450.         end
  451.       end
  452.       if (hasClippers == false) then
  453.         mode = "replacing"
  454.         return nil
  455.       end
  456.     end
  457.   end
  458.   local itemInClippingSlot = ic.getStackInInternalSlot(clippingSeedSlot)
  459.   if (itemInClippingSlot ~= nil) then
  460.     clearInv()
  461.   end
  462.   goTo(location, 0)
  463.   r.select(clippersSlot)
  464.   ic.equip()
  465.   r.select(clippingSeedSlot)
  466.   local gotClipping = r.use() --true or false
  467.   r.select(clippersSlot)
  468.   ic.equip()
  469.   return gotClipping
  470. end
  471.  
  472. --------------------------------------
  473. function setup()
  474.   m.open(112)
  475. end
  476.  
  477. setup()
  478. local loopOneControl = true
  479. while true do
  480.   loopOneControl = true
  481.   while not (getCropSticks()) do
  482.     os.sleep(10)
  483.   end
  484.   clearInv()
  485.   breakAll()
  486.   clearInv()
  487.   while not (getNewSeeds()) do
  488.     goTo(0, 0)
  489.     os.sleep(10)
  490.   end
  491.   placeSingleCropstick(1, 0)
  492.   placeDoubleCropstick(0, 0)
  493.   placeSingleCropstick(-1, 0)
  494.   if (numParentSeedsObtained == 2) then
  495.     plantSeed(newParentSeedSlot, -1, 0)
  496.     plantSeed(newParentSeedSlot, 1, 0)
  497.   elseif (numParentSeedsObtained == 1) then
  498.     plantSeed(newParentSeedSlot, 1, 0)
  499.     while true do
  500.       if (obtainClipping(1) == nil) then
  501.         loopOneControl = false
  502.       elseif (obtainClipping(1) == true) then
  503.         placeAnalyzeSeed(clippingSeedSlot)
  504.         getAnalyzedSeed(clippingSeedSlot)
  505.         plantSeed(clippingSeedSlot, -1, 0)
  506.         break
  507.       end
  508.       os.sleep(1) --wait for plant to grow more
  509.     end
  510.   end
  511.   while loopOneControl do
  512.     while not (childSeedPresent) do
  513.       waterCrops()
  514.       childSeedPresent = requestData(1, 0)
  515.       --check for clippers
  516.       local invClipperSlot = ic.getStackInInternalSlot(clippersSlot)
  517.       if ((invClipperSlot ~= nil) and (string.match(invClipperSlot["name"], "clipper") == "clipper")) then
  518.         mode = "clipping"
  519.       end
  520.     end
  521.     breakChildSeed()
  522.     childSeedPresent = false
  523.     placeDoubleCropstick(0, 0)
  524.     placeAnalyzeSeed(childSeedSlot)
  525.     actionSlot = requestData(4, 0)
  526.     getAnalyzedSeed(childSeedSlot)
  527.     if (actionSlot == 2) then --means that seed is 10 10 10
  528.       storeFinished(childSeedSlot)
  529.       breakAll()
  530.       clearInv()
  531.       break
  532.     elseif (actionSlot == 0) then --means the seed is equal to or less than parents (trash)
  533.       destroy(childSeedSlot)
  534.       clearInv()
  535.     elseif (actionSlot == nil) then  --the seed wasnt analyzed
  536.       print("the seed wasnt analyzed")
  537.       storeOther(childSeedSlot)
  538.       clearInv()
  539.     else                            -- 1 or -1 means it is better than the specified parent
  540.       breakParentSeed(actionSlot)
  541.       placeSingleCropstick(actionSlot, 0)
  542.       plantSeed(childSeedSlot, actionSlot, 0)
  543.       clearInv()
  544.       if (mode == "clipping") then
  545.         local invSlot = obtainClipping(actionSlot)
  546.         while (invSlot == false) do
  547.           invSlot = obtainClipping(actionSlot)
  548.         end
  549.       end
  550.       if (mode == "clipping") then
  551.         placeAnalyzeSeed(clippingSeedSlot)
  552.         actionSlot = requestData(4, 0)
  553.         getAnalyzedSeed(clippingSeedSlot)
  554.         breakParentSeed(actionSlot)
  555.         placeSingleCropstick(actionSlot, 0)
  556.         plantSeed(clippingSeedSlot, actionSlot, 0)
  557.         clearInv()
  558.       end
  559.     end
  560.   end
  561. end
RAW Paste Data